startup.c 14.5 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.69 2002/11/08 19:12:21 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>
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

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

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

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

38 39
#include "mb/pg_wchar.h"

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

45
#define PSQLRC ".psqlrc"
46

47 48
/*
 * Structures to pass information between the option parsing routine
49 50
 * and the main function
 */
B
Bruce Momjian 已提交
51 52
enum _actions
{
53 54 55 56 57
	ACT_NOTHING = 0,
	ACT_SINGLE_SLASH,
	ACT_LIST_DB,
	ACT_SINGLE_QUERY,
	ACT_FILE
58 59
};

B
Bruce Momjian 已提交
60 61 62 63 64 65 66 67 68
struct adhoc_opts
{
	char	   *dbname;
	char	   *host;
	char	   *port;
	char	   *username;
	enum _actions action;
	char	   *action_string;
	bool		no_readline;
69
	bool		no_psqlrc;
70 71 72
};

static void
73
			parse_psql_options(int argc, char *argv[], struct adhoc_opts * options);
74

75
static void
76
			process_psqlrc(void);
77 78

static void
79
			showVersion(void);
80

B
Bruce Momjian 已提交
81 82
#ifdef USE_SSL
static void
B
Bruce Momjian 已提交
83
			printSSLInfo(void);
B
Bruce Momjian 已提交
84
#endif
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

P
Peter Eisentraut 已提交
102
	setlocale(LC_ALL, "");
103
#ifdef ENABLE_NLS
P
Peter Eisentraut 已提交
104 105 106 107
	bindtextdomain("psql", LOCALEDIR);
	textdomain("psql");
#endif

108
	if (!strrchr(argv[0], '/'))
109 110
		pset.progname = argv[0];
	else
111
		pset.progname = strrchr(argv[0], '/') + 1;
112

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

127 128
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
129
	pset.encoding = PQenv2encoding();
130

131
	pset.vars = CreateVariableSpace();
132 133
	if (!pset.vars)
	{
P
Peter Eisentraut 已提交
134
		fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
135 136
		exit(EXIT_FAILURE);
	}
137 138 139
	pset.popt.topt.format = PRINT_ALIGNED;
	pset.queryFout = stdout;
	pset.popt.topt.border = 1;
140
	pset.popt.topt.pager = 1;
141
	pset.popt.default_footer = true;
142

143
	SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
144

145
	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
146

147
	/* This is obsolete and should be removed sometime. */
148
#ifdef PSQL_ALWAYS_GET_PASSWORDS
149
	pset.getPassword = true;
150
#else
151
	pset.getPassword = false;
152 153
#endif

154
	parse_psql_options(argc, argv, &options);
155

156 157 158 159
	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);
160

B
Bruce Momjian 已提交
161 162
	if (options.username)
	{
163 164 165 166 167
		/*
		 * 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.
		 */
168
		if (strcmp(options.username, "\001") == 0)
169
			username = simple_prompt("User name: ", 100, true);
B
Bruce Momjian 已提交
170 171
		else
			username = strdup(options.username);
172 173
	}

174
	if (pset.getPassword)
B
Bruce Momjian 已提交
175
		password = simple_prompt("Password: ", 100, false);
176

B
Bruce Momjian 已提交
177 178 179 180
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
181
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
182 183
			options.action == ACT_LIST_DB ? "template1" : options.dbname,
							   username, password);
B
Bruce Momjian 已提交
184

185
		if (PQstatus(pset.db) == CONNECTION_BAD &&
186 187
			strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0 &&
			!feof(stdin))
B
Bruce Momjian 已提交
188
		{
189
			PQfinish(pset.db);
B
Bruce Momjian 已提交
190 191 192 193 194 195
			need_pass = true;
			free(password);
			password = NULL;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);
196

B
Bruce Momjian 已提交
197 198 199
	free(username);
	free(password);

200
	if (PQstatus(pset.db) == CONNECTION_BAD)
