From e78648891926d9efb9e06fed253120c6ec2e6bb8 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Mon, 3 Nov 2003 13:59:07 +0000 Subject: 19216: save and restore $pipestatus. 19218: extend 19216 to zle widgets --- Src/Zle/zle_main.c | 6 ++---- Src/exec.c | 36 +++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 11 deletions(-) (limited to 'Src') diff --git a/Src/Zle/zle_main.c b/Src/Zle/zle_main.c index 6156b40d0..c5225ccdb 100644 --- a/Src/Zle/zle_main.c +++ b/Src/Zle/zle_main.c @@ -684,7 +684,7 @@ execzlefunc(Thingy func, char **args) zsfree(msg); ret = 1; } else { - int osc = sfcontext, osi = movefd(0), olv = lastval; + int osc = sfcontext, osi = movefd(0); LinkList largs = NULL; if (*args) { @@ -696,9 +696,7 @@ execzlefunc(Thingy func, char **args) startparamscope(); makezleparams(0); sfcontext = SFC_WIDGET; - doshfunc(w->u.fnnam, prog, largs, 0, 0); - ret = lastval; - lastval = olv; + ret = doshfunc(w->u.fnnam, prog, largs, 0, 1); sfcontext = osc; endparamscope(); lastcmd = 0; diff --git a/Src/exec.c b/Src/exec.c index 81c6ac641..77b657c18 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -3310,17 +3310,23 @@ loadautofn(Shfunc shf, int fksh, int autol) return shf; } -/* execute a shell function */ +/* + * execute a shell function + * + * If noreturnval is nonzero, then reset the current return + * value (lastval) to its value before the shell function + * was executed. However, in any case return the status value + * from the function (i.e. if noreturnval is not set, this + * will be the same as lastval). + */ /**/ -mod_export void +mod_export int doshfunc(char *name, Eprog prog, LinkList doshargs, int flags, int noreturnval) -/* If noreturnval is nonzero, then reset the current return * - * value (lastval) to its value before the shell function * - * was executed. */ { char **tab, **x, *oargv0; - int oldzoptind, oldlastval, oldoptcind; + int oldzoptind, oldlastval, oldoptcind, oldnumpipestats, ret; + int *oldpipestats = NULL; char saveopts[OPT_SIZE], *oldscriptname = scriptname, *fname = dupstring(name); int obreaks; struct funcstack fstack; @@ -3335,6 +3341,16 @@ doshfunc(char *name, Eprog prog, LinkList doshargs, int flags, int noreturnval) if (trapreturn < 0) trapreturn--; oldlastval = lastval; + oldnumpipestats = numpipestats; + if (noreturnval) { + /* + * Easiest to use the heap here since we're bracketed + * immediately by a pushheap/popheap pair. + */ + size_t bytes = sizeof(int)*numpipestats; + oldpipestats = (int *)zhalloc(bytes); + memcpy(oldpipestats, pipestats, bytes); + } starttrapscope(); @@ -3440,9 +3456,15 @@ doshfunc(char *name, Eprog prog, LinkList doshargs, int flags, int noreturnval) if (trapreturn < -1) trapreturn++; - if (noreturnval) + ret = lastval; + if (noreturnval) { lastval = oldlastval; + numpipestats = oldnumpipestats; + memcpy(pipestats, oldpipestats, sizeof(int)*numpipestats); + } popheap(); + + return ret; } /* This finally executes a shell function and any function wrappers * -- cgit 1.4.1