2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2017, PostgreSQL Global Development Group
6 * src/bin/psql/prompt.c
8 #include "postgres_fe.h"
15 #ifdef HAVE_UNIX_SOCKETS
26 /*--------------------------
29 * Returns a statically allocated prompt made by interpolating certain
30 * tcsh style escape sequences into pset.vars "PROMPT1|2|3".
31 * (might not be completely multibyte safe)
33 * Defined interpolations are:
34 * %M - database server "hostname.domainname", "[local]" for AF_UNIX
35 * sockets, "[local:/dir/name]" if not default
36 * %m - like %M, but hostname only (before first dot), or always "[local]"
38 * %> - database server port number
39 * %n - database user name
40 * %/ - current database
41 * %~ - like %/ but "~" when database name equals user name
42 * %# - "#" if superuser, ">" otherwise
43 * %R - in prompt1 normally =, or ^ if single line mode,
44 * or a ! if session is not connected to a database;
45 * in prompt2 -, *, ', or ";
47 * %x - transaction status: empty, *, !, ? (unknown or no connection)
48 * %l - The line number inside the current statement, starting from 1.
49 * %? - the error code of the last query (not yet implemented)
52 * %[0-9] - the character with the given decimal code
53 * %0[0-7] - the character with the given octal code
54 * %0x[0-9A-Fa-f] - the character with the given hexadecimal code
56 * %`command` - The result of executing command in /bin/sh with trailing
58 * %:name: - The value of the psql variable 'name'
59 * (those will not be rescanned for more escape sequences!)
61 * %[ ... %] - tell readline that the contained text is invisible
63 * If the application-wide prompts become NULL somehow, the returned string
64 * will be empty (not NULL!).
65 *--------------------------
69 get_prompt(promptStatus_t status, ConditionalStack cstack)
71 #define MAX_PROMPT_SIZE 256
72 static char destination[MAX_PROMPT_SIZE + 1];
73 char buf[MAX_PROMPT_SIZE + 1];
76 const char *prompt_string = "? ";
81 prompt_string = pset.prompt1;
85 case PROMPT_SINGLEQUOTE:
86 case PROMPT_DOUBLEQUOTE:
87 case PROMPT_DOLLARQUOTE:
90 prompt_string = pset.prompt2;
94 prompt_string = pset.prompt3;
98 destination[0] = '\0';
100 for (p = prompt_string;
101 *p && strlen(destination) < sizeof(destination) - 1;
104 memset(buf, 0, sizeof(buf));
109 /* Current database */
112 strlcpy(buf, PQdb(pset.db), sizeof(buf));
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));
123 strlcpy(buf, PQdb(pset.db), sizeof(buf));
127 /* DB server hostname (long/short) */
132 const char *host = PQhost(pset.db);
135 if (host && host[0] && !is_absolute_path(host))
137 strlcpy(buf, host, sizeof(buf));
139 buf[strcspn(buf, ".")] = '\0';
141 #ifdef HAVE_UNIX_SOCKETS
146 || strcmp(host, DEFAULT_PGSOCKET_DIR) == 0
148 strlcpy(buf, "[local]", sizeof(buf));
150 snprintf(buf, sizeof(buf), "[local:%s]", host);
155 /* DB server port number */
157 if (pset.db && PQport(pset.db))
158 strlcpy(buf, PQport(pset.db), sizeof(buf));
160 /* DB server user name */
163 strlcpy(buf, session_username(), sizeof(buf));
169 int pid = PQbackendPID(pset.db);
172 snprintf(buf, sizeof(buf), "%d", pid);
184 *buf = (char) strtol(p, (char **) &p, 8);
191 if (cstack != NULL && !conditional_active(cstack))
195 else if (!pset.singleline)
200 case PROMPT_CONTINUE:
203 case PROMPT_SINGLEQUOTE:
206 case PROMPT_DOUBLEQUOTE:
209 case PROMPT_DOLLARQUOTE:
228 switch (PQtransactionStatus(pset.db))
234 case PQTRANS_INTRANS:
237 case PQTRANS_INERROR:
247 snprintf(buf, sizeof(buf), UINT64_FORMAT, pset.stmt_lineno);
261 /* execute command */
265 char *file = pg_strdup(p + 1);
268 cmdend = strcspn(file, "`");
270 fd = popen(file, "r");
273 if (fgets(buf, sizeof(buf), fd) == NULL)
277 if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
278 buf[strlen(buf) - 1] = '\0';
284 /* interpolate variable */
291 name = pg_strdup(p + 1);
292 nameend = strcspn(name, ":");
293 name[nameend] = '\0';
294 val = GetVariable(pset.vars, name);
296 strlcpy(buf, val, sizeof(buf));
304 #if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
307 * readline >=4.0 undocumented feature: non-printing
308 * characters in prompt strings must be marked as such, in
309 * order to properly display the line during editing.
311 buf[0] = (*p == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
313 #endif /* USE_READLINE */
334 strlcat(destination, buf, sizeof(destination));