9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Alex Musolino <alex@musolino.id.au>
To: 9front@9front.org
Subject: Re: [9front] Patch to fix test(1) expression parsing
Date: Mon, 3 Aug 2020 23:15:28 +0930	[thread overview]
Message-ID: <2D6A76C7C50986C7669481E6D1A7D780@musolino.id.au> (raw)
In-Reply-To: <8155F03D042A477BCB2881C5C2FD20E1@musolino.id.au>

> Yeah, I did this in an updated patch I sent in a subsequent email.

It turns out that the second patch is rubbish too.  This one should be
better.  I've further amended the man page to also drop the
description of an unimplemented feature.  I'm using a test suite [1]
derived from something joe9 posted in #cat-v which, in turn, was based
on something from OpenBSD.

[1] http://musolino.id.au/up/2020/08/03/test.testsuite.rc

diff -r aa3d84fc81f6 sys/man/1/test
--- a/sys/man/1/test	Sun Aug 02 18:30:01 2020 +0930
+++ b/sys/man/1/test	Mon Aug 03 23:11:04 2020 +0930
@@ -100,11 +100,6 @@
 .BR -le
 may be used in place of
 .BR -eq .
-The (nonstandard) construct
-.BI -l " string\f1,
-meaning the length of
-.IR string ,
-may be used in place of an integer.
 .TP
 .IB a " -nt " b
 True if file
@@ -209,10 +204,3 @@
 .B /sys/src/cmd/test.c
 .SH "SEE ALSO"
 .IR rc (1) 
-.SH BUGS
-Won't complain about extraneous arguments
-since there may be arguments left unprocessed by
-short-circuit evaluation of
-.B -a
-or
-.BR -o .
diff -r aa3d84fc81f6 sys/src/cmd/test.c
--- a/sys/src/cmd/test.c	Sun Aug 02 18:30:01 2020 +0930
+++ b/sys/src/cmd/test.c	Mon Aug 03 23:11:04 2020 +0930
@@ -30,7 +30,7 @@
 int	isnewerthan(char *, char *);
 int	hasmode(char *, ulong);
 int	tio(char *, int);
-int	e(void), e1(void), e2(void), e3(void);
+int	e(int), e1(int), e2(int), e3(int);
 char	*nxtarg(int);
 
 void
@@ -47,12 +47,8 @@
 	argv[ac] = 0;
 	if (ac<=1)
 		exits("usage");
-	r = e();
-	/*
-	 * nice idea but short-circuit -o and -a operators may have
-	 * not consumed their right-hand sides.
-	 */
-	if(0 && (c = nxtarg(1)) != nil)
+	r = e(1);
+	if((c = nxtarg(1)) != nil)
 		synbad("unexpected operator/operand: ", c);
 	exits(r?0:"false");
 }
@@ -81,78 +77,128 @@
 }
 
 int
-e(void)
+e(int eval)
 {
+	char *op;
 	int p1;
 
-	p1 = e1();
-	if (EQ(nxtarg(1), "-o"))
-		return(p1 || e());
-	ap--;
-	return(p1);
+	p1 = e1(eval);
+	for(;;){
+		op = nxtarg(1);
+		if(op == nil)
+			break;
+		if(!EQ(op, "-o")){
+			ap--;
+			return p1;
+		}
+		if(!p1 && eval)
+			p1 |= e1(1);
+		else
+			e1(0);
+	}
+	return p1;
 }
 
 int
-e1(void)
+e1(int eval)
 {
+	char *op;
 	int p1;
 
-	p1 = e2();
-	if (EQ(nxtarg(1), "-a"))
-		return (p1 && e1());
-	ap--;
-	return(p1);
+	p1 = e2(eval);
+	for(;;){
+		op = nxtarg(1);
+		if(op == nil)
+			break;
+		if(!EQ(op, "-a")){
+			ap--;
+			return p1;
+		}
+		if(p1 && eval)
+			p1 &= e2(1);
+		else
+			e2(0);
+	}
+	return p1;
 }
 
 int
-e2(void)
+e2(int eval)
 {
-	if (EQ(nxtarg(0), "!"))
-		return(!e2());
+	char *op;
+	int p1;
+
+	p1 = 0;
+	for(;;){
+		op = nxtarg(1);
+		if(op == nil)
+			return p1 ^ 1;
+		if(!EQ(op, "!"))
+			break;
+		p1 ^= 1;
+	}
 	ap--;
-	return(e3());
+	return(p1^e3(eval));
 }
 
 int
