startup.c 14.0 KB
Newer Older
P
Peter Eisentraut 已提交
1 2 3
/*
 * psql - the PostgreSQL interactive terminal
 *
P
Peter Eisentraut 已提交
4
 * Copyright 2000 by PostgreSQL Global Development Group
P
Peter Eisentraut 已提交
5
 *
6
 * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.30 2000/05/11 01:37:54 momjian Exp $
P
Peter Eisentraut 已提交
7
 */
8
#include "postgres.h"
9 10 11

#include <sys/types.h>

12 13
#ifndef WIN32
#include <unistd.h>
14
#else							/* WIN32 */
15
#include <io.h>
B
Hi!  
Bruce Momjian 已提交
16
#include <windows.h>
17
#include <win32.h>
18
#endif	 /* WIN32 */
19 20 21 22 23

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

24 25
#include "libpq-fe.h"
#include "version.h"
26 27 28

#include "command.h"
#include "common.h"
29 30
#include "describe.h"
#include "help.h"
31
#include "input.h"
32
#include "mainloop.h"
33
#include "print.h"
34 35
#include "settings.h"
#include "variables.h"
36

37 38 39 40 41 42 43 44
#ifdef MULTIBYTE
#include "miscadmin.h"
#include "mb/pg_wchar.h"
#else
/* XXX Grand unified hard-coded badness; this should go into libpq */
#define pg_encoding_to_char(x) "SQL_ASCII"
#endif

45 46 47
/*
 * Global psql options
 */
48
PsqlSettings pset;
49 50


51 52
/*
 * Structures to pass information between the option parsing routine
53 54
 * and the main function
 */
B
Bruce Momjian 已提交
55 56 57 58 59 60 61
enum _actions
{
	ACT_NOTHING = 0,
	ACT_SINGLE_SLASH,
	ACT_LIST_DB,
	ACT_SINGLE_QUERY,
	ACT_FILE
62 63
};

B
Bruce Momjian 已提交
64 65 66 67 68 69 70 71 72
struct adhoc_opts
{
	char	   *dbname;
	char	   *host;
	char	   *port;
	char	   *username;
	enum _actions action;
	char	   *action_string;
	bool		no_readline;
73
	bool		no_psqlrc;
74 75 76
};

static void
77
			parse_psql_options(int argc, char *argv[], struct adhoc_opts * options);
78

79
static void
80
			process_psqlrc(void);
81 82

static void
83
			showVersion(void);
84

85 86 87 88


/*
 *
89
 * main
90 91 92
 *
 */
int
93
main(int argc, char *argv[])
94
{
B
Bruce Momjian 已提交
95 96
	struct adhoc_opts options;
	int			successResult;
97

B
Bruce Momjian 已提交
98 99 100
	char	   *username = NULL;
	char	   *password = NULL;
	bool		need_pass;
101

102 103 104 105
	if (!strrchr(argv[0], SEP_CHAR))
		pset.progname = argv[0];
	else
		pset.progname = strrchr(argv[0], SEP_CHAR) + 1;
106

107 108
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
109
	pset.encoding = PQenv2encoding();
110

111
	pset.vars = CreateVariableSpace();
112 113 114 115 116
	if (!pset.vars)
	{
		fprintf(stderr, "%s: out of memory\n", pset.progname);
		exit(EXIT_FAILURE);
	}
117 118 119
	pset.popt.topt.format = PRINT_ALIGNED;
	pset.queryFout = stdout;
	pset.popt.topt.border = 1;
P
Peter Eisentraut 已提交
120
	pset.popt.topt.pager = true;
121

122
	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
123

124
	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
125

126
	/* This is obsolete and should be removed sometime. */
127
#ifdef PSQL_ALWAYS_GET_PASSWORDS
128
	pset.getPassword = true;
129
#else
130
	pset.getPassword = false;
131 132
#endif

133
	parse_psql_options(argc, argv, &options);
134

135 136 137 138
	if (!pset.popt.topt.fieldSep)
		pset.popt.topt.fieldSep = xstrdup(DEFAULT_FIELD_SEP);
	if (!pset.popt.topt.recordSep)
		pset.popt.topt.recordSep = xstrdup(DEFAULT_RECORD_SEP);
139

B
Bruce Momjian 已提交
140 141
	if (options.username)
	{
142 143 144 145 146 147

		/*
		 * The \001 is a hack to support the deprecated -u option which
		 * issues a username prompt. The recommended option is -U followed
		 * by the name on the command line.
		 */
148
		if (strcmp(options.username, "\001") == 0)
B
Bruce Momjian 已提交
149 150 151
			username = simple_prompt("Username: ", 100, true);
		else
			username = strdup(options.username);
152 153
	}

154
	if (pset.getPassword)
B
Bruce Momjian 已提交
155
		password = simple_prompt("Password: ", 100, false);
156

B
Bruce Momjian 已提交
157 158 159 160
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
161
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
162 163
			options.action == ACT_LIST_DB ? "template1" : options.dbname,
							   username, password);
