psql: customizable readline history filename - Mailing list pgsql-patches

From Andreas Seltenreich
Subject psql: customizable readline history filename
Date
Msg-id 87k6tegfcm.fsf@gate450.dyndns.org
Whole thread Raw
Responses Re: psql: customizable readline history filename  (Bruce Momjian <pgman@candle.pha.pa.us>)
Re: psql: customizable readline history filename  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-patches
Hi,

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

regards,
Andreas

Index: doc/src/sgml/ref/psql-ref.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v
retrieving revision 1.123
diff -c -r1.123 psql-ref.sgml
*** doc/src/sgml/ref/psql-ref.sgml    6 Oct 2004 18:39:15 -0000    1.123
--- doc/src/sgml/ref/psql-ref.sgml    26 Oct 2004 05:57:57 -0000
***************
*** 1973,1978 ****
--- 1973,2001 ----
        </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>.
+         When unset or empty, the command history is not saved upon
+         program termination.  For example,
+ <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>
          <term><varname>HISTSIZE</varname></term>
          <listitem>
          <para>
Index: src/bin/psql/input.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/input.c,v
retrieving revision 1.41
diff -c -r1.41 input.c
*** src/bin/psql/input.c    12 Oct 2004 21:54:44 -0000    1.41
--- src/bin/psql/input.c    26 Oct 2004 05:57:57 -0000
***************
*** 38,46 ****
  static void finishInput(int, void *);
  #endif

- #define PSQLHISTORY ".psql_history"
-
-
  #ifdef USE_READLINE
  static enum histcontrol
  GetHistControlConfig(void)
--- 38,43 ----
***************
*** 167,173 ****
  #ifdef USE_READLINE
      if (flags & 1)
      {
!         char        home[MAXPGPATH];

          useReadline = true;
          initialize_readline();
--- 164,171 ----
  #ifdef USE_READLINE
      if (flags & 1)
      {
!         const char *psql_history;
!         char       *tilde_expanded;

          useReadline = true;
          initialize_readline();
***************
*** 176,191 ****
          if (GetVariable(pset.vars, "HISTSIZE") == NULL)
              SetVariable(pset.vars, "HISTSIZE", "500");
          using_history();
-         if (get_home_path(home))
-         {
-             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);
!         }
      }
  #endif

--- 174,189 ----
          if (GetVariable(pset.vars, "HISTSIZE") == NULL)
              SetVariable(pset.vars, "HISTSIZE", "500");
          using_history();

!         if (GetVariable(pset.vars, "HISTFILE") == NULL)
!             SetVariable(pset.vars, "HISTFILE", "~/.psql_history");
!
!         psql_history = GetVariable(pset.vars, "HISTFILE");
!
!         tilde_expanded = pg_strdup(psql_history);
!         expand_tilde(&tilde_expanded);
!         read_history(tilde_expanded);
!         free(tilde_expanded);
      }
  #endif

***************
*** 228,252 ****
  #ifdef USE_READLINE
      if (useHistory)
      {
!         char        home[MAXPGPATH];
!
!         if (get_home_path(home))
!         {
!             char       *psql_history;
!             int            hist_size;

!             psql_history = pg_malloc(strlen(home) + 1 +
!                                      strlen(PSQLHISTORY) + 1);

!             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);
!         }
      }
  #endif
  }
--- 226,251 ----
  #ifdef USE_READLINE
      if (useHistory)
      {
!         const char *psql_history;
!         char       *tilde_expanded;
!         int            hist_size;
!
!         psql_history = GetVariable(pset.vars, "HISTFILE");
!
!         if (!psql_history || !strlen(psql_history))
!             return;
!
!         tilde_expanded = pg_strdup(psql_history);
!         expand_tilde(&tilde_expanded);
!
!         hist_size = GetVariableNum(pset.vars, "HISTSIZE", -1, -1, true);

!         if (hist_size >= 0)
!             stifle_history(hist_size);

!         saveHistory(tilde_expanded);

!         free(tilde_expanded);
      }
  #endif
  }

pgsql-patches by date:

Previous
From: "Matthew T. O'Connor"
Date:
Subject: pg_autovacuum vacuum cost variables patch
Next
From: "Dave Page"
Date:
Subject: Re: pg_autovacuum vacuum cost variables patch