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