startup.c 14.1 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.31 2000/05/11 03:14:19 momjian Exp $
P
Peter Eisentraut 已提交
7
 */
8
#include "postgres.h"
9 10 11

#include <sys/types.h>

12 13
#ifndef WIN32
#include <unistd.h>
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
#include "libpq-fe.h"
#include "version.h"
26 27 28

#include "command.h"
#include "common.h"
29 30
#include "describe.h"
#include "help.h"
31
#include "input.h"
32
#include "mainloop.h"
33
#include "print.h"
34 35
#include "settings.h"
#include "variables.h"
36

37 38 39 40 41 42 43 44
#ifdef MULTIBYTE
#include "miscadmin.h"
#include "mb/pg_wchar.h"
#else
/* XXX Grand unified hard-coded badness; this should go into libpq */
#define pg_encoding_to_char(x) "SQL_ASCII"
#endif

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


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

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

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

79
static void
80
			process_psqlrc(void);
81 82

static void
83
			showVersion(void);
84

85 86
static void
			explain_help_and_exit(void);
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

103 104 105 106
	if (!strrchr(argv[0], SEP_CHAR))
		pset.progname = argv[0];
	else
		pset.progname = strrchr(argv[0], SEP_CHAR) + 1;
107

108 109
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
110
	pset.encoding = PQenv2encoding();
111

112
	pset.vars = CreateVariableSpace();
113 114 115 116 117
	if (!pset.vars)
	{
		fprintf(stderr, "%s: out of memory\n", pset.progname);
		exit(EXIT_FAILURE);
	}
118 119 120
	pset.popt.topt.format = PRINT_ALIGNED;
	pset.queryFout = stdout;
	pset.popt.topt.border = 1;
P
Peter Eisentraut 已提交
121
	pset.popt.topt.pager = true;
122

123
	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
124

125
	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
126

127
	/* This is obsolete and should be removed sometime. */
128
#ifdef PSQL_ALWAYS_GET_PASSWORDS
129
	pset.getPassword = true;
130
#else
131
	pset.getPassword = false;
132 133
#endif

134
	parse_psql_options(argc, argv, &options);
135

136 137 138 139
	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);
140

B
Bruce Momjian 已提交
141 142
	if (options.username)
	{
143 144 145 146 147 148

		/*
		 * 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.
		 */
149
		if (strcmp(options.username, "\001") == 0)
B
Bruce Momjian 已提交
150 151 152
			username = simple_prompt("Username: ", 100, true);
		else
			username = strdup(options.username);
153 154
	}

155
	if (pset.getPassword)
B
Bruce Momjian 已提交
156
		password = simple_prompt("Password: ", 100, false);
157

B
Bruce Momjian 已提交
158 159 160 161
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
162
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
163 164
			options.action == ACT_LIST_DB ? "template1" : options.dbname,
							   username, password);
B
Bruce Momjian 已提交
165

166 167
		if (PQstatus(pset.db) == CONNECTION_BAD &&
			strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0)
B
Bruce Momjian 已提交
168 169 170 171 172 173 174
		{
			need_pass = true;
			free(password);
			password = NULL;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);
175

B
Bruce Momjian 已提交
176 177 178
	free(username);
	free(password);

179
	if (PQstatus(pset.db) == CONNECTION_BAD)
B
Bruce Momjian 已提交
180
	{
181
		fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
182
		PQfinish(pset.db);
B
Bruce Momjian 已提交
183 184 185
		exit(EXIT_BADCONN);
	}

186 187 188 189 190 191 192
	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 已提交
193

B
Bruce Momjian 已提交
194 195
	if (options.action == ACT_LIST_DB)
	{
196
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
197

198
		PQfinish(pset.db);
199
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
200 201
	}

202 203 204 205 206
	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));
207 208 209 210

#ifndef WIN32
	pqsignal(SIGINT, handle_sigint);	/* control-C => cancel */
#endif
B
Bruce Momjian 已提交
211

212
	/*
213 214
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
215

216
	/*
217 218
	 * process file given by -f
	 */
B
Bruce Momjian 已提交
219
	if (options.action == ACT_FILE)
220 221 222
	{
		if (!options.no_psqlrc)
			process_psqlrc();
223

224
		successResult = process_file(options.action_string);
225 226
	}

227
	/*
228 229
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
230
	else if (options.action == ACT_SINGLE_SLASH)
231 232
	{
		const char *value;
233

234 235
		if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "all") == 0)
			puts(options.action_string);
236
		successResult = HandleSlashCmds(options.action_string, NULL, NULL) != CMD_ERROR
237 238 239
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

240
	/*
241 242
	 * If the query given to -c was a normal one, send it
	 */
B
Bruce Momjian 已提交
243
	else if (options.action == ACT_SINGLE_QUERY)
244 245
	{
		const char *value;
246

247 248
		if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "all") == 0)
			puts(options.action_string);
249
		successResult = SendQuery(options.action_string)
250 251 252
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

253
	/*
254 255
	 * or otherwise enter interactive main loop
	 */
