PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
prompt.h File Reference
Include dependency graph for prompt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * get_prompt (promptStatus_t status, ConditionalStack cstack)
 

Function Documentation

◆ get_prompt()

char* get_prompt ( promptStatus_t  status,
ConditionalStack  cstack 
)

Definition at line 66 of file prompt.c.

67 {
68 #define MAX_PROMPT_SIZE 256
69  static char destination[MAX_PROMPT_SIZE + 1];
70  char buf[MAX_PROMPT_SIZE + 1];
71  bool esc = false;
72  const char *p;
73  const char *prompt_string = "? ";
74  static size_t last_prompt1_width = 0;
75 
76  switch (status)
77  {
78  case PROMPT_READY:
79  prompt_string = pset.prompt1;
80  break;
81 
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;
90 
91  case PROMPT_COPY:
92  prompt_string = pset.prompt3;
93  break;
94  }
95 
96  destination[0] = '\0';
97 
98  for (p = prompt_string;
99  *p && strlen(destination) < sizeof(destination) - 1;
100  p++)
101  {
102  memset(buf, 0, sizeof(buf));
103  if (esc)
104  {
105  switch (*p)
106  {
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)
114  {
115  const char *var;
116 
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));
122  }
123  break;
124 
125  /* Whitespace of the same width as the last PROMPT1 */
126  case 'w':
127  if (pset.db)
128  memset(buf, ' ',
129  Min(last_prompt1_width, sizeof(buf) - 1));
130  break;
131 
132  /* DB server hostname (long/short) */
133  case 'M':
134  case 'm':
135  if (pset.db)
136  {
137  const char *host = PQhost(pset.db);
138 
139  /* INET socket */
140  if (host && host[0] && !is_unixsock_path(host))
141  {
142  strlcpy(buf, host, sizeof(buf));
143  if (*p == 'm')
144  buf[strcspn(buf, ".")] = '\0';
145  }
146  /* UNIX socket */
147  else
148  {
149  if (!host
150  || strcmp(host, DEFAULT_PGSOCKET_DIR) == 0
151  || *p == 'm')
152  strlcpy(buf, "[local]", sizeof(buf));
153  else
154  snprintf(buf, sizeof(buf), "[local:%s]", host);
155  }
156  }
157  break;
158  /* DB server port number */
159  case '>':
160  if (pset.db && PQport(pset.db))
161  strlcpy(buf, PQport(pset.db), sizeof(buf));
162  break;
163  /* DB server user name */
164  case 'n':
165  if (pset.db)
166  strlcpy(buf, session_username(), sizeof(buf));
167  break;
168  /* backend pid */
169  case 'p':
170  if (pset.db)
171  {
172  int pid = PQbackendPID(pset.db);
173 
174  if (pid)
175  snprintf(buf, sizeof(buf), "%d", pid);
176  }
177  break;
178 
179  case '0':
180  case '1':
181  case '2':
182  case '3':
183  case '4':
184  case '5':
185  case '6':
186  case '7':
187  *buf = (char) strtol(p, unconstify(char **, &p), 8);
188  --p;
189  break;
190  case 'R':
191  switch (status)
192  {
193  case PROMPT_READY:
194  if (cstack != NULL && !conditional_active(cstack))
195  buf[0] = '@';
196  else if (!pset.db)
197  buf[0] = '!';
198  else if (!pset.singleline)
199  buf[0] = '=';
200  else
201  buf[0] = '^';
202  break;
203  case PROMPT_CONTINUE:
204  buf[0] = '-';
205  break;
206  case PROMPT_SINGLEQUOTE:
207  buf[0] = '\'';
208  break;
209  case PROMPT_DOUBLEQUOTE:
210  buf[0] = '"';
211  break;
212  case PROMPT_DOLLARQUOTE:
213  buf[0] = '$';
214  break;
215  case PROMPT_COMMENT:
216  buf[0] = '*';
217  break;
218  case PROMPT_PAREN:
219  buf[0] = '(';
220  break;
221  default:
222  buf[0] = '\0';
223  break;
224  }
225  break;
226 
227  case 'x':
228  if (!pset.db)
229  buf[0] = '?';
230  else
231  switch (PQtransactionStatus(pset.db))
232  {
233  case PQTRANS_IDLE:
234  buf[0] = '\0';
235  break;
236  case PQTRANS_ACTIVE:
237  case PQTRANS_INTRANS:
238  buf[0] = '*';
239  break;
240  case PQTRANS_INERROR:
241  buf[0] = '!';
242  break;
243  default:
244  buf[0] = '?';
245  break;
246  }
247  break;
248 
249  case 'l':
251  break;
252 
253  case '?':
254  /* not here yet */
255  break;
256 
257  case '#':
258  if (is_superuser())
259  buf[0] = '#';
260  else
261  buf[0] = '>';
262  break;
263 
264  /* execute command */
265  case '`':
266  {
267  int cmdend = strcspn(p + 1, "`");
268  char *file = pnstrdup(p + 1, cmdend);
269  FILE *fd;
270 
271  fflush(NULL);
272  fd = popen(file, "r");
273  if (fd)
274  {
275  if (fgets(buf, sizeof(buf), fd) == NULL)
276  buf[0] = '\0';
277  pclose(fd);
278  }
279 
280  /* strip trailing newline and carriage return */
281  (void) pg_strip_crlf(buf);
282 
283  free(file);
284  p += cmdend + 1;
285  break;
286  }
287 
288  /* interpolate variable */
289  case ':':
290  {
291  int nameend = strcspn(p + 1, ":");
292  char *name = pnstrdup(p + 1, nameend);
293  const char *val;
294 
296  if (val)
297  strlcpy(buf, val, sizeof(buf));
298  free(name);
299  p += nameend + 1;
300  break;
301  }
302 
303  case '[':
304  case ']':
305 #if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
306 
307  /*
308  * readline >=4.0 undocumented feature: non-printing
309  * characters in prompt strings must be marked as such, in
310  * order to properly display the line during editing.
311  */
312  buf[0] = (*p == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
313  buf[1] = '\0';
314 #endif /* USE_READLINE */
315  break;
316 
317  default:
318  buf[0] = *p;
319  buf[1] = '\0';
320  break;
321  }
322  esc = false;
323  }
324  else if (*p == '%')
325  esc = true;
326  else
327  {
328  buf[0] = *p;
329  buf[1] = '\0';
330  esc = false;
331  }
332 
333  if (!esc)
334  strlcat(destination, buf, sizeof(destination));
335  }
336 
337  /* Compute the visible width of PROMPT1, for PROMPT2's %w */
338  if (prompt_string == pset.prompt1)
339  {
340  char *p = destination;
341  char *end = p + strlen(p);
342  bool visible = true;
343 
344  last_prompt1_width = 0;
345  while (*p)
346  {
347 #if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
348  if (*p == RL_PROMPT_START_IGNORE)
349  {
350  visible = false;
351  ++p;
352  }
353  else if (*p == RL_PROMPT_END_IGNORE)
354  {
355  visible = true;
356  ++p;
357  }
358  else
359 #endif
360  {
361  int chlen,
362  chwidth;
363 
364  chlen = PQmblen(p, pset.encoding);
365  if (p + chlen > end)
366  break; /* Invalid string */
367 
368  if (visible)
369  {
370  chwidth = PQdsplen(p, pset.encoding);
371 
372  if (*p == '\n')
373  last_prompt1_width = 0;
374  else if (chwidth > 0)
375  last_prompt1_width += chwidth;
376  }
377 
378  p += chlen;
379  }
380  }
381  }
382 
383  return destination;
384 }
const char * session_username(void)
Definition: common.c:2174
#define unconstify(underlying_type, expr)
Definition: c.h:1250
#define Min(x, y)
Definition: c.h:1009
#define UINT64_FORMAT
Definition: c.h:552
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:7069
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7036
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7145
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7044
int PQbackendPID(const PGconn *conn)
Definition: fe-connect.c:7234
char * PQport(const PGconn *conn)
Definition: fe-connect.c:7105
int PQmblen(const char *s, int encoding)
Definition: fe-misc.c:1224
int PQdsplen(const char *s, int encoding)
Definition: fe-misc.c:1244
#define free(a)
Definition: header.h:65
long val
Definition: informix.c:689
@ PQTRANS_INTRANS
Definition: libpq-fe.h:144
@ PQTRANS_IDLE
Definition: libpq-fe.h:142
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:143
@ PQTRANS_INERROR
Definition: libpq-fe.h:145
static void const char fflush(stdout)
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1707
#define DEFAULT_PGSOCKET_DIR
static bool is_superuser(Archive *fout)
Definition: pg_dump.c:4806
static char * buf
Definition: pg_test_fsync.c:72
#define snprintf
Definition: port.h:238
size_t strlcat(char *dst, const char *src, size_t siz)
Definition: strlcat.c:33
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static bool is_unixsock_path(const char *path)
Definition: pqcomm.h:67
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define MAX_PROMPT_SIZE
@ PROMPT_READY
Definition: psqlscan.h:41
@ PROMPT_COPY
Definition: psqlscan.h:48
@ PROMPT_PAREN
Definition: psqlscan.h:47
@ PROMPT_COMMENT
Definition: psqlscan.h:43
@ PROMPT_CONTINUE
Definition: psqlscan.h:42
@ PROMPT_SINGLEQUOTE
Definition: psqlscan.h:44
@ PROMPT_DOLLARQUOTE
Definition: psqlscan.h:46
@ PROMPT_DOUBLEQUOTE
Definition: psqlscan.h:45
PsqlSettings pset
Definition: startup.c:32
int pg_strip_crlf(char *str)
Definition: string.c:154
VariableSpace vars
Definition: settings.h:133
int encoding
Definition: settings.h:92
const char * prompt2
Definition: settings.h:163
const char * prompt3
Definition: settings.h:164
PGconn * db
Definition: settings.h:91
uint64 stmt_lineno
Definition: settings.h:127
bool singleline
Definition: settings.h:150
const char * prompt1
Definition: settings.h:162
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:71
const char * name

References buf, conditional_active(), _psqlSettings::db, DEFAULT_PGSOCKET_DIR, _psqlSettings::encoding, fd(), fflush(), free, GetVariable(), is_superuser(), is_unixsock_path(), MAX_PROMPT_SIZE, Min, name, pg_strip_crlf(), pnstrdup(), PQbackendPID(), PQdb(), PQdsplen(), PQhost(), PQmblen(), PQport(), PQTRANS_ACTIVE, PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQtransactionStatus(), PQuser(), _psqlSettings::prompt1, _psqlSettings::prompt2, _psqlSettings::prompt3, PROMPT_COMMENT, PROMPT_CONTINUE, PROMPT_COPY, PROMPT_DOLLARQUOTE, PROMPT_DOUBLEQUOTE, PROMPT_PAREN, PROMPT_READY, PROMPT_SINGLEQUOTE, pset, session_username(), _psqlSettings::singleline, snprintf, _psqlSettings::stmt_lineno, strlcat(), strlcpy(), UINT64_FORMAT, unconstify, val, and _psqlSettings::vars.

Referenced by handleCopyIn(), and MainLoop().