startup.c 13.3 KB
Newer Older
P
Peter Eisentraut 已提交
1 2 3 4 5
/*
 * psql - the PostgreSQL interactive terminal
 *
 * Copyright 2000 by PostgreSQL Global Development Team
 *
B
Bruce Momjian 已提交
6
 * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.19 2000/01/27 05:33:51 momjian Exp $
P
Peter Eisentraut 已提交
7
 */
8 9 10 11 12 13 14 15 16 17 18 19
#include <c.h>

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

#ifdef WIN32
#include <io.h>
B
Hi!  
Bruce Momjian 已提交
20
#include <windows.h>
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#else
#include <unistd.h>
#endif

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

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

#include "settings.h"
#include "command.h"
#include "help.h"
#include "mainloop.h"
#include "common.h"
#include "input.h"
#include "variables.h"
#include "print.h"
#include "describe.h"

43
PsqlSettings pset;
44 45

static void
46
process_psqlrc(void);
47 48

static void
49
showVersion(void);
50 51 52 53 54


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

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

static void
76
parse_options(int argc, char *argv[], struct adhoc_opts * options);
77 78 79 80 81 82 83 84 85 86 87



/*
 *
 * main()
 *
 */
int
main(int argc, char **argv)
{
B
Bruce Momjian 已提交
88 89
	struct adhoc_opts options;
	int			successResult;
90

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

95
	memset(&pset, 0, sizeof pset);
96

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

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

106 107 108
	pset.vars = CreateVariableSpace();
	pset.popt.topt.format = PRINT_ALIGNED;
	pset.queryFout = stdout;
P
Peter Eisentraut 已提交
109 110
	pset.popt.topt.fieldSep = xstrdup(DEFAULT_FIELD_SEP);
	pset.popt.topt.recordSep = xstrdup(DEFAULT_RECORD_SEP);
111
	pset.popt.topt.border = 1;
P
Peter Eisentraut 已提交
112
	pset.popt.topt.pager = true;
113

114 115 116 117
	SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
	SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
	SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
    SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
118

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

B
Bruce Momjian 已提交
121
	/* This is obsolete and will be removed very soon. */
122
#ifdef PSQL_ALWAYS_GET_PASSWORDS
123
	pset.getPassword = true;
124
#else
125
	pset.getPassword = false;
126 127
#endif

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

130
	if (options.action == ACT_LIST_DB)
B
Bruce Momjian 已提交
131
		options.dbname = "template1";
132

B
Bruce Momjian 已提交
133 134
	if (options.username)
	{
135
		if (strcmp(options.username, "\001") == 0)
B
Bruce Momjian 已提交
136 137 138
			username = simple_prompt("Username: ", 100, true);
		else
			username = strdup(options.username);
139 140
	}

141
	if (pset.getPassword)
B
Bruce Momjian 已提交
142
		password = simple_prompt("Password: ", 100, false);
143

B
Bruce Momjian 已提交
144 145 146 147
	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
148
		pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL, options.dbname, username, password);
B
Bruce Momjian 已提交
149

150 151
		if (PQstatus(pset.db) == CONNECTION_BAD &&
			strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0)
B
Bruce Momjian 已提交
152 153 154 155 156 157 158
		{
			need_pass = true;
			free(password);
			password = NULL;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);
159

B
Bruce Momjian 已提交
160 161 162
	free(username);
	free(password);

163
	if (PQstatus(pset.db) == CONNECTION_BAD)
B
Bruce Momjian 已提交
164
	{
P
Peter Eisentraut 已提交
165 166
		fprintf(stderr, "%s: connection to database \"%s\" failed - %s",
                pset.progname, PQdb(pset.db), PQerrorMessage(pset.db));
167
		PQfinish(pset.db);
B
Bruce Momjian 已提交
168 169 170
		exit(EXIT_BADCONN);
	}

P
Peter Eisentraut 已提交
171 172 173 174 175 176 177
    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);

B
Bruce Momjian 已提交
178 179
	if (options.action == ACT_LIST_DB)
	{
180
		int			success = listAllDbs(false);
B
Bruce Momjian 已提交
181

182
		PQfinish(pset.db);
B
Bruce Momjian 已提交
183 184 185
		exit(!success);
	}

186 187 188 189
    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 已提交
190

191
	if (!QUIET() && !pset.notty && !options.action)
B
Bruce Momjian 已提交
192
	{
193 194 195 196 197
		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"
198
               "       \\q to quit\n", pset.progname);
