Обсуждение: [HACKERS] Patch to psql.c to allow .psqlrc file
OK, here's a quick patch to psql.c which first reads commands from
/etc/psqlrc and then from $HOME/.psqlrc
I've used access() to check on the existence of these files; I'm
not sure if every system has access(), so it might be necessary
to change to stat() instead. Certainly Linux and Irix *do* have
access()
Andrew
*** src/bin/psql/psql.c.orig Fri Jun 20 21:54:31 1997
- --- src/bin/psql/psql.c Fri Jun 20 22:23:49 1997
***************
*** 1598,1603 ****
- --- 1598,1605 ----
bool singleSlashCmd = 0;
int c;
+ char *home = NULL; /* Used to store $HOME */
+
memset(&settings, 0, sizeof settings);
settings.opt.align = 1;
settings.opt.header = 1;
***************
*** 1728,1733 ****
- --- 1730,1760 ----
printf(" type \\g or terminate with semicolon to execute query\n");
printf(" You are currently connected to the database: %s\n\n", dbname);
}
+
+ /*
+ * 20.06.97 ACRM See if we've got a /etc/psqlrc or .psqlrc file
+ */
+ if(!access("/etc/psqlrc",R_OK))
+ HandleSlashCmds(&settings, "\\i /etc/psqlrc", "");
+ if((home = getenv("HOME"))!=NULL) {
+ char *psqlrc = NULL,
+ *line = NULL;
+
+ if((psqlrc = (char *)malloc(strlen(home) + 10))!=NULL) {
+ sprintf(psqlrc, "%s/.psqlrc", home);
+ if(!access(psqlrc, R_OK)) {
+ if((line = (char *)malloc(strlen(psqlrc) + 5))!=NULL) {
+ sprintf(line, "\\i %s", psqlrc);
+ HandleSlashCmds(&settings, line, "");
+ free(line);
+ }
+ }
+ free(psqlrc);
+ }
+ }
+ /* End of check for psqlrc files */
+
+
if (qfilename || singleSlashCmd) {
/*
* read in a file full of queries instead of reading in queries
- ----------------------------------------------------------------------------
Dr. Andrew C.R. Martin University College London
EMAIL: (Work) martin@biochem.ucl.ac.uk (Home) andrew@stagleys.demon.co.uk
URL: http://www.biochem.ucl.ac.uk/~martin
Tel: (Work) +44(0)171 419 3890 (Home) +44(0)1372 275775
------------------------------
>
>
> OK, here's a quick patch to psql.c which first reads commands from
> /etc/psqlrc and then from $HOME/.psqlrc
>
> I've used access() to check on the existence of these files; I'm
> not sure if every system has access(), so it might be necessary
> to change to stat() instead. Certainly Linux and Irix *do* have
> access()
Can I request we search in the postgres install directory first, then
the home directory, or is that stupid because there may not be a
postgresql install directory on the machine you are running psql on.
Just asking.
>
> Andrew
>
>
> *** src/bin/psql/psql.c.orig Fri Jun 20 21:54:31 1997
> --- src/bin/psql/psql.c Fri Jun 20 22:23:49 1997
> ***************
> *** 1598,1603 ****
> --- 1598,1605 ----
> bool singleSlashCmd = 0;
> int c;
>
> + char *home = NULL; /* Used to store $HOME */
> +
> memset(&settings, 0, sizeof settings);
> settings.opt.align = 1;
> settings.opt.header = 1;
> ***************
> *** 1728,1733 ****
> --- 1730,1760 ----
> printf(" type \\g or terminate with semicolon to execute query\n");
> printf(" You are currently connected to the database: %s\n\n", dbname);
> }
> +
> + /*
> + * 20.06.97 ACRM See if we've got a /etc/psqlrc or .psqlrc file
> + */
> + if(!access("/etc/psqlrc",R_OK))
> + HandleSlashCmds(&settings, "\\i /etc/psqlrc", "");
> + if((home = getenv("HOME"))!=NULL) {
> + char *psqlrc = NULL,
> + *line = NULL;
> +
> + if((psqlrc = (char *)malloc(strlen(home) + 10))!=NULL) {
> + sprintf(psqlrc, "%s/.psqlrc", home);
> + if(!access(psqlrc, R_OK)) {
> + if((line = (char *)malloc(strlen(psqlrc) + 5))!=NULL) {
> + sprintf(line, "\\i %s", psqlrc);
> + HandleSlashCmds(&settings, line, "");
> + free(line);
> + }
> + }
> + free(psqlrc);
> + }
> + }
> + /* End of check for psqlrc files */
> +
> +
> if (qfilename || singleSlashCmd) {
> /*
> * read in a file full of queries instead of reading in queries
>
> ----------------------------------------------------------------------------
> Dr. Andrew C.R. Martin University College London
> EMAIL: (Work) martin@biochem.ucl.ac.uk (Home) andrew@stagleys.demon.co.uk
> URL: http://www.biochem.ucl.ac.uk/~martin
> Tel: (Work) +44(0)171 419 3890 (Home) +44(0)1372 275775
>
>
- --
Bruce Momjian
maillist@candle.pha.pa.us
------------------------------
> > > > OK, here's a quick patch to psql.c which first reads commands from > > /etc/psqlrc and then from $HOME/.psqlrc > > > > I've used access() to check on the existence of these files; I'm > > not sure if every system has access(), so it might be necessary > > to change to stat() instead. Certainly Linux and Irix *do* have > > access() > > Can I request we search in the postgres install directory first, then > the home directory, or is that stupid because there may not be a > postgresql install directory on the machine you are running psql on. > > Just asking. > It's trivial to search whatever directories we want to, but using /etc/ and $HOME is the convention which every other program I know of follows, so I just did the same. It's certainly true that every machine will have a /etc whereas maybe only one machine on a network may have the PG installation directory. I'd vote for leaving it as is... Andrew - ---------------------------------------------------------------------------- Dr. Andrew C.R. Martin University College London EMAIL: (Work) martin@biochem.ucl.ac.uk (Home) andrew@stagleys.demon.co.uk URL: http://www.biochem.ucl.ac.uk/~martin Tel: (Work) +44(0)171 419 3890 (Home) +44(0)1372 275775 ------------------------------
> It's trivial to search whatever directories we want to, but using > /etc/ and $HOME is the convention which every other program I know > of follows, so I just did the same. > > It's certainly true that every machine will have a /etc whereas > maybe only one machine on a network may have the PG > installation directory. > > I'd vote for leaving it as is... Very good. I was just asking. - -- Bruce Momjian maillist@candle.pha.pa.us ------------------------------
> > Can I request we search in the postgres install directory first, then > > the home directory, or is that stupid because there may not be a > > postgresql install directory on the machine you are running psql on. > It's trivial to search whatever directories we want to, but using > /etc/ and $HOME is the convention which every other program I know > of follows, so I just did the same. > > It's certainly true that every machine will have a /etc whereas > maybe only one machine on a network may have the PG > installation directory. > > I'd vote for leaving it as is... > > > Andrew How about some sort of PATH variable? setenv PG_INITPATH /etc:/usr/local/pgsql:~ or export PG_INITPATH=/etc:/usr/local/pgsql:/home/bennett In the absense of the environment variable, it would default to "/etc:$HOME". This could be considered a security issue, however. If someone, without thinking, put "." in their PG_INITPATH, they could be tricked into executing something nasty. -Erik Erik Bennett bennett@corp.oneworld.com ------------------------------
On Sat, 21 Jun 1997 bennett@research.oneworld.com wrote:
>
> > > Can I request we search in the postgres install directory first, then
> > > the home directory, or is that stupid because there may not be a
> > > postgresql install directory on the machine you are running psql on.
>
> > It's trivial to search whatever directories we want to, but using
> > /etc/ and $HOME is the convention which every other program I know
> > of follows, so I just did the same.
> >
> > It's certainly true that every machine will have a /etc whereas
> > maybe only one machine on a network may have the PG
> > installation directory.
> >
> > I'd vote for leaving it as is...
> >
> >
> > Andrew
>
> How about some sort of PATH variable?
> setenv PG_INITPATH /etc:/usr/local/pgsql:~
> or
> export PG_INITPATH=/etc:/usr/local/pgsql:/home/bennett
Hrmm...was just thinking about this, and there is a flaw here:
PostgreSQL isn't installed as root. What about an admin that sets up a
pgsql account, gives it to someone else to installed/maintain...no global
psqlrc file :(
I don't know if there is any way of doing it, but I would tend to think that
having on 'central to the install directory' in this case might actually
be advisable. Maybe use the --pgdata variable to setup the initial file,
similar to pg_hba.conf?
Marc G. Fournier
Systems Administrator @ hub.org
primary: scrappy@hub.org secondary: scrappy@{freebsd|postgresql}.org
------------------------------
> Hrmm...was just thinking about this, and there is a flaw here: > > PostgreSQL isn't installed as root. What about an admin that sets up a > pgsql account, gives it to someone else to installed/maintain...no global > psqlrc file :( > > I don't know if there is any way of doing it, but I would tend to think that > having on 'central to the install directory' in this case might actually > be advisable. Maybe use the --pgdata variable to setup the initial file, > similar to pg_hba.conf? Kind of hard to CENTRALLY locate something if you are not root. I wouldn't worry about it. - -- Bruce Momjian maillist@candle.pha.pa.us ------------------------------
On Sun, 22 Jun 1997, Bruce Momjian wrote:
> > Hrmm...was just thinking about this, and there is a flaw here:
> >
> > PostgreSQL isn't installed as root. What about an admin that sets up a
> > pgsql account, gives it to someone else to installed/maintain...no global
> > psqlrc file :(
> >
> > I don't know if there is any way of doing it, but I would tend to think that
> > having on 'central to the install directory' in this case might actually
> > be advisable. Maybe use the --pgdata variable to setup the initial file,
> > similar to pg_hba.conf?
>
> Kind of hard to CENTRALLY locate something if you are not root. I
> wouldn't worry about it.
No, you actually missed the point.
If I install pgsql as userid pgsql, I can also install a psqlrc
file into pgdata/psqlrc...but, as pgsql, I can't install the same thing in
/etc
So, what we are saying, is that in order to *completely* install
PostgreSQL, you now have to have root access? Completely including the
psqlrc file...
Marc G. Fournier
Systems Administrator @ hub.org
primary: scrappy@hub.org secondary: scrappy@{freebsd|postgresql}.org
------------------------------
> > Kind of hard to CENTRALLY locate something if you are not root. I > > wouldn't worry about it. > > No, you actually missed the point. > > If I install pgsql as userid pgsql, I can also install a psqlrc > file into pgdata/psqlrc...but, as pgsql, I can't install the same thing in > /etc > > So, what we are saying, is that in order to *completely* install > PostgreSQL, you now have to have root access? Completely including the > psqlrc file... Makes sense. Maybe we should look in /etc, the PostgreSQL install directory, then the user's home directory. - -- Bruce Momjian maillist@candle.pha.pa.us ------------------------------
> > > Kind of hard to CENTRALLY locate something if you are not root. I
> > > wouldn't worry about it.
> >
> > No, you actually missed the point.
> >
> > If I install pgsql as userid pgsql, I can also install a psqlrc
> > file into pgdata/psqlrc...but, as pgsql, I can't install the same thing in
> > /etc
> >
> > So, what we are saying, is that in order to *completely* install
> > PostgreSQL, you now have to have root access? Completely including the
> > psqlrc file...
>
> Makes sense. Maybe we should look in /etc, the PostgreSQL install
> directory, then the user's home directory.
>
> --
> Bruce Momjian
> maillist@candle.pha.pa.us
>
OK, here's a patch (to be applied after the previous one) which now checks
1. /etc/pgsqlrc
2. $(PGDATA)/pgsqlrc
3. $(HOME)/.pgsqlrc
Andrew
*** psql.c.new Tue Jun 24 13:54:34 1997
- --- psql.c Tue Jun 24 13:56:32 1997
***************
*** 1736,1741 ****
- --- 1736,1757 ----
*/
if(!access("/etc/psqlrc",R_OK))
HandleSlashCmds(&settings, "\\i /etc/psqlrc", "");
+ if((home = getenv("PGDATA"))!=NULL) {
+ char *psqlrc = NULL,
+ *line = NULL;
+
+ if((psqlrc = (char *)malloc(strlen(home) + 10))!=NULL) {
+ sprintf(psqlrc, "%s/psqlrc", home);
+ if(!access(psqlrc, R_OK)) {
+ if((line = (char *)malloc(strlen(psqlrc) + 5))!=NULL) {
+ sprintf(line, "\\i %s", psqlrc);
+ HandleSlashCmds(&settings, line, "");
+ free(line);
+ }
+ }
+ free(psqlrc);
+ }
+ }
if((home = getenv("HOME"))!=NULL) {
char *psqlrc = NULL,
*line = NULL;
- ----------------------------------------------------------------------------
Dr. Andrew C.R. Martin University College London
EMAIL: (Work) martin@biochem.ucl.ac.uk (Home) andrew@stagleys.demon.co.uk
URL: http://www.biochem.ucl.ac.uk/~martin
Tel: (Work) +44(0)171 419 3890 (Home) +44(0)1372 275775
------------------------------
Andrew Martin wrote: > > > OK, here's a patch (to be applied after the previous one) which now checks > 1. /etc/pgsqlrc > 2. $(PGDATA)/pgsqlrc > 3. $(HOME)/.pgsqlrc Ok - can you also change libpq to get it working with pgsqlrc (a "global" one) on startup ? (Useful if user want to have the same settings for all applications.) Vadim ------------------------------
On Tue, 24 Jun 1997, Vadim B. Mikheev wrote: > Andrew Martin wrote: > > > > > > OK, here's a patch (to be applied after the previous one) which now checks > > 1. /etc/pgsqlrc > > 2. $(PGDATA)/pgsqlrc > > 3. $(HOME)/.pgsqlrc > > Ok - can you also change libpq to get it working with pgsqlrc > (a "global" one) on startup ? > (Useful if user want to have the same settings for all > applications.) And if you don't want your application to use a startup-file? There must be some switch to pgconnect that disables reading a startup-file IMO. Maarten _____________________________________________________________________________ | Maarten Boekhold, Faculty of Electrical Engineering TU Delft, NL | | M.Boekhold@et.tudelft.nl | - ----------------------------------------------------------------------------- ------------------------------
> On Tue, 24 Jun 1997, Vadim B. Mikheev wrote: > > > Andrew Martin wrote: > > > > > > > > > OK, here's a patch (to be applied after the previous one) which now checks > > > 1. /etc/pgsqlrc > > > 2. $(PGDATA)/pgsqlrc > > > 3. $(HOME)/.pgsqlrc > > > > Ok - can you also change libpq to get it working with pgsqlrc > > (a "global" one) on startup ? > > (Useful if user want to have the same settings for all > > applications.) Hmmm. Personally I think it should be an application level thing not libpq. In any case, I don't think we should be considering libpq patches for 6.1.1; these should be for 6.2 > > And if you don't want your application to use a startup-file? There must > be some switch to pgconnect that disables reading a startup-file IMO. More to the point, should there be some such flag for psql?? > > Maarten > Andrew - ---------------------------------------------------------------------------- Dr. Andrew C.R. Martin University College London EMAIL: (Work) martin@biochem.ucl.ac.uk (Home) andrew@stagleys.demon.co.uk URL: http://www.biochem.ucl.ac.uk/~martin Tel: (Work) +44(0)171 419 3890 (Home) +44(0)1372 275775 ------------------------------