Skip to content

Commit d9cc832

Browse files
committed
Somehow, we got out of sync here
Pointed out by Bryan
1 parent 8be2860 commit d9cc832

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/bin/psql/psql.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* 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 $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -133,7 +133,7 @@ slashUsage(PsqlSettings *ps)
133133
fprintf(stderr,"\t \\C [<captn>] -- set html3 caption (currently '%s')\n", ps->opt.caption? ps->opt.caption: "");
134134
fprintf(stderr,"\t \\c <dbname> -- connect to new database (currently '%s')\n", PQdb(ps->db));
135135
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");
137137
fprintf(stderr,"\t \\f [<sep>] -- change field separater (currently '%s')\n", ps->opt.fieldSep);
138138
fprintf(stderr,"\t \\g [<fname>] -- send query to backend [and place results in <fname>]\n");
139139
fprintf(stderr,"\t \\g |<cmd> -- send query to backend and pipe results into <cmd>\n");
@@ -988,6 +988,10 @@ MainLoop(PsqlSettings *settings, FILE *source)
988988
bool querySent = 0;
989989
bool interactive;
990990
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. */
991995

992996
interactive = ((source == stdin) && !settings->notty);
993997
#define PROMPT "=> "
@@ -1012,9 +1016,13 @@ MainLoop(PsqlSettings *settings, FILE *source)
10121016
query[0] = '\0';
10131017

10141018
/* 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;
10181026
line = rightTrim(line); /* remove whitespaces on the right, incl. \n's */
10191027

10201028
if (line[0] == '\0') {
@@ -1064,10 +1072,14 @@ MainLoop(PsqlSettings *settings, FILE *source)
10641072
slashCmdStatus = HandleSlashCmds(settings,
10651073
line,
10661074
query);
1067-
if (slashCmdStatus == 1)
1075+
if (slashCmdStatus == 1) {
1076+
free(line);
10681077
continue;
1069-
if (slashCmdStatus == 2)
1078+
}
1079+
if (slashCmdStatus == 2) {
1080+
free(line);
10701081
break;
1082+
}
10711083
if (slashCmdStatus == 0)
10721084
sendQuery = 1;
10731085
}
@@ -1095,11 +1107,16 @@ MainLoop(PsqlSettings *settings, FILE *source)
10951107

10961108
exitStatus = SendQuery(settings, query);
10971109
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+
}
10981115
}
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;
11031120
}
11041121

11051122
int
@@ -1196,7 +1213,7 @@ main(int argc, char **argv)
11961213
settings.opt.tableOpt = optarg;
11971214
break;
11981215
case 'x':
1199-
settings.opt.expanded = 0;
1216+
settings.opt.expanded = 1;
12001217
break;
12011218
default:
12021219
usage(argv[0]);
@@ -1274,7 +1291,9 @@ handleCopyOut(PGresult *res, bool quiet)
12741291
while (!copydone) {
12751292
ret = PQgetline(res->conn, copybuf, COPYBUFSIZ);
12761293

1277-
if (copybuf[0] == '.' && copybuf[1] =='\0') {
1294+
if (copybuf[0] == '\\' &&
1295+
copybuf[1] == '.' &&
1296+
copybuf[2] =='\0') {
12781297
copydone = true; /* don't print this... */
12791298
} else {
12801299
fputs(copybuf, stdout);
@@ -1308,7 +1327,7 @@ handleCopyIn(PGresult *res, bool quiet)
13081327

13091328
if (!quiet) {
13101329
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);
13121331
}
13131332

13141333
/*
@@ -1337,14 +1356,14 @@ handleCopyIn(PGresult *res, bool quiet)
13371356
}
13381357
if (c == EOF) {
13391358
/* reading from stdin, but from a file */
1340-
PQputline(res->conn, ".");
1359+
PQputline(res->conn, "\\.");
13411360
copydone = true;
13421361
break;
13431362
}
13441363
*s = '\0';
13451364
PQputline(res->conn, copybuf);
13461365
if (firstload) {
1347-
if (!strcmp(copybuf, ".")) {
1366+
if (!strcmp(copybuf, "\\.")) {
13481367
copydone = true;
13491368
}
13501369
firstload = false;

0 commit comments

Comments
 (0)