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