startup.c 14.8 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.119 2005/07/14 08:42:37 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 void parse_psql_options(int argc, char *argv[],
B
Bruce Momjian 已提交
82
				   struct adhoc_opts * options);
83
static void process_psqlrc(char *argv0);
84
static void process_psqlrc_file(char *filename);
85
static void showVersion(void);
86

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

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

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

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

111
	set_pglocale_pgservice(argv[0], "psql");
P
Peter Eisentraut 已提交
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.progname = get_progname(argv[0]);

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

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

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

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

160
	pset.verbosity = PQERRORS_DEFAULT;
161

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

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

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

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

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

191
	if (pset.getPassword)
B
Bruce Momjian 已提交
192
		password = simple_prompt("Password: ", 100, false);
193

B
Bruce Momjian 已提交
194 195 196 197
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
198
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
199
			options.action == ACT_LIST_DB ? "postgres" : options.dbname,
200
							   username, password);
B
Bruce Momjian 已提交
201

202
		if (PQstatus(pset.db) == CONNECTION_BAD &&
203
			strcmp(PQerrorMessage(pset.db), PQnoPasswordSupplied) == 0 &&
204
			!feof(stdin))
B
Bruce Momjian 已提交
205
		{
206
			PQfinish(pset.db);
B
Bruce Momjian 已提交
207 208 209 210 211 212
			need_pass = true;
			free(password);
			password = NULL;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);
213

B
Bruce Momjian 已提交
214 215 216
	free(username);
	free(password);

217
	if (PQstatus(pset.db) == CONNECTION_BAD)
B
Bruce Momjian 已提交
218
	{
219
		fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
220
		PQfinish(pset.db);
B
Bruce Momjian 已提交
221 222 223
		exit(EXIT_BADCONN);
	}

224 225
	PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);

226
	SyncVariables();
P
Peter Eisentraut 已提交
227

228 229 230
	/* Grab the backend server version */
	pset.sversion = PQserverVersion(pset.db);

B
Bruce Momjian 已提交
231 232
	if (options.action == ACT_LIST_DB)
	{
233
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
234

235
		PQfinish(pset.db);
236
		exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
B
Bruce Momjian 已提交
237 238
	}

239 240 241 242 243 244 245
	if (options.logfilename)
	{
		pset.logfile = fopen(options.logfilename, "a");
		if (!pset.logfile)
			fprintf(stderr, gettext("logfile open failed for %s\n\n"), options.logfilename);
	}

246
	/*
247 248
	 * Now find something to do
	 */
B
Bruce Momjian 已提交
249

250
	/*
251 252
	 * process file given by -f
	 */
253
	if (options.action == ACT_FILE && strcmp(options.action_string, "-") != 0)
254 255
	{
		if (!options.no_psqlrc)
256
			process_psqlrc(argv[0]);
257

258
		successResult = process_file(options.action_string);
259 260
	}

261
	/*
262 263
	 * process slash command if one was given to -c
	 */
B
Bruce Momjian 已提交
264
	else if (options.action == ACT_SINGLE_SLASH)
265
	{
266 267
		PsqlScanState scan_state;

268
		if (VariableEquals(pset.vars, "ECHO", "all"))
269
			puts(options.action_string);
270

271 272 273 274 275 276
		scan_state = psql_scan_create();
		psql_scan_setup(scan_state,
						options.action_string,
						strlen(options.action_string));

		successResult = HandleSlashCmds(scan_state, NULL) != CMD_ERROR
277
			? EXIT_SUCCESS : EXIT_FAILURE;
278 279

		psql_scan_destroy(scan_state);
280 281
	}

282
	/*
283 284
	 * If the query given to -c was a normal one, send it
	 */
B
Bruce Momjian 已提交
285
	else if (options.action == ACT_SINGLE_QUERY)
286
	{
287
		if (VariableEquals(pset.vars, "ECHO", "all"))
288
			puts(options.action_string);
289

290
		successResult = SendQuery(options.action_string)
291 292 293
			? EXIT_SUCCESS : EXIT_FAILURE;
	}

294
	/*
295 296
	 * or otherwise enter interactive main loop
	 */
