2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)dol.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$Id: dol.c,v 1.7 1998/05/06 06:50:39 charnier Exp $";
43 #include <sys/types.h>
59 * These routines perform variable substitution and quoting via ' and ".
60 * To this point these constructs have been preserved in the divided
61 * input words. Here we expand variables and turn quoting via ' and " into
62 * QUOTE bits on characters (which prevent further interpretation).
63 * If the `:q' modifier was applied during history expansion, then
64 * some QUOTEing may have occurred already, so we dont "trim()" here.
67 static int Dpeekc, Dpeekrd; /* Peeks for DgetC and Dreadc */
68 static Char *Dcp, **Dvp; /* Input vector for Dreadc */
72 #define unDgetC(c) Dpeekc = c
74 #define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
77 * The following variables give the information about the current
78 * $ expansion, recording the current word position, the remaining
79 * words within this expansion, the count of remaining words, and the
80 * information about any : modifier which is being applied.
82 #define MAXWLEN (BUFSIZ - 4)
83 #define MAXMOD MAXWLEN /* This cannot overflow */
84 static Char *dolp; /* Remaining chars from this word */
85 static Char **dolnxt; /* Further words */
86 static int dolcnt; /* Count of further words */
87 static Char dolmod[MAXMOD]; /* : modifier character */
88 static int dolnmod; /* Number of modifiers */
89 static int dolmcnt; /* :gx -> 10000, else 1 */
90 static int dolwcnt; /* :wx -> 10000, else 1 */
92 static void Dfix2 __P((Char **));
93 static Char *Dpack __P((Char *, Char *));
94 static int Dword __P((void));
95 static void dolerror __P((Char *));
96 static int DgetC __P((int));
97 static void Dgetdol __P((void));
98 static void fixDolMod __P((void));
99 static void setDolp __P((Char *));
100 static void unDredc __P((int));
101 static int Dredc __P((void));
102 static void Dtestq __P((int));
106 * Fix up the $ expansions and quotations in the
107 * argument list to command t.
118 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
119 for (pp = t->t_dcom; (p = *pp++) != NULL;)
121 if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
122 Dfix2(t->t_dcom); /* found one */
132 * $ substitute one word, for i/o redirection
146 setname(vis_str(cp));
147 stderror(ERR_NAME | ERR_AMBIG);
149 cp = Strsave(gargv[0]);
150 blkfree(gargv), gargv = 0;
155 * Subroutine to do actual fixing after state initialization.
161 ginit(); /* Initialize glob's area pointers */
163 Dcp = STRNULL; /* Setup input vector for Dreadc */
165 unDredc(0); /* Clear out any old peeks (at error) */
167 dolcnt = 0; /* Clear out residual $ expands (...) */
173 * Pack up more characters in this word
180 int i = MAXWLEN - (wp - wbuf);
203 if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
212 stderror(ERR_WTOOLONG);
218 * Get a word. This routine is analogous to the routine
219 * word() in sh.lex.c for the main lexical input. One difference
220 * here is that we don't get a newline to terminate our expansion.
221 * Rather, DgetC will return a DEOF when we hit the end-of-input.
231 bool sofar = 0, done = 0;
241 /* finish this word and catch the code above the next time */
256 /* We preserve ` quotations which are done yet later */
261 * Note that DgetC never returns a QUOTES character from an
262 * expansion, so only true input quotes will get us here or out.
265 dolflg = c1 == '"' ? DODOL : 0;
270 if (c == '\n' || c == DEOF)
271 stderror(ERR_UNMATCHED, c1);
272 if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE))
275 stderror(ERR_WTOOLONG);
280 * Leave any `s alone for later. Other chars are all
281 * quoted, thus `...` can tell it was within "...".
283 *wp++ = c == '`' ? '`' : c | QUOTE;
287 /* Prevent all further interpretation */
292 /* Leave all text alone for later */
301 *wp++ = '`' /* i--; eliminated */;
303 if ((wp = Dpack(wbuf, wp)) == NULL)
306 i = MAXWLEN - (wp - wbuf);
312 c = DgetC(0); /* No $ subst! */
313 if (c == '\n' || c == DEOF) {
326 if ((wp = Dpack(wbuf, wp)) == NULL)
329 i = MAXWLEN - (wp - wbuf);
334 /* Really NOTREACHED */
340 * Get a character, performing $ substitution unless flag is 0.
341 * Any QUOTES character which is returned from a $ expansion is
342 * QUOTEd so that it will not be recognized above.
351 if ((c = Dpeekc) != '\0') {
356 c = *lap++ & (QUOTE | TRIM);
367 if ((c = *dolp++ & (QUOTE | TRIM)) != '\0')
382 if (c == '$' && flag) {
389 static Char *nulvec[] = {0};
390 static struct varent nulargv = {nulvec, STRargv, { NULL, NULL, NULL }, 0};
397 stderror(ERR_NAME | ERR_RANGE);
401 * Handle the multitudinous $ expansion forms.
408 struct varent *vp = NULL;
409 Char name[4 * MAXVARLEN + 1];
411 int subscr = 0, lwb = 1, upb = 0;
412 bool dimen = 0, bitset = 0;
415 static Char *dolbang = NULL;
417 dolnmod = dolmcnt = dolwcnt = 0;
420 c = DgetC(0); /* sc is { to take } later */
421 if ((c & TRIM) == '#')
422 dimen++, c = DgetC(0); /* $# takes dimension */
424 bitset++, c = DgetC(0); /* $? tests existence */
429 stderror(ERR_SYNTAX);
432 xfree((ptr_t) dolbang);
433 setDolp(dolbang = putn(backpid));
439 stderror(ERR_SYNTAX);
445 stderror(ERR_NOTALLOWED, "$?<");
447 stderror(ERR_NOTALLOWED, "$?#");
448 for (np = wbuf; read(OLDSTD, &tnp, 1) == 1; np++) {
449 *np = (unsigned char) tnp;
450 if (np >= &wbuf[BUFSIZ - 1])
451 stderror(ERR_LTOOLONG);
457 * KLUDGE: dolmod is set here because it will cause setDolp to call
458 * domod and thus to copy wbuf. Otherwise setDolp would use it
459 * directly. If we saved it ourselves, no one would know when to free
460 * it. The actual function of the 'q' causes filename expansion not to
461 * be done on the interpolated value.
463 dolmod[dolnmod++] = 'q';
470 stderror(ERR_SYNTAX);
475 (void) Strcpy(name, STRargv);
477 subscr = -1; /* Prevent eating [...] */
484 stderror(ERR_NOTALLOWED, "$#<num>");
487 subscr = subscr * 10 + c - '0';
489 } while (Isdigit(c));
492 dolerror(vp->v_name);
497 dolp = ffile ? STR1 : STR0;
501 stderror(ERR_DOLZERO);
507 stderror(ERR_DOLQUEST);
516 stderror(ERR_VARALNUM);
522 if (np >= &name[MAXVARLEN])
523 stderror(ERR_VARTOOLONG);
530 dolp = (vp || getenv(short2str(name))) ? STR1 : STR0;
534 np = str2short(getenv(short2str(name)));
544 upb = blklen(vp->vec);
545 if (dimen == 0 && subscr == 0 && c == '[') {
548 c = DgetC(DODOL); /* Allow $ expand within [ ] */
551 if (c == '\n' || c == DEOF)
553 if (np >= &name[sizeof(name) / sizeof(Char) - 2])
554 stderror(ERR_VARTOOLONG);
558 if (dolp || dolcnt) /* $ exp must end before ] */
559 stderror(ERR_EXPORD);
561 stderror(ERR_SYNTAX);
565 for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
567 if ((i < 0 || i > upb) && !any("-*", *np)) {
568 dolerror(vp->v_name);
573 upb = lwb, np = STRstar;
578 stderror(ERR_MISSING, '-');
586 i = i * 10 + *np++ - '0';
587 if (i < 0 || i > upb) {
588 dolerror(vp->v_name);
599 dolerror(vp->v_name);
605 stderror(ERR_SYNTAX);
616 Char *cp = putn(upb - lwb + 1);
624 dolnxt = &vp->vec[lwb - 1];
625 dolcnt = upb - lwb + 1;
631 stderror(ERR_MISSING, '}');
643 c = DgetC(0), dolmcnt = 1, dolwcnt = 1;
644 if (c == 'g' || c == 'a') {
651 if ((c == 'g' && dolmcnt != 10000) ||
652 (c == 'a' && dolwcnt != 10000)) {
660 if (c == 's') { /* [eichin:19910926.0755EST] */
662 int delim = DgetC(0);
663 dolmod[dolnmod++] = c;
664 dolmod[dolnmod++] = delim;
666 if (!delim || letter(delim)
667 || Isdigit(delim) || any(" \t\n", delim)) {
668 seterror(ERR_BADSUBST);
671 while ((c = DgetC(0)) != (-1)) {
672 dolmod[dolnmod++] = c;
673 if(c == delim) delimcnt--;
677 seterror(ERR_BADSUBST);
682 if (!any("htrqxes", c))
683 stderror(ERR_BADMOD, c);
684 dolmod[dolnmod++] = c;
688 while ((c = DgetC(0)) == ':');
702 if (dolnmod == 0 || dolmcnt == 0) {
706 dp = cp = Strsave(cp);
707 for (i = 0; i < dolnmod; i++) {
708 /* handle s// [eichin:19910926.0510EST] */
709 if(dolmod[i] == 's') {
711 Char *lhsub, *rhsub, *np;
712 size_t lhlen = 0, rhlen = 0;
716 if (!delim || letter(delim)
717 || Isdigit(delim) || any(" \t\n", delim)) {
718 seterror(ERR_BADSUBST);
721 lhsub = &dolmod[++i];
722 while(dolmod[i] != delim && dolmod[++i]) {
726 rhsub = &dolmod[++i];
727 while(dolmod[i] != delim && dolmod[++i]) {
733 dp = Strstr(cp, lhsub);
735 np = (Char *) xmalloc((size_t)
736 ((Strlen(cp) + 1 - lhlen + rhlen) *
738 (void) Strncpy(np, cp, dp - cp);
739 (void) Strcpy(np + (dp - cp), rhsub);
740 (void) Strcpy(np + (dp - cp) + rhlen, dp + lhlen);
746 /* should this do a seterror? */
750 while (dolwcnt == 10000);
752 * restore dolmod for additional words
754 dolmod[i] = rhsub[-1] = delim;
763 if ((dp = domod(cp, dolmod[i]))) {
765 if (Strcmp(cp, dp) == 0) {
778 while (dolwcnt == 10000);
812 if ((c = Dpeekrd) != '\0') {
816 if (Dcp && (c = *Dcp++))
817 return (c & (QUOTE | TRIM));
836 * Form a shell temporary file (in unit 0) from the words
837 * of the shell input up to EOF or a line the same as "term".
838 * Unit 0 should have been closed before this call.
847 Char obuf[BUFSIZ], lbuf[BUFSIZ], mbuf[BUFSIZ];
848 int ocnt, lcnt, mcnt;
849 Char *lbp, *obp, *mbp;
854 if (creat(tmp = short2str(shtemp), 0600) < 0)
855 stderror(ERR_SYSTEM, tmp, strerror(errno));
857 if (open(tmp, O_RDWR) < 0) {
862 stderror(ERR_SYSTEM, tmp, strerror(errno));
864 (void) unlink(tmp); /* 0 0 inode! */
880 c = readc(1); /* 1 -> Want EOF returns */
881 if (c < 0 || c == '\n')
883 if ((c &= TRIM) != '\0') {
887 stderror(ERR_NAME | ERR_OVERFLOW);
894 * Check for EOF or compare to terminator -- before expansion
896 if (c < 0 || eq(lbuf, term)) {
897 (void) write(0, short2str(obuf), (size_t) (BUFSIZ - ocnt));
898 (void) lseek(0, 0l, L_SET);
903 * If term was quoted or -n just pass it on
905 if (quoted || noexec) {
908 for (lbp = lbuf; (c = *lbp++) != '\0';) {
911 (void) write(0, short2str(obuf), BUFSIZ);
920 * Term wasn't quoted so variable and then command expand the input
931 if ((c &= TRIM) == 0)
933 /* \ quotes \ $ ` here */
937 unDgetC(c | QUOTE), c = '\\';
944 stderror(ERR_NAME | ERR_OVERFLOW);
950 * If any ` in line do command substitution
953 if (any(short2str(mbp), '`')) {
955 * 1 arg to dobackp causes substitution to be literal. Words are
956 * broken only at newlines so that all blanks and tabs are
957 * preserved. Blank lines (null words) are not discarded.
959 vp = dobackp(mbuf, 1);
962 /* Setup trivial vector similar to return of dobackp */
963 Dv[0] = mbp, Dv[1] = NULL, vp = Dv;
966 * Resurrect the words from the command substitution each separated by
967 * a newline. Note that the last newline of a command substitution
968 * will have been discarded, but we put a newline after the last word
969 * because this represents the newline after the last input line!
972 for (mbp = *vp; *mbp; mbp++) {
973 *obp++ = *mbp & TRIM;
975 (void) write(0, short2str(obuf), BUFSIZ);
982 (void) write(0, short2str(obuf), BUFSIZ);
988 blkfree(pargv), pargv = 0;