startup.c 14.1 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.23 2000/02/13 21:45:14 petere Exp $
P
Peter Eisentraut 已提交
7
 */
8 9 10 11 12 13 14
#include <c.h>

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

15 16 17
#ifndef WIN32
#include <unistd.h>
#else /* WIN32 */
18
#include <io.h>
B
Hi!  
Bruce Momjian 已提交
19
#include <windows.h>
20 21
#include <win32.h>
#endif /* WIN32 */
22 23 24 25 26 27 28 29 30 31

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

#include <libpq-fe.h>
#include <version.h>

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

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


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

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

static void
71
parse_options(int argc, char *argv[], struct adhoc_opts * options);
72

73 74 75 76 77 78
static void
process_psqlrc(void);

static void
showVersion(void);

79 80 81 82


/*
 *
83
 * main
84 85 86
 *
 */
int
87
main(int argc, char *argv[])
88
{
B
Bruce Momjian 已提交
89 90
	struct adhoc_opts options;
	int			successResult;
91

B
Bruce Momjian 已提交
92 93 94
	char	   *username = NULL;
	char	   *password = NULL;
	bool		need_pass;
95

96
    if (!strrchr(argv[0], SEP_CHAR))
97
        pset.progname = argv[0];
98
    else
99
        pset.progname = strrchr(argv[0], SEP_CHAR) + 1;
100

101 102
	pset.cur_cmd_source = stdin;
	pset.cur_cmd_interactive = false;
P
Peter Eisentraut 已提交
103
    pset.encoding = PQenv2encoding();
104

105
	pset.vars = CreateVariableSpace();
106 107 108 109 110
    if (!pset.vars)
    {
        fprintf(stderr, "%s: out of memory\n", pset.progname);
        exit(EXIT_FAILURE);
    }
111 112 113
	pset.popt.topt.format = PRINT_ALIGNED;
	pset.queryFout = stdout;
	pset.popt.topt.border = 1;
P
Peter Eisentraut 已提交
114
	pset.popt.topt.pager = true;
115

116
    SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
117

118
	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
119

120
	/* This is obsolete and should be removed sometime. */
121
#ifdef PSQL_ALWAYS_GET_PASSWORDS
122
	pset.getPassword = true;
123
#else
124
	pset.getPassword = false;
125 126
#endif

127
	parse_options(argc, argv, &options);
128

129 130 131 132
    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);
133

B
Bruce Momjian 已提交
134 135
	if (options.username)
	{
136 137 138 139 140
        /*
         * 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.
         */
141
		if (strcmp(options.username, "\001") == 0)
B
Bruce Momjian 已提交
142 143 144
			username = simple_prompt("Username: ", 100, true);
		else
			username = strdup(options.username);
145 146
	}

147
	if (pset.getPassword)
B
Bruce Momjian 已提交
148
		password = simple_prompt("Password: ", 100, false);
149

B
Bruce Momjian 已提交
150 151 152 153
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
154 155 156
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
                               options.action == ACT_LIST_DB ? "template1" : options.dbname,
                               username, password);
B
Bruce Momjian 已提交
157

158 159
		if (PQstatus(pset.db) == CONNECTION_BAD &&
			strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0)
B
Bruce Momjian 已提交
160 161 162 163 164 165 166
		{
			need_pass = true;
			free(password);
			password = NULL;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);
167

B
Bruce Momjian 已提交
168 169 170
	free(username);
	free(password);

171
	if (PQstatus(pset.db) == CONNECTION_BAD)
B
Bruce Momjian 已提交
172
	{
173
        fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
174
		PQfinish(pset.db);
B
Bruce Momjian 已提交
175 176 177
		exit(EXIT_BADCONN);
	}

P
Peter Eisentraut 已提交
178 179 180 181 182
    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.
     */
183
    pset.encoding = PQclientEncoding(pset.db);
P
Peter Eisentraut 已提交
184

B
Bruce Momjian 已提交
185 186
	if (options.action == ACT_LIST_DB)
	{
187
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
188

189
		PQfinish(pset.db);
B
Bruce Momjian 已提交
190 191 192
		exit(!success);
	}

193 194 195 196
    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));
B
Bruce Momjian 已提交
197

198 199 200
	/*
     * Now find something to do
     */
B
Bruce Momjian 已提交
201

202 203 204
	/*
     * process file given by -f
     */
B
Bruce Momjian 已提交
205
	if (options.action == ACT_FILE)
P
Peter Eisentraut 已提交
206
		successResult = process_file(options.action_string) ? 0 : 1;
207 208 209
	/*
     * process slash command if one was given to -c
     */
B
Bruce Momjian 已提交
210
	else if (options.action == ACT_SINGLE_SLASH)
