From rea-fbsd@codelabs.ru  Sat Sep 22 18:15:52 2007
Return-Path: <rea-fbsd@codelabs.ru>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id E1A8C16A417
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 22 Sep 2007 18:15:52 +0000 (UTC)
	(envelope-from rea-fbsd@codelabs.ru)
Received: from pobox.codelabs.ru (pobox.codelabs.ru [144.206.177.45])
	by mx1.freebsd.org (Postfix) with ESMTP id 99A9E13C45D
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 22 Sep 2007 18:15:52 +0000 (UTC)
	(envelope-from rea-fbsd@codelabs.ru)
Received: from void.codelabs.ru (void.codelabs.ru [144.206.177.25])
	by pobox.codelabs.ru with esmtps (TLSv1:CAMELLIA256-SHA:256)
	id 1IZ9WB-0009Zd-AG for FreeBSD-gnats-submit@freebsd.org; Sat, 22 Sep 2007 22:15:51 +0400
Message-Id: <20070922181550.019AE1AF41C@void.codelabs.ru>
Date: Sat, 22 Sep 2007 22:15:49 +0400 (MSD)
From: Eygene Ryabinkin <rea-fbsd@codelabs.ru>
Reply-To: Eygene Ryabinkin <rea-fbsd@codelabs.ru>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: make case statement of Bourne shell IEEE 1003.2-conformant
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         116559
>Category:       bin
>Synopsis:       make case statement of Bourne shell IEEE 1003.2-conformant
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    stefanf
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Sep 22 18:20:02 GMT 2007
>Closed-Date:    Fri Oct 26 10:26:37 UTC 2007
>Last-Modified:  Fri Oct 26 10:26:37 UTC 2007
>Originator:     Eygene Ryabinkin
>Release:        FreeBSD 7.0-CURRENT i386
>Organization:
Code Labs
>Environment:

System: FreeBSD XXX 7.0-CURRENT FreeBSD 7.0-CURRENT #10: Wed Sep 12 16:16:49 MSD 2007 root@XXX:/usr/src/sys/i386/compile/XXX i386

>Description:

IEEE 1003.2 specifies that the return value of the 'case' construct
that did not matched any patterns shall be zero.  It is not the
case for the current FreeBSD /bin/sh.

>How-To-Repeat:

Run the test script that is embedded into the patch comments.

>Fix:

The following patch will correct and document the expected behaviour.

--- sh.patch begins here ---
According to the standard (http://opengroup.org/onlinepubs/000095399,
Shell and Utilities Volume (XCU), Case Conditional Construct), the
'case' statement shall return zero if no patterns were matched.

Currently, Bourne shell in FreeBSD does not change the exit code
on the 'case' construct that had not matched anything; it is
completely transparent in this regard.

The following test script shall not produce any messages with the
1003.2-conformant shells and its return code shall be zero:
-----
err () {
	echo "ERROR:    $@"
}

case_and_immediate () {
	false
	case Ultra in
	Super)
		false
		;;
	Hyper)
		true
		;;
	esac && echo ok
}

case_while_immediate () {
	false
	while case Ultra in Super) break ;; esac
	do
		echo ok
		break
	done
}

retval=0

for test in \
	case_and_immediate \
	case_while_immediate
do
	result=`$test`
	if test "$result" != ok; then
		err "$test" failed
		retval=$(($retval + 1))
	fi
done

exit $retval
-----

The real rationale for this change is that some utilities are
using constructs like
-----
while case "#?" in 0) break ;; esac
do
	case "$1" in
	--someflag)
		...
		;;
	...
	esac
done
-----
for the argument parsing.  The example I had before my eyes is
Git 1.5.3.2, script named git-commit.

Signed-off-by: Eygene Ryabinkin <rea-fbsd@codelabs.ru>
---
 eval.c |    1 +
 sh.1   |    4 ++++
 2 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/eval.c b/eval.c
index 9b41f63..6d24df2 100644
--- a/eval.c
+++ b/eval.c
@@ -367,6 +367,7 @@ evalcase(union node *n, int flags)
 	setstackmark(&smark);
 	arglist.lastp = &arglist.list;
 	oexitstatus = exitstatus;
+	exitstatus = 0;
 	expandarg(n->ncase.expr, &arglist, EXP_TILDE);
 	for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
 		for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
diff --git a/sh.1 b/sh.1
index 72aa5c5..41779fe 100644
--- a/sh.1
+++ b/sh.1
@@ -861,6 +861,10 @@ described later),
 separated by
 .Dq Li \&|
 characters.
+The exit code of the
+.Ic case
+command is the exit code of the last command executed in the list
+or zero if no patterns were matched.
 .Ss Grouping Commands Together
 Commands may be grouped by writing either
 .Bd -literal -offset indent
--- sh.patch ends here ---
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->stefanf 
Responsible-Changed-By: stefanf 
Responsible-Changed-When: Sat Sep 29 06:51:00 UTC 2007 
Responsible-Changed-Why:  
Grab. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=116559 
State-Changed-From-To: open->patched 
State-Changed-By: stefanf 
State-Changed-When: Thu Oct 4 16:17:23 UTC 2007 
State-Changed-Why:  
Fixed in current. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=116559 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/116559: commit references a PR
Date: Thu,  4 Oct 2007 16:15:01 +0000 (UTC)

 stefanf     2007-10-04 16:14:49 UTC
 
   FreeBSD src repository
 
   Modified files:
     bin/sh               eval.c sh.1 
   Added files:
     tools/regression/bin/sh/builtins case1.0 
   Log:
   The exit status of a case statement where none of the patterns is matched
   is supposed to be 0, not the status of the previous command.
   
   Reported by:    Eygene Ryabinkin
   PR:             116559
   Approved by:    re (gnn)
   
   Revision  Changes    Path
   1.54      +1 -0      src/bin/sh/eval.c
   1.125     +4 -0      src/bin/sh/sh.1
   1.1       +13 -0     src/tools/regression/bin/sh/builtins/case1.0 (new)
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: stefanf 
State-Changed-When: Fri Oct 26 10:26:14 UTC 2007 
State-Changed-Why:  
Also merged to RELENG_6.  Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=116559 
>Unformatted:
