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
41 * These routines perform variable substitution and quoting via ' and ".
42 * To this point these constructs have been preserved in the divided
43 * input words. Here we expand variables and turn quoting via ' and " into
44 * QUOTE bits on characters (which prevent further interpretation).
45 * If the `:q' modifier was applied during history expansion, then
46 * some QUOTEing may have occurred already, so we dont "trim()" here.
49 static eChar Dpeekc; /* Peek for DgetC */
50 static eChar Dpeekrd; /* Peek for Dreadc */
51 static Char *Dcp, *const *Dvp; /* Input vector for Dreadc */
55 #define unDgetC(c) Dpeekc = c
57 #define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
60 * The following variables give the information about the current
61 * $ expansion, recording the current word position, the remaining
62 * words within this expansion, the count of remaining words, and the
63 * information about any : modifier which is being applied.
65 static Char *dolp; /* Remaining chars from this word */
66 static Char **dolnxt; /* Further words */
67 static int dolcnt; /* Count of further words */
68 static struct Strbuf dolmod; /* = Strbuf_INIT; : modifier characters */
70 static int ndolflags; /* keep track of mod counts for each modifier */
71 static int *dolmcnts; /* :gx -> INT_MAX, else 1 */
72 static int *dolaflags; /* :ax -> 1, else 0 */
74 static Char **Dfix2 (Char *const *);
75 static int Dpack (struct Strbuf *);
76 static int Dword (struct blk_buf *);
77 static void dolerror (Char *);
78 static eChar DgetC (int);
79 static void Dgetdol (void);
80 static void fixDolMod (void);
81 static void setDolp (Char *);
82 static void unDredc (eChar);
83 static eChar Dredc (void);
84 static void Dtestq (Char);
87 * Fix up the $ expansions and quotations in the
88 * argument list to command t.
91 Dfix(struct command *t)
98 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
99 for (pp = t->t_dcom; (p = *pp++) != NULL;) {
101 if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
104 expanded = Dfix2(t->t_dcom); /* found one */
106 t->t_dcom = expanded;
114 * $ substitute one word, for i/o redirection
119 Char *Dv[2], **expanded;
125 expanded = Dfix2(Dv);
126 if (expanded[0] == NULL || expanded[1] != NULL) {
128 setname(short2str(cp));
129 stderror(ERR_NAME | ERR_AMBIG);
131 cp = Strsave(expanded[0]);
137 * Subroutine to do actual fixing after state initialization.
140 Dfix2(Char *const *v)
142 struct blk_buf *bb = bb_alloc();
146 Dcp = STRNULL; /* Setup input vector for Dreadc */
148 unDredc(0); /* Clear out any old peeks (at error) */
150 dolcnt = 0; /* Clear out residual $ expands (...) */
151 cleanup_push(bb, bb_free);
162 * Pack up more characters in this word
165 Dpack(struct Strbuf *wbuf)
186 if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
192 Strbuf_append1(wbuf, (Char) c);
197 * Get a word. This routine is analogous to the routine
198 * word() in sh.lex.c for the main lexical input. One difference
199 * here is that we don't get a newline to terminate our expansion.
200 * Rather, DgetC will return a DEOF when we hit the end-of-input.
203 Dword(struct blk_buf *bb)
206 struct Strbuf *wbuf = Strbuf_alloc();
211 cleanup_push(wbuf, Strbuf_free);
221 /* finish this word and catch the code above the next time */
233 /* We preserve ` quotations which are done yet later */
234 Strbuf_append1(wbuf, (Char) c);
239 * Note that DgetC never returns a QUOTES character from an
240 * expansion, so only true input quotes will get us here or out.
243 dolflg = c1 == '"' ? DODOL : 0;
248 if (c == '\n' || c == DEOF) {
250 stderror(ERR_UNMATCHED, (int)c1);
252 if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE)) {
253 if (wbuf->len != 0 && (wbuf->s[wbuf->len - 1] & TRIM) == '\\')
260 * Leave any `s alone for later. Other chars are all
261 * quoted, thus `...` can tell it was within "...".
263 Strbuf_append1(wbuf, c == '`' ? '`' : c | QUOTE);
267 /* Prevent all further interpretation */
268 Strbuf_append1(wbuf, c | QUOTE);
272 /* Leave all text alone for later */
273 Strbuf_append1(wbuf, (Char) c);
281 Strbuf_append1(wbuf, '`');
283 if (Dpack(wbuf) != 0)
288 c = DgetC(0); /* No $ subst! */
289 if (c == '\n' || c == DEOF)
299 if (Dpack(wbuf) != 0)
304 cleanup_ignore(wbuf);
306 str = Strbuf_finish(wbuf);
314 * Get a character, performing $ substitution unless flag is 0.
315 * Any QUOTES character which is returned from a $ expansion is
316 * QUOTEd so that it will not be recognized above.
324 if ((c = Dpeekc) != 0) {
328 if (lap < labuf.len) {
329 c = labuf.s[lap++] & (QUOTE | TRIM);
336 if ((c = *dolp++ & (QUOTE | TRIM)) != 0)
351 if (c == '$' && flag) {
358 static Char *nulvec[] = { NULL };
359 static struct varent nulargv = {nulvec, STRargv, VAR_READWRITE,
360 { NULL, NULL, NULL }, 0 };
365 setname(short2str(s));
366 stderror(ERR_NAME | ERR_RANGE);
370 * Handle the multitudinous $ expansion forms.
377 struct varent *vp = NULL;
378 struct Strbuf *name = Strbuf_alloc();
380 int subscr = 0, lwb = 1, upb = 0;
381 int dimen = 0, bitset = 0, length = 0;
382 static Char *dolbang = NULL;
384 cleanup_push(name, Strbuf_free);
385 dolmod.len = ndolflags = 0;
388 stderror(ERR_SYNTAX);
391 if ((c & TRIM) == '\'') {
393 struct Strbuf *expanded = Strbuf_alloc();
395 cleanup_push(expanded, Strbuf_free);
398 if ((c & TRIM) == '\'')
400 if ((c & TRIM) == '\\') {
401 Strbuf_append1(name, (Char) c);
404 if (c == '\n' || c == DEOF) {
406 stderror(ERR_MISSING, '\'');
409 Strbuf_append1(name, (Char) c);
411 Strbuf_terminate(name);
412 for (cp = name->s; (c = *cp) != 0; cp++) {
413 if (c == '\\' && (c = parseescape(&cp, TRUE)) == CHAR_ERR)
415 Strbuf_append1(expanded, (Char) c | QUOTE);
417 Strbuf_terminate(expanded);
418 np = Strsave(expanded->s);
424 c = DgetC(0); /* sc is { to take } later */
425 if ((c & TRIM) == '#')
426 dimen++, c = DgetC(0); /* $# takes dimension */
428 bitset++, c = DgetC(0); /* $? tests existence */
430 length++, c = DgetC(0); /* $% returns length in chars */
434 if (dimen || bitset || length)
435 stderror(ERR_SYNTAX);
438 setDolp(dolbang = putn((tcsh_number_t)backpid));
444 if (dimen || bitset || length)
445 stderror(ERR_SYNTAX);
451 static struct Strbuf wbuf; /* = Strbuf_INIT; */
454 stderror(ERR_NOTALLOWED, "$?<");
456 stderror(ERR_NOTALLOWED, "$#<");
458 stderror(ERR_NOTALLOWED, "$%<");
461 char cbuf[MB_LEN_MAX];
463 int old_pintr_disabled;
470 pintr_push_enable(&old_pintr_disabled);
471 res = force_read(OLDSTD, cbuf + cbp, 1);
472 cleanup_until(&old_pintr_disabled);
476 len = normal_mbtowc(&wc, cbuf, cbp);
479 if (cbp < MB_LEN_MAX)
480 continue; /* Maybe a partial character */
481 wc = (unsigned char)*cbuf | INVALID_BYTE;
485 if (cbp != (size_t)len)
486 memmove(cbuf, cbuf + len, cbp - len);
490 Strbuf_append1(&wbuf, wc);
496 len = normal_mbtowc(&wc, cbuf, cbp);
499 wc = (unsigned char)*cbuf | INVALID_BYTE;
503 if (cbp != (size_t)len)
504 memmove(cbuf, cbuf + len, cbp - len);
508 Strbuf_append1(&wbuf, wc);
510 Strbuf_terminate(&wbuf);
514 setDolp(wbuf.s); /* Kept allocated until next $< expansion */
520 Strbuf_append(name, STRargv);
521 Strbuf_terminate(name);
523 subscr = -1; /* Prevent eating [...] */
528 np = dimen ? STRargv : (bitset ? STRstatus : NULL);
531 Strbuf_append(name, np);
532 Strbuf_terminate(name);
534 subscr = -1; /* Prevent eating [...] */
539 stderror(ERR_SYNTAX);
545 stderror(ERR_NOTALLOWED, "$#<num>");
548 subscr = subscr * 10 + c - '0';
550 } while (c != DEOF && Isdigit(c));
556 dolp = dolzero ? STR1 : STR0;
561 stderror(ERR_DOLZERO);
563 length = Strlen(ffile);
564 addla(putn((tcsh_number_t)length));
575 stderror(ERR_NOTALLOWED, "$?<num>");
577 stderror(ERR_NOTALLOWED, "$%<num>");
587 if (c == DEOF || !alnum(c)) {
588 np = dimen ? STRargv : (bitset ? STRstatus : NULL);
591 Strbuf_append(name, np);
592 Strbuf_terminate(name);
594 subscr = -1; /* Prevent eating [...] */
599 stderror(ERR_VARALNUM);
602 Strbuf_append1(name, (Char) c);
604 if (c == DEOF || !alnum(c))
607 Strbuf_terminate(name);
612 dolp = (vp || getenv(short2str(name->s))) ? STR1 : STR0;
616 if (vp == NULL || vp->vec == NULL) {
617 np = str2short(getenv(short2str(name->s)));
619 static Char *env_val; /* = NULL; */
624 addla(putn((tcsh_number_t)Strlen(np)));
627 env_val = Strsave(np);
637 upb = blklen(vp->vec);
638 if (dimen == 0 && subscr == 0 && c == '[') {
639 name = Strbuf_alloc();
640 cleanup_push(name, Strbuf_free);
643 c = DgetC(DODOL); /* Allow $ expand within [ ] */
646 if (c == '\n' || c == DEOF)
648 Strbuf_append1(name, (Char) c);
650 Strbuf_terminate(name);
652 if (dolp || dolcnt) /* $ exp must end before ] */
653 stderror(ERR_EXPORD);
655 stderror(ERR_SYNTAX);
659 for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
661 if (i < 0 || (i > upb && !any("-*", *np))) {
663 dolerror(vp->v_name);
668 upb = lwb, np = STRstar;
673 stderror(ERR_MISSING, '-');
681 i = i * 10 + *np++ - '0';
682 if (i < 0 || i > upb) {
684 dolerror(vp->v_name);
696 dolerror(vp->v_name);
702 stderror(ERR_SYNTAX);
715 /* this is a kludge. It prevents Dgetdol() from */
716 /* pushing erroneous ${#<error> values into the labuf. */
720 stderror(ERR_MISSING, '}');
723 addla(putn((tcsh_number_t)(upb - lwb + 1)));
728 for (i = lwb - 1, length = 0; i < upb; i++)
729 length += Strlen(vp->vec[i]);
731 /* We don't want that, since we can always compute it by adding $#xxx */
732 length += i - 1; /* Add the number of spaces in */
734 addla(putn((tcsh_number_t)length));
739 dolnxt = &vp->vec[lwb - 1];
740 dolcnt = upb - lwb + 1;
746 stderror(ERR_MISSING, '}');
760 dolmcnts = xrealloc(dolmcnts, ndolflags * sizeof(int));
761 dolaflags = xrealloc(dolaflags, ndolflags * sizeof(int));
762 c = DgetC(0), dolmcnts[ndolflags - 1] = 1, dolaflags[ndolflags - 1] = 0;
763 if (c == 'g' || c == 'a') {
765 dolmcnts[ndolflags - 1] = INT_MAX;
767 dolaflags[ndolflags - 1] = 1;
771 if ((c == 'g' && dolmcnts[ndolflags - 1] != INT_MAX) ||
772 (c == 'a' && dolaflags[ndolflags - 1] == 0)) {
774 dolmcnts[ndolflags - 1] = INT_MAX;
776 dolaflags[ndolflags - 1] = 1;
781 if (c == 's') { /* [eichin:19910926.0755EST] */
784 eChar delim = DgetC(0);
785 Strbuf_append1(&dolmod, (Char) c);
786 Strbuf_append1(&dolmod, (Char) delim);
788 if (delim == DEOF || !delim || letter(delim)
789 || Isdigit(delim) || any(" \t\n", delim)) {
790 seterror(ERR_BADSUBST);
793 while ((c = DgetC(0)) != DEOF) {
794 if (esc == 0 && c == '\\') {
798 Strbuf_append1(&dolmod, (Char) c);
799 if (!esc && c == delim) delimcnt--;
800 if (!delimcnt) break;
804 seterror(ERR_BADSUBST);
809 if (!any(TCSH_MODIFIERS, c))
810 stderror(ERR_BADMOD, (int)c);
811 Strbuf_append1(&dolmod, (Char) c);
813 dolmcnts[ndolflags - 1] = INT_MAX;
816 while ((c = DgetC(0)) == ':');
824 all_dolmcnts_are_0(void)
827 for (; i < ndolflags; ++i) {
828 if (dolmcnts[i] != 0)
841 if (dolmod.len == 0 || all_dolmcnts_are_0()) {
846 for (i = 0; i < dolmod.len; i++) {
849 /* handle s// [eichin:19910926.0510EST] */
850 if (dolmod.s[i] == 's') {
852 Char *lhsub, *rhsub, *np;
853 size_t lhlen = 0, rhlen = 0;
854 /* keep track of where the last :a match hit */
855 ptrdiff_t last_match = 0;
857 delim = dolmod.s[++i];
858 if (!delim || letter(delim)
859 || Isdigit(delim) || any(" \t\n", delim)) {
860 seterror(ERR_BADSUBST);
863 lhsub = &dolmod.s[++i];
864 while (dolmod.s[i] != delim && dolmod.s[++i]) {
868 rhsub = &dolmod.s[++i];
869 while (dolmod.s[i] != delim && dolmod.s[++i]) {
876 if (dolmcnts[nthMod] != 0) {
880 dp = Strstr(dp + last_match, lhsub);
882 ptrdiff_t diff = dp - cp;
883 size_t len = (Strlen(cp) + 1 - lhlen + rhlen);
884 np = xmalloc(len * sizeof(Char));
885 (void) Strncpy(np, cp, diff);
886 (void) Strcpy(np + diff, rhsub);
887 (void) Strcpy(np + diff + rhlen, dp + lhlen);
888 last_match = diff + rhlen;
894 if (diff >= (ssize_t)len)
897 /* should this do a seterror? */
901 while (dolaflags[nthMod] != 0);
904 * restore dolmod for additional words
906 dolmod.s[i] = rhsub[-1] = (Char) delim;
907 } else if (dolmcnts[nthMod] != 0) {
910 if ((dp = domod(cp, dolmod.s[i])) != NULL) {
912 if (Strcmp(cp, dp) == 0) {
925 while (dolaflags[nthMod] != 0);
927 if (didmod && dolmcnts[nthMod] != INT_MAX)
956 if ((c = Dpeekrd) != 0) {
960 if (Dcp && (c = *Dcp++))
961 return (c & (QUOTE | TRIM));
981 inheredoc_cleanup(void *dummy)
991 (void) gettimeofday(&tv, NULL);
992 return putn((((tcsh_number_t)tv.tv_sec) ^
993 ((tcsh_number_t)tv.tv_usec) ^
994 ((tcsh_number_t)getpid())) & 0x00ffffff);
996 return putn(getpid());
1001 * Form a shell temporary file (in unit 0) from the words
1002 * of the shell input up to EOF or a line the same as "term".
1003 * Unit 0 should have been closed before this call.
1010 struct Strbuf lbuf = Strbuf_INIT, mbuf = Strbuf_INIT;
1011 Char obuf[BUFSIZE + 1];
1012 #define OBUF_END (obuf + sizeof(obuf) / sizeof (*obuf) - 1)
1013 Char *lbp, *obp, *mbp;
1017 char *tmp = short2str(shtemp);
1018 char *dot = strrchr(tmp, '.');
1021 stderror(ERR_NAME | ERR_NOMATCH);
1022 strcpy(dot, TMP_TEMPLATE);
1025 if (mkstemp(tmp) == -1)
1026 stderror(ERR_SYSTEM, tmp, strerror(errno));
1027 #else /* !HAVE_MKSTEMP */
1029 # ifndef WINNT_NATIVE
1032 # endif /* WINNT_NATIVE */
1033 tmp = short2str(shtemp);
1035 if (xcreat(tmp, 0600) < 0)
1036 stderror(ERR_SYSTEM, tmp, strerror(errno));
1039 if (xopen(tmp, O_RDWR|O_CREAT|O_EXCL|O_TEMPORARY|O_LARGEFILE, 0600) ==
1042 # ifndef WINNT_NATIVE
1043 if (errno == EEXIST) {
1044 if (unlink(tmp) == -1) {
1047 shtemp = Strspl(STRtmpsh, mbp);
1052 # endif /* WINNT_NATIVE */
1055 stderror(ERR_SYSTEM, tmp, strerror(errno));
1057 #endif /* HAVE_MKSTEMP */
1058 (void) unlink(tmp); /* 0 0 inode! */
1068 cleanup_push(&inheredoc, inheredoc_cleanup);
1071 #endif /* WINNT_NATIVE */
1072 cleanup_push(&lbuf, Strbuf_cleanup);
1073 cleanup_push(&mbuf, Strbuf_cleanup);
1082 c = readc(1); /* 1 -> Want EOF returns */
1083 if (c == CHAR_ERR || c == '\n')
1085 if ((c &= TRIM) != 0)
1086 Strbuf_append1(&lbuf, (Char) c);
1088 Strbuf_terminate(&lbuf);
1090 /* Catch EOF in the middle of a line. */
1091 if (c == CHAR_ERR && lbuf.len != 0)
1095 * Check for EOF or compare to terminator -- before expansion
1097 if (c == CHAR_ERR || eq(lbuf.s, term))
1101 * If term was quoted or -n just pass it on
1103 if (quoted || noexec) {
1104 Strbuf_append1(&lbuf, '\n');
1105 Strbuf_terminate(&lbuf);
1106 for (lbp = lbuf.s; (c = *lbp++) != 0;) {
1108 if (obp == OBUF_END) {
1109 tmp = short2str(obuf);
1110 (void) xwrite(0, tmp, strlen (tmp));
1118 * Term wasn't quoted so variable and then command expand the input
1128 if ((c &= TRIM) == 0)
1130 /* \ quotes \ $ ` here */
1133 if (!any("$\\`", c))
1134 unDgetC(c | QUOTE), c = '\\';
1138 Strbuf_append1(&mbuf, (Char) c);
1140 Strbuf_terminate(&mbuf);
1143 * If any ` in line do command substitution
1146 if (Strchr(mbp, '`') != NULL) {
1148 * 1 arg to dobackp causes substitution to be literal. Words are
1149 * broken only at newlines so that all blanks and tabs are
1150 * preserved. Blank lines (null words) are not discarded.
1152 words = dobackp(mbp, 1);
1155 /* Setup trivial vector similar to return of dobackp */
1156 Dv[0] = mbp, Dv[1] = NULL, words = Dv;
1159 * Resurrect the words from the command substitution each separated by
1160 * a newline. Note that the last newline of a command substitution
1161 * will have been discarded, but we put a newline after the last word
1162 * because this represents the newline after the last input line!
1164 for (vp= words; *vp; vp++) {
1165 for (mbp = *vp; *mbp; mbp++) {
1166 *obp++ = *mbp & TRIM;
1167 if (obp == OBUF_END) {
1168 tmp = short2str(obuf);
1169 (void) xwrite(0, tmp, strlen (tmp));
1174 if (obp == OBUF_END) {
1175 tmp = short2str(obuf);
1176 (void) xwrite(0, tmp, strlen (tmp));
1184 tmp = short2str(obuf);
1185 (void) xwrite(0, tmp, strlen (tmp));
1186 (void) lseek(0, (off_t) 0, L_SET);
1187 cleanup_until(&inheredoc);