]> granicus.if.org Git - postgresql/blob - src/bin/psql/help.c
psql: Fix \? output alignment
[postgresql] / src / bin / psql / help.c
1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Copyright (c) 2000-2014, PostgreSQL Global Development Group
5  *
6  * src/bin/psql/help.c
7  */
8 #include "postgres_fe.h"
9
10 #ifndef WIN32
11 #include <sys/types.h>                  /* (ditto) */
12 #include <unistd.h>                             /* for geteuid() */
13 #else
14 #include <win32.h>
15 #endif
16
17 #ifndef WIN32
18 #include <sys/ioctl.h>                  /* for ioctl() */
19 #endif
20
21 #ifdef HAVE_TERMIOS_H
22 #include <termios.h>
23 #endif
24
25 #include "common.h"
26 #include "common/username.h"
27 #include "help.h"
28 #include "input.h"
29 #include "settings.h"
30 #include "sql_help.h"
31
32
33 /*
34  * PLEASE:
35  * If you change something in this file, also make the same changes
36  * in the DocBook documentation, file ref/psql-ref.sgml. If you don't
37  * know how to do it, please find someone who can help you.
38  */
39
40
41 /*
42  * usage
43  *
44  * print out command line arguments
45  */
46 #define ON(var) (var ? _("on") : _("off"))
47
48 void
49 usage(unsigned short int pager)
50 {
51         const char *env;
52         const char *user;
53         char       *errstr;
54         FILE       *output;
55
56         /* Find default user, in case we need it. */
57         user = getenv("PGUSER");
58         if (!user)
59         {
60                 user = get_user_name(&errstr);
61                 if (!user)
62                 {
63                         psql_error("%s\n", errstr);
64                         exit(EXIT_FAILURE);
65                 }
66         }
67
68         output = PageOutput(59, pager);
69
70         fprintf(output, _("psql is the PostgreSQL interactive terminal.\n\n"));
71         fprintf(output, _("Usage:\n"));
72         fprintf(output, _("  psql [OPTION]... [DBNAME [USERNAME]]\n\n"));
73
74         fprintf(output, _("General options:\n"));
75         /* Display default database */
76         env = getenv("PGDATABASE");
77         if (!env)
78                 env = user;
79         fprintf(output, _("  -c, --command=COMMAND    run only single command (SQL or internal) and exit\n"));
80         fprintf(output, _("  -d, --dbname=DBNAME      database name to connect to (default: \"%s\")\n"), env);
81         fprintf(output, _("  -f, --file=FILENAME      execute commands from file, then exit\n"));
82         fprintf(output, _("  -l, --list               list available databases, then exit\n"));
83         fprintf(output, _("  -v, --set=, --variable=NAME=VALUE\n"
84                          "                           set psql variable NAME to VALUE e.g.: -v ON_ERROR_STOP=1\n"));
85         fprintf(output, _("  -V, --version            output version information, then exit\n"));
86         fprintf(output, _("  -X, --no-psqlrc          do not read startup file (~/.psqlrc)\n"));
87         fprintf(output, _("  -1 (\"one\"), --single-transaction\n"
88                          "                           execute as a single transaction (if non-interactive)\n"));
89         fprintf(output, _("  -?, --help[=options]     show this help, then exit\n"));
90         fprintf(output, _("      --help=variables     show a list of all specially treated variables, then exit\n"));
91         fprintf(output, _("      --help=commands      show a list of backslash commands, then exit\n"));
92
93         fprintf(output, _("\nInput and output options:\n"));
94         fprintf(output, _("  -a, --echo-all           echo all input from script\n"));
95         fprintf(output, _("  -b, --echo-errors        echo failed commands\n"));
96         fprintf(output, _("  -e, --echo-queries       echo commands sent to server\n"));
97         fprintf(output, _("  -E, --echo-hidden        display queries that internal commands generate\n"));
98         fprintf(output, _("  -L, --log-file=FILENAME  send session log to file\n"));
99         fprintf(output, _("  -n, --no-readline        disable enhanced command line editing (readline)\n"));
100         fprintf(output, _("  -o, --output=FILENAME    send query results to file (or |pipe)\n"));
101         fprintf(output, _("  -q, --quiet              run quietly (no messages, only query output)\n"));
102         fprintf(output, _("  -s, --single-step        single-step mode (confirm each query)\n"));
103         fprintf(output, _("  -S, --single-line        single-line mode (end of line terminates SQL command)\n"));
104
105         fprintf(output, _("\nOutput format options:\n"));
106         fprintf(output, _("  -A, --no-align           unaligned table output mode\n"));
107         fprintf(output, _("  -F, --field-separator=STRING\n"
108                          "                           field separator for unaligned output (default: \"%s\")\n"),
109                    DEFAULT_FIELD_SEP);
110         fprintf(output, _("  -H, --html               HTML table output mode\n"));
111         fprintf(output, _("  -P, --pset=VAR[=ARG]     set printing option VAR to ARG (see \\pset command)\n"));
112         fprintf(output, _("  -R, --record-separator=STRING\n"
113                          "                           record separator for unaligned output (default: newline)\n"));
114         fprintf(output, _("  -t, --tuples-only        print rows only\n"));
115         fprintf(output, _("  -T, --table-attr=TEXT    set HTML table tag attributes (e.g., width, border)\n"));
116         fprintf(output, _("  -x, --expanded           turn on expanded table output\n"));
117         fprintf(output, _("  -z, --field-separator-zero\n"
118                          "                           set field separator for unaligned output to zero byte\n"));
119         fprintf(output, _("  -0, --record-separator-zero\n"
120                          "                           set record separator for unaligned output to zero byte\n"));
121
122         fprintf(output, _("\nConnection options:\n"));
123         /* Display default host */
124         env = getenv("PGHOST");
125         fprintf(output, _("  -h, --host=HOSTNAME      database server host or socket directory (default: \"%s\")\n"),
126                    env ? env : _("local socket"));
127         /* Display default port */
128         env = getenv("PGPORT");
129         fprintf(output, _("  -p, --port=PORT          database server port (default: \"%s\")\n"),
130                    env ? env : DEF_PGPORT_STR);
131         /* Display default user */
132         env = getenv("PGUSER");
133         if (!env)
134                 env = user;
135         fprintf(output, _("  -U, --username=USERNAME  database user name (default: \"%s\")\n"), env);
136         fprintf(output, _("  -w, --no-password        never prompt for password\n"));
137         fprintf(output, _("  -W, --password           force password prompt (should happen automatically)\n"));
138
139         fprintf(output, _("\nFor more information, type \"\\?\" (for internal commands) or \"\\help\" (for SQL\n"
140                          "commands) from within psql, or consult the psql section in the PostgreSQL\n"
141                          "documentation.\n\n"));
142         fprintf(output, _("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
143
144         ClosePager(output);
145 }
146
147
148 /*
149  * slashUsage
150  *
151  * print out help for the backslash commands
152  */
153 void
154 slashUsage(unsigned short int pager)
155 {
156         FILE       *output;
157         char       *currdb;
158
159         currdb = PQdb(pset.db);
160
161         output = PageOutput(103, pager);
162
163         /* if you add/remove a line here, change the row count above */
164
165         fprintf(output, _("General\n"));
166         fprintf(output, _("  \\copyright             show PostgreSQL usage and distribution terms\n"));
167         fprintf(output, _("  \\g [FILE] or ;         execute query (and send results to file or |pipe)\n"));
168         fprintf(output, _("  \\gset [PREFIX]         execute query and store results in psql variables\n"));
169         fprintf(output, _("  \\q                     quit psql\n"));
170         fprintf(output, _("  \\watch [SEC]           execute query every SEC seconds\n"));
171         fprintf(output, "\n");
172
173         fprintf(output, _("Help\n"));
174
175         fprintf(output, _("  \\? [commands]          description of all psql backslash commands\n"));
176         fprintf(output, _("  \\? options             description of all psql commandline options\n"));
177         fprintf(output, _("  \\? variables           description of all psql configuration variables\n"));
178         fprintf(output, _("  \\h [NAME]              help on syntax of SQL commands, * for all commands\n"));
179         fprintf(output, "\n");
180
181         fprintf(output, _("Query Buffer\n"));
182         fprintf(output, _("  \\e [FILE] [LINE]       edit the query buffer (or file) with external editor\n"));
183         fprintf(output, _("  \\ef [FUNCNAME [LINE]]  edit function definition with external editor\n"));
184         fprintf(output, _("  \\p                     show the contents of the query buffer\n"));
185         fprintf(output, _("  \\r                     reset (clear) the query buffer\n"));
186 #ifdef USE_READLINE
187         fprintf(output, _("  \\s [FILE]              display history or save it to file\n"));
188 #endif
189         fprintf(output, _("  \\w FILE                write query buffer to file\n"));
190         fprintf(output, "\n");
191
192         fprintf(output, _("Input/Output\n"));
193         fprintf(output, _("  \\copy ...              perform SQL COPY with data stream to the client host\n"));
194         fprintf(output, _("  \\echo [STRING]         write string to standard output\n"));
195         fprintf(output, _("  \\i FILE                execute commands from file\n"));
196         fprintf(output, _("  \\ir FILE               as \\i, but relative to location of current script\n"));
197         fprintf(output, _("  \\o [FILE]              send all query results to file or |pipe\n"));
198         fprintf(output, _("  \\qecho [STRING]        write string to query output stream (see \\o)\n"));
199         fprintf(output, "\n");
200
201         fprintf(output, _("Informational\n"));
202         fprintf(output, _("  (options: S = show system objects, + = additional detail)\n"));
203         fprintf(output, _("  \\d[S+]                 list tables, views, and sequences\n"));
204         fprintf(output, _("  \\d[S+]  NAME           describe table, view, sequence, or index\n"));
205         fprintf(output, _("  \\da[S]  [PATTERN]      list aggregates\n"));
206         fprintf(output, _("  \\db[+]  [PATTERN]      list tablespaces\n"));
207         fprintf(output, _("  \\dc[S+] [PATTERN]      list conversions\n"));
208         fprintf(output, _("  \\dC[+]  [PATTERN]      list casts\n"));
209         fprintf(output, _("  \\dd[S]  [PATTERN]      show object descriptions not displayed elsewhere\n"));
210         fprintf(output, _("  \\ddp    [PATTERN]      list default privileges\n"));
211         fprintf(output, _("  \\dD[S+] [PATTERN]      list domains\n"));
212         fprintf(output, _("  \\det[+] [PATTERN]      list foreign tables\n"));
213         fprintf(output, _("  \\des[+] [PATTERN]      list foreign servers\n"));
214         fprintf(output, _("  \\deu[+] [PATTERN]      list user mappings\n"));
215         fprintf(output, _("  \\dew[+] [PATTERN]      list foreign-data wrappers\n"));
216         fprintf(output, _("  \\df[antw][S+] [PATRN]  list [only agg/normal/trigger/window] functions\n"));
217         fprintf(output, _("  \\dF[+]  [PATTERN]      list text search configurations\n"));
218         fprintf(output, _("  \\dFd[+] [PATTERN]      list text search dictionaries\n"));
219         fprintf(output, _("  \\dFp[+] [PATTERN]      list text search parsers\n"));
220         fprintf(output, _("  \\dFt[+] [PATTERN]      list text search templates\n"));
221         fprintf(output, _("  \\dg[+]  [PATTERN]      list roles\n"));
222         fprintf(output, _("  \\di[S+] [PATTERN]      list indexes\n"));
223         fprintf(output, _("  \\dl                    list large objects, same as \\lo_list\n"));
224         fprintf(output, _("  \\dL[S+] [PATTERN]      list procedural languages\n"));
225         fprintf(output, _("  \\dm[S+] [PATTERN]      list materialized views\n"));
226         fprintf(output, _("  \\dn[S+] [PATTERN]      list schemas\n"));
227         fprintf(output, _("  \\do[S]  [PATTERN]      list operators\n"));
228         fprintf(output, _("  \\dO[S+] [PATTERN]      list collations\n"));
229         fprintf(output, _("  \\dp     [PATTERN]      list table, view, and sequence access privileges\n"));
230         fprintf(output, _("  \\drds [PATRN1 [PATRN2]] list per-database role settings\n"));
231         fprintf(output, _("  \\ds[S+] [PATTERN]      list sequences\n"));
232         fprintf(output, _("  \\dt[S+] [PATTERN]      list tables\n"));
233         fprintf(output, _("  \\dT[S+] [PATTERN]      list data types\n"));
234         fprintf(output, _("  \\du[+]  [PATTERN]      list roles\n"));
235         fprintf(output, _("  \\dv[S+] [PATTERN]      list views\n"));
236         fprintf(output, _("  \\dE[S+] [PATTERN]      list foreign tables\n"));
237         fprintf(output, _("  \\dx[+]  [PATTERN]      list extensions\n"));
238         fprintf(output, _("  \\dy     [PATTERN]      list event triggers\n"));
239         fprintf(output, _("  \\l[+]   [PATTERN]      list databases\n"));
240         fprintf(output, _("  \\sf[+] FUNCNAME        show a function's definition\n"));
241         fprintf(output, _("  \\z      [PATTERN]      same as \\dp\n"));
242         fprintf(output, "\n");
243
244         fprintf(output, _("Formatting\n"));
245         fprintf(output, _("  \\a                     toggle between unaligned and aligned output mode\n"));
246         fprintf(output, _("  \\C [STRING]            set table title, or unset if none\n"));
247         fprintf(output, _("  \\f [STRING]            show or set field separator for unaligned query output\n"));
248         fprintf(output, _("  \\H                     toggle HTML output mode (currently %s)\n"),
249                         ON(pset.popt.topt.format == PRINT_HTML));
250         fprintf(output, _("  \\pset [NAME [VALUE]]   set table output option\n"
251                                           "                         (NAME := {format|border|expanded|fieldsep|fieldsep_zero|footer|null|\n"
252                                           "                         numericlocale|recordsep|recordsep_zero|tuples_only|title|tableattr|pager|\n"
253                                           "                         unicode_border_linestyle|unicode_column_linestyle|unicode_header_linestyle})\n"));
254         fprintf(output, _("  \\t [on|off]            show only rows (currently %s)\n"),
255                         ON(pset.popt.topt.tuples_only));
256         fprintf(output, _("  \\T [STRING]            set HTML <table> tag attributes, or unset if none\n"));
257         fprintf(output, _("  \\x [on|off|auto]       toggle expanded output (currently %s)\n"),
258                 pset.popt.topt.expanded == 2 ? "auto" : ON(pset.popt.topt.expanded));
259         fprintf(output, "\n");
260
261         fprintf(output, _("Connection\n"));
262         if (currdb)
263                 fprintf(output, _("  \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
264                                                   "                         connect to new database (currently \"%s\")\n"),
265                                 currdb);
266         else
267                 fprintf(output, _("  \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
268                                                   "                         connect to new database (currently no connection)\n"));
269         fprintf(output, _("  \\encoding [ENCODING]   show or set client encoding\n"));
270         fprintf(output, _("  \\password [USERNAME]   securely change the password for a user\n"));
271         fprintf(output, _("  \\conninfo              display information about current connection\n"));
272         fprintf(output, "\n");
273
274         fprintf(output, _("Operating System\n"));
275         fprintf(output, _("  \\cd [DIR]              change the current working directory\n"));
276         fprintf(output, _("  \\setenv NAME [VALUE]   set or unset environment variable\n"));
277         fprintf(output, _("  \\timing [on|off]       toggle timing of commands (currently %s)\n"),
278                         ON(pset.timing));
279         fprintf(output, _("  \\! [COMMAND]           execute command in shell or start interactive shell\n"));
280         fprintf(output, "\n");
281
282         fprintf(output, _("Variables\n"));
283         fprintf(output, _("  \\prompt [TEXT] NAME    prompt user to set internal variable\n"));
284         fprintf(output, _("  \\set [NAME [VALUE]]    set internal variable, or list all if no parameters\n"));
285         fprintf(output, _("  \\unset NAME            unset (delete) internal variable\n"));
286         fprintf(output, "\n");
287
288         fprintf(output, _("Large Objects\n"));
289         fprintf(output, _("  \\lo_export LOBOID FILE\n"
290                                           "  \\lo_import FILE [COMMENT]\n"
291                                           "  \\lo_list\n"
292                                           "  \\lo_unlink LOBOID      large object operations\n"));
293
294         ClosePager(output);
295 }
296
297
298 /*
299  * helpVariables
300  *
301  * show list of available variables (options) from command line
302  */
303 void
304 helpVariables(unsigned short int pager)
305 {
306         FILE       *output;
307
308         output = PageOutput(85, pager);
309
310         fprintf(output, _("List of specially treated variables.\n"));
311
312         fprintf(output, _("psql variables:\n"));
313         fprintf(output, _("Usage:\n"));
314         fprintf(output, _("  psql --set=NAME=VALUE\n  or \\set NAME VALUE in interactive mode\n\n"));
315
316         fprintf(output, _("  AUTOCOMMIT         if set, successful SQL commands are automatically committed\n"));
317         fprintf(output, _("  COMP_KEYWORD_CASE  determine the case used to complete SQL keywords\n"
318                                          "                     [lower, upper, preserve-lower, preserve-upper]\n"));
319         fprintf(output, _("  DBNAME             the currently connected database name\n"));
320         fprintf(output, _("  ECHO               control what input is written to standard output\n"
321                                          "                     [all, errors, none, queries]\n"));
322         fprintf(output, _("  ECHO_HIDDEN        display internal queries executed by backslash commands when it is set\n"
323                                          "                     or with [noexec] just show without execution\n"));
324         fprintf(output, _("  ENCODING           current client character set encoding\n"));
325         fprintf(output, _("  FETCH_COUNT        the number of result rows to fetch and display at a time\n"
326                                          "                     (default: 0=unlimited)\n"));
327         fprintf(output, _("  HISTCONTROL        control history list [ignorespace, ignoredups, ignoreboth]\n"));
328         fprintf(output, _("  HISTFILE           file name used to store the history list\n"));
329         fprintf(output, _("  HISTSIZE           the number of commands to store in the command history\n"));
330         fprintf(output, _("  HOST               the currently connected database server\n"));
331         fprintf(output, _("  IGNOREEOF          if unset, sending an EOF to interactive session terminates application\n"));
332         fprintf(output, _("  LASTOID            the value of last affected OID\n"));
333         fprintf(output, _("  ON_ERROR_ROLLBACK  if set, an error doesn't stop a transaction (uses implicit SAVEPOINTs)\n"));
334         fprintf(output, _("  ON_ERROR_STOP      stop batch execution after error\n"));
335         fprintf(output, _("  PORT               server port of the current connection\n"));
336         fprintf(output, _("  PROMPT1            specify the standard psql prompt\n"));
337         fprintf(output, _("  PROMPT2            specify the prompt used when a statement continues from a previous line\n"));
338         fprintf(output, _("  PROMPT3            specify the prompt used during COPY ... FROM STDIN\n"));
339         fprintf(output, _("  QUIET              run quietly (same as -q option)\n"));
340         fprintf(output, _("  SINGLELINE         end of line terminates SQL command mode (same as -S option)\n"));
341         fprintf(output, _("  SINGLESTEP         single-step mode (same as -s option)\n"));
342         fprintf(output, _("  USER               the currently connected database user\n"));
343         fprintf(output, _("  VERBOSITY          control verbosity of error reports [default, verbose, terse]\n"));
344
345         fprintf(output, _("\nDisplay influencing variables:\n"));
346         fprintf(output, _("Usage:\n"));
347         fprintf(output, _("  psql --pset=NAME[=VALUE]\n  or \\pset NAME [VALUE] in interactive mode\n\n"));
348
349         fprintf(output, _("  border             border style (number)\n"));
350         fprintf(output, _("  columns            set the target width for the wrapped format\n"));
351         fprintf(output, _("  expanded (or x)    toggle expanded output\n"));
352         fprintf(output, _("  fieldsep           field separator for unaligned output (default '|')\n"));
353         fprintf(output, _("  fieldsep_zero      set field separator in unaligned mode to zero\n"));
354         fprintf(output, _("  format             set output format [unaligned, aligned, wrapped, html, latex, ..]\n"));
355         fprintf(output, _("  footer             enable or disable display of the table footer [on, off]\n"));
356         fprintf(output, _("  linestyle          set the border line drawing style [ascii, old-ascii, unicode]\n"));
357         fprintf(output, _("  null               set the string to be printed in place of a null value\n"));
358         fprintf(output, _("  numericlocale      enable or disable display of a locale-specific character to separate\n"
359                                          "                     groups of digits [on, off]\n"));
360         fprintf(output, _("  pager              control when an external pager is used [yes, no, always]\n"));
361         fprintf(output, _("  recordsep          specify the record (line) separator to use in unaligned output format\n"));
362         fprintf(output, _("  recordsep_zero     set the record separator to use in unaligned output format to a zero byte.\n"));
363         fprintf(output, _("  tableattr (or T)   specify attributes for table tag in html format or proportional\n"
364                                          "                     column width of left aligned data type in latex format\n"));
365         fprintf(output, _("  title              set the table title for any subsequently printed tables\n"));
366         fprintf(output, _("  tuples_only        if set, only actual table data is shown\n"));
367         fprintf(output, _("  unicode_border_linestyle\n"));
368         fprintf(output, _("  unicode_column_linestyle\n"));
369         fprintf(output, _("  unicode_header_linestyle\n"
370                                          "                     set the style of unicode line drawing [single, double]\n"));
371
372         fprintf(output, _("\nEnvironment variables:\n"));
373         fprintf(output, _("Usage:\n"));
374
375 #ifndef WIN32
376         fprintf(output, _("  NAME=VALUE [NAME=VALUE] psql ...\n  or \\setenv NAME [VALUE] in interactive mode\n\n"));
377 #else
378         fprintf(output, _("  set NAME=VALUE\n  psql ...\n  or \\setenv NAME VALUE in interactive mode\n\n"));
379 #endif
380
381         fprintf(output, _("  COLUMNS            number of columns for wrapped format\n"));
382         fprintf(output, _("  PAGER              name of external pager program\n"));
383         fprintf(output, _("  PGAPPNAME          same as the application_name connection parameter\n"));
384         fprintf(output, _("  PGDATABASE         same as the dbname connection parameter\n"));
385         fprintf(output, _("  PGHOST             same as the host connection parameter\n"));
386         fprintf(output, _("  PGPORT             same as the port connection parameter\n"));
387         fprintf(output, _("  PGUSER             same as the user connection parameter\n"));
388         fprintf(output, _("  PGPASSWORD         connection password (not recommended)\n"));
389         fprintf(output, _("  PGPASSFILE         password file name\n"));
390         fprintf(output, _("  PSQL_EDITOR, EDITOR, VISUAL\n"
391                                          "                     editor used by the \\e and \\ef commands\n"));
392         fprintf(output, _("  PSQL_EDITOR_LINENUMBER_ARG\n"
393                                          "                     how to specify a line number when invoking the editor\n"));
394         fprintf(output, _("  PSQL_HISTORY       alternative location for the command history file\n"));
395         fprintf(output, _("  PSQLRC             alternative location for the user's .psqlrc file\n"));
396         fprintf(output, _("  SHELL              shell used by the \\! command\n"));
397         fprintf(output, _("  TMPDIR             directory for temporary files\n"));
398
399         ClosePager(output);
400 }
401
402
403 /*
404  * helpSQL -- help with SQL commands
405  *
406  * Note: we assume caller removed any trailing spaces in "topic".
407  */
408 void
409 helpSQL(const char *topic, unsigned short int pager)
410 {
411 #define VALUE_OR_NULL(a) ((a) ? (a) : "")
412
413         if (!topic || strlen(topic) == 0)
414         {
415                 /* Print all the available command names */
416                 int                     screen_width;
417                 int                     ncolumns;
418                 int                     nrows;
419                 FILE       *output;
420                 int                     i;
421                 int                     j;
422
423 #ifdef TIOCGWINSZ
424                 struct winsize screen_size;
425
426                 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1)
427                         screen_width = 80;      /* ioctl failed, assume 80 */
428                 else
429                         screen_width = screen_size.ws_col;
430 #else
431                 screen_width = 80;              /* default assumption */
432 #endif
433
434                 ncolumns = (screen_width - 3) / (QL_MAX_CMD_LEN + 1);
435                 ncolumns = Max(ncolumns, 1);
436                 nrows = (QL_HELP_COUNT + (ncolumns - 1)) / ncolumns;
437
438                 output = PageOutput(nrows + 1, pager);
439
440                 fputs(_("Available help:\n"), output);
441
442                 for (i = 0; i < nrows; i++)
443                 {
444                         fprintf(output, "  ");
445                         for (j = 0; j < ncolumns - 1; j++)
446                                 fprintf(output, "%-*s",
447                                                 QL_MAX_CMD_LEN + 1,
448                                                 VALUE_OR_NULL(QL_HELP[i + j * nrows].cmd));
449                         if (i + j * nrows < QL_HELP_COUNT)
450                                 fprintf(output, "%s",
451                                                 VALUE_OR_NULL(QL_HELP[i + j * nrows].cmd));
452                         fputc('\n', output);
453                 }
454
455                 ClosePager(output);
456         }
457         else
458         {
459                 int                     i,
460                                         j,
461                                         x = 0;
462                 bool            help_found = false;
463                 FILE       *output = NULL;
464                 size_t          len,
465                                         wordlen;
466                 int                     nl_count = 0;
467
468                 /*
469                  * We first try exact match, then first + second words, then first
470                  * word only.
471                  */
472                 len = strlen(topic);
473
474                 for (x = 1; x <= 3; x++)
475                 {
476                         if (x > 1)                      /* Nothing on first pass - try the opening
477                                                                  * word(s) */
478                         {
479                                 wordlen = j = 1;
480                                 while (topic[j] != ' ' && j++ < len)
481                                         wordlen++;
482                                 if (x == 2)
483                                 {
484                                         j++;
485                                         while (topic[j] != ' ' && j++ <= len)
486                                                 wordlen++;
487                                 }
488                                 if (wordlen >= len)             /* Don't try again if the same word */
489                                 {
490                                         if (!output)
491                                                 output = PageOutput(nl_count, pager);
492                                         break;
493                                 }
494                                 len = wordlen;
495                         }
496
497                         /* Count newlines for pager */
498                         for (i = 0; QL_HELP[i].cmd; i++)
499                         {
500                                 if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
501                                         strcmp(topic, "*") == 0)
502                                 {
503                                         nl_count += 5 + QL_HELP[i].nl_count;
504
505                                         /* If we have an exact match, exit.  Fixes \h SELECT */
506                                         if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
507                                                 break;
508                                 }
509                         }
510
511                         if (!output)
512                                 output = PageOutput(nl_count, pager);
513
514                         for (i = 0; QL_HELP[i].cmd; i++)
515                         {
516                                 if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
517                                         strcmp(topic, "*") == 0)
518                                 {
519                                         PQExpBufferData buffer;
520
521                                         initPQExpBuffer(&buffer);
522                                         QL_HELP[i].syntaxfunc(&buffer);
523                                         help_found = true;
524                                         fprintf(output, _("Command:     %s\n"
525                                                                           "Description: %s\n"
526                                                                           "Syntax:\n%s\n\n"),
527                                                         QL_HELP[i].cmd,
528                                                         _(QL_HELP[i].help),
529                                                         buffer.data);
530                                         /* If we have an exact match, exit.  Fixes \h SELECT */
531                                         if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
532                                                 break;
533                                 }
534                         }
535                         if (help_found)         /* Don't keep trying if we got a match */
536                                 break;
537                 }
538
539                 if (!help_found)
540                         fprintf(output, _("No help available for \"%s\".\nTry \\h with no arguments to see available help.\n"), topic);
541
542                 ClosePager(output);
543         }
544 }
545
546
547
548 void
549 print_copyright(void)
550 {
551         puts(
552                  "PostgreSQL Database Management System\n"
553                  "(formerly known as Postgres, then as Postgres95)\n\n"
554                  "Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group\n\n"
555                  "Portions Copyright (c) 1994, The Regents of the University of California\n\n"
556         "Permission to use, copy, modify, and distribute this software and its\n"
557                  "documentation for any purpose, without fee, and without a written agreement\n"
558          "is hereby granted, provided that the above copyright notice and this\n"
559            "paragraph and the following two paragraphs appear in all copies.\n\n"
560                  "IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR\n"
561                  "DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING\n"
562                  "LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS\n"
563                  "DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE\n"
564                  "POSSIBILITY OF SUCH DAMAGE.\n\n"
565           "THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,\n"
566                  "INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY\n"
567                  "AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS\n"
568                  "ON AN \"AS IS\" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO\n"
569         "PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.\n"
570                 );
571 }