PostgreSQL Source Code: src/bin/psql/prompt.c Source File (original) (raw)
1
2
3
4
5
6
7
9
10#ifdef WIN32
11#include <io.h>
12#include <win32.h>
13#endif
14
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67char *
69{
70#define MAX_PROMPT_SIZE 256
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 {
82 break;
83
91 break;
92
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
110 case '/':
113 break;
114 case '~':
116 {
117 const char *var;
118
120 ((var = getenv("PGDATABASE")) && strcmp(var, PQdb(pset.db)) == 0))
122 else
124 }
125 break;
126
127
128 case 'w':
130 memset(buf, ' ',
131 Min(last_prompt1_width, sizeof(buf) - 1));
132 break;
133
134
135 case 'M':
136 case 'm':
138 {
140
141
143 {
145 if (*p == 'm')
146 buf[strcspn(buf, ".")] = '\0';
147 }
148
149 else
150 {
151 if (!host
153 || *p == 'm')
155 else
157 }
158 }
159 break;
160
161 case '>':
164 break;
165
166 case 'n':
169 break;
170
171 case 's':
174 break;
175
176 case 'p':
178 {
180
181 if (pid)
183 }
184 break;
185
186 case 'P':
187 {
189
194 else
196 break;
197 }
198
199 case '0':
200 case '1':
201 case '2':
202 case '3':
203 case '4':
204 case '5':
205 case '6':
206 case '7':
207 *buf = (char) strtol(p, unconstify(char **, &p), 8);
208 --p;
209 break;
210 case 'R':
211 switch (status)
212 {
215 buf[0] = '@';
217 buf[0] = '!';
219 buf[0] = '=';
220 else
221 buf[0] = '^';
222 break;
224 buf[0] = '-';
225 break;
227 buf[0] = '\'';
228 break;
230 buf[0] = '"';
231 break;
233 buf[0] = '$';
234 break;
236 buf[0] = '*';
237 break;
239 buf[0] = '(';
240 break;
241 default:
242 buf[0] = '\0';
243 break;
244 }
245 break;
246
247 case 'x':
249 buf[0] = '?';
250 else
252 {
254 buf[0] = '\0';
255 break;
258 buf[0] = '*';
259 break;
261 buf[0] = '!';
262 break;
263 default:
264 buf[0] = '?';
265 break;
266 }
267 break;
268
269 case 'l':
271 break;
272
273 case '?':
274
275 break;
276
277 case '#':
279 buf[0] = '#';
280 else
281 buf[0] = '>';
282 break;
283
284
285 case '`':
286 {
287 int cmdend = strcspn(p + 1, "`");
288 char *file = pnstrdup(p + 1, cmdend);
289 FILE *fd;
290
291 fflush(NULL);
292 fd = popen(file, "r");
293 if (fd)
294 {
295 if (fgets(buf, sizeof(buf), fd) == NULL)
296 buf[0] = '\0';
297 pclose(fd);
298 }
299
300
302
304 p += cmdend + 1;
305 break;
306 }
307
308
309 case ':':
310 {
311 int nameend = strcspn(p + 1, ":");
313 const char *val;
314
319 p += nameend + 1;
320 break;
321 }
322
323 case '[':
324 case ']':
325#if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
326
327
328
329
330
331
332 buf[0] = (*p == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
333 buf[1] = '\0';
334#endif
335 break;
336
337 default:
338 buf[0] = *p;
339 buf[1] = '\0';
340 break;
341 }
342 esc = false;
343 }
344 else if (*p == '%')
345 esc = true;
346 else
347 {
348 buf[0] = *p;
349 buf[1] = '\0';
350 esc = false;
351 }
352
353 if (!esc)
354 strlcat(destination, buf, sizeof(destination));
355 }
356
357
359 {
360 char *p = destination;
361 char *end = p + strlen(p);
362 bool visible = true;
363
364 last_prompt1_width = 0;
365 while (*p)
366 {
367#if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
368 if (*p == RL_PROMPT_START_IGNORE)
369 {
370 visible = false;
371 ++p;
372 }
373 else if (*p == RL_PROMPT_END_IGNORE)
374 {
375 visible = true;
376 ++p;
377 }
378 else
379#endif
380 {
381 int chlen,
382 chwidth;
383
385 if (p + chlen > end)
386 break;
387
388 if (visible)
389 {
391
392 if (*p == '\n')
393 last_prompt1_width = 0;
394 else if (chwidth > 0)
395 last_prompt1_width += chwidth;
396 }
397
398 p += chlen;
399 }
400 }
401 }
402
403 return destination;
404}
const char * session_username(void)
#define unconstify(underlying_type, expr)
bool conditional_active(ConditionalStack cstack)
char * PQdb(const PGconn *conn)
char * PQport(const PGconn *conn)
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
char * PQhost(const PGconn *conn)
char * PQservice(const PGconn *conn)
int PQbackendPID(const PGconn *conn)
char * PQuser(const PGconn *conn)
PGpipelineStatus PQpipelineStatus(const PGconn *conn)
int PQmblen(const char *s, int encoding)
int PQdsplen(const char *s, int encoding)
char * pnstrdup(const char *in, Size len)
#define DEFAULT_PGSOCKET_DIR
static bool is_superuser(Archive *fout)
size_t strlcat(char *dst, const char *src, size_t siz)
size_t strlcpy(char *dst, const char *src, size_t siz)
static bool is_unixsock_path(const char *path)
static int fd(const char *x, int i)
char * get_prompt(promptStatus_t status, ConditionalStack cstack)
enum _promptStatus promptStatus_t
int pg_strip_crlf(char *str)
const char * GetVariable(VariableSpace space, const char *name)