Skip to content

Commit 718bb2c

Browse files
committed
Moved psql \eset and \eshow to \encoding
Improved psql's Ctrl-C handling Fixed configure test for sigsetjmp that now even recognizes it if it's a macro
1 parent 5253c51 commit 718bb2c

15 files changed

+284
-182
lines changed

src/bin/psql/command.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.21 2000/02/20 02:37:40 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.22 2000/02/20 14:28:20 petere Exp $
77
*/
88
#include "postgres.h"
99
#include "command.h"
@@ -36,6 +36,9 @@
3636
#ifdef MULTIBYTE
3737
#include "miscadmin.h"
3838
#include "mb/pg_wchar.h"
39+
#else
40+
/* Grand unified hard-coded badness */
41+
#define pg_encoding_to_char(x) "SQL_ASCII"
3942
#endif
4043

4144

@@ -351,30 +354,29 @@ exec_command(const char *cmd,
351354
fputs("\n", fout);
352355
}
353356

354-
#ifdef MULTIBYTE
355-
/* \eset -- set client side encoding */
356-
else if (strcmp(cmd, "eset") == 0)
357+
/* \encoding -- set client side encoding */
358+
else if (strcmp(cmd, "encoding") == 0)
357359
{
358360
char *encoding = scan_option(&string, OT_NORMAL, NULL);
359-
if (PQsetClientEncoding(pset.db, encoding) == -1)
360-
{
361-
psql_error("\\%s: invalid encoding\n", cmd);
362-
}
363-
/* save encoding info into psql internal data */
364-
pset.encoding = PQclientEncoding(pset.db);
365-
free(encoding);
366-
}
367-
/* \eshow -- show encoding info */
368-
else if (strcmp(cmd, "eshow") == 0)
369-
{
370-
int encoding = PQclientEncoding(pset.db);
371-
if (encoding == -1)
372-
{
373-
psql_error("\\%s: there is no connection\n", cmd);
374-
}
375-
printf("%s\n", pg_encoding_to_char(encoding));
376-
}
361+
362+
if (!encoding)
363+
puts(pg_encoding_to_char(pset.encoding));
364+
else
365+
{
366+
#ifdef MULTIBYTE
367+
if (PQsetClientEncoding(pset.db, encoding) == -1)
368+
psql_error("%s: invalid encoding name\n", encoding);
369+
370+
/* save encoding info into psql internal data */
371+
pset.encoding = PQclientEncoding(pset.db);
372+
SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
373+
#else
374+
psql_error("\\%s: multi-byte support is not enabled\n", cmd);
377375
#endif
376+
}
377+
free(encoding);
378+
}
379+
378380
/* \f -- change field separator */
379381
else if (strcmp(cmd, "f") == 0)
380382
{
@@ -425,7 +427,7 @@ exec_command(const char *cmd,
425427
}
426428
else
427429
{
428-
success = process_file(fname);
430+
success = process_file(fname) == EXIT_SUCCESS;
429431
free (fname);
430432
}
431433
}
@@ -1148,6 +1150,7 @@ do_connect(const char *new_dbname, const char *new_user)
11481150
SetVariable(pset.vars, "USER", NULL);
11491151
SetVariable(pset.vars, "HOST", NULL);
11501152
SetVariable(pset.vars, "PORT", NULL);
1153+
SetVariable(pset.vars, "ENCODING", NULL);
11511154

11521155
/* If dbname is "" then use old name, else new one (even if NULL) */
11531156
if (oldconn && new_dbname && PQdb(oldconn) && strcmp(new_dbname, "") == 0)
@@ -1247,6 +1250,7 @@ do_connect(const char *new_dbname, const char *new_user)
12471250
SetVariable(pset.vars, "USER", PQuser(pset.db));
12481251
SetVariable(pset.vars, "HOST", PQhost(pset.db));
12491252
SetVariable(pset.vars, "PORT", PQport(pset.db));
1253+
SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
12501254

12511255
pset.issuper = test_superuser(PQuser(pset.db));
12521256

@@ -1471,7 +1475,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
14711475
* Read commands from filename and then them to the main processing loop
14721476
* Handler for \i, but can be used for other things as well.
14731477
*/
1474-
bool
1478+
int
14751479
process_file(char *filename)
14761480
{
14771481
FILE *fd;
@@ -1494,7 +1498,7 @@ process_file(char *filename)
14941498
result = MainLoop(fd);
14951499
fclose(fd);
14961500
pset.inputfile = oldfilename;
1497-
return (result == EXIT_SUCCESS);
1501+
return result;
14981502
}
14991503

