psql \i handling ~ in specified file name

From: Zach Irmen <zirmen(at)shaw(dot)ca>
To: pgsql-patches(at)postgresql(dot)org
Subject: psql \i handling ~ in specified file name
Date: 2003-12-21 04:33:02
Message-ID: 005501c3c77b$85a39160$0b01010a@ed.shawcable.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

refers to following item on TODO
Have psql '\i ~/<tab><tab>' actually load files it displays from home dir

A reviewable patch that could handle tilde expansion for
psql \i ~/filename
or
psql \i ~username/filename

--
Zach Irmen

cvs server: Diffing .
Index: command.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/command.c,v
retrieving revision 1.108
diff -c -r1.108 command.c
*** command.c 1 Dec 2003 22:21:54 -0000 1.108
--- command.c 21 Dec 2003 04:17:24 -0000
***************
*** 65,70 ****
--- 65,72 ----
static bool do_connect(const char *new_dbname, const char *new_user);
static bool do_shell(const char *command);

+ static char *expand_tilde(char *filename);
+
/*----------
* HandleSlashCmds:
*
***************
*** 522,527 ****
--- 524,530 ----
/* \i is include file */
else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0)
{
+ char *expanded_fname;
char *fname = scan_option(&string, OT_NORMAL, NULL,
true);

if (!fname)
***************
*** 531,537 ****
}
else
{
! success = (process_file(fname) == EXIT_SUCCESS);
free(fname);
}
}
--- 534,542 ----
}
else
{
! expanded_fname = expand_tilde(fname);
! success = (process_file(expanded_fname) ==
EXIT_SUCCESS);
! free(expanded_fname);
free(fname);
}
}
***************
*** 1678,1683 ****
--- 1683,1731 ----
}

+ /* expand_tilde
+ *
+ * substitute '~' with HOME or '~username' with username's home dir
+ *
+ */
+ static char *
+ expand_tilde(char *filename)
+ {
+ char *home;
+ char oldp, *p;
+ struct passwd *pw;
+
+ filename = xstrdup(filename);
+
+ /* try tilde expansion */
+ if (*filename == '~')
+ {
+ home = NULL;
+
+ p = filename+1;
+ while (*p != '/' && *p != '\0')
+ p++;
+
+ oldp = *p;
+ *p = '\0';
+
+ if (*(filename+1) == '\0')
+ home = xstrdup(getenv("HOME"));
+ else if ((pw = getpwnam(filename+1)) != NULL)
+ home = xstrdup(pw->pw_dir);
+
+ *p = oldp;
+ if (home)
+ {
+ home = realloc(home, strlen(home) + strlen(p) + 1);
+ strcat(home,p);
+ free(filename);
+ filename = home;
+ }
+ }
+
+ return filename;
+ }

/*
* process_file

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2003-12-21 04:37:06 Re: [COMMITTERS] pgsql-server/ oc/src/sgml/datatype.sgml rc/bac ...
Previous Message Bruce Momjian 2003-12-21 04:26:04 Re: [GENERAL] Temporary tables and miscellaneous schemas