2 * psql - the PostgreSQL interactive terminal
4 * Copyright 2000 by PostgreSQL Global Development Group
6 * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.57 2002/09/22 20:57:21 petere Exp $
8 #include "postgres_fe.h"
16 #include <sys/ioctl.h> /* for ioctl() */
18 #include <pwd.h> /* for getpwuid() */
20 #include <sys/types.h> /* (ditto) */
21 #include <unistd.h> /* for getuid() */
33 #define _(x) gettext((x))
37 * If you change something in this file, also make the same changes
38 * in the DocBook documentation, file ref/psql-ref.sgml. If you don't
39 * know how to do it, please find someone who can help you.
46 * print out command line arguments
48 #define ON(var) (var ? _("on") : _("off"))
57 struct passwd *pw = NULL;
60 /* Find default user, in case we need it. */
61 user = getenv("PGUSER");
65 pw = getpwuid(geteuid());
70 psql_error("could not get current user name: %s\n", strerror(errno));
75 DWORD bufsize = sizeof(buf) - 1;
77 if (GetUserName(buf, &bufsize))
82 /* If this " is the start of the string then it ought to end there to fit in 80 columns >> " */
83 printf(_("This is psql %s, the PostgreSQL interactive terminal.\n\n"),
86 puts(_(" psql [OPTIONS] [DBNAME [USERNAME]]\n"));
88 puts(_(" -a Echo all input from script"));
89 puts(_(" -A Unaligned table output mode (-P format=unaligned)"));
90 puts(_(" -c COMMAND Run only single command (SQL or internal) and exit"));
92 /* Display default database */
93 env = getenv("PGDATABASE");
96 printf(_(" -d DBNAME Specify database name to connect to (default: %s)\n"), env);
98 puts(_(" -e Echo commands sent to server"));
99 puts(_(" -E Display queries that internal commands generate"));
100 puts(_(" -f FILENAME Execute commands from file, then exit"));
101 printf(_(" -F STRING Set field separator (default: \"%s\") (-P fieldsep=)\n"),
104 /* Display default host */
105 env = getenv("PGHOST");
106 printf(_(" -h HOSTNAME Specify database server host (default: %s)\n"),
107 env ? env : _("local socket"));
109 puts(_(" -H HTML table output mode (-P format=html)"));
110 puts(_(" -l List available databases, then exit"));
111 puts(_(" -n Disable enhanced command line editing (readline)"));
112 puts(_(" -o FILENAME Send query results to file (or |pipe)"));
114 /* Display default port */
115 env = getenv("PGPORT");
116 printf(_(" -p PORT Specify database server port (default: %s)\n"),
117 env ? env : DEF_PGPORT_STR);
119 puts(_(" -P VAR[=ARG] Set printing option 'VAR' to 'ARG' (see \\pset command)"));
120 puts(_(" -q Run quietly (no messages, only query output)"));
121 puts(_(" -R STRING Set record separator (default: newline) (-P recordsep=)"));
122 puts(_(" -s Single step mode (confirm each query)"));
123 puts(_(" -S Single line mode (end of line terminates SQL command)"));
124 puts(_(" -t Print rows only (-P tuples_only)"));
125 puts(_(" -T TEXT Set HTML table tag attributes (width, border) (-P tableattr=)"));
127 /* Display default user */
128 env = getenv("PGUSER");
131 printf(_(" -U NAME Specify database user name (default: %s)\n"), env);
133 puts(_(" -v NAME=VALUE Set psql variable 'NAME' to 'VALUE'"));
134 puts(_(" -V Show version information and exit"));
135 puts(_(" -W Prompt for password (should happen automatically)"));
136 puts(_(" -x Turn on expanded table output (-P expanded)"));
137 puts(_(" -X Do not read startup file (~/.psqlrc)"));
140 "\nFor more information, type \"\\?\" (for internal commands) or \"\\help\"\n"
141 "(for SQL commands) from within psql, or consult the psql section in\n"
142 "the PostgreSQL documentation.\n\n"
143 "Report bugs to <pgsql-bugs@postgresql.org>."));
150 * print out help for the backslash commands
162 slashUsage(bool pager)
167 /* check whether we need / can / are supposed to use pager */
171 isatty(fileno(stdin)) &&
172 isatty(fileno(stdout))
176 const char *pagerprog;
180 struct winsize screen_size;
182 result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
183 if (result == -1 || 50 > screen_size.ws_row)
186 pagerprog = getenv("PAGER");
188 pagerprog = DEFAULT_PAGER;
189 pagerfd = popen(pagerprog, "w");
199 pqsignal(SIGPIPE, SIG_IGN);
205 /* if you add/remove a line here, change the row test above */
208 * if this " is the start of the string then it ought to end there to
209 * fit in 80 columns >> "
211 fprintf(output, _(" \\a toggle between unaligned and aligned output mode\n"));
212 fprintf(output, _(" \\c[onnect] [DBNAME|- [USER]]\n"
213 " connect to new database (currently \"%s\")\n"),
215 fprintf(output, _(" \\C [STRING] set table title, or unset if none\n"));
216 fprintf(output, _(" \\cd [DIR] change the current working directory\n"));
217 fprintf(output, _(" \\copy ... perform SQL COPY with data stream to the client host\n"));
218 fprintf(output, _(" \\copyright show PostgreSQL usage and distribution terms\n"));
219 fprintf(output, _(" \\d [NAME] describe table, index, sequence, or view\n"));
220 fprintf(output, _(" \\d{t|i|s|v|S} [PATTERN] (add \"+\" for more detail)\n"
221 " list tables/indexes/sequences/views/system tables\n"));
222 fprintf(output, _(" \\da [PATTERN] list aggregate functions\n"));
223 fprintf(output, _(" \\dd [PATTERN] show comment for object\n"));
224 fprintf(output, _(" \\dD [PATTERN] list domains\n"));
225 fprintf(output, _(" \\df [PATTERN] list functions (add \"+\" for more detail)\n"));
226 fprintf(output, _(" \\do [NAME] list operators\n"));
227 fprintf(output, _(" \\dl list large objects, same as \\lo_list\n"));
228 fprintf(output, _(" \\dp [PATTERN] list table access privileges\n"));
229 fprintf(output, _(" \\dT [PATTERN] list data types (add \"+\" for more detail)\n"));
230 fprintf(output, _(" \\du [PATTERN] list users\n"));
231 fprintf(output, _(" \\e [FILE] edit the query buffer (or file) with external editor\n"));
232 fprintf(output, _(" \\echo [STRING] write string to standard output\n"));
233 fprintf(output, _(" \\encoding [ENCODING] show or set client encoding\n"));
234 fprintf(output, _(" \\f [STRING] show or set field separator for unaligned query output\n"));
235 fprintf(output, _(" \\g [FILE] send query buffer to server (and results to file or |pipe)\n"));
236 fprintf(output, _(" \\h [NAME] help on syntax of SQL commands, * for all commands\n"));
237 fprintf(output, _(" \\H toggle HTML output mode (currently %s)\n"),
238 ON(pset.popt.topt.format == PRINT_HTML));
239 fprintf(output, _(" \\i FILE execute commands from file\n"));
240 fprintf(output, _(" \\l list all databases\n"));
241 fprintf(output, _(" \\lo_export, \\lo_import, \\lo_list, \\lo_unlink\n"
242 " large object operations\n"));
243 fprintf(output, _(" \\o FILE send all query results to file or |pipe\n"));
244 fprintf(output, _(" \\p show the contents of the query buffer\n"));
245 fprintf(output, _(" \\pset NAME [VALUE] set table output option\n"
246 " (NAME := {format|border|expanded|fieldsep|null|recordsep|\n"
247 " tuples_only|title|tableattr|pager})\n"));
248 fprintf(output, _(" \\q quit psql\n"));
249 fprintf(output, _(" \\qecho [STRING] write string to query output stream (see \\o)\n"));
250 fprintf(output, _(" \\r reset (clear) the query buffer\n"));
251 fprintf(output, _(" \\s [FILE] display history or save it to file\n"));
252 fprintf(output, _(" \\set [NAME [VALUE]] set internal variable, or list all if no parameters\n"));
253 fprintf(output, _(" \\t show only rows (currently %s)\n"),
254 ON(pset.popt.topt.tuples_only));
255 fprintf(output, _(" \\T [STRING] set HTML <table> tag attributes, or unset if none\n"));
256 fprintf(output, _(" \\timing toggle timing of commands (currently %s)\n"),
258 fprintf(output, _(" \\unset NAME unset (delete) internal variable\n"));
259 fprintf(output, _(" \\w [FILE] write query buffer to file\n"));
260 fprintf(output, _(" \\x toggle expanded output (currently %s)\n"),
261 ON(pset.popt.topt.expanded));
262 fprintf(output, _(" \\z [PATTERN] list table access privileges (same as \\dp)\n"));
263 fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n"));
269 pqsignal(SIGPIPE, SIG_DFL);
277 * helpSQL -- help with SQL commands
281 helpSQL(const char *topic)
283 #define VALUE_OR_NULL(a) ((a) ? (a) : "")
285 if (!topic || strlen(topic) == 0)
288 int items_per_column = (QL_HELP_COUNT + 2) / 3;
290 puts(_("Available help:"));
292 for (i = 0; i < items_per_column; i++)
294 printf(" %-26s%-26s",
295 VALUE_OR_NULL(QL_HELP[i].cmd),
296 VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd));
297 if (i + 2 * items_per_column < QL_HELP_COUNT)
299 VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
307 bool help_found = false;
310 /* don't care about trailing spaces */
312 while (topic[len - 1] == ' ')
315 for (i = 0; QL_HELP[i].cmd; i++)
317 if (strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
318 strcmp(topic, "*") == 0)
321 printf(_("Command: %s\n"
324 QL_HELP[i].cmd, QL_HELP[i].help, QL_HELP[i].syntax);
325 /* If we have an exact match, exit. Fixes \h SELECT */
326 if (strcasecmp(topic, QL_HELP[i].cmd) == 0)
332 printf(_("No help available for '%-.*s'.\nTry \\h with no arguments to see available help.\n"), (int) len, topic);
339 print_copyright(void)
342 "PostgreSQL Data Base Management System\n\n"
343 "Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group\n\n"
344 "This software is based on Postgres95, formerly known as Postgres, which\n"
345 "contains the following notice:\n\n"
346 "Portions Copyright(c) 1994 - 7 Regents of the University of California\n\n"
347 "Permission to use, copy, modify, and distribute this software and its\n"
348 "documentation for any purpose, without fee, and without a written agreement\n"
349 "is hereby granted, provided that the above copyright notice and this paragraph\n"
350 "and the following two paragraphs appear in all copies.\n\n"
351 "IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR\n"
352 "DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST\n"
353 "PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF\n"
354 "THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH\n"
356 "THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,\n"
357 "BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\n"
358 "PARTICULAR PURPOSE.THE SOFTWARE PROVIDED HEREUNDER IS ON AN \"AS IS\" BASIS,\n"
359 "AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,\n"
360 "SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."