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