startup.c 14.2 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
 * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.88 2004/04/19 17:42:58 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
#include "getopt_long.h"
21 22

#ifndef HAVE_OPTRESET
B
Bruce Momjian 已提交
23
int			optreset;
24 25
#endif

P
Peter Eisentraut 已提交
26 27
#include <locale.h>

28
#include "libpq-fe.h"
29 30 31

#include "command.h"
#include "common.h"
32 33
#include "describe.h"
#include "help.h"
34
#include "input.h"
35
#include "mainloop.h"
36
#include "print.h"
37 38
#include "settings.h"
#include "variables.h"
39

40 41
#include "mb/pg_wchar.h"

42 43 44
/*
 * Global psql options
 */
45
PsqlSettings pset;
46

47
#define PSQLRC ".psqlrc"
48

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

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

74
static void parse_psql_options(int argc, char *argv[],
B
Bruce Momjian 已提交
75
				   struct adhoc_opts * options);
76 77
static void process_psqlrc(void);
static void showVersion(void);
78

B
Bruce Momjian 已提交
79
#ifdef USE_SSL
80
static void printSSLInfo(void);
B
Bruce Momjian 已提交
81
#endif
82

B
> >  
Bruce Momjian 已提交
83 84 85 86
#ifdef WIN32
static void
			checkWin32Codepage(void);
#endif
87 88 89

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

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

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

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

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

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

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

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

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

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

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

157 158 159 160 161
#ifndef HAVE_UNIX_SOCKETS
	/* default to localhost on platforms without unix sockets */
	options.host = "localhost";
#endif

162
	parse_psql_options(argc, argv, &options);
163

164
	if (!pset.popt.topt.fieldSep)
165
		pset.popt.topt.fieldSep = pg_strdup(DEFAULT_FIELD_SEP);
166
	if (!pset.popt.topt.recordSep)
167
		pset.popt.topt.recordSep = pg_strdup(DEFAULT_RECORD_SEP);
168

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

182
	if (pset.getPassword)
B
Bruce Momjian 已提交
183
		password = simple_prompt("Password: ", 100, false);
184

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

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

B
Bruce Momjian 已提交
205 206 207
	free(username);
	free(password);

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

215 216
	PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);

217
	SyncVariables();
P
Peter Eisentraut 已提交
218

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

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

227
	/*
228 229
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
230

231
	/*
232 233
	 * process file given by -f
	 */
234
	if (options.action == ACT_FILE && strcmp(options.action_string, "-") != 0)
235 236 237
	{
		if (!options.no_psqlrc)
			process_psqlrc();
238

239
		successResult = process_file(options.action_string);
240 241
	}

242
	/*
243 244
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
245
	else if (options.action == ACT_SINGLE_SLASH)
246
	{
247 248
		PsqlScanState scan_state;

249
		if (VariableEquals(pset.vars, "ECHO", "all"))
250
			puts(options.action_string);
251

252 253 254 255 256 257
		scan_state = psql_scan_create();
		psql_scan_setup(scan_state,
						options.action_string,
						strlen(options.action_string));

		successResult = HandleSlashCmds(scan_state, NULL) != CMD_ERROR
258
			? EXIT_SUCCESS : EXIT_FAILURE;
259 260

		psql_scan_destroy(scan_state);
261 262
	}

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

271
		successResult = SendQuery(options.action_string)
272 273 274
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

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

297
		/* Default values for variables that are used in interactive case */
298 299 300
		SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
		SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
		SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
301

302 303 304 305
		if (!options.no_psqlrc)
			process_psqlrc();
		if (!pset.notty)
			initializeInput(options.no_readline ? 0 : 1);
306
		if (options.action_string)		/* -f - was used */
307
			pset.inputfile = "<stdin>";
308

P
Peter Eisentraut 已提交
309
		successResult = MainLoop(stdin);
310
	}
B
Bruce Momjian 已提交
311 312

	/* clean up */
313 314
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
315 316

	return successResult;
317 318 319 320 321 322 323 324 325
}



/*
 * Parse command line options
 */

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

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
366
	bool		used_old_u_option = false;
367

368
	memset(options, 0, sizeof *options);
369

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

431
					value = pg_strdup(optarg);
B
Bruce Momjian 已提交
432 433
					equal_loc = strchr(value, '=');
					if (!equal_loc)
434
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
435 436 437
					else
					{
						*equal_loc = '\0';
438
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
439 440 441 442
					}

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

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

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

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

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

		optind++;
	}
559

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

563 564 565 566 567
}



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

576 577
#if defined(WIN32) && (!defined(__MINGW32__))
#define R_OK 4
578 579
#endif

B
Bruce Momjian 已提交
580 581
	/* Look for one in the home dir */
	home = getenv("HOME");
582

B
Bruce Momjian 已提交
583 584
	if (home)
	{
585 586
		psqlrc = pg_malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
						   strlen(PG_VERSION) + 1);
587
		sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
588

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



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

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



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

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

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

B
Bruce Momjian 已提交
640
#endif
B
> >  
Bruce Momjian 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664



/*
 * checkWin32Codepage
 *
 * Prints a warning when win32 console codepage differs from Windows codepage
 */
#ifdef WIN32
static void
checkWin32Codepage(void)
{
	unsigned int wincp, concp;

	wincp = GetACP();
	concp = GetConsoleCP();
	if (wincp != concp) {
	  printf("Warning: Console codepage (%u) differs from windows codepage (%u)\n"
			 "         8-bit characters will not work correctly. See PostgreSQL\n"
			 "         documentation \"Installation on Windows\" for details.\n\n",
			 concp, wincp);
	}
}
#endif