1 /* $Header: /src/pub/tcsh/sh.dol.c,v 3.55 2004/12/25 21:15:06 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("$Id: sh.dol.c,v 3.55 2004/12/25 21:15:06 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, **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 #define MAXWLEN (BUFSIZE - 4)
68 #define MAXMOD MAXWLEN /* This cannot overflow */
70 static Char *dolp; /* Remaining chars from this word */
71 static Char **dolnxt; /* Further words */
72 static int dolcnt; /* Count of further words */
74 static Char dolmod; /* : modifier character */
76 static Char dolmod[MAXMOD]; /* : modifier character */
77 static int dolnmod; /* Number of modifiers */
79 static int dolmcnt; /* :gx -> 10000, else 1 */
80 static int dolwcnt; /* :ax -> 10000, else 1 */
82 static void Dfix2 __P((Char **));
83 static Char *Dpack __P((Char *, Char *));
84 static int Dword __P((void));
85 static void dolerror __P((Char *));
86 static eChar DgetC __P((int));
87 static void Dgetdol __P((void));
88 static void fixDolMod __P((void));
89 static void setDolp __P((Char *));
90 static void unDredc __P((eChar));
91 static eChar Dredc __P((void));
92 static void Dtestq __P((Char));
95 * Fix up the $ expansions and quotations in the
96 * argument list to command t.
107 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
108 for (pp = t->t_dcom; (p = *pp++) != NULL;) {
110 if (cmap(*p, _DOL | QUOTES)) { /* $, \, ', ", ` */
111 Dfix2(t->t_dcom); /* found one */
122 * $ substitute one word, for i/o redirection
136 setname(short2str(cp));
137 stderror(ERR_NAME | ERR_AMBIG);
139 cp = Strsave(gargv[0]);
140 blkfree(gargv), gargv = 0;
145 * Subroutine to do actual fixing after state initialization.
151 ginit(); /* Initialize glob's area pointers */
153 Dcp = STRNULL; /* Setup input vector for Dreadc */
155 unDredc(0); /* Clear out any old peeks (at error) */
157 dolcnt = 0; /* Clear out residual $ expands (...) */
163 * Pack up more characters in this word
170 int i = MAXWLEN - (int) (wp - wbuf);
193 if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */
202 stderror(ERR_WTOOLONG);
208 * Get a word. This routine is analogous to the routine
209 * word() in sh.lex.c for the main lexical input. One difference
210 * here is that we don't get a newline to terminate our expansion.
211 * Rather, DgetC will return a DEOF when we hit the end-of-input.
221 int sofar = 0, done = 0;
231 /* finish this word and catch the code above the next time */
246 /* We preserve ` quotations which are done yet later */
247 *wp++ = (Char) c, --i;
252 * Note that DgetC never returns a QUOTES character from an
253 * expansion, so only true input quotes will get us here or out.
256 dolflg = c1 == '"' ? DODOL : 0;
261 if (c == '\n' || c == DEOF)
262 stderror(ERR_UNMATCHED, (int)c1);
263 if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE)) {
264 if ((wp[-1] & TRIM) == '\\')
269 stderror(ERR_WTOOLONG);
274 * Leave any `s alone for later. Other chars are all
275 * quoted, thus `...` can tell it was within "...".
277 *wp++ = c == '`' ? '`' : c | QUOTE;
281 /* Prevent all further interpretation */
286 /* Leave all text alone for later */
295 *wp++ = '`' /* i--; eliminated */;
297 if ((wp = Dpack(wbuf, wp)) == NULL)
302 * Avoid a nasty message from the RTU 4.1A & RTU 5.0 compiler concerning
303 * the "overuse of registers". According to the compiler release notes,
304 * incorrect code may be produced unless the offending expression is
305 * rewritten. Therefore, we can't just ignore it, DAS DEC-90.
308 i -= (int) (wp - wbuf);
309 #else /* !masscomp */
310 i = MAXWLEN - (int) (wp - wbuf);
311 #endif /* masscomp */
317 c = DgetC(0); /* No $ subst! */
318 if (c == '\n' || c == DEOF) {
331 if ((wp = Dpack(wbuf, wp)) == NULL)
336 * Avoid a nasty message from the RTU 4.1A & RTU 5.0 compiler concerning
337 * the "overuse of registers". According to the compiler release notes,
338 * incorrect code may be produced unless the offending expression is
339 * rewritten. Therefore, we can't just ignore it, DAS DEC-90.
342 i -= (int) (wp - wbuf);
343 #else /* !masscomp */
344 i = MAXWLEN - (int) (wp - wbuf);
345 #endif /* masscomp */
350 /* Really NOTREACHED */
356 * Get a character, performing $ substitution unless flag is 0.
357 * Any QUOTES character which is returned from a $ expansion is
358 * QUOTEd so that it will not be recognized above.
367 if ((c = Dpeekc) != 0) {
372 c = *lap++ & (QUOTE | TRIM);
383 if ((c = *dolp++ & (QUOTE | TRIM)) != 0)
398 if (c == '$' && flag) {
405 static Char *nulvec[] = { NULL };
406 static struct varent nulargv = {nulvec, STRargv, VAR_READWRITE,
407 { NULL, NULL, NULL }, 0 };
413 setname(short2str(s));
414 stderror(ERR_NAME | ERR_RANGE);
418 * Handle the multitudinous $ expansion forms.
425 struct varent *vp = NULL;
426 Char name[4 * MAXVARLEN + 1];
428 int subscr = 0, lwb = 1, upb = 0;
429 int dimen = 0, bitset = 0, length = 0;
431 static Char *dolbang = NULL;
434 dolmod = dolmcnt = dolwcnt = 0;
436 dolnmod = dolmcnt = dolwcnt = 0;
440 c = DgetC(0); /* sc is { to take } later */
441 if ((c & TRIM) == '#')
442 dimen++, c = DgetC(0); /* $# takes dimension */
444 bitset++, c = DgetC(0); /* $? tests existence */
446 length++, c = DgetC(0); /* $% returns length in chars */
450 if (dimen || bitset || length)
451 stderror(ERR_SYNTAX);
454 xfree((ptr_t) dolbang);
455 setDolp(dolbang = putn(backpid));
460 if (dimen || bitset || length)
461 stderror(ERR_SYNTAX);
466 /* Coherent compiler doesn't allow case-labels that are not
467 constant-expressions */
469 case 0x4000003C: /* Does Coherent have 32-bit int at all? */
470 #elif defined (SHORT_STRINGS)
472 #else /* !SHORT_STRINGS */
475 #else /* !COHERENT */
479 stderror(ERR_NOTALLOWED, "$?<");
481 stderror(ERR_NOTALLOWED, "$#<");
483 stderror(ERR_NOTALLOWED, "$%<");
485 char cbuf[MB_LEN_MAX];
489 sigmask_t omask = sigsetmask(sigblock(0) & ~sigmask(SIGINT));
491 (void) sigrelse(SIGINT);
494 while (force_read(OLDSTD, cbuf + cbp++, 1) == 1) {
497 len = normal_mbtowc(np, cbuf, cbp);
500 if (cbp < MB_LEN_MAX)
501 continue; /* Maybe a partial character */
502 *np = (unsigned char)*cbuf | INVALID_BYTE;
506 if (cbp != (size_t)len)
507 memmove(cbuf, cbuf + len, cbp - len);
509 if (np >= &wbuf[BUFSIZE - 1])
510 stderror(ERR_LTOOLONG);
516 *np = (unsigned char)*cbuf;
517 if (np >= &wbuf[BUFSIZE - 1])
518 stderror(ERR_LTOOLONG);
523 memmove(cbuf, cbuf + 1, cbp);
527 (void) sigsetmask(omask);
529 (void) sighold(SIGINT);
535 * KLUDGE: dolmod is set here because it will cause setDolp to call
536 * domod and thus to copy wbuf. Otherwise setDolp would use it
537 * directly. If we saved it ourselves, no one would know when to free
538 * it. The actual function of the 'q' causes filename expansion not to
539 * be done on the interpolated value.
542 * If we do that, then other modifiers don't work.
543 * in addition, let the user specify :q if wanted
546 /*old*/ dolmod = 'q';
547 /*new*/ dolmod[dolnmod++] = 'q';
556 (void) Strcpy(name, STRargv);
558 subscr = -1; /* Prevent eating [...] */
563 np = dimen ? STRargv : (bitset ? STRstatus : NULL);
566 (void) Strcpy(name, np);
568 subscr = -1; /* Prevent eating [...] */
573 stderror(ERR_SYNTAX);
580 stderror(ERR_NOTALLOWED, "$#<num>");
583 subscr = subscr * 10 + c - '0';
585 } while (Isdigit(c));
591 dolp = dolzero ? STR1 : STR0;
595 stderror(ERR_DOLZERO);
598 length = NLSChars(ffile);
611 stderror(ERR_NOTALLOWED, "$?<num>");
613 stderror(ERR_NOTALLOWED, "$%<num>");
623 np = dimen ? STRargv : (bitset ? STRstatus : NULL);
626 (void) Strcpy(name, np);
628 subscr = -1; /* Prevent eating [...] */
633 stderror(ERR_VARALNUM);
640 if (np >= &name[MAXVARLEN])
641 stderror(ERR_VARTOOLONG);
648 dolp = (vp || getenv(short2str(name))) ? STR1 : STR0;
651 if (vp == NULL || vp->vec == NULL) {
652 np = str2short(getenv(short2str(name)));
662 upb = blklen(vp->vec);
663 if (dimen == 0 && subscr == 0 && c == '[') {
666 c = DgetC(DODOL); /* Allow $ expand within [ ] */
669 if (c == '\n' || c == DEOF)
671 if (np >= &name[sizeof(name) / sizeof(Char) - 2])
672 stderror(ERR_VARTOOLONG);
676 if (dolp || dolcnt) /* $ exp must end before ] */
677 stderror(ERR_EXPORD);
679 stderror(ERR_SYNTAX);
683 for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0')
685 if ((i < 0 || i > upb) && !any("-*", *np)) {
686 dolerror(vp->v_name);
691 upb = lwb, np = STRstar;
696 stderror(ERR_MISSING, '-');
704 i = i * 10 + *np++ - '0';
705 if (i < 0 || i > upb) {
706 dolerror(vp->v_name);
717 dolerror(vp->v_name);
723 stderror(ERR_SYNTAX);
735 Char *cp = putn(upb - lwb + 1);
737 /* this is a kludge. It prevents Dgetdol() from */
738 /* pushing erroneous ${#<error> values into the labuf. */
744 stderror(ERR_MISSING, '}');
755 for (i = lwb - 1, length = 0; i < upb; i++)
756 length += NLSChars(vp->vec[i]);
758 /* We don't want that, since we can always compute it by adding $#xxx */
759 length += i - 1; /* Add the number of spaces in */
768 dolnxt = &vp->vec[lwb - 1];
769 dolcnt = upb - lwb + 1;
775 stderror(ERR_MISSING, '}');
789 c = DgetC(0), dolmcnt = 1, dolwcnt = 1;
790 if (c == 'g' || c == 'a') {
797 if ((c == 'g' && dolmcnt != 10000) ||
798 (c == 'a' && dolwcnt != 10000)) {
806 if (c == 's') { /* [eichin:19910926.0755EST] */
808 eChar delim = DgetC(0);
809 dolmod[dolnmod++] = (Char) c;
810 dolmod[dolnmod++] = (Char) delim;
812 if (!delim || letter(delim)
813 || Isdigit(delim) || any(" \t\n", delim)) {
814 seterror(ERR_BADSUBST);
817 while ((c = DgetC(0)) != DEOF) {
818 dolmod[dolnmod++] = (Char) c;
819 if(c == delim) delimcnt--;
823 seterror(ERR_BADSUBST);
828 if (!any("luhtrqxes", c))
829 stderror(ERR_BADMOD, (int)c);
831 dolmod[dolnmod++] = (Char) c;
839 while ((c = DgetC(0)) == ':');
857 if (dolmod == 0 || dolmcnt == 0) {
859 if (dolnmod == 0 || dolmcnt == 0) {
861 for (dp = cp; *dp; dp++) {
862 if (NLSSize(dp, -1) != 1) {
871 dp = domod(cp, dolmod);
873 dp = cp = Strsave(cp);
874 for (i = 0; i < dolnmod; i++) {
875 /* handle s// [eichin:19910926.0510EST] */
876 if(dolmod[i] == 's') {
878 Char *lhsub, *rhsub, *np;
879 size_t lhlen = 0, rhlen = 0;
883 if (!delim || letter(delim)
884 || Isdigit(delim) || any(" \t\n", delim)) {
885 seterror(ERR_BADSUBST);
888 lhsub = &dolmod[++i];
889 while(dolmod[i] != delim && dolmod[++i]) {
893 rhsub = &dolmod[++i];
894 while(dolmod[i] != delim && dolmod[++i]) {
902 dp = Strstr(cp, lhsub);
904 np = (Char *) xmalloc((size_t)
905 ((Strlen(cp) + 1 - lhlen + rhlen) *
907 (void) Strncpy(np, cp, (size_t) (dp - cp));
908 (void) Strcpy(np + (dp - cp), rhsub);
909 (void) Strcpy(np + (dp - cp) + rhlen, dp + lhlen);
915 /* should this do a seterror? */
919 while (dolwcnt == 10000);
921 * restore dolmod for additional words
923 dolmod[i] = rhsub[-1] = (Char) delim;
934 if ((dp = domod(cp, dolmod[i])) != NULL) {
936 if (Strcmp(cp, dp) == 0) {
949 while (dolwcnt == 10000);
991 if ((c = Dpeekrd) != 0) {
995 if (Dcp && (c = *Dcp++))
996 return (c & (QUOTE | TRIM));
1010 if (cmap(c, QUOTES))
1015 * Form a shell temporary file (in unit 0) from the words
1016 * of the shell input up to EOF or a line the same as "term".
1017 * Unit 0 should have been closed before this call.
1025 Char obuf[BUFSIZE + 1], lbuf[BUFSIZE], mbuf[BUFSIZE];
1026 int ocnt, lcnt, mcnt;
1027 Char *lbp, *obp, *mbp;
1031 #ifndef WINNT_NATIVE
1035 #endif /* WINNT_NATIVE */
1036 tmp = short2str(shtemp);
1039 if (creat(tmp, 0600) < 0)
1040 stderror(ERR_SYSTEM, tmp, strerror(errno));
1044 # define O_TEMPORARY 0
1049 if (open(tmp, O_RDWR|O_CREAT|O_EXCL|O_TEMPORARY|O_LARGEFILE, 0600) == -1) {
1051 #ifndef WINNT_NATIVE
1052 if (errno == EEXIST) {
1053 if (unlink(tmp) == -1) {
1054 (void) gettimeofday(&tv, NULL);
1055 shtemp = Strspl(STRtmpsh, putn((((int)tv.tv_sec) ^
1056 ((int)tv.tv_usec) ^ ((int)getpid())) & 0x00ffffff));
1060 #endif /* WINNT_NATIVE */
1063 stderror(ERR_SYSTEM, tmp, strerror(errno));
1065 (void) unlink(tmp); /* 0 0 inode! */
1078 #endif /* WINNT_NATIVE */
1083 setmode(0, O_BINARY);
1092 c = readc(1); /* 1 -> Want EOF returns */
1093 if (c == CHAR_ERR || c == '\n')
1095 if ((c &= TRIM) != 0) {
1099 stderror(ERR_NAME | ERR_OVERFLOW);
1106 * Check for EOF or compare to terminator -- before expansion
1108 if (c == CHAR_ERR || eq(lbuf, term)) {
1110 tmp = short2str(obuf);
1111 (void) write(0, tmp, strlen (tmp));
1112 (void) lseek(0, (off_t) 0, L_SET);
1118 * If term was quoted or -n just pass it on
1120 if (quoted || noexec) {
1123 for (lbp = lbuf; (c = *lbp++) != 0;) {
1126 tmp = short2str(obuf);
1127 (void) write(0, tmp, strlen (tmp));
1136 * Term wasn't quoted so variable and then command expand the input
1147 if ((c &= TRIM) == 0)
1149 /* \ quotes \ $ ` here */
1152 if (!any("$\\`", c))
1153 unDgetC(c | QUOTE), c = '\\';
1160 stderror(ERR_NAME | ERR_OVERFLOW);
1166 * If any ` in line do command substitution
1169 if (Strchr(mbp, '`') != NULL) {
1171 * 1 arg to dobackp causes substitution to be literal. Words are
1172 * broken only at newlines so that all blanks and tabs are
1173 * preserved. Blank lines (null words) are not discarded.
1175 vp = dobackp(mbuf, 1);
1178 /* Setup trivial vector similar to return of dobackp */
1179 Dv[0] = mbp, Dv[1] = NULL, vp = Dv;
1182 * Resurrect the words from the command substitution each separated by
1183 * a newline. Note that the last newline of a command substitution
1184 * will have been discarded, but we put a newline after the last word
1185 * because this represents the newline after the last input line!
1188 for (mbp = *vp; *mbp; mbp++) {
1189 *obp++ = *mbp & TRIM;
1191 tmp = short2str(obuf);
1192 (void) write(0, tmp, strlen (tmp));
1199 tmp = short2str(obuf);
1200 (void) write(0, tmp, strlen (tmp));
1206 blkfree(pargv), pargv = 0;