BSD 4_3_Tahoe release
[unix-history] / usr / src / bin / csh / sh.dol.c
index 6ed0cb3..c70774c 100644 (file)
@@ -1,4 +1,12 @@
-static char *sccsid = "@(#)sh.dol.c 4.3 5/7/82";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley Software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char *sccsid = "@(#)sh.dol.c    5.5 (Berkeley) 1/15/88";
+#endif
 
 #include "sh.h"
 
 
 #include "sh.h"
 
@@ -12,7 +20,7 @@ static        char *sccsid = "@(#)sh.dol.c 4.3 5/7/82";
  * input words.  Here we expand variables and turn quoting via ' and " into
  * QUOTE bits on characters (which prevent further interpretation).
  * If the `:q' modifier was applied during history expansion, then
  * input words.  Here we expand variables and turn quoting via ' and " into
  * QUOTE bits on characters (which prevent further interpretation).
  * If the `:q' modifier was applied during history expansion, then
- * some QUOTEing may have occurred already, so we dont "scan(,&trim)" here.
+ * some QUOTEing may have occurred already, so we dont "trim()" here.
  */
 
 int    Dpeekc, Dpeekrd;                /* Peeks for DgetC and Dreadc */
  */
 
 int    Dpeekc, Dpeekrd;                /* Peeks for DgetC and Dreadc */
@@ -22,7 +30,7 @@ char  *Dcp, **Dvp;                    /* Input vector for Dreadc */
 
 #define        unDgetC(c)      Dpeekc = c
 
 
 #define        unDgetC(c)      Dpeekc = c
 
-char   *QUOTES = "\\'`\"";
+#define QUOTES         (_Q|_Q1|_ESC)   /* \ ' " ` */
 
 /*
  * The following variables give the information about the current
 
 /*
  * The following variables give the information about the current
@@ -36,8 +44,6 @@ int   dolcnt;                 /* Count of further words */
 char   dolmod;                 /* : modifier character */
 int    dolmcnt;                /* :gx -> 10000, else 1 */
 
 char   dolmod;                 /* : modifier character */
 int    dolmcnt;                /* :gx -> 10000, else 1 */
 
-int    Dtest();                /* Test for \ " ` or ' */
-
 /*
  * Fix up the $ expansions and quotations in the
  * argument list to command t.
 /*
  * Fix up the $ expansions and quotations in the
  * argument list to command t.
@@ -45,14 +51,21 @@ int Dtest();                /* Test for \ " ` or ' */
 Dfix(t)
        register struct command *t;
 {
 Dfix(t)
        register struct command *t;
 {
+       register char **pp;
+       register char *p;
 
        if (noexec)
                return;
 
        if (noexec)
                return;
-       gflag = 0, rscan(t->t_dcom, Dtest);
-       if (gflag == 0)
-               return;
-       Dfix2(t->t_dcom);
-       blkfree(t->t_dcom), t->t_dcom = gargv, gargv = 0;
+       /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
+       for (pp = t->t_dcom; p = *pp++;)
+               while (*p)
+                       if (cmap(*p++, _DOL|QUOTES)) {  /* $, \, ', ", ` */
+                               Dfix2(t->t_dcom);       /* found one */
+                               blkfree(t->t_dcom);
+                               t->t_dcom = gargv;
+                               gargv = 0;
+                               return;
+                       }
 }
 
 /*
 }
 
 /*
@@ -201,9 +214,9 @@ pack:
                }
                if (c == DEOF)
                        goto deof;
                }
                if (c == DEOF)
                        goto deof;
-               if (any(c, " '`\"\t\n")) {
+               if (cmap(c, _SP|_NL|_Q|_Q1)) {          /* sp \t\n'"` */
                        unDgetC(c);
                        unDgetC(c);
-                       if (any(c, QUOTES))
+                       if (cmap(c, QUOTES))
                                goto loop;
                        *wp++ = 0;
                        goto ret;
                                goto loop;
                        *wp++ = 0;
                        goto ret;
@@ -240,7 +253,7 @@ top:
                        goto top;
                }
 quotspec:
                        goto top;
                }
 quotspec:
-               if (any(c, QUOTES))
+               if (cmap(c, QUOTES))
                        return (c | QUOTE);
                return (c);
        }
                        return (c | QUOTE);
                return (c);
        }
@@ -328,7 +341,7 @@ Dgetdol()
                goto syntax;
 
        case '*':
                goto syntax;
 
        case '*':
-               strcpy(name, "argv");
+               (void) strcpy(name, "argv");
                vp = adrof("argv");
                subscr = -1;                    /* Prevent eating [...] */
                break;
                vp = adrof("argv");
                subscr = -1;                    /* Prevent eating [...] */
                break;
@@ -419,7 +432,7 @@ syntax:
                                i = i * 10 + *np++ - '0';
                        if ((i < 0 || i > upb) && !any(*np, "-*")) {
 oob:
                                i = i * 10 + *np++ - '0';
                        if ((i < 0 || i > upb) && !any(*np, "-*")) {
 oob:
-                               setname(vp->name);
+                               setname(vp->v_name);
                                error("Subscript out of range");
                        }
                        lwb = i;
                                error("Subscript out of range");
                        }
                        lwb = i;
@@ -508,6 +521,8 @@ setDolp(cp)
        } else
                addla(cp);
        dolp = "";
        } else
                addla(cp);
        dolp = "";
