Re: Trailing semicolons in psql patch - Mailing list pgsql-patches

From Greg Sabino Mullane
Subject Re: Trailing semicolons in psql patch
Date
Msg-id 200110041152.HAA31074@hall.mail.mindspring.net
Whole thread Raw
In response to Trailing semicolons in psql patch  (greg@turnstep.com)
Responses Re: Trailing semicolons in psql patch  (Bruce Momjian <pgman@candle.pha.pa.us>)
Re: Trailing semicolons in psql patch  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-patches
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Attached is the updated version of the patch, which matches
on words as opposed to lines, which means that all of the
following work in psql:

\d foo \d bar
\d foo; \d bar
\d foo \d bar;;
\d foo; <space>


This one also uses "true and false" and strips semicolons
for the following backslash commands: \C \c \d \e \i \o \s \z

Thanks to Bruce Momjian, Peter Eisentraut, and Tom Lane for
the suggestions.

Greg Sabino Mullane
greg@turnstep.com
PGP Key: 0x14964AC8 200110040743

-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html

iQA+AwUBO7xNp7ybkGcUlkrIEQKzwgCXcaQGh16cXELnjKDs7lpg7BP7twCg1jHW
lDE1BVxPTrLXQZeQRn+ieu4=
=PK53
-----END PGP SIGNATURE-----
*** ./src/bin/psql/command.c.orig    Thu Oct  4 07:26:30 2001
--- ./src/bin/psql/command.c    Thu Oct  4 07:30:37 2001
***************
*** 55,61 ****
  {
      OT_NORMAL, OT_SQLID, OT_FILEPIPE
  };
! static char *scan_option(char **string, enum option_type type, char *quote);
  static char *unescape(const unsigned char *source, size_t len);

  static bool do_edit(const char *filename_arg, PQExpBuffer query_buf);
--- 55,61 ----
  {
      OT_NORMAL, OT_SQLID, OT_FILEPIPE
  };
