Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 77a4729

Browse files
committed
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
1 parent 97dec77 commit 77a4729

File tree

7 files changed

+63
-40
lines changed

7 files changed

+63
-40
lines changed

src/bin/psql/command.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ exec_command(const char *cmd,
624624

625625
if (!options[0])
626626
{
627-
fprintf(stderr, "Usage \\%s <filename>\n", cmd);
627+
fprintf(stderr, "Usage: \\%s <filename>\n", cmd);
628628
success = false;
629629
}
630630
else

src/bin/psql/common.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ SendQuery(PsqlSettings *pset, const char *query)
433433
fgets(buf, 3, stdin);
434434
if (buf[0] == 'x')
435435
return false;
436-
fflush(stdin);
437436
}
438437

439438
cancelConn = pset->db;
@@ -479,7 +478,6 @@ SendQuery(PsqlSettings *pset, const char *query)
479478
{
480479
success = true;
481480
printQuery(results, &pset->popt, pset->queryFout);
482-
fflush(pset->queryFout);
483481
}
484482
break;
485483
case PGRES_EMPTY_QUERY:
@@ -488,10 +486,8 @@ SendQuery(PsqlSettings *pset, const char *query)
488486
case PGRES_COMMAND_OK:
489487
success = true;
490488
pset->lastOid = PQoidValue(results);
491-
if (!GetVariableBool(pset->vars, "quiet")) {
489+
if (!GetVariableBool(pset->vars, "quiet"))
492490
fprintf(pset->queryFout, "%s\n", PQcmdStatus(results));
493-
fflush(pset->queryFout);
494-
}
495491
break;
496492

497493
case PGRES_COPY_OUT:
@@ -515,10 +511,11 @@ SendQuery(PsqlSettings *pset, const char *query)
515511
case PGRES_BAD_RESPONSE:
516512
success = false;
517513
fputs(PQerrorMessage(pset->db), pset->queryFout);
518-
fflush(pset->queryFout);
519514
break;
520515
}
521516

517+
fflush(pset->queryFout);
518+
522519
if (PQstatus(pset->db) == CONNECTION_BAD)
523520
{
524521
fputs("The connection to the server was lost. Attempting reset: ", stderr);
@@ -541,6 +538,7 @@ SendQuery(PsqlSettings *pset, const char *query)
541538
fprintf(pset->queryFout, "Asynchronous NOTIFY '%s' from backend with pid '%d' received.\n",
542539
notify->relname, notify->be_pid);
543540
free(notify);
541+
fflush(pset->queryFout);
544542
}
545543

546544
if (results)

src/bin/psql/input.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ gets_interactive(const char *prompt)
3232

