PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

char* gets_fromFile ( FILE *  source)

Definition at line 187 of file input.c.

References appendPQExpBufferStr(), buffer, createPQExpBuffer(), PQExpBufferData::data, PQExpBufferData::len, NULL, pg_strdup(), PQExpBufferBroken, psql_error(), resetPQExpBuffer(), sigint_interrupt_enabled, and strerror().

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

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  psql_error("could not read from input file: %s\n",
217  strerror(errno));
218  return NULL;
219  }
220  break;
221  }
222 
223  appendPQExpBufferStr(buffer, line);
224 
225  if (PQExpBufferBroken(buffer))
226  {
227  psql_error("out of memory\n");
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:234
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void psql_error(const char *fmt,...)
Definition: common.c:181
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define NULL
Definition: c.h:229
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
#define PQExpBufferBroken(str)
Definition: pqexpbuffer.h:59
const char * strerror(int errnum)
Definition: strerror.c:19
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
char* gets_interactive ( const char *  prompt,
PQExpBuffer  query_buf 
)

Definition at line 66 of file input.c.

References gets_fromFile(), NULL, sigint_interrupt_enabled, and tab_completion_query_buf.

Referenced by MainLoop().

67 {
68 #ifdef USE_READLINE
69  if (useReadline)
70  {
71  char *result;
72 
73  /*
74  * Some versions of readline don't notice SIGWINCH signals that arrive
75  * when not actively reading input. The simplest fix is to always
76  * re-read the terminal size. This leaves a window for SIGWINCH to be
77  * missed between here and where readline() enables libreadline's
78  * signal handler, but that's probably short enough to be ignored.
79  */
80 #ifdef HAVE_RL_RESET_SCREEN_SIZE
81  rl_reset_screen_size();
82 #endif
83 
84  /* Make current query_buf available to tab completion callback */
85  tab_completion_query_buf = query_buf;
86 
87  /* Enable SIGINT to longjmp to sigint_interrupt_jmp */
89 
90  /* On some platforms, readline is declared as readline(char *) */
91  result = readline((char *) prompt);
92 
93  /* Disable SIGINT again */
95 
96  /* Pure neatnik-ism */
98 
99  return result;
100  }
101 #endif
102 
103  fputs(prompt, stdout);
104  fflush(stdout);
105  return gets_fromFile(stdin);
106 }
volatile bool sigint_interrupt_enabled
Definition: common.c:234
#define NULL
Definition: c.h:229
char * gets_fromFile(FILE *source)
Definition: input.c:187
PQExpBuffer tab_completion_query_buf
void initializeInput ( int  flags)

Definition at line 346 of file input.c.

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

Referenced by main().

347 {
348 #ifdef USE_READLINE
349  if (flags & 1)
350  {
351  const char *histfile;
352  char home[MAXPGPATH];
353 
354  useReadline = true;
355 
356  /* these two things must be done in this order: */
358  rl_initialize();
359 
360  useHistory = true;
361  using_history();
362  history_lines_added = 0;
363 
364  histfile = GetVariable(pset.vars, "HISTFILE");
365 
366  if (histfile == NULL)
367  {
368  char *envhist;
369 
370  envhist = getenv("PSQL_HISTORY");
371  if (envhist != NULL && strlen(envhist) > 0)
372  histfile = envhist;
373  }
374 
375  if (histfile == NULL)
376  {
377  if (get_home_path(home))
378  psql_history = psprintf("%s/%s", home, PSQLHISTORY);
379  }
380  else
381  {
382  psql_history = pg_strdup(histfile);
383  expand_tilde(&psql_history);
384  }
385 
386  if (psql_history)
387  {
388  read_history(psql_history);
389  decode_history();
390  }
391  }
392 #endif
393 
394  atexit(finishInput);
395 }
static void finishInput(void)
Definition: input.c:537
PsqlSettings pset
Definition: startup.c:33
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PSQLHISTORY
Definition: input.c:22
void expand_tilde(char **filename)
Definition: common.c:2064
#define MAXPGPATH
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:71
#define NULL
Definition: c.h:229
void initialize_readline(void)
bool get_home_path(char *ret_path)
Definition: path.c:807
VariableSpace vars
Definition: settings.h:116
void pg_append_history ( const char *  s,
PQExpBuffer  history_buf 
)

Definition at line 113 of file input.c.

References appendPQExpBufferChar(), and appendPQExpBufferStr().

Referenced by MainLoop().

114 {
115 #ifdef USE_READLINE
116  if (useHistory && s)
117  {
118  appendPQExpBufferStr(history_buf, s);
119  if (!s[0] || s[strlen(s) - 1] != '\n')
120  appendPQExpBufferChar(history_buf, '\n');
121  }
122 #endif
123 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
void pg_send_history ( PQExpBuffer  history_buf)

Definition at line 135 of file input.c.

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

Referenced by MainLoop().

136 {
137 #ifdef USE_READLINE
138  static char *prev_hist = NULL;
139 
140  char *s = history_buf->data;
141  int i;
142 
143  /* Trim any trailing \n's (OK to scribble on history_buf) */
144  for (i = strlen(s) - 1; i >= 0 && s[i] == '\n'; i--)
145  ;
146  s[i + 1] = '\0';
147 
148  if (useHistory && s[0])
149  {
150  if (((pset.histcontrol & hctl_ignorespace) &&
151  s[0] == ' ') ||
153  prev_hist && strcmp(s, prev_hist) == 0))
154  {
155  /* Ignore this line as far as history is concerned */
156  }
157  else
158  {
159  /* Save each previous line for ignoredups processing */
160  if (prev_hist)
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 }
PsqlSettings pset
Definition: startup.c:33
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
int i
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
HistControl histcontrol
Definition: settings.h:135
bool printHistory ( const char *  fname,
unsigned short int  pager 
)

Definition at line 490 of file input.c.

References ClosePager(), NULL, output(), PageOutput(), _psqlSettings::popt, pset, psql_error(), strerror(), and printQueryOpt::topt.

Referenced by exec_command().

491 {
492 #ifdef USE_READLINE
493  FILE *output;
494  bool is_pager;
495 
496  if (!useHistory)
497  return false;
498 
499  if (fname == NULL)
500  {
501  /* use pager, if enabled, when printing to console */
502  output = PageOutput(INT_MAX, pager ? &(pset.popt.topt) : NULL);
503  is_pager = true;
504  }
505  else
506  {
507  output = fopen(fname, "w");
508  if (output == NULL)
509  {
510  psql_error("could not save history to file \"%s\": %s\n",
511  fname, strerror(errno));
512  return false;
513  }
514  is_pager = false;
515  }
516 
517  BEGIN_ITERATE_HISTORY(cur_hist);
518  {
519  fprintf(output, "%s\n", cur_hist->line);
520  }
521  END_ITERATE_HISTORY();
522 
523  if (is_pager)
524  ClosePager(output);
525  else
526  fclose(output);
527 
528  return true;
529 #else
530  psql_error("history is not supported by this installation\n");
531  return false;
532 #endif
533 }
PsqlSettings pset
Definition: startup.c:33
static void output(uint64 loop_count)
void ClosePager(FILE *pagerpipe)
Definition: print.c:2900
printTableOpt topt
Definition: print.h:165
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:2851
void psql_error(const char *fmt,...)
Definition: common.c:181
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
const char * strerror(int errnum)
Definition: strerror.c:19