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