B
Bruce Momjian 已提交
297
	else
298
	{
299 300 301
		if (!options.no_psqlrc)
			process_psqlrc(argv[0]);

302 303
		if (!QUIET() && !pset.notty)
		{
304
			printf(_("Welcome to %s %s, the PostgreSQL interactive terminal.\n\n"
P
Peter Eisentraut 已提交
305 306
						   "Type:  \\copyright for distribution terms\n"
						   "       \\h for help with SQL commands\n"
307
						   "       \\? for help with psql commands\n"
B
Bruce Momjian 已提交
308
			  "       \\g or terminate with semicolon to execute query\n"
P
Peter Eisentraut 已提交
309
						   "       \\q to quit\n\n"),
310
				   pset.progname, PG_VERSION);
B
Bruce Momjian 已提交
311
#ifdef USE_SSL
312
			printSSLInfo();
B
> >  
Bruce Momjian 已提交
313 314 315
#endif
#ifdef WIN32
			checkWin32Codepage();
B
Bruce Momjian 已提交
316
#endif
317 318
		}

319 320
		if (!pset.notty)
			initializeInput(options.no_readline ? 0 : 1);
321
		if (options.action_string)		/* -f - was used */
322
			pset.inputfile = "<stdin>";
323

P
Peter Eisentraut 已提交
324
		successResult = MainLoop(stdin);
325
	}
B
Bruce Momjian 已提交
326 327

	/* clean up */
328 329
	if (pset.logfile)
		fclose(pset.logfile);
330 331
	PQfinish(pset.db);
	setQFout(NULL);
B
Bruce Momjian 已提交
332 333

	return successResult;
334 335 336 337 338 339 340 341 342
}



/*
 * Parse command line options
 */

static void
343
parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
344
{
345
	static struct option long_options[] =
346 347
	{
		{"echo-all", no_argument, NULL, 'a'},
B
Bruce Momjian 已提交
348 349 350
		{"no-align", no_argument, NULL, 'A'},
		{"command", required_argument, NULL, 'c'},
		{"dbname", required_argument, NULL, 'd'},
351
		{"echo-queries", no_argument, NULL, 'e'},
352
		{"echo-hidden", no_argument, NULL, 'E'},
B
Bruce Momjian 已提交
353
		{"file", required_argument, NULL, 'f'},
354
		{"field-separator", required_argument, NULL, 'F'},
B
Bruce Momjian 已提交
355 356 357
		{"host", required_argument, NULL, 'h'},
		{"html", no_argument, NULL, 'H'},
		{"list", no_argument, NULL, 'l'},
358
		{"log", required_argument, NULL, 'L'},
359
		{"no-readline", no_argument, NULL, 'n'},
360
		{"output", required_argument, NULL, 'o'},
B
Bruce Momjian 已提交
361 362 363
		{"port", required_argument, NULL, 'p'},
		{"pset", required_argument, NULL, 'P'},
		{"quiet", no_argument, NULL, 'q'},
364
		{"record-separator", required_argument, NULL, 'R'},
B
Bruce Momjian 已提交
365 366 367 368 369 370 371 372 373
		{"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'},
374
		{"expanded", no_argument, NULL, 'x'},
375
		{"no-psqlrc", no_argument, NULL, 'X'},
B
Bruce Momjian 已提交
376
		{"help", no_argument, NULL, '?'},
T
Tatsuo Ishii 已提交
377
		{NULL, 0, NULL, 0}
B
Bruce Momjian 已提交
378 379 380 381 382 383
	};

	int			optindex;
	extern char *optarg;
	extern int	optind;
	int			c;
384
	bool		used_old_u_option = false;
385

386
	memset(options, 0, sizeof *options);
387

388
	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:uU:v:VWxX?",
389
							long_options, &optindex)) != -1)