B
Bruce Momjian 已提交
164

165 166
		if (PQstatus(pset.db) == CONNECTION_BAD &&
			strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0)
B
Bruce Momjian 已提交
167 168 169 170 171 172 173
		{
			need_pass = true;
			free(password);
			password = NULL;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);
174

B
Bruce Momjian 已提交
175 176 177
	free(username);
	free(password);

178
	if (PQstatus(pset.db) == CONNECTION_BAD)
B
Bruce Momjian 已提交
179
	{
180
		fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
181
		PQfinish(pset.db);
B
Bruce Momjian 已提交
182 183 184
		exit(EXIT_BADCONN);
	}

185 186 187 188 189 190 191
	PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);

	/*
	 * We need to save the encoding because we want to have it available
	 * even if the database connection goes bad.
	 */
	pset.encoding = PQclientEncoding(pset.db);
P
Peter Eisentraut 已提交
192

B
Bruce Momjian 已提交
193 194
	if (options.action == ACT_LIST_DB)
	{
195
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
196

197
		PQfinish(pset.db);
198
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
199 200
	}

201 202 203 204 205
	SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
	SetVariable(pset.vars, "USER", PQuser(pset.db));
	SetVariable(pset.vars, "HOST", PQhost(pset.db));
	SetVariable(pset.vars, "PORT", PQport(pset.db));
	SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
206 207 208 209

#ifndef WIN32
	pqsignal(SIGINT, handle_sigint);	/* control-C => cancel */
#endif
B
Bruce Momjian 已提交
210

211
	/*
212 213
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
214

215
	/*
216 217
	 * process file given by -f
	 */
B
Bruce Momjian 已提交
218
	if (options.action == ACT_FILE)
219 220 221
	{
		if (!options.no_psqlrc)
			process_psqlrc();
222

223
		successResult = process_file(options.action_string);
224 225
	}

226
	/*
227 228
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
229
	else if (options.action == ACT_SINGLE_SLASH)
230 231
	{
		const char *value;
232

233 234
		if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "all") == 0)
			puts(options.action_string);
235
		successResult = HandleSlashCmds(options.action_string, NULL, NULL) != CMD_ERROR
236 237 238
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

239
	/*
240 241
	 * If the query given to -c was a normal one, send it
	 */
B
Bruce Momjian 已提交
242
	else if (options.action == ACT_SINGLE_QUERY)
243 244
	{
		const char *value;
245

246 247
		if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "all") == 0)
			puts(options.action_string);
248
		successResult = SendQuery(options.action_string)
249 250 251
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

252
	/*
253 254
	 * or otherwise enter interactive main loop
	 */
B
Bruce Momjian 已提交
255
	else
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	{
		pset.issuper = test_superuser(PQuser(pset.db));
		if (!QUIET() && !pset.notty)
		{
			printf("Welcome to %s, the PostgreSQL interactive terminal.\n\n"
				   "Type:  \\copyright for distribution terms\n"
				   "       \\h for help with SQL commands\n"
				   "       \\? for help on internal slash commands\n"
			  "       \\g or terminate with semicolon to execute query\n"
				   "       \\q to quit\n\n", pset.progname);
		}

		SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
		SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
		SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
		if (!options.no_psqlrc)
			process_psqlrc();
		if (!pset.notty)
			initializeInput(options.no_readline ? 0 : 1);
P
Peter Eisentraut 已提交
275
		successResult = MainLoop(stdin);
276
	}
B
Bruce Momjian 已提交
277 278

	/* clean up */
279 280
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
281 282

	return successResult;
283 284 285 286 287 288 289 290 291 292
}



