startup.c 16.9 KB
Newer Older
P
Peter Eisentraut 已提交
1 2 3
/*
 * psql - the PostgreSQL interactive terminal
 *
4
 * Copyright (c) 2000-2006, PostgreSQL Global Development Group
P
Peter Eisentraut 已提交
5
 *
6
 * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.133 2006/06/14 16:49:02 tgl Exp $
P
Peter Eisentraut 已提交
7
 */
8
#include "postgres_fe.h"
9 10

#include <sys/types.h>
11 12 13
#ifdef USE_SSL
#include <openssl/ssl.h>
#endif
14

15 16
#ifndef WIN32
#include <unistd.h>
B
Bruce Momjian 已提交
17
#else							/* WIN32 */
18
#include <io.h>
19
#include <win32.h>
20
#endif   /* WIN32 */
21

22
#include "getopt_long.h"
23

24
#ifndef HAVE_INT_OPTRESET
B
Bruce Momjian 已提交
25
int			optreset;
26 27
#endif

P
Peter Eisentraut 已提交
28 29
#include <locale.h>

30
#include "libpq-fe.h"
31 32 33

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

42 43
#include "mb/pg_wchar.h"

44

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

50
#ifndef WIN32
51
#define SYSPSQLRC	"psqlrc"
B
Bruce Momjian 已提交
52
#define PSQLRC		".psqlrc"
53 54
#else
#define SYSPSQLRC	"psqlrc"
55
#define PSQLRC		"psqlrc.conf"
56
#endif
57

58 59
/*
 * Structures to pass information between the option parsing routine
60 61
 * and the main function
 */
B
Bruce Momjian 已提交
62 63
enum _actions
{
64 65 66 67 68
	ACT_NOTHING = 0,
	ACT_SINGLE_SLASH,
	ACT_LIST_DB,
	ACT_SINGLE_QUERY,
	ACT_FILE
69 70
};

B
Bruce Momjian 已提交
71 72 73 74 75 76
struct adhoc_opts
{
	char	   *dbname;
	char	   *host;
	char	   *port;
	char	   *username;
77
	char	   *logfilename;
B
Bruce Momjian 已提交
78 79 80
	enum _actions action;
	char	   *action_string;
	bool		no_readline;
81
	bool		no_psqlrc;
B
Bruce Momjian 已提交
82
    bool        single_txn;
83 84
};

85
static int	parse_version(const char *versionString);
86
static void parse_psql_options(int argc, char *argv[],
B
Bruce Momjian 已提交
87
				   struct adhoc_opts * options);
88
static void process_psqlrc(char *argv0);
89
static void process_psqlrc_file(char *filename);
90
static void showVersion(void);
91

B
Bruce Momjian 已提交
92
#ifdef USE_SSL
93
static void printSSLInfo(void);
B
Bruce Momjian 已提交
94
#endif
95

B
> >  
Bruce Momjian 已提交
96 97 98 99
#ifdef WIN32
static void
			checkWin32Codepage(void);
#endif
100 101 102

/*
 *
103
 * main
104 105 106
 *
 */
int
107
main(int argc, char *argv[])
108
{
B
Bruce Momjian 已提交
109 110
	struct adhoc_opts options;
	int			successResult;
111

B
Bruce Momjian 已提交
112 113
	char	   *username = NULL;
	char	   *password = NULL;
B
Bruce Momjian 已提交
114
	char	   *password_prompt = NULL;
B
Bruce Momjian 已提交
115
	bool		need_pass;
116

117
	set_pglocale_pgservice(argv[0], "psql");
P
Peter Eisentraut 已提交
118

119 120
	if (argc > 1)
	{
B
Bruce Momjian 已提交
121
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
122 123 124 125
		{
			usage();
			exit(EXIT_SUCCESS);
		}
B
Bruce Momjian 已提交
126
		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
127 128 129 130
		{
			showVersion();
			exit(EXIT_SUCCESS);
		}
B
Bruce Momjian 已提交
131
	}
132

133 134
	pset.progname = get_progname(argv[0]);

135
#ifdef WIN32
B
Bruce Momjian 已提交
136
	setvbuf(stderr, NULL, _IONBF, 0);
137
#endif
138
	setDecimalLocale();
139 140
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
141
	pset.encoding = PQenv2encoding();
142

143
	pset.vars = CreateVariableSpace();
144 145
	if (!pset.vars)
	{
146
		fprintf(stderr, _("%s: out of memory\n"), pset.progname);
147 148
		exit(EXIT_FAILURE);
	}
149 150 151
	pset.popt.topt.format = PRINT_ALIGNED;
	pset.queryFout = stdout;
	pset.popt.topt.border = 1;
152
	pset.popt.topt.pager = 1;
153
	pset.popt.default_footer = true;
154

155
	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
156

157
	/* Default values for variables */
158
	SetVariableBool(pset.vars, "AUTOCOMMIT");
159
	SetVariable(pset.vars, "VERBOSITY", "default");
160 161 162 163
	SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
	SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
	SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);

