startup.c 13.8 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.71 2003/03/18 22:15:44 petere Exp $
P
Peter Eisentraut 已提交
7
 */
8
#include "postgres_fe.h"
9 10 11

#include <sys/types.h>

12 13
#ifndef WIN32
#include <unistd.h>
B
Bruce Momjian 已提交
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 26 27 28
#ifndef HAVE_GETOPT_LONG
#include "getopt_long.h"
int optreset;
#endif

P
Peter Eisentraut 已提交
29 30
#include <locale.h>

31
#include "libpq-fe.h"
32 33 34

#include "command.h"
#include "common.h"
35 36
#include "describe.h"
#include "help.h"
37
#include "input.h"
38
#include "mainloop.h"
39
#include "print.h"
40 41
#include "settings.h"
#include "variables.h"
42

43 44
#include "mb/pg_wchar.h"

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

50
#define PSQLRC ".psqlrc"
51

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

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

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

80
static void
81
			process_psqlrc(void);
82 83

static void
84
			showVersion(void);
85

B
Bruce Momjian 已提交
86 87
#ifdef USE_SSL
static void
B
Bruce Momjian 已提交
88
			printSSLInfo(void);
B
Bruce Momjian 已提交
89
#endif
90

91 92 93

/*
 *
94
 * main
95 96 97
 *
 */
int
98
main(int argc, char *argv[])
99
{
B
Bruce Momjian 已提交
100 101
	struct adhoc_opts options;
	int			successResult;
102

B
Bruce Momjian 已提交
103 104 105
	char	   *username = NULL;
	char	   *password = NULL;
	bool		need_pass;
106

P
Peter Eisentraut 已提交
107
	setlocale(LC_ALL, "");
108
#ifdef ENABLE_NLS
P
Peter Eisentraut 已提交
109 110 111 112
	bindtextdomain("psql", LOCALEDIR);
	textdomain("psql");
#endif

113
	if (!strrchr(argv[0], '/'))
114 115
		pset.progname = argv[0];
	else
116
		pset.progname = strrchr(argv[0], '/') + 1;
117

118 119
	if (argc > 1)
	{
B
Bruce Momjian 已提交
120
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
121 122 123 124
		{
			usage();
			exit(EXIT_SUCCESS);
		}
B
Bruce Momjian 已提交
125
		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
126 127 128 129
		{
			showVersion();
			exit(EXIT_SUCCESS);
		}
B
Bruce Momjian 已提交
130
	}
131

132 133
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
134
	pset.encoding = PQenv2encoding();
135

136
	pset.vars = CreateVariableSpace();
137 138
	if (!pset.vars)
	{
P
Peter Eisentraut 已提交
139
		fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
140 141
		exit(EXIT_FAILURE);
	}
142 143 144
	pset.popt.topt.format = PRINT_ALIGNED;
	pset.queryFout = stdout;
	pset.popt.topt.border = 1;
145
	pset.popt.topt.pager = 1;
146
	pset.popt.default_footer = true;
147

148
	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
149

150
	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
151

152
	/* This is obsolete and should be removed sometime. */
153
#ifdef PSQL_ALWAYS_GET_PASSWORDS
154
	pset.getPassword = true;
155
#else
156
	pset.getPassword = false;
157 158
#endif

159
	parse_psql_options(argc, argv, &options);
160

161 162 163 164
	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);
165

B
Bruce Momjian 已提交
166 167
	if (options.username)
	{
168 169 170 171 172
		/*
		 * 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.
		 */
173
		if (strcmp(options.username, "\001") == 0)
174
			username = simple_prompt("User name: ", 100, true);
B
Bruce Momjian 已提交
175 176
		else
			username = strdup(options.username);
177 178
	}

179
	if (pset.getPassword)
B
Bruce Momjian 已提交
180
		password = simple_prompt("Password: ", 100, false);
181

B
Bruce Momjian 已提交
182 183 184 185
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
186
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
187 188
			options.action == ACT_LIST_DB ? "template1" : options.dbname,
							   username, password);
B
Bruce Momjian 已提交
189

190
		if (PQstatus(pset.db) == CONNECTION_BAD &&
191 192
			strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0 &&
			!feof(stdin))
