fixed bug in realloc: copy the min size of the new and old block.
[unix-history] / usr / src / bin / csh / lex.c
index 5512be6..dcafbe0 100644 (file)
@@ -6,15 +6,24 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)lex.c      5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)lex.c      5.26 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
-#include "sh.h"
-#include <sgtty.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#if __STDC__
+# include <stdarg.h>
+#else
+# include <varargs.h>
+#endif
 
 
-/*
- * C shell
- */
+#include "csh.h"
+#include "extern.h"
 
 /*
  * These lexical routines read input and form lists of words.
 
 /*
  * These lexical routines read input and form lists of words.
@@ -22,10 +31,27 @@ static char sccsid[] = "@(#)lex.c   5.10 (Berkeley) %G%";
  * of input buffering, and especially because of history substitution.
  */
 
  * of input buffering, and especially because of history substitution.
  */
 
-char   *word();
+static Char    *word __P((void));
+static int      getC1 __P((int));
+static void     getdol __P((void));
+static void     getexcl __P((int));
+static struct Hist
+               *findev __P((Char *, bool));
+static void     setexclp __P((Char *));
+static int      bgetc __P((void));
+static void     bfree __P((void));
+static struct wordent
+               *gethent __P((int));
+static int      matchs __P((Char *, Char *));
+static int      getsel __P((int *, int *, int));
+static struct wordent
+               *getsub __P((struct wordent *));
+static Char    *subword __P((Char *, int, bool *));
+static struct wordent
+               *dosub __P((int, struct wordent *, bool));
 
 /*
 
 /*
- * Peekc is a peek characer for getC, peekread for readc.
+ * Peekc is a peek character for getC, peekread for readc.
  * There is a subtlety here in many places... history routines
  * will read ahead and then insert stuff into the input stream.
  * If they push back a character then they must push it behind
  * There is a subtlety here in many places... history routines
  * will read ahead and then insert stuff into the input stream.
  * If they push back a character then they must push it behind
@@ -41,13 +67,30 @@ char        *word();
  * Getdol invokes history substitution, hence the extra peek, peekd,
  * which it can ungetD to be before history substitutions.
  */
  * Getdol invokes history substitution, hence the extra peek, peekd,
  * which it can ungetD to be before history substitutions.
  */
-char   peekc, peekd;
-char   peekread;
+static Char peekc = 0, peekd = 0;
+static Char peekread = 0;
+
+/* (Tail of) current word from ! subst */
+static Char *exclp = NULL;
+
+/* The rest of the ! subst words */
+static struct wordent *exclnxt = NULL;
 
 
-char   *exclp;                 /* (Tail of) current word from ! subst */
-struct wordent *exclnxt;       /* The rest of the ! subst words */
-int    exclc;                  /* Count of remainig words in ! subst */
-char   *alvecp;                /* "Globp" for alias resubstitution */
+/* Count of remaining words in ! subst */
+static int exclc = 0;
+
+/* "Globp" for alias resubstitution */
+Char *alvecp = NULL;
+int aret = F_SEEK;
+
+/*
+ * Labuf implements a general buffer for lookahead during lexical operations.
+ * Text which is to be placed in the input stream can be stuck here.
+ * We stick parsed ahead $ constructs during initial input,
+ * process id's from `$$', and modified variable values (from qualifiers
+ * during expansion in sh.dol.c) here.
+ */
+static Char labuf[BUFSIZ];
 
 /*
  * Lex returns to its caller not only a wordlist (as a "var" parameter)
 
 /*
  * Lex returns to its caller not only a wordlist (as a "var" parameter)
@@ -56,853 +99,1031 @@ char     *alvecp;                /* "Globp" for alias resubstitution */
  * when called by the alias routine to determine whether to keep the
  * argument list.
  */
  * when called by the alias routine to determine whether to keep the
  * argument list.
  */
-bool   hadhist;
+static bool hadhist = 0;
+
+/*
+ * Avoid alias expansion recursion via \!#
+ */
+int     hleft;
+
+static Char getCtmp;
 
 
-char getCtmp;
 #define getC(f)                ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
 #define        ungetC(c)       peekc = c
 #define        ungetD(c)       peekd = c
 
 #define getC(f)                ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
 #define        ungetC(c)       peekc = c
 #define        ungetD(c)       peekd = c
 
+int
 lex(hp)
 lex(hp)
-       register struct wordent *hp;
+    register struct wordent *hp;
 {
 {
-       register struct wordent *wdp;
-       int c;
-
-       lineloc = btell();
-       hp->next = hp->prev = hp;
-       hp->word = "";
-       alvecp = 0, hadhist = 0;
-       do
-               c = readc(0);
-       while (c == ' ' || c == '\t');
-       if (c == HISTSUB && intty)
-               /* ^lef^rit     from tty is short !:s^lef^rit */
-               getexcl(c);
-       else
-               unreadc(c);
-       wdp = hp;
-       /*
-        * The following loop is written so that the links needed
-        * by freelex will be ready and rarin to go even if it is
-        * interrupted.
-        */
-       do {
-               register struct wordent *new = (struct wordent *) xalloc(sizeof *wdp);
-
-               new->word = 0;
-               new->prev = wdp;
-               new->next = hp;
-               wdp->next = new;
-               wdp = new;
-               wdp->word = word();
-       } while (wdp->word[0] != '\n');
-       hp->prev = wdp;
-       return (hadhist);
+    register struct wordent *wdp;
+    int     c;
+
+    btell(&lineloc);
+    hp->next = hp->prev = hp;
+    hp->word = STRNULL;
+    hadhist = 0;
+    do
+       c = readc(0);
+    while (c == ' ' || c == '\t');
+    if (c == HISTSUB && intty)
+       /* ^lef^rit     from tty is short !:s^lef^rit */
+       getexcl(c);
+    else
+       unreadc(c);
+    wdp = hp;
+    /*
+     * The following loop is written so that the links needed by freelex will
+     * be ready and rarin to go even if it is interrupted.
+     */
+    do {
+       register struct wordent *new;
+
+       new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
+       new->word = 0;
+       new->prev = wdp;
+       new->next = hp;
+       wdp->next = new;
+       wdp = new;
+       wdp->word = word();
+    } while (wdp->word[0] != '\n');
+    hp->prev = wdp;
+    return (hadhist);
 }
 
 }
 
-prlex(sp0)
-       struct wordent *sp0;
+void
+prlex(fp, sp0)
+    FILE *fp;
+    struct wordent *sp0;
 {
 {
-       register struct wordent *sp = sp0->next;
-
-       for (;;) {
-               printf("%s", sp->word);
-               sp = sp->next;
-               if (sp == sp0)
-                       break;
-               if (sp->word[0] != '\n')
-                       cshputchar(' ');
-       }
+    register struct wordent *sp = sp0->next;
+
+    for (;;) {
+       (void) fprintf(fp, "%s", vis_str(sp->word));
+       sp = sp->next;
+       if (sp == sp0)
+           break;
+       if (sp->word[0] != '\n')
+           (void) fputc(' ', fp);
+    }
 }
 
 }
 
+void
 copylex(hp, fp)
 copylex(hp, fp)
-       register struct wordent *hp;
-       register struct wordent *fp;
+    register struct wordent *hp;
+    register struct wordent *fp;
 {
 {
-       register struct wordent *wdp;
-
-       wdp = hp;
+    register struct wordent *wdp;
+
+    wdp = hp;
+    fp = fp->next;
+    do {
+       register struct wordent *new;
+
+       new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
+       new->prev = wdp;
+       new->next = hp;
+       wdp->next = new;
+       wdp = new;
+       wdp->word = Strsave(fp->word);
        fp = fp->next;
        fp = fp->next;
-       do {
-               register struct wordent *new = (struct wordent *) xalloc(sizeof *wdp);
-
-               new->prev = wdp;
-               new->next = hp;
-               wdp->next = new;
-               wdp = new;
-               wdp->word = savestr(fp->word);
-               fp = fp->next;
-       } while (wdp->word[0] != '\n');
-       hp->prev = wdp;
+    } while (wdp->word[0] != '\n');
+    hp->prev = wdp;
 }
 
 }
 
+void
 freelex(vp)
 freelex(vp)
-       register struct wordent *vp;
+    register struct wordent *vp;
 {
 {
-       register struct wordent *fp;
-
-       while (vp->next != vp) {
-               fp = vp->next;
-               vp->next = fp->next;
-               XFREE(fp->word)
-               XFREE((char *)fp)
-       }
-       vp->prev = vp;
+    register struct wordent *fp;
+
+    while (vp->next != vp) {
+       fp = vp->next;
+       vp->next = fp->next;
+       xfree((ptr_t) fp->word);
+       xfree((ptr_t) fp);
+    }
+    vp->prev = vp;
 }
 
 }
 
