help.c 10.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/help.c,v 1.20 2000/02/19 05:01:16 ishii Exp $
P
Peter Eisentraut 已提交
7
 */
8
#include "postgres.h"
9 10 11
#include "help.h"

#include <signal.h>
12
#include <errno.h>
13 14

#ifndef WIN32
B
Bruce Momjian 已提交
15
#include <sys/ioctl.h>			/* for ioctl() */
16
#ifdef HAVE_PWD_H
B
Bruce Momjian 已提交
17
#include <pwd.h>				/* for getpwuid() */
18
#endif
B
Bruce Momjian 已提交
19 20
#include <sys/types.h>			/* (ditto) */
#include <unistd.h>				/* for getuid() */
21
#else
22
#include <win32.h>
23 24
#endif

25 26
#include "pqsignal.h"
#include "libpq-fe.h"
27 28 29 30 31 32 33 34 35

#include "settings.h"
#include "common.h"
#include "sql_help.h"


/*
 * usage
 *
36
 * print out command line arguments
37 38 39
 */
#define ON(var) (var ? "on" : "off")

B
Bruce Momjian 已提交
40 41
void
usage(void)
42
{
B
Bruce Momjian 已提交
43 44 45
	const char *env;
	const char *user;

46
#ifndef WIN32
B
Bruce Momjian 已提交
47
	struct passwd *pw = NULL;
48 49
#endif

B
Bruce Momjian 已提交
50 51 52 53
	/* Find default user, in case we need it. */
	user = getenv("USER");
	if (!user)
	{
54
#ifndef WIN32
P
Peter Eisentraut 已提交
55
		pw = getpwuid(geteuid());
B
Bruce Momjian 已提交
56 57 58 59
		if (pw)
			user = pw->pw_name;
		else
		{
60
			psql_error("could not get current user name: %s", strerror(errno));
B
Bruce Momjian 已提交
61 62
			exit(EXIT_FAILURE);
		}
63
#else
B
Bruce Momjian 已提交
64
		user = "?";
65
#endif
B
Bruce Momjian 已提交
66
	}
67

68
/* If this " is the start of the string then it ought to end there to fit in 80 columns >> " */
69 70 71 72
    puts(  "This is psql, the PostgreSQL interactive terminal.\n");
	puts(  "Usage:");
    puts(  "  psql [options] [dbname [username]]\n");
    puts(  "Options:");
73
	puts(  "  -a              Echo all input from script");
P
Peter Eisentraut 已提交
74 75
	puts(  "  -A              Unaligned table output mode (-P format=unaligned)");
	puts(  "  -c <query>      Run only single query (or slash command) and exit");
B
Bruce Momjian 已提交
76 77 78 79 80

	/* Display default database */
	env = getenv("PGDATABASE");
	if (!env)
		env = user;
P
Peter Eisentraut 已提交
81
	printf("  -d <dbname>     Specify database name to connect to (default: %s)\n", env);
B
Bruce Momjian 已提交
82

83
    puts(  "  -e              Echo queries sent to backend");
84
	puts(  "  -E              Display queries that internal commands generate");
P
Peter Eisentraut 已提交
85 86
	puts(  "  -f <filename>   Execute queries from file, then exit");
	puts(  "  -F <string>     Set field separator (default: \"" DEFAULT_FIELD_SEP "\") (-P fieldsep=)");
B
Bruce Momjian 已提交
87 88 89

	/* Display default host */
	env = getenv("PGHOST");
P
Peter Eisentraut 已提交
90
	printf("  -h <host>       Specify database server host (default: ");
B
Bruce Momjian 已提交
91
	if (env)
92
		fputs(env, stdout);
B
Bruce Momjian 已提交
93
	else
94 95
		fputs("domain socket", stdout);
	puts(")");
B
Bruce Momjian 已提交
96

97 98
	puts(  "  -H              HTML table output mode (-P format=html)");
	puts(  "  -l              List available databases, then exit");
P
Peter Eisentraut 已提交
99
	puts(  "  -o <filename>   Send query output to filename (or |pipe)");
B
Bruce Momjian 已提交
100 101 102

	/* Display default port */
	env = getenv("PGPORT");
P
Peter Eisentraut 已提交
103
	printf("  -p <port>       Specify database server port (default: %s)\n",
B
Bruce Momjian 已提交
104
	   env ? env : "hardwired");
B
Bruce Momjian 已提交
105

106 107
	puts(  "  -P var[=arg]    Set printing option 'var' to 'arg' (see \\pset command)");
	puts(  "  -q              Run quietly (no messages, only query output)");
108
    puts(  "  -R <string>     Set record separator (default: newline) (-P recordsep=)");
109 110 111 112
	puts(  "  -s              Single step mode (confirm each query)");
	puts(  "  -S              Single line mode (newline terminates query)");
	puts(  "  -t              Don't print headings and row count (-P tuples_only)");
	puts(  "  -T text         Set HTML table tag options (width, border) (-P tableattr=)");
B
Bruce Momjian 已提交
113 114 115 116 117

	/* Display default user */
	env = getenv("PGUSER");
	if (!env)
		env = user;
118
	printf("  -U <username>   Specify database username (default: %s)\n", env);
B
Bruce Momjian 已提交
119

120 121 122 123
	puts(  "  -x              Turn on expanded table output (-P expanded)");
	puts(  "  -v name=val     Set psql variable 'name' to 'value'");
	puts(  "  -V              Show version information and exit");
	puts(  "  -W              Prompt for password (should happen automatically)");
B
Bruce Momjian 已提交
124

125
    puts(  "\nFor more information, type \"\\?\" (for internal commands) or \"\\help\"");
P
Peter Eisentraut 已提交
126 127 128
    puts(  "(for SQL commands) from within psql, or consult the psql section in");
    puts(  "the PostgreSQL manual, which accompanies the distribution and is also");
    puts(  "available at <http://www.postgresql.org>.");
129
    puts(  "Report bugs to <pgsql-bugs@postgresql.org>.");
130 131

#ifndef WIN32
B
Bruce Momjian 已提交
132 133
	if (pw)
		free(pw);
134 135 136 137 138 139 140 141 142 143 144 145
#endif
}



