5 #include <pqexpbuffer.h>
8 #include "tab-complete.h"
10 /* Runtime options for turning off readline and history */
11 /* (of course there is no runtime command for doing that :) */
13 static bool useReadline;
17 static bool useHistory;
25 * Gets a line of interactive input, using readline of desired.
26 * The result is malloced.
29 gets_interactive(const char *prompt)
39 fputs(prompt, stdout);
41 s = gets_fromFile(stdin);
47 if (useHistory && s && s[0] != '\0')
59 * Gets a line of noninteractive input from a file (which could be stdin).
62 gets_fromFile(FILE *source)
64 PQExpBufferData buffer;
67 initPQExpBuffer(&buffer);
69 while (fgets(line, 1024, source) != NULL)
71 appendPQExpBufferStr(&buffer, line);
72 if (buffer.data[buffer.len - 1] == '\n')
74 buffer.data[buffer.len - 1] = '\0';
80 return buffer.data; /* EOF after reading some bufferload(s) */
82 /* EOF, so return null */
83 termPQExpBuffer(&buffer);
90 * Put any startup stuff related to input in here. It's good to maintain
91 * abstraction this way.
93 * The only "flag" right now is 1 for use readline & history.
96 initializeInput(int flags, PsqlSettings *pset)
102 rl_readline_name = "psql";
103 initialize_readline(&(pset->db));
114 home = getenv("HOME");
117 char *psql_history = (char *) malloc(strlen(home) + 20);
121 sprintf(psql_history, "%s/.psql_history", home);
122 read_history(psql_history);
133 saveHistory(const char *fname)
138 if (write_history(fname) != 0)
163 home = getenv("HOME");
166 psql_history = (char *) malloc(strlen(home) + 20);
169 sprintf(psql_history, "%s/.psql_history", home);
170 write_history(psql_history);