-char *
+static Char *
 word()
 {
 word()
 {
-       register char c, c1;
-       register char *wp;
-       char wbuf[BUFSIZ];
-       register bool dolflg;
-       register int i;
-
-       wp = wbuf;
-       i = BUFSIZ - 4;
+    register Char c, c1;
+    register Char *wp;
+    Char    wbuf[BUFSIZ];
+    register bool dolflg;
+    register int i;
+
+    wp = wbuf;
+    i = BUFSIZ - 4;
 loop:
 loop:
-       while ((c = getC(DOALL)) == ' ' || c == '\t')
-               ;
-       if (cmap(c, _META|_ESC))
-               switch (c) {
-               case '&':
-               case '|':
-               case '<':
-               case '>':
-                       *wp++ = c;
-                       c1 = getC(DOALL);
-                       if (c1 == c)
-                               *wp++ = c1;
-                       else
-                               ungetC(c1);
-                       goto ret;
-
-               case '#':
-                       if (intty)
-                               break;
-                       c = 0;
-                       do {
-                               c1 = c;
-                               c = getC(0);
-                       } while (c != '\n');
-                       if (c1 == '\\')
-                               goto loop;
-                       /* fall into ... */
-
-               case ';':
-               case '(':
-               case ')':
-               case '\n':
-                       *wp++ = c;
-                       goto ret;
-
-               case '\\':
-                       c = getC(0);
-                       if (c == '\n') {
-                               if (onelflg == 1)
-                                       onelflg = 2;
-                               goto loop;
-                       }
-                       if (c != HIST)
-                               *wp++ = '\\', --i;
+    while ((c = getC(DOALL)) == ' ' || c == '\t')
+       continue;
+    if (cmap(c, _META | _ESC))
+       switch (c) {
+       case '&':
+       case '|':
+       case '<':
+       case '>':
+           *wp++ = c;
+           c1 = getC(DOALL);
+           if (c1 == c)
+               *wp++ = c1;
+           else
+               ungetC(c1);
+           goto ret;
+
+       case '#':
+           if (intty)
+               break;
+           c = 0;
+           do {
+               c1 = c;
+               c = getC(0);
+           } while (c != '\n');
+           if (c1 == '\\')
+               goto loop;
+           /* fall into ... */
+
+       case ';':
+       case '(':
+       case ')':
+       case '\n':
+           *wp++ = c;
+           goto ret;
+
+       case '\\':
+           c = getC(0);
+           if (c == '\n') {
+               if (onelflg == 1)
+                   onelflg = 2;
+               goto loop;
+           }
+           if (c != HIST)
+               *wp++ = '\\', --i;
+           c |= QUOTE;
+       }
+    c1 = 0;
+    dolflg = DOALL;
+    for (;;) {
+       if (c1) {
+           if (c == c1) {
+               c1 = 0;
+               dolflg = DOALL;
+           }
+           else if (c == '\\') {
+               c = getC(0);
+               if (c == HIST)
+                   c |= QUOTE;
+               else {
+                   if (c == '\n')
+                       /*
+                        * if (c1 == '`') c = ' '; else
+                        */
                        c |= QUOTE;
                        c |= QUOTE;
+                   ungetC(c);
+                   c = '\\';
                }
                }
-       c1 = 0;
-       dolflg = DOALL;
-       for (;;) {
-               if (c1) {
-                       if (c == c1) {
-                               c1 = 0;
-                               dolflg = DOALL;
-                       } else if (c == '\\') {
-                               c = getC(0);
-                               if (c == HIST)
-                                       c |= QUOTE;
-                               else {
-                                       if (c == '\n')
-                                               /*
-                                               if (c1 == '`')
-                                                       c = ' ';
-                                               else
-                                               */
-                                                       c |= QUOTE;
-                                       ungetC(c);
-                                       c = '\\';
-                               }
-                       } else if (c == '\n') {
-                               seterrc("Unmatched ", c1);
-                               ungetC(c);
-                               break;
-                       }
-               } else if (cmap(c, _META|_Q|_Q1|_ESC)) {
-                       if (c == '\\') {
-                               c = getC(0);
-                               if (c == '\n') {
-                                       if (onelflg == 1)
-                                               onelflg = 2;
-                                       break;
-                               }
-                               if (c != HIST)
-                                       *wp++ = '\\', --i;
-                               c |= QUOTE;
-                       } else if (cmap(c, _Q|_Q1)) {           /* '"` */
-                               c1 = c;
-                               dolflg = c == '"' ? DOALL : DOEXCL;
-                       } else if (c != '#' || !intty) {
-                               ungetC(c);
-                               break;
-                       }
-               }
-               if (--i > 0) {
-                       *wp++ = c;
-                       c = getC(dolflg);
-               } else {
-                       seterr("Word too long");
-                       wp = &wbuf[1];
-                       break;
+           }
+           else if (c == '\n') {
+               seterror(ERR_UNMATCHED, c1);
+               ungetC(c);
+               break;
+           }
+       }
+       else if (cmap(c, _META | _Q | _Q1 | _ESC)) {
+           if (c == '\\') {
+               c = getC(0);
+               if (c == '\n') {
+                   if (onelflg == 1)
+                       onelflg = 2;
+                   break;
                }
                }
+               if (c != HIST)
+                   *wp++ = '\\', --i;
+               c |= QUOTE;
+           }
+           else if (cmap(c, _Q | _Q1)) {       /* '"` */
+               c1 = c;
+               dolflg = c == '"' ? DOALL : DOEXCL;
+           }
+           else if (c != '#' || !intty) {
+               ungetC(c);
+               break;
+           }
+       }
+       if (--i > 0) {
+           *wp++ = c;
+           c = getC(dolflg);
+       }
+       else {
+           seterror(ERR_WTOOLONG);
+           wp = &wbuf[1];
+           break;
        }
        }
+    }
 ret:
 ret:
-       *wp = 0;
-       return (savestr(wbuf));
+    *wp = 0;
+    return (Strsave(wbuf));
 }
 
 }
 
+static int
 getC1(flag)
 getC1(flag)
-       register int flag;
+    register int flag;
 {
 {
-       register char c;
+    register Char c;
 
 
-top:
+    while (1) {
        if (c = peekc) {
        if (c = peekc) {
-               peekc = 0;
-               return (c);
+           peekc = 0;
+           return (c);
        }
        if (lap) {
        }
        if (lap) {
-               if ((c = *lap++) == 0)
-                       lap = 0;
-               else {
-                       if (cmap(c, _META|_Q|_Q1))
-                               c |= QUOTE;
-                       return (c);
-               }
+           if ((c = *lap++) == 0)
+               lap = 0;
+           else {
+               if (cmap(c, _META | _Q | _Q1))
+                   c |= QUOTE;
+               return (c);
+           }
        }
        if (c = peekd) {
        }
        if (c = peekd) {
-               peekd = 0;
-               return (c);
+           peekd = 0;
+           return (c);
        }
        if (exclp) {
        }
        if (exclp) {
-               if (c = *exclp++)
-                       return (c);
-               if (exclnxt && --exclc >= 0) {
-                       exclnxt = exclnxt->next;
-                       setexclp(exclnxt->word);
-                       return (' ');
-               }
-               exclp = 0;
-               exclnxt = 0;
+           if (c = *exclp++)
+               return (c);
+           if (exclnxt && --exclc >= 0) {
+               exclnxt = exclnxt->next;
+               setexclp(exclnxt->word);
+               return (' ');
+           }
+           exclp = 0;
+           exclnxt = 0;
        }
        if (exclnxt) {
        }
        if (exclnxt) {
-               exclnxt = exclnxt->next;
-               if (--exclc < 0)
-                       exclnxt = 0;
-               else
-                       setexclp(exclnxt->word);
-               goto top;
+           exclnxt = exclnxt->next;
+           if (--exclc < 0)
+               exclnxt = 0;
+           else
+               setexclp(exclnxt->word);
+           continue;
        }
        c = readc(0);
        if (c == '$' && (flag & DODOL)) {
        }
        c = readc(0);
        if (c == '$' && (flag & DODOL)) {
-               getdol();
-               goto top;
+           getdol();
+           continue;
        }
        if (c == HIST && (flag & DOEXCL)) {
        }
        if (c == HIST && (flag & DOEXCL)) {
-               getexcl(0);
-               goto top;
+           getexcl(0);
+           continue;
        }
        }
-       return (c);
+       break;
+    }
+    return (c);
 }
 
 }
 