B
Bruce Momjian 已提交
201
	{
202
		fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
203
		PQfinish(pset.db);
B
Bruce Momjian 已提交
204 205 206
		exit(EXIT_BADCONN);
	}

207 208 209 210 211 212 213
	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 已提交
214

B
Bruce Momjian 已提交
215 216
	if (options.action == ACT_LIST_DB)
	{
217
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
218

219
		PQfinish(pset.db);
220
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
221 222
	}

223 224 225 226 227
	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));
228

229
	/*
230 231
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
232

233
	/*
234 235
	 * process file given by -f
	 */
236
	if (options.action == ACT_FILE && strcmp(options.action_string, "-") != 0)
237 238 239
	{
		if (!options.no_psqlrc)
			process_psqlrc();
240

241
		successResult = process_file(options.action_string);
242 243
	}

244
	/*
245 246
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
247
	else if (options.action == ACT_SINGLE_SLASH)
248 249
	{
		const char *value;
250

251 252
		if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "all") == 0)
			puts(options.action_string);
253
		successResult = HandleSlashCmds(options.action_string, NULL, NULL, NULL) != CMD_ERROR
254 255 256
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

257
	/*
258 259
	 * If the query given to -c was a normal one, send it
	 */
B
Bruce Momjian 已提交
260
	else if (options.action == ACT_SINGLE_QUERY)
261 262
	{
		const char *value;
263

264 265
		if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "all") == 0)
			puts(options.action_string);
266
		successResult = SendQuery(options.action_string)
267 268 269
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

270
	/*
271 272
	 * or otherwise enter interactive main loop
	 */
B
Bruce Momjian 已提交
273
	else
274 275 276 277
	{
		pset.issuper = test_superuser(PQuser(pset.db));
		if (!QUIET() && !pset.notty)
		{
278
			printf(gettext("Welcome to %s %s, the PostgreSQL interactive terminal.\n\n"
P
Peter Eisentraut 已提交
279 280
						   "Type:  \\copyright for distribution terms\n"
						   "       \\h for help with SQL commands\n"
281 282
					   "       \\? for help on internal slash commands\n"
			  "       \\g or terminate with semicolon to execute query\n"
P
Peter Eisentraut 已提交
283
						   "       \\q to quit\n\n"),
284
				   pset.progname, PG_VERSION);
B
Bruce Momjian 已提交
285
#ifdef USE_SSL
286
			printSSLInfo();
B
Bruce Momjian 已提交
287
#endif
288 289
		}

290 291 292 293 294 295 296
		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);
297
		if (options.action_string)		/* -f - was used */
298
			pset.inputfile = "<stdin>";
P
Peter Eisentraut 已提交
299
		successResult = MainLoop(stdin);
300
	}
B
Bruce Momjian 已提交
301 302

	/* clean up */
303 304
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
305 306

	return successResult;
307 308 309 310 311 312 313 314 315 316
}



/*
 * Parse command line options
 */

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

B
Hi!  
Bruce Momjian 已提交
319
/* And it requires progname to be set */
320
char	   *__progname = "psql";
321 322 323
#endif

