startup.c 16.8 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.135 2006/07/14 14:52:26 momjian 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 31 32

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

39

40

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

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

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

B
Bruce Momjian 已提交
67 68 69 70 71 72
struct adhoc_opts
{
	char	   *dbname;
	char	   *host;
	char	   *port;
	char	   *username;
73
	char	   *logfilename;
B
Bruce Momjian 已提交
74 75 76
	enum _actions action;
	char	   *action_string;
	bool		no_readline;
77
	bool		no_psqlrc;
B
Bruce Momjian 已提交
78
    bool        single_txn;
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
#ifdef WIN32
B
Bruce Momjian 已提交
130
	setvbuf(stderr, NULL, _IONBF, 0);
131
#endif
132 133 134 135 136

	setup_cancel_handler();

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

137
	setDecimalLocale();
138 139
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
140
	pset.encoding = PQenv2encoding();
141

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

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

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

163
	pset.verbosity = PQERRORS_DEFAULT;
164

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		psql_scan_destroy(scan_state);
295 296
	}

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

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

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

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

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

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

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

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

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

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

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

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

	return successResult;
382 383 384
}


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

407 408 409 410 411 412

/*
 * Parse command line options
 */

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

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
455
	bool		used_old_u_option = false;
456

457
	memset(options, 0, sizeof *options);
458

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

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

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

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

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

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

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

		optind++;
	}
653

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

657 658 659 660
}


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

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

674 675
	snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
	process_psqlrc_file(rc_file);
676

677
	if (get_home_path(home))
678
	{
679 680
		snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
		process_psqlrc_file(rc_file);
681 682 683 684 685 686 687 688 689
	}
}



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

691 692
#if defined(WIN32) && (!defined(__MINGW32__))
#define R_OK 4
693 694
#endif

695 696
	psqlrc = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1);
	sprintf(psqlrc, "%s-%s", filename, PG_VERSION);
697

698
	if (access(psqlrc, R_OK) == 0)
B
Bruce Momjian 已提交
699
		(void) process_file(psqlrc, false);
700
	else if (access(filename, R_OK) == 0)
B
Bruce Momjian 已提交
701
		(void) process_file(filename, false);
702
	free(psqlrc);
703 704 705 706 707 708
}



/* showVersion
 *
709
 * This output format is intended to match GNU standards.
710 711
 */
static void
712
showVersion(void)
713
{
714
	puts("psql (PostgreSQL) " PG_VERSION);
715

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



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

	ssl = PQgetssl(pset.db);
	if (!ssl)
B
Bruce Momjian 已提交
737
		return;					/* no SSL */
B
Bruce Momjian 已提交
738 739

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



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

	wincp = GetACP();
	concp = GetConsoleCP();
B
Bruce Momjian 已提交
761 762
	if (wincp != concp)
	{
B
Bruce Momjian 已提交
763 764 765
		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 已提交
766
			   concp, wincp);
B
> >  
Bruce Momjian 已提交
767 768
	}
}
B
Bruce Momjian 已提交
769

B
> >  
Bruce Momjian 已提交
770
#endif