fgets(buf, 3, stdin);
if (buf[0] == 'x')
return false;
- fflush(stdin);
}
cancelConn = pset->db;
{
success = true;
printQuery(results, &pset->popt, pset->queryFout);
- fflush(pset->queryFout);
}
break;
case PGRES_EMPTY_QUERY:
case PGRES_COMMAND_OK:
success = true;
pset->lastOid = PQoidValue(results);
- if (!GetVariableBool(pset->vars, "quiet")) {
+ if (!GetVariableBool(pset->vars, "quiet"))
fprintf(pset->queryFout, "%s\n", PQcmdStatus(results));
- fflush(pset->queryFout);
- }
break;
case PGRES_COPY_OUT:
case PGRES_BAD_RESPONSE:
success = false;
fputs(PQerrorMessage(pset->db), pset->queryFout);
- fflush(pset->queryFout);
break;
}
+ fflush(pset->queryFout);
+
if (PQstatus(pset->db) == CONNECTION_BAD)
{
fputs("The connection to the server was lost. Attempting reset: ", stderr);
fprintf(pset->queryFout, "Asynchronous NOTIFY '%s' from backend with pid '%d' received.\n",
notify->relname, notify->be_pid);
free(notify);
+ fflush(pset->queryFout);
}
if (results)
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;
query_buf = createPQExpBuffer();
- if (!query_buf)
+ previous_buf = createPQExpBuffer();
+ if (!query_buf || !previous_buf)
{
perror("createPQExpBuffer");
exit(EXIT_FAILURE);
{
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
puts(line);
- slashCmdStatus = CMD_UNKNOWN;
-
len = strlen(line);
query_start = 0;
/* 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
/* 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;
}
/* 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
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? */
} /* for (line) */
- if (!success && die_on_error && !pset->cur_cmd_interactive)
- {
- successResult = EXIT_USER;
- break;
- }
-
-
if (slashCmdStatus == CMD_TERMINATE)
{
successResult = EXIT_SUCCESS;
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;
}
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;
(void)end; /* not used */
- rl_filename_quoting_desired = 1;
+ rl_completion_append_character = ' ';
/* Clear a few things. */
completion_charp = NULL;
COMPLETE_WITH_QUERY(Query_for_list_of_tables);
+/* ... FROM ... */
+ else if (strcasecmp(prev_wd, "FROM") == 0 )
+ COMPLETE_WITH_QUERY(Query_for_list_of_tables);
+
+
/* Backslash commands */
else if (strcmp(prev_wd, "\\connect")==0 || strcmp(prev_wd, "\\c")==0)
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
COMPLETE_WITH_LIST(sql_commands);
else if (strcmp(prev_wd, "\\pset")==0) {
char * my_list[] = { "format", "border", "expanded", "null", "fieldsep",
- "tuples_only", "title", "tableattr", "pager" };
+ "tuples_only", "title", "tableattr", "pager", NULL };
COMPLETE_WITH_LIST(my_list);
}
else if( strcmp(prev_wd, "\\e")==0 || strcmp(prev_wd, "\\edit")==0 ||
of default list. If we were to just return NULL, readline automatically
attempts filename completion, and that's usually no good. */
if (matches == NULL) {
- char * my_list[] = { "", "", NULL };
- COMPLETE_WITH_LIST(my_list);
+ COMPLETE_WITH_CONST("");
+ rl_completion_append_character = '\0';
}