2
* psql - the PostgreSQL interactive terminal
4
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
6
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.43 2005-01-06 18:29:09 tgl Exp $
8
#include "postgres_fe.h"
11
#include "pqexpbuffer.h"
13
#include "tab-complete.h"
17
#define PSQLHISTORY ".psql_history"
19
#define PSQLHISTORY "psql_history"
22
/* Runtime options for turning off readline and history */
23
/* (of course there is no runtime command for doing that :) */
25
static bool useReadline;
26
static bool useHistory;
33
hctl_ignoreboth = hctl_ignorespace | hctl_ignoredups
38
static void finishInput(void);
40
/* designed for use with on_exit() */
41
static void finishInput(int, void *);
46
static enum histcontrol
47
GetHistControlConfig(void)
52
var = GetVariable(pset.vars, "HISTCONTROL");
56
else if (strcmp(var, "ignorespace") == 0)
57
HC = hctl_ignorespace;
58
else if (strcmp(var, "ignoredups") == 0)
60
else if (strcmp(var, "ignoreboth") == 0)
71
gets_basic(const char prompt[])
73
fputs(prompt, stdout);
75
return gets_fromFile(stdin);
82
* Gets a line of interactive input, using readline of desired.
83
* The result is malloc'ed.
86
gets_interactive(const char *prompt)
91
static char *prev_hist = NULL;
94
/* On some platforms, readline is declared as readline(char *) */
95
s = readline((char *) prompt);
97
s = gets_basic(prompt);
99
if (useHistory && s && s[0])
103
HC = GetHistControlConfig();
105
if (((HC & hctl_ignorespace) && s[0] == ' ') ||
106
((HC & hctl_ignoredups) && prev_hist && strcmp(s, prev_hist) == 0))
108
/* Ignore this line as far as history is concerned */
113
prev_hist = pg_strdup(s);
120
return gets_basic(prompt);
129
* Gets a line of noninteractive input from a file (which could be stdin).
132
gets_fromFile(FILE *source)
134
PQExpBufferData buffer;
137
initPQExpBuffer(&buffer);
139
while (fgets(line, sizeof(line), source) != NULL)
141
appendPQExpBufferStr(&buffer, line);
142
if (buffer.data[buffer.len - 1] == '\n')
144
buffer.data[buffer.len - 1] = '\0';
150
return buffer.data; /* EOF after reading some bufferload(s) */
152
/* EOF, so return null */
153
termPQExpBuffer(&buffer);
160
* Put any startup stuff related to input in here. It's good to maintain
161
* abstraction this way.
163
* The only "flag" right now is 1 for use readline & history.
166
initializeInput(int flags)
171
char home[MAXPGPATH];
174
initialize_readline();
177
if (GetVariable(pset.vars, "HISTSIZE") == NULL)
178
SetVariable(pset.vars, "HISTSIZE", "500");
180
if (get_home_path(home))
184
psql_history = pg_malloc(strlen(home) + 1 +
185
strlen(PSQLHISTORY) + 1);
186
sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
187
read_history(psql_history);
196
on_exit(finishInput, NULL);
203
saveHistory(char *fname)
206
if (useHistory && fname)
208
if (write_history(fname) == 0)
211
psql_error("could not save history to file \"%s\": %s\n", fname, strerror(errno));
214
psql_error("history is not supported by this installation\n");
226
finishInput(int exitstatus, void *arg)
232
char home[MAXPGPATH];
234
if (get_home_path(home))
239
hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);
241
stifle_history(hist_size);
243
psql_history = pg_malloc(strlen(home) + 1 +
244
strlen(PSQLHISTORY) + 1);
245
sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
246
write_history(psql_history);