211
    {
212 213 214
        const char * value;

        if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "full")==0)
215
            puts(options.action_string);
P
Peter Eisentraut 已提交
216
		successResult = HandleSlashCmds(options.action_string, NULL, NULL) != CMD_ERROR ? 0 : 1;
217
    }
218 219 220
	/*
     * If the query given to -c was a normal one, send it
     */
B
Bruce Momjian 已提交
221
	else if (options.action == ACT_SINGLE_QUERY)
222
    {
223 224 225
        const char * value;

        if ((value = GetVariable(pset.vars, "ECHO")) && strcmp(value, "full")==0)
226
            puts(options.action_string);
227
		successResult = SendQuery(options.action_string) ? 0 : 1;
228
    }
229 230 231
	/*
     * or otherwise enter interactive main loop
     */
B
Bruce Momjian 已提交
232
	else
233
    {
234 235 236 237 238 239 240 241 242 243 244 245 246 247
        pset.issuper = test_superuser(PQuser(pset.db));
        if (!QUIET() && !pset.notty)
        {
            printf("Welcome to %s, the PostgreSQL interactive terminal.\n\n"
                   "Type:  \\copyright for distribution terms\n"
                   "       \\h for help with SQL commands\n"
                   "       \\? for help on internal slash commands\n"
                   "       \\g or terminate with semicolon to execute query\n"
                   "       \\q to quit\n\n", pset.progname);
        }

        SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
        SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
        SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
248
        process_psqlrc();
249 250
        if (!pset.notty)
            initializeInput(options.no_readline ? 0 : 1);
P
Peter Eisentraut 已提交
251
		successResult = MainLoop(stdin);
252 253
        if (!pset.notty)
            finishInput();
254
    }
B
Bruce Momjian 已提交
255 256

	/* clean up */
257 258 259
	PQfinish(pset.db);
	setQFout(NULL);
	DestroyVariableSpace(pset.vars);
B
Bruce Momjian 已提交
260 261

	return successResult;
262 263 264 265 266 267 268 269 270 271
}



/*
 * Parse command line options
 */

#ifdef WIN32
/* getopt is not in the standard includes on Win32 */
B
Bruce Momjian 已提交
272
int			getopt(int, char *const[], const char *);
B
Hi!  
Bruce Momjian 已提交
273 274
/* And it requires progname to be set */
char        *__progname = "psql";
275 276 277
#endif

