2000-01-19 00:30:24 +01:00
|
|
|
/*
|
|
|
|
* psql - the PostgreSQL interactive terminal
|
|
|
|
*
|
2000-01-29 17:58:54 +01:00
|
|
|
* Copyright 2000 by PostgreSQL Global Development Group
|
2000-01-19 00:30:24 +01:00
|
|
|
*
|
2002-04-11 00:47:09 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.19 2002/04/10 22:46:58 petere Exp $
|
2000-01-19 00:30:24 +01:00
|
|
|
*/
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "postgres_fe.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
#include "input.h"
|
|
|
|
|
2000-02-08 00:10:11 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2000-02-16 14:15:26 +01:00
|
|
|
#include "pqexpbuffer.h"
|
* Includes tab completion. It's not magic, but it's very cool. At any
rate
it's better than what used to be there.
* Does proper SQL "host variable" substitution as pointed out by Andreas
Zeugwetter (thanks): select * from :foo; Also some changes in how ':'
and ';' are treated (escape with \ to send to backend). This does
_not_
affect the '::' cast operator, but perhaps others that contain : or ;
(but there are none right now).
* To show description with a <something> listing, append '?' to command
name, e.g., \df?. This seemed to be the convenient and logical
solution.
Or append a '+' to see more useless information, e.g., \df+.
* Fixed fflush()'ing bug pointed out by Jan during the regression test
discussion.
* Added LastOid variable. This ought to take care of TODO item "Add a
function to return the last inserted oid, for use in psql scripts"
(under CLIENTS)
E.g.,
insert into foo values(...);
insert into bar values(..., :LastOid);
\echo $LastOid
* \d command shows constraints, rules, and triggers defined on the table
(in addition to indices)
* Various fixes, optimizations, corrections
* Documentation update as well
Note: This now requires snprintf(), which, if necessary, is taken from
src/backend/port. This is certainly a little weird, but it should
suffice
until a source tree cleanup is done.
Enjoy.
--
Peter Eisentraut Sernanders väg 10:115
1999-11-26 05:24:17 +01:00
|
|
|
#include "settings.h"
|
|
|
|
#include "tab-complete.h"
|
2000-02-08 00:10:11 +01:00
|
|
|
#include "common.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
#endif
|
|
|
|
|
2001-09-12 01:08:07 +02:00
|
|
|
#ifdef HAVE_ATEXIT
|
2001-10-25 07:50:21 +02:00
|
|
|
static void finishInput(void);
|
|
|
|
|
2001-09-12 01:08:07 +02:00
|
|
|
#else
|
|
|
|
/* designed for use with on_exit() */
|
2001-10-25 07:50:21 +02:00
|
|
|
static void finishInput(int, void *);
|
2001-09-12 01:08:07 +02:00
|
|
|
#endif
|
|
|
|
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* gets_interactive()
|
|
|
|
*
|
|
|
|
* Gets a line of interactive input, using readline of desired.
|
|
|
|
* The result is malloced.
|
|
|
|
*/
|
|
|
|
char *
|
2000-02-08 00:10:11 +01:00
|
|
|
gets_interactive(char *prompt)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
char *s;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2002-04-11 00:47:09 +02:00
|
|
|
#ifdef USE_READLINE
|
2000-04-12 19:17:23 +02:00
|
|
|
const char *var;
|
|
|
|
static char *prev_hist = NULL;
|
2000-01-14 23:18:03 +01:00
|
|
|
#endif
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
#ifdef USE_READLINE
|
1999-11-05 00:14:30 +01:00
|
|
|
if (useReadline)
|
2000-02-08 00:10:11 +01:00
|
|
|
s = readline(prompt);
|
1999-11-05 00:14:30 +01:00
|
|
|
else
|
|
|
|
{
|
1999-11-04 22:56:02 +01:00
|
|
|
#endif
|
1999-11-05 00:14:30 +01:00
|
|
|
fputs(prompt, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
s = gets_fromFile(stdin);
|
1999-11-04 22:56:02 +01:00
|
|
|
#ifdef USE_READLINE
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
#endif
|
|
|
|
|
2002-04-11 00:47:09 +02:00
|
|
|
#ifdef USE_READLINE
|
2000-04-12 19:17:23 +02:00
|
|
|
if (useHistory && s && s[0] != '\0')
|
|
|
|
{
|
|
|
|
var = GetVariable(pset.vars, "HISTCONTROL");
|
|
|
|
if (!var || (var
|
|
|
|
&& !((strcmp(var, "ignorespace") == 0 || strcmp(var, "ignoreboth") == 0) && s[0] == ' ')
|
|
|
|
&& !((strcmp(var, "ignoredups") == 0 || strcmp(var, "ignoreboth") == 0) && prev_hist && strcmp(s, prev_hist) == 0)
|
|
|
|
))
|
|
|
|
{
|
|
|
|
free(prev_hist);
|
|
|
|
prev_hist = strdup(s);
|
|
|
|
add_history(s);
|
|
|
|
}
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
#endif
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
return s;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2000-11-27 03:20:36 +01:00
|
|
|
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-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (buffer.len > 0)
|
|
|
|
return buffer.data; /* EOF after reading some bufferload(s) */
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* EOF, so return null */
|
|
|
|
termPQExpBuffer(&buffer);
|
|
|
|
return NULL;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2000-01-14 23:18:03 +01:00
|
|
|
initializeInput(int flags)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
|
|
|
#ifdef USE_READLINE
|
1999-11-05 00:14:30 +01:00
|
|
|
if (flags == 1)
|
|
|
|
{
|
|
|
|
useReadline = true;
|
2000-04-12 19:17:23 +02:00
|
|
|
initialize_readline();
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
#endif
|
|
|
|
|
2002-04-11 00:47:09 +02:00
|
|
|
#ifdef USE_READLINE
|
1999-11-05 00:14:30 +01:00
|
|
|
if (flags == 1)
|
|
|
|
{
|
|
|
|
const char *home;
|
|
|
|
|
|
|
|
useHistory = true;
|
2000-04-12 19:17:23 +02:00
|
|
|
SetVariable(pset.vars, "HISTSIZE", "500");
|
1999-11-05 00:14:30 +01:00
|
|
|
using_history();
|
|
|
|
home = getenv("HOME");
|
|
|
|
if (home)
|
|
|
|
{
|
|
|
|
char *psql_history = (char *) malloc(strlen(home) + 20);
|
|
|
|
|
|
|
|
if (psql_history)
|
|
|
|
{
|
|
|
|
sprintf(psql_history, "%s/.psql_history", home);
|
|
|
|
read_history(psql_history);
|
|
|
|
free(psql_history);
|
|
|
|
}
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
#endif
|
2000-02-20 15:28:28 +01:00
|
|
|
|
2001-02-27 09:13:31 +01:00
|
|
|
#ifdef HAVE_ATEXIT
|
2000-04-12 19:17:23 +02:00
|
|
|
atexit(finishInput);
|
2001-02-27 09:13:31 +01:00
|
|
|
#else
|
2001-09-12 01:08:07 +02:00
|
|
|
on_exit(finishInput, NULL);
|
2001-02-27 09:13:31 +01:00
|
|
|
#endif
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2000-02-08 00:10:11 +01:00
|
|
|
saveHistory(char *fname)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2002-04-11 00:47:09 +02:00
|
|
|
#ifdef USE_READLINE
|
2000-02-08 00:10:11 +01:00
|
|
|
if (useHistory && fname)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-02-08 00:10:11 +01:00
|
|
|
if (write_history(fname) != 0)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2000-02-20 03:37:40 +01:00
|
|
|
psql_error("could not save history to %s: %s\n", fname, strerror(errno));
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
else
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
#else
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-12 01:08:07 +02:00
|
|
|
static void
|
|
|
|
#ifdef HAVE_ATEXIT
|
1999-11-04 22:56:02 +01:00
|
|
|
finishInput(void)
|
2001-09-12 01:08:07 +02:00
|
|
|
#else
|
|
|
|
finishInput(int exitstatus, void *arg)
|
|
|
|
#endif
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2002-04-11 00:47:09 +02:00
|
|
|
#ifdef USE_READLINE
|
1999-11-05 00:14:30 +01:00
|
|
|
if (useHistory)
|
|
|
|
{
|
|
|
|
char *home;
|
|
|
|
char *psql_history;
|
|
|
|
|
|
|
|
home = getenv("HOME");
|
|
|
|
if (home)
|
|
|
|
{
|
|
|
|
psql_history = (char *) malloc(strlen(home) + 20);
|
|
|
|
if (psql_history)
|
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
const char *var = GetVariable(pset.vars, "HISTSIZE");
|
|
|
|
|
|
|
|
if (var)
|
|
|
|
stifle_history(atoi(var));
|
1999-11-05 00:14:30 +01:00
|
|
|
sprintf(psql_history, "%s/.psql_history", home);
|
|
|
|
write_history(psql_history);
|
|
|
|
free(psql_history);
|
|
|
|
}
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|