PostgreSQL Source Code  git master
startup.c
Go to the documentation of this file.
1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Copyright (c) 2000-2021, PostgreSQL Global Development Group
5  *
6  * src/bin/psql/startup.c
7  */
8 #include "postgres_fe.h"
9 
10 #ifndef WIN32
11 #include <unistd.h>
12 #else /* WIN32 */
13 #include <io.h>
14 #include <win32.h>
15 #endif /* WIN32 */
16 
17 #include "command.h"
18 #include "common.h"
19 #include "common/logging.h"
20 #include "common/string.h"
21 #include "describe.h"
22 #include "fe_utils/print.h"
23 #include "getopt_long.h"
24 #include "help.h"
25 #include "input.h"
26 #include "mainloop.h"
27 #include "settings.h"
28 
29 /*
30  * Global psql options
31  */
33 
34 #ifndef WIN32
35 #define SYSPSQLRC "psqlrc"
36 #define PSQLRC ".psqlrc"
37 #else
38 #define SYSPSQLRC "psqlrc"
39 #define PSQLRC "psqlrc.conf"
40 #endif
41 
42 /*
43  * Structures to pass information between the option parsing routine
44  * and the main function
45  */
47 {
50  ACT_FILE
51 };
52 
53 typedef struct SimpleActionListCell
54 {
56  enum _actions action;
57  char *val;
59 
60 typedef struct SimpleActionList
61 {
65 
66 struct adhoc_opts
67 {
68  char *dbname;
69  char *host;
70  char *port;
71  char *username;
72  char *logfilename;
74  bool no_psqlrc;
75  bool single_txn;
76  bool list_dbs;
78 };
79 
80 static void parse_psql_options(int argc, char *argv[],
81  struct adhoc_opts *options);
83  enum _actions action, const char *val);
84 static void process_psqlrc(char *argv0);
85 static void process_psqlrc_file(char *filename);
86 static void showVersion(void);
87 static void EstablishVariableSpace(void);
88 
89 #define NOPAGER 0
90 
91 static void
93 {
96 }
97 
98 static void
99 log_locus_callback(const char **filename, uint64 *lineno)
100 {
101  if (pset.inputfile)
102  {
104  *lineno = pset.lineno;
105  }
106  else
107  {
108  *filename = NULL;
109  *lineno = 0;
110  }
111 }
112 
113 #ifdef HAVE_POSIX_DECL_SIGWAIT
114 static void
115 empty_signal_handler(SIGNAL_ARGS)
116 {
117 }
118 #endif
119 
120 /*
121  *
122  * main
123  *
124  */
125 int
126 main(int argc, char *argv[])
127 {
128  struct adhoc_opts options;
129  int successResult;
130  char *password = NULL;
131  bool new_pass;
132 
133  pg_logging_init(argv[0]);
136  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
137 
138  if (argc > 1)
139  {
140  if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
141  {
142  usage(NOPAGER);
144  }
145  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
146  {
147  showVersion();
149  }
150  }
151 
152  pset.progname = get_progname(argv[0]);
153 
154  pset.db = NULL;
155  pset.dead_conn = NULL;
159  pset.queryFoutPipe = false;
160  pset.copyStream = NULL;
161  pset.last_error_result = NULL;
162  pset.cur_cmd_source = stdin;
163  pset.cur_cmd_interactive = false;
164 
165  /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
167  pset.popt.topt.border = 1;
168  pset.popt.topt.pager = 1;
170  pset.popt.topt.start_table = true;
171  pset.popt.topt.stop_table = true;
172  pset.popt.topt.default_footer = true;
173 
175  pset.popt.topt.csvFieldSep[1] = '\0';
176 
180 
182 
183  /* We must get COLUMNS here before readline() sets it */
184  pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
185 
186  pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
187 
189 
191 
192  /* Create variables showing psql version number */
193  SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
194  SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
195  SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
196 
197  /* Initialize variables for last error */
198  SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
199  SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
200 
201  /* Default values for variables (that don't match the result of \unset) */
202  SetVariableBool(pset.vars, "AUTOCOMMIT");
203  SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
204  SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
205  SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
206 
207  parse_psql_options(argc, argv, &options);
208 
209  /*
210  * If no action was specified and we're in non-interactive mode, treat it
211  * as if the user had specified "-f -". This lets single-transaction mode
212  * work in this case.
213  */
214  if (options.actions.head == NULL && pset.notty)
215  simple_action_list_append(&options.actions, ACT_FILE, NULL);
216 
217  /* Bail out if -1 was specified but will be ignored. */
218  if (options.single_txn && options.actions.head == NULL)
219  {
220  pg_log_fatal("-1 can only be used in non-interactive mode");
222  }
223 
226  {
229  }
232  {
235  }
236 
237  if (pset.getPassword == TRI_YES)
238  {
239  /*
240  * We can't be sure yet of the username that will be used, so don't
241  * offer a potentially wrong one. Typical uses of this option are
242  * noninteractive anyway. (Note: since we've not yet set up our
243  * cancel handler, there's no need to use simple_prompt_extended.)
244  */
245  password = simple_prompt("Password: ", false);
246  }
247 
248  /* loop until we have a password if requested by backend */
249  do
250  {
251 #define PARAMS_ARRAY_SIZE 8
252  const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
253  const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
254 
255  keywords[0] = "host";
256  values[0] = options.host;
257  keywords[1] = "port";
258  values[1] = options.port;
259  keywords[2] = "user";
260  values[2] = options.username;
261  keywords[3] = "password";
262  values[3] = password;
263  keywords[4] = "dbname"; /* see do_connect() */
264  values[4] = (options.list_dbs && options.dbname == NULL) ?
265  "postgres" : options.dbname;
266  keywords[5] = "fallback_application_name";
267  values[5] = pset.progname;
268  keywords[6] = "client_encoding";
269  values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
270  keywords[7] = NULL;
271  values[7] = NULL;
272 
273  new_pass = false;
274  pset.db = PQconnectdbParams(keywords, values, true);
275  free(keywords);
276  free(values);
277 
278  if (PQstatus(pset.db) == CONNECTION_BAD &&
280  !password &&
282  {
283  /*
284  * Before closing the old PGconn, extract the user name that was
285  * actually connected with --- it might've come out of a URI or
286  * connstring "database name" rather than options.username.
287  */
288  const char *realusername = PQuser(pset.db);
289  char *password_prompt;
290 
291  if (realusername && realusername[0])
292  password_prompt = psprintf(_("Password for user %s: "),
293  realusername);
294  else
295  password_prompt = pg_strdup(_("Password: "));
296  PQfinish(pset.db);
297 
298  password = simple_prompt(password_prompt, false);
299  free(password_prompt);
300  new_pass = true;
301  }
302  } while (new_pass);
303 
304  if (PQstatus(pset.db) == CONNECTION_BAD)
305  {
307  PQfinish(pset.db);
309  }
310 
312 
313 #ifdef HAVE_POSIX_DECL_SIGWAIT
314 
315  /*
316  * do_watch() needs signal handlers installed (otherwise sigwait() will
317  * filter them out on some platforms), but doesn't need them to do
318  * anything, and they shouldn't ever run (unless perhaps a stray SIGALRM
319  * arrives due to a race when do_watch() cancels an itimer).
320  */
321  pqsignal(SIGCHLD, empty_signal_handler);
322  pqsignal(SIGALRM, empty_signal_handler);
323 #endif
324 
326 
327  SyncVariables();
328 
329  if (options.list_dbs)
330  {
331  int success;
332 
333  if (!options.no_psqlrc)
334  process_psqlrc(argv[0]);
335 
336  success = listAllDbs(NULL, false);
337  PQfinish(pset.db);
339  }
340 
341  if (options.logfilename)
342  {
343  pset.logfile = fopen(options.logfilename, "a");
344  if (!pset.logfile)
345  {
346  pg_log_fatal("could not open log file \"%s\": %m",
347  options.logfilename);
349  }
350  }
351 
352  if (!options.no_psqlrc)
353  process_psqlrc(argv[0]);
354 
355  /*
356  * If any actions were given by user, process them in the order in which
357  * they were specified. Note single_txn is only effective in this mode.
358  */
359  if (options.actions.head != NULL)
360  {
361  PGresult *res;
362  SimpleActionListCell *cell;
363 
364  successResult = EXIT_SUCCESS; /* silence compiler */
365 
366  if (options.single_txn)
367  {
368  if ((res = PSQLexec("BEGIN")) == NULL)
369  {
370  if (pset.on_error_stop)
371  {
372  successResult = EXIT_USER;
373  goto error;
374  }
375  }
376  else
377  PQclear(res);
378  }
379 
380  for (cell = options.actions.head; cell; cell = cell->next)
381  {
382  if (cell->action == ACT_SINGLE_QUERY)
383  {
385 
386  if (pset.echo == PSQL_ECHO_ALL)
387  puts(cell->val);
388 
389  successResult = SendQuery(cell->val)
391  }
392  else if (cell->action == ACT_SINGLE_SLASH)
393  {
394  PsqlScanState scan_state;
395  ConditionalStack cond_stack;
396 
398 
399  if (pset.echo == PSQL_ECHO_ALL)
400  puts(cell->val);
401 
402  scan_state = psql_scan_create(&psqlscan_callbacks);
403  psql_scan_setup(scan_state,
404  cell->val, strlen(cell->val),
406  cond_stack = conditional_stack_create();
407  psql_scan_set_passthrough(scan_state, (void *) cond_stack);
408 
409  successResult = HandleSlashCmds(scan_state,
410  cond_stack,
411  NULL,
412  NULL) != PSQL_CMD_ERROR
414 
415  psql_scan_destroy(scan_state);
416  conditional_stack_destroy(cond_stack);
417  }
418  else if (cell->action == ACT_FILE)
419  {
420  successResult = process_file(cell->val, false);
421  }
422  else
423  {
424  /* should never come here */
425  Assert(false);
426  }
427 
428  if (successResult != EXIT_SUCCESS && pset.on_error_stop)
429  break;
430  }
431 
432  if (options.single_txn)
433  {
434  if ((res = PSQLexec("COMMIT")) == NULL)
435  {
436  if (pset.on_error_stop)
437  {
438  successResult = EXIT_USER;
439  goto error;
440  }
441  }
442  else
443  PQclear(res);
444  }
445 
446 error:
447  ;
448  }
449 
450  /*
451  * or otherwise enter interactive main loop
452  */
453  else
454  {
456  connection_warnings(true);
457  if (!pset.quiet)
458  printf(_("Type \"help\" for help.\n\n"));
459  initializeInput(options.no_readline ? 0 : 1);
460  successResult = MainLoop(stdin);
461  }
462 
463  /* clean up */
464  if (pset.logfile)
465  fclose(pset.logfile);
466  if (pset.db)
467  PQfinish(pset.db);
468  if (pset.dead_conn)
470  setQFout(NULL);
471 
472  return successResult;
473 }
474 
475 
476 /*
477  * Parse command line options
478  */
479 
480 static void
481 parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
482 {
483  static struct option long_options[] =
484  {
485  {"echo-all", no_argument, NULL, 'a'},
486  {"no-align", no_argument, NULL, 'A'},
487  {"command", required_argument, NULL, 'c'},
488  {"dbname", required_argument, NULL, 'd'},
489  {"echo-queries", no_argument, NULL, 'e'},
490  {"echo-errors", no_argument, NULL, 'b'},
491  {"echo-hidden", no_argument, NULL, 'E'},
492  {"file", required_argument, NULL, 'f'},
493  {"field-separator", required_argument, NULL, 'F'},
494  {"field-separator-zero", no_argument, NULL, 'z'},
495  {"host", required_argument, NULL, 'h'},
496  {"html", no_argument, NULL, 'H'},
497  {"list", no_argument, NULL, 'l'},
498  {"log-file", required_argument, NULL, 'L'},
499  {"no-readline", no_argument, NULL, 'n'},
500  {"single-transaction", no_argument, NULL, '1'},
501  {"output", required_argument, NULL, 'o'},
502  {"port", required_argument, NULL, 'p'},
503  {"pset", required_argument, NULL, 'P'},
504  {"quiet", no_argument, NULL, 'q'},
505  {"record-separator", required_argument, NULL, 'R'},
506  {"record-separator-zero", no_argument, NULL, '0'},
507  {"single-step", no_argument, NULL, 's'},
508  {"single-line", no_argument, NULL, 'S'},
509  {"tuples-only", no_argument, NULL, 't'},
510  {"table-attr", required_argument, NULL, 'T'},
511  {"username", required_argument, NULL, 'U'},
512  {"set", required_argument, NULL, 'v'},
513  {"variable", required_argument, NULL, 'v'},
514  {"version", no_argument, NULL, 'V'},
515  {"no-password", no_argument, NULL, 'w'},
516  {"password", no_argument, NULL, 'W'},
517  {"expanded", no_argument, NULL, 'x'},
518  {"no-psqlrc", no_argument, NULL, 'X'},
519  {"help", optional_argument, NULL, 1},
520  {"csv", no_argument, NULL, 2},
521  {NULL, 0, NULL, 0}
522  };
523 
524  int optindex;
525  int c;
526 
527  memset(options, 0, sizeof *options);
528 
529  while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
530  long_options, &optindex)) != -1)
531  {
532  switch (c)
533  {
534  case 'a':
535  SetVariable(pset.vars, "ECHO", "all");
536  break;
537  case 'A':
539  break;
540  case 'b':
541  SetVariable(pset.vars, "ECHO", "errors");
542  break;
543  case 'c':
544  if (optarg[0] == '\\')
547  optarg + 1);
548  else
551  optarg);
552  break;
553  case 'd':
555  break;
556  case 'e':
557  SetVariable(pset.vars, "ECHO", "queries");
558  break;
559  case 'E':
560  SetVariableBool(pset.vars, "ECHO_HIDDEN");
561  break;
562  case 'f':
564  ACT_FILE,
565  optarg);
566  break;
567  case 'F':
570  break;
571  case 'h':
572  options->host = pg_strdup(optarg);
573  break;
574  case 'H':
576  break;
577  case 'l':
578  options->list_dbs = true;
579  break;
580  case 'L':
581  options->logfilename = pg_strdup(optarg);
582  break;
583  case 'n':
584  options->no_readline = true;
585  break;
586  case 'o':
587  if (!setQFout(optarg))
589  break;
590  case 'p':
592  break;
593  case 'P':
594  {
595  char *value;
596  char *equal_loc;
597  bool result;
598 
600  equal_loc = strchr(value, '=');
601  if (!equal_loc)
602  result = do_pset(value, NULL, &pset.popt, true);
603  else
604  {
605  *equal_loc = '\0';
606  result = do_pset(value, equal_loc + 1, &pset.popt, true);
607  }
608 
609  if (!result)
610  {
611  pg_log_fatal("could not set printing parameter \"%s\"", value);
613  }
614 
615  free(value);
616  break;
617  }
618  case 'q':
619  SetVariableBool(pset.vars, "QUIET");
620  break;
621  case 'R':
624  break;
625  case 's':
626  SetVariableBool(pset.vars, "SINGLESTEP");
627  break;
628  case 'S':
629  SetVariableBool(pset.vars, "SINGLELINE");
630  break;
631  case 't':
632  pset.popt.topt.tuples_only = true;
633  break;
634  case 'T':
636  break;
637  case 'U':
639  break;
640  case 'v':
641  {
642  char *value;
643  char *equal_loc;
644 
646  equal_loc = strchr(value, '=');
647  if (!equal_loc)
648  {
649  if (!DeleteVariable(pset.vars, value))
650  exit(EXIT_FAILURE); /* error already printed */
651  }
652  else
653  {
654  *equal_loc = '\0';
655  if (!SetVariable(pset.vars, value, equal_loc + 1))
656  exit(EXIT_FAILURE); /* error already printed */
657  }
658 
659  free(value);
660  break;
661  }
662  case 'V':
663  showVersion();
665  case 'w':
667  break;
668  case 'W':
670  break;
671  case 'x':
672  pset.popt.topt.expanded = true;
673  break;
674  case 'X':
675  options->no_psqlrc = true;
676  break;
677  case 'z':
679  break;
680  case '0':
682  break;
683  case '1':
684  options->single_txn = true;
685  break;
686  case '?':
687  if (optind <= argc &&
688  strcmp(argv[optind - 1], "-?") == 0)
689  {
690  /* actual help option given */
691  usage(NOPAGER);
693  }
694  else
695  {
696  /* getopt error (unknown option or missing argument) */
697  goto unknown_option;
698  }
699  break;
700  case 1:
701  {
702  if (!optarg || strcmp(optarg, "options") == 0)
703  usage(NOPAGER);
704  else if (optarg && strcmp(optarg, "commands") == 0)
706  else if (optarg && strcmp(optarg, "variables") == 0)
708  else
709  goto unknown_option;
710 
712  }
713  break;
714  case 2:
716  break;
717  default:
718  unknown_option:
719  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
720  pset.progname);
722  break;
723  }
724  }
725 
726  /*
727  * if we still have arguments, use it as the database name and username
728  */
729  while (argc - optind >= 1)
730  {
731  if (!options->dbname)
732  options->dbname = argv[optind];
733  else if (!options->username)
734  options->username = argv[optind];
735  else if (!pset.quiet)
736  pg_log_warning("extra command-line argument \"%s\" ignored",
737  argv[optind]);
738 
739  optind++;
740  }
741 }
742 
743 
744 /*
745  * Append a new item to the end of the SimpleActionList.
746  * Note that "val" is copied if it's not NULL.
747  */
748 static void
750  enum _actions action, const char *val)
751 {
752  SimpleActionListCell *cell;
753 
755 
756  cell->next = NULL;
757  cell->action = action;
758  if (val)
759  cell->val = pg_strdup(val);
760  else
761  cell->val = NULL;
762 
763  if (list->tail)
764  list->tail->next = cell;
765  else
766  list->head = cell;
767  list->tail = cell;
768 }
769 
770 
771 /*
772  * Load .psqlrc file, if found.
773  */
774 static void
776 {
777  char home[MAXPGPATH];
778  char rc_file[MAXPGPATH];
779  char my_exec_path[MAXPGPATH];
780  char etc_path[MAXPGPATH];
781  char *envrc = getenv("PSQLRC");
782 
783  if (find_my_exec(argv0, my_exec_path) < 0)
784  {
785  pg_log_fatal("could not find own program executable");
787  }
788 
789  get_etc_path(my_exec_path, etc_path);
790 
791  snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
792  process_psqlrc_file(rc_file);
793 
794  if (envrc != NULL && strlen(envrc) > 0)
795  {
796  /* might need to free() this */
797  char *envrc_alloc = pstrdup(envrc);
798 
799  expand_tilde(&envrc_alloc);
800  process_psqlrc_file(envrc_alloc);
801  }
802  else if (get_home_path(home))
803  {
804  snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
805  process_psqlrc_file(rc_file);
806  }
807 }
808 
809 
810 
811 static void
813 {
814  char *psqlrc_minor,
815  *psqlrc_major;
816 
817 #if defined(WIN32) && (!defined(__MINGW32__))
818 #define R_OK 4
819 #endif
820 
821  psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
822  psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
823 
824  /* check for minor version first, then major, then no version */
825  if (access(psqlrc_minor, R_OK) == 0)
826  (void) process_file(psqlrc_minor, false);
827  else if (access(psqlrc_major, R_OK) == 0)
828  (void) process_file(psqlrc_major, false);
829  else if (access(filename, R_OK) == 0)
830  (void) process_file(filename, false);
831 
832  free(psqlrc_minor);
833  free(psqlrc_major);
834 }
835 
836 
837 
838 /* showVersion
839  *
840  * This output format is intended to match GNU standards.
841  */
842 static void
844 {
845  puts("psql (PostgreSQL) " PG_VERSION);
846 }
847 
848 
849 
850 /*
851  * Substitute hooks and assign hooks for psql variables.
852  *
853  * This isn't an amazingly good place for them, but neither is anywhere else.
854  *
855  * By policy, every special variable that controls any psql behavior should
856  * have one or both hooks, even if they're just no-ops. This ensures that
857  * the variable will remain present in variables.c's list even when unset,
858  * which ensures that it's known to tab completion.
859  */
860 
861 static char *
863 {
864  if (newval == NULL)
865  {
866  /* "\unset FOO" becomes "\set FOO off" */
867  newval = pg_strdup("off");
868  }
869  else if (newval[0] == '\0')
870  {
871  /* "\set FOO" becomes "\set FOO on" */
872  pg_free(newval);
873  newval = pg_strdup("on");
874  }
875  return newval;
876 }
877 
878 static bool
880 {
881  return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
882 }
883 
884 static bool
886 {
887  return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
888 }
889 
890 static bool
891 quiet_hook(const char *newval)
892 {
893  return ParseVariableBool(newval, "QUIET", &pset.quiet);
894 }
895 
896 static bool
898 {
899  return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
900 }
901 
902 static bool
904 {
905  return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
906 }
907 
908 static char *
910 {
911  if (newval == NULL)
912  newval = pg_strdup("0");
913  return newval;
914 }
915 
916 static bool
918 {
919  return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
920 }
921 
922 static bool
923 histfile_hook(const char *newval)
924 {
925  /*
926  * Someday we might try to validate the filename, but for now, this is
927  * just a placeholder to ensure HISTFILE is known to tab completion.
928  */
929  return true;
930 }
931 
932 static char *
934 {
935  if (newval == NULL)
936  newval = pg_strdup("500");
937  return newval;
938 }
939 
940 static bool
941 histsize_hook(const char *newval)
942 {
943  return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
944 }
945 
946 static char *
948 {
949  int dummy;
950 
951  /*
952  * This tries to mimic the behavior of bash, to wit "If set, the value is
953  * the number of consecutive EOF characters which must be typed as the
954  * first characters on an input line before bash exits. If the variable
955  * exists but does not have a numeric value, or has no value, the default
956  * value is 10. If it does not exist, EOF signifies the end of input to
957  * the shell." Unlike bash, however, we insist on the stored value
958  * actually being a valid integer.
959  */
960  if (newval == NULL)
961  newval = pg_strdup("0");
962  else if (!ParseVariableNum(newval, NULL, &dummy))
963  newval = pg_strdup("10");
964  return newval;
965 }
966 
967 static bool
968 ignoreeof_hook(const char *newval)
969 {
970  return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
971 }
972 
973 static char *
975 {
976  if (newval == NULL)
977  newval = pg_strdup("none");
978  return newval;
979 }
980 
981 static bool
982 echo_hook(const char *newval)
983 {
984  Assert(newval != NULL); /* else substitute hook messed up */
985  if (pg_strcasecmp(newval, "queries") == 0)
987  else if (pg_strcasecmp(newval, "errors") == 0)
989  else if (pg_strcasecmp(newval, "all") == 0)
991  else if (pg_strcasecmp(newval, "none") == 0)
993  else
994  {
995  PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
996  return false;
997  }
998  return true;
999 }
1000 
1001 static bool
1003 {
1004  Assert(newval != NULL); /* else substitute hook messed up */
1005  if (pg_strcasecmp(newval, "noexec") == 0)
1007  else
1008  {
1009  bool on_off;
1010 
1011  if (ParseVariableBool(newval, NULL, &on_off))
1013  else
1014  {
1015  PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
1016  return false;
1017  }
1018  }
1019  return true;
1020 }
1021 
1022 static bool
1024 {
1025  Assert(newval != NULL); /* else substitute hook messed up */
1026  if (pg_strcasecmp(newval, "interactive") == 0)
1028  else
1029  {
1030  bool on_off;
1031 
1032  if (ParseVariableBool(newval, NULL, &on_off))
1034  else
1035  {
1036  PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
1037  return false;
1038  }
1039  }
1040  return true;
1041 }
1042 
1043 static char *
1045 {
1046  if (newval == NULL)
1047  newval = pg_strdup("preserve-upper");
1048  return newval;
1049 }
1050 
1051 static bool
1053 {
1054  Assert(newval != NULL); /* else substitute hook messed up */
1055  if (pg_strcasecmp(newval, "preserve-upper") == 0)
1057  else if (pg_strcasecmp(newval, "preserve-lower") == 0)
1059  else if (pg_strcasecmp(newval, "upper") == 0)
1061  else if (pg_strcasecmp(newval, "lower") == 0)
1063  else
1064  {
1065  PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
1066  "lower, upper, preserve-lower, preserve-upper");
1067  return false;
1068  }
1069  return true;
1070 }
1071 
1072 static char *
1074 {
1075  if (newval == NULL)
1076  newval = pg_strdup("none");
1077  return newval;
1078 }
1079 
1080 static bool
1082 {
1083  Assert(newval != NULL); /* else substitute hook messed up */
1084  if (pg_strcasecmp(newval, "ignorespace") == 0)
1086  else if (pg_strcasecmp(newval, "ignoredups") == 0)
1088  else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1090  else if (pg_strcasecmp(newval, "none") == 0)
1092  else
1093  {
1094  PsqlVarEnumError("HISTCONTROL", newval,
1095  "none, ignorespace, ignoredups, ignoreboth");
1096  return false;
1097  }
1098  return true;
1099 }
1100 
1101 static bool
1102 prompt1_hook(const char *newval)
1103 {
1104  pset.prompt1 = newval ? newval : "";
1105  return true;
1106 }
1107 
1108 static bool
1109 prompt2_hook(const char *newval)
1110 {
1111  pset.prompt2 = newval ? newval : "";
1112  return true;
1113 }
1114 
1115 static bool
1116 prompt3_hook(const char *newval)
1117 {
1118  pset.prompt3 = newval ? newval : "";
1119  return true;
1120 }
1121 
1122 static char *
1124 {
1125  if (newval == NULL)
1126  newval = pg_strdup("default");
1127  return newval;
1128 }
1129 
1130 static bool
1132 {
1133  Assert(newval != NULL); /* else substitute hook messed up */
1134  if (pg_strcasecmp(newval, "default") == 0)
1136  else if (pg_strcasecmp(newval, "verbose") == 0)
1138  else if (pg_strcasecmp(newval, "terse") == 0)
1140  else if (pg_strcasecmp(newval, "sqlstate") == 0)
1142  else
1143  {
1144  PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
1145  return false;
1146  }
1147 
1148  if (pset.db)
1150  return true;
1151 }
1152 
1153 static char *
1155 {
1156  if (newval == NULL)
1157  newval = pg_strdup("errors");
1158  return newval;
1159 }
1160 
1161 static bool
1163 {
1164  Assert(newval != NULL); /* else substitute hook messed up */
1165  if (pg_strcasecmp(newval, "never") == 0)
1167  else if (pg_strcasecmp(newval, "errors") == 0)
1169  else if (pg_strcasecmp(newval, "always") == 0)
1171  else
1172  {
1173  PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1174  return false;
1175  }
1176 
1177  if (pset.db)
1179  return true;
1180 }
1181 
1182 static bool
1184 {
1185  return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
1187 }
1188 
1189 static bool
1191 {
1192  return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
1193 }
1194 
1195 static void
1197 {
1199 
1200  SetVariableHooks(pset.vars, "AUTOCOMMIT",
1202  autocommit_hook);
1203  SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1206  SetVariableHooks(pset.vars, "QUIET",
1208  quiet_hook);
1209  SetVariableHooks(pset.vars, "SINGLELINE",
1211  singleline_hook);
1212  SetVariableHooks(pset.vars, "SINGLESTEP",
1214  singlestep_hook);
1215  SetVariableHooks(pset.vars, "FETCH_COUNT",
1218  SetVariableHooks(pset.vars, "HISTFILE",
1219  NULL,
1220  histfile_hook);
1221  SetVariableHooks(pset.vars, "HISTSIZE",
1223  histsize_hook);
1224  SetVariableHooks(pset.vars, "IGNOREEOF",
1226  ignoreeof_hook);
1227  SetVariableHooks(pset.vars, "ECHO",
1229  echo_hook);
1230  SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1233  SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1236  SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1239  SetVariableHooks(pset.vars, "HISTCONTROL",
1242  SetVariableHooks(pset.vars, "PROMPT1",
1243  NULL,
1244  prompt1_hook);
1245  SetVariableHooks(pset.vars, "PROMPT2",
1246  NULL,
1247  prompt2_hook);
1248  SetVariableHooks(pset.vars, "PROMPT3",
1249  NULL,
1250  prompt3_hook);
1251  SetVariableHooks(pset.vars, "VERBOSITY",
1253  verbosity_hook);
1254  SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1257  SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
1260  SetVariableHooks(pset.vars, "HIDE_TABLEAM",
1263 }
void expand_tilde(char **filename)
Definition: common.c:2227
void psql_setup_cancel_handler(void)
Definition: common.c:266
PGresult * PSQLexec(const char *query)
Definition: common.c:540
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:220
bool standard_strings(void)
Definition: common.c:2186
bool setQFout(const char *fname)
Definition: common.c:85
bool SendQuery(const char *query)
Definition: common.c:1194
static bool show_context_hook(const char *newval)
Definition: startup.c:1162
int main(int argc, char *argv[])
Definition: startup.c:126
static bool hide_tableam_hook(const char *newval)
Definition: startup.c:1190
static bool quiet_hook(const char *newval)
Definition: startup.c:891
static bool singleline_hook(const char *newval)
Definition: startup.c:897
static bool prompt2_hook(const char *newval)
Definition: startup.c:1109
static bool comp_keyword_case_hook(const char *newval)
Definition: startup.c:1052
static char * histsize_substitute_hook(char *newval)
Definition: startup.c:933
static char * ignoreeof_substitute_hook(char *newval)
Definition: startup.c:947
#define SYSPSQLRC
Definition: startup.c:35
static bool histcontrol_hook(const char *newval)
Definition: startup.c:1081
static bool prompt1_hook(const char *newval)
Definition: startup.c:1102
#define PSQLRC
Definition: startup.c:36
static bool verbosity_hook(const char *newval)
Definition: startup.c:1131
struct SimpleActionListCell SimpleActionListCell
static bool fetch_count_hook(const char *newval)
Definition: startup.c:917
static void process_psqlrc(char *argv0)
Definition: startup.c:775
static void EstablishVariableSpace(void)
Definition: startup.c:1196
static bool histsize_hook(const char *newval)
Definition: startup.c:941
static void log_pre_callback(void)
Definition: startup.c:92
struct SimpleActionList SimpleActionList
#define NOPAGER
Definition: startup.c:89
static char * echo_substitute_hook(char *newval)
Definition: startup.c:974
static bool hide_compression_hook(const char *newval)
Definition: startup.c:1183
#define PARAMS_ARRAY_SIZE
static void log_locus_callback(const char **filename, uint64 *lineno)
Definition: startup.c:99
static bool singlestep_hook(const char *newval)
Definition: startup.c:903
static char * histcontrol_substitute_hook(char *newval)
Definition: startup.c:1073
static bool histfile_hook(const char *newval)
Definition: startup.c:923
static bool prompt3_hook(const char *newval)
Definition: startup.c:1116
static bool on_error_rollback_hook(const char *newval)
Definition: startup.c:1023
static bool on_error_stop_hook(const char *newval)
Definition: startup.c:885
static char * fetch_count_substitute_hook(char *newval)
Definition: startup.c:909
static bool autocommit_hook(const char *newval)
Definition: startup.c:879
static bool ignoreeof_hook(const char *newval)
Definition: startup.c:968
PsqlSettings pset
Definition: startup.c:32
static bool echo_hidden_hook(const char *newval)
Definition: startup.c:1002
static void process_psqlrc_file(char *filename)
Definition: startup.c:812
static char * verbosity_substitute_hook(char *newval)
Definition: startup.c:1123
static bool echo_hook(const char *newval)
Definition: startup.c:982
static void simple_action_list_append(SimpleActionList *list, enum _actions action, const char *val)
Definition: startup.c:749
static char * bool_substitute_hook(char *newval)
Definition: startup.c:862
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition: startup.c:481
_actions
Definition: startup.c:47
@ ACT_SINGLE_SLASH
Definition: startup.c:49
@ ACT_FILE
Definition: startup.c:50
@ ACT_SINGLE_QUERY
Definition: startup.c:48
static void showVersion(void)
Definition: startup.c:843
static char * show_context_substitute_hook(char *newval)
Definition: startup.c:1154
static char * comp_keyword_case_substitute_hook(char *newval)
Definition: startup.c:1044
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define SIGNAL_ARGS
Definition: c.h:1342
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1212
#define CppAsString2(x)
Definition: c.h:289
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:4198
backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:211
void SyncVariables(void)
Definition: command.c:3708
void connection_warnings(bool in_startup)
Definition: command.c:3585
@ PSQL_CMD_ERROR
Definition: command.h:22
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:127
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:433
ConditionalStack conditional_stack_create(void)
Definition: conditional.c:18
void conditional_stack_destroy(ConditionalStack cstack)
Definition: conditional.c:30
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:1025
#define _(x)
Definition: elog.c:89
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:657
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6821
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6770
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6717
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4257
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:6909
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6624
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:6897
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6938
void PQclear(PGresult *res)
Definition: fe-exec.c:694
int PQenv2encoding(void)
Definition: fe-misc.c:1221
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3632
void setDecimalLocale(void)
Definition: print.c:3582
@ UNICODE_LINESTYLE_SINGLE
Definition: print.h:89
@ 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:57
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
#define optional_argument
Definition: getopt_long.h:26
char my_exec_path[MAXPGPATH]
Definition: globals.c:75
#define newval
#define free(a)
Definition: header.h:65
void helpVariables(unsigned short int pager)
Definition: help.c:340
void slashUsage(unsigned short int pager)
Definition: help.c:157
long val
Definition: informix.c:664
static struct @142 value
static bool success
Definition: initdb.c:166
void initializeInput(int flags)
Definition: input.c:346
@ CONNECTION_BAD
Definition: libpq-fe.h:59
@ PQSHOW_CONTEXT_NEVER
Definition: libpq-fe.h:133
@ PQSHOW_CONTEXT_ALWAYS
Definition: libpq-fe.h:135
@ PQSHOW_CONTEXT_ERRORS
Definition: libpq-fe.h:134
@ PQERRORS_VERBOSE
Definition: libpq-fe.h:127
@ PQERRORS_DEFAULT
Definition: libpq-fe.h:126
@ PQERRORS_TERSE
Definition: libpq-fe.h:125
@ PQERRORS_SQLSTATE
Definition: libpq-fe.h:128
static void const char fflush(stdout)
Assert(fmt[strlen(fmt) - 1] !='\n')
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:81
void pg_logging_set_locus_callback(void(*cb)(const char **filename, uint64 *lineno))
Definition: logging.c:191
void pg_logging_config(int new_flags)
Definition: logging.c:155
void pg_logging_set_pre_callback(void(*cb)(void))
Definition: logging.c:185
#define pg_log_error(...)
Definition: logging.h:80
#define PG_LOG_FLAG_TERSE
Definition: logging.h:64
#define pg_log_fatal(...)
Definition: logging.h:76
const PsqlScanCallbacks psqlscan_callbacks
Definition: mainloop.c:20
int MainLoop(FILE *source)
Definition: mainloop.c:33
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define MAXPGPATH
static char * argv0
Definition: pg_ctl.c:97
static char * filename
Definition: pg_dumpall.c:92
int optind
Definition: getopt.c:50
char * optarg
Definition: getopt.c:52
static void process_file(const char *filename, int weight)
Definition: pgbench.c:5279
#define pg_log_warning(...)
Definition: pgfnames.c:24
bool get_home_path(char *ret_path)
Definition: path.c:807
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * get_progname(const char *argv0)
Definition: path.c:453
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition: path.c:713
#define snprintf
Definition: port.h:222
#define fprintf
Definition: port.h:226
#define printf(...)
Definition: port.h:228
char * c
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void psql_scan_destroy(PsqlScanState state)
PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks)
void psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
void psql_scan_setup(PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
#define DEFAULT_PROMPT1
Definition: settings.h:26
#define DEFAULT_PROMPT3
Definition: settings.h:28
@ PSQL_ERROR_ROLLBACK_INTERACTIVE
Definition: settings.h:53
@ PSQL_ERROR_ROLLBACK_ON
Definition: settings.h:54
@ PSQL_ERROR_ROLLBACK_OFF
Definition: settings.h:52
#define DEFAULT_RECORD_SEP
Definition: settings.h:16
@ hctl_ignoredups
Definition: settings.h:69
@ hctl_ignoreboth
Definition: settings.h:70
@ hctl_none
Definition: settings.h:67
@ hctl_ignorespace
Definition: settings.h:68
@ PSQL_ECHO_ALL
Definition: settings.h:40
@ PSQL_ECHO_ERRORS
Definition: settings.h:39
@ PSQL_ECHO_NONE
Definition: settings.h:37
@ PSQL_ECHO_QUERIES
Definition: settings.h:38
#define DEFAULT_FIELD_SEP
Definition: settings.h:15
#define EXIT_SUCCESS
Definition: settings.h:158
#define EXIT_FAILURE
Definition: settings.h:162
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:47
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:45
@ PSQL_ECHO_HIDDEN_ON
Definition: settings.h:46
#define EXIT_BADCONN
Definition: settings.h:165
#define DEFAULT_PROMPT2
Definition: settings.h:27
#define EXIT_USER
Definition: settings.h:167
@ PSQL_COMP_CASE_PRESERVE_LOWER
Definition: settings.h:60
@ PSQL_COMP_CASE_LOWER
Definition: settings.h:62
@ PSQL_COMP_CASE_PRESERVE_UPPER
Definition: settings.h:59
@ PSQL_COMP_CASE_UPPER
Definition: settings.h:61
#define DEFAULT_CSV_FIELD_SEP
Definition: settings.h:14
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static void error(void)
Definition: sql-dyntest.c:147
static char * password
Definition: streamutil.c:53
struct SimpleActionListCell * next
Definition: startup.c:55
enum _actions action
Definition: startup.c:56
SimpleActionListCell * head
Definition: startup.c:62
SimpleActionListCell * tail
Definition: startup.c:63
uint64 lineno
Definition: settings.h:111
printQueryOpt popt
Definition: settings.h:91
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:144
bool hide_tableam
Definition: settings.h:138
VariableSpace vars
Definition: settings.h:118
PSQL_COMP_CASE comp_case
Definition: settings.h:145
int encoding
Definition: settings.h:83
char * inputfile
Definition: settings.h:110
PGVerbosity verbosity
Definition: settings.h:150
bool hide_compression
Definition: settings.h:137
FILE * logfile
Definition: settings.h:116
bool on_error_stop
Definition: settings.h:133
PGconn * dead_conn
Definition: settings.h:125
bool autocommit
Definition: settings.h:132
PGresult * last_error_result
Definition: settings.h:89
const char * prompt2
Definition: settings.h:148
const char * prompt3
Definition: settings.h:149
FILE * copyStream
Definition: settings.h:87
PGconn * db
Definition: settings.h:82
FILE * queryFout
Definition: settings.h:84
bool queryFoutPipe
Definition: settings.h:85
PSQL_ECHO echo
Definition: settings.h:142
bool singlestep
Definition: settings.h:136
enum trivalue getPassword
Definition: settings.h:104
bool singleline
Definition: settings.h:135
PGContextVisibility show_context
Definition: settings.h:151
const char * prompt1
Definition: settings.h:147
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:143
int fetch_count
Definition: settings.h:139
bool cur_cmd_interactive
Definition: settings.h:107
FILE * cur_cmd_source
Definition: settings.h:105
HistControl histcontrol
Definition: settings.h:146
const char * progname
Definition: settings.h:109
char * username
Definition: startup.c:71
bool no_readline
Definition: startup.c:73
char * port
Definition: startup.c:70
bool list_dbs
Definition: startup.c:76
bool single_txn
Definition: startup.c:75
bool no_psqlrc
Definition: startup.c:74
char * logfilename
Definition: startup.c:72
SimpleActionList actions
Definition: startup.c:77
char * dbname
Definition: startup.c:68
char * host
Definition: startup.c:69
char * username
Definition: oid2name.c:45
char * dbname
Definition: oid2name.c:42
char * port
Definition: oid2name.c:44
printTableOpt topt
Definition: print.h:169
bool start_table
Definition: print.h:111
unsigned short int expanded
Definition: print.h:102
unicode_linestyle unicode_border_linestyle
Definition: print.h:125
bool tuples_only
Definition: print.h:110
enum printFormat format
Definition: print.h:101
struct separator fieldSep
Definition: print.h:116
struct separator recordSep
Definition: print.h:117
char csvFieldSep[2]
Definition: print.h:118
bool default_footer
Definition: print.h:113
int pager_min_lines
Definition: print.h:108
unsigned short int pager
Definition: print.h:106
char * tableAttr
Definition: print.h:121
unsigned short int border
Definition: print.h:104
unicode_linestyle unicode_header_linestyle
Definition: print.h:127
int env_columns
Definition: print.h:123
unicode_linestyle unicode_column_linestyle
Definition: print.h:126
bool stop_table
Definition: print.h:112
bool separator_zero
Definition: print.h:96
char * separator
Definition: print.h:95
static void usage(const char *progname)
Definition: vacuumlo.c:417
@ 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:405
void SetVariableHooks(VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
Definition: variables.c:315
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:417
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
bool SetVariableBool(VariableSpace space, const char *name)
Definition: variables.c:393
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
VariableSpace CreateVariableSpace(void)
Definition: variables.c:51
#define SIGCHLD
Definition: win32_port.h:177
#define SIGALRM
Definition: win32_port.h:173