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