postgresql/src/bin/psql/input.c

253 lines
4.4 KiB
C
Raw Normal View History

2000-01-19 00:30:24 +01:00
/*
* psql - the PostgreSQL interactive terminal
*
2004-08-29 06:13:13 +02:00
* Copyright (c) 2000-2004, PostgreSQL Global Development Group
2000-01-19 00:30:24 +01:00
*
2004-10-12 23:54:45 +02:00
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.41 2004/10/12 21:54:44 petere Exp $
2000-01-19 00:30:24 +01:00
*/
#include "postgres_fe.h"
#include "input.h"
#include <errno.h>
2000-02-16 14:15:26 +01:00
#include "pqexpbuffer.h"
#include "settings.h"
#include "tab-complete.h"
#include "common.h"
/* Runtime options for turning off readline and history */
/* (of course there is no runtime command for doing that :) */
#ifdef USE_READLINE
static bool useReadline;
static bool useHistory;
enum histcontrol
{
2003-08-04 02:43:34 +02:00
hctl_none = 0,
hctl_ignorespace = 1,
hctl_ignoredups = 2,
hctl_ignoreboth = hctl_ignorespace | hctl_ignoredups
};
#endif
#ifdef HAVE_ATEXIT
static void finishInput(void);
#else
/* designed for use with on_exit() */
static void finishInput(int, void *);
#endif
2003-08-04 02:43:34 +02:00
#define PSQLHISTORY ".psql_history"
#ifdef USE_READLINE
static enum histcontrol
GetHistControlConfig(void)
{
enum histcontrol HC;
const char *var;
var = GetVariable(pset.vars, "HISTCONTROL");
2003-08-04 02:43:34 +02:00
if (!var)
HC = hctl_none;
else if (strcmp(var, "ignorespace") == 0)
HC = hctl_ignorespace;
else if (strcmp(var, "ignoredups") == 0)
HC = hctl_ignoredups;
else if (strcmp(var, "ignoreboth") == 0)
HC = hctl_ignoreboth;
else
HC = hctl_none;
return HC;
}
#endif
static char *
gets_basic(const char prompt[])
{
fputs(prompt, stdout);
fflush(stdout);
return gets_fromFile(stdin);
}
/*
* gets_interactive()
*
* Gets a line of interactive input, using readline of desired.
* The result is malloc'ed.
*/
char *
gets_interactive(const char *prompt)
{
#ifdef USE_READLINE
1999-11-05 00:14:30 +01:00
char *s;
static char *prev_hist = NULL;
1999-11-05 00:14:30 +01:00
if (useReadline)
/* On some platforms, readline is declared as readline(char *) */
s = readline((char *) prompt);
1999-11-05 00:14:30 +01:00
else
s = gets_basic(prompt);
if (useHistory && s && s[0])
1999-11-05 00:14:30 +01:00
{
enum histcontrol HC;
HC = GetHistControlConfig();
if (((HC & hctl_ignorespace) && s[0] == ' ') ||
2003-08-04 02:43:34 +02:00
((HC & hctl_ignoredups) && prev_hist && strcmp(s, prev_hist) == 0))
{
/* Ignore this line as far as history is concerned */
}
else
{
free(prev_hist);
prev_hist = pg_strdup(s);
add_history(s);
}
}
1999-11-05 00:14:30 +01:00
return s;
#else
return gets_basic(prompt);
#endif
}
/*
* gets_fromFile
*
* Gets a line of noninteractive input from a file (which could be stdin).
*/
char *
gets_fromFile(FILE *source)
{
1999-11-05 00:14:30 +01:00
PQExpBufferData buffer;
char line[1024];
initPQExpBuffer(&buffer);
while (fgets(line, sizeof(line), source) != NULL)
1999-11-05 00:14:30 +01:00
{
appendPQExpBufferStr(&buffer, line);
if (buffer.data[buffer.len - 1] == '\n')
{
buffer.data[buffer.len - 1] = '\0';
return buffer.data;
}
}
1999-11-05 00:14:30 +01:00
if (buffer.len > 0)
return buffer.data; /* EOF after reading some bufferload(s) */
1999-11-05 00:14:30 +01:00
/* EOF, so return null */
termPQExpBuffer(&buffer);
return NULL;
}
/*
* Put any startup stuff related to input in here. It's good to maintain
* abstraction this way.
*
* The only "flag" right now is 1 for use readline & history.
*/
void
initializeInput(int flags)
{
#ifdef USE_READLINE
if (flags & 1)
1999-11-05 00:14:30 +01:00
{
2004-08-29 07:07:03 +02:00
char home[MAXPGPATH];
1999-11-05 00:14:30 +01:00
useReadline = true;
initialize_readline();
1999-11-05 00:14:30 +01:00
useHistory = true;
if (GetVariable(pset.vars, "HISTSIZE") == NULL)
SetVariable(pset.vars, "HISTSIZE", "500");
1999-11-05 00:14:30 +01:00
using_history();
if (get_home_path(home))
1999-11-05 00:14:30 +01:00
{
2004-08-29 07:07:03 +02:00
char *psql_history;
psql_history = pg_malloc(strlen(home) + 1 +
strlen(PSQLHISTORY) + 1);
sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
read_history(psql_history);
free(psql_history);
1999-11-05 00:14:30 +01:00
}
}
#endif
2001-02-27 09:13:31 +01:00
#ifdef HAVE_ATEXIT
atexit(finishInput);
2001-02-27 09:13:31 +01:00
#else
on_exit(finishInput, NULL);
2001-02-27 09:13:31 +01:00
#endif
}
bool
saveHistory(char *fname)
{
#ifdef USE_READLINE
if (useHistory && fname)
1999-11-05 00:14:30 +01:00
{
if (write_history(fname) == 0)
2003-08-04 02:43:34 +02:00
return true;
psql_error("could not save history to file \"%s\": %s\n", fname, strerror(errno));
}
#else
2004-10-12 23:54:45 +02:00
psql_error("history is not supported by this installation\n");
#endif
return false;
}
static void
#ifdef HAVE_ATEXIT
finishInput(void)
#else
finishInput(int exitstatus, void *arg)
#endif
{
#ifdef USE_READLINE
1999-11-05 00:14:30 +01:00
if (useHistory)
{
2004-08-29 07:07:03 +02:00
char home[MAXPGPATH];
1999-11-05 00:14:30 +01:00
if (get_home_path(home))
1999-11-05 00:14:30 +01:00
{
2004-08-29 07:07:03 +02:00
char *psql_history;
int hist_size;
psql_history = pg_malloc(strlen(home) + 1 +
strlen(PSQLHISTORY) + 1);
2003-08-04 02:43:34 +02:00
hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);
if (hist_size >= 0)
stifle_history(hist_size);
sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
write_history(psql_history);
free(psql_history);
1999-11-05 00:14:30 +01:00
}
}
#endif
}