/*
 * Parse command line options
 */

#ifdef WIN32
/* getopt is not in the standard includes on Win32 */
B
Bruce Momjian 已提交
293
int			getopt(int, char *const[], const char *);
294

B
Hi!  
Bruce Momjian 已提交
295
/* And it requires progname to be set */
296 297
char	   *__progname = "psql";

298 299 300
#endif

static void
301
parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
302 303
{
#ifdef HAVE_GETOPT_LONG
304
	static struct option long_options[] =
305 306
	{
		{"echo-all", no_argument, NULL, 'a'},
B
Bruce Momjian 已提交
307 308 309
		{"no-align", no_argument, NULL, 'A'},
		{"command", required_argument, NULL, 'c'},
		{"dbname", required_argument, NULL, 'd'},
310
		{"echo-queries", no_argument, NULL, 'e'},
311
		{"echo-hidden", no_argument, NULL, 'E'},
B
Bruce Momjian 已提交
312
		{"file", required_argument, NULL, 'f'},
313
		{"field-separator", required_argument, NULL, 'F'},
B
Bruce Momjian 已提交
314 315 316
		{"host", required_argument, NULL, 'h'},
		{"html", no_argument, NULL, 'H'},
		{"list", no_argument, NULL, 'l'},
317
		{"noreadline", no_argument, NULL, 'n'},
318
		{"output", required_argument, NULL, 'o'},
B
Bruce Momjian 已提交
319 320 321
		{"port", required_argument, NULL, 'p'},
		{"pset", required_argument, NULL, 'P'},
		{"quiet", no_argument, NULL, 'q'},
322
		{"record-separator", required_argument, NULL, 'R'},
B
Bruce Momjian 已提交
323 324 325 326 327 328 329 330 331
		{"single-step", no_argument, NULL, 's'},
		{"single-line", no_argument, NULL, 'S'},
		{"tuples-only", no_argument, NULL, 't'},
		{"table-attr", required_argument, NULL, 'T'},
		{"username", required_argument, NULL, 'U'},
		{"set", required_argument, NULL, 'v'},
		{"variable", required_argument, NULL, 'v'},
		{"version", no_argument, NULL, 'V'},
		{"password", no_argument, NULL, 'W'},
332
		{"expanded", no_argument, NULL, 'x'},
333
		{"no-psqlrc", no_argument, NULL, 'X'},
B
Bruce Momjian 已提交
334 335 336 337
		{"help", no_argument, NULL, '?'},
	};

	int			optindex;
338 339

#endif	 /* HAVE_GETOPT_LONG */
340

B
Bruce Momjian 已提交
341 342 343
	extern char *optarg;
	extern int	optind;
	int			c;
344
	bool		used_old_u_option = false;
345

346
	memset(options, 0, sizeof *options);
347 348

#ifdef HAVE_GETOPT_LONG
349
	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:lh:Hno:p:P:qRsStT:uU:v:VWxX?", long_options, &optindex)) != -1)
350 351
#else							/* not HAVE_GETOPT_LONG */

B
Bruce Momjian 已提交
352 353 354 355
	/*
	 * Be sure to leave the '-' in here, so we can catch accidental long
	 * options.
	 */
356
	while ((c = getopt(argc, argv, "aAc:d:eEf:F:lh:Hno:p:P:qRsStT:uU:v:VWxX?-")) != -1)
357
#endif	 /* not HAVE_GETOPT_LONG */
358
	{
B
Bruce Momjian 已提交
359 360
		switch (c)
		{
361 362 363
			case 'a':
				SetVariable(pset.vars, "ECHO", "all");
				break;
B
Bruce Momjian 已提交
364
			case 'A':
365
				pset.popt.topt.format = PRINT_UNALIGNED;
B
Bruce Momjian 已提交
366 367 368 369
				break;
			case 'c':
				options->action_string = optarg;
				if (optarg[0] == '\\')
P
Peter Eisentraut 已提交
370
				{
B
Bruce Momjian 已提交
371
					options->action = ACT_SINGLE_SLASH;
P
Peter Eisentraut 已提交
372 373
					options->action_string++;
				}
B
Bruce Momjian 已提交
374 375 376 377 378 379 380
				else
					options->action = ACT_SINGLE_QUERY;
				break;
			case 'd':
				options->dbname = optarg;
				break;
			case 'e':
381
				SetVariable(pset.vars, "ECHO", "queries");
B
Bruce Momjian 已提交
382 383
				break;
			case 'E':
384
				SetVariableBool(pset.vars, "ECHO_HIDDEN");
B
Bruce Momjian 已提交
385 386 387 388 389 390
				break;
			case 'f':
				options->action = ACT_FILE;
				options->action_string = optarg;
				break;
			case 'F':
391
				pset.popt.topt.fieldSep = xstrdup(optarg);
B
Bruce Momjian 已提交
392 393 394 395 396
				break;
			case 'h':
				options->host = optarg;
				break;
			case 'H':
397
				pset.popt.topt.format = PRINT_HTML;
B
Bruce Momjian 已提交
398 399 400 401 402 403 404 405
				break;
			case 'l':
				options->action = ACT_LIST_DB;
				break;
			case 'n':
				options->no_readline = true;
				break;
			case 'o':
406
				setQFout(optarg);
B
Bruce Momjian 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419
				break;
			case 'p':
				options->port = optarg;
				break;
			case 'P':
				{
					char	   *value;
					char	   *equal_loc;
					bool		result;

					value = xstrdup(optarg);
					equal_loc = strchr(value, '=');
					if (!equal_loc)
420
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
421 422 423
					else
					{
						*equal_loc = '\0';
424
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
425 426 427 428
					}

					if (!result)
					{
429
						fprintf(stderr, "%s: couldn't set printing parameter %s\n", pset.progname, value);
B
Bruce Momjian 已提交
430 431 432 433 434 435 436
						exit(EXIT_FAILURE);
					}

					free(value);
					break;
				}
			case 'q':
437
				SetVariableBool(pset.vars, "QUIET");
B
Bruce Momjian 已提交
438
				break;
439 440 441
			case 'R':
				pset.popt.topt.recordSep = xstrdup(optarg);
				break;
B
Bruce Momjian 已提交
442
			case 's':
443
				SetVariableBool(pset.vars, "SINGLESTEP");
B
Bruce Momjian 已提交
444 445
				break;
			case 'S':
446
				SetVariableBool(pset.vars, "SINGLELINE");
B
Bruce Momjian 已提交
447 448
				break;
			case 't':
449
				pset.popt.topt.tuples_only = true;
B
Bruce Momjian 已提交
450 451
				break;
			case 'T':
452
				pset.popt.topt.tableAttr = xstrdup(optarg);
B
Bruce Momjian 已提交
453 454
				break;
			case 'u':
455
				pset.getPassword = true;
456 457 458 459
				options->username = "\001";		/* hopefully nobody has
												 * that username */
				/* this option is out */
				used_old_u_option = true;
B
Bruce Momjian 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472
				break;
			case 'U':
				options->username = optarg;
				break;
			case 'v':
				{
					char	   *value;
					char	   *equal_loc;

					value = xstrdup(optarg);
					equal_loc = strchr(value, '=');
					if (!equal_loc)
					{
473
						if (!DeleteVariable(pset.vars, value))
B
Bruce Momjian 已提交
474
						{
475
							fprintf(stderr, "%s: could not delete variable %s\n",
476
									pset.progname, value);
B
Bruce Momjian 已提交
477 478 479 480 481 482
							exit(EXIT_FAILURE);
						}
					}
					else
					{
						*equal_loc = '\0';
483
						if (!SetVariable(pset.vars, value, equal_loc + 1))
B
Bruce Momjian 已提交
484
						{
485
							fprintf(stderr, "%s: could not set variable %s\n",
486
									pset.progname, value);
B
Bruce Momjian 已提交
487 488 489 490 491 492 493 494
							exit(EXIT_FAILURE);
						}
					}

					free(value);
					break;
				}
			case 'V':
495 496
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
497
			case 'W':
498
				pset.getPassword = true;
B
Bruce Momjian 已提交
499
				break;
500 501 502
			case 'x':
				pset.popt.topt.expanded = true;
				break;
503 504 505
			case 'X':
				options->no_psqlrc = true;
				break;
B
Bruce Momjian 已提交
506
			case '?':
507 508 509 510 511 512 513 514 515 516 517 518
				/* Actual help option given */
				if (strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0)
				{
					usage();
					exit(EXIT_SUCCESS);
				}
				/* unknown option reported by getopt */
				else
				{
					fputs("Try -? for help.\n", stderr);
					exit(EXIT_FAILURE);
				}
B
Bruce Momjian 已提交
519
				break;
520
#ifndef HAVE_GETOPT_LONG
B
Bruce Momjian 已提交
521
			case '-':
522
				fprintf(stderr, "%s was compiled without support for long options.\n"
523
						"Use -? for help on invocation options.\n", pset.progname);
B
Bruce Momjian 已提交
524 525
				exit(EXIT_FAILURE);
				break;
526
#endif
B
Bruce Momjian 已提交
527
			default:
P
Peter Eisentraut 已提交
528
				fputs("Try -? for help.\n", stderr);
B
Bruce Momjian 已提交
529 530 531
				exit(EXIT_FAILURE);
				break;
		}
532 533
	}

B
Bruce Momjian 已提交
534 535 536 537 538 539 540 541 542 543
	/*
	 * if we still have arguments, use it as the database name and
	 * username
	 */
	while (argc - optind >= 1)
	{
		if (!options->dbname)
			options->dbname = argv[optind];
		else if (!options->username)
			options->username = argv[optind];
544
		else if (!QUIET())
545
			fprintf(stderr, "%s: warning: extra option %s ignored\n",
546
					pset.progname, argv[optind]);
B
Bruce Momjian 已提交
547 548 549

		optind++;
	}
550

551 552
	if (used_old_u_option && !QUIET())
		fprintf(stderr, "%s: Warning: The -u option is deprecated. Use -U.\n", pset.progname);
553

554 555 556 557 558
}



