Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Builtin test and parsing of conditionals



On Thu, 05 Sep 2013 07:25:45 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> That's good, but it still doesn't fix the other case mentioned in my
> original email:
> 
> torch% /usr/bin/test ! = -a o ; print $?
> 1
> torch% test ! = -a o ; print $? 
> test: too many arguments
> 1
> 
> Do you want to try to handle that in parse.c, or am I correct that peeling
> off the "!" in builtin.c:bin_test [the way parens are peeled off] is the
> better way to go?

That seems sensible.

diff --git a/Src/builtin.c b/Src/builtin.c
index f8be4ac..c3f0169 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5995,7 +5995,7 @@ bin_test(char *name, char **argv, UNUSED(Options ops), int func)
     char **s;
     Eprog prog;
     struct estate state;
-    int nargs;
+    int nargs, sense = 0, ret;
 
     /* if "test" was invoked as "[", it needs a matching "]" *
      * which is subsequently ignored                         */
@@ -6014,7 +6014,7 @@ bin_test(char *name, char **argv, UNUSED(Options ops), int func)
     /*
      * Implement some XSI extensions to POSIX here.
      * See
-     * http://www.opengroup.org/onlinepubs/009695399/utilities/test.html.
+     * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
      */
     nargs = arrlen(argv);
     if (nargs == 3 || nargs == 4)
@@ -6023,6 +6023,10 @@ bin_test(char *name, char **argv, UNUSED(Options ops), int func)
 	    argv[nargs-1] = NULL;
 	    argv++;
 	}
+	if (nargs == 4 && !strcmp("!", argv[0])) {
+	    sense = 1;
+	    argv++;
+	}
     }
 
     lexsave();
@@ -6057,8 +6061,11 @@ bin_test(char *name, char **argv, UNUSED(Options ops), int func)
     state.pc = prog->prog;
     state.strs = prog->strs;
 
+    ret = evalcond(&state, name);
+    if (ret < 2 && sense)
+	ret = ! ret;
 
-    return evalcond(&state, name);
+    return ret;
 }
 
 /* display a time, provided in units of 1/60s, as minutes and seconds */
diff --git a/Test/C02cond.ztst b/Test/C02cond.ztst
index 8562519..94fca8b 100644
--- a/Test/C02cond.ztst
+++ b/Test/C02cond.ztst
@@ -331,7 +331,9 @@ F:Failures in these cases do not indicate a problem in the shell.
     '! -o'
     '! -a ! -a !'
     '! = !'
-    '! !')
+    '! !'
+    '= -a o'
+    '! = -a o')
   for w in $weirdies; do
      eval test $w
      print $?
@@ -344,6 +346,8 @@ F:Failures in these cases do not indicate a problem in the shell.
 >0
 >0
 >1
+>0
+>1
 
 %clean
   # This works around a bug in rm -f in some versions of Cygwin



Messages sorted by: Reverse Date, Date, Thread, Author