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