diff options
author | Bruce Momjian <bruce@momjian.us> | 1999-12-10 03:59:30 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 1999-12-10 03:59:30 +0000 |
commit | 77a47299366e0246e3ec7419a9f8472281394b1e (patch) | |
tree | f68c0692a91f465a5b3b1f4e765f0a7e61e936c4 /src/bin/psql/mainloop.c | |
parent | 97dec77fab612cfa285f6bd3dd5463a0d55526b2 (diff) | |
download | postgresql-77a47299366e0246e3ec7419a9f8472281394b1e.tar.gz postgresql-77a47299366e0246e3ec7419a9f8472281394b1e.zip |
This should fix the \e (\p, \g, ...) behaviour on an empty query buffer.
Also, minor tweakage of tab completion, and I hope the output is flushed
on time now.
--
Peter Eisentraut Sernanders väg 10:115
Diffstat (limited to 'src/bin/psql/mainloop.c')
-rw-r--r-- | src/bin/psql/mainloop.c | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c index 067be54e2cc..f85247689b9 100644 --- a/src/bin/psql/mainloop.c +++ b/src/bin/psql/mainloop.c @@ -29,6 +29,8 @@ int MainLoop(PsqlSettings *pset, FILE *source) { PQExpBuffer query_buf; /* buffer for query being accumulated */ + PQExpBuffer previous_buf; /* if there isn't anything in the new buffer + yet, use this one for \e, etc. */ char *line; /* current line of input */ int len; /* length of the line */ int successResult = EXIT_SUCCESS; @@ -63,7 +65,8 @@ MainLoop(PsqlSettings *pset, FILE *source) query_buf = createPQExpBuffer(); - if (!query_buf) + previous_buf = createPQExpBuffer(); + if (!query_buf || !previous_buf) { perror("createPQExpBuffer"); exit(EXIT_FAILURE); @@ -80,21 +83,21 @@ MainLoop(PsqlSettings *pset, FILE *source) { if (slashCmdStatus == CMD_NEWEDIT) { - /* * just returned from editing the line? then just copy to the * input buffer */ - line = strdup(query_buf->data); + line = xstrdup(query_buf->data); resetPQExpBuffer(query_buf); - /* reset parsing state since we are rescanning whole query */ + /* reset parsing state since we are rescanning whole line */ xcomment = false; in_quote = 0; paren_level = 0; + slashCmdStatus = CMD_UNKNOWN; } else { - + fflush(stdout); /* * otherwise, set interactive prompt if necessary and get * another line @@ -170,8 +173,6 @@ MainLoop(PsqlSettings *pset, FILE *source) puts(line); - slashCmdStatus = CMD_UNKNOWN; - len = strlen(line); query_start = 0; @@ -275,11 +276,13 @@ MainLoop(PsqlSettings *pset, FILE *source) /* semicolon? then send query */ else if (line[i] == ';' && !was_bslash) { + /* delete the old query buffer from last time around */ + if (slashCmdStatus == CMD_SEND) + line[i] = '\0'; /* is there anything else on the line? */ if (line[query_start + strspn(line + query_start, " \t")] != '\0') { - /* * insert a cosmetic newline, if this is not the first * line in the buffer @@ -292,8 +295,11 @@ MainLoop(PsqlSettings *pset, FILE *source) /* execute query */ success = SendQuery(pset, query_buf->data); + slashCmdStatus = success ? CMD_SEND : CMD_ERROR; - resetPQExpBuffer(query_buf); + resetPQExpBuffer(previous_buf); + appendPQExpBufferStr(previous_buf, query_buf->data); + resetPQExpBuffer(query_buf); query_start = i + thislen; } @@ -316,7 +322,6 @@ MainLoop(PsqlSettings *pset, FILE *source) /* is there anything else on the line? */ if (line[query_start + strspn(line + query_start, " \t")] != '\0') { - /* * insert a cosmetic newline, if this is not the first * line in the buffer @@ -327,17 +332,27 @@ MainLoop(PsqlSettings *pset, FILE *source) appendPQExpBufferStr(query_buf, line + query_start); } - /* handle backslash command */ - - slashCmdStatus = HandleSlashCmds(pset, &line[i], query_buf, &end_of_cmd); + /* handle backslash command */ + slashCmdStatus = HandleSlashCmds(pset, &line[i], + query_buf->len>0 ? query_buf : previous_buf, + &end_of_cmd); success = slashCmdStatus != CMD_ERROR; + if ((slashCmdStatus == CMD_SEND || slashCmdStatus == CMD_NEWEDIT) && + query_buf->len == 0) { + /* copy previous buffer to current for for handling */ + appendPQExpBufferStr(query_buf, previous_buf->data); + } + if (slashCmdStatus == CMD_SEND) { success = SendQuery(pset, query_buf->data); - resetPQExpBuffer(query_buf); query_start = i + thislen; + + resetPQExpBuffer(previous_buf); + appendPQExpBufferStr(previous_buf, query_buf->data); + resetPQExpBuffer(query_buf); } /* is there anything left after the backslash command? */ @@ -358,13 +373,6 @@ MainLoop(PsqlSettings *pset, FILE *source) } /* for (line) */ - if (!success && die_on_error && !pset->cur_cmd_interactive) - { - successResult = EXIT_USER; - break; - } - - if (slashCmdStatus == CMD_TERMINATE) { successResult = EXIT_SUCCESS; @@ -387,7 +395,17 @@ MainLoop(PsqlSettings *pset, FILE *source) if (query_buf->data[0] != '\0' && GetVariableBool(pset->vars, "singleline")) { success = SendQuery(pset, query_buf->data); - resetPQExpBuffer(query_buf); + slashCmdStatus = success ? CMD_SEND : CMD_ERROR; + resetPQExpBuffer(previous_buf); + appendPQExpBufferStr(previous_buf, query_buf->data); + resetPQExpBuffer(query_buf); + } + + + if (!success && die_on_error && !pset->cur_cmd_interactive) + { + successResult = EXIT_USER; + break; } @@ -397,9 +415,10 @@ MainLoop(PsqlSettings *pset, FILE *source) successResult = EXIT_BADCONN; break; } - } /* while */ + } /* while !EOF */ destroyPQExpBuffer(query_buf); + destroyPQExpBuffer(previous_buf); pset->cur_cmd_source = prev_cmd_source; pset->cur_cmd_interactive = prev_cmd_interactive; |