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))
167 history_lines_added++;
207 result = fgets(line,
sizeof(line),
source);
232 if (buffer->
len > 0 && buffer->
data[buffer->
len - 1] ==
'\n')
234 buffer->
data[buffer->
len - 1] =
'\0';
279 #define BEGIN_ITERATE_HISTORY(VARNAME) \
281 HIST_ENTRY *VARNAME; \
284 history_set_pos(0); \
285 use_prev_ = (previous_history() != NULL); \
286 history_set_pos(0); \
287 for (VARNAME = current_history(); VARNAME != NULL; \
288 VARNAME = use_prev_ ? previous_history() : next_history()) \
292 #define END_ITERATE_HISTORY() \
303 BEGIN_ITERATE_HISTORY(cur_hist);
308 for (cur_ptr = (
char *) cur_hist->line; *cur_ptr; cur_ptr++)
310 if (*cur_ptr ==
'\n')
311 *cur_ptr = NL_IN_HISTORY;
314 END_ITERATE_HISTORY();
323 BEGIN_ITERATE_HISTORY(cur_hist);
328 for (cur_ptr = (
char *) cur_hist->line; *cur_ptr; cur_ptr++)
330 if (*cur_ptr == NL_IN_HISTORY)
334 END_ITERATE_HISTORY();
351 const char *histfile;
359 #ifdef HAVE_RL_VARIABLE_BIND
361 (void) rl_variable_bind(
"comment-begin",
"-- ");
369 history_lines_added = 0;
373 if (histfile == NULL)
377 envhist = getenv(
"PSQL_HISTORY");
378 if (envhist != NULL && strlen(envhist) > 0)
382 if (histfile == NULL)
395 read_history(psql_history);
415 saveHistory(
char *fname,
int max_lines)
425 if (strcmp(fname,
DEVNULL) != 0)
442 #if defined(HAVE_HISTORY_TRUNCATE_FILE) && defined(HAVE_APPEND_HISTORY)
450 nlines =
Max(max_lines - history_lines_added, 0);
451 (void) history_truncate_file(fname, nlines);
454 fd = open(fname, O_CREAT | O_WRONLY |
PG_BINARY, 0600);
459 nlines =
Min(max_lines, history_lines_added);
461 nlines = history_lines_added;
462 errnum = append_history(nlines, fname);
470 stifle_history(max_lines);
472 errnum = write_history(fname);
478 pg_log_error(
"could not save history to file \"%s\": %m", fname);
513 output = fopen(fname,
"w");
516 pg_log_error(
"could not save history to file \"%s\": %m", fname);
522 BEGIN_ITERATE_HISTORY(cur_hist);
526 END_ITERATE_HISTORY();
535 pg_log_error(
"history is not supported by this installation");
545 if (useHistory && psql_history)
void expand_tilde(char **filename)
volatile bool 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
static void output(uint64 loop_count)
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)