PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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

chargets_interactive (const char *prompt, PQExpBuffer query_buf)
 
chargets_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)
extern

Definition at line 186 of file input.c.

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

References appendPQExpBufferStr(), createPQExpBuffer(), PQExpBufferData::data, fb(), 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 
)
extern

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
83#endif
84
85 /* Make current query_buf available to tab completion callback */
87
88 /* Enable SIGINT to longjmp to sigint_interrupt_jmp */
90
91 result = readline(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}
char * gets_fromFile(FILE *source)
Definition input.c:186
PQExpBuffer tab_completion_query_buf

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

Referenced by MainLoop().

◆ initializeInput()

void initializeInput ( int  flags)
extern

Definition at line 344 of file input.c.

345{
346#ifdef USE_READLINE
347 if (flags & 1)
348 {
349 const char *histfile;
350 char home[MAXPGPATH];
351
352 useReadline = true;
353
354 /* set appropriate values for Readline's global variables */
356
357#ifdef HAVE_RL_VARIABLE_BIND
358 /* set comment-begin to a useful value for SQL */
359 (void) rl_variable_bind("comment-begin", "-- ");
360#endif
361
362 /* this reads ~/.inputrc, so do it after rl_variable_bind */
364
365 useHistory = true;
368
369 histfile = GetVariable(pset.vars, "HISTFILE");
370
371 if (histfile == NULL)
372 {
373 char *envhist;
374
375 envhist = getenv("PSQL_HISTORY");
376 if (envhist != NULL && strlen(envhist) > 0)
378 }
379
380 if (histfile == NULL)
381 {
382 if (get_home_path(home))
384 }
385 else
386 {
389 }
390
391 if (psql_history)
392 {
395 }
396 }
397#endif
398
400}
void expand_tilde(char **filename)
Definition common.c:2576
static void finishInput(void)
Definition input.c:540
#define PSQLHISTORY
Definition input.c:23
#define MAXPGPATH
bool get_home_path(char *ret_path)
Definition path.c:1005
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
PsqlSettings pset
Definition startup.c:32
VariableSpace vars
Definition settings.h:151
void initialize_readline(void)
const char * GetVariable(VariableSpace space, const char *name)
Definition variables.c:73

References expand_tilde(), fb(), 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 
)
extern

Definition at line 113 of file input.c.

114{
115#ifdef USE_READLINE
116 if (useHistory && s)
117 {
119 if (!s[0] || s[strlen(s) - 1] != '\n')
121 }
122#endif
123}
void appendPQExpBufferChar(PQExpBuffer str, char ch)

References appendPQExpBufferChar(), appendPQExpBufferStr(), and fb().

Referenced by MainLoop().

◆ pg_send_history()

void pg_send_history ( PQExpBuffer  history_buf)
extern

Definition at line 135 of file input.c.

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 {
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 */
161 prev_hist = pg_strdup(s);
162 /* And send it to readline */
163 add_history(s);
164 /* Count lines added to history for use later */
166 }
167 }
168
170#endif
171}
int i
Definition isn.c:77
@ hctl_ignoredups
Definition settings.h:90
@ hctl_ignorespace
Definition settings.h:89
#define free(a)
HistControl histcontrol
Definition settings.h:180

References fb(), 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 
)
extern

Definition at line 494 of file input.c.

495{
496#ifdef USE_READLINE
497 FILE *output;
498 bool is_pager;
499
500 if (!useHistory)
501 return false;
502
503 if (fname == NULL)
504 {
505 /* use pager, if enabled, when printing to console */
506 output = PageOutput(INT_MAX, pager ? &(pset.popt.topt) : NULL);
507 is_pager = true;
508 }
509 else
510 {
511 output = fopen(fname, "w");
512 if (output == NULL)
513 {
514 pg_log_error("could not save history to file \"%s\": %m", fname);
515 return false;
516 }
517 is_pager = false;
518 }
519
521 {
522 fprintf(output, "%s\n", cur_hist->line);
523 }
525
526 if (is_pager)
528 else
529 fclose(output);
530
531 return true;
532#else
533 pg_log_error("history is not supported by this installation");
534 return false;
535#endif
536}
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition print.c:3078
void ClosePager(FILE *pagerpipe)
Definition print.c:3160
FILE * output
printQueryOpt popt
Definition settings.h:112
printTableOpt topt
Definition print.h:185

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

Referenced by exec_command_s().