B
Bruce Momjian 已提交
193
		{
194
			PQfinish(pset.db);
B
Bruce Momjian 已提交
195 196 197 198 199 200
			need_pass = true;
			free(password);
			password = NULL;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);
201

B
Bruce Momjian 已提交
202 203 204
	free(username);
	free(password);

205
	if (PQstatus(pset.db) == CONNECTION_BAD)
B
Bruce Momjian 已提交
206
	{
207
		fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
208
		PQfinish(pset.db);
B
Bruce Momjian 已提交
209 210 211
		exit(EXIT_BADCONN);
	}

212 213 214 215 216 217 218
	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 已提交
219

B
Bruce Momjian 已提交
220 221
	if (options.action == ACT_LIST_DB)
	{
222
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
223

224
		PQfinish(pset.db);
225
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
226 227
	}

228 229 230 231 232
	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));
233

234 235
	pset.popt.topt.encoding = pset.encoding;

236
	/*
237 238
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
239

240
	/*
241 242
	 * process file given by -f
	 */
243
	if (options.action == ACT_FILE && strcmp(options.action_string, "-") != 0)
244 245 246
	{
		if (!options.no_psqlrc)
			process_psqlrc();
247

248
		successResult = process_file(options.action_string);
249 250
	}

251
	/*
252 253
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
254
	else if (options.action == ACT_SINGLE_SLASH)
255 256
	{
		const char *value;
257

258 259
		if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "all") == 0)
			puts(options.action_string);
260
		successResult = HandleSlashCmds(options.action_string, NULL, NULL, NULL) != CMD_ERROR
261 262 263
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

264
	/*
265 266
	 * If the query given to -c was a normal one, send it
	 */
B
Bruce Momjian 已提交
267
	else if (options.action == ACT_SINGLE_QUERY)
268 269
	{
		const char *value;
270

271 272
		if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "all") == 0)
			puts(options.action_string);
273
		successResult = SendQuery(options.action_string)
274 275 276
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

277
	/*
278 279
	 * or otherwise enter interactive main loop
	 */
B
Bruce Momjian 已提交
280
	else
281 282 283 284
	{
		pset.issuper = test_superuser(PQuser(pset.db));
		if (!QUIET() && !pset.notty)
		{
285
			printf(gettext("Welcome to %s %s, the PostgreSQL interactive terminal.\n\n"
P
Peter Eisentraut 已提交
286 287
						   "Type:  \\copyright for distribution terms\n"
						   "       \\h for help with SQL commands\n"
288 289
					   "       \\? for help on internal slash commands\n"
			  "       \\g or terminate with semicolon to execute query\n"
P
Peter Eisentraut 已提交
290
						   "       \\q to quit\n\n"),
291
				   pset.progname, PG_VERSION);
B
Bruce Momjian 已提交
292
#ifdef USE_SSL
293
			printSSLInfo();
B
Bruce Momjian 已提交
294
#endif
295 296
		}

297 298 299 300 301 302 303
		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);
304
		if (options.action_string)		/* -f - was used */
305
			pset.inputfile = "<stdin>";
P
Peter Eisentraut 已提交
306
		successResult = MainLoop(stdin);
307
	}
B
Bruce Momjian 已提交
308 309

	/* clean up */
310 311
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
312 313

	return successResult;
314 315 316 317 318 319 320 321 322
}



/*
 * Parse command line options
 */

static void
323
parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
324
{
325
	static struct option long_options[] =
326 327
	{
		{"echo-all", no_argument, NULL, 'a'},
B
Bruce Momjian 已提交
328 329 330
		{"no-align", no_argument, NULL, 'A'},
		{"command", required_argument, NULL, 'c'},
		{"dbname", required_argument, NULL, 'd'},
331
		{"echo-queries", no_argument, NULL, 'e'},
332
		{"echo-hidden", no_argument, NULL, 'E'},
B
Bruce Momjian 已提交
333
		{"file", required_argument, NULL, 'f'},
334
		{"field-separator", required_argument, NULL, 'F'},
B
Bruce Momjian 已提交
335 336 337
		{"host", required_argument, NULL, 'h'},
		{"html", no_argument, NULL, 'H'},
		{"list", no_argument, NULL, 'l'},
338
		{"no-readline", no_argument, NULL, 'n'},
339
		{"output", required_argument, NULL, 'o'},
B
Bruce Momjian 已提交
340 341 342
		{"port", required_argument, NULL, 'p'},
		{"pset", required_argument, NULL, 'P'},
		{"quiet", no_argument, NULL, 'q'},
343
		{"record-separator", required_argument, NULL, 'R'},
B
Bruce Momjian 已提交
344 345 346 347 348 349 350 351 352
		{"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'},
353
		{"expanded", no_argument, NULL, 'x'},
354
		{"no-psqlrc", no_argument, NULL, 'X'},
B
Bruce Momjian 已提交
355
		{"help", no_argument, NULL, '?'},
T
Tatsuo Ishii 已提交
356
		{NULL, 0, NULL, 0}
B
Bruce Momjian 已提交
357 358 359 360 361 362
	};

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
363
	bool		used_old_u_option = false;
364

365
	memset(options, 0, sizeof *options);
366

367 368
	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?",
							long_options, &optindex)) != -1)
