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();
91 result = readline(prompt);
119 if (!s[0] || s[strlen(s) - 1] !=
'\n')
138 static char *prev_hist = NULL;
140 char *s = history_buf->
data;
144 for (
i = strlen(s) - 1;
i >= 0 && s[
i] ==
'\n';
i--)
148 if (useHistory && s[0])
153 prev_hist && strcmp(s, prev_hist) == 0))
165 history_lines_added++;
205 result = fgets(line,
sizeof(line),
source);
230 if (buffer->
len > 0 && buffer->
data[buffer->
len - 1] ==
'\n')
232 buffer->
data[buffer->
len - 1] =
'\0';
277 #define BEGIN_ITERATE_HISTORY(VARNAME) \
279 HIST_ENTRY *VARNAME; \
282 history_set_pos(0); \
283 use_prev_ = (previous_history() != NULL); \
284 history_set_pos(0); \
285 for (VARNAME = current_history(); VARNAME != NULL; \
286 VARNAME = use_prev_ ? previous_history() : next_history()) \
290 #define END_ITERATE_HISTORY() \
301 BEGIN_ITERATE_HISTORY(cur_hist);
306 for (cur_ptr = (
char *) cur_hist->line; *cur_ptr; cur_ptr++)
308 if (*cur_ptr ==
'\n')
309 *cur_ptr = NL_IN_HISTORY;
312 END_ITERATE_HISTORY();
321 BEGIN_ITERATE_HISTORY(cur_hist);
326 for (cur_ptr = (
char *) cur_hist->line; *cur_ptr; cur_ptr++)
328 if (*cur_ptr == NL_IN_HISTORY)
332 END_ITERATE_HISTORY();
349 const char *histfile;
357 #ifdef HAVE_RL_VARIABLE_BIND
359 (void) rl_variable_bind(
"comment-begin",
"-- ");
367 history_lines_added = 0;
371 if (histfile == NULL)
375 envhist = getenv(
"PSQL_HISTORY");
376 if (envhist != NULL && strlen(envhist) > 0)
380 if (histfile == NULL)
393 read_history(psql_history);
413 saveHistory(
char *fname,
int max_lines)
423 if (strcmp(fname,
DEVNULL) != 0)
440 #if defined(HAVE_HISTORY_TRUNCATE_FILE) && defined(HAVE_APPEND_HISTORY)
448 nlines =
Max(max_lines - history_lines_added, 0);
449 (void) history_truncate_file(fname, nlines);
452 fd = open(fname, O_CREAT | O_WRONLY |
PG_BINARY, 0600);
457 nlines =
Min(max_lines, history_lines_added);
459 nlines = history_lines_added;
460 errnum = append_history(nlines, fname);
468 stifle_history(max_lines);
470 errnum = write_history(fname);
476 pg_log_error(
"could not save history to file \"%s\": %m", fname);
511 output = fopen(fname,
"w");
514 pg_log_error(
"could not save history to file \"%s\": %m", fname);
520 BEGIN_ITERATE_HISTORY(cur_hist);
524 END_ITERATE_HISTORY();
533 pg_log_error(
"history is not supported by this installation");
543 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)