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/*
844 * showVersion
845 *
846 * This output format is intended to match GNU standards.
847 */
848static void
850{
851 puts("psql (PostgreSQL) " PG_VERSION);
852}
853
854
855
856/*
857 * Substitute hooks and assign hooks for psql variables.
858 *
859 * This isn't an amazingly good place for them, but neither is anywhere else.
860 *
861 * By policy, every special variable that controls any psql behavior should
862 * have one or both hooks, even if they're just no-ops. This ensures that
863 * the variable will remain present in variables.c's list even when unset,
864 * which ensures that it's known to tab completion.
865 */
866
867static char *
869{
870 if (newval == NULL)
871 {
872 /* "\unset FOO" becomes "\set FOO off" */
873 newval = pg_strdup("off");
874 }
875 else if (newval[0] == '\0')
876 {
877 /* "\set FOO" becomes "\set FOO on" */
879 newval = pg_strdup("on");
880 }
881 return newval;
882}
883
884static bool
886{
887 return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
888}
889
890static bool
892{
893 return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
894}
895
896static bool
897quiet_hook(const char *newval)
898{
899 return ParseVariableBool(newval, "QUIET", &pset.quiet);
900}
901
902static bool
904{
905 return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
906}
907
908static bool
910{
911 return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
912}
913
914static char *
916{
917 if (newval == NULL)
918 newval = pg_strdup("0");
919 return newval;
920}
921
922static bool
924{
925 return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
926}
927
928static bool
930{
931 /*
932 * Someday we might try to validate the filename, but for now, this is
933 * just a placeholder to ensure HISTFILE is known to tab completion.
934 */
935 return true;
936}
937
938static char *
940{
941 if (newval == NULL)
942 newval = pg_strdup("500");
943 return newval;
944}
945
946static bool
948{
949 return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
950}
951
952static char *
959
960static bool
962{
963 return ParseVariableDouble(newval, "WATCH_INTERVAL", &pset.watch_interval,
965}
966
967static char *
969{
970 int dummy;
971
972 /*
973 * This tries to mimic the behavior of bash, to wit "If set, the value is
974 * the number of consecutive EOF characters which must be typed as the
975 * first characters on an input line before bash exits. If the variable
976 * exists but does not have a numeric value, or has no value, the default
977 * value is 10. If it does not exist, EOF signifies the end of input to
978 * the shell." Unlike bash, however, we insist on the stored value
979 * actually being a valid integer.
980 */
981 if (newval == NULL)
982 newval = pg_strdup("0");
983 else if (!ParseVariableNum(newval, NULL, &dummy))
984 newval = pg_strdup("10");
985 return newval;
986}
987
988static bool
990{
991 return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
992}
993
994static char *
996{
997 if (newval == NULL)
998 newval = pg_strdup("none");
999 return newval;
1000}
1001
1002static bool
1003echo_hook(const char *newval)
1004{
1005 Assert(newval != NULL); /* else substitute hook messed up */
1006 if (pg_strcasecmp(newval, "queries") == 0)
1008 else if (pg_strcasecmp(newval, "errors") == 0)
1010 else if (pg_strcasecmp(newval, "all") == 0)
1012 else if (pg_strcasecmp(newval, "none") == 0)
1014 else
1015 {
1016 PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
1017 return false;
1018 }
1019 return true;
1020}
1021
1022static bool
1024{
1025 Assert(newval != NULL); /* else substitute hook messed up */
1026 if (pg_strcasecmp(newval, "noexec") == 0)
1028 else
1029 {
1030 bool on_off;
1031
1034 else
1035 {
1036 PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
1037 return false;
1038 }
1039 }
1040 return true;
1041}
1042
1043static bool
1045{
1046 Assert(newval != NULL); /* else substitute hook messed up */
1047 if (pg_strcasecmp(newval, "interactive") == 0)
1049 else
1050 {
1051 bool on_off;
1052
1055 else
1056 {
1057 PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
1058 return false;
1059 }
1060 }
1061 return true;
1062}
1063
1064static char *
1066{
1067 if (newval == NULL)
1068 newval = pg_strdup("preserve-upper");
1069 return newval;
1070}
1071
1072static bool
1074{
1075 Assert(newval != NULL); /* else substitute hook messed up */
1076 if (pg_strcasecmp(newval, "preserve-upper") == 0)
1078 else if (pg_strcasecmp(newval, "preserve-lower") == 0)
1080 else if (pg_strcasecmp(newval, "upper") == 0)
1082 else if (pg_strcasecmp(newval, "lower") == 0)
1084 else
1085 {
1086 PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
1087 "lower, upper, preserve-lower, preserve-upper");
1088 return false;
1089 }
1090 return true;
1091}
1092
1093static char *
1095{
1096 if (newval == NULL)
1097 newval = pg_strdup("none");
1098 return newval;
1099}
1100
1101static bool
1103{
1104 Assert(newval != NULL); /* else substitute hook messed up */
1105 if (pg_strcasecmp(newval, "ignorespace") == 0)
1107 else if (pg_strcasecmp(newval, "ignoredups") == 0)
1109 else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1111 else if (pg_strcasecmp(newval, "none") == 0)
1113 else
1114 {
1115 PsqlVarEnumError("HISTCONTROL", newval,
1116 "none, ignorespace, ignoredups, ignoreboth");
1117 return false;
1118 }
1119 return true;
1120}
1121
1122static bool
1124{
1125 pset.prompt1 = newval ? newval : "";
1126 return true;
1127}
1128
1129static bool
1131{
1132 pset.prompt2 = newval ? newval : "";
1133 return true;
1134}
1135
1136static bool
1138{
1139 pset.prompt3 = newval ? newval : "";
1140 return true;
1141}
1142
1143static char *
1145{
1146 if (newval == NULL)
1147 newval = pg_strdup("default");
1148 return newval;
1149}
1150
1151static bool
1153{
1154 Assert(newval != NULL); /* else substitute hook messed up */
1155 if (pg_strcasecmp(newval, "default") == 0)
1157 else if (pg_strcasecmp(newval, "verbose") == 0)
1159 else if (pg_strcasecmp(newval, "terse") == 0)
1161 else if (pg_strcasecmp(newval, "sqlstate") == 0)
1163 else
1164 {
1165 PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
1166 return false;
1167 }
1168
1169 if (pset.db)
1171 return true;
1172}
1173
1174static bool
1176{
1177 return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
1178}
1179
1180static char *
1182{
1183 if (newval == NULL)
1184 newval = pg_strdup("errors");
1185 return newval;
1186}
1187
1188static bool
1190{
1191 Assert(newval != NULL); /* else substitute hook messed up */
1192 if (pg_strcasecmp(newval, "never") == 0)
1194 else if (pg_strcasecmp(newval, "errors") == 0)
1196 else if (pg_strcasecmp(newval, "always") == 0)
1198 else
1199 {
1200 PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1201 return false;
1202 }
1203
1204 if (pset.db)
1206 return true;
1207}
1208
1209static bool
1211{
1212 return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
1214}
1215
1216static bool
1218{
1219 return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
1220}
1221
1222static void
1224{
1226
1227 SetVariableHooks(pset.vars, "AUTOCOMMIT",
1230 SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1233 SetVariableHooks(pset.vars, "QUIET",
1235 quiet_hook);
1236 SetVariableHooks(pset.vars, "SINGLELINE",
1239 SetVariableHooks(pset.vars, "SINGLESTEP",
1242 SetVariableHooks(pset.vars, "FETCH_COUNT",
1245 SetVariableHooks(pset.vars, "HISTFILE",
1246 NULL,
1248 SetVariableHooks(pset.vars, "HISTSIZE",
1251 SetVariableHooks(pset.vars, "IGNOREEOF",
1254 SetVariableHooks(pset.vars, "ECHO",
1256 echo_hook);
1257 SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1260 SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1263 SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1266 SetVariableHooks(pset.vars, "HISTCONTROL",
1269 SetVariableHooks(pset.vars, "PROMPT1",
1270 NULL,
1271 prompt1_hook);
1272 SetVariableHooks(pset.vars, "PROMPT2",
1273 NULL,
1274 prompt2_hook);
1275 SetVariableHooks(pset.vars, "PROMPT3",
1276 NULL,
1277 prompt3_hook);
1278 SetVariableHooks(pset.vars, "VERBOSITY",
1281 SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
1284 SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1287 SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
1290 SetVariableHooks(pset.vars, "HIDE_TABLEAM",
1293 SetVariableHooks(pset.vars, "WATCH_INTERVAL",
1296}
void expand_tilde(char **filename)
Definition common.c:2579
PGresult * PSQLexec(const char *query)
Definition common.c:657
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:2502
bool setQFout(const char *fname)
Definition common.c:144
bool SendQuery(const char *query)
Definition common.c:1120
static char * verbosity_substitute_hook(char *newval)
Definition startup.c:1144
static bool show_context_hook(const char *newval)
Definition startup.c:1189
static bool hide_tableam_hook(const char *newval)
Definition startup.c:1217
static bool quiet_hook(const char *newval)
Definition startup.c:897
static bool singleline_hook(const char *newval)
Definition startup.c:903
static bool prompt2_hook(const char *newval)
Definition startup.c:1130
static bool comp_keyword_case_hook(const char *newval)
Definition startup.c:1073
#define SYSPSQLRC
Definition startup.c:36
static bool histcontrol_hook(const char *newval)
Definition startup.c:1102
static bool prompt1_hook(const char *newval)
Definition startup.c:1123
#define PSQLRC
Definition startup.c:37
static bool verbosity_hook(const char *newval)
Definition startup.c:1152
static void empty_signal_handler(SIGNAL_ARGS)
Definition startup.c:116
static char * ignoreeof_substitute_hook(char *newval)
Definition startup.c:968
static bool show_all_results_hook(const char *newval)
Definition startup.c:1175
static bool fetch_count_hook(const char *newval)
Definition startup.c:923
static void process_psqlrc(char *argv0)
Definition startup.c:783
static char * fetch_count_substitute_hook(char *newval)
Definition startup.c:915
static void EstablishVariableSpace(void)
Definition startup.c:1223
static bool histsize_hook(const char *newval)
Definition startup.c:947
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:995
static char * watch_interval_substitute_hook(char *newval)
Definition startup.c:953
static bool hide_compression_hook(const char *newval)
Definition startup.c:1210
static char * bool_substitute_hook(char *newval)
Definition startup.c:868
#define PARAMS_ARRAY_SIZE
static bool singlestep_hook(const char *newval)
Definition startup.c:909
static bool histfile_hook(const char *newval)
Definition startup.c:929
static bool prompt3_hook(const char *newval)
Definition startup.c:1137
static bool on_error_rollback_hook(const char *newval)
Definition startup.c:1044
static char * histsize_substitute_hook(char *newval)
Definition startup.c:939
static bool on_error_stop_hook(const char *newval)
Definition startup.c:891
static bool autocommit_hook(const char *newval)
Definition startup.c:885
static char * show_context_substitute_hook(char *newval)
Definition startup.c:1181
static bool watch_interval_hook(const char *newval)
Definition startup.c:961
static bool ignoreeof_hook(const char *newval)
Definition startup.c:989
PsqlSettings pset
Definition startup.c:33
static bool echo_hidden_hook(const char *newval)
Definition startup.c:1023
static void process_psqlrc_file(char *filename)
Definition startup.c:817
static bool echo_hook(const char *newval)
Definition startup.c:1003
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:849
static char * histcontrol_substitute_hook(char *newval)
Definition startup.c:1094
static char * comp_keyword_case_substitute_hook(char *newval)
Definition startup.c:1065
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define SIGNAL_ARGS
Definition c.h:1462
#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:96
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:91
void pg_free(void *ptr)
#define pg_malloc_array(type, count)
Definition fe_memutils.h:66
#define pg_malloc_object(type)
Definition fe_memutils.h:60
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:84
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:85
void pg_logging_set_locus_callback(void(*cb)(const char **filename, uint64 *lineno))
Definition logging.c:204
static void(* log_locus_callback)(const char **, uint64 *)
Definition logging.c:27
void pg_logging_config(int new_flags)
Definition logging.c:168
void pg_logging_set_pre_callback(void(*cb)(void))
Definition logging.c:198
#define pg_log_error(...)
Definition logging.h:108
#define pg_log_error_hint(...)
Definition logging.h:114
#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:1910
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:6172
#define pg_log_warning(...)
Definition pgfnames.c:24
#define pqsignal
Definition port.h:548
bool get_home_path(char *ret_path)
Definition path.c:1022
int pg_strcasecmp(const char *s1, const char *s2)
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition path.c:928
#define snprintf
Definition port.h:261
const char * get_progname(const char *argv0)
Definition path.c:669
#define printf(...)
Definition port.h:267
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:475
void SetVariableHooks(VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
Definition variables.c:385
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition variables.c:487
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:463
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:282
VariableSpace CreateVariableSpace(void)
Definition variables.c:53
#define SIGCHLD
Definition win32_port.h:168
#define SIGALRM
Definition win32_port.h:164