B
Bruce Momjian 已提交
199 200 201 202 203 204
	}

	/* Now find something to do */

	/* process file given by -f */
	if (options.action == ACT_FILE)
P
Peter Eisentraut 已提交
205
		successResult = process_file(options.action_string) ? 0 : 1;
B
Bruce Momjian 已提交
206 207
	/* process slash command if one was given to -c */
	else if (options.action == ACT_SINGLE_SLASH)
208 209 210
    {
        if (GetVariable(pset.vars, "ECHO") && strcmp(GetVariable(pset.vars, "ECHO"), "full")==0)
            puts(options.action_string);
P
Peter Eisentraut 已提交
211
		successResult = HandleSlashCmds(options.action_string, NULL, NULL) != CMD_ERROR ? 0 : 1;
212
    }
B
Bruce Momjian 已提交
213 214
	/* If the query given to -c was a normal one, send it */
	else if (options.action == ACT_SINGLE_QUERY)
215 216 217
    {
        if (GetVariable(pset.vars, "ECHO") && strcmp(GetVariable(pset.vars, "ECHO"), "full")==0)
            puts(options.action_string);
218
		successResult = SendQuery( options.action_string) ? 0 : 1;
219
    }
B
Bruce Momjian 已提交
220 221
	/* or otherwise enter interactive main loop */
	else
222 223 224
    {
        process_psqlrc();
        initializeInput(options.no_readline ? 0 : 1);
P
Peter Eisentraut 已提交
225
		successResult = MainLoop(stdin);
226 227
        finishInput();
    }
B
Bruce Momjian 已提交
228 229

	/* clean up */
230 231 232
	PQfinish(pset.db);
	setQFout(NULL);
	DestroyVariableSpace(pset.vars);
B
Bruce Momjian 已提交
233 234

	return successResult;
235 236 237 238 239 240 241 242 243 244
}



/*
 * Parse command line options
 */

#ifdef WIN32
/* getopt is not in the standard includes on Win32 */
B
Bruce Momjian 已提交
245
int			getopt(int, char *const[], const char *);
B
Hi!  
Bruce Momjian 已提交
246 247
/* And it requires progname to be set */
char        *__progname = "psql";
248 249 250
#endif

