BSD 4_3_Net_2 release
[unix-history] / usr / src / bin / csh / exp.c
index e7b30de..16963a7 100644 (file)
@@ -2,18 +2,51 @@
  * Copyright (c) 1980, 1991 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980, 1991 The Regents of the University of California.
  * All rights reserved.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)exp.c      5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)exp.c      5.11 (Berkeley) 6/8/91";
 #endif /* not lint */
 
 #endif /* not lint */
 
-#include "sh.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <unistd.h>
+#if __STDC__
+# include <stdarg.h>
+#else
+# include <varargs.h>
+#endif
 
 
-/*
- * C shell
- */
+#include "csh.h"
+#include "extern.h"
 
 #define IGNORE 1       /* in ignore, it means to ignore value, just parse */
 #define NOGLOB 2       /* in ignore, it means not to globone */
 
 #define IGNORE 1       /* in ignore, it means to ignore value, just parse */
 #define NOGLOB 2       /* in ignore, it means not to globone */
@@ -32,566 +65,644 @@ static char sccsid[] = "@(#)exp.c 5.6 (Berkeley) %G%";
 #define EQMATCH 7
 #define NOTEQMATCH 8
 
 #define EQMATCH 7
 #define NOTEQMATCH 8
 
+static int     exp1    __P((Char ***, bool));
+static int     exp2    __P((Char ***, bool));
+static int     exp2a   __P((Char ***, bool));
+static int     exp2b   __P((Char ***, bool));
+static int     exp2c   __P((Char ***, bool));
+static Char *  exp3    __P((Char ***, bool));
+static Char *  exp3a   __P((Char ***, bool));
+static Char *  exp4    __P((Char ***, bool));
+static Char *  exp5    __P((Char ***, bool));
+static Char *  exp6    __P((Char ***, bool));
+static void    evalav  __P((Char **));
+static int     isa     __P((Char *, int));
+static int     egetn   __P((Char *));
+
+#ifdef EDEBUG
+static void    etracc  __P((char *, Char *, Char ***));
+static void    etraci  __P((char *, int, Char ***));
+#endif
+
+int
 exp(vp)
 exp(vp)
-       register char ***vp;
+    register Char ***vp;
 {
 {
-
-       return (exp0(vp, 0));
+    return (exp0(vp, 0));
 }
 
 }
 
+int
 exp0(vp, ignore)
 exp0(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register int p1 = exp1(vp, ignore);
-       
+    register int p1 = exp1(vp, ignore);
+
 #ifdef EDEBUG
 #ifdef EDEBUG
-       etraci("exp0 p1", p1, vp);
+    etraci("exp0 p1", p1, vp);
 #endif
 #endif
-       if (**vp && eq(**vp, "||")) {
-               register int p2;
+    if (**vp && eq(**vp, STRor2)) {
+       register int p2;
 
 
-               (*vp)++;
-               p2 = exp0(vp, (ignore&IGNORE) || p1);
+       (*vp)++;
+       p2 = exp0(vp, (ignore & IGNORE) || p1);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etraci("exp0 p2", p2, vp);
+       etraci("exp0 p2", p2, vp);
 #endif
 #endif
-               return (p1 || p2);
-       }
-       return (p1);
+       return (p1 || p2);
+    }
+    return (p1);
 }
 
 }
 
+static int
 exp1(vp, ignore)
 exp1(vp, ignore)
-       register char ***vp;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register int p1 = exp2(vp, ignore);
+    register int p1 = exp2(vp, ignore);
 
 #ifdef EDEBUG
 
 #ifdef EDEBUG
-       etraci("exp1 p1", p1, vp);
+    etraci("exp1 p1", p1, vp);
 #endif
 #endif