164
	pset.verbosity = PQERRORS_DEFAULT;
165

166
	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
167

168
	/* This is obsolete and should be removed sometime. */
169
#ifdef PSQL_ALWAYS_GET_PASSWORDS
170
	pset.getPassword = true;
171
#else
172
	pset.getPassword = false;
173 174
#endif

175
	parse_psql_options(argc, argv, &options);
176

177
	if (!pset.popt.topt.fieldSep)
178
		pset.popt.topt.fieldSep = pg_strdup(DEFAULT_FIELD_SEP);
179
	if (!pset.popt.topt.recordSep)
180
		pset.popt.topt.recordSep = pg_strdup(DEFAULT_RECORD_SEP);
181

B
Bruce Momjian 已提交
182 183
	if (options.username)
	{
184
		/*
B
Bruce Momjian 已提交
185 186 187
		 * 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.
188
		 */
189
		if (strcmp(options.username, "\001") == 0)
190
			username = simple_prompt("User name: ", 100, true);
B
Bruce Momjian 已提交
191
		else
192
			username = pg_strdup(options.username);
193 194
	}

195
	if (options.username == NULL)
196
		password_prompt = pg_strdup(_("Password: "));
197 198
	else
	{
199
		password_prompt = malloc(strlen(_("Password for user %s: ")) - 2 +
200
								 strlen(options.username) + 1);
201
		sprintf(password_prompt, _("Password for user %s: "), options.username);
202
	}
203

204
	if (pset.getPassword)
205
		password = simple_prompt(password_prompt, 100, false);
206

B
Bruce Momjian 已提交
207 208 209 210
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
211
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
212
					options.action == ACT_LIST_DB && options.dbname == NULL ?
A
 
Andrew Dunstan 已提交
213
							   "postgres" : options.dbname,
214
							   username, password);
B
Bruce Momjian 已提交
215

216
		if (PQstatus(pset.db) == CONNECTION_BAD &&
217
			strcmp(PQerrorMessage(pset.db), PQnoPasswordSupplied) == 0 &&
218
			!feof(stdin))
B
Bruce Momjian 已提交
219
		{
220
			PQfinish(pset.db);
B
Bruce Momjian 已提交
221 222 223
			need_pass = true;
			free(password);
			password = NULL;
224
			password = simple_prompt(password_prompt, 100, false);
B
Bruce Momjian 已提交
225 226
		}
	} while (need_pass);
227

B
Bruce Momjian 已提交
228 229
	free(username);
	free(password);
230
	free(password_prompt);
B
Bruce Momjian 已提交
231

232
	if (PQstatus(pset.db) == CONNECTION_BAD)
B
Bruce Momjian 已提交
233
	{
234
		fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
235
		PQfinish(pset.db);
B
Bruce Momjian 已提交
236 237 238
		exit(EXIT_BADCONN);
	}

239 240
	PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);

241
	SyncVariables();
P
Peter Eisentraut 已提交
242

243 244 245
	/* Grab the backend server version */
	pset.sversion = PQserverVersion(pset.db);

B
Bruce Momjian 已提交
246 247
	if (options.action == ACT_LIST_DB)
	{
248
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
249

250
		PQfinish(pset.db);
251
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
252 253
	}

254 255 256 257
	if (options.logfilename)
	{
		pset.logfile = fopen(options.logfilename, "a");
		if (!pset.logfile)
258 259
			fprintf(stderr, _("%s: could not open log file \"%s\": %s\n"),
					pset.progname, options.logfilename, strerror(errno));
260 261
	}

262
	/*
263 264
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
265

266
	/*
267 268
	 * process file given by -f
	 */
269
	if (options.action == ACT_FILE && strcmp(options.action_string, "-") != 0)