369
	{
B
Bruce Momjian 已提交
370 371
		switch (c)
		{
372 373 374
			case 'a':
				SetVariable(pset.vars, "ECHO", "all");
				break;
B
Bruce Momjian 已提交
375
			case 'A':
376
				pset.popt.topt.format = PRINT_UNALIGNED;
B
Bruce Momjian 已提交
377 378 379 380
				break;
			case 'c':
				options->action_string = optarg;
				if (optarg[0] == '\\')
P
Peter Eisentraut 已提交
381
				{
B
Bruce Momjian 已提交
382
					options->action = ACT_SINGLE_SLASH;
P
Peter Eisentraut 已提交
383 384
					options->action_string++;
				}
B
Bruce Momjian 已提交
385 386 387 388 389 390 391
				else
					options->action = ACT_SINGLE_QUERY;
				break;
			case 'd':
				options->dbname = optarg;
				break;
			case 'e':
392
				SetVariable(pset.vars, "ECHO", "queries");
B
Bruce Momjian 已提交
393 394
				break;
			case 'E':
395
				SetVariableBool(pset.vars, "ECHO_HIDDEN");
B
Bruce Momjian 已提交
396 397 398 399 400 401
				break;
			case 'f':
				options->action = ACT_FILE;
				options->action_string = optarg;
				break;
			case 'F':
402
				pset.popt.topt.fieldSep = xstrdup(optarg);
B
Bruce Momjian 已提交
403 404 405 406 407
				break;
			case 'h':
				options->host = optarg;
				break;
			case 'H':
408
				pset.popt.topt.format = PRINT_HTML;
B
Bruce Momjian 已提交
409 410 411 412 413 414 415 416
				break;
			case 'l':
				options->action = ACT_LIST_DB;
				break;
			case 'n':
				options->no_readline = true;
				break;
			case 'o':
417
				setQFout(optarg);
B
Bruce Momjian 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430
				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)
431
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
432 433 434
					else
					{
						*equal_loc = '\0';
435
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
436 437 438 439
					}

					if (!result)
					{
P
Peter Eisentraut 已提交
440
						fprintf(stderr, gettext("%s: couldn't set printing parameter %s\n"), pset.progname, value);
B
Bruce Momjian 已提交
441 442 443 444 445 446 447
						exit(EXIT_FAILURE);
					}

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

					value = xstrdup(optarg);
					equal_loc = strchr(value, '=');
					if (!equal_loc)
					{
484
						if (!DeleteVariable(pset.vars, value))
B
Bruce Momjian 已提交
485
						{
P
Peter Eisentraut 已提交
486
							fprintf(stderr, gettext("%s: could not delete variable %s\n"),
487
									pset.progname, value);
B
Bruce Momjian 已提交
488 489 490 491 492 493
							exit(EXIT_FAILURE);
						}
					}
					else
					{
						*equal_loc = '\0';
494
						if (!SetVariable(pset.vars, value, equal_loc + 1))
B
Bruce Momjian 已提交
495
						{
P
Peter Eisentraut 已提交
496
							fprintf(stderr, gettext("%s: could not set variable %s\n"),
497
									pset.progname, value);
B
Bruce Momjian 已提交
498 499 500 501 502 503 504 505
							exit(EXIT_FAILURE);
						}
					}

					free(value);
					break;
				}
			case 'V':
506 507
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
508
			case 'W':
