From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29617 invoked by alias); 1 Oct 2014 19:22:57 -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: 33312 Received: (qmail 3106 invoked from network); 1 Oct 2014 19:22:43 -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=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-Originating-IP: [80.3.229.105] X-Spam: 0 X-Authority: v=2.1 cv=Cq4xcxID c=1 sm=1 tr=0 a=uz1KDxDNIq33yePw376BBA==:117 a=uz1KDxDNIq33yePw376BBA==:17 a=NLZqzBF-AAAA:8 a=uObrxnre4hsA:10 a=kj9zAlcOel0A:10 a=q2GGsy2AAAAA:8 a=5y7KStzSQP4S2h1FE_wA:9 a=CjuIK1q_8ugA:10 a=I6wTmPyJxzYA:10 a=_dQi-Dcv4p4A:10 Date: Wed, 1 Oct 2014 20:17:05 +0100 From: Peter Stephenson To: Zsh hackers list Subject: Re: PATCH: functions with redirections Message-ID: <20141001201705.23ee198d@pws-pc.ntlworld.com> In-Reply-To: References: <20140929205236.2eb5e622@pws-pc.ntlworld.com> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 29 Sep 2014 14:37:35 -0700 Bart Schaefer wrote: > My first idea when this came up was to alter the wordcode for functions > with redirections to explicitly add the implicit set of braces, e.g., > > foo () { echo foo } >&3 > > becomes > > foo () { { echo foo } >&3 } > > Then it would not be necessary to mess with printing/dumping etc. of the > definition, or invent a new place to store the redirections. Yeah, output > of "functions" et al. becomes a little less faithful but so what? > > Sadly I never managed to make that work, but maybe the concept is helpful > for dump file creation. This appears to be about the only way I could even conceivably get this to work --- otherwise it means reengineering multiple different ways of constructing and reading a dump file --- but even this appears too difficult. The main sticking point is where you need to add the wrapper you have an Eprog, and it's hard to convert that back into the form you need for adding additional components or glueing bits together --- Eprogs are already glued together seamlessly. I fixed it for the redir on its own by writing a function to extract the simple redir wordcode directly, but that's not feasible here. It's not fatal that it doesn't work, you just lose the redirections. So I'll simply document around this and be satisfied with the fact that it is possible to compile an autoload file like foo() { echo foo } >&3 with or without the zsh kludge 'foo "$@"' stuck at the end. I suspect compiling existing autoload files is much more common than compiling functions typed into the shell. I've also documented the basic feature and verified for the first time that foo() echo foo >&3 also works as expected. (As expected if you understand weird squiggles, that is.) diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index 9862c63..41c189f 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -2174,6 +2174,20 @@ match one of these patterns will be written. If no var(name) is given, the definitions of all functions currently defined or marked as autoloaded will be written. +Note the second form cannot be used for compiling functions that +include redirections as part of the definition rather than within +the body of the function; for example + +example(fn1() { { ... } >~/logfile }) + +can be compiled but + +example(fn1() { ... } >~/logfile) + +cannot. It is possible to use the first form of tt(zcompile) to compile +autoloadable functions that include the full function definition instead +of just the body of the function. + The third form, with the tt(-t) option, examines an existing compiled file. Without further arguments, the names of the original files compiled into it are listed. The first line of output shows diff --git a/Doc/Zsh/grammar.yo b/Doc/Zsh/grammar.yo index 77f0098..eb1edf7 100644 --- a/Doc/Zsh/grammar.yo +++ b/Doc/Zsh/grammar.yo @@ -352,6 +352,15 @@ If the option tt(SH_GLOB) is set for compatibility with other shells, then whitespace may appear between between the left and right parentheses when there is a single var(word); otherwise, the parentheses will be treated as forming a globbing pattern in that case. + +In any of the forms above, a redirection may appear outside the +function body, for example + +example(func() { ... } 2>&1) + +The redirection is stored with the function and applied whenever the +function is executed. Any variables in the redirection are expanded +at the point the function is executed, but outside the function scope. ) cindex(timing) findex(time) -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/