-       if (**vp && eq(**vp, "&&")) {
-               register int p2;
+    if (**vp && eq(**vp, STRand2)) {
+       register int p2;
 
 
-               (*vp)++;
-               p2 = exp1(vp, (ignore&IGNORE) || !p1);
+       (*vp)++;
+       p2 = exp1(vp, (ignore & IGNORE) || !p1);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etraci("exp1 p2", p2, vp);
+       etraci("exp1 p2", p2, vp);
 #endif
 #endif
-               return (p1 && p2);
-       }
-       return (p1);
+       return (p1 && p2);
+    }
+    return (p1);
 }
 
 }
 
+static int
 exp2(vp, ignore)
 exp2(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register int p1 = exp2a(vp, ignore);
+    register int p1 = exp2a(vp, ignore);
 
 #ifdef EDEBUG
 
 #ifdef EDEBUG
-       etraci("exp3 p1", p1, vp);
+    etraci("exp3 p1", p1, vp);
 #endif
 #endif
-       if (**vp && eq(**vp, "|")) {
-               register int p2;
+    if (**vp && eq(**vp, STRor)) {
+       register int p2;
 
 
-               (*vp)++;
-               p2 = exp2(vp, ignore);
+       (*vp)++;
+       p2 = exp2(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etraci("exp3 p2", p2, vp);
+       etraci("exp3 p2", p2, vp);
 #endif
 #endif
-               return (p1 | p2);
-       }
-       return (p1);
+       return (p1 | p2);
+    }
+    return (p1);
 }
 
 }
 
+static int
 exp2a(vp, ignore)
 exp2a(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register int p1 = exp2b(vp, ignore);
+    register int p1 = exp2b(vp, ignore);
 
 #ifdef EDEBUG
 
 #ifdef EDEBUG
-       etraci("exp2a p1", p1, vp);
+    etraci("exp2a p1", p1, vp);
 #endif
 #endif
-       if (**vp && eq(**vp, "^")) {
-               register int p2;
+    if (**vp && eq(**vp, STRcaret)) {
+       register int p2;
 
 
-               (*vp)++;
-               p2 = exp2a(vp, ignore);
+       (*vp)++;
+       p2 = exp2a(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etraci("exp2a p2", p2, vp);
+       etraci("exp2a p2", p2, vp);
 #endif
 #endif
-               return (p1 ^ p2);
-       }
-       return (p1);
+       return (p1 ^ p2);
+    }
+    return (p1);
 }
 
 }
 
+static int
 exp2b(vp, ignore)
 exp2b(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register int p1 = exp2c(vp, ignore);
+    register int p1 = exp2c(vp, ignore);
 
 #ifdef EDEBUG
 
 #ifdef EDEBUG
-       etraci("exp2b p1", p1, vp);
+    etraci("exp2b p1", p1, vp);
 #endif
 #endif
-       if (**vp && eq(**vp, "&")) {
-               register int p2;
+    if (**vp && eq(**vp, STRand)) {
+       register int p2;
 
 
-               (*vp)++;
-               p2 = exp2b(vp, ignore);
+       (*vp)++;
+       p2 = exp2b(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etraci("exp2b p2", p2, vp);
+       etraci("exp2b p2", p2, vp);
 #endif
 #endif
-               return (p1 & p2);
-       }
-       return (p1);
+       return (p1 & p2);
+    }
+    return (p1);
 }
 
 }
 