270 271
	{
		if (!options.no_psqlrc)
272
			process_psqlrc(argv[0]);
273

B
Bruce Momjian 已提交
274
		successResult = process_file(options.action_string, options.single_txn);
275 276
	}

277
	/*
278 279
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
280
	else if (options.action == ACT_SINGLE_SLASH)
281
	{
282 283
		PsqlScanState scan_state;

284
		if (VariableEquals(pset.vars, "ECHO", "all"))
285
			puts(options.action_string);
286

287 288 289 290 291
		scan_state = psql_scan_create();
		psql_scan_setup(scan_state,
						options.action_string,
						strlen(options.action_string));

292
		successResult = HandleSlashCmds(scan_state, NULL) != PSQL_CMD_ERROR
293
			? EXIT_SUCCESS : EXIT_FAILURE;
294 295

		psql_scan_destroy(scan_state);
296 297
	}

298
	/*
299 300
	 * If the query given to -c was a normal one, send it
	 */
B
Bruce Momjian 已提交
301
	else if (options.action == ACT_SINGLE_QUERY)
302
	{
303
		if (VariableEquals(pset.vars, "ECHO", "all"))
304
			puts(options.action_string);
305

306
		successResult = SendQuery(options.action_string)
307 308 309
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

310
	/*
311 312
	 * or otherwise enter interactive main loop
	 */
B
Bruce Momjian 已提交
313
	else
314
	{
315 316 317
		if (!options.no_psqlrc)
			process_psqlrc(argv[0]);

318 319
		if (!QUIET() && !pset.notty)
		{
B
Bruce Momjian 已提交
320
			int			client_ver = parse_version(PG_VERSION);
321

322
			if (pset.sversion != client_ver)
323
			{
324
				const char *server_version;
B
Bruce Momjian 已提交
325
				char		server_ver_str[16];
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340
				/* Try to get full text form, might include "devel" etc */
				server_version = PQparameterStatus(pset.db, "server_version");
				if (!server_version)
				{
					snprintf(server_ver_str, sizeof(server_ver_str),
							 "%d.%d.%d",
							 pset.sversion / 10000,
							 (pset.sversion / 100) % 100,
							 pset.sversion % 100);
					server_version = server_ver_str;
				}

				printf(_("Welcome to %s %s (server %s), the PostgreSQL interactive terminal.\n\n"),
					   pset.progname, PG_VERSION, server_version);
341 342 343
			}
			else
				printf(_("Welcome to %s %s, the PostgreSQL interactive terminal.\n\n"),
344
					   pset.progname, PG_VERSION);
345 346

			printf(_("Type:  \\copyright for distribution terms\n"
B
Bruce Momjian 已提交
347 348 349 350
					 "       \\h for help with SQL commands\n"
					 "       \\? for help with psql commands\n"
				  "       \\g or terminate with semicolon to execute query\n"
					 "       \\q to quit\n\n"));
351 352 353 354 355

			if (pset.sversion / 100 != client_ver / 100)
				printf(_("WARNING:  You are connected to a server with major version %d.%d,\n"
						 "but your %s client is major version %d.%d.  Some backslash commands,\n"
						 "such as \\d, might not work properly.\n\n"),
B
Bruce Momjian 已提交
356 357 358
					   pset.sversion / 10000, (pset.sversion / 100) % 100,
					   pset.progname,
					   client_ver / 10000, (client_ver / 100) % 100);
359

B
Bruce Momjian 已提交
360
#ifdef USE_SSL
361
			printSSLInfo();
B
> >  
Bruce Momjian 已提交
362 363 364
#endif
#ifdef WIN32
			checkWin32Codepage();
B
Bruce Momjian 已提交
365
#endif
366 367
		}

368 369
		if (!pset.notty)
			initializeInput(options.no_readline ? 0 : 1);
370
		if (options.action_string)		/* -f - was used */
371
			pset.inputfile = "<stdin>";
372

373 374 375
		/* establish control-C handling for interactive operation */
		setup_cancel_handler();

P
Peter Eisentraut 已提交
376
		successResult = MainLoop(stdin);
377
	}
B
Bruce Momjian 已提交
378 379

	/* clean up */
380 381
	if (pset.logfile)
		fclose(pset.logfile);
382 383
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
384 385

	return successResult;
386 387 388
}


