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
37 static char sccsid[] = "@(#)dol.c 8.1 (Berkeley) 5/31/93";
40 #include <sys/types.h>
56 * These routines perform variable substitution and quoting via ' and ".
57 * To this point these constructs have been preserved in the divided
58 * input words. Here we expand variables and turn quoting via ' and " into
59 * QUOTE bits on characters (which prevent further interpretation).
60 * If the `:q' modifier was applied during history expansion, then
61 * some QUOTEing may have occurred already, so we dont "trim()" here.
64 static int Dpeekc, Dpeekrd; /* Peeks for DgetC and Dreadc */
65 static Char *Dcp, **Dvp; /* Input vector for Dreadc */
69 #define unDgetC(c) Dpeekc = c
71 #define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
74 * The following variables give the information about the current
75 * $ expansion, recording the current word position, the remaining
76 * words within this expansion, the count of remaining words, and the
77 * information about any : modifier which is being applied.
79 #define MAXWLEN (BUFSIZ - 4)
80 #define MAXMOD MAXWLEN /* This cannot overflow */
81 static Char *dolp; /* Remaining chars from this word */
82 static Char **dolnxt; /* Further words */
83 static int dolcnt; /* Count of further words */
84 static Char dolmod[MAXMOD]; /* : modifier character */
85 static int dolnmod; /* Number of modifiers */
86 static int dolmcnt; /* :gx -> 10000, else 1 */
87 static int dolwcnt; /* :wx -> 10000, else 1 */
89 static void Dfix2 __P((Char **));
90 static Char *Dpack __P((Char *, Char *));
91 static int Dword __P((void));
92 static void dolerror __P((Char *));
93 static int DgetC __P((int));
94 static void Dgetdol __P((void));
95 static void fixDolMod __P((void));
96 static void setDolp __P((Char *));
97 static void unDredc __P((int));
98 static int Dredc __P((void));
99 static void Dtestq __P((int));
103 * Fix up the $ expansions and quotations in the
104 * argument list to command t.
108 register struct command *t;
115 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
116 for (pp = t->t_dcom; (p = *pp++) != NULL;)
118 if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
119 Dfix2(t->t_dcom); /* found one */
129 * $ substitute one word, for i/o redirection
143 setname(vis_str(cp));
144 stderror(ERR_NAME | ERR_AMBIG);
146 cp = Strsave(gargv[0]);
147 blkfree(gargv), gargv = 0;
152 * Subroutine to do actual fixing after state initialization.
158 ginit(); /* Initialize glob's area pointers */
160 Dcp = STRNULL; /* Setup input vector for Dreadc */
162 unDredc(0); /* Clear out any old peeks (at error) */
164 dolcnt = 0; /* Clear out residual $ expands (...) */
170 * Pack up more characters in this word
177 register int i = MAXWLEN - (wp - wbuf);
200 if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
209 stderror(ERR_WTOOLONG);
215 * Get a word. This routine is analogous to the routine
216 * word() in sh.lex.c for the main lexical input. One difference
217 * here is that we don't get a newline to terminate our expansion.
218 * Rather, DgetC will return a DEOF when we hit the end-of-input.
225 register Char *wp = wbuf;
226 register int i = MAXWLEN;
227 register bool dolflg;
228 bool sofar = 0, done = 0;
238 /* finish this word and catch the code above the next time */
253 /* We preserve ` quotations which are done yet later */
258 * Note that DgetC never returns a QUOTES character from an
259 * expansion, so only true input quotes will get us here or out.
262 dolflg = c1 == '"' ? DODOL : 0;
267 if (c == '\n' || c == DEOF)
268 stderror(ERR_UNMATCHED, c1);
269 if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE))
272 stderror(ERR_WTOOLONG);
277 * Leave any `s alone for later. Other chars are all
278 * quoted, thus `...` can tell it was within "...".
280 *wp++ = c == '`' ? '`' : c | QUOTE;
284 /* Prevent all further interpretation */
289 /* Leave all text alone for later */
298 *wp++ = '`' /* i--; eliminated */;
300 if ((wp = Dpack(wbuf, wp)) == NULL)
303 i = MAXWLEN - (wp - wbuf);
309 c = DgetC(0); /* No $ subst! */
310 if (c == '\n' || c == DEOF) {
323 if ((wp = Dpack(wbuf, wp)) == NULL)
326 i = MAXWLEN - (wp - wbuf);
331 /* Really NOTREACHED */
337 * Get a character, performing $ substitution unless flag is 0.
338 * Any QUOTES character which is returned from a $ expansion is
339 * QUOTEd so that it will not be recognized above.
348 if ((c = Dpeekc) != '\0') {
353 c = *lap++ & (QUOTE | TRIM);
364 if ((c = *dolp++ & (QUOTE | TRIM)) != '\0')
379 if (c == '$' && flag) {
386 static Char *nulvec[] = {0};
387 static struct varent nulargv = {nulvec, STRargv, { NULL, NULL, NULL }, 0};
394 stderror(ERR_NAME | ERR_RANGE);
398 * Handle the multitudinous $ expansion forms.
405 register struct varent *vp = NULL;
406 Char name[4 * MAXVARLEN + 1];
408 int subscr = 0, lwb = 1, upb = 0;
409 bool dimen = 0, bitset = 0;
412 static Char *dolbang = NULL;
414 dolnmod = dolmcnt = dolwcnt = 0;
417 c = DgetC(0); /* sc is { to take } later */
418 if ((c & TRIM) == '#')
419 dimen++, c = DgetC(0); /* $# takes dimension */
421 bitset++, c = DgetC(0); /* $? tests existence */
426 stderror(ERR_SYNTAX);
429 xfree((ptr_t) dolbang);
430 setDolp(dolbang = putn(backpid));
436 stderror(ERR_SYNTAX);
442 stderror(ERR_NOTALLOWED, "$?<");
444 stderror(ERR_NOTALLOWED, "$?#");
445 for (np = wbuf; read(OLDSTD, &tnp, 1) == 1; np++) {
446 *np = (unsigned char) tnp;
447 if (np >= &wbuf[BUFSIZ - 1])
448 stderror(ERR_LTOOLONG);
454 * KLUDGE: dolmod is set here because it will cause setDolp to call
455 * domod and thus to copy wbuf. Otherwise setDolp would use it
456 * directly. If we saved it ourselves, no one would know when to free
457 * it. The actual function of the 'q' causes filename expansion not to
458 * be done on the interpolated value.
460 dolmod[dolnmod++] = 'q';
467 stderror(ERR_SYNTAX);
472 (void) Strcpy(name, STRargv);
474 subscr = -1; /* Prevent eating [...] */
481 stderror(ERR_NOTALLOWED, "$#<num>");
484 subscr = subscr * 10 + c - '0';
486 } while (Isdigit(c));
489 dolerror(vp->v_name);
494 dolp = ffile ? STR1 : STR0;
498 stderror(ERR_DOLZERO);
504 stderror(ERR_DOLQUEST);
513 stderror(ERR_VARALNUM);
519 if (np >= &name[MAXVARLEN])
520 stderror(ERR_VARTOOLONG);
527 dolp = (vp || getenv(short2str(name))) ? STR1 : STR0;
531 np = str2short(getenv(short2str(name)));
541 upb = blklen(vp->vec);
542 if (dimen == 0 && subscr == 0 && c == '[') {
545 c = DgetC(DODOL); /* Allow $ expand within [ ] */
548 if (c == '\n' || c == DEOF)
550 if (np >= &name[sizeof(name) / sizeof(Char) - 2])
551 stderror(ERR_VARTOOLONG);
555 if (dolp || dolcnt) /* $ exp must end before ] */
556 stderror(ERR_EXPORD);
558 stderror(ERR_SYNTAX);
562 for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
564 if ((i < 0 || i > upb) && !any("-*", *np)) {
565 dolerror(vp->v_name);
570 upb = lwb, np = STRstar;
575 stderror(ERR_MISSING, '-');
577 register int i = upb;
583 i = i * 10 + *np++ - '0';
584 if (i < 0 || i > upb) {
585 dolerror(vp->v_name);
596 dolerror(vp->v_name);
602 stderror(ERR_SYNTAX);
613 Char *cp = putn(upb - lwb + 1);
621 dolnxt = &vp->vec[lwb - 1];
622 dolcnt = upb - lwb + 1;
628 stderror(ERR_MISSING, '}');
640 c = DgetC(0), dolmcnt = 1, dolwcnt = 1;
641 if (c == 'g' || c == 'a') {
648 if ((c == 'g' && dolmcnt != 10000) ||
649 (c == 'a' && dolwcnt != 10000)) {
657 if (c == 's') { /* [eichin:19910926.0755EST] */
659 int delim = DgetC(0);
660 dolmod[dolnmod++] = c;
661 dolmod[dolnmod++] = delim;
663 if (!delim || letter(delim)
664 || Isdigit(delim) || any(" \t\n", delim)) {
665 seterror(ERR_BADSUBST);
668 while ((c = DgetC(0)) != (-1)) {
669 dolmod[dolnmod++] = c;
670 if(c == delim) delimcnt--;
674 seterror(ERR_BADSUBST);
679 if (!any("htrqxes", c))
680 stderror(ERR_BADMOD, c);
681 dolmod[dolnmod++] = c;
685 while ((c = DgetC(0)) == ':');
699 if (dolnmod == 0 || dolmcnt == 0) {
703 dp = cp = Strsave(cp);
704 for (i = 0; i < dolnmod; i++) {
705 /* handle s// [eichin:19910926.0510EST] */
706 if(dolmod[i] == 's') {
708 Char *lhsub, *rhsub, *np;
709 size_t lhlen = 0, rhlen = 0;
713 if (!delim || letter(delim)
714 || Isdigit(delim) || any(" \t\n", delim)) {
715 seterror(ERR_BADSUBST);
718 lhsub = &dolmod[++i];
719 while(dolmod[i] != delim && dolmod[++i]) {
723 rhsub = &dolmod[++i];
724 while(dolmod[i] != delim && dolmod[++i]) {
730 dp = Strstr(cp, lhsub);
732 np = (Char *) xmalloc((size_t)
733 ((Strlen(cp) + 1 - lhlen + rhlen) *
735 (void) Strncpy(np, cp, dp - cp);
736 (void) Strcpy(np + (dp - cp), rhsub);
737 (void) Strcpy(np + (dp - cp) + rhlen, dp + lhlen);
743 /* should this do a seterror? */
747 while (dolwcnt == 10000);
749 * restore dolmod for additional words
751 dolmod[i] = rhsub[-1] = delim;
760 if ((dp = domod(cp, dolmod[i]))) {
762 if (Strcmp(cp, dp) == 0) {
775 while (dolwcnt == 10000);
809 if ((c = Dpeekrd) != '\0') {
813 if (Dcp && (c = *Dcp++))
814 return (c & (QUOTE | TRIM));
833 * Form a shell temporary file (in unit 0) from the words
834 * of the shell input up to EOF or a line the same as "term".
835 * Unit 0 should have been closed before this call.
844 Char obuf[BUFSIZ], lbuf[BUFSIZ], mbuf[BUFSIZ];
845 int ocnt, lcnt, mcnt;
846 register Char *lbp, *obp, *mbp;
851 if (creat(tmp = short2str(shtemp), 0600) < 0)
852 stderror(ERR_SYSTEM, tmp, strerror(errno));
854 if (open(tmp, O_RDWR) < 0) {
859 stderror(ERR_SYSTEM, tmp, strerror(errno));
861 (void) unlink(tmp); /* 0 0 inode! */
877 c = readc(1); /* 1 -> Want EOF returns */
878 if (c < 0 || c == '\n')
880 if ((c &= TRIM) != '\0') {
884 stderror(ERR_NAME | ERR_OVERFLOW);
891 * Check for EOF or compare to terminator -- before expansion
893 if (c < 0 || eq(lbuf, term)) {
894 (void) write(0, short2str(obuf), (size_t) (BUFSIZ - ocnt));
895 (void) lseek(0, 0l, L_SET);
900 * If term was quoted or -n just pass it on
902 if (quoted || noexec) {
905 for (lbp = lbuf; (c = *lbp++) != '\0';) {
908 (void) write(0, short2str(obuf), BUFSIZ);
917 * Term wasn't quoted so variable and then command expand the input
928 if ((c &= TRIM) == 0)
930 /* \ quotes \ $ ` here */
934 unDgetC(c | QUOTE), c = '\\';
941 stderror(ERR_NAME | ERR_OVERFLOW);
947 * If any ` in line do command substitution
950 if (any(short2str(mbp), '`')) {
952 * 1 arg to dobackp causes substitution to be literal. Words are
953 * broken only at newlines so that all blanks and tabs are
954 * preserved. Blank lines (null words) are not discarded.
956 vp = dobackp(mbuf, 1);
959 /* Setup trivial vector similar to return of dobackp */
960 Dv[0] = mbp, Dv[1] = NULL, vp = Dv;
963 * Resurrect the words from the command substitution each separated by
964 * a newline. Note that the last newline of a command substitution
965 * will have been discarded, but we put a newline after the last word
966 * because this represents the newline after the last input line!
969 for (mbp = *vp; *mbp; mbp++) {
970 *obp++ = *mbp & TRIM;
972 (void) write(0, short2str(obuf), BUFSIZ);
979 (void) write(0, short2str(obuf), BUFSIZ);
985 blkfree(pargv), pargv = 0;