+static int
 exp2c(vp, ignore)
 exp2c(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register char *p1 = exp3(vp, ignore);
-       register char *p2;
-       register int i;
+    register Char *p1 = exp3(vp, ignore);
+    register Char *p2;
+    register int i;
 
 #ifdef EDEBUG
 
 #ifdef EDEBUG
-       etracc("exp2c p1", p1, vp);
+    etracc("exp2c p1", p1, vp);
 #endif
 #endif
-       if (i = isa(**vp, EQOP)) {
-               (*vp)++;
-               if (i == EQMATCH || i == NOTEQMATCH)
-                       ignore |= NOGLOB;
-               p2 = exp3(vp, ignore);
+    if (i = isa(**vp, EQOP)) {
+       (*vp)++;
+       if (i == EQMATCH || i == NOTEQMATCH)
+           ignore |= NOGLOB;
+       p2 = exp3(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etracc("exp2c p2", p2, vp);
+       etracc("exp2c p2", p2, vp);
 #endif
 #endif
-               if (!(ignore&IGNORE)) switch (i) {
-
-               case EQEQ:
-                       i = eq(p1, p2);
-                       break;
-
-               case NOTEQ:
-                       i = !eq(p1, p2);
-                       break;
-
-               case EQMATCH:
-                       i = Gmatch(p1, p2);
-                       break;
-
-               case NOTEQMATCH:
-                       i = !Gmatch(p1, p2);
-                       break;
-               }
-               xfree(p1), xfree(p2);
-               return (i);
-       }
-       i = egetn(p1);
-       xfree(p1);
+       if (!(ignore & IGNORE))
+           switch (i) {
+
+           case EQEQ:
+               i = eq(p1, p2);
+               break;
+
+           case NOTEQ:
+               i = !eq(p1, p2);
+               break;
+
+           case EQMATCH:
+               i = Gmatch(p1, p2);
+               break;
+
+           case NOTEQMATCH:
+               i = !Gmatch(p1, p2);
+               break;
+           }
+       xfree((ptr_t) p1);
+       xfree((ptr_t) p2);
        return (i);
        return (i);
+    }
+    i = egetn(p1);
+    xfree((ptr_t) p1);
+    return (i);
 }
 
 }
 
