2 * sh.dol.c: Variable substitutions
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * These routines perform variable substitution and quoting via ' and ".
40 * To this point these constructs have been preserved in the divided
41 * input words. Here we expand variables and turn quoting via ' and " into
42 * QUOTE bits on characters (which prevent further interpretation).
43 * If the `:q' modifier was applied during history expansion, then
44 * some QUOTEing may have occurred already, so we dont "trim()" here.
47 static eChar Dpeekc; /* Peek for DgetC */
48 static eChar Dpeekrd; /* Peek for Dreadc */
49 static Char *Dcp, *const *Dvp; /* Input vector for Dreadc */
53 #define unDgetC(c) Dpeekc = c
55 #define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
58 * The following variables give the information about the current
59 * $ expansion, recording the current word position, the remaining
60 * words within this expansion, the count of remaining words, and the
61 * information about any : modifier which is being applied.
63 static Char *dolp; /* Remaining chars from this word */
64 static Char **dolnxt; /* Further words */
65 static int dolcnt; /* Count of further words */
66 static struct Strbuf dolmod; /* = Strbuf_INIT; : modifier characters */
68 static int ndolflags; /* keep track of mod counts for each modifier */
69 static int *dolmcnts; /* :gx -> INT_MAX, else 1 */
70 static int *dolaflags; /* :ax -> 1, else 0 */
72 static Char **Dfix2 (Char *const *);
73 static int Dpack (struct Strbuf *);
74 static int Dword (struct blk_buf *);
75 static void dolerror (Char *);
76 static eChar DgetC (int);
77 static void Dgetdol (void);
78 static void fixDolMod (void);
79 static void setDolp (Char *);
80 static void unDredc (eChar);
81 static eChar Dredc (void);
82 static void Dtestq (Char);
85 * Fix up the $ expansions and quotations in the
86 * argument list to command t.
89 Dfix(struct command *t)
96 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
97 for (pp = t->t_dcom; (p = *pp++) != NULL;) {
99 if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
102 expanded = Dfix2(t->t_dcom); /* found one */
104 t->t_dcom = expanded;
112 * $ substitute one word, for i/o redirection
117 Char *Dv[2], **expanded;
123 expanded = Dfix2(Dv);
124 if (expanded[0] == NULL || expanded[1] != NULL) {
126 setname(short2str(cp));
127 stderror(ERR_NAME | ERR_AMBIG);
129 cp = Strsave(expanded[0]);
135 * Subroutine to do actual fixing after state initialization.
138 Dfix2(Char *const *v)
140 struct blk_buf *bb = bb_alloc();
144 Dcp = STRNULL; /* Setup input vector for Dreadc */
146 unDredc(0); /* Clear out any old peeks (at error) */
148 dolcnt = 0; /* Clear out residual $ expands (...) */
149 cleanup_push(bb, bb_free);
160 * Pack up more characters in this word
163 Dpack(struct Strbuf *wbuf)
184 if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
190 Strbuf_append1(wbuf, (Char) c);
195 * Get a word. This routine is analogous to the routine
196 * word() in sh.lex.c for the main lexical input. One difference
197 * here is that we don't get a newline to terminate our expansion.
198 * Rather, DgetC will return a DEOF when we hit the end-of-input.
201 Dword(struct blk_buf *bb)
204 struct Strbuf *wbuf = Strbuf_alloc();
209 cleanup_push(wbuf, Strbuf_free);
219 /* finish this word and catch the code above the next time */
231 /* We preserve ` quotations which are done yet later */
232 Strbuf_append1(wbuf, (Char) c);
237 * Note that DgetC never returns a QUOTES character from an
238 * expansion, so only true input quotes will get us here or out.
241 dolflg = c1 == '"' ? DODOL : 0;
246 if (c == '\n' || c == DEOF) {
248 stderror(ERR_UNMATCHED, (int)c1);
250 if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE)) {
251 if (wbuf->len != 0 && (wbuf->s[wbuf->len - 1] & TRIM) == '\\')
258 * Leave any `s alone for later. Other chars are all
259 * quoted, thus `...` can tell it was within "...".
261 Strbuf_append1(wbuf, c == '`' ? '`' : c | QUOTE);
265 /* Prevent all further interpretation */
266 Strbuf_append1(wbuf, c | QUOTE);
270 /* Leave all text alone for later */
271 Strbuf_append1(wbuf, (Char) c);
279 Strbuf_append1(wbuf, '`');
281 if (Dpack(wbuf) != 0)
286 c = DgetC(0); /* No $ subst! */
287 if (c == '\n' || c == DEOF)
297 if (Dpack(wbuf) != 0)
302 cleanup_ignore(wbuf);
304 str = Strbuf_finish(wbuf);
312 * Get a character, performing $ substitution unless flag is 0.
313 * Any QUOTES character which is returned from a $ expansion is
314 * QUOTEd so that it will not be recognized above.
322 if ((c = Dpeekc) != 0) {
326 if (lap < labuf.len) {
327 c = labuf.s[lap++] & (QUOTE | TRIM);
334 if ((c = *dolp++ & (QUOTE | TRIM)) != 0)
349 if (c == '$' && flag) {
356 static Char *nulvec[] = { NULL };
357 static struct varent nulargv = {nulvec, STRargv, VAR_READWRITE,
358 { NULL, NULL, NULL }, 0 };
363 setname(short2str(s));
364 stderror(ERR_NAME | ERR_RANGE);
368 * Handle the multitudinous $ expansion forms.
375 struct varent *vp = NULL;
376 struct Strbuf *name = Strbuf_alloc();
378 int subscr = 0, lwb = 1, upb = 0;
379 int dimen = 0, bitset = 0, length = 0;
380 static Char *dolbang = NULL;
382 cleanup_push(name, Strbuf_free);
383 dolmod.len = ndolflags = 0;
386 stderror(ERR_SYNTAX);
390 c = DgetC(0); /* sc is { to take } later */
391 if ((c & TRIM) == '#')
392 dimen++, c = DgetC(0); /* $# takes dimension */
394 bitset++, c = DgetC(0); /* $? tests existence */
396 length++, c = DgetC(0); /* $% returns length in chars */
400 if (dimen || bitset || length)
401 stderror(ERR_SYNTAX);
404 setDolp(dolbang = putn((tcsh_number_t)backpid));
410 if (dimen || bitset || length)
411 stderror(ERR_SYNTAX);
417 static struct Strbuf wbuf; /* = Strbuf_INIT; */
420 stderror(ERR_NOTALLOWED, "$?<");
422 stderror(ERR_NOTALLOWED, "$#<");
424 stderror(ERR_NOTALLOWED, "$%<");
427 char cbuf[MB_LEN_MAX];
429 int old_pintr_disabled;
436 pintr_push_enable(&old_pintr_disabled);
437 res = force_read(OLDSTD, cbuf + cbp, 1);
438 cleanup_until(&old_pintr_disabled);
442 len = normal_mbtowc(&wc, cbuf, cbp);
445 if (cbp < MB_LEN_MAX)
446 continue; /* Maybe a partial character */
447 wc = (unsigned char)*cbuf | INVALID_BYTE;
451 if (cbp != (size_t)len)
452 memmove(cbuf, cbuf + len, cbp - len);
456 Strbuf_append1(&wbuf, wc);
462 len = normal_mbtowc(&wc, cbuf, cbp);
465 wc = (unsigned char)*cbuf | INVALID_BYTE;
469 if (cbp != (size_t)len)
470 memmove(cbuf, cbuf + len, cbp - len);
474 Strbuf_append1(&wbuf, wc);
476 Strbuf_terminate(&wbuf);
480 setDolp(wbuf.s); /* Kept allocated until next $< expansion */
486 Strbuf_append(name, STRargv);
487 Strbuf_terminate(name);
489 subscr = -1; /* Prevent eating [...] */
494 np = dimen ? STRargv : (bitset ? STRstatus : NULL);
497 Strbuf_append(name, np);
498 Strbuf_terminate(name);
500 subscr = -1; /* Prevent eating [...] */
505 stderror(ERR_SYNTAX);
511 stderror(ERR_NOTALLOWED, "$#<num>");
514 subscr = subscr * 10 + c - '0';
516 } while (c != DEOF && Isdigit(c));
522 dolp = dolzero ? STR1 : STR0;
527 stderror(ERR_DOLZERO);
529 length = Strlen(ffile);
530 addla(putn((tcsh_number_t)length));
541 stderror(ERR_NOTALLOWED, "$?<num>");
543 stderror(ERR_NOTALLOWED, "$%<num>");
553 if (c == DEOF || !alnum(c)) {
554 np = dimen ? STRargv : (bitset ? STRstatus : NULL);
557 Strbuf_append(name, np);
558 Strbuf_terminate(name);
560 subscr = -1; /* Prevent eating [...] */
565 stderror(ERR_VARALNUM);
568 Strbuf_append1(name, (Char) c);
570 if (c == DEOF || !alnum(c))
573 Strbuf_terminate(name);
578 dolp = (vp || getenv(short2str(name->s))) ? STR1 : STR0;
582 if (vp == NULL || vp->vec == NULL) {
583 np = str2short(getenv(short2str(name->s)));
585 static Char *env_val; /* = NULL; */
590 addla(putn((tcsh_number_t)Strlen(np)));
593 env_val = Strsave(np);
603 upb = blklen(vp->vec);
604 if (dimen == 0 && subscr == 0 && c == '[') {
605 name = Strbuf_alloc();
606 cleanup_push(name, Strbuf_free);
609 c = DgetC(DODOL); /* Allow $ expand within [ ] */
612 if (c == '\n' || c == DEOF)
614 Strbuf_append1(name, (Char) c);
616 Strbuf_terminate(name);
618 if (dolp || dolcnt) /* $ exp must end before ] */
619 stderror(ERR_EXPORD);
621 stderror(ERR_SYNTAX);
625 for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
627 if (i < 0 || (i > upb && !any("-*", *np))) {
629 dolerror(vp->v_name);
634 upb = lwb, np = STRstar;
639 stderror(ERR_MISSING, '-');
647 i = i * 10 + *np++ - '0';
648 if (i < 0 || i > upb) {
650 dolerror(vp->v_name);
662 dolerror(vp->v_name);
668 stderror(ERR_SYNTAX);
681 /* this is a kludge. It prevents Dgetdol() from */
682 /* pushing erroneous ${#<error> values into the labuf. */
686 stderror(ERR_MISSING, '}');
689 addla(putn((tcsh_number_t)(upb - lwb + 1)));
694 for (i = lwb - 1, length = 0; i < upb; i++)
695 length += Strlen(vp->vec[i]);
697 /* We don't want that, since we can always compute it by adding $#xxx */
698 length += i - 1; /* Add the number of spaces in */
700 addla(putn((tcsh_number_t)length));
705 dolnxt = &vp->vec[lwb - 1];
706 dolcnt = upb - lwb + 1;
712 stderror(ERR_MISSING, '}');
726 dolmcnts = xrealloc(dolmcnts, ndolflags * sizeof(int));
727 dolaflags = xrealloc(dolaflags, ndolflags * sizeof(int));
728 c = DgetC(0), dolmcnts[ndolflags - 1] = 1, dolaflags[ndolflags - 1] = 0;
729 if (c == 'g' || c == 'a') {
731 dolmcnts[ndolflags - 1] = INT_MAX;
733 dolaflags[ndolflags - 1] = 1;
737 if ((c == 'g' && dolmcnts[ndolflags - 1] != INT_MAX) ||
738 (c == 'a' && dolaflags[ndolflags - 1] == 0)) {
740 dolmcnts[ndolflags - 1] = INT_MAX;
742 dolaflags[ndolflags - 1] = 1;
747 if (c == 's') { /* [eichin:19910926.0755EST] */
749 eChar delim = DgetC(0);
750 Strbuf_append1(&dolmod, (Char) c);
751 Strbuf_append1(&dolmod, (Char) delim);
753 if (delim == DEOF || !delim || letter(delim)
754 || Isdigit(delim) || any(" \t\n", delim)) {
755 seterror(ERR_BADSUBST);
758 while ((c = DgetC(0)) != DEOF) {
759 Strbuf_append1(&dolmod, (Char) c);
760 if (c == delim) delimcnt--;
761 if (!delimcnt) break;
764 seterror(ERR_BADSUBST);
769 if (!any(TCSH_MODIFIERS, c))
770 stderror(ERR_BADMOD, (int)c);
771 Strbuf_append1(&dolmod, (Char) c);
773 dolmcnts[ndolflags - 1] = INT_MAX;
776 while ((c = DgetC(0)) == ':');
784 all_dolmcnts_are_0(void)
787 for (; i < ndolflags; ++i) {
788 if (dolmcnts[i] != 0)
801 if (dolmod.len == 0 || all_dolmcnts_are_0()) {
806 for (i = 0; i < dolmod.len; i++) {
809 /* handle s// [eichin:19910926.0510EST] */
810 if (dolmod.s[i] == 's') {
812 Char *lhsub, *rhsub, *np;
813 size_t lhlen = 0, rhlen = 0;
814 /* keep track of where the last :a match hit */
815 ptrdiff_t last_match = 0;
817 delim = dolmod.s[++i];
818 if (!delim || letter(delim)
819 || Isdigit(delim) || any(" \t\n", delim)) {
820 seterror(ERR_BADSUBST);
823 lhsub = &dolmod.s[++i];
824 while (dolmod.s[i] != delim && dolmod.s[++i]) {
828 rhsub = &dolmod.s[++i];
829 while (dolmod.s[i] != delim && dolmod.s[++i]) {
836 if (dolmcnts[nthMod] != 0) {
840 dp = Strstr(dp + last_match, lhsub);
842 ptrdiff_t diff = dp - cp;
843 size_t len = (Strlen(cp) + 1 - lhlen + rhlen);
844 np = xmalloc(len * sizeof(Char));
845 (void) Strncpy(np, cp, diff);
846 (void) Strcpy(np + diff, rhsub);
847 (void) Strcpy(np + diff + rhlen, dp + lhlen);
848 last_match = diff + rhlen;
854 if (diff >= (ssize_t)len)
857 /* should this do a seterror? */
861 while (dolaflags[nthMod] != 0);
864 * restore dolmod for additional words
866 dolmod.s[i] = rhsub[-1] = (Char) delim;
867 } else if (dolmcnts[nthMod] != 0) {
870 if ((dp = domod(cp, dolmod.s[i])) != NULL) {
872 if (Strcmp(cp, dp) == 0) {
885 while (dolaflags[nthMod] != 0);
887 if (didmod && dolmcnts[nthMod] != INT_MAX)
916 if ((c = Dpeekrd) != 0) {
920 if (Dcp && (c = *Dcp++))
921 return (c & (QUOTE | TRIM));
941 inheredoc_cleanup(void *dummy)
951 (void) gettimeofday(&tv, NULL);
952 return putn((((tcsh_number_t)tv.tv_sec) ^
953 ((tcsh_number_t)tv.tv_usec) ^
954 ((tcsh_number_t)getpid())) & 0x00ffffff);
956 return putn(getpid());
961 * Form a shell temporary file (in unit 0) from the words
962 * of the shell input up to EOF or a line the same as "term".
963 * Unit 0 should have been closed before this call.
970 struct Strbuf lbuf = Strbuf_INIT, mbuf = Strbuf_INIT;
971 Char obuf[BUFSIZE + 1];
972 #define OBUF_END (obuf + sizeof(obuf) / sizeof (*obuf) - 1)
973 Char *lbp, *obp, *mbp;
977 char *tmp = short2str(shtemp);
978 char *dot = strrchr(tmp, '.');
981 stderror(ERR_NAME | ERR_NOMATCH);
982 strcpy(dot, TMP_TEMPLATE);
985 if (mkstemp(tmp) == -1)
986 stderror(ERR_SYSTEM, tmp, strerror(errno));
987 #else /* !HAVE_MKSTEMP */
989 # ifndef WINNT_NATIVE
992 # endif /* WINNT_NATIVE */
993 tmp = short2str(shtemp);
995 if (xcreat(tmp, 0600) < 0)
996 stderror(ERR_SYSTEM, tmp, strerror(errno));
999 if (xopen(tmp, O_RDWR|O_CREAT|O_EXCL|O_TEMPORARY|O_LARGEFILE, 0600) ==
1002 # ifndef WINNT_NATIVE
1003 if (errno == EEXIST) {
1004 if (unlink(tmp) == -1) {
1007 shtemp = Strspl(STRtmpsh, mbp);
1012 # endif /* WINNT_NATIVE */
1015 stderror(ERR_SYSTEM, tmp, strerror(errno));
1017 #endif /* HAVE_MKSTEMP */
1018 (void) unlink(tmp); /* 0 0 inode! */
1028 cleanup_push(&inheredoc, inheredoc_cleanup);
1031 #endif /* WINNT_NATIVE */
1032 cleanup_push(&lbuf, Strbuf_cleanup);
1033 cleanup_push(&mbuf, Strbuf_cleanup);
1042 c = readc(1); /* 1 -> Want EOF returns */
1043 if (c == CHAR_ERR || c == '\n')
1045 if ((c &= TRIM) != 0)
1046 Strbuf_append1(&lbuf, (Char) c);
1048 Strbuf_terminate(&lbuf);
1050 /* Catch EOF in the middle of a line. */
1051 if (c == CHAR_ERR && lbuf.len != 0)
1055 * Check for EOF or compare to terminator -- before expansion
1057 if (c == CHAR_ERR || eq(lbuf.s, term))
1061 * If term was quoted or -n just pass it on
1063 if (quoted || noexec) {
1064 Strbuf_append1(&lbuf, '\n');
1065 Strbuf_terminate(&lbuf);
1066 for (lbp = lbuf.s; (c = *lbp++) != 0;) {
1068 if (obp == OBUF_END) {
1069 tmp = short2str(obuf);
1070 (void) xwrite(0, tmp, strlen (tmp));
1078 * Term wasn't quoted so variable and then command expand the input
1088 if ((c &= TRIM) == 0)
1090 /* \ quotes \ $ ` here */
1093 if (!any("$\\`", c))
1094 unDgetC(c | QUOTE), c = '\\';
1098 Strbuf_append1(&mbuf, (Char) c);
1100 Strbuf_terminate(&mbuf);
1103 * If any ` in line do command substitution
1106 if (Strchr(mbp, '`') != NULL) {
1108 * 1 arg to dobackp causes substitution to be literal. Words are
1109 * broken only at newlines so that all blanks and tabs are
1110 * preserved. Blank lines (null words) are not discarded.
1112 words = dobackp(mbp, 1);
1115 /* Setup trivial vector similar to return of dobackp */
1116 Dv[0] = mbp, Dv[1] = NULL, words = Dv;
1119 * Resurrect the words from the command substitution each separated by
1120 * a newline. Note that the last newline of a command substitution
1121 * will have been discarded, but we put a newline after the last word
1122 * because this represents the newline after the last input line!
1124 for (vp= words; *vp; vp++) {
1125 for (mbp = *vp; *mbp; mbp++) {
1126 *obp++ = *mbp & TRIM;
1127 if (obp == OBUF_END) {
1128 tmp = short2str(obuf);
1129 (void) xwrite(0, tmp, strlen (tmp));
1134 if (obp == OBUF_END) {
1135 tmp = short2str(obuf);
1136 (void) xwrite(0, tmp, strlen (tmp));
1144 tmp = short2str(obuf);
1145 (void) xwrite(0, tmp, strlen (tmp));
1146 (void) lseek(0, (off_t) 0, L_SET);
1147 cleanup_until(&inheredoc);