/*
 * slashUsage
 *
 * print out help for the backslash commands
 */

#ifndef TIOCGWINSZ
B
Bruce Momjian 已提交
146 147 148 149
struct winsize
{
	int			ws_row;
	int			ws_col;
150
};
B
Bruce Momjian 已提交
151

152 153 154
#endif

void
155
slashUsage(void)
156
{
B
Bruce Momjian 已提交
157 158 159 160
	bool		usePipe = false;
	const char *pagerenv;
	FILE	   *fout;
	struct winsize screen_size;
161 162

#ifdef TIOCGWINSZ
163
	if (pset.notty == 0 &&
B
Bruce Momjian 已提交
164 165 166 167
		(ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1 ||
		 screen_size.ws_col == 0 ||
		 screen_size.ws_row == 0))
	{
168
#endif
B
Bruce Momjian 已提交
169 170
		screen_size.ws_row = 24;
		screen_size.ws_col = 80;
171
#ifdef TIOCGWINSZ
B
Bruce Momjian 已提交
172
	}
173 174
#endif

175
	if (pset.notty == 0 &&
B
Bruce Momjian 已提交
176 177 178 179 180 181
		(pagerenv = getenv("PAGER")) &&
		(pagerenv[0] != '\0') &&
		screen_size.ws_row <= 36 &&
		(fout = popen(pagerenv, "w")))
	{
		usePipe = true;
B
Hi!  
Bruce Momjian 已提交
182
#ifndef WIN32
B
Bruce Momjian 已提交
183
		pqsignal(SIGPIPE, SIG_IGN);
B
Hi!  
Bruce Momjian 已提交
184
#endif
B
Bruce Momjian 已提交
185 186 187 188 189
	}
	else
		fout = stdout;

	/* if you add/remove a line here, change the row test above */
190
	fprintf(fout, " \\c[onnect] [dbname|- [user]]\n"
B
Bruce Momjian 已提交
191
		  "                 connect to new database (currently '%s')\n", PQdb(pset.db));
192
	fprintf(fout, " \\copy ...      perform SQL COPY with data stream to the client machine\n");
193 194 195 196 197 198 199 200 201 202
	fprintf(fout, " \\copyright     show PostgreSQL usage and distribution terms\n");
	fprintf(fout, " \\d <table>     describe table (or view, index, sequence)\n");
	fprintf(fout, " \\d{i|s|t|v|S}  list only indices/sequences/tables/views/system tables\n");
	fprintf(fout, " \\da            list aggregates\n");
	fprintf(fout, " \\dd [object]   list comment for table, type, function, or operator\n");
	fprintf(fout, " \\df            list functions\n");
	fprintf(fout, " \\do            list operators\n");
	fprintf(fout, " \\dT            list data types\n");
	fprintf(fout, " \\e [fname]     edit the current query buffer or <fname> with external editor\n");
	fprintf(fout, " \\echo <text>   write text to stdout\n");
203 204 205 206
#ifdef MULTIBYTE
	fprintf(fout, " \\eset <encoding> set client encoding\n");
	fprintf(fout, " \\eshow         show client encoding\n");
#endif
207 208 209 210 211
	fprintf(fout, " \\g [fname]     send query to backend (and results in <fname> or |pipe)\n");
	fprintf(fout, " \\h [cmd]       help on syntax of sql commands, * for all commands\n");
	fprintf(fout, " \\i <fname>     read and execute queries from filename\n");
	fprintf(fout, " \\l             list all databases\n");
	fprintf(fout, " \\lo_export, \\lo_import, \\lo_list, \\lo_unlink\n"
212
            "                  large object operations\n");
213 214
	fprintf(fout, " \\o [fname]     send all query results to <fname>, or |pipe\n");
	fprintf(fout, " \\p             show the content of the current query buffer\n");
215 216
	fprintf(fout, " \\pset {format|border|expanded|fieldsep|recordsep|tuples_only|title|tableattr\n"
            "     |pager}    set table output options\n");
217 218 219 220
	fprintf(fout, " \\q             quit psql\n");
	fprintf(fout, " \\qecho <text>  write text to query output stream (see \\o)\n");
	fprintf(fout, " \\r             reset (clear) the query buffer\n");
	fprintf(fout, " \\s [fname]     print history or save it in <fname>\n");
221 222 223 224
	fprintf(fout, " \\set <var> <value>  set internal variable\n");
	fprintf(fout, " \\t             don't show table headers or footers (currently %s)\n", ON(pset.popt.topt.tuples_only));
	fprintf(fout, " \\unset <var>   unset (delete) internal variable\n");
	fprintf(fout, " \\x             toggle expanded output (currently %s)\n", ON(pset.popt.topt.expanded));
225 226 227
	fprintf(fout, " \\w <fname>     write current query buffer to a file\n");
	fprintf(fout, " \\z             list table access permissions\n");
	fprintf(fout, " \\! [cmd]       shell escape or command\n");
B
Bruce Momjian 已提交
228 229 230 231

	if (usePipe)
	{
		pclose(fout);
B
Hi!  
Bruce Momjian 已提交
232
#ifndef WIN32
B
Bruce Momjian 已提交
233
		pqsignal(SIGPIPE, SIG_DFL);
B
Hi!  
Bruce Momjian 已提交
234
#endif
B
Bruce Momjian 已提交
235
	}
236 237 238 239 240 241 242 243 244 245 246
}