+static void
 getdol()
 {
 getdol()
 {
-       register char *np;
-       char name[40];
-       register int c;
-       int sc;
-       bool special = 0;
-
-       np = name, *np++ = '$';
-       c = sc = getC(DOEXCL);
-       if (index("\t \n", c)) {
-               ungetD(c);
-               ungetC('$' | QUOTE);
+    register Char *np, *ep;
+    Char    name[4 * MAXVARLEN + 1];
+    register int c;
+    int     sc;
+    bool    special = 0, toolong;
+
+    np = name, *np++ = '$';
+    c = sc = getC(DOEXCL);
+    if (any("\t \n", c)) {
+       ungetD(c);
+       ungetC('$' | QUOTE);
+       return;
+    }
+    if (c == '{')
+       *np++ = c, c = getC(DOEXCL);
+    if (c == '#' || c == '?')
+       special++, *np++ = c, c = getC(DOEXCL);
+    *np++ = c;
+    switch (c) {
+
+    case '<':
+    case '$':
+    case '!':
+       if (special)
+           seterror(ERR_SPDOLLT);
+       *np = 0;
+       addla(name);
+       return;
+
+    case '\n':
+       ungetD(c);
+       np--;
+       seterror(ERR_NEWLINE);
+       *np = 0;
+       addla(name);
+       return;
+
+    case '*':
+       if (special)
+           seterror(ERR_SPSTAR);
+       *np = 0;
+       addla(name);
+       return;
+
+    default:
+       toolong = 0;
+       if (Isdigit(c)) {
+#ifdef notdef
+           /* let $?0 pass for now */
+           if (special) {
+               seterror(ERR_DIGIT);
+               *np = 0;
+               addla(name);
                return;
                return;
+           }
+#endif
+           /* we know that np < &name[4] */
+           ep = &np[MAXVARLEN];
+           while (c = getC(DOEXCL)) {
+               if (!Isdigit(c))
+                   break;
+               if (np < ep)
+                   *np++ = c;
+               else
+                   toolong = 1;
+           }
+       }
+       else if (letter(c)) {
+           /* we know that np < &name[4] */
+           ep = &np[MAXVARLEN];
+           toolong = 0;
+           while (c = getC(DOEXCL)) {
+               /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
+               if (!letter(c) && !Isdigit(c))
+                   break;
+               if (np < ep)
+                   *np++ = c;
+               else
+                   toolong = 1;
+           }
+       }
+       else {
+           *np = 0;
+           seterror(ERR_VARILL);
+           addla(name);
+           return;
        }
        }
-       if (c == '{')
-               *np++ = c, c = getC(DOEXCL);
-       if (c == '#' || c == '?')
-               special++, *np++ = c, c = getC(DOEXCL);
+       if (toolong) {
+           seterror(ERR_VARTOOLONG);
+           *np = 0;
+           addla(name);
+           return;
+       }
+       break;
+    }
+    if (c == '[') {
        *np++ = c;
        *np++ = c;
-       switch (c) {
-       
-       case '<':
-       case '$':
-               if (special)
-                       goto vsyn;
-               goto ret;
-
-       case '\n':
+       /*
+        * Name up to here is a max of MAXVARLEN + 8.
+        */
+       ep = &np[2 * MAXVARLEN + 8];
+       do {
+           /*
+            * Michael Greim: Allow $ expansion to take place in selector
+            * expressions. (limits the number of characters returned)
+            */
+           c = getC(DOEXCL | DODOL);
+           if (c == '\n') {
                ungetD(c);
                np--;
                ungetD(c);
                np--;
-               goto vsyn;
+               seterror(ERR_NLINDEX);
+               *np = 0;
+               addla(name);
+               return;
+           }
+           if (np < ep)
+               *np++ = c;
+       } while (c != ']');
+       *np = '\0';
+       if (np >= ep) {
+           seterror(ERR_SELOVFL);
+           addla(name);
+           return;
+       }
+       c = getC(DOEXCL);
+    }
+    /*
+     * Name up to here is a max of 2 * MAXVARLEN + 8.
+     */
+    if (c == ':') {
+       /*
+        * if the :g modifier is followed by a newline, then error right away!
+        * -strike
+        */
 
 
-       case '*':
-               if (special)
-                       goto vsyn;
-               goto ret;
+       int     gmodflag = 0, amodflag = 0;
 
 
-       default:
-               if (digit(c)) {
-/*
- * let $?0 pass for now
-                       if (special)
-                               goto vsyn;
-*/
-                       while (digit(c = getC(DOEXCL))) {
-                               if (np < &name[sizeof name / 2])
-                                       *np++ = c;
-                       }
-               } else if (letter(c))
-                       while (letter(c = getC(DOEXCL))) {
-                               if (np < &name[sizeof name / 2])
-                                       *np++ = c;
-                       }
+       do {
+           *np++ = c, c = getC(DOEXCL);
+           if (c == 'g' || c == 'a') {
+               if (c == 'g')
+                   gmodflag++;
                else
                else
-                       goto vsyn;
-       }
-       if (c == '[') {
-               *np++ = c;
-               do {
-                       c = getC(DOEXCL);
-                       if (c == '\n') {
-                               ungetD(c);
-                               np--;
-                               goto vsyn;
-                       }
-                       if (np >= &name[sizeof name - 8])
-                               goto vsyn;
-                       *np++ = c;
-               } while (c != ']');
-               c = getC(DOEXCL);
-       }
-       if (c == ':') {
-               *np++ = c, c = getC(DOEXCL);
+                   amodflag++;
+               *np++ = c; c = getC(DOEXCL);
+           }
+           if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
                if (c == 'g')
                if (c == 'g')
-                       *np++ = c, c = getC(DOEXCL);
-               *np++ = c;
-               if (!index("htrqxe", c))
-                       goto vsyn;
-       } else
-               ungetD(c);
-       if (sc == '{') {
-               c = getC(DOEXCL);
-               if (c != '}') {
-                       ungetC(c);
-                       goto vsyn;
+                   gmodflag++;
+               else
+                   amodflag++;
+               *np++ = c; c = getC(DOEXCL);
+           }
+           *np++ = c;
+           /* scan s// [eichin:19910926.0512EST] */
+           if (c == 's') {
+               int delimcnt = 2;
+               int delim = getC(0);
+               *np++ = delim;
+               
+               if (!delim || letter(delim)
+                   || Isdigit(delim) || any(" \t\n", delim)) {
+                   seterror(ERR_BADSUBST);
+                   break;
+               }       
+               while ((c = getC(0)) != (-1)) {
+                   *np++ = c;
+                   if(c == delim) delimcnt--;
+                   if(!delimcnt) break;
                }
                }
-               *np++ = c;
+               if(delimcnt) {
+                   seterror(ERR_BADSUBST);
+                   break;
+               }
+               c = 's';
+           }
+           if (!any("htrqxes", c)) {
+               if ((amodflag || gmodflag) && c == '\n')
+                   stderror(ERR_VARSYN);       /* strike */
+               seterror(ERR_VARMOD, c);
+               *np = 0;
+               addla(name);
+               return;
+           }
        }
        }
-ret:
-       *np = 0;
-       addla(name);
-       return;
-
-vsyn:
-       seterr("Variable syntax");
-       goto ret;
+       while ((c = getC(DOEXCL)) == ':');
+       ungetD(c);
+    }
+    else
+       ungetD(c);
+    if (sc == '{') {
+       c = getC(DOEXCL);
+       if (c != '}') {
+           ungetD(c);
+           seterror(ERR_MISSING, '}');
+           *np = 0;
+           addla(name);
+           return;
+       }
+       *np++ = c;
+    }
+    *np = 0;
+    addla(name);
+    return;
 }
 
 }
 
+void
 addla(cp)
 addla(cp)
-       char *cp;
+    Char   *cp;
 {
 {
-       char buf[BUFSIZ];
+    Char    buf[BUFSIZ];
 
 
-       if (strlen(cp) + (lap ? strlen(lap) : 0) >= sizeof (labuf) - 4) {
-               seterr("Expansion buffer overflow");
-               return;
-       }
-       if (lap)
-               (void) strcpy(buf, lap);
-       (void) strcpy(labuf, cp);
-       if (lap)
-               (void) strcat(labuf, buf);
-       lap = labuf;
+    if (Strlen(cp) + (lap ? Strlen(lap) : 0) >=
+       (sizeof(labuf) - 4) / sizeof(Char)) {
+       seterror(ERR_EXPOVFL);
+       return;
+    }
+    if (lap)
+       (void) Strcpy(buf, lap);
+    (void) Strcpy(labuf, cp);
+    if (lap)
+       (void) Strcat(labuf, buf);
+    lap = labuf;
 }
 
 }
 
-char   lhsb[32];
-char   slhs[32];
-char   rhsb[64];
-int    quesarg;
+static Char lhsb[32];
+static Char slhs[32];
+static Char rhsb[64];
+static int quesarg;
 
 
+static void
 getexcl(sc)
 getexcl(sc)
