* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley Software License Agreement
* specifies the terms and conditions for redistribution.
static char *sccsid
= "@(#)dol.c 5.4 (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.
int Dpeekc
, Dpeekrd
; /* Peeks for DgetC and Dreadc */
char *Dcp
, **Dvp
; /* Input vector for Dreadc */
#define unDgetC(c) Dpeekc = c
#define QUOTES (_Q|_Q1|_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.
char *dolp
; /* Remaining chars from this word */
char **dolnxt
; /* Further words */
int dolcnt
; /* Count of further words */
char dolmod
; /* : modifier character */
int dolmcnt
; /* :gx -> 10000, else 1 */
* 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
++;)
if (cmap(*p
++, _DOL
|QUOTES
)) { /* $, \, ', ", ` */
Dfix2(t
->t_dcom
); /* found one */
* $ substitute one word, for i/o redirection
Dv
[0] = cp
; Dv
[1] = NOSTR
;
blkfree(gargv
), gargv
= 0;
* Subroutine to do actual fixing after state initialization.
ginit(agargv
); /* Initialize glob's area pointers */
Dvp
= v
; Dcp
= ""; /* Setup input vector for Dreadc */
unDgetC(0); unDredc(0); /* Clear out any old peeks (at error) */
dolp
= 0; dolcnt
= 0; /* Clear out residual $ expands (...) */
* 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
= BUFSIZ
- 4;
/* 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
dolflg
= c1
== '"' ? DODOL
: 0;
if (c
== '\n' || c
== DEOF
)
error("Unmatched %c", 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 */
goto pack
; /* continue the word */
c
= DgetC(0); /* No $ subst! */
if (c
== '\n' || c
== DEOF
)
/* pack up more characters in this word */
if (cmap(c
, _SP
|_NL
|_Q
|_Q1
)) { /* sp \t\n'"` */
* 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.
c
= *lap
++ & (QUOTE
|TRIM
);
if (c
= *dolp
++ & (QUOTE
|TRIM
))
struct varent nulargv
= { nulvec
, "argv", 0 };
* Handle the multitudinous $ expansion forms.
register struct varent
*vp
;
int subscr
= 0, lwb
= 1, upb
= 0;
bool dimen
= 0, bitset
= 0;
c
= DgetC(0); /* sc is { to take } later */
dimen
++, c
= DgetC(0); /* $# takes dimension */
bitset
++, c
= DgetC(0); /* $? tests existence */
goto syntax
; /* No $?$, $#$ */
goto syntax
; /* No $?<, $#< */
for (np
= wbuf
; read(OLDSTD
, np
, 1) == 1; np
++) {
if (np
>= &wbuf
[BUFSIZ
-1])
error("$< line too long");
if (*np
<= 0 || *np
== '\n')
* 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
, "argv");
subscr
= -1; /* Prevent eating [...] */
goto syntax
; /* No $#1, e.g. */
subscr
= subscr
* 10 + c
- '0';
if (np
>= &name
[sizeof name
- 2])
error("Variable syntax");
dolp
= (vp
|| getenv(name
)) ? "1" : "0";
if (dimen
== 0 && subscr
== 0 && c
== '[') {
c
= DgetC(DODOL
); /* Allow $ expand within [ ] */
if (c
== '\n' || c
== DEOF
)
if (np
>= &name
[sizeof name
- 2])
if (dolp
|| dolcnt
) /* $ exp must end before ] */
i
= i
* 10 + *np
++ - '0';
if ((i
< 0 || i
> upb
) && !any(*np
, "-*")) {
error("Subscript out of range");
i
= i
* 10 + *np
++ - '0';
char *cp
= putn(upb
- lwb
+ 1);
c
= DgetC(0), dolmcnt
= 1;
c
= DgetC(0), dolmcnt
= 10000;
dolnxt
= &vp
->vec
[lwb
- 1];
if (dolmod
== 0 || dolmcnt
== 0) {
* 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(shtemp
, 0600) < 0)
if (open(shtemp
, 2) < 0) {
(void) unlink(shtemp
); /* 0 0 inode! */
Dv
[0] = term
; Dv
[1] = NOSTR
; gflag
= 0;
trim(Dv
); rscan(Dv
, Dtestq
); quoted
= gflag
;
ocnt
= BUFSIZ
; obp
= obuf
;
lbp
= lbuf
; lcnt
= BUFSIZ
- 4;
c
= readc(1); /* 1 -> Want EOF returns */
* Check for EOF or compare to terminator -- before expansion
if (c
< 0 || eq(lbuf
, term
)) {
(void) write(0, obuf
, BUFSIZ
- ocnt
);
(void) lseek(0, (off_t
)0, 0);
* If term was quoted or -n just pass it on
for (lbp
= lbuf
; c
= *lbp
++;) {
(void) write(0, obuf
, BUFSIZ
);
obp
= obuf
; ocnt
= BUFSIZ
;
* Term wasn't quoted so variable and then command
Dcp
= lbuf
; Dvp
= Dv
+ 1; mbp
= mbuf
; mcnt
= BUFSIZ
- 4;
/* \ quotes \ $ ` here */
unDgetC(c
| QUOTE
), c
= '\\';
* If any ` in line do command substitution
* 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)
/* Setup trivial vector similar to return of dobackp */
Dv
[0] = mbp
, Dv
[1] = NOSTR
, 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
for (mbp
= *vp
; *mbp
; mbp
++) {
(void) write(0, obuf
, BUFSIZ
);
obp
= obuf
; ocnt
= BUFSIZ
;
(void) write(0, obuf
, BUFSIZ
);
obp
= obuf
; ocnt
= BUFSIZ
;
blkfree(pargv
), pargv
= 0;