509
				pset.getPassword = true;
B
Bruce Momjian 已提交
510
				break;
511 512 513
			case 'x':
				pset.popt.topt.expanded = true;
				break;
514 515 516
			case 'X':
				options->no_psqlrc = true;
				break;
B
Bruce Momjian 已提交
517
			case '?':
518 519 520 521 522 523 524 525 526
				/* 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
				{
P
Peter Eisentraut 已提交
527
					fprintf(stderr, gettext("Try '%s --help' for more information.\n"),
528
							pset.progname);
529
					exit(EXIT_FAILURE);
530
				}
B
Bruce Momjian 已提交
531 532
				break;
			default:
P
Peter Eisentraut 已提交
533
				fprintf(stderr, gettext("Try '%s --help' for more information.\n"),
534
						pset.progname);
535
				exit(EXIT_FAILURE);
B
Bruce Momjian 已提交
536 537
				break;
		}
538 539
	}

B
Bruce Momjian 已提交
540 541 542 543 544 545 546 547 548 549
	/*
	 * 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];
550
		else if (!QUIET())
P
Peter Eisentraut 已提交
551
			fprintf(stderr, gettext("%s: warning: extra option %s ignored\n"),
552
					pset.progname, argv[optind]);
B
Bruce Momjian 已提交
553 554 555

		optind++;
	}
556

557
	if (used_old_u_option && !QUIET())
P
Peter Eisentraut 已提交
558
		fprintf(stderr, gettext("%s: Warning: The -u option is deprecated. Use -U.\n"), pset.progname);
559

560 561 562 563 564
}



/*
P
Peter Eisentraut 已提交
565
 * Load .psqlrc file, if found.
566 567
 */
static void
568
process_psqlrc(void)
569
{
B
Bruce Momjian 已提交
570 571
	char	   *psqlrc;
	char	   *home;
572 573 574 575 576

#ifdef WIN32
#define R_OK 0
#endif

B
Bruce Momjian 已提交
577 578
	/* Look for one in the home dir */
	home = getenv("HOME");
579

B
Bruce Momjian 已提交
580 581
	if (home)
	{
582
		psqlrc = malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
583
				 strlen(PG_VERSION) + 1);
B
Bruce Momjian 已提交
584 585
		if (!psqlrc)
		{
P
Peter Eisentraut 已提交
586
			fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
B
Bruce Momjian 已提交
587 588
			exit(EXIT_FAILURE);
		}
589

590
		sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
B
Bruce Momjian 已提交
591
		if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
592
			process_file(psqlrc);
B
Bruce Momjian 已提交
593 594
		else
		{
595
			sprintf(psqlrc, "%s/%s", home, PSQLRC);
B
Bruce Momjian 已提交
596
			if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
597
				process_file(psqlrc);
B
Bruce Momjian 已提交
598 599
		}
		free(psqlrc);
600 601 602 603 604 605 606
	}
}



/* showVersion
 *
607
 * This output format is intended to match GNU standards.
608 609
 */
static void
610
showVersion(void)
611
{
612
	puts("psql (PostgreSQL) " PG_VERSION);
613

614
#if defined(USE_READLINE)
615
	puts(gettext("contains support for command-line editing"));
616
#endif
617
}
B
Bruce Momjian 已提交
618 619 620 621 622 623 624 625 626



/*
 * printSSLInfo
 *
 * Prints information about the current SSL connection, if SSL is in use
 */
#ifdef USE_SSL
B
Bruce Momjian 已提交
627
static void
B
Bruce Momjian 已提交
628 629
printSSLInfo(void)
{
B
Bruce Momjian 已提交
630 631
	int			sslbits = -1;
	SSL		   *ssl;
B
Bruce Momjian 已提交
632 633 634

	ssl = PQgetssl(pset.db);
	if (!ssl)
B
Bruce Momjian 已提交
635
		return;					/* no SSL */
B
Bruce Momjian 已提交
636 637

	SSL_get_cipher_bits(ssl, &sslbits);
638
	printf(gettext("SSL connection (cipher: %s, bits: %i)\n\n"),
B
Bruce Momjian 已提交
639
		   SSL_get_cipher(ssl), sslbits);
B
Bruce Momjian 已提交
640
}
641

B
Bruce Momjian 已提交
642
#endif