]> granicus.if.org Git - postgresql/blob - src/bin/psql/help.c
Translation updates, some messages tweaked.
[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.57 2002/09/22 20:57:21 petere Exp $
7  */
8 #include "postgres_fe.h"
9 #include "print.h"
10 #include "help.h"
11
12 #include <signal.h>
13 #include <errno.h>
14
15 #ifndef WIN32
16 #include <sys/ioctl.h>                  /* for ioctl() */
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         puts(_("Options:"));
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"));
91
92         /* Display default database */
93         env = getenv("PGDATABASE");
94         if (!env)
95                 env = user;
96         printf(_("  -d DBNAME       Specify database name to connect to (default: %s)\n"), env);
97
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"),
102                    DEFAULT_FIELD_SEP);
103
104         /* Display default host */
105         env = getenv("PGHOST");
106         printf(_("  -h HOSTNAME     Specify database server host (default: %s)\n"),
107                    env ? env : _("local socket"));
108
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)"));
113
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);
118
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=)"));
126
127         /* Display default user */
128         env = getenv("PGUSER");
129         if (!env)
130                 env = user;
131         printf(_("  -U NAME         Specify database user name (default: %s)\n"), env);
132
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)"));
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(bool pager)
163 {
164         FILE       *output,
165                            *pagerfd = NULL;
166
167         /* check whether we need / can / are supposed to use pager */
168         if (pager
169 #ifndef WIN32
170                 &&
171                 isatty(fileno(stdin)) &&
172                 isatty(fileno(stdout))
173 #endif
174                 )
175         {
176                 const char *pagerprog;
177
178 #ifdef TIOCGWINSZ
179                 int                     result;
180                 struct winsize screen_size;
181
182                 result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
183                 if (result == -1 || 50 > screen_size.ws_row)
184                 {
185 #endif
186                         pagerprog = getenv("PAGER");
187                         if (!pagerprog)
188                                 pagerprog = DEFAULT_PAGER;
189                         pagerfd = popen(pagerprog, "w");
190 #ifdef TIOCGWINSZ
191                 }
192 #endif
193         }
194
195         if (pagerfd)
196         {
197                 output = pagerfd;
198 #ifndef WIN32
199                 pqsignal(SIGPIPE, SIG_IGN);
200 #endif
201         }
202         else
203                 output = stdout;
204
205         /* if you add/remove a line here, change the row test above */
206
207         /*
208          * if this " is the start of the string then it ought to end there to
209          * fit in 80 columns >> "
210          */
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"),
214                         PQdb(pset.db));
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"),
257                         ON(pset.timing));
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"));
264
265         if (pagerfd)
266         {
267                 pclose(pagerfd);
268 #ifndef WIN32
269                 pqsignal(SIGPIPE, SIG_DFL);
270 #endif
271         }
272 }
273
274
275
276 /*
277  * helpSQL -- help with SQL commands
278  *
279  */
280 void
281 helpSQL(const char *topic)
282 {
283 #define VALUE_OR_NULL(a) ((a) ? (a) : "")
284
285         if (!topic || strlen(topic) == 0)
286         {
287                 int                     i;
288                 int                     items_per_column = (QL_HELP_COUNT + 2) / 3;
289
290                 puts(_("Available help:"));
291
292                 for (i = 0; i < items_per_column; i++)
293                 {
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)
298                                 printf("%-26s",
299                                    VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
300                         fputc('\n', stdout);
301                 }
302         }
303
304         else
305         {
306                 int                     i;
307                 bool            help_found = false;
308                 size_t          len;
309
310                 /* don't care about trailing spaces */
311                 len = strlen(topic);
312                 while (topic[len - 1] == ' ')
313                         len--;
314
315                 for (i = 0; QL_HELP[i].cmd; i++)
316                 {
317                         if (strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
318                                 strcmp(topic, "*") == 0)
319                         {
320                                 help_found = true;
321                                 printf(_("Command:     %s\n"
322                                                  "Description: %s\n"
323                                                  "Syntax:\n%s\n\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)
327                                         break;
328                         }
329                 }
330
331                 if (!help_found)
332                         printf(_("No help available for '%-.*s'.\nTry \\h with no arguments to see available help.\n"), (int) len, topic);
333         }
334 }
335
336
337
338 void
339 print_copyright(void)
340 {
341         puts(
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"
355                  "DAMAGE.\n\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."
361                 );
362 }