-char *
+static Char *
 exp3(vp, ignore)
 exp3(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register char *p1, *p2;
-       register int i;
+    register Char *p1, *p2;
+    register int i;
 
 
-       p1 = exp3a(vp, ignore);
+    p1 = exp3a(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-       etracc("exp3 p1", p1, vp);
+    etracc("exp3 p1", p1, vp);
 #endif
 #endif
-       if (i = isa(**vp, RELOP)) {
-               (*vp)++;
-               if (**vp && eq(**vp, "="))
-                       i |= 1, (*vp)++;
-               p2 = exp3(vp, ignore);
+    if (i = isa(**vp, RELOP)) {
+       (*vp)++;
+       if (**vp && eq(**vp, STRequal))
+           i |= 1, (*vp)++;
+       p2 = exp3(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etracc("exp3 p2", p2, vp);
+       etracc("exp3 p2", p2, vp);
 #endif
 #endif
-               if (!(ignore&IGNORE)) switch (i) {
-
-               case GTR:
-                       i = egetn(p1) > egetn(p2);
-                       break;
-
-               case GTR|1:
-                       i = egetn(p1) >= egetn(p2);
-                       break;
-
-               case LSS:
-                       i = egetn(p1) < egetn(p2);
-                       break;
-
-               case LSS|1:
-                       i = egetn(p1) <= egetn(p2);
-                       break;
-               }
-               xfree(p1), xfree(p2);
-               return (putn(i));
-       }
-       return (p1);
+       if (!(ignore & IGNORE))
+           switch (i) {
+
+           case GTR:
+               i = egetn(p1) > egetn(p2);
+               break;
+
+           case GTR | 1:
+               i = egetn(p1) >= egetn(p2);
+               break;
+
+           case LSS:
+               i = egetn(p1) < egetn(p2);
+               break;
+
+           case LSS | 1:
+               i = egetn(p1) <= egetn(p2);
+               break;
+           }
+       xfree((ptr_t) p1);
+       xfree((ptr_t) p2);
+       return (putn(i));
+    }
+    return (p1);
 }
 
 }
 
-char *
+static Char *
 exp3a(vp, ignore)
 exp3a(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register char *p1, *p2, *op;
-       register int i;
+    register Char *p1, *p2, *op;
+    register int i;
 
 
-       p1 = exp4(vp, ignore);
+    p1 = exp4(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-       etracc("exp3a p1", p1, vp);
+    etracc("exp3a p1", p1, vp);
 #endif
 #endif
-       op = **vp;
-       if (op && index("<>", op[0]) && op[0] == op[1]) {
-               (*vp)++;
-               p2 = exp3a(vp, ignore);
+    op = **vp;
+    if (op && any("<>", op[0]) && op[0] == op[1]) {
+       (*vp)++;
+       p2 = exp3a(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etracc("exp3a p2", p2, vp);
-#endif
-               if (op[0] == '<')
-                       i = egetn(p1) << egetn(p2);
-               else
-                       i = egetn(p1) >> egetn(p2);
-               xfree(p1), xfree(p2);
-               return (putn(i));
-       }
-       return (p1);
+       etracc("exp3a p2", p2, vp);
+#endif
+       if (op[0] == '<')
+           i = egetn(p1) << egetn(p2);
+       else
+           i = egetn(p1) >> egetn(p2);
+       xfree((ptr_t) p1);
+       xfree((ptr_t) p2);
+       return (putn(i));
+    }
+    return (p1);
 }
 
 }
 
-char *
+static Char *
 exp4(vp, ignore)
 exp4(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register char *p1, *p2;
-       register int i = 0;
+    register Char *p1, *p2;
+    register int i = 0;
 
 
-       p1 = exp5(vp, ignore);
+    p1 = exp5(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-       etracc("exp4 p1", p1, vp);
+    etracc("exp4 p1", p1, vp);
 #endif
 #endif
-       if (isa(**vp, ADDOP)) {
-               register char *op = *(*vp)++;
+    if (isa(**vp, ADDOP)) {
+       register Char *op = *(*vp)++;
 
 
-               p2 = exp4(vp, ignore);
+       p2 = exp4(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etracc("exp4 p2", p2, vp);
+       etracc("exp4 p2", p2, vp);
 #endif
 #endif
-               if (!(ignore&IGNORE)) switch (op[0]) {
-
-               case '+':
-                       i = egetn(p1) + egetn(p2);
-                       break;
-
-               case '-':
-                       i = egetn(p1) - egetn(p2);
-                       break;
-               }
-               xfree(p1), xfree(p2);
-               return (putn(i));
-       }
-       return (p1);
+       if (!(ignore & IGNORE))
+           switch (op[0]) {
+
+           case '+':
+               i = egetn(p1) + egetn(p2);
+               break;
+
+           case '-':
+               i = egetn(p1) - egetn(p2);
+               break;
+           }
+       xfree((ptr_t) p1);
+       xfree((ptr_t) p2);
+       return (putn(i));
+    }
+    return (p1);
 }
 
 }
 
-char *
+static Char *
 exp5(vp, ignore)
 exp5(vp, ignore)
-       register char ***vp;
-       bool ignore;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       register char *p1, *p2;
-       register int i = 0;
+    register Char *p1, *p2;
+    register int i = 0;
 
 
-       p1 = exp6(vp, ignore);
+    p1 = exp6(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-       etracc("exp5 p1", p1, vp);
+    etracc("exp5 p1", p1, vp);
 #endif
 #endif
-       if (isa(**vp, MULOP)) {
-               register char *op = *(*vp)++;
+    if (isa(**vp, MULOP)) {
+       register Char *op = *(*vp)++;
 
 
-               p2 = exp5(vp, ignore);
+       p2 = exp5(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etracc("exp5 p2", p2, vp);
-#endif
-               if (!(ignore&IGNORE)) switch (op[0]) {
-
-               case '*':
-                       i = egetn(p1) * egetn(p2);
-                       break;
-
-               case '/':
-                       i = egetn(p2);
-                       if (i == 0)
-                               error("Divide by 0");
-                       i = egetn(p1) / i;
-                       break;
-
-               case '%':
-                       i = egetn(p2);
-                       if (i == 0)
-                               error("Mod by 0");
-                       i = egetn(p1) % i;
-                       break;
-               }
-               xfree(p1), xfree(p2);
-               return (putn(i));
-       }
-       return (p1);
+       etracc("exp5 p2", p2, vp);
+#endif
+       if (!(ignore & IGNORE))
+           switch (op[0]) {
+
+           case '*':
+               i = egetn(p1) * egetn(p2);
+               break;
+
+           case '/':
+               i = egetn(p2);
+               if (i == 0)
+                   stderror(ERR_DIV0);
+               i = egetn(p1) / i;
+               break;
+
+           case '%':
+               i = egetn(p2);
+               if (i == 0)
+                   stderror(ERR_MOD0);
+               i = egetn(p1) % i;
+               break;
+           }
+       xfree((ptr_t) p1);
+       xfree((ptr_t) p2);
+       return (putn(i));
+    }
+    return (p1);
 }
 
 }
 
-char *
+static Char *
 exp6(vp, ignore)
 exp6(vp, ignore)
-       register char ***vp;
+    register Char ***vp;
+    bool    ignore;
 {
 {
-       int ccode, i;
-       register char *cp, *dp, *ep;
-
-       if (**vp == 0)
-               bferr("Expression syntax");
-       if (eq(**vp, "!")) {
-               (*vp)++;
-               cp = exp6(vp, ignore);
+    int     ccode, i = 0;
+    register Char *cp, *dp, *ep;
+
+    if (**vp == 0)
+       stderror(ERR_NAME | ERR_EXPRESSION);
+    if (eq(**vp, STRbang)) {
+       (*vp)++;
+       cp = exp6(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etracc("exp6 ! cp", cp, vp);
+       etracc("exp6 ! cp", cp, vp);
 #endif
 #endif
-               i = egetn(cp);
-               xfree(cp);
-               return (putn(!i));
-       }
-       if (eq(**vp, "~")) {
-               (*vp)++;
-               cp = exp6(vp, ignore);
+       i = egetn(cp);
+       xfree((ptr_t) cp);
+       return (putn(!i));
+    }
+    if (eq(**vp, STRtilde)) {
+       (*vp)++;
+       cp = exp6(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etracc("exp6 ~ cp", cp, vp);
+       etracc("exp6 ~ cp", cp, vp);
 #endif
 #endif
-               i = egetn(cp);
-               xfree(cp);
-               return (putn(~i));
-       }
-       if (eq(**vp, "(")) {
-               (*vp)++;
-               ccode = exp0(vp, ignore);
+       i = egetn(cp);
+       xfree((ptr_t) cp);
+       return (putn(~i));
+    }
+    if (eq(**vp, STRLparen)) {
+       (*vp)++;
+       ccode = exp0(vp, ignore);
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etraci("exp6 () ccode", ccode, vp);
+       etraci("exp6 () ccode", ccode, vp);
 #endif
 #endif
-               if (*vp == 0 || **vp == 0 || ***vp != ')')
-                       bferr("Expression syntax");
-               (*vp)++;
-               return (putn(ccode));
+       if (*vp == 0 || **vp == 0 || ***vp != ')')
+           stderror(ERR_NAME | ERR_EXPRESSION);
+       (*vp)++;
+       return (putn(ccode));
+    }
+    if (eq(**vp, STRLbrace)) {
+       register Char **v;
+       struct command faket;
+       Char   *fakecom[2];
+
+       faket.t_dtyp = NODE_COMMAND;
+       faket.t_dflg = 0;
+       faket.t_dcar = faket.t_dcdr = faket.t_dspr = NULL;
+       faket.t_dcom = fakecom;
+       fakecom[0] = STRfakecom;
+       fakecom[1] = NULL;
+       (*vp)++;
+       v = *vp;
+       for (;;) {
+           if (!**vp)
+               stderror(ERR_NAME | ERR_MISSING, '}');
+           if (eq(*(*vp)++, STRRbrace))
+               break;
+       }
+       if (ignore & IGNORE)
+           return (Strsave(STRNULL));
+       psavejob();
+       if (pfork(&faket, -1) == 0) {
+           *--(*vp) = 0;
+           evalav(v);
+           exitstat();
        }
        }
-       if (eq(**vp, "{")) {
-               register char **v;
-               struct command faket;
-               char *fakecom[2];
-
-               faket.t_dtyp = NODE_COMMAND;
-               faket.t_dflg = 0;
-               faket.t_dcar = faket.t_dcdr = faket.t_dspr = (struct command *)0;
-               faket.t_dcom = fakecom;
-               fakecom[0] = "{ ... }";
-               fakecom[1] = NOSTR;
-               (*vp)++;
-               v = *vp;
-               for (;;) {
-                       if (!**vp)
-                               bferr("Missing }");
-                       if (eq(*(*vp)++, "}"))
-                               break;
-               }
-               if (ignore&IGNORE)
-                       return ("");
-               psavejob();
-               if (pfork(&faket, -1) == 0) {
-                       *--(*vp) = 0;
-                       evalav(v);
-                       exitstat();
-               }
-               pwait();
-               prestjob();
+       pwait();
+       prestjob();
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etraci("exp6 {} status", egetn(value("status")), vp);
+       etraci("exp6 {} status", egetn(value(STRstatus)), vp);
+#endif
+       return (putn(egetn(value(STRstatus)) == 0));
+    }
+    if (isa(**vp, ANYOP))
+       return (Strsave(STRNULL));
+    cp = *(*vp)++;
+    if (*cp == '-' && any("erwxfdzopls", cp[1])) {
+       struct stat stb;
+
+       if (cp[2] != '\0')
+           stderror(ERR_NAME | ERR_FILEINQ);
+       /*
+        * Detect missing file names by checking for operator in the file name
+        * position.  However, if an operator name appears there, we must make
+        * sure that there's no file by that name (e.g., "/") before announcing
+        * an error.  Even this check isn't quite right, since it doesn't take
+        * globbing into account.
+        */
+       if (isa(**vp, ANYOP) && stat(short2str(**vp), &stb))
+           stderror(ERR_NAME | ERR_FILENAME);
+
+       dp = *(*vp)++;
+       if (ignore & IGNORE)
+           return (Strsave(STRNULL));
+       ep = globone(dp, G_ERROR);
+       switch (cp[1]) {
+
+       case 'r':
+           i = !access(short2str(ep), R_OK);
+           break;
+
+       case 'w':
+           i = !access(short2str(ep), W_OK);
+           break;
+
+       case 'x':
+           i = !access(short2str(ep), X_OK);
+           break;
+
+       default:
+           if (
+#ifdef S_IFLNK
+               cp[1] == 'l' ? lstat(short2str(ep), &stb) :
+#endif
+               stat(short2str(ep), &stb)) {
+               xfree((ptr_t) ep);
+               return (Strsave(STR0));
+           }
+           switch (cp[1]) {
+
+           case 'f':
+               i = S_ISREG(stb.st_mode);
+               break;
+
+           case 'd':
+               i = S_ISDIR(stb.st_mode);
+               break;
+
+           case 'p':
+#ifdef S_ISFIFO
+               i = S_ISFIFO(stb.st_mode);
+#else
+               i = 0;
+#endif
+               break;
+
+           case 'l':
+#ifdef S_ISLNK
+               i = S_ISLNK(stb.st_mode);
+#else
+               i = 0;
 #endif
 #endif
-               return (putn(egetn(value("status")) == 0));
+               break;
+
+           case 's':
+#ifdef S_ISSOCK
+               i = S_ISSOCK(stb.st_mode);
+#else
+               i = 0;
+#endif
+               break;
+
+           case 'z':
+               i = stb.st_size == 0;
+               break;
+
+           case 'e':
+               i = 1;
+               break;
+
+           case 'o':
+               i = stb.st_uid == uid;
+               break;
+           }
        }
        }
-       if (isa(**vp, ANYOP))
-               return ("");
-       cp = *(*vp)++;
-       if (*cp == '-' && index("erwxfdzo", cp[1])) {
-               struct stat stb;
-
-               if (isa(**vp, ANYOP))
-                       bferr("Missing file name");
-               dp = *(*vp)++;
-               if (ignore&IGNORE)
-                       return ("");
-               ep = globone(dp);
-               switch (cp[1]) {
-
-               case 'r':
-                       i = !access(ep, 4);
-                       break;
-
-               case 'w':
-                       i = !access(ep, 2);
-                       break;
-
-               case 'x':
-                       i = !access(ep, 1);
-                       break;
-
-               default:
-                       if (stat(ep, &stb)) {
-                               xfree(ep);
-                               return ("0");
-                       }
-                       switch (cp[1]) {
-
-                       case 'f':
-                               i = (stb.st_mode & S_IFMT) == S_IFREG;
-                               break;
-
-                       case 'd':
-                               i = (stb.st_mode & S_IFMT) == S_IFDIR;
-                               break;
-
-                       case 'z':
-                               i = stb.st_size == 0;
-                               break;
-
-                       case 'e':
-                               i = 1;
-                               break;
-
-                       case 'o':
-                               i = stb.st_uid == uid;
-                               break;
-                       }
-               }
 #ifdef EDEBUG
 #ifdef EDEBUG
-               etraci("exp6 -? i", i, vp);
+       etraci("exp6 -? i", i, vp);
 #endif
 #endif
-               xfree(ep);
-               return (putn(i));
-       }
+       xfree((ptr_t) ep);
+       return (putn(i));
+    }
 #ifdef EDEBUG
 #ifdef EDEBUG
-       etracc("exp6 default", cp, vp);
+    etracc("exp6 default", cp, vp);
 #endif
 #endif
-       return (ignore&NOGLOB ? savestr(cp) : globone(cp));
+    return (ignore & NOGLOB ? Strsave(cp) : globone(cp, G_ERROR));
 }
 
 }
 
+static void
 evalav(v)
 evalav(v)
-       register char **v;
+    register Char **v;
 {
 {
-       struct wordent paraml;
-       register struct wordent *hp = &paraml;
-       struct command *t;
-       register struct wordent *wdp = hp;
-       
-       set("status", "0");
-       hp->prev = hp->next = hp;
-       hp->word = "";
-       while (*v) {
-               register struct wordent *new = (struct wordent *) calloc(1, sizeof *wdp);
-
-               new->prev = wdp;
-               new->next = hp;
-               wdp->next = new;
-               wdp = new;
-               wdp->word = savestr(*v++);
-       }
-       hp->prev = wdp;
-       alias(&paraml);
-       t = syntax(paraml.next, &paraml, 0);
-       if (err)
-               error(err);
-       execute(t, -1);
-       freelex(&paraml), freesyn(t);
+    struct wordent paraml1;
+    register struct wordent *hp = &paraml1;
+    struct command *t;
+    register struct wordent *wdp = hp;
+
+    set(STRstatus, Strsave(STR0));
+    hp->prev = hp->next = hp;
+    hp->word = STRNULL;
+    while (*v) {
+       register struct wordent *new =
+       (struct wordent *) xcalloc(1, sizeof *wdp);
+
+       new->prev = wdp;
+       new->next = hp;
+       wdp->next = new;
+       wdp = new;
+       wdp->word = Strsave(*v++);
+    }
+    hp->prev = wdp;
+    alias(&paraml1);
+    t = syntax(paraml1.next, &paraml1, 0);
+    if (seterr)
+       stderror(ERR_OLD);
+    execute(t, -1, NULL, NULL);
+    freelex(&paraml1), freesyn(t);
 }
 
 }
 
+static int
 isa(cp, what)
 isa(cp, what)
-       register char *cp;
-       register int what;
+    register Char *cp;
+    register int what;
 {
 {
-
-       if (cp == 0)
-               return ((what & RESTOP) != 0);
-       if (cp[1] == 0) {
-               if (what & ADDOP && (*cp == '+' || *cp == '-'))
-                       return (1);
-               if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
-                       return (1);
-               if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
-                                     *cp == '~' || *cp == '^' || *cp == '"'))
-                       return (1);
-       } else if (cp[2] == 0) {
-               if (what & RESTOP) {
-                       if (cp[0] == '|' && cp[1] == '&')
-                               return (1);
-                       if (cp[0] == '<' && cp[1] == '<')
-                               return (1);
-                       if (cp[0] == '>' && cp[1] == '>')
-                               return (1);
-               }
-               if (what & EQOP) {
-                       if (cp[0] == '=') {
-                               if (cp[1] == '=')
-                                       return (EQEQ);
-                               if (cp[1] == '~')
-                                       return (EQMATCH);
-                       } else if (cp[0] == '!') {
-                               if (cp[1] == '=')
-                                       return (NOTEQ);
-                               if (cp[1] == '~')
-                                       return (NOTEQMATCH);
-                       }
-               }
+    if (cp == 0)
+       return ((what & RESTOP) != 0);
+    if (cp[1] == 0) {
+       if (what & ADDOP && (*cp == '+' || *cp == '-'))
+           return (1);
+       if (what & MULOP && (*cp == '*' || *cp == '/' || *cp == '%'))
+           return (1);
+       if (what & RESTOP && (*cp == '(' || *cp == ')' || *cp == '!' ||
+                             *cp == '~' || *cp == '^' || *cp == '"'))
+           return (1);
+    }
+    else if (cp[2] == 0) {
+       if (what & RESTOP) {
+           if (cp[0] == '|' && cp[1] == '&')
+               return (1);
+           if (cp[0] == '<' && cp[1] == '<')
+               return (1);
+           if (cp[0] == '>' && cp[1] == '>')
+               return (1);
        }
        }
-       if (what & RELOP) {
-               if (*cp == '<')
-                       return (LSS);
-               if (*cp == '>')
-                       return (GTR);
+       if (what & EQOP) {
+           if (cp[0] == '=') {
+               if (cp[1] == '=')
+                   return (EQEQ);
+               if (cp[1] == '~')
+                   return (EQMATCH);
+           }
+           else if (cp[0] == '!') {
+               if (cp[1] == '=')
+                   return (NOTEQ);
+               if (cp[1] == '~')
+                   return (NOTEQMATCH);
+           }
        }
        }
-       return (0);
+    }
+    if (what & RELOP) {
+       if (*cp == '<')
+           return (LSS);
+       if (*cp == '>')
+           return (GTR);
+    }
+    return (0);
 }
 
 }
 
+static int
 egetn(cp)
 egetn(cp)
-       register char *cp;
+    register Char *cp;
 {
 {
-
-       if (*cp && *cp != '-' && !digit(*cp))
-               bferr("Expression syntax");
-       return (getn(cp));
+    if (*cp && *cp != '-' && !Isdigit(*cp))
+       stderror(ERR_NAME | ERR_EXPRESSION);
+    return (getn(cp));
 }
 
 /* Phew! */
 
 #ifdef EDEBUG
 }
 
 /* Phew! */
 
 #ifdef EDEBUG
+static void
 etraci(str, i, vp)
 etraci(str, i, vp)
-       char *str;
-       int i;
-       char ***vp;
+    char   *str;
+    int     i;
+    Char ***vp;
 {
 {
-
-       printf("%s=%d\t", str, i);
-       blkpr(*vp);
-       printf("\n");
+    xprintf("%s=%d\t", str, i);
+    blkpr(*vp);
+    xprintf("\n");
 }
 }
-
+static void
 etracc(str, cp, vp)
 etracc(str, cp, vp)
-       char *str, *cp;
-       char ***vp;
+    char   *str;
+    Char   *cp;
+    Char ***vp;
 {
 {
-
-       printf("%s=%s\t", str, cp);
-       blkpr(*vp);
-       printf("\n");
+    xprintf("%s=%s\t", str, cp);
+    blkpr(*vp);
+    xprintf("\n");
 }
 #endif
 }
 #endif