PostgreSQL Source Code  git master
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 70 of file prompt.c.

References buf, conditional_active(), _psqlSettings::db, DEFAULT_PGSOCKET_DIR, _psqlSettings::encoding, fd(), free, GetVariable(), is_absolute_path, is_superuser(), MAX_PROMPT_SIZE, Min, name, pg_strdup(), pg_strip_crlf(), 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().

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