15001504

src/bin/psql/command.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.h,v 1.8 2000/02/16 13:15:26 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.h,v 1.9 2000/02/20 14:28:20 petere Exp $
77
*/
88
#ifndef COMMAND_H
99
#define COMMAND_H
@@ -31,7 +31,7 @@ HandleSlashCmds(const char *line,
3131
PQExpBuffer query_buf,
3232
const char **end_of_cmd);
3333

34-
bool
34+
int
3535
process_file(char *filename);
3636

3737
bool

src/bin/psql/common.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.15 2000/02/20 02:37:40 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.16 2000/02/20 14:28:20 petere Exp $
77
*/
88
#include "postgres.h"
99
#include "common.h"
@@ -19,6 +19,7 @@
1919
#include <signal.h>
2020
#ifndef WIN32
2121
#include <unistd.h> /* for write() */
22+
#include <setjmp.h>
2223
#else
2324
#include <io.h> /* for _write() */
2425
#include <win32.h>
@@ -34,7 +35,7 @@
3435
#include "copy.h"
3536
#include "prompt.h"
3637
#include "print.h"
37-
38+
#include "mainloop.h"
3839

3940

4041
/*
@@ -184,7 +185,7 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
184185
if (!destination)
185186
return NULL;
186187
if (prompt)
187-
fputs(prompt, stdout);
188+
fputs(prompt, stderr);
188189

189190
#ifdef HAVE_TERMIOS_H
190191
if (!echo)
@@ -238,14 +239,22 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
238239
*/
239240

240241
static PGconn *cancelConn;
242+
volatile bool cancel_pressed;
241243

242244
#define write_stderr(String) write(fileno(stderr), String, strlen(String))
243245

244-
static void
246+
void
245247
handle_sigint(SIGNAL_ARGS)
246248
{
247249
if (cancelConn == NULL)
250+
#ifndef WIN32
251+
siglongjmp(main_loop_jmp, 1);
252+
#else
248253
return;
254+
#endif
255+
256+
cancel_pressed = true;
257+
249258
/* Try to send cancel request */
250259
if (PQrequestCancel(cancelConn))
251260
write_stderr("\nCancel request sent\n");
@@ -287,15 +296,8 @@ PSQLexec(const char *query)
287296
return NULL;
288297

289298
cancelConn = pset.db;
290-
#ifndef WIN32
291-
pqsignal(SIGINT, handle_sigint); /* control-C => cancel */
292-
#endif
293-
294299
res = PQexec(pset.db, query);
295-
296-
#ifndef WIN32
297-
pqsignal(SIGINT, SIG_DFL); /* now control-C is back to normal */
298-
#endif
300+
cancelConn = NULL;
299301

300302
if (PQstatus(pset.db) == CONNECTION_BAD)
301303
{
@@ -316,6 +318,7 @@ PSQLexec(const char *query)
316318
SetVariable(pset.vars, "HOST", NULL);
317319
SetVariable(pset.vars, "PORT", NULL);
318320
SetVariable(pset.vars, "USER", NULL);
321+
SetVariable(pset.vars, "ENCODING", NULL);
319322
return NULL;
320323
}
321324
else
@@ -359,7 +362,7 @@ SendQuery(const char *query)
359362

360363
if (!pset.db)
361364
{
362-
psql_error("you are currently not connected to a database.\n");
365+
psql_error("You are currently not connected to a database.\n");
363366
return false;
364367
}
365368

@@ -384,15 +387,8 @@ SendQuery(const char *query)
384387
}
385388

386389
cancelConn = pset.db;
387-
#ifndef WIN32
388-
pqsignal(SIGINT, handle_sigint);
389-
#endif
390-
391390
results = PQexec(pset.db, query);
392-
393-
#ifndef WIN32
394-
pqsignal(SIGINT, SIG_DFL);
395-
#endif
391+
cancelConn = NULL;
396392

397393
if (results == NULL)
398394
{
@@ -494,6 +490,7 @@ SendQuery(const char *query)
494490
SetVariable(pset.vars, "HOST", NULL);
495491
SetVariable(pset.vars, "PORT", NULL);
496492
SetVariable(pset.vars, "USER", NULL);
493+
SetVariable(pset.vars, "ENCODING", NULL);
497494
return false;
498495
}
499496
else

src/bin/psql/common.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.6 2000/02/16 13:15:26 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.7 2000/02/20 14:28:20 petere Exp $
77
*/
88
#ifndef COMMON_H
99
#define COMMON_H
1010

