Re: PSQLRC environment variable. - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: PSQLRC environment variable.
Date
Msg-id 200404210352.i3L3qVe23763@candle.pha.pa.us
Whole thread Raw
In response to Re: PSQLRC environment variable.  (Bruce Momjian <pgman@candle.pha.pa.us>)
Responses Re: PSQLRC environment variable.
List pgsql-patches
Bruce Momjian wrote:
> Tom Lane wrote:
> > Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > > I just checked a few apps and I see they usually allow a global config
> > > file to be specified.  How about SYSPSQLRC that adds a system-wide psql
> > > config file to be read before the one in the home directory?
> >
> > The ones I know of that allow such a thing generally hard-wire the
> > location of the global config file at build time, rather than taking it
> > from an environment variable.  The env var approach seems weird, and a
> > tad inefficient (since you'd have to put such an env var into the global
> > .profile, meaning it propagates into every single process ever launched
> > on your system).  Also I think we have at least one global config file
> > already for libpq, and its location is hard-wired.
>
> Agreed.  There is usually one global config file, and SYS* overrides it.
> The global one we have now is pg_service.conf.  Maybe we should forget
> the environment variable idea and just have a pgsql.rc.sample file in
> share, with documentation in the file on how to install it.  That is
> what we do with pg_service.conf now for libpq.

The attached patch implements a global psql.rc file that is read before
the one in the user's home directory --- doc changes included.

It is configured just like pg_service.conf.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: doc/src/sgml/ref/psql-ref.sgml
===================================================================
RCS file: /cvsroot/pgsql-server/doc/src/sgml/ref/psql-ref.sgml,v
retrieving revision 1.112
diff -c -c -r1.112 psql-ref.sgml
*** doc/src/sgml/ref/psql-ref.sgml    21 Apr 2004 00:34:18 -0000    1.112
--- doc/src/sgml/ref/psql-ref.sgml    21 Apr 2004 03:51:13 -0000
***************
*** 440,446 ****
        <term><option>--no-psqlrc</></term>
        <listitem>
        <para>
!       Do not read the start-up file <filename>~/.psqlrc</filename>.
        </para>
        </listitem>
      </varlistentry>
--- 440,447 ----
        <term><option>--no-psqlrc</></term>
        <listitem>
        <para>
!       Do not read the start-up file <filename>/psql.rc</filename> or
!       <filename>~/.psqlrc</filename>.
        </para>
        </listitem>
      </varlistentry>
***************
*** 1859,1866 ****
      <para>
       The autocommit-on mode is <productname>PostgreSQL</>'s traditional
       behavior, but autocommit-off is closer to the SQL spec.  If you
!      prefer autocommit-off, you may wish to set it in
!      your <filename>.psqlrc</filename> file.
      </para>
      </note>
      </listitem>
--- 1860,1868 ----
      <para>
       The autocommit-on mode is <productname>PostgreSQL</>'s traditional
       behavior, but autocommit-off is closer to the SQL spec.  If you
!      prefer autocommit-off, you may wish to set it in the system-wide
!          <filename>psql.rc</filename> or your
!          <filename>.psqlrc</filename> file.
      </para>
      </note>
      </listitem>
***************
*** 2488,2496 ****
     <listitem>
      <para>
       Before starting up, <application>psql</application> attempts to
!      read and execute commands from the file
!      <filename>$HOME/.psqlrc</filename>. It could be used to set up
!      the client or the server to taste (using the <command>\set
       </command> and <command>SET</command> commands).
      </para>
     </listitem>
--- 2490,2501 ----
     <listitem>
      <para>
       Before starting up, <application>psql</application> attempts to
!      read and execute commands from the the system-wide
!      <filename>psql.rc</filename> file and the
!      <filename>$HOME/.psqlrc</filename> file in the user's home
!      directory.  See <filename><replaceable>PREFIX</>/share/psql.rc.sample</>
!      for information on setting up the system-wide file.  It could be used
!      to set up the client or the server to taste (using the <command>\set
       </command> and <command>SET</command> commands).
      </para>
     </listitem>
Index: src/bin/psql/Makefile
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/Makefile,v
retrieving revision 1.40
diff -c -c -r1.40 Makefile
*** src/bin/psql/Makefile    9 Mar 2004 19:47:05 -0000    1.40
--- src/bin/psql/Makefile    21 Apr 2004 03:51:14 -0000
***************
*** 15,21 ****

  REFDOCDIR= $(top_srcdir)/doc/src/sgml/ref

