1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.dol.c,v 3.70 2006/09/14 18:30:16 christos Exp $ */
3 * sh.dol.c: Variable substitutions
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. 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
35 RCSID("$tcsh: sh.dol.c,v 3.70 2006/09/14 18:30:16 christos Exp $")
42 * These routines perform variable substitution and quoting via ' and ".
43 * To this point these constructs have been preserved in the divided
44 * input words. Here we expand variables and turn quoting via ' and " into
45 * QUOTE bits on characters (which prevent further interpretation).
46 * If the `:q' modifier was applied during history expansion, then
47 * some QUOTEing may have occurred already, so we dont "trim()" here.
50 static Char Dpeekc; /* Peek for DgetC */
51 static eChar Dpeekrd; /* Peek for Dreadc */
52 static Char *Dcp, *const *Dvp; /* Input vector for Dreadc */
56 #define unDgetC(c) Dpeekc = c
58 #define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
61 * The following variables give the information about the current
62 * $ expansion, recording the current word position, the remaining
63 * words within this expansion, the count of remaining words, and the
64 * information about any : modifier which is being applied.
66 static Char *dolp; /* Remaining chars from this word */
67 static Char **dolnxt; /* Further words */
68 static int dolcnt; /* Count of further words */
69 static struct Strbuf dolmod; /* = Strbuf_INIT; : modifier characters */
70 static int dolmcnt; /* :gx -> INT_MAX, else 1 */
71 static int dol_flag_a; /* :ax -> 1, else 0 */
73 static Char **Dfix2 (Char *const *);
74 static int Dpack (struct Strbuf *);
75 static int Dword (struct blk_buf *);
76 static void dolerror (Char *);
77 static eChar DgetC (int);
78 static void Dgetdol (void);
79 static void fixDolMod (void);
80 static void setDolp (Char *);
81 static void unDredc (eChar);
82 static eChar Dredc (void);
83 static void Dtestq (Char);
86 * Fix up the $ expansions and quotations in the
87 * argument list to command t.
90 Dfix(struct command *t)
97 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
98 for (pp = t->t_dcom; (p = *pp++) != NULL;) {
100 if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
103 expanded = Dfix2(t->t_dcom); /* found one */
105 t->t_dcom = expanded;
113 * $ substitute one word, for i/o redirection
118 Char *Dv[2], **expanded;
124 expanded = Dfix2(Dv);
125 if (expanded[0] == NULL || expanded[1] != NULL) {
127 setname(short2str(cp));
128 stderror(ERR_NAME | ERR_AMBIG);
130 cp = Strsave(expanded[0]);
136 * Subroutine to do actual fixing after state initialization.
139 Dfix2(Char *const *v)
141 struct blk_buf bb = BLK_BUF_INIT;
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_cleanup);
154 return bb_finish(&bb);
158 * Pack up more characters in this word
161 Dpack(struct Strbuf *wbuf)
182 if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
188 Strbuf_append1(wbuf, (Char) c);
193 * Get a word. This routine is analogous to the routine
194 * word() in sh.lex.c for the main lexical input. One difference
195 * here is that we don't get a newline to terminate our expansion.
196 * Rather, DgetC will return a DEOF when we hit the end-of-input.
199 Dword(struct blk_buf *bb)
202 struct Strbuf wbuf = Strbuf_INIT;
206 cleanup_push(&wbuf, Strbuf_cleanup);
213 cleanup_until(&wbuf);
216 /* finish this word and catch the code above the next time */
228 /* We preserve ` quotations which are done yet later */
229 Strbuf_append1(&wbuf, (Char) c);
234 * Note that DgetC never returns a QUOTES character from an
235 * expansion, so only true input quotes will get us here or out.
238 dolflg = c1 == '"' ? DODOL : 0;
243 if (c == '\n' || c == DEOF)
244 stderror(ERR_UNMATCHED, (int)c1);
245 if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE)) {
246 if (wbuf.len != 0 && (wbuf.s[wbuf.len - 1] & TRIM) == '\\')
253 * Leave any `s alone for later. Other chars are all
254 * quoted, thus `...` can tell it was within "...".
256 Strbuf_append1(&wbuf, c == '`' ? '`' : c | QUOTE);
260 /* Prevent all further interpretation */
261 Strbuf_append1(&wbuf, c | QUOTE);
265 /* Leave all text alone for later */
266 Strbuf_append1(&wbuf, (Char) c);
274 Strbuf_append1(&wbuf, '`');
276 if (Dpack(&wbuf) != 0)
281 c = DgetC(0); /* No $ subst! */
282 if (c == '\n' || c == DEOF)
292 if (Dpack(&wbuf) != 0)
297 cleanup_ignore(&wbuf);
298 cleanup_until(&wbuf);
299 bb_append(bb, Strbuf_finish(&wbuf));
305 * Get a character, performing $ substitution unless flag is 0.
306 * Any QUOTES character which is returned from a $ expansion is
307 * QUOTEd so that it will not be recognized above.
315 if ((c = Dpeekc) != 0) {
319 if (lap < labuf.len) {
320 c = labuf.s[lap++] & (QUOTE | TRIM);
327 if ((c = *dolp++ & (QUOTE | TRIM)) != 0)
342 if (c == '$' && flag) {
349 static Char *nulvec[] = { NULL };
350 static struct varent nulargv = {nulvec, STRargv, VAR_READWRITE,
351 { NULL, NULL, NULL }, 0 };
356 setname(short2str(s));
357 stderror(ERR_NAME | ERR_RANGE);
361 * Handle the multitudinous $ expansion forms.
368 struct varent *vp = NULL;
369 struct Strbuf name = Strbuf_INIT;
371 int subscr = 0, lwb = 1, upb = 0;
372 int dimen = 0, bitset = 0, length = 0;
373 static Char *dolbang = NULL;
375 cleanup_push(&name, Strbuf_cleanup);
376 dolmod.len = dolmcnt = dol_flag_a = 0;
379 stderror(ERR_SYNTAX);
383 c = DgetC(0); /* sc is { to take } later */
384 if ((c & TRIM) == '#')
385 dimen++, c = DgetC(0); /* $# takes dimension */
387 bitset++, c = DgetC(0); /* $? tests existence */
389 length++, c = DgetC(0); /* $% returns length in chars */
393 if (dimen || bitset || length)
394 stderror(ERR_SYNTAX);
397 setDolp(dolbang = putn(backpid));
399 cleanup_until(&name);
403 if (dimen || bitset || length)
404 stderror(ERR_SYNTAX);
406 cleanup_until(&name);
410 static struct Strbuf wbuf; /* = Strbuf_INIT; */
413 stderror(ERR_NOTALLOWED, "$?<");
415 stderror(ERR_NOTALLOWED, "$#<");
417 stderror(ERR_NOTALLOWED, "$%<");
420 char cbuf[MB_LEN_MAX];
422 int old_pintr_disabled;
429 pintr_push_enable(&old_pintr_disabled);
430 res = force_read(OLDSTD, cbuf + cbp, 1);
431 cleanup_until(&old_pintr_disabled);
435 len = normal_mbtowc(&wc, cbuf, cbp);
438 if (cbp < MB_LEN_MAX)
439 continue; /* Maybe a partial character */
440 wc = (unsigned char)*cbuf | INVALID_BYTE;
444 if (cbp != (size_t)len)
445 memmove(cbuf, cbuf + len, cbp - len);
449 Strbuf_append1(&wbuf, wc);
455 len = normal_mbtowc(&wc, cbuf, cbp);
458 wc = (unsigned char)*cbuf | INVALID_BYTE;
462 if (cbp != (size_t)len)
463 memmove(cbuf, cbuf + len, cbp - len);
467 Strbuf_append1(&wbuf, wc);
469 Strbuf_terminate(&wbuf);
473 setDolp(wbuf.s); /* Kept allocated until next $< expansion */
474 cleanup_until(&name);
479 Strbuf_append(&name, STRargv);
480 Strbuf_terminate(&name);
482 subscr = -1; /* Prevent eating [...] */
487 np = dimen ? STRargv : (bitset ? STRstatus : NULL);
490 Strbuf_append(&name, np);
491 Strbuf_terminate(&name);
493 subscr = -1; /* Prevent eating [...] */
498 stderror(ERR_SYNTAX);
504 stderror(ERR_NOTALLOWED, "$#<num>");
507 subscr = subscr * 10 + c - '0';
509 } while (c != DEOF && Isdigit(c));
515 dolp = dolzero ? STR1 : STR0;
516 cleanup_until(&name);
520 stderror(ERR_DOLZERO);
522 length = Strlen(ffile);
529 cleanup_until(&name);
534 stderror(ERR_NOTALLOWED, "$?<num>");
536 stderror(ERR_NOTALLOWED, "$%<num>");
541 cleanup_until(&name);
546 if (c == DEOF || !alnum(c)) {
547 np = dimen ? STRargv : (bitset ? STRstatus : NULL);
550 Strbuf_append(&name, np);
551 Strbuf_terminate(&name);
553 subscr = -1; /* Prevent eating [...] */
558 stderror(ERR_VARALNUM);
561 Strbuf_append1(&name, (Char) c);
563 if (c == DEOF || !alnum(c))
566 Strbuf_terminate(&name);
571 dolp = (vp || getenv(short2str(name.s))) ? STR1 : STR0;
572 cleanup_until(&name);
575 if (vp == NULL || vp->vec == NULL) {
576 np = str2short(getenv(short2str(name.s)));
578 static Char *env_val; /* = NULL; */
580 cleanup_until(&name);
583 env_val = Strsave(np);
590 cleanup_until(&name);
592 upb = blklen(vp->vec);
593 if (dimen == 0 && subscr == 0 && c == '[') {
595 cleanup_push(&name, Strbuf_cleanup);
598 c = DgetC(DODOL); /* Allow $ expand within [ ] */
601 if (c == '\n' || c == DEOF)
603 Strbuf_append1(&name, (Char) c);
605 Strbuf_terminate(&name);
607 if (dolp || dolcnt) /* $ exp must end before ] */
608 stderror(ERR_EXPORD);
610 stderror(ERR_SYNTAX);
614 for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
616 if ((i < 0 || i > upb) && !any("-*", *np)) {
617 cleanup_until(&name);
618 dolerror(vp->v_name);
623 upb = lwb, np = STRstar;
628 stderror(ERR_MISSING, '-');
636 i = i * 10 + *np++ - '0';
637 if (i < 0 || i > upb) {
638 cleanup_until(&name);
639 dolerror(vp->v_name);
650 cleanup_until(&name);
651 dolerror(vp->v_name);
657 stderror(ERR_SYNTAX);
658 cleanup_until(&name);
670 /* this is a kludge. It prevents Dgetdol() from */
671 /* pushing erroneous ${#<error> values into the labuf. */
675 stderror(ERR_MISSING, '}');
678 addla(putn(upb - lwb + 1));
683 for (i = lwb - 1, length = 0; i < upb; i++)
684 length += Strlen(vp->vec[i]);
686 /* We don't want that, since we can always compute it by adding $#xxx */
687 length += i - 1; /* Add the number of spaces in */
694 dolnxt = &vp->vec[lwb - 1];
695 dolcnt = upb - lwb + 1;
701 stderror(ERR_MISSING, '}');
713 c = DgetC(0), dolmcnt = 1, dol_flag_a = 0;
714 if (c == 'g' || c == 'a') {
721 if ((c == 'g' && dolmcnt != INT_MAX) ||
722 (c == 'a' && dol_flag_a == 0)) {
730 if (c == 's') { /* [eichin:19910926.0755EST] */
732 eChar delim = DgetC(0);
733 Strbuf_append1(&dolmod, (Char) c);
734 Strbuf_append1(&dolmod, (Char) delim);
736 if (delim == DEOF || !delim || letter(delim)
737 || Isdigit(delim) || any(" \t\n", delim)) {
738 seterror(ERR_BADSUBST);
741 while ((c = DgetC(0)) != DEOF) {
742 Strbuf_append1(&dolmod, (Char) c);
743 if(c == delim) delimcnt--;
747 seterror(ERR_BADSUBST);
752 if (!any("luhtrqxes", c))
753 stderror(ERR_BADMOD, (int)c);
754 Strbuf_append1(&dolmod, (Char) c);
758 while ((c = DgetC(0)) == ':');
771 if (dolmod.len == 0 || dolmcnt == 0) {
776 for (i = 0; i < dolmod.len; i++) {
779 /* handle s// [eichin:19910926.0510EST] */
780 if(dolmod.s[i] == 's') {
782 Char *lhsub, *rhsub, *np;
783 size_t lhlen = 0, rhlen = 0;
785 delim = dolmod.s[++i];
786 if (!delim || letter(delim)
787 || Isdigit(delim) || any(" \t\n", delim)) {
788 seterror(ERR_BADSUBST);
791 lhsub = &dolmod.s[++i];
792 while(dolmod.s[i] != delim && dolmod.s[++i]) {
796 rhsub = &dolmod.s[++i];
797 while(dolmod.s[i] != delim && dolmod.s[++i]) {
806 dp = Strstr(dp, lhsub);
808 ptrdiff_t diff = dp - cp;
809 np = xmalloc((Strlen(cp) + 1 - lhlen + rhlen) *
811 (void) Strncpy(np, cp, diff);
812 (void) Strcpy(np + diff, rhsub);
813 (void) Strcpy(np + diff + rhlen, dp + lhlen);
820 /* should this do a seterror? */
824 while (dol_flag_a != 0);
826 * restore dolmod for additional words
828 dolmod.s[i] = rhsub[-1] = (Char) delim;
832 if ((dp = domod(cp, dolmod.s[i])) != NULL) {
834 if (Strcmp(cp, dp) == 0) {
847 while (dol_flag_a != 0);
849 if (didmod && dolmcnt != INT_MAX)
876 if ((c = Dpeekrd) != 0) {
880 if (Dcp && (c = *Dcp++))
881 return (c & (QUOTE | TRIM));
901 inheredoc_cleanup(void *dummy)
908 * Form a shell temporary file (in unit 0) from the words
909 * of the shell input up to EOF or a line the same as "term".
910 * Unit 0 should have been closed before this call.
917 struct Strbuf lbuf = Strbuf_INIT, mbuf = Strbuf_INIT;
918 Char obuf[BUFSIZE + 1];
919 #define OBUF_END (obuf + sizeof(obuf) / sizeof (*obuf) - 1)
920 Char *lbp, *obp, *mbp;
928 #endif /* WINNT_NATIVE */
929 tmp = short2str(shtemp);
932 if (xcreat(tmp, 0600) < 0)
933 stderror(ERR_SYSTEM, tmp, strerror(errno));
937 # define O_TEMPORARY 0
942 if (xopen(tmp, O_RDWR|O_CREAT|O_EXCL|O_TEMPORARY|O_LARGEFILE, 0600) ==
946 if (errno == EEXIST) {
947 if (unlink(tmp) == -1) {
948 (void) gettimeofday(&tv, NULL);
950 mbp = putn((((int)tv.tv_sec) ^
951 ((int)tv.tv_usec) ^ ((int)getpid())) & 0x00ffffff);
952 shtemp = Strspl(STRtmpsh, mbp);
957 #endif /* WINNT_NATIVE */
960 stderror(ERR_SYSTEM, tmp, strerror(errno));
962 (void) unlink(tmp); /* 0 0 inode! */
972 cleanup_push(&inheredoc, inheredoc_cleanup);
975 #endif /* WINNT_NATIVE */
980 setmode(0, O_BINARY);
982 cleanup_push(&lbuf, Strbuf_cleanup);
983 cleanup_push(&mbuf, Strbuf_cleanup);
992 c = readc(1); /* 1 -> Want EOF returns */
993 if (c == CHAR_ERR || c == '\n')
995 if ((c &= TRIM) != 0)
996 Strbuf_append1(&lbuf, (Char) c);
998 Strbuf_terminate(&lbuf);
1001 * Check for EOF or compare to terminator -- before expansion
1003 if (c == CHAR_ERR || eq(lbuf.s, term))
1007 * If term was quoted or -n just pass it on
1009 if (quoted || noexec) {
1010 Strbuf_append1(&lbuf, '\n');
1011 Strbuf_terminate(&lbuf);
1012 for (lbp = lbuf.s; (c = *lbp++) != 0;) {
1014 if (obp == OBUF_END) {
1015 tmp = short2str(obuf);
1016 (void) xwrite(0, tmp, strlen (tmp));
1024 * Term wasn't quoted so variable and then command expand the input
1034 if ((c &= TRIM) == 0)
1036 /* \ quotes \ $ ` here */
1039 if (!any("$\\`", c))
1040 unDgetC(c | QUOTE), c = '\\';
1044 Strbuf_append1(&mbuf, (Char) c);
1046 Strbuf_terminate(&mbuf);
1049 * If any ` in line do command substitution
1052 if (Strchr(mbp, '`') != NULL) {
1054 * 1 arg to dobackp causes substitution to be literal. Words are
1055 * broken only at newlines so that all blanks and tabs are
1056 * preserved. Blank lines (null words) are not discarded.
1058 words = dobackp(mbp, 1);
1061 /* Setup trivial vector similar to return of dobackp */
1062 Dv[0] = mbp, Dv[1] = NULL, words = Dv;
1065 * Resurrect the words from the command substitution each separated by
1066 * a newline. Note that the last newline of a command substitution
1067 * will have been discarded, but we put a newline after the last word
1068 * because this represents the newline after the last input line!
1070 for (vp= words; *vp; vp++) {
1071 for (mbp = *vp; *mbp; mbp++) {
1072 *obp++ = *mbp & TRIM;
1073 if (obp == OBUF_END) {
1074 tmp = short2str(obuf);
1075 (void) xwrite(0, tmp, strlen (tmp));
1080 if (obp == OBUF_END) {
1081 tmp = short2str(obuf);
1082 (void) xwrite(0, tmp, strlen (tmp));
1090 tmp = short2str(obuf);
1091 (void) xwrite(0, tmp, strlen (tmp));
1092 (void) lseek(0, (off_t) 0, L_SET);
1093 cleanup_until(&inheredoc);