2000-01-19 00:30:24 +01:00
|
|
|
/*
|
|
|
|
* psql - the PostgreSQL interactive terminal
|
|
|
|
*
|
2012-01-02 00:01:58 +01:00
|
|
|
* Copyright (c) 2000-2012, PostgreSQL Global Development Group
|
2000-01-19 00:30:24 +01:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/bin/psql/input.c
|
2000-01-19 00:30:24 +01:00
|
|
|
*/
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "postgres_fe.h"
|
2000-02-08 00:10:11 +01:00
|
|
|
|
2009-09-14 00:18:22 +02:00
|
|
|
#ifndef WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2006-02-11 22:55:35 +01:00
|
|
|
#include "input.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
|
|
|
|
2005-01-06 19:29:11 +01:00
|
|
|
#ifndef WIN32
|
|
|
|
#define PSQLHISTORY ".psql_history"
|
|
|
|
#else
|
|
|
|
#define PSQLHISTORY "psql_history"
|
|
|
|
#endif
|
|
|
|
|
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;
|
2009-09-14 00:18:22 +02:00
|
|
|
|
|
|
|
static char *psql_history;
|
|
|
|
|
|
|
|
static int history_lines_added;
|
|
|
|
|
2005-06-10 17:34:26 +02:00
|
|
|
|
2006-02-12 06:24:38 +01:00
|
|
|
/*
|
|
|
|
* Preserve newlines in saved queries by mapping '\n' to NL_IN_HISTORY
|
|
|
|
*
|
|
|
|
* It is assumed NL_IN_HISTORY will never be entered by the user
|
2006-10-04 02:30:14 +02:00
|
|
|
* nor appear inside a multi-byte string. 0x00 is not properly
|
2006-02-12 06:24:38 +01:00
|
|
|
* handled by the readline routines so it can not be used
|
|
|
|
* for this purpose.
|
|
|
|
*/
|
|
|
|
#define NL_IN_HISTORY 0x01
|
1999-11-04 22:56:02 +01:00
|
|
|
#endif
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
static void finishInput(void);
|
2001-09-12 01:08:07 +02:00
|
|
|
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* gets_interactive()
|
|
|
|
*
|
2006-06-12 01:06:00 +02:00
|
|
|
* Gets a line of interactive input, using readline if desired.
|
2006-06-14 18:49:03 +02:00
|
|
|
* The result is a malloc'd string.
|
|
|
|
*
|
|
|
|
* Caller *must* have set up sigint_interrupt_jmp before calling.
|
1999-11-04 22:56:02 +01:00
|
|
|
*/
|
|
|
|
char *
|
2003-03-20 07:00:12 +01:00
|
|
|
gets_interactive(const char *prompt)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2003-03-20 07:00:12 +01:00
|
|
|
#ifdef USE_READLINE
|
1999-11-05 00:14:30 +01:00
|
|
|
if (useReadline)
|
2006-06-12 01:06:00 +02:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
char *result;
|
2006-06-14 18:49:03 +02:00
|
|
|
|
|
|
|
/* Enable SIGINT to longjmp to sigint_interrupt_jmp */
|
|
|
|
sigint_interrupt_enabled = true;
|
|
|
|
|
2003-07-25 21:27:06 +02:00
|
|
|
/* On some platforms, readline is declared as readline(char *) */
|
2006-06-14 18:49:03 +02:00
|
|
|
result = readline((char *) prompt);
|
|
|
|
|
|
|
|
/* Disable SIGINT again */
|
|
|
|
sigint_interrupt_enabled = false;
|
|
|
|
|
|
|
|
return result;
|
2006-06-12 01:06:00 +02:00
|
|
|
}
|
2006-02-11 22:55:35 +01:00
|
|
|
#endif
|
2006-06-12 01:06:00 +02:00
|
|
|
|
|
|
|
fputs(prompt, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
return gets_fromFile(stdin);
|
2006-02-11 22:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-12 01:06:00 +02:00
|
|
|
/*
|
|
|
|
* Append the line to the history buffer, making sure there is a trailing '\n'
|
|
|
|
*/
|
2006-02-13 18:09:25 +01:00
|
|
|
void
|
2006-06-12 01:06:00 +02:00
|
|
|
pg_append_history(const char *s, PQExpBuffer history_buf)
|
2006-02-11 22:55:35 +01:00
|
|
|
{
|
|
|
|
#ifdef USE_READLINE
|
2011-11-16 02:34:47 +01:00
|
|
|
if (useHistory && s)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2006-06-12 01:06:00 +02:00
|
|
|
appendPQExpBufferStr(history_buf, s);
|
2011-11-16 02:34:47 +01:00
|
|
|
if (!s[0] || s[strlen(s) - 1] != '\n')
|
2006-02-11 22:55:35 +01:00
|
|
|
appendPQExpBufferChar(history_buf, '\n');
|
2006-10-04 02:30:14 +02:00
|
|
|
}
|
|
|
|
#endif
|
2006-02-11 22:55:35 +01:00
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-02-11 22:55:35 +01:00
|
|
|
|
2006-03-06 05:45:21 +01:00
|
|
|
/*
|
2006-06-12 01:06:00 +02:00
|
|
|
* Emit accumulated history entry to readline's history mechanism,
|
|
|
|
* then reset the buffer to empty.
|
|
|
|
*
|
|
|
|
* Note: we write nothing if history_buf is empty, so extra calls to this
|
|
|
|
* function don't hurt. There must have been at least one line added by
|
|
|
|
* pg_append_history before we'll do anything.
|
2006-03-06 05:45:21 +01:00
|
|
|
*/
|
2006-02-13 18:09:25 +01:00
|
|
|
void
|
2006-06-12 01:06:00 +02:00
|
|
|
pg_send_history(PQExpBuffer history_buf)
|
2006-02-11 22:55:35 +01:00
|
|
|
{
|
2006-03-06 05:45:21 +01:00
|
|
|
#ifdef USE_READLINE
|
2006-06-12 01:06:00 +02:00
|
|
|
static char *prev_hist = NULL;
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
char *s = history_buf->data;
|
2006-12-24 20:14:28 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Trim any trailing \n's (OK to scribble on history_buf) */
|
|
|
|
for (i = strlen(s) - 1; i >= 0 && s[i] == '\n'; i--)
|
|
|
|
;
|
|
|
|
s[i + 1] = '\0';
|
2006-06-12 01:06:00 +02:00
|
|
|
|
|
|
|
if (useHistory && s[0])
|
2006-02-11 22:55:35 +01:00
|
|
|
{
|
2006-08-29 17:19:51 +02:00
|
|
|
if (((pset.histcontrol & hctl_ignorespace) &&
|
|
|
|
s[0] == ' ') ||
|
|
|
|
((pset.histcontrol & hctl_ignoredups) &&
|
|
|
|
prev_hist && strcmp(s, prev_hist) == 0))
|
2003-08-04 02:43:34 +02:00
|
|
|
{
|
|
|
|
/* Ignore this line as far as history is concerned */
|
2003-03-20 07:00:12 +01:00
|
|
|
}
|
|
|
|
else
|
2000-04-12 19:17:23 +02:00
|
|
|
{
|
2006-06-12 01:06:00 +02:00
|
|
|
/* Save each previous line for ignoredups processing */
|
|
|
|
if (prev_hist)
|
|
|
|
free(prev_hist);
|
2004-01-25 04:07:22 +01:00
|
|
|
prev_hist = pg_strdup(s);
|
2006-06-12 01:06:00 +02:00
|
|
|
/* And send it to readline */
|
2000-04-12 19:17:23 +02:00
|
|
|
add_history(s);
|
2009-09-14 00:18:22 +02:00
|
|
|
/* Count lines added to history for use later */
|
|
|
|
history_lines_added++;
|
2000-04-12 19:17:23 +02:00
|
|
|
}
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-06-12 01:06:00 +02:00
|
|
|
resetPQExpBuffer(history_buf);
|
2006-02-11 22:55:35 +01:00
|
|
|
#endif
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gets_fromFile
|
|
|
|
*
|
|
|
|
* Gets a line of noninteractive input from a file (which could be stdin).
|
2007-11-28 10:17:46 +01:00
|
|
|
* The result is a malloc'd string, or NULL on EOF or input error.
|
2006-06-14 18:49:03 +02:00
|
|
|
*
|
|
|
|
* Caller *must* have set up sigint_interrupt_jmp before calling.
|
|
|
|
*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Note: we re-use a static PQExpBuffer for each call. This is to avoid
|
2006-06-14 18:49:03 +02:00
|
|
|
* leaking memory if interrupted by SIGINT.
|
1999-11-04 22:56:02 +01:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
gets_fromFile(FILE *source)
|
|
|
|
{
|
2006-06-14 18:49:03 +02:00
|
|
|
static PQExpBuffer buffer = NULL;
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
char line[1024];
|
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
if (buffer == NULL) /* first time through? */
|
|
|
|
buffer = createPQExpBuffer();
|
|
|
|
else
|
|
|
|
resetPQExpBuffer(buffer);
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
for (;;)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
char *result;
|
2006-06-14 18:49:03 +02:00
|
|
|
|
|
|
|
/* Enable SIGINT to longjmp to sigint_interrupt_jmp */
|
|
|
|
sigint_interrupt_enabled = true;
|
|
|
|
|
|
|
|
/* Get some data */
|
|
|
|
result = fgets(line, sizeof(line), source);
|
|
|
|
|
|
|
|
/* Disable SIGINT again */
|
|
|
|
sigint_interrupt_enabled = false;
|
|
|
|
|
2007-11-28 10:17:46 +01:00
|
|
|
/* EOF or error? */
|
2006-06-14 18:49:03 +02:00
|
|
|
if (result == NULL)
|
2007-11-28 10:17:46 +01:00
|
|
|
{
|
|
|
|
if (ferror(source))
|
|
|
|
{
|
2008-11-26 01:26:23 +01:00
|
|
|
psql_error("could not read from input file: %s\n",
|
|
|
|
strerror(errno));
|
2007-11-28 10:17:46 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-06-14 18:49:03 +02:00
|
|
|
break;
|
2007-11-28 10:17:46 +01:00
|
|
|
}
|
2006-06-14 18:49:03 +02:00
|
|
|
|
|
|
|
appendPQExpBufferStr(buffer, line);
|
|
|
|
|
2008-11-26 01:26:23 +01:00
|
|
|
if (PQExpBufferBroken(buffer))
|
|
|
|
{
|
|
|
|
psql_error("out of memory\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
/* EOL? */
|
|
|
|
if (buffer->data[buffer->len - 1] == '\n')
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2006-06-14 18:49:03 +02:00
|
|
|
buffer->data[buffer->len - 1] = '\0';
|
|
|
|
return pg_strdup(buffer->data);
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
if (buffer->len > 0) /* EOF after reading some bufferload(s) */
|
|
|
|
return pg_strdup(buffer->data);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* EOF, so return null */
|
|
|
|
return NULL;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-13 15:57:15 +01:00
|
|
|
#ifdef USE_READLINE
|
2006-06-12 01:06:00 +02:00
|
|
|
/*
|
|
|
|
* Convert newlines to NL_IN_HISTORY for safe saving in readline history file
|
|
|
|
*/
|
2006-02-13 18:09:25 +01:00
|
|
|
static void
|
|
|
|
encode_history(void)
|
2006-02-11 22:55:35 +01:00
|
|
|
{
|
|
|
|
HIST_ENTRY *cur_hist;
|
2006-10-04 02:30:14 +02:00
|
|
|
char *cur_ptr;
|
2006-02-11 22:55:35 +01:00
|
|
|
|
2006-06-18 19:30:48 +02:00
|
|
|
history_set_pos(0);
|
|
|
|
for (cur_hist = current_history(); cur_hist; cur_hist = next_history())
|
|
|
|
{
|
|
|
|
/* some platforms declare HIST_ENTRY.line as const char * */
|
|
|
|
for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
|
2006-02-11 22:55:35 +01:00
|
|
|
if (*cur_ptr == '\n')
|
2006-02-12 06:24:38 +01:00
|
|
|
*cur_ptr = NL_IN_HISTORY;
|
2006-06-18 19:30:48 +02:00
|
|
|
}
|
2006-02-11 22:55:35 +01:00
|
|
|
}
|
|
|
|
|
2006-06-12 01:06:00 +02:00
|
|
|
/*
|
|
|
|
* Reverse the above encoding
|
|
|
|
*/
|
2006-02-13 18:09:25 +01:00
|
|
|
static void
|
|
|
|
decode_history(void)
|
2006-02-11 22:55:35 +01:00
|
|
|
{
|
|
|
|
HIST_ENTRY *cur_hist;
|
2006-10-04 02:30:14 +02:00
|
|
|
char *cur_ptr;
|
2006-02-11 22:55:35 +01:00
|
|
|
|
2006-06-18 19:30:48 +02:00
|
|
|
history_set_pos(0);
|
|
|
|
for (cur_hist = current_history(); cur_hist; cur_hist = next_history())
|
|
|
|
{
|
|
|
|
/* some platforms declare HIST_ENTRY.line as const char * */
|
|
|
|
for (cur_ptr = (char *) cur_hist->line; *cur_ptr; cur_ptr++)
|
2006-02-12 06:24:38 +01:00
|
|
|
if (*cur_ptr == NL_IN_HISTORY)
|
2006-02-11 22:55:35 +01:00
|
|
|
*cur_ptr = '\n';
|
2006-06-18 19:30:48 +02:00
|
|
|
}
|
2006-02-11 22:55:35 +01:00
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
#endif /* USE_READLINE */
|
2006-02-11 22:55:35 +01:00
|
|
|
|
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
|
2003-03-20 07:00:12 +01:00
|
|
|
if (flags & 1)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2006-08-29 17:19:51 +02:00
|
|
|
const char *histfile;
|
2004-08-29 07:07:03 +02:00
|
|
|
char home[MAXPGPATH];
|
2003-03-20 07:00:12 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
useReadline = true;
|
2000-04-12 19:17:23 +02:00
|
|
|
initialize_readline();
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
useHistory = true;
|
|
|
|
using_history();
|
2009-09-14 00:18:22 +02:00
|
|
|
history_lines_added = 0;
|
2005-06-10 17:34:26 +02:00
|
|
|
|
2006-08-29 17:19:51 +02:00
|
|
|
histfile = GetVariable(pset.vars, "HISTFILE");
|
|
|
|
if (histfile == NULL)
|
2005-06-10 17:34:26 +02:00
|
|
|
{
|
|
|
|
if (get_home_path(home))
|
|
|
|
{
|
|
|
|
psql_history = pg_malloc(strlen(home) + 1 +
|
|
|
|
strlen(PSQLHISTORY) + 1);
|
|
|
|
snprintf(psql_history, MAXPGPATH, "%s/%s", home, PSQLHISTORY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2006-08-29 17:19:51 +02:00
|
|
|
psql_history = pg_strdup(histfile);
|
2005-06-10 17:34:26 +02:00
|
|
|
expand_tilde(&psql_history);
|
|
|
|
}
|
2004-01-24 20:38:49 +01:00
|
|
|
|
2005-06-10 17:34:26 +02:00
|
|
|
if (psql_history)
|
2006-06-12 01:06:00 +02:00
|
|
|
{
|
2004-01-24 20:38:49 +01:00
|
|
|
read_history(psql_history);
|
2006-06-12 01:06:00 +02:00
|
|
|
decode_history();
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
#endif
|
2000-02-20 15:28:28 +01:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
atexit(finishInput);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-12 01:06:00 +02:00
|
|
|
/*
|
2009-09-14 00:18:22 +02:00
|
|
|
* This function saves the readline history when user
|
|
|
|
* runs \s command or when psql exits.
|
|
|
|
*
|
|
|
|
* fname: pathname of history file. (Should really be "const char *",
|
|
|
|
* but some ancient versions of readline omit the const-decoration.)
|
|
|
|
*
|
|
|
|
* max_lines: if >= 0, limit history file to that many entries.
|
2006-06-12 01:06:00 +02:00
|
|
|
*
|
2009-09-14 00:18:22 +02:00
|
|
|
* appendFlag: if true, try to append just our new lines to the file.
|
|
|
|
* If false, write the whole available history.
|
|
|
|
*
|
|
|
|
* encodeFlag: whether to encode \n as \x01. For \s calls we don't wish
|
|
|
|
* to do that, but must do so when saving the final history file.
|
2006-03-21 14:38:12 +01:00
|
|
|
*/
|
1999-11-04 22:56:02 +01:00
|
|
|
bool
|
2009-09-14 00:18:22 +02:00
|
|
|
saveHistory(char *fname, int max_lines, bool appendFlag, bool encodeFlag)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
2002-04-11 00:47:09 +02:00
|
|
|
#ifdef USE_READLINE
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-08-27 17:05:20 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Suppressing the write attempt when HISTFILE is set to /dev/null may
|
|
|
|
* look like a negligible optimization, but it's necessary on e.g. Darwin,
|
|
|
|
* where write_history will fail because it tries to chmod the target
|
|
|
|
* file.
|
2006-08-27 17:05:20 +02:00
|
|
|
*/
|
|
|
|
if (useHistory && fname &&
|
|
|
|
strcmp(fname, DEVNULL) != 0)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2006-03-21 14:38:12 +01:00
|
|
|
if (encodeFlag)
|
|
|
|
encode_history();
|
2006-06-18 19:30:48 +02:00
|
|
|
|
|
|
|
/*
|
2009-09-14 00:18:22 +02:00
|
|
|
* On newer versions of libreadline, truncate the history file as
|
|
|
|
* needed and then append what we've added. This avoids overwriting
|
|
|
|
* history from other concurrent sessions (although there are still
|
2010-02-26 03:01:40 +01:00
|
|
|
* race conditions when two sessions exit at about the same time). If
|
|
|
|
* we don't have those functions, fall back to write_history().
|
2009-09-14 00:18:22 +02:00
|
|
|
*
|
|
|
|
* Note: return value of write_history is not standardized across GNU
|
2006-10-04 02:30:14 +02:00
|
|
|
* readline and libedit. Therefore, check for errno becoming set to
|
2009-09-14 00:18:22 +02:00
|
|
|
* see if the write failed. Similarly for append_history.
|
2006-06-18 19:30:48 +02:00
|
|
|
*/
|
2009-09-14 00:18:22 +02:00
|
|
|
#if defined(HAVE_HISTORY_TRUNCATE_FILE) && defined(HAVE_APPEND_HISTORY)
|
|
|
|
if (appendFlag)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int nlines;
|
|
|
|
int fd;
|
2009-09-14 00:18:22 +02:00
|
|
|
|
|
|
|
/* truncate previous entries if needed */
|
|
|
|
if (max_lines >= 0)
|
|
|
|
{
|
|
|
|
nlines = Max(max_lines - history_lines_added, 0);
|
|
|
|
(void) history_truncate_file(fname, nlines);
|
|
|
|
}
|
|
|
|
/* append_history fails if file doesn't already exist :-( */
|
|
|
|
fd = open(fname, O_CREAT | O_WRONLY | PG_BINARY, 0600);
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
/* append the appropriate number of lines */
|
|
|
|
if (max_lines >= 0)
|
|
|
|
nlines = Min(max_lines, history_lines_added);
|
|
|
|
else
|
|
|
|
nlines = history_lines_added;
|
|
|
|
errno = 0;
|
|
|
|
(void) append_history(nlines, fname);
|
|
|
|
if (errno == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* truncate what we have ... */
|
|
|
|
if (max_lines >= 0)
|
|
|
|
stifle_history(max_lines);
|
2010-02-26 03:01:40 +01:00
|
|
|
/* ... and overwrite file. Tough luck for concurrent sessions. */
|
2009-09-14 00:18:22 +02:00
|
|
|
errno = 0;
|
|
|
|
(void) write_history(fname);
|
|
|
|
if (errno == 0)
|
|
|
|
return true;
|
|
|
|
}
|
2003-03-20 07:00:12 +01:00
|
|
|
|
2006-06-12 01:06:00 +02:00
|
|
|
psql_error("could not save history to file \"%s\": %s\n",
|
|
|
|
fname, strerror(errno));
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
2004-10-06 20:39:16 +02:00
|
|
|
#else
|
2006-06-18 19:30:48 +02:00
|
|
|
/* only get here in \s case, so complain */
|
2005-10-15 04:49:52 +02:00
|
|
|
psql_error("history is not supported by this installation\n");
|
1999-11-04 22:56:02 +01:00
|
|
|
#endif
|
2003-03-20 07:00:12 +01:00
|
|
|
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-12 01:08:07 +02:00
|
|
|
static void
|
1999-11-04 22:56:02 +01:00
|
|
|
finishInput(void)
|
|
|
|
{
|
2002-04-11 00:47:09 +02:00
|
|
|
#ifdef USE_READLINE
|
2005-06-10 17:34:26 +02:00
|
|
|
if (useHistory && psql_history)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2005-06-10 17:34:26 +02:00
|
|
|
int hist_size;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2006-08-29 17:19:51 +02:00
|
|
|
hist_size = GetVariableNum(pset.vars, "HISTSIZE", 500, -1, true);
|
2009-09-14 00:18:22 +02:00
|
|
|
saveHistory(psql_history, hist_size, true, true);
|
2005-06-10 17:34:26 +02:00
|
|
|
free(psql_history);
|
|
|
|
psql_history = NULL;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|