! override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) -DFRONTEND

  OBJS=    command.o common.o help.o input.o stringutils.o mainloop.o copy.o \
      startup.o prompt.o variables.o large_obj.o print.o describe.o \
--- 15,21 ----

  REFDOCDIR= $(top_srcdir)/doc/src/sgml/ref

! override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) -DFRONTEND -DSYSCONFDIR='"$(sysconfdir)"'

  OBJS=    command.o common.o help.o input.o stringutils.o mainloop.o copy.o \
      startup.o prompt.o variables.o large_obj.o print.o describe.o \
***************
*** 50,55 ****
--- 50,56 ----

  install: all installdirs
      $(INSTALL_PROGRAM) psql$(X) $(DESTDIR)$(bindir)/psql$(X)
+     $(INSTALL_DATA) $(srcdir)/psql.rc.sample $(DESTDIR)$(datadir)/psql.rc.sample

  installdirs:
      $(mkinstalldirs) $(DESTDIR)$(bindir)
Index: src/bin/psql/startup.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/startup.c,v
retrieving revision 1.88
diff -c -c -r1.88 startup.c
*** src/bin/psql/startup.c    19 Apr 2004 17:42:58 -0000    1.88
--- src/bin/psql/startup.c    21 Apr 2004 03:51:15 -0000
***************
*** 44,50 ****
   */
  PsqlSettings pset;

! #define PSQLRC ".psqlrc"

  /*
   * Structures to pass information between the option parsing routine
--- 44,51 ----
   */
  PsqlSettings pset;

! #define PSQLRC         ".psqlrc"
! #define SYSPSQLRC    "psql.rc"

  /*
   * Structures to pass information between the option parsing routine
***************
*** 74,79 ****
--- 75,81 ----
  static void parse_psql_options(int argc, char *argv[],
                     struct adhoc_opts * options);
  static void process_psqlrc(void);
+ static void process_psqlrc_file(char *filename);
  static void showVersion(void);

  #ifdef USE_SSL
***************
*** 562,567 ****
--- 564,572 ----

  }

+ #ifndef SYSCONFDIR
+ #error "You must compile this file with SYSCONFDIR defined."
+ #endif


  /*
***************
*** 570,601 ****
  static void
  process_psqlrc(void)
  {
!     char       *psqlrc;
      char       *home;

  #if defined(WIN32) && (!defined(__MINGW32__))
  #define R_OK 4
  #endif

!     /* Look for one in the home dir */
!     home = getenv("HOME");
!
!     if (home)
!     {
!         psqlrc = pg_malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
!                            strlen(PG_VERSION) + 1);
!         sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);

!         if (access(psqlrc, R_OK) == 0)
!             process_file(psqlrc);
!         else
!         {
!             sprintf(psqlrc, "%s/%s", home, PSQLRC);
!             if (access(psqlrc, R_OK) == 0)
!                 process_file(psqlrc);
!         }
!         free(psqlrc);
!     }
  }


--- 575,613 ----
  static void
  process_psqlrc(void)
  {
!     char       *globalFile = SYSCONFDIR "/" SYSPSQLRC;
      char       *home;
+     char       *psqlrc;
+
+     process_psqlrc_file(globalFile);
+
+     if ((home = getenv("HOME")) != NULL)
+     {
+         psqlrc = pg_malloc(strlen(home) + 1 + strlen(PSQLRC) + 1);
+         sprintf(psqlrc, "%s/%s", home, PSQLRC);
+         process_psqlrc_file(psqlrc);
+     }
+ }
+
+
+
+ static void
+ process_psqlrc_file(char *filename)
+ {
+     char       *psqlrc;

  #if defined(WIN32) && (!defined(__MINGW32__))
  #define R_OK 4
  #endif

!     psqlrc = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1);
!     sprintf(psqlrc, "%s-%s", filename, PG_VERSION);

!     if (access(psqlrc, R_OK) == 0)
!         process_file(psqlrc);
!     else if (access(filename, R_OK) == 0)
!             process_file(filename);
!     free(psqlrc);
  }



pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: CSV keyword change
Next
From: Neil Conway
Date:
Subject: doc improv: backup/restore