7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.15 1996/07/31 02:11:23 scrappy Exp $
10
+ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.15.2.1 1996/08/27 17:33:33 scrappy Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -133,7 +133,7 @@ slashUsage(PsqlSettings *ps)
133
133
fprintf (stderr ,"\t \\C [<captn>] -- set html3 caption (currently '%s')\n" , ps -> opt .caption ? ps -> opt .caption : "" );
134
134
fprintf (stderr ,"\t \\c <dbname> -- connect to new database (currently '%s')\n" , PQdb (ps -> db ));
135
135
fprintf (stderr ,"\t \\d [<table>] -- list tables in database or columns in <table>,* for all\n" );
136
- fprintf (stderr ,"\t \\e [<fname>] -- edit the current query buffer or <fname>, \E execute too\n" );
136
+ fprintf (stderr ,"\t \\e [<fname>] -- edit the current query buffer or <fname>,\ \E execute too\n" );
137
137
fprintf (stderr ,"\t \\f [<sep>] -- change field separater (currently '%s')\n" , ps -> opt .fieldSep );
138
138
fprintf (stderr ,"\t \\g [<fname>] -- send query to backend [and place results in <fname>]\n" );
139
139
fprintf (stderr ,"\t \\g |<cmd> -- send query to backend and pipe results into <cmd>\n" );
@@ -988,6 +988,10 @@ MainLoop(PsqlSettings *settings, FILE *source)
988
988
bool querySent = 0 ;
989
989
bool interactive ;
990
990
READ_ROUTINE GetNextLine ;
991
+ bool connected = 1 ;
992
+ /* We are connected to the backend (last time we looked) */
993
+ bool eof = 0 ;
994
+ /* We've reached the end of our command input. */
991
995
992
996
interactive = ((source == stdin ) && !settings -> notty );
993
997
#define PROMPT "=> "
@@ -1012,9 +1016,13 @@ MainLoop(PsqlSettings *settings, FILE *source)
1012
1016
query [0 ] = '\0' ;
1013
1017
1014
1018
/* main loop for getting queries and executing them */
1015
- while ((line = GetNextLine (settings -> prompt , source )) != NULL )
1016
- {
1017
- exitStatus = 0 ;
1019
+ while (connected && !eof ) {
1020
+ line = GetNextLine (settings -> prompt , source );
1021
+ if (line == NULL ) { /* No more input. Time to quit */
1022
+ printf ("EOF\n" ); /* Goes on prompt line */
1023
+ eof = 1 ;
1024
+ } else {
1025
+ exitStatus = 0 ;
1018
1026
line = rightTrim (line ); /* remove whitespaces on the right, incl. \n's */
1019
1027
1020
1028
if (line [0 ] == '\0' ) {
@@ -1064,10 +1072,14 @@ MainLoop(PsqlSettings *settings, FILE *source)
1064
1072
slashCmdStatus = HandleSlashCmds (settings ,
1065
1073
line ,
1066
1074
query );
1067
- if (slashCmdStatus == 1 )
1075
+ if (slashCmdStatus == 1 ) {
1076
+ free (line );
1068
1077
continue ;
1069
- if (slashCmdStatus == 2 )
1078
+ }
1079
+ if (slashCmdStatus == 2 ) {
1080
+ free (line );
1070
1081
break ;
1082
+ }
1071
1083
if (slashCmdStatus == 0 )
1072
1084
sendQuery = 1 ;
1073
1085
}
@@ -1095,11 +1107,16 @@ MainLoop(PsqlSettings *settings, FILE *source)
1095
1107
1096
1108
exitStatus = SendQuery (settings , query );
1097
1109
querySent = 1 ;
1110
+ if (PQstatus (settings -> db ) == CONNECTION_BAD ) {
1111
+ connected = 0 ;
1112
+ fprintf (stderr , "We have lost the connection to the backend, so "
1113
+ "further processing is impossible. Terminating.\n" );
1114
+ }
1098
1115
}
1099
-
1100
- free ( line ); /* free storage malloc'd by GetNextLine */
1101
- } /* while */
1102
- return exitStatus ;
1116
+ free ( line ); /* free storage malloc'd by GetNextLine */
1117
+ }
1118
+ } /* while */
1119
+ return exitStatus ;
1103
1120
}
1104
1121
1105
1122
int
@@ -1196,7 +1213,7 @@ main(int argc, char **argv)
1196
1213
settings .opt .tableOpt = optarg ;
1197
1214
break ;
1198
1215
case 'x' :
1199
- settings .opt .expanded = 0 ;
1216
+ settings .opt .expanded = 1 ;
1200
1217
break ;
1201
1218
default :
1202
1219
usage (argv [0 ]);
@@ -1274,7 +1291,9 @@ handleCopyOut(PGresult *res, bool quiet)
1274
1291
while (!copydone ) {
1275
1292
ret = PQgetline (res -> conn , copybuf , COPYBUFSIZ );
1276
1293
1277
- if (copybuf [0 ] == '.' && copybuf [1 ] == '\0' ) {
1294
+ if (copybuf [0 ] == '\\' &&
1295
+ copybuf [1 ] == '.' &&
1296
+ copybuf [2 ] == '\0' ) {
1278
1297
copydone = true; /* don't print this... */
1279
1298
} else {
1280
1299
fputs (copybuf , stdout );
@@ -1308,7 +1327,7 @@ handleCopyIn(PGresult *res, bool quiet)
1308
1327
1309
1328
if (!quiet ) {
1310
1329
fputs ("Enter info followed by a newline\n" , stdout );
1311
- fputs ("End with a dot on a line by itself.\n" , stdout );
1330
+ fputs ("End with a backslash and a period on a line by itself.\n" , stdout );
1312
1331
}
1313
1332
1314
1333
/*
@@ -1337,14 +1356,14 @@ handleCopyIn(PGresult *res, bool quiet)
1337
1356
}
1338
1357
if (c == EOF ) {
1339
1358
/* reading from stdin, but from a file */
1340
- PQputline (res -> conn , "." );
1359
+ PQputline (res -> conn , "\\ ." );
1341
1360
copydone = true;
1342
1361
break ;
1343
1362
}
1344
1363
* s = '\0' ;
1345
1364
PQputline (res -> conn , copybuf );
1346
1365
if (firstload ) {
1347
- if (!strcmp (copybuf , "." )) {
1366
+ if (!strcmp (copybuf , "\\ ." )) {
1348
1367
copydone = true;
1349
1368
}
1350
1369
firstload = false;
0 commit comments