startup.c 13.9 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.29 2000/04/12 17:16:23 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 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

252
	/*
253 254
	 * or otherwise enter interactive main loop
	 */
B
Bruce Momjian 已提交
255
	else
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	{
		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 已提交
275
		successResult = MainLoop(stdin);
276
	}
B
Bruce Momjian 已提交
277 278

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

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



/*
 * Parse command line options
 */

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

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

298 299 300
#endif

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

	int			optindex;
337 338

#endif	 /* HAVE_GETOPT_LONG */
339

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

345
	memset(options, 0, sizeof *options);
346 347

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

B
Bruce Momjian 已提交
351 352 353 354
	/*
	 * Be sure to leave the '-' in here, so we can catch accidental long
	 * options.
	 */
355
	while ((c = getopt(argc, argv, "aAc:d:eEf:F:lh:Hno:p:P:qRsStT:uU:v:VWxX?-")) != -1)
356
#endif	 /* not HAVE_GETOPT_LONG */
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 = xstrdup(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 416 417 418
				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)
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, "%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 439 440
			case 'R':
				pset.popt.topt.recordSep = xstrdup(optarg);
				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 = xstrdup(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 468 469 470 471
				break;
			case 'U':
				options->username = optarg;
				break;
			case 'v':
				{
					char	   *value;
					char	   *equal_loc;

					value = xstrdup(optarg);
					equal_loc = strchr(value, '=');
					if (!equal_loc)
					{
472
						if (!DeleteVariable(pset.vars, value))
B
Bruce Momjian 已提交
473
						{
474
							fprintf(stderr, "%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, "%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 515 516 517
				/* 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
				{
					fputs("Try -? for help.\n", stderr);
					exit(EXIT_FAILURE);
				}
B
Bruce Momjian 已提交
518
				break;
519
#ifndef HAVE_GETOPT_LONG
B
Bruce Momjian 已提交
520
			case '-':
521
				fprintf(stderr, "%s was compiled without support for long options.\n"
522
						"Use -? for help on invocation options.\n", pset.progname);
B
Bruce Momjian 已提交
523 524
				exit(EXIT_FAILURE);
				break;
525
#endif
B
Bruce Momjian 已提交
526
			default:
P
Peter Eisentraut 已提交
527
				fputs("Try -? for help.\n", stderr);
B
Bruce Momjian 已提交
528 529 530
				exit(EXIT_FAILURE);
				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
}