help.c 10.7 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.31 2000/05/26 15:47:18 momjian 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

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

32 33 34 35 36 37 38
/*
 * PLEASE:
 * If you change something in this file, also make the same changes
 * in the DocBook documentation, file ref/psql-ref.sgml. If you don't
 * know how to do it, please find someone who can help you.
 */

39 40 41 42

/*
 * usage
 *
43
 * print out command line arguments
44 45 46
 */
#define ON(var) (var ? "on" : "off")

B
Bruce Momjian 已提交
47 48
void
usage(void)
49
{
B
Bruce Momjian 已提交
50 51 52
	const char *env;
	const char *user;

53
#ifndef WIN32
B
Bruce Momjian 已提交
54
	struct passwd *pw = NULL;
55

56 57
#endif

B
Bruce Momjian 已提交
58 59 60 61
	/* Find default user, in case we need it. */
	user = getenv("USER");
	if (!user)
	{
62
#ifndef WIN32
P
Peter Eisentraut 已提交
63
		pw = getpwuid(geteuid());
B
Bruce Momjian 已提交
64 65 66 67
		if (pw)
			user = pw->pw_name;
		else
		{
68
			psql_error("could not get current user name: %s\n", strerror(errno));
B
Bruce Momjian 已提交
69 70
			exit(EXIT_FAILURE);
		}
71
#else
B
Bruce Momjian 已提交
72
		user = "?";
73
#endif
B
Bruce Momjian 已提交
74
	}
75

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

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

91 92 93 94
	puts("  -e              Echo queries sent to backend");
	puts("  -E              Display queries that internal commands generate");
	puts("  -f <filename>   Execute queries from file, then exit");
	puts("  -F <string>     Set field separator (default: \"" DEFAULT_FIELD_SEP "\") (-P fieldsep=)");
B
Bruce Momjian 已提交
95 96 97

	/* Display default host */
	env = getenv("PGHOST");
P
Peter Eisentraut 已提交
98
	printf("  -h <host>       Specify database server host (default: ");
B
Bruce Momjian 已提交
99
	if (env)
100
		fputs(env, stdout);
B
Bruce Momjian 已提交
101
	else
102 103
		fputs("domain socket", stdout);
	puts(")");
B
Bruce Momjian 已提交
104

105 106
	puts("  -H              HTML table output mode (-P format=html)");
	puts("  -l              List available databases, then exit");
107
	puts("  -n              Disable readline");
108
	puts("  -o <filename>   Send query output to filename (or |pipe)");
B
Bruce Momjian 已提交
109 110 111

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

115 116 117 118 119
	puts("  -P var[=arg]    Set printing option 'var' to 'arg' (see \\pset command)");
	puts("  -q              Run quietly (no messages, only query output)");
	puts("  -R <string>     Set record separator (default: newline) (-P recordsep=)");
	puts("  -s              Single step mode (confirm each query)");
	puts("  -S              Single line mode (newline terminates query)");
B
Bruce Momjian 已提交
120
	puts("  -t              Print rows only (-P tuples_only)");
121
	puts("  -T text         Set HTML table tag options (width, border) (-P tableattr=)");
B
Bruce Momjian 已提交
122 123 124 125 126

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

129 130 131 132 133
	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)");
	puts("  -x              Turn on expanded table output (-P expanded)");
	puts("  -X              Do not read startup file (~/.psqlrc)");
B
Bruce Momjian 已提交
134

135 136 137 138 139
	puts("\nFor more information, type \"\\?\" (for internal commands) or \"\\help\"");
	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>.");
	puts("Report bugs to <pgsql-bugs@postgresql.org>.");
140 141

#ifndef WIN32
B
Bruce Momjian 已提交
142 143
	if (pw)
		free(pw);
144 145 146 147 148 149 150 151 152 153 154 155
#endif
}



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

#ifndef TIOCGWINSZ
B
Bruce Momjian 已提交
156 157 158 159
struct winsize
{
	int			ws_row;
	int			ws_col;
160
};
B
Bruce Momjian 已提交
161

162 163 164
#endif

