From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 8855 invoked from network); 8 Nov 2022 12:40:36 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 8 Nov 2022 12:40:36 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1667911236; b=oQcQ4/y2pc020+La9lbjRB6xCgs2Jlu64Ze9cdLb9d0y6AdDwqS6BcFt4nvYEF4qwoCS7hAGDs NRxCqd6q3JoC3dkO66InxmUR5ry8toiRINynA3MIDz11XB+ik7sqwAJPuYvvz0cobSUM0oWAbj nsIb6VL01xhHTQglFKUY2hZ3ydT2yJOTo/7iAJ8j8bZvaQQI48O+1w/DJWFx7aE1x4N8TDqGqa 8DBPTAQ6AVfQbwxiqQ6qVUR3f6cdHkx/Fzhib0U+Sul25P61o+4697b0GXwToWEBRHN1WKjspb ByomkWhYTygJsQ6KR6rPyPB4UU8NrK3h1Bk7vJwjpmY5Iw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (smtpq2.tb.ukmail.iss.as9143.net) smtp.remote-ip=212.54.57.97; dkim=pass header.d=ntlworld.com header.s=meg.feb2017 header.a=rsa-sha256; dmarc=pass header.from=ntlworld.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1667911236; bh=O+Unw6/CbKcCzIpmehDQJLRReac/4qRdPVR/8hOilJ0=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject: References:In-Reply-To:Message-ID:To:From:Date:DKIM-Signature: DKIM-Signature; b=PSVpFQc1/JEX+e8qdMP+wPp6B9BVw5EdeAG6aGGmXilPVPPxlqUno7o2EBA94AY6kPNsL+qn1u KqHcaNtRn2XznTR8TX1Hq7RHeHoJXiy6ZWhGTN/1ex7IfPBvPk1XyjeBYfDUgzy9EHM4bbCwUq kp68V6vWoTxKWonjNva2srg2l2imRp0sjLHaZA7TRTN/Abq3FxdE2fJXAccA1wwchDqP5ioADF RSw4IC0uWr5AeSOazlnzBewzgHrqzkPih8pP/dYTIUTTzUq9aH7tfRb4y5s1ZBuzrwLjD1iNVX B+r4Q81QWBH/sQDDLjWdMAnFuzCDiVBMAbZi0e3XO3t0Eg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:References:In-Reply-To:Message-ID:To:From: Date:Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=MQEneBHwv9MzCOQdxPUIALJac3q88ndZSb4EZTLdztc=; b=T8x2LC2bXguj0xFsAOvDYVLnvv nXZ/2lxn9cwzJ/Zyy0F5tqPFAmT7kkX8oRDFfoFiVsIKNR380Au+RGq4lioYi4DnyP2hM+n4pGWnt sElegN7ld34O4O+8PcZ7BN7Ohm4hByYrbQoSXGFFmm+5dpQq0hNfk1XQJmFXGxvaKMRhBL50n4vhD f3aW0CpCqakkd25uZWFMxYIHxJ7i0XiOvlRUcjd+4ICg3pVmo+6D+D7Cl3gjNEPNJGTXWIuF0qjwm SSH3T6GXyXZvPB/aGkMRbTaoh8eoWsZ/k/ncjAzN+8ANezv5rfTA4Pf6HRYUEA+q+USyncvnihkco C4mVQoIQ==; Received: by zero.zsh.org with local id 1osNu6-000Jd1-7H; Tue, 08 Nov 2022 12:40:34 +0000 Authentication-Results: zsh.org; iprev=pass (smtpq2.tb.ukmail.iss.as9143.net) smtp.remote-ip=212.54.57.97; dkim=pass header.d=ntlworld.com header.s=meg.feb2017 header.a=rsa-sha256; dmarc=pass header.from=ntlworld.com; arc=none Received: from smtpq2.tb.ukmail.iss.as9143.net ([212.54.57.97]:35146) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1osNss-000IwS-U5; Tue, 08 Nov 2022 12:39:19 +0000 Received: from [212.54.57.82] (helo=smtp3.tb.ukmail.iss.as9143.net) by smtpq2.tb.ukmail.iss.as9143.net with esmtp (Exim 4.90_1) (envelope-from ) id 1osNss-0003hb-BC for zsh-users@zsh.org; Tue, 08 Nov 2022 13:39:18 +0100 Received: from oxbe15.tb.ukmail.iss.as9143.net ([172.25.160.146]) by smtp3.tb.ukmail.iss.as9143.net with ESMTP id sNssoZ9GXsyQZsNssoDxak; Tue, 08 Nov 2022 13:39:18 +0100 X-Env-Mailfrom: p.w.stephenson@ntlworld.com X-Env-Rcptto: zsh-users@zsh.org X-SourceIP: 172.25.160.146 X-CNFS-Analysis: v=2.4 cv=W/8r6Dak c=1 sm=1 tr=0 ts=636a4df6 cx=a_exe a=3VDdwIuOneLoWcGUQKPbfw==:117 a=1DWFKdCB1IcA:10 a=IkcTkHD0fZMA:10 a=ZjyAdWuDOD4A:10 a=q2GGsy2AAAAA:8 a=pGLkceISAAAA:8 a=MONanHz0MTU2zpH_GRgA:9 a=QEXdDO2ut3YA:10 a=z9dJwno5l634igLiVhy-:22 X-Authenticated-Sender: p.w.stephenson@ntlworld.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ntlworld.com; s=meg.feb2017; t=1667911158; bh=O+Unw6/CbKcCzIpmehDQJLRReac/4qRdPVR/8hOilJ0=; h=Date:From:To:In-Reply-To:References:Subject; b=hWEpq2LCdYS+xdZMTjKzhy2vckjX3ghFHCv6VXeTdEY+p6FLEEcu8psSIuaDBkZy+ MzHeiEUUGx/R6DmPg36kQE/Tl7HLGhhtBs2EOh3AT5fQcZOMK4CJ0zU5pquvHtawg7 JMlHROF2EWinEt8BRG4IoRMyN73rPPE4QEvImNJRsZAlnSYSV4z8pPtmCjpB2dMXGV H1Pk+Sb3EGaSSR2IW2fH06kdeEl1MFqa5SPJuwkLlWJ3R8mrNsQYeC/ykG7HGCofdI F7QNsoe1LegB43PFLShDaegVQk3XkEVVgTGzyXC3ui7CqS3nhJ0cBBb9t4dzf5vXeF +M/Dp1y0Pv3bA== Date: Tue, 8 Nov 2022 12:39:18 +0000 (GMT) From: Peter Stephenson To: Zsh-Users List Message-ID: <1113483502.4632116.1667911158058@mail.virginmedia.com> In-Reply-To: References: Subject: Re: while alias MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal X-Mailer: Open-Xchange Mailer X-Originating-IP: 147.161.224.167 X-Originating-Client: open-xchange-appsuite X-CMAE-Envelope: MS4xfOoMuMZvYk5ckIUGI55f9AG4prrFuLuAId61vs7Tg9jmCmbCVcBl4Ui4gylsw2XvRYkEJvnB0SR2IRvPdWDDSAPgvg4m5ZTJK3AbtZhD80DRf0YZ/bDr vJHCWT2Lnhe1zROblkgA/lP+rUxWBCk9KzHdiZjDXCBOYi9K7iwtipqGuK8xknqmJ38CN2tT4BCAS/mj2XNfv8Pyd+i7F4Y7zAKMf8cSDIzqs5SdZP2AaKYo X-Seq: 28338 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: (This is really for zsh-workers but in case anyone wants to see the result...) > On 08/11/2022 06:13 Bart Schaefer wrote: > On Mon, Nov 7, 2022 at 5:57 PM Pier Paolo Grassi wrote: > > > > Hello, I have the following alias defined: > > > > WI='while {read -r it; ! [[ $? -ne 0 && -z $it ]]}' > > > > that works fine except in command substitution: > > Put spaces around your braces. > > alias WI='while { read -r it; ! [[ $? -ne 0 && -z $it ]] }' > > Aliases in command substitutions are parsed by a complicated dance > requiring that they be expanded and then backtracked over and then > re-parsed. The character count for the "{" and/or "}" tokens is off > by one for the backtracking step when they don't have surrounding > whitespace. (Leaving it to PWS, who choreographed this dance number, > to fix the footwork if possible.) Yes, it's that full combination of a command substitution, an alias, and expansion text with a lexically significant closing brace with no preceding space that's doing it, so it's very specific. This will come as a shock, but there's a hack in the lexical analyser! (Counselling will be available by the usual channels.) It seems that the lexer really doesn't like closing braces without a space before them and sometimes we have to fix up the text after the event. It seems the extra special code for command substitution needs a slightly modified version of the hack. The new test in D08 checks the case Pier Paolo hit; it turns out the existing test just above checks the case where we do after all need to unget an extra closing brace, i.e. lex_add_raw ends up 1 in the code added by the first hunk. I am not going to attempt to get my mind round this any further and will be going out for some fresh air. pws diff --git a/Src/lex.c b/Src/lex.c index ece02659e..e2f8bcfb1 100644 --- a/Src/lex.c +++ b/Src/lex.c @@ -1429,10 +1429,18 @@ gettokstr(int c, int sub) peek == STRING && lexbuf.ptr[-1] == '}' && lexbuf.ptr[-2] != Bnull) { /* hack to get {foo} command syntax work */ + /* + * Alias expansion when parsing command substitution means that + * the case for raw lexical analysis may not be the same. + * (Just go with it, OK?) + */ + int lar = lex_add_raw; + lex_add_raw = lexbuf_raw.len > 0 && lexbuf_raw.ptr[-1] == '}'; lexbuf.ptr--; lexbuf.len--; lexstop = 0; hungetc('}'); + lex_add_raw = lar; } *lexbuf.ptr = '\0'; DPUTS(cmdsp != ocmdsp, "BUG: gettok: cmdstack changed."); diff --git a/Test/D08cmdsubst.ztst b/Test/D08cmdsubst.ztst index 04bf698aa..8b6bcb469 100644 --- a/Test/D08cmdsubst.ztst +++ b/Test/D08cmdsubst.ztst @@ -177,3 +177,11 @@ 0:Alias expansion needed in parsing substitutions >hi >bye + +# This should silently print a blank line; the original problem was +# a parse error as the last character of the unexpanded alias +# was erased, symptom: "command not found: W" + alias WI='while {false}' + eval 'echo $(WI blah)' +0:Aliases with braces in command substitution can cause havoc +>