7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.59 1997/04/10 11:54:29 scrappy Exp $
10
+ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.60 1997/05/21 03:12:02 momjian Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
14
14
#include <stdio.h>
15
+ #include <stdlib.h>
15
16
#include <string.h>
16
17
#include <signal.h>
17
18
#include <errno.h>
@@ -155,7 +156,7 @@ slashUsage(PsqlSettings * ps)
155
156
fprintf (stderr , " \\? -- help\n" );
156
157
fprintf (stderr , " \\a -- toggle field-alignment (currenty %s)\n" , on (ps -> opt .align ));
157
158
fprintf (stderr , " \\C [<captn>] -- set html3 caption (currently '%s')\n" , ps -> opt .caption ? ps -> opt .caption : "" );
158
- fprintf (stderr , " \\connect <dbname> -- connect to new database (currently '%s')\n" , PQdb (ps -> db ));
159
+ fprintf (stderr , " \\connect <dbname> <user> -- connect to new database (currently '%s')\n" , PQdb (ps -> db ));
159
160
fprintf (stderr , " \\copy table {from | to} <fname>\n" );
160
161
fprintf (stderr , " \\d [<table>] -- list tables in database or columns in <table>, * for all\n" );
161
162
fprintf (stderr , " \\e [<fname>] -- edit the current query buffer or <fname>, \\E execute too\n" );
@@ -825,19 +826,36 @@ do_copy(const char *args, PsqlSettings * settings)
825
826
826
827
827
828
static void
828
- do_connect (const char * new_dbname , PsqlSettings * settings )
829
+ do_connect (const char * new_dbname ,
830
+ const char * new_user ,
831
+ PsqlSettings * settings )
829
832
{
830
833
831
834
char * dbname = PQdb (settings -> db );
832
835
if (!new_dbname )
833
836
fprintf (stderr , "\\connect must be followed by a database name\n" );
834
837
else {
835
- PGconn * olddb = settings -> db ;
838
+ PGconn * olddb = settings -> db ;
839
+ char * userenv ;
836
840
837
841
printf ("closing connection to database: %s\n" , dbname );
842
+ if (new_user != NULL ) {
843
+ /*
844
+ PQsetdb() does not allow us to specify the user,
845
+ so we have to do it via PGUSER
846
+ */
847
+ userenv = malloc (strlen ("PGUSER=" ) + strlen (new_user ) + 1 );
848
+ sprintf (userenv ,"PGUSER=%s" ,new_user );
849
+ putenv (userenv );
850
+ free (userenv );
851
+ }
838
852
settings -> db = PQsetdb (PQhost (olddb ), PQport (olddb ),
839
853
NULL , NULL , new_dbname );
840
- printf ("connecting to new database: %s\n" , new_dbname );
854
+ if (!new_user )
855
+ printf ("connecting to new database: %s\n" , new_dbname );
856
+ else
857
+ printf ("connecting to new database: %s as user: %s\n" ,
858
+ new_dbname ,new_user );
841
859
if (PQstatus (settings -> db ) == CONNECTION_BAD ) {
842
860
fprintf (stderr , "%s\n" , PQerrorMessage (settings -> db ));
843
861
printf ("reconnecting to %s\n" , dbname );
@@ -1037,12 +1055,19 @@ HandleSlashCmds(PsqlSettings * settings,
1037
1055
* assuming it's not a one-character command. If it's a one-character
1038
1056
* command, this is meaningless.
1039
1057
*/
1058
+ char * optarg3 ;
1059
+ /*
1060
+ * Pointer inside the second <cmd> string to the argument of the slash command
1061
+ * assuming it's not a one-character command. If it's a one-character
1062
+ * command, this is meaningless.
1063
+ */
1040
1064
char * cmd ;
1041
1065
/*
1042
1066
* String: value of the slash command, less the slash and with escape
1043
1067
* sequences decoded.
1044
1068
*/
1045
1069
int blank_loc ;
1070
+ int blank_loc2 ;
1046
1071
/* Offset within <cmd> of first blank */
1047
1072
1048
1073
cmd = malloc (strlen (line )); /* unescaping better not make string grow. */
@@ -1064,12 +1089,20 @@ HandleSlashCmds(PsqlSettings * settings,
1064
1089
optarg = NULL ;
1065
1090
1066
1091
blank_loc = strcspn (cmd , " \t" );
1067
- if (blank_loc == 0 )
1092
+ if (blank_loc == 0 ) {
1068
1093
optarg2 = NULL ;
1069
- else
1094
+ optarg3 = NULL ;
1095
+ } else {
1070
1096
optarg2 = cmd + blank_loc + strspn (cmd + blank_loc , " \t" );
1071
-
1072
-
1097
+ blank_loc2 = strcspn (optarg2 , " \t" );
1098
+ if (blank_loc2 == 0 || * (optarg2 + blank_loc2 ) == '\0' )
1099
+ optarg3 = NULL ;
1100
+ else {
1101
+ optarg3 = optarg2 + blank_loc2 + strspn (optarg2 + blank_loc2 , " \t" );
1102
+ * (optarg2 + blank_loc2 ) = '\0' ;
1103
+ }
1104
+ }
1105
+
1073
1106
switch (cmd [0 ]) {
1074
1107
case 'a' : /* toggles to align fields on output */
1075
1108
toggle (settings , & settings -> opt .align , "field alignment" );
@@ -1092,9 +1125,9 @@ HandleSlashCmds(PsqlSettings * settings,
1092
1125
if (strncmp (cmd , "copy " , strlen ("copy " )) == 0 )
1093
1126
do_copy (optarg2 , settings );
1094
1127
else if (strncmp (cmd , "connect " , strlen ("connect " )) == 0 )
1095
- do_connect (optarg2 , settings );
1128
+ do_connect (optarg2 , optarg3 , settings );
1096
1129
else
1097
- do_connect (optarg , settings );
1130
+ do_connect (optarg , optarg2 , settings );
1098
1131
}
1099
1132
break ;
1100
1133
case 'd' : /* \d describe tables or columns in a table */
0 commit comments