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