Commit ea88824a authored by Bruce Momjian's avatar Bruce Momjian

the following patch makes the filename used to store the readline

history customizable through a variable named HISTFILE, analogous to
psql's already implemented HISTCONTROL and HISTSIZE variables, and
bash's HISTFILE-Variable.

The motivation was to be able to get psql to maintain separate
histories for separate databases.  This is now easily achievable
through a line like the following in ~/.psqlrc:

\set HISTFILE ~/.psql_history-:DBNAME

Andreas Seltenreich
parent 92eadf6c
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.139 2005/06/09 15:27:26 momjian Exp $ $PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.140 2005/06/10 15:34:25 momjian Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
...@@ -1987,6 +1987,28 @@ bar ...@@ -1987,6 +1987,28 @@ bar
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><varname>HISTFILE</varname></term>
<listitem>
<para>
This variable contains the filename used to save the history.
Its default value is <filename>~/.psql_history</filename>.
For example, use:
<programlisting>
\set HISTFILE ~/.psql_history-:DBNAME
</programlisting>
in your <filename>~/.psqlrc</filename> will get psql to
maintain a separate history for each database.
</para>
<note>
<para>
This feature was shamelessly plagiarized from
<application>Bash</application>.
</para>
</note>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><varname>HISTSIZE</varname></term> <term><varname>HISTSIZE</varname></term>
<listitem> <listitem>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2005, PostgreSQL Global Development Group * Copyright (c) 2000-2005, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.43 2005/01/06 18:29:09 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.44 2005/06/10 15:34:26 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#ifdef USE_READLINE #ifdef USE_READLINE
static bool useReadline; static bool useReadline;
static bool useHistory; static bool useHistory;
char *psql_history;
enum histcontrol enum histcontrol
{ {
...@@ -177,16 +179,24 @@ initializeInput(int flags) ...@@ -177,16 +179,24 @@ initializeInput(int flags)
if (GetVariable(pset.vars, "HISTSIZE") == NULL) if (GetVariable(pset.vars, "HISTSIZE") == NULL)
SetVariable(pset.vars, "HISTSIZE", "500"); SetVariable(pset.vars, "HISTSIZE", "500");
using_history(); using_history();
if (get_home_path(home))
if (GetVariable(pset.vars, "HISTFILE") == NULL)
{
if (get_home_path(home))
{
psql_history = pg_malloc(strlen(home) + 1 +
strlen(PSQLHISTORY) + 1);
snprintf(psql_history, MAXPGPATH, "%s/%s", home, PSQLHISTORY);
}
}
else
{ {
char *psql_history; psql_history = pg_strdup(GetVariable(pset.vars, "HISTFILE"));
expand_tilde(&psql_history);
}
psql_history = pg_malloc(strlen(home) + 1 + if (psql_history)
strlen(PSQLHISTORY) + 1);
sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
read_history(psql_history); read_history(psql_history);
free(psql_history);
}
} }
#endif #endif
...@@ -227,25 +237,17 @@ finishInput(int exitstatus, void *arg) ...@@ -227,25 +237,17 @@ finishInput(int exitstatus, void *arg)
#endif #endif
{ {
#ifdef USE_READLINE #ifdef USE_READLINE
if (useHistory) if (useHistory && psql_history)
{ {
char home[MAXPGPATH]; int hist_size;
if (get_home_path(home)) hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);
{ if (hist_size >= 0)
char *psql_history; stifle_history(hist_size);
int hist_size;
write_history(psql_history);
hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true); free(psql_history);
if (hist_size >= 0) psql_history = NULL;
stifle_history(hist_size);
psql_history = pg_malloc(strlen(home) + 1 +
strlen(PSQLHISTORY) + 1);
sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
write_history(psql_history);
free(psql_history);
}
} }
#endif #endif
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment