zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh-workers@zsh.org
Subject: Re: Anonymous function syntax and "sh" emulation
Date: Fri, 21 Aug 2015 15:52:49 +0100	[thread overview]
Message-ID: <20150821155249.3f773977@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <150820211638.ZM29649@torch.brasslantern.com>

On Thu, 20 Aug 2015 21:16:38 -0700
Bart Schaefer <schaefer@brasslantern.com> 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.

pws

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;
 	    break;
 	}


  reply	other threads:[~2015-08-21 14:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-21  4:16 Bart Schaefer
2015-08-21 14:52 ` Peter Stephenson [this message]
2015-08-21 15:29   ` Bart Schaefer
2015-08-21 16:03     ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150821155249.3f773977@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).