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

Re: Anonymous function syntax and "sh" emulation

On Thu, 20 Aug 2015 21:16:38 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> This works in zsh emulation but not in sh emulation:
>   function { print hello; }

This is a combination of two effects.

The one specific to sh is, as you might guess, IGNOREBRACES.  Clearly
we're not supposed to ignore a brace as the start of a function, so I
think this can be considered a bug.  I think compensating for this is
unproblematic, since we see the raw string input coming from lex.c and
hence any quoting that should stop this being treated as the start of a
function, so I've done that.

What's worried me is the other part of the effect, which isn't specific
to sh emulation, which is that the first word after "function" is
treated as not a command word, while the remaining words are treated as
command words:

% alias first='fn1 fn2' second='fn3 fn4'
% function first second { print This is a function; }
% functions first fn3 fn4
first () {
	print This is a function
fn3 () {
	print This is a function
fn4 () {
	print This is a function

I would hazard a guess this is a bad thing, which hasn't been noticed
because multiple words after "function" aren't very common.

More tentatively, I suspect it may have been a trick to get the shell to
cough up an INBRACE before we had the test I've just patched below.  So
I suspect we can just move the "incmdpos = 1" until after we've found
something that is either not a STRING or a possibly tokenized "{".  Not
included in the patch below, but I'll do it unless anyone contradicts.
Does not cause any tests to fail.


diff --git a/Src/parse.c b/Src/parse.c
index 1a74164..c2dcd2b 100644
--- a/Src/parse.c
+++ b/Src/parse.c
@@ -1602,7 +1602,8 @@ par_funcdef(int *cmplx)
     incmdpos = 1;
     while (tok == STRING) {
-	if (*tokstr == Inbrace && !tokstr[1]) {
+	if ((*tokstr == Inbrace || *tokstr == '{') &&
+	    !tokstr[1]) {
 	    tok = INBRACE;

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