389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/*
 * Convert a version string into a number.
 */
static int
parse_version(const char *versionString)
{
	int			cnt;
	int			vmaj,
				vmin,
				vrev;

	cnt = sscanf(versionString, "%d.%d.%d", &vmaj, &vmin, &vrev);

	if (cnt < 2)
		return -1;

	if (cnt == 2)
		vrev = 0;

	return (100 * vmaj + vmin) * 100 + vrev;
}

411 412 413 414 415 416

/*
 * Parse command line options
 */

static void
417
parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
418
{
419
	static struct option long_options[] =
420 421
	{
		{"echo-all", no_argument, NULL, 'a'},
B
Bruce Momjian 已提交
422 423 424
		{"no-align", no_argument, NULL, 'A'},
		{"command", required_argument, NULL, 'c'},
		{"dbname", required_argument, NULL, 'd'},
425
		{"echo-queries", no_argument, NULL, 'e'},
426
		{"echo-hidden", no_argument, NULL, 'E'},
B
Bruce Momjian 已提交
427
		{"file", required_argument, NULL, 'f'},
428
		{"field-separator", required_argument, NULL, 'F'},
B
Bruce Momjian 已提交
429 430 431
		{"host", required_argument, NULL, 'h'},
		{"html", no_argument, NULL, 'H'},
		{"list", no_argument, NULL, 'l'},
432
		{"log-file", required_argument, NULL, 'L'},
433
		{"no-readline", no_argument, NULL, 'n'},
B
Bruce Momjian 已提交
434
		{"single-transaction", no_argument, NULL, '1'},
435
		{"output", required_argument, NULL, 'o'},
B
Bruce Momjian 已提交
436 437 438
		{"port", required_argument, NULL, 'p'},
		{"pset", required_argument, NULL, 'P'},
		{"quiet", no_argument, NULL, 'q'},
439
		{"record-separator", required_argument, NULL, 'R'},
B
Bruce Momjian 已提交
440 441 442 443 444 445 446 447 448
		{"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'},
449
		{"expanded", no_argument, NULL, 'x'},
450
		{"no-psqlrc", no_argument, NULL, 'X'},
B
Bruce Momjian 已提交
451
		{"help", no_argument, NULL, '?'},
T
Tatsuo Ishii 已提交
452
		{NULL, 0, NULL, 0}
B
Bruce Momjian 已提交
453 454 455 456 457 458
	};

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
459
	bool		used_old_u_option = false;
460

461
	memset(options, 0, sizeof *options);
462

B
Bruce Momjian 已提交
463
	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:uU:v:VWxX?1",
464
							long_options, &optindex)) != -1)