-       char sc;
+    int    sc;
 {
 {
-       register struct wordent *hp, *ip;
-       int left, right, dol;
-       register int c;
-
-       if (sc == 0) {
-               sc = getC(0);
-               if (sc != '{') {
-                       ungetC(sc);
-                       sc = 0;
-               }
-       }
-       quesarg = -1;
-       lastev = eventno;
-       hp = gethent(sc);
-       if (hp == 0)
-               return;
-       hadhist = 1;
-       dol = 0;
-       if (hp == alhistp)
-               for (ip = hp->next->next; ip != alhistt; ip = ip->next)
-                       dol++;
-       else
-               for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
-                       dol++;
-       left = 0, right = dol;
-       if (sc == HISTSUB) {
-               ungetC('s'), unreadc(HISTSUB), c = ':';
-               goto subst;
+    register struct wordent *hp, *ip;
+    int     left, right, dol;
+    register int c;
+
+    if (sc == 0) {
+       sc = getC(0);
+       if (sc != '{') {
+           ungetC(sc);
+           sc = 0;
        }
        }
+    }
+    quesarg = -1;
+    lastev = eventno;
+    hp = gethent(sc);
+    if (hp == 0)
+       return;
+    hadhist = 1;
+    dol = 0;
+    if (hp == alhistp)
+       for (ip = hp->next->next; ip != alhistt; ip = ip->next)
+           dol++;
+    else
+       for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
+           dol++;
+    left = 0, right = dol;
+    if (sc == HISTSUB) {
+       ungetC('s'), unreadc(HISTSUB), c = ':';
+       goto subst;
+    }
+    c = getC(0);
+    if (!any(":^$*-%", c))
+       goto subst;
+    left = right = -1;
+    if (c == ':') {
        c = getC(0);
        c = getC(0);
-       if (!index(":^$*-%", c))
-               goto subst;
-       left = right = -1;
-       if (c == ':') {
-               c = getC(0);
-               unreadc(c);
-               if (letter(c) || c == '&') {
-                       c = ':';
-                       left = 0, right = dol;
-                       goto subst;
-               }
-       } else
-               ungetC(c);
+       unreadc(c);
+       if (letter(c) || c == '&') {
+           c = ':';
+           left = 0, right = dol;
+           goto subst;
+       }
+    }
+    else
+       ungetC(c);
+    if (!getsel(&left, &right, dol))
+       return;
+    c = getC(0);
+    if (c == '*')
+       ungetC(c), c = '-';
+    if (c == '-') {
        if (!getsel(&left, &right, dol))
        if (!getsel(&left, &right, dol))
-               return;
+           return;
        c = getC(0);
        c = getC(0);
-       if (c == '*')
-               ungetC(c), c = '-';
-       if (c == '-') {
-               if (!getsel(&left, &right, dol))
-                       return;
-               c = getC(0);
-       }
+    }
 subst:
 subst:
-       exclc = right - left + 1;
-       while (--left >= 0)
-               hp = hp->next;
-       if (sc == HISTSUB || c == ':') {
-               do {
-                       hp = getsub(hp);
-                       c = getC(0);
-               } while (c == ':');
-       }
-       unreadc(c);
-       if (sc == '{') {
-               c = getC(0);
-               if (c != '}')
-                       seterr("Bad ! form");
-       }
-       exclnxt = hp;
+    exclc = right - left + 1;
+    while (--left >= 0)
+       hp = hp->next;
+    if (sc == HISTSUB || c == ':') {
+       do {
+           hp = getsub(hp);
+           c = getC(0);
+       } while (c == ':');
+    }
+    unreadc(c);
+    if (sc == '{') {
+       c = getC(0);
+       if (c != '}')
+           seterror(ERR_BADBANG);
+    }
+    exclnxt = hp;
 }
 
 }
 
-struct wordent *
+static struct wordent *
 getsub(en)
 getsub(en)
-       struct wordent *en;
+    struct wordent *en;
 {
 {
-       register char *cp;
-       int delim;
-       register int c;
-       int sc;
-       bool global = 0;
-       char orhsb[sizeof rhsb];
-
+    register Char *cp;
+    int     delim;
+    register int c;
+    int     sc;
+    bool global;
+    Char    orhsb[sizeof(rhsb) / sizeof(Char)];
+
+    do {
        exclnxt = 0;
        exclnxt = 0;
+       global = 0;
        sc = c = getC(0);
        sc = c = getC(0);
-       if (c == 'g')
-               global++, c = getC(0);
-       switch (c) {
+       if (c == 'g' || c == 'a') {
+           global |= (c == 'g') ? 1 : 2;
+           sc = c = getC(0);
+       }
+       if (((c =='g') && !(global & 1)) || ((c == 'a') && !(global & 2))) {
+           global |= (c == 'g') ? 1 : 2;
+           sc = c = getC(0);
+       }
 
 
+       switch (c) {
        case 'p':
        case 'p':
-               justpr++;
-               goto ret;
+           justpr++;
+           return (en);
 
        case 'x':
        case 'q':
 
        case 'x':
        case 'q':
-               global++;
-               /* fall into ... */
+           global |= 1;
+
+           /* fall into ... */
 
        case 'h':
        case 'r':
        case 't':
        case 'e':
 
        case 'h':
        case 'r':
        case 't':
        case 'e':
-               break;
+           break;
 
        case '&':
 
        case '&':
-               if (slhs[0] == 0) {
-                       seterr("No prev sub");
-                       goto ret;
-               }
-               (void) strcpy(lhsb, slhs);
-               break;
-
-/*
+           if (slhs[0] == 0) {
+               seterror(ERR_NOSUBST);
+               return (en);
+           }
+           (void) Strcpy(lhsb, slhs);
+           break;
+
+#ifdef notdef
        case '~':
        case '~':
-               if (lhsb[0] == 0)
-                       goto badlhs;
-               break;
-*/
+           if (lhsb[0] == 0)
+               goto badlhs;
+           break;
+#endif
 
        case 's':
 
        case 's':
-               delim = getC(0);
-               if (letter(delim) || digit(delim) || index(" \t\n", delim)) {
-                       unreadc(delim);
-bads:
-                       lhsb[0] = 0;
-                       seterr("Bad substitute");
-                       goto ret;
-               }
-               cp = lhsb;
-               for (;;) {
-                       c = getC(0);
-                       if (c == '\n') {
-                               unreadc(c);
-                               break;
-                       }
-                       if (c == delim)
-                               break;
-                       if (cp > &lhsb[sizeof lhsb - 2])
-                               goto bads;
-                       if (c == '\\') {
-                               c = getC(0);
-                               if (c != delim && c != '\\')
-                                       *cp++ = '\\';
-                       }
-                       *cp++ = c;
+           delim = getC(0);
+           if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
+               unreadc(delim);
+               lhsb[0] = 0;
+               seterror(ERR_BADSUBST);
+               return (en);
+           }
+           cp = lhsb;
+           for (;;) {
+               c = getC(0);
+               if (c == '\n') {
+                   unreadc(c);
+                   break;
                }
                }
-               if (cp != lhsb)
-                       *cp++ = 0;
-               else if (lhsb[0] == 0) {
-/*badlhs:*/
-                       seterr("No prev lhs");
-                       goto ret;
+               if (c == delim)
+                   break;
+               if (cp > &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) {
+                   lhsb[0] = 0;
+                   seterror(ERR_BADSUBST);
+                   return (en);
                }
                }
-               cp = rhsb;
-               (void) strcpy(orhsb, cp);
-               for (;;) {
-                       c = getC(0);
-                       if (c == '\n') {
-                               unreadc(c);
-                               break;
-                       }
-                       if (c == delim)
-                               break;
-/*
-                       if (c == '~') {
-                               if (&cp[strlen(orhsb)] > &rhsb[sizeof rhsb - 2])
-                                       goto toorhs;
-                               (void) strcpy(cp, orhsb);
-                               cp = strend(cp);
-                               continue;
-                       }
-*/
-                       if (cp > &rhsb[sizeof rhsb - 2]) {
-/*toorhs:*/
-                               seterr("Rhs too long");
-                               goto ret;
-                       }
-                       if (c == '\\') {
-                               c = getC(0);
-                               if (c != delim /* && c != '~' */)
-                                       *cp++ = '\\';
-                       }
-                       *cp++ = c;
+               if (c == '\\') {
+                   c = getC(0);
+                   if (c != delim && c != '\\')
+                       *cp++ = '\\';
                }
                }
+               *cp++ = c;
+           }
+           if (cp != lhsb)
                *cp++ = 0;
                *cp++ = 0;
-               break;
+           else if (lhsb[0] == 0) {
+               seterror(ERR_LHS);
+               return (en);
+           }
+           cp = rhsb;
+           (void) Strcpy(orhsb, cp);
+           for (;;) {
+               c = getC(0);
+               if (c == '\n') {
+                   unreadc(c);
+                   break;
+               }
+               if (c == delim)
+                   break;
+#ifdef notdef
+               if (c == '~') {
+                   if (&cp[Strlen(orhsb)] > &rhsb[sizeof(rhsb) /
+                                                  sizeof(Char) - 2])
+                       goto toorhs;
+                   (void) Strcpy(cp, orhsb);
+                   cp = Strend(cp);
+                   continue;
+               }
+#endif
+               if (cp > &rhsb[sizeof(rhsb) / sizeof(Char) - 2]) {
+                   seterror(ERR_RHSLONG);
+                   return (en);
+               }
+               if (c == '\\') {
+                   c = getC(0);
+                   if (c != delim /* && c != '~' */ )
+                       *cp++ = '\\';
+               }
+               *cp++ = c;
+           }
+           *cp++ = 0;
+           break;
 
        default:
 
        default:
-               if (c == '\n')
-                       unreadc(c);
-               seterrc("Bad ! modifier: ", c);
-               goto ret;
+           if (c == '\n')
+               unreadc(c);
+           seterror(ERR_BADBANGMOD, c);
+           return (en);
        }
        }
-       (void) strcpy(slhs, lhsb);
+       (void) Strcpy(slhs, lhsb);
        if (exclc)
        if (exclc)
-               en = dosub(sc, en, global);
-ret:
-       return (en);
+           en = dosub(sc, en, global);
+    }
+    while ((c = getC(0)) == ':');
+    unreadc(c);
+    return (en);
 }
 
 }
 
-struct wordent *
+static struct wordent *
 dosub(sc, en, global)
 dosub(sc, en, global)
