Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: bug: $? after empty command
On Fri, 10 Jul 2009 23:05:54 +0100
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Fri, 10 Jul 2009 10:02:28 +0100
> Peter Stephenson <pws@xxxxxxx> wrote:
> > On Thu, 9 Jul 2009 21:41:51 +0000 (UTC)
> > Eric Blake <ebb9@xxxxxxx> wrote:
> >> Eric Blake <ebb9 <at> byu.net> writes:
> >>> $ zsh -c 'false; . /dev/null; echo $?'
> >>> 1
> >>> $ zsh -c 'false; ``; echo $?'
> >>> 1
> >> $ zsh -c 'false; sleep& echo $?'
> >> 1
> >
> > "fn() { }" is valid, and I suspect "fn"
> > logically ought to reset the status, too
>
> These four were all fairly easy; in each case there is a local context
> where the command in question is being executed in which the status can
> be reset. There may be a a more generic place, such as execlist(), for
> these to go, but I like the transparency of resetting them locally;
> execlist()/execode() are used in just too many places.
Change of plan: I woke up in the night and realised that doing it this
way breaks the case
fn() { print $?; }
false
fn
which is a bit strange but perfectly valid and should print "1".
> However, that's a bit craven.
This takes the brave option, but actually it's easy except for the case
of "." which needs a bit more information from down below. For both
executing a list and sourcing a file line by line we only reset the
status if there is no code to execute.
The case of starting a background function isn't affected, in that case
the foreground code is always empty.
Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.167
diff -u -r1.167 exec.c
--- Src/exec.c 10 Jul 2009 22:10:26 -0000 1.167
+++ Src/exec.c 11 Jul 2009 16:32:42 -0000
@@ -1056,6 +1056,10 @@
/* Loop over all sets of comands separated by newline, *
* semi-colon or ampersand (`sublists'). */
code = *state->pc++;
+ if (wc_code(code) != WC_LIST) {
+ /* Empty list; this returns status zero. */
+ lastval = 0;
+ }
while (wc_code(code) == WC_LIST && !breaks && !retflag && !errflag) {
int donedebug;
@@ -3513,7 +3517,6 @@
return retval;
}
/* pid == 0 */
- lastval = 0; /* status of empty list is zero */
child_unblock();
zclose(pipes[0]);
redup(pipes[1], 1);
@@ -4261,7 +4264,6 @@
if (trap_state == TRAP_STATE_PRIMED)
trap_return--;
oldlastval = lastval;
- lastval = 0; /* status of empty function is zero */
oldnumpipestats = numpipestats;
if (noreturnval) {
/*
Index: Src/init.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/init.c,v
retrieving revision 1.104
diff -u -r1.104 init.c
--- Src/init.c 10 Jul 2009 22:10:26 -0000 1.104
+++ Src/init.c 11 Jul 2009 16:32:42 -0000
@@ -99,11 +99,11 @@
/* keep executing lists until EOF found */
/**/
-int
+enum loop_return
loop(int toplevel, int justonce)
{
Eprog prog;
- int err;
+ int err, non_empty = 0;
pushheap();
if (!toplevel)
@@ -151,6 +151,7 @@
if (hend(prog)) {
int toksav = tok;
+ non_empty = 1;
if (toplevel &&
(getshfunc("preexec") ||
paramtab->getnode(paramtab, "preexec" HOOK_SUFFIX))) {
@@ -207,7 +208,11 @@
lexrestore();
popheap();
- return err;
+ if (err)
+ return LOOP_ERROR;
+ if (!non_empty)
+ return LOOP_EMPTY;
+ return LOOP_OK;
}
static char *cmd;
@@ -1131,7 +1136,6 @@
fstack.tp = FS_SOURCE;
funcstack = &fstack;
- lastval = 0; /* status of empty file is zero */
if (prog) {
pushheap();
errflag = 0;
@@ -1141,8 +1145,21 @@
ret = SOURCE_ERROR;
} else {
/* loop through the file to be sourced */
- if (loop(0, 0))
+ switch (loop(0, 0))
+ {
+ case LOOP_OK:
+ /* nothing to do but compilers like a complete enum */
+ break;
+
+ case LOOP_EMPTY:
+ /* Empty code resets status */
+ lastval = 0;
+ break;
+
+ case LOOP_ERROR:
ret = SOURCE_ERROR;
+ break;
+ }
}
funcstack = funcstack->prev;
sourcelevel--;
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.159
diff -u -r1.159 zsh.h
--- Src/zsh.h 10 Jul 2009 11:08:48 -0000 1.159
+++ Src/zsh.h 11 Jul 2009 16:32:43 -0000
@@ -1726,6 +1726,17 @@
#define PRINT_WHENCE_FUNCDEF (1<<9)
#define PRINT_WHENCE_WORD (1<<10)
+/* Return values from loop() */
+
+enum loop_return {
+ /* Loop executed OK */
+ LOOP_OK,
+ /* Loop executed no code */
+ LOOP_EMPTY,
+ /* Loop encountered an error */
+ LOOP_ERROR
+};
+
/* Return values from source() */
enum source_return {
Index: Test/A01grammar.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A01grammar.ztst,v
retrieving revision 1.24
diff -u -r1.24 A01grammar.ztst
--- Test/A01grammar.ztst 10 Jul 2009 22:10:27 -0000 1.24
+++ Test/A01grammar.ztst 11 Jul 2009 16:32:43 -0000
@@ -553,3 +553,9 @@
0:Last status of successfully executed "." file is retained
>1
>0
+
+ echo 'echo $?' >dot_status
+ false
+ . ./dot_status
+0:"." file sees status from previous command
+>1
Index: Test/C04funcdef.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/C04funcdef.ztst,v
retrieving revision 1.6
diff -u -r1.6 C04funcdef.ztst
--- Test/C04funcdef.ztst 10 Jul 2009 22:10:27 -0000 1.6
+++ Test/C04funcdef.ztst 11 Jul 2009 16:32:43 -0000
@@ -15,6 +15,13 @@
fnz
0:Empty function body resets status
+ fn3() { return 3; }
+ fnstat() { print $?; }
+ fn3
+ fnstat
+0:Status is not reset on non-empty function body
+>3
+
function f$$ () {
print regress expansion of function names
}
Index: Test/D08cmdsubst.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D08cmdsubst.ztst,v
retrieving revision 1.3
diff -u -r1.3 D08cmdsubst.ztst
--- Test/D08cmdsubst.ztst 10 Jul 2009 22:10:27 -0000 1.3
+++ Test/D08cmdsubst.ztst 11 Jul 2009 16:32:43 -0000
@@ -93,3 +93,8 @@
false
``
0:Empty command substitution resets status
+
+ false
+ echo `echo $?`
+0:Non-empty command substitution inherits status
+>1
--
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
Messages sorted by:
Reverse Date,
Date,
Thread,
Author