From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12054 invoked by alias); 30 Sep 2014 11:53:09 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 33294 Received: (qmail 11451 invoked from network); 30 Sep 2014 11:52:55 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_PASS autolearn=ham version=3.3.2 X-AuditID: cbfec7f5-b7f776d000003e54-85-542a99925dc5 Date: Tue, 30 Sep 2014 12:52:49 +0100 From: Peter Stephenson To: Zsh hackers list Subject: Re: PATCH: functions with redirections Message-id: <20140930125249.0235af2a@pwslap01u.europe.root.pri> In-reply-to: <20140930094813.22965438@pwslap01u.europe.root.pri> References: <20140929205236.2eb5e622@pws-pc.ntlworld.com> <140929180257.ZM7637@torch.brasslantern.com> <20140930094813.22965438@pwslap01u.europe.root.pri> Organization: Samsung Cambridge Solution Centre X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFuphluLIzCtJLcpLzFFi42I5/e/4Nd1JM7VCDHp3cVkcbH7I5MDoserg B6YAxigum5TUnMyy1CJ9uwSujJ0rN7MX3FOtWL67ibWBcbpMFyMnh4SAicS3X52MELaYxIV7 69m6GLk4hASWMkpcX3qDHcJZziQxufc/UBUHB4uAqsS/n9wgDWwChhJTN80GaxYR0JLYcfIk E4gtLKAv8WfjSbByXgF7ibaZUiBhTgEHiWVTXzJCjHzKKNHzexZYPT9Q/dW/n5ggjrCXmHnl DNhMXgFBiR+T77GA2MxA8zdva2KFsOUlNq95yzyBUWAWkrJZSMpmISlbwMi8ilE0tTS5oDgp PddIrzgxt7g0L10vOT93EyMkBL/uYFx6zOoQowAHoxIPb4KiVogQa2JZcWXuIUYJDmYlEd6z /UAh3pTEyqrUovz4otKc1OJDjEwcnFINjGlLTq5cEikc8GnNpsvL7d4fmFhhfqM0+OKhTtvX 9pNv7s50lpiyZsGcuOcJt/8Fz+N0f/tinYb/gu6CWQFKOx9sVtp88dkR7pg9TLEPGKNOhXVO PmQUmv172bOdjGmzljNe/OUsu7lgjfEHe9du7iQZ/8blz1lqbhieOZd/VFc+webN1cS9YaFK LMUZiYZazEXFiQAiS4maHwIAAA== On Tue, 30 Sep 2014 09:48:13 +0100 Peter Stephenson wrote: > On Mon, 29 Sep 2014 18:02:57 -0700 > Bart Schaefer wrote: > > On Sep 29, 1:53pm, Bart Schaefer wrote: > > } > > } foo() { echo foo; } >&3 > > } foo 3>&2 > > > > Tried this now; happily it works the same as bash. > > It works by appending the list from the function definition to the list > from the command line, so the effect is "3>&2 >&3" as you expect. > Should probably add a test for it --- it's slightly different code from > the normal case. There's one here. > $functions[foo] output will need a special case. I'll look at it. This is straightforward, except the code is messily duplicated in two places and both have an unmetafy() I'm extremely dubious about --- surely as the string is internal to a parameter value it should still be metafied? If something's not unmetafying it that's a bug elsewhere. I'm also a bit dubious about the way output for functions with the EF_RUN flag is handled --- the original code rather than the additions. I think this is to do with ksh-style autoloads. diff --git a/Src/Modules/parameter.c b/Src/Modules/parameter.c index 0385a70..55157a9 100644 --- a/Src/Modules/parameter.c +++ b/Src/Modules/parameter.c @@ -392,20 +392,36 @@ getfunction(UNUSED(HashTable ht), const char *name, int dis) ((shf->node.flags & PM_TAGGED) ? "t" : ""))); } else { char *t = getpermtext(shf->funcdef, NULL, 1), *n, *h; + char *start; + + if (shf->redir) + start = "{\n\t"; + else + start = "\t"; if (shf->funcdef->flags & EF_RUN) { n = nicedupstring(name); - h = (char *) zhalloc(strlen(t) + strlen(n) + 9); - h[0] = '\t'; - strcpy(h + 1, t); + h = (char *) zhalloc(strlen(start) + strlen(t) + strlen(n) + 8); + strcpy(h, start); + strcat(h, t); strcat(h, "\n\t"); strcat(h, n); strcat(h, " \"$@\""); } else - h = dyncat("\t", t); + h = dyncat(start, t); zsfree(t); + /* + * TBD: Is this unmetafy correct? Surely as this + * is a parameter value it stays metafied? + */ unmetafy(h, NULL); + if (shf->redir) { + t = getpermtext(shf->redir, NULL, 1); + h = zhtricat(h, "\n}", t); + zsfree(t); + } + pm->u.str = h; } } else { @@ -456,21 +472,38 @@ scanfunctions(UNUSED(HashTable ht), ScanFunc func, int flags, int dis) ((shf->node.flags & PM_TAGGED) ? "Ut" : "U") : ((shf->node.flags & PM_TAGGED) ? "t" : ""))); } else { - char *t = getpermtext(((Shfunc) hn)->funcdef, NULL, 1); - char *n; + Shfunc shf = (Shfunc)hn; + char *t = getpermtext(shf->funcdef, NULL, 1); + char *n, *start; - if (((Shfunc) hn)->funcdef->flags & EF_RUN) { + if (shf->redir) + start = "{\n\t"; + else + start = "\t"; + + if (shf->funcdef->flags & EF_RUN) { n = nicedupstring(hn->nam); - pm.u.str = (char *) zhalloc(strlen(t) + strlen(n) + 9); - pm.u.str[0] = '\t'; - strcpy(pm.u.str + 1, t); + pm.u.str = (char *) zhalloc( + strlen(start) + strlen(t) + strlen(n) + 8); + strcpy(pm.u.str, start); + strcat(pm.u.str, t); strcat(pm.u.str, "\n\t"); strcat(pm.u.str, n); strcat(pm.u.str, " \"$@\""); } else - pm.u.str = dyncat("\t", t); + pm.u.str = dyncat(start, t); + /* + * TBD: Is this unmetafy correct? Surely as this + * is a parameter value it stays metafied? + */ unmetafy(pm.u.str, NULL); zsfree(t); + + if (shf->redir) { + t = getpermtext(shf->redir, NULL, 1); + pm.u.str = zhtricat(pm.u.str, "\n}", t); + zsfree(t); + } } } func(&pm.node, flags); diff --git a/Test/A04redirect.ztst b/Test/A04redirect.ztst index 6c38a31..a39ce46 100644 --- a/Test/A04redirect.ztst +++ b/Test/A04redirect.ztst @@ -507,3 +507,27 @@ >Ich heisse 2func >Running 3func >Ich heisse 3func + + redirfn2() { print The latest output; } >&3 + redirfn2 3>output4 + print No output yet + cat output4 +0:Redirections in both function definition and command line +>No output yet +>The latest output + +# This relies on the fact that the test harness always loads +# the zsh/parameter module. + print $functions[redirfn] +0:Output from $functions[] for definition with redirection +>{ +> local var +> read var +> print I want to tell you about $var +> print Also, this might be an error >&2 +>} < input2 > output2 2>&1 + + noredirfn() { print This rather boring function has no redirection.; } + print $functions[noredirfn] +0:Output from $functions[] for definition with no redirection +> print This rather boring function has no redirection. pws