-       int sc;
-       struct wordent *en;
-       bool global;
+    int     sc;
+    struct wordent *en;
+    bool global;
 {
 {
-       struct wordent lex;
-       bool didsub = 0;
-       struct wordent *hp = &lex;
-       register struct wordent *wdp;
-       register int i = exclc;
-
-       wdp = hp;
-       while (--i >= 0) {
-               register struct wordent *new = (struct wordent *) calloc(1, sizeof *wdp);
-
-               new->prev = wdp;
-               new->next = hp;
-               wdp->next = new;
-               wdp = new;
-               en = en->next;
-               wdp->word = global || didsub == 0 ?
-                   subword(en->word, sc, &didsub) : savestr(en->word);
+    struct wordent lexi;
+    bool    didsub = 0, didone = 0;
+    struct wordent *hp = &lexi;
+    register struct wordent *wdp;
+    register int i = exclc;
+
+    wdp = hp;
+    while (--i >= 0) {
+       register struct wordent *new = 
+               (struct wordent *) xcalloc(1, sizeof *wdp);
+
+       new->word = 0;
+       new->prev = wdp;
+       new->next = hp;
+       wdp->next = new;
+       wdp = new;
+       en = en->next;
+       if (en->word) {
+           Char *tword, *otword;
+
+           if ((global & 1) || didsub == 0) {
+               tword = subword(en->word, sc, &didone);
+               if (didone)
+                   didsub = 1;
+               if (global & 2) {
+                   while (didone && tword != STRNULL) {
+                       otword = tword;
+                       tword = subword(otword, sc, &didone);
+                       if (Strcmp(tword, otword) == 0) {
+                           xfree((ptr_t) otword);
+                           break;
+                       }
+                       else
+                           xfree((ptr_t) otword);
+                   }
+               }
+           }
+           else
+               tword = Strsave(en->word);
+           wdp->word = tword;
        }
        }
-       if (didsub == 0)
-               seterr("Modifier failed");
-       hp->prev = wdp;
-       return (&enthist(-1000, &lex, 0)->Hlex);
+    }
+    if (didsub == 0)
+       seterror(ERR_MODFAIL);
+    hp->prev = wdp;
+    return (&enthist(-1000, &lexi, 0)->Hlex);
 }
 
 }
 
-char *
+static Char *
 subword(cp, type, adid)
 subword(cp, type, adid)
-       char *cp;
-       int type;
-       bool *adid;
+    Char   *cp;
+    int     type;
+    bool   *adid;
 {
 {
-       char wbuf[BUFSIZ];
-       register char *wp, *mp, *np;
-       register int i;
-
-       switch (type) {
+    Char    wbuf[BUFSIZ];
+    register Char *wp, *mp, *np;
+    register int i;
+
+    *adid = 0;
+    switch (type) {
+
+    case 'r':
+    case 'e':
+    case 'h':
+    case 't':
+    case 'q':
+    case 'x':
+       wp = domod(cp, type);
+       if (wp == 0)
+           return (Strsave(cp));
+       *adid = 1;
+       return (wp);
+
+    default:
+       wp = wbuf;
+       i = BUFSIZ - 4;
+       for (mp = cp; *mp; mp++)
+           if (matchs(mp, lhsb)) {
+               for (np = cp; np < mp;)
+                   *wp++ = *np++, --i;
+               for (np = rhsb; *np; np++)
+                   switch (*np) {
+
+                   case '\\':
+                       if (np[1] == '&')
+                           np++;
+                       /* fall into ... */
 
 
-       case 'r':
-       case 'e':
-       case 'h':
-       case 't':
-       case 'q':
-       case 'x':
-               wp = domod(cp, type);
-               if (wp == 0)
-                       return (savestr(cp));
-               *adid = 1;
-               return (wp);
+                   default:
+                       if (--i < 0) {
+                           seterror(ERR_SUBOVFL);
+                           return (STRNULL);
+                       }
+                       *wp++ = *np;
+                       continue;
 
 
-       default:
-               wp = wbuf;
-               i = BUFSIZ - 4;
-               for (mp = cp; *mp; mp++)
-                       if (matchs(mp, lhsb)) {
-                               for (np = cp; np < mp;)
-                                       *wp++ = *np++, --i;
-                               for (np = rhsb; *np; np++) switch (*np) {
-
-                               case '\\':
-                                       if (np[1] == '&')
-                                               np++;
-                                       /* fall into ... */
-
-                               default:
-                                       if (--i < 0)
-                                               goto ovflo;
-                                       *wp++ = *np;
-                                       continue;
-
-                               case '&':
-                                       i -= strlen(lhsb);
-                                       if (i < 0)
-                                               goto ovflo;
-                                       *wp = 0;
-                                       (void) strcat(wp, lhsb);
-                                       wp = strend(wp);
-                                       continue;
-                               }
-                               mp += strlen(lhsb);
-                               i -= strlen(mp);
-                               if (i < 0) {
-ovflo:
-                                       seterr("Subst buf ovflo");
-                                       return ("");
-                               }
-                               *wp = 0;
-                               (void) strcat(wp, mp);
-                               *adid = 1;
-                               return (savestr(wbuf));
+                   case '&':
+                       i -= Strlen(lhsb);
+                       if (i < 0) {
+                           seterror(ERR_SUBOVFL);
+                           return (STRNULL);
                        }
                        }
-               return (savestr(cp));
-       }
+                       *wp = 0;
+                       (void) Strcat(wp, lhsb);
+                       wp = Strend(wp);
+                       continue;
+                   }
+               mp += Strlen(lhsb);
+               i -= Strlen(mp);
+               if (i < 0) {
+                   seterror(ERR_SUBOVFL);
+                   return (STRNULL);
+               }
+               *wp = 0;
+               (void) Strcat(wp, mp);
+               *adid = 1;
+               return (Strsave(wbuf));
+           }
+       return (Strsave(cp));
+    }
 }
 
 }
 
-char *
+Char   *
 domod(cp, type)
 domod(cp, type)
-       char *cp;
-       int type;
+    Char   *cp;
+    int     type;
 {
 {
-       register char *wp, *xp;
-       register int c;
-
-       switch (type) {
-
-       case 'x':
-       case 'q':
-               wp = savestr(cp);
-               for (xp = wp; c = *xp; xp++)
-                       if ((c != ' ' && c != '\t') || type == 'q')
-                               *xp |= QUOTE;
-               return (wp);
-
-       case 'h':
-       case 't':
-               if (!index(cp, '/'))
-                       return (type == 't' ? savestr(cp) : 0);
-               wp = strend(cp);
-               while (*--wp != '/')
-                       continue;
-               if (type == 'h')
-                       xp = savestr(cp), xp[wp - cp] = 0;
+    register Char *wp, *xp;
+    register int c;
+
+    switch (type) {
+
+    case 'x':
+    case 'q':
+       wp = Strsave(cp);
+       for (xp = wp; c = *xp; xp++)
+           if ((c != ' ' && c != '\t') || type == 'q')
+               *xp |= QUOTE;
+       return (wp);
+
+    case 'h':
+    case 't':
+       if (!any(short2str(cp), '/'))
+           return (type == 't' ? Strsave(cp) : 0);
+       wp = Strend(cp);
+       while (*--wp != '/')
+           continue;
+       if (type == 'h')
+           xp = Strsave(cp), xp[wp - cp] = 0;
+       else
+           xp = Strsave(wp + 1);
+       return (xp);
+
+    case 'e':
+    case 'r':
+       wp = Strend(cp);
+       for (wp--; wp >= cp && *wp != '/'; wp--)
+           if (*wp == '.') {
+               if (type == 'e')
+                   xp = Strsave(wp + 1);
                else
                else
-                       xp = savestr(wp + 1);
+                   xp = Strsave(cp), xp[wp - cp] = 0;
                return (xp);
                return (xp);
-
-       case 'e':
-       case 'r':
-               wp = strend(cp);
-               for (wp--; wp >= cp && *wp != '/'; wp--)
-                       if (*wp == '.') {
-                               if (type == 'e')
-                                       xp = savestr(wp + 1);
-                               else
-                                       xp = savestr(cp), xp[wp - cp] = 0;
-                               return (xp);
-                       }
-               return (savestr(type == 'e' ? "" : cp));
-       }
-       return (0);
+           }
+       return (Strsave(type == 'e' ? STRNULL : cp));
+    default:
+       break;
+    }
+    return (0);
 }
 
 }
 
+static int
 matchs(str, pat)
 matchs(str, pat)
-       register char *str, *pat;
+    register Char *str, *pat;
 {
 {
-
-       while (*str && *pat && *str == *pat)
-               str++, pat++;
-       return (*pat == 0);
+    while (*str && *pat && *str == *pat)
+       str++, pat++;
+    return (*pat == 0);
 }
 
 }
 
+static int
 getsel(al, ar, dol)
 getsel(al, ar, dol)
