Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: functions with redirections

On Mon, 29 Sep 2014 14:37:35 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> 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

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.

Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/

Messages sorted by: Reverse Date, Date, Thread, Author