All of lore.kernel.org
 help / color / mirror / Atom feed
From: Herbert Xu <herbert@gondor.apana.org.au>
To: Michael Greenberg <Michael.Greenberg@pomona.edu>
Cc: "dash@vger.kernel.org" <dash@vger.kernel.org>
Subject: [v2 PATCH] parser: Fix handling of empty aliases
Date: Tue, 28 Apr 2020 01:15:26 +1000	[thread overview]
Message-ID: <20200427151526.GA26444@gondor.apana.org.au> (raw)
In-Reply-To: <m2y33ftyu7.fsf@pomona.edu>

On Thu, May 09, 2019 at 03:46:43PM +0000, Michael Greenberg wrote:
> Dash was incorrectly handling empty aliases. When attempting to use an
> empty alias with nothing else, I'm (incorrectly) prompted for more
> input:
> 
> ```
> $ alias empty=''
> $ empty
> >
> ```
> 
> Other shells (e.g., bash, yash) correctly handle the lone, empty alias as an
> empty command:
> 
> ```
> $ alias empty=''
> $ empty
> $
> ```
> 
> This patch fixes the parser to handle the case where an alias is empty,
> i.e., produces no token.
> 
> Signed-off-by: Michael Greenberg <michael.greenberg@pomona.edu>

Thanks for the patch! I think your patch doesn't fix all the cases
though as it's also possible to encounter the problem if the alias
consists entirely of space characters.  Here is my attempt at the
problem.

---8<---
Dash was incorrectly handling empty aliases. When attempting to use an
empty alias with nothing else, I'm (incorrectly) prompted for more
input:

```
$ alias empty=''
$ empty
>
```

Other shells (e.g., bash, yash) correctly handle the lone, empty alias as an
empty command:

```
$ alias empty=''
$ empty
$
```

The problem here is that we incorrectly enter the loop eating TNLs
in readtoken().  This patch fixes it by setting checkkwd correctly.

Reported-by: Michael Greenberg <michael.greenberg@pomona.edu>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/src/parser.c b/src/parser.c
index b318b08..5c9e9a0 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -115,7 +115,6 @@ STATIC union node *simplecmd(void);
 STATIC union node *makename(void);
 STATIC void parsefname(void);
 STATIC void parseheredoc(void);
-STATIC int peektoken(void);
 STATIC int readtoken(void);
 STATIC int xxreadtoken(void);
 STATIC int pgetc_eatbnl();
@@ -161,21 +160,23 @@ parsecmd(int interact)
 STATIC union node *
 list(int nlflag)
 {
+	int chknl = nlflag & 1 ? 0 : CHKNL;
 	union node *n1, *n2, *n3;
 	int tok;
 
 	n1 = NULL;
 	for (;;) {
-		switch (readtoken()) {
+		checkkwd = chknl | CHKKWD | CHKALIAS;
+		tok = readtoken();
+		switch (tok) {
 		case TNL:
-			if (!(nlflag & 1))
-				break;
 			parseheredoc();
 			return n1;
 
 		case TEOF:
-			if (!n1 && (nlflag & 1))
+			if (!n1 && !chknl)
 				n1 = NEOF;
+out_eof:
 			parseheredoc();
 			tokpushback++;
 			lasttoken = TEOF;
@@ -183,8 +184,7 @@ list(int nlflag)
 		}
 
 		tokpushback++;
-		checkkwd = CHKNL | CHKKWD | CHKALIAS;
-		if (nlflag == 2 && tokendlist[peektoken()])
+		if (nlflag == 2 && tokendlist[tok])
 			return n1;
 		nlflag |= 2;
 
@@ -214,15 +214,16 @@ list(int nlflag)
 			n1 = n3;
 		}
 		switch (tok) {
-		case TNL:
 		case TEOF:
+			goto out_eof;
+		case TNL:
 			tokpushback++;
 			/* fall through */
 		case TBACKGND:
 		case TSEMI:
 			break;
 		default:
-			if ((nlflag & 1))
+			if (!chknl)
 				synexpect(-1);
 			tokpushback++;
 			return n1;
@@ -685,16 +686,6 @@ parseheredoc(void)
 	}
 }
 
-STATIC int
-peektoken(void)
-{
-	int t;
-
-	t = readtoken();
-	tokpushback++;
-	return (t);
-}
-
 STATIC int
 readtoken(void)
 {
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

       reply	other threads:[~2020-04-27 15:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <m2y33ftyu7.fsf@pomona.edu>
2020-04-27 15:15 ` Herbert Xu [this message]
2020-04-27 17:07   ` [v2 PATCH] parser: Fix handling of empty aliases Michael Greenberg
2020-04-27 20:31 ` [PATCH] alias: " Martijn Dekker

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=20200427151526.GA26444@gondor.apana.org.au \
    --to=herbert@gondor.apana.org.au \
    --cc=Michael.Greenberg@pomona.edu \
    --cc=dash@vger.kernel.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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.