startup.c 16.6 KB
Newer Older
P
Peter Eisentraut 已提交
1 2 3
/*
 * psql - the PostgreSQL interactive terminal
 *
4
 * Copyright (c) 2000-2005, PostgreSQL Global Development Group
P
Peter Eisentraut 已提交
5
 *
6
 * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.123 2005/09/20 18:59:02 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>
16
#include <win32.h>
17
#endif   /* WIN32 */
18

19
#include "getopt_long.h"
20

21
#ifndef HAVE_INT_OPTRESET
22
int optreset;
23 24
#endif

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

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

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

39 40
#include "mb/pg_wchar.h"

41

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

47
#ifndef WIN32
48
#define SYSPSQLRC	"psqlrc"
B
Bruce Momjian 已提交
49
#define PSQLRC		".psqlrc"
50 51
#else
#define SYSPSQLRC	"psqlrc"
52
#define PSQLRC		"psqlrc.conf"
53
#endif
54

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

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

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

B
Bruce Momjian 已提交
88
#ifdef USE_SSL
89
static void printSSLInfo(void);
B
Bruce Momjian 已提交
90
#endif
91

B
> >  
Bruce Momjian 已提交
92 93 94 95
#ifdef WIN32
static void
			checkWin32Codepage(void);
#endif
96 97 98

/*
 *
99
 * main
100 101 102
 *
 */
int
103
main(int argc, char *argv[])
104
{
B
Bruce Momjian 已提交
105 106
	struct adhoc_opts options;
	int			successResult;
107

B
Bruce Momjian 已提交
108 109
	char	   *username = NULL;
	char	   *password = NULL;
110
	char       *password_prompt = NULL;
B
Bruce Momjian 已提交
111
	bool		need_pass;
112

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

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

129 130
	pset.progname = get_progname(argv[0]);

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

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

153
	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
154

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

162
	pset.verbosity = PQERRORS_DEFAULT;
163

164
	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
165

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

173
	parse_psql_options(argc, argv, &options);
174

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

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

193 194 195 196 197 198 199 200 201
	if (options.username == NULL)
		password_prompt = strdup("Password: ");
	else
	{
		password_prompt = malloc(strlen("Password for user %s: ") - 2 +
								 strlen(options.username) + 1);
		sprintf(password_prompt,"Password for user %s: ", options.username);
	}
	
202
	if (pset.getPassword)
203
		password = simple_prompt(password_prompt, 100, false);
204

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

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

B
Bruce Momjian 已提交
225 226
	free(username);
	free(password);
227
	free(password_prompt);
B
Bruce Momjian 已提交
228

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

236 237
	PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);

238
	SyncVariables();
P
Peter Eisentraut 已提交
239

240 241 242
	/* Grab the backend server version */
	pset.sversion = PQserverVersion(pset.db);

B
Bruce Momjian 已提交
243 244
	if (options.action == ACT_LIST_DB)
	{
245
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
246

247
		PQfinish(pset.db);
248
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
249 250
	}

251 252 253 254 255 256 257
	if (options.logfilename)
	{
		pset.logfile = fopen(options.logfilename, "a");
		if (!pset.logfile)
			fprintf(stderr, gettext("logfile open failed for %s\n\n"), options.logfilename);
	}

258
	/*
259 260
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
261

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

270
		successResult = process_file(options.action_string);
271 272
	}

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

280
		if (VariableEquals(pset.vars, "ECHO", "all"))
281
			puts(options.action_string);
282

283 284 285 286 287 288
		scan_state = psql_scan_create();
		psql_scan_setup(scan_state,
						options.action_string,
						strlen(options.action_string));

		successResult = HandleSlashCmds(scan_state, NULL) != CMD_ERROR
289
			? EXIT_SUCCESS : EXIT_FAILURE;
290 291

		psql_scan_destroy(scan_state);
292 293
	}

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

302
		successResult = SendQuery(options.action_string)
303 304 305
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

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

314 315
		if (!QUIET() && !pset.notty)
		{
316
			int		client_ver = parse_version(PG_VERSION);
317

318
			if (pset.sversion != client_ver)
319
			{
320
				const char *server_version;
321 322
				char	server_ver_str[16];

323 324 325 326 327 328 329 330 331 332 333 334 335 336
				/* 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);
337 338 339
			}
			else
				printf(_("Welcome to %s %s, the PostgreSQL interactive terminal.\n\n"),
340
					   pset.progname, PG_VERSION);
341 342

			printf(_("Type:  \\copyright for distribution terms\n"
P
Peter Eisentraut 已提交
343
						   "       \\h for help with SQL commands\n"
344
						   "       \\? for help with psql commands\n"
B
Bruce Momjian 已提交
345
			  "       \\g or terminate with semicolon to execute query\n"
346
						   "       \\q to quit\n\n"));
347 348 349 350 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"),
						 pset.sversion / 10000, (pset.sversion / 100) % 100,
						 pset.progname,
						 client_ver / 10000, (client_ver / 100) % 100);

B
Bruce Momjian 已提交
356
#ifdef USE_SSL
357
			printSSLInfo();
B
> >  
Bruce Momjian 已提交
358 359 360
#endif
#ifdef WIN32
			checkWin32Codepage();
B
Bruce Momjian 已提交
361
#endif
362 363
		}

364 365
		if (!pset.notty)
			initializeInput(options.no_readline ? 0 : 1);
366
		if (options.action_string)		/* -f - was used */