static void
324
parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
325 326
{
#ifdef HAVE_GETOPT_LONG
327
	static struct option long_options[] =
328 329
	{
		{"echo-all", no_argument, NULL, 'a'},
B
Bruce Momjian 已提交
330 331 332
		{"no-align", no_argument, NULL, 'A'},
		{"command", required_argument, NULL, 'c'},
		{"dbname", required_argument, NULL, 'd'},
333
		{"echo-queries", no_argument, NULL, 'e'},
334
		{"echo-hidden", no_argument, NULL, 'E'},
B
Bruce Momjian 已提交
335
		{"file", required_argument, NULL, 'f'},
336
		{"field-separator", required_argument, NULL, 'F'},
B
Bruce Momjian 已提交
337 338 339
		{"host", required_argument, NULL, 'h'},
		{"html", no_argument, NULL, 'H'},
		{"list", no_argument, NULL, 'l'},
340
		{"no-readline", no_argument, NULL, 'n'},
341
		{"output", required_argument, NULL, 'o'},
B
Bruce Momjian 已提交
342 343 344
		{"port", required_argument, NULL, 'p'},
		{"pset", required_argument, NULL, 'P'},
		{"quiet", no_argument, NULL, 'q'},
345
		{"record-separator", required_argument, NULL, 'R'},
B
Bruce Momjian 已提交
346 347 348 349 350 351 352 353 354
		{"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'},
355
		{"expanded", no_argument, NULL, 'x'},
356
		{"no-psqlrc", no_argument, NULL, 'X'},
B
Bruce Momjian 已提交
357
		{"help", no_argument, NULL, '?'},
T
Tatsuo Ishii 已提交
358
		{NULL, 0, NULL, 0}
B
Bruce Momjian 已提交
359 360 361
	};

	int			optindex;
362
#endif   /* HAVE_GETOPT_LONG */
363

B
Bruce Momjian 已提交
364 365 366
	extern char *optarg;
	extern int	optind;
	int			c;
367
	bool		used_old_u_option = false;
368

369
	memset(options, 0, sizeof *options);
370 371

#ifdef HAVE_GETOPT_LONG
372
	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?", long_options, &optindex)) != -1)
373 374
#else							/* not HAVE_GETOPT_LONG */

B
Bruce Momjian 已提交
375 376 377 378
	/*
	 * Be sure to leave the '-' in here, so we can catch accidental long
	 * options.
	 */
379
	while ((c = getopt(argc, argv, "aAc:d:eEf:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?-")) != -1)
380
#endif   /* not HAVE_GETOPT_LONG */
381
	{
B
Bruce Momjian 已提交
382 383
		switch (c)
		{
384 385 386
			case 'a':
				SetVariable(pset.vars, "ECHO", "all");
				break;
B
Bruce Momjian 已提交
387
			case 'A':
388
				pset.popt.topt.format = PRINT_UNALIGNED;
B
Bruce Momjian 已提交
389 390 391 392
				break;
			case 'c':
				options->action_string = optarg;
				if (optarg[0] == '\\')
P
Peter Eisentraut 已提交
393
				{
B
Bruce Momjian 已提交
394
					options->action = ACT_SINGLE_SLASH;
P
Peter Eisentraut 已提交
395 396
					options->action_string++;
				}
B
Bruce Momjian 已提交
397 398 399 400 401 402 403
				else
					options->action = ACT_SINGLE_QUERY;
				break;
			case 'd':
				options->dbname = optarg;
				break;
			case 'e':
404
				SetVariable(pset.vars, "ECHO", "queries");
B
Bruce Momjian 已提交
405 406
				break;
			case 'E':
407
				SetVariableBool(pset.vars, "ECHO_HIDDEN");
B
Bruce Momjian 已提交
408 409 410 411 412 413
				break;
			case 'f':
				options->action = ACT_FILE;
				options->action_string = optarg;
				break;
			case 'F':
414
				pset.popt.topt.fieldSep = xstrdup(optarg);
B
Bruce Momjian 已提交
415 416 417 418 419
				break;
			case 'h':
				options->host = optarg;
				break;
			case 'H':
420
				pset.popt.topt.format = PRINT_HTML;
B
Bruce Momjian 已提交
421 422 423 424 425 426 427 428
				break;
			case 'l':
				options->action = ACT_LIST_DB;
				break;
			case 'n':
				options->no_readline = true;
				break;
			case 'o':
429
				setQFout(optarg);
B
Bruce Momjian 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442
				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)
443
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
444 445 446
					else
					{
						*equal_loc = '\0';
447
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
448 449 450 451
					}

					if (!result)
					{
P
Peter Eisentraut 已提交
452
						fprintf(stderr, gettext("%s: couldn't set printing parameter %s\n"), pset.progname, value);
B
Bruce Momjian 已提交
453 454 455 456 457 458 459
						exit(EXIT_FAILURE);
					}

					free(value);
					break;
				}
			case 'q':
460
				SetVariableBool(pset.vars, "QUIET");
B
Bruce Momjian 已提交
461
				break;
462 463 464
			case 'R':
				pset.popt.topt.recordSep = xstrdup(optarg);
				break;
B
Bruce Momjian 已提交
465
			case 's':
466
				SetVariableBool(pset.vars, "SINGLESTEP");
B
Bruce Momjian 已提交
467 468
				break;
			case 'S':
469
				SetVariableBool(pset.vars, "SINGLELINE");
B
Bruce Momjian 已提交
470 471
				break;
			case 't':
472
				pset.popt.topt.tuples_only = true;
B
Bruce Momjian 已提交
473 474
				break;
			case 'T':
475
				pset.popt.topt.tableAttr = xstrdup(optarg);
B
Bruce Momjian 已提交
476 477
				break;
			case 'u':
478
				pset.getPassword = true;
479 480 481 482
				options->username = "\001";		/* hopefully nobody has
												 * that username */
				/* this option is out */
				used_old_u_option = true;
B
Bruce Momjian 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495
				break;
			case 'U':
				options->username = optarg;
				break;
			case 'v':
				{
					char	   *value;
					char	   *equal_loc;

					value = xstrdup(optarg);
					equal_loc = strchr(value, '=');
					if (!equal_loc)
					{
496
						if (!DeleteVariable(pset.vars, value))
B
Bruce Momjian 已提交
497
						{
P
Peter Eisentraut 已提交
498
							fprintf(stderr, gettext("%s: could not delete variable %s\n"),
499
									pset.progname, value);
B
Bruce Momjian 已提交
500 501 502 503 504 505
							exit(EXIT_FAILURE);
						}
					}
					else
					{
						*equal_loc = '\0';
506
						if (!SetVariable(pset.vars, value, equal_loc + 1))
B
Bruce Momjian 已提交
507
						{
P
Peter Eisentraut 已提交
508
							fprintf(stderr, gettext("%s: could not set variable %s\n"),
509
									pset.progname, value);
B
Bruce Momjian 已提交
510 511 512 513 514 515 516 517
							exit(EXIT_FAILURE);
						}
					}

					free(value);
					break;
				}
			case 'V':
518 519
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
520
			case 'W':
521
				pset.getPassword = true;
B
Bruce Momjian 已提交
522
				break;
523 524 525
			case 'x':
				pset.popt.topt.expanded = true;
				break;
526 527 528
			case 'X':
				options->no_psqlrc = true;
				break;
B
Bruce Momjian 已提交
529
			case '?':
530 531 532 533 534 535 536 537 538
				/* 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
				{
P
Peter Eisentraut 已提交
539
					fprintf(stderr, gettext("Try '%s --help' for more information.\n"),
540
							pset.progname);
541
					exit(EXIT_FAILURE);
542
				}
B
Bruce Momjian 已提交
543
				break;
544
#ifndef HAVE_GETOPT_LONG
B
Bruce Momjian 已提交
545 546 547 548 549

				/*
				 * FreeBSD has a broken getopt that causes this test to
				 * fail.
				 */
B
Bruce Momjian 已提交
550
			case '-':
P
Peter Eisentraut 已提交
551 552
				fprintf(stderr,
						gettext("%s was compiled without support for long options.\n"
553
						 "Use --help for help on invocation options.\n"),
P
Peter Eisentraut 已提交
554
						pset.progname);
B
Bruce Momjian 已提交
555 556
				exit(EXIT_FAILURE);
				break;
557
#endif
B
Bruce Momjian 已提交
558
			default:
P
Peter Eisentraut 已提交
559
				fprintf(stderr, gettext("Try '%s --help' for more information.\n"),
560
						pset.progname);
561
				exit(EXIT_FAILURE);
B
Bruce Momjian 已提交
562 563
				break;
		}
564 565
	}