static void
278
parse_options(int argc, char *argv[], struct adhoc_opts * options)
279 280
{
#ifdef HAVE_GETOPT_LONG
281 282
	static struct option long_options[] =
    {
283
        {"echo-all", no_argument, NULL, 'a'},
B
Bruce Momjian 已提交
284 285 286
		{"no-align", no_argument, NULL, 'A'},
		{"command", required_argument, NULL, 'c'},
		{"dbname", required_argument, NULL, 'd'},
287
		{"echo-queries", no_argument, NULL, 'e'},
288
		{"echo-hidden", no_argument, NULL, 'E'},
B
Bruce Momjian 已提交
289
		{"file", required_argument, NULL, 'f'},
290
		{"field-separator", required_argument, NULL, 'F'},
B
Bruce Momjian 已提交
291 292 293
		{"host", required_argument, NULL, 'h'},
		{"html", no_argument, NULL, 'H'},
		{"list", no_argument, NULL, 'l'},
294
		{"output", required_argument, NULL, 'o'},
B
Bruce Momjian 已提交
295 296 297
		{"port", required_argument, NULL, 'p'},
		{"pset", required_argument, NULL, 'P'},
		{"quiet", no_argument, NULL, 'q'},
298
        {"record-separator", required_argument, NULL, 'R'},
B
Bruce Momjian 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312
		{"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'},
		{"expanded", no_argument, NULL, 'x'},
		{"set", required_argument, NULL, 'v'},
		{"variable", required_argument, NULL, 'v'},
		{"version", no_argument, NULL, 'V'},
		{"password", no_argument, NULL, 'W'},
		{"help", no_argument, NULL, '?'},
	};

	int			optindex;
313
#endif /* HAVE_GETOPT_LONG */
314

B
Bruce Momjian 已提交
315 316 317
	extern char *optarg;
	extern int	optind;
	int			c;
318
    bool        used_old_u_option = false;
319

320
	memset(options, 0, sizeof *options);
321 322

#ifdef HAVE_GETOPT_LONG
323
	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:lh:Hno:p:P:qRsStT:uU:v:VWx?", long_options, &optindex)) != -1)
324
#else /* not HAVE_GETOPT_LONG */
B
Bruce Momjian 已提交
325 326 327 328
	/*
	 * Be sure to leave the '-' in here, so we can catch accidental long
	 * options.
	 */
329
	while ((c = getopt(argc, argv, "aAc:d:eEf:F:lh:Hno:p:P:qRsStT:uU:v:VWx?-")) != -1)
330
#endif /* not HAVE_GETOPT_LONG */
331
	{
B
Bruce Momjian 已提交
332 333
		switch (c)
		{
334 335 336
			case 'a':
				SetVariable(pset.vars, "ECHO", "all");
				break;
B
Bruce Momjian 已提交
337
			case 'A':
338
				pset.popt.topt.format = PRINT_UNALIGNED;
B
Bruce Momjian 已提交
339 340 341 342 343 344 345 346 347 348 349 350
				break;
			case 'c':
				options->action_string = optarg;
				if (optarg[0] == '\\')
					options->action = ACT_SINGLE_SLASH;
				else
					options->action = ACT_SINGLE_QUERY;
				break;
			case 'd':
				options->dbname = optarg;
				break;
			case 'e':
351
				SetVariable(pset.vars, "ECHO", "queries");
B
Bruce Momjian 已提交
352 353
				break;
			case 'E':
354
				SetVariableBool(pset.vars, "ECHO_HIDDEN");
B
Bruce Momjian 已提交
355 356 357 358 359 360
				break;
			case 'f':
				options->action = ACT_FILE;
				options->action_string = optarg;
				break;
			case 'F':
361
				pset.popt.topt.fieldSep = xstrdup(optarg);
B
Bruce Momjian 已提交
362 363 364 365 366
				break;
			case 'h':
				options->host = optarg;
				break;
			case 'H':
367
				pset.popt.topt.format = PRINT_HTML;
B
Bruce Momjian 已提交
368 369 370 371 372 373 374 375
				break;
			case 'l':
				options->action = ACT_LIST_DB;
				break;
			case 'n':
				options->no_readline = true;
				break;
			case 'o':
376
				setQFout(optarg);
B
Bruce Momjian 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389
				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)
390
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
391 392 393
					else
					{
						*equal_loc = '\0';
394
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
395 396 397 398
					}

					if (!result)
					{
399
						fprintf(stderr, "%s: couldn't set printing parameter %s\n", pset.progname, value);
B
Bruce Momjian 已提交
400 401 402 403 404 405 406
						exit(EXIT_FAILURE);
					}

					free(value);
					break;
				}
			case 'q':
407
				SetVariableBool(pset.vars, "QUIET");
B
Bruce Momjian 已提交
408
				break;
409 410 411
            case 'R':
                pset.popt.topt.recordSep = xstrdup(optarg);
                break;
B
Bruce Momjian 已提交
412
			case 's':
413
				SetVariableBool(pset.vars, "SINGLESTEP");
B
Bruce Momjian 已提交
414 415
				break;
			case 'S':
416
				SetVariableBool(pset.vars, "SINGLELINE");
B
Bruce Momjian 已提交
417 418
				break;
			case 't':
419
				pset.popt.topt.tuples_only = true;
B
Bruce Momjian 已提交
420 421
				break;
			case 'T':
422
				pset.popt.topt.tableAttr = xstrdup(optarg);
B
Bruce Momjian 已提交
423 424
				break;
			case 'u':
425 426 427 428
				pset.getPassword = true;
				options->username = "\001"; /* hopefully nobody has that username */
                /* this option is out */
                used_old_u_option = true;
B
Bruce Momjian 已提交
429 430 431 432 433
				break;
			case 'U':
				options->username = optarg;
				break;
			case 'x':
434
				pset.popt.topt.expanded = true;
B
Bruce Momjian 已提交
435 436 437 438 439 440 441 442 443 444
				break;
			case 'v':
				{
					char	   *value;
					char	   *equal_loc;

					value = xstrdup(optarg);
					equal_loc = strchr(value, '=');
					if (!equal_loc)
					{
445
						if (!DeleteVariable(pset.vars, value))
B
Bruce Momjian 已提交
446
						{
447
							fprintf(stderr, "%s: could not delete variable %s\n",
448
                                    pset.progname, value);
B
Bruce Momjian 已提交
449 450 451 452 453 454
							exit(EXIT_FAILURE);
						}
					}
					else
					{
						*equal_loc = '\0';
455
						if (!SetVariable(pset.vars, value, equal_loc + 1))
B
Bruce Momjian 已提交
456
						{
457 458
							fprintf(stderr, "%s: could not set variable %s\n",
                                    pset.progname, value);
B
Bruce Momjian 已提交
459 460 461 462 463 464 465 466
							exit(EXIT_FAILURE);
						}
					}

					free(value);
					break;
				}
			case 'V':
467 468
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
469
			case 'W':
470
				pset.getPassword = true;
B
Bruce Momjian 已提交
471 472
				break;
			case '?':
P
Peter Eisentraut 已提交
473 474
                /* Actual help option given */
                if (strcmp(argv[optind-1], "-?")==0 || strcmp(argv[optind-1], "--help")==0)
P
Peter Eisentraut 已提交
475 476 477 478
                {
                    usage();
                    exit(EXIT_SUCCESS);
                }
P
Peter Eisentraut 已提交
479
                /* unknown option reported by getopt */
P
Peter Eisentraut 已提交
480 481 482 483 484
                else
                {
                    fputs("Try -? for help.\n", stderr);
                    exit(EXIT_FAILURE);
                }
B
Bruce Momjian 已提交
485
				break;
486
#ifndef HAVE_GETOPT_LONG
B
Bruce Momjian 已提交
487
			case '-':
488
				fprintf(stderr, "%s was compiled without support for long options.\n"
489
						"Use -? for help on invocation options.\n", pset.progname);
B
Bruce Momjian 已提交
490 491
				exit(EXIT_FAILURE);
				break;
492
#endif
B
Bruce Momjian 已提交
493
			default:
P
Peter Eisentraut 已提交
494
				fputs("Try -? for help.\n", stderr);
B
Bruce Momjian 已提交
495 496 497
				exit(EXIT_FAILURE);
				break;
		}
498 499
	}