+       if (err)
+               error(err);
 }
 
 unDredc(c)
 }
 
 unDredc(c)
@@ -535,26 +550,17 @@ Dredc()
        return (' ');
 }
 
        return (' ');
 }
 
-Dtest(c)
-       register int c;
-{
-
-       /* Note that c isn't trimmed thus !...:q's aren't lost */
-       if (any(c, "$\\'`\""))
-               gflag = 1;
-}
-
 Dtestq(c)
        register int c;
 {
 
 Dtestq(c)
        register int c;
 {
 
-       if (any(c, "\\'`\""))
+       if (cmap(c, QUOTES))
                gflag = 1;
 }
 
 /*
  * Form a shell temporary file (in unit 0) from the words
                gflag = 1;
 }
 
 /*
  * Form a shell temporary file (in unit 0) from the words
- * of the shell input up to a line the same as "term".
+ * of the shell input up to EOF or a line the same as "term".
  * Unit 0 should have been closed before this call.
  */
 heredoc(term)
  * Unit 0 should have been closed before this call.
  */
 heredoc(term)
@@ -570,17 +576,17 @@ heredoc(term)
 
        if (creat(shtemp, 0600) < 0)
                Perror(shtemp);
 
        if (creat(shtemp, 0600) < 0)
                Perror(shtemp);
-       close(0);
+       (void) close(0);
        if (open(shtemp, 2) < 0) {
                int oerrno = errno;
 
        if (open(shtemp, 2) < 0) {
                int oerrno = errno;
 
-               unlink(shtemp);
+               (void) unlink(shtemp);
                errno = oerrno;
                Perror(shtemp);
        }
                errno = oerrno;
                Perror(shtemp);
        }
-       unlink(shtemp);                 /* 0 0 inode! */
+       (void) unlink(shtemp);                  /* 0 0 inode! */
        Dv[0] = term; Dv[1] = NOSTR; gflag = 0;
        Dv[0] = term; Dv[1] = NOSTR; gflag = 0;
-       scan(Dv, trim); rscan(Dv, Dtestq); quoted = gflag;
+       trim(Dv); rscan(Dv, Dtestq); quoted = gflag;
        ocnt = BUFSIZ; obp = obuf;
        for (;;) {
                /*
        ocnt = BUFSIZ; obp = obuf;
        for (;;) {
                /*
@@ -589,11 +595,7 @@ heredoc(term)
                lbp = lbuf; lcnt = BUFSIZ - 4;
                for (;;) {
                        c = readc(1);           /* 1 -> Want EOF returns */
                lbp = lbuf; lcnt = BUFSIZ - 4;
                for (;;) {
                        c = readc(1);           /* 1 -> Want EOF returns */
-                       if (c < 0) {
-                               setname(term);
-                               bferr("<< terminator not found");
-                       }
-                       if (c == '\n')
+                       if (c < 0 || c == '\n')
                                break;
                        if (c &= TRIM) {
                                *lbp++ = c;
                                break;
                        if (c &= TRIM) {
                                *lbp++ = c;
@@ -606,11 +608,11 @@ heredoc(term)
                *lbp = 0;
 
                /*
                *lbp = 0;
 
                /*
-                * Compare to terminator -- before expansion
+                * Check for EOF or compare to terminator -- before expansion
                 */
                 */
-               if (eq(lbuf, term)) {
-                       write(0, obuf, BUFSIZ - ocnt);
-                       lseek(0, 0l, 0);
+               if (c < 0 || eq(lbuf, term)) {
+                       (void) write(0, obuf, BUFSIZ - ocnt);
+                       (void) lseek(0, (off_t)0, 0);
                        return;
                }
 
                        return;
                }
 
@@ -622,7 +624,7 @@ heredoc(term)
                        for (lbp = lbuf; c = *lbp++;) {
                                *obp++ = c;
                                if (--ocnt == 0) {
                        for (lbp = lbuf; c = *lbp++;) {
                                *obp++ = c;
                                if (--ocnt == 0) {
-                                       write(0, obuf, BUFSIZ);
+                                       (void) write(0, obuf, BUFSIZ);
                                        obp = obuf; ocnt = BUFSIZ;
                                }
                        }
                                        obp = obuf; ocnt = BUFSIZ;
                                }
                        }
@@ -684,13 +686,13 @@ heredoc(term)
                        for (mbp = *vp; *mbp; mbp++) {
                                *obp++ = *mbp & TRIM;
                                if (--ocnt == 0) {
                        for (mbp = *vp; *mbp; mbp++) {
                                *obp++ = *mbp & TRIM;
                                if (--ocnt == 0) {
-                                       write(0, obuf, BUFSIZ);
+                                       (void) write(0, obuf, BUFSIZ);
                                        obp = obuf; ocnt = BUFSIZ;
                                }
                        }
                        *obp++ = '\n';
                        if (--ocnt == 0) {
                                        obp = obuf; ocnt = BUFSIZ;
                                }
                        }
                        *obp++ = '\n';
                        if (--ocnt == 0) {
-                               write(0, obuf, BUFSIZ);
+                               (void) write(0, obuf, BUFSIZ);
                                obp = obuf; ocnt = BUFSIZ;
                        }
                }
                                obp = obuf; ocnt = BUFSIZ;
                        }
                }