summaryrefslogtreecommitdiff
path: root/Src/exec.c
diff options
context:
space:
mode:
authorBarton E. Schaefer <schaefer@zsh.org>2014-09-29 17:52:33 -0700
committerBarton E. Schaefer <schaefer@zsh.org>2014-09-29 17:52:33 -0700
commit32473f59a38631c4bb2403d835b9e007c8a0ddc7 (patch)
tree72df8959fcde37e5968a6c6301f8a98a9f860e5f /Src/exec.c
parent60e31a45b0c015d42e9dc8ca2c991930c3f7fd66 (diff)
parent8cb67e721b3f02779bf4cf0d6a368f67c49c11f8 (diff)
downloadzsh-32473f59a38631c4bb2403d835b9e007c8a0ddc7.tar.gz
zsh-32473f59a38631c4bb2403d835b9e007c8a0ddc7.zip
Merge branch 'master' of git://git.code.sf.net/p/zsh/code
Conflicts: ChangeLog
Diffstat (limited to 'Src/exec.c')
-rw-r--r--Src/exec.c92
1 files changed, 88 insertions, 4 deletions
diff --git a/Src/exec.c b/Src/exec.c
index fbd309580..10f71dab6 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -198,7 +198,8 @@ static char *blank_env[] = { NULL };
/* Execution functions. */
static int (*execfuncs[WC_COUNT-WC_CURSH]) _((Estate, int)) = {
- execcursh, exectime, execfuncdef, execfor, execselect,
+ execcursh, exectime, NULL /* execfuncdef handled specially */,
+ execfor, execselect,
execwhile, execrepeat, execcase, execif, execcond,
execarith, execautofn, exectry
};
@@ -1118,8 +1119,11 @@ execsimple(Estate state)
fflush(xtrerr);
}
lv = (errflag ? errflag : cmdoutval);
- } else
+ } else if (code == WC_FUNCDEF) {
+ lv = execfuncdef(state, NULL);
+ } else {
lv = (execfuncs[code - WC_CURSH])(state, 0);
+ }
thisjob = otj;
@@ -2792,6 +2796,42 @@ execcmd(Estate state, int input, int output, int how, int last1)
return;
}
+ if (type == WC_FUNCDEF) {
+ /*
+ * The first word of a function definition is a list of
+ * names. If this is empty, we're doing an anonymous function:
+ * in that case redirections are handled normally.
+ * If not, it's a function definition: then we don't do
+ * redirections here but pass in the list of redirections to
+ * be stored for recall with the function.
+ */
+ if (*state->pc != 0) {
+ /* Nonymous, don't do redirections here */
+ redir = NULL;
+ }
+ } else if (is_shfunc) {
+ Shfunc shf = (Shfunc)hn;
+ /*
+ * A function definition may have a list of additional
+ * redirections to apply, so retrieve it.
+ */
+ if (shf->redir) {
+ struct estate s;
+ LinkList redir2;
+
+ s.prog = shf->redir;
+ s.pc = shf->redir->prog;
+ s.strs = shf->redir->strs;
+ redir2 = ecgetredirs(&s);
+ if (!redir)
+ redir = redir2;
+ else {
+ while (nonempty(redir2))
+ addlinknode(redir, ugetnode(redir2));
+ }
+ }
+ }
+
if (type == WC_SIMPLE && !nullexec) {
char *s;
char trycd = (isset(AUTOCD) && isset(SHINSTDIN) &&
@@ -3240,7 +3280,33 @@ execcmd(Estate state, int input, int output, int how, int last1)
flags |= ESUB_REVERTPGRP;
entersubsh(flags);
}
- if (type >= WC_CURSH) {
+ if (type == WC_FUNCDEF) {
+ Eprog redir_prog;
+ if (!redir && wc_code(*beg) == WC_REDIR) {
+ /*
+ * We're not using a redirection from the currently
+ * parsed environment, which is what we'd do for an
+ * anonymous function, but there are redirections we
+ * should store with the new function.
+ */
+ struct estate s;
+
+ s.prog = state->prog;
+ s.pc = beg;
+ s.strs = state->prog->strs;
+
+ /*
+ * The copy uses the wordcode parsing area, so save and
+ * restore state.
+ */
+ lexsave();
+ redir_prog = eccopyredirs(&s);
+ lexrestore();
+ } else
+ redir_prog = NULL;
+
+ lastval = execfuncdef(state, redir_prog);
+ } else if (type >= WC_CURSH) {
if (last1 == 1)
do_exec = 1;
lastval = (execfuncs[type - WC_CURSH])(state, do_exec);
@@ -4237,11 +4303,12 @@ exectime(Estate state, UNUSED(int do_exec))
/**/
static int
-execfuncdef(Estate state, UNUSED(int do_exec))
+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;
Wordcode beg = state->pc, end;
Eprog prog;
Patprog *pp;
@@ -4266,6 +4333,8 @@ execfuncdef(Estate state, UNUSED(int do_exec))
}
}
+ DPUTS(!names && redir_prog,
+ "Passing redirection to anon function definition.");
while (!names || (s = (char *) ugetnode(names))) {
if (!names) {
prog = (Eprog) zhalloc(sizeof(*prog));
@@ -4307,6 +4376,15 @@ execfuncdef(Estate state, UNUSED(int do_exec))
shf->node.flags = 0;
shf->filename = ztrdup(scriptfilename);
shf->lineno = lineno;
+ /*
+ * redir_prog is permanently allocated --- but if
+ * this function has multiple names we need an additional
+ * one.
+ */
+ if (nfunc++ && redir_prog)
+ shf->redir = dupeprog(redir_prog, 0);
+ else
+ shf->redir = redir_prog;
shfunc_set_sticky(shf);
if (!names) {
@@ -4337,6 +4415,8 @@ execfuncdef(Estate state, UNUSED(int do_exec))
ret = lastval;
freeeprog(shf->funcdef);
+ if (shf->redir) /* shouldn't be */
+ freeeprog(shf->redir);
zsfree(shf->filename);
zfree(shf, sizeof(*shf));
break;
@@ -4360,6 +4440,10 @@ execfuncdef(Estate state, UNUSED(int do_exec))
shfunctab->addnode(shfunctab, ztrdup(s), shf);
}
}
+ if (!nfunc && redir_prog) {
+ /* For completeness, shouldn't happen */
+ freeeprog(redir_prog);
+ }
state->pc = end;
return ret;
}