From itfs!info!nnd%itfs.nsk.su@gw.itfs.nsk.su  Sat Dec  9 09:41:07 1995
Received: from gw.itfs.nsk.su (gw.itfs.nsk.su [193.124.36.33])
          by freefall.freebsd.org (8.7.3/8.7.3) with SMTP id JAA04143
          for <FreeBSD-gnats-submit@freebsd.org>; Sat, 9 Dec 1995 09:40:35 -0800 (PST)
Received: from itfs.UUCP (uucp@localhost) by gw.itfs.nsk.su (8.6.12/8.6.12) with UUCP id XAA14879 for FreeBSD-gnats-submit@freebsd.org; Sat, 9 Dec 1995 23:40:07 +0600
Received: by itfs.nsk.su; Sat,  9 Dec 95 23:32:56 +0600 (NSK)
Received: (from root@localhost) by info.itfs.nsk.su (8.6.11/8.6.9) id XAA16125; Sat, 9 Dec 1995 23:28:06 +0600
Message-Id: <199512091728.XAA16125@info.itfs.nsk.su>
Date: Sat, 9 Dec 1995 23:28:06 +0600
From: nnd@itfs.nsk.su
Reply-To: nnd@itfs.nsk.su
To: FreeBSD-gnats-submit@freebsd.org
Subject: /bin/sh incorrectly parse command lists
X-Send-Pr-Version: 3.2

>Number:         880
>Category:       bin
>Synopsis:       Incorrect parsing of command lists by /bin/sh
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Dec  9 09:50:01 PST 1995
>Closed-Date:    Sun Dec 10 19:22:28 MET 1995
>Last-Modified:  Sun Dec 10 19:24:21 MET 1995
>Originator:     Nickolay N. Dudorov
>Release:        FreeBSD 2.0-BUILT-19950524 i386
>Organization:
Infoteka Ltd.
>Environment:

	FreeBSD versions from 1.1 to 2.2-current

>Description:

	/bin/sh (ash) incorrectly parse command lists if
	any (but not first) of list components marked as 'background',
	f.e construction

	{ a; b; c & d; e; }

	parsed as if it was

	{ { a; b; c; } & d; e; }

	All other shells which I can test parse this
	as

	{ a; b; { c& } ; d; e; }

	(including /bin/sh in NetBSD-current which is
	 originated from the same sources as FreeBSD one)

>How-To-Repeat:

	Try this one:

#!/bin/sh
while true
do
	date +'1:%H:%M:%S'
	sleep 2
	exit
	ps jx &
	date +'2:%H:%M:%S'
	sleep 3
	exit
done

	(while ... do ... done is here to force using
	command-list )

>Fix:
	
	Applay the patch.
(Here is the result of
diff -u /usr/src/bin/sh/parse.c.ORG /usr/src/bin/sh/parse.c)

--- /usr/src/bin/sh/parser.c.ORG	Wed Aug 30 15:44:33 1995
+++ /usr/src/bin/sh/parser.c	Tue Dec  5 18:56:28 1995
@@ -149,30 +149,13 @@
 	n1 = andor();
 	for (;;) {
 		switch (readtoken()) {
-		case TBACKGND:
-			if (n1->type == NCMD || n1->type == NPIPE) {
-				n1->ncmd.backgnd = 1;
-			} else if (n1->type == NREDIR) {
-				n1->type = NBACKGND;
-			} else {
-				n3 = (union node *)stalloc(sizeof (struct nredir));
-				n3->type = NBACKGND;
-				n3->nredir.n = n1;
-				n3->nredir.redirect = NULL;
-				n1 = n3;
-			}
-			goto tsemi;
 		case TNL:
-			tokpushback++;
+			parseheredoc();
+			if (nlflag)
+				return n1;
 			/* fall through */
-tsemi:	    case TSEMI:
-			if (readtoken() == TNL) {
-				parseheredoc();
-				if (nlflag)
-					return n1;
-			} else {
-				tokpushback++;
-			}
+		case TBACKGND:
+		case TSEMI:
 			checkkwd = 2;
 			if (tokendlist[peektoken()])
 				return n1;
@@ -212,6 +195,19 @@
 		} else if (t == TOR) {
 			t = NOR;
 		} else {
+			if (t == TBACKGND) {
+				if (n1->type == NCMD || n1->type == NPIPE) {
+					n1->ncmd.backgnd = 1;
+				} else if (n1->type == NREDIR) {
+					n1->type = NBACKGND;
+				} else {
+					n3 = (union node *)stalloc(sizeof (struct nredir));
+					n3->type = NBACKGND;
+					n3->nredir.n = n1;
+					n3->nredir.redirect = NULL;
+					n1 = n3;
+				}
+			}
 			tokpushback++;
 			return n1;
 		}
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: joerg 
State-Changed-When: Sun Dec 10 19:22:28 MET 1995 
State-Changed-Why:  
Suggested fix applied in rev 1.10 of sh/parser.c. 

This does also fix an earlier (non-GNATS) bug report by Ben Jackson 
under the subject ``bug in /bin/sh for loops''. 
>Unformatted:
