You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postgres/src/bin/psql/help.c

317 lines
10 KiB

/*
* psql - the PostgreSQL interactive terminal
*
* Copyright 2000 by PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.21 2000/02/20 02:37:40 tgl Exp $
*/
#include "postgres.h"
#include "help.h"
#include <signal.h>
#include <errno.h>
#ifndef WIN32
26 years ago
#include <sys/ioctl.h> /* for ioctl() */
#ifdef HAVE_PWD_H
26 years ago
#include <pwd.h> /* for getpwuid() */
#endif
26 years ago
#include <sys/types.h> /* (ditto) */
#include <unistd.h> /* for getuid() */
#else
#include <win32.h>
#endif
#include "pqsignal.h"
#include "libpq-fe.h"
#include "settings.h"
#include "common.h"
#include "sql_help.h"
/*
* usage
*
* print out command line arguments
*/
#define ON(var) (var ? "on" : "off")
26 years ago
void
usage(void)
{
26 years ago
const char *env;
const char *user;
#ifndef WIN32
26 years ago
struct passwd *pw = NULL;
#endif
26 years ago
/* Find default user, in case we need it. */
user = getenv("USER");
if (!user)
{
#ifndef WIN32
pw = getpwuid(geteuid());
26 years ago
if (pw)
user = pw->pw_name;
else
{
psql_error("could not get current user name: %s\n", strerror(errno));
26 years ago
exit(EXIT_FAILURE);
}
#else
26 years ago
user = "?";
#endif
26 years ago
}
/* If this " is the start of the string then it ought to end there to fit in 80 columns >> " */
puts( "This is psql, the PostgreSQL interactive terminal.\n");
puts( "Usage:");
puts( " psql [options] [dbname [username]]\n");
puts( "Options:");
puts( " -a Echo all input from script");
puts( " -A Unaligned table output mode (-P format=unaligned)");
puts( " -c <query> Run only single query (or slash command) and exit");
26 years ago
/* Display default database */
env = getenv("PGDATABASE");
if (!env)
env = user;
printf(" -d <dbname> Specify database name to connect to (default: %s)\n", env);
26 years ago
puts( " -e Echo queries sent to backend");
puts( " -E Display queries that internal commands generate");
puts( " -f <filename> Execute queries from file, then exit");
puts( " -F <string> Set field separator (default: \"" DEFAULT_FIELD_SEP "\") (-P fieldsep=)");
26 years ago
/* Display default host */
env = getenv("PGHOST");
printf(" -h <host> Specify database server host (default: ");
26 years ago
if (env)
fputs(env, stdout);
26 years ago
else
fputs("domain socket", stdout);
puts(")");
26 years ago
puts( " -H HTML table output mode (-P format=html)");
puts( " -l List available databases, then exit");
puts( " -o <filename> Send query output to filename (or |pipe)");
26 years ago
/* Display default port */
env = getenv("PGPORT");
printf(" -p <port> Specify database server port (default: %s)\n",
26 years ago
env ? env : "hardwired");
26 years ago
puts( " -P var[=arg] Set printing option 'var' to 'arg' (see \\pset command)");
puts( " -q Run quietly (no messages, only query output)");
puts( " -R <string> Set record separator (default: newline) (-P recordsep=)");
puts( " -s Single step mode (confirm each query)");
puts( " -S Single line mode (newline terminates query)");
puts( " -t Don't print headings and row count (-P tuples_only)");
puts( " -T text Set HTML table tag options (width, border) (-P tableattr=)");
26 years ago
/* Display default user */
env = getenv("PGUSER");
if (!env)
env = user;
printf(" -U <username> Specify database username (default: %s)\n", env);
26 years ago
puts( " -x Turn on expanded table output (-P expanded)");
puts( " -v name=val Set psql variable 'name' to 'value'");
puts( " -V Show version information and exit");
puts( " -W Prompt for password (should happen automatically)");
26 years ago
puts( "\nFor more information, type \"\\?\" (for internal commands) or \"\\help\"");
puts( "(for SQL commands) from within psql, or consult the psql section in");
puts( "the PostgreSQL manual, which accompanies the distribution and is also");
puts( "available at <http://www.postgresql.org>.");
puts( "Report bugs to <pgsql-bugs@postgresql.org>.");
#ifndef WIN32
26 years ago
if (pw)
free(pw);
#endif
}
/*
* slashUsage
*
* print out help for the backslash commands
*/
#ifndef TIOCGWINSZ
26 years ago
struct winsize
{
int ws_row;
int ws_col;
};
26 years ago
#endif
void
slashUsage(void)
{
26 years ago
bool usePipe = false;
const char *pagerenv;
FILE *fout;
struct winsize screen_size;
#ifdef TIOCGWINSZ
if (pset.notty == 0 &&
26 years ago
(ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1 ||
screen_size.ws_col == 0 ||
screen_size.ws_row == 0))
{
#endif
26 years ago
screen_size.ws_row = 24;
screen_size.ws_col = 80;
#ifdef TIOCGWINSZ
26 years ago
}
#endif
if (pset.notty == 0 &&
26 years ago
(pagerenv = getenv("PAGER")) &&
(pagerenv[0] != '\0') &&
screen_size.ws_row <= 36 &&
(fout = popen(pagerenv, "w")))
{
usePipe = true;
#ifndef WIN32
26 years ago
pqsignal(SIGPIPE, SIG_IGN);
#endif
26 years ago
}
else
fout = stdout;
/* if you add/remove a line here, change the row test above */
fprintf(fout, " \\c[onnect] [dbname|- [user]]\n"
26 years ago
" connect to new database (currently '%s')\n", PQdb(pset.db));
fprintf(fout, " \\copy ... perform SQL COPY with data stream to the client machine\n");
fprintf(fout, " \\copyright show PostgreSQL usage and distribution terms\n");
fprintf(fout, " \\d <table> describe table (or view, index, sequence)\n");
fprintf(fout, " \\d{i|s|t|v|S} list only indices/sequences/tables/views/system tables\n");
fprintf(fout, " \\da list aggregates\n");
fprintf(fout, " \\dd [object] list comment for table, type, function, or operator\n");
fprintf(fout, " \\df list functions\n");
fprintf(fout, " \\do list operators\n");
fprintf(fout, " \\dT list data types\n");
fprintf(fout, " \\e [fname] edit the current query buffer or <fname> with external editor\n");
fprintf(fout, " \\echo <text> write text to stdout\n");
#ifdef MULTIBYTE
fprintf(fout, " \\eset <encoding> set client encoding\n");
fprintf(fout, " \\eshow show client encoding\n");
#endif
fprintf(fout, " \\g [fname] send query to backend (and results in <fname> or |pipe)\n");
fprintf(fout, " \\h [cmd] help on syntax of sql commands, * for all commands\n");
fprintf(fout, " \\i <fname> read and execute queries from filename\n");
fprintf(fout, " \\l list all databases\n");
fprintf(fout, " \\lo_export, \\lo_import, \\lo_list, \\lo_unlink\n"
" large object operations\n");
fprintf(fout, " \\o [fname] send all query results to <fname>, or |pipe\n");
fprintf(fout, " \\p show the content of the current query buffer\n");
fprintf(fout, " \\pset {format|border|expanded|fieldsep|recordsep|tuples_only|title|tableattr\n"
" |pager} set table output options\n");
fprintf(fout, " \\q quit psql\n");
fprintf(fout, " \\qecho <text> write text to query output stream (see \\o)\n");
fprintf(fout, " \\r reset (clear) the query buffer\n");
fprintf(fout, " \\s [fname] print history or save it in <fname>\n");
fprintf(fout, " \\set <var> <value> set internal variable\n");
fprintf(fout, " \\t don't show table headers or footers (currently %s)\n", ON(pset.popt.topt.tuples_only));
fprintf(fout, " \\unset <var> unset (delete) internal variable\n");
fprintf(fout, " \\x toggle expanded output (currently %s)\n", ON(pset.popt.topt.expanded));
fprintf(fout, " \\w <fname> write current query buffer to a file\n");
fprintf(fout, " \\z list table access permissions\n");
fprintf(fout, " \\! [cmd] shell escape or command\n");
26 years ago
if (usePipe)
{
pclose(fout);
#ifndef WIN32
26 years ago
pqsignal(SIGPIPE, SIG_DFL);
#endif
26 years ago
}
}
/*
* helpSQL -- help with SQL commands
*
*/
void
helpSQL(const char *topic)
{
#define VALUE_OR_NULL(a) ((a) ? (a) : "")
26 years ago
if (!topic || strlen(topic) == 0)
{
int i;
int items_per_column = (QL_HELP_COUNT + 2)/3;
26 years ago
puts("Available help:");
26 years ago
for (i = 0; i < items_per_column; i++)
26 years ago
{
printf(" %-25s%-25s%-25s\n",
VALUE_OR_NULL(QL_HELP[i].cmd),
VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd),
VALUE_OR_NULL(QL_HELP[i + 2*items_per_column].cmd)
);
26 years ago
}
putc('\n', stdout);
}
26 years ago
else
{
26 years ago
int i;
bool help_found = false;
for (i = 0; QL_HELP[i].cmd; i++)
{
if (strcasecmp(QL_HELP[i].cmd, topic) == 0 ||
strcmp(topic, "*") == 0)
{
help_found = true;
printf("Command: %s\n"
"Description: %s\n"
"Syntax:\n%s\n\n",
QL_HELP[i].cmd, QL_HELP[i].help, QL_HELP[i].syntax);
26 years ago
}
}
if (!help_found)
printf("No help available for '%s'.\nTry \\h with no arguments to see available help.\n", topic);
}
}
void
print_copyright(void)
{
26 years ago
puts(
"PostgreSQL Data Base Management System\n\n"
"Portions Copyright (c) 1996-2000, PostgreSQL, Inc\n\n"
"This software is based on Postgres95, formerly known as Postgres, which\n"
"contains the following notice:\n\n"
"Portions Copyright(c) 1994 - 7 Regents of the University of California\n\n"
"Permission to use, copy, modify, and distribute this software and its\n"
"documentation for any purpose, without fee, and without a written agreement\n"
"is hereby granted, provided that the above copyright notice and this paragraph\n"
"and the following two paragraphs appear in all copies.\n\n"
"IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR\n"
"DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST\n"
"PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF\n"
"THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH\n"
"DAMAGE.\n\n"
"THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,\n"
"BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\n"
"PARTICULAR PURPOSE.THE SOFTWARE PROVIDED HEREUNDER IS ON AN \"AS IS\" BASIS,\n"
"AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,\n"
"SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
);
}