465
	{
B
Bruce Momjian 已提交
466 467
		switch (c)
		{
468 469 470
			case 'a':
				SetVariable(pset.vars, "ECHO", "all");
				break;
B
Bruce Momjian 已提交
471
			case 'A':
472
				pset.popt.topt.format = PRINT_UNALIGNED;
B
Bruce Momjian 已提交
473 474 475 476
				break;
			case 'c':
				options->action_string = optarg;
				if (optarg[0] == '\\')
P
Peter Eisentraut 已提交
477
				{
B
Bruce Momjian 已提交
478
					options->action = ACT_SINGLE_SLASH;
P
Peter Eisentraut 已提交
479 480
					options->action_string++;
				}
B
Bruce Momjian 已提交
481 482 483 484 485 486 487
				else
					options->action = ACT_SINGLE_QUERY;
				break;
			case 'd':
				options->dbname = optarg;
				break;
			case 'e':
488
				SetVariable(pset.vars, "ECHO", "queries");
B
Bruce Momjian 已提交
489 490
				break;
			case 'E':
491
				SetVariableBool(pset.vars, "ECHO_HIDDEN");
B
Bruce Momjian 已提交
492 493 494 495 496 497
				break;
			case 'f':
				options->action = ACT_FILE;
				options->action_string = optarg;
				break;
			case 'F':
498
				pset.popt.topt.fieldSep = pg_strdup(optarg);
B
Bruce Momjian 已提交
499 500 501 502 503
				break;
			case 'h':
				options->host = optarg;
				break;
			case 'H':
504
				pset.popt.topt.format = PRINT_HTML;
B
Bruce Momjian 已提交
505 506 507 508
				break;
			case 'l':
				options->action = ACT_LIST_DB;
				break;
509 510 511
			case 'L':
				options->logfilename = optarg;
				break;
B
Bruce Momjian 已提交
512 513 514 515
			case 'n':
				options->no_readline = true;
				break;
			case 'o':
516
				setQFout(optarg);
B
Bruce Momjian 已提交
517 518 519 520 521 522 523 524 525 526
				break;
			case 'p':
				options->port = optarg;
				break;
			case 'P':
				{
					char	   *value;
					char	   *equal_loc;
					bool		result;

527
					value = pg_strdup(optarg);
B
Bruce Momjian 已提交
528 529
					equal_loc = strchr(value, '=');
					if (!equal_loc)
530
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
531 532 533
					else
					{
						*equal_loc = '\0';
534
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
535 536 537 538
					}

					if (!result)
					{
539
						fprintf(stderr, _("%s: could not set printing parameter \"%s\"\n"), pset.progname, value);
B
Bruce Momjian 已提交
540 541 542 543 544 545 546
						exit(EXIT_FAILURE);
					}

					free(value);
					break;
				}
			case 'q':
547
				SetVariableBool(pset.vars, "QUIET");
B
Bruce Momjian 已提交
548
				break;
549
			case 'R':
550
				pset.popt.topt.recordSep = pg_strdup(optarg);
551
				break;
B
Bruce Momjian 已提交
552
			case 's':
553
				SetVariableBool(pset.vars, "SINGLESTEP");
B
Bruce Momjian 已提交
554 555
				break;
			case 'S':
556
				SetVariableBool(pset.vars, "SINGLELINE");
B
Bruce Momjian 已提交
557 558
				break;
			case 't':
559
				pset.popt.topt.tuples_only = true;
B
Bruce Momjian 已提交
560 561
				break;
			case 'T':
562
				pset.popt.topt.tableAttr = pg_strdup(optarg);
B
Bruce Momjian 已提交
563 564
				break;
			case 'u':
565
				pset.getPassword = true;
B
Bruce Momjian 已提交
566 567
				options->username = "\001";		/* hopefully nobody has that
												 * username */
568 569
				/* this option is out */
				used_old_u_option = true;
B
Bruce Momjian 已提交
570 571 572 573 574 575 576 577 578
				break;
			case 'U':
				options->username = optarg;
				break;
			case 'v':
				{
					char	   *value;
					char	   *equal_loc;

579
					value = pg_strdup(optarg);
B
Bruce Momjian 已提交
580 581 582
					equal_loc = strchr(value, '=');
					if (!equal_loc)
					{
583
						if (!DeleteVariable(pset.vars, value))
B
Bruce Momjian 已提交
584
						{
585
							fprintf(stderr, _("%s: could not delete variable \"%s\"\n"),
586
									pset.progname, value);
B
Bruce Momjian 已提交
587 588 589 590 591 592
							exit(EXIT_FAILURE);
						}
					}
					else
					{
						*equal_loc = '\0';
593
						if (!SetVariable(pset.vars, value, equal_loc + 1))
B
Bruce Momjian 已提交
594
						{
595
							fprintf(stderr, _("%s: could not set variable \"%s\"\n"),
596
									pset.progname, value);
B
Bruce Momjian 已提交
597 598 599 600 601 602 603 604
							exit(EXIT_FAILURE);
						}
					}

					free(value);
					break;
				}
			case 'V':
605 606
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
607
			case 'W':
608
				pset.getPassword = true;
B
Bruce Momjian 已提交
609
				break;
610 611 612
			case 'x':
				pset.popt.topt.expanded = true;
				break;
613 614 615
			case 'X':
				options->no_psqlrc = true;
				break;
B
Bruce Momjian 已提交
616 617 618
			case '1':
				options->single_txn = true;
				break;
B
Bruce Momjian 已提交
619
			case '?':
620 621 622 623 624 625 626 627 628
				/* 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
				{
629
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
630
							pset.progname);
631
					exit(EXIT_FAILURE);
632
				}
B
Bruce Momjian 已提交
633 634
				break;
			default:
635
				fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
636
						pset.progname);
637
				exit(EXIT_FAILURE);
B
Bruce Momjian 已提交
638 639
				break;
		}
640 641
	}

B
Bruce Momjian 已提交
642
	/*
B
Bruce Momjian 已提交
643
	 * if we still have arguments, use it as the database name and username
B
Bruce Momjian 已提交
644 645 646 647 648 649 650
	 */
	while (argc - optind >= 1)
	{
		if (!options->dbname)
			options->dbname = argv[optind];
		else if (!options->username)
			options->username = argv[optind];
651
		else if (!QUIET())
652
			fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"),
653
					pset.progname, argv[optind]);
B
Bruce Momjian 已提交
654 655 656

		optind++;
	}
657

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

661 662 663 664
}


