1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.dol.c,v 3.77 2009/06/19 16:25:00 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.77 2009/06/19 16:25:00 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
= bb_alloc();
145 Dcp
= STRNULL
; /* Setup input vector for Dreadc */
147 unDredc(0); /* Clear out any old peeks (at error) */
149 dolcnt
= 0; /* Clear out residual $ expands (...) */
150 cleanup_push(bb
, bb_free
);
161 * Pack up more characters in this word
164 Dpack(struct Strbuf
*wbuf
)
185 if (cmap(c
, _SP
| _NL
| _QF
| _QB
)) { /* sp \t\n'"` */
191 Strbuf_append1(wbuf
, (Char
) c
);
196 * Get a word. This routine is analogous to the routine
197 * word() in sh.lex.c for the main lexical input. One difference
198 * here is that we don't get a newline to terminate our expansion.
199 * Rather, DgetC will return a DEOF when we hit the end-of-input.
202 Dword(struct blk_buf
*bb
)
205 struct Strbuf
*wbuf
= Strbuf_alloc();
210 cleanup_push(wbuf
, Strbuf_free
);
220 /* finish this word and catch the code above the next time */
232 /* We preserve ` quotations which are done yet later */
233 Strbuf_append1(wbuf
, (Char
) c
);
238 * Note that DgetC never returns a QUOTES character from an
239 * expansion, so only true input quotes will get us here or out.
242 dolflg
= c1
== '"' ? DODOL
: 0;
247 if (c
== '\n' || c
== DEOF
) {
249 stderror(ERR_UNMATCHED
, (int)c1
);
251 if ((c
& (QUOTE
| TRIM
)) == ('\n' | QUOTE
)) {
252 if (wbuf
->len
!= 0 && (wbuf
->s
[wbuf
->len
- 1] & TRIM
) == '\\')
259 * Leave any `s alone for later. Other chars are all
260 * quoted, thus `...` can tell it was within "...".
262 Strbuf_append1(wbuf
, c
== '`' ? '`' : c
| QUOTE
);
266 /* Prevent all further interpretation */
267 Strbuf_append1(wbuf
, c
| QUOTE
);
271 /* Leave all text alone for later */
272 Strbuf_append1(wbuf
, (Char
) c
);
280 Strbuf_append1(wbuf
, '`');
282 if (Dpack(wbuf
) != 0)
287 c
= DgetC(0); /* No $ subst! */
288 if (c
== '\n' || c
== DEOF
)
298 if (Dpack(wbuf
) != 0)
303 cleanup_ignore(wbuf
);
305 str
= Strbuf_finish(wbuf
);
313 * Get a character, performing $ substitution unless flag is 0.
314 * Any QUOTES character which is returned from a $ expansion is
315 * QUOTEd so that it will not be recognized above.
323 if ((c
= Dpeekc
) != 0) {
327 if (lap
< labuf
.len
) {
328 c
= labuf
.s
[lap
++] & (QUOTE
| TRIM
);
335 if ((c
= *dolp
++ & (QUOTE
| TRIM
)) != 0)
350 if (c
== '$' && flag
) {
357 static Char
*nulvec
[] = { NULL
};
358 static struct varent nulargv
= {nulvec
, STRargv
, VAR_READWRITE
,
359 { NULL
, NULL
, NULL
}, 0 };
364 setname(short2str(s
));
365 stderror(ERR_NAME
| ERR_RANGE
);
369 * Handle the multitudinous $ expansion forms.
376 struct varent
*vp
= NULL
;
377 struct Strbuf
*name
= Strbuf_alloc();
379 int subscr
= 0, lwb
= 1, upb
= 0;
380 int dimen
= 0, bitset
= 0, length
= 0;
381 static Char
*dolbang
= NULL
;
383 cleanup_push(name
, Strbuf_free
);
384 dolmod
.len
= dolmcnt
= dol_flag_a
= 0;
387 stderror(ERR_SYNTAX
);
391 c
= DgetC(0); /* sc is { to take } later */
392 if ((c
& TRIM
) == '#')
393 dimen
++, c
= DgetC(0); /* $# takes dimension */
395 bitset
++, c
= DgetC(0); /* $? tests existence */
397 length
++, c
= DgetC(0); /* $% returns length in chars */
401 if (dimen
|| bitset
|| length
)
402 stderror(ERR_SYNTAX
);
405 setDolp(dolbang
= putn(backpid
));
411 if (dimen
|| bitset
|| length
)
412 stderror(ERR_SYNTAX
);
418 static struct Strbuf wbuf
; /* = Strbuf_INIT; */
421 stderror(ERR_NOTALLOWED
, "$?<");
423 stderror(ERR_NOTALLOWED
, "$#<");
425 stderror(ERR_NOTALLOWED
, "$%<");
428 char cbuf
[MB_LEN_MAX
];
430 int old_pintr_disabled
;
437 pintr_push_enable(&old_pintr_disabled
);
438 res
= force_read(OLDSTD
, cbuf
+ cbp
, 1);
439 cleanup_until(&old_pintr_disabled
);
443 len
= normal_mbtowc(&wc
, cbuf
, cbp
);
446 if (cbp
< MB_LEN_MAX
)
447 continue; /* Maybe a partial character */
448 wc
= (unsigned char)*cbuf
| INVALID_BYTE
;
452 if (cbp
!= (size_t)len
)
453 memmove(cbuf
, cbuf
+ len
, cbp
- len
);
457 Strbuf_append1(&wbuf
, wc
);
463 len
= normal_mbtowc(&wc
, cbuf
, cbp
);
466 wc
= (unsigned char)*cbuf
| INVALID_BYTE
;
470 if (cbp
!= (size_t)len
)
471 memmove(cbuf
, cbuf
+ len
, cbp
- len
);
475 Strbuf_append1(&wbuf
, wc
);
477 Strbuf_terminate(&wbuf
);
481 setDolp(wbuf
.s
); /* Kept allocated until next $< expansion */
487 Strbuf_append(name
, STRargv
);
488 Strbuf_terminate(name
);
490 subscr
= -1; /* Prevent eating [...] */
495 np
= dimen
? STRargv
: (bitset
? STRstatus
: NULL
);
498 Strbuf_append(name
, np
);
499 Strbuf_terminate(name
);
501 subscr
= -1; /* Prevent eating [...] */
506 stderror(ERR_SYNTAX
);
512 stderror(ERR_NOTALLOWED
, "$#<num>");
515 subscr
= subscr
* 10 + c
- '0';
517 } while (c
!= DEOF
&& Isdigit(c
));
523 dolp
= dolzero
? STR1
: STR0
;
528 stderror(ERR_DOLZERO
);
530 length
= Strlen(ffile
);
542 stderror(ERR_NOTALLOWED
, "$?<num>");
544 stderror(ERR_NOTALLOWED
, "$%<num>");
554 if (c
== DEOF
|| !alnum(c
)) {
555 np
= dimen
? STRargv
: (bitset
? STRstatus
: NULL
);
558 Strbuf_append(name
, np
);
559 Strbuf_terminate(name
);
561 subscr
= -1; /* Prevent eating [...] */
566 stderror(ERR_VARALNUM
);
569 Strbuf_append1(name
, (Char
) c
);
571 if (c
== DEOF
|| !alnum(c
))
574 Strbuf_terminate(name
);
579 dolp
= (vp
|| getenv(short2str(name
->s
))) ? STR1
: STR0
;
583 if (vp
== NULL
|| vp
->vec
== NULL
) {
584 np
= str2short(getenv(short2str(name
->s
)));
586 static Char
*env_val
; /* = NULL; */
591 addla(putn(Strlen(np
)));
594 env_val
= Strsave(np
);
604 upb
= blklen(vp
->vec
);
605 if (dimen
== 0 && subscr
== 0 && c
== '[') {
606 name
= Strbuf_alloc();
607 cleanup_push(name
, Strbuf_free
);
610 c
= DgetC(DODOL
); /* Allow $ expand within [ ] */
613 if (c
== '\n' || c
== DEOF
)
615 Strbuf_append1(name
, (Char
) c
);
617 Strbuf_terminate(name
);
619 if (dolp
|| dolcnt
) /* $ exp must end before ] */
620 stderror(ERR_EXPORD
);
622 stderror(ERR_SYNTAX
);
626 for (i
= 0; Isdigit(*np
); i
= i
* 10 + *np
++ - '0')
628 if (i
< 0 || (i
> upb
&& !any("-*", *np
))) {
630 dolerror(vp
->v_name
);
635 upb
= lwb
, np
= STRstar
;
640 stderror(ERR_MISSING
, '-');
648 i
= i
* 10 + *np
++ - '0';
649 if (i
< 0 || i
> upb
) {
651 dolerror(vp
->v_name
);
663 dolerror(vp
->v_name
);
669 stderror(ERR_SYNTAX
);
682 /* this is a kludge. It prevents Dgetdol() from */
683 /* pushing erroneous ${#<error> values into the labuf. */
687 stderror(ERR_MISSING
, '}');
690 addla(putn(upb
- lwb
+ 1));
695 for (i
= lwb
- 1, length
= 0; i
< upb
; i
++)
696 length
+= Strlen(vp
->vec
[i
]);
698 /* We don't want that, since we can always compute it by adding $#xxx */
699 length
+= i
- 1; /* Add the number of spaces in */
706 dolnxt
= &vp
->vec
[lwb
- 1];
707 dolcnt
= upb
- lwb
+ 1;
713 stderror(ERR_MISSING
, '}');
725 c
= DgetC(0), dolmcnt
= 1, dol_flag_a
= 0;
726 if (c
== 'g' || c
== 'a') {
733 if ((c
== 'g' && dolmcnt
!= INT_MAX
) ||
734 (c
== 'a' && dol_flag_a
== 0)) {
742 if (c
== 's') { /* [eichin:19910926.0755EST] */
744 eChar delim
= DgetC(0);
745 Strbuf_append1(&dolmod
, (Char
) c
);
746 Strbuf_append1(&dolmod
, (Char
) delim
);
748 if (delim
== DEOF
|| !delim
|| letter(delim
)
749 || Isdigit(delim
) || any(" \t\n", delim
)) {
750 seterror(ERR_BADSUBST
);
753 while ((c
= DgetC(0)) != DEOF
) {
754 Strbuf_append1(&dolmod
, (Char
) c
);
755 if(c
== delim
) delimcnt
--;
759 seterror(ERR_BADSUBST
);
764 if (!any("luhtrqxes", c
))
765 stderror(ERR_BADMOD
, (int)c
);
766 Strbuf_append1(&dolmod
, (Char
) c
);
770 while ((c
= DgetC(0)) == ':');
783 if (dolmod
.len
== 0 || dolmcnt
== 0) {
788 for (i
= 0; i
< dolmod
.len
; i
++) {
791 /* handle s// [eichin:19910926.0510EST] */
792 if(dolmod
.s
[i
] == 's') {
794 Char
*lhsub
, *rhsub
, *np
;
795 size_t lhlen
= 0, rhlen
= 0;
797 delim
= dolmod
.s
[++i
];
798 if (!delim
|| letter(delim
)
799 || Isdigit(delim
) || any(" \t\n", delim
)) {
800 seterror(ERR_BADSUBST
);
803 lhsub
= &dolmod
.s
[++i
];
804 while(dolmod
.s
[i
] != delim
&& dolmod
.s
[++i
]) {
808 rhsub
= &dolmod
.s
[++i
];
809 while(dolmod
.s
[i
] != delim
&& dolmod
.s
[++i
]) {
819 dp
= Strstr(dp
, lhsub
);
821 ptrdiff_t diff
= dp
- cp
;
822 size_t len
= (Strlen(cp
) + 1 - lhlen
+ rhlen
);
823 np
= xmalloc(len
* sizeof(Char
));
824 (void) Strncpy(np
, cp
, diff
);
825 (void) Strcpy(np
+ diff
, rhsub
);
826 (void) Strcpy(np
+ diff
+ rhlen
, dp
+ lhlen
);
836 /* should this do a seterror? */
840 while (dol_flag_a
!= 0);
842 * restore dolmod for additional words
844 dolmod
.s
[i
] = rhsub
[-1] = (Char
) delim
;
848 if ((dp
= domod(cp
, dolmod
.s
[i
])) != NULL
) {
850 if (Strcmp(cp
, dp
) == 0) {
863 while (dol_flag_a
!= 0);
865 if (didmod
&& dolmcnt
!= INT_MAX
)
892 if ((c
= Dpeekrd
) != 0) {
896 if (Dcp
&& (c
= *Dcp
++))
897 return (c
& (QUOTE
| TRIM
));
917 inheredoc_cleanup(void *dummy
)
924 * Form a shell temporary file (in unit 0) from the words
925 * of the shell input up to EOF or a line the same as "term".
926 * Unit 0 should have been closed before this call.
933 struct Strbuf lbuf
= Strbuf_INIT
, mbuf
= Strbuf_INIT
;
934 Char obuf
[BUFSIZE
+ 1];
935 #define OBUF_END (obuf + sizeof(obuf) / sizeof (*obuf) - 1)
936 Char
*lbp
, *obp
, *mbp
;
944 #endif /* WINNT_NATIVE */
945 tmp
= short2str(shtemp
);
948 if (xcreat(tmp
, 0600) < 0)
949 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
953 # define O_TEMPORARY 0
958 if (xopen(tmp
, O_RDWR
|O_CREAT
|O_EXCL
|O_TEMPORARY
|O_LARGEFILE
, 0600) ==
962 if (errno
== EEXIST
) {
963 if (unlink(tmp
) == -1) {
964 (void) gettimeofday(&tv
, NULL
);
966 mbp
= putn((((int)tv
.tv_sec
) ^
967 ((int)tv
.tv_usec
) ^ ((int)getpid())) & 0x00ffffff);
968 shtemp
= Strspl(STRtmpsh
, mbp
);
973 #endif /* WINNT_NATIVE */
976 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
978 (void) unlink(tmp
); /* 0 0 inode! */
988 cleanup_push(&inheredoc
, inheredoc_cleanup
);
991 #endif /* WINNT_NATIVE */
992 cleanup_push(&lbuf
, Strbuf_cleanup
);
993 cleanup_push(&mbuf
, Strbuf_cleanup
);
1002 c
= readc(1); /* 1 -> Want EOF returns */
1003 if (c
== CHAR_ERR
|| c
== '\n')
1005 if ((c
&= TRIM
) != 0)
1006 Strbuf_append1(&lbuf
, (Char
) c
);
1008 Strbuf_terminate(&lbuf
);
1011 * Check for EOF or compare to terminator -- before expansion
1013 if (c
== CHAR_ERR
|| eq(lbuf
.s
, term
))
1017 * If term was quoted or -n just pass it on
1019 if (quoted
|| noexec
) {
1020 Strbuf_append1(&lbuf
, '\n');
1021 Strbuf_terminate(&lbuf
);
1022 for (lbp
= lbuf
.s
; (c
= *lbp
++) != 0;) {
1024 if (obp
== OBUF_END
) {
1025 tmp
= short2str(obuf
);
1026 (void) xwrite(0, tmp
, strlen (tmp
));
1034 * Term wasn't quoted so variable and then command expand the input
1044 if ((c
&= TRIM
) == 0)
1046 /* \ quotes \ $ ` here */
1049 if (!any("$\\`", c
))
1050 unDgetC(c
| QUOTE
), c
= '\\';
1054 Strbuf_append1(&mbuf
, (Char
) c
);
1056 Strbuf_terminate(&mbuf
);
1059 * If any ` in line do command substitution
1062 if (Strchr(mbp
, '`') != NULL
) {
1064 * 1 arg to dobackp causes substitution to be literal. Words are
1065 * broken only at newlines so that all blanks and tabs are
1066 * preserved. Blank lines (null words) are not discarded.
1068 words
= dobackp(mbp
, 1);
1071 /* Setup trivial vector similar to return of dobackp */
1072 Dv
[0] = mbp
, Dv
[1] = NULL
, words
= Dv
;
1075 * Resurrect the words from the command substitution each separated by
1076 * a newline. Note that the last newline of a command substitution
1077 * will have been discarded, but we put a newline after the last word
1078 * because this represents the newline after the last input line!
1080 for (vp
= words
; *vp
; vp
++) {
1081 for (mbp
= *vp
; *mbp
; mbp
++) {
1082 *obp
++ = *mbp
& TRIM
;
1083 if (obp
== OBUF_END
) {
1084 tmp
= short2str(obuf
);
1085 (void) xwrite(0, tmp
, strlen (tmp
));
1090 if (obp
== OBUF_END
) {
1091 tmp
= short2str(obuf
);
1092 (void) xwrite(0, tmp
, strlen (tmp
));
1100 tmp
= short2str(obuf
);
1101 (void) xwrite(0, tmp
, strlen (tmp
));
1102 (void) lseek(0, (off_t
) 0, L_SET
);
1103 cleanup_until(&inheredoc
);