startup.c 13.9 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.84 2004/02/12 19:58:16 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

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
	if (!pset.popt.topt.fieldSep)
159
		pset.popt.topt.fieldSep = pg_strdup(DEFAULT_FIELD_SEP);
160
	if (!pset.popt.topt.recordSep)
161
		pset.popt.topt.recordSep = pg_strdup(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
		else
173
			username = pg_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
	PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);

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

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

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

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

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

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

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

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

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

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

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

282
		/* Default values for variables that are used in interactive case */
283 284 285
		SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
		SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
		SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
286

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

P
Peter Eisentraut 已提交
294
		successResult = MainLoop(stdin);
295
	}
B
Bruce Momjian 已提交
296 297

	/* clean up */
298 299
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
300 301

	return successResult;
302 303 304 305 306 307 308 309 310
}



/*
 * Parse command line options
 */

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

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
351
	bool		used_old_u_option = false;
352

353
	memset(options, 0, sizeof *options);
354

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

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

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

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

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

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

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

		optind++;
	}
544

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

548 549 550 551 552
}



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

#ifdef WIN32
#define R_OK 0
#endif

B
Bruce Momjian 已提交
565 566
	/* Look for one in the home dir */
	home = getenv("HOME");
567

B
Bruce Momjian 已提交
568 569
	if (home)
	{
570 571
		psqlrc = pg_malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
						   strlen(PG_VERSION) + 1);
572
		sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
573

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



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

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



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

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

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

B
Bruce Momjian 已提交
625
#endif
B
> >  
Bruce Momjian 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649



/*
 * 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