]> granicus.if.org Git - postgresql/blob - src/bin/psql/help.c
e99f6f17204b4ec48d19314c848cbf53f4330c04
[postgresql] / src / bin / psql / help.c
1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Copyright 2000 by PostgreSQL Global Development Group
5  *
6  * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.66 2002/12/11 23:07:06 momjian Exp $
7  */
8 #include "postgres_fe.h"
9 #include "common.h"
10 #include "print.h"
11 #include "help.h"
12
13 #include <signal.h>
14 #include <errno.h>
15
16 #ifndef WIN32
17 #ifdef HAVE_PWD_H
18 #include <pwd.h>                                /* for getpwuid() */
19 #endif
20 #include <sys/types.h>                  /* (ditto) */
21 #include <unistd.h>                             /* for getuid() */
22 #else
23 #include <win32.h>
24 #endif
25
26 #include "pqsignal.h"
27 #include "libpq-fe.h"
28
29 #include "settings.h"
30 #include "common.h"
31 #include "sql_help.h"
32
33 #define _(x) gettext((x))
34
35 /*
36  * PLEASE:
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.
40  */
41
42
43 /*
44  * usage
45  *
46  * print out command line arguments
47  */
48 #define ON(var) (var ? _("on") : _("off"))
49
50 void
51 usage(void)
52 {
53         const char *env;
54         const char *user;
55
56 #ifndef WIN32
57         struct passwd *pw = NULL;
58 #endif
59
60         /* Find default user, in case we need it. */
61         user = getenv("PGUSER");
62         if (!user)
63         {
64 #ifndef WIN32
65                 pw = getpwuid(geteuid());
66                 if (pw)
67                         user = pw->pw_name;
68                 else
69                 {
70                         psql_error("could not get current user name: %s\n", strerror(errno));
71                         exit(EXIT_FAILURE);
72                 }
73 #else                                                   /* WIN32 */
74                 char            buf[128];
75                 DWORD           bufsize = sizeof(buf) - 1;
76
77                 if (GetUserName(buf, &bufsize))
78                         user = buf;
79 #endif   /* WIN32 */
80         }
81
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"),
84                    PG_VERSION);
85         puts(_("Usage:"));
86         puts(_("  psql [OPTIONS]... [DBNAME [USERNAME]]\n"));
87
88         puts(_("General options:"));
89         /* Display default database */
90         env = getenv("PGDATABASE");
91         if (!env)
92                 env = user;
93         printf(_("  -d DBNAME       specify database name to connect to (default: %s)\n"), env);
94         puts(_("  -c COMMAND      run only single command (SQL or internal) and exit"));
95         puts(_("  -f FILENAME     execute commands from file, then exit"));
96         puts(_("  -l              list available databases, then exit"));
97         puts(_("  -v NAME=VALUE   set psql variable 'NAME' to 'VALUE'"));
98         puts(_("  -X              do not read startup file (~/.psqlrc)"));
99         puts(_("  --help          show this help, then exit"));
100         puts(_("  --version       output version information, then exit"));
101
102         puts(_("\nInput and output options:"));
103         puts(_("  -a              echo all input from script"));
104         puts(_("  -e              echo commands sent to server"));
105         puts(_("  -E              display queries that internal commands generate"));
106         puts(_("  -q              run quietly (no messages, only query output)"));
107         puts(_("  -o FILENAME     send query results to file (or |pipe)"));
108         puts(_("  -n              disable enhanced command line editing (readline)"));
109         puts(_("  -s              single step mode (confirm each query)"));
110         puts(_("  -S              single line mode (end of line terminates SQL command)"));
111
112         puts(_("\nOutput format options:"));
113         puts(_("  -A              unaligned table output mode (-P format=unaligned)"));
114         puts(_("  -H              HTML table output mode (-P format=html)"));
115         puts(_("  -t              print rows only (-P tuples_only)"));
116         puts(_("  -T TEXT         set HTML table tag attributes (width, border) (-P tableattr=)"));
117         puts(_("  -x              turn on expanded table output (-P expanded)"));
118         puts(_("  -P VAR[=ARG]    set printing option 'VAR' to 'ARG' (see \\pset command)"));
119         printf(_("  -F STRING       set field separator (default: \"%s\") (-P fieldsep=)\n"),
120                    DEFAULT_FIELD_SEP);
121         puts(_("  -R STRING       set record separator (default: newline) (-P recordsep=)"));
122
123         puts(_("\nConnection options:"));
124         /* Display default host */
125         env = getenv("PGHOST");
126         printf(_("  -h HOSTNAME     specify database server host (default: %s)\n"),
127                    env ? env : _("local socket"));
128         /* Display default port */
129         env = getenv("PGPORT");
130         printf(_("  -p PORT         specify database server port (default: %s)\n"),
131                    env ? env : DEF_PGPORT_STR);
132         /* Display default user */
133         env = getenv("PGUSER");
134         if (!env)
135                 env = user;
136         printf(_("  -U NAME         specify database user name (default: %s)\n"), env);
137         puts(_("  -W              prompt for password (should happen automatically)"));
138
139         puts(_(
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>."));
144 }
145
146
147 /*
148  * slashUsage
149  *
150  * print out help for the backslash commands
151  */
152
153 #ifndef TIOCGWINSZ
154 struct winsize
155 {
156         int                     ws_row;
157         int                     ws_col;
158 };
159 #endif
160
161 void
162 slashUsage(unsigned short int pager)
163 {
164         FILE       *output;
165
166         output = PageOutput(50, pager);
167
168         /* if you add/remove a line here, change the row count above */
169
170         /*
171          * if this " is the start of the string then it ought to end there to
172          * fit in 80 columns >> "
173          */
174         fprintf(output, _("General\n"));
175         fprintf(output, _(" \\c[onnect] [DBNAME|- [USER]]\n"
176                                           "                connect to new database (currently \"%s\")\n"),
177                         PQdb(pset.db));
178         fprintf(output, _(" \\q             quit psql\n"));
179         fprintf(output, _(" \\h [NAME]      help on syntax of SQL commands, * for all commands\n"));
180         fprintf(output, _(" \\copyright     show PostgreSQL usage and distribution terms\n"));
181         fprintf(output, _(" \\cd [DIR]      change the current working directory\n"));
182         fprintf(output, _(" \\! [COMMAND]   execute command in shell or start interactive shell\n"));
183         fprintf(output, _(" \\encoding [ENCODING]  show or set client encoding\n"));
184         fprintf(output, _(" \\set [NAME [VALUE]]  set internal variable, or list all if no parameters\n"));
185         fprintf(output, _(" \\unset NAME    unset (delete) internal variable\n"));
186         fprintf(output, _(" \\timing        toggle timing of commands (currently %s)\n"),
187                         ON(pset.timing));
188         fprintf(output, _("\n"));
189
190         fprintf(output, _("Query Buffer\n"));
191         fprintf(output, _(" \\g [FILE]      send query buffer to server (and results to file or |pipe)\n"));
192         fprintf(output, _(" \\r             reset (clear) the query buffer\n"));
193         fprintf(output, _(" \\e [FILE]      edit the query buffer (or file) with external editor\n"));
194         fprintf(output, _(" \\p             show the contents of the query buffer\n"));
195         fprintf(output, _(" \\w [FILE]      write query buffer to file\n"));
196         fprintf(output, _(" \\s [FILE]      display history or save it to file\n"));
197         fprintf(output, _("\n"));
198
199         fprintf(output, _("I/O Redirection\n"));
200         fprintf(output, _(" \\i FILE        execute commands from file\n"));
201         fprintf(output, _(" \\o FILE        send all query results to file or |pipe\n"));
202         fprintf(output, _("\n"));
203
204         fprintf(output, _("Informational\n"));
205         fprintf(output, _(" \\d [NAME]      describe table, index, sequence, or view\n"));
206         fprintf(output, _(" \\d{t|i|s|v|S} [PATTERN] (add \"+\" for more detail)\n"
207                                           "                list tables/indexes/sequences/views/system tables\n"));
208         fprintf(output, _(" \\da [PATTERN]  list aggregate functions\n"));
209         fprintf(output, _(" \\dd [PATTERN]  show comment for object\n"));
210         fprintf(output, _(" \\dD [PATTERN]  list domains\n"));
211         fprintf(output, _(" \\df [PATTERN]  list functions (add \"+\" for more detail)\n"));
212         fprintf(output, _(" \\do [NAME]     list operators\n"));
213         fprintf(output, _(" \\dl            list large objects, same as \\lo_list\n"));
214         fprintf(output, _(" \\dp [PATTERN]  list table access privileges\n"));
215         fprintf(output, _(" \\dT [PATTERN]  list data types (add \"+\" for more detail)\n"));
216         fprintf(output, _(" \\du [PATTERN]  list users\n"));
217         fprintf(output, _(" \\l             list all databases (add \"+\" for more detail)\n"));
218         fprintf(output, _(" \\z [PATTERN]   list table access privileges (same as \\dp)\n"));
219         fprintf(output, _("\n"));
220
221         fprintf(output, _("Formatting\n"));
222         fprintf(output, _(" \\x             toggle expanded output (currently %s)\n"),
223                         ON(pset.popt.topt.expanded));
224         fprintf(output, _(" \\a             toggle between unaligned and aligned output mode\n"));
225         fprintf(output, _(" \\f [STRING]    show or set field separator for unaligned query output\n"));
226         fprintf(output, _(" \\pset NAME [VALUE]  set table output option\n"
227                                           "                (NAME := {format|border|expanded|fieldsep|null|recordsep|\n"
228                                           "                tuples_only|title|tableattr|pager})\n"));
229         fprintf(output, _(" \\C [STRING]    set table title, or unset if none\n"));
230         fprintf(output, _(" \\t             show only rows (currently %s)\n"),
231                         ON(pset.popt.topt.tuples_only));
232         fprintf(output, _(" \\echo [STRING] write string to standard output\n"));
233         fprintf(output, _(" \\qecho [STRING]  write string to query output stream (see \\o)\n"));
234         fprintf(output, _(" \\H             toggle HTML output mode (currently %s)\n"),
235                         ON(pset.popt.topt.format == PRINT_HTML));
236         fprintf(output, _(" \\T [STRING]    set HTML <table> tag attributes, or unset if none\n"));
237         fprintf(output, _("\n"));
238
239         fprintf(output, _("Large Object, Copy\n"));
240         fprintf(output, _(" \\lo_export\n"));
241         fprintf(output, _(" \\lo_import\n"));
242         fprintf(output, _(" \\lo_list\n"));
243         fprintf(output, _(" \\lo_unlink     large object operations\n"));
244         fprintf(output, _(" \\copy ...      perform SQL COPY with data stream to the client host\n"));
245         fprintf(output, _("\n"));
246
247         if (output != stdout)
248         {
249                 pclose(output);
250 #ifndef WIN32
251                 pqsignal(SIGPIPE, SIG_DFL);
252 #endif
253         }
254 }
255
256
257
258 /*
259  * helpSQL -- help with SQL commands
260  *
261  */
262 void
263 helpSQL(const char *topic, bool pager)
264 {
265 #define VALUE_OR_NULL(a) ((a) ? (a) : "")
266
267         if (!topic || strlen(topic) == 0)
268         {
269                 int                     i;
270                 int                     items_per_column = (QL_HELP_COUNT + 2) / 3;
271                 FILE            *output;
272
273                 output = PageOutput(items_per_column, pager);
274
275                 fputs(_("Available help:\n"), output);
276
277                 for (i = 0; i < items_per_column; i++)
278                 {
279                         fprintf(output, "  %-26s%-26s",
280                                    VALUE_OR_NULL(QL_HELP[i].cmd),
281                                    VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd));
282                         if (i + 2 * items_per_column < QL_HELP_COUNT)
283                                 fprintf(output, "%-26s",
284                                    VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
285                         fputc('\n', output);
286                 }
287                 /* Only close if we used the pager */
288                 if (output != stdout)
289                 {
290                         pclose(output);
291 #ifndef WIN32
292                         pqsignal(SIGPIPE, SIG_DFL);
293 #endif
294                 }
295         }
296         else
297         {
298                 int                     i;
299                 bool            help_found = false;
300                 size_t          len;
301
302                 /* don't care about trailing spaces */
303                 len = strlen(topic);
304                 while (topic[len - 1] == ' ')
305                         len--;
306
307                 for (i = 0; QL_HELP[i].cmd; i++)
308                 {
309                         if (strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
310                                 strcmp(topic, "*") == 0)
311                         {
312                                 help_found = true;
313                                 printf(_("Command:     %s\n"
314                                                  "Description: %s\n"
315                                                  "Syntax:\n%s\n\n"),
316                                          QL_HELP[i].cmd, QL_HELP[i].help, QL_HELP[i].syntax);
317                                 /* If we have an exact match, exit.  Fixes \h SELECT */
318                                 if (strcasecmp(topic, QL_HELP[i].cmd) == 0)
319                                         break;
320                         }
321                 }
322
323                 if (!help_found)
324                         printf(_("No help available for '%-.*s'.\nTry \\h with no arguments to see available help.\n"), (int) len, topic);
325         }
326 }
327
328
329
330 void
331 print_copyright(void)
332 {
333         puts(
334                  "PostgreSQL Data Base Management System\n\n"
335                  "Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group\n\n"
336                  "This software is based on Postgres95, formerly known as Postgres, which\n"
337                  "contains the following notice:\n\n"
338                  "Portions Copyright(c) 1994 - 7 Regents of the University of California\n\n"
339                  "Permission to use, copy, modify, and distribute this software and its\n"
340                  "documentation for any purpose, without fee, and without a written agreement\n"
341                  "is hereby granted, provided that the above copyright notice and this paragraph\n"
342                  "and the following two paragraphs appear in all copies.\n\n"
343                  "IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR\n"
344                  "DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST\n"
345                  "PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF\n"
346                  "THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH\n"
347                  "DAMAGE.\n\n"
348                  "THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,\n"
349                  "BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\n"
350                  "PARTICULAR PURPOSE.THE SOFTWARE PROVIDED HEREUNDER IS ON AN \"AS IS\" BASIS,\n"
351                  "AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,\n"
352                  "SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
353                 );
354 }