/*
 * helpSQL -- help with SQL commands
 *
 */
void
helpSQL(const char *topic)
{
247 248
#define VALUE_OR_NULL(a) ((a) ? (a) : "")

B
Bruce Momjian 已提交
249
	if (!topic || strlen(topic) == 0)
250
	{
251 252
		int			i;
        int         items_per_column = (QL_HELP_COUNT + 2)/3;
B
Bruce Momjian 已提交
253

254
		puts("Available help:");
B
Bruce Momjian 已提交
255

256
        for (i = 0; i < items_per_column; i++)
B
Bruce Momjian 已提交
257
		{
258 259 260 261 262
            printf("    %-25s%-25s%-25s\n",
                   VALUE_OR_NULL(QL_HELP[i].cmd),
                   VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd),
                   VALUE_OR_NULL(QL_HELP[i + 2*items_per_column].cmd)
                   );
B
Bruce Momjian 已提交
263
		}
264
        putc('\n', stdout);
265 266
	}

B
Bruce Momjian 已提交
267
	else
268
	{
B
Bruce Momjian 已提交
269 270 271 272 273 274 275 276 277
		int			i;
		bool		help_found = false;

		for (i = 0; QL_HELP[i].cmd; i++)
		{
			if (strcasecmp(QL_HELP[i].cmd, topic) == 0 ||
				strcmp(topic, "*") == 0)
			{
				help_found = true;
278 279 280 281
				printf("Command:     %s\n"
                       "Description: %s\n"
                       "Syntax:\n%s\n\n",
                       QL_HELP[i].cmd, QL_HELP[i].help, QL_HELP[i].syntax);
B
Bruce Momjian 已提交
282 283 284 285 286
			}
		}

		if (!help_found)
			printf("No help available for '%s'.\nTry \\h with no arguments to see available help.\n", topic);
287 288 289 290 291 292 293 294
	}
}



void
print_copyright(void)
{
B
Bruce Momjian 已提交
295
	puts(
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
        "PostgreSQL Data Base Management System\n\n"
        "Portions Copyright (c) 1996-2000, PostgreSQL, Inc\n\n"
        "This software is based on Postgres95, formerly known as Postgres, which\n"
        "contains the following notice:\n\n"
        "Portions Copyright(c) 1994 - 7 Regents of the University of California\n\n"
        "Permission to use, copy, modify, and distribute this software and its\n"
        "documentation for any purpose, without fee, and without a written agreement\n"
        "is hereby granted, provided that the above copyright notice and this paragraph\n"
        "and the following two paragraphs appear in all copies.\n\n"
        "IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR\n"
        "DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST\n"
        "PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF\n"
        "THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH\n"
        "DAMAGE.\n\n"
        "THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,\n"
        "BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\n"
        "PARTICULAR PURPOSE.THE SOFTWARE PROVIDED HEREUNDER IS ON AN \"AS IS\" BASIS,\n"
        "AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,\n"
        "SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
        );
316
}