Home | History | Annotate | Line # | Download | only in csh
dol.c revision 1.1
      1 /*-
      2  * Copyright (c) 1980, 1991 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char sccsid[] = "@(#)dol.c	5.13 (Berkeley) 6/8/91";
     36 #endif /* not lint */
     37 
     38 #include <sys/types.h>
     39 #include <fcntl.h>
     40 #include <errno.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 #if __STDC__
     45 # include <stdarg.h>
     46 #else
     47 # include <varargs.h>
     48 #endif
     49 
     50 #include "csh.h"
     51 #include "extern.h"
     52 
     53 /*
     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.
     60  */
     61 
     62 static int Dpeekc, Dpeekrd;	/* Peeks for DgetC and Dreadc */
     63 static Char *Dcp, **Dvp;	/* Input vector for Dreadc */
     64 
     65 #define	DEOF	-1
     66 
     67 #define	unDgetC(c)	Dpeekc = c
     68 
     69 #define QUOTES		(_Q|_Q1|_ESC)	/* \ ' " ` */
     70 
     71 /*
     72  * The following variables give the information about the current
     73  * $ expansion, recording the current word position, the remaining
     74  * words within this expansion, the count of remaining words, and the
     75  * information about any : modifier which is being applied.
     76  */
     77 static Char *dolp;		/* Remaining chars from this word */
     78 static Char **dolnxt;		/* Further words */
     79 static int dolcnt;		/* Count of further words */
     80 static Char dolmod;		/* : modifier character */
     81 static int dolmcnt;		/* :gx -> 10000, else 1 */
     82 
     83 static void	 Dfix2 __P((Char **));
     84 static Char 	*Dpack __P((Char *, Char *));
     85 static int	 Dword __P((void));
     86 static void	 dolerror __P((Char *));
     87 static int	 DgetC __P((int));
     88 static void	 Dgetdol __P((void));
     89 static void	 fixDolMod __P((void));
     90 static void	 setDolp __P((Char *));
     91 static void	 unDredc __P((int));
     92 static int	 Dredc __P((void));
     93 static void	 Dtestq __P((int));
     94 
     95 
     96 /*
     97  * Fix up the $ expansions and quotations in the
     98  * argument list to command t.
     99  */
    100 void
    101 Dfix(t)
    102     register struct command *t;
    103 {
    104     register Char **pp;
    105     register Char *p;
    106 
    107     if (noexec)
    108 	return;
    109     /* Note that t_dcom isn't trimmed thus !...:q's aren't lost */
    110     for (pp = t->t_dcom; p = *pp++;)
    111 	for (; *p; p++) {
    112 	    if (cmap(*p, _DOL | QUOTES)) {	/* $, \, ', ", ` */
    113 		Dfix2(t->t_dcom);	/* found one */
    114 		blkfree(t->t_dcom);
    115 		t->t_dcom = gargv;
    116 		gargv = 0;
    117 		return;
    118 	    }
    119 	}
    120 }
    121 
    122 /*
    123  * $ substitute one word, for i/o redirection
    124  */
    125 Char   *
    126 Dfix1(cp)
    127     register Char *cp;
    128 {
    129     Char   *Dv[2];
    130 
    131     if (noexec)
    132 	return (0);
    133     Dv[0] = cp;
    134     Dv[1] = NULL;
    135     Dfix2(Dv);
    136     if (gargc != 1) {
    137 	setname(short2str(cp));
    138 	stderror(ERR_NAME | ERR_AMBIG);
    139     }
    140     cp = Strsave(gargv[0]);
    141     blkfree(gargv), gargv = 0;
    142     return (cp);
    143 }
    144 
    145 /*
    146  * Subroutine to do actual fixing after state initialization.
    147  */
    148 static void
    149 Dfix2(v)
    150     Char  **v;
    151 {
    152     ginit();			/* Initialize glob's area pointers */
    153     Dvp = v;
    154     Dcp = STRNULL;		/* Setup input vector for Dreadc */
    155     unDgetC(0);
    156     unDredc(0);			/* Clear out any old peeks (at error) */
    157     dolp = 0;
    158     dolcnt = 0;			/* Clear out residual $ expands (...) */
    159     while (Dword())
    160 	continue;
    161 }
    162 
    163 #define MAXWLEN (BUFSIZ - 4)
    164 /*
    165  * Pack up more characters in this word
    166  */
    167 static Char *
    168 Dpack(wbuf, wp)
    169     Char   *wbuf, *wp;
    170 {
    171     register int c;
    172     register int i = MAXWLEN - (wp - wbuf);
    173 
    174     for (;;) {
    175 	c = DgetC(DODOL);
    176 	if (c == '\\') {
    177 	    c = DgetC(0);
    178 	    if (c == DEOF) {
    179 		unDredc(c);
    180 		*wp = 0;
    181 		Gcat(STRNULL, wbuf);
    182 		return (NULL);
    183 	    }
    184 	    if (c == '\n')
    185 		c = ' ';
    186 	    else
    187 		c |= QUOTE;
    188 	}
    189 	if (c == DEOF) {
    190 	    unDredc(c);
    191 	    *wp = 0;
    192 	    Gcat(STRNULL, wbuf);
    193 	    return (NULL);
    194 	}
    195 	if (cmap(c, _SP | _NL | _Q | _Q1)) {	/* sp \t\n'"` */
    196 	    unDgetC(c);
    197 	    if (cmap(c, QUOTES))
    198 		return (wp);
    199 	    *wp++ = 0;
    200 	    Gcat(STRNULL, wbuf);
    201 	    return (NULL);
    202 	}
    203 	if (--i <= 0)
    204 	    stderror(ERR_WTOOLONG);
    205 	*wp++ = c;
    206     }
    207 }
    208 
    209 /*
    210  * Get a word.  This routine is analogous to the routine
    211  * word() in sh.lex.c for the main lexical input.  One difference
    212  * here is that we don't get a newline to terminate our expansion.
    213  * Rather, DgetC will return a DEOF when we hit the end-of-input.
    214  */
    215 static int
    216 Dword()
    217 {
    218     register int c, c1;
    219     Char    wbuf[BUFSIZ];
    220     register Char *wp = wbuf;
    221     register int i = MAXWLEN;
    222     register bool dolflg;
    223     bool    sofar = 0, done = 0;
    224 
    225     while (!done) {
    226 	done = 1;
    227 	c = DgetC(DODOL);
    228 	switch (c) {
    229 
    230 	case DEOF:
    231 	    if (sofar == 0)
    232 		return (0);
    233 	    /* finish this word and catch the code above the next time */
    234 	    unDredc(c);
    235 	    /* fall into ... */
    236 
    237 	case '\n':
    238 	    *wp = 0;
    239 	    Gcat(STRNULL, wbuf);
    240 	    return (1);
    241 
    242 	case ' ':
    243 	case '\t':
    244 	    done = 0;
    245 	    break;
    246 
    247 	case '`':
    248 	    /* We preserve ` quotations which are done yet later */
    249 	    *wp++ = c, --i;
    250 	case '\'':
    251 	case '"':
    252 	    /*
    253 	     * Note that DgetC never returns a QUOTES character from an
    254 	     * expansion, so only true input quotes will get us here or out.
    255 	     */
    256 	    c1 = c;
    257 	    dolflg = c1 == '"' ? DODOL : 0;
    258 	    for (;;) {
    259 		c = DgetC(dolflg);
    260 		if (c == c1)
    261 		    break;
    262 		if (c == '\n' || c == DEOF)
    263 		    stderror(ERR_UNMATCHED, c1);
    264 		if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE))
    265 		    --wp, ++i;
    266 		if (--i <= 0)
    267 		    stderror(ERR_WTOOLONG);
    268 		switch (c1) {
    269 
    270 		case '"':
    271 		    /*
    272 		     * Leave any `s alone for later. Other chars are all
    273 		     * quoted, thus `...` can tell it was within "...".
    274 		     */
    275 		    *wp++ = c == '`' ? '`' : c | QUOTE;
    276 		    break;
    277 
    278 		case '\'':
    279 		    /* Prevent all further interpretation */
    280 		    *wp++ = c | QUOTE;
    281 		    break;
    282 
    283 		case '`':
    284 		    /* Leave all text alone for later */
    285 		    *wp++ = c;
    286 		    break;
    287 		}
    288 	    }
    289 	    if (c1 == '`')
    290 		*wp++ = '`', --i;
    291 	    sofar = 1;
    292 	    if ((wp = Dpack(wbuf, wp)) == NULL)
    293 		return (1);
    294 	    else {
    295 		i = MAXWLEN - (wp - wbuf);
    296 		done = 0;
    297 	    }
    298 	    break;
    299 
    300 	case '\\':
    301 	    c = DgetC(0);	/* No $ subst! */
    302 	    if (c == '\n' || c == DEOF) {
    303 		done = 0;
    304 		break;
    305 	    }
    306 	    c |= QUOTE;
    307 	    break;
    308 	}
    309 	if (done) {
    310 	    unDgetC(c);
    311 	    sofar = 1;
    312 	    if ((wp = Dpack(wbuf, wp)) == NULL)
    313 		return (1);
    314 	    else {
    315 		i = MAXWLEN - (wp - wbuf);
    316 		done = 0;
    317 	    }
    318 	}
    319     }
    320     /* Really NOTREACHED */
    321     return (0);
    322 }
    323 
    324 
    325 /*
    326  * Get a character, performing $ substitution unless flag is 0.
    327  * Any QUOTES character which is returned from a $ expansion is
    328  * QUOTEd so that it will not be recognized above.
    329  */
    330 static int
    331 DgetC(flag)
    332     register int flag;
    333 {
    334     register int c;
    335 
    336 top:
    337     if (c = Dpeekc) {
    338 	Dpeekc = 0;
    339 	return (c);
    340     }
    341     if (lap) {
    342 	c = *lap++ & (QUOTE | TRIM);
    343 	if (c == 0) {
    344 	    lap = 0;
    345 	    goto top;
    346 	}
    347 quotspec:
    348 	if (cmap(c, QUOTES))
    349 	    return (c | QUOTE);
    350 	return (c);
    351     }
    352     if (dolp) {
    353 	if (c = *dolp++ & (QUOTE | TRIM))
    354 	    goto quotspec;
    355 	if (dolcnt > 0) {
    356 	    setDolp(*dolnxt++);
    357 	    --dolcnt;
    358 	    return (' ');
    359 	}
    360 	dolp = 0;
    361     }
    362     if (dolcnt > 0) {
    363 	setDolp(*dolnxt++);
    364 	--dolcnt;
    365 	goto top;
    366     }
    367     c = Dredc();
    368     if (c == '$' && flag) {
    369 	Dgetdol();
    370 	goto top;
    371     }
    372     return (c);
    373 }
    374 
    375 static Char *nulvec[] = {0};
    376 static struct varent nulargv = {nulvec, STRargv, 0};
    377 
    378 static void
    379 dolerror(s)
    380     Char   *s;
    381 {
    382     setname(short2str(s));
    383     stderror(ERR_NAME | ERR_RANGE);
    384 }
    385 
    386 /*
    387  * Handle the multitudinous $ expansion forms.
    388  * Ugh.
    389  */
    390 static void
    391 Dgetdol()
    392 {
    393     register Char *np;
    394     register struct varent *vp = NULL;
    395     Char    name[4 * MAXVARLEN + 1];
    396     int     c, sc;
    397     int     subscr = 0, lwb = 1, upb = 0;
    398     bool    dimen = 0, bitset = 0;
    399     char    tnp;
    400     Char    wbuf[BUFSIZ];
    401 
    402     dolmod = dolmcnt = 0;
    403     c = sc = DgetC(0);
    404     if (c == '{')
    405 	c = DgetC(0);		/* sc is { to take } later */
    406     if ((c & TRIM) == '#')
    407 	dimen++, c = DgetC(0);	/* $# takes dimension */
    408     else if (c == '?')
    409 	bitset++, c = DgetC(0);	/* $? tests existence */
    410     switch (c) {
    411 
    412     case '$':
    413 	if (dimen || bitset)
    414 	    stderror(ERR_SYNTAX);
    415 	setDolp(doldol);
    416 	goto eatbrac;
    417 
    418     case '<' | QUOTE:
    419 	if (bitset)
    420 	    stderror(ERR_NOTALLOWED, "$?<");
    421 	if (dimen)
    422 	    stderror(ERR_NOTALLOWED, "$?#");
    423 	for (np = wbuf; read(OLDSTD, &tnp, 1) == 1; np++) {
    424 	    *np = tnp;
    425 	    if (np >= &wbuf[BUFSIZ - 1])
    426 		stderror(ERR_LTOOLONG);
    427 	    if (SIGN_EXTEND_CHAR(tnp) <= 0 || tnp == '\n')
    428 		break;
    429 	}
    430 	*np = 0;
    431 	/*
    432 	 * KLUDGE: dolmod is set here because it will cause setDolp to call
    433 	 * domod and thus to copy wbuf. Otherwise setDolp would use it
    434 	 * directly. If we saved it ourselves, no one would know when to free
    435 	 * it. The actual function of the 'q' causes filename expansion not to
    436 	 * be done on the interpolated value.
    437 	 */
    438 	dolmod = 'q';
    439 	dolmcnt = 10000;
    440 	setDolp(wbuf);
    441 	goto eatbrac;
    442 
    443     case DEOF:
    444     case '\n':
    445 	stderror(ERR_SYNTAX);
    446 	/* NOTREACHED */
    447 	break;
    448 
    449     case '*':
    450 	(void) Strcpy(name, STRargv);
    451 	vp = adrof(STRargv);
    452 	subscr = -1;		/* Prevent eating [...] */
    453 	break;
    454 
    455     default:
    456 	np = name;
    457 	if (Isdigit(c)) {
    458 	    if (dimen)
    459 		stderror(ERR_NOTALLOWED, "$#<num>");
    460 	    subscr = 0;
    461 	    do {
    462 		subscr = subscr * 10 + c - '0';
    463 		c = DgetC(0);
    464 	    } while (Isdigit(c));
    465 	    unDredc(c);
    466 	    if (subscr < 0) {
    467 		dolerror(vp->v_name);
    468 		return;
    469 	    }
    470 	    if (subscr == 0) {
    471 		if (bitset) {
    472 		    dolp = ffile ? STR1 : STR0;
    473 		    goto eatbrac;
    474 		}
    475 		if (ffile == 0)
    476 		    stderror(ERR_DOLZERO);
    477 		fixDolMod();
    478 		setDolp(ffile);
    479 		goto eatbrac;
    480 	    }
    481 	    if (bitset)
    482 		stderror(ERR_DOLQUEST);
    483 	    vp = adrof(STRargv);
    484 	    if (vp == 0) {
    485 		vp = &nulargv;
    486 		goto eatmod;
    487 	    }
    488 	    break;
    489 	}
    490 	if (!alnum(c))
    491 	    stderror(ERR_VARALNUM);
    492 	for (;;) {
    493 	    *np++ = c;
    494 	    c = DgetC(0);
    495 	    if (!alnum(c))
    496 		break;
    497 	    if (np >= &name[MAXVARLEN])
    498 		stderror(ERR_VARTOOLONG);
    499 	}
    500 	*np++ = 0;
    501 	unDredc(c);
    502 	vp = adrof(name);
    503     }
    504     if (bitset) {
    505 	dolp = (vp || getenv(short2str(name))) ? STR1 : STR0;
    506 	goto eatbrac;
    507     }
    508     if (vp == 0) {
    509 	np = str2short(getenv(short2str(name)));
    510 	if (np) {
    511 	    fixDolMod();
    512 	    setDolp(np);
    513 	    goto eatbrac;
    514 	}
    515 	udvar(name);
    516 	/* NOTREACHED */
    517     }
    518     c = DgetC(0);
    519     upb = blklen(vp->vec);
    520     if (dimen == 0 && subscr == 0 && c == '[') {
    521 	np = name;
    522 	for (;;) {
    523 	    c = DgetC(DODOL);	/* Allow $ expand within [ ] */
    524 	    if (c == ']')
    525 		break;
    526 	    if (c == '\n' || c == DEOF)
    527 		stderror(ERR_INCBR);
    528 	    if (np >= &name[sizeof(name) / sizeof(Char) - 2])
    529 		stderror(ERR_VARTOOLONG);
    530 	    *np++ = c;
    531 	}
    532 	*np = 0, np = name;
    533 	if (dolp || dolcnt)	/* $ exp must end before ] */
    534 	    stderror(ERR_EXPORD);
    535 	if (!*np)
    536 	    stderror(ERR_SYNTAX);
    537 	if (Isdigit(*np)) {
    538 	    int     i;
    539 
    540 	    for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0');
    541 	    if ((i < 0 || i > upb) && !any("-*", *np)) {
    542 		dolerror(vp->v_name);
    543 		return;
    544 	    }
    545 	    lwb = i;
    546 	    if (!*np)
    547 		upb = lwb, np = STRstar;
    548 	}
    549 	if (*np == '*')
    550 	    np++;
    551 	else if (*np != '-')
    552 	    stderror(ERR_MISSING, '-');
    553 	else {
    554 	    register int i = upb;
    555 
    556 	    np++;
    557 	    if (Isdigit(*np)) {
    558 		i = 0;
    559 		while (Isdigit(*np))
    560 		    i = i * 10 + *np++ - '0';
    561 		if (i < 0 || i > upb) {
    562 		    dolerror(vp->v_name);
    563 		    return;
    564 		}
    565 	    }
    566 	    if (i < lwb)
    567 		upb = lwb - 1;
    568 	    else
    569 		upb = i;
    570 	}
    571 	if (lwb == 0) {
    572 	    if (upb != 0) {
    573 		dolerror(vp->v_name);
    574 		return;
    575 	    }
    576 	    upb = -1;
    577 	}
    578 	if (*np)
    579 	    stderror(ERR_SYNTAX);
    580     }
    581     else {
    582 	if (subscr > 0)
    583 	    if (subscr > upb)
    584 		lwb = 1, upb = 0;
    585 	    else
    586 		lwb = upb = subscr;
    587 	unDredc(c);
    588     }
    589     if (dimen) {
    590 	Char   *cp = putn(upb - lwb + 1);
    591 
    592 	addla(cp);
    593 	xfree((ptr_t) cp);
    594     }
    595     else {
    596 eatmod:
    597 	fixDolMod();
    598 	dolnxt = &vp->vec[lwb - 1];
    599 	dolcnt = upb - lwb + 1;
    600     }
    601 eatbrac:
    602     if (sc == '{') {
    603 	c = Dredc();
    604 	if (c != '}')
    605 	    stderror(ERR_MISSING, '}');
    606     }
    607 }
    608 
    609 static void
    610 fixDolMod()
    611 {
    612     register int c;
    613 
    614     c = DgetC(0);
    615     if (c == ':') {
    616 	c = DgetC(0), dolmcnt = 1;
    617 	if (c == 'g')
    618 	    c = DgetC(0), dolmcnt = 10000;
    619 	if (!any("htrqxe", c))
    620 	    stderror(ERR_BADMOD, c);
    621 	dolmod = c;
    622 	if (c == 'q')
    623 	    dolmcnt = 10000;
    624     }
    625     else
    626 	unDredc(c);
    627 }
    628 
    629 static void
    630 setDolp(cp)
    631     register Char *cp;
    632 {
    633     register Char *dp;
    634 
    635     if (dolmod == 0 || dolmcnt == 0) {
    636 	dolp = cp;
    637 	return;
    638     }
    639     dp = domod(cp, dolmod);
    640     if (dp) {
    641 	dolmcnt--;
    642 	addla(dp);
    643 	xfree((ptr_t) dp);
    644     }
    645     else
    646 	addla(cp);
    647     dolp = STRNULL;
    648     if (seterr)
    649 	stderror(ERR_OLD);
    650 }
    651 
    652 static void
    653 unDredc(c)
    654     int     c;
    655 {
    656 
    657     Dpeekrd = c;
    658 }
    659 
    660 static int
    661 Dredc()
    662 {
    663     register int c;
    664 
    665     if (c = Dpeekrd) {
    666 	Dpeekrd = 0;
    667 	return (c);
    668     }
    669     if (Dcp && (c = *Dcp++))
    670 	return (c & (QUOTE | TRIM));
    671     if (*Dvp == 0) {
    672 	Dcp = 0;
    673 	return (DEOF);
    674     }
    675     Dcp = *Dvp++;
    676     return (' ');
    677 }
    678 
    679 static void
    680 Dtestq(c)
    681     register int c;
    682 {
    683 
    684     if (cmap(c, QUOTES))
    685 	gflag = 1;
    686 }
    687 
    688 /*
    689  * Form a shell temporary file (in unit 0) from the words
    690  * of the shell input up to EOF or a line the same as "term".
    691  * Unit 0 should have been closed before this call.
    692  */
    693 void
    694 heredoc(term)
    695     Char   *term;
    696 {
    697     register int c;
    698     Char   *Dv[2];
    699     Char    obuf[BUFSIZ], lbuf[BUFSIZ], mbuf[BUFSIZ];
    700     int     ocnt, lcnt, mcnt;
    701     register Char *lbp, *obp, *mbp;
    702     Char  **vp;
    703     bool    quoted;
    704     char   *tmp;
    705 
    706     if (creat(tmp = short2str(shtemp), 0600) < 0)
    707 	stderror(ERR_SYSTEM, tmp, strerror(errno));
    708     (void) close(0);
    709     if (open(tmp, O_RDWR) < 0) {
    710 	int     oerrno = errno;
    711 
    712 	(void) unlink(tmp);
    713 	errno = oerrno;
    714 	stderror(ERR_SYSTEM, tmp, strerror(errno));
    715     }
    716     (void) unlink(tmp);		/* 0 0 inode! */
    717     Dv[0] = term;
    718     Dv[1] = NULL;
    719     gflag = 0;
    720     trim(Dv);
    721     rscan(Dv, Dtestq);
    722     quoted = gflag;
    723     ocnt = BUFSIZ;
    724     obp = obuf;
    725     for (;;) {
    726 	/*
    727 	 * Read up a line
    728 	 */
    729 	lbp = lbuf;
    730 	lcnt = BUFSIZ - 4;
    731 	for (;;) {
    732 	    c = readc(1);	/* 1 -> Want EOF returns */
    733 	    if (c < 0 || c == '\n')
    734 		break;
    735 	    if (c &= TRIM) {
    736 		*lbp++ = c;
    737 		if (--lcnt < 0) {
    738 		    setname("<<");
    739 		    stderror(ERR_NAME | ERR_OVERFLOW);
    740 		}
    741 	    }
    742 	}
    743 	*lbp = 0;
    744 
    745 	/*
    746 	 * Check for EOF or compare to terminator -- before expansion
    747 	 */
    748 	if (c < 0 || eq(lbuf, term)) {
    749 	    (void) write(0, short2str(obuf), (size_t) (BUFSIZ - ocnt));
    750 	    (void) lseek(0, 0l, L_SET);
    751 	    return;
    752 	}
    753 
    754 	/*
    755 	 * If term was quoted or -n just pass it on
    756 	 */
    757 	if (quoted || noexec) {
    758 	    *lbp++ = '\n';
    759 	    *lbp = 0;
    760 	    for (lbp = lbuf; c = *lbp++;) {
    761 		*obp++ = c;
    762 		if (--ocnt == 0) {
    763 		    (void) write(0, short2str(obuf), BUFSIZ);
    764 		    obp = obuf;
    765 		    ocnt = BUFSIZ;
    766 		}
    767 	    }
    768 	    continue;
    769 	}
    770 
    771 	/*
    772 	 * Term wasn't quoted so variable and then command expand the input
    773 	 * line
    774 	 */
    775 	Dcp = lbuf;
    776 	Dvp = Dv + 1;
    777 	mbp = mbuf;
    778 	mcnt = BUFSIZ - 4;
    779 	for (;;) {
    780 	    c = DgetC(DODOL);
    781 	    if (c == DEOF)
    782 		break;
    783 	    if ((c &= TRIM) == 0)
    784 		continue;
    785 	    /* \ quotes \ $ ` here */
    786 	    if (c == '\\') {
    787 		c = DgetC(0);
    788 		if (!any("$\\`", c))
    789 		    unDgetC(c | QUOTE), c = '\\';
    790 		else
    791 		    c |= QUOTE;
    792 	    }
    793 	    *mbp++ = c;
    794 	    if (--mcnt == 0) {
    795 		setname("<<");
    796 		stderror(ERR_NAME | ERR_OVERFLOW);
    797 	    }
    798 	}
    799 	*mbp++ = 0;
    800 
    801 	/*
    802 	 * If any ` in line do command substitution
    803 	 */
    804 	mbp = mbuf;
    805 	if (any(short2str(mbp), '`')) {
    806 	    /*
    807 	     * 1 arg to dobackp causes substitution to be literal. Words are
    808 	     * broken only at newlines so that all blanks and tabs are
    809 	     * preserved.  Blank lines (null words) are not discarded.
    810 	     */
    811 	    vp = dobackp(mbuf, 1);
    812 	}
    813 	else
    814 	    /* Setup trivial vector similar to return of dobackp */
    815 	    Dv[0] = mbp, Dv[1] = NULL, vp = Dv;
    816 
    817 	/*
    818 	 * Resurrect the words from the command substitution each separated by
    819 	 * a newline.  Note that the last newline of a command substitution
    820 	 * will have been discarded, but we put a newline after the last word
    821 	 * because this represents the newline after the last input line!
    822 	 */
    823 	for (; *vp; vp++) {
    824 	    for (mbp = *vp; *mbp; mbp++) {
    825 		*obp++ = *mbp & TRIM;
    826 		if (--ocnt == 0) {
    827 		    (void) write(0, short2str(obuf), BUFSIZ);
    828 		    obp = obuf;
    829 		    ocnt = BUFSIZ;
    830 		}
    831 	    }
    832 	    *obp++ = '\n';
    833 	    if (--ocnt == 0) {
    834 		(void) write(0, short2str(obuf), BUFSIZ);
    835 		obp = obuf;
    836 		ocnt = BUFSIZ;
    837 	    }
    838 	}
    839 	if (pargv)
    840 	    blkfree(pargv), pargv = 0;
    841     }
    842 }
    843