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 /* Current database */
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 /* Whitespace of the same width as the last PROMPT1 */
128 case 'w':
131 Min(last_prompt1_width,
sizeof(
buf) - 1));
132 break;
133
134 /* DB server hostname (long/short) */
135 case 'M':
136 case 'm':
138 {
140
141 /* INET socket */
143 {
145 if (*p == 'm')
146 buf[strcspn(
buf,
".")] =
'0円';
147 }
148 /* UNIX socket */
149 else
150 {
151 if (!host
153 || *p == 'm')
155 else
157 }
158 }
159 break;
160 /* DB server port number */
161 case '>':
164 break;
165 /* DB server user name */
166 case 'n':
169 break;
170 /* service name */
171 case 's':
172 {
174
175 if (service_name)
177 }
178 break;
179 /* backend pid */
180 case 'p':
182 {
184
185 if (pid)
187 }
188 break;
189 /* pipeline status */
190 case 'P':
191 {
193
198 else
200 break;
201 }
202
203 case '0':
204 case '1':
205 case '2':
206 case '3':
207 case '4':
208 case '5':
209 case '6':
210 case '7':
212 --p;
213 break;
214 case 'R':
215 switch (status)
216 {
224 else
226 break;
229 break;
232 break;
235 break;
238 break;
241 break;
244 break;
245 default:
247 break;
248 }
249 break;
250
251 case 'x':
254 else
256 {
259 break;
263 break;
266 break;
267 default:
269 break;
270 }
271 break;
272
273 case 'l':
275 break;
276
277 case '?':
278 /* not here yet */
279 break;
280
281 case '#':
284 else
286 break;
287
288 /* execute command */
289 case '`':
290 {
291 int cmdend = strcspn(p + 1, "`");
292 char *file =
pnstrdup(p + 1, cmdend);
294
295 fflush(NULL);
296 fd = popen(file,
"r");
298 {
299 if (fgets(
buf,
sizeof(
buf),
fd) == NULL)
302 }
303
304 /* strip trailing newline and carriage return */
306
308 p += cmdend + 1;
309 break;
310 }
311
312 /* interpolate variable */
313 case ':':
314 {
315 int nameend = strcspn(p + 1, ":");
318
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;
338#endif /* USE_READLINE */
339 break;
340
341 default:
344 break;
345 }
346 esc = false;
347 }
348 else if (*p == '%')
349 esc = true;
350 else
351 {
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 */
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
389 if (p + chlen > end)
390 break; /* Invalid string */
391
392 if (visible)
393 {
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}
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)
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)
int pg_strip_crlf(char *str)
const char * GetVariable(VariableSpace space, const char *name)