B
Bruce Momjian 已提交
566 567 568 569 570 571 572 573 574 575
	/*
	 * 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];
576
		else if (!QUIET())
P
Peter Eisentraut 已提交
577
			fprintf(stderr, gettext("%s: warning: extra option %s ignored\n"),
578
					pset.progname, argv[optind]);
B
Bruce Momjian 已提交
579 580 581

		optind++;
	}
582

583
	if (used_old_u_option && !QUIET())
P
Peter Eisentraut 已提交
584
		fprintf(stderr, gettext("%s: Warning: The -u option is deprecated. Use -U.\n"), pset.progname);
585

586 587 588 589 590
}



/*
P
Peter Eisentraut 已提交
591
 * Load .psqlrc file, if found.
592 593
 */
static void
594
process_psqlrc(void)
595
{
B
Bruce Momjian 已提交
596 597
	char	   *psqlrc;
	char	   *home;
598 599 600 601 602

#ifdef WIN32
#define R_OK 0
#endif

B
Bruce Momjian 已提交
603 604
	/* Look for one in the home dir */
	home = getenv("HOME");
605

B
Bruce Momjian 已提交
606 607
	if (home)
	{
608
		psqlrc = malloc(strlen(home) + 1 + strlen(PSQLRC) + 1 +
609
				 strlen(PG_VERSION) + 1);
B
Bruce Momjian 已提交
610 611
		if (!psqlrc)
		{
P
Peter Eisentraut 已提交
612
			fprintf(stderr, gettext("%s: out of memory\n"), pset.progname);
B
Bruce Momjian 已提交
613 614
			exit(EXIT_FAILURE);
		}
615

616
		sprintf(psqlrc, "%s/%s-%s", home, PSQLRC, PG_VERSION);
B
Bruce Momjian 已提交
617
		if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
618
			process_file(psqlrc);
B
Bruce Momjian 已提交
619 620
		else
		{
621
			sprintf(psqlrc, "%s/%s", home, PSQLRC);
B
Bruce Momjian 已提交
622
			if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
623
				process_file(psqlrc);
B
Bruce Momjian 已提交
624 625
		}
		free(psqlrc);
626 627 628 629 630 631 632
	}
}