11+
#include "postgres.h"
12+
#include <signal.h>
13+
#include "pqsignal.h"
1114
#include "libpq-fe.h"
1215

1316
char * xstrdup(const char *string);
@@ -25,6 +28,9 @@ void NoticeProcessor(void * arg, const char * message);
2528

2629
char * simple_prompt(const char *prompt, int maxlen, bool echo);
2730

31+
extern volatile bool cancel_pressed;
32+
void handle_sigint(SIGNAL_ARGS);
33+
2834
PGresult * PSQLexec(const char *query);
2935

3036
bool SendQuery(const char *query);

src/bin/psql/help.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.21 2000/02/20 02:37:40 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.22 2000/02/20 14:28:20 petere Exp $
77
*/
88
#include "postgres.h"
99
#include "help.h"
@@ -29,6 +29,13 @@
2929
#include "common.h"
3030
#include "sql_help.h"
3131

32+
/*
33+
* PLEASE:
34+
* If you change something in this file, also make the same changes
35+
* in the DocBook documentation, file ref/psql-ref.sgml. If you don't
36+
* know how to do it, please find someone who can help you.
37+
*/
38+
3239

3340
/*
3441
* usage
@@ -200,10 +207,7 @@ slashUsage(void)
200207
fprintf(fout, " \\dT list data types\n");
201208
fprintf(fout, " \\e [fname] edit the current query buffer or <fname> with external editor\n");
202209
fprintf(fout, " \\echo <text> write text to stdout\n");
203-
#ifdef MULTIBYTE
204-
fprintf(fout, " \\eset <encoding> set client encoding\n");
205-
fprintf(fout, " \\eshow show client encoding\n");
206-
#endif
210+
fprintf(fout, " \\encoding <encoding> set client encoding\n");
207211
fprintf(fout, " \\g [fname] send query to backend (and results in <fname> or |pipe)\n");
208212
fprintf(fout, " \\h [cmd] help on syntax of sql commands, * for all commands\n");
209213
fprintf(fout, " \\i <fname> read and execute queries from filename\n");

src/bin/psql/input.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.11 2000/02/20 02:37:40 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.12 2000/02/20 14:28:20 petere Exp $
77
*/
88
#include "postgres.h"
99
#include "input.h"
@@ -148,6 +148,8 @@ initializeInput(int flags)
148148
}
149149
}
150150
#endif
151+
152+
atexit(finishInput);
151153
}
152154

153155

src/bin/psql/mainloop.c

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.21 2000/02/20 02:37:40 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.22 2000/02/20 14:28:20 petere Exp $
77
*/
88
#include "postgres.h"
99
#include "mainloop.h"
@@ -16,6 +16,11 @@
1616
#include "common.h"
1717
#include "command.h"
1818

19+
#ifndef WIN32
20+
#include <setjmp.h>
21+
22+
sigjmp_buf main_loop_jmp;
23+
#endif
1924

2025

2126
/*
@@ -88,6 +93,40 @@ MainLoop(FILE *source)
8893
/* main loop to get queries and execute them */
8994
while (1)
9095
{
96+
/*
97+
* Welcome code for Control-C
98+
*/
99+
if (cancel_pressed)
100+
{
101+
cancel_pressed = false;
102+
if (!pset.cur_cmd_interactive)
103+
{
104+
/*
105+
* You get here if you stopped a script with Ctrl-C and a query
106+
* cancel was issued. In that case we don't do the longjmp, so
107+
* the query routine can finish nicely.
108+
*/
109+
successResult = EXIT_USER;
110+
break;
111+
}
112+
}
113+
#ifndef WIN32
114+
if (sigsetjmp(main_loop_jmp, 1) != 0)
115+
{
116+
/* got here with longjmp */
117+
if (pset.cur_cmd_interactive)
118+
{
119+
fputc('\n', stdout);
120+
resetPQExpBuffer(query_buf);
121+
}
122+
else
123+
{
124+
successResult = EXIT_USER;
125+
break;
126+
}
127+
}
128+
#endif
129+
91130
if (slashCmdStatus == CMD_NEWEDIT)
92131
{
93132
/*
@@ -213,7 +252,7 @@ MainLoop(FILE *source)
213252

214253
/* echo back if flag is set */
215254
var = GetVariable(pset.vars, "ECHO");
216-
if (var && strcmp(var, "all")==0)
255+
if (!pset.cur_cmd_interactive && var && strcmp(var, "all")==0)
217256
puts(line);
218257
fflush(stdout);
219258

0 commit comments

Comments
 (0)