367
			pset.inputfile = "<stdin>";
368

P
Peter Eisentraut 已提交
369
		successResult = MainLoop(stdin);
370
	}
B
Bruce Momjian 已提交
371 372

	/* clean up */
373 374
	if (pset.logfile)
		fclose(pset.logfile);
375 376
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
377 378

	return successResult;
379 380 381
}


382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
/*
 * 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;
}

404 405 406 407 408 409

/*
 * Parse command line options
 */

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

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
451
	bool		used_old_u_option = false;
452

453
	memset(options, 0, sizeof *options);
454

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

519
					value = pg_strdup(optarg);
B
Bruce Momjian 已提交
520 521
					equal_loc = strchr(value, '=');
					if (!equal_loc)
522
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
523 524 525
					else
					{
						*equal_loc = '\0';
526
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
527 528 529 530
					}

					if (!result)
					{
531
						fprintf(stderr, _("%s: couldn't set printing parameter \"%s\"\n"), pset.progname, value);
B
Bruce Momjian 已提交
532 533 534 535 536 537 538
						exit(EXIT_FAILURE);
					}

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

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

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

B
Bruce Momjian 已提交
631 632 633 634 635 636 637 638 639 640
	/*
	 * 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];
641
		else if (!QUIET())
642
			fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"),
643
					pset.progname, argv[optind]);
B
Bruce Momjian 已提交
644 645 646

		optind++;
	}
647

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

651 652 653 654
}


/*
P
Peter Eisentraut 已提交
655
 * Load .psqlrc file, if found.
656 657
 */
static void
658
process_psqlrc(char *argv0)
659
{
B
Bruce Momjian 已提交
660
	char		home[MAXPGPATH];
661
	char		rc_file[MAXPGPATH];
B
Bruce Momjian 已提交
662 663
	char		my_exec_path[MAXPGPATH];
	char		etc_path[MAXPGPATH];
664 665 666

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

668 669
	snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
	process_psqlrc_file(rc_file);
670

671
	if (get_home_path(home))
672
	{
673 674
		snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
		process_psqlrc_file(rc_file);
675 676 677 678 679 680 681 682 683
	}
}



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

685 686
#if defined(WIN32) && (!defined(__MINGW32__))
#define R_OK 4
687 688
#endif

689 690
	psqlrc = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1);
	sprintf(psqlrc, "%s-%s", filename, PG_VERSION);
691

692
	if (access(psqlrc, R_OK) == 0)
693
		(void)process_file(psqlrc);
694
	else if (access(filename, R_OK) == 0)
695
		(void)process_file(filename);
696
	free(psqlrc);
697 698 699 700 701 702
}



/* showVersion
 *
703
 * This output format is intended to match GNU standards.
704 705
 */
static void
706
showVersion(void)
707
{
708
	puts("psql (PostgreSQL) " PG_VERSION);
709

710
#if defined(USE_READLINE)
711
	puts(_("contains support for command-line editing"));
712
#endif
713
}
B
Bruce Momjian 已提交
714 715 716 717 718 719 720 721 722



/*
 * printSSLInfo
 *
 * Prints information about the current SSL connection, if SSL is in use
 */
#ifdef USE_SSL
B
Bruce Momjian 已提交
723
static void
B
Bruce Momjian 已提交
724 725
printSSLInfo(void)
{
B
Bruce Momjian 已提交
726 727
	int			sslbits = -1;
	SSL		   *ssl;
B
Bruce Momjian 已提交
728 729 730

	ssl = PQgetssl(pset.db);
	if (!ssl)
B
Bruce Momjian 已提交
731
		return;					/* no SSL */
B
Bruce Momjian 已提交
732 733

	SSL_get_cipher_bits(ssl, &sslbits);
734
	printf(_("SSL connection (cipher: %s, bits: %i)\n\n"),
B
Bruce Momjian 已提交
735
		   SSL_get_cipher(ssl), sslbits);
B
Bruce Momjian 已提交
736 737
}
#endif
B
> >  
Bruce Momjian 已提交
738 739 740 741 742 743 744 745 746 747 748 749



/*
 * checkWin32Codepage
 *
 * Prints a warning when win32 console codepage differs from Windows codepage
 */
#ifdef WIN32
static void
checkWin32Codepage(void)
{
B
Bruce Momjian 已提交
750 751
	unsigned int wincp,
				concp;
B
> >  
Bruce Momjian 已提交
752 753 754

	wincp = GetACP();
	concp = GetConsoleCP();
B
Bruce Momjian 已提交
755 756
	if (wincp != concp)
	{
757
			printf(_("Warning: Console code page (%u) differs from Windows code page (%u)\n"
758 759
					   "         8-bit characters may not work correctly. See psql reference\n"
					   "         page \"Notes for Windows users\" for details.\n\n"),
B
Bruce Momjian 已提交
760
			   concp, wincp);
B
> >  
Bruce Momjian 已提交
761 762
	}
}
B
Bruce Momjian 已提交
763

B
> >  
Bruce Momjian 已提交
764
#endif