-       register int *al, *ar;
-       int dol;
+    register int *al, *ar;
+    int     dol;
 {
 {
-       register int c = getC(0);
-       register int i;
-       bool first = *al < 0;
-
-       switch (c) {
+    register int c = getC(0);
+    register int i;
+    bool    first = *al < 0;
 
 
-       case '%':
-               if (quesarg == -1)
-                       goto bad;
-               if (*al < 0)
-                       *al = quesarg;
-               *ar = quesarg;
-               break;
+    switch (c) {
 
 
-       case '-':
-               if (*al < 0) {
-                       *al = 0;
-                       *ar = dol - 1;
-                       unreadc(c);
-               }
-               return (1);
-
-       case '^':
-               if (*al < 0)
-                       *al = 1;
-               *ar = 1;
-               break;
-
-       case '$':
-               if (*al < 0)
-                       *al = dol;
-               *ar = dol;
-               break;
-
-       case '*':
-               if (*al < 0)
-                       *al = 1;
-               *ar = dol;
-               if (*ar < *al) {
-                       *ar = 0;
-                       *al = 1;
-                       return (1);
-               }
-               break;
+    case '%':
+       if (quesarg == -1) {
+           seterror(ERR_BADBANGARG);
+           return (0);
+       }
+       if (*al < 0)
+           *al = quesarg;
+       *ar = quesarg;
+       break;
+
+    case '-':
+       if (*al < 0) {
+           *al = 0;
+           *ar = dol - 1;
+           unreadc(c);
+       }
+       return (1);
 
 
-       default:
-               if (digit(c)) {
-                       i = 0;
-                       while (digit(c)) {
-                               i = i * 10 + c - '0';
-                               c = getC(0);
-                       }
-                       if (i < 0)
-                               i = dol + 1;
-                       if (*al < 0)
-                               *al = i;
-                       *ar = i;
-               } else
-                       if (*al < 0)
-                               *al = 0, *ar = dol;
-                       else
-                               *ar = dol - 1;
-               unreadc(c);
-               break;
+    case '^':
+       if (*al < 0)
+           *al = 1;
+       *ar = 1;
+       break;
+
+    case '$':
+       if (*al < 0)
+           *al = dol;
+       *ar = dol;
+       break;
+
+    case '*':
+       if (*al < 0)
+           *al = 1;
+       *ar = dol;
+       if (*ar < *al) {
+           *ar = 0;
+           *al = 1;
+           return (1);
        }
        }
-       if (first) {
+       break;
+
+    default:
+       if (Isdigit(c)) {
+           i = 0;
+           while (Isdigit(c)) {
+               i = i * 10 + c - '0';
                c = getC(0);
                c = getC(0);
-               unreadc(c);
-               if (index("-$*", c))
-                       return (1);
+           }
+           if (i < 0)
+               i = dol + 1;
+           if (*al < 0)
+               *al = i;
+           *ar = i;
        }
        }
-       if (*al > *ar || *ar > dol) {
-bad:
-               seterr("Bad ! arg selector");
-               return (0);
-       }
-       return (1);
+       else if (*al < 0)
+           *al = 0, *ar = dol;
+       else
+           *ar = dol - 1;
+       unreadc(c);
+       break;
+    }
+    if (first) {
+       c = getC(0);
+       unreadc(c);
+       if (any("-$*", c))
+           return (1);
+    }
+    if (*al > *ar || *ar > dol) {
+       seterror(ERR_BADBANGARG);
+       return (0);
+    }
+    return (1);
 
 }
 
 
 }
 
-struct wordent *
+static struct wordent *
 gethent(sc)
 gethent(sc)
-       int sc;
+    int     sc;
 {
 {
-       register struct Hist *hp;
-       register char *np;
-       register int c;
-       int event;
-       bool back = 0;
-
-       c = sc == HISTSUB ? HIST : getC(0);
-       if (c == HIST) {
-               if (alhistp)
-                       return (alhistp);
-               event = eventno;
-               goto skip;
-       }
+    register struct Hist *hp;
+    register Char *np;
+    register int c;
+    int     event;
+    bool    back = 0;
+
+    c = sc == HISTSUB ? HIST : getC(0);
+    if (c == HIST) {
+       if (alhistp)
+           return (alhistp);
+       event = eventno;
+    }
+    else
        switch (c) {
 
        case ':':
        switch (c) {
 
        case ':':
@@ -910,438 +1131,483 @@ gethent(sc)
        case '$':
        case '*':
        case '%':
        case '$':
        case '*':
        case '%':
-               ungetC(c);
-               if (lastev == eventno && alhistp)
-                       return (alhistp);
-               event = lastev;
-               break;
+           ungetC(c);
+           if (lastev == eventno && alhistp)
+               return (alhistp);
+           event = lastev;
+           break;
+
+       case '#':               /* !# is command being typed in (mrh) */
+           if (--hleft == 0) {
+               seterror(ERR_HISTLOOP);
+               return (0);
+           }
+           else
+               return (&paraml);
+           /* NOTREACHED */
 
        case '-':
 
        case '-':
-               back = 1;
-               c = getC(0);
-               goto number;
-
-       case '#':                       /* !# is command being typed in (mrh) */
-               return(&paraml);
+           back = 1;
+           c = getC(0);
+           /* FALLSTHROUGH */
 
        default:
 
        default:
-               if (index("(=~", c)) {
-                       unreadc(c);
-                       ungetC(HIST);
-                       return (0);
-               }
-               if (digit(c))
-                       goto number;
-               np = lhsb;
-               while (!index(": \t\\\n}", c)) {
-                       if (np < &lhsb[sizeof lhsb - 2])
-                               *np++ = c;
-                       c = getC(0);
-               }
+           if (any("(=~", c)) {
                unreadc(c);
                unreadc(c);
-               if (np == lhsb) {
-                       ungetC(HIST);
-                       return (0);
-               }
-               *np++ = 0;
-               hp = findev(lhsb, 0);
-               if (hp)
-                       lastev = hp->Hnum;
-               return (&hp->Hlex);
+               ungetC(HIST);
+               return (0);
+           }
+           np = lhsb;
+           event = 0;
+           while (!cmap(c, _ESC | _META | _Q | _Q1) && !any("${}:", c)) {
+               if (event != -1 && Isdigit(c))
+                   event = event * 10 + c - '0';
+               else
+                   event = -1;
+               if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
+                   *np++ = c;
+               c = getC(0);
+           }
+           unreadc(c);
+           if (np == lhsb) {
+               ungetC(HIST);
+               return (0);
+           }
+           *np++ = 0;
+           if (event != -1) {
+               /*
+                * History had only digits
+                */
+               if (back)
+                   event = eventno + (alhistp == 0) - (event ? event : 0);
+               break;
+           }
+           hp = findev(lhsb, 0);
+           if (hp)
+               lastev = hp->Hnum;
+           return (&hp->Hlex);
 
        case '?':
 
        case '?':
-               np = lhsb;
-               for (;;) {
-                       c = getC(0);
-                       if (c == '\n') {
-                               unreadc(c);
-                               break;
-                       }
-                       if (c == '?')
-                               break;
-                       if (np < &lhsb[sizeof lhsb - 2])
-                               *np++ = c;
+           np = lhsb;
+           for (;;) {
+               c = getC(0);
+               if (c == '\n') {
+                   unreadc(c);
+                   break;
                }
                }
-               if (np == lhsb) {
-                       if (lhsb[0] == 0) {
-                               seterr("No prev search");
-                               return (0);
-                       }
-               } else
-                       *np++ = 0;
-               hp = findev(lhsb, 1);
-               if (hp)
-                       lastev = hp->Hnum;
-               return (&hp->Hlex);
-
-       number:
-               event = 0;
-               while (digit(c)) {
-                       event = event * 10 + c - '0';
-                       c = getC(0);
+               if (c == '?')
+                   break;
+               if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
+                   *np++ = c;
+           }
+           if (np == lhsb) {
+               if (lhsb[0] == 0) {
+                   seterror(ERR_NOSEARCH);
+                   return (0);
                }
                }
-               if (back)
-                       event = eventno + (alhistp == 0) - (event ? event : 0);
-               unreadc(c);
-               break;
+           }
+           else
+               *np++ = 0;
+           hp = findev(lhsb, 1);
+           if (hp)
+               lastev = hp->Hnum;
+           return (&hp->Hlex);
        }
        }
-skip:
-       for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
-               if (hp->Hnum == event) {
-                       hp->Href = eventno;
-                       lastev = hp->Hnum;
-                       return (&hp->Hlex);
-               }
-       np = putn(event);
-       noev(np);
-       return (0);
+
+    for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
+       if (hp->Hnum == event) {
+           hp->Href = eventno;
+           lastev = hp->Hnum;
+           return (&hp->Hlex);
+       }
+    np = putn(event);
+    seterror(ERR_NOEVENT, vis_str(np));
+    return (0);
 }
 
 }
 
-struct Hist *
+static struct Hist *
 findev(cp, anyarg)
 findev(cp, anyarg)
