PostgreSQL Source Code  git master
help.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include <sys/ioctl.h>
#include "common.h"
#include "common/logging.h"
#include "common/username.h"
#include "help.h"
#include "input.h"
#include "settings.h"
#include "sql_help.h"
Include dependency graph for help.c:

Go to the source code of this file.

Macros

#define HELP0(str)   appendPQExpBufferStr(&buf, _(str))
 
#define HELPN(str, ...)   appendPQExpBuffer(&buf, _(str), __VA_ARGS__)
 
#define ON(var)   ((var) ? _("on") : _("off"))
 
#define VALUE_OR_NULL(a)   ((a) ? (a) : "")
 

Functions

void usage (unsigned short int pager)
 
void slashUsage (unsigned short int pager)
 

Macro Definition Documentation

◆ HELP0

#define HELP0 (   str)    appendPQExpBufferStr(&buf, _(str))

Definition at line 40 of file help.c.

◆ HELPN

#define HELPN (   str,
  ... 
)    appendPQExpBuffer(&buf, _(str), __VA_ARGS__)

Definition at line 41 of file help.c.

◆ ON

#define ON (   var)    ((var) ? _("on") : _("off"))

Definition at line 42 of file help.c.

◆ VALUE_OR_NULL

#define VALUE_OR_NULL (   a)    ((a) ? (a) : "")

Function Documentation

◆ slashUsage()

◆ usage()

void usage ( unsigned short int  pager)

Definition at line 51 of file help.c.

52 {
53  const char *env;
54  const char *user;
55  char *errstr;
57  int nlcount;
58  FILE *output;
59 
60  /* Find default user, in case we need it. */
61  user = getenv("PGUSER");
62  if (!user)
63  {
64  user = get_user_name(&errstr);
65  if (!user)
66  pg_fatal("%s", errstr);
67  }
68 
69  /*
70  * To avoid counting the output lines manually, build the output in "buf"
71  * and then count them.
72  */
74 
75  HELP0("psql is the PostgreSQL interactive terminal.\n\n");
76  HELP0("Usage:\n");
77  HELP0(" psql [OPTION]... [DBNAME [USERNAME]]\n\n");
78 
79  HELP0("General options:\n");
80  /* Display default database */
81  env = getenv("PGDATABASE");
82  if (!env)
83  env = user;
84  HELP0(" -c, --command=COMMAND run only single command (SQL or internal) and exit\n");
85  HELPN(" -d, --dbname=DBNAME database name to connect to (default: \"%s\")\n",
86  env);
87  HELP0(" -f, --file=FILENAME execute commands from file, then exit\n");
88  HELP0(" -l, --list list available databases, then exit\n");
89  HELP0(" -v, --set=, --variable=NAME=VALUE\n"
90  " set psql variable NAME to VALUE\n"
91  " (e.g., -v ON_ERROR_STOP=1)\n");
92  HELP0(" -V, --version output version information, then exit\n");
93  HELP0(" -X, --no-psqlrc do not read startup file (~/.psqlrc)\n");
94  HELP0(" -1 (\"one\"), --single-transaction\n"
95  " execute as a single transaction (if non-interactive)\n");
96  HELP0(" -?, --help[=options] show this help, then exit\n");
97  HELP0(" --help=commands list backslash commands, then exit\n");
98  HELP0(" --help=variables list special variables, then exit\n");
99 
100  HELP0("\nInput and output options:\n");
101  HELP0(" -a, --echo-all echo all input from script\n");
102  HELP0(" -b, --echo-errors echo failed commands\n");
103  HELP0(" -e, --echo-queries echo commands sent to server\n");
104  HELP0(" -E, --echo-hidden display queries that internal commands generate\n");
105  HELP0(" -L, --log-file=FILENAME send session log to file\n");
106  HELP0(" -n, --no-readline disable enhanced command line editing (readline)\n");
107  HELP0(" -o, --output=FILENAME send query results to file (or |pipe)\n");
108  HELP0(" -q, --quiet run quietly (no messages, only query output)\n");
109  HELP0(" -s, --single-step single-step mode (confirm each query)\n");
110  HELP0(" -S, --single-line single-line mode (end of line terminates SQL command)\n");
111 
112  HELP0("\nOutput format options:\n");
113  HELP0(" -A, --no-align unaligned table output mode\n");
114  HELP0(" --csv CSV (Comma-Separated Values) table output mode\n");
115  HELPN(" -F, --field-separator=STRING\n"
116  " field separator for unaligned output (default: \"%s\")\n",
118  HELP0(" -H, --html HTML table output mode\n");
119  HELP0(" -P, --pset=VAR[=ARG] set printing option VAR to ARG (see \\pset command)\n");
120  HELP0(" -R, --record-separator=STRING\n"
121  " record separator for unaligned output (default: newline)\n");
122  HELP0(" -t, --tuples-only print rows only\n");
123  HELP0(" -T, --table-attr=TEXT set HTML table tag attributes (e.g., width, border)\n");
124  HELP0(" -x, --expanded turn on expanded table output\n");
125  HELP0(" -z, --field-separator-zero\n"
126  " set field separator for unaligned output to zero byte\n");
127  HELP0(" -0, --record-separator-zero\n"
128  " set record separator for unaligned output to zero byte\n");
129 
130  HELP0("\nConnection options:\n");
131  /* Display default host */
132  env = getenv("PGHOST");
133  HELPN(" -h, --host=HOSTNAME database server host or socket directory (default: \"%s\")\n",
134  env ? env : _("local socket"));
135  /* Display default port */
136  env = getenv("PGPORT");
137  HELPN(" -p, --port=PORT database server port (default: \"%s\")\n",
138  env ? env : DEF_PGPORT_STR);
139  /* Display default user */
140  HELPN(" -U, --username=USERNAME database user name (default: \"%s\")\n",
141  user);
142  HELP0(" -w, --no-password never prompt for password\n");
143  HELP0(" -W, --password force password prompt (should happen automatically)\n");
144 
145  HELP0("\nFor more information, type \"\\?\" (for internal commands) or \"\\help\" (for SQL\n"
146  "commands) from within psql, or consult the psql section in the PostgreSQL\n"
147  "documentation.\n\n");
148  HELPN("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
149  HELPN("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
150 
151  /* Now we can count the lines. */
152  nlcount = 0;
153  for (const char *ptr = buf.data; *ptr; ptr++)
154  {
155  if (*ptr == '\n')
156  nlcount++;
157  }
158 
159  /* And dump the output, with appropriate pagination. */
160  output = PageOutput(nlcount, pager ? &(pset.popt.topt) : NULL);
161 
162  fputs(buf.data, output);
163 
165 
167 }
#define _(x)
Definition: elog.c:91
void ClosePager(FILE *pagerpipe)
Definition: print.c:3141
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3089
#define HELPN(str,...)
Definition: help.c:41
#define HELP0(str)
Definition: help.c:40
FILE * output
#define pg_fatal(...)
static char * user
Definition: pg_regress.c:112
static char * buf
Definition: pg_test_fsync.c:67
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
#define DEFAULT_FIELD_SEP
Definition: settings.h:15
PsqlSettings pset
Definition: startup.c:32
printQueryOpt popt
Definition: settings.h:91
printTableOpt topt
Definition: print.h:185
const char * get_user_name(char **errstr)
Definition: username.c:31

References _, buf, ClosePager(), DEFAULT_FIELD_SEP, get_user_name(), HELP0, HELPN, initPQExpBuffer(), output, PageOutput(), pg_fatal, _psqlSettings::popt, pset, termPQExpBuffer(), printQueryOpt::topt, and user.