| 1 |
/* |
| 2 |
* psql - the PostgreSQL interactive terminal |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2025, PostgreSQL Global Development Group |
| 5 |
* |
| 6 |
* src/bin/psql/prompt.c |
| 7 |
*/ |
| 8 |
#include "postgres_fe.h" |
| 9 |
|
| 10 |
#ifdef WIN32 |
| 11 |
#include <io.h> |
| 12 |
#include <win32.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "common.h" |
| 16 |
#include "common/string.h" |
| 17 |
#include "input.h" |
| 18 |
#include "libpq/pqcomm.h" |
| 19 |
#include "prompt.h" |
| 20 |
#include "settings.h" |
| 21 |
|
| 22 |
/*-------------------------- |
| 23 |
* get_prompt |
| 24 |
* |
| 25 |
* Returns a statically allocated prompt made by interpolating certain |
| 26 |
* tcsh style escape sequences into pset.vars "PROMPT1|2|3". |
| 27 |
* (might not be completely multibyte safe) |
| 28 |
* |
| 29 |
* Defined interpolations are: |
| 30 |
* %M - database server "hostname.domainname", "[local]" for AF_UNIX |
| 31 |
* sockets, "[local:/dir/name]" if not default |
| 32 |
* %m - like %M, but hostname only (before first dot), or always "[local]" |
| 33 |
* %p - backend pid |
| 34 |
* %P - pipeline status: on, off or abort |
| 35 |
* %> - database server port number |
| 36 |
* %n - database user name |
| 37 |
* %s - service |
| 38 |
* %/ - current database |
| 39 |
* %~ - like %/ but "~" when database name equals user name |
| 40 |
* %w - whitespace of the same width as the most recent output of PROMPT1 |
| 41 |
* %# - "#" if superuser, ">" otherwise |
| 42 |
* %R - in prompt1 normally =, or ^ if single line mode, |
| 43 |
* or a ! if session is not connected to a database; |
| 44 |
* in prompt2 -, *, ', or "; |
| 45 |
* in prompt3 nothing |
| 46 |
* %x - transaction status: empty, *, !, ? (unknown or no connection) |
| 47 |
* %l - The line number inside the current statement, starting from 1. |
| 48 |
* %? - the error code of the last query (not yet implemented) |
| 49 |
* %% - a percent sign |
| 50 |
* |
| 51 |
* %[0-9] - the character with the given decimal code |
| 52 |
* %0[0-7] - the character with the given octal code |
| 53 |
* %0x[0-9A-Fa-f] - the character with the given hexadecimal code |
| 54 |
* |
| 55 |
* %`command` - The result of executing command in /bin/sh with trailing |
| 56 |
* newline stripped. |
| 57 |
* %:name: - The value of the psql variable 'name' |
| 58 |
* (those will not be rescanned for more escape sequences!) |
| 59 |
* |
| 60 |
* %[ ... %] - tell readline that the contained text is invisible |
| 61 |
* |
| 62 |
* If the application-wide prompts become NULL somehow, the returned string |
| 63 |
* will be empty (not NULL!). |
| 64 |
*-------------------------- |
| 65 |
*/ |
| 66 |
|
| 67 |
char * |
| 68 |
get_prompt(promptStatus_t status, ConditionalStack cstack) |
| 69 |
{ |
| 70 |
#define MAX_PROMPT_SIZE 256 |
| 71 |
static char destination[MAX_PROMPT_SIZE + 1]; |
| 72 |
char buf[MAX_PROMPT_SIZE + 1]; |
| 73 |
bool esc = false; |
| 74 |
const char *p; |
| 75 |
const char *prompt_string = "? "; |
| 76 |
static size_t last_prompt1_width = 0; |
| 77 |
|
| 78 |
switch (status) |
| 79 |
{ |
| 80 |
case PROMPT_READY: |
| 81 |
prompt_string = pset.prompt1; |
| 82 |
break; |
| 83 |
|
| 84 |
case PROMPT_CONTINUE: |
| 85 |
case PROMPT_SINGLEQUOTE: |
| 86 |
case PROMPT_DOUBLEQUOTE: |
| 87 |
case PROMPT_DOLLARQUOTE: |
| 88 |
case PROMPT_COMMENT: |
| 89 |
case PROMPT_PAREN: |
| 90 |
prompt_string = pset.prompt2; |
| 91 |
break; |
| 92 |
|
| 93 |
case PROMPT_COPY: |
| 94 |
prompt_string = pset.prompt3; |
| 95 |
break; |
| 96 |
} |
| 97 |
|
| 98 |
destination[0] = '\0'; |
| 99 |
|
| 100 |
for (p = prompt_string; |
| 101 |
*p && strlen(destination) < sizeof(destination) - 1; |
| 102 |
p++) |
| 103 |
{ |
| 104 |
memset(buf, 0, sizeof(buf)); |
| 105 |
if (esc) |
| 106 |
{ |
| 107 |
switch (*p) |
| 108 |
{ |
| 109 |
/* Current database */ |
| 110 |
case '/': |
| 111 |
if (pset.db) |
| 112 |
strlcpy(buf, PQdb(pset.db), sizeof(buf)); |
| 113 |
break; |
| 114 |
case '~': |
| 115 |
if (pset.db) |
| 116 |
{ |
| 117 |
const char *var; |
| 118 |
|
| 119 |
if (strcmp(PQdb(pset.db), PQuser(pset.db)) == 0 || |
| 120 |
((var = getenv("PGDATABASE")) && strcmp(var, PQdb(pset.db)) == 0)) |
| 121 |
strlcpy(buf, "~", sizeof(buf)); |
| 122 |
else |
| 123 |
strlcpy(buf, PQdb(pset.db), sizeof(buf)); |
| 124 |
} |
| 125 |
break; |
| 126 |
|
| 127 |
/* Whitespace of the same width as the last PROMPT1 */ |
| 128 |
case 'w': |
| 129 |
if (pset.db) |
| 130 |
memset(buf, ' ', |
| 131 |
Min(last_prompt1_width, sizeof(buf) - 1)); |
| 132 |
break; |
| 133 |
|
| 134 |
/* DB server hostname (long/short) */ |
| 135 |
case 'M': |
| 136 |
case 'm': |
| 137 |
if (pset.db) |
| 138 |
{ |
| 139 |
const char *host = PQhost(pset.db); |
| 140 |
|
| 141 |
/* INET socket */ |
| 142 |
if (host && host[0] && !is_unixsock_path(host)) |
| 143 |
{ |
| 144 |
strlcpy(buf, host, sizeof(buf)); |
| 145 |
if (*p == 'm') |
| 146 |
buf[strcspn(buf, ".")] = '\0'; |
| 147 |
} |
| 148 |
/* UNIX socket */ |
| 149 |
else |
| 150 |
{ |
| 151 |
if (!host |
| 152 |
|| strcmp(host, DEFAULT_PGSOCKET_DIR) == 0 |
| 153 |
|| *p == 'm') |
| 154 |
strlcpy(buf, "[local]", sizeof(buf)); |
| 155 |
else |
| 156 |
snprintf(buf, sizeof(buf), "[local:%s]", host); |
| 157 |
} |
| 158 |
} |
| 159 |
break; |
| 160 |
/* DB server port number */ |
| 161 |
case '>': |
| 162 |
if (pset.db && PQport(pset.db)) |
| 163 |
strlcpy(buf, PQport(pset.db), sizeof(buf)); |
| 164 |
break; |
| 165 |
/* DB server user name */ |
| 166 |
case 'n': |
| 167 |
if (pset.db) |
| 168 |
strlcpy(buf, session_username(), sizeof(buf)); |
| 169 |
break; |
| 170 |
/* service name */ |
| 171 |
case 's': |
| 172 |
{ |
| 173 |
const char *service_name = GetVariable(pset.vars, "SERVICE"); |
| 174 |
|
| 175 |
if (service_name) |
| 176 |
strlcpy(buf, service_name, sizeof(buf)); |
| 177 |
} |
| 178 |
break; |
| 179 |
/* backend pid */ |
| 180 |
case 'p': |
| 181 |
if (pset.db) |
| 182 |
{ |
| 183 |
int pid = PQbackendPID(pset.db); |
| 184 |
|
| 185 |
if (pid) |
| 186 |
snprintf(buf, sizeof(buf), "%d", pid); |
| 187 |
} |
| 188 |
break; |
| 189 |
/* pipeline status */ |
| 190 |
case 'P': |
| 191 |
if (pset.db) |
| 192 |
{ |
| 193 |
PGpipelineStatus status = PQpipelineStatus(pset.db); |
| 194 |
|
| 195 |
if (status == PQ_PIPELINE_ON) |
| 196 |
strlcpy(buf, "on", sizeof(buf)); |
| 197 |
else if (status == PQ_PIPELINE_ABORTED) |
| 198 |
strlcpy(buf, "abort", sizeof(buf)); |
| 199 |
else |
| 200 |
strlcpy(buf, "off", sizeof(buf)); |
| 201 |
} |
| 202 |
break; |
| 203 |
case '0': |
| 204 |
case '1': |
| 205 |
case '2': |
| 206 |
case '3': |
| 207 |
case '4': |
| 208 |
case '5': |
| 209 |
case '6': |
| 210 |
case '7': |
| 211 |
*buf = (char) strtol(p, unconstify(char **, &p), 8); |
| 212 |
--p; |
| 213 |
break; |
| 214 |
case 'R': |
| 215 |
switch (status) |
| 216 |
{ |
| 217 |
case PROMPT_READY: |
| 218 |
if (cstack != NULL && !conditional_active(cstack)) |
| 219 |
buf[0] = '@'; |
| 220 |
else if (!pset.db) |
| 221 |
buf[0] = '!'; |
| 222 |
else if (!pset.singleline) |
| 223 |
buf[0] = '='; |
| 224 |
else |
| 225 |
buf[0] = '^'; |
| 226 |
break; |
| 227 |
case PROMPT_CONTINUE: |
| 228 |
buf[0] = '-'; |
| 229 |
break; |
| 230 |
case PROMPT_SINGLEQUOTE: |
| 231 |
buf[0] = '\''; |
| 232 |
break; |
| 233 |
case PROMPT_DOUBLEQUOTE: |
| 234 |
buf[0] = '"'; |
| 235 |
break; |
| 236 |
case PROMPT_DOLLARQUOTE: |
| 237 |
buf[0] = '$'; |
| 238 |
break; |
| 239 |
case PROMPT_COMMENT: |
| 240 |
buf[0] = '*'; |
| 241 |
break; |
| 242 |
case PROMPT_PAREN: |
| 243 |
buf[0] = '('; |
| 244 |
break; |
| 245 |
default: |
| 246 |
buf[0] = '\0'; |
| 247 |
break; |
| 248 |
} |
| 249 |
break; |
| 250 |
|
| 251 |
case 'x': |
| 252 |
if (!pset.db) |
| 253 |
buf[0] = '?'; |
| 254 |
else |
| 255 |
switch (PQtransactionStatus(pset.db)) |
| 256 |
{ |
| 257 |
case PQTRANS_IDLE: |
| 258 |
buf[0] = '\0'; |
| 259 |
break; |
| 260 |
case PQTRANS_ACTIVE: |
| 261 |
case PQTRANS_INTRANS: |
| 262 |
buf[0] = '*'; |
| 263 |
break; |
| 264 |
case PQTRANS_INERROR: |
| 265 |
buf[0] = '!'; |
| 266 |
break; |
| 267 |
default: |
| 268 |
buf[0] = '?'; |
| 269 |
break; |
| 270 |
} |
| 271 |
break; |
| 272 |
|
| 273 |
case 'l': |
| 274 |
snprintf(buf, sizeof(buf), UINT64_FORMAT, pset.stmt_lineno); |
| 275 |
break; |
| 276 |
|
| 277 |
case '?': |
| 278 |
/* not here yet */ |
| 279 |
break; |
| 280 |
|
| 281 |
case '#': |
| 282 |
if (is_superuser()) |
| 283 |
buf[0] = '#'; |
| 284 |
else |
| 285 |
buf[0] = '>'; |
| 286 |
break; |
| 287 |
|
| 288 |
/* execute command */ |
| 289 |
case '`': |
| 290 |
{ |
| 291 |
int cmdend = strcspn(p + 1, "`"); |
| 292 |
char *file = pnstrdup(p + 1, cmdend); |
| 293 |
FILE *fd; |
| 294 |
|
| 295 |
fflush(NULL); |
| 296 |
fd = popen(file, "r"); |
| 297 |
if (fd) |
| 298 |
{ |
| 299 |
if (fgets(buf, sizeof(buf), fd) == NULL) |
| 300 |
buf[0] = '\0'; |
| 301 |
pclose(fd); |
| 302 |
} |
| 303 |
|
| 304 |
/* strip trailing newline and carriage return */ |
| 305 |
(void) pg_strip_crlf(buf); |
| 306 |
|
| 307 |
free(file); |
| 308 |
p += cmdend + 1; |
| 309 |
break; |
| 310 |
} |
| 311 |
|
| 312 |
/* interpolate variable */ |
| 313 |
case ':': |
| 314 |
{ |
| 315 |
int nameend = strcspn(p + 1, ":"); |
| 316 |
char *name = pnstrdup(p + 1, nameend); |
| 317 |
const char *val; |
| 318 |
|
| 319 |
val = GetVariable(pset.vars, name); |
| 320 |
if (val) |
| 321 |
strlcpy(buf, val, sizeof(buf)); |
| 322 |
free(name); |
| 323 |
p += nameend + 1; |
| 324 |
break; |
| 325 |
} |
| 326 |
|
| 327 |
case '[': |
| 328 |
case ']': |
| 329 |
#if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE) |
| 330 |
|
| 331 |
/* |
| 332 |
* readline >=4.0 undocumented feature: non-printing |
| 333 |
* characters in prompt strings must be marked as such, in |
| 334 |
* order to properly display the line during editing. |
| 335 |
*/ |
| 336 |
buf[0] = (*p == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE; |
| 337 |
buf[1] = '\0'; |
| 338 |
#endif /* USE_READLINE */ |
| 339 |
break; |
| 340 |
|
| 341 |
default: |
| 342 |
buf[0] = *p; |
| 343 |
buf[1] = '\0'; |
| 344 |
break; |
| 345 |
} |
| 346 |
esc = false; |
| 347 |
} |
| 348 |
else if (*p == '%') |
| 349 |
esc = true; |
| 350 |
else |
| 351 |
{ |
| 352 |
buf[0] = *p; |
| 353 |
buf[1] = '\0'; |
| 354 |
esc = false; |
| 355 |
} |
| 356 |
|
| 357 |
if (!esc) |
| 358 |
strlcat(destination, buf, sizeof(destination)); |
| 359 |
} |
| 360 |
|
| 361 |
/* Compute the visible width of PROMPT1, for PROMPT2's %w */ |
| 362 |
if (prompt_string == pset.prompt1) |
| 363 |
{ |
| 364 |
char *p = destination; |
| 365 |
char *end = p + strlen(p); |
| 366 |
bool visible = true; |
| 367 |
|
| 368 |
last_prompt1_width = 0; |
| 369 |
while (*p) |
| 370 |
{ |
| 371 |
#if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE) |
| 372 |
if (*p == RL_PROMPT_START_IGNORE) |
| 373 |
{ |
| 374 |
visible = false; |
| 375 |
++p; |
| 376 |
} |
| 377 |
else if (*p == RL_PROMPT_END_IGNORE) |
| 378 |
{ |
| 379 |
visible = true; |
| 380 |
++p; |
| 381 |
} |
| 382 |
else |
| 383 |
#endif |
| 384 |
{ |
| 385 |
int chlen, |
| 386 |
chwidth; |
| 387 |
|
| 388 |
chlen = PQmblen(p, pset.encoding); |
| 389 |
if (p + chlen > end) |
| 390 |
break; /* Invalid string */ |
| 391 |
|
| 392 |
if (visible) |
| 393 |
{ |
| 394 |
chwidth = PQdsplen(p, pset.encoding); |
| 395 |
|
| 396 |
if (*p == '\n') |
| 397 |
last_prompt1_width = 0; |
| 398 |
else if (chwidth > 0) |
| 399 |
last_prompt1_width += chwidth; |
| 400 |
} |
| 401 |
|
| 402 |
p += chlen; |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
return destination; |
| 408 |
} |