1 /* $NetBSD: dol.c,v 1.29 2013/07/16 17:47:43 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.29 2013/07/16 17:47:43 christos Exp $");
41 #include <sys/types.h>
55 * These routines perform variable substitution and quoting via ' and ".
56 * To this point these constructs have been preserved in the divided
57 * input words. Here we expand variables and turn quoting via ' and " into
58 * QUOTE bits on characters (which prevent further interpretation).
59 * If the `:q' modifier was applied during history expansion, then
60 * some QUOTEing may have occurred already, so we dont "trim()" here.
63 static int Dpeekc
, Dpeekrd
; /* Peeks for DgetC and Dreadc */
64 static Char
*Dcp
, **Dvp
; /* Input vector for Dreadc */
67 #define unDgetC(c) Dpeekc = c
68 #define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
71 * The following variables give the information about the current
72 * $ expansion, recording the current word position, the remaining
73 * words within this expansion, the count of remaining words, and the
74 * information about any : modifier which is being applied.
76 #define MAXWLEN (BUFSIZE - 4)
77 #define MAXMOD MAXWLEN /* This cannot overflow */
78 static Char dolmod
[MAXMOD
]; /* : modifier character */
79 static Char
*dolp
; /* Remaining chars from this word */
80 static Char
**dolnxt
; /* Further words */
81 static int dolcnt
; /* Count of further words */
82 static int dolnmod
; /* Number of modifiers */
83 static int dolmcnt
; /* :gx -> 10000, else 1 */
84 static int dolwcnt
; /* :wx -> 10000, else 1 */
86 static void Dfix2(Char
**);
87 static Char
*Dpack(Char
*, Char
*);
88 static int Dword(void);
89 __dead
static void dolerror(Char
*);
90 static int DgetC(int);
91 static void Dgetdol(void);
92 static void fixDolMod(void);
93 static void setDolp(Char
*);
94 static void unDredc(int);
95 static int Dredc(void);
96 static void Dtestq(int);
100 * Fix up the $ expansions and quotations in the
101 * argument list to command t.
104 Dfix(struct command
*t
)
110 /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
111 for (pp
= t
->t_dcom
; (p
= *pp
++) != NULL
;)
113 if (cmap(*p
, _DOL
| QUOTES
)) { /* $, \, ', ", ` */
114 Dfix2(t
->t_dcom
); /* found one */
124 * $ substitute one word, for i/o redirection
137 setname(vis_str(cp
));
138 stderror(ERR_NAME
| ERR_AMBIG
);
140 cp
= Strsave(gargv
[0]);
141 blkfree(gargv
), gargv
= 0;
146 * 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
166 Dpack(Char
*wbuf
, Char
*wp
)
171 i
= MAXWLEN
- (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.
216 Char wbuf
[BUFSIZE
], *wp
;
219 int dolflg
, done
, sofar
;
233 /* finish this word and catch the code above the next time */
245 /* We preserve ` quotations which are done yet later */
246 *wp
++ = (Char
)c
, --i
;
251 * Note that DgetC never returns a QUOTES character from an
252 * expansion, so only true input quotes will get us here or out.
255 dolflg
= c1
== '"' ? DODOL
: 0;
260 if (c
== '\n' || c
== DEOF
)
261 stderror(ERR_UNMATCHED
, c1
);
262 if ((c
& (QUOTE
| TRIM
)) == ('\n' | QUOTE
))
265 stderror(ERR_WTOOLONG
);
269 * Leave any `s alone for later. Other chars are all
270 * quoted, thus `...` can tell it was within "...".
272 *wp
++ = (Char
)(c
== '`' ? '`' : (c
| QUOTE
));
275 /* Prevent all further interpretation */
276 *wp
++ = (Char
)(c
| QUOTE
);
279 /* Leave all text alone for later */
287 *wp
++ = '`' /* i--; eliminated */;
289 if ((wp
= Dpack(wbuf
, wp
)) == NULL
)
292 i
= MAXWLEN
- (wp
- wbuf
);
297 c
= DgetC(0); /* No $ subst! */
298 if (c
== '\n' || c
== DEOF
) {
310 if ((wp
= Dpack(wbuf
, wp
)) == NULL
)
313 i
= MAXWLEN
- (wp
- wbuf
);
318 /* Really NOTREACHED */
324 * Get a character, performing $ substitution unless flag is 0.
325 * Any QUOTES character which is returned from a $ expansion is
326 * QUOTEd so that it will not be recognized above.
333 if ((c
= Dpeekc
) != '\0') {
338 c
= *lap
++ & (QUOTE
| TRIM
);
349 if ((c
= *dolp
++ & (QUOTE
| TRIM
)) != '\0')
364 if (c
== '$' && flag
) {
371 static Char
*nulvec
[] = {0};
372 static struct varent nulargv
= {nulvec
, STRargv
, { NULL
, NULL
, NULL
}, 0};
378 stderror(ERR_NAME
| ERR_RANGE
);
383 * Handle the multitudinous $ expansion forms.
389 static Char
*dolbang
= NULL
;
390 Char name
[4*MAXVARLEN
+1];
394 int c
, lwb
, sc
, subscr
, upb
;
405 dolnmod
= dolmcnt
= dolwcnt
= 0;
408 c
= DgetC(0); /* sc is { to take } later */
409 if ((c
& TRIM
) == '#')
410 dimen
++, c
= DgetC(0); /* $# takes dimension */
412 bitset
++, c
= DgetC(0); /* $? tests existence */
416 stderror(ERR_SYNTAX
);
419 xfree((ptr_t
)dolbang
);
420 setDolp(dolbang
= putn(backpid
));
425 stderror(ERR_SYNTAX
);
430 stderror(ERR_NOTALLOWED
, "$?<");
432 stderror(ERR_NOTALLOWED
, "$?#");
433 for (np
= wbuf
; read(OLDSTD
, &tnp
, 1) == 1; np
++) {
434 *np
= (unsigned char)tnp
;
435 if (np
>= &wbuf
[BUFSIZE
- 1])
436 stderror(ERR_LTOOLONG
);
442 * KLUDGE: dolmod is set here because it will cause setDolp to call
443 * domod and thus to copy wbuf. Otherwise setDolp would use it
444 * directly. If we saved it ourselves, no one would know when to free
445 * it. The actual function of the 'q' causes filename expansion not to
446 * be done on the interpolated value.
448 dolmod
[dolnmod
++] = 'q';
454 stderror(ERR_SYNTAX
);
457 (void) Strcpy(name
, STRargv
);
459 subscr
= -1; /* Prevent eating [...] */
465 stderror(ERR_NOTALLOWED
, "$#<num>");
468 subscr
= subscr
* 10 + c
- '0';
470 } while (Isdigit(c
));
476 dolp
= ffile
? STR1
: STR0
;
480 stderror(ERR_DOLZERO
);
486 stderror(ERR_DOLQUEST
);
495 stderror(ERR_VARALNUM
);
501 if (np
>= &name
[MAXVARLEN
])
502 stderror(ERR_VARTOOLONG
);
509 dolp
= (vp
|| getenv(short2str(name
))) ? STR1
: STR0
;
513 np
= str2short(getenv(short2str(name
)));
522 upb
= blklen(vp
->vec
);
523 if (dimen
== 0 && subscr
== 0 && c
== '[') {
526 c
= DgetC(DODOL
); /* Allow $ expand within [ ] */
529 if (c
== '\n' || c
== DEOF
)
531 if (np
>= &name
[sizeof(name
) / sizeof(Char
) - 2])
532 stderror(ERR_VARTOOLONG
);
536 if (dolp
|| dolcnt
) /* $ exp must end before ] */
537 stderror(ERR_EXPORD
);
539 stderror(ERR_SYNTAX
);
543 for (i
= 0; Isdigit(*np
); i
= i
* 10 + *np
++ - '0')
545 if ((i
< 0 || i
> upb
) && !any("-*", *np
)) {
546 dolerror(vp
->v_name
);
551 upb
= lwb
, np
= STRstar
;
556 stderror(ERR_MISSING
, '-');
564 i
= i
* 10 + *np
++ - '0';
565 if (i
< 0 || i
> upb
) {
566 dolerror(vp
->v_name
);
577 dolerror(vp
->v_name
);
583 stderror(ERR_SYNTAX
);
595 Char
*cp
= putn(upb
- lwb
+ 1);
603 dolnxt
= &vp
->vec
[lwb
- 1];
604 dolcnt
= upb
- lwb
+ 1;
610 stderror(ERR_MISSING
, '}');
622 c
= DgetC(0), dolmcnt
= 1, dolwcnt
= 1;
623 if (c
== 'g' || c
== 'a') {
630 if ((c
== 'g' && dolmcnt
!= 10000) ||
631 (c
== 'a' && dolwcnt
!= 10000)) {
639 if (c
== 's') { /* [eichin:19910926.0755EST] */
641 int delim
= DgetC(0);
642 dolmod
[dolnmod
++] = (Char
)c
;
643 dolmod
[dolnmod
++] = (Char
)delim
;
645 if (!delim
|| letter(delim
)
646 || Isdigit(delim
) || any(" \t\n", delim
)) {
647 seterror(ERR_BADSUBST
);
650 while ((c
= DgetC(0)) != (-1)) {
651 dolmod
[dolnmod
++] = (Char
)c
;
652 if(c
== delim
) delimcnt
--;
656 seterror(ERR_BADSUBST
);
661 if (!any("htrqxes", c
))
662 stderror(ERR_BADMOD
, c
);
663 dolmod
[dolnmod
++] = (Char
)c
;
667 while ((c
= DgetC(0)) == ':');
680 if (dolnmod
== 0 || dolmcnt
== 0) {
684 dp
= cp
= Strsave(cp
);
685 for (i
= 0; i
< dolnmod
; i
++) {
686 /* handle s// [eichin:19910926.0510EST] */
687 if(dolmod
[i
] == 's') {
689 Char
*lhsub
, *rhsub
, *np
;
690 size_t lhlen
= 0, rhlen
= 0;
694 if (!delim
|| letter(delim
)
695 || Isdigit(delim
) || any(" \t\n", delim
)) {
696 seterror(ERR_BADSUBST
);
699 lhsub
= &dolmod
[++i
];
700 while(dolmod
[i
] != delim
&& dolmod
[++i
]) {
704 rhsub
= &dolmod
[++i
];
705 while(dolmod
[i
] != delim
&& dolmod
[++i
]) {
711 dp
= Strstr(cp
, lhsub
);
714 (size_t)((Strlen(cp
) + 1 - lhlen
+ rhlen
) *
716 (void)Strncpy(np
, cp
, (size_t)(dp
- cp
));
717 (void)Strcpy(np
+ (dp
- cp
), rhsub
);
718 (void)Strcpy(np
+ (dp
- cp
) + rhlen
, dp
+ lhlen
);
724 /* should this do a seterror? */
728 while (dolwcnt
== 10000);
730 * restore dolmod for additional words
732 dolmod
[i
] = rhsub
[-1] = (Char
)delim
;
741 if ((dp
= domod(cp
, dolmod
[i
]))) {
743 if (Strcmp(cp
, dp
) == 0) {
756 while (dolwcnt
== 10000);
790 if ((c
= Dpeekrd
) != '\0') {
794 if (Dcp
&& (c
= *Dcp
++))
795 return (c
& (QUOTE
| TRIM
));
812 * Form a shell temporary file (in unit 0) from the words
813 * of the shell input up to EOF or a line the same as "term".
814 * Unit 0 should have been closed before this call.
820 Char obuf
[BUFSIZE
], lbuf
[BUFSIZE
], mbuf
[BUFSIZE
];
822 Char
*Dv
[2], *lbp
, *obp
, *mbp
, **vp
;
824 int c
, ocnt
, lcnt
, mcnt
;
828 tmp
= short2str(shtemp
);
829 if (open(tmp
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, 0600) < 0) {
830 if (errno
== EEXIST
) {
831 if (unlink(tmp
) == -1) {
832 (void)clock_gettime(CLOCK_MONOTONIC
, &tv
);
833 mbp
= putn((((int)tv
.tv_sec
) ^
834 ((int)tv
.tv_nsec
) ^ ((int)getpid())) & 0x00ffffff);
835 shtemp
= Strspl(STRtmpsh
, mbp
);
840 stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
842 (void)unlink(tmp
); /* 0 0 inode! */
858 c
= readc(1); /* 1 -> Want EOF returns */
859 if (c
< 0 || c
== '\n')
861 if ((c
&= TRIM
) != '\0') {
865 stderror(ERR_NAME
| ERR_OVERFLOW
);
872 * Check for EOF or compare to terminator -- before expansion
874 if (c
< 0 || eq(lbuf
, term
)) {
875 (void)write(0, short2str(obuf
), (size_t)(BUFSIZE
- ocnt
));
876 (void)lseek(0, (off_t
)0, SEEK_SET
);
881 * If term was quoted or -n just pass it on
883 if (quoted
|| noexec
) {
886 for (lbp
= lbuf
; (c
= *lbp
++) != '\0';) {
889 (void) write(0, short2str(obuf
), BUFSIZE
);
898 * Term wasn't quoted so variable and then command expand the input
909 if ((c
&= TRIM
) == 0)
911 /* \ quotes \ $ ` here */
915 unDgetC(c
| QUOTE
), c
= '\\';
922 stderror(ERR_NAME
| ERR_OVERFLOW
);
928 * If any ` in line do command substitution
931 if (any(short2str(mbp
), '`')) {
933 * 1 arg to dobackp causes substitution to be literal. Words are
934 * broken only at newlines so that all blanks and tabs are
935 * preserved. Blank lines (null words) are not discarded.
937 vp
= dobackp(mbuf
, 1);
940 /* Setup trivial vector similar to return of dobackp */
941 Dv
[0] = mbp
, Dv
[1] = NULL
, vp
= Dv
;
944 * Resurrect the words from the command substitution each separated by
945 * a newline. Note that the last newline of a command substitution
946 * will have been discarded, but we put a newline after the last word
947 * because this represents the newline after the last input line!
950 for (mbp
= *vp
; *mbp
; mbp
++) {
951 *obp
++ = *mbp
& TRIM
;
953 (void)write(0, short2str(obuf
), BUFSIZE
);
960 (void)write(0, short2str(obuf
), BUFSIZE
);
966 blkfree(pargv
), pargv
= 0;