*
* Copyright 2000 by PostgreSQL Global Development Group
*
- * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.58 2001/09/10 14:51:33 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.59 2001/10/05 19:01:13 momjian Exp $
*/
#include "postgres_fe.h"
#include "command.h"
{
OT_NORMAL, OT_SQLID, OT_FILEPIPE
};
-static char *scan_option(char **string, enum option_type type, char *quote);
+static char *scan_option(char **string, enum option_type type, char *quote, bool semicolon);
static char *unescape(const unsigned char *source, size_t len);
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf);
/* \C -- override table title (formerly change HTML caption) */
else if (strcmp(cmd, "C") == 0)
{
- char *opt = scan_option(&string, OT_NORMAL, NULL);
+ char *opt = scan_option(&string, OT_NORMAL, NULL, true);
success = do_pset("title", opt, &pset.popt, quiet);
free(opt);
char opt1q,
opt2q;
- opt1 = scan_option(&string, OT_SQLID, &opt1q);
- opt2 = scan_option(&string, OT_SQLID, &opt2q);
+ opt1 = scan_option(&string, OT_SQLID, &opt1q, true);
+ opt2 = scan_option(&string, OT_SQLID, &opt2q, true);
if (opt2)
/* gave username */
/* \cd */
else if (strcmp(cmd, "cd") == 0)
{
- char *opt = scan_option(&string, OT_NORMAL, NULL);
+ char *opt = scan_option(&string, OT_NORMAL, NULL, true);
char *dir;
if (opt)
{
char *name;
bool show_verbose;
+ name = scan_option(&string, OT_SQLID, NULL, true);
- name = scan_option(&string, OT_SQLID, NULL);
show_verbose = strchr(cmd, '+') ? true : false;
switch (cmd[1])
}
else
{
- fname = scan_option(&string, OT_NORMAL, NULL);
+ fname = scan_option(&string, OT_NORMAL, NULL, true);
status = do_edit(fname, query_buf) ? CMD_NEWEDIT : CMD_ERROR;
free(fname);
}
fout = pset.queryFout;
else
fout = stdout;
-
- while ((value = scan_option(&string, OT_NORMAL, "ed)))
+
+ while ((value = scan_option(&string, OT_NORMAL, "ed, false)))
{
if (!quoted && strcmp(value, "-n") == 0)
no_newline = true;
/* \encoding -- set/show client side encoding */
else if (strcmp(cmd, "encoding") == 0)
{
- char *encoding = scan_option(&string, OT_NORMAL, NULL);
+ char *encoding = scan_option(&string, OT_NORMAL, NULL, false);
if (!encoding)
/* show encoding */
/* \f -- change field separator */
else if (strcmp(cmd, "f") == 0)
{
- char *fname = scan_option(&string, OT_NORMAL, NULL);
+ char *fname = scan_option(&string, OT_NORMAL, NULL, false);
success = do_pset("fieldsep", fname, &pset.popt, quiet);
free(fname);
/* \g means send query */
else if (strcmp(cmd, "g") == 0)
{
- char *fname = scan_option(&string, OT_FILEPIPE, NULL);
+ char *fname = scan_option(&string, OT_FILEPIPE, NULL, false);
if (!fname)
pset.gfname = NULL;
/* \i is include file */
else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0)
{
- char *fname = scan_option(&string, OT_NORMAL, NULL);
+ char *fname = scan_option(&string, OT_NORMAL, NULL, true);
if (!fname)
{
char *opt1,
*opt2;
- opt1 = scan_option(&string, OT_NORMAL, NULL);
- opt2 = scan_option(&string, OT_NORMAL, NULL);
+ opt1 = scan_option(&string, OT_NORMAL, NULL, true);
+ opt2 = scan_option(&string, OT_NORMAL, NULL, true);
if (strcmp(cmd + 3, "export") == 0)
{
/* \o -- set query output */
else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
{
- char *fname = scan_option(&string, OT_FILEPIPE, NULL);
+ char *fname = scan_option(&string, OT_FILEPIPE, NULL, true);
success = setQFout(fname);
free(fname);
/* \pset -- set printing parameters */
else if (strcmp(cmd, "pset") == 0)
{
- char *opt0 = scan_option(&string, OT_NORMAL, NULL);
- char *opt1 = scan_option(&string, OT_NORMAL, NULL);
+ char *opt0 = scan_option(&string, OT_NORMAL, NULL, false);
+ char *opt1 = scan_option(&string, OT_NORMAL, NULL, false);
if (!opt0)
{
/* \s save history in a file or show it on the screen */
else if (strcmp(cmd, "s") == 0)
{
- char *fname = scan_option(&string, OT_NORMAL, NULL);
+ char *fname = scan_option(&string, OT_NORMAL, NULL, true);
success = saveHistory(fname ? fname : "/dev/tty");
/* \set -- generalized set variable/option command */
else if (strcmp(cmd, "set") == 0)
{
- char *opt0 = scan_option(&string, OT_NORMAL, NULL);
+ char *opt0 = scan_option(&string, OT_NORMAL, NULL, false);
if (!opt0)
{
char *newval = NULL;
char *opt;
- opt = scan_option(&string, OT_NORMAL, NULL);
+ opt = scan_option(&string, OT_NORMAL, NULL, false);
newval = xstrdup(opt ? opt : "");
free(opt);
- while ((opt = scan_option(&string, OT_NORMAL, NULL)))
+ while ((opt = scan_option(&string, OT_NORMAL, NULL, false)))
{
newval = realloc(newval, strlen(newval) + strlen(opt) + 1);
if (!newval)
/* \T -- define html <table ...> attributes */
else if (strcmp(cmd, "T") == 0)
{
- char *value = scan_option(&string, OT_NORMAL, NULL);
+ char *value = scan_option(&string, OT_NORMAL, NULL, false);
success = do_pset("tableattr", value, &pset.popt, quiet);
free(value);
/* \unset */
else if (strcmp(cmd, "unset") == 0)
{
- char *opt = scan_option(&string, OT_NORMAL, NULL);
+ char *opt = scan_option(&string, OT_NORMAL, NULL, false);
if (!opt)
{
}
else
{
- fname = scan_option(&string, OT_FILEPIPE, NULL);
+ fname = scan_option(&string, OT_FILEPIPE, NULL, true);
if (!fname)
{
/* \z -- list table rights (grant/revoke) */
else if (strcmp(cmd, "z") == 0)
{
- char *opt = scan_option(&string, OT_SQLID, NULL);
+ char *opt = scan_option(&string, OT_SQLID, NULL, true);
success = permissionsList(opt);
free(opt);
char *value;
fprintf(stderr, "+ optstr = |%s|\n", options_string);
- while ((value = scan_option(&string, OT_NORMAL, NULL)))
+ while ((value = scan_option(&string, OT_NORMAL, NULL, true)))
{
fprintf(stderr, "+ opt(%d) = |%s|\n", i++, value);
free(value);
status = CMD_ERROR;
/* eat the rest of the options string */
- while ((val = scan_option(&string, OT_NORMAL, NULL)))
+ while ((val = scan_option(&string, OT_NORMAL, NULL, false)))
{
if (status != CMD_UNKNOWN)
psql_error("\\%s: extra argument '%s' ignored\n", cmd, val);
* scan_option()
*/
static char *
-scan_option(char **string, enum option_type type, char *quote)
+scan_option(char **string, enum option_type type, char *quote, bool semicolon)
{
unsigned int pos = 0;
char *options_string;
* If this is expected to be an SQL identifier like option
* then we strip out the double quotes
*/
+
if (type == OT_SQLID)
{
unsigned int k,
*string = options_string + jj + 1;
if (quote)
*quote = '"';
-
+
return return_val;
}
strncpy(return_val, &options_string[pos], token_end);
return_val[token_end] = 0;
+ /* Strip any trailing semi-colons for some types */
+ if (semicolon) {
+ int i;
+ for (i = strlen(return_val)-1; i && return_val[i]==';'; i--);
+ if (i<strlen(return_val)-1) return_val[i+1]='\0';
+ }
+
if (type == OT_SQLID)
for (cp = return_val; *cp; cp += PQmblen(cp, pset.encoding))
if (isupper((unsigned char) *cp))