From owner-freebsd-bugs@FreeBSD.ORG Sat Apr 4 22:30:03 2009 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D59601065673 for ; Sat, 4 Apr 2009 22:30:03 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id C28D58FC12 for ; Sat, 4 Apr 2009 22:30:03 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.3/8.14.3) with ESMTP id n34MU3Mk009177 for ; Sat, 4 Apr 2009 22:30:03 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.3/8.14.3/Submit) id n34MU3mM009172; Sat, 4 Apr 2009 22:30:03 GMT (envelope-from gnats) Date: Sat, 4 Apr 2009 22:30:03 GMT Message-Id: <200904042230.n34MU3mM009172@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org From: Jilles Tjoelker Cc: Subject: Re: bin/130298: sh(1) does not handle negation correctly in complex commands X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Jilles Tjoelker List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Apr 2009 22:30:04 -0000 The following reply was made to PR bin/130298; it has been noted by GNATS. From: Jilles Tjoelker To: bug-followup@FreeBSD.org, mb@tns.cz, stefanf@freebsd.org Cc: Subject: Re: bin/130298: sh(1) does not handle negation correctly in complex commands Date: Sun, 5 Apr 2009 00:28:14 +0200 --C7zPtVaVf+AK4Oqc Content-Type: text/plain; charset=us-ascii Content-Disposition: inline This is a subtle bug in the parser. It keeps track of whether it should recognize keywords such as '!' and 'if' (checkkwd global), but it fails to enable this recognition again after && and ||. Hence, the '!' is not recognized by pipeline(). Then, command() does enable the recognition and picks up the '!'. So, cmd1 && ! cmd2 | cmd3 puts the negation on cmd2 instead of cmd2 | cmd3 and does not work properly. Setting checkkwd in pipeline() fixes the problem (patch attached). After I figured this out I noticed NetBSD had already done it, and in the same way. http://cvsweb.netbsd.org/bsdweb.cgi/src/bin/sh/parser.c.diff?r1=1.64&r2=1.65&f=h If the attachment is mangled, try http://www.stack.nl/~jilles/unix/sh-parser-and-pipe-not.patch -- Jilles Tjoelker --C7zPtVaVf+AK4Oqc Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="sh-parser-and-pipe-not.patch.txt" --- src/bin/sh/parser.c.orig 2008-09-04 19:34:53.000000000 +0200 +++ src/bin/sh/parser.c 2009-04-05 00:02:51.000000000 +0200 @@ -250,6 +250,7 @@ pipeline(void) int negate; negate = 0; + checkkwd = 2; TRACE(("pipeline: entered\n")); while (readtoken() == TNOT) negate = !negate; --C7zPtVaVf+AK4Oqc--