PostgreSQL Source Code git master
Loading...
Searching...
No Matches
startup.c
Go to the documentation of this file.
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"
28#include "settings.h"
29
30/*
31 * Global psql options
32 */
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 */
53
60
66
80
81static void parse_psql_options(int argc, char *argv[],
82 struct adhoc_opts *options);
84 enum _actions action, const char *val);
85static void process_psqlrc(char *argv0);
86static void process_psqlrc_file(char *filename);
87static void showVersion(void);
88static void EstablishVariableSpace(void);
89
90#define NOPAGER 0
91
92static void
94{
95 if (pset.queryFout && pset.queryFout != stdout)
97}
98
99static void
100log_locus_callback(const char **filename, uint64 *lineno)
101{
102 if (pset.inputfile)
103 {
105 *lineno = pset.lineno;
106 }
107 else
108 {
109 *filename = NULL;
110 *lineno = 0;
111 }
112}
113
114#ifndef WIN32
115static void
119#endif
120
121/*
122 *
123 * main
124 *
125 */
126int
127main(int argc, char *argv[])
128{
129 struct adhoc_opts options;
130 int successResult;
131 char *password = NULL;
132 bool new_pass;
133
134 pg_logging_init(argv[0]);
137 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
138
139 if (argc > 1)
140 {
141 if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
142 {
143 usage(NOPAGER);
145 }
146 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
147 {
148 showVersion();
150 }
151 }
152
153 pset.progname = get_progname(argv[0]);
154
155 pset.db = NULL;
159 pset.queryFout = stdout;
160 pset.queryFoutPipe = false;
165
166 /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
168 pset.popt.topt.border = 1;
169 pset.popt.topt.pager = 1;
171 pset.popt.topt.start_table = true;
172 pset.popt.topt.stop_table = true;
174
176 pset.popt.topt.csvFieldSep[1] = '\0';
177
181
183
184 /* We must get COLUMNS here before readline() sets it */
185 pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
186
187 pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
188
190
192
193 /* Create variables showing psql version number */
194 SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
195 SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
197
198 /* Initialize variables for last error */
199 SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
200 SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
201
202 /* Default values for variables (that don't match the result of \unset) */
203 SetVariableBool(pset.vars, "AUTOCOMMIT");
207 SetVariableBool(pset.vars, "SHOW_ALL_RESULTS");
208
209 /* Initialize pipeline variables */
210 SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", "0");
211 SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", "0");
212 SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", "0");
213
214 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 */
221 if (options.actions.head == NULL && pset.notty)
223
224 /* Bail out if -1 was specified but will be ignored. */
225 if (options.single_txn && options.actions.head == NULL)
226 pg_fatal("-1 can only be used in non-interactive mode");
227
230 {
233 }
236 {
239 }
240
241 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 */
249 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
256 const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
257 const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
258
259 keywords[0] = "host";
260 values[0] = options.host;
261 keywords[1] = "port";
262 values[1] = options.port;
263 keywords[2] = "user";
265 keywords[3] = "password";
266 values[3] = password;
267 keywords[4] = "dbname"; /* see do_connect() */
268 values[4] = (options.list_dbs && options.dbname == NULL) ?
269 "postgres" : options.dbname;
270 keywords[5] = "fallback_application_name";
271 values[5] = pset.progname;
272 keywords[6] = "client_encoding";
273 values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
274 keywords[7] = NULL;
275 values[7] = NULL;
276
277 new_pass = false;
279 free(keywords);
280 free(values);
281
282 if (PQstatus(pset.db) == CONNECTION_BAD &&
284 !password &&
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 */
292 const char *realusername = PQuser(pset.db);
293 char *password_prompt;
294
295 if (realusername && realusername[0])
296 password_prompt = psprintf(_("Password for user %s: "),
298 else
299 password_prompt = pg_strdup(_("Password: "));
301
304 new_pass = true;
305 }
306 } while (new_pass);
307
309 {
313 }
314
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 */
327#endif
328
330
331 /* initialize timing infrastructure (required for INSTR_* calls) */
333
335
336 if (options.list_dbs)
337 {
338 int success;
339
340 if (!options.no_psqlrc)
341 process_psqlrc(argv[0]);
342
343 success = listAllDbs(NULL, false);
346 }
347
348 if (options.logfilename)
349 {
350 pset.logfile = fopen(options.logfilename, "a");
351 if (!pset.logfile)
352 pg_fatal("could not open log file \"%s\": %m",
353 options.logfilename);
354 }
355
356 if (!options.no_psqlrc)
357 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 */
363 if (options.actions.head != NULL)
364 {
365 PGresult *res;
367
368 successResult = EXIT_SUCCESS; /* silence compiler */
369
370 if (options.single_txn)
371 {
372 if ((res = PSQLexec("BEGIN")) == NULL)
373 {
375 {
377 goto error;
378 }
379 }
380 else
381 PQclear(res);
382 }
383
384 for (cell = options.actions.head; cell; cell = cell->next)
385 {
386 if (cell->action == ACT_SINGLE_QUERY)
387 {
389
390 if (pset.echo == PSQL_ECHO_ALL)
391 puts(cell->val);
392
395 }
396 else if (cell->action == ACT_SINGLE_SLASH)
397 {
400
402
403 if (pset.echo == PSQL_ECHO_ALL)
404 puts(cell->val);
405
408 cell->val, strlen(cell->val),
412
415 NULL,
418
421 }
422 else if (cell->action == ACT_FILE)
423 {
424 successResult = process_file(cell->val, false);
425 }
426 else
427 {
428 /* should never come here */
429 Assert(false);
430 }
431
433 break;
434 }
435
436 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 */
444 "ROLLBACK" : "COMMIT");
445 if (res == NULL)
446 {
448 {
450 goto error;
451 }
452 }
453 else
454 PQclear(res);
455 }
456
457error:
458 ;
459 }
460
461 /*
462 * or otherwise enter interactive main loop
463 */
464 else
465 {
468 if (!pset.quiet)
469 printf(_("Type \"help\" for help.\n\n"));
470 initializeInput(options.no_readline ? 0 : 1);
472 }
473
474 /* clean up */
475 if (pset.logfile)
477 if (pset.db)
479 if (pset.dead_conn)
481 setQFout(NULL);
482
483 return successResult;
484}
485
486
487/*
488 * Parse command line options
489 */
490
491static void
492parse_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
538 memset(options, 0, sizeof *options);
539
540 while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
541 long_options, &optindex)) != -1)
542 {
543 switch (c)
544 {
545 case 'a':
546 SetVariable(pset.vars, "ECHO", "all");
547 break;
548 case 'A':
550 break;
551 case 'b':
552 SetVariable(pset.vars, "ECHO", "errors");
553 break;
554 case 'c':
555 if (optarg[0] == '\\')
558 optarg + 1);
559 else
562 optarg);
563 break;
564 case 'd':
566 break;
567 case 'e':
568 SetVariable(pset.vars, "ECHO", "queries");
569 break;
570 case 'E':
571 SetVariableBool(pset.vars, "ECHO_HIDDEN");
572 break;
573 case 'f':
575 ACT_FILE,
576 optarg);
577 break;
578 case 'F':
581 break;
582 case 'h':
583 options->host = pg_strdup(optarg);
584 break;
585 case 'H':
587 break;
588 case 'l':
589 options->list_dbs = true;
590 break;
591 case 'L':
592 options->logfilename = pg_strdup(optarg);
593 break;
594 case 'n':
595 options->no_readline = true;
596 break;
597 case 'o':
598 if (!setQFout(optarg))
600 break;
601 case 'p':
603 break;
604 case 'P':
605 {
606 char *value;
607 char *equal_loc;
608 bool result;
609
611 equal_loc = strchr(value, '=');
612 if (!equal_loc)
613 result = do_pset(value, NULL, &pset.popt, true);
614 else
615 {
616 *equal_loc = '\0';
617 result = do_pset(value, equal_loc + 1, &pset.popt, true);
618 }
619
620 if (!result)
621 pg_fatal("could not set printing parameter \"%s\"", value);
622
623 free(value);
624 break;
625 }
626 case 'q':
627 SetVariableBool(pset.vars, "QUIET");
628 break;
629 case 'R':
632 break;
633 case 's':
634 SetVariableBool(pset.vars, "SINGLESTEP");
635 break;
636 case 'S':
637 SetVariableBool(pset.vars, "SINGLELINE");
638 break;
639 case 't':
640 pset.popt.topt.tuples_only = true;
641 break;
642 case 'T':
644 break;
645 case 'U':
647 break;
648 case 'v':
649 {
650 char *value;
651 char *equal_loc;
652
654 equal_loc = strchr(value, '=');
655 if (!equal_loc)
656 {
658 exit(EXIT_FAILURE); /* error already printed */
659 }
660 else
661 {
662 *equal_loc = '\0';
663 if (!SetVariable(pset.vars, value, equal_loc + 1))
664 exit(EXIT_FAILURE); /* error already printed */
665 }
666
667 free(value);
668 break;
669 }
670 case 'V':
671 showVersion();
673 case 'w':
675 break;
676 case 'W':
678 break;
679 case 'x':
680 pset.popt.topt.expanded = true;
681 break;
682 case 'X':
683 options->no_psqlrc = true;
684 break;
685 case 'z':
687 break;
688 case '0':
690 break;
691 case '1':
692 options->single_txn = true;
693 break;
694 case '?':
695 if (optind <= argc &&
696 strcmp(argv[optind - 1], "-?") == 0)
697 {
698 /* actual help option given */
699 usage(NOPAGER);
701 }
702 else
703 {
704 /* getopt error (unknown option or missing argument) */
705 goto unknown_option;
706 }
707 break;
708 case 1:
709 {
710 if (!optarg || strcmp(optarg, "options") == 0)
711 usage(NOPAGER);
712 else if (optarg && strcmp(optarg, "commands") == 0)
714 else if (optarg && strcmp(optarg, "variables") == 0)
716 else
717 goto unknown_option;
718
720 }
721 break;
722 case 2:
724 break;
725 default:
727 /* getopt_long already emitted a complaint */
728 pg_log_error_hint("Try \"%s --help\" for more information.",
729 pset.progname);
731 }
732 }
733
734 /*
735 * if we still have arguments, use it as the database name and username
736 */
737 while (argc - optind >= 1)
738 {
739 if (!options->dbname)
740 options->dbname = argv[optind];
741 else if (!options->username)
742 options->username = argv[optind];
743 else if (!pset.quiet)
744 pg_log_warning("extra command-line argument \"%s\" ignored",
745 argv[optind]);
746
747 optind++;
748 }
749}
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 */
756static void
758 enum _actions action, const char *val)
759{
761
763
764 cell->next = NULL;
765 cell->action = action;
766 if (val)
767 cell->val = pg_strdup(val);
768 else
769 cell->val = NULL;
770
771 if (list->tail)
772 list->tail->next = cell;
773 else
774 list->head = cell;
775 list->tail = cell;
776}
777
778
779/*
780 * Load .psqlrc file, if found.
781 */
782static void
784{
785 char home[MAXPGPATH];
786 char rc_file[MAXPGPATH];
788 char etc_path[MAXPGPATH];
789 char *envrc = getenv("PSQLRC");
790
792 pg_fatal("could not find own program executable");
793
795
798
799 if (envrc != NULL && strlen(envrc) > 0)
800 {
801 /* might need to free() this */
802 char *envrc_alloc = pstrdup(envrc);
803
806 }
807 else if (get_home_path(home))
808 {
809 snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
811 }
812}
813
814
815
816static void
818{
819 char *psqlrc_minor,
821
822#if defined(WIN32) && (!defined(__MINGW32__))
823#define R_OK 4
824#endif
825
828
829 /* check for minor version first, then major, then no version */
830 if (access(psqlrc_minor, R_OK) == 0)
832 else if (access(psqlrc_major, R_OK) == 0)
833 (void) process_file(psqlrc_major, false);
834 else if (access(filename, R_OK) == 0)
835 (void) process_file(filename, false);
836
839}
840
841
842
843/* showVersion
844 *
845 * This output format is intended to match GNU standards.
846 */
847static void
849{
850 puts("psql (PostgreSQL) " PG_VERSION);
851}
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
866static char *
868{
869 if (newval == NULL)
870 {
871 /* "\unset FOO" becomes "\set FOO off" */
872 newval = pg_strdup("off");
873 }
874 else if (newval[0] == '\0')
875 {
876 /* "\set FOO" becomes "\set FOO on" */
878 newval = pg_strdup("on");
879 }
880 return newval;
881}
882
883static bool
885{
886 return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
887}
888
889static bool
891{
892 return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
893}
894
895static bool
896quiet_hook(const char *newval)
897{
898 return ParseVariableBool(newval, "QUIET", &pset.quiet);
899}
900
901static bool
903{
904 return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
905}
906
907static bool
909{
910 return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
911}
912
913static char *
915{
916 if (newval == NULL)
917 newval = pg_strdup("0");
918 return newval;
919}
920
921static bool
923{
924 return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
925}
926
927static bool
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 */
934 return true;
935}
936
937static char *
939{
940 if (newval == NULL)
941 newval = pg_strdup("500");
942 return newval;
943}
944
945static bool
947{
948 return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
949}
950
951static char *
958
959static bool
961{
962 return ParseVariableDouble(newval, "WATCH_INTERVAL", &pset.watch_interval,
964}
965
966static char *
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 if (newval == NULL)
981 newval = pg_strdup("0");
982 else if (!ParseVariableNum(newval, NULL, &dummy))
983 newval = pg_strdup("10");
984 return newval;
985}
986
987static bool
989{
990 return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
991}
992
993static char *
995{
996 if (newval == NULL)
997 newval = pg_strdup("none");
998 return newval;
999}
1000
1001static bool
1002echo_hook(const char *newval)
1003{
1004 Assert(newval != NULL); /* else substitute hook messed up */
1005 if (pg_strcasecmp(newval, "queries") == 0)
1007 else if (pg_strcasecmp(newval, "errors") == 0)
1009 else if (pg_strcasecmp(newval, "all") == 0)
1011 else if (pg_strcasecmp(newval, "none") == 0)
1013 else
1014 {
1015 PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
1016 return false;
1017 }
1018 return true;
1019}
1020
1021static bool
1023{
1024 Assert(newval != NULL); /* else substitute hook messed up */
1025 if (pg_strcasecmp(newval, "noexec") == 0)
1027 else
1028 {
1029 bool on_off;
1030
1033 else
1034 {
1035 PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
1036 return false;
1037 }
1038 }
1039 return true;
1040}
1041
1042static bool
1044{
1045 Assert(newval != NULL); /* else substitute hook messed up */
1046 if (pg_strcasecmp(newval, "interactive") == 0)
1048 else
1049 {
1050 bool on_off;
1051
1054 else
1055 {
1056 PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
1057 return false;
1058 }
1059 }
1060 return true;
1061}
1062
1063static char *
1065{
1066 if (newval == NULL)
1067 newval = pg_strdup("preserve-upper");
1068 return newval;
1069}
1070
1071static bool
1073{
1074 Assert(newval != NULL); /* else substitute hook messed up */
1075 if (pg_strcasecmp(newval, "preserve-upper") == 0)
1077 else if (pg_strcasecmp(newval, "preserve-lower") == 0)
1079 else if (pg_strcasecmp(newval, "upper") == 0)
1081 else if (pg_strcasecmp(newval, "lower") == 0)
1083 else
1084 {
1085 PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
1086 "lower, upper, preserve-lower, preserve-upper");
1087 return false;
1088 }
1089 return true;
1090}
1091
1092static char *
1094{
1095 if (newval == NULL)
1096 newval = pg_strdup("none");
1097 return newval;
1098}
1099
1100static bool
1102{
1103 Assert(newval != NULL); /* else substitute hook messed up */
1104 if (pg_strcasecmp(newval, "ignorespace") == 0)
1106 else if (pg_strcasecmp(newval, "ignoredups") == 0)
1108 else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1110 else if (pg_strcasecmp(newval, "none") == 0)
1112 else
1113 {
1114 PsqlVarEnumError("HISTCONTROL", newval,
1115 "none, ignorespace, ignoredups, ignoreboth");
1116 return false;
1117 }
1118 return true;
1119}
1120
1121static bool
1123{
1124 pset.prompt1 = newval ? newval : "";
1125 return true;
1126}
1127
1128static bool
1130{
1131 pset.prompt2 = newval ? newval : "";
1132 return true;
1133}
1134
1135static bool
1137{
1138 pset.prompt3 = newval ? newval : "";
1139 return true;
1140}
1141
1142static char *
1144{
1145 if (newval == NULL)
1146 newval = pg_strdup("default");
1147 return newval;
1148}
1149
1150static bool
1152{
1153 Assert(newval != NULL); /* else substitute hook messed up */
1154 if (pg_strcasecmp(newval, "default") == 0)
1156 else if (pg_strcasecmp(newval, "verbose") == 0)
1158 else if (pg_strcasecmp(newval, "terse") == 0)
1160 else if (pg_strcasecmp(newval, "sqlstate") == 0)
1162 else
1163 {
1164 PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
1165 return false;
1166 }
1167
1168 if (pset.db)
1170 return true;
1171}
1172
1173static bool
1175{
1176 return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
1177}
1178
1179static char *
1181{
1182 if (newval == NULL)
1183 newval = pg_strdup("errors");
1184 return newval;
1185}
1186
1187static bool
1189{
1190 Assert(newval != NULL); /* else substitute hook messed up */
1191 if (pg_strcasecmp(newval, "never") == 0)
1193 else if (pg_strcasecmp(newval, "errors") == 0)
1195 else if (pg_strcasecmp(newval, "always") == 0)
1197 else
1198 {
1199 PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1200 return false;
1201 }
1202
1203 if (pset.db)
1205 return true;
1206}
1207
1208static bool
1210{
1211 return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
1213}
1214
1215static bool
1217{
1218 return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
1219}
1220
1221static void
1223{
1225
1226 SetVariableHooks(pset.vars, "AUTOCOMMIT",
1229 SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1232 SetVariableHooks(pset.vars, "QUIET",
1234 quiet_hook);
1235 SetVariableHooks(pset.vars, "SINGLELINE",
1238 SetVariableHooks(pset.vars, "SINGLESTEP",
1241 SetVariableHooks(pset.vars, "FETCH_COUNT",
1244 SetVariableHooks(pset.vars, "HISTFILE",
1245 NULL,
1247 SetVariableHooks(pset.vars, "HISTSIZE",
1250 SetVariableHooks(pset.vars, "IGNOREEOF",
1253 SetVariableHooks(pset.vars, "ECHO",
1255 echo_hook);
1256 SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1259 SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1262 SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1265 SetVariableHooks(pset.vars, "HISTCONTROL",
1268 SetVariableHooks(pset.vars, "PROMPT1",
1269 NULL,
1270 prompt1_hook);
1271 SetVariableHooks(pset.vars, "PROMPT2",
1272 NULL,
1273 prompt2_hook);
1274 SetVariableHooks(pset.vars, "PROMPT3",
1275 NULL,
1276 prompt3_hook);
1277 SetVariableHooks(pset.vars, "VERBOSITY",
1280 SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
1283 SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1286 SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
1289 SetVariableHooks(pset.vars, "HIDE_TABLEAM",
1292 SetVariableHooks(pset.vars, "WATCH_INTERVAL",
1295}
void expand_tilde(char **filename)
Definition common.c:2576
PGresult * PSQLexec(const char *query)
Definition common.c:655
void psql_setup_cancel_handler(void)
Definition common.c:325
void NoticeProcessor(void *arg, const char *message)
Definition common.c:279
bool standard_strings(void)
Definition common.c:2500
bool setQFout(const char *fname)
Definition common.c:144
bool SendQuery(const char *query)
Definition common.c:1118
static char * verbosity_substitute_hook(char *newval)
Definition startup.c:1143
static bool show_context_hook(const char *newval)
Definition startup.c:1188
static bool hide_tableam_hook(const char *newval)
Definition startup.c:1216
static bool quiet_hook(const char *newval)
Definition startup.c:896
static bool singleline_hook(const char *newval)
Definition startup.c:902
static bool prompt2_hook(const char *newval)
Definition startup.c:1129
static bool comp_keyword_case_hook(const char *newval)
Definition startup.c:1072
#define SYSPSQLRC
Definition startup.c:36
static bool histcontrol_hook(const char *newval)
Definition startup.c:1101
static bool prompt1_hook(const char *newval)
Definition startup.c:1122
#define PSQLRC
Definition startup.c:37
static bool verbosity_hook(const char *newval)
Definition startup.c:1151
static void empty_signal_handler(SIGNAL_ARGS)
Definition startup.c:116
static char * ignoreeof_substitute_hook(char *newval)
Definition startup.c:967
static bool show_all_results_hook(const char *newval)
Definition startup.c:1174
static bool fetch_count_hook(const char *newval)
Definition startup.c:922
static void process_psqlrc(char *argv0)
Definition startup.c:783
static char * fetch_count_substitute_hook(char *newval)
Definition startup.c:914
static void EstablishVariableSpace(void)
Definition startup.c:1222
static bool histsize_hook(const char *newval)
Definition startup.c:946
static void log_pre_callback(void)
Definition startup.c:93
#define NOPAGER
Definition startup.c:90
static char * echo_substitute_hook(char *newval)
Definition startup.c:994
static char * watch_interval_substitute_hook(char *newval)
Definition startup.c:952
static bool hide_compression_hook(const char *newval)
Definition startup.c:1209
static char * bool_substitute_hook(char *newval)
Definition startup.c:867
#define PARAMS_ARRAY_SIZE
static bool singlestep_hook(const char *newval)
Definition startup.c:908
static bool histfile_hook(const char *newval)
Definition startup.c:928
static bool prompt3_hook(const char *newval)
Definition startup.c:1136
static bool on_error_rollback_hook(const char *newval)
Definition startup.c:1043
static char * histsize_substitute_hook(char *newval)
Definition startup.c:938
static bool on_error_stop_hook(const char *newval)
Definition startup.c:890
static bool autocommit_hook(const char *newval)
Definition startup.c:884
static char * show_context_substitute_hook(char *newval)
Definition startup.c:1180
static bool watch_interval_hook(const char *newval)
Definition startup.c:960
static bool ignoreeof_hook(const char *newval)
Definition startup.c:988
PsqlSettings pset
Definition startup.c:33
static bool echo_hidden_hook(const char *newval)
Definition startup.c:1022
static void process_psqlrc_file(char *filename)
Definition startup.c:817
static bool echo_hook(const char *newval)
Definition startup.c:1002
static void simple_action_list_append(SimpleActionList *list, enum _actions action, const char *val)
Definition startup.c:757
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition startup.c:492
_actions
Definition startup.c:48
@ ACT_SINGLE_SLASH
Definition startup.c:50
@ ACT_FILE
Definition startup.c:51
@ ACT_SINGLE_QUERY
Definition startup.c:49
static void showVersion(void)
Definition startup.c:848
static char * histcontrol_substitute_hook(char *newval)
Definition startup.c:1093
static char * comp_keyword_case_substitute_hook(char *newval)
Definition startup.c:1064
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define SIGNAL_ARGS
Definition c.h:1450
#define Assert(condition)
Definition c.h:943
#define PG_TEXTDOMAIN(domain)
Definition c.h:1303
#define CppAsString2(x)
Definition c.h:506
uint64_t uint64
Definition c.h:625
uint32 result
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition command.c:5078
backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition command.c:231
void SyncVariables(void)
Definition command.c:4571
void connection_warnings(bool in_startup)
Definition command.c:4443
@ PSQL_CMD_ERROR
Definition command.h:22
int find_my_exec(const char *argv0, char *retpath)
Definition exec.c:161
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition exec.c:430
int main(void)
ConditionalStack conditional_stack_create(void)
Definition conditional.c:18
void conditional_stack_destroy(ConditionalStack cstack)
Definition conditional.c:43
bool listAllDbs(const char *pattern, bool verbose)
Definition describe.c:953
#define _(x)
Definition elog.c:95
int PQconnectionNeedsPassword(const PGconn *conn)
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
char * PQuser(const PGconn *conn)
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
char * PQerrorMessage(const PGconn *conn)
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition fe-connect.c:775
int PQenv2encoding(void)
Definition fe-misc.c:1285
char * pg_strdup(const char *in)
Definition fe_memutils.c:85
void pg_free(void *ptr)
#define pg_malloc_array(type, count)
Definition fe_memutils.h:56
#define pg_malloc_object(type)
Definition fe_memutils.h:50
void refresh_utf8format(const printTableOpt *opt)
Definition print.c:3889
void setDecimalLocale(void)
Definition print.c:3839
@ UNICODE_LINESTYLE_SINGLE
Definition print.h:101
@ PRINT_CSV
Definition print.h:33
@ PRINT_UNALIGNED
Definition print.h:38
@ PRINT_ALIGNED
Definition print.h:31
@ PRINT_HTML
Definition print.h:34
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition getopt_long.c:60
#define no_argument
Definition getopt_long.h:25
#define required_argument
Definition getopt_long.h:26
#define optional_argument
Definition getopt_long.h:27
char my_exec_path[MAXPGPATH]
Definition globals.c:83
#define newval
void helpVariables(unsigned short int pager)
Definition help.c:370
void slashUsage(unsigned short int pager)
Definition help.c:148
long val
Definition informix.c:689
static struct @177 value
static bool success
Definition initdb.c:188
void initializeInput(int flags)
Definition input.c:344
void pg_initialize_timing(void)
Definition instr_time.c:82
static const JsonPathKeyword keywords[]
#define PQclear
@ CONNECTION_BAD
Definition libpq-fe.h:91
@ PQSHOW_CONTEXT_NEVER
Definition libpq-fe.h:170
@ PQSHOW_CONTEXT_ALWAYS
Definition libpq-fe.h:172
@ PQSHOW_CONTEXT_ERRORS
Definition libpq-fe.h:171
@ PQERRORS_VERBOSE
Definition libpq-fe.h:164
@ PQERRORS_DEFAULT
Definition libpq-fe.h:163
@ PQERRORS_TERSE
Definition libpq-fe.h:162
@ PQERRORS_SQLSTATE
Definition libpq-fe.h:165
void pg_logging_init(const char *argv0)
Definition logging.c:83
void pg_logging_set_locus_callback(void(*cb)(const char **filename, uint64 *lineno))
Definition logging.c:202
static void(* log_locus_callback)(const char **, uint64 *)
Definition logging.c:27
void pg_logging_config(int new_flags)
Definition logging.c:166
void pg_logging_set_pre_callback(void(*cb)(void))
Definition logging.c:196
#define pg_log_error(...)
Definition logging.h:106
#define pg_log_error_hint(...)
Definition logging.h:112
#define PG_LOG_FLAG_TERSE
Definition logging.h:86
const PsqlScanCallbacks psqlscan_callbacks
Definition mainloop.c:20
int MainLoop(FILE *source)
Definition mainloop.c:33
char * pstrdup(const char *in)
Definition mcxt.c:1781
static void usage(void)
#define pg_fatal(...)
#define MAXPGPATH
static char * argv0
Definition pg_ctl.c:94
static char * filename
Definition pg_dumpall.c:133
PGDLLIMPORT int optind
Definition getopt.c:47
PGDLLIMPORT char * optarg
Definition getopt.c:49
static void process_file(const char *filename, int weight)
Definition pgbench.c:6173
#define pg_log_warning(...)
Definition pgfnames.c:24
#define pqsignal
Definition port.h:547
bool get_home_path(char *ret_path)
Definition path.c:1005
int pg_strcasecmp(const char *s1, const char *s2)
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition path.c:911
#define snprintf
Definition port.h:260
const char * get_progname(const char *argv0)
Definition path.c:652
#define printf(...)
Definition port.h:266
char * c
static int fb(int x)
short access
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
void psql_scan_destroy(PsqlScanState state)
Definition psqlscan.l:1034
PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks)
Definition psqlscan.l:1013
void psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
Definition psqlscan.l:1053
void psql_scan_setup(PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
Definition psqlscan.l:1071
#define DEFAULT_PROMPT1
Definition settings.h:26
#define DEFAULT_PROMPT3
Definition settings.h:28
@ PSQL_ERROR_ROLLBACK_INTERACTIVE
Definition settings.h:59
@ PSQL_ERROR_ROLLBACK_ON
Definition settings.h:60
@ PSQL_ERROR_ROLLBACK_OFF
Definition settings.h:58
#define DEFAULT_RECORD_SEP
Definition settings.h:16
@ hctl_ignoredups
Definition settings.h:90
@ hctl_ignoreboth
Definition settings.h:91
@ hctl_none
Definition settings.h:88
@ hctl_ignorespace
Definition settings.h:89
#define DEFAULT_WATCH_INTERVAL_MAX
Definition settings.h:35
@ PSQL_ECHO_ALL
Definition settings.h:46
@ PSQL_ECHO_ERRORS
Definition settings.h:45
@ PSQL_ECHO_NONE
Definition settings.h:43
@ PSQL_ECHO_QUERIES
Definition settings.h:44
#define DEFAULT_FIELD_SEP
Definition settings.h:15
#define EXIT_SUCCESS
Definition settings.h:193
#define EXIT_FAILURE
Definition settings.h:197
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition settings.h:53
@ PSQL_ECHO_HIDDEN_OFF
Definition settings.h:51
@ PSQL_ECHO_HIDDEN_ON
Definition settings.h:52
#define EXIT_BADCONN
Definition settings.h:200
#define DEFAULT_WATCH_INTERVAL
Definition settings.h:30
#define DEFAULT_PROMPT2
Definition settings.h:27
#define EXIT_USER
Definition settings.h:202
@ PSQL_COMP_CASE_PRESERVE_LOWER
Definition settings.h:66
@ PSQL_COMP_CASE_LOWER
Definition settings.h:68
@ PSQL_COMP_CASE_PRESERVE_UPPER
Definition settings.h:65
@ PSQL_COMP_CASE_UPPER
Definition settings.h:67
#define DEFAULT_CSV_FIELD_SEP
Definition settings.h:14
#define free(a)
char * simple_prompt(const char *prompt, bool echo)
Definition sprompt.c:38
static void error(void)
static char * password
Definition streamutil.c:51
struct SimpleActionListCell * next
Definition startup.c:56
enum _actions action
Definition startup.c:57
SimpleActionListCell * head
Definition startup.c:63
SimpleActionListCell * tail
Definition startup.c:64
uint64 lineno
Definition settings.h:144
printQueryOpt popt
Definition settings.h:112
double watch_interval
Definition settings.h:175
PSQL_ERROR_ROLLBACK on_error_rollback
Definition settings.h:178
bool hide_tableam
Definition settings.h:171
VariableSpace vars
Definition settings.h:151
PSQL_COMP_CASE comp_case
Definition settings.h:179
char * inputfile
Definition settings.h:143
PGVerbosity verbosity
Definition settings.h:184
bool hide_compression
Definition settings.h:170
FILE * logfile
Definition settings.h:149
bool on_error_stop
Definition settings.h:166
PGconn * dead_conn
Definition settings.h:158
bool autocommit
Definition settings.h:165
PGresult * last_error_result
Definition settings.h:110
bool show_all_results
Definition settings.h:185
const char * prompt2
Definition settings.h:182
const char * prompt3
Definition settings.h:183
FILE * copyStream
Definition settings.h:108
PGconn * db
Definition settings.h:103
FILE * queryFout
Definition settings.h:105
bool queryFoutPipe
Definition settings.h:106
PSQL_ECHO echo
Definition settings.h:176
bool singlestep
Definition settings.h:169
enum trivalue getPassword
Definition settings.h:137
bool singleline
Definition settings.h:168
PGContextVisibility show_context
Definition settings.h:186
const char * prompt1
Definition settings.h:181
PSQL_ECHO_HIDDEN echo_hidden
Definition settings.h:177
bool cur_cmd_interactive
Definition settings.h:140
FILE * cur_cmd_source
Definition settings.h:138
HistControl histcontrol
Definition settings.h:180
const char * progname
Definition settings.h:142
char * username
Definition startup.c:72
bool no_readline
Definition startup.c:74
char * port
Definition startup.c:71
bool list_dbs
Definition startup.c:77
bool single_txn
Definition startup.c:76
bool no_psqlrc
Definition startup.c:75
char * logfilename
Definition startup.c:73
SimpleActionList actions
Definition startup.c:78
char * dbname
Definition startup.c:69
char * host
Definition startup.c:70
char * username
Definition oid2name.c:45
char * dbname
Definition oid2name.c:42
char * port
Definition oid2name.c:44
printTableOpt topt
Definition print.h:185
bool start_table
Definition print.h:127
unsigned short int expanded
Definition print.h:114
unicode_linestyle unicode_border_linestyle
Definition print.h:141
bool tuples_only
Definition print.h:126
enum printFormat format
Definition print.h:113
struct separator fieldSep
Definition print.h:132
struct separator recordSep
Definition print.h:133
char csvFieldSep[2]
Definition print.h:134
bool default_footer
Definition print.h:129
int pager_min_lines
Definition print.h:124
unsigned short int pager
Definition print.h:122
char * tableAttr
Definition print.h:137
unsigned short int border
Definition print.h:120
unicode_linestyle unicode_header_linestyle
Definition print.h:143
int env_columns
Definition print.h:139
unicode_linestyle unicode_column_linestyle
Definition print.h:142
bool stop_table
Definition print.h:128
bool separator_zero
Definition print.h:108
char * separator
Definition print.h:107
@ TRI_YES
Definition vacuumlo.c:38
@ TRI_DEFAULT
Definition vacuumlo.c:36
@ TRI_NO
Definition vacuumlo.c:37
bool DeleteVariable(VariableSpace space, const char *name)
Definition variables.c:474
void SetVariableHooks(VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
Definition variables.c:384
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition variables.c:486
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition variables.c:109
bool ParseVariableDouble(const char *value, const char *name, double *result, double min, double max)
Definition variables.c:195
bool SetVariableBool(VariableSpace space, const char *name)
Definition variables.c:462
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition variables.c:158
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition variables.c:281
VariableSpace CreateVariableSpace(void)
Definition variables.c:53
#define SIGCHLD
Definition win32_port.h:168
#define SIGALRM
Definition win32_port.h:164