10 #include <sys/ioctl.h> /* for ioctl() */
12 #include <pwd.h> /* for getpwuid() */
14 #include <sys/types.h> /* (ditto) */
15 #include <unistd.h> /* for getuid() */
17 #define strcasecmp(x,y) stricmp(x,y)
18 #define popen(x,y) _popen(x,y)
19 #define pclose(x) _pclose(x)
33 * print out command line arguments and exit
35 #define ON(var) (var ? "on" : "off")
44 struct passwd *pw = NULL;
48 /* Find default user, in case we need it. */
49 user = getenv("USER");
53 pw = getpwuid(getuid());
66 /* If string begins " here, then it ought to end there to fit on an 80 column terminal> > > > > > > " */
67 fprintf(stderr, "Usage: psql [options] [dbname [username]] \n");
68 fprintf(stderr, " -A Unaligned table output mode (-P format=unaligned)\n");
69 fprintf(stderr, " -c query Run single query (slash commands, too) and exit\n");
71 /* Display default database */
72 env = getenv("PGDATABASE");
75 fprintf(stderr, " -d dbname Specify database name to connect to (default: %s)\n", env);
77 fprintf(stderr, " -e Echo all input in non-interactive mode\n");
78 fprintf(stderr, " -E Display queries that internal commands generate\n");
79 fprintf(stderr, " -f filename Execute queries from file, then exit\n");
80 fprintf(stderr, " -F sep Set field separator (default: '" DEFAULT_FIELD_SEP "') (-P fieldsep=)\n");
82 /* Display default host */
83 env = getenv("PGHOST");
84 fprintf(stderr, " -h host Specify database server host (default: ");
88 fprintf(stderr, "domain socket");
89 fprintf(stderr, ")\n");
91 fprintf(stderr, " -H HTML table output mode (-P format=html)\n");
92 fprintf(stderr, " -l List available databases, then exit\n");
93 fprintf(stderr, " -n Do not use readline and history\n");
94 fprintf(stderr, " -o filename Send query output to filename (or |pipe)\n");
96 /* Display default port */
97 env = getenv("PGPORT");
98 fprintf(stderr, " -p port Specify database server port (default: %s)\n",
99 env ? env : "hardwired");
101 fprintf(stderr, " -P var[=arg] Set printing option 'var' to 'arg'. (see \\pset command)\n");
102 fprintf(stderr, " -q Run quietly (no messages, no prompts)\n");
103 fprintf(stderr, " -s Single step mode (confirm each query)\n");
104 fprintf(stderr, " -S Single line mode (newline sends query)\n");
105 fprintf(stderr, " -t Don't print headings and row count (-P tuples_only)\n");
106 fprintf(stderr, " -T text Set HTML table tag options (e.g., width, border)\n");
107 fprintf(stderr, " -u Prompt for username and password (same as \"-U ? -W\")\n");
109 /* Display default user */
110 env = getenv("PGUSER");
113 fprintf(stderr, " -U [username] Specifiy username, \"?\"=prompt (default user: %s)\n", env);
115 fprintf(stderr, " -x Turn on expanded table output (-P expanded)\n");
116 fprintf(stderr, " -v name=val Set psql variable 'name' to 'value'\n");
117 fprintf(stderr, " -V Show version information and exit\n");
118 fprintf(stderr, " -W Prompt for password (should happen automatically)\n");
120 fprintf(stderr, "Consult the documentation for the complete details.\n");
133 * print out help for the backslash commands
146 slashUsage(PsqlSettings *pset)
148 bool usePipe = false;
149 const char *pagerenv;
151 struct winsize screen_size;
154 if (pset->notty == 0 &&
155 (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1 ||
156 screen_size.ws_col == 0 ||
157 screen_size.ws_row == 0))
160 screen_size.ws_row = 24;
161 screen_size.ws_col = 80;
166 if (pset->notty == 0 &&
167 (pagerenv = getenv("PAGER")) &&
168 (pagerenv[0] != '\0') &&
169 screen_size.ws_row <= 36 &&
170 (fout = popen(pagerenv, "w")))
173 pqsignal(SIGPIPE, SIG_IGN);
178 /* if you add/remove a line here, change the row test above */
179 fprintf(fout, " \\? -- help\n");
180 fprintf(fout, " \\c[onnect] [<dbname>|- [<user>|?]] -- connect to new database (currently '%s')\n", PQdb(pset->db));
181 fprintf(fout, " \\copy [binary] <table> [with oids] {from|to} <fname> [with delimiters '<char>']\n");
182 fprintf(fout, " \\copyright -- show PostgreSQL copyright\n");
183 fprintf(fout, " \\d -- list tables, views, and sequences\n");
184 fprintf(fout, " \\distvS -- list only indices/sequences/tables/views/system tables\n");
185 fprintf(fout, " \\da -- list aggregates\n");
186 fprintf(fout, " \\dd [<object>]- list comment for table, type, function, or operator\n");
187 fprintf(fout, " \\df -- list functions\n");
188 fprintf(fout, " \\do -- list operators\n");
189 fprintf(fout, " \\dT -- list data types\n");
190 fprintf(fout, " \\e [<fname>] -- edit the current query buffer or <fname> with external editor\n");
191 fprintf(fout, " \\echo <text> -- write text to stdout\n");
192 fprintf(fout, " \\g [<fname>] -- send query to backend (and results in <fname> or |pipe)\n");
193 fprintf(fout, " \\h [<cmd>] -- help on syntax of sql commands, * for all commands\n");
194 fprintf(fout, " \\i <fname> -- read and execute queries from filename\n");
195 fprintf(fout, " \\l -- list all databases\n");
196 fprintf(fout, " \\lo_export, \\lo_import, \\lo_list, \\lo_unlink -- large object operations\n");
197 fprintf(fout, " \\o [<fname>] -- send all query results to <fname>, or |pipe\n");
198 fprintf(fout, " \\p -- print the content of the current query buffer\n");
199 fprintf(fout, " \\pset -- set table output options\n");
200 fprintf(fout, " \\q -- quit\n");
201 fprintf(fout, " \\qecho <text>-- write text to query output stream (see \\o)\n");
202 fprintf(fout, " \\r -- reset (clear) the query buffer\n");
203 fprintf(fout, " \\s [<fname>] -- print history or save it in <fname>\n");
204 fprintf(fout, " \\set <var> [<value>] -- set/unset internal variable\n");
205 fprintf(fout, " \\t -- don't show table headers or footers (currently %s)\n", ON(pset->popt.topt.tuples_only));
206 fprintf(fout, " \\x -- toggle expanded output (currently %s)\n", ON(pset->popt.topt.expanded));
207 fprintf(fout, " \\w <fname> -- write current query buffer to a file\n");
208 fprintf(fout, " \\z -- list table access permissions\n");
209 fprintf(fout, " \\! [<cmd>] -- shell escape or command\n");
214 pqsignal(SIGPIPE, SIG_DFL);
221 * helpSQL -- help with SQL commands
225 helpSQL(const char *topic)
227 if (!topic || strlen(topic) == 0)
229 char left_center_right; /* Which column we're displaying */
230 int i; /* Index into QL_HELP[] */
232 puts("Syntax: \\h <cmd> or \\help <cmd>, where <cmd> is one of the following:");
234 left_center_right = 'L';/* Start with left column */
236 while (QL_HELP[i].cmd != NULL)
238 switch (left_center_right)
241 printf(" %-25s", QL_HELP[i].cmd);
242 left_center_right = 'C';
245 printf("%-25s", QL_HELP[i].cmd);
246 left_center_right = 'R';
249 printf("%-25s\n", QL_HELP[i].cmd);
250 left_center_right = 'L';
255 if (left_center_right != 'L')
257 puts("Or type \\h * for a complete description of all commands.");
264 bool help_found = false;
266 for (i = 0; QL_HELP[i].cmd; i++)
268 if (strcasecmp(QL_HELP[i].cmd, topic) == 0 ||
269 strcmp(topic, "*") == 0)
272 printf("Command: %s\nDescription: %s\nSyntax:\n%s\n\n",
273 QL_HELP[i].cmd, QL_HELP[i].help, QL_HELP[i].syntax);
278 printf("No help available for '%s'.\nTry \\h with no arguments to see available help.\n", topic);
286 print_copyright(void)
290 PostgreSQL Data Base Management System
292 Copyright(c) 1996 - 9 PostgreSQL Global Development Group
294 This software is based on Postgres95, formerly known as Postgres, which
295 contains the following notice:
297 Copyright(c) 1994 - 7 Regents of the University of California
299 Permission to use, copy, modify, and distribute this software and its
300 documentation for any purpose, without fee, and without a written agreement
301 is hereby granted, provided that the above copyright notice and this paragraph
302 and the following two paragraphs appear in all copies.
304 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
305 DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
306 PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
307 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
310 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
311 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
312 PARTICULAR PURPOSE.THE SOFTWARE PROVIDED HEREUNDER IS ON AN \"AS IS\" BASIS,
313 AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
314 SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.