startup.c 13.4 KB
Newer Older
P
Peter Eisentraut 已提交
1 2 3
/*
 * psql - the PostgreSQL interactive terminal
 *
4
 * Copyright (c) 2000-2003, PostgreSQL Global Development Group
P
Peter Eisentraut 已提交
5
 *
6
 * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.78 2003/08/04 23:59:40 tgl 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
#ifndef HAVE_GETOPT_LONG
#include "getopt_long.h"
B
Bruce Momjian 已提交
26
int			optreset;
27 28
#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 parse_psql_options(int argc, char *argv[],
B
Bruce Momjian 已提交
78
				   struct adhoc_opts * options);
79 80
static void process_psqlrc(void);
static void showVersion(void);
81

B
Bruce Momjian 已提交
82
#ifdef USE_SSL
83
static void printSSLInfo(void);
B
Bruce Momjian 已提交
84
#endif
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

P
Peter Eisentraut 已提交
102
	setlocale(LC_ALL, "");
103
#ifdef ENABLE_NLS
P
Peter Eisentraut 已提交
104 105 106 107
	bindtextdomain("psql", LOCALEDIR);
	textdomain("psql");
#endif

108
	pset.progname = get_progname(argv[0]);
109

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

124 125
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
126
	pset.encoding = PQenv2encoding();
127

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

140
	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
141

142 143
	/* Default values for variables that are used in noninteractive cases */
	SetVariableBool(pset.vars, "AUTOCOMMIT");
144
	SetVariable(pset.vars, "VERBOSITY", "default");
145

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

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

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

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

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

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

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

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

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

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

208 209
	PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);

210
	SyncVariables();
P
Peter Eisentraut 已提交
211

B
Bruce Momjian 已提交
212 213
	if (options.action == ACT_LIST_DB)
	{
214
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
215

216
		PQfinish(pset.db);
217
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
218 219
	}

220
	/*
221 222
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
223

224
	/*
225 226
	 * process file given by -f
	 */
227
	if (options.action == ACT_FILE && strcmp(options.action_string, "-") != 0)
228 229 230
	{
		if (!options.no_psqlrc)
			process_psqlrc();
231

232
		successResult = process_file(options.action_string);
233 234
	}

235
	/*
236 237
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
238
	else if (options.action == ACT_SINGLE_SLASH)
239
	{
240
		if (VariableEquals(pset.vars, "ECHO", "all"))
241
			puts(options.action_string);
242

243
		successResult = HandleSlashCmds(options.action_string, NULL, NULL, NULL) != CMD_ERROR
244 245 246
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

247
	/*
248 249
	 * If the query given to -c was a normal one, send it
	 */
B
Bruce Momjian 已提交
250
	else if (options.action == ACT_SINGLE_QUERY)
251
	{
252
		if (VariableEquals(pset.vars, "ECHO", "all"))
253
			puts(options.action_string);
254

255
		successResult = SendQuery(options.action_string)
256 257 258
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

259
	/*
260 261
	 * or otherwise enter interactive main loop
	 */
B
Bruce Momjian 已提交
262
	else
263 264 265
	{
		if (!QUIET() && !pset.notty)
		{
266
			printf(gettext("Welcome to %s %s, the PostgreSQL interactive terminal.\n\n"
P
Peter Eisentraut 已提交
267 268
						   "Type:  \\copyright for distribution terms\n"
						   "       \\h for help with SQL commands\n"
269 270
					   "       \\? for help on internal slash commands\n"
			  "       \\g or terminate with semicolon to execute query\n"
P
Peter Eisentraut 已提交
271
						   "       \\q to quit\n\n"),
272
				   pset.progname, PG_VERSION);
B
Bruce Momjian 已提交
273
#ifdef USE_SSL
274
			printSSLInfo();
B
Bruce Momjian 已提交
275
#endif
276 277
		}

278
		/* Default values for variables that are used in interactive case */
279 280 281
		SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
		SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
		SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
282

283 284 285 286
		if (!options.no_psqlrc)
			process_psqlrc();
		if (!pset.notty)
			initializeInput(options.no_readline ? 0 : 1);
287
		if (options.action_string)		/* -f - was used */
288
			pset.inputfile = "<stdin>";
289

P
Peter Eisentraut 已提交
290
		successResult = MainLoop(stdin);
291
	}
B
Bruce Momjian 已提交
292 293

	/* clean up */
294 295
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
296 297

	return successResult;
298 299 300 301 302 303 304 305 306
}