B
Bruce Momjian 已提交
500 501 502 503 504 505 506 507 508 509
	/*
	 * 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];
510
		else if (!QUIET())
511
			fprintf(stderr, "%s: warning: extra option %s ignored\n",
512
                    pset.progname, argv[optind]);
B
Bruce Momjian 已提交
513 514 515

		optind++;
	}
516 517 518 519

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

520 521 522 523 524
}



/*
P
Peter Eisentraut 已提交
525
 * Load .psqlrc file, if found.
526 527
 */
static void
528
process_psqlrc(void)
529
{
B
Bruce Momjian 已提交
530 531
	char	   *psqlrc;
	char	   *home;
532 533 534 535 536

#ifdef WIN32
#define R_OK 0
#endif

B
Bruce Momjian 已提交
537 538
	/* Look for one in the home dir */
	home = getenv("HOME");
539

B
Bruce Momjian 已提交
540 541
	if (home)
	{
542
		psqlrc = malloc(strlen(home) + 20);
B
Bruce Momjian 已提交
543 544
		if (!psqlrc)
		{
P
Peter Eisentraut 已提交
545
            fprintf(stderr, "%s: out of memory\n", pset.progname);
B
Bruce Momjian 已提交
546 547
			exit(EXIT_FAILURE);
		}
548

B
Bruce Momjian 已提交
549 550
		sprintf(psqlrc, "%s/.psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, home);
		if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
551
			process_file(psqlrc);
B
Bruce Momjian 已提交
552 553 554 555
		else
		{
			sprintf(psqlrc, "%s/.psqlrc", home);
			if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
556
				process_file(psqlrc);
B
Bruce Momjian 已提交
557 558
		}
		free(psqlrc);
559 560 561 562 563 564 565
	}
}



/* showVersion
 *
566
 * This output format is intended to match GNU standards.
567 568
 */
static void
569
showVersion(void)
570
{
571
    puts("psql (PostgreSQL) " PG_RELEASE "." PG_VERSION "." PG_SUBVERSION);
572

573 574
#if defined(USE_READLINE) || defined (USE_HISTORY) || defined(MULTIBYTE)
    fputs("contains ", stdout);
575 576

#ifdef USE_READLINE
577 578
    fputs("readline", stdout);
#define _Feature
579
#endif
580

581
#ifdef USE_HISTORY
582 583 584 585
#ifdef _Feature
    fputs(", ", stdout);
#else
#define _Feature
586
#endif
587
    fputs("history", stdout);
588
#endif
589 590 591 592 593 594

#ifdef MULTIBYTE
#ifdef _Feature
    fputs(", ", stdout);
#else
#define _Feature
595
#endif
596
    fputs("multibyte", stdout);
597
#endif
598 599
    
#undef _Feature
600

601 602
    puts(" support");
#endif
603

B
Bruce Momjian 已提交
604
    puts("Portions Copyright (c) 1996-2000, PostgreSQL, Inc");
605
    puts("Portions Copyright (c) 1996 Regents of the University of California");
P
Peter Eisentraut 已提交
606
    puts("Read the file COPYRIGHT or use the command \\copyright to see the");
607
    puts("usage and distribution terms.");
608
}