-e3(void)
+e3(int eval)
 {
 	int p1, int1, int2;
-	char *a, *p2;
+	char *a, *b, *p2;
 
 	a = nxtarg(0);
 	if(EQ(a, "(")) {
-		p1 = e();
+		p1 = e(eval);
 		if(!EQ(nxtarg(0), ")"))
 			synbad(") expected","");
 		return(p1);
 	}
 
-	if(EQ(a, "-A"))
-		return(hasmode(nxtarg(0), DMAPPEND));
+	if(EQ(a, "-A")){
+		b = nxtarg(0);
+		return(eval && hasmode(b, DMAPPEND));
+	}
 
-	if(EQ(a, "-L"))
-		return(hasmode(nxtarg(0), DMEXCL));
+	if(EQ(a, "-L")){
+		b = nxtarg(0);
+		return(eval && hasmode(b, DMEXCL));
+	}
 
-	if(EQ(a, "-T"))
-		return(hasmode(nxtarg(0), DMTMP));
+	if(EQ(a, "-T")){
+		b = nxtarg(0);
+		return(eval && hasmode(b, DMTMP));
+	}
 
-	if(EQ(a, "-f"))
-		return(isreg(nxtarg(0)));
+	if(EQ(a, "-f")){
+		b = nxtarg(0);
+		return(eval && isreg(b));
+	}
 
-	if(EQ(a, "-d"))
-		return(isdir(nxtarg(0)));
+	if(EQ(a, "-d")){
+		b = nxtarg(0);
+		return(eval && isdir(b));
+	}
 
-	if(EQ(a, "-r"))
-		return(tio(nxtarg(0), AREAD));
+	if(EQ(a, "-r")){
+		b = nxtarg(0);
+		return(eval && tio(b, AREAD));
+	}
 
-	if(EQ(a, "-w"))
-		return(tio(nxtarg(0), AWRITE));
+	if(EQ(a, "-w")){
+		b = nxtarg(0);
+		return(eval && tio(b, AWRITE));
+	}
 
-	if(EQ(a, "-x"))
-		return(tio(nxtarg(0), AEXEC));
+	if(EQ(a, "-x")){
+		b = nxtarg(0);
+		return(eval && tio(b, AEXEC));
+	}
 
-	if(EQ(a, "-e"))
-		return(tio(nxtarg(0), AEXIST));
+	if(EQ(a, "-e")){
+		b = nxtarg(0);
+		return(eval && tio(b, AEXIST));
+	}
 
 	if(EQ(a, "-c"))
 		return(0);
@@ -166,14 +212,16 @@
 	if(EQ(a, "-g"))
 		return(0);
 
-	if(EQ(a, "-s"))
-		return(fsizep(nxtarg(0)));
+	if(EQ(a, "-s")){
+		b = nxtarg(0);
+		return(eval && fsizep(b));
+	}
 
 	if(EQ(a, "-t"))
 		if(ap>=ac)
-			return(isatty(1));
+			return(eval && isatty(1));
 		else if(nxtintarg(&int1))
-			return(isatty(int1));
+			return(eval && isatty(int1));
 		else
 			synbad("not a valid file descriptor number ", "");
 
@@ -191,35 +239,40 @@
 	if(EQ(p2, "!="))
 		return(!EQ(nxtarg(0), a));
 
-	if(EQ(p2, "-older"))
-		return(isolder(nxtarg(0), a));
-
-	if(EQ(p2, "-ot"))
-		return(isolderthan(nxtarg(0), a));
-
-	if(EQ(p2, "-nt"))
-		return(isnewerthan(nxtarg(0), a));
-
-	if(!isint(a, &int1))
-		synbad("unexpected operator/operand: ", p2);
-
-	if(nxtintarg(&int2)){
-		if(EQ(p2, "-eq"))
-			return(int1==int2);
-		if(EQ(p2, "-ne"))
-			return(int1!=int2);
-		if(EQ(p2, "-gt"))
-			return(int1>int2);
-		if(EQ(p2, "-lt"))
-			return(int1<int2);
-		if(EQ(p2, "-ge"))
-			return(int1>=int2);
-		if(EQ(p2, "-le"))
-			return(int1<=int2);
+	if(EQ(p2, "-older")){
+		b = nxtarg(0);
+		return(eval && isolder(b, a));
 	}
 
-	synbad("unknown operator ",p2);
-	return 0;		/* to shut ken up */
+	if(EQ(p2, "-ot")){
+		b = nxtarg(0);
+		return(eval && isolderthan(b, a));
+	}
+
+	if(EQ(p2, "-nt")){
+		b = nxtarg(0);
+		return(eval && isnewerthan(b, a));
+	}
+
+	if(isint(a, &int1)){
+		if(nxtintarg(&int2)){
+			if(EQ(p2, "-eq"))
+				return(int1==int2);
+			if(EQ(p2, "-ne"))
+				return(int1!=int2);
+			if(EQ(p2, "-gt"))
+				return(int1>int2);
+			if(EQ(p2, "-lt"))
+				return(int1<int2);
+			if(EQ(p2, "-ge"))
+				return(int1>=int2);
+			if(EQ(p2, "-le"))
+				return(int1<=int2);
+			ap--;
+		}
+	}
+	ap--;
+	return !EQ(a, "");
 }
 
 int


  parent reply	other threads:[~2020-08-03 13:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-01 16:14 Alex Musolino
2020-08-01 16:38 ` [9front] " Alex Musolino
2020-08-02  4:35 ` ori
2020-08-02  4:39   ` Alex Musolino
2020-08-02  4:51     ` ori
2020-08-03 13:45     ` Alex Musolino [this message]
2020-10-31 14:10       ` cinap_lenrek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2D6A76C7C50986C7669481E6D1A7D780@musolino.id.au \
    --to=alex@musolino.id.au \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).