/*
 * Parse command line options
 */

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

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
347
	bool		used_old_u_option = false;
348

349
	memset(options, 0, sizeof *options);
350

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

					if (!result)
					{
424
						fprintf(stderr, gettext("%s: couldn't set printing parameter \"%s\"\n"), pset.progname, value);
B
Bruce Momjian 已提交
425 426 427 428 429 430 431
						exit(EXIT_FAILURE);
					}

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

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

					free(value);
					break;
				}
			case 'V':
490 491
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
492
			case 'W':
493
				pset.getPassword = true;
B
Bruce Momjian 已提交
494
				break;
495 496 497
			case 'x':
				pset.popt.topt.expanded = true;
				break;
498 499 500
			case 'X':
				options->no_psqlrc = true;
				break;
B
Bruce Momjian 已提交
501
			case '?':
502 503 504 505 506 507 508 509 510
				/* 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
				{
511
					fprintf(stderr, gettext("Try \"%s --help\" for more information.\n"),
512
							pset.progname);
513
					exit(EXIT_FAILURE);
514
				}
B
Bruce Momjian 已提交
515 516
				break;
			default:
517
				fprintf(stderr, gettext("Try \"%s --help\" for more information.\n"),
518
						pset.progname);
519
				exit(EXIT_FAILURE);
B
Bruce Momjian 已提交
520 521
				break;
		}
522 523
	}

B
Bruce Momjian 已提交
524 525 526 527 528 529 530 531 532 533
	/*
	 * 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];
534
		else if (!QUIET())
535
			fprintf(stderr, gettext("%s: warning: extra command-line argument \"%s\" ignored\n"),
536
					pset.progname, argv[optind]);
B
Bruce Momjian 已提交
537 538 539

		optind++;
	}
540

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

544 545 546 547 548
}



/*
P
Peter Eisentraut 已提交
549
 * Load .psqlrc file, if found.
550 551
 */
static void
552
process_psqlrc(void)
553
{
B
Bruce Momjian 已提交
554 555
	char	   *psqlrc;
	char	   *home;
556 557 558 559 560

#ifdef WIN32
#define R_OK 0
#endif

B
Bruce Momjian 已提交
561 562
	/* Look for one in the home dir */
	home = getenv("HOME");
563

B
Bruce Momjian 已提交
564 565
	if (home)
	{
566
		psqlrc = malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
B
Bruce Momjian 已提交
567
						strlen(PG_VERSION) + 1);
B
Bruce Momjian 已提交
568 569
		if (!psqlrc)
		{
P
Peter Eisentraut 已提交
570
			fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
B
Bruce Momjian 已提交
571 572
			exit(EXIT_FAILURE);
		}
573

574
		sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
B
Bruce Momjian 已提交
575
		if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
576
			process_file(psqlrc);
B
Bruce Momjian 已提交
577 578
		else
		{
579
			sprintf(psqlrc, "%s/%s", home, PSQLRC);
B
Bruce Momjian 已提交
580
			if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
581
				process_file(psqlrc);
B
Bruce Momjian 已提交
582 583
		}
		free(psqlrc);
584 585 586 587 588 589 590
	}
}



/* showVersion
 *
591
 * This output format is intended to match GNU standards.
592 593
 */
static void
594
showVersion(void)
595
{
596
	puts("psql (PostgreSQL) " PG_VERSION);
597

598
#if defined(USE_READLINE)
599
	puts(gettext("contains support for command-line editing"));
600
#endif
601
}
B
Bruce Momjian 已提交
602 603 604 605 606 607 608 609 610



/*
 * printSSLInfo
 *
 * Prints information about the current SSL connection, if SSL is in use
 */
#ifdef USE_SSL
B
Bruce Momjian 已提交
611
static void
B
Bruce Momjian 已提交
612 613
printSSLInfo(void)
{
B
Bruce Momjian 已提交
614 615
	int			sslbits = -1;
	SSL		   *ssl;
B
Bruce Momjian 已提交
616 617 618

	ssl = PQgetssl(pset.db);
	if (!ssl)
B
Bruce Momjian 已提交
619
		return;					/* no SSL */
B
Bruce Momjian 已提交
620 621

	SSL_get_cipher_bits(ssl, &sslbits);
622
	printf(gettext("SSL connection (cipher: %s, bits: %i)\n\n"),
B
Bruce Momjian 已提交
623
		   SSL_get_cipher(ssl), sslbits);
B
Bruce Momjian 已提交
624
}
625

B
Bruce Momjian 已提交
626
#endif