Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: minor 'select' snag



On Mon, 15 Feb 2016 00:05:06 +0100
Martijn Dekker <martijn@xxxxxxxx> wrote:
> I'm analysing the behaviour of 'select' in various shells and found a a
> way in which it's different in zsh from bash, ksh93 and {pd,m}ksh.
> 
> If a user presses Ctrl-D (EOF) within a 'select' loop, the REPLY
> variable is left unchanged on zsh. On the other shells with 'select', it
> is cleared, which is the same behaviour as 'read' (including 'read' on
> zsh) and seems more logical. This makes it possible to decide whether to
> continue after the loop by testing for the emptiness of $REPLY without
> having to initialise it before entering the loop. It would be nice if
> this worked the same way on zsh.

This looks easy.  I don't think there's any existing moral right to
expect REPLY to be unaffected in such circumstances.

diff --git a/Doc/Zsh/grammar.yo b/Doc/Zsh/grammar.yo
index 2a76964..7488829 100644
--- a/Doc/Zsh/grammar.yo
+++ b/Doc/Zsh/grammar.yo
@@ -279,8 +279,10 @@ is set to the var(word) corresponding to this number.
 If this line is empty, the selection list is printed again.
 Otherwise, the value of the parameter var(name) is set to null.
 The contents of the line read from standard input is saved
-in the parameter tt(REPLY).  var(list) is executed
-for each selection until a break or end-of-file is encountered.
+in the parameter tt(REPLY); this is set to the empty string if no
+input was done for the benefit of code following the tt(select).
+var(list) is executed for each selection until a break or end-of-file is
+encountered.
 )
 cindex(subshell)
 item(tt(LPAR()) var(list) tt(RPAR()))(
diff --git a/Src/loop.c b/Src/loop.c
index 19d7f73..dd4b282 100644
--- a/Src/loop.c
+++ b/Src/loop.c
@@ -224,6 +224,7 @@ execselect(Estate state, UNUSED(int do_exec))
     size_t more;
     LinkList args;
     int old_simple_pline = simple_pline;
+    int reply_set = 0;
 
     /* See comments in execwhile() */
     simple_pline = 1;
@@ -303,6 +304,7 @@ execselect(Estate state, UNUSED(int do_exec))
 	    more = selectlist(args, more);
 	}
 	setsparam("REPLY", ztrdup(str));
+	reply_set = 1;
 	i = atoi(str);
 	if (!i)
 	    str = "";
@@ -327,6 +329,8 @@ execselect(Estate state, UNUSED(int do_exec))
 	    break;
     }
   done:
+    if (!reply_set)
+	setsparam("REPLY", ztrdup(""));
     cmdpop();
     popheap();
     fclose(inp);



Messages sorted by: Reverse Date, Date, Thread, Author