2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2014, PostgreSQL Global Development Group
6 * src/bin/psql/startup.c
8 #include "postgres_fe.h"
10 #include <sys/types.h>
19 #include "getopt_long.h"
39 #define SYSPSQLRC "psqlrc"
40 #define PSQLRC ".psqlrc"
42 #define SYSPSQLRC "psqlrc"
43 #define PSQLRC "psqlrc.conf"
47 * Structures to pass information between the option parsing routine
48 * and the main function
73 static void parse_psql_options(int argc, char *argv[],
74 struct adhoc_opts * options);
75 static void process_psqlrc(char *argv0);
76 static void process_psqlrc_file(char *filename);
77 static void showVersion(void);
78 static void EstablishVariableSpace(void);
86 main(int argc, char *argv[])
88 struct adhoc_opts options;
90 char *password = NULL;
91 char *password_prompt = NULL;
94 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
98 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
103 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
111 setvbuf(stderr, NULL, _IONBF, 0);
114 pset.progname = get_progname(argv[0]);
118 pset.encoding = PQenv2encoding();
119 pset.queryFout = stdout;
120 pset.queryFoutPipe = false;
121 pset.copyStream = NULL;
122 pset.cur_cmd_source = stdin;
123 pset.cur_cmd_interactive = false;
125 /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
126 pset.popt.topt.format = PRINT_ALIGNED;
127 pset.popt.topt.border = 1;
128 pset.popt.topt.pager = 1;
129 pset.popt.topt.start_table = true;
130 pset.popt.topt.stop_table = true;
131 pset.popt.topt.default_footer = true;
132 /* We must get COLUMNS here before readline() sets it */
133 pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
135 pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
137 pset.getPassword = TRI_DEFAULT;
139 EstablishVariableSpace();
141 SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
143 /* Default values for variables */
144 SetVariableBool(pset.vars, "AUTOCOMMIT");
145 SetVariable(pset.vars, "VERBOSITY", "default");
146 SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
147 SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
148 SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
150 parse_psql_options(argc, argv, &options);
153 * If no action was specified and we're in non-interactive mode, treat it
154 * as if the user had specified "-f -". This lets single-transaction mode
157 if (options.action == ACT_NOTHING && pset.notty)
159 options.action = ACT_FILE;
160 options.action_string = NULL;
163 /* Bail out if -1 was specified but will be ignored. */
164 if (options.single_txn && options.action != ACT_FILE && options.action == ACT_NOTHING)
166 fprintf(stderr, _("%s: -1 can only be used in non-interactive mode\n"), pset.progname);
170 if (!pset.popt.topt.fieldSep.separator &&
171 !pset.popt.topt.fieldSep.separator_zero)
173 pset.popt.topt.fieldSep.separator = pg_strdup(DEFAULT_FIELD_SEP);
174 pset.popt.topt.fieldSep.separator_zero = false;
176 if (!pset.popt.topt.recordSep.separator &&
177 !pset.popt.topt.recordSep.separator_zero)
179 pset.popt.topt.recordSep.separator = pg_strdup(DEFAULT_RECORD_SEP);
180 pset.popt.topt.recordSep.separator_zero = false;
183 if (options.username == NULL)
184 password_prompt = pg_strdup(_("Password: "));
186 password_prompt = psprintf(_("Password for user %s: "),
189 if (pset.getPassword == TRI_YES)
190 password = simple_prompt(password_prompt, 100, false);
192 /* loop until we have a password if requested by backend */
195 #define PARAMS_ARRAY_SIZE 8
196 const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
197 const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
199 keywords[0] = "host";
200 values[0] = options.host;
201 keywords[1] = "port";
202 values[1] = options.port;
203 keywords[2] = "user";
204 values[2] = options.username;
205 keywords[3] = "password";
206 values[3] = password;
207 keywords[4] = "dbname";
208 values[4] = (options.action == ACT_LIST_DB &&
209 options.dbname == NULL) ?
210 "postgres" : options.dbname;
211 keywords[5] = "fallback_application_name";
212 values[5] = pset.progname;
213 keywords[6] = "client_encoding";
214 values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
219 pset.db = PQconnectdbParams(keywords, values, true);
223 if (PQstatus(pset.db) == CONNECTION_BAD &&
224 PQconnectionNeedsPassword(pset.db) &&
226 pset.getPassword != TRI_NO)
229 password = simple_prompt(password_prompt, 100, false);
235 free(password_prompt);
237 if (PQstatus(pset.db) == CONNECTION_BAD)
239 fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
244 setup_cancel_handler();
246 PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
250 if (options.action == ACT_LIST_DB)
254 if (!options.no_psqlrc)
255 process_psqlrc(argv[0]);
257 success = listAllDbs(NULL, false);
259 exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
262 if (options.logfilename)
264 pset.logfile = fopen(options.logfilename, "a");
266 fprintf(stderr, _("%s: could not open log file \"%s\": %s\n"),
267 pset.progname, options.logfilename, strerror(errno));
271 * Now find something to do
275 * process file given by -f
277 if (options.action == ACT_FILE)
279 if (!options.no_psqlrc)
280 process_psqlrc(argv[0]);
282 successResult = process_file(options.action_string, options.single_txn, false);
286 * process slash command if one was given to -c
288 else if (options.action == ACT_SINGLE_SLASH)
290 PsqlScanState scan_state;
292 if (pset.echo == PSQL_ECHO_ALL)
293 puts(options.action_string);
295 scan_state = psql_scan_create();
296 psql_scan_setup(scan_state,
297 options.action_string,
298 strlen(options.action_string));
300 successResult = HandleSlashCmds(scan_state, NULL) != PSQL_CMD_ERROR
301 ? EXIT_SUCCESS : EXIT_FAILURE;
303 psql_scan_destroy(scan_state);
307 * If the query given to -c was a normal one, send it
309 else if (options.action == ACT_SINGLE_QUERY)
311 if (pset.echo == PSQL_ECHO_ALL)
312 puts(options.action_string);
314 successResult = SendQuery(options.action_string)
315 ? EXIT_SUCCESS : EXIT_FAILURE;
319 * or otherwise enter interactive main loop
323 if (!options.no_psqlrc)
324 process_psqlrc(argv[0]);
326 connection_warnings(true);
328 printf(_("Type \"help\" for help.\n\n"));
329 initializeInput(options.no_readline ? 0 : 1);
330 successResult = MainLoop(stdin);
335 fclose(pset.logfile);
339 return successResult;
344 * Parse command line options
348 parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
350 static struct option long_options[] =
352 {"echo-all", no_argument, NULL, 'a'},
353 {"no-align", no_argument, NULL, 'A'},
354 {"command", required_argument, NULL, 'c'},
355 {"dbname", required_argument, NULL, 'd'},
356 {"echo-queries", no_argument, NULL, 'e'},
357 {"echo-errors", no_argument, NULL, 'b'},
358 {"echo-hidden", no_argument, NULL, 'E'},
359 {"file", required_argument, NULL, 'f'},
360 {"field-separator", required_argument, NULL, 'F'},
361 {"field-separator-zero", no_argument, NULL, 'z'},
362 {"host", required_argument, NULL, 'h'},
363 {"html", no_argument, NULL, 'H'},
364 {"list", no_argument, NULL, 'l'},
365 {"log-file", required_argument, NULL, 'L'},
366 {"no-readline", no_argument, NULL, 'n'},
367 {"single-transaction", no_argument, NULL, '1'},
368 {"output", required_argument, NULL, 'o'},
369 {"port", required_argument, NULL, 'p'},
370 {"pset", required_argument, NULL, 'P'},
371 {"quiet", no_argument, NULL, 'q'},
372 {"record-separator", required_argument, NULL, 'R'},
373 {"record-separator-zero", no_argument, NULL, '0'},
374 {"single-step", no_argument, NULL, 's'},
375 {"single-line", no_argument, NULL, 'S'},
376 {"tuples-only", no_argument, NULL, 't'},
377 {"table-attr", required_argument, NULL, 'T'},
378 {"username", required_argument, NULL, 'U'},
379 {"set", required_argument, NULL, 'v'},
380 {"variable", required_argument, NULL, 'v'},
381 {"version", no_argument, NULL, 'V'},
382 {"no-password", no_argument, NULL, 'w'},
383 {"password", no_argument, NULL, 'W'},
384 {"expanded", no_argument, NULL, 'x'},
385 {"no-psqlrc", no_argument, NULL, 'X'},
386 {"help", no_argument, NULL, '?'},
393 memset(options, 0, sizeof *options);
395 while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
396 long_options, &optindex)) != -1)
401 SetVariable(pset.vars, "ECHO", "all");
404 pset.popt.topt.format = PRINT_UNALIGNED;
407 SetVariable(pset.vars, "ECHO", "errors");
410 options->action_string = pg_strdup(optarg);
411 if (optarg[0] == '\\')
413 options->action = ACT_SINGLE_SLASH;
414 options->action_string++;
417 options->action = ACT_SINGLE_QUERY;
420 options->dbname = pg_strdup(optarg);
423 SetVariable(pset.vars, "ECHO", "queries");
426 SetVariableBool(pset.vars, "ECHO_HIDDEN");
429 options->action = ACT_FILE;
430 options->action_string = pg_strdup(optarg);
433 pset.popt.topt.fieldSep.separator = pg_strdup(optarg);
434 pset.popt.topt.fieldSep.separator_zero = false;
437 options->host = pg_strdup(optarg);
440 pset.popt.topt.format = PRINT_HTML;
443 options->action = ACT_LIST_DB;
446 options->logfilename = pg_strdup(optarg);
449 options->no_readline = true;
455 options->port = pg_strdup(optarg);
463 value = pg_strdup(optarg);
464 equal_loc = strchr(value, '=');
466 result = do_pset(value, NULL, &pset.popt, true);
470 result = do_pset(value, equal_loc + 1, &pset.popt, true);
475 fprintf(stderr, _("%s: could not set printing parameter \"%s\"\n"), pset.progname, value);
483 SetVariableBool(pset.vars, "QUIET");
486 pset.popt.topt.recordSep.separator = pg_strdup(optarg);
487 pset.popt.topt.recordSep.separator_zero = false;
490 SetVariableBool(pset.vars, "SINGLESTEP");
493 SetVariableBool(pset.vars, "SINGLELINE");
496 pset.popt.topt.tuples_only = true;
499 pset.popt.topt.tableAttr = pg_strdup(optarg);
502 options->username = pg_strdup(optarg);
509 value = pg_strdup(optarg);
510 equal_loc = strchr(value, '=');
513 if (!DeleteVariable(pset.vars, value))
515 fprintf(stderr, _("%s: could not delete variable \"%s\"\n"),
516 pset.progname, value);
523 if (!SetVariable(pset.vars, value, equal_loc + 1))
525 fprintf(stderr, _("%s: could not set variable \"%s\"\n"),
526 pset.progname, value);
538 pset.getPassword = TRI_NO;
541 pset.getPassword = TRI_YES;
544 pset.popt.topt.expanded = true;
547 options->no_psqlrc = true;
550 pset.popt.topt.fieldSep.separator_zero = true;
553 pset.popt.topt.recordSep.separator_zero = true;
556 options->single_txn = true;
559 /* Actual help option given */
560 if (strcmp(argv[optind - 1], "--help") == 0 || strcmp(argv[optind - 1], "-?") == 0)
565 /* unknown option reported by getopt */
568 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
574 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
582 * if we still have arguments, use it as the database name and username
584 while (argc - optind >= 1)
586 if (!options->dbname)
587 options->dbname = argv[optind];
588 else if (!options->username)
589 options->username = argv[optind];
590 else if (!pset.quiet)
591 fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"),
592 pset.progname, argv[optind]);
600 * Load .psqlrc file, if found.
603 process_psqlrc(char *argv0)
605 char home[MAXPGPATH];
606 char rc_file[MAXPGPATH];
607 char my_exec_path[MAXPGPATH];
608 char etc_path[MAXPGPATH];
609 char *envrc = getenv("PSQLRC");
611 if (find_my_exec(argv0, my_exec_path) < 0)
613 fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
617 get_etc_path(my_exec_path, etc_path);
619 snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
620 process_psqlrc_file(rc_file);
622 if (envrc != NULL && strlen(envrc) > 0)
624 /* might need to free() this */
625 char *envrc_alloc = pstrdup(envrc);
627 expand_tilde(&envrc_alloc);
628 process_psqlrc_file(envrc_alloc);
630 else if (get_home_path(home))
632 snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
633 process_psqlrc_file(rc_file);
640 process_psqlrc_file(char *filename)
645 #if defined(WIN32) && (!defined(__MINGW32__))
649 psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
650 psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
652 /* check for minor version first, then major, then no version */
653 if (access(psqlrc_minor, R_OK) == 0)
654 (void) process_file(psqlrc_minor, false, false);
655 else if (access(psqlrc_major, R_OK) == 0)
656 (void) process_file(psqlrc_major, false, false);
657 else if (access(filename, R_OK) == 0)
658 (void) process_file(filename, false, false);
668 * This output format is intended to match GNU standards.
673 puts("psql (PostgreSQL) " PG_VERSION);
679 * Assign hooks for psql variables.
681 * This isn't an amazingly good place for them, but neither is anywhere else.
685 autocommit_hook(const char *newval)
687 pset.autocommit = ParseVariableBool(newval);
691 on_error_stop_hook(const char *newval)
693 pset.on_error_stop = ParseVariableBool(newval);
697 quiet_hook(const char *newval)
699 pset.quiet = ParseVariableBool(newval);
703 singleline_hook(const char *newval)
705 pset.singleline = ParseVariableBool(newval);
709 singlestep_hook(const char *newval)
711 pset.singlestep = ParseVariableBool(newval);
715 fetch_count_hook(const char *newval)
717 pset.fetch_count = ParseVariableNum(newval, -1, -1, false);
721 echo_hook(const char *newval)
724 pset.echo = PSQL_ECHO_NONE;
725 else if (strcmp(newval, "queries") == 0)
726 pset.echo = PSQL_ECHO_QUERIES;
727 else if (strcmp(newval, "errors") == 0)
728 pset.echo = PSQL_ECHO_ERRORS;
729 else if (strcmp(newval, "all") == 0)
730 pset.echo = PSQL_ECHO_ALL;
732 pset.echo = PSQL_ECHO_NONE;
736 echo_hidden_hook(const char *newval)
739 pset.echo_hidden = PSQL_ECHO_HIDDEN_OFF;
740 else if (strcmp(newval, "noexec") == 0)
741 pset.echo_hidden = PSQL_ECHO_HIDDEN_NOEXEC;
742 else if (pg_strcasecmp(newval, "off") == 0)
743 pset.echo_hidden = PSQL_ECHO_HIDDEN_OFF;
745 pset.echo_hidden = PSQL_ECHO_HIDDEN_ON;
749 on_error_rollback_hook(const char *newval)
752 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_OFF;
753 else if (pg_strcasecmp(newval, "interactive") == 0)
754 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_INTERACTIVE;
755 else if (pg_strcasecmp(newval, "off") == 0)
756 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_OFF;
758 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_ON;
762 histcontrol_hook(const char *newval)
765 pset.histcontrol = hctl_none;
766 else if (strcmp(newval, "ignorespace") == 0)
767 pset.histcontrol = hctl_ignorespace;
768 else if (strcmp(newval, "ignoredups") == 0)
769 pset.histcontrol = hctl_ignoredups;
770 else if (strcmp(newval, "ignoreboth") == 0)
771 pset.histcontrol = hctl_ignoreboth;
773 pset.histcontrol = hctl_none;
777 prompt1_hook(const char *newval)
779 pset.prompt1 = newval ? newval : "";
783 prompt2_hook(const char *newval)
785 pset.prompt2 = newval ? newval : "";
789 prompt3_hook(const char *newval)
791 pset.prompt3 = newval ? newval : "";
795 verbosity_hook(const char *newval)
798 pset.verbosity = PQERRORS_DEFAULT;
799 else if (strcmp(newval, "default") == 0)
800 pset.verbosity = PQERRORS_DEFAULT;
801 else if (strcmp(newval, "terse") == 0)
802 pset.verbosity = PQERRORS_TERSE;
803 else if (strcmp(newval, "verbose") == 0)
804 pset.verbosity = PQERRORS_VERBOSE;
806 pset.verbosity = PQERRORS_DEFAULT;
809 PQsetErrorVerbosity(pset.db, pset.verbosity);
814 EstablishVariableSpace(void)
816 pset.vars = CreateVariableSpace();
818 SetVariableAssignHook(pset.vars, "AUTOCOMMIT", autocommit_hook);
819 SetVariableAssignHook(pset.vars, "ON_ERROR_STOP", on_error_stop_hook);
820 SetVariableAssignHook(pset.vars, "QUIET", quiet_hook);
821 SetVariableAssignHook(pset.vars, "SINGLELINE", singleline_hook);
822 SetVariableAssignHook(pset.vars, "SINGLESTEP", singlestep_hook);
823 SetVariableAssignHook(pset.vars, "FETCH_COUNT", fetch_count_hook);
824 SetVariableAssignHook(pset.vars, "ECHO", echo_hook);
825 SetVariableAssignHook(pset.vars, "ECHO_HIDDEN", echo_hidden_hook);
826 SetVariableAssignHook(pset.vars, "ON_ERROR_ROLLBACK", on_error_rollback_hook);
827 SetVariableAssignHook(pset.vars, "HISTCONTROL", histcontrol_hook);
828 SetVariableAssignHook(pset.vars, "PROMPT1", prompt1_hook);
829 SetVariableAssignHook(pset.vars, "PROMPT2", prompt2_hook);
830 SetVariableAssignHook(pset.vars, "PROMPT3", prompt3_hook);
831 SetVariableAssignHook(pset.vars, "VERBOSITY", verbosity_hook);