2 * psql - the PostgreSQL interactive terminal
4 * Copyright 2000 by PostgreSQL Global Development Group
6 * $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.8 2000/01/29 16:58:48 petere Exp $
11 #include <pqexpbuffer.h>
14 #include "tab-complete.h"
16 /* Runtime options for turning off readline and history */
17 /* (of course there is no runtime command for doing that :) */
19 static bool useReadline;
23 static bool useHistory;
31 * Gets a line of interactive input, using readline of desired.
32 * The result is malloced.
35 gets_interactive(const char *prompt)
40 static char * prev_hist = NULL;
45 s = readline((char *) prompt);
49 fputs(prompt, stdout);
51 s = gets_fromFile(stdin);
57 if (useHistory && s && s[0] != '\0')
59 var = GetVariable(pset.vars, "HISTCONTROL");
61 && !((strcmp(var, "ignorespace") == 0 || strcmp(var, "ignoreboth") ==0) && s[0] == ' ' )
62 && !((strcmp(var, "ignoredups") == 0 || strcmp(var, "ignoreboth") ==0) && prev_hist && strcmp(s, prev_hist) == 0)
66 prev_hist = strdup(s);
80 * Gets a line of noninteractive input from a file (which could be stdin).
83 gets_fromFile(FILE *source)
85 PQExpBufferData buffer;
88 initPQExpBuffer(&buffer);
90 while (fgets(line, 1024, source) != NULL)
92 appendPQExpBufferStr(&buffer, line);
93 if (buffer.data[buffer.len - 1] == '\n')
95 buffer.data[buffer.len - 1] = '\0';
101 return buffer.data; /* EOF after reading some bufferload(s) */
103 /* EOF, so return null */
104 termPQExpBuffer(&buffer);
111 * Put any startup stuff related to input in here. It's good to maintain
112 * abstraction this way.
114 * The only "flag" right now is 1 for use readline & history.
117 initializeInput(int flags)
123 rl_readline_name = "psql";
124 initialize_readline();
134 SetVariable(pset.vars, "HISTSIZE", "500");
136 home = getenv("HOME");
139 char *psql_history = (char *) malloc(strlen(home) + 20);
143 sprintf(psql_history, "%s/.psql_history", home);
144 read_history(psql_history);
155 saveHistory(const char *fname)
160 if (write_history((char *) fname) != 0)
185 home = getenv("HOME");
188 psql_history = (char *) malloc(strlen(home) + 20);
191 const char * var = GetVariable(pset.vars, "HISTSIZE");
193 stifle_history(atoi(var));
194 sprintf(psql_history, "%s/.psql_history", home);
195 write_history(psql_history);