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 
335  if (pset.echo == PSQL_ECHO_ALL)
336  puts(cell->val);
337 
338  scan_state = psql_scan_create(&psqlscan_callbacks);
339  psql_scan_setup(scan_state,
340  cell->val, strlen(cell->val),
341  pset.encoding, standard_strings());
342 
343  successResult = HandleSlashCmds(scan_state, NULL) != PSQL_CMD_ERROR
345 
346  psql_scan_destroy(scan_state);
347  }
348  else if (cell->action == ACT_FILE)
349  {
350  successResult = process_file(cell->val, false);
351  }
352  else
353  {
354  /* should never come here */
355  Assert(false);
356  }
357 
358  if (successResult != EXIT_SUCCESS && pset.on_error_stop)
359  break;
360  }
361 
362  if (options.single_txn)
363  {
364  if ((res = PSQLexec("COMMIT")) == NULL)
365  {
366  if (pset.on_error_stop)
367  {
368  successResult = EXIT_USER;
369  goto error;
370  }
371  }
372  else
373  PQclear(res);
374  }
375 
376 error:
377  ;
378  }
379 
380  /*
381  * or otherwise enter interactive main loop
382  */
383  else
384  {
385  connection_warnings(true);
386  if (!pset.quiet)
387  printf(_("Type \"help\" for help.\n\n"));
388  initializeInput(options.no_readline ? 0 : 1);
389  successResult = MainLoop(stdin);
390  }
391 
392  /* clean up */
393  if (pset.logfile)
394  fclose(pset.logfile);
395  PQfinish(pset.db);
396  setQFout(NULL);
397 
398  return successResult;
399 }
400 
401 
402 /*
403  * Parse command line options
404  */
405 
406 static void
407 parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
408 {
409  static struct option long_options[] =
410  {
411  {"echo-all", no_argument, NULL, 'a'},
412  {"no-align", no_argument, NULL, 'A'},
413  {"command", required_argument, NULL, 'c'},
414  {"dbname", required_argument, NULL, 'd'},
415  {"echo-queries", no_argument, NULL, 'e'},
416  {"echo-errors", no_argument, NULL, 'b'},
417  {"echo-hidden", no_argument, NULL, 'E'},
418  {"file", required_argument, NULL, 'f'},
419  {"field-separator", required_argument, NULL, 'F'},
420  {"field-separator-zero", no_argument, NULL, 'z'},
421  {"host", required_argument, NULL, 'h'},
422  {"html", no_argument, NULL, 'H'},
423  {"list", no_argument, NULL, 'l'},
424  {"log-file", required_argument, NULL, 'L'},
425  {"no-readline", no_argument, NULL, 'n'},
426  {"single-transaction", no_argument, NULL, '1'},
427  {"output", required_argument, NULL, 'o'},
428  {"port", required_argument, NULL, 'p'},
429  {"pset", required_argument, NULL, 'P'},
430  {"quiet", no_argument, NULL, 'q'},
431  {"record-separator", required_argument, NULL, 'R'},
432  {"record-separator-zero", no_argument, NULL, '0'},
433  {"single-step", no_argument, NULL, 's'},
434  {"single-line", no_argument, NULL, 'S'},
435  {"tuples-only", no_argument, NULL, 't'},
436  {"table-attr", required_argument, NULL, 'T'},
437  {"username", required_argument, NULL, 'U'},
438  {"set", required_argument, NULL, 'v'},
439  {"variable", required_argument, NULL, 'v'},
440  {"version", no_argument, NULL, 'V'},
441  {"no-password", no_argument, NULL, 'w'},
442  {"password", no_argument, NULL, 'W'},
443  {"expanded", no_argument, NULL, 'x'},
444  {"no-psqlrc", no_argument, NULL, 'X'},
445  {"help", optional_argument, NULL, 1},
446  {NULL, 0, NULL, 0}
447  };
448 
449  int optindex;
450  int c;
451 
452  memset(options, 0, sizeof *options);
453 
454  while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
455  long_options, &optindex)) != -1)
456  {
457  switch (c)
458  {
459  case 'a':
460  SetVariable(pset.vars, "ECHO", "all");
461  break;
462  case 'A':
464  break;
465  case 'b':
466  SetVariable(pset.vars, "ECHO", "errors");
467  break;
468  case 'c':
469  if (optarg[0] == '\\')
472  optarg + 1);
473  else
476  optarg);
477  break;
478  case 'd':
479  options->dbname = pg_strdup(optarg);
480  break;
481  case 'e':
482  SetVariable(pset.vars, "ECHO", "queries");
483  break;
484  case 'E':
485  SetVariableBool(pset.vars, "ECHO_HIDDEN");
486  break;
487  case 'f':
489  ACT_FILE,
490  optarg);
491  break;
492  case 'F':
494  pset.popt.topt.fieldSep.separator_zero = false;
495  break;
496  case 'h':
497  options->host = pg_strdup(optarg);
498  break;
499  case 'H':
500  pset.popt.topt.format = PRINT_HTML;
501  break;
502  case 'l':
503  options->list_dbs = true;
504  break;
505  case 'L':
506  options->logfilename = pg_strdup(optarg);
507  break;
508  case 'n':
509  options->no_readline = true;
510  break;
511  case 'o':
512  if (!setQFout(optarg))
513  exit(EXIT_FAILURE);
514  break;
515  case 'p':
516  options->port = pg_strdup(optarg);
517  break;
518  case 'P':
519  {
520  char *value;
521  char *equal_loc;
522  bool result;
523 
524  value = pg_strdup(optarg);
525  equal_loc = strchr(value, '=');
526  if (!equal_loc)
527  result = do_pset(value, NULL, &pset.popt, true);
528  else
529  {
530  *equal_loc = '\0';
531  result = do_pset(value, equal_loc + 1, &pset.popt, true);
532  }
533 
534  if (!result)
535  {
536  fprintf(stderr, _("%s: could not set printing parameter \"%s\"\n"), pset.progname, value);
537  exit(EXIT_FAILURE);
538  }
539 
540  free(value);
541  break;
542  }
543  case 'q':
544  SetVariableBool(pset.vars, "QUIET");
545  break;
546  case 'R':
548  pset.popt.topt.recordSep.separator_zero = false;
549  break;
550  case 's':
551  SetVariableBool(pset.vars, "SINGLESTEP");
552  break;
553  case 'S':
554  SetVariableBool(pset.vars, "SINGLELINE");
555  break;
556  case 't':
557  pset.popt.topt.tuples_only = true;
558  break;
559  case 'T':
561  break;
562  case 'U':
563  options->username = pg_strdup(optarg);
564  break;
565  case 'v':
566  {
567  char *value;
568  char *equal_loc;
569 
570  value = pg_strdup(optarg);
571  equal_loc = strchr(value, '=');
572  if (!equal_loc)
573  {
574  if (!DeleteVariable(pset.vars, value))
575  exit(EXIT_FAILURE); /* error already printed */
576  }
577  else
578  {
579  *equal_loc = '\0';
580  if (!SetVariable(pset.vars, value, equal_loc + 1))
581  exit(EXIT_FAILURE); /* error already printed */
582  }
583 
584  free(value);
585  break;
586  }
587  case 'V':
588  showVersion();
589  exit(EXIT_SUCCESS);
590  case 'w':
591  pset.getPassword = TRI_NO;
592  break;
593  case 'W':
594  pset.getPassword = TRI_YES;
595  break;
596  case 'x':
597  pset.popt.topt.expanded = true;
598  break;
599  case 'X':
600  options->no_psqlrc = true;
601  break;
602  case 'z':
603  pset.popt.topt.fieldSep.separator_zero = true;
604  break;
605  case '0':
606  pset.popt.topt.recordSep.separator_zero = true;
607  break;
608  case '1':
609  options->single_txn = true;
610  break;
611  case '?':
612  /* Actual help option given */
613  if (strcmp(argv[optind - 1], "-?") == 0)
614  {
615  usage(NOPAGER);
616  exit(EXIT_SUCCESS);
617  }
618  /* unknown option reported by getopt */
619  else
620  goto unknown_option;
621  break;
622  case 1:
623  {
624  if (!optarg || strcmp(optarg, "options") == 0)
625  usage(NOPAGER);
626  else if (optarg && strcmp(optarg, "commands") == 0)
628  else if (optarg && strcmp(optarg, "variables") == 0)
630  else
631  goto unknown_option;
632 
633  exit(EXIT_SUCCESS);
634  }
635  break;
636  default:
637  unknown_option:
638  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
639  pset.progname);
640  exit(EXIT_FAILURE);
641  break;
642  }
643  }
644 
645  /*
646  * if we still have arguments, use it as the database name and username
647  */
648  while (argc - optind >= 1)
649  {
650  if (!options->dbname)
651  options->dbname = argv[optind];
652  else if (!options->username)
653  options->username = argv[optind];
654  else if (!pset.quiet)
655  fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"),
656  pset.progname, argv[optind]);
657 
658  optind++;
659  }
660 }
661 
662 
663 /*
664  * Append a new item to the end of the SimpleActionList.
665  * Note that "val" is copied if it's not NULL.
666  */
667 static void
669  enum _actions action, const char *val)
670 {
671  SimpleActionListCell *cell;
672 
674 
675  cell->next = NULL;
676  cell->action = action;
677  if (val)
678  cell->val = pg_strdup(val);
679  else
680  cell->val = NULL;
681 
682  if (list->tail)
683  list->tail->next = cell;
684  else
685  list->head = cell;
686  list->tail = cell;
687 }
688 
689 
690 /*
691  * Load .psqlrc file, if found.
692  */
693 static void
695 {
696  char home[MAXPGPATH];
697  char rc_file[MAXPGPATH];
698  char my_exec_path[MAXPGPATH];
699  char etc_path[MAXPGPATH];
700  char *envrc = getenv("PSQLRC");
701 
702  if (find_my_exec(argv0, my_exec_path) < 0)
703  {
704  fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
705  exit(EXIT_FAILURE);
706  }
707 
708  get_etc_path(my_exec_path, etc_path);
709 
710  snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
711  process_psqlrc_file(rc_file);
712 
713  if (envrc != NULL && strlen(envrc) > 0)
714  {
715  /* might need to free() this */
716  char *envrc_alloc = pstrdup(envrc);
717 
718  expand_tilde(&envrc_alloc);
719  process_psqlrc_file(envrc_alloc);
720  }
721  else if (get_home_path(home))
722  {
723  snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
724  process_psqlrc_file(rc_file);
725  }
726 }
727 
728 
729 
730 static void
732 {
733  char *psqlrc_minor,
734  *psqlrc_major;
735 
736 #if defined(WIN32) && (!defined(__MINGW32__))
737 #define R_OK 4
738 #endif
739 
740  psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
741  psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
742 
743  /* check for minor version first, then major, then no version */
744  if (access(psqlrc_minor, R_OK) == 0)
745  (void) process_file(psqlrc_minor, false);
746  else if (access(psqlrc_major, R_OK) == 0)
747  (void) process_file(psqlrc_major, false);
748  else if (access(filename, R_OK) == 0)
749  (void) process_file(filename, false);
750 
751  free(psqlrc_minor);
752  free(psqlrc_major);
753 }
754 
755 
756 
757 /* showVersion
758  *
759  * This output format is intended to match GNU standards.
760  */
761 static void
763 {
764  puts("psql (PostgreSQL) " PG_VERSION);
765 }
766 
767 
768 
769 /*
770  * Substitute hooks and assign hooks for psql variables.
771  *
772  * This isn't an amazingly good place for them, but neither is anywhere else.
773  *
774  * By policy, every special variable that controls any psql behavior should
775  * have one or both hooks, even if they're just no-ops. This ensures that
776  * the variable will remain present in variables.c's list even when unset,
777  * which ensures that it's known to tab completion.
778  */
779 
780 static char *
782 {
783  if (newval == NULL)
784  {
785  /* "\unset FOO" becomes "\set FOO off" */
786  newval = pg_strdup("off");
787  }
788  else if (newval[0] == '\0')
789  {
790  /* "\set FOO" becomes "\set FOO on" */
791  pg_free(newval);
792  newval = pg_strdup("on");
793  }
794  return newval;
795 }
796 
797 static bool
799 {
800  return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
801 }
802 
803 static bool
805 {
806  return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
807 }
808 
809 static bool
810 quiet_hook(const char *newval)
811 {
812  return ParseVariableBool(newval, "QUIET", &pset.quiet);
813 }
814 
815 static bool
817 {
818  return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
819 }
820 
821 static bool
823 {
824  return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
825 }
826 
827 static char *
829 {
830  if (newval == NULL)
831  newval = pg_strdup("0");
832  return newval;
833 }
834 
835 static bool
837 {
838  return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
839 }
840 
841 static bool
842 histfile_hook(const char *newval)
843 {
844  /*
845  * Someday we might try to validate the filename, but for now, this is
846  * just a placeholder to ensure HISTFILE is known to tab completion.
847  */
848  return true;
849 }
850 
851 static char *
853 {
854  if (newval == NULL)
855  newval = pg_strdup("500");
856  return newval;
857 }
858 
859 static bool
860 histsize_hook(const char *newval)
861 {
862  return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
863 }
864 
865 static char *
867 {
868  int dummy;
869 
870  /*
871  * This tries to mimic the behavior of bash, to wit "If set, the value is
872  * the number of consecutive EOF characters which must be typed as the
873  * first characters on an input line before bash exits. If the variable
874  * exists but does not have a numeric value, or has no value, the default
875  * value is 10. If it does not exist, EOF signifies the end of input to
876  * the shell." Unlike bash, however, we insist on the stored value
877  * actually being a valid integer.
878  */
879  if (newval == NULL)
880  newval = pg_strdup("0");
881  else if (!ParseVariableNum(newval, NULL, &dummy))
882  newval = pg_strdup("10");
883  return newval;
884 }
885 
886 static bool
887 ignoreeof_hook(const char *newval)
888 {
889  return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
890 }
891 
892 static char *
894 {
895  if (newval == NULL)
896  newval = pg_strdup("none");
897  return newval;
898 }
899 
900 static bool
901 echo_hook(const char *newval)
902 {
903  Assert(newval != NULL); /* else substitute hook messed up */
904  if (pg_strcasecmp(newval, "queries") == 0)
905  pset.echo = PSQL_ECHO_QUERIES;
906  else if (pg_strcasecmp(newval, "errors") == 0)
907  pset.echo = PSQL_ECHO_ERRORS;
908  else if (pg_strcasecmp(newval, "all") == 0)
909  pset.echo = PSQL_ECHO_ALL;
910  else if (pg_strcasecmp(newval, "none") == 0)
911  pset.echo = PSQL_ECHO_NONE;
912  else
913  {
914  PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
915  return false;
916  }
917  return true;
918 }
919 
920 static bool
922 {
923  Assert(newval != NULL); /* else substitute hook messed up */
924  if (pg_strcasecmp(newval, "noexec") == 0)
926  else
927  {
928  bool on_off;
929 
930  if (ParseVariableBool(newval, NULL, &on_off))
932  else
933  {
934  PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
935  return false;
936  }
937  }
938  return true;
939 }
940 
941 static bool
943 {
944  Assert(newval != NULL); /* else substitute hook messed up */
945  if (pg_strcasecmp(newval, "interactive") == 0)
947  else
948  {
949  bool on_off;
950 
951  if (ParseVariableBool(newval, NULL, &on_off))
953  else
954  {
955  PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
956  return false;
957  }
958  }
959  return true;
960 }
961 
962 static char *
964 {
965  if (newval == NULL)
966  newval = pg_strdup("preserve-upper");
967  return newval;
968 }
969 
970 static bool
972 {
973  Assert(newval != NULL); /* else substitute hook messed up */
974  if (pg_strcasecmp(newval, "preserve-upper") == 0)
976  else if (pg_strcasecmp(newval, "preserve-lower") == 0)
978  else if (pg_strcasecmp(newval, "upper") == 0)
980  else if (pg_strcasecmp(newval, "lower") == 0)
982  else
983  {
984  PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
985  "lower, upper, preserve-lower, preserve-upper");
986  return false;
987  }
988  return true;
989 }
990 
991 static char *
993 {
994  if (newval == NULL)
995  newval = pg_strdup("none");
996  return newval;
997 }
998 
999 static bool
1001 {
1002  Assert(newval != NULL); /* else substitute hook messed up */
1003  if (pg_strcasecmp(newval, "ignorespace") == 0)
1005  else if (pg_strcasecmp(newval, "ignoredups") == 0)
1007  else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1009  else if (pg_strcasecmp(newval, "none") == 0)
1010  pset.histcontrol = hctl_none;
1011  else
1012  {
1013  PsqlVarEnumError("HISTCONTROL", newval,
1014  "none, ignorespace, ignoredups, ignoreboth");
1015  return false;
1016  }
1017  return true;
1018 }
1019 
1020 static bool
1021 prompt1_hook(const char *newval)
1022 {
1023  pset.prompt1 = newval ? newval : "";
1024  return true;
1025 }
1026 
1027 static bool
1028 prompt2_hook(const char *newval)
1029 {
1030  pset.prompt2 = newval ? newval : "";
1031  return true;
1032 }
1033 
1034 static bool
1035 prompt3_hook(const char *newval)
1036 {
1037  pset.prompt3 = newval ? newval : "";
1038  return true;
1039 }
1040 
1041 static char *
1043 {
1044  if (newval == NULL)
1045  newval = pg_strdup("default");
1046  return newval;
1047 }
1048 
1049 static bool
1051 {
1052  Assert(newval != NULL); /* else substitute hook messed up */
1053  if (pg_strcasecmp(newval, "default") == 0)
1054  pset.verbosity = PQERRORS_DEFAULT;
1055  else if (pg_strcasecmp(newval, "terse") == 0)
1056  pset.verbosity = PQERRORS_TERSE;
1057  else if (pg_strcasecmp(newval, "verbose") == 0)
1058  pset.verbosity = PQERRORS_VERBOSE;
1059  else
1060  {
1061  PsqlVarEnumError("VERBOSITY", newval, "default, terse, verbose");
1062  return false;
1063  }
1064 
1065  if (pset.db)
1066  PQsetErrorVerbosity(pset.db, pset.verbosity);
1067  return true;
1068 }
1069 
1070 static char *
1072 {
1073  if (newval == NULL)
1074  newval = pg_strdup("errors");
1075  return newval;
1076 }
1077 
1078 static bool
1080 {
1081  Assert(newval != NULL); /* else substitute hook messed up */
1082  if (pg_strcasecmp(newval, "never") == 0)
1084  else if (pg_strcasecmp(newval, "errors") == 0)
1086  else if (pg_strcasecmp(newval, "always") == 0)
1088  else
1089  {
1090  PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1091  return false;
1092  }
1093 
1094  if (pset.db)
1096  return true;
1097 }
1098 
1099 
1100 static void
1102 {
1103  pset.vars = CreateVariableSpace();
1104 
1105  SetVariableHooks(pset.vars, "AUTOCOMMIT",
1107  autocommit_hook);
1108  SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1111  SetVariableHooks(pset.vars, "QUIET",
1113  quiet_hook);
1114  SetVariableHooks(pset.vars, "SINGLELINE",
1116  singleline_hook);
1117  SetVariableHooks(pset.vars, "SINGLESTEP",
1119  singlestep_hook);
1120  SetVariableHooks(pset.vars, "FETCH_COUNT",
1123  SetVariableHooks(pset.vars, "HISTFILE",
1124  NULL,
1125  histfile_hook);
1126  SetVariableHooks(pset.vars, "HISTSIZE",
1128  histsize_hook);
1129  SetVariableHooks(pset.vars, "IGNOREEOF",
1131  ignoreeof_hook);
1132  SetVariableHooks(pset.vars, "ECHO",
1134  echo_hook);
1135  SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1138  SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1141  SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1144  SetVariableHooks(pset.vars, "HISTCONTROL",
1147  SetVariableHooks(pset.vars, "PROMPT1",
1148  NULL,
1149  prompt1_hook);
1150  SetVariableHooks(pset.vars, "PROMPT2",
1151  NULL,
1152  prompt2_hook);
1153  SetVariableHooks(pset.vars, "PROMPT3",
1154  NULL,
1155  prompt3_hook);
1156  SetVariableHooks(pset.vars, "VERBOSITY",
1158  verbosity_hook);
1159  SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1162 }
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition: startup.c:407
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:694
static void usage(void)
Definition: pg_standby.c:503
static char password[100]
Definition: streamutil.c:41
SimpleActionList actions
Definition: startup.c:78
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
static char * argv0
Definition: pg_ctl.c:89
static char * verbosity_substitute_hook(char *newval)
Definition: startup.c:1042
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:6187
void SyncVariables(void)
Definition: command.c:2155
static void error(void)
Definition: sql-dyntest.c:147
static void EstablishVariableSpace(void)
Definition: startup.c:1101
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:804
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:901
#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:3568
static bool quiet_hook(const char *newval)
Definition: startup.c:810
bool SetVariableBool(VariableSpace space, const char *name)
Definition: variables.c:367
return result
Definition: formatting.c:1618
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:668
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:762
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:470
static bool histcontrol_hook(const char *newval)
Definition: startup.c:1000
#define EXIT_BADCONN
Definition: settings.h:154
static char * bool_substitute_hook(char *newval)
Definition: startup.c:781
#define PSQLRC
Definition: startup.c:37
static struct @114 value
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:1028
#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:2064
#define MAXPGPATH
unicode_linestyle unicode_column_linestyle
Definition: print.h:122
bool DeleteVariable(VariableSpace space, const char *name)
Definition: variables.c:379
static bool singleline_hook(const char *newval)
Definition: startup.c:816
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:893
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:798
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:2562
static char * comp_keyword_case_substitute_hook(char *newval)
Definition: startup.c:963
static bool fetch_count_hook(const char *newval)
Definition: startup.c:836
char * tableAttr
Definition: print.h:117
static bool singlestep_hook(const char *newval)
Definition: startup.c:822
static bool show_context_hook(const char *newval)
Definition: startup.c:1079
_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:942
static char * show_context_substitute_hook(char *newval)
Definition: startup.c:1071
static bool histsize_hook(const char *newval)
Definition: startup.c:860
int PQenv2encoding(void)
Definition: fe-misc.c:1206
#define no_argument
Definition: getopt_long.h:24
char my_exec_path[MAXPGPATH]
Definition: globals.c:63
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1012
PGContextVisibility show_context
Definition: settings.h:140
void helpVariables(unsigned short int pager)
Definition: help.c:320
static void process_psqlrc_file(char *filename)
Definition: startup.c:731
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:6137
static char * histsize_substitute_hook(char *newval)
Definition: startup.c:852
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:2019
#define free(a)
Definition: header.h:65
struct separator fieldSep
Definition: print.h:113
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:202
struct SimpleActionListCell SimpleActionListCell
static bool have_password
Definition: streamutil.c:40
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:971
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:992
#define optional_argument
Definition: getopt_long.h:26
static bool ignoreeof_hook(const char *newval)
Definition: startup.c:887
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:866
backslashResult HandleSlashCmds(PsqlScanState scan_state, PQExpBuffer query_buf)
Definition: command.c:108
static bool prompt1_hook(const char *newval)
Definition: startup.c:1021
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static Datum values[MAXATTR]
Definition: bootstrap.c:162
static char * filename
Definition: pg_dumpall.c:87
tuple list
Definition: sort-test.py:11
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6045
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * host
Definition: startup.c:70
char * optarg
Definition: getopt.c:53
static bool histfile_hook(const char *newval)
Definition: startup.c:842
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:1191
void connection_warnings(bool in_startup)
Definition: command.c:2048
static bool verbosity_hook(const char *newval)
Definition: startup.c:1050
static char * fetch_count_substitute_hook(char *newval)
Definition: startup.c:828
static bool prompt3_hook(const char *newval)
Definition: startup.c:1035
struct separator recordSep
Definition: print.h:114
int encoding
Definition: settings.h:83
static bool echo_hidden_hook(const char *newval)
Definition: startup.c:921
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5958
#define DEFAULT_FIELD_SEP
Definition: settings.h:15
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:6125
#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:779
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:603
VariableSpace vars
Definition: settings.h:116