/*
P
Peter Eisentraut 已提交
665
 * Load .psqlrc file, if found.
666 667
 */
static void
668
process_psqlrc(char *argv0)
669
{
B
Bruce Momjian 已提交
670
	char		home[MAXPGPATH];
671
	char		rc_file[MAXPGPATH];
B
Bruce Momjian 已提交
672 673
	char		my_exec_path[MAXPGPATH];
	char		etc_path[MAXPGPATH];
674 675 676

	find_my_exec(argv0, my_exec_path);
	get_etc_path(my_exec_path, etc_path);
677

678 679
	snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
	process_psqlrc_file(rc_file);
680

681
	if (get_home_path(home))
682
	{
683 684
		snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
		process_psqlrc_file(rc_file);
685 686 687 688 689 690 691 692 693
	}
}



static void
process_psqlrc_file(char *filename)
{
	char	   *psqlrc;
694

695 696
#if defined(WIN32) && (!defined(__MINGW32__))
#define R_OK 4
697 698
#endif

699 700
	psqlrc = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1);
	sprintf(psqlrc, "%s-%s", filename, PG_VERSION);
701

702
	if (access(psqlrc, R_OK) == 0)
B
Bruce Momjian 已提交
703
		(void) process_file(psqlrc, false);
704
	else if (access(filename, R_OK) == 0)
B
Bruce Momjian 已提交
705
		(void) process_file(filename, false);
706
	free(psqlrc);
707 708 709 710 711 712
}



/* showVersion
 *
713
 * This output format is intended to match GNU standards.
714 715
 */
static void
716
showVersion(void)
717
{
718
	puts("psql (PostgreSQL) " PG_VERSION);
719

720
#if defined(USE_READLINE)
721
	puts(_("contains support for command-line editing"));
722
#endif
723
}
B
Bruce Momjian 已提交
724 725 726 727 728 729 730 731 732



/*
 * printSSLInfo
 *
 * Prints information about the current SSL connection, if SSL is in use
 */
#ifdef USE_SSL
B
Bruce Momjian 已提交
733
static void
B
Bruce Momjian 已提交
734 735
printSSLInfo(void)
{
B
Bruce Momjian 已提交
736 737
	int			sslbits = -1;
	SSL		   *ssl;
B
Bruce Momjian 已提交
738 739 740

	ssl = PQgetssl(pset.db);
	if (!ssl)
B
Bruce Momjian 已提交
741
		return;					/* no SSL */
B
Bruce Momjian 已提交
742 743

	SSL_get_cipher_bits(ssl, &sslbits);
744
	printf(_("SSL connection (cipher: %s, bits: %i)\n\n"),
B
Bruce Momjian 已提交
745
		   SSL_get_cipher(ssl), sslbits);
B
Bruce Momjian 已提交
746 747
}
#endif
B
> >  
Bruce Momjian 已提交
748 749 750 751 752 753 754 755 756 757 758 759



/*
 * checkWin32Codepage
 *
 * Prints a warning when win32 console codepage differs from Windows codepage
 */
#ifdef WIN32
static void
checkWin32Codepage(void)
{
B
Bruce Momjian 已提交
760 761
	unsigned int wincp,
				concp;
B
> >  
Bruce Momjian 已提交
762 763 764

	wincp = GetACP();
	concp = GetConsoleCP();
B
Bruce Momjian 已提交
765 766
	if (wincp != concp)
	{
B
Bruce Momjian 已提交
767 768 769
		printf(_("Warning: Console code page (%u) differs from Windows code page (%u)\n"
				 "         8-bit characters may not work correctly. See psql reference\n"
			   "         page \"Notes for Windows users\" for details.\n\n"),
B
Bruce Momjian 已提交
770
			   concp, wincp);
B
> >  
Bruce Momjian 已提交
771 772
	}
}
B
Bruce Momjian 已提交
773

B
> >  
Bruce Momjian 已提交
774
#endif