void
165
slashUsage(void)
166
{
B
Bruce Momjian 已提交
167 168 169 170
	bool		usePipe = false;
	const char *pagerenv;
	FILE	   *fout;
	struct winsize screen_size;
171 172

#ifdef TIOCGWINSZ
173
	if (pset.notty == 0 &&
B
Bruce Momjian 已提交
174 175 176 177
		(ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1 ||
		 screen_size.ws_col == 0 ||
		 screen_size.ws_row == 0))
	{
178
#endif
B
Bruce Momjian 已提交
179 180
		screen_size.ws_row = 24;
		screen_size.ws_col = 80;
181
#ifdef TIOCGWINSZ
B
Bruce Momjian 已提交
182
	}
183 184
#endif

185
	if (pset.notty == 0 &&
B
Bruce Momjian 已提交
186 187
		(pagerenv = getenv("PAGER")) &&
		(pagerenv[0] != '\0') &&
188
		screen_size.ws_row <= 39 &&
B
Bruce Momjian 已提交
189 190 191
		(fout = popen(pagerenv, "w")))
	{
		usePipe = true;
B
Hi!  
Bruce Momjian 已提交
192
#ifndef WIN32
B
Bruce Momjian 已提交
193
		pqsignal(SIGPIPE, SIG_IGN);
B
Hi!  
Bruce Momjian 已提交
194
#endif
B
Bruce Momjian 已提交
195 196 197 198 199
	}
	else
		fout = stdout;

	/* if you add/remove a line here, change the row test above */
200
	fprintf(fout, " \\a             toggle between unaligned and aligned mode\n");
201
	fprintf(fout, " \\c[onnect] [dbname|- [user]]\n"
202 203
			"                connect to new database (currently '%s')\n", PQdb(pset.db));
	fprintf(fout, " \\C <title>     table title\n");
204
	fprintf(fout, " \\copy ...      perform SQL COPY with data stream to the client machine\n");
205 206
	fprintf(fout, " \\copyright     show PostgreSQL usage and distribution terms\n");
	fprintf(fout, " \\d <table>     describe table (or view, index, sequence)\n");
207 208
	fprintf(fout, " \\d{t|i|s|v}    list tables/indices/sequences/views\n");
	fprintf(fout, " \\d{p|S|l}      list permissions/system tables/lobjects\n");
209 210 211 212 213
	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");
214
	fprintf(fout, " \\e [file]      edit the current query buffer or [file] with external editor\n");
215
	fprintf(fout, " \\echo <text>   write text to stdout\n");
216
	fprintf(fout, " \\encoding <encoding>  set client encoding\n");
217
	fprintf(fout, " \\f <sep>       change field separator\n");
218
	fprintf(fout, " \\g [file]      send query to backend (and results in [file] or |pipe)\n");
219
	fprintf(fout, " \\h [cmd]       help on syntax of sql commands, * for all commands\n");
220 221
	fprintf(fout, " \\H             toggle HTML mode (currently %s)\n",
			ON(pset.popt.topt.format == PRINT_HTML));
222
	fprintf(fout, " \\i <file>      read and execute queries from <file>\n");
223 224
	fprintf(fout, " \\l             list all databases\n");
	fprintf(fout, " \\lo_export, \\lo_import, \\lo_list, \\lo_unlink\n"
225
			"                large object operations\n");
226
	fprintf(fout, " \\o [file]      send all query results to [file], or |pipe\n");
227
	fprintf(fout, " \\p             show the content of the current query buffer\n");
228 229
	fprintf(fout, " \\pset <opt>    set table output  <opt> = {format|border|expanded|fieldsep|\n"
			"                null|recordsep|tuples_only|title|tableattr|pager}\n");
230 231 232
	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");
233
	fprintf(fout, " \\s [file]      print history or save it in [file]\n");
234
	fprintf(fout, " \\set <var> <value>  set internal variable\n");
B
Bruce Momjian 已提交
235
	fprintf(fout, " \\t             show only rows (currently %s)\n", ON(pset.popt.topt.tuples_only));
236
	fprintf(fout, " \\T <tags>      HTML table tags\n");
237
	fprintf(fout, " \\unset <var>   unset (delete) internal variable\n");
238
	fprintf(fout, " \\w <file>      write current query buffer to a <file>\n");
239
	fprintf(fout, " \\x             toggle expanded output (currently %s)\n", ON(pset.popt.topt.expanded));
240 241
	fprintf(fout, " \\z             list table access permissions\n");
	fprintf(fout, " \\! [cmd]       shell escape or command\n");
B
Bruce Momjian 已提交
242 243 244 245

	if (usePipe)
	{
		pclose(fout);
B
Hi!  
Bruce Momjian 已提交
246
#ifndef WIN32
B
Bruce Momjian 已提交
247
		pqsignal(SIGPIPE, SIG_DFL);
B
Hi!  
Bruce Momjian 已提交
248
#endif
B
Bruce Momjian 已提交
249
	}
250 251 252 253 254 255 256 257 258 259 260
}



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

B
Bruce Momjian 已提交
263
	if (!topic || strlen(topic) == 0)
264
	{
265
		int			i;
266
		int			items_per_column = (QL_HELP_COUNT + 2) / 3;
B
Bruce Momjian 已提交
267

268
		puts("Available help:");
B
Bruce Momjian 已提交
269

270
		for (i = 0; i < items_per_column; i++)
B
Bruce Momjian 已提交
271
		{
P
Peter Eisentraut 已提交
272
			printf("  %-26s%-26s",
273 274 275
					VALUE_OR_NULL(QL_HELP[i].cmd),
					VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd));
			if (i + 2 * items_per_column < QL_HELP_COUNT)
P
Peter Eisentraut 已提交
276
				printf("%-26s",
277 278
					VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
			fputc('\n', stdout);
B
Bruce Momjian 已提交
279
		}
280 281
	}

B
Bruce Momjian 已提交
282
	else
283
	{
B
Bruce Momjian 已提交
284 285
		int			i;
		bool		help_found = false;
P
Peter Eisentraut 已提交
286 287 288 289 290 291
		size_t      len;

		/* don't care about trailing spaces */
		len = strlen(topic);
		while (topic[len-1] == ' ')
			len--;
B
Bruce Momjian 已提交
292 293 294

		for (i = 0; QL_HELP[i].cmd; i++)
		{
P
Peter Eisentraut 已提交
295
			if (strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
B
Bruce Momjian 已提交
296 297 298
				strcmp(topic, "*") == 0)
			{
				help_found = true;
299
				printf("Command:     %s\n"
300 301 302
					   "Description: %s\n"
					   "Syntax:\n%s\n\n",
					 QL_HELP[i].cmd, QL_HELP[i].help, QL_HELP[i].syntax);
303 304 305
				/* If we have an exact match, exit.  Fixes \h SELECT */
				if (strcasecmp(topic, QL_HELP[i].cmd) == 0)
					break;
B
Bruce Momjian 已提交
306 307 308 309
			}
		}

		if (!help_found)
P
Peter Eisentraut 已提交
310
			printf("No help available for '%-.*s'.\nTry \\h with no arguments to see available help.\n", (int)len, topic);
311 312 313 314 315 316 317 318
	}
}



void
print_copyright(void)
{
B
Bruce Momjian 已提交
319
	puts(
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
		 "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."
	);
340
}