! 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);
***************
*** 216,222 ****
      /* \C -- override table title (formerly change HTML caption) */
      else if (strcmp(cmd, "C") == 0)
      {
!         char       *opt = scan_option(&string, OT_NORMAL, NULL);

          success = do_pset("title", opt, &pset.popt, quiet);
          free(opt);
--- 216,222 ----
      /* \C -- override table title (formerly change HTML caption) */
      else if (strcmp(cmd, "C") == 0)
      {
!         char       *opt = scan_option(&string, OT_NORMAL, NULL, true);

          success = do_pset("title", opt, &pset.popt, quiet);
          free(opt);
***************
*** 238,245 ****
          char        opt1q,
                      opt2q;

!         opt1 = scan_option(&string, OT_NORMAL, &opt1q);
!         opt2 = scan_option(&string, OT_NORMAL, &opt2q);

          if (opt2)
              /* gave username */
--- 238,245 ----
          char        opt1q,
                      opt2q;

!         opt1 = scan_option(&string, OT_NORMAL, &opt1q, true);
!         opt2 = scan_option(&string, OT_NORMAL, &opt2q, true);

          if (opt2)
              /* gave username */
***************
*** 273,280 ****
      {
          char       *name;
          bool        show_verbose;

-         name = scan_option(&string, OT_SQLID, NULL);
          show_verbose = strchr(cmd, '+') ? true : false;

          switch (cmd[1])
--- 273,280 ----
      {
          char       *name;
          bool        show_verbose;
+         name = scan_option(&string, OT_SQLID, NULL, true);

          show_verbose = strchr(cmd, '+') ? true : false;

          switch (cmd[1])
***************
*** 337,343 ****
          }
          else
          {
!             fname = scan_option(&string, OT_NORMAL, NULL);
              status = do_edit(fname, query_buf) ? CMD_NEWEDIT : CMD_ERROR;
              free(fname);
          }
--- 337,343 ----
          }
          else
          {
!             fname = scan_option(&string, OT_NORMAL, NULL, true);
              status = do_edit(fname, query_buf) ? CMD_NEWEDIT : CMD_ERROR;
              free(fname);
          }
***************
*** 356,363 ****
              fout = pset.queryFout;
          else
              fout = stdout;
!
!         while ((value = scan_option(&string, OT_NORMAL, "ed)))
          {
              if (!quoted && strcmp(value, "-n") == 0)
                  no_newline = true;
--- 356,363 ----
              fout = pset.queryFout;
          else
              fout = stdout;
!
!         while ((value = scan_option(&string, OT_NORMAL, "ed, false)))
          {
              if (!quoted && strcmp(value, "-n") == 0)
                  no_newline = true;
***************
*** 378,384 ****
      /* \encoding -- set/show client side encoding */
      else if (strcmp(cmd, "encoding") == 0)
      {
!         char       *encoding = scan_option(&string, OT_NORMAL, NULL);

          if (!encoding)
              /* show encoding */
--- 378,384 ----
      /* \encoding -- set/show client side encoding */
      else if (strcmp(cmd, "encoding") == 0)
      {
!         char       *encoding = scan_option(&string, OT_NORMAL, NULL, false);

          if (!encoding)
              /* show encoding */
***************
*** 406,412 ****
      /* \f -- change field separator */
      else if (strcmp(cmd, "f") == 0)
      {
!         char       *fname = scan_option(&string, OT_NORMAL, NULL);

          success = do_pset("fieldsep", fname, &pset.popt, quiet);
          free(fname);
--- 406,412 ----
      /* \f -- change field separator */
      else if (strcmp(cmd, "f") == 0)
      {
!         char       *fname = scan_option(&string, OT_NORMAL, NULL, false);

          success = do_pset("fieldsep", fname, &pset.popt, quiet);
          free(fname);
***************
*** 415,421 ****
      /* \g means send query */
      else if (strcmp(cmd, "g") == 0)
      {
!         char       *fname = scan_option(&string, OT_FILEPIPE, NULL);

          if (!fname)
              pset.gfname = NULL;
--- 415,421 ----
      /* \g means send query */
      else if (strcmp(cmd, "g") == 0)
      {
!         char       *fname = scan_option(&string, OT_FILEPIPE, NULL, false);

          if (!fname)
              pset.gfname = NULL;
***************
*** 447,453 ****
      /* \i is include file */
      else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0)
      {
!         char       *fname = scan_option(&string, OT_NORMAL, NULL);

          if (!fname)
          {
--- 447,453 ----
      /* \i is include file */
      else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0)
      {
!         char       *fname = scan_option(&string, OT_NORMAL, NULL, true);

          if (!fname)
          {
***************
*** 475,482 ****
          char       *opt1,
                     *opt2;

!         opt1 = scan_option(&string, OT_NORMAL, NULL);
!         opt2 = scan_option(&string, OT_NORMAL, NULL);

          if (strcmp(cmd + 3, "export") == 0)
          {
--- 475,482 ----
          char       *opt1,
                     *opt2;

!         opt1 = scan_option(&string, OT_NORMAL, NULL, true);
!         opt2 = scan_option(&string, OT_NORMAL, NULL, true);

          if (strcmp(cmd + 3, "export") == 0)
          {
***************
*** 525,531 ****
      /* \o -- set query output */
      else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
      {
!         char       *fname = scan_option(&string, OT_FILEPIPE, NULL);

          success = setQFout(fname);
          free(fname);
--- 525,531 ----
      /* \o -- set query output */
      else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
      {
!         char       *fname = scan_option(&string, OT_FILEPIPE, NULL, true);

          success = setQFout(fname);
          free(fname);
***************
*** 544,551 ****
      /* \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);

          if (!opt0)
          {
--- 544,551 ----
      /* \pset -- set printing parameters */
      else if (strcmp(cmd, "pset") == 0)
      {
!         char       *opt0 = scan_option(&string, OT_NORMAL, NULL, false);
!         char       *opt1 = scan_option(&string, OT_NORMAL, NULL, false);

          if (!opt0)
          {
***************
*** 574,580 ****
      /* \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);

          success = saveHistory(fname ? fname : "/dev/tty");

--- 574,580 ----
      /* \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, true);

          success = saveHistory(fname ? fname : "/dev/tty");

***************
*** 586,592 ****
      /* \set -- generalized set variable/option command */
      else if (strcmp(cmd, "set") == 0)
      {
!         char       *opt0 = scan_option(&string, OT_NORMAL, NULL);

          if (!opt0)
          {
--- 586,592 ----
      /* \set -- generalized set variable/option command */
      else if (strcmp(cmd, "set") == 0)
      {
!         char       *opt0 = scan_option(&string, OT_NORMAL, NULL, false);

          if (!opt0)
          {
***************
*** 611,621 ****
              char       *newval = NULL;
              char       *opt;

!             opt = scan_option(&string, OT_NORMAL, NULL);
              newval = xstrdup(opt ? opt : "");
              free(opt);

!             while ((opt = scan_option(&string, OT_NORMAL, NULL)))
              {
                  newval = realloc(newval, strlen(newval) + strlen(opt) + 1);
                  if (!newval)
--- 611,621 ----
              char       *newval = NULL;
              char       *opt;

!             opt = scan_option(&string, OT_NORMAL, NULL, false);
              newval = xstrdup(opt ? opt : "");
              free(opt);

!             while ((opt = scan_option(&string, OT_NORMAL, NULL, false)))
              {
                  newval = realloc(newval, strlen(newval) + strlen(opt) + 1);
                  if (!newval)
***************
*** 645,651 ****
      /* \T -- define html <table ...> attributes */
      else if (strcmp(cmd, "T") == 0)
      {
!         char       *value = scan_option(&string, OT_NORMAL, NULL);

          success = do_pset("tableattr", value, &pset.popt, quiet);
          free(value);
--- 645,651 ----
      /* \T -- define html <table ...> attributes */
      else if (strcmp(cmd, "T") == 0)
      {
!         char       *value = scan_option(&string, OT_NORMAL, NULL, false);

          success = do_pset("tableattr", value, &pset.popt, quiet);
          free(value);
***************
*** 654,660 ****
      /* \unset */
      else if (strcmp(cmd, "unset") == 0)
      {
!         char       *opt = scan_option(&string, OT_NORMAL, NULL);

          if (!opt)
          {
--- 654,660 ----
      /* \unset */
      else if (strcmp(cmd, "unset") == 0)
      {
!         char       *opt = scan_option(&string, OT_NORMAL, NULL, false);

          if (!opt)
          {
***************
*** 683,689 ****
          }
          else
          {
!             fname = scan_option(&string, OT_FILEPIPE, NULL);

              if (!fname)
              {
--- 683,689 ----
          }
          else
          {
!             fname = scan_option(&string, OT_FILEPIPE, NULL, true);

              if (!fname)
              {
***************
*** 738,744 ****
      /* \z -- list table rights (grant/revoke) */
      else if (strcmp(cmd, "z") == 0)
      {
!         char       *opt = scan_option(&string, OT_SQLID, NULL);

          success = permissionsList(opt);
          free(opt);
--- 738,744 ----
      /* \z -- list table rights (grant/revoke) */
      else if (strcmp(cmd, "z") == 0)
      {
!         char       *opt = scan_option(&string, OT_SQLID, NULL, true);

          success = permissionsList(opt);
          free(opt);
***************
*** 769,775 ****
          char       *value;

          fprintf(stderr, "+ optstr = |%s|\n", options_string);
!         while ((value = scan_option(&string, OT_NORMAL, NULL)))
          {
              fprintf(stderr, "+ opt(%d) = |%s|\n", i++, value);
              free(value);
--- 769,775 ----
          char       *value;

          fprintf(stderr, "+ optstr = |%s|\n", options_string);
!         while ((value = scan_option(&string, OT_NORMAL, NULL, true)))
          {
              fprintf(stderr, "+ opt(%d) = |%s|\n", i++, value);
              free(value);
***************
*** 784,790 ****
          status = CMD_ERROR;

      /* eat the rest of the options string */
!     while ((val = scan_option(&string, OT_NORMAL, NULL)))
      {
          if (status != CMD_UNKNOWN)
              psql_error("\\%s: extra argument '%s' ignored\n", cmd, val);
--- 784,790 ----
          status = CMD_ERROR;

      /* eat the rest of the options string */
!     while ((val = scan_option(&string, OT_NORMAL, NULL, false)))
      {
          if (status != CMD_UNKNOWN)
              psql_error("\\%s: extra argument '%s' ignored\n", cmd, val);
***************
*** 803,809 ****
   * scan_option()
   */
  static char *
! scan_option(char **string, enum option_type type, char *quote)
  {
      unsigned int pos = 0;
      char       *options_string;
--- 803,809 ----
   * scan_option()
   */
  static char *
! scan_option(char **string, enum option_type type, char *quote, bool semicolon)
  {
      unsigned int pos = 0;
      char       *options_string;
***************
*** 860,865 ****
--- 860,866 ----
                   * 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,
***************
*** 891,897 ****
                  *string = options_string + jj + 1;
                  if (quote)
                      *quote = '"';
!
                  return return_val;
              }

--- 892,898 ----
                  *string = options_string + jj + 1;
                  if (quote)
                      *quote = '"';
!
                  return return_val;
              }

***************
*** 1071,1076 ****
--- 1072,1084 ----
                  }
                  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))

pgsql-patches by date:

Previous
From: Vsevolod Lobko
Date:
Subject: Re: [PATCH] Patch for more verbose error reporting in
Next
From: Bruce Momjian
Date:
Subject: Re: Trailing semicolons in psql patch