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.128 2005/11/22 18:17:29 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
B
Bruce Momjian 已提交
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;
B
Bruce Momjian 已提交
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.default_footer = true;
151

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

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

161
	pset.verbosity = PQERRORS_DEFAULT;
162

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

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

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

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

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

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

201
	if (pset.getPassword)
202
		password = simple_prompt(password_prompt, 100, false);
203

B
Bruce Momjian 已提交
204 205 206 207
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
208
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
209
					options.action == ACT_LIST_DB && options.dbname == NULL ?
A
 
Andrew Dunstan 已提交
210
							   "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
	if (options.logfilename)
	{
		pset.logfile = fopen(options.logfilename, "a");
		if (!pset.logfile)
255 256
			fprintf(stderr, _("%s: could not open log file \"%s\": %s\n"),
					pset.progname, options.logfilename, strerror(errno));
257 258
	}

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

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

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

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

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

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

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

		psql_scan_destroy(scan_state);
293 294
	}

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

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

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

315 316
		if (!QUIET() && !pset.notty)
		{
B
Bruce Momjian 已提交
317
			int			client_ver = parse_version(PG_VERSION);
318

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

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

			printf(_("Type:  \\copyright for distribution terms\n"
B
Bruce Momjian 已提交
344 345 346 347
					 "       \\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"));
348 349 350 351 352

			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 已提交
353 354 355
					   pset.sversion / 10000, (pset.sversion / 100) % 100,
					   pset.progname,
					   client_ver / 10000, (client_ver / 100) % 100);
356

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

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

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

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

	return successResult;
380 381 382
}


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

405 406 407 408 409 410

/*
 * Parse command line options
 */

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

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

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

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

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

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

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

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

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

B
Bruce Momjian 已提交
632
	/*
B
Bruce Momjian 已提交
633
	 * if we still have arguments, use it as the database name and username
B
Bruce Momjian 已提交
634 635 636 637 638 639 640
	 */
	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)
B
Bruce Momjian 已提交
693
		(void) process_file(psqlrc);
694
	else if (access(filename, R_OK) == 0)
B
Bruce Momjian 已提交
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)
	{
B
Bruce Momjian 已提交
757 758 759
		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 已提交
760
			   concp, wincp);
B
> >  
Bruce Momjian 已提交
761 762
	}
}
B
Bruce Momjian 已提交
763

B
> >  
Bruce Momjian 已提交
764
#endif