390
	{
B
Bruce Momjian 已提交
391 392
		switch (c)
		{
393 394 395
			case 'a':
				SetVariable(pset.vars, "ECHO", "all");
				break;
B
Bruce Momjian 已提交
396
			case 'A':
397
				pset.popt.topt.format = PRINT_UNALIGNED;
B
Bruce Momjian 已提交
398 399 400 401
				break;
			case 'c':
				options->action_string = optarg;
				if (optarg[0] == '\\')
P
Peter Eisentraut 已提交
402
				{
B
Bruce Momjian 已提交
403
					options->action = ACT_SINGLE_SLASH;
P
Peter Eisentraut 已提交
404 405
					options->action_string++;
				}
B
Bruce Momjian 已提交
406 407 408 409 410 411 412
				else
					options->action = ACT_SINGLE_QUERY;
				break;
			case 'd':
				options->dbname = optarg;
				break;
			case 'e':
413
				SetVariable(pset.vars, "ECHO", "queries");
B
Bruce Momjian 已提交
414 415
				break;
			case 'E':
416
				SetVariableBool(pset.vars, "ECHO_HIDDEN");
B
Bruce Momjian 已提交
417 418 419 420 421 422
				break;
			case 'f':
				options->action = ACT_FILE;
				options->action_string = optarg;
				break;
			case 'F':
423
				pset.popt.topt.fieldSep = pg_strdup(optarg);
B
Bruce Momjian 已提交
424 425 426 427 428
				break;
			case 'h':
				options->host = optarg;
				break;
			case 'H':
429
				pset.popt.topt.format = PRINT_HTML;
B
Bruce Momjian 已提交
430 431 432 433
				break;
			case 'l':
				options->action = ACT_LIST_DB;
				break;
434 435 436
			case 'L':
				options->logfilename = optarg;
				break;
B
Bruce Momjian 已提交
437 438 439 440
			case 'n':
				options->no_readline = true;
				break;
			case 'o':
441
				setQFout(optarg);
B
Bruce Momjian 已提交
442 443 444 445 446 447 448 449 450 451
				break;
			case 'p':
				options->port = optarg;
				break;
			case 'P':
				{
					char	   *value;
					char	   *equal_loc;
					bool		result;

452
					value = pg_strdup(optarg);
B
Bruce Momjian 已提交
453 454
					equal_loc = strchr(value, '=');
					if (!equal_loc)
455
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
456 457 458
					else
					{
						*equal_loc = '\0';
459
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
460 461 462 463
					}

					if (!result)
					{
464
						fprintf(stderr, _("%s: couldn't set printing parameter \"%s\"\n"), pset.progname, value);
B
Bruce Momjian 已提交
465 466 467 468 469 470 471
						exit(EXIT_FAILURE);
					}

					free(value);
					break;
				}
			case 'q':
472
				SetVariableBool(pset.vars, "QUIET");
B
Bruce Momjian 已提交
473
				break;
474
			case 'R':
475
				pset.popt.topt.recordSep = pg_strdup(optarg);
476
				break;
B
Bruce Momjian 已提交
477
			case 's':
478
				SetVariableBool(pset.vars, "SINGLESTEP");
B
Bruce Momjian 已提交
479 480
				break;
			case 'S':
481
				SetVariableBool(pset.vars, "SINGLELINE");
B
Bruce Momjian 已提交
482 483
				break;
			case 't':
484
				pset.popt.topt.tuples_only = true;
B
Bruce Momjian 已提交
485 486
				break;
			case 'T':
487
				pset.popt.topt.tableAttr = pg_strdup(optarg);
B
Bruce Momjian 已提交
488 489
				break;
			case 'u':
490
				pset.getPassword = true;
491 492 493 494
				options->username = "\001";		/* hopefully nobody has
												 * that username */
				/* this option is out */
				used_old_u_option = true;
B
Bruce Momjian 已提交
495 496 497 498 499 500 501 502 503
				break;
			case 'U':
				options->username = optarg;
				break;
			case 'v':
				{
					char	   *value;
					char	   *equal_loc;

504
					value = pg_strdup(optarg);
B
Bruce Momjian 已提交
505 506 507
					equal_loc = strchr(value, '=');
					if (!equal_loc)
					{
508
						if (!DeleteVariable(pset.vars, value))
B
Bruce Momjian 已提交
509
						{
510
							fprintf(stderr, _("%s: could not delete variable \"%s\"\n"),
511
									pset.progname, value);
B
Bruce Momjian 已提交
512 513 514 515 516 517
							exit(EXIT_FAILURE);
						}
					}
					else
					{
						*equal_loc = '\0';
518
						if (!SetVariable(pset.vars, value, equal_loc + 1))
B
Bruce Momjian 已提交
519
						{
520
							fprintf(stderr, _("%s: could not set variable \"%s\"\n"),
521
									pset.progname, value);
B
Bruce Momjian 已提交
522 523 524 525 526 527 528 529
							exit(EXIT_FAILURE);
						}
					}

					free(value);
					break;
				}
			case 'V':
530 531
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
532
			case 'W':
533
				pset.getPassword = true;
B
Bruce Momjian 已提交
534
				break;
535 536 537
			case 'x':
				pset.popt.topt.expanded = true;
				break;
538 539 540
			case 'X':
				options->no_psqlrc = true;
				break;
B
Bruce Momjian 已提交
541
			case '?':
542 543 544 545 546 547 548 549 550
				/* 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
				{
551
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
552
							pset.progname);
553
					exit(EXIT_FAILURE);
554
				}
B
Bruce Momjian 已提交
555 556
				break;
			default:
557
				fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
558
						pset.progname);
559
				exit(EXIT_FAILURE);
B
Bruce Momjian 已提交
560 561
				break;
		}
562 563
	}

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

		optind++;
	}
580

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

584 585 586 587
}


/*
P
Peter Eisentraut 已提交
588
 * Load .psqlrc file, if found.
589 590
 */
static void
591
process_psqlrc(char *argv0)
592
{
B
Bruce Momjian 已提交
593
	char		home[MAXPGPATH];
594
	char		rc_file[MAXPGPATH];
B
Bruce Momjian 已提交
595 596
	char		my_exec_path[MAXPGPATH];
	char		etc_path[MAXPGPATH];
597 598 599

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

601 602
	snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
	process_psqlrc_file(rc_file);
603

604
	if (get_home_path(home))
605
	{
606 607
		snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
		process_psqlrc_file(rc_file);
608 609 610 611 612 613 614 615 616
	}
}



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

618 619
#if defined(WIN32) && (!defined(__MINGW32__))
#define R_OK 4
620 621
#endif

622 623
	psqlrc = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1);
	sprintf(psqlrc, "%s-%s", filename, PG_VERSION);
624

625 626 627
	if (access(psqlrc, R_OK) == 0)
		process_file(psqlrc);
	else if (access(filename, R_OK) == 0)
B
Bruce Momjian 已提交
628
		process_file(filename);
629
	free(psqlrc);
630 631 632 633 634 635
}



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

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



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

	ssl = PQgetssl(pset.db);
	if (!ssl)
