Linux-Dash Archive mirror
 help / color / mirror / Atom feed
From: Herbert Xu <herbert@gondor.apana.org.au>
To: dash@vger.kernel.org
Subject: [BUILTIN] Return without arguments in a trap should use status outside traps
Date: Mon, 6 Oct 2014 21:53:35 +0800	[thread overview]
Message-ID: <20141006135335.GA23072@gondor.apana.org.au> (raw)

commit 70c16dd30d4cf824aa895e9f6c095fec741c65a8
Author: Herbert Xu <herbert@gondor.apana.org.au>
Date:   Mon Oct 6 21:51:26 2014 +0800

    [BUILTIN] Return without arguments in a trap should use status outside traps
    
    POSIX now requires that return without arguments in a trap should
    return the last command status prior to executing traps.  This
    patch implements this behaviour.
    
    Incidentally this also changes the behaviour of return without
    arguments in a loop conditional to use the last exit status in
    the body as opposed to the last command in the conditional when
    there is one.
    
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/ChangeLog b/ChangeLog
index 297b81a..aa230ab 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,7 @@
 	* Do not allow break to break across function calls.
 	* Move common skipcount logic into skiploop.
 	* Allow return in loop conditional to set exit status.
+	* Return without arguments in a trap should use status outside traps.
 
 2014-10-03  Herbert Xu <herbert@gondor.apana.org.au>
 
diff --git a/src/eval.c b/src/eval.c
index 7b341f3..071fb1b 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -967,7 +967,7 @@ funcdone:
 	shellparam = saveparam;
 	handler = savehandler;
 	INTON;
-	evalskip &= ~SKIPFUNC;
+	evalskip &= ~(SKIPFUNC | SKIPFUNCDEF);
 	return e;
 }
 
@@ -1047,12 +1047,23 @@ breakcmd(int argc, char **argv)
 int
 returncmd(int argc, char **argv)
 {
+	int skip;
+	int status;
+
 	/*
 	 * If called outside a function, do what ksh does;
 	 * skip the rest of the file.
 	 */
-	evalskip = SKIPFUNC;
-	return argv[1] ? number(argv[1]) : exitstatus;
+	if (argv[1]) {
+		skip = SKIPFUNC;
+		status = number(argv[1]);
+	} else {
+		skip = SKIPFUNCDEF;
+		status = exitstatus;
+	}
+	evalskip = skip;
+
+	return status;
 }
 
 
diff --git a/src/eval.h b/src/eval.h
index 6e62137..6e8acda 100644
--- a/src/eval.h
+++ b/src/eval.h
@@ -62,3 +62,4 @@ extern int evalskip;
 #define SKIPBREAK	(1 << 0)
 #define SKIPCONT	(1 << 1)
 #define SKIPFUNC	(1 << 2)
+#define SKIPFUNCDEF	(1 << 3)
diff --git a/src/main.c b/src/main.c
index 29a258d..00c5e00 100644
--- a/src/main.c
+++ b/src/main.c
@@ -242,7 +242,7 @@ cmdloop(int top)
 
 		skip = evalskip;
 		if (skip) {
-			evalskip &= ~SKIPFUNC;
+			evalskip &= ~(SKIPFUNC | SKIPFUNCDEF);
 			break;
 		}
 	}
diff --git a/src/trap.c b/src/trap.c
index 15faeff..b924661 100644
--- a/src/trap.c
+++ b/src/trap.c
@@ -343,7 +343,8 @@ void dotrap(void)
 		if (!p)
 			continue;
 		evalstring(p, 0);
-		exitstatus = status;
+		if (evalskip != SKIPFUNC)
+			exitstatus = status;
 	}
 
 	savestatus = last_status;

Cheers,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

                 reply	other threads:[~2014-10-06 13:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20141006135335.GA23072@gondor.apana.org.au \
    --to=herbert@gondor.apana.org.au \
    --cc=dash@vger.kernel.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 read-only IMAP folder(s) and NNTP newsgroup(s).