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

Generated by: LCOV version 1.14