3333
#ifdef USE_READLINE
3434
if (useReadline)
35-
{
3635
s = readline(prompt);
37-
fputc('\r', stdout);
38-
fflush(stdout);
39-
}
4036
else
4137
{
4238
#endif

src/bin/psql/mainloop.c

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ int
2929
MainLoop(PsqlSettings *pset, FILE *source)
3030
{
3131
PQExpBuffer query_buf; /* buffer for query being accumulated */
32+
PQExpBuffer previous_buf; /* if there isn't anything in the new buffer
33+
yet, use this one for \e, etc. */
3234
char *line; /* current line of input */
3335
int len; /* length of the line */
3436
int successResult = EXIT_SUCCESS;
@@ -63,7 +65,8 @@ MainLoop(PsqlSettings *pset, FILE *source)
6365

6466

6567
query_buf = createPQExpBuffer();
66-
if (!query_buf)
68+
previous_buf = createPQExpBuffer();
69+
if (!query_buf || !previous_buf)
6770
{
6871
perror("createPQExpBuffer");
6972
exit(EXIT_FAILURE);
@@ -80,21 +83,21 @@ MainLoop(PsqlSettings *pset, FILE *source)
8083
{
8184
if (slashCmdStatus == CMD_NEWEDIT)
8285
{
83-
8486
/*
8587
* just returned from editing the line? then just copy to the
8688
* input buffer
8789
*/
88-
line = strdup(query_buf->data);
90+
line = xstrdup(query_buf->data);
8991
resetPQExpBuffer(query_buf);
90-
/* reset parsing state since we are rescanning whole query */
92+
/* reset parsing state since we are rescanning whole line */
9193
xcomment = false;
9294
in_quote = 0;
9395
paren_level = 0;
96+
slashCmdStatus = CMD_UNKNOWN;
9497
}
9598
else
9699
{
97-
100+
fflush(stdout);
98101
/*
99102
* otherwise, set interactive prompt if necessary and get
100103
* another line
@@ -170,8 +173,6 @@ MainLoop(PsqlSettings *pset, FILE *source)
170173
puts(line);
171174

172175

173-
slashCmdStatus = CMD_UNKNOWN;
174-
175176
len = strlen(line);
176177
query_start = 0;
177178

@@ -275,11 +276,13 @@ MainLoop(PsqlSettings *pset, FILE *source)
275276
/* semicolon? then send query */
276277
else if (line[i] == ';' && !was_bslash)
277278
{
279+
/* delete the old query buffer from last time around */
280+
if (slashCmdStatus == CMD_SEND)
281+
278282
line[i] = '\0';
279283
/* is there anything else on the line? */
280284
if (line[query_start + strspn(line + query_start, " \t")] != '\0')
281285
{
282-
283286
/*
284287
* insert a cosmetic newline, if this is not the first
285288
* line in the buffer
@@ -292,8 +295,11 @@ MainLoop(PsqlSettings *pset, FILE *source)
292295

293296
/* execute query */
294297
success = SendQuery(pset, query_buf->data);
298+
slashCmdStatus = success ? CMD_SEND : CMD_ERROR;
295299

296-
resetPQExpBuffer(query_buf);
300+
resetPQExpBuffer(previous_buf);
301+
appendPQExpBufferStr(previous_buf, query_buf->data);
302+
resetPQExpBuffer(query_buf);
297303
query_start = i + thislen;
298304
}
299305

@@ -316,7 +322,6 @@ MainLoop(PsqlSettings *pset, FILE *source)
316322
/* is there anything else on the line? */
317323
if (line[query_start + strspn(line + query_start, " \t")] != '\0')
318324
{
319-
320325
/*
321326
* insert a cosmetic newline, if this is not the first
322327
* line in the buffer
@@ -327,17 +332,27 @@ MainLoop(PsqlSettings *pset, FILE *source)
327332
appendPQExpBufferStr(query_buf, line + query_start);
328333
}
329334

330-
/* handle backslash command */
331-
332-
slashCmdStatus = HandleSlashCmds(pset, &line[i], query_buf, &end_of_cmd);
335+
/* handle backslash command */
336+
slashCmdStatus = HandleSlashCmds(pset, &line[i],
337+
query_buf->len>0 ? query_buf : previous_buf,
338+
&end_of_cmd);
333339

334340
success = slashCmdStatus != CMD_ERROR;
335341

342+
if ((slashCmdStatus == CMD_SEND || slashCmdStatus == CMD_NEWEDIT) &&
343+
query_buf->len == 0) {
344+
/* copy previous buffer to current for for handling */
345+
appendPQExpBufferStr(query_buf, previous_buf->data);
346+
}
347+
336348
if (slashCmdStatus == CMD_SEND)
337349
{
338350
success = SendQuery(pset, query_buf->data);
339-
resetPQExpBuffer(query_buf);
340351
query_start = i + thislen;
352+
353+
resetPQExpBuffer(previous_buf);
354+
appendPQExpBufferStr(previous_buf, query_buf->data);
355+
resetPQExpBuffer(query_buf);
341356
}
342357

343358
/* is there anything left after the backslash command? */
@@ -358,13 +373,6 @@ MainLoop(PsqlSettings *pset, FILE *source)
358373
} /* for (line) */
359374

360375

361-
if (!success && die_on_error && !pset->cur_cmd_interactive)
362-
{
363-
successResult = EXIT_USER;
364-
break;
365-
}
366-
367-
368376
if (slashCmdStatus == CMD_TERMINATE)
369377
{
370378
successResult = EXIT_SUCCESS;
@@ -387,7 +395,17 @@ MainLoop(PsqlSettings *pset, FILE *source)
387395
if (query_buf->data[0] != '\0' && GetVariableBool(pset->vars, "singleline"))
388396
{
389397
success = SendQuery(pset, query_buf->data);
390-
resetPQExpBuffer(query_buf);
398+
slashCmdStatus = success ? CMD_SEND : CMD_ERROR;
399+
resetPQExpBuffer(previous_buf);
400+
appendPQExpBufferStr(previous_buf, query_buf->data);
401+
resetPQExpBuffer(query_buf);
402+
}
403+
404+
405+
if (!success && die_on_error && !pset->cur_cmd_interactive)
406+
{
407+
successResult = EXIT_USER;
408+
break;
391409
}
392410

393411

@@ -397,9 +415,10 @@ MainLoop(PsqlSettings *pset, FILE *source)
397415
successResult = EXIT_BADCONN;
398416
break;
399417
}
400-
} /* while */
418+
} /* while !EOF */
401419

402420
destroyPQExpBuffer(query_buf);
421+
destroyPQExpBuffer(previous_buf);
403422

404423
pset->cur_cmd_source = prev_cmd_source;
405424
pset->cur_cmd_interactive = prev_cmd_interactive;

src/bin/psql/print.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <libpq-fe.h>
1919
#include <postgres_ext.h> /* for Oid type */
2020

21-
#define DEFAULT_PAGER "/bin/more"
21+
#define DEFAULT_PAGER "more"
2222

2323

2424

@@ -325,6 +325,11 @@ print_aligned_vertical(const char *title, const char * const * headers,
325325
dwidth = 0;
326326
char *divider;
327327

328+
if (cells[0] == NULL) {
329+
puts("(No rows)\n");
330+
return;
331+
}
332+
328333
/* count columns and find longest header */
329334
for (ptr = headers; *ptr; ptr++)
330335
{

src/bin/psql/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "print.h"
1414

1515
#define DEFAULT_FIELD_SEP "|"
16-
#define DEFAULT_EDITOR "/bin/vi"
16+
#define DEFAULT_EDITOR "vi"
1717

1818
#define DEFAULT_PROMPT1 "%/%R%# "
1919
#define DEFAULT_PROMPT2 "%/%R%# "

src/bin/psql/tab-complete.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ char ** psql_completion(char *text, int start, int end)
203203

204204
(void)end; /* not used */
205205

206-
rl_filename_quoting_desired = 1;
206+
rl_completion_append_character = ' ';
207207

208208
/* Clear a few things. */
209209
completion_charp = NULL;
@@ -501,6 +501,11 @@ char ** psql_completion(char *text, int start, int end)
501501
COMPLETE_WITH_QUERY(Query_for_list_of_tables);
502502

503503

504+
/* ... FROM ... */
505+
else if (strcasecmp(prev_wd, "FROM") == 0 )
506+
COMPLETE_WITH_QUERY(Query_for_list_of_tables);
507+
508+
504509
/* Backslash commands */
505510
else if (strcmp(prev_wd, "\\connect")==0 || strcmp(prev_wd, "\\c")==0)
506511
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
@@ -510,7 +515,7 @@ char ** psql_completion(char *text, int start, int end)
510515
COMPLETE_WITH_LIST(sql_commands);
511516
else if (strcmp(prev_wd, "\\pset")==0) {
512517
char * my_list[] = { "format", "border", "expanded", "null", "fieldsep",
513-
"tuples_only", "title", "tableattr", "pager" };
518+
"tuples_only", "title", "tableattr", "pager", NULL };
514519
COMPLETE_WITH_LIST(my_list);
515520
}
516521
else if( strcmp(prev_wd, "\\e")==0 || strcmp(prev_wd, "\\edit")==0 ||
@@ -541,8 +546,8 @@ char ** psql_completion(char *text, int start, int end)
541546
of default list. If we were to just return NULL, readline automatically
542547
attempts filename completion, and that's usually no good. */
543548
if (matches == NULL) {
544-
char * my_list[] = { "", "", NULL };
545-
COMPLETE_WITH_LIST(my_list);
549+
COMPLETE_WITH_CONST("");
550+
rl_completion_append_character = '\0';
546551
}
547552

548553

0 commit comments

Comments
 (0)