PostgreSQL Source Code  git master
input.h File Reference
#include "pqexpbuffer.h"
Include dependency graph for input.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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

Function Documentation

◆ gets_fromFile()

char* gets_fromFile ( FILE *  source)

Definition at line 188 of file input.c.

189 {
190  static PQExpBuffer buffer = NULL;
191 
192  char line[1024];
193 
194  if (buffer == NULL) /* first time through? */
195  buffer = createPQExpBuffer();
196  else
197  resetPQExpBuffer(buffer);
198 
199  for (;;)
200  {
201  char *result;
202 
203  /* Enable SIGINT to longjmp to sigint_interrupt_jmp */
205 
206  /* Get some data */
207  result = fgets(line, sizeof(line), source);
208 
209  /* Disable SIGINT again */
210  sigint_interrupt_enabled = false;
211 
212  /* EOF or error? */
213  if (result == NULL)
214  {
215  if (ferror(source))
216  {
217  pg_log_error("could not read from input file: %m");
218  return NULL;
219  }
220  break;
221  }
222 
223  appendPQExpBufferStr(buffer, line);
224 
225  if (PQExpBufferBroken(buffer))
226  {
227  pg_log_error("out of memory");
228  return NULL;
229  }
230 
231  /* EOL? */
232  if (buffer->len > 0 && buffer->data[buffer->len - 1] == '\n')
233  {
234  buffer->data[buffer->len - 1] = '\0';
235  return pg_strdup(buffer->data);
236  }
237  }
238 
239  if (buffer->len > 0) /* EOF after reading some bufferload(s) */
240  return pg_strdup(buffer->data);
241 
242  /* EOF, so return null */
243  return NULL;
244 }
volatile bool sigint_interrupt_enabled
Definition: common.c:246
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define pg_log_error(...)
Definition: logging.h:80
static rewind_source * source
Definition: pg_rewind.c:79
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:188
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 346 of file input.c.

347 {
348 #ifdef USE_READLINE
349  if (flags & 1)
350  {
351  const char *histfile;
352  char home[MAXPGPATH];
353 
354  useReadline = true;
355 
356  /* set appropriate values for Readline's global variables */
358 
359 #ifdef HAVE_RL_VARIABLE_BIND
360  /* set comment-begin to a useful value for SQL */
361  (void) rl_variable_bind("comment-begin", "-- ");
362 #endif
363 
364  /* this reads ~/.inputrc, so do it after rl_variable_bind */
365  rl_initialize();
366 
367  useHistory = true;
368  using_history();
369  history_lines_added = 0;
370 
371  histfile = GetVariable(pset.vars, "HISTFILE");
372 
373  if (histfile == NULL)
374  {
375  char *envhist;
376 
377  envhist = getenv("PSQL_HISTORY");
378  if (envhist != NULL && strlen(envhist) > 0)
379  histfile = envhist;
380  }
381 
382  if (histfile == NULL)
383  {
384  if (get_home_path(home))
385  psql_history = psprintf("%s/%s", home, PSQLHISTORY);
386  }
387  else
388  {
389  psql_history = pg_strdup(histfile);
390  expand_tilde(&psql_history);
391  }
392 
393  if (psql_history)
394  {
395  read_history(psql_history);
396  decode_history();
397  }
398  }
399 #endif
400 
401  atexit(finishInput);
402 }
void expand_tilde(char **filename)
Definition: common.c:2215
static void finishInput(void)
Definition: input.c:542
#define PSQLHISTORY
Definition: input.c:23
#define MAXPGPATH
bool get_home_path(char *ret_path)
Definition: path.c:807
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
PsqlSettings pset
Definition: startup.c:32
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  if (prev_hist)
162  free(prev_hist);
163  prev_hist = pg_strdup(s);
164  /* And send it to readline */
165  add_history(s);
166  /* Count lines added to history for use later */
167  history_lines_added++;
168  }
169  }
170 
171  resetPQExpBuffer(history_buf);
172 #endif
173 }
#define free(a)
Definition: header.h:65
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 496 of file input.c.

497 {
498 #ifdef USE_READLINE
499  FILE *output;
500  bool is_pager;
501 
502  if (!useHistory)
503  return false;
504 
505  if (fname == NULL)
506  {
507  /* use pager, if enabled, when printing to console */
508  output = PageOutput(INT_MAX, pager ? &(pset.popt.topt) : NULL);
509  is_pager = true;
510  }
511  else
512  {
513  output = fopen(fname, "w");
514  if (output == NULL)
515  {
516  pg_log_error("could not save history to file \"%s\": %m", fname);
517  return false;
518  }
519  is_pager = false;
520  }
521 
522  BEGIN_ITERATE_HISTORY(cur_hist);
523  {
524  fprintf(output, "%s\n", cur_hist->line);
525  }
526  END_ITERATE_HISTORY();
527 
528  if (is_pager)
530  else
531  fclose(output);
532 
533  return true;
534 #else
535  pg_log_error("history is not supported by this installation");
536  return false;
537 #endif
538 }
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().