B
Bruce Momjian 已提交
256
	else
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	{
		pset.issuper = test_superuser(PQuser(pset.db));
		if (!QUIET() && !pset.notty)
		{
			printf("Welcome to %s, the PostgreSQL interactive terminal.\n\n"
				   "Type:  \\copyright for distribution terms\n"
				   "       \\h for help with SQL commands\n"
				   "       \\? for help on internal slash commands\n"
			  "       \\g or terminate with semicolon to execute query\n"
				   "       \\q to quit\n\n", pset.progname);
		}

		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);
P
Peter Eisentraut 已提交
276
		successResult = MainLoop(stdin);
277
	}
B
Bruce Momjian 已提交
278 279

	/* clean up */
280 281
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
282 283

	return successResult;
284 285 286 287 288 289 290 291 292 293
}



/*
 * Parse command line options
 */

#ifdef WIN32
/* getopt is not in the standard includes on Win32 */
B
Bruce Momjian 已提交
294
int			getopt(int, char *const[], const char *);
295

B
Hi!  
Bruce Momjian 已提交
296
/* And it requires progname to be set */
297 298
char	   *__progname = "psql";

299 300 301
#endif

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

	int			optindex;
339 340

#endif	 /* HAVE_GETOPT_LONG */
341

B
Bruce Momjian 已提交
342 343 344
	extern char *optarg;
	extern int	optind;
	int			c;
345
	bool		used_old_u_option = false;
346

347
	memset(options, 0, sizeof *options);
348 349

#ifdef HAVE_GETOPT_LONG
350
	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:lh:Hno:p:P:qRsStT:uU:v:VWxX?", long_options, &optindex)) != -1)
351 352
#else							/* not HAVE_GETOPT_LONG */

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

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

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

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

					free(value);
					break;
				}
			case 'V':
496 497
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
498
			case 'W':
499
				pset.getPassword = true;
B
Bruce Momjian 已提交
500
				break;
501 502 503
			case 'x':
				pset.popt.topt.expanded = true;
				break;
504 505 506
			case 'X':
				options->no_psqlrc = true;
				break;
B
Bruce Momjian 已提交
507
			case '?':
508 509 510 511 512 513 514 515 516
				/* 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
				{
517
					explain_help_and_exit();
518
				}
B
Bruce Momjian 已提交
519
				break;
520
#ifndef HAVE_GETOPT_LONG
B
Bruce Momjian 已提交
521
			case '-':
522
				fprintf(stderr, "%s was compiled without support for long options.\n"
523
						"Use -? for help on invocation options.\n", pset.progname);
B
Bruce Momjian 已提交
524 525
				exit(EXIT_FAILURE);
				break;
526
#endif
B
Bruce Momjian 已提交
527
			default:
528
				explain_help_and_exit();
B
Bruce Momjian 已提交
529 530
				break;
		}
531 532
	}

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

		optind++;
	}
549

550 551
	if (used_old_u_option && !QUIET())
		fprintf(stderr, "%s: Warning: The -u option is deprecated. Use -U.\n", pset.progname);
552

553 554 555 556 557
}



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

#ifdef WIN32
#define R_OK 0
#endif

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

B
Bruce Momjian 已提交
573 574
	if (home)
	{
575
		psqlrc = malloc(strlen(home) + 20);
B
Bruce Momjian 已提交
576 577
		if (!psqlrc)
		{
578
			fprintf(stderr, "%s: out of memory\n", pset.progname);
B
Bruce Momjian 已提交
579 580
			exit(EXIT_FAILURE);
		}
581

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



/* showVersion
 *
599
 * This output format is intended to match GNU standards.
600 601
 */
static void
602
showVersion(void)
603
{
604
	puts("psql (PostgreSQL) " PG_RELEASE "." PG_VERSION "." PG_SUBVERSION);
605

606
#if defined(USE_READLINE) || defined (USE_HISTORY) || defined(MULTIBYTE)
607
	fputs("contains ", stdout);
608 609

#ifdef USE_READLINE
610
	fputs("readline", stdout);
611
#define _Feature
612
#endif
613

614
#ifdef USE_HISTORY
615
#ifdef _Feature
616
	fputs(", ", stdout);
617 618
#else
#define _Feature
619
#endif
620
	fputs("history", stdout);
621
#endif
622 623 624

#ifdef MULTIBYTE
#ifdef _Feature
625
	fputs(", ", stdout);
626 627
#else
#define _Feature
628
#endif
629
	fputs("multibyte", stdout);
630
#endif
631

632
#undef _Feature
633

634
	puts(" support");
635
#endif
636

637 638 639 640
	puts("Portions Copyright (c) 1996-2000, PostgreSQL, Inc");
	puts("Portions Copyright (c) 1996 Regents of the University of California");
	puts("Read the file COPYRIGHT or use the command \\copyright to see the");
	puts("usage and distribution terms.");
641
}
642 643 644 645 646 647 648 649 650 651 652 653 654 655

static void
explain_help_and_exit(void)
{

#ifdef WIN32
	fputs("Try -? for help.\n", stderr);
#else /* !WIN32 */
	fputs("Try -\\? for help.\n", stderr);
#endif /* WIN32 */

	exit(EXIT_FAILURE);
}