startup.c 13.6 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.73 2003/04/04 20:42:13 momjian 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
	pset.progname = get_progname(argv[0]);
114

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

129 130
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
131
	pset.encoding = PQenv2encoding();
132

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

145
	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
146

147
	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
148

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

156
	parse_psql_options(argc, argv, &options);
157

158 159 160 161
	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);
162

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

176
	if (pset.getPassword)
B
Bruce Momjian 已提交
177
		password = simple_prompt("Password: ", 100, false);
178

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

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

B
Bruce Momjian 已提交
199 200 201
	free(username);
	free(password);

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

209 210 211 212 213 214 215
	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 已提交
216

B
Bruce Momjian 已提交
217 218
	if (options.action == ACT_LIST_DB)
	{
219
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
220

221
		PQfinish(pset.db);
222
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
223 224
	}

225 226 227 228 229
	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));
230

231 232
	pset.popt.topt.encoding = pset.encoding;

233
	/*
234 235
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
236

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

245
		successResult = process_file(options.action_string);
246 247
	}

248
	/*
249 250
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
251
	else if (options.action == ACT_SINGLE_SLASH)
252
	{
253
		if (VariableEquals(pset.vars, "ECHO", "all"))
254
			puts(options.action_string);
255

256
		successResult = HandleSlashCmds(options.action_string, NULL, NULL, NULL) != CMD_ERROR
257 258 259
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

260
	/*
261 262
	 * If the query given to -c was a normal one, send it
	 */
B
Bruce Momjian 已提交
263
	else if (options.action == ACT_SINGLE_QUERY)
264
	{
265
		if (VariableEquals(pset.vars, "ECHO", "all"))
266
			puts(options.action_string);
267

268
		successResult = SendQuery(options.action_string)
269 270 271
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

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

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

	/* clean up */
305 306
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
307 308

	return successResult;
309 310 311 312 313 314 315 316 317
}



/*
 * Parse command line options
 */

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

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
358
	bool		used_old_u_option = false;
359

360
	memset(options, 0, sizeof *options);
361

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

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

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

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

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

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

		optind++;
	}
551

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

555 556 557 558 559
}



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

#ifdef WIN32
#define R_OK 0
#endif

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

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

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



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

609
#if defined(USE_READLINE)
610
	puts(gettext("contains support for command-line editing"));
611
#endif
612
}
B
Bruce Momjian 已提交
613 614 615 616 617 618 619 620 621



/*
 * printSSLInfo
 *
 * Prints information about the current SSL connection, if SSL is in use
 */
#ifdef USE_SSL
B
Bruce Momjian 已提交
622
static void
B
Bruce Momjian 已提交
623 624
printSSLInfo(void)
{
B
Bruce Momjian 已提交
625 626
	int			sslbits = -1;
	SSL		   *ssl;
B
Bruce Momjian 已提交
627 628 629

	ssl = PQgetssl(pset.db);
	if (!ssl)
B
Bruce Momjian 已提交
630
		return;					/* no SSL */
B
Bruce Momjian 已提交
631 632

	SSL_get_cipher_bits(ssl, &sslbits);
633
	printf(gettext("SSL connection (cipher: %s, bits: %i)\n\n"),
B
Bruce Momjian 已提交
634
		   SSL_get_cipher(ssl), sslbits);
B
Bruce Momjian 已提交
635
}
636

B
Bruce Momjian 已提交
637
#endif