PostgreSQL Source Code  git master
input.c
Go to the documentation of this file.
1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Copyright (c) 2000-2023, PostgreSQL Global Development Group
5  *
6  * src/bin/psql/input.c
7  */
8 #include "postgres_fe.h"
9 
10 #ifndef WIN32
11 #include <unistd.h>
12 #endif
13 #include <fcntl.h>
14 #include <limits.h>
15 
16 #include "common.h"
17 #include "common/logging.h"
18 #include "input.h"
19 #include "settings.h"
20 #include "tab-complete.h"
21 
22 #ifndef WIN32
23 #define PSQLHISTORY ".psql_history"
24 #else
25 #define PSQLHISTORY "psql_history"
26 #endif
27 
28 /* Runtime options for turning off readline and history */
29 /* (of course there is no runtime command for doing that :) */
30 #ifdef USE_READLINE
31 static bool useReadline;
32 static bool useHistory;
33 
34 static char *psql_history;
35 
36 static int history_lines_added;
37 
38 
39 /*
40  * Preserve newlines in saved queries by mapping '\n' to NL_IN_HISTORY
41  *
42  * It is assumed NL_IN_HISTORY will never be entered by the user
43  * nor appear inside a multi-byte string. 0x00 is not properly
44  * handled by the readline routines so it can not be used
45  * for this purpose.
46  */
47 #define NL_IN_HISTORY 0x01
48 #endif
49 
50 static void finishInput(void);
51 
52 
53 /*
54  * gets_interactive()
55  *
56  * Gets a line of interactive input, using readline if desired.
57  *
58  * prompt: the prompt string to be used
59  * query_buf: buffer containing lines already read in the current command
60  * (query_buf is not modified here, but may be consulted for tab completion)
61  *
62  * The result is a malloc'd string.
63  *
64  * Caller *must* have set up sigint_interrupt_jmp before calling.
65  */
66 char *
67 gets_interactive(const char *prompt, PQExpBuffer query_buf)
68 {
69 #ifdef USE_READLINE
70  if (useReadline)
71  {
72  char *result;
73 
74  /*
75  * Some versions of readline don't notice SIGWINCH signals that arrive
76  * when not actively reading input. The simplest fix is to always
77  * re-read the terminal size. This leaves a window for SIGWINCH to be
78  * missed between here and where readline() enables libreadline's
79  * signal handler, but that's probably short enough to be ignored.
80  */
81 #ifdef HAVE_RL_RESET_SCREEN_SIZE
82  rl_reset_screen_size();
83 #endif
84 
85  /* Make current query_buf available to tab completion callback */
86  tab_completion_query_buf = query_buf;
87 
88  /* Enable SIGINT to longjmp to sigint_interrupt_jmp */
90 
91  /* On some platforms, readline is declared as readline(char *) */
92  result = readline((char *) prompt);
93 
94  /* Disable SIGINT again */
96 
97  /* Pure neatnik-ism */
99 
100  return result;
101  }
102 #endif
103 
104  fputs(prompt, stdout);
105  fflush(stdout);
106  return gets_fromFile(stdin);
107 }
108 
109 
110 /*
111  * Append the line to the history buffer, making sure there is a trailing '\n'
112  */
113 void
114 pg_append_history(const char *s, PQExpBuffer history_buf)
115 {
116 #ifdef USE_READLINE
117  if (useHistory && s)
118  {
119  appendPQExpBufferStr(history_buf, s);
120  if (!s[0] || s[strlen(s) - 1] != '\n')
121  appendPQExpBufferChar(history_buf, '\n');
122  }
123 #endif
124 }
125 
126 
127 /*
128  * Emit accumulated history entry to readline's history mechanism,
129  * then reset the buffer to empty.
130  *
131  * Note: we write nothing if history_buf is empty, so extra calls to this
132  * function don't hurt. There must have been at least one line added by
133  * pg_append_history before we'll do anything.
134  */
135 void
137 {
138 #ifdef USE_READLINE
139  static char *prev_hist = NULL;
140 
141  char *s = history_buf->data;
142  int i;
143 
144  /* Trim any trailing \n's (OK to scribble on history_buf) */
145  for (i = strlen(s) - 1; i >= 0 && s[i] == '\n'; i--)
146  ;
147  s[i + 1] = '\0';
148 
149  if (useHistory && s[0])
150  {
151  if (((pset.histcontrol & hctl_ignorespace) &&
152  s[0] == ' ') ||
154  prev_hist && strcmp(s, prev_hist) == 0))
155  {
156  /* Ignore this line as far as history is concerned */
157  }
158  else
159  {
160  /* Save each previous line for ignoredups processing */
161  free(prev_hist);
162  prev_hist = pg_strdup(s);
163  /* And send it to readline */
164  add_history(s);
165  /* Count lines added to history for use later */
166  history_lines_added++;
167  }
168  }
169 
170  resetPQExpBuffer(history_buf);
171 #endif
172 }
173 
174 
175 /*
176  * gets_fromFile
177  *
178  * Gets a line of noninteractive input from a file (which could be stdin).
179  * The result is a malloc'd string, or NULL on EOF or input error.
180  *
181  * Caller *must* have set up sigint_interrupt_jmp before calling.
182  *
183  * Note: we re-use a static PQExpBuffer for each call. This is to avoid
184  * leaking memory if interrupted by SIGINT.
185  */
186 char *
188 {
189  static PQExpBuffer buffer = NULL;
190 
191  char line[1024];
192 
193  if (buffer == NULL) /* first time through? */
194  buffer = createPQExpBuffer();
195  else
196  resetPQExpBuffer(buffer);
197 
198  for (;;)
199  {
200  char *result;
201 
202  /* Enable SIGINT to longjmp to sigint_interrupt_jmp */
204 
205  /* Get some data */
206  result = fgets(line, sizeof(line), source);
207 
208  /* Disable SIGINT again */
209  sigint_interrupt_enabled = false;
210 
211  /* EOF or error? */
212  if (result == NULL)
213  {
214  if (ferror(source))
215  {
216  pg_log_error("could not read from input file: %m");
217  return NULL;
218  }
219  break;
220  }
221 
222  appendPQExpBufferStr(buffer, line);
223 
224  if (PQExpBufferBroken(buffer))
225  {
226  pg_log_error("out of memory");
227  return NULL;
228  }
229 
230  /* EOL? */
231  if (buffer->len > 0 && buffer->data[buffer->len - 1] == '\n')
232  {
233  buffer->data[buffer->len - 1] = '\0';
234  return pg_strdup(buffer->data);
235  }
236  }
237 
238  if (buffer->len > 0) /* EOF after reading some bufferload(s) */
239  return pg_strdup(buffer->data);
240 
241  /* EOF, so return null */
242  return NULL;
243 }
244 
245 
246 #ifdef USE_READLINE
247 
248 /*
249  * Macros to iterate over each element of the history list in order
250  *
251  * You would think this would be simple enough, but in its inimitable fashion
252  * libedit has managed to break it: in libreadline we must use next_history()
253  * to go from oldest to newest, but in libedit we must use previous_history().
254  * To detect what to do, we make a trial call of previous_history(): if it
255  * fails, then either next_history() is what to use, or there's zero or one
256  * history entry so that it doesn't matter which direction we go.
257  *
258  * In case that wasn't disgusting enough: the code below is not as obvious as
259  * it might appear. In some libedit releases history_set_pos(0) fails until
260  * at least one add_history() call has been done. This is not an issue for
261  * printHistory() or encode_history(), which cannot be invoked before that has
262  * happened. In decode_history(), that's not so, and what actually happens is
263  * that we are sitting on the newest entry to start with, previous_history()
264  * fails, and we iterate over all the entries using next_history(). So the
265  * decode_history() loop iterates over the entries in the wrong order when
266  * using such a libedit release, and if there were another attempt to use
267  * BEGIN_ITERATE_HISTORY() before some add_history() call had happened, it
268  * wouldn't work. Fortunately we don't care about either of those things.
269  *
270  * Usage pattern is:
271  *
272  * BEGIN_ITERATE_HISTORY(varname);
273  * {
274  * loop body referencing varname->line;
275  * }
276  * END_ITERATE_HISTORY();
277  */
278 #define BEGIN_ITERATE_HISTORY(VARNAME) \
279  do { \
280  HIST_ENTRY *VARNAME; \
281  bool use_prev_; \
282  \
283  history_set_pos(0); \
284  use_prev_ = (previous_history() != NULL); \
285  history_set_pos(0); \
286  for (VARNAME = current_history(); VARNAME != NULL; \
287  VARNAME = use_prev_ ? previous_history() : next_history()) \
288  { \
289  (void) 0
290 
291 #define END_ITERATE_HISTORY() \
292  } \
293  } while(0)
294 
295 
296 /*
297  * Convert newlines to NL_IN_HISTORY for safe saving in readline history file
298  */
299 static void
300 encode_history(void)
301 {
302  BEGIN_ITERATE_HISTORY(cur_hist);
303  {
304  char *cur_ptr;
305 
306  /* some platforms declare HIST_ENTRY.line as const char * */
307  for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
308  {
309  if (*cur_ptr == '\n')
310  *cur_ptr = NL_IN_HISTORY;
311  }
312  }
313  END_ITERATE_HISTORY();
314 }
315 
316 /*
317  * Reverse the above encoding
318  */
319 static void
320 decode_history(void)
321 {
322  BEGIN_ITERATE_HISTORY(cur_hist);
323  {
324  char *cur_ptr;
325 
326  /* some platforms declare HIST_ENTRY.line as const char * */
327  for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
328  {
329  if (*cur_ptr == NL_IN_HISTORY)
330  *cur_ptr = '\n';
331  }
332  }
333  END_ITERATE_HISTORY();
334 }
335 #endif /* USE_READLINE */
336 
337 
338 /*
339  * Put any startup stuff related to input in here. It's good to maintain
340  * abstraction this way.
341  *
342  * The only "flag" right now is 1 for use readline & history.
343  */
344 void
345 initializeInput(int flags)
346 {
347 #ifdef USE_READLINE
348  if (flags & 1)
349  {
350  const char *histfile;
351  char home[MAXPGPATH];
352 
353  useReadline = true;
354 
355  /* set appropriate values for Readline's global variables */
357 
358 #ifdef HAVE_RL_VARIABLE_BIND
359  /* set comment-begin to a useful value for SQL */
360  (void) rl_variable_bind("comment-begin", "-- ");
361 #endif
362 
363  /* this reads ~/.inputrc, so do it after rl_variable_bind */
364  rl_initialize();
365 
366  useHistory = true;
367  using_history();
368  history_lines_added = 0;
369 
370  histfile = GetVariable(pset.vars, "HISTFILE");
371 
372  if (histfile == NULL)
373  {
374  char *envhist;
375 
376  envhist = getenv("PSQL_HISTORY");
377  if (envhist != NULL && strlen(envhist) > 0)
378  histfile = envhist;
379  }
380 
381  if (histfile == NULL)
382  {
383  if (get_home_path(home))
384  psql_history = psprintf("%s/%s", home, PSQLHISTORY);
385  }
386  else
387  {
388  psql_history = pg_strdup(histfile);
389  expand_tilde(&psql_history);
390  }
391 
392  if (psql_history)
393  {
394  read_history(psql_history);
395  decode_history();
396  }
397  }
398 #endif
399 
400  atexit(finishInput);
401 }
402 
403 
404 /*
405  * This function saves the readline history when psql exits.
406  *
407  * fname: pathname of history file. (Should really be "const char *",
408  * but some ancient versions of readline omit the const-decoration.)
409  *
410  * max_lines: if >= 0, limit history file to that many entries.
411  */
412 #ifdef USE_READLINE
413 static bool
414 saveHistory(char *fname, int max_lines)
415 {
416  int errnum;
417 
418  /*
419  * Suppressing the write attempt when HISTFILE is set to /dev/null may
420  * look like a negligible optimization, but it's necessary on e.g. macOS,
421  * where write_history will fail because it tries to chmod the target
422  * file.
423  */
424  if (strcmp(fname, DEVNULL) != 0)
425  {
426  /*
427  * Encode \n, since otherwise readline will reload multiline history
428  * entries as separate lines. (libedit doesn't really need this, but
429  * we do it anyway since it's too hard to tell which implementation we
430  * are using.)
431  */
432  encode_history();
433 
434  /*
435  * On newer versions of libreadline, truncate the history file as
436  * needed and then append what we've added. This avoids overwriting
437  * history from other concurrent sessions (although there are still
438  * race conditions when two sessions exit at about the same time). If
439  * we don't have those functions, fall back to write_history().
440  */
441 #if defined(HAVE_HISTORY_TRUNCATE_FILE) && defined(HAVE_APPEND_HISTORY)
442  {
443  int nlines;
444  int fd;
445 
446  /* truncate previous entries if needed */
447  if (max_lines >= 0)
448  {
449  nlines = Max(max_lines - history_lines_added, 0);
450  (void) history_truncate_file(fname, nlines);
451  }
452  /* append_history fails if file doesn't already exist :-( */
453  fd = open(fname, O_CREAT | O_WRONLY | PG_BINARY, 0600);
454  if (fd >= 0)
455  close(fd);
456  /* append the appropriate number of lines */
457  if (max_lines >= 0)
458  nlines = Min(max_lines, history_lines_added);
459  else
460  nlines = history_lines_added;
461  errnum = append_history(nlines, fname);
462  if (errnum == 0)
463  return true;
464  }
465 #else /* don't have append support */
466  {
467  /* truncate what we have ... */
468  if (max_lines >= 0)
469  stifle_history(max_lines);
470  /* ... and overwrite file. Tough luck for concurrent sessions. */
471  errnum = write_history(fname);
472  if (errnum == 0)
473  return true;
474  }
475 #endif
476 
477  pg_log_error("could not save history to file \"%s\": %m", fname);
478  }
479  return false;
480 }
481 #endif
482 
483 
484 
485 /*
486  * Print history to the specified file, or to the console if fname is NULL
487  * (psql \s command)
488  *
489  * We used to use saveHistory() for this purpose, but that doesn't permit
490  * use of a pager; moreover libedit's implementation behaves incompatibly
491  * (preferring to encode its output) and may fail outright when the target
492  * file is specified as /dev/tty.
493  */
494 bool
495 printHistory(const char *fname, unsigned short int pager)
496 {
497 #ifdef USE_READLINE
498  FILE *output;
499  bool is_pager;
500 
501  if (!useHistory)
502  return false;
503 
504  if (fname == NULL)
505  {
506  /* use pager, if enabled, when printing to console */
507  output = PageOutput(INT_MAX, pager ? &(pset.popt.topt) : NULL);
508  is_pager = true;
509  }
510  else
511  {
512  output = fopen(fname, "w");
513  if (output == NULL)
514  {
515  pg_log_error("could not save history to file \"%s\": %m", fname);
516  return false;
517  }
518  is_pager = false;
519  }
520 
521  BEGIN_ITERATE_HISTORY(cur_hist);
522  {
523  fprintf(output, "%s\n", cur_hist->line);
524  }
525  END_ITERATE_HISTORY();
526 
527  if (is_pager)
529  else
530  fclose(output);
531 
532  return true;
533 #else
534  pg_log_error("history is not supported by this installation");
535  return false;
536 #endif
537 }
538 
539 
540 static void
542 {
543 #ifdef USE_READLINE
544  if (useHistory && psql_history)
545  {
546  (void) saveHistory(psql_history, pset.histsize);
547  free(psql_history);
548  psql_history = NULL;
549  }
550 #endif
551 }
void expand_tilde(char **filename)
Definition: common.c:2339
volatile sig_atomic_t sigint_interrupt_enabled
Definition: common.c:253
#define Min(x, y)
Definition: c.h:988
#define Max(x, y)
Definition: c.h:982
#define PG_BINARY
Definition: c.h:1278
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void ClosePager(FILE *pagerpipe)
Definition: print.c:3141
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3089
#define free(a)
Definition: header.h:65
FILE * output
void pg_send_history(PQExpBuffer history_buf)
Definition: input.c:136
bool printHistory(const char *fname, unsigned short int pager)
Definition: input.c:495
char * gets_interactive(const char *prompt, PQExpBuffer query_buf)
Definition: input.c:67
static void finishInput(void)
Definition: input.c:541
void initializeInput(int flags)
Definition: input.c:345
char * gets_fromFile(FILE *source)
Definition: input.c:187
void pg_append_history(const char *s, PQExpBuffer history_buf)
Definition: input.c:114
#define PSQLHISTORY
Definition: input.c:23
#define close(a)
Definition: win32.h:12
int i
Definition: isn.c:73
static void const char fflush(stdout)
#define pg_log_error(...)
Definition: logging.h:106
#define MAXPGPATH
static rewind_source * source
Definition: pg_rewind.c:87
bool get_home_path(char *ret_path)
Definition: path.c:928
#define DEVNULL
Definition: port.h:160
#define fprintf
Definition: port.h:242
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
#define PQExpBufferBroken(str)
Definition: pqexpbuffer.h:59
static int fd(const char *x, int i)
Definition: preproc-init.c:105
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
@ hctl_ignoredups
Definition: settings.h:69
@ hctl_ignorespace
Definition: settings.h:68
PsqlSettings pset
Definition: startup.c:32
printQueryOpt popt
Definition: settings.h:91
VariableSpace vars
Definition: settings.h:122
HistControl histcontrol
Definition: settings.h:150
printTableOpt topt
Definition: print.h:185
void initialize_readline(void)
PQExpBuffer tab_completion_query_buf
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:71