From daa7d99702b8a3580b71ee74f962719ad1d052c8 Mon Sep 17 00:00:00 2001
From: Peter Stephenson
Date: Fri, 13 Feb 2015 21:31:31 +0000
Subject: 34546: further $_ with anon function fix.
Also add tests.
---
ChangeLog | 5 +++++
Src/exec.c | 17 +++++++++++++----
Test/D04parameter.ztst | 29 +++++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 4 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 2660e4f22..641dfa75a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2015-02-13 Peter Stephenson
+
+ * 34546: Src/exec.c, Test/D04parameter.ztst: fix another
+ oddity with $_ and anonymous functions and add tests.
+
2015-02-12 Peter Stephenson
* 34519: Src/exec.c: handle $_ with arguments to anonymous
diff --git a/Src/exec.c b/Src/exec.c
index 302e2b510..de6b9c574 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -2784,9 +2784,14 @@ execcmd(Estate state, int input, int output, int how, int last1)
else
text = NULL;
- /* Set up special parameter $_ */
-
- setunderscore((args && nonempty(args)) ? ((char *) getdata(lastnode(args))) : "");
+ /*
+ * Set up special parameter $_
+ * For execfuncdef we may need to take account of an
+ * anonymous function with arguments.
+ */
+ if (type != WC_FUNCDEF)
+ setunderscore((args && nonempty(args)) ?
+ ((char *) getdata(lastnode(args))) : "");
/* Warn about "rm *" */
if (type == WC_SIMPLE && interact && unset(RMSTARSILENT) &&
@@ -4374,7 +4379,7 @@ execfuncdef(Estate state, Eprog redir_prog)
Shfunc shf;
char *s = NULL;
int signum, nprg, sbeg, nstrs, npats, len, plen, i, htok = 0, ret = 0;
- int nfunc = 0;
+ int nfunc = 0, anon_func = 0;
Wordcode beg = state->pc, end;
Eprog prog;
Patprog *pp;
@@ -4460,6 +4465,8 @@ execfuncdef(Estate state, Eprog redir_prog)
*/
LinkList args;
+ anon_func = 1;
+
state->pc = end;
end += *state->pc++;
args = ecgetlist(state, *state->pc++, EC_DUPTOK, &htok);
@@ -4515,6 +4522,8 @@ execfuncdef(Estate state, Eprog redir_prog)
shfunctab->addnode(shfunctab, ztrdup(s), shf);
}
}
+ if (!anon_func)
+ setunderscore("");
if (!nfunc && redir_prog) {
/* For completeness, shouldn't happen */
freeeprog(redir_prog);
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index cf639fa8c..42c7b4ec6 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1670,3 +1670,32 @@
print ${(e)param}
0:Alias expansion in command substitution in parameter evaluation
>Expanded in substitution
+
+ a=1 b=2 c=3
+ : One;
+ function {
+ : Two
+ echo $_
+ print -l $argv
+ } $_ Three
+ print -l $_ Four;
+0:$_ with anonymous function
+>Two
+>One
+>Three
+>Three
+>Four
+
+ a=1 b=2 c=3
+ : One
+ function {
+ : Two
+ echo $_
+ print -l $argv
+ }
+ print -l "$_" Four
+0:$_ with anonymous function without arguments
+>Two
+>
+>
+>Four
--
cgit 1.4.1