23 #define PSQLHISTORY ".psql_history"
25 #define PSQLHISTORY "psql_history"
31 static bool useReadline;
32 static bool useHistory;
34 static char *psql_history;
36 static int history_lines_added;
47 #define NL_IN_HISTORY 0x01
81 #ifdef HAVE_RL_RESET_SCREEN_SIZE
82 rl_reset_screen_size();
92 result = readline((
char *) prompt);
120 if (!s[0] || s[strlen(s) - 1] !=
'\n')
139 static char *prev_hist = NULL;
141 char *s = history_buf->
data;
145 for (
i = strlen(s) - 1;
i >= 0 && s[
i] ==
'\n';
i--)
149 if (useHistory && s[0])
154 prev_hist && strcmp(s, prev_hist) == 0))
166 history_lines_added++;
206 result = fgets(line,
sizeof(line),
source);
231 if (buffer->
len > 0 && buffer->
data[buffer->
len - 1] ==
'\n')
233 buffer->
data[buffer->
len - 1] =
'\0';
278 #define BEGIN_ITERATE_HISTORY(VARNAME) \
280 HIST_ENTRY *VARNAME; \
283 history_set_pos(0); \
284 use_prev_ = (previous_history() != NULL); \
285 history_set_pos(0); \
286 for (VARNAME = current_history(); VARNAME != NULL; \
287 VARNAME = use_prev_ ? previous_history() : next_history()) \
291 #define END_ITERATE_HISTORY() \
302 BEGIN_ITERATE_HISTORY(cur_hist);
307 for (cur_ptr = (
char *) cur_hist->line; *cur_ptr; cur_ptr++)
309 if (*cur_ptr ==
'\n')
310 *cur_ptr = NL_IN_HISTORY;
313 END_ITERATE_HISTORY();
322 BEGIN_ITERATE_HISTORY(cur_hist);
327 for (cur_ptr = (
char *) cur_hist->line; *cur_ptr; cur_ptr++)
329 if (*cur_ptr == NL_IN_HISTORY)
333 END_ITERATE_HISTORY();
350 const char *histfile;
358 #ifdef HAVE_RL_VARIABLE_BIND
360 (void) rl_variable_bind(
"comment-begin",
"-- ");
368 history_lines_added = 0;
372 if (histfile == NULL)
376 envhist = getenv(
"PSQL_HISTORY");
377 if (envhist != NULL && strlen(envhist) > 0)
381 if (histfile == NULL)
394 read_history(psql_history);
414 saveHistory(
char *fname,
int max_lines)
424 if (strcmp(fname,
DEVNULL) != 0)
441 #if defined(HAVE_HISTORY_TRUNCATE_FILE) && defined(HAVE_APPEND_HISTORY)
449 nlines =
Max(max_lines - history_lines_added, 0);
450 (void) history_truncate_file(fname, nlines);
453 fd = open(fname, O_CREAT | O_WRONLY |
PG_BINARY, 0600);
458 nlines =
Min(max_lines, history_lines_added);
460 nlines = history_lines_added;
461 errnum = append_history(nlines, fname);
469 stifle_history(max_lines);
471 errnum = write_history(fname);
477 pg_log_error(
"could not save history to file \"%s\": %m", fname);
512 output = fopen(fname,
"w");
515 pg_log_error(
"could not save history to file \"%s\": %m", fname);
521 BEGIN_ITERATE_HISTORY(cur_hist);
525 END_ITERATE_HISTORY();
534 pg_log_error(
"history is not supported by this installation");
544 if (useHistory && psql_history)
void expand_tilde(char **filename)
volatile sig_atomic_t sigint_interrupt_enabled
char * pg_strdup(const char *in)
void ClosePager(FILE *pagerpipe)
FILE * PageOutput(int lines, const printTableOpt *topt)
static void const char fflush(stdout)
#define pg_log_error(...)
static rewind_source * source
bool get_home_path(char *ret_path)
PQExpBuffer createPQExpBuffer(void)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
#define PQExpBufferBroken(str)
static int fd(const char *x, int i)
char * psprintf(const char *fmt,...)
void initialize_readline(void)
PQExpBuffer tab_completion_query_buf
const char * GetVariable(VariableSpace space, const char *name)