Fix obsolete comment regarding FSM truncation.
[PostgreSQL.git] / src / bin / psql / prompt.c
blobaeec9abd82bde4eda40a3b6a877fdce44433f642
1 /*
2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2008, PostgreSQL Global Development Group
6 * $PostgreSQL$
7 */
8 #include "postgres_fe.h"
10 #ifdef WIN32
11 #include <io.h>
12 #include <win32.h>
13 #endif
15 #ifdef HAVE_UNIX_SOCKETS
16 #include <unistd.h>
17 #include <netdb.h>
18 #endif
20 #include "common.h"
21 #include "input.h"
22 #include "prompt.h"
23 #include "settings.h"
26 /*--------------------------
27 * get_prompt
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]"
37 * %> - database server port number
38 * %n - database user name
39 * %/ - current database
40 * %~ - like %/ but "~" when database name equals user name
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 * %? - the error code of the last query (not yet implemented)
48 * %% - a percent sign
50 * %[0-9] - the character with the given decimal code
51 * %0[0-7] - the character with the given octal code
52 * %0x[0-9A-Fa-f] - the character with the given hexadecimal code
54 * %`command` - The result of executing command in /bin/sh with trailing
55 * newline stripped.
56 * %:name: - The value of the psql variable 'name'
57 * (those will not be rescanned for more escape sequences!)
59 * %[ ... %] - tell readline that the contained text is invisible
61 * If the application-wide prompts become NULL somehow, the returned string
62 * will be empty (not NULL!).
63 *--------------------------
66 char *
67 get_prompt(promptStatus_t status)
69 #define MAX_PROMPT_SIZE 256
70 static char destination[MAX_PROMPT_SIZE + 1];
71 char buf[MAX_PROMPT_SIZE + 1];
72 bool esc = false;
73 const char *p;
74 const char *prompt_string = "? ";
76 switch (status)
78 case PROMPT_READY:
79 prompt_string = pset.prompt1;
80 break;
82 case PROMPT_CONTINUE:
83 case PROMPT_SINGLEQUOTE:
84 case PROMPT_DOUBLEQUOTE:
85 case PROMPT_DOLLARQUOTE:
86 case PROMPT_COMMENT:
87 case PROMPT_PAREN:
88 prompt_string = pset.prompt2;
89 break;
91 case PROMPT_COPY:
92 prompt_string = pset.prompt3;
93 break;
96 destination[0] = '\0';
98 for (p = prompt_string;
99 *p && strlen(destination) < sizeof(destination) - 1;
100 p++)
102 memset(buf, 0, sizeof(buf));
103 if (esc)
105 switch (*p)
107 /* Current database */
108 case '/':
109 if (pset.db)
110 strlcpy(buf, PQdb(pset.db), sizeof(buf));
111 break;
112 case '~':
113 if (pset.db)
115 const char *var;
117 if (strcmp(PQdb(pset.db), PQuser(pset.db)) == 0 ||
118 ((var = getenv("PGDATABASE")) && strcmp(var, PQdb(pset.db)) == 0))
119 strlcpy(buf, "~", sizeof(buf));
120 else
121 strlcpy(buf, PQdb(pset.db), sizeof(buf));
123 break;
125 /* DB server hostname (long/short) */
126 case 'M':
127 case 'm':
128 if (pset.db)
130 const char *host = PQhost(pset.db);
132 /* INET socket */
133 if (host && host[0] && !is_absolute_path(host))
135 strlcpy(buf, host, sizeof(buf));
136 if (*p == 'm')
137 buf[strcspn(buf, ".")] = '\0';
139 #ifdef HAVE_UNIX_SOCKETS
140 /* UNIX socket */
141 else
143 if (!host
144 || strcmp(host, DEFAULT_PGSOCKET_DIR) == 0
145 || *p == 'm')
146 strlcpy(buf, "[local]", sizeof(buf));
147 else
148 snprintf(buf, sizeof(buf), "[local:%s]", host);
150 #endif
152 break;
153 /* DB server port number */
154 case '>':
155 if (pset.db && PQport(pset.db))
156 strlcpy(buf, PQport(pset.db), sizeof(buf));
157 break;
158 /* DB server user name */
159 case 'n':
160 if (pset.db)
161 strlcpy(buf, session_username(), sizeof(buf));
162 break;
164 case '0':
165 case '1':
166 case '2':
167 case '3':
168 case '4':
169 case '5':
170 case '6':
171 case '7':
172 *buf = (char) strtol(p, (char **) &p, 8);
173 --p;
174 break;
175 case 'R':
176 switch (status)
178 case PROMPT_READY:
179 if (!pset.db)
180 buf[0] = '!';
181 else if (!pset.singleline)
182 buf[0] = '=';
183 else
184 buf[0] = '^';
185 break;
186 case PROMPT_CONTINUE:
187 buf[0] = '-';
188 break;
189 case PROMPT_SINGLEQUOTE:
190 buf[0] = '\'';
191 break;
192 case PROMPT_DOUBLEQUOTE:
193 buf[0] = '"';
194 break;
195 case PROMPT_DOLLARQUOTE:
196 buf[0] = '$';
197 break;
198 case PROMPT_COMMENT:
199 buf[0] = '*';
200 break;
201 case PROMPT_PAREN:
202 buf[0] = '(';
203 break;
204 default:
205 buf[0] = '\0';
206 break;
208 break;
210 case 'x':
211 if (!pset.db)
212 buf[0] = '?';
213 else
214 switch (PQtransactionStatus(pset.db))
216 case PQTRANS_IDLE:
217 buf[0] = '\0';
218 break;
219 case PQTRANS_ACTIVE:
220 case PQTRANS_INTRANS:
221 buf[0] = '*';
222 break;
223 case PQTRANS_INERROR:
224 buf[0] = '!';
225 break;
226 default:
227 buf[0] = '?';
228 break;
230 break;
232 case '?':
233 /* not here yet */
234 break;
236 case '#':
237 if (is_superuser())
238 buf[0] = '#';
239 else
240 buf[0] = '>';
241 break;
243 /* execute command */
244 case '`':
246 FILE *fd;
247 char *file = pg_strdup(p + 1);
248 int cmdend;
250 cmdend = strcspn(file, "`");
251 file[cmdend] = '\0';
252 fd = popen(file, "r");
253 if (fd)
255 fgets(buf, sizeof(buf), fd);
256 pclose(fd);
258 if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
259 buf[strlen(buf) - 1] = '\0';
260 free(file);
261 p += cmdend + 1;
262 break;
265 /* interpolate variable */
266 case ':':
268 char *name;
269 const char *val;
270 int nameend;
272 name = pg_strdup(p + 1);
273 nameend = strcspn(name, ":");
274 name[nameend] = '\0';
275 val = GetVariable(pset.vars, name);
276 if (val)
277 strlcpy(buf, val, sizeof(buf));
278 free(name);
279 p += nameend + 1;
280 break;
283 case '[':
284 case ']':
285 #if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
288 * readline >=4.0 undocumented feature: non-printing
289 * characters in prompt strings must be marked as such, in
290 * order to properly display the line during editing.
292 buf[0] = (*p == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
293 buf[1] = '\0';
294 #endif /* USE_READLINE */
295 break;
297 default:
298 buf[0] = *p;
299 buf[1] = '\0';
300 break;
303 esc = false;
305 else if (*p == '%')
306 esc = true;
307 else
309 buf[0] = *p;
310 buf[1] = '\0';
311 esc = false;
314 if (!esc)
315 strlcat(destination, buf, sizeof(destination));
318 return destination;