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

Re: Builtin test and parsing of conditionals



On Wed, 04 Sep 2013 10:39:40 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Sep 4,  5:31pm, Peter Stephenson wrote:
> } On Wed, 04 Sep 2013 09:15:03 -0700
> } Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> } > According to some discussion on the austin-group (POSIX) mailing list,
> } > the following:
> } > 
> } > 	test ! -a !
> } > 	test ! -o !
> } > 	test ! = !
> } > 
> } > should all be parsed as comparing the string "!" to the string "!", but
> } > zsh gets this right only in the last case.
> 
> Sorry, perhaps I should have been clearer:  "! -a !" means to compare the
> truth value "!" to the truth value of "!".  But "truth value" in "test"
> is the same as "non-empty string" (true) and "empty string" (false) [not
> 0 or 1 as numeric values], so the -a operator is still comparing two
> strings.

OK, this looks straightforward enough.

diff --git a/Src/parse.c b/Src/parse.c
index 0c2a458..f0d0855 100644
--- a/Src/parse.c
+++ b/Src/parse.c
@@ -2088,9 +2088,17 @@ par_cond_2(void)
 	}
     }
     if (tok == BANG) {
-	condlex();
-	ecadd(WCB_COND(COND_NOT, 0));
-	return par_cond_2();
+	/*
+	 * In "test" compatibility mode, "! -a ..." and "! -o ..."
+	 * are treated as "[string] [and] ..." and "[string] [or] ...".
+	 */
+	if (!(condlex == testlex && *testargs && 
+	      (!strcmp(*testargs, "-a") || !strcmp(*testargs, "-o"))))
+	{
+	    condlex();
+	    ecadd(WCB_COND(COND_NOT, 0));
+	    return par_cond_2();
+	}
     }
     if (tok == INPAR) {
 	int r;
diff --git a/Test/C02cond.ztst b/Test/C02cond.ztst
index 494261e..8562519 100644
--- a/Test/C02cond.ztst
+++ b/Test/C02cond.ztst
@@ -324,6 +324,27 @@ F:Failures in these cases do not indicate a problem in the shell.
 >	fi
 >}
 
+  weirdies=(
+    '! -a !'
+    '! -o !'
+    '! -a'
+    '! -o'
+    '! -a ! -a !'
+    '! = !'
+    '! !')
+  for w in $weirdies; do
+     eval test $w
+     print $?
+  done
+0:test compatability weirdness: treat ! as a string sometimes
+>0
+>0
+>1
+>0
+>0
+>0
+>1
+
 %clean
   # This works around a bug in rm -f in some versions of Cygwin
   chmod 644 unmodish

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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