-       char *cp;
-       bool anyarg;
+    Char   *cp;
+    bool    anyarg;
 {
 {
-       register struct Hist *hp;
+    register struct Hist *hp;
 
 
-       for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
-               char *dp;
-               register char *p, *q;
-               register struct wordent *lp = hp->Hlex.next;
-               int argno = 0;
+    for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
+       Char   *dp;
+       register Char *p, *q;
+       register struct wordent *lp = hp->Hlex.next;
+       int     argno = 0;
 
 
-               /*
-                * The entries added by alias substitution don't
-                * have a newline but do have a negative event number.
-                * Savehist() trims off these entries, but it happens
-                * before alias expansion, too early to delete those
-                * from the previous command.
-                */
-               if (hp->Hnum < 0)
-                       continue;
-               if (lp->word[0] == '\n')
-                       continue;
-               if (!anyarg) {
-                       p = cp;
-                       q = lp->word;
-                       do
-                               if (!*p)
-                                       return (hp);
-                       while (*p++ == *q++);
-                       continue;
-               }
-               do {
-                       for (dp = lp->word; *dp; dp++) {
-                               p = cp;
-                               q = dp;
-                               do
-                                       if (!*p) {
-                                               quesarg = argno;
-                                               return (hp);
-                                       }
-                               while (*p++ == *q++);
-                       }
-                       lp = lp->next;
-                       argno++;
-               } while (lp->word[0] != '\n');
+       /*
+        * The entries added by alias substitution don't have a newline but do
+        * have a negative event number. Savehist() trims off these entries,
+        * but it happens before alias expansion, too early to delete those
+        * from the previous command.
+        */
+       if (hp->Hnum < 0)
+           continue;
+       if (lp->word[0] == '\n')
+           continue;
+       if (!anyarg) {
+           p = cp;
+           q = lp->word;
+           do
+               if (!*p)
+                   return (hp);
+           while (*p++ == *q++);
+           continue;
        }
        }
-       noev(cp);
-       return (0);
+       do {
+           for (dp = lp->word; *dp; dp++) {
+               p = cp;
+               q = dp;
+               do
+                   if (!*p) {
+                       quesarg = argno;
+                       return (hp);
+                   }
+               while (*p++ == *q++);
+           }
+           lp = lp->next;
+           argno++;
+       } while (lp->word[0] != '\n');
+    }
+    seterror(ERR_NOEVENT, vis_str(cp));
+    return (0);
 }
 
 }
 
-noev(cp)
-       char *cp;
-{
-
-       seterr2(cp, ": Event not found");
-}
 
 
+static void
 setexclp(cp)
 setexclp(cp)
-       register char *cp;
+    register Char *cp;
 {
 {
-
-       if (cp && cp[0] == '\n')
-               return;
-       exclp = cp;
+    if (cp && cp[0] == '\n')
+       return;
+    exclp = cp;
 }
 
 }
 
+void
 unreadc(c)
 unreadc(c)
-       char c;
+    int    c;
 {
 {
-
-       peekread = c;
+    peekread = c;
 }
 
 }
 
+int
 readc(wanteof)
 readc(wanteof)
