LCOV - code coverage report
Current view: top level - src/bin/psql - help.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 324 343 94.5 %
Date: 2021-12-04 23:09:10 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * psql - the PostgreSQL interactive terminal
       3             :  *
       4             :  * Copyright (c) 2000-2021, PostgreSQL Global Development Group
       5             :  *
       6             :  * src/bin/psql/help.c
       7             :  */
       8             : #include "postgres_fe.h"
       9             : 
      10             : #ifndef WIN32
      11             : #include <unistd.h>               /* for geteuid() */
      12             : #else
      13             : #include <win32.h>
      14             : #endif
      15             : 
      16             : #ifndef WIN32
      17             : #include <sys/ioctl.h>            /* for ioctl() */
      18             : #endif
      19             : 
      20             : #ifdef HAVE_TERMIOS_H
      21             : #include <termios.h>
      22             : #endif
      23             : 
      24             : #include "common.h"
      25             : #include "common/logging.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             :  * PLEASE:
      34             :  * If you change something in this file, also make the same changes
      35             :  * in the DocBook documentation, file ref/psql-ref.sgml. If you don't
      36             :  * know how to do it, please find someone who can help you.
      37             :  */
      38             : 
      39             : 
      40             : /*
      41             :  * usage
      42             :  *
      43             :  * print out command line arguments
      44             :  */
      45             : #define ON(var) (var ? _("on") : _("off"))
      46             : 
      47             : void
      48           2 : usage(unsigned short int pager)
      49             : {
      50             :     const char *env;
      51             :     const char *user;
      52             :     char       *errstr;
      53             :     FILE       *output;
      54             : 
      55             :     /* Find default user, in case we need it. */
      56           2 :     user = getenv("PGUSER");
      57           2 :     if (!user)
      58             :     {
      59           2 :         user = get_user_name(&errstr);
      60           2 :         if (!user)
      61             :         {
      62           0 :             pg_log_fatal("%s", errstr);
      63           0 :             exit(EXIT_FAILURE);
      64             :         }
      65             :     }
      66             : 
      67             :     /*
      68             :      * Keep this line count in sync with the number of lines printed below!
      69             :      * Use "psql --help=options | wc" to count correctly.
      70             :      */
      71           2 :     output = PageOutput(63, pager ? &(pset.popt.topt) : NULL);
      72             : 
      73           2 :     fprintf(output, _("psql is the PostgreSQL interactive terminal.\n\n"));
      74           2 :     fprintf(output, _("Usage:\n"));
      75           2 :     fprintf(output, _("  psql [OPTION]... [DBNAME [USERNAME]]\n\n"));
      76             : 
      77           2 :     fprintf(output, _("General options:\n"));
      78             :     /* Display default database */
      79           2 :     env = getenv("PGDATABASE");
      80           2 :     if (!env)
      81           0 :         env = user;
      82           2 :     fprintf(output, _("  -c, --command=COMMAND    run only single command (SQL or internal) and exit\n"));
      83           2 :     fprintf(output, _("  -d, --dbname=DBNAME      database name to connect to (default: \"%s\")\n"), env);
      84           2 :     fprintf(output, _("  -f, --file=FILENAME      execute commands from file, then exit\n"));
      85           2 :     fprintf(output, _("  -l, --list               list available databases, then exit\n"));
      86           2 :     fprintf(output, _("  -v, --set=, --variable=NAME=VALUE\n"
      87             :                       "                           set psql variable NAME to VALUE\n"
      88             :                       "                           (e.g., -v ON_ERROR_STOP=1)\n"));
      89           2 :     fprintf(output, _("  -V, --version            output version information, then exit\n"));
      90           2 :     fprintf(output, _("  -X, --no-psqlrc          do not read startup file (~/.psqlrc)\n"));
      91           2 :     fprintf(output, _("  -1 (\"one\"), --single-transaction\n"
      92             :                       "                           execute as a single transaction (if non-interactive)\n"));
      93           2 :     fprintf(output, _("  -?, --help[=options]     show this help, then exit\n"));
      94           2 :     fprintf(output, _("      --help=commands      list backslash commands, then exit\n"));
      95           2 :     fprintf(output, _("      --help=variables     list special variables, then exit\n"));
      96             : 
      97           2 :     fprintf(output, _("\nInput and output options:\n"));
      98           2 :     fprintf(output, _("  -a, --echo-all           echo all input from script\n"));
      99           2 :     fprintf(output, _("  -b, --echo-errors        echo failed commands\n"));
     100           2 :     fprintf(output, _("  -e, --echo-queries       echo commands sent to server\n"));
     101           2 :     fprintf(output, _("  -E, --echo-hidden        display queries that internal commands generate\n"));
     102           2 :     fprintf(output, _("  -L, --log-file=FILENAME  send session log to file\n"));
     103           2 :     fprintf(output, _("  -n, --no-readline        disable enhanced command line editing (readline)\n"));
     104           2 :     fprintf(output, _("  -o, --output=FILENAME    send query results to file (or |pipe)\n"));
     105           2 :     fprintf(output, _("  -q, --quiet              run quietly (no messages, only query output)\n"));
     106           2 :     fprintf(output, _("  -s, --single-step        single-step mode (confirm each query)\n"));
     107           2 :     fprintf(output, _("  -S, --single-line        single-line mode (end of line terminates SQL command)\n"));
     108             : 
     109           2 :     fprintf(output, _("\nOutput format options:\n"));
     110           2 :     fprintf(output, _("  -A, --no-align           unaligned table output mode\n"));
     111           2 :     fprintf(output, _("      --csv                CSV (Comma-Separated Values) table output mode\n"));
     112           2 :     fprintf(output, _("  -F, --field-separator=STRING\n"
     113             :                       "                           field separator for unaligned output (default: \"%s\")\n"),
     114             :             DEFAULT_FIELD_SEP);
     115           2 :     fprintf(output, _("  -H, --html               HTML table output mode\n"));
     116           2 :     fprintf(output, _("  -P, --pset=VAR[=ARG]     set printing option VAR to ARG (see \\pset command)\n"));
     117           2 :     fprintf(output, _("  -R, --record-separator=STRING\n"
     118             :                       "                           record separator for unaligned output (default: newline)\n"));
     119           2 :     fprintf(output, _("  -t, --tuples-only        print rows only\n"));
     120           2 :     fprintf(output, _("  -T, --table-attr=TEXT    set HTML table tag attributes (e.g., width, border)\n"));
     121           2 :     fprintf(output, _("  -x, --expanded           turn on expanded table output\n"));
     122           2 :     fprintf(output, _("  -z, --field-separator-zero\n"
     123             :                       "                           set field separator for unaligned output to zero byte\n"));
     124           2 :     fprintf(output, _("  -0, --record-separator-zero\n"
     125             :                       "                           set record separator for unaligned output to zero byte\n"));
     126             : 
     127           2 :     fprintf(output, _("\nConnection options:\n"));
     128             :     /* Display default host */
     129           2 :     env = getenv("PGHOST");
     130           2 :     fprintf(output, _("  -h, --host=HOSTNAME      database server host or socket directory (default: \"%s\")\n"),
     131           0 :             env ? env : _("local socket"));
     132             :     /* Display default port */
     133           2 :     env = getenv("PGPORT");
     134           2 :     fprintf(output, _("  -p, --port=PORT          database server port (default: \"%s\")\n"),
     135             :             env ? env : DEF_PGPORT_STR);
     136             :     /* Display default user */
     137           2 :     fprintf(output, _("  -U, --username=USERNAME  database user name (default: \"%s\")\n"), user);
     138           2 :     fprintf(output, _("  -w, --no-password        never prompt for password\n"));
     139           2 :     fprintf(output, _("  -W, --password           force password prompt (should happen automatically)\n"));
     140             : 
     141           2 :     fprintf(output, _("\nFor more information, type \"\\?\" (for internal commands) or \"\\help\" (for SQL\n"
     142             :                       "commands) from within psql, or consult the psql section in the PostgreSQL\n"
     143             :                       "documentation.\n\n"));
     144           2 :     fprintf(output, _("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     145           2 :     fprintf(output, _("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     146             : 
     147           2 :     ClosePager(output);
     148           2 : }
     149             : 
     150             : 
     151             : /*
     152             :  * slashUsage
     153             :  *
     154             :  * print out help for the backslash commands
     155             :  */
     156             : void
     157           2 : slashUsage(unsigned short int pager)
     158             : {
     159             :     FILE       *output;
     160             :     char       *currdb;
     161             : 
     162           2 :     currdb = PQdb(pset.db);
     163             : 
     164             :     /*
     165             :      * Keep this line count in sync with the number of lines printed below!
     166             :      * Use "psql --help=commands | wc" to count correctly.  It's okay to count
     167             :      * the USE_READLINE line even in builds without that.
     168             :      */
     169           2 :     output = PageOutput(136, pager ? &(pset.popt.topt) : NULL);
     170             : 
     171           2 :     fprintf(output, _("General\n"));
     172           2 :     fprintf(output, _("  \\copyright             show PostgreSQL usage and distribution terms\n"));
     173           2 :     fprintf(output, _("  \\crosstabview [COLUMNS] execute query and display results in crosstab\n"));
     174           2 :     fprintf(output, _("  \\errverbose            show most recent error message at maximum verbosity\n"));
     175           2 :     fprintf(output, _("  \\g [(OPTIONS)] [FILE]  execute query (and send results to file or |pipe);\n"
     176             :                       "                         \\g with no arguments is equivalent to a semicolon\n"));
     177           2 :     fprintf(output, _("  \\gdesc                 describe result of query, without executing it\n"));
     178           2 :     fprintf(output, _("  \\gexec                 execute query, then execute each value in its result\n"));
     179           2 :     fprintf(output, _("  \\gset [PREFIX]         execute query and store results in psql variables\n"));
     180           2 :     fprintf(output, _("  \\gx [(OPTIONS)] [FILE] as \\g, but forces expanded output mode\n"));
     181           2 :     fprintf(output, _("  \\q                     quit psql\n"));
     182           2 :     fprintf(output, _("  \\watch [SEC]           execute query every SEC seconds\n"));
     183           2 :     fprintf(output, "\n");
     184             : 
     185           2 :     fprintf(output, _("Help\n"));
     186             : 
     187           2 :     fprintf(output, _("  \\? [commands]          show help on backslash commands\n"));
     188           2 :     fprintf(output, _("  \\? options             show help on psql command-line options\n"));
     189           2 :     fprintf(output, _("  \\? variables           show help on special variables\n"));
     190           2 :     fprintf(output, _("  \\h [NAME]              help on syntax of SQL commands, * for all commands\n"));
     191           2 :     fprintf(output, "\n");
     192             : 
     193           2 :     fprintf(output, _("Query Buffer\n"));
     194           2 :     fprintf(output, _("  \\e [FILE] [LINE]       edit the query buffer (or file) with external editor\n"));
     195           2 :     fprintf(output, _("  \\ef [FUNCNAME [LINE]]  edit function definition with external editor\n"));
     196           2 :     fprintf(output, _("  \\ev [VIEWNAME [LINE]]  edit view definition with external editor\n"));
     197           2 :     fprintf(output, _("  \\p                     show the contents of the query buffer\n"));
     198           2 :     fprintf(output, _("  \\r                     reset (clear) the query buffer\n"));
     199             : #ifdef USE_READLINE
     200           2 :     fprintf(output, _("  \\s [FILE]              display history or save it to file\n"));
     201             : #endif
     202           2 :     fprintf(output, _("  \\w FILE                write query buffer to file\n"));
     203           2 :     fprintf(output, "\n");
     204             : 
     205           2 :     fprintf(output, _("Input/Output\n"));
     206           2 :     fprintf(output, _("  \\copy ...              perform SQL COPY with data stream to the client host\n"));
     207           2 :     fprintf(output, _("  \\echo [-n] [STRING]    write string to standard output (-n for no newline)\n"));
     208           2 :     fprintf(output, _("  \\i FILE                execute commands from file\n"));
     209           2 :     fprintf(output, _("  \\ir FILE               as \\i, but relative to location of current script\n"));
     210           2 :     fprintf(output, _("  \\o [FILE]              send all query results to file or |pipe\n"));
     211           2 :     fprintf(output, _("  \\qecho [-n] [STRING]   write string to \\o output stream (-n for no newline)\n"));
     212           2 :     fprintf(output, _("  \\warn [-n] [STRING]    write string to standard error (-n for no newline)\n"));
     213           2 :     fprintf(output, "\n");
     214             : 
     215           2 :     fprintf(output, _("Conditional\n"));
     216           2 :     fprintf(output, _("  \\if EXPR               begin conditional block\n"));
     217           2 :     fprintf(output, _("  \\elif EXPR             alternative within current conditional block\n"));
     218           2 :     fprintf(output, _("  \\else                  final alternative within current conditional block\n"));
     219           2 :     fprintf(output, _("  \\endif                 end conditional block\n"));
     220           2 :     fprintf(output, "\n");
     221             : 
     222           2 :     fprintf(output, _("Informational\n"));
     223           2 :     fprintf(output, _("  (options: S = show system objects, + = additional detail)\n"));
     224           2 :     fprintf(output, _("  \\d[S+]                 list tables, views, and sequences\n"));
     225           2 :     fprintf(output, _("  \\d[S+]  NAME           describe table, view, sequence, or index\n"));
     226           2 :     fprintf(output, _("  \\da[S]  [PATTERN]      list aggregates\n"));
     227           2 :     fprintf(output, _("  \\dA[+]  [PATTERN]      list access methods\n"));
     228           2 :     fprintf(output, _("  \\dAc[+] [AMPTRN [TYPEPTRN]]  list operator classes\n"));
     229           2 :     fprintf(output, _("  \\dAf[+] [AMPTRN [TYPEPTRN]]  list operator families\n"));
     230           2 :     fprintf(output, _("  \\dAo[+] [AMPTRN [OPFPTRN]]   list operators of operator families\n"));
     231           2 :     fprintf(output, _("  \\dAp[+] [AMPTRN [OPFPTRN]]   list support functions of operator families\n"));
     232           2 :     fprintf(output, _("  \\db[+]  [PATTERN]      list tablespaces\n"));
     233           2 :     fprintf(output, _("  \\dc[S+] [PATTERN]      list conversions\n"));
     234           2 :     fprintf(output, _("  \\dC[+]  [PATTERN]      list casts\n"));
     235           2 :     fprintf(output, _("  \\dd[S]  [PATTERN]      show object descriptions not displayed elsewhere\n"));
     236           2 :     fprintf(output, _("  \\dD[S+] [PATTERN]      list domains\n"));
     237           2 :     fprintf(output, _("  \\ddp    [PATTERN]      list default privileges\n"));
     238           2 :     fprintf(output, _("  \\dE[S+] [PATTERN]      list foreign tables\n"));
     239           2 :     fprintf(output, _("  \\des[+] [PATTERN]      list foreign servers\n"));
     240           2 :     fprintf(output, _("  \\det[+] [PATTERN]      list foreign tables\n"));
     241           2 :     fprintf(output, _("  \\deu[+] [PATTERN]      list user mappings\n"));
     242           2 :     fprintf(output, _("  \\dew[+] [PATTERN]      list foreign-data wrappers\n"));
     243           2 :     fprintf(output, _("  \\df[anptw][S+] [FUNCPTRN [TYPEPTRN ...]]\n"
     244             :                       "                         list [only agg/normal/procedure/trigger/window] functions\n"));
     245           2 :     fprintf(output, _("  \\dF[+]  [PATTERN]      list text search configurations\n"));
     246           2 :     fprintf(output, _("  \\dFd[+] [PATTERN]      list text search dictionaries\n"));
     247           2 :     fprintf(output, _("  \\dFp[+] [PATTERN]      list text search parsers\n"));
     248           2 :     fprintf(output, _("  \\dFt[+] [PATTERN]      list text search templates\n"));
     249           2 :     fprintf(output, _("  \\dg[S+] [PATTERN]      list roles\n"));
     250           2 :     fprintf(output, _("  \\di[S+] [PATTERN]      list indexes\n"));
     251           2 :     fprintf(output, _("  \\dl                    list large objects, same as \\lo_list\n"));
     252           2 :     fprintf(output, _("  \\dL[S+] [PATTERN]      list procedural languages\n"));
     253           2 :     fprintf(output, _("  \\dm[S+] [PATTERN]      list materialized views\n"));
     254           2 :     fprintf(output, _("  \\dn[S+] [PATTERN]      list schemas\n"));
     255           2 :     fprintf(output, _("  \\do[S+] [OPPTRN [TYPEPTRN [TYPEPTRN]]]\n"
     256             :                       "                         list operators\n"));
     257           2 :     fprintf(output, _("  \\dO[S+] [PATTERN]      list collations\n"));
     258           2 :     fprintf(output, _("  \\dp     [PATTERN]      list table, view, and sequence access privileges\n"));
     259           2 :     fprintf(output, _("  \\dP[itn+] [PATTERN]    list [only index/table] partitioned relations [n=nested]\n"));
     260           2 :     fprintf(output, _("  \\drds [ROLEPTRN [DBPTRN]] list per-database role settings\n"));
     261           2 :     fprintf(output, _("  \\dRp[+] [PATTERN]      list replication publications\n"));
     262           2 :     fprintf(output, _("  \\dRs[+] [PATTERN]      list replication subscriptions\n"));
     263           2 :     fprintf(output, _("  \\ds[S+] [PATTERN]      list sequences\n"));
     264           2 :     fprintf(output, _("  \\dt[S+] [PATTERN]      list tables\n"));
     265           2 :     fprintf(output, _("  \\dT[S+] [PATTERN]      list data types\n"));
     266           2 :     fprintf(output, _("  \\du[S+] [PATTERN]      list roles\n"));
     267           2 :     fprintf(output, _("  \\dv[S+] [PATTERN]      list views\n"));
     268           2 :     fprintf(output, _("  \\dx[+]  [PATTERN]      list extensions\n"));
     269           2 :     fprintf(output, _("  \\dX     [PATTERN]      list extended statistics\n"));
     270           2 :     fprintf(output, _("  \\dy[+]  [PATTERN]      list event triggers\n"));
     271           2 :     fprintf(output, _("  \\l[+]   [PATTERN]      list databases\n"));
     272           2 :     fprintf(output, _("  \\sf[+]  FUNCNAME       show a function's definition\n"));
     273           2 :     fprintf(output, _("  \\sv[+]  VIEWNAME       show a view's definition\n"));
     274           2 :     fprintf(output, _("  \\z      [PATTERN]      same as \\dp\n"));
     275           2 :     fprintf(output, "\n");
     276             : 
     277           2 :     fprintf(output, _("Formatting\n"));
     278           2 :     fprintf(output, _("  \\a                     toggle between unaligned and aligned output mode\n"));
     279           2 :     fprintf(output, _("  \\C [STRING]            set table title, or unset if none\n"));
     280           2 :     fprintf(output, _("  \\f [STRING]            show or set field separator for unaligned query output\n"));
     281           2 :     fprintf(output, _("  \\H                     toggle HTML output mode (currently %s)\n"),
     282           2 :             ON(pset.popt.topt.format == PRINT_HTML));
     283           2 :     fprintf(output, _("  \\pset [NAME [VALUE]]   set table output option\n"
     284             :                       "                         (border|columns|csv_fieldsep|expanded|fieldsep|\n"
     285             :                       "                         fieldsep_zero|footer|format|linestyle|null|\n"
     286             :                       "                         numericlocale|pager|pager_min_lines|recordsep|\n"
     287             :                       "                         recordsep_zero|tableattr|title|tuples_only|\n"
     288             :                       "                         unicode_border_linestyle|unicode_column_linestyle|\n"
     289             :                       "                         unicode_header_linestyle)\n"));
     290           2 :     fprintf(output, _("  \\t [on|off]            show only rows (currently %s)\n"),
     291           2 :             ON(pset.popt.topt.tuples_only));
     292           2 :     fprintf(output, _("  \\T [STRING]            set HTML <table> tag attributes, or unset if none\n"));
     293           2 :     fprintf(output, _("  \\x [on|off|auto]       toggle expanded output (currently %s)\n"),
     294           2 :             pset.popt.topt.expanded == 2 ? "auto" : ON(pset.popt.topt.expanded));
     295           2 :     fprintf(output, "\n");
     296             : 
     297           2 :     fprintf(output, _("Connection\n"));
     298           2 :     if (currdb)
     299           0 :         fprintf(output, _("  \\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
     300             :                           "                         connect to new database (currently \"%s\")\n"),
     301             :                 currdb);
     302             :     else
     303           2 :         fprintf(output, _("  \\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
     304             :                           "                         connect to new database (currently no connection)\n"));
     305           2 :     fprintf(output, _("  \\conninfo              display information about current connection\n"));
     306           2 :     fprintf(output, _("  \\encoding [ENCODING]   show or set client encoding\n"));
     307           2 :     fprintf(output, _("  \\password [USERNAME]   securely change the password for a user\n"));
     308           2 :     fprintf(output, "\n");
     309             : 
     310           2 :     fprintf(output, _("Operating System\n"));
     311           2 :     fprintf(output, _("  \\cd [DIR]              change the current working directory\n"));
     312           2 :     fprintf(output, _("  \\setenv NAME [VALUE]   set or unset environment variable\n"));
     313           2 :     fprintf(output, _("  \\timing [on|off]       toggle timing of commands (currently %s)\n"),
     314           2 :             ON(pset.timing));
     315           2 :     fprintf(output, _("  \\! [COMMAND]           execute command in shell or start interactive shell\n"));
     316           2 :     fprintf(output, "\n");
     317             : 
     318           2 :     fprintf(output, _("Variables\n"));
     319           2 :     fprintf(output, _("  \\prompt [TEXT] NAME    prompt user to set internal variable\n"));
     320           2 :     fprintf(output, _("  \\set [NAME [VALUE]]    set internal variable, or list all if no parameters\n"));
     321           2 :     fprintf(output, _("  \\unset NAME            unset (delete) internal variable\n"));
     322           2 :     fprintf(output, "\n");
     323             : 
     324           2 :     fprintf(output, _("Large Objects\n"));
     325           2 :     fprintf(output, _("  \\lo_export LOBOID FILE\n"
     326             :                       "  \\lo_import FILE [COMMENT]\n"
     327             :                       "  \\lo_list\n"
     328             :                       "  \\lo_unlink LOBOID      large object operations\n"));
     329             : 
     330           2 :     ClosePager(output);
     331           2 : }
     332             : 
     333             : 
     334             : /*
     335             :  * helpVariables
     336             :  *
     337             :  * show list of available variables (options) from command line
     338             :  */
     339             : void
     340           2 : helpVariables(unsigned short int pager)
     341             : {
     342             :     FILE       *output;
     343             : 
     344             :     /*
     345             :      * Keep this line count in sync with the number of lines printed below!
     346             :      * Use "psql --help=variables | wc" to count correctly; but notice that
     347             :      * Windows builds currently print one fewer line than non-Windows builds.
     348             :      * Using the larger number is fine.
     349             :      */
     350           2 :     output = PageOutput(161, pager ? &(pset.popt.topt) : NULL);
     351             : 
     352           2 :     fprintf(output, _("List of specially treated variables\n\n"));
     353             : 
     354           2 :     fprintf(output, _("psql variables:\n"));
     355           2 :     fprintf(output, _("Usage:\n"));
     356           2 :     fprintf(output, _("  psql --set=NAME=VALUE\n  or \\set NAME VALUE inside psql\n\n"));
     357             : 
     358           2 :     fprintf(output, _("  AUTOCOMMIT\n"
     359             :                       "    if set, successful SQL commands are automatically committed\n"));
     360           2 :     fprintf(output, _("  COMP_KEYWORD_CASE\n"
     361             :                       "    determines the case used to complete SQL key words\n"
     362             :                       "    [lower, upper, preserve-lower, preserve-upper]\n"));
     363           2 :     fprintf(output, _("  DBNAME\n"
     364             :                       "    the currently connected database name\n"));
     365           2 :     fprintf(output, _("  ECHO\n"
     366             :                       "    controls what input is written to standard output\n"
     367             :                       "    [all, errors, none, queries]\n"));
     368           2 :     fprintf(output, _("  ECHO_HIDDEN\n"
     369             :                       "    if set, display internal queries executed by backslash commands;\n"
     370             :                       "    if set to \"noexec\", just show them without execution\n"));
     371           2 :     fprintf(output, _("  ENCODING\n"
     372             :                       "    current client character set encoding\n"));
     373           2 :     fprintf(output, _("  ERROR\n"
     374             :                       "    true if last query failed, else false\n"));
     375           2 :     fprintf(output, _("  FETCH_COUNT\n"
     376             :                       "    the number of result rows to fetch and display at a time (0 = unlimited)\n"));
     377           2 :     fprintf(output, _("  HIDE_TABLEAM\n"
     378             :                       "    if set, table access methods are not displayed\n"));
     379           2 :     fprintf(output, _("  HIDE_TOAST_COMPRESSION\n"
     380             :                       "    if set, compression methods are not displayed\n"));
     381           2 :     fprintf(output, _("  HISTCONTROL\n"
     382             :                       "    controls command history [ignorespace, ignoredups, ignoreboth]\n"));
     383           2 :     fprintf(output, _("  HISTFILE\n"
     384             :                       "    file name used to store the command history\n"));
     385           2 :     fprintf(output, _("  HISTSIZE\n"
     386             :                       "    maximum number of commands to store in the command history\n"));
     387           2 :     fprintf(output, _("  HOST\n"
     388             :                       "    the currently connected database server host\n"));
     389           2 :     fprintf(output, _("  IGNOREEOF\n"
     390             :                       "    number of EOFs needed to terminate an interactive session\n"));
     391           2 :     fprintf(output, _("  LASTOID\n"
     392             :                       "    value of the last affected OID\n"));
     393           2 :     fprintf(output, _("  LAST_ERROR_MESSAGE\n"
     394             :                       "  LAST_ERROR_SQLSTATE\n"
     395             :                       "    message and SQLSTATE of last error, or empty string and \"00000\" if none\n"));
     396           2 :     fprintf(output, _("  ON_ERROR_ROLLBACK\n"
     397             :                       "    if set, an error doesn't stop a transaction (uses implicit savepoints)\n"));
     398           2 :     fprintf(output, _("  ON_ERROR_STOP\n"
     399             :                       "    stop batch execution after error\n"));
     400           2 :     fprintf(output, _("  PORT\n"
     401             :                       "    server port of the current connection\n"));
     402           2 :     fprintf(output, _("  PROMPT1\n"
     403             :                       "    specifies the standard psql prompt\n"));
     404           2 :     fprintf(output, _("  PROMPT2\n"
     405             :                       "    specifies the prompt used when a statement continues from a previous line\n"));
     406           2 :     fprintf(output, _("  PROMPT3\n"
     407             :                       "    specifies the prompt used during COPY ... FROM STDIN\n"));
     408           2 :     fprintf(output, _("  QUIET\n"
     409             :                       "    run quietly (same as -q option)\n"));
     410           2 :     fprintf(output, _("  ROW_COUNT\n"
     411             :                       "    number of rows returned or affected by last query, or 0\n"));
     412           2 :     fprintf(output, _("  SERVER_VERSION_NAME\n"
     413             :                       "  SERVER_VERSION_NUM\n"
     414             :                       "    server's version (in short string or numeric format)\n"));
     415           2 :     fprintf(output, _("  SHOW_CONTEXT\n"
     416             :                       "    controls display of message context fields [never, errors, always]\n"));
     417           2 :     fprintf(output, _("  SINGLELINE\n"
     418             :                       "    if set, end of line terminates SQL commands (same as -S option)\n"));
     419           2 :     fprintf(output, _("  SINGLESTEP\n"
     420             :                       "    single-step mode (same as -s option)\n"));
     421           2 :     fprintf(output, _("  SQLSTATE\n"
     422             :                       "    SQLSTATE of last query, or \"00000\" if no error\n"));
     423           2 :     fprintf(output, _("  USER\n"
     424             :                       "    the currently connected database user\n"));
     425           2 :     fprintf(output, _("  VERBOSITY\n"
     426             :                       "    controls verbosity of error reports [default, verbose, terse, sqlstate]\n"));
     427           2 :     fprintf(output, _("  VERSION\n"
     428             :                       "  VERSION_NAME\n"
     429             :                       "  VERSION_NUM\n"
     430             :                       "    psql's version (in verbose string, short string, or numeric format)\n"));
     431             : 
     432           2 :     fprintf(output, _("\nDisplay settings:\n"));
     433           2 :     fprintf(output, _("Usage:\n"));
     434           2 :     fprintf(output, _("  psql --pset=NAME[=VALUE]\n  or \\pset NAME [VALUE] inside psql\n\n"));
     435             : 
     436           2 :     fprintf(output, _("  border\n"
     437             :                       "    border style (number)\n"));
     438           2 :     fprintf(output, _("  columns\n"
     439             :                       "    target width for the wrapped format\n"));
     440           2 :     fprintf(output, _("  expanded (or x)\n"
     441             :                       "    expanded output [on, off, auto]\n"));
     442           2 :     fprintf(output, _("  fieldsep\n"
     443             :                       "    field separator for unaligned output (default \"%s\")\n"),
     444             :             DEFAULT_FIELD_SEP);
     445           2 :     fprintf(output, _("  fieldsep_zero\n"
     446             :                       "    set field separator for unaligned output to a zero byte\n"));
     447           2 :     fprintf(output, _("  footer\n"
     448             :                       "    enable or disable display of the table footer [on, off]\n"));
     449           2 :     fprintf(output, _("  format\n"
     450             :                       "    set output format [unaligned, aligned, wrapped, html, asciidoc, ...]\n"));
     451           2 :     fprintf(output, _("  linestyle\n"
     452             :                       "    set the border line drawing style [ascii, old-ascii, unicode]\n"));
     453           2 :     fprintf(output, _("  null\n"
     454             :                       "    set the string to be printed in place of a null value\n"));
     455           2 :     fprintf(output, _("  numericlocale\n"
     456             :                       "    enable display of a locale-specific character to separate groups of digits\n"));
     457           2 :     fprintf(output, _("  pager\n"
     458             :                       "    control when an external pager is used [yes, no, always]\n"));
     459           2 :     fprintf(output, _("  recordsep\n"
     460             :                       "    record (line) separator for unaligned output\n"));
     461           2 :     fprintf(output, _("  recordsep_zero\n"
     462             :                       "    set record separator for unaligned output to a zero byte\n"));
     463           2 :     fprintf(output, _("  tableattr (or T)\n"
     464             :                       "    specify attributes for table tag in html format, or proportional\n"
     465             :                       "    column widths for left-aligned data types in latex-longtable format\n"));
     466           2 :     fprintf(output, _("  title\n"
     467             :                       "    set the table title for subsequently printed tables\n"));
     468           2 :     fprintf(output, _("  tuples_only\n"
     469             :                       "    if set, only actual table data is shown\n"));
     470           2 :     fprintf(output, _("  unicode_border_linestyle\n"
     471             :                       "  unicode_column_linestyle\n"
     472             :                       "  unicode_header_linestyle\n"
     473             :                       "    set the style of Unicode line drawing [single, double]\n"));
     474             : 
     475           2 :     fprintf(output, _("\nEnvironment variables:\n"));
     476           2 :     fprintf(output, _("Usage:\n"));
     477             : 
     478             : #ifndef WIN32
     479           2 :     fprintf(output, _("  NAME=VALUE [NAME=VALUE] psql ...\n  or \\setenv NAME [VALUE] inside psql\n\n"));
     480             : #else
     481             :     fprintf(output, _("  set NAME=VALUE\n  psql ...\n  or \\setenv NAME [VALUE] inside psql\n\n"));
     482             : #endif
     483             : 
     484           2 :     fprintf(output, _("  COLUMNS\n"
     485             :                       "    number of columns for wrapped format\n"));
     486           2 :     fprintf(output, _("  PGAPPNAME\n"
     487             :                       "    same as the application_name connection parameter\n"));
     488           2 :     fprintf(output, _("  PGDATABASE\n"
     489             :                       "    same as the dbname connection parameter\n"));
     490           2 :     fprintf(output, _("  PGHOST\n"
     491             :                       "    same as the host connection parameter\n"));
     492           2 :     fprintf(output, _("  PGPASSFILE\n"
     493             :                       "    password file name\n"));
     494           2 :     fprintf(output, _("  PGPASSWORD\n"
     495             :                       "    connection password (not recommended)\n"));
     496           2 :     fprintf(output, _("  PGPORT\n"
     497             :                       "    same as the port connection parameter\n"));
     498           2 :     fprintf(output, _("  PGUSER\n"
     499             :                       "    same as the user connection parameter\n"));
     500           2 :     fprintf(output, _("  PSQL_EDITOR, EDITOR, VISUAL\n"
     501             :                       "    editor used by the \\e, \\ef, and \\ev commands\n"));
     502           2 :     fprintf(output, _("  PSQL_EDITOR_LINENUMBER_ARG\n"
     503             :                       "    how to specify a line number when invoking the editor\n"));
     504           2 :     fprintf(output, _("  PSQL_HISTORY\n"
     505             :                       "    alternative location for the command history file\n"));
     506           2 :     fprintf(output, _("  PSQL_PAGER, PAGER\n"
     507             :                       "    name of external pager program\n"));
     508             : #ifndef WIN32
     509           2 :     fprintf(output, _("  PSQL_WATCH_PAGER\n"
     510             :                       "    name of external pager program used for \\watch\n"));
     511             : #endif
     512           2 :     fprintf(output, _("  PSQLRC\n"
     513             :                       "    alternative location for the user's .psqlrc file\n"));
     514           2 :     fprintf(output, _("  SHELL\n"
     515             :                       "    shell used by the \\! command\n"));
     516           2 :     fprintf(output, _("  TMPDIR\n"
     517             :                       "    directory for temporary files\n"));
     518             : 
     519           2 :     ClosePager(output);
     520           2 : }
     521             : 
     522             : 
     523             : /*
     524             :  * helpSQL -- help with SQL commands
     525             :  *
     526             :  * Note: we assume caller removed any trailing spaces in "topic".
     527             :  */
     528             : void
     529           4 : helpSQL(const char *topic, unsigned short int pager)
     530             : {
     531             : #define VALUE_OR_NULL(a) ((a) ? (a) : "")
     532             : 
     533           4 :     if (!topic || strlen(topic) == 0)
     534           2 :     {
     535             :         /* Print all the available command names */
     536             :         int         screen_width;
     537             :         int         ncolumns;
     538             :         int         nrows;
     539             :         FILE       *output;
     540             :         int         i;
     541             :         int         j;
     542             : 
     543             :         /* Find screen width to determine how many columns will fit */
     544             : #ifdef TIOCGWINSZ
     545             :         struct winsize screen_size;
     546             : 
     547           2 :         if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1)
     548           2 :             screen_width = 80;  /* ioctl failed, assume 80 */
     549             :         else
     550           0 :             screen_width = screen_size.ws_col;
     551             : #else
     552             :         screen_width = 80;      /* default assumption */
     553             : #endif
     554             : 
     555           2 :         ncolumns = (screen_width - 3) / (QL_MAX_CMD_LEN + 1);
     556           2 :         ncolumns = Max(ncolumns, 1);
     557           2 :         nrows = (QL_HELP_COUNT + (ncolumns - 1)) / ncolumns;
     558             : 
     559           2 :         output = PageOutput(nrows + 1, pager ? &(pset.popt.topt) : NULL);
     560             : 
     561           2 :         fputs(_("Available help:\n"), output);
     562             : 
     563         186 :         for (i = 0; i < nrows; i++)
     564             :         {
     565         184 :             fprintf(output, "  ");
     566         368 :             for (j = 0; j < ncolumns - 1; j++)
     567         184 :                 fprintf(output, "%-*s",
     568             :                         QL_MAX_CMD_LEN + 1,
     569         184 :                         VALUE_OR_NULL(QL_HELP[i + j * nrows].cmd));
     570         184 :             if (i + j * nrows < QL_HELP_COUNT)
     571         184 :                 fprintf(output, "%s",
     572         184 :                         VALUE_OR_NULL(QL_HELP[i + j * nrows].cmd));
     573         184 :             fputc('\n', output);
     574             :         }
     575             : 
     576           2 :         ClosePager(output);
     577             :     }
     578             :     else
     579             :     {
     580             :         int         i,
     581             :                     pass;
     582           2 :         FILE       *output = NULL;
     583             :         size_t      len,
     584             :                     wordlen,
     585             :                     j;
     586             :         int         nl_count;
     587             : 
     588             :         /*
     589             :          * len is the amount of the input to compare to the help topic names.
     590             :          * We first try exact match, then first + second words, then first
     591             :          * word only.
     592             :          */
     593           2 :         len = strlen(topic);
     594             : 
     595           2 :         for (pass = 1; pass <= 3; pass++)
     596             :         {
     597           2 :             if (pass > 1)        /* Nothing on first pass - try the opening
     598             :                                  * word(s) */
     599             :             {
     600           0 :                 wordlen = j = 1;
     601           0 :                 while (j < len && topic[j++] != ' ')
     602           0 :                     wordlen++;
     603           0 :                 if (pass == 2 && j < len)
     604             :                 {
     605           0 :                     wordlen++;
     606           0 :                     while (j < len && topic[j++] != ' ')
     607           0 :                         wordlen++;
     608             :                 }
     609           0 :                 if (wordlen >= len)
     610             :                 {
     611             :                     /* Failed to shorten input, so try next pass if any */
     612           0 :                     continue;
     613             :                 }
     614           0 :                 len = wordlen;
     615             :             }
     616             : 
     617             :             /*
     618             :              * Count newlines for pager.  This logic must agree with what the
     619             :              * following loop will do!
     620             :              */
     621           2 :             nl_count = 0;
     622         338 :             for (i = 0; QL_HELP[i].cmd; i++)
     623             :             {
     624         338 :                 if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
     625         336 :                     strcmp(topic, "*") == 0)
     626             :                 {
     627             :                     /* magic constant here must match format below! */
     628           2 :                     nl_count += 7 + QL_HELP[i].nl_count;
     629             : 
     630             :                     /* If we have an exact match, exit.  Fixes \h SELECT */
     631           2 :                     if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
     632           2 :                         break;
     633             :                 }
     634             :             }
     635             :             /* If no matches, don't open the output yet */
     636           2 :             if (nl_count == 0)
     637           0 :                 continue;
     638             : 
     639           2 :             if (!output)
     640           2 :                 output = PageOutput(nl_count, pager ? &(pset.popt.topt) : NULL);
     641             : 
     642         338 :             for (i = 0; QL_HELP[i].cmd; i++)
     643             :             {
     644         338 :                 if (pg_strncasecmp(topic, QL_HELP[i].cmd, len) == 0 ||
     645         336 :                     strcmp(topic, "*") == 0)
     646             :                 {
     647             :                     PQExpBufferData buffer;
     648             :                     char       *url;
     649             : 
     650           2 :                     initPQExpBuffer(&buffer);
     651           2 :                     QL_HELP[i].syntaxfunc(&buffer);
     652           2 :                     url = psprintf("https://www.postgresql.org/docs/%s/%s.html",
     653             :                                    strstr(PG_VERSION, "devel") ? "devel" : PG_MAJORVERSION,
     654             :                                    QL_HELP[i].docbook_id);
     655             :                     /* # of newlines in format must match constant above! */
     656           2 :                     fprintf(output, _("Command:     %s\n"
     657             :                                       "Description: %s\n"
     658             :                                       "Syntax:\n%s\n\n"
     659             :                                       "URL: %s\n\n"),
     660             :                             QL_HELP[i].cmd,
     661             :                             _(QL_HELP[i].help),
     662             :                             buffer.data,
     663             :                             url);
     664           2 :                     free(url);
     665           2 :                     termPQExpBuffer(&buffer);
     666             : 
     667             :                     /* If we have an exact match, exit.  Fixes \h SELECT */
     668           2 :                     if (pg_strcasecmp(topic, QL_HELP[i].cmd) == 0)
     669           2 :                         break;
     670             :                 }
     671             :             }
     672           2 :             break;
     673             :         }
     674             : 
     675             :         /* If we never found anything, report that */
     676           2 :         if (!output)
     677             :         {
     678           0 :             output = PageOutput(2, pager ? &(pset.popt.topt) : NULL);
     679           0 :             fprintf(output, _("No help available for \"%s\".\n"
     680             :                               "Try \\h with no arguments to see available help.\n"),
     681             :                     topic);
     682             :         }
     683             : 
     684           2 :         ClosePager(output);
     685             :     }
     686           4 : }
     687             : 
     688             : 
     689             : 
     690             : void
     691           2 : print_copyright(void)
     692             : {
     693           2 :     puts("PostgreSQL Database Management System\n"
     694             :          "(formerly known as Postgres, then as Postgres95)\n\n"
     695             :          "Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group\n\n"
     696             :          "Portions Copyright (c) 1994, The Regents of the University of California\n\n"
     697             :          "Permission to use, copy, modify, and distribute this software and its\n"
     698             :          "documentation for any purpose, without fee, and without a written agreement\n"
     699             :          "is hereby granted, provided that the above copyright notice and this\n"
     700             :          "paragraph and the following two paragraphs appear in all copies.\n\n"
     701             :          "IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR\n"
     702             :          "DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING\n"
     703             :          "LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS\n"
     704             :          "DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE\n"
     705             :          "POSSIBILITY OF SUCH DAMAGE.\n\n"
     706             :          "THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,\n"
     707             :          "INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY\n"
     708             :          "AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS\n"
     709             :          "ON AN \"AS IS\" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO\n"
     710             :          "PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.\n");
     711           2 : }

Generated by: LCOV version 1.14