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