2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2011, 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 setup_cancel_handler();
116 pset.progname = get_progname(argv[0]);
120 pset.encoding = PQenv2encoding();
121 pset.queryFout = stdout;
122 pset.queryFoutPipe = false;
123 pset.cur_cmd_source = stdin;
124 pset.cur_cmd_interactive = false;
126 /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
127 pset.popt.topt.format = PRINT_ALIGNED;
128 pset.popt.topt.border = 1;
129 pset.popt.topt.pager = 1;
130 pset.popt.topt.start_table = true;
131 pset.popt.topt.stop_table = true;
132 pset.popt.default_footer = true;
133 /* We must get COLUMNS here before readline() sets it */
134 pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
136 pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
138 pset.getPassword = TRI_DEFAULT;
140 EstablishVariableSpace();
142 SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
144 /* Default values for variables */
145 SetVariableBool(pset.vars, "AUTOCOMMIT");
146 SetVariable(pset.vars, "VERBOSITY", "default");
147 SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
148 SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
149 SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
151 parse_psql_options(argc, argv, &options);
153 if (!pset.popt.topt.fieldSep)
154 pset.popt.topt.fieldSep = pg_strdup(DEFAULT_FIELD_SEP);
155 if (!pset.popt.topt.recordSep)
156 pset.popt.topt.recordSep = pg_strdup(DEFAULT_RECORD_SEP);
158 if (options.username == NULL)
159 password_prompt = pg_strdup(_("Password: "));
162 password_prompt = malloc(strlen(_("Password for user %s: ")) - 2 +
163 strlen(options.username) + 1);
164 sprintf(password_prompt, _("Password for user %s: "),
168 if (pset.getPassword == TRI_YES)
169 password = simple_prompt(password_prompt, 100, false);
171 /* loop until we have a password if requested by backend */
174 #define PARAMS_ARRAY_SIZE 8
175 const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
176 const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
178 keywords[0] = "host";
179 values[0] = options.host;
180 keywords[1] = "port";
181 values[1] = options.port;
182 keywords[2] = "user";
183 values[2] = options.username;
184 keywords[3] = "password";
185 values[3] = password;
186 keywords[4] = "dbname";
187 values[4] = (options.action == ACT_LIST_DB &&
188 options.dbname == NULL) ?
189 "postgres" : options.dbname;
190 keywords[5] = "fallback_application_name";
191 values[5] = pset.progname;
192 keywords[6] = "client_encoding";
193 values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
198 pset.db = PQconnectdbParams(keywords, values, true);
202 if (PQstatus(pset.db) == CONNECTION_BAD &&
203 PQconnectionNeedsPassword(pset.db) &&
205 pset.getPassword != TRI_NO)
208 password = simple_prompt(password_prompt, 100, false);
214 free(password_prompt);
216 if (PQstatus(pset.db) == CONNECTION_BAD)
218 fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
223 PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
227 if (options.action == ACT_LIST_DB)
231 if (!options.no_psqlrc)
232 process_psqlrc(argv[0]);
234 success = listAllDbs(false);
236 exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
239 if (options.logfilename)
241 pset.logfile = fopen(options.logfilename, "a");
243 fprintf(stderr, _("%s: could not open log file \"%s\": %s\n"),
244 pset.progname, options.logfilename, strerror(errno));
248 * Now find something to do
252 * process file given by -f
254 if (options.action == ACT_FILE)
256 if (!options.no_psqlrc)
257 process_psqlrc(argv[0]);
259 successResult = process_file(options.action_string, options.single_txn);
263 * process slash command if one was given to -c
265 else if (options.action == ACT_SINGLE_SLASH)
267 PsqlScanState scan_state;
269 if (pset.echo == PSQL_ECHO_ALL)
270 puts(options.action_string);
272 scan_state = psql_scan_create();
273 psql_scan_setup(scan_state,
274 options.action_string,
275 strlen(options.action_string));
277 successResult = HandleSlashCmds(scan_state, NULL) != PSQL_CMD_ERROR
278 ? EXIT_SUCCESS : EXIT_FAILURE;
280 psql_scan_destroy(scan_state);
284 * If the query given to -c was a normal one, send it
286 else if (options.action == ACT_SINGLE_QUERY)
288 if (pset.echo == PSQL_ECHO_ALL)
289 puts(options.action_string);
291 successResult = SendQuery(options.action_string)
292 ? EXIT_SUCCESS : EXIT_FAILURE;
296 * or otherwise enter interactive main loop
300 if (!options.no_psqlrc)
301 process_psqlrc(argv[0]);
303 connection_warnings(true);
304 if (!pset.quiet && !pset.notty)
305 printf(_("Type \"help\" for help.\n\n"));
307 initializeInput(options.no_readline ? 0 : 1);
308 if (options.action_string) /* -f - was used */
309 pset.inputfile = "<stdin>";
311 successResult = MainLoop(stdin);
316 fclose(pset.logfile);
320 return successResult;
325 * Parse command line options
329 parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
331 static struct option long_options[] =
333 {"echo-all", no_argument, NULL, 'a'},
334 {"no-align", no_argument, NULL, 'A'},
335 {"command", required_argument, NULL, 'c'},
336 {"dbname", required_argument, NULL, 'd'},
337 {"echo-queries", no_argument, NULL, 'e'},
338 {"echo-hidden", no_argument, NULL, 'E'},
339 {"file", required_argument, NULL, 'f'},
340 {"field-separator", required_argument, NULL, 'F'},
341 {"host", required_argument, NULL, 'h'},
342 {"html", no_argument, NULL, 'H'},
343 {"list", no_argument, NULL, 'l'},
344 {"log-file", required_argument, NULL, 'L'},
345 {"no-readline", no_argument, NULL, 'n'},
346 {"single-transaction", no_argument, NULL, '1'},
347 {"output", required_argument, NULL, 'o'},
348 {"port", required_argument, NULL, 'p'},
349 {"pset", required_argument, NULL, 'P'},
350 {"quiet", no_argument, NULL, 'q'},
351 {"record-separator", required_argument, NULL, 'R'},
352 {"single-step", no_argument, NULL, 's'},
353 {"single-line", no_argument, NULL, 'S'},
354 {"tuples-only", no_argument, NULL, 't'},
355 {"table-attr", required_argument, NULL, 'T'},
356 {"username", required_argument, NULL, 'U'},
357 {"set", required_argument, NULL, 'v'},
358 {"variable", required_argument, NULL, 'v'},
359 {"version", no_argument, NULL, 'V'},
360 {"no-password", no_argument, NULL, 'w'},
361 {"password", no_argument, NULL, 'W'},
362 {"expanded", no_argument, NULL, 'x'},
363 {"no-psqlrc", no_argument, NULL, 'X'},
364 {"help", no_argument, NULL, '?'},
373 memset(options, 0, sizeof *options);
375 while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxX?1",
376 long_options, &optindex)) != -1)
381 SetVariable(pset.vars, "ECHO", "all");
384 pset.popt.topt.format = PRINT_UNALIGNED;
387 options->action_string = optarg;
388 if (optarg[0] == '\\')
390 options->action = ACT_SINGLE_SLASH;
391 options->action_string++;
394 options->action = ACT_SINGLE_QUERY;
397 options->dbname = optarg;
400 SetVariable(pset.vars, "ECHO", "queries");
403 SetVariableBool(pset.vars, "ECHO_HIDDEN");
406 options->action = ACT_FILE;
407 options->action_string = optarg;
410 pset.popt.topt.fieldSep = pg_strdup(optarg);
413 options->host = optarg;
416 pset.popt.topt.format = PRINT_HTML;
419 options->action = ACT_LIST_DB;
422 options->logfilename = optarg;
425 options->no_readline = true;
431 options->port = optarg;
439 value = pg_strdup(optarg);
440 equal_loc = strchr(value, '=');
442 result = do_pset(value, NULL, &pset.popt, true);
446 result = do_pset(value, equal_loc + 1, &pset.popt, true);
451 fprintf(stderr, _("%s: could not set printing parameter \"%s\"\n"), pset.progname, value);
459 SetVariableBool(pset.vars, "QUIET");
462 pset.popt.topt.recordSep = pg_strdup(optarg);
465 SetVariableBool(pset.vars, "SINGLESTEP");
468 SetVariableBool(pset.vars, "SINGLELINE");
471 pset.popt.topt.tuples_only = true;
474 pset.popt.topt.tableAttr = pg_strdup(optarg);
477 options->username = optarg;
484 value = pg_strdup(optarg);
485 equal_loc = strchr(value, '=');
488 if (!DeleteVariable(pset.vars, value))
490 fprintf(stderr, _("%s: could not delete variable \"%s\"\n"),
491 pset.progname, value);
498 if (!SetVariable(pset.vars, value, equal_loc + 1))
500 fprintf(stderr, _("%s: could not set variable \"%s\"\n"),
501 pset.progname, value);
513 pset.getPassword = TRI_NO;
516 pset.getPassword = TRI_YES;
519 pset.popt.topt.expanded = true;
522 options->no_psqlrc = true;
525 options->single_txn = true;
528 /* Actual help option given */
529 if (strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0)
534 /* unknown option reported by getopt */
537 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
543 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
551 * if we still have arguments, use it as the database name and username
553 while (argc - optind >= 1)
555 if (!options->dbname)
556 options->dbname = argv[optind];
557 else if (!options->username)
558 options->username = argv[optind];
559 else if (!pset.quiet)
560 fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"),
561 pset.progname, argv[optind]);
569 * Load .psqlrc file, if found.
572 process_psqlrc(char *argv0)
574 char home[MAXPGPATH];
575 char rc_file[MAXPGPATH];
576 char my_exec_path[MAXPGPATH];
577 char etc_path[MAXPGPATH];
579 find_my_exec(argv0, my_exec_path);
580 get_etc_path(my_exec_path, etc_path);
582 snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
583 process_psqlrc_file(rc_file);
585 if (get_home_path(home))
587 snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
588 process_psqlrc_file(rc_file);
595 process_psqlrc_file(char *filename)
599 #if defined(WIN32) && (!defined(__MINGW32__))
603 psqlrc = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1);
604 sprintf(psqlrc, "%s-%s", filename, PG_VERSION);
606 if (access(psqlrc, R_OK) == 0)
607 (void) process_file(psqlrc, false);
608 else if (access(filename, R_OK) == 0)
609 (void) process_file(filename, false);
617 * This output format is intended to match GNU standards.
622 puts("psql (PostgreSQL) " PG_VERSION);
624 #if defined(USE_READLINE)
625 puts(_("contains support for command-line editing"));
632 * Assign hooks for psql variables.
634 * This isn't an amazingly good place for them, but neither is anywhere else.
638 autocommit_hook(const char *newval)
640 pset.autocommit = ParseVariableBool(newval);
644 on_error_stop_hook(const char *newval)
646 pset.on_error_stop = ParseVariableBool(newval);
650 quiet_hook(const char *newval)
652 pset.quiet = ParseVariableBool(newval);
656 singleline_hook(const char *newval)
658 pset.singleline = ParseVariableBool(newval);
662 singlestep_hook(const char *newval)
664 pset.singlestep = ParseVariableBool(newval);
668 fetch_count_hook(const char *newval)
670 pset.fetch_count = ParseVariableNum(newval, -1, -1, false);
674 echo_hook(const char *newval)
677 pset.echo = PSQL_ECHO_NONE;
678 else if (strcmp(newval, "queries") == 0)
679 pset.echo = PSQL_ECHO_QUERIES;
680 else if (strcmp(newval, "all") == 0)
681 pset.echo = PSQL_ECHO_ALL;
683 pset.echo = PSQL_ECHO_NONE;
687 echo_hidden_hook(const char *newval)
690 pset.echo_hidden = PSQL_ECHO_HIDDEN_OFF;
691 else if (strcmp(newval, "noexec") == 0)
692 pset.echo_hidden = PSQL_ECHO_HIDDEN_NOEXEC;
693 else if (pg_strcasecmp(newval, "off") == 0)
694 pset.echo_hidden = PSQL_ECHO_HIDDEN_OFF;
696 pset.echo_hidden = PSQL_ECHO_HIDDEN_ON;
700 on_error_rollback_hook(const char *newval)
703 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_OFF;
704 else if (pg_strcasecmp(newval, "interactive") == 0)
705 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_INTERACTIVE;
706 else if (pg_strcasecmp(newval, "off") == 0)
707 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_OFF;
709 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_ON;
713 histcontrol_hook(const char *newval)
716 pset.histcontrol = hctl_none;
717 else if (strcmp(newval, "ignorespace") == 0)
718 pset.histcontrol = hctl_ignorespace;
719 else if (strcmp(newval, "ignoredups") == 0)
720 pset.histcontrol = hctl_ignoredups;
721 else if (strcmp(newval, "ignoreboth") == 0)
722 pset.histcontrol = hctl_ignoreboth;
724 pset.histcontrol = hctl_none;
728 prompt1_hook(const char *newval)
730 pset.prompt1 = newval ? newval : "";
734 prompt2_hook(const char *newval)
736 pset.prompt2 = newval ? newval : "";
740 prompt3_hook(const char *newval)
742 pset.prompt3 = newval ? newval : "";
746 verbosity_hook(const char *newval)
749 pset.verbosity = PQERRORS_DEFAULT;
750 else if (strcmp(newval, "default") == 0)
751 pset.verbosity = PQERRORS_DEFAULT;
752 else if (strcmp(newval, "terse") == 0)
753 pset.verbosity = PQERRORS_TERSE;
754 else if (strcmp(newval, "verbose") == 0)
755 pset.verbosity = PQERRORS_VERBOSE;
757 pset.verbosity = PQERRORS_DEFAULT;
760 PQsetErrorVerbosity(pset.db, pset.verbosity);
765 EstablishVariableSpace(void)
767 pset.vars = CreateVariableSpace();
769 SetVariableAssignHook(pset.vars, "AUTOCOMMIT", autocommit_hook);
770 SetVariableAssignHook(pset.vars, "ON_ERROR_STOP", on_error_stop_hook);
771 SetVariableAssignHook(pset.vars, "QUIET", quiet_hook);
772 SetVariableAssignHook(pset.vars, "SINGLELINE", singleline_hook);
773 SetVariableAssignHook(pset.vars, "SINGLESTEP", singlestep_hook);
774 SetVariableAssignHook(pset.vars, "FETCH_COUNT", fetch_count_hook);
775 SetVariableAssignHook(pset.vars, "ECHO", echo_hook);
776 SetVariableAssignHook(pset.vars, "ECHO_HIDDEN", echo_hidden_hook);
777 SetVariableAssignHook(pset.vars, "ON_ERROR_ROLLBACK", on_error_rollback_hook);
778 SetVariableAssignHook(pset.vars, "HISTCONTROL", histcontrol_hook);
779 SetVariableAssignHook(pset.vars, "PROMPT1", prompt1_hook);
780 SetVariableAssignHook(pset.vars, "PROMPT2", prompt2_hook);
781 SetVariableAssignHook(pset.vars, "PROMPT3", prompt3_hook);
782 SetVariableAssignHook(pset.vars, "VERBOSITY", verbosity_hook);