1 /* $NetBSD: dol.c,v 1.25 2007/07/16 14:07:00 christos Exp $ */
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)dol.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: dol.c,v 1.25 2007/07/16 14:07:00 christos Exp $");
41 #include <sys/types.h>
54 * These routines perform variable substitution and quoting via ' and ".
55 * To this point these constructs have been preserved in the divided
56 * input words. Here we expand variables and turn quoting via ' and " into
57 * QUOTE bits on characters (which prevent further interpretation).
58 * If the `:q' modifier was applied during history expansion, then
59 * some QUOTEing may have occurred already, so we dont "trim()" here.
62 static int Dpeekc
, Dpeekrd
; /* Peeks for DgetC and Dreadc */
63 static Char
*Dcp
, **Dvp
; /* Input vector for Dreadc */
66 #define unDgetC(c) Dpeekc = c
67 #define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
70 * The following variables give the information about the current
71 * $ expansion, recording the current word position, the remaining
72 * words within this expansion, the count of remaining words, and the
73 * information about any : modifier which is being applied.
75 #define MAXWLEN (BUFSIZE - 4)
76 #define MAXMOD MAXWLEN /* This cannot overflow */
77 static Char dolmod
[MAXMOD
]; /* : modifier character */
78 static Char
*dolp
; /* Remaining chars from this word */
79 static Char
**dolnxt
; /* Further words */
80 static int dolcnt
; /* Count of further words */
81 static int dolnmod
; /* Number of modifiers */
82 static int dolmcnt
; /* :gx -> 10000, else 1 */
83 static int dolwcnt
; /* :wx -> 10000, else 1 */
85 static void Dfix2(Char
**);
86 static Char
*Dpack(Char
*, Char
*);
87 static int Dword(void);
88 static void dolerror(Char
*);
89 static int DgetC(int);
90 static void Dgetdol(void);
91 static void fixDolMod(void);
92 static void setDolp(Char
*);
93 static void unDredc(int);
94 static int Dredc(void);
95 static void Dtestq(int);
99 * Fix up the $ expansions and quotations in the
100 * argument list to command t.
103 Dfix(struct command
*t
)
109 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
110 for (pp
= t
->t_dcom
; (p
= *pp
++) != NULL
;)
112 if (cmap(*p
, _DOL
| QUOTES
)) { /* $, \, ', ", ` */
113 Dfix2(t
->t_dcom
); /* found one */
123 * $ substitute one word, for i/o redirection
136 setname(vis_str(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.
150 ginit(); /* Initialize glob's area pointers */
152 Dcp
= STRNULL
; /* Setup input vector for Dreadc */
154 unDredc(0); /* Clear out any old peeks (at error) */
156 dolcnt
= 0; /* Clear out residual $ expands (...) */
162 * Pack up more characters in this word
165 Dpack(Char
*wbuf
, Char
*wp
)
169 i
= MAXWLEN
- (wp
- wbuf
);
191 if (cmap(c
, _SP
| _NL
| _QF
| _QB
)) { /* sp \t\n'"` */
200 stderror(ERR_WTOOLONG
);
206 * Get a word. This routine is analogous to the routine
207 * word() in sh.lex.c for the main lexical input. One difference
208 * here is that we don't get a newline to terminate our expansion.
209 * Rather, DgetC will return a DEOF when we hit the end-of-input.
214 Char wbuf
[BUFSIZE
], *wp
;
216 int dolflg
, done
, sofar
;
230 /* finish this word and catch the code above the next time */
242 /* We preserve ` quotations which are done yet later */
248 * Note that DgetC never returns a QUOTES character from an
249 * expansion, so only true input quotes will get us here or out.
252 dolflg
= c1
== '"' ? DODOL
: 0;
257 if (c
== '\n' || c
== DEOF
)
258 stderror(ERR_UNMATCHED
, c1
);
259 if ((c
& (QUOTE
| TRIM
)) == ('\n' | QUOTE
))
262 stderror(ERR_WTOOLONG
);
266 * Leave any `s alone for later. Other chars are all
267 * quoted, thus `...` can tell it was within "...".
269 *wp
++ = c
== '`' ? '`' : c
| QUOTE
;
272 /* Prevent all further interpretation */
276 /* Leave all text alone for later */
284 *wp
++ = '`' /* i--; eliminated */;
286 if ((wp
= Dpack(wbuf
, wp
)) == NULL
)
289 i
= MAXWLEN
- (wp
- wbuf
);
294 c
= DgetC(0); /* No $ subst! */
295 if (c
== '\n' || c
== DEOF
) {
307 if ((wp
= Dpack(wbuf
, wp
)) == NULL
)
310 i
= MAXWLEN
- (wp
- wbuf
);
315 /* Really NOTREACHED */
321 * Get a character, performing $ substitution unless flag is 0.
322 * Any QUOTES character which is returned from a $ expansion is
323 * QUOTEd so that it will not be recognized above.
330 if ((c
= Dpeekc
) != '\0') {
335 c
= *lap
++ & (QUOTE
| TRIM
);
346 if ((c
= *dolp
++ & (QUOTE
| TRIM
)) != '\0')
361 if (c
== '$' && flag
) {
368 static Char
*nulvec
[] = {0};
369 static struct varent nulargv
= {nulvec
, STRargv
, { NULL
, NULL
, NULL
}, 0};
375 stderror(ERR_NAME
| ERR_RANGE
);
380 * Handle the multitudinous $ expansion forms.
386 static Char
*dolbang
= NULL
;
387 Char name
[4*MAXVARLEN
+1];
391 int c
, lwb
, sc
, subscr
, upb
;
402 dolnmod
= dolmcnt
= dolwcnt
= 0;
405 c
= DgetC(0); /* sc is { to take } later */
406 if ((c
& TRIM
) == '#')
407 dimen
++, c
= DgetC(0); /* $# takes dimension */
409 bitset
++, c
= DgetC(0); /* $? tests existence */
413 stderror(ERR_SYNTAX
);
416 xfree((ptr_t
)dolbang
);
417 setDolp(dolbang
= putn(backpid
));
422 stderror(ERR_SYNTAX
);
427 stderror(ERR_NOTALLOWED
, "$?<");
429 stderror(ERR_NOTALLOWED
, "$?#");
430 for (np
= wbuf
; read(OLDSTD
, &tnp
, 1) == 1; np
++) {
431 *np
= (unsigned char)tnp
;
432 if (np
>= &wbuf
[BUFSIZE
- 1])
433 stderror(ERR_LTOOLONG
);
439 * KLUDGE: dolmod is set here because it will cause setDolp to call
440 * domod and thus to copy wbuf. Otherwise setDolp would use it
441 * directly. If we saved it ourselves, no one would know when to free
442 * it. The actual function of the 'q' causes filename expansion not to
443 * be done on the interpolated value.
445 dolmod
[dolnmod
++] = 'q';
451 stderror(ERR_SYNTAX
);
454 (void) Strcpy(name
, STRargv
);
456 subscr
= -1; /* Prevent eating [...] */
462 stderror(ERR_NOTALLOWED
, "$#<num>");
465 subscr
= subscr
* 10 + c
- '0';
467 } while (Isdigit(c
));
473 dolp
= ffile
? STR1
: STR0
;
477 stderror(ERR_DOLZERO
);
483 stderror(ERR_DOLQUEST
);
492 stderror(ERR_VARALNUM
);
498 if (np
>= &name
[MAXVARLEN
])
499 stderror(ERR_VARTOOLONG
);
506 dolp
= (vp
|| getenv(short2str(name
))) ? STR1
: STR0
;
510 np
= str2short(getenv(short2str(name
)));
519 upb
= blklen(vp
->vec
);
520 if (dimen
== 0 && subscr
== 0 && c
== '[') {
523 c
= DgetC(DODOL
); /* Allow $ expand within [ ] */
526 if (c
== '\n' || c
== DEOF
)
528 if (np
>= &name
[sizeof(name
) / sizeof(Char
) - 2])
529 stderror(ERR_VARTOOLONG
);
533 if (dolp
|| dolcnt
) /* $ exp must end before ] */
534 stderror(ERR_EXPORD
);
536 stderror(ERR_SYNTAX
);
540 for (i
= 0; Isdigit(*np
); i
= i
* 10 + *np
++ - '0')
542 if ((i
< 0 || i
> upb
) && !any("-*", *np
)) {
543 dolerror(vp
->v_name
);
548 upb
= lwb
, np
= STRstar
;
553 stderror(ERR_MISSING
, '-');
561 i
= i
* 10 + *np
++ - '0';
562 if (i
< 0 || i
> upb
) {
563 dolerror(vp
->v_name
);
574 dolerror(vp
->v_name
);
580 stderror(ERR_SYNTAX
);
592 Char
*cp
= putn(upb
- lwb
+ 1);
600 dolnxt
= &vp
->vec
[lwb
- 1];
601 dolcnt
= upb
- lwb
+ 1;
607 stderror(ERR_MISSING
, '}');
619 c
= DgetC(0), dolmcnt
= 1, dolwcnt
= 1;
620 if (c
== 'g' || c
== 'a') {
627 if ((c
== 'g' && dolmcnt
!= 10000) ||
628 (c
== 'a' && dolwcnt
!= 10000)) {
636 if (c
== 's') { /* [eichin:19910926.0755EST] */
638 int delim
= DgetC(0);
639 dolmod
[dolnmod
++] = c
;
640 dolmod
[dolnmod
++] = delim
;
642 if (!delim
|| letter(delim
)
643 || Isdigit(delim
) || any(" \t\n", delim
)) {
644 seterror(ERR_BADSUBST
);
647 while ((c
= DgetC(0)) != (-1)) {
648 dolmod
[dolnmod
++] = c
;
649 if(c
== delim
) delimcnt
--;
653 seterror(ERR_BADSUBST
);
658 if (!any("htrqxes", c
))
659 stderror(ERR_BADMOD
, c
);
660 dolmod
[dolnmod
++] = c
;
664 while ((c
= DgetC(0)) == ':');
677 if (dolnmod
== 0 || dolmcnt
== 0) {
681 dp
= cp
= Strsave(cp
);
682 for (i
= 0; i
< dolnmod
; i
++) {
683 /* handle s// [eichin:19910926.0510EST] */
684 if(dolmod
[i
] == 's') {
686 Char
*lhsub
, *rhsub
, *np
;
687 size_t lhlen
= 0, rhlen
= 0;
691 if (!delim
|| letter(delim
)
692 || Isdigit(delim
) || any(" \t\n", delim
)) {
693 seterror(ERR_BADSUBST
);
696 lhsub
= &dolmod
[++i
];
697 while(dolmod
[i
] != delim
&& dolmod
[++i
]) {
701 rhsub
= &dolmod
[++i
];
702 while(dolmod
[i
] != delim
&& dolmod
[++i
]) {
708 dp
= Strstr(cp
, lhsub
);
710 np
= (Char
*)xmalloc(
711 (size_t)((Strlen(cp
) + 1 - lhlen
+ rhlen
) *
713 (void)Strncpy(np
, cp
, dp
- cp
);
714 (void)Strcpy(np
+ (dp
- cp
), rhsub
);
715 (void)Strcpy(np
+ (dp
- cp
) + rhlen
, dp
+ lhlen
);
721 /* should this do a seterror? */
725 while (dolwcnt
== 10000);
727 * restore dolmod for additional words
729 dolmod
[i
] = rhsub
[-1] = delim
;
738 if ((dp
= domod(cp
, dolmod
[i
]))) {
740 if (Strcmp(cp
, dp
) == 0) {
753 while (dolwcnt
== 10000);
787 if ((c
= Dpeekrd
) != '\0') {
791 if (Dcp
&& (c
= *Dcp
++))
792 return (c
& (QUOTE
| TRIM
));
809 * Form a shell temporary file (in unit 0) from the words
810 * of the shell input up to EOF or a line the same as "term".
811 * Unit 0 should have been closed before this call.
817 Char obuf
[BUFSIZE
], lbuf
[BUFSIZE
], mbuf
[BUFSIZE
];
819 Char
*Dv
[2], *lbp
, *obp
, *mbp
, **vp
;
821 int c
, ocnt
, lcnt
, mcnt
;
825 tmp
= short2str(shtemp
);
826 if (open(tmp
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, 0600) < 0) {
827 if (errno
== EEXIST
) {
828 if (unlink(tmp
) == -1) {
829 (void)gettimeofday(&tv
, NULL
);
830 mbp
= putn((((int)tv
.tv_sec
) ^
831 ((int)tv
.tv_usec
) ^ ((int)getpid())) & 0x00ffffff);
832 shtemp
= Strspl(STRtmpsh
, mbp
);
837 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
839 (void)unlink(tmp
); /* 0 0 inode! */
855 c
= readc(1); /* 1 -> Want EOF returns */
856 if (c
< 0 || c
== '\n')
858 if ((c
&= TRIM
) != '\0') {
862 stderror(ERR_NAME
| ERR_OVERFLOW
);
869 * Check for EOF or compare to terminator -- before expansion
871 if (c
< 0 || eq(lbuf
, term
)) {
872 (void)write(0, short2str(obuf
), (size_t)(BUFSIZE
- ocnt
));
873 (void)lseek(0, (off_t
)0, SEEK_SET
);
878 * If term was quoted or -n just pass it on
880 if (quoted
|| noexec
) {
883 for (lbp
= lbuf
; (c
= *lbp
++) != '\0';) {
886 (void) write(0, short2str(obuf
), BUFSIZE
);
895 * Term wasn't quoted so variable and then command expand the input
906 if ((c
&= TRIM
) == 0)
908 /* \ quotes \ $ ` here */
912 unDgetC(c
| QUOTE
), c
= '\\';
919 stderror(ERR_NAME
| ERR_OVERFLOW
);
925 * If any ` in line do command substitution
928 if (any(short2str(mbp
), '`')) {
930 * 1 arg to dobackp causes substitution to be literal. Words are
931 * broken only at newlines so that all blanks and tabs are
932 * preserved. Blank lines (null words) are not discarded.
934 vp
= dobackp(mbuf
, 1);
937 /* Setup trivial vector similar to return of dobackp */
938 Dv
[0] = mbp
, Dv
[1] = NULL
, vp
= Dv
;
941 * Resurrect the words from the command substitution each separated by
942 * a newline. Note that the last newline of a command substitution
943 * will have been discarded, but we put a newline after the last word
944 * because this represents the newline after the last input line!
947 for (mbp
= *vp
; *mbp
; mbp
++) {
948 *obp
++ = *mbp
& TRIM
;
950 (void)write(0, short2str(obuf
), BUFSIZE
);
957 (void)write(0, short2str(obuf
), BUFSIZE
);
963 blkfree(pargv
), pargv
= 0;