Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Fix switch alias handling. Inspired from FreeBSD, but...



details:   https://anonhg.NetBSD.org/src/rev/bcedfb9c53a3
branches:  trunk
changeset: 516951:bcedfb9c53a3
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Nov 02 23:49:14 2001 +0000

description:
Fix switch alias handling. Inspired from FreeBSD, but corrected to handle
alias expansion inside the switch as appropriate. This is achieved by a
flag noalias which is turned on and off in as we parse. In the following
example [1] and [0] indicate the value of noalias.

[0] case <expr> in
[1] <lit> ) [0] <expr> ;;
[1] <lit> ) [0] <expr> ;;
...
[1] esac [0]

FreeBSD does:

[0] case <expr> in [1]
<lit> ) <expr> ;;
<lit> )  <expr> ;;
...
esac [0]

This handles the following shell script:

alias a=ls

case $1 in
a)      echo a;
        a;;
f)      echo f;;
*)      echo default;;
esac

diffstat:

 bin/sh/parser.c |  26 ++++++++++++++++++--------
 1 files changed, 18 insertions(+), 8 deletions(-)

diffs (87 lines):

diff -r c415026d9031 -r bcedfb9c53a3 bin/sh/parser.c
--- a/bin/sh/parser.c   Fri Nov 02 23:43:13 2001 +0000
+++ b/bin/sh/parser.c   Fri Nov 02 23:49:14 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parser.c,v 1.47 2001/04/03 15:00:11 christos Exp $     */
+/*     $NetBSD: parser.c,v 1.48 2001/11/02 23:49:14 christos Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "@(#)parser.c   8.7 (Berkeley) 5/16/95";
 #else
-__RCSID("$NetBSD: parser.c,v 1.47 2001/04/03 15:00:11 christos Exp $");
+__RCSID("$NetBSD: parser.c,v 1.48 2001/11/02 23:49:14 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -87,6 +87,7 @@
 
 
 
+static int noalias = 0;                /* when set, don't handle aliases */
 struct heredoc *heredoclist;   /* list of here documents to read */
 int parsebackquote;            /* nonzero if we are inside backquotes */
 int doprompt;                  /* if set, prompt the user */
@@ -417,6 +418,7 @@
                if (lasttoken != TWORD || ! equal(wordtext, "in"))
                        synerror("expecting \"in\"");
                cpp = &n1->ncase.cases;
+               noalias = 1;
                checkkwd = 2, readtoken();
                do {
                        *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
@@ -433,19 +435,26 @@
                                readtoken();
                        }
                        ap->narg.next = NULL;
-                       if (lasttoken != TRP)
+                       noalias = 0;
+                       if (lasttoken != TRP) {
                                synexpect(TRP);
+                       }
                        cp->nclist.body = list(0);
 
                        checkkwd = 2;
                        if ((t = readtoken()) != TESAC) {
-                               if (t != TENDCASE)
+                               if (t != TENDCASE) {
+                                       noalias = 0;
                                        synexpect(TENDCASE);
-                               else
-                                       checkkwd = 2, readtoken();
+                               } else {
+                                       noalias = 1;
+                                       checkkwd = 2;
+                                       readtoken();
+                               }
                        }
                        cpp = &cp->nclist.next;
                } while(lasttoken != TESAC);
+               noalias = 0;
                *cpp = NULL;
                checkkwd = 1;
                break;
@@ -702,10 +711,10 @@
 readtoken() {
        int t;
        int savecheckkwd = checkkwd;
-       struct alias *ap;
 #ifdef DEBUG
        int alreadyseen = tokpushback;
 #endif
+       struct alias *ap;
 
        top:
        t = xxreadtoken();
@@ -738,7 +747,8 @@
                                        goto out;
                                }
                        }
-                       if ((ap = lookupalias(wordtext, 1)) != NULL) {
+                       if(!noalias &&
+                           (ap = lookupalias(wordtext, 1)) != NULL) {
                                pushstring(ap->val, strlen(ap->val), ap);
                                checkkwd = savecheckkwd;
                                goto top;



Home | Main Index | Thread Index | Old Index