Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ea88824

Browse files
committed
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
1 parent 92eadf6 commit ea88824

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

doc/src/sgml/ref/psql-ref.sgml

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.139 2005/06/09 15:27:26 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.140 2005/06/10 15:34:25 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -1987,6 +1987,28 @@ bar
19871987
</listitem>
19881988
</varlistentry>
19891989

1990+
<varlistentry>
1991+
<term><varname>HISTFILE</varname></term>
1992+
<listitem>
1993+
<para>
1994+
This variable contains the filename used to save the history.
1995+
Its default value is <filename>~/.psql_history</filename>.
1996+
For example, use:
1997+
<programlisting>
1998+
\set HISTFILE ~/.psql_history-:DBNAME
1999+
</programlisting>
2000+
in your <filename>~/.psqlrc</filename> will get psql to
2001+
maintain a separate history for each database.
2002+
</para>
2003+
<note>
2004+
<para>
2005+
This feature was shamelessly plagiarized from
2006+
<application>Bash</application>.
2007+
</para>
2008+
</note>
2009+
</listitem>
2010+
</varlistentry>
2011+
19902012
<varlistentry>
19912013
<term><varname>HISTSIZE</varname></term>
19922014
<listitem>

src/bin/psql/input.c

+27-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.43 2005/01/06 18:29:09 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.44 2005/06/10 15:34:26 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -24,6 +24,8 @@
2424
#ifdef USE_READLINE
2525
static bool useReadline;
2626
static bool useHistory;
27+
char *psql_history;
28+
2729

2830
enum histcontrol
2931
{
@@ -177,16 +179,24 @@ initializeInput(int flags)
177179
if (GetVariable(pset.vars, "HISTSIZE") == NULL)
178180
SetVariable(pset.vars, "HISTSIZE", "500");
179181
using_history();
180-
if (get_home_path(home))
182+
183+
if (GetVariable(pset.vars, "HISTFILE") == NULL)
184+
{
185+
if (get_home_path(home))
186+
{
187+
psql_history = pg_malloc(strlen(home) + 1 +
188+
strlen(PSQLHISTORY) + 1);
189+
snprintf(psql_history, MAXPGPATH, "%s/%s", home, PSQLHISTORY);
190+
}
191+
}
192+
else
181193
{
182-
char *psql_history;
194+
psql_history = pg_strdup(GetVariable(pset.vars, "HISTFILE"));
195+
expand_tilde(&psql_history);
196+
}
183197

184-
psql_history = pg_malloc(strlen(home) + 1 +
185-
strlen(PSQLHISTORY) + 1);
186-
sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
198+
if (psql_history)
187199
read_history(psql_history);
188-
free(psql_history);
189-
}
190200
}
191201
#endif
192202

@@ -227,25 +237,17 @@ finishInput(int exitstatus, void *arg)
227237
#endif
228238
{
229239
#ifdef USE_READLINE
230-
if (useHistory)
240+
if (useHistory && psql_history)
231241
{
232-
char home[MAXPGPATH];
242+
int hist_size;
233243

234-
if (get_home_path(home))
235-
{
236-
char *psql_history;
237-
int hist_size;
238-
239-
hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);
240-
if (hist_size >= 0)
241-
stifle_history(hist_size);
242-
243-
psql_history = pg_malloc(strlen(home) + 1 +
244-
strlen(PSQLHISTORY) + 1);
245-
sprintf(psql_history, "%s/%s", home, PSQLHISTORY);
246-
write_history(psql_history);
247-
free(psql_history);
248-
}
244+
hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);
245+
if (hist_size >= 0)
246+
stifle_history(hist_size);
247+
248+
write_history(psql_history);
249+
free(psql_history);
250+
psql_history = NULL;
249251
}
250252
#endif
251253
}

0 commit comments

Comments
 (0)