bug-sh-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

suggestion: "+ FOO" rather than "quote FOO" for GNU sh-utils 'expr'


From: Paul Eggert
Subject: suggestion: "+ FOO" rather than "quote FOO" for GNU sh-utils 'expr'
Date: Thu, 21 Jun 2001 11:56:55 -0700 (PDT)

GNU 'expr' has an incompatibility with POSIX: it uses the "quote"
keyword to quote operands, and POSIX does not allow the implementation
to add keywords to 'expr'.

Here is an idea to remove this incompatibility: use "+" instead of
"quote" to quote keywords.  Using "+" is upward compatible with POSIX,
since POSIX says that "+" is reserved.  Also, "+" is shorter and
easier to read.

This fix won't affect any portable shell scripts, since portable code
cannot use "quote".  I don't know of any GNU shell scripts that use
"quote" so I think it's OK to make this change.  It's more likely that
a future POSIX revision will accept an upward-compatible extension
like "+" than the incompatible extension "quote", so I think the
change is worthwhile.

This patch is relative to the latest test version of GNU sh-utils that
I have and assumes earlier patches that I've sent privately, but I'm
sending this patch via address@hidden because I want to hear
from people (if anybody) who are using "quote".


Also, GNU 'expr --help' does not work as expected if POSIXLY_CORRECT
is set.  This behavior isn't required by POSIX, since POSIX allows
expr to have implementation-defined options.  The following patch
removes this glitch as well.


2001-06-21  Paul Eggert  <address@hidden>

        * NEWS, doc/coreutils.texi: 'expr' now requires '+' rather
        than 'quote' to quote tokens.

        * src/expr.c (posixly_correct): Remove; no longer needed.
        (main): Do not worry about POSIXLY_CORRECT, as it's OK for
        expr to have options (so long as they do not begin with digits).
        (eval6, usage): Use "+" rather than "quote" to quote tokens.

===================================================================
RCS file: NEWS,v
retrieving revision 1.144.0.1
retrieving revision 1.144.0.2
diff -pu -r1.144.0.1 -r1.144.0.2
--- NEWS        2001/06/20 02:13:31     1.144.0.1
+++ NEWS        2001/06/21 18:43:31     1.144.0.2
@@ -1,4 +1,6 @@
 Changes in release 2.1
+* 'expr' now requires '+' rather than 'quote' to quote tokens;
+  this removes an incompatibility with POSIX.
 * 'expr' now uses the LC_COLLATE locale for string comparison, as per POSIX.
  [2.0.12]
 * date -d 'last friday' would print a date/time that was one hour off
===================================================================
RCS file: doc/coreutils.texi,v
retrieving revision 1.2.0.2
retrieving revision 1.2.0.3
diff -pu -r1.2.0.2 -r1.2.0.3
--- doc/coreutils.texi  2001/06/20 02:10:12     1.2.0.2
+++ doc/coreutils.texi  2001/06/21 18:43:31     1.2.0.3
@@ -333,7 +333,7 @@ Conditions
 
 @code{expr}: Evaluate expression
 
-* String expressions::         <colon> match substr index length quote
+* String expressions::         + : match substr index length
 * Numeric expressions::        + - * / %
 * Relations for expr::         | & < <= = == != >= >
 * Examples of expr::           Examples of using @code{expr}
@@ -8154,7 +8154,7 @@ Exit status:
 @end display
 
 @menu
-* String expressions::          <colon> match substr index length quote
+* String expressions::          + : match substr index length
 * Numeric expressions::         + - * / %
 * Relations for expr::          | & < <= = == != >= >
 * Examples of expr::            Examples.
@@ -8245,19 +8245,16 @@ compatibility.  For a portable way to co
 bytes, use the @code{:} operator instead, e.g., @code{expr @w{"
 $string"} : '.*' - 1}.
 
address@hidden quote @var{token}
address@hidden quote
address@hidden + @var{token}
address@hidden +
 Interpret @var{token} as a string, even if it is a keyword like @code{match}
 or an operator like @code{/}.
 This makes it possible to test
address@hidden quote "$x" : '.*/\(.\)'} and have it do the right thing even if
address@hidden + "$x" : '.*/\(.\)'} and have it do the right thing even if
 the value of @var{$x} happens to be (for example) @code{/} or @code{index}.
 
address@hidden POSIXLY_CORRECT
-This operator is a GNU extension that is incompatible with @sc{posix},
-and it is disabled when the environment variable @env{POSIXLY_CORRECT}
-is set.  Portable shell scripts should use @address@hidden" $token"} : @w{'
-\(.*\)'}} instead of @code{quote "$token"}.
+This operator is a GNU extension.  Portable shell scripts should use
address@hidden@w{" $token"} : @w{' \(.*\)'}} instead of @code{+ "$token"}.
 
 @end table
 
===================================================================
RCS file: src/expr.c,v
retrieving revision 1.63.1.5
retrieving revision 1.63.1.6
diff -pu -r1.63.1.5 -r1.63.1.6
--- src/expr.c  2001/06/21 01:06:24     1.63.1.5
+++ src/expr.c  2001/06/21 18:43:31     1.63.1.6
@@ -68,10 +68,6 @@ struct valinfo
 };
 typedef struct valinfo VALUE;
 
-/* Non-zero if the POSIXLY_CORRECT environment variable is set.
-   The unary operator `quote' is disabled when this variable is zero.  */
-static int posixly_correct;
-
 /* The arguments given to the program, minus the program name.  */
 static char **args;
 
@@ -130,7 +126,7 @@ separates increasing precedence groups. 
   substr STRING POS LENGTH   substring of STRING, POS counted from 1\n\
   index STRING CHARS         index in STRING where any CHARS is found, or 0\n\
   length STRING              length of STRING\n\
-  quote TOKEN                interpret TOKEN as a string, even if it is a\n\
+  + TOKEN                    interpret TOKEN as a string, even if it is a\n\
                                keyword like `match' or an operator like `/'\n\
 \n\
   ( EXPRESSION )             value of EXPRESSION\n\
@@ -159,12 +155,8 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
-
-  /* Recognize --help or --version only if POSIXLY_CORRECT is not set.  */
-  if (!posixly_correct)
-    parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
-                       AUTHORS, usage);
+  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
+                     AUTHORS, usage);
   /* The above handles --help and --version.
      Since there is no other invocation of getopt, handle `--' here.  */
   if (argc > 1 && STREQ (argv[1], "--"))
@@ -530,7 +522,7 @@ eval7 (void)
   return str_value (*args++);
 }
 
-/* Handle match, substr, index, length, and quote keywords.  */
+/* Handle match, substr, index, and length keywords, and quoting "+".  */
 
 static VALUE *
 eval6 (void)
@@ -544,7 +536,7 @@ eval6 (void)
 #ifdef EVAL_TRACE
   trace ("eval6");
 #endif
-  if (!posixly_correct && nextarg ("quote"))
+  if (nextarg ("+"))
     {
       args++;
       if (nomoreargs ())



reply via email to

[Prev in Thread] Current Thread [Next in Thread]