/*
P
Peter Eisentraut 已提交
559
 * Load .psqlrc file, if found.
560 561
 */
static void
562
process_psqlrc(void)
563
{
B
Bruce Momjian 已提交
564 565
	char	   *psqlrc;
	char	   *home;
566 567 568 569 570

#ifdef WIN32
#define R_OK 0
#endif

B
Bruce Momjian 已提交
571 572
	/* Look for one in the home dir */
	home = getenv("HOME");
573

B
Bruce Momjian 已提交
574 575
	if (home)
	{
576
		psqlrc = malloc(strlen(home) + 20);
B
Bruce Momjian 已提交
577 578
		if (!psqlrc)
		{
579
			fprintf(stderr, "%s: out of memory\n", pset.progname);
B
Bruce Momjian 已提交
580 581
			exit(EXIT_FAILURE);
		}
582

B
Bruce Momjian 已提交
583 584
		sprintf(psqlrc, "%s/.psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, home);
		if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
585
			process_file(psqlrc);
B
Bruce Momjian 已提交
586 587 588 589
		else
		{
			sprintf(psqlrc, "%s/.psqlrc", home);
			if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
590
				process_file(psqlrc);
B
Bruce Momjian 已提交
591 592
		}
		free(psqlrc);
593 594 595 596 597 598 599
	}
}



/* showVersion
 *
600
 * This output format is intended to match GNU standards.
601 602
 */
static void
603
showVersion(void)
604
{
605
	puts("psql (PostgreSQL) " PG_RELEASE "." PG_VERSION "." PG_SUBVERSION);
606

607
#if defined(USE_READLINE) || defined (USE_HISTORY) || defined(MULTIBYTE)
608
	fputs("contains ", stdout);
609 610

#ifdef USE_READLINE
611
	fputs("readline", stdout);
612
#define _Feature
613
#endif
614

615
#ifdef USE_HISTORY
616
#ifdef _Feature
617
	fputs(", ", stdout);
618 619
#else
#define _Feature
620
#endif
621
	fputs("history", stdout);
622
#endif
623 624 625

#ifdef MULTIBYTE
#ifdef _Feature
626
	fputs(", ", stdout);
627 628
#else
#define _Feature
629
#endif
630
	fputs("multibyte", stdout);
631
#endif
632

633
#undef _Feature
634

635
	puts(" support");
636
#endif
637

638 639 640 641
	puts("Portions Copyright (c) 1996-2000, PostgreSQL, Inc");
	puts("Portions Copyright (c) 1996 Regents of the University of California");
	puts("Read the file COPYRIGHT or use the command \\copyright to see the");
	puts("usage and distribution terms.");
642
}