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 */
67 static int dolmcnt
; /* :gx -> INT_MAX, else 1 */
68 static int dol_flag_a
; /* :ax -> 1, else 0 */
70 static Char
**Dfix2 (Char
*const *);
71 static int Dpack (struct Strbuf
*);
72 static int Dword (struct blk_buf
*);
73 static void dolerror (Char
*);
74 static eChar
DgetC (int);
75 static void Dgetdol (void);
76 static void fixDolMod (void);
77 static void setDolp (Char
*);
78 static void unDredc (eChar
);
79 static eChar
Dredc (void);
80 static void Dtestq (Char
);
83 * Fix up the $ expansions and quotations in the
84 * argument list to command t.
87 Dfix(struct command
*t
)
94 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
95 for (pp
= t
->t_dcom
; (p
= *pp
++) != NULL
;) {
97 if (cmap(*p
, _DOL
| QUOTES
)) { /* $, \, ', ", ` */
100 expanded
= Dfix2(t
->t_dcom
); /* found one */
102 t
->t_dcom
= expanded
;
110 * $ substitute one word, for i/o redirection
115 Char
*Dv
[2], **expanded
;
121 expanded
= Dfix2(Dv
);
122 if (expanded
[0] == NULL
|| expanded
[1] != NULL
) {
124 setname(short2str(cp
));
125 stderror(ERR_NAME
| ERR_AMBIG
);
127 cp
= Strsave(expanded
[0]);
133 * Subroutine to do actual fixing after state initialization.
136 Dfix2(Char
*const *v
)
138 struct blk_buf
*bb
= bb_alloc();
142 Dcp
= STRNULL
; /* Setup input vector for Dreadc */
144 unDredc(0); /* Clear out any old peeks (at error) */
146 dolcnt
= 0; /* Clear out residual $ expands (...) */
147 cleanup_push(bb
, bb_free
);
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_alloc();
207 cleanup_push(wbuf
, Strbuf_free
);
217 /* finish this word and catch the code above the next time */
229 /* We preserve ` quotations which are done yet later */
230 Strbuf_append1(wbuf
, (Char
) c
);
235 * Note that DgetC never returns a QUOTES character from an
236 * expansion, so only true input quotes will get us here or out.
239 dolflg
= c1
== '"' ? DODOL
: 0;
244 if (c
== '\n' || c
== DEOF
) {
246 stderror(ERR_UNMATCHED
, (int)c1
);
248 if ((c
& (QUOTE
| TRIM
)) == ('\n' | QUOTE
)) {
249 if (wbuf
->len
!= 0 && (wbuf
->s
[wbuf
->len
- 1] & TRIM
) == '\\')
256 * Leave any `s alone for later. Other chars are all
257 * quoted, thus `...` can tell it was within "...".
259 Strbuf_append1(wbuf
, c
== '`' ? '`' : c
| QUOTE
);
263 /* Prevent all further interpretation */
264 Strbuf_append1(wbuf
, c
| QUOTE
);
268 /* Leave all text alone for later */
269 Strbuf_append1(wbuf
, (Char
) c
);
277 Strbuf_append1(wbuf
, '`');
279 if (Dpack(wbuf
) != 0)
284 c
= DgetC(0); /* No $ subst! */
285 if (c
== '\n' || c
== DEOF
)
295 if (Dpack(wbuf
) != 0)
300 cleanup_ignore(wbuf
);
302 str
= Strbuf_finish(wbuf
);
310 * Get a character, performing $ substitution unless flag is 0.
311 * Any QUOTES character which is returned from a $ expansion is
312 * QUOTEd so that it will not be recognized above.
320 if ((c
= Dpeekc
) != 0) {
324 if (lap
< labuf
.len
) {
325 c
= labuf
.s
[lap
++] & (QUOTE
| TRIM
);
332 if ((c
= *dolp
++ & (QUOTE
| TRIM
)) != 0)
347 if (c
== '$' && flag
) {
354 static Char
*nulvec
[] = { NULL
};
355 static struct varent nulargv
= {nulvec
, STRargv
, VAR_READWRITE
,
356 { NULL
, NULL
, NULL
}, 0 };
361 setname(short2str(s
));
362 stderror(ERR_NAME
| ERR_RANGE
);
366 * Handle the multitudinous $ expansion forms.
373 struct varent
*vp
= NULL
;
374 struct Strbuf
*name
= Strbuf_alloc();
376 int subscr
= 0, lwb
= 1, upb
= 0;
377 int dimen
= 0, bitset
= 0, length
= 0;
378 static Char
*dolbang
= NULL
;
380 cleanup_push(name
, Strbuf_free
);
381 dolmod
.len
= dolmcnt
= dol_flag_a
= 0;
384 stderror(ERR_SYNTAX
);
388 c
= DgetC(0); /* sc is { to take } later */
389 if ((c
& TRIM
) == '#')
390 dimen
++, c
= DgetC(0); /* $# takes dimension */
392 bitset
++, c
= DgetC(0); /* $? tests existence */
394 length
++, c
= DgetC(0); /* $% returns length in chars */
398 if (dimen
|| bitset
|| length
)
399 stderror(ERR_SYNTAX
);
402 setDolp(dolbang
= putn((tcsh_number_t
)backpid
));
408 if (dimen
|| bitset
|| length
)
409 stderror(ERR_SYNTAX
);
415 static struct Strbuf wbuf
; /* = Strbuf_INIT; */
418 stderror(ERR_NOTALLOWED
, "$?<");
420 stderror(ERR_NOTALLOWED
, "$#<");
422 stderror(ERR_NOTALLOWED
, "$%<");
425 char cbuf
[MB_LEN_MAX
];
427 int old_pintr_disabled
;
434 pintr_push_enable(&old_pintr_disabled
);
435 res
= force_read(OLDSTD
, cbuf
+ cbp
, 1);
436 cleanup_until(&old_pintr_disabled
);
440 len
= normal_mbtowc(&wc
, cbuf
, cbp
);
443 if (cbp
< MB_LEN_MAX
)
444 continue; /* Maybe a partial character */
445 wc
= (unsigned char)*cbuf
| INVALID_BYTE
;
449 if (cbp
!= (size_t)len
)
450 memmove(cbuf
, cbuf
+ len
, cbp
- len
);
454 Strbuf_append1(&wbuf
, wc
);
460 len
= normal_mbtowc(&wc
, cbuf
, cbp
);
463 wc
= (unsigned char)*cbuf
| INVALID_BYTE
;
467 if (cbp
!= (size_t)len
)
468 memmove(cbuf
, cbuf
+ len
, cbp
- len
);
472 Strbuf_append1(&wbuf
, wc
);
474 Strbuf_terminate(&wbuf
);
478 setDolp(wbuf
.s
); /* Kept allocated until next $< expansion */
484 Strbuf_append(name
, STRargv
);
485 Strbuf_terminate(name
);
487 subscr
= -1; /* Prevent eating [...] */
492 np
= dimen
? STRargv
: (bitset
? STRstatus
: NULL
);
495 Strbuf_append(name
, np
);
496 Strbuf_terminate(name
);
498 subscr
= -1; /* Prevent eating [...] */
503 stderror(ERR_SYNTAX
);
509 stderror(ERR_NOTALLOWED
, "$#<num>");
512 subscr
= subscr
* 10 + c
- '0';
514 } while (c
!= DEOF
&& Isdigit(c
));
520 dolp
= dolzero
? STR1
: STR0
;
525 stderror(ERR_DOLZERO
);
527 length
= Strlen(ffile
);
528 addla(putn((tcsh_number_t
)length
));
539 stderror(ERR_NOTALLOWED
, "$?<num>");
541 stderror(ERR_NOTALLOWED
, "$%<num>");
551 if (c
== DEOF
|| !alnum(c
)) {
552 np
= dimen
? STRargv
: (bitset
? STRstatus
: NULL
);
555 Strbuf_append(name
, np
);
556 Strbuf_terminate(name
);
558 subscr
= -1; /* Prevent eating [...] */
563 stderror(ERR_VARALNUM
);
566 Strbuf_append1(name
, (Char
) c
);
568 if (c
== DEOF
|| !alnum(c
))
571 Strbuf_terminate(name
);
576 dolp
= (vp
|| getenv(short2str(name
->s
))) ? STR1
: STR0
;
580 if (vp
== NULL
|| vp
->vec
== NULL
) {
581 np
= str2short(getenv(short2str(name
->s
)));
583 static Char
*env_val
; /* = NULL; */
588 addla(putn((tcsh_number_t
)Strlen(np
)));
591 env_val
= Strsave(np
);
601 upb
= blklen(vp
->vec
);
602 if (dimen
== 0 && subscr
== 0 && c
== '[') {
603 name
= Strbuf_alloc();
604 cleanup_push(name
, Strbuf_free
);
607 c
= DgetC(DODOL
); /* Allow $ expand within [ ] */
610 if (c
== '\n' || c
== DEOF
)
612 Strbuf_append1(name
, (Char
) c
);
614 Strbuf_terminate(name
);
616 if (dolp
|| dolcnt
) /* $ exp must end before ] */
617 stderror(ERR_EXPORD
);
619 stderror(ERR_SYNTAX
);
623 for (i
= 0; Isdigit(*np
); i
= i
* 10 + *np
++ - '0')
625 if (i
< 0 || (i
> upb
&& !any("-*", *np
))) {
627 dolerror(vp
->v_name
);
632 upb
= lwb
, np
= STRstar
;
637 stderror(ERR_MISSING
, '-');
645 i
= i
* 10 + *np
++ - '0';
646 if (i
< 0 || i
> upb
) {
648 dolerror(vp
->v_name
);
660 dolerror(vp
->v_name
);
666 stderror(ERR_SYNTAX
);
679 /* this is a kludge. It prevents Dgetdol() from */
680 /* pushing erroneous ${#<error> values into the labuf. */
684 stderror(ERR_MISSING
, '}');
687 addla(putn((tcsh_number_t
)(upb
- lwb
+ 1)));
692 for (i
= lwb
- 1, length
= 0; i
< upb
; i
++)
693 length
+= Strlen(vp
->vec
[i
]);
695 /* We don't want that, since we can always compute it by adding $#xxx */
696 length
+= i
- 1; /* Add the number of spaces in */
698 addla(putn((tcsh_number_t
)length
));
703 dolnxt
= &vp
->vec
[lwb
- 1];
704 dolcnt
= upb
- lwb
+ 1;
710 stderror(ERR_MISSING
, '}');
722 c
= DgetC(0), dolmcnt
= 1, dol_flag_a
= 0;
723 if (c
== 'g' || c
== 'a') {
730 if ((c
== 'g' && dolmcnt
!= INT_MAX
) ||
731 (c
== 'a' && dol_flag_a
== 0)) {
739 if (c
== 's') { /* [eichin:19910926.0755EST] */
741 eChar delim
= DgetC(0);
742 Strbuf_append1(&dolmod
, (Char
) c
);
743 Strbuf_append1(&dolmod
, (Char
) delim
);
745 if (delim
== DEOF
|| !delim
|| letter(delim
)
746 || Isdigit(delim
) || any(" \t\n", delim
)) {
747 seterror(ERR_BADSUBST
);
750 while ((c
= DgetC(0)) != DEOF
) {
751 Strbuf_append1(&dolmod
, (Char
) c
);
752 if(c
== delim
) delimcnt
--;
756 seterror(ERR_BADSUBST
);
761 if (!any("luhtrqxes", c
))
762 stderror(ERR_BADMOD
, (int)c
);
763 Strbuf_append1(&dolmod
, (Char
) c
);
767 while ((c
= DgetC(0)) == ':');
780 if (dolmod
.len
== 0 || dolmcnt
== 0) {
785 for (i
= 0; i
< dolmod
.len
; i
++) {
788 /* handle s// [eichin:19910926.0510EST] */
789 if(dolmod
.s
[i
] == 's') {
791 Char
*lhsub
, *rhsub
, *np
;
792 size_t lhlen
= 0, rhlen
= 0;
794 delim
= dolmod
.s
[++i
];
795 if (!delim
|| letter(delim
)
796 || Isdigit(delim
) || any(" \t\n", delim
)) {
797 seterror(ERR_BADSUBST
);
800 lhsub
= &dolmod
.s
[++i
];
801 while(dolmod
.s
[i
] != delim
&& dolmod
.s
[++i
]) {
805 rhsub
= &dolmod
.s
[++i
];
806 while(dolmod
.s
[i
] != delim
&& dolmod
.s
[++i
]) {
816 dp
= Strstr(dp
, lhsub
);
818 ptrdiff_t diff
= dp
- cp
;
819 size_t len
= (Strlen(cp
) + 1 - lhlen
+ rhlen
);
820 np
= xmalloc(len
* sizeof(Char
));
821 (void) Strncpy(np
, cp
, diff
);
822 (void) Strcpy(np
+ diff
, rhsub
);
823 (void) Strcpy(np
+ diff
+ rhlen
, dp
+ lhlen
);
829 if (diff
>= (ssize_t
)len
)
832 /* should this do a seterror? */
836 while (dol_flag_a
!= 0);
838 * restore dolmod for additional words
840 dolmod
.s
[i
] = rhsub
[-1] = (Char
) delim
;
844 if ((dp
= domod(cp
, dolmod
.s
[i
])) != NULL
) {
846 if (Strcmp(cp
, dp
) == 0) {
859 while (dol_flag_a
!= 0);
861 if (didmod
&& dolmcnt
!= INT_MAX
)
888 if ((c
= Dpeekrd
) != 0) {
892 if (Dcp
&& (c
= *Dcp
++))
893 return (c
& (QUOTE
| TRIM
));
913 inheredoc_cleanup(void *dummy
)
923 (void) gettimeofday(&tv
, NULL
);
924 return putn((((tcsh_number_t
)tv
.tv_sec
) ^
925 ((tcsh_number_t
)tv
.tv_usec
) ^
926 ((tcsh_number_t
)getpid())) & 0x00ffffff);
928 return putn(getpid());
933 * Form a shell temporary file (in unit 0) from the words
934 * of the shell input up to EOF or a line the same as "term".
935 * Unit 0 should have been closed before this call.
942 struct Strbuf lbuf
= Strbuf_INIT
, mbuf
= Strbuf_INIT
;
943 Char obuf
[BUFSIZE
+ 1];
944 #define OBUF_END (obuf + sizeof(obuf) / sizeof (*obuf) - 1)
945 Char
*lbp
, *obp
, *mbp
;
949 char *tmp
= short2str(shtemp
);
950 char *dot
= strrchr(tmp
, '.');
953 stderror(ERR_NAME
| ERR_NOMATCH
);
954 strcpy(dot
, TMP_TEMPLATE
);
957 if (mkstemp(tmp
) == -1)
958 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
959 #else /* !HAVE_MKSTEMP */
961 # ifndef WINNT_NATIVE
964 # endif /* WINNT_NATIVE */
965 tmp
= short2str(shtemp
);
967 if (xcreat(tmp
, 0600) < 0)
968 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
971 if (xopen(tmp
, O_RDWR
|O_CREAT
|O_EXCL
|O_TEMPORARY
|O_LARGEFILE
, 0600) ==
974 # ifndef WINNT_NATIVE
975 if (errno
== EEXIST
) {
976 if (unlink(tmp
) == -1) {
979 shtemp
= Strspl(STRtmpsh
, mbp
);
984 # endif /* WINNT_NATIVE */
987 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
989 #endif /* HAVE_MKSTEMP */
990 (void) unlink(tmp
); /* 0 0 inode! */
1000 cleanup_push(&inheredoc
, inheredoc_cleanup
);
1003 #endif /* WINNT_NATIVE */
1004 cleanup_push(&lbuf
, Strbuf_cleanup
);
1005 cleanup_push(&mbuf
, Strbuf_cleanup
);
1014 c
= readc(1); /* 1 -> Want EOF returns */
1015 if (c
== CHAR_ERR
|| c
== '\n')
1017 if ((c
&= TRIM
) != 0)
1018 Strbuf_append1(&lbuf
, (Char
) c
);
1020 Strbuf_terminate(&lbuf
);
1022 /* Catch EOF in the middle of a line. */
1023 if (c
== CHAR_ERR
&& lbuf
.len
!= 0)
1027 * Check for EOF or compare to terminator -- before expansion
1029 if (c
== CHAR_ERR
|| eq(lbuf
.s
, term
))
1033 * If term was quoted or -n just pass it on
1035 if (quoted
|| noexec
) {
1036 Strbuf_append1(&lbuf
, '\n');
1037 Strbuf_terminate(&lbuf
);
1038 for (lbp
= lbuf
.s
; (c
= *lbp
++) != 0;) {
1040 if (obp
== OBUF_END
) {
1041 tmp
= short2str(obuf
);
1042 (void) xwrite(0, tmp
, strlen (tmp
));
1050 * Term wasn't quoted so variable and then command expand the input
1060 if ((c
&= TRIM
) == 0)
1062 /* \ quotes \ $ ` here */
1065 if (!any("$\\`", c
))
1066 unDgetC(c
| QUOTE
), c
= '\\';
1070 Strbuf_append1(&mbuf
, (Char
) c
);
1072 Strbuf_terminate(&mbuf
);
1075 * If any ` in line do command substitution
1078 if (Strchr(mbp
, '`') != NULL
) {
1080 * 1 arg to dobackp causes substitution to be literal. Words are
1081 * broken only at newlines so that all blanks and tabs are
1082 * preserved. Blank lines (null words) are not discarded.
1084 words
= dobackp(mbp
, 1);
1087 /* Setup trivial vector similar to return of dobackp */
1088 Dv
[0] = mbp
, Dv
[1] = NULL
, words
= Dv
;
1091 * Resurrect the words from the command substitution each separated by
1092 * a newline. Note that the last newline of a command substitution
1093 * will have been discarded, but we put a newline after the last word
1094 * because this represents the newline after the last input line!
1096 for (vp
= words
; *vp
; vp
++) {
1097 for (mbp
= *vp
; *mbp
; mbp
++) {
1098 *obp
++ = *mbp
& TRIM
;
1099 if (obp
== OBUF_END
) {
1100 tmp
= short2str(obuf
);
1101 (void) xwrite(0, tmp
, strlen (tmp
));
1106 if (obp
== OBUF_END
) {
1107 tmp
= short2str(obuf
);
1108 (void) xwrite(0, tmp
, strlen (tmp
));
1116 tmp
= short2str(obuf
);
1117 (void) xwrite(0, tmp
, strlen (tmp
));
1118 (void) lseek(0, (off_t
) 0, L_SET
);
1119 cleanup_until(&inheredoc
);