-       bool wanteof;
+    bool    wanteof;
 {
 {
-       register int c;
-       static sincereal;
+    register int c;
+    static  sincereal;
 
 
-       if (c = peekread) {
-               peekread = 0;
-               return (c);
-       }
+    aret = F_SEEK;
+    if (c = peekread) {
+       peekread = 0;
+       return (c);
+    }
 top:
 top:
-       if (alvecp) {
-               if (c = *alvecp++)
-                       return (c);
-               if (*alvec) {
-                       alvecp = *alvec++;
-                       return (' ');
-               }
+    aret = F_SEEK;
+    if (alvecp) {
+       aret = A_SEEK;
+       if (c = *alvecp++)
+           return (c);
+       if (alvec && *alvec) {
+               alvecp = *alvec++;
+               return (' ');
        }
        }
-       if (alvec) {
-               if (alvecp = *alvec) {
-                       alvec++;
-                       goto top;
-               }
-               /* Infinite source! */
-               return ('\n');
+       else {
+           aret = F_SEEK;
+           alvecp = NULL;
+           return('\n');
        }
        }
-       if (evalp) {
-               if (c = *evalp++)
-                       return (c);
-               if (*evalvec) {
-                       evalp = *evalvec++;
-                       return (' ');
-               }
-               evalp = 0;
+    }
+    if (alvec) {
+       if (alvecp = *alvec) {
+           alvec++;
+           goto top;
        }
        }
-       if (evalvec) {
-               if (evalvec == (char **)1) {
-                       doneinp = 1;
-                       longjmp(reslab, 0);
-               }
-               if (evalp = *evalvec) {
-                       evalvec++;
-                       goto top;
-               }
-               evalvec = (char **)1;
+       /* Infinite source! */
+       return ('\n');
+    }
+    if (evalp) {
+       aret = E_SEEK;
+       if (c = *evalp++)
+           return (c);
+       if (evalvec && *evalvec) {
+           evalp = *evalvec++;
+           return (' ');
+       }
+       aret = F_SEEK;
+       evalp = 0;
+    }
+    if (evalvec) {
+       if (evalvec == (Char **) 1) {
+           doneinp = 1;
+           reset();
+       }
+       if (evalp = *evalvec) {
+           evalvec++;
+           goto top;
+       }
+       evalvec = (Char **) 1;
+       return ('\n');
+    }
+    do {
+       if (arginp == (Char *) 1 || onelflg == 1) {
+           if (wanteof)
+               return (-1);
+           exitstat();
+       }
+       if (arginp) {
+           if ((c = *arginp++) == 0) {
+               arginp = (Char *) 1;
                return ('\n');
                return ('\n');
+           }
+           return (c);
        }
        }
-       do {
-               if (arginp == (char *) 1 || onelflg == 1) {
-                       if (wanteof)
-                               return (-1);
-                       exitstat();
-               }
-               if (arginp) {
-                       if ((c = *arginp++) == 0) {
-                               arginp = (char *) 1;
-                               return ('\n');
-                       }
-                       return (c);
-               }
 reread:
 reread:
-               c = bgetc();
-               if (c < 0) {
-                       struct sgttyb tty;
-
-                       if (wanteof)
-                               return (-1);
-                       /* was isatty but raw with ignoreeof yields problems */
-                       if (ioctl(SHIN, TIOCGETP, (char *)&tty) == 0 &&
-                           (tty.sg_flags & RAW) == 0) {
-                               /* was 'short' for FILEC */
-                               int ctpgrp;
-
-                               if (++sincereal > 25)
-                                       goto oops;
-                               if (tpgrp != -1 &&
-                                   ioctl(FSHTTY, TIOCGPGRP, (char *)&ctpgrp) == 0 &&
-                                   tpgrp != ctpgrp) {
-                                       (void) ioctl(FSHTTY, TIOCSPGRP,
-                                               (char *)&tpgrp);
-                                       (void) killpg(ctpgrp, SIGHUP);
-printf("Reset tty pgrp from %d to %d\n", ctpgrp, tpgrp);
-                                       goto reread;
-                               }
-                               if (adrof("ignoreeof")) {
-                                       if (loginsh)
-                                               printf("\nUse \"logout\" to logout.\n");
-                                       else
-                                               printf("\nUse \"exit\" to leave csh.\n");
-                                       longjmp(reslab, 0);
-                               }
-                               if (chkstop == 0)
-                                       panystop(1);
-                       }
-oops:
-                       doneinp = 1;
-                       longjmp(reslab, 0);
+       c = bgetc();
+       if (c < 0) {
+           struct termios tty;
+           if (wanteof)
+               return (-1);
+           /* was isatty but raw with ignoreeof yields problems */
+           if (tcgetattr(SHIN, &tty) == 0 && (tty.c_lflag & ICANON))
+           {
+               /* was 'short' for FILEC */
+               int     ctpgrp;
+
+               if (++sincereal > 25)
+                   goto oops;
+               if (tpgrp != -1 &&
+                   (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
+                   tpgrp != ctpgrp) {
+                   (void) tcsetpgrp(FSHTTY, tpgrp);
+                   (void) killpg((pid_t) ctpgrp, SIGHUP);
+                   (void) fprintf(csherr, "Reset tty pgrp from %d to %d\n",
+                                  ctpgrp, tpgrp);
+                   goto reread;
                }
                }
-               sincereal = 0;
-               if (c == '\n' && onelflg)
-                       onelflg--;
-       } while (c == 0);
-       return (c);
+               if (adrof(STRignoreeof)) {
+                   if (loginsh)
+                       (void) fprintf(csherr,"\nUse \"logout\" to logout.\n");
+                   else
+                       (void) fprintf(csherr,"\nUse \"exit\" to leave csh.\n");
+                   reset();
+               }
+               if (chkstop == 0)
+                   panystop(1);
+           }
+    oops:
+           doneinp = 1;
+           reset();
+       }
+       sincereal = 0;
+       if (c == '\n' && onelflg)
+           onelflg--;
+    } while (c == 0);
+    return (c);
 }
 
 }
 
+static int
 bgetc()
 {
 bgetc()
 {
-       register int buf, off, c;
+    register int buf, off, c;
+
 #ifdef FILEC
 #ifdef FILEC
-       char ttyline[BUFSIZ];
-       register int numleft = 0, roomleft;
+    register int numleft = 0, roomleft;
+    Char    ttyline[BUFSIZ];
 #endif
 #endif
+    char    tbuf[BUFSIZ + 1];
 
 
-#ifdef TELL
-       if (cantell) {
-               if (fseekp < fbobp || fseekp > feobp) {
-                       fbobp = feobp = fseekp;
-                       (void) lseek(SHIN, fseekp, 0);
-               }
-               if (fseekp == feobp) {
-                       fbobp = feobp;
-                       do
-                               c = read(SHIN, fbuf[0], BUFSIZ);
-                       while (c < 0 && errno == EINTR);
-                       if (c <= 0)
-                               return (-1);
-                       feobp += c;
-               }
-               c = fbuf[0][fseekp - fbobp];
-               fseekp++;
-               return (c);
+    if (cantell) {
+       if (fseekp < fbobp || fseekp > feobp) {
+           fbobp = feobp = fseekp;
+           (void) lseek(SHIN, fseekp, L_SET);
        }
        }
-#endif
+       if (fseekp == feobp) {
+           int     i;
+
+           fbobp = feobp;
+           do
+               c = read(SHIN, tbuf, BUFSIZ);
+           while (c < 0 && errno == EINTR);
+           if (c <= 0)
+               return (-1);
+           for (i = 0; i < c; i++)
+               fbuf[0][i] = (unsigned char) tbuf[i];
+           feobp += c;
+       }
+       c = fbuf[0][fseekp - fbobp];
+       fseekp++;
+       return (c);
+    }
+
 again:
 again:
-       buf = (int) fseekp / BUFSIZ;
-       if (buf >= fblocks) {
-               register char **nfbuf =
-                       (char **) calloc((unsigned) (fblocks + 2),
-                               sizeof (char **));
-
-               if (fbuf) {
-                       (void) blkcpy(nfbuf, fbuf);
-                       xfree((char *)fbuf);
-               }
-               fbuf = nfbuf;
-               fbuf[fblocks] = calloc(BUFSIZ, sizeof (char));
-               fblocks++;
-               goto again;
+    buf = (int) fseekp / BUFSIZ;
+    if (buf >= fblocks) {
+       register Char **nfbuf =
+       (Char **) xcalloc((size_t) (fblocks + 2),
+                         sizeof(Char **));
+
+       if (fbuf) {
+           (void) blkcpy(nfbuf, fbuf);
+           xfree((ptr_t) fbuf);
        }
        }
-       if (fseekp >= feobp) {
-               buf = (int) feobp / BUFSIZ;
-               off = (int) feobp % BUFSIZ;
-#ifndef FILEC
-               for (;;) {
-                       c = read(SHIN, fbuf[buf] + off, BUFSIZ - off);
-#else
-               roomleft = BUFSIZ - off;
-               for (;;) {
-                       if (filec && intty) {
-                               c = numleft ? numleft : tenex(ttyline, BUFSIZ);
-                               if (c > roomleft) {
-                                       /* start with fresh buffer */
-                                       feobp = fseekp = fblocks * BUFSIZ;
-                                       numleft = c;
-                                       goto again;
-                               }
-                               if (c > 0)
-                                       bcopy(ttyline, fbuf[buf] + off, c);
-                               numleft = 0;
-                       } else
-                               c = read(SHIN, fbuf[buf] + off, roomleft);
+       fbuf = nfbuf;
+       fbuf[fblocks] = (Char *) xcalloc(BUFSIZ, sizeof(Char));
+       fblocks++;
+       if (!intty)
+           goto again;
+    }
+    if (fseekp >= feobp) {
+       buf = (int) feobp / BUFSIZ;
+       off = (int) feobp % BUFSIZ;
+       roomleft = BUFSIZ - off;
+
+#ifdef FILEC
+       roomleft = BUFSIZ - off;
+       for (;;) {
+           if (filec && intty) {
+               c = numleft ? numleft : tenex(ttyline, BUFSIZ);
+               if (c > roomleft) {
+                   /* start with fresh buffer */
+                   feobp = fseekp = fblocks * BUFSIZ;
+                   numleft = c;
+                   goto again;
+               }
+               if (c > 0)
+                   bcopy(ttyline, fbuf[buf] + off, c * sizeof(Char));
+               numleft = 0;
+           }
+           else {
 #endif
 #endif
-                       if (c >= 0)
-                               break;
-                       if (errno == EWOULDBLOCK) {
-                               int off = 0;
-
-                               (void) ioctl(SHIN, FIONBIO, (char *)&off);
-                       } else if (errno != EINTR)
-                               break;
+               c = read(SHIN, tbuf, roomleft);
+               if (c > 0) {
+                   int     i;
+                   Char   *ptr = fbuf[buf] + off;
+
+                   for (i = 0; i < c; i++)
+                       ptr[i] = (unsigned char) tbuf[i];
                }
                }
-               if (c <= 0)
-                       return (-1);
-               feobp += c;
+#ifdef FILEC
+           }
+#endif
+           if (c >= 0)
+               break;
+           if (errno == EWOULDBLOCK) {
+               int     off = 0;
+
+               (void) ioctl(SHIN, FIONBIO, (ioctl_t) & off);
+           }
+           else if (errno != EINTR)
+               break;
+       }
+       if (c <= 0)
+           return (-1);
+       feobp += c;
 #ifndef FILEC
 #ifndef FILEC
-               goto again;
+       goto again;
 #else
 #else
-               if (filec && !intty)
-                       goto again;
+       if (filec && !intty)
+           goto again;
 #endif
 #endif
-       }
-       c = fbuf[buf][(int) fseekp % BUFSIZ];
-       fseekp++;
-       return (c);
+    }
+    c = fbuf[buf][(int) fseekp % BUFSIZ];
+    fseekp++;
+    return (c);
 }
 
 }
 
+static void
 bfree()
 {
 bfree()
 {
-       register int sb, i;
+    register int sb, i;
 
 
-#ifdef TELL
-       if (cantell)
-               return;
-#endif
-       if (whyles)
-               return;
-       sb = (int) (fseekp - 1) / BUFSIZ;
-       if (sb > 0) {
-               for (i = 0; i < sb; i++)
-                       xfree(fbuf[i]);
-               (void) blkcpy(fbuf, &fbuf[sb]);
-               fseekp -= BUFSIZ * sb;
-               feobp -= BUFSIZ * sb;
-               fblocks -= sb;
-       }
+    if (cantell)
+       return;
+    if (whyles)
+       return;
+    sb = (int) (fseekp - 1) / BUFSIZ;
+    if (sb > 0) {
+       for (i = 0; i < sb; i++)
+           xfree((ptr_t) fbuf[i]);
+       (void) blkcpy(fbuf, &fbuf[sb]);
+       fseekp -= BUFSIZ * sb;
+       feobp -= BUFSIZ * sb;
+       fblocks -= sb;
+    }
 }
 
 }
 
+void
 bseek(l)
 bseek(l)
-       off_t l;
+    struct Ain   *l;
 {
 {
-       register struct whyle *wp;
-
-       fseekp = l;
-#ifdef TELL
-       if (!cantell) {
-#endif
-               if (!whyles)
-                       return;
-               for (wp = whyles; wp->w_next; wp = wp->w_next)
-                       continue;
-               if (wp->w_start > l)
-                       l = wp->w_start;
-#ifdef TELL
-       }
-#endif
+    switch (aret = l->type) {
+    case E_SEEK:
+       evalvec = l->a_seek;
+       evalp = (Char *) l->f_seek;
+       return;
+    case A_SEEK:
+       alvec = l->a_seek;
+       alvecp = (Char *) l->f_seek;
+       return;
+    case F_SEEK:
+       fseekp = l->f_seek;
+       return;
+    default:
+       (void) fprintf(csherr, "Bad seek type %d\n", aret);
+       abort();
+    }
 }
 
 }
 
-/* any similarity to bell telephone is purely accidental */
-#ifndef btell
-off_t
-btell()
+void
+btell(l)
+    struct Ain *l;
 {
 {
-
-       return (fseekp);
+    switch (l->type = aret) {
+    case E_SEEK:
+       l->a_seek = evalvec;
+       l->f_seek = (off_t) evalp;
+       return;
+    case A_SEEK:
+       l->a_seek = alvec;
+       l->f_seek = (off_t) alvecp;
+       return;
+    case F_SEEK:
+       l->f_seek = fseekp;
+       l->a_seek = NULL;
+       return;
+    default:
+       (void) fprintf(csherr, "Bad seek type %d\n", aret);
+       abort();
+    }
 }
 }
-#endif
 
 
+void
 btoeof()
 {
 btoeof()
 {
-
-       (void) lseek(SHIN, (off_t)0, 2);
-       fseekp = feobp;
-       wfree();
-       bfree();
+    (void) lseek(SHIN, (off_t) 0, L_XTND);
+    aret = F_SEEK;
+    fseekp = feobp;
+    alvec = NULL;
+    alvecp = NULL;
+    evalvec = NULL;
+    evalp = NULL;
+    wfree();
+    bfree();
 }
 
 }
 
-#ifdef TELL
+void
 settell()
 {
 settell()
 {
-
-       cantell = 0;
-       if (arginp || onelflg || intty)
-               return;
-       if (lseek(SHIN, (off_t)0, 1) < 0 || errno == ESPIPE)
-               return;
-       fbuf = (char **) calloc(2, sizeof (char **));
-       fblocks = 1;
-       fbuf[0] = calloc(BUFSIZ, sizeof (char));
-       fseekp = fbobp = feobp = lseek(SHIN, (off_t)0, 1);
-       cantell = 1;
+    cantell = 0;
+    if (arginp || onelflg || intty)
+       return;
+    if (lseek(SHIN, (off_t) 0, L_INCR) < 0 || errno == ESPIPE)
+       return;
+    fbuf = (Char **) xcalloc(2, sizeof(Char **));
+    fblocks = 1;
+    fbuf[0] = (Char *) xcalloc(BUFSIZ, sizeof(Char));
+    fseekp = fbobp = feobp = lseek(SHIN, (off_t) 0, L_INCR);
+    cantell = 1;
 }
 }
-#endif