2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2006, PostgreSQL Global Development Group
6 * $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.60 2006/10/04 00:30:06 momjian Exp $
8 #include "postgres_fe.h"
12 #include "tab-complete.h"
16 #define PSQLHISTORY ".psql_history"
18 #define PSQLHISTORY "psql_history"
21 /* Runtime options for turning off readline and history */
22 /* (of course there is no runtime command for doing that :) */
24 static bool useReadline;
25 static bool useHistory;
29 * Preserve newlines in saved queries by mapping '\n' to NL_IN_HISTORY
31 * It is assumed NL_IN_HISTORY will never be entered by the user
32 * nor appear inside a multi-byte string. 0x00 is not properly
33 * handled by the readline routines so it can not be used
36 #define NL_IN_HISTORY 0x01
40 static void finishInput(void);
42 /* designed for use with on_exit() */
43 static void finishInput(int, void *);
50 * Gets a line of interactive input, using readline if desired.
51 * The result is a malloc'd string.
53 * Caller *must* have set up sigint_interrupt_jmp before calling.
56 gets_interactive(const char *prompt)
63 /* Enable SIGINT to longjmp to sigint_interrupt_jmp */
64 sigint_interrupt_enabled = true;
66 /* On some platforms, readline is declared as readline(char *) */
67 result = readline((char *) prompt);
69 /* Disable SIGINT again */
70 sigint_interrupt_enabled = false;
76 fputs(prompt, stdout);
78 return gets_fromFile(stdin);
83 * Append the line to the history buffer, making sure there is a trailing '\n'
86 pg_append_history(const char *s, PQExpBuffer history_buf)
89 if (useHistory && s && s[0])
91 appendPQExpBufferStr(history_buf, s);
92 if (s[strlen(s) - 1] != '\n')
93 appendPQExpBufferChar(history_buf, '\n');
100 * Emit accumulated history entry to readline's history mechanism,
101 * then reset the buffer to empty.
103 * Note: we write nothing if history_buf is empty, so extra calls to this
104 * function don't hurt. There must have been at least one line added by
105 * pg_append_history before we'll do anything.
108 pg_send_history(PQExpBuffer history_buf)
111 static char *prev_hist = NULL;
113 char *s = history_buf->data;
115 if (useHistory && s[0])
117 if (((pset.histcontrol & hctl_ignorespace) &&
119 ((pset.histcontrol & hctl_ignoredups) &&
120 prev_hist && strcmp(s, prev_hist) == 0))
122 /* Ignore this line as far as history is concerned */
128 /* Trim any trailing \n's (OK to scribble on history_buf) */
129 for (i = strlen(s) - 1; i >= 0 && s[i] == '\n'; i--)
132 /* Save each previous line for ignoredups processing */
135 prev_hist = pg_strdup(s);
136 /* And send it to readline */
141 resetPQExpBuffer(history_buf);
149 * Gets a line of noninteractive input from a file (which could be stdin).
150 * The result is a malloc'd string.
152 * Caller *must* have set up sigint_interrupt_jmp before calling.
154 * Note: we re-use a static PQExpBuffer for each call. This is to avoid
155 * leaking memory if interrupted by SIGINT.
158 gets_fromFile(FILE *source)
160 static PQExpBuffer buffer = NULL;
164 if (buffer == NULL) /* first time through? */
165 buffer = createPQExpBuffer();
167 resetPQExpBuffer(buffer);
173 /* Enable SIGINT to longjmp to sigint_interrupt_jmp */
174 sigint_interrupt_enabled = true;
177 result = fgets(line, sizeof(line), source);
179 /* Disable SIGINT again */
180 sigint_interrupt_enabled = false;
186 appendPQExpBufferStr(buffer, line);
189 if (buffer->data[buffer->len - 1] == '\n')
191 buffer->data[buffer->len - 1] = '\0';
192 return pg_strdup(buffer->data);
196 if (buffer->len > 0) /* EOF after reading some bufferload(s) */
197 return pg_strdup(buffer->data);
199 /* EOF, so return null */
206 * Convert newlines to NL_IN_HISTORY for safe saving in readline history file
211 HIST_ENTRY *cur_hist;
215 for (cur_hist = current_history(); cur_hist; cur_hist = next_history())
217 /* some platforms declare HIST_ENTRY.line as const char * */
218 for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
219 if (*cur_ptr == '\n')
220 *cur_ptr = NL_IN_HISTORY;
225 * Reverse the above encoding
230 HIST_ENTRY *cur_hist;
234 for (cur_hist = current_history(); cur_hist; cur_hist = next_history())
236 /* some platforms declare HIST_ENTRY.line as const char * */
237 for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
238 if (*cur_ptr == NL_IN_HISTORY)
242 #endif /* USE_READLINE */
246 * Put any startup stuff related to input in here. It's good to maintain
247 * abstraction this way.
249 * The only "flag" right now is 1 for use readline & history.
252 initializeInput(int flags)
257 const char *histfile;
258 char home[MAXPGPATH];
261 initialize_readline();
266 histfile = GetVariable(pset.vars, "HISTFILE");
267 if (histfile == NULL)
269 if (get_home_path(home))
271 psql_history = pg_malloc(strlen(home) + 1 +
272 strlen(PSQLHISTORY) + 1);
273 snprintf(psql_history, MAXPGPATH, "%s/%s", home, PSQLHISTORY);
278 psql_history = pg_strdup(histfile);
279 expand_tilde(&psql_history);
284 read_history(psql_history);
293 on_exit(finishInput, NULL);
299 * This function is for saving the readline history when user
300 * runs \s command or when psql finishes.
302 * We have an argument named encodeFlag to handle the cases differently.
303 * In case of call via \s we don't really need to encode \n as \x01,
304 * but when we save history for Readline we must do that conversion.
307 saveHistory(char *fname, bool encodeFlag)
312 * Suppressing the write attempt when HISTFILE is set to /dev/null may
313 * look like a negligible optimization, but it's necessary on e.g. Darwin,
314 * where write_history will fail because it tries to chmod the target
317 if (useHistory && fname &&
318 strcmp(fname, DEVNULL) != 0)
324 * return value of write_history is not standardized across GNU
325 * readline and libedit. Therefore, check for errno becoming set to
326 * see if the write failed.
329 (void) write_history(fname);
333 psql_error("could not save history to file \"%s\": %s\n",
334 fname, strerror(errno));
337 /* only get here in \s case, so complain */
338 psql_error("history is not supported by this installation\n");
349 finishInput(int exitstatus, void *arg)
353 if (useHistory && psql_history)
357 hist_size = GetVariableNum(pset.vars, "HISTSIZE", 500, -1, true);
359 stifle_history(hist_size);
361 saveHistory(psql_history, true);