B
Bruce Momjian 已提交
664
		return;					/* no SSL */
B
Bruce Momjian 已提交
665 666

	SSL_get_cipher_bits(ssl, &sslbits);
667
	printf(_("SSL connection (cipher: %s, bits: %i)\n\n"),
B
Bruce Momjian 已提交
668
		   SSL_get_cipher(ssl), sslbits);
B
Bruce Momjian 已提交
669 670
}
#endif
B
> >  
Bruce Momjian 已提交
671 672 673 674 675 676 677 678 679 680 681 682



/*
 * checkWin32Codepage
 *
 * Prints a warning when win32 console codepage differs from Windows codepage
 */
#ifdef WIN32
static void
checkWin32Codepage(void)
{
B
Bruce Momjian 已提交
683 684
	unsigned int wincp,
				concp;
B
> >  
Bruce Momjian 已提交
685 686 687

	wincp = GetACP();
	concp = GetConsoleCP();
B
Bruce Momjian 已提交
688 689
	if (wincp != concp)
	{
690
			printf(_("Warning: Console code page (%u) differs from Windows code page (%u)\n"
691 692
					   "         8-bit characters may not work correctly. See psql reference\n"
					   "         page \"Notes for Windows users\" for details.\n\n"),
B
Bruce Momjian 已提交
693
			   concp, wincp);
B
> >  
Bruce Momjian 已提交
694 695
	}
}
B
Bruce Momjian 已提交
696

B
> >  
Bruce Momjian 已提交
697
#endif