PostgreSQL Source Code  git master
input.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include "common.h"
#include "common/logging.h"
#include "input.h"
#include "settings.h"
#include "tab-complete.h"
Include dependency graph for input.c:

Go to the source code of this file.

Macros

#define PSQLHISTORY   ".psql_history"
 

Functions

static void finishInput (void)
 
char * gets_interactive (const char *prompt, PQExpBuffer query_buf)
 
void pg_append_history (const char *s, PQExpBuffer history_buf)
 
void pg_send_history (PQExpBuffer history_buf)
 
char * gets_fromFile (FILE *source)
 
void initializeInput (int flags)
 
bool printHistory (const char *fname, unsigned short int pager)
 

Macro Definition Documentation

◆ PSQLHISTORY

#define PSQLHISTORY   ".psql_history"

Definition at line 23 of file input.c.

Function Documentation

◆ finishInput()

static void finishInput ( void  )
static

Definition at line 541 of file input.c.

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 }
#define free(a)
Definition: header.h:65
PsqlSettings pset
Definition: startup.c:32

References free, _psqlSettings::histsize, and pset.

Referenced by initializeInput().

◆ gets_fromFile()

char* gets_fromFile ( FILE *  source)

Definition at line 187 of file input.c.

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 }
volatile bool sigint_interrupt_enabled
Definition: common.c:248
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define pg_log_error(...)
Definition: logging.h:106
static rewind_source * source
Definition: pg_rewind.c:81
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define PQExpBufferBroken(str)
Definition: pqexpbuffer.h:59

References appendPQExpBufferStr(), createPQExpBuffer(), PQExpBufferData::data, PQExpBufferData::len, pg_log_error, pg_strdup(), PQExpBufferBroken, resetPQExpBuffer(), sigint_interrupt_enabled, and source.

Referenced by exec_command_prompt(), gets_interactive(), and MainLoop().

◆ gets_interactive()

char* gets_interactive ( const char *  prompt,
PQExpBuffer  query_buf 
)

Definition at line 67 of file input.c.

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 }
char * gets_fromFile(FILE *source)
Definition: input.c:187
static void const char fflush(stdout)
PQExpBuffer tab_completion_query_buf

References fflush(), gets_fromFile(), sigint_interrupt_enabled, generate_unaccent_rules::stdout, and tab_completion_query_buf.

Referenced by MainLoop().

◆ initializeInput()

void initializeInput ( int  flags)

Definition at line 345 of file input.c.

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 }
void expand_tilde(char **filename)
Definition: common.c:2267
static void finishInput(void)
Definition: input.c:541
#define PSQLHISTORY
Definition: input.c:23
#define MAXPGPATH
bool get_home_path(char *ret_path)
Definition: path.c:928
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
VariableSpace vars
Definition: settings.h:118
void initialize_readline(void)
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:71

References expand_tilde(), finishInput(), get_home_path(), GetVariable(), initialize_readline(), MAXPGPATH, pg_strdup(), pset, psprintf(), PSQLHISTORY, and _psqlSettings::vars.

Referenced by main().

◆ pg_append_history()

void pg_append_history ( const char *  s,
PQExpBuffer  history_buf 
)

Definition at line 114 of file input.c.

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 }
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380

References appendPQExpBufferChar(), and appendPQExpBufferStr().

Referenced by MainLoop().

◆ pg_send_history()

void pg_send_history ( PQExpBuffer  history_buf)

Definition at line 136 of file input.c.

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 }
int i
Definition: isn.c:73
@ hctl_ignoredups
Definition: settings.h:69
@ hctl_ignorespace
Definition: settings.h:68
HistControl histcontrol
Definition: settings.h:146

References PQExpBufferData::data, free, hctl_ignoredups, hctl_ignorespace, _psqlSettings::histcontrol, i, pg_strdup(), pset, and resetPQExpBuffer().

Referenced by MainLoop().

◆ printHistory()

bool printHistory ( const char *  fname,
unsigned short int  pager 
)

Definition at line 495 of file input.c.

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 }
void ClosePager(FILE *pagerpipe)
Definition: print.c:3096
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3045
static void output(uint64 loop_count)
#define fprintf
Definition: port.h:229
printQueryOpt popt
Definition: settings.h:91
printTableOpt topt
Definition: print.h:169

References ClosePager(), fprintf, output(), PageOutput(), pg_log_error, _psqlSettings::popt, pset, and printQueryOpt::topt.

Referenced by exec_command_s().