/* showVersion
 *
633
 * This output format is intended to match GNU standards.
634 635
 */
static void
636
showVersion(void)
637
{
638
	puts("psql (PostgreSQL) " PG_VERSION);
639

640
#if defined(USE_READLINE)
641
	puts(gettext("contains support for command-line editing"));
642
#endif
643
}
B
Bruce Momjian 已提交
644 645 646 647 648 649 650 651 652



/*
 * printSSLInfo
 *
 * Prints information about the current SSL connection, if SSL is in use
 */
#ifdef USE_SSL
B
Bruce Momjian 已提交
653
static void
B
Bruce Momjian 已提交
654 655
printSSLInfo(void)
{
B
Bruce Momjian 已提交
656 657
	int			sslbits = -1;
	SSL		   *ssl;
B
Bruce Momjian 已提交
658 659 660

	ssl = PQgetssl(pset.db);
	if (!ssl)
B
Bruce Momjian 已提交
661
		return;					/* no SSL */
B
Bruce Momjian 已提交
662 663

	SSL_get_cipher_bits(ssl, &sslbits);
664
	printf(gettext("SSL connection (cipher: %s, bits: %i)\n\n"),
B
Bruce Momjian 已提交
665
		   SSL_get_cipher(ssl), sslbits);
B
Bruce Momjian 已提交
666
}
667

B
Bruce Momjian 已提交
668
#endif