* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)dol.c 8.1 (Berkeley) %G%";
* These routines perform variable substitution and quoting via ' and ".
* To this point these constructs have been preserved in the divided
* input words. Here we expand variables and turn quoting via ' and " into
* QUOTE bits on characters (which prevent further interpretation).
* If the `:q' modifier was applied during history expansion, then
* some QUOTEing may have occurred already, so we dont "trim()" here.
static int Dpeekc
, Dpeekrd
; /* Peeks for DgetC and Dreadc */
static Char
*Dcp
, **Dvp
; /* Input vector for Dreadc */
#define unDgetC(c) Dpeekc = c
#define QUOTES (_QF|_QB|_ESC) /* \ ' " ` */
* The following variables give the information about the current
* $ expansion, recording the current word position, the remaining
* words within this expansion, the count of remaining words, and the
* information about any : modifier which is being applied.
#define MAXWLEN (BUFSIZ - 4)
#define MAXMOD MAXWLEN /* This cannot overflow */
static Char
*dolp
; /* Remaining chars from this word */
static Char
**dolnxt
; /* Further words */
static int dolcnt
; /* Count of further words */
static Char dolmod
[MAXMOD
]; /* : modifier character */
static int dolnmod
; /* Number of modifiers */
static int dolmcnt
; /* :gx -> 10000, else 1 */
static int dolwcnt
; /* :wx -> 10000, else 1 */
static void Dfix2
__P((Char
**));
static Char
*Dpack
__P((Char
*, Char
*));
static int Dword
__P((void));
static void dolerror
__P((Char
*));
static int DgetC
__P((int));
static void Dgetdol
__P((void));
static void fixDolMod
__P((void));
static void setDolp
__P((Char
*));
static void unDredc
__P((int));
static int Dredc
__P((void));
static void Dtestq
__P((int));
* Fix up the $ expansions and quotations in the
* argument list to command t.
register struct command
*t
;
/* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
for (pp
= t
->t_dcom
; (p
= *pp
++) != NULL
;)
if (cmap(*p
, _DOL
| QUOTES
)) { /* $, \, ', ", ` */
Dfix2(t
->t_dcom
); /* found one */
* $ substitute one word, for i/o redirection
stderror(ERR_NAME
| ERR_AMBIG
);
blkfree(gargv
), gargv
= 0;
* Subroutine to do actual fixing after state initialization.
ginit(); /* Initialize glob's area pointers */
Dcp
= STRNULL
; /* Setup input vector for Dreadc */
unDredc(0); /* Clear out any old peeks (at error) */
dolcnt
= 0; /* Clear out residual $ expands (...) */
* Pack up more characters in this word
register int i
= MAXWLEN
- (wp
- wbuf
);
if (cmap(c
, _SP
| _NL
| _QF
| _QB
)) { /* sp \t\n'"` */
* Get a word. This routine is analogous to the routine
* word() in sh.lex.c for the main lexical input. One difference
* here is that we don't get a newline to terminate our expansion.
* Rather, DgetC will return a DEOF when we hit the end-of-input.
register Char
*wp
= wbuf
;
register int i
= MAXWLEN
;
bool sofar
= 0, done
= 0;
/* finish this word and catch the code above the next time */
/* We preserve ` quotations which are done yet later */
* Note that DgetC never returns a QUOTES character from an
* expansion, so only true input quotes will get us here or out.
dolflg
= c1
== '"' ? DODOL
: 0;
if (c
== '\n' || c
== DEOF
)
stderror(ERR_UNMATCHED
, c1
);
if ((c
& (QUOTE
| TRIM
)) == ('\n' | QUOTE
))
* Leave any `s alone for later. Other chars are all
* quoted, thus `...` can tell it was within "...".
*wp
++ = c
== '`' ? '`' : c
| QUOTE
;
/* Prevent all further interpretation */
/* Leave all text alone for later */
*wp
++ = '`' /* i--; eliminated */;
if ((wp
= Dpack(wbuf
, wp
)) == NULL
)
i
= MAXWLEN
- (wp
- wbuf
);
c
= DgetC(0); /* No $ subst! */
if (c
== '\n' || c
== DEOF
) {
if ((wp
= Dpack(wbuf
, wp
)) == NULL
)
i
= MAXWLEN
- (wp
- wbuf
);
* Get a character, performing $ substitution unless flag is 0.
* Any QUOTES character which is returned from a $ expansion is
* QUOTEd so that it will not be recognized above.
if ((c
= Dpeekc
) != '\0') {
c
= *lap
++ & (QUOTE
| TRIM
);
if ((c
= *dolp
++ & (QUOTE
| TRIM
)) != '\0')
static Char
*nulvec
[] = {0};
static struct varent nulargv
= {nulvec
, STRargv
, { NULL
, NULL
, NULL
}, 0};
stderror(ERR_NAME
| ERR_RANGE
);
* Handle the multitudinous $ expansion forms.
register struct varent
*vp
= NULL
;
Char name
[4 * MAXVARLEN
+ 1];
int subscr
= 0, lwb
= 1, upb
= 0;
bool dimen
= 0, bitset
= 0;
static Char
*dolbang
= NULL
;
dolnmod
= dolmcnt
= dolwcnt
= 0;
c
= DgetC(0); /* sc is { to take } later */
dimen
++, c
= DgetC(0); /* $# takes dimension */
bitset
++, c
= DgetC(0); /* $? tests existence */
setDolp(dolbang
= putn(backpid
));
stderror(ERR_NOTALLOWED
, "$?<");
stderror(ERR_NOTALLOWED
, "$?#");
for (np
= wbuf
; read(OLDSTD
, &tnp
, 1) == 1; np
++) {
*np
= (unsigned char) tnp
;
if (np
>= &wbuf
[BUFSIZ
- 1])
* KLUDGE: dolmod is set here because it will cause setDolp to call
* domod and thus to copy wbuf. Otherwise setDolp would use it
* directly. If we saved it ourselves, no one would know when to free
* it. The actual function of the 'q' causes filename expansion not to
* be done on the interpolated value.
(void) Strcpy(name
, STRargv
);
subscr
= -1; /* Prevent eating [...] */
stderror(ERR_NOTALLOWED
, "$#<num>");
subscr
= subscr
* 10 + c
- '0';
dolp
= ffile
? STR1
: STR0
;
if (np
>= &name
[MAXVARLEN
])
stderror(ERR_VARTOOLONG
);
dolp
= (vp
|| getenv(short2str(name
))) ? STR1
: STR0
;
np
= str2short(getenv(short2str(name
)));
if (dimen
== 0 && subscr
== 0 && c
== '[') {
c
= DgetC(DODOL
); /* Allow $ expand within [ ] */
if (c
== '\n' || c
== DEOF
)
if (np
>= &name
[sizeof(name
) / sizeof(Char
) - 2])
stderror(ERR_VARTOOLONG
);
if (dolp
|| dolcnt
) /* $ exp must end before ] */
for (i
= 0; Isdigit(*np
); i
= i
* 10 + *np
++ - '0')
if ((i
< 0 || i
> upb
) && !any("-*", *np
)) {
stderror(ERR_MISSING
, '-');
i
= i
* 10 + *np
++ - '0';
Char
*cp
= putn(upb
- lwb
+ 1);
dolnxt
= &vp
->vec
[lwb
- 1];
stderror(ERR_MISSING
, '}');
c
= DgetC(0), dolmcnt
= 1, dolwcnt
= 1;
if (c
== 'g' || c
== 'a') {
if ((c
== 'g' && dolmcnt
!= 10000) ||
(c
== 'a' && dolwcnt
!= 10000)) {
if (c
== 's') { /* [eichin:19910926.0755EST] */
dolmod
[dolnmod
++] = delim
;
if (!delim
|| letter(delim
)
|| Isdigit(delim
) || any(" \t\n", delim
)) {
while ((c
= DgetC(0)) != (-1)) {
if(c
== delim
) delimcnt
--;
while ((c
= DgetC(0)) == ':');
if (dolnmod
== 0 || dolmcnt
== 0) {
for (i
= 0; i
< dolnmod
; i
++) {
/* handle s// [eichin:19910926.0510EST] */
Char
*lhsub
, *rhsub
, *np
;
size_t lhlen
= 0, rhlen
= 0;
if (!delim
|| letter(delim
)
|| Isdigit(delim
) || any(" \t\n", delim
)) {
while(dolmod
[i
] != delim
&& dolmod
[++i
]) {
while(dolmod
[i
] != delim
&& dolmod
[++i
]) {
np
= (Char
*) xmalloc((size_t)
((Strlen(cp
) + 1 - lhlen
+ rhlen
) *
(void) Strncpy(np
, cp
, dp
- cp
);
(void) Strcpy(np
+ (dp
- cp
), rhsub
);
(void) Strcpy(np
+ (dp
- cp
) + rhlen
, dp
+ lhlen
);
/* should this do a seterror? */
while (dolwcnt
== 10000);
* restore dolmod for additional words
dolmod
[i
] = rhsub
[-1] = delim
;
if ((dp
= domod(cp
, dolmod
[i
]))) {
if (Strcmp(cp
, dp
) == 0) {
while (dolwcnt
== 10000);
if ((c
= Dpeekrd
) != '\0') {
return (c
& (QUOTE
| TRIM
));
* Form a shell temporary file (in unit 0) from the words
* of the shell input up to EOF or a line the same as "term".
* Unit 0 should have been closed before this call.
Char obuf
[BUFSIZ
], lbuf
[BUFSIZ
], mbuf
[BUFSIZ
];
register Char
*lbp
, *obp
, *mbp
;
if (creat(tmp
= short2str(shtemp
), 0600) < 0)
stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
if (open(tmp
, O_RDWR
) < 0) {
stderror(ERR_SYSTEM
, tmp
, strerror(errno
));
(void) unlink(tmp
); /* 0 0 inode! */
c
= readc(1); /* 1 -> Want EOF returns */
if ((c
&= TRIM
) != '\0') {
stderror(ERR_NAME
| ERR_OVERFLOW
);
* Check for EOF or compare to terminator -- before expansion
if (c
< 0 || eq(lbuf
, term
)) {
(void) write(0, short2str(obuf
), (size_t) (BUFSIZ
- ocnt
));
(void) lseek(0, 0l, L_SET
);
* If term was quoted or -n just pass it on
for (lbp
= lbuf
; (c
= *lbp
++) != '\0';) {
(void) write(0, short2str(obuf
), BUFSIZ
);
* Term wasn't quoted so variable and then command expand the input
/* \ quotes \ $ ` here */
unDgetC(c
| QUOTE
), c
= '\\';
stderror(ERR_NAME
| ERR_OVERFLOW
);
* If any ` in line do command substitution
if (any(short2str(mbp
), '`')) {
* 1 arg to dobackp causes substitution to be literal. Words are
* broken only at newlines so that all blanks and tabs are
* preserved. Blank lines (null words) are not discarded.
/* Setup trivial vector similar to return of dobackp */
Dv
[0] = mbp
, Dv
[1] = NULL
, vp
= Dv
;
* Resurrect the words from the command substitution each separated by
* a newline. Note that the last newline of a command substitution
* will have been discarded, but we put a newline after the last word
* because this represents the newline after the last input line!
for (mbp
= *vp
; *mbp
; mbp
++) {
(void) write(0, short2str(obuf
), BUFSIZ
);
(void) write(0, short2str(obuf
), BUFSIZ
);
blkfree(pargv
), pargv
= 0;