Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * psql - the PostgreSQL interactive terminal
3 : : *
4 : : * Copyright (c) 2000-2025, PostgreSQL Global Development Group
5 : : *
6 : : * src/bin/psql/startup.c
7 : : */
8 : : #include "postgres_fe.h"
9 : :
10 : : #ifndef WIN32
11 : : #include <unistd.h>
12 : : #else /* WIN32 */
13 : : #include <io.h>
14 : : #include <win32.h>
15 : : #endif /* WIN32 */
16 : :
17 : : #include "command.h"
18 : : #include "common.h"
19 : : #include "common/logging.h"
20 : : #include "common/string.h"
21 : : #include "describe.h"
22 : : #include "fe_utils/print.h"
23 : : #include "getopt_long.h"
24 : : #include "help.h"
25 : : #include "input.h"
26 : : #include "mainloop.h"
27 : : #include "settings.h"
28 : :
29 : : /*
30 : : * Global psql options
31 : : */
32 : : PsqlSettings pset;
33 : :
34 : : #ifndef WIN32
35 : : #define SYSPSQLRC "psqlrc"
36 : : #define PSQLRC ".psqlrc"
37 : : #else
38 : : #define SYSPSQLRC "psqlrc"
39 : : #define PSQLRC "psqlrc.conf"
40 : : #endif
41 : :
42 : : /*
43 : : * Structures to pass information between the option parsing routine
44 : : * and the main function
45 : : */
46 : : enum _actions
47 : : {
48 : : ACT_SINGLE_QUERY,
49 : : ACT_SINGLE_SLASH,
50 : : ACT_FILE,
51 : : };
52 : :
53 : : typedef struct SimpleActionListCell
54 : : {
55 : : struct SimpleActionListCell *next;
56 : : enum _actions action;
57 : : char *val;
58 : : } SimpleActionListCell;
59 : :
60 : : typedef struct SimpleActionList
61 : : {
62 : : SimpleActionListCell *head;
63 : : SimpleActionListCell *tail;
64 : : } SimpleActionList;
65 : :
66 : : struct adhoc_opts
67 : : {
68 : : char *dbname;
69 : : char *host;
70 : : char *port;
71 : : char *username;
72 : : char *logfilename;
73 : : bool no_readline;
74 : : bool no_psqlrc;
75 : : bool single_txn;
76 : : bool list_dbs;
77 : : SimpleActionList actions;
78 : : };
79 : :
80 : : static void parse_psql_options(int argc, char *argv[],
81 : : struct adhoc_opts *options);
82 : : static void simple_action_list_append(SimpleActionList *list,
83 : : enum _actions action, const char *val);
84 : : static void process_psqlrc(char *argv0);
85 : : static void process_psqlrc_file(char *filename);
86 : : static void showVersion(void);
87 : : static void EstablishVariableSpace(void);
88 : :
89 : : #define NOPAGER 0
90 : :
91 : : static void
2350 peter@eisentraut.org 92 :GIC 32829 : log_pre_callback(void)
93 : : {
94 [ + - - + ]: 32829 : if (pset.queryFout && pset.queryFout != stdout)
2350 peter@eisentraut.org 95 :UIC 0 : fflush(pset.queryFout);
2350 peter@eisentraut.org 96 :GIC 32829 : }
97 : :
98 : : static void
99 : 32829 : log_locus_callback(const char **filename, uint64 *lineno)
100 : : {
101 [ + + ]: 32829 : if (pset.inputfile)
102 : : {
103 : 652 : *filename = pset.inputfile;
2299 tgl@sss.pgh.pa.us 104 : 652 : *lineno = pset.lineno;
105 : : }
106 : : else
107 : : {
2350 peter@eisentraut.org 108 : 32177 : *filename = NULL;
109 : 32177 : *lineno = 0;
110 : : }
111 : 32829 : }
112 : :
113 : : #ifndef WIN32
114 : : static void
1516 tmunro@postgresql.or 115 : 4 : empty_signal_handler(SIGNAL_ARGS)
116 : : {
117 : 4 : }
118 : : #endif
119 : :
120 : : /*
121 : : *
122 : : * main
123 : : *
124 : : */
125 : : int
9343 peter_e@gmx.net 126 : 8504 : main(int argc, char *argv[])
127 : : {
128 : : struct adhoc_opts options;
129 : : int successResult;
1829 tgl@sss.pgh.pa.us 130 : 8504 : char *password = NULL;
131 : : bool new_pass;
132 : :
2350 peter@eisentraut.org 133 : 8504 : pg_logging_init(argv[0]);
134 : 8504 : pg_logging_set_pre_callback(log_pre_callback);
135 : 8504 : pg_logging_set_locus_callback(log_locus_callback);
6113 peter_e@gmx.net 136 : 8504 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
137 : :
9051 138 [ + - ]: 8504 : if (argc > 1)
139 : : {
4015 andres@anarazel.de 140 [ + - + + : 8504 : if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
+ + ]
141 : : {
142 : 1 : usage(NOPAGER);
9051 peter_e@gmx.net 143 : 1 : exit(EXIT_SUCCESS);
144 : : }
8934 bruce@momjian.us 145 [ + + + + ]: 8503 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
146 : : {
9051 peter_e@gmx.net 147 : 18 : showVersion();
148 : 18 : exit(EXIT_SUCCESS);
149 : : }
150 : : }
151 : :
7017 tgl@sss.pgh.pa.us 152 : 8485 : pset.progname = get_progname(argv[0]);
153 : :
6948 154 : 8485 : pset.db = NULL;
1779 155 : 8485 : pset.dead_conn = NULL;
7359 bruce@momjian.us 156 : 8485 : setDecimalLocale();
6948 tgl@sss.pgh.pa.us 157 : 8485 : pset.encoding = PQenv2encoding();
158 : 8485 : pset.queryFout = stdout;
159 : 8485 : pset.queryFoutPipe = false;
4198 160 : 8485 : pset.copyStream = NULL;
3443 161 : 8485 : pset.last_error_result = NULL;
9367 peter_e@gmx.net 162 : 8485 : pset.cur_cmd_source = stdin;
163 : 8485 : pset.cur_cmd_interactive = false;
164 : :
165 : : /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
166 : 8485 : pset.popt.topt.format = PRINT_ALIGNED;
167 : 8485 : pset.popt.topt.border = 1;
8338 bruce@momjian.us 168 : 8485 : pset.popt.topt.pager = 1;
3815 andrew@dunslane.net 169 : 8485 : pset.popt.topt.pager_min_lines = 0;
6948 tgl@sss.pgh.pa.us 170 : 8485 : pset.popt.topt.start_table = true;
171 : 8485 : pset.popt.topt.stop_table = true;
4876 rhaas@postgresql.org 172 : 8485 : pset.popt.topt.default_footer = true;
173 : :
2476 tgl@sss.pgh.pa.us 174 : 8485 : pset.popt.topt.csvFieldSep[0] = DEFAULT_CSV_FIELD_SEP;
175 : 8485 : pset.popt.topt.csvFieldSep[1] = '\0';
176 : :
4012 sfrost@snowman.net 177 : 8485 : pset.popt.topt.unicode_border_linestyle = UNICODE_LINESTYLE_SINGLE;
178 : 8485 : pset.popt.topt.unicode_column_linestyle = UNICODE_LINESTYLE_SINGLE;
179 : 8485 : pset.popt.topt.unicode_header_linestyle = UNICODE_LINESTYLE_SINGLE;
180 : :
181 : 8485 : refresh_utf8format(&(pset.popt.topt));
182 : :
183 : : /* We must get COLUMNS here before readline() sets it */
6330 bruce@momjian.us 184 [ - + ]: 8485 : pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
185 : :
9367 peter_e@gmx.net 186 [ + + - + ]: 8485 : pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
187 : :
6036 188 : 8485 : pset.getPassword = TRI_DEFAULT;
189 : :
6948 tgl@sss.pgh.pa.us 190 : 8485 : EstablishVariableSpace();
191 : :
192 : : /* Create variables showing psql version number */
193 : 8485 : SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
2923 194 : 8485 : SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
195 : 8485 : SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
196 : :
197 : : /* Initialize variables for last error */
2916 198 : 8485 : SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
199 : 8485 : SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
200 : :
201 : : /* Default values for variables (that don't match the result of \unset) */
6948 202 : 8485 : SetVariableBool(pset.vars, "AUTOCOMMIT");
203 : 8485 : SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
204 : 8485 : SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
205 : 8485 : SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
1251 peter@eisentraut.org 206 : 8485 : SetVariableBool(pset.vars, "SHOW_ALL_RESULTS");
207 : :
208 : : /* Initialize pipeline variables */
193 michael@paquier.xyz 209 : 8485 : SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", "0");
210 : 8485 : SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", "0");
211 : 8485 : SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", "0");
212 : :
9330 peter_e@gmx.net 213 : 8485 : parse_psql_options(argc, argv, &options);
214 : :
215 : : /*
216 : : * If no action was specified and we're in non-interactive mode, treat it
217 : : * as if the user had specified "-f -". This lets single-transaction mode
218 : : * work in this case.
219 : : */
3560 rhaas@postgresql.org 220 [ + + + + ]: 8482 : if (options.actions.head == NULL && pset.notty)
221 : 2139 : simple_action_list_append(&options.actions, ACT_FILE, NULL);
222 : :
223 : : /* Bail out if -1 was specified but will be ignored. */
224 [ + + - + ]: 8482 : if (options.single_txn && options.actions.head == NULL)
1247 tgl@sss.pgh.pa.us 225 :UIC 0 : pg_fatal("-1 can only be used in non-interactive mode");
226 : :
4958 peter_e@gmx.net 227 [ + + ]:GIC 8482 : if (!pset.popt.topt.fieldSep.separator &&
228 [ + - ]: 8480 : !pset.popt.topt.fieldSep.separator_zero)
229 : : {
230 : 8480 : pset.popt.topt.fieldSep.separator = pg_strdup(DEFAULT_FIELD_SEP);
231 : 8480 : pset.popt.topt.fieldSep.separator_zero = false;
232 : : }
233 [ + - ]: 8482 : if (!pset.popt.topt.recordSep.separator &&
234 [ + - ]: 8482 : !pset.popt.topt.recordSep.separator_zero)
235 : : {
236 : 8482 : pset.popt.topt.recordSep.separator = pg_strdup(DEFAULT_RECORD_SEP);
237 : 8482 : pset.popt.topt.recordSep.separator_zero = false;
238 : : }
239 : :
6036 240 [ - + ]: 8482 : if (pset.getPassword == TRI_YES)
241 : : {
242 : : /*
243 : : * We can't be sure yet of the username that will be used, so don't
244 : : * offer a potentially wrong one. Typical uses of this option are
245 : : * noninteractive anyway. (Note: since we've not yet set up our
246 : : * cancel handler, there's no need to use simple_prompt_extended.)
247 : : */
1829 tgl@sss.pgh.pa.us 248 :UIC 0 : password = simple_prompt("Password: ", false);
249 : : }
250 : :
251 : : /* loop until we have a password if requested by backend */
252 : : do
253 : : {
254 : : #define PARAMS_ARRAY_SIZE 8
1090 peter@eisentraut.org 255 :GIC 8482 : const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
256 : 8482 : const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
257 : :
5671 bruce@momjian.us 258 : 8482 : keywords[0] = "host";
259 : 8482 : values[0] = options.host;
260 : 8482 : keywords[1] = "port";
261 : 8482 : values[1] = options.port;
262 : 8482 : keywords[2] = "user";
263 : 8482 : values[2] = options.username;
264 : 8482 : keywords[3] = "password";
1829 tgl@sss.pgh.pa.us 265 : 8482 : values[3] = password;
3316 noah@leadboat.com 266 : 8482 : keywords[4] = "dbname"; /* see do_connect() */
3560 rhaas@postgresql.org 267 [ # # ]:UIC 0 : values[4] = (options.list_dbs && options.dbname == NULL) ?
5671 bruce@momjian.us 268 [ - + ]:GIC 8482 : "postgres" : options.dbname;
269 : 8482 : keywords[5] = "fallback_application_name";
270 : 8482 : values[5] = pset.progname;
5313 peter_e@gmx.net 271 : 8482 : keywords[6] = "client_encoding";
272 [ + + + - ]: 8482 : values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
273 : 8482 : keywords[7] = NULL;
274 : 8482 : values[7] = NULL;
275 : :
5692 mail@joeconway.com 276 : 8482 : new_pass = false;
277 : 8482 : pset.db = PQconnectdbParams(keywords, values, true);
278 : 8482 : free(keywords);
279 : 8482 : free(values);
280 : :
9367 peter_e@gmx.net 281 [ + + + + ]: 8782 : if (PQstatus(pset.db) == CONNECTION_BAD &&
6481 tgl@sss.pgh.pa.us 282 [ + - ]: 301 : PQconnectionNeedsPassword(pset.db) &&
1829 283 : 1 : !password &&
6036 peter_e@gmx.net 284 [ - + ]: 1 : pset.getPassword != TRI_NO)
285 : : {
286 : : /*
287 : : * Before closing the old PGconn, extract the user name that was
288 : : * actually connected with --- it might've come out of a URI or
289 : : * connstring "database name" rather than options.username.
290 : : */
2777 tgl@sss.pgh.pa.us 291 :UIC 0 : const char *realusername = PQuser(pset.db);
292 : : char *password_prompt;
293 : :
294 [ # # # # ]: 0 : if (realusername && realusername[0])
295 : 0 : password_prompt = psprintf(_("Password for user %s: "),
296 : : realusername);
297 : : else
298 : 0 : password_prompt = pg_strdup(_("Password: "));
9120 peter_e@gmx.net 299 : 0 : PQfinish(pset.db);
300 : :
1829 tgl@sss.pgh.pa.us 301 : 0 : password = simple_prompt(password_prompt, false);
2777 302 : 0 : free(password_prompt);
6635 303 : 0 : new_pass = true;
304 : : }
6635 tgl@sss.pgh.pa.us 305 [ - + ]:GIC 8482 : } while (new_pass);
306 : :
9367 peter_e@gmx.net 307 [ + + ]: 8482 : if (PQstatus(pset.db) == CONNECTION_BAD)
308 : : {
1764 peter@eisentraut.org 309 : 300 : pg_log_error("%s", PQerrorMessage(pset.db));
9367 peter_e@gmx.net 310 : 300 : PQfinish(pset.db);
9438 bruce@momjian.us 311 : 300 : exit(EXIT_BADCONN);
312 : : }
313 : :
2105 michael@paquier.xyz 314 : 8182 : psql_setup_cancel_handler();
315 : :
316 : : #ifndef WIN32
317 : :
318 : : /*
319 : : * do_watch() needs signal handlers installed (otherwise sigwait() will
320 : : * filter them out on some platforms), but doesn't need them to do
321 : : * anything, and they shouldn't ever run (unless perhaps a stray SIGALRM
322 : : * arrives due to a race when do_watch() cancels an itimer).
323 : : */
1516 tmunro@postgresql.or 324 : 8182 : pqsignal(SIGCHLD, empty_signal_handler);
325 : 8182 : pqsignal(SIGALRM, empty_signal_handler);
326 : : #endif
327 : :
9278 bruce@momjian.us 328 : 8182 : PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
329 : :
8106 tgl@sss.pgh.pa.us 330 : 8182 : SyncVariables();
331 : :
3560 rhaas@postgresql.org 332 [ - + ]: 8182 : if (options.list_dbs)
333 : : {
334 : : int success;
335 : :
5316 peter_e@gmx.net 336 [ # # ]:UIC 0 : if (!options.no_psqlrc)
337 : 0 : process_psqlrc(argv[0]);
338 : :
4570 339 : 0 : success = listAllDbs(NULL, false);
9367 340 : 0 : PQfinish(pset.db);
9320 341 : 0 : exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
342 : : }
343 : :
7389 bruce@momjian.us 344 [ - + ]:GIC 8182 : if (options.logfilename)
345 : : {
7389 bruce@momjian.us 346 :UIC 0 : pset.logfile = fopen(options.logfilename, "a");
347 [ # # ]: 0 : if (!pset.logfile)
1247 tgl@sss.pgh.pa.us 348 : 0 : pg_fatal("could not open log file \"%s\": %m",
349 : : options.logfilename);
350 : : }
351 : :
3560 rhaas@postgresql.org 352 [ - + ]:GIC 8182 : if (!options.no_psqlrc)
3560 rhaas@postgresql.org 353 :UIC 0 : process_psqlrc(argv[0]);
354 : :
355 : : /*
356 : : * If any actions were given by user, process them in the order in which
357 : : * they were specified. Note single_txn is only effective in this mode.
358 : : */
3560 rhaas@postgresql.org 359 [ + + ]:GIC 8182 : if (options.actions.head != NULL)
360 : : {
361 : : PGresult *res;
362 : : SimpleActionListCell *cell;
363 : :
364 : 8180 : successResult = EXIT_SUCCESS; /* silence compiler */
365 : :
366 [ + + ]: 8180 : if (options.single_txn)
367 : : {
368 [ - + ]: 7 : if ((res = PSQLexec("BEGIN")) == NULL)
369 : : {
3560 rhaas@postgresql.org 370 [ # # ]:UIC 0 : if (pset.on_error_stop)
371 : : {
372 : 0 : successResult = EXIT_USER;
373 : 0 : goto error;
374 : : }
375 : : }
376 : : else
3560 rhaas@postgresql.org 377 :GIC 7 : PQclear(res);
378 : : }
379 : :
380 [ + + ]: 16489 : for (cell = options.actions.head; cell; cell = cell->next)
381 : : {
382 [ + + ]: 8411 : if (cell->action == ACT_SINGLE_QUERY)
383 : : {
2350 peter@eisentraut.org 384 : 475 : pg_logging_config(PG_LOG_FLAG_TERSE);
385 : :
3560 rhaas@postgresql.org 386 [ - + ]: 475 : if (pset.echo == PSQL_ECHO_ALL)
3560 rhaas@postgresql.org 387 :UIC 0 : puts(cell->val);
388 : :
3560 rhaas@postgresql.org 389 :GIC 475 : successResult = SendQuery(cell->val)
390 : 474 : ? EXIT_SUCCESS : EXIT_FAILURE;
391 : : }
392 [ + + ]: 7936 : else if (cell->action == ACT_SINGLE_SLASH)
393 : : {
394 : : PsqlScanState scan_state;
395 : : ConditionalStack cond_stack;
396 : :
2350 peter@eisentraut.org 397 : 2 : pg_logging_config(PG_LOG_FLAG_TERSE);
398 : :
3560 rhaas@postgresql.org 399 [ - + ]: 2 : if (pset.echo == PSQL_ECHO_ALL)
3560 rhaas@postgresql.org 400 :UIC 0 : puts(cell->val);
401 : :
3459 tgl@sss.pgh.pa.us 402 :GIC 2 : scan_state = psql_scan_create(&psqlscan_callbacks);
3560 rhaas@postgresql.org 403 : 2 : psql_scan_setup(scan_state,
3459 tgl@sss.pgh.pa.us 404 : 2 : cell->val, strlen(cell->val),
405 : 2 : pset.encoding, standard_strings());
3082 406 : 2 : cond_stack = conditional_stack_create();
282 peter@eisentraut.org 407 : 2 : psql_scan_set_passthrough(scan_state, cond_stack);
408 : :
3082 tgl@sss.pgh.pa.us 409 : 2 : successResult = HandleSlashCmds(scan_state,
410 : : cond_stack,
411 : : NULL,
412 : : NULL) != PSQL_CMD_ERROR
3560 rhaas@postgresql.org 413 : 2 : ? EXIT_SUCCESS : EXIT_FAILURE;
414 : :
415 : 2 : psql_scan_destroy(scan_state);
3082 tgl@sss.pgh.pa.us 416 : 2 : conditional_stack_destroy(cond_stack);
417 : : }
3560 rhaas@postgresql.org 418 [ + - ]: 7934 : else if (cell->action == ACT_FILE)
419 : : {
420 : 7934 : successResult = process_file(cell->val, false);
421 : : }
422 : : else
423 : : {
424 : : /* should never come here */
3555 tgl@sss.pgh.pa.us 425 :UIC 0 : Assert(false);
426 : : }
427 : :
3560 rhaas@postgresql.org 428 [ + + + + ]:GIC 8399 : if (successResult != EXIT_SUCCESS && pset.on_error_stop)
429 : 90 : break;
430 : : }
431 : :
432 [ + + ]: 8168 : if (options.single_txn)
433 : : {
434 : : /*
435 : : * Rollback the contents of the single transaction if the caller
436 : : * has set ON_ERROR_STOP and one of the steps has failed. This
437 : : * check needs to match the one done a couple of lines above.
438 : : */
1179 michael@paquier.xyz 439 [ + + + + ]: 7 : res = PSQLexec((successResult != EXIT_SUCCESS && pset.on_error_stop) ?
440 : : "ROLLBACK" : "COMMIT");
1188 441 [ - + ]: 7 : if (res == NULL)
442 : : {
3560 rhaas@postgresql.org 443 [ # # ]:UIC 0 : if (pset.on_error_stop)
444 : : {
445 : 0 : successResult = EXIT_USER;
446 : 0 : goto error;
447 : : }
448 : : }
449 : : else
3560 rhaas@postgresql.org 450 :GIC 7 : PQclear(res);
451 : : }
452 : :
453 : 8168 : error:
454 : : ;
455 : : }
456 : :
457 : : /*
458 : : * or otherwise enter interactive main loop
459 : : */
460 : : else
461 : : {
2254 peter@eisentraut.org 462 : 2 : pg_logging_config(PG_LOG_FLAG_TERSE);
5681 bruce@momjian.us 463 : 2 : connection_warnings(true);
4776 rhaas@postgresql.org 464 [ + - ]: 2 : if (!pset.quiet)
6257 bruce@momjian.us 465 : 2 : printf(_("Type \"help\" for help.\n\n"));
4776 rhaas@postgresql.org 466 : 2 : initializeInput(options.no_readline ? 0 : 1);
9363 peter_e@gmx.net 467 : 2 : successResult = MainLoop(stdin);
468 : : }
469 : :
470 : : /* clean up */
7389 bruce@momjian.us 471 [ - + ]: 8170 : if (pset.logfile)
7389 bruce@momjian.us 472 :UIC 0 : fclose(pset.logfile);
1779 tgl@sss.pgh.pa.us 473 [ + - ]:GIC 8170 : if (pset.db)
474 : 8170 : PQfinish(pset.db);
475 [ - + ]: 8170 : if (pset.dead_conn)
1779 tgl@sss.pgh.pa.us 476 :UIC 0 : PQfinish(pset.dead_conn);
9367 peter_e@gmx.net 477 :GIC 8170 : setQFout(NULL);
478 : :
9438 bruce@momjian.us 479 : 8170 : return successResult;
480 : : }
481 : :
482 : :
483 : : /*
484 : : * Parse command line options
485 : : */
486 : :
487 : : static void
2999 tgl@sss.pgh.pa.us 488 : 8485 : parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
489 : : {
490 : : static struct option long_options[] =
491 : : {
492 : : {"echo-all", no_argument, NULL, 'a'},
493 : : {"no-align", no_argument, NULL, 'A'},
494 : : {"command", required_argument, NULL, 'c'},
495 : : {"dbname", required_argument, NULL, 'd'},
496 : : {"echo-queries", no_argument, NULL, 'e'},
497 : : {"echo-errors", no_argument, NULL, 'b'},
498 : : {"echo-hidden", no_argument, NULL, 'E'},
499 : : {"file", required_argument, NULL, 'f'},
500 : : {"field-separator", required_argument, NULL, 'F'},
501 : : {"field-separator-zero", no_argument, NULL, 'z'},
502 : : {"host", required_argument, NULL, 'h'},
503 : : {"html", no_argument, NULL, 'H'},
504 : : {"list", no_argument, NULL, 'l'},
505 : : {"log-file", required_argument, NULL, 'L'},
506 : : {"no-readline", no_argument, NULL, 'n'},
507 : : {"single-transaction", no_argument, NULL, '1'},
508 : : {"output", required_argument, NULL, 'o'},
509 : : {"port", required_argument, NULL, 'p'},
510 : : {"pset", required_argument, NULL, 'P'},
511 : : {"quiet", no_argument, NULL, 'q'},
512 : : {"record-separator", required_argument, NULL, 'R'},
513 : : {"record-separator-zero", no_argument, NULL, '0'},
514 : : {"single-step", no_argument, NULL, 's'},
515 : : {"single-line", no_argument, NULL, 'S'},
516 : : {"tuples-only", no_argument, NULL, 't'},
517 : : {"table-attr", required_argument, NULL, 'T'},
518 : : {"username", required_argument, NULL, 'U'},
519 : : {"set", required_argument, NULL, 'v'},
520 : : {"variable", required_argument, NULL, 'v'},
521 : : {"version", no_argument, NULL, 'V'},
522 : : {"no-password", no_argument, NULL, 'w'},
523 : : {"password", no_argument, NULL, 'W'},
524 : : {"expanded", no_argument, NULL, 'x'},
525 : : {"no-psqlrc", no_argument, NULL, 'X'},
526 : : {"help", optional_argument, NULL, 1},
527 : : {"csv", no_argument, NULL, 2},
528 : : {NULL, 0, NULL, 0}
529 : : };
530 : :
531 : : int optindex;
532 : : int c;
533 : :
9416 bruce@momjian.us 534 : 8485 : memset(options, 0, sizeof *options);
535 : :
4076 fujii@postgresql.org 536 : 63055 : while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
8279 peter_e@gmx.net 537 [ + + ]: 63055 : long_options, &optindex)) != -1)
538 : : {
9438 bruce@momjian.us 539 [ + + - + : 54573 : switch (c)
+ + - + +
+ - - - -
- + + + -
- - + - +
+ - + - -
+ - - + +
+ - - ]
540 : : {
9337 peter_e@gmx.net 541 : 988 : case 'a':
542 : 988 : SetVariable(pset.vars, "ECHO", "all");
543 : 988 : break;
9438 bruce@momjian.us 544 : 7251 : case 'A':
9367 peter_e@gmx.net 545 : 7251 : pset.popt.topt.format = PRINT_UNALIGNED;
9438 bruce@momjian.us 546 : 7251 : break;
4076 fujii@postgresql.org 547 :UIC 0 : case 'b':
548 : 0 : SetVariable(pset.vars, "ECHO", "errors");
549 : 0 : break;
9438 bruce@momjian.us 550 :GIC 619 : case 'c':
551 [ + + ]: 619 : if (optarg[0] == '\\')
3560 rhaas@postgresql.org 552 : 2 : simple_action_list_append(&options->actions,
553 : : ACT_SINGLE_SLASH,
3555 tgl@sss.pgh.pa.us 554 : 2 : optarg + 1);
555 : : else
3560 rhaas@postgresql.org 556 : 617 : simple_action_list_append(&options->actions,
557 : : ACT_SINGLE_QUERY,
558 : : optarg);
9438 bruce@momjian.us 559 : 619 : break;
560 : 8396 : case 'd':
4712 561 : 8396 : options->dbname = pg_strdup(optarg);
9438 562 : 8396 : break;
563 : 8 : case 'e':
9337 peter_e@gmx.net 564 : 8 : SetVariable(pset.vars, "ECHO", "queries");
9438 bruce@momjian.us 565 : 8 : break;
9438 bruce@momjian.us 566 :UIC 0 : case 'E':
9337 peter_e@gmx.net 567 : 0 : SetVariableBool(pset.vars, "ECHO_HIDDEN");
9438 bruce@momjian.us 568 : 0 : break;
9438 bruce@momjian.us 569 :GIC 6095 : case 'f':
3560 rhaas@postgresql.org 570 : 6095 : simple_action_list_append(&options->actions,
571 : : ACT_FILE,
572 : : optarg);
9438 bruce@momjian.us 573 : 6095 : break;
574 : 2 : case 'F':
4958 peter_e@gmx.net 575 : 2 : pset.popt.topt.fieldSep.separator = pg_strdup(optarg);
576 : 2 : pset.popt.topt.fieldSep.separator_zero = false;
9438 bruce@momjian.us 577 : 2 : break;
578 : 8 : case 'h':
4712 579 : 8 : options->host = pg_strdup(optarg);
9438 580 : 8 : break;
9438 bruce@momjian.us 581 :UIC 0 : case 'H':
9367 peter_e@gmx.net 582 : 0 : pset.popt.topt.format = PRINT_HTML;
9438 bruce@momjian.us 583 : 0 : break;
584 : 0 : case 'l':
3560 rhaas@postgresql.org 585 : 0 : options->list_dbs = true;
9438 bruce@momjian.us 586 : 0 : break;
7389 587 : 0 : case 'L':
4712 588 : 0 : options->logfilename = pg_strdup(optarg);
7389 589 : 0 : break;
9438 590 : 0 : case 'n':
591 : 0 : options->no_readline = true;
592 : 0 : break;
593 : 0 : case 'o':
3565 tgl@sss.pgh.pa.us 594 [ # # ]: 0 : if (!setQFout(optarg))
595 : 0 : exit(EXIT_FAILURE);
9438 bruce@momjian.us 596 : 0 : break;
9438 bruce@momjian.us 597 :GIC 9 : case 'p':
4712 598 : 9 : options->port = pg_strdup(optarg);
9438 599 : 9 : break;
600 : 2 : case 'P':
601 : : {
602 : : char *value;
603 : : char *equal_loc;
604 : : bool result;
605 : :
7895 neilc@samurai.com 606 : 2 : value = pg_strdup(optarg);
9438 bruce@momjian.us 607 : 2 : equal_loc = strchr(value, '=');
608 [ - + ]: 2 : if (!equal_loc)
9367 peter_e@gmx.net 609 :UIC 0 : result = do_pset(value, NULL, &pset.popt, true);
610 : : else
611 : : {
9438 bruce@momjian.us 612 :GIC 2 : *equal_loc = '\0';
9367 peter_e@gmx.net 613 : 2 : result = do_pset(value, equal_loc + 1, &pset.popt, true);
614 : : }
615 : :
9438 bruce@momjian.us 616 [ - + ]: 2 : if (!result)
1247 tgl@sss.pgh.pa.us 617 :UIC 0 : pg_fatal("could not set printing parameter \"%s\"", value);
618 : :
9438 bruce@momjian.us 619 :GIC 2 : free(value);
620 : 2 : break;
621 : : }
622 : 7304 : case 'q':
9337 peter_e@gmx.net 623 : 7304 : SetVariableBool(pset.vars, "QUIET");
9438 bruce@momjian.us 624 : 7304 : break;
9278 bruce@momjian.us 625 :UIC 0 : case 'R':
4958 peter_e@gmx.net 626 : 0 : pset.popt.topt.recordSep.separator = pg_strdup(optarg);
627 : 0 : pset.popt.topt.recordSep.separator_zero = false;
9278 bruce@momjian.us 628 : 0 : break;
9438 629 : 0 : case 's':
9337 peter_e@gmx.net 630 : 0 : SetVariableBool(pset.vars, "SINGLESTEP");
9438 bruce@momjian.us 631 : 0 : break;
632 : 0 : case 'S':
9337 peter_e@gmx.net 633 : 0 : SetVariableBool(pset.vars, "SINGLELINE");
9438 bruce@momjian.us 634 : 0 : break;
9438 bruce@momjian.us 635 :GIC 7245 : case 't':
9367 peter_e@gmx.net 636 : 7245 : pset.popt.topt.tuples_only = true;
9438 bruce@momjian.us 637 : 7245 : break;
9438 bruce@momjian.us 638 :UIC 0 : case 'T':
7895 neilc@samurai.com 639 : 0 : pset.popt.topt.tableAttr = pg_strdup(optarg);
9438 bruce@momjian.us 640 : 0 : break;
9438 bruce@momjian.us 641 :GIC 16 : case 'U':
4712 642 : 16 : options->username = pg_strdup(optarg);
9438 643 : 16 : break;
644 : 7502 : case 'v':
645 : : {
646 : : char *value;
647 : : char *equal_loc;
648 : :
7895 neilc@samurai.com 649 : 7502 : value = pg_strdup(optarg);
9438 bruce@momjian.us 650 : 7502 : equal_loc = strchr(value, '=');
651 [ - + ]: 7502 : if (!equal_loc)
652 : : {
9367 peter_e@gmx.net 653 [ # # ]:UIC 0 : if (!DeleteVariable(pset.vars, value))
3139 tgl@sss.pgh.pa.us 654 : 0 : exit(EXIT_FAILURE); /* error already printed */
655 : : }
656 : : else
657 : : {
9438 bruce@momjian.us 658 :GIC 7502 : *equal_loc = '\0';
9367 peter_e@gmx.net 659 [ - + ]: 7502 : if (!SetVariable(pset.vars, value, equal_loc + 1))
3139 tgl@sss.pgh.pa.us 660 :UIC 0 : exit(EXIT_FAILURE); /* error already printed */
661 : : }
662 : :
9438 bruce@momjian.us 663 :GIC 7502 : free(value);
664 : 7502 : break;
665 : : }
9438 bruce@momjian.us 666 :UIC 0 : case 'V':
9369 peter_e@gmx.net 667 : 0 : showVersion();
668 : 0 : exit(EXIT_SUCCESS);
6036 peter_e@gmx.net 669 :GIC 636 : case 'w':
670 : 636 : pset.getPassword = TRI_NO;
671 : 636 : break;
9438 bruce@momjian.us 672 :UIC 0 : case 'W':
6036 peter_e@gmx.net 673 : 0 : pset.getPassword = TRI_YES;
9438 bruce@momjian.us 674 : 0 : break;
9320 peter_e@gmx.net 675 : 0 : case 'x':
676 : 0 : pset.popt.topt.expanded = true;
677 : 0 : break;
9278 bruce@momjian.us 678 :GIC 8482 : case 'X':
679 : 8482 : options->no_psqlrc = true;
680 : 8482 : break;
4958 peter_e@gmx.net 681 :UIC 0 : case 'z':
682 : 0 : pset.popt.topt.fieldSep.separator_zero = true;
683 : 0 : break;
684 : 0 : case '0':
685 : 0 : pset.popt.topt.recordSep.separator_zero = true;
686 : 0 : break;
7146 bruce@momjian.us 687 :GIC 7 : case '1':
688 : 7 : options->single_txn = true;
689 : 7 : break;
9438 690 : 1 : case '?':
2204 tgl@sss.pgh.pa.us 691 [ + - ]: 1 : if (optind <= argc &&
692 [ - + ]: 1 : strcmp(argv[optind - 1], "-?") == 0)
693 : : {
694 : : /* actual help option given */
4015 andres@anarazel.de 695 :UIC 0 : usage(NOPAGER);
9278 bruce@momjian.us 696 : 0 : exit(EXIT_SUCCESS);
697 : : }
698 : : else
699 : : {
700 : : /* getopt error (unknown option or missing argument) */
4015 andres@anarazel.de 701 :GIC 1 : goto unknown_option;
702 : : }
703 : : break;
704 : 2 : case 1:
705 : : {
706 [ + - - + ]: 2 : if (!optarg || strcmp(optarg, "options") == 0)
4015 andres@anarazel.de 707 :UIC 0 : usage(NOPAGER);
4015 andres@anarazel.de 708 [ + - + + ]:GIC 2 : else if (optarg && strcmp(optarg, "commands") == 0)
709 : 1 : slashUsage(NOPAGER);
710 [ + - + - ]: 1 : else if (optarg && strcmp(optarg, "variables") == 0)
711 : 1 : helpVariables(NOPAGER);
712 : : else
4015 andres@anarazel.de 713 :UIC 0 : goto unknown_option;
714 : :
4015 andres@anarazel.de 715 :GIC 2 : exit(EXIT_SUCCESS);
716 : : }
717 : : break;
2476 tgl@sss.pgh.pa.us 718 :UIC 0 : case 2:
719 : 0 : pset.popt.topt.format = PRINT_CSV;
720 : 0 : break;
721 : : default:
3759 bruce@momjian.us 722 :GIC 1 : unknown_option:
723 : : /* getopt_long already emitted a complaint */
1247 tgl@sss.pgh.pa.us 724 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.",
725 : : pset.progname);
9248 bruce@momjian.us 726 : 1 : exit(EXIT_FAILURE);
727 : : }
728 : : }
729 : :
730 : : /*
731 : : * if we still have arguments, use it as the database name and username
732 : : */
9438 733 [ + + ]: 8586 : while (argc - optind >= 1)
734 : : {
735 [ + - ]: 104 : if (!options->dbname)
736 : 104 : options->dbname = argv[optind];
9438 bruce@momjian.us 737 [ # # ]:UIC 0 : else if (!options->username)
738 : 0 : options->username = argv[optind];
6948 tgl@sss.pgh.pa.us 739 [ # # ]: 0 : else if (!pset.quiet)
2350 peter@eisentraut.org 740 : 0 : pg_log_warning("extra command-line argument \"%s\" ignored",
741 : : argv[optind]);
742 : :
9438 bruce@momjian.us 743 :GIC 104 : optind++;
744 : : }
745 : 8482 : }
746 : :
747 : :
748 : : /*
749 : : * Append a new item to the end of the SimpleActionList.
750 : : * Note that "val" is copied if it's not NULL.
751 : : */
752 : : static void
3555 tgl@sss.pgh.pa.us 753 : 8853 : simple_action_list_append(SimpleActionList *list,
754 : : enum _actions action, const char *val)
755 : : {
756 : : SimpleActionListCell *cell;
757 : :
1090 peter@eisentraut.org 758 : 8853 : cell = pg_malloc_object(SimpleActionListCell);
759 : :
3555 tgl@sss.pgh.pa.us 760 : 8853 : cell->next = NULL;
761 : 8853 : cell->action = action;
762 [ + + ]: 8853 : if (val)
763 : 6714 : cell->val = pg_strdup(val);
764 : : else
765 : 2139 : cell->val = NULL;
766 : :
767 [ + + ]: 8853 : if (list->tail)
768 : 373 : list->tail->next = cell;
769 : : else
770 : 8480 : list->head = cell;
771 : 8853 : list->tail = cell;
772 : 8853 : }
773 : :
774 : :
775 : : /*
776 : : * Load .psqlrc file, if found.
777 : : */
778 : : static void
5662 magnus@hagander.net 779 :UIC 0 : process_psqlrc(char *argv0)
780 : : {
781 : : char home[MAXPGPATH];
782 : : char rc_file[MAXPGPATH];
783 : : char my_exec_path[MAXPGPATH];
784 : : char etc_path[MAXPGPATH];
4538 bruce@momjian.us 785 : 0 : char *envrc = getenv("PSQLRC");
786 : :
4207 sfrost@snowman.net 787 [ # # ]: 0 : if (find_my_exec(argv0, my_exec_path) < 0)
1247 tgl@sss.pgh.pa.us 788 : 0 : pg_fatal("could not find own program executable");
789 : :
7782 bruce@momjian.us 790 : 0 : get_etc_path(my_exec_path, etc_path);
791 : :
7548 tgl@sss.pgh.pa.us 792 : 0 : snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
793 : 0 : process_psqlrc_file(rc_file);
794 : :
4935 andrew@dunslane.net 795 [ # # # # ]: 0 : if (envrc != NULL && strlen(envrc) > 0)
796 : 0 : {
797 : : /* might need to free() this */
4483 bruce@momjian.us 798 : 0 : char *envrc_alloc = pstrdup(envrc);
799 : :
4538 800 : 0 : expand_tilde(&envrc_alloc);
801 : 0 : process_psqlrc_file(envrc_alloc);
802 : : }
4935 andrew@dunslane.net 803 [ # # ]: 0 : else if (get_home_path(home))
804 : : {
7548 tgl@sss.pgh.pa.us 805 : 0 : snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
806 : 0 : process_psqlrc_file(rc_file);
807 : : }
7807 bruce@momjian.us 808 : 0 : }
809 : :
810 : :
811 : :
812 : : static void
813 : 0 : process_psqlrc_file(char *filename)
814 : : {
815 : : char *psqlrc_minor,
816 : : *psqlrc_major;
817 : :
818 : : #if defined(WIN32) && (!defined(__MINGW32__))
819 : : #define R_OK 4
820 : : #endif
821 : :
4337 tgl@sss.pgh.pa.us 822 : 0 : psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
823 : 0 : psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
824 : :
825 : : /* check for minor version first, then major, then no version */
5076 bruce@momjian.us 826 [ # # ]: 0 : if (access(psqlrc_minor, R_OK) == 0)
3560 rhaas@postgresql.org 827 : 0 : (void) process_file(psqlrc_minor, false);
5076 bruce@momjian.us 828 [ # # ]: 0 : else if (access(psqlrc_major, R_OK) == 0)
3560 rhaas@postgresql.org 829 : 0 : (void) process_file(psqlrc_major, false);
7807 bruce@momjian.us 830 [ # # ]: 0 : else if (access(filename, R_OK) == 0)
3560 rhaas@postgresql.org 831 : 0 : (void) process_file(filename, false);
832 : :
5076 bruce@momjian.us 833 : 0 : free(psqlrc_minor);
834 : 0 : free(psqlrc_major);
9438 835 : 0 : }
836 : :
837 : :
838 : :
839 : : /* showVersion
840 : : *
841 : : * This output format is intended to match GNU standards.
842 : : */
843 : : static void
9369 peter_e@gmx.net 844 :GIC 18 : showVersion(void)
845 : : {
9197 846 : 18 : puts("psql (PostgreSQL) " PG_VERSION);
9438 bruce@momjian.us 847 : 18 : }
848 : :
849 : :
850 : :
851 : : /*
852 : : * Substitute hooks and assign hooks for psql variables.
853 : : *
854 : : * This isn't an amazingly good place for them, but neither is anywhere else.
855 : : *
856 : : * By policy, every special variable that controls any psql behavior should
857 : : * have one or both hooks, even if they're just no-ops. This ensures that
858 : : * the variable will remain present in variables.c's list even when unset,
859 : : * which ensures that it's known to tab completion.
860 : : */
861 : :
862 : : static char *
3139 tgl@sss.pgh.pa.us 863 : 116732 : bool_substitute_hook(char *newval)
864 : : {
865 [ + + ]: 116732 : if (newval == NULL)
866 : : {
867 : : /* "\unset FOO" becomes "\set FOO off" */
868 : 84853 : newval = pg_strdup("off");
869 : : }
870 [ + + ]: 31879 : else if (newval[0] == '\0')
871 : : {
872 : : /* "\set FOO" becomes "\set FOO on" */
873 : 3 : pg_free(newval);
874 : 3 : newval = pg_strdup("on");
875 : : }
876 : 116732 : return newval;
877 : : }
878 : :
879 : : static bool
6948 880 : 16991 : autocommit_hook(const char *newval)
881 : : {
3141 882 : 16991 : return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
883 : : }
884 : :
885 : : static bool
6948 886 : 14011 : on_error_stop_hook(const char *newval)
887 : : {
3141 888 : 14011 : return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
889 : : }
890 : :
891 : : static bool
6948 892 : 15825 : quiet_hook(const char *newval)
893 : : {
3141 894 : 15825 : return ParseVariableBool(newval, "QUIET", &pset.quiet);
895 : : }
896 : :
897 : : static bool
6948 898 : 8485 : singleline_hook(const char *newval)
899 : : {
3141 900 : 8485 : return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
901 : : }
902 : :
903 : : static bool
6948 904 : 8485 : singlestep_hook(const char *newval)
905 : : {
3141 906 : 8485 : return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
907 : : }
908 : :
909 : : static char *
3138 910 : 8525 : fetch_count_substitute_hook(char *newval)
911 : : {
912 [ + + ]: 8525 : if (newval == NULL)
913 : 8503 : newval = pg_strdup("0");
914 : 8525 : return newval;
915 : : }
916 : :
917 : : static bool
6948 918 : 8525 : fetch_count_hook(const char *newval)
919 : : {
3138 920 : 8525 : return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
921 : : }
922 : :
923 : : static bool
924 : 8485 : histfile_hook(const char *newval)
925 : : {
926 : : /*
927 : : * Someday we might try to validate the filename, but for now, this is
928 : : * just a placeholder to ensure HISTFILE is known to tab completion.
929 : : */
3141 930 : 8485 : return true;
931 : : }
932 : :
933 : : static char *
3138 934 : 8485 : histsize_substitute_hook(char *newval)
935 : : {
936 [ + - ]: 8485 : if (newval == NULL)
937 : 8485 : newval = pg_strdup("500");
938 : 8485 : return newval;
939 : : }
940 : :
941 : : static bool
942 : 8485 : histsize_hook(const char *newval)
943 : : {
944 : 8485 : return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
945 : : }
946 : :
947 : : static char *
165 dgustafsson@postgres 948 : 8489 : watch_interval_substitute_hook(char *newval)
949 : : {
950 [ + + ]: 8489 : if (newval == NULL)
951 : 8486 : newval = pg_strdup(DEFAULT_WATCH_INTERVAL);
952 : 8489 : return newval;
953 : : }
954 : :
955 : : static bool
956 : 8489 : watch_interval_hook(const char *newval)
957 : : {
958 : 8489 : return ParseVariableDouble(newval, "WATCH_INTERVAL", &pset.watch_interval,
959 : : 0, DEFAULT_WATCH_INTERVAL_MAX);
960 : : }
961 : :
962 : : static char *
3138 tgl@sss.pgh.pa.us 963 : 8485 : ignoreeof_substitute_hook(char *newval)
964 : : {
965 : : int dummy;
966 : :
967 : : /*
968 : : * This tries to mimic the behavior of bash, to wit "If set, the value is
969 : : * the number of consecutive EOF characters which must be typed as the
970 : : * first characters on an input line before bash exits. If the variable
971 : : * exists but does not have a numeric value, or has no value, the default
972 : : * value is 10. If it does not exist, EOF signifies the end of input to
973 : : * the shell." Unlike bash, however, we insist on the stored value
974 : : * actually being a valid integer.
975 : : */
976 [ + - ]: 8485 : if (newval == NULL)
977 : 8485 : newval = pg_strdup("0");
3138 tgl@sss.pgh.pa.us 978 [ # # ]:UIC 0 : else if (!ParseVariableNum(newval, NULL, &dummy))
979 : 0 : newval = pg_strdup("10");
3138 tgl@sss.pgh.pa.us 980 :GIC 8485 : return newval;
981 : : }
982 : :
983 : : static bool
984 : 8485 : ignoreeof_hook(const char *newval)
985 : : {
986 : 8485 : return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
987 : : }
988 : :
989 : : static char *
3139 990 : 9501 : echo_substitute_hook(char *newval)
991 : : {
992 [ + + ]: 9501 : if (newval == NULL)
993 : 8485 : newval = pg_strdup("none");
994 : 9501 : return newval;
995 : : }
996 : :
997 : : static bool
6948 998 : 9501 : echo_hook(const char *newval)
999 : : {
3139 1000 [ - + ]: 9501 : Assert(newval != NULL); /* else substitute hook messed up */
1001 [ + + ]: 9501 : if (pg_strcasecmp(newval, "queries") == 0)
6948 1002 : 8 : pset.echo = PSQL_ECHO_QUERIES;
3902 1003 [ + + ]: 9493 : else if (pg_strcasecmp(newval, "errors") == 0)
4076 fujii@postgresql.org 1004 : 3 : pset.echo = PSQL_ECHO_ERRORS;
3902 tgl@sss.pgh.pa.us 1005 [ + + ]: 9490 : else if (pg_strcasecmp(newval, "all") == 0)
6948 1006 : 998 : pset.echo = PSQL_ECHO_ALL;
3902 1007 [ + - ]: 8492 : else if (pg_strcasecmp(newval, "none") == 0)
1008 : 8492 : pset.echo = PSQL_ECHO_NONE;
1009 : : else
1010 : : {
3141 tgl@sss.pgh.pa.us 1011 :UIC 0 : PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
1012 : 0 : return false;
1013 : : }
3141 tgl@sss.pgh.pa.us 1014 :GIC 9501 : return true;
1015 : : }
1016 : :
1017 : : static bool
6948 1018 : 8485 : echo_hidden_hook(const char *newval)
1019 : : {
3139 1020 [ - + ]: 8485 : Assert(newval != NULL); /* else substitute hook messed up */
1021 [ - + ]: 8485 : if (pg_strcasecmp(newval, "noexec") == 0)
6948 tgl@sss.pgh.pa.us 1022 :UIC 0 : pset.echo_hidden = PSQL_ECHO_HIDDEN_NOEXEC;
1023 : : else
1024 : : {
1025 : : bool on_off;
1026 : :
3141 tgl@sss.pgh.pa.us 1027 [ + - ]:GIC 8485 : if (ParseVariableBool(newval, NULL, &on_off))
1028 : 8485 : pset.echo_hidden = on_off ? PSQL_ECHO_HIDDEN_ON : PSQL_ECHO_HIDDEN_OFF;
1029 : : else
1030 : : {
3141 tgl@sss.pgh.pa.us 1031 :UIC 0 : PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
1032 : 0 : return false;
1033 : : }
1034 : : }
3141 tgl@sss.pgh.pa.us 1035 :GIC 8485 : return true;
1036 : : }
1037 : :
1038 : : static bool
6948 1039 : 8509 : on_error_rollback_hook(const char *newval)
1040 : : {
3139 1041 [ - + ]: 8509 : Assert(newval != NULL); /* else substitute hook messed up */
1042 [ - + ]: 8509 : if (pg_strcasecmp(newval, "interactive") == 0)
6948 tgl@sss.pgh.pa.us 1043 :UIC 0 : pset.on_error_rollback = PSQL_ERROR_ROLLBACK_INTERACTIVE;
1044 : : else
1045 : : {
1046 : : bool on_off;
1047 : :
3141 tgl@sss.pgh.pa.us 1048 [ + + ]:GIC 8509 : if (ParseVariableBool(newval, NULL, &on_off))
1049 [ + + ]: 8506 : pset.on_error_rollback = on_off ? PSQL_ERROR_ROLLBACK_ON : PSQL_ERROR_ROLLBACK_OFF;
1050 : : else
1051 : : {
1052 : 3 : PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
1053 : 3 : return false;
1054 : : }
1055 : : }
1056 : 8506 : return true;
1057 : : }
1058 : :
1059 : : static char *
3139 1060 : 8489 : comp_keyword_case_substitute_hook(char *newval)
1061 : : {
1062 [ + + ]: 8489 : if (newval == NULL)
1063 : 8485 : newval = pg_strdup("preserve-upper");
1064 : 8489 : return newval;
1065 : : }
1066 : :
1067 : : static bool
3902 1068 : 8489 : comp_keyword_case_hook(const char *newval)
1069 : : {
3139 1070 [ - + ]: 8489 : Assert(newval != NULL); /* else substitute hook messed up */
1071 [ + + ]: 8489 : if (pg_strcasecmp(newval, "preserve-upper") == 0)
3902 1072 : 8486 : pset.comp_case = PSQL_COMP_CASE_PRESERVE_UPPER;
1073 [ + + ]: 3 : else if (pg_strcasecmp(newval, "preserve-lower") == 0)
1074 : 1 : pset.comp_case = PSQL_COMP_CASE_PRESERVE_LOWER;
1075 [ + + ]: 2 : else if (pg_strcasecmp(newval, "upper") == 0)
1076 : 1 : pset.comp_case = PSQL_COMP_CASE_UPPER;
1077 [ + - ]: 1 : else if (pg_strcasecmp(newval, "lower") == 0)
1078 : 1 : pset.comp_case = PSQL_COMP_CASE_LOWER;
1079 : : else
1080 : : {
3141 tgl@sss.pgh.pa.us 1081 :UIC 0 : PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
1082 : : "lower, upper, preserve-lower, preserve-upper");
1083 : 0 : return false;
1084 : : }
3141 tgl@sss.pgh.pa.us 1085 :GIC 8489 : return true;
1086 : : }
1087 : :
1088 : : static char *
3139 1089 : 8485 : histcontrol_substitute_hook(char *newval)
1090 : : {
1091 [ + - ]: 8485 : if (newval == NULL)
1092 : 8485 : newval = pg_strdup("none");
1093 : 8485 : return newval;
1094 : : }
1095 : :
1096 : : static bool
6948 1097 : 8485 : histcontrol_hook(const char *newval)
1098 : : {
3139 1099 [ - + ]: 8485 : Assert(newval != NULL); /* else substitute hook messed up */
1100 [ - + ]: 8485 : if (pg_strcasecmp(newval, "ignorespace") == 0)
6948 tgl@sss.pgh.pa.us 1101 :UIC 0 : pset.histcontrol = hctl_ignorespace;
3902 tgl@sss.pgh.pa.us 1102 [ - + ]:GIC 8485 : else if (pg_strcasecmp(newval, "ignoredups") == 0)
6948 tgl@sss.pgh.pa.us 1103 :UIC 0 : pset.histcontrol = hctl_ignoredups;
3902 tgl@sss.pgh.pa.us 1104 [ - + ]:GIC 8485 : else if (pg_strcasecmp(newval, "ignoreboth") == 0)
6948 tgl@sss.pgh.pa.us 1105 :UIC 0 : pset.histcontrol = hctl_ignoreboth;
3902 tgl@sss.pgh.pa.us 1106 [ + - ]:GIC 8485 : else if (pg_strcasecmp(newval, "none") == 0)
1107 : 8485 : pset.histcontrol = hctl_none;
1108 : : else
1109 : : {
3141 tgl@sss.pgh.pa.us 1110 :UIC 0 : PsqlVarEnumError("HISTCONTROL", newval,
1111 : : "none, ignorespace, ignoredups, ignoreboth");
1112 : 0 : return false;
1113 : : }
3141 tgl@sss.pgh.pa.us 1114 :GIC 8485 : return true;
1115 : : }
1116 : :
1117 : : static bool
6948 1118 : 16970 : prompt1_hook(const char *newval)
1119 : : {
1120 [ + + ]: 16970 : pset.prompt1 = newval ? newval : "";
3141 1121 : 16970 : return true;
1122 : : }
1123 : :
1124 : : static bool
6948 1125 : 16970 : prompt2_hook(const char *newval)
1126 : : {
1127 [ + + ]: 16970 : pset.prompt2 = newval ? newval : "";
3141 1128 : 16970 : return true;
1129 : : }
1130 : :
1131 : : static bool
6948 1132 : 16970 : prompt3_hook(const char *newval)
1133 : : {
1134 [ + + ]: 16970 : pset.prompt3 = newval ? newval : "";
3141 1135 : 16970 : return true;
1136 : : }
1137 : :
1138 : : static char *
3139 1139 : 8575 : verbosity_substitute_hook(char *newval)
1140 : : {
1141 [ + + ]: 8575 : if (newval == NULL)
1142 : 8485 : newval = pg_strdup("default");
1143 : 8575 : return newval;
1144 : : }
1145 : :
1146 : : static bool
6948 1147 : 8575 : verbosity_hook(const char *newval)
1148 : : {
3139 1149 [ - + ]: 8575 : Assert(newval != NULL); /* else substitute hook messed up */
1150 [ + + ]: 8575 : if (pg_strcasecmp(newval, "default") == 0)
6948 1151 : 8523 : pset.verbosity = PQERRORS_DEFAULT;
3902 1152 [ - + ]: 52 : else if (pg_strcasecmp(newval, "verbose") == 0)
6948 tgl@sss.pgh.pa.us 1153 :UIC 0 : pset.verbosity = PQERRORS_VERBOSE;
2347 tgl@sss.pgh.pa.us 1154 [ + + ]:GIC 52 : else if (pg_strcasecmp(newval, "terse") == 0)
1155 : 31 : pset.verbosity = PQERRORS_TERSE;
1156 [ + - ]: 21 : else if (pg_strcasecmp(newval, "sqlstate") == 0)
1157 : 21 : pset.verbosity = PQERRORS_SQLSTATE;
1158 : : else
1159 : : {
2347 tgl@sss.pgh.pa.us 1160 :UIC 0 : PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
3141 1161 : 0 : return false;
1162 : : }
1163 : :
6948 tgl@sss.pgh.pa.us 1164 [ + + ]:GIC 8575 : if (pset.db)
1165 : 90 : PQsetErrorVerbosity(pset.db, pset.verbosity);
3141 1166 : 8575 : return true;
1167 : : }
1168 : :
1169 : : static bool
1251 peter@eisentraut.org 1170 : 16977 : show_all_results_hook(const char *newval)
1171 : : {
1172 : 16977 : return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
1173 : : }
1174 : :
1175 : : static char *
3139 tgl@sss.pgh.pa.us 1176 : 8512 : show_context_substitute_hook(char *newval)
1177 : : {
1178 [ + + ]: 8512 : if (newval == NULL)
1179 : 8486 : newval = pg_strdup("errors");
1180 : 8512 : return newval;
1181 : : }
1182 : :
1183 : : static bool
3654 1184 : 8512 : show_context_hook(const char *newval)
1185 : : {
3139 1186 [ - + ]: 8512 : Assert(newval != NULL); /* else substitute hook messed up */
1187 [ + + ]: 8512 : if (pg_strcasecmp(newval, "never") == 0)
3654 1188 : 8 : pset.show_context = PQSHOW_CONTEXT_NEVER;
1189 [ + + ]: 8504 : else if (pg_strcasecmp(newval, "errors") == 0)
1190 : 8496 : pset.show_context = PQSHOW_CONTEXT_ERRORS;
1191 [ + - ]: 8 : else if (pg_strcasecmp(newval, "always") == 0)
1192 : 8 : pset.show_context = PQSHOW_CONTEXT_ALWAYS;
1193 : : else
1194 : : {
3141 tgl@sss.pgh.pa.us 1195 :UIC 0 : PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1196 : 0 : return false;
1197 : : }
1198 : :
3654 tgl@sss.pgh.pa.us 1199 [ + + ]:GIC 8512 : if (pset.db)
1200 : 27 : PQsetErrorContextVisibility(pset.db, pset.show_context);
3141 1201 : 8512 : return true;
1202 : : }
1203 : :
1204 : : static bool
1632 rhaas@postgresql.org 1205 : 9485 : hide_compression_hook(const char *newval)
1206 : : {
1207 : 9485 : return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
1208 : : &pset.hide_compression);
1209 : : }
1210 : :
1211 : : static bool
2376 andres@anarazel.de 1212 : 9479 : hide_tableam_hook(const char *newval)
1213 : : {
1214 : 9479 : return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
1215 : : }
1216 : :
1217 : : static void
6948 tgl@sss.pgh.pa.us 1218 : 8485 : EstablishVariableSpace(void)
1219 : : {
1220 : 8485 : pset.vars = CreateVariableSpace();
1221 : :
3139 1222 : 8485 : SetVariableHooks(pset.vars, "AUTOCOMMIT",
1223 : : bool_substitute_hook,
1224 : : autocommit_hook);
1225 : 8485 : SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1226 : : bool_substitute_hook,
1227 : : on_error_stop_hook);
1228 : 8485 : SetVariableHooks(pset.vars, "QUIET",
1229 : : bool_substitute_hook,
1230 : : quiet_hook);
1231 : 8485 : SetVariableHooks(pset.vars, "SINGLELINE",
1232 : : bool_substitute_hook,
1233 : : singleline_hook);
1234 : 8485 : SetVariableHooks(pset.vars, "SINGLESTEP",
1235 : : bool_substitute_hook,
1236 : : singlestep_hook);
1237 : 8485 : SetVariableHooks(pset.vars, "FETCH_COUNT",
1238 : : fetch_count_substitute_hook,
1239 : : fetch_count_hook);
3138 1240 : 8485 : SetVariableHooks(pset.vars, "HISTFILE",
1241 : : NULL,
1242 : : histfile_hook);
1243 : 8485 : SetVariableHooks(pset.vars, "HISTSIZE",
1244 : : histsize_substitute_hook,
1245 : : histsize_hook);
1246 : 8485 : SetVariableHooks(pset.vars, "IGNOREEOF",
1247 : : ignoreeof_substitute_hook,
1248 : : ignoreeof_hook);
3139 1249 : 8485 : SetVariableHooks(pset.vars, "ECHO",
1250 : : echo_substitute_hook,
1251 : : echo_hook);
1252 : 8485 : SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1253 : : bool_substitute_hook,
1254 : : echo_hidden_hook);
1255 : 8485 : SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1256 : : bool_substitute_hook,
1257 : : on_error_rollback_hook);
1258 : 8485 : SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1259 : : comp_keyword_case_substitute_hook,
1260 : : comp_keyword_case_hook);
1261 : 8485 : SetVariableHooks(pset.vars, "HISTCONTROL",
1262 : : histcontrol_substitute_hook,
1263 : : histcontrol_hook);
1264 : 8485 : SetVariableHooks(pset.vars, "PROMPT1",
1265 : : NULL,
1266 : : prompt1_hook);
1267 : 8485 : SetVariableHooks(pset.vars, "PROMPT2",
1268 : : NULL,
1269 : : prompt2_hook);
1270 : 8485 : SetVariableHooks(pset.vars, "PROMPT3",
1271 : : NULL,
1272 : : prompt3_hook);
1273 : 8485 : SetVariableHooks(pset.vars, "VERBOSITY",
1274 : : verbosity_substitute_hook,
1275 : : verbosity_hook);
1251 peter@eisentraut.org 1276 : 8485 : SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
1277 : : bool_substitute_hook,
1278 : : show_all_results_hook);
3139 tgl@sss.pgh.pa.us 1279 : 8485 : SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1280 : : show_context_substitute_hook,
1281 : : show_context_hook);
1632 rhaas@postgresql.org 1282 : 8485 : SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
1283 : : bool_substitute_hook,
1284 : : hide_compression_hook);
2376 andres@anarazel.de 1285 : 8485 : SetVariableHooks(pset.vars, "HIDE_TABLEAM",
1286 : : bool_substitute_hook,
1287 : : hide_tableam_hook);
165 dgustafsson@postgres 1288 : 8485 : SetVariableHooks(pset.vars, "WATCH_INTERVAL",
1289 : : watch_interval_substitute_hook,
1290 : : watch_interval_hook);
6948 tgl@sss.pgh.pa.us 1291 : 8485 : }
|