static void
251
parse_options(int argc, char *argv[], struct adhoc_opts * options)
252 253
{
#ifdef HAVE_GETOPT_LONG
B
Bruce Momjian 已提交
254 255 256 257 258
	static struct option long_options[] = {
		{"no-align", no_argument, NULL, 'A'},
		{"command", required_argument, NULL, 'c'},
		{"dbname", required_argument, NULL, 'd'},
		{"echo", no_argument, NULL, 'e'},
259
		{"echo-hidden", no_argument, NULL, 'E'},
B
Bruce Momjian 已提交
260
		{"file", required_argument, NULL, 'f'},
261
		{"field-separator", required_argument, NULL, 'F'},
B
Bruce Momjian 已提交
262 263 264 265
		{"host", required_argument, NULL, 'h'},
		{"html", no_argument, NULL, 'H'},
		{"list", no_argument, NULL, 'l'},
		{"no-readline", no_argument, NULL, 'n'},
266
		{"output", required_argument, NULL, 'o'},
B
Bruce Momjian 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
		{"port", required_argument, NULL, 'p'},
		{"pset", required_argument, NULL, 'P'},
		{"quiet", no_argument, NULL, 'q'},
		{"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;
284 285
#endif

B
Bruce Momjian 已提交
286 287 288
	extern char *optarg;
	extern int	optind;
	int			c;
289
    bool        used_old_u_option = false;
290

291
	memset(options, 0, sizeof *options);
292 293

#ifdef HAVE_GETOPT_LONG
B
Bruce Momjian 已提交
294
	while ((c = getopt_long(argc, argv, "Ac:d:eEf:F:lh:Hno:p:P:qsStT:uU:v:VWx?", long_options, &optindex)) != -1)
295
#else
B
Bruce Momjian 已提交
296 297 298 299 300 301

	/*
	 * Be sure to leave the '-' in here, so we can catch accidental long
	 * options.
	 */
	while ((c = getopt(argc, argv, "Ac:d:eEf:F:lh:Hno:p:P:qsStT:uU:v:VWx?-")) != -1)
302 303
#endif
	{
B
Bruce Momjian 已提交
304 305 306
		switch (c)
		{
			case 'A':
307
				pset.popt.topt.format = PRINT_UNALIGNED;
B
Bruce Momjian 已提交
308 309 310 311 312 313 314 315 316 317 318 319
				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':
320
				SetVariable(pset.vars, "ECHO", "full");
B
Bruce Momjian 已提交
321 322
				break;
			case 'E':
323
				SetVariable(pset.vars, "ECHO_HIDDEN", "");
B
Bruce Momjian 已提交
324 325 326 327 328 329
				break;
			case 'f':
				options->action = ACT_FILE;
				options->action_string = optarg;
				break;
			case 'F':
330
				pset.popt.topt.fieldSep = strdup(optarg);
B
Bruce Momjian 已提交
331 332 333 334 335
				break;
			case 'h':
				options->host = optarg;
				break;
			case 'H':
336
				pset.popt.topt.format = PRINT_HTML;
B
Bruce Momjian 已提交
337 338 339 340 341 342 343 344
				break;
			case 'l':
				options->action = ACT_LIST_DB;
				break;
			case 'n':
				options->no_readline = true;
				break;
			case 'o':
345
				setQFout(optarg);
B
Bruce Momjian 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358
				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)
359
						result = do_pset(value, NULL, &pset.popt, true);
B
Bruce Momjian 已提交
360 361 362
					else
					{
						*equal_loc = '\0';
363
						result = do_pset(value, equal_loc + 1, &pset.popt, true);
B
Bruce Momjian 已提交
364 365 366 367
					}

					if (!result)
					{
368
						fprintf(stderr, "%s: couldn't set printing parameter %s\n", pset.progname, value);
B
Bruce Momjian 已提交
369 370 371 372 373 374 375
						exit(EXIT_FAILURE);
					}

					free(value);
					break;
				}
			case 'q':
376
				SetVariable(pset.vars, "QUIET", "");
B
Bruce Momjian 已提交
377 378
				break;
			case 's':
379
				SetVariable(pset.vars, "SINGLESTEP", "");
B
Bruce Momjian 已提交
380 381
				break;
			case 'S':
382
				SetVariable(pset.vars, "SINGLELINE", "");
B
Bruce Momjian 已提交
383 384
				break;
			case 't':
385
				pset.popt.topt.tuples_only = true;
B
Bruce Momjian 已提交
386 387
				break;
			case 'T':
388
				pset.popt.topt.tableAttr = xstrdup(optarg);
B
Bruce Momjian 已提交
389 390
				break;
			case 'u':
391 392 393 394
				pset.getPassword = true;
				options->username = "\001"; /* hopefully nobody has that username */
                /* this option is out */
                used_old_u_option = true;
B
Bruce Momjian 已提交
395 396 397 398 399
				break;
			case 'U':
				options->username = optarg;
				break;
			case 'x':
400
				pset.popt.topt.expanded = true;
B
Bruce Momjian 已提交
401 402 403 404 405 406 407 408 409 410
				break;
			case 'v':
				{
					char	   *value;
					char	   *equal_loc;

					value = xstrdup(optarg);
					equal_loc = strchr(value, '=');
					if (!equal_loc)
					{
411
						if (!DeleteVariable(pset.vars, value))
B
Bruce Momjian 已提交
412
						{
413
							fprintf(stderr, "%s: could not delete variable %s\n",
414
                                    pset.progname, value);
B
Bruce Momjian 已提交
415 416 417 418 419 420
							exit(EXIT_FAILURE);
						}
					}
					else
					{
						*equal_loc = '\0';
421
						if (!SetVariable(pset.vars, value, equal_loc + 1))
B
Bruce Momjian 已提交
422
						{
423 424
							fprintf(stderr, "%s: could not set variable %s\n",
                                    pset.progname, value);
B
Bruce Momjian 已提交
425 426 427 428 429 430 431 432
							exit(EXIT_FAILURE);
						}
					}

					free(value);
					break;
				}
			case 'V':
433 434
				showVersion();
				exit(EXIT_SUCCESS);
B
Bruce Momjian 已提交
435
			case 'W':
436
				pset.getPassword = true;
B
Bruce Momjian 已提交
437 438
				break;
			case '?':
P
Peter Eisentraut 已提交
439 440 441 442 443 444 445 446 447 448
                if (strcmp(argv[optind-1], "-?")==0)
                {
                    usage();
                    exit(EXIT_SUCCESS);
                }
                else
                {
                    fputs("Try -? for help.\n", stderr);
                    exit(EXIT_FAILURE);
                }
B
Bruce Momjian 已提交
449
				break;
450
#ifndef HAVE_GETOPT_LONG
B
Bruce Momjian 已提交
451
			case '-':
452
				fprintf(stderr, "%s was compiled without support for long options.\n"
453
						"Use -? for help on invocation options.\n", pset.progname);
B
Bruce Momjian 已提交
454 455
				exit(EXIT_FAILURE);
				break;
456
#endif
B
Bruce Momjian 已提交
457
			default:
P
Peter Eisentraut 已提交
458
				fputs("Try -? for help.\n", stderr);
B
Bruce Momjian 已提交
459 460 461
				exit(EXIT_FAILURE);
				break;
		}
462 463
	}

B
Bruce Momjian 已提交
464 465 466 467 468 469 470 471 472 473
	/*
	 * 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];
474
		else if (!QUIET())
475
			fprintf(stderr, "%s: warning: extra option %s ignored\n",
476
                    pset.progname, argv[optind]);
B
Bruce Momjian 已提交
477 478 479

		optind++;
	}
480 481 482 483

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

484 485 486 487 488 489 490 491
}



/*
 * Load /etc/psqlrc or .psqlrc file, if found.
 */
static void
492
process_psqlrc(void)
493
{
B
Bruce Momjian 已提交
494 495
	char	   *psqlrc;
	char	   *home;
496 497 498 499 500

#ifdef WIN32
#define R_OK 0
#endif

B
Bruce Momjian 已提交
501 502
	/* System-wide startup file */
	if (access("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, R_OK) == 0)
P
Peter Eisentraut 已提交
503
		process_file("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION);
B
Bruce Momjian 已提交
504
	else if (access("/etc/psqlrc", R_OK) == 0)
P
Peter Eisentraut 已提交
505
		process_file("/etc/psqlrc");
506

B
Bruce Momjian 已提交
507 508
	/* Look for one in the home dir */
	home = getenv("HOME");
509

B
Bruce Momjian 已提交
510 511 512 513 514
	if (home)
	{
		psqlrc = (char *) malloc(strlen(home) + 20);
		if (!psqlrc)
		{
P
Peter Eisentraut 已提交
515
            fprintf(stderr, "%s: out of memory\n", pset.progname);
B
Bruce Momjian 已提交
516 517
			exit(EXIT_FAILURE);
		}
518

B
Bruce Momjian 已提交
519 520
		sprintf(psqlrc, "%s/.psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, home);
		if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
521
			process_file(psqlrc);
B
Bruce Momjian 已提交
522 523 524 525
		else
		{
			sprintf(psqlrc, "%s/.psqlrc", home);
			if (access(psqlrc, R_OK) == 0)
P
Peter Eisentraut 已提交
526
				process_file(psqlrc);
B
Bruce Momjian 已提交
527 528
		}
		free(psqlrc);
529 530 531 532 533 534 535
	}
}



/* showVersion
 *
536
 * This output format is intended to match GNU standards.
537 538
 */
static void
539
showVersion(void)
540
{
541
    puts("psql (PostgreSQL) " PG_RELEASE "." PG_VERSION "." PG_SUBVERSION);
542

543 544
#if defined(USE_READLINE) || defined (USE_HISTORY) || defined(MULTIBYTE)
    fputs("contains ", stdout);
545 546

#ifdef USE_READLINE
547 548
    fputs("readline", stdout);
#define _Feature
549
#endif
550

551
#ifdef USE_HISTORY
552 553 554 555
#ifdef _Feature
    fputs(", ", stdout);
#else
#define _Feature
556
#endif
557
    fputs("history", stdout);
558
#endif
559 560 561 562 563 564

#ifdef MULTIBYTE
#ifdef _Feature
    fputs(", ", stdout);
#else
#define _Feature
565
#endif
566
    fputs("multibyte", stdout);
567
#endif
568 569
    
#undef _Feature
570

571 572
    puts(" support");
#endif
573

B
Bruce Momjian 已提交
574
    puts("Portions Copyright (c) 1996-2000, PostgreSQL, Inc");
B
Add:  
Bruce Momjian 已提交
575
    puts("Portions Copyright (C) 1996 Regents of the University of California");
576 577
    puts("Read the file COPYING or use the command \\copyright to see the");
    puts("usage and distribution terms.");
578
}