Home | History | Annotate | Line # | Download | only in csh
func.c revision 1.40.26.1
      1  1.40.26.1  pgoyette /* $NetBSD: func.c,v 1.40.26.1 2019/01/18 08:48:24 pgoyette Exp $ */
      2        1.9       cgd 
      3        1.1       cgd /*-
      4        1.7   mycroft  * Copyright (c) 1980, 1991, 1993
      5        1.7   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.26       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.13  christos #include <sys/cdefs.h>
     33        1.1       cgd #ifndef lint
     34        1.9       cgd #if 0
     35        1.9       cgd static char sccsid[] = "@(#)func.c	8.1 (Berkeley) 5/31/93";
     36        1.9       cgd #else
     37  1.40.26.1  pgoyette __RCSID("$NetBSD: func.c,v 1.40.26.1 2019/01/18 08:48:24 pgoyette Exp $");
     38        1.9       cgd #endif
     39        1.1       cgd #endif /* not lint */
     40        1.1       cgd 
     41       1.21       wiz #include <sys/stat.h>
     42        1.1       cgd #include <sys/types.h>
     43       1.21       wiz 
     44       1.21       wiz #include <locale.h>
     45       1.40  christos #include <inttypes.h>
     46        1.1       cgd #include <signal.h>
     47       1.23       wiz #include <stdarg.h>
     48        1.1       cgd #include <stdlib.h>
     49        1.1       cgd #include <string.h>
     50        1.1       cgd #include <unistd.h>
     51       1.36     ragge #include <errno.h>
     52        1.1       cgd 
     53        1.1       cgd #include "csh.h"
     54        1.1       cgd #include "extern.h"
     55        1.1       cgd #include "pathnames.h"
     56        1.1       cgd 
     57        1.1       cgd extern char **environ;
     58       1.21       wiz extern int progprintf(int, char **);
     59        1.1       cgd 
     60       1.21       wiz static void islogin(void);
     61       1.21       wiz static void reexecute(struct command *);
     62       1.21       wiz static void preread(void);
     63       1.21       wiz static void doagain(void);
     64       1.21       wiz static void search(int, int, Char *);
     65       1.21       wiz static int getword(Char *);
     66       1.21       wiz static int keyword(Char *);
     67       1.21       wiz static void toend(void);
     68       1.21       wiz static void xecho(int, Char **);
     69       1.21       wiz static void Unsetenv(Char *);
     70       1.27  christos static void wpfree(struct whyle *);
     71        1.1       cgd 
     72        1.1       cgd struct biltins *
     73       1.21       wiz isbfunc(struct command *t)
     74        1.1       cgd {
     75        1.1       cgd     static struct biltins label = {"", dozip, 0, 0};
     76        1.1       cgd     static struct biltins foregnd = {"%job", dofg1, 0, 0};
     77        1.1       cgd     static struct biltins backgnd = {"%job &", dobg1, 0, 0};
     78       1.21       wiz     struct biltins *bp, *bp1, *bp2;
     79       1.21       wiz     Char *cp;
     80       1.21       wiz 
     81       1.21       wiz     cp = t->t_dcom[0];
     82        1.1       cgd 
     83        1.1       cgd     if (lastchr(cp) == ':') {
     84        1.1       cgd 	label.bname = short2str(cp);
     85        1.1       cgd 	return (&label);
     86        1.1       cgd     }
     87        1.1       cgd     if (*cp == '%') {
     88        1.1       cgd 	if (t->t_dflg & F_AMPERSAND) {
     89        1.1       cgd 	    t->t_dflg &= ~F_AMPERSAND;
     90        1.1       cgd 	    backgnd.bname = short2str(cp);
     91        1.1       cgd 	    return (&backgnd);
     92        1.1       cgd 	}
     93        1.1       cgd 	foregnd.bname = short2str(cp);
     94        1.1       cgd 	return (&foregnd);
     95        1.1       cgd     }
     96        1.1       cgd     /*
     97        1.1       cgd      * Binary search Bp1 is the beginning of the current search range. Bp2 is
     98        1.1       cgd      * one past the end.
     99        1.1       cgd      */
    100        1.1       cgd     for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2;) {
    101       1.12       tls 	int i;
    102        1.1       cgd 
    103        1.1       cgd 	bp = bp1 + ((bp2 - bp1) >> 1);
    104        1.1       cgd 	if ((i = *cp - *bp->bname) == 0 &&
    105        1.1       cgd 	    (i = Strcmp(cp, str2short(bp->bname))) == 0)
    106        1.1       cgd 	    return bp;
    107        1.1       cgd 	if (i < 0)
    108        1.1       cgd 	    bp2 = bp;
    109        1.1       cgd 	else
    110        1.1       cgd 	    bp1 = bp + 1;
    111        1.1       cgd     }
    112        1.1       cgd     return (0);
    113        1.1       cgd }
    114        1.1       cgd 
    115        1.1       cgd void
    116       1.21       wiz func(struct command *t, struct biltins *bp)
    117        1.1       cgd {
    118       1.21       wiz     int i;
    119        1.1       cgd 
    120        1.1       cgd     xechoit(t->t_dcom);
    121        1.1       cgd     setname(bp->bname);
    122        1.1       cgd     i = blklen(t->t_dcom) - 1;
    123       1.16   mycroft     if (i < bp->minargs)
    124        1.1       cgd 	stderror(ERR_NAME | ERR_TOOFEW);
    125       1.16   mycroft     if (i > bp->maxargs)
    126        1.1       cgd 	stderror(ERR_NAME | ERR_TOOMANY);
    127        1.1       cgd     (*bp->bfunct) (t->t_dcom, t);
    128        1.1       cgd }
    129        1.1       cgd 
    130        1.1       cgd void
    131        1.7   mycroft /*ARGSUSED*/
    132       1.21       wiz doonintr(Char **v, struct command *t)
    133        1.1       cgd {
    134       1.21       wiz     Char *cp, *vv;
    135       1.25    kleink     sigset_t nsigset;
    136        1.1       cgd 
    137       1.21       wiz     vv = v[1];
    138        1.1       cgd     if (parintr == SIG_IGN)
    139        1.1       cgd 	return;
    140       1.16   mycroft     if (setintr && intty)
    141        1.1       cgd 	stderror(ERR_NAME | ERR_TERMINAL);
    142        1.1       cgd     cp = gointr;
    143        1.1       cgd     gointr = 0;
    144  1.40.26.1  pgoyette     free(cp);
    145        1.1       cgd     if (vv == 0) {
    146       1.10   mycroft 	if (setintr) {
    147       1.25    kleink 	    sigemptyset(&nsigset);
    148       1.25    kleink 	    (void)sigaddset(&nsigset, SIGINT);
    149       1.25    kleink 	    (void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
    150       1.10   mycroft 	} else
    151       1.21       wiz 	    (void)signal(SIGINT, SIG_DFL);
    152        1.1       cgd 	gointr = 0;
    153        1.1       cgd     }
    154        1.1       cgd     else if (eq((vv = strip(vv)), STRminus)) {
    155       1.21       wiz 	(void)signal(SIGINT, SIG_IGN);
    156        1.1       cgd 	gointr = Strsave(STRminus);
    157        1.1       cgd     }
    158        1.1       cgd     else {
    159        1.1       cgd 	gointr = Strsave(vv);
    160       1.21       wiz 	(void)signal(SIGINT, pintr);
    161        1.1       cgd     }
    162        1.1       cgd }
    163        1.1       cgd 
    164        1.1       cgd void
    165        1.7   mycroft /*ARGSUSED*/
    166       1.21       wiz donohup(Char **v, struct command *t)
    167        1.1       cgd {
    168       1.16   mycroft     if (intty)
    169        1.1       cgd 	stderror(ERR_NAME | ERR_TERMINAL);
    170        1.1       cgd     if (setintr == 0) {
    171        1.1       cgd 	(void) signal(SIGHUP, SIG_IGN);
    172        1.1       cgd     }
    173        1.1       cgd }
    174        1.1       cgd 
    175        1.1       cgd void
    176        1.7   mycroft /*ARGSUSED*/
    177       1.21       wiz dozip(Char **v, struct command *t)
    178        1.1       cgd {
    179        1.1       cgd     ;
    180        1.1       cgd }
    181        1.1       cgd 
    182        1.1       cgd void
    183       1.21       wiz prvars(void)
    184        1.1       cgd {
    185        1.1       cgd     plist(&shvhed);
    186        1.1       cgd }
    187        1.1       cgd 
    188        1.1       cgd void
    189        1.7   mycroft /*ARGSUSED*/
    190       1.21       wiz doalias(Char **v, struct command *t)
    191        1.1       cgd {
    192       1.12       tls     struct varent *vp;
    193       1.12       tls     Char *p;
    194        1.1       cgd 
    195        1.1       cgd     v++;
    196        1.1       cgd     p = *v++;
    197        1.1       cgd     if (p == 0)
    198        1.1       cgd 	plist(&aliases);
    199        1.1       cgd     else if (*v == 0) {
    200        1.1       cgd 	vp = adrof1(strip(p), &aliases);
    201        1.7   mycroft 	if (vp) {
    202        1.7   mycroft 	    blkpr(cshout, vp->vec);
    203       1.15   mycroft 	    (void) fputc('\n', cshout);
    204        1.7   mycroft 	}
    205        1.1       cgd     }
    206        1.1       cgd     else {
    207        1.1       cgd 	if (eq(p, STRalias) || eq(p, STRunalias)) {
    208        1.7   mycroft 	    setname(vis_str(p));
    209        1.1       cgd 	    stderror(ERR_NAME | ERR_DANGER);
    210        1.1       cgd 	}
    211        1.1       cgd 	set1(strip(p), saveblk(v), &aliases);
    212        1.1       cgd     }
    213        1.1       cgd }
    214        1.1       cgd 
    215        1.1       cgd void
    216        1.7   mycroft /*ARGSUSED*/
    217       1.21       wiz unalias(Char **v, struct command *t)
    218        1.1       cgd {
    219        1.1       cgd     unset1(v, &aliases);
    220        1.1       cgd }
    221        1.1       cgd 
    222        1.1       cgd void
    223        1.7   mycroft /*ARGSUSED*/
    224       1.21       wiz dologout(Char **v, struct command *t)
    225        1.1       cgd {
    226        1.1       cgd     islogin();
    227        1.1       cgd     goodbye();
    228        1.1       cgd }
    229        1.1       cgd 
    230        1.1       cgd void
    231        1.7   mycroft /*ARGSUSED*/
    232       1.21       wiz dologin(Char **v, struct command *t)
    233        1.1       cgd {
    234        1.1       cgd     islogin();
    235        1.1       cgd     rechist();
    236       1.21       wiz     (void)signal(SIGTERM, parterm);
    237       1.21       wiz     (void)execl(_PATH_LOGIN, "login", short2str(v[1]), NULL);
    238        1.1       cgd     untty();
    239        1.1       cgd     xexit(1);
    240       1.14   mycroft     /* NOTREACHED */
    241        1.1       cgd }
    242        1.1       cgd 
    243        1.1       cgd static void
    244       1.21       wiz islogin(void)
    245        1.1       cgd {
    246        1.1       cgd     if (chkstop == 0 && setintr)
    247        1.1       cgd 	panystop(0);
    248        1.1       cgd     if (loginsh)
    249        1.1       cgd 	return;
    250        1.1       cgd     stderror(ERR_NOTLOGIN);
    251       1.14   mycroft     /* NOTREACHED */
    252        1.1       cgd }
    253        1.1       cgd 
    254        1.1       cgd void
    255       1.21       wiz doif(Char **v, struct command *kp)
    256        1.1       cgd {
    257       1.21       wiz     Char **vv;
    258       1.12       tls     int i;
    259        1.1       cgd 
    260        1.1       cgd     v++;
    261        1.7   mycroft     i = expr(&v);
    262        1.1       cgd     vv = v;
    263       1.16   mycroft     if (*vv == NULL)
    264        1.1       cgd 	stderror(ERR_NAME | ERR_EMPTYIF);
    265        1.1       cgd     if (eq(*vv, STRthen)) {
    266       1.16   mycroft 	if (*++vv)
    267        1.1       cgd 	    stderror(ERR_NAME | ERR_IMPRTHEN);
    268        1.7   mycroft 	setname(vis_str(STRthen));
    269        1.1       cgd 	/*
    270        1.1       cgd 	 * If expression was zero, then scan to else, otherwise just fall into
    271        1.1       cgd 	 * following code.
    272        1.1       cgd 	 */
    273        1.1       cgd 	if (!i)
    274        1.1       cgd 	    search(T_IF, 0, NULL);
    275        1.1       cgd 	return;
    276        1.1       cgd     }
    277        1.1       cgd     /*
    278        1.1       cgd      * Simple command attached to this if. Left shift the node in this tree,
    279        1.1       cgd      * munging it so we can reexecute it.
    280        1.1       cgd      */
    281        1.1       cgd     if (i) {
    282       1.40  christos 	lshift(kp->t_dcom, (size_t)(vv - kp->t_dcom));
    283        1.1       cgd 	reexecute(kp);
    284        1.1       cgd 	donefds();
    285        1.1       cgd     }
    286        1.1       cgd }
    287        1.1       cgd 
    288        1.1       cgd /*
    289        1.1       cgd  * Reexecute a command, being careful not
    290        1.1       cgd  * to redo i/o redirection, which is already set up.
    291        1.1       cgd  */
    292        1.1       cgd static void
    293       1.21       wiz reexecute(struct command *kp)
    294        1.1       cgd {
    295        1.1       cgd     kp->t_dflg &= F_SAVE;
    296        1.1       cgd     kp->t_dflg |= F_REPEAT;
    297        1.1       cgd     /*
    298        1.1       cgd      * If tty is still ours to arbitrate, arbitrate it; otherwise dont even set
    299        1.1       cgd      * pgrp's as the jobs would then have no way to get the tty (we can't give
    300        1.1       cgd      * it to them, and our parent wouldn't know their pgrp, etc.
    301        1.1       cgd      */
    302        1.1       cgd     execute(kp, (tpgrp > 0 ? tpgrp : -1), NULL, NULL);
    303        1.1       cgd }
    304        1.1       cgd 
    305        1.1       cgd void
    306        1.7   mycroft /*ARGSUSED*/
    307       1.21       wiz doelse(Char **v, struct command *t)
    308        1.1       cgd {
    309        1.1       cgd     search(T_ELSE, 0, NULL);
    310        1.1       cgd }
    311        1.1       cgd 
    312        1.1       cgd void
    313        1.7   mycroft /*ARGSUSED*/
    314       1.21       wiz dogoto(Char **v, struct command *t)
    315        1.1       cgd {
    316       1.21       wiz     Char *lp;
    317        1.1       cgd 
    318        1.7   mycroft     gotolab(lp = globone(v[1], G_ERROR));
    319  1.40.26.1  pgoyette     free(lp);
    320        1.7   mycroft }
    321        1.7   mycroft 
    322        1.7   mycroft void
    323       1.21       wiz gotolab(Char *lab)
    324        1.7   mycroft {
    325       1.12       tls     struct whyle *wp;
    326        1.1       cgd     /*
    327        1.1       cgd      * While we still can, locate any unknown ends of existing loops. This
    328        1.1       cgd      * obscure code is the WORST result of the fact that we don't really parse.
    329        1.1       cgd      */
    330        1.1       cgd     for (wp = whyles; wp; wp = wp->w_next)
    331        1.7   mycroft 	if (wp->w_end.type == F_SEEK && wp->w_end.f_seek == 0) {
    332        1.1       cgd 	    search(T_BREAK, 0, NULL);
    333        1.7   mycroft 	    btell(&wp->w_end);
    334        1.1       cgd 	}
    335        1.1       cgd 	else
    336        1.7   mycroft 	    bseek(&wp->w_end);
    337        1.7   mycroft     search(T_GOTO, 0, lab);
    338        1.1       cgd     /*
    339        1.1       cgd      * Eliminate loops which were exited.
    340        1.1       cgd      */
    341        1.1       cgd     wfree();
    342        1.1       cgd }
    343        1.1       cgd 
    344        1.1       cgd void
    345        1.7   mycroft /*ARGSUSED*/
    346       1.21       wiz doswitch(Char **v, struct command *t)
    347        1.1       cgd {
    348       1.12       tls     Char *cp, *lp;
    349        1.1       cgd 
    350        1.1       cgd     v++;
    351       1.16   mycroft     if (!*v || *(*v++) != '(')
    352        1.1       cgd 	stderror(ERR_SYNTAX);
    353        1.1       cgd     cp = **v == ')' ? STRNULL : *v++;
    354        1.1       cgd     if (*(*v++) != ')')
    355        1.1       cgd 	v--;
    356       1.16   mycroft     if (*v)
    357        1.1       cgd 	stderror(ERR_SYNTAX);
    358        1.1       cgd     search(T_SWITCH, 0, lp = globone(cp, G_ERROR));
    359  1.40.26.1  pgoyette     free(lp);
    360        1.1       cgd }
    361        1.1       cgd 
    362        1.1       cgd void
    363        1.7   mycroft /*ARGSUSED*/
    364       1.21       wiz dobreak(Char **v, struct command *t)
    365        1.1       cgd {
    366        1.1       cgd     if (whyles)
    367        1.1       cgd 	toend();
    368       1.16   mycroft     else
    369        1.1       cgd 	stderror(ERR_NAME | ERR_NOTWHILE);
    370        1.1       cgd }
    371        1.1       cgd 
    372        1.1       cgd void
    373        1.7   mycroft /*ARGSUSED*/
    374       1.21       wiz doexit(Char **v, struct command *t)
    375        1.1       cgd {
    376        1.1       cgd     if (chkstop == 0 && (intty || intact) && evalvec == 0)
    377        1.1       cgd 	panystop(0);
    378        1.1       cgd     /*
    379        1.1       cgd      * Don't DEMAND parentheses here either.
    380        1.1       cgd      */
    381        1.1       cgd     v++;
    382        1.1       cgd     if (*v) {
    383        1.7   mycroft 	set(STRstatus, putn(expr(&v)));
    384       1.16   mycroft 	if (*v)
    385        1.1       cgd 	    stderror(ERR_NAME | ERR_EXPRESSION);
    386        1.1       cgd     }
    387        1.1       cgd     btoeof();
    388        1.1       cgd     if (intty)
    389        1.1       cgd 	(void) close(SHIN);
    390        1.1       cgd }
    391        1.1       cgd 
    392        1.1       cgd void
    393        1.7   mycroft /*ARGSUSED*/
    394       1.21       wiz doforeach(Char **v, struct command *t)
    395        1.1       cgd {
    396       1.21       wiz     struct whyle *nwp;
    397       1.12       tls     Char *cp, *sp;
    398        1.1       cgd 
    399        1.1       cgd     v++;
    400        1.1       cgd     sp = cp = strip(*v);
    401       1.16   mycroft     if (!letter(*sp))
    402        1.1       cgd 	stderror(ERR_NAME | ERR_VARBEGIN);
    403        1.1       cgd     while (*cp && alnum(*cp))
    404        1.1       cgd 	cp++;
    405       1.16   mycroft     if (*cp)
    406        1.1       cgd 	stderror(ERR_NAME | ERR_VARALNUM);
    407       1.16   mycroft     if ((cp - sp) > MAXVARLEN)
    408        1.1       cgd 	stderror(ERR_NAME | ERR_VARTOOLONG);
    409        1.1       cgd     cp = *v++;
    410       1.16   mycroft     if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')')
    411        1.1       cgd 	stderror(ERR_NAME | ERR_NOPAREN);
    412        1.1       cgd     v++;
    413        1.1       cgd     gflag = 0, tglob(v);
    414        1.1       cgd     v = globall(v);
    415       1.16   mycroft     if (v == 0)
    416        1.1       cgd 	stderror(ERR_NAME | ERR_NOMATCH);
    417        1.1       cgd     nwp = (struct whyle *) xcalloc(1, sizeof *nwp);
    418        1.1       cgd     nwp->w_fe = nwp->w_fe0 = v;
    419        1.1       cgd     gargv = 0;
    420        1.7   mycroft     btell(&nwp->w_start);
    421        1.1       cgd     nwp->w_fename = Strsave(cp);
    422        1.1       cgd     nwp->w_next = whyles;
    423        1.7   mycroft     nwp->w_end.type = F_SEEK;
    424        1.1       cgd     whyles = nwp;
    425        1.1       cgd     /*
    426        1.1       cgd      * Pre-read the loop so as to be more comprehensible to a terminal user.
    427        1.1       cgd      */
    428        1.1       cgd     if (intty)
    429        1.1       cgd 	preread();
    430        1.1       cgd     doagain();
    431        1.1       cgd }
    432        1.1       cgd 
    433        1.1       cgd void
    434        1.7   mycroft /*ARGSUSED*/
    435       1.21       wiz dowhile(Char **v, struct command *t)
    436        1.1       cgd {
    437       1.12       tls     int status;
    438       1.35  christos     int again;
    439        1.1       cgd 
    440       1.21       wiz     again = whyles != 0 && SEEKEQ(&whyles->w_start, &lineloc) &&
    441       1.21       wiz         whyles->w_fename == 0;
    442        1.1       cgd     v++;
    443        1.1       cgd     /*
    444        1.1       cgd      * Implement prereading here also, taking care not to evaluate the
    445        1.1       cgd      * expression before the loop has been read up from a terminal.
    446        1.1       cgd      */
    447        1.1       cgd     if (intty && !again)
    448        1.1       cgd 	status = !exp0(&v, 1);
    449        1.1       cgd     else
    450        1.7   mycroft 	status = !expr(&v);
    451       1.16   mycroft     if (*v)
    452        1.1       cgd 	stderror(ERR_NAME | ERR_EXPRESSION);
    453        1.1       cgd     if (!again) {
    454       1.12       tls 	struct whyle *nwp =
    455       1.21       wiz 	(struct whyle *)xcalloc(1, sizeof(*nwp));
    456        1.1       cgd 
    457        1.1       cgd 	nwp->w_start = lineloc;
    458        1.7   mycroft 	nwp->w_end.type = F_SEEK;
    459        1.7   mycroft 	nwp->w_end.f_seek = 0;
    460        1.1       cgd 	nwp->w_next = whyles;
    461        1.1       cgd 	whyles = nwp;
    462        1.1       cgd 	if (intty) {
    463        1.1       cgd 	    /*
    464        1.1       cgd 	     * The tty preread
    465        1.1       cgd 	     */
    466        1.1       cgd 	    preread();
    467        1.1       cgd 	    doagain();
    468        1.1       cgd 	    return;
    469        1.1       cgd 	}
    470        1.1       cgd     }
    471        1.1       cgd     if (status)
    472        1.1       cgd 	/* We ain't gonna loop no more, no more! */
    473        1.1       cgd 	toend();
    474        1.1       cgd }
    475        1.1       cgd 
    476        1.1       cgd static void
    477       1.21       wiz preread(void)
    478        1.1       cgd {
    479       1.25    kleink     sigset_t nsigset;
    480       1.10   mycroft 
    481        1.7   mycroft     whyles->w_end.type = I_SEEK;
    482       1.10   mycroft     if (setintr) {
    483       1.25    kleink 	sigemptyset(&nsigset);
    484       1.25    kleink 	(void) sigaddset(&nsigset, SIGINT);
    485       1.25    kleink 	(void) sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
    486       1.10   mycroft     }
    487        1.1       cgd 
    488        1.1       cgd     search(T_BREAK, 0, NULL);		/* read the expression in */
    489        1.1       cgd     if (setintr)
    490       1.25    kleink 	(void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
    491        1.7   mycroft     btell(&whyles->w_end);
    492        1.1       cgd }
    493        1.1       cgd 
    494        1.1       cgd void
    495        1.7   mycroft /*ARGSUSED*/
    496       1.21       wiz doend(Char **v, struct command *t)
    497        1.1       cgd {
    498       1.16   mycroft     if (!whyles)
    499        1.1       cgd 	stderror(ERR_NAME | ERR_NOTWHILE);
    500        1.7   mycroft     btell(&whyles->w_end);
    501        1.1       cgd     doagain();
    502        1.1       cgd }
    503        1.1       cgd 
    504        1.1       cgd void
    505        1.7   mycroft /*ARGSUSED*/
    506       1.21       wiz docontin(Char **v, struct command *t)
    507        1.1       cgd {
    508       1.16   mycroft     if (!whyles)
    509        1.1       cgd 	stderror(ERR_NAME | ERR_NOTWHILE);
    510        1.1       cgd     doagain();
    511        1.1       cgd }
    512        1.1       cgd 
    513        1.1       cgd static void
    514       1.21       wiz doagain(void)
    515        1.1       cgd {
    516        1.1       cgd     /* Repeating a while is simple */
    517        1.1       cgd     if (whyles->w_fename == 0) {
    518        1.7   mycroft 	bseek(&whyles->w_start);
    519        1.1       cgd 	return;
    520        1.1       cgd     }
    521        1.1       cgd     /*
    522        1.1       cgd      * The foreach variable list actually has a spurious word ")" at the end of
    523        1.1       cgd      * the w_fe list.  Thus we are at the of the list if one word beyond this
    524        1.1       cgd      * is 0.
    525        1.1       cgd      */
    526        1.1       cgd     if (!whyles->w_fe[1]) {
    527        1.7   mycroft 	dobreak(NULL, NULL);
    528        1.1       cgd 	return;
    529        1.1       cgd     }
    530        1.1       cgd     set(whyles->w_fename, Strsave(*whyles->w_fe++));
    531        1.7   mycroft     bseek(&whyles->w_start);
    532        1.1       cgd }
    533        1.1       cgd 
    534        1.1       cgd void
    535       1.21       wiz dorepeat(Char **v, struct command *kp)
    536        1.1       cgd {
    537       1.12       tls     int i;
    538       1.25    kleink     sigset_t nsigset;
    539        1.1       cgd 
    540        1.1       cgd     i = getn(v[1]);
    541       1.10   mycroft     if (setintr) {
    542       1.25    kleink 	sigemptyset(&nsigset);
    543       1.25    kleink 	(void)sigaddset(&nsigset, SIGINT);
    544       1.25    kleink 	(void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
    545       1.10   mycroft     }
    546        1.1       cgd     lshift(v, 2);
    547        1.1       cgd     while (i > 0) {
    548        1.1       cgd 	if (setintr)
    549       1.25    kleink 	    (void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
    550        1.1       cgd 	reexecute(kp);
    551        1.1       cgd 	--i;
    552        1.1       cgd     }
    553        1.1       cgd     donefds();
    554        1.1       cgd     if (setintr)
    555       1.25    kleink 	(void) sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
    556        1.1       cgd }
    557        1.1       cgd 
    558        1.1       cgd void
    559        1.7   mycroft /*ARGSUSED*/
    560       1.21       wiz doswbrk(Char **v, struct command *t)
    561        1.1       cgd {
    562        1.1       cgd     search(T_BRKSW, 0, NULL);
    563        1.1       cgd }
    564        1.1       cgd 
    565        1.1       cgd int
    566       1.21       wiz srchx(Char *cp)
    567        1.1       cgd {
    568       1.12       tls     struct srch *sp, *sp1, *sp2;
    569       1.12       tls     int i;
    570        1.1       cgd 
    571        1.1       cgd     /*
    572        1.1       cgd      * Binary search Sp1 is the beginning of the current search range. Sp2 is
    573        1.1       cgd      * one past the end.
    574        1.1       cgd      */
    575        1.1       cgd     for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2;) {
    576        1.1       cgd 	sp = sp1 + ((sp2 - sp1) >> 1);
    577        1.1       cgd 	if ((i = *cp - *sp->s_name) == 0 &&
    578        1.1       cgd 	    (i = Strcmp(cp, str2short(sp->s_name))) == 0)
    579        1.1       cgd 	    return sp->s_value;
    580        1.1       cgd 	if (i < 0)
    581        1.1       cgd 	    sp2 = sp;
    582        1.1       cgd 	else
    583        1.1       cgd 	    sp1 = sp + 1;
    584        1.1       cgd     }
    585        1.1       cgd     return (-1);
    586        1.1       cgd }
    587        1.1       cgd 
    588        1.1       cgd static Char Stype;
    589        1.1       cgd static Char *Sgoal;
    590        1.1       cgd 
    591        1.1       cgd /*VARARGS2*/
    592        1.7   mycroft static void
    593       1.21       wiz search(int type, int level, Char *goal)
    594        1.1       cgd {
    595       1.21       wiz     Char wordbuf[BUFSIZE];
    596       1.21       wiz     Char *aword, *cp;
    597       1.27  christos     struct whyle *wp;
    598       1.29  christos     int wlevel = 0;
    599        1.1       cgd 
    600       1.21       wiz     aword = wordbuf;
    601       1.40  christos     Stype = (Char)type;
    602        1.1       cgd     Sgoal = goal;
    603        1.7   mycroft     if (type == T_GOTO) {
    604        1.7   mycroft 	struct Ain a;
    605        1.7   mycroft 	a.type = F_SEEK;
    606        1.7   mycroft 	a.f_seek = 0;
    607        1.7   mycroft 	bseek(&a);
    608        1.7   mycroft     }
    609        1.1       cgd     do {
    610        1.7   mycroft 	if (intty && fseekp == feobp && aret == F_SEEK)
    611       1.21       wiz 	    (void)fprintf(cshout, "? "), (void)fflush(cshout);
    612        1.1       cgd 	aword[0] = 0;
    613       1.21       wiz 	(void)getword(aword);
    614        1.1       cgd 	switch (srchx(aword)) {
    615       1.21       wiz 	case T_CASE:
    616       1.21       wiz 	    if (type != T_SWITCH || level != 0)
    617       1.21       wiz 		break;
    618       1.21       wiz 	    (void) getword(aword);
    619       1.21       wiz 	    if (lastchr(aword) == ':')
    620       1.21       wiz 		aword[Strlen(aword) - 1] = 0;
    621       1.21       wiz 	    cp = strip(Dfix1(aword));
    622       1.21       wiz 	    if (Gmatch(goal, cp))
    623       1.21       wiz 		level = -1;
    624  1.40.26.1  pgoyette 	    free(cp);
    625       1.21       wiz 	    break;
    626       1.21       wiz 	case T_DEFAULT:
    627       1.21       wiz 	    if (type == T_SWITCH && level == 0)
    628       1.21       wiz 		level = -1;
    629       1.21       wiz 	    break;
    630        1.1       cgd 	case T_ELSE:
    631        1.1       cgd 	    if (level == 0 && type == T_IF)
    632        1.1       cgd 		return;
    633        1.1       cgd 	    break;
    634       1.21       wiz 	case T_END:
    635       1.27  christos 	    if (type == T_BRKSW) {
    636       1.29  christos 		if (wlevel == 0) {
    637       1.29  christos 		    wp = whyles;
    638       1.29  christos 		    if (wp) {
    639       1.27  christos 			whyles = wp->w_next;
    640       1.27  christos 			wpfree(wp);
    641       1.29  christos 		    }
    642       1.27  christos 		}
    643       1.27  christos 	    }
    644       1.21       wiz 	    if (type == T_BREAK)
    645       1.21       wiz 		level--;
    646       1.29  christos 	    wlevel--;
    647        1.1       cgd 	    break;
    648        1.1       cgd 	case T_ENDIF:
    649        1.1       cgd 	    if (type == T_IF || type == T_ELSE)
    650        1.1       cgd 		level--;
    651        1.1       cgd 	    break;
    652       1.21       wiz 	case T_ENDSW:
    653       1.21       wiz 	    if (type == T_SWITCH || type == T_BRKSW)
    654        1.1       cgd 		level--;
    655        1.1       cgd 	    break;
    656       1.21       wiz 	case T_IF:
    657       1.21       wiz 	    while (getword(aword))
    658       1.21       wiz 		continue;
    659       1.21       wiz 	    if ((type == T_IF || type == T_ELSE) &&
    660       1.21       wiz 		eq(aword, STRthen))
    661        1.1       cgd 		level++;
    662        1.1       cgd 	    break;
    663        1.1       cgd 	case T_LABEL:
    664        1.1       cgd 	    if (type == T_GOTO && getword(aword) && eq(aword, goal))
    665        1.1       cgd 		level = -1;
    666        1.1       cgd 	    break;
    667       1.21       wiz 	case T_SWITCH:
    668       1.21       wiz 	    if (type == T_SWITCH || type == T_BRKSW)
    669       1.21       wiz 		level++;
    670       1.21       wiz 	    break;
    671       1.21       wiz 	case T_FOREACH:
    672       1.21       wiz 	case T_WHILE:
    673       1.29  christos 	    wlevel++;
    674       1.21       wiz 	    if (type == T_BREAK)
    675       1.21       wiz 		level++;
    676       1.21       wiz 	    break;
    677        1.1       cgd 	default:
    678        1.1       cgd 	    if (type != T_GOTO && (type != T_SWITCH || level != 0))
    679        1.1       cgd 		break;
    680        1.1       cgd 	    if (lastchr(aword) != ':')
    681        1.1       cgd 		break;
    682        1.1       cgd 	    aword[Strlen(aword) - 1] = 0;
    683        1.7   mycroft 	    if ((type == T_GOTO && eq(aword, goal)) ||
    684        1.7   mycroft 		(type == T_SWITCH && eq(aword, STRdefault)))
    685        1.1       cgd 		level = -1;
    686        1.1       cgd 	    break;
    687        1.1       cgd 	}
    688        1.1       cgd 	(void) getword(NULL);
    689        1.1       cgd     } while (level >= 0);
    690        1.1       cgd }
    691        1.1       cgd 
    692       1.27  christos static void
    693       1.27  christos wpfree(struct whyle *wp)
    694       1.27  christos {
    695       1.27  christos     if (wp->w_fe0)
    696       1.27  christos 	blkfree(wp->w_fe0);
    697       1.27  christos     if (wp->w_fename)
    698  1.40.26.1  pgoyette 	free(wp->w_fename);
    699  1.40.26.1  pgoyette     free(wp);
    700       1.27  christos }
    701       1.27  christos 
    702        1.1       cgd static int
    703       1.21       wiz getword(Char *wp)
    704        1.1       cgd {
    705       1.21       wiz     int c, d, found, kwd;
    706       1.21       wiz     Char *owp;
    707        1.1       cgd 
    708        1.1       cgd     c = readc(1);
    709        1.1       cgd     d = 0;
    710       1.21       wiz     found = 0;
    711       1.21       wiz     kwd = 0;
    712       1.21       wiz     owp = wp;
    713        1.1       cgd     do {
    714        1.1       cgd 	while (c == ' ' || c == '\t')
    715        1.1       cgd 	    c = readc(1);
    716        1.1       cgd 	if (c == '#')
    717        1.1       cgd 	    do
    718        1.1       cgd 		c = readc(1);
    719        1.1       cgd 	    while (c >= 0 && c != '\n');
    720        1.1       cgd 	if (c < 0)
    721        1.1       cgd 	    goto past;
    722        1.1       cgd 	if (c == '\n') {
    723        1.1       cgd 	    if (wp)
    724        1.1       cgd 		break;
    725        1.1       cgd 	    return (0);
    726        1.1       cgd 	}
    727        1.1       cgd 	unreadc(c);
    728        1.1       cgd 	found = 1;
    729        1.1       cgd 	do {
    730        1.1       cgd 	    c = readc(1);
    731        1.1       cgd 	    if (c == '\\' && (c = readc(1)) == '\n')
    732        1.1       cgd 		c = ' ';
    733       1.17   thorpej 	    if (c == '\'' || c == '"') {
    734        1.1       cgd 		if (d == 0)
    735        1.1       cgd 		    d = c;
    736        1.1       cgd 		else if (d == c)
    737        1.1       cgd 		    d = 0;
    738       1.17   thorpej 	    }
    739        1.1       cgd 	    if (c < 0)
    740        1.1       cgd 		goto past;
    741        1.1       cgd 	    if (wp) {
    742       1.40  christos 		*wp++ = (Char)c;
    743        1.1       cgd 		*wp = 0;	/* end the string b4 test */
    744        1.1       cgd 	    }
    745        1.7   mycroft 	} while ((d || (!(kwd = keyword(owp)) && c != ' '
    746        1.7   mycroft 		  && c != '\t')) && c != '\n');
    747        1.1       cgd     } while (wp == 0);
    748        1.1       cgd 
    749        1.1       cgd     /*
    750        1.1       cgd      * if we have read a keyword ( "if", "switch" or "while" ) then we do not
    751        1.1       cgd      * need to unreadc the look-ahead char
    752        1.1       cgd      */
    753        1.1       cgd     if (!kwd) {
    754        1.1       cgd 	unreadc(c);
    755        1.1       cgd 	if (found)
    756        1.1       cgd 	    *--wp = 0;
    757        1.1       cgd     }
    758        1.1       cgd 
    759        1.1       cgd     return (found);
    760        1.1       cgd 
    761        1.1       cgd past:
    762        1.1       cgd     switch (Stype) {
    763       1.21       wiz     case T_BREAK:
    764       1.21       wiz 	stderror(ERR_NAME | ERR_NOTFOUND, "end");
    765       1.14   mycroft 	/* NOTREACHED */
    766        1.1       cgd     case T_ELSE:
    767        1.1       cgd 	stderror(ERR_NAME | ERR_NOTFOUND, "endif");
    768       1.14   mycroft 	/* NOTREACHED */
    769       1.21       wiz     case T_GOTO:
    770       1.21       wiz 	setname(vis_str(Sgoal));
    771       1.21       wiz 	stderror(ERR_NAME | ERR_NOTFOUND, "label");
    772       1.21       wiz 	/* NOTREACHED */
    773       1.21       wiz     case T_IF:
    774       1.21       wiz 	stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
    775       1.21       wiz 	/* NOTREACHED */
    776        1.1       cgd     case T_BRKSW:
    777        1.1       cgd     case T_SWITCH:
    778        1.1       cgd 	stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
    779       1.14   mycroft 	/* NOTREACHED */
    780        1.1       cgd     }
    781        1.1       cgd     return (0);
    782        1.1       cgd }
    783        1.1       cgd 
    784        1.1       cgd /*
    785        1.1       cgd  * keyword(wp) determines if wp is one of the built-n functions if,
    786        1.1       cgd  * switch or while. It seems that when an if statement looks like
    787        1.1       cgd  * "if(" then getword above sucks in the '(' and so the search routine
    788        1.1       cgd  * never finds what it is scanning for. Rather than rewrite doword, I hack
    789        1.1       cgd  * in a test to see if the string forms a keyword. Then doword stops
    790        1.1       cgd  * and returns the word "if" -strike
    791        1.1       cgd  */
    792        1.1       cgd 
    793        1.1       cgd static int
    794       1.21       wiz keyword(Char *wp)
    795        1.1       cgd {
    796       1.21       wiz     static Char STRswitch[] = {'s', 'w', 'i', 't', 'c', 'h', '\0'};
    797       1.21       wiz     static Char STRwhile[] = {'w', 'h', 'i', 'l', 'e', '\0'};
    798        1.1       cgd     static Char STRif[] = {'i', 'f', '\0'};
    799        1.1       cgd 
    800        1.1       cgd     if (!wp)
    801        1.1       cgd 	return (0);
    802        1.1       cgd 
    803        1.1       cgd     if ((Strcmp(wp, STRif) == 0) || (Strcmp(wp, STRwhile) == 0)
    804        1.1       cgd 	|| (Strcmp(wp, STRswitch) == 0))
    805        1.1       cgd 	return (1);
    806        1.1       cgd 
    807        1.1       cgd     return (0);
    808        1.1       cgd }
    809        1.1       cgd 
    810        1.1       cgd static void
    811       1.21       wiz toend(void)
    812        1.1       cgd {
    813        1.7   mycroft     if (whyles->w_end.type == F_SEEK && whyles->w_end.f_seek == 0) {
    814        1.1       cgd 	search(T_BREAK, 0, NULL);
    815        1.7   mycroft 	btell(&whyles->w_end);
    816        1.7   mycroft 	whyles->w_end.f_seek--;
    817        1.1       cgd     }
    818        1.1       cgd     else
    819        1.7   mycroft 	bseek(&whyles->w_end);
    820        1.1       cgd     wfree();
    821        1.1       cgd }
    822        1.1       cgd 
    823        1.1       cgd void
    824       1.21       wiz wfree(void)
    825        1.1       cgd {
    826       1.21       wiz     struct Ain o;
    827        1.7   mycroft     struct whyle *nwp;
    828        1.7   mycroft 
    829        1.7   mycroft     btell(&o);
    830        1.1       cgd 
    831        1.7   mycroft     for (; whyles; whyles = nwp) {
    832       1.12       tls 	struct whyle *wp = whyles;
    833        1.7   mycroft 	nwp = wp->w_next;
    834        1.7   mycroft 
    835        1.7   mycroft 	/*
    836        1.7   mycroft 	 * We free loops that have different seek types.
    837        1.7   mycroft 	 */
    838        1.7   mycroft 	if (wp->w_end.type != I_SEEK && wp->w_start.type == wp->w_end.type &&
    839        1.7   mycroft 	    wp->w_start.type == o.type) {
    840        1.7   mycroft 	    if (wp->w_end.type == F_SEEK) {
    841        1.7   mycroft 		if (o.f_seek >= wp->w_start.f_seek &&
    842        1.7   mycroft 		    (wp->w_end.f_seek == 0 || o.f_seek < wp->w_end.f_seek))
    843        1.7   mycroft 		    break;
    844        1.7   mycroft 	    }
    845        1.7   mycroft 	    else {
    846        1.7   mycroft 		if (o.a_seek >= wp->w_start.a_seek &&
    847        1.7   mycroft 		    (wp->w_end.a_seek == 0 || o.a_seek < wp->w_end.a_seek))
    848        1.7   mycroft 		    break;
    849        1.7   mycroft 	    }
    850        1.7   mycroft 	}
    851        1.1       cgd 
    852       1.27  christos 	wpfree(wp);
    853        1.1       cgd     }
    854        1.1       cgd }
    855        1.1       cgd 
    856        1.1       cgd void
    857        1.7   mycroft /*ARGSUSED*/
    858       1.21       wiz doecho(Char **v, struct command *t)
    859        1.1       cgd {
    860        1.1       cgd     xecho(' ', v);
    861        1.1       cgd }
    862        1.1       cgd 
    863        1.1       cgd void
    864        1.7   mycroft /*ARGSUSED*/
    865       1.21       wiz doglob(Char **v, struct command *t)
    866        1.1       cgd {
    867        1.1       cgd     xecho(0, v);
    868       1.21       wiz     (void)fflush(cshout);
    869        1.1       cgd }
    870        1.1       cgd 
    871        1.1       cgd static void
    872       1.21       wiz xecho(int sep, Char **v)
    873        1.1       cgd {
    874       1.12       tls     Char *cp;
    875       1.25    kleink     sigset_t nsigset;
    876       1.21       wiz     int nonl;
    877        1.1       cgd 
    878       1.21       wiz     nonl = 0;
    879       1.10   mycroft     if (setintr) {
    880       1.25    kleink 	sigemptyset(&nsigset);
    881       1.25    kleink 	(void)sigaddset(&nsigset, SIGINT);
    882       1.25    kleink 	(void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
    883       1.10   mycroft     }
    884        1.1       cgd     v++;
    885        1.1       cgd     if (*v == 0)
    886       1.20  christos 	goto done;
    887        1.1       cgd     gflag = 0, tglob(v);
    888        1.1       cgd     if (gflag) {
    889        1.1       cgd 	v = globall(v);
    890       1.16   mycroft 	if (v == 0)
    891        1.1       cgd 	    stderror(ERR_NAME | ERR_NOMATCH);
    892        1.1       cgd     }
    893        1.1       cgd     else {
    894        1.1       cgd 	v = gargv = saveblk(v);
    895        1.1       cgd 	trim(v);
    896        1.1       cgd     }
    897        1.1       cgd     if (sep == ' ' && *v && eq(*v, STRmn))
    898        1.1       cgd 	nonl++, v++;
    899        1.7   mycroft     while ((cp = *v++) != NULL) {
    900       1.12       tls 	int c;
    901        1.1       cgd 
    902        1.7   mycroft 	while ((c = *cp++) != '\0')
    903       1.21       wiz 	    (void)vis_fputc(c | QUOTE, cshout);
    904        1.1       cgd 
    905        1.1       cgd 	if (*v)
    906       1.21       wiz 	    (void)vis_fputc(sep | QUOTE, cshout);
    907        1.1       cgd     }
    908       1.20  christos done:
    909        1.1       cgd     if (sep && nonl == 0)
    910       1.21       wiz 	(void)fputc('\n', cshout);
    911        1.1       cgd     else
    912       1.21       wiz 	(void)fflush(cshout);
    913        1.1       cgd     if (setintr)
    914       1.25    kleink 	(void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
    915        1.1       cgd     if (gargv)
    916        1.1       cgd 	blkfree(gargv), gargv = 0;
    917        1.1       cgd }
    918        1.1       cgd 
    919        1.1       cgd void
    920        1.7   mycroft /*ARGSUSED*/
    921       1.21       wiz dosetenv(Char **v, struct command *t)
    922        1.1       cgd {
    923       1.21       wiz     Char *lp, *vp;
    924       1.25    kleink     sigset_t nsigset;
    925        1.1       cgd 
    926        1.1       cgd     v++;
    927        1.1       cgd     if ((vp = *v++) == 0) {
    928       1.12       tls 	Char **ep;
    929        1.1       cgd 
    930       1.10   mycroft 	if (setintr) {
    931       1.25    kleink 	    sigemptyset(&nsigset);
    932       1.25    kleink 	    (void)sigaddset(&nsigset, SIGINT);
    933       1.25    kleink 	    (void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
    934       1.10   mycroft 	}
    935        1.1       cgd 	for (ep = STR_environ; *ep; ep++)
    936       1.21       wiz 	    (void)fprintf(cshout, "%s\n", vis_str(*ep));
    937        1.1       cgd 	return;
    938        1.1       cgd     }
    939        1.1       cgd     if ((lp = *v++) == 0)
    940        1.1       cgd 	lp = STRNULL;
    941        1.7   mycroft     Setenv(vp, lp = globone(lp, G_APPEND));
    942        1.1       cgd     if (eq(vp, STRPATH)) {
    943        1.1       cgd 	importpath(lp);
    944        1.7   mycroft 	dohash(NULL, NULL);
    945        1.1       cgd     }
    946        1.1       cgd     else if (eq(vp, STRLANG) || eq(vp, STRLC_CTYPE)) {
    947        1.1       cgd #ifdef NLS
    948       1.21       wiz 	int k;
    949        1.1       cgd 
    950       1.21       wiz 	(void)setlocale(LC_ALL, "");
    951        1.7   mycroft 	for (k = 0200; k <= 0377 && !Isprint(k); k++)
    952        1.7   mycroft 		continue;
    953        1.1       cgd 	AsciiOnly = k > 0377;
    954        1.1       cgd #else
    955        1.1       cgd 	AsciiOnly = 0;
    956        1.1       cgd #endif				/* NLS */
    957        1.1       cgd     }
    958  1.40.26.1  pgoyette     free(lp);
    959        1.1       cgd }
    960        1.1       cgd 
    961        1.1       cgd void
    962        1.7   mycroft /*ARGSUSED*/
    963       1.21       wiz dounsetenv(Char **v, struct command *t)
    964        1.1       cgd {
    965        1.1       cgd     static Char *name = NULL;
    966       1.21       wiz     Char **ep, *p, *n;
    967       1.21       wiz     int i, maxi;
    968        1.1       cgd 
    969        1.1       cgd     if (name)
    970  1.40.26.1  pgoyette 	free(name);
    971        1.1       cgd     /*
    972        1.1       cgd      * Find the longest environment variable
    973        1.1       cgd      */
    974        1.1       cgd     for (maxi = 0, ep = STR_environ; *ep; ep++) {
    975        1.7   mycroft 	for (i = 0, p = *ep; *p && *p != '='; p++, i++)
    976        1.7   mycroft 	    continue;
    977        1.1       cgd 	if (i > maxi)
    978        1.1       cgd 	    maxi = i;
    979        1.1       cgd     }
    980        1.1       cgd 
    981       1.40  christos     name = xmalloc((size_t)(maxi + 1) * sizeof(Char));
    982        1.1       cgd 
    983        1.1       cgd     while (++v && *v)
    984        1.1       cgd 	for (maxi = 1; maxi;)
    985        1.1       cgd 	    for (maxi = 0, ep = STR_environ; *ep; ep++) {
    986        1.7   mycroft 		for (n = name, p = *ep; *p && *p != '='; *n++ = *p++)
    987        1.7   mycroft 		    continue;
    988        1.1       cgd 		*n = '\0';
    989        1.1       cgd 		if (!Gmatch(name, *v))
    990        1.1       cgd 		    continue;
    991        1.1       cgd 		maxi = 1;
    992        1.1       cgd 		if (eq(name, STRLANG) || eq(name, STRLC_CTYPE)) {
    993        1.1       cgd #ifdef NLS
    994        1.1       cgd 		    int     k;
    995        1.1       cgd 
    996        1.1       cgd 		    (void) setlocale(LC_ALL, "");
    997        1.7   mycroft 		    for (k = 0200; k <= 0377 && !Isprint(k); k++)
    998        1.7   mycroft 			continue;
    999        1.1       cgd 		    AsciiOnly = k > 0377;
   1000        1.1       cgd #else
   1001        1.1       cgd 		    AsciiOnly = getenv("LANG") == NULL &&
   1002        1.1       cgd 			getenv("LC_CTYPE") == NULL;
   1003        1.1       cgd #endif				/* NLS */
   1004        1.1       cgd 		}
   1005        1.1       cgd 		/*
   1006        1.1       cgd 		 * Delete name, and start again cause the environment changes
   1007        1.1       cgd 		 */
   1008        1.1       cgd 		Unsetenv(name);
   1009        1.1       cgd 		break;
   1010        1.1       cgd 	    }
   1011  1.40.26.1  pgoyette     free(name);
   1012        1.7   mycroft     name = NULL;
   1013        1.1       cgd }
   1014        1.1       cgd 
   1015        1.1       cgd void
   1016       1.21       wiz Setenv(Char *name, Char *val)
   1017        1.1       cgd {
   1018       1.21       wiz     Char *blk[2], *cp, *dp, **ep, **oep;
   1019        1.1       cgd 
   1020       1.21       wiz     ep = STR_environ;
   1021       1.21       wiz     oep = ep;
   1022        1.1       cgd 
   1023        1.1       cgd     for (; *ep; ep++) {
   1024        1.1       cgd 	for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
   1025        1.1       cgd 	    continue;
   1026        1.1       cgd 	if (*cp != 0 || *dp != '=')
   1027        1.1       cgd 	    continue;
   1028        1.1       cgd 	cp = Strspl(STRequal, val);
   1029  1.40.26.1  pgoyette 	free(* ep);
   1030        1.1       cgd 	*ep = strip(Strspl(name, cp));
   1031  1.40.26.1  pgoyette 	free(cp);
   1032       1.21       wiz 	blkfree((Char **)environ);
   1033        1.1       cgd 	environ = short2blk(STR_environ);
   1034        1.1       cgd 	return;
   1035        1.1       cgd     }
   1036        1.1       cgd     cp = Strspl(name, STRequal);
   1037        1.1       cgd     blk[0] = strip(Strspl(cp, val));
   1038  1.40.26.1  pgoyette     free(cp);
   1039        1.1       cgd     blk[1] = 0;
   1040        1.1       cgd     STR_environ = blkspl(STR_environ, blk);
   1041       1.21       wiz     blkfree((Char **)environ);
   1042        1.1       cgd     environ = short2blk(STR_environ);
   1043  1.40.26.1  pgoyette     free(oep);
   1044        1.1       cgd }
   1045        1.1       cgd 
   1046        1.1       cgd static void
   1047       1.21       wiz Unsetenv(Char *name)
   1048        1.1       cgd {
   1049       1.21       wiz     Char *cp, *dp, **ep, **oep;
   1050       1.21       wiz 
   1051       1.21       wiz     ep = STR_environ;
   1052       1.21       wiz     oep = ep;
   1053        1.1       cgd 
   1054        1.1       cgd     for (; *ep; ep++) {
   1055        1.1       cgd 	for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
   1056        1.1       cgd 	    continue;
   1057        1.1       cgd 	if (*cp != 0 || *dp != '=')
   1058        1.1       cgd 	    continue;
   1059        1.1       cgd 	cp = *ep;
   1060        1.1       cgd 	*ep = 0;
   1061        1.1       cgd 	STR_environ = blkspl(STR_environ, ep + 1);
   1062        1.1       cgd 	environ = short2blk(STR_environ);
   1063        1.1       cgd 	*ep = cp;
   1064  1.40.26.1  pgoyette 	free(cp);
   1065  1.40.26.1  pgoyette 	free(oep);
   1066        1.1       cgd 	return;
   1067        1.1       cgd     }
   1068        1.1       cgd }
   1069        1.1       cgd 
   1070        1.1       cgd void
   1071        1.7   mycroft /*ARGSUSED*/
   1072       1.21       wiz doumask(Char **v, struct command *t)
   1073        1.1       cgd {
   1074       1.21       wiz     Char *cp;
   1075       1.40  christos     mode_t i;
   1076        1.1       cgd 
   1077       1.21       wiz     cp = v[1];
   1078        1.1       cgd     if (cp == 0) {
   1079        1.1       cgd 	i = umask(0);
   1080       1.21       wiz 	(void)umask(i);
   1081       1.21       wiz 	(void)fprintf(cshout, "%o\n", i);
   1082        1.1       cgd 	return;
   1083        1.1       cgd     }
   1084        1.1       cgd     i = 0;
   1085        1.1       cgd     while (Isdigit(*cp) && *cp != '8' && *cp != '9')
   1086       1.40  christos 	i = i * 8 + (mode_t)(*cp++ - '0');
   1087       1.40  christos     if (*cp || i > 0777)
   1088        1.1       cgd 	stderror(ERR_NAME | ERR_MASK);
   1089       1.21       wiz     (void)umask(i);
   1090        1.1       cgd }
   1091        1.1       cgd 
   1092       1.24       wiz typedef rlim_t RLIM_TYPE;
   1093        1.1       cgd 
   1094       1.14   mycroft static const struct limits {
   1095        1.1       cgd     int     limconst;
   1096       1.14   mycroft     const   char *limname;
   1097        1.1       cgd     int     limdiv;
   1098       1.14   mycroft     const   char *limscale;
   1099        1.1       cgd }       limits[] = {
   1100        1.7   mycroft     { RLIMIT_CPU,	"cputime",	1,	"seconds" },
   1101        1.7   mycroft     { RLIMIT_FSIZE,	"filesize",	1024,	"kbytes" },
   1102        1.7   mycroft     { RLIMIT_DATA,	"datasize",	1024,	"kbytes" },
   1103        1.7   mycroft     { RLIMIT_STACK,	"stacksize",	1024,	"kbytes" },
   1104        1.7   mycroft     { RLIMIT_CORE,	"coredumpsize", 1024,	"kbytes" },
   1105        1.7   mycroft     { RLIMIT_RSS,	"memoryuse",	1024,	"kbytes" },
   1106        1.7   mycroft     { RLIMIT_MEMLOCK,	"memorylocked",	1024,	"kbytes" },
   1107        1.7   mycroft     { RLIMIT_NPROC,	"maxproc",	1,	"" },
   1108       1.39  christos     { RLIMIT_NTHR,	"maxthread",	1,	"" },
   1109        1.7   mycroft     { RLIMIT_NOFILE,	"openfiles",	1,	"" },
   1110       1.28  christos     { RLIMIT_SBSIZE,	"sbsize",	1,	"bytes" },
   1111       1.37       mrg     { RLIMIT_AS,	"vmemoryuse",	1024,	"kbytes" },
   1112        1.7   mycroft     { -1,		NULL,		0,	NULL }
   1113        1.1       cgd };
   1114        1.1       cgd 
   1115       1.21       wiz static const struct limits *findlim(Char *);
   1116       1.21       wiz static RLIM_TYPE getval(const struct limits *, Char **);
   1117       1.30  christos static void limtail(Char *, const char *);
   1118       1.21       wiz static void plim(const struct limits *, Char);
   1119       1.21       wiz static int setlim(const struct limits *, Char, RLIM_TYPE);
   1120        1.1       cgd 
   1121       1.14   mycroft static const struct limits *
   1122       1.21       wiz findlim(Char *cp)
   1123        1.1       cgd {
   1124       1.14   mycroft     const struct limits *lp, *res;
   1125        1.1       cgd 
   1126       1.38    plunky     res = NULL;
   1127        1.1       cgd     for (lp = limits; lp->limconst >= 0; lp++)
   1128        1.1       cgd 	if (prefix(cp, str2short(lp->limname))) {
   1129       1.16   mycroft 	    if (res)
   1130        1.1       cgd 		stderror(ERR_NAME | ERR_AMBIG);
   1131        1.1       cgd 	    res = lp;
   1132        1.1       cgd 	}
   1133        1.1       cgd     if (res)
   1134        1.1       cgd 	return (res);
   1135        1.1       cgd     stderror(ERR_NAME | ERR_LIMIT);
   1136        1.1       cgd     /* NOTREACHED */
   1137        1.1       cgd }
   1138        1.1       cgd 
   1139        1.1       cgd void
   1140        1.7   mycroft /*ARGSUSED*/
   1141       1.21       wiz dolimit(Char **v, struct command *t)
   1142        1.1       cgd {
   1143       1.14   mycroft     const struct limits *lp;
   1144       1.12       tls     RLIM_TYPE limit;
   1145       1.21       wiz     char hard;
   1146        1.1       cgd 
   1147       1.21       wiz     hard = 0;
   1148        1.1       cgd     v++;
   1149        1.1       cgd     if (*v && eq(*v, STRmh)) {
   1150        1.1       cgd 	hard = 1;
   1151        1.1       cgd 	v++;
   1152        1.1       cgd     }
   1153        1.1       cgd     if (*v == 0) {
   1154        1.1       cgd 	for (lp = limits; lp->limconst >= 0; lp++)
   1155        1.1       cgd 	    plim(lp, hard);
   1156        1.1       cgd 	return;
   1157        1.1       cgd     }
   1158        1.1       cgd     lp = findlim(v[0]);
   1159        1.1       cgd     if (v[1] == 0) {
   1160        1.1       cgd 	plim(lp, hard);
   1161        1.1       cgd 	return;
   1162        1.1       cgd     }
   1163        1.1       cgd     limit = getval(lp, v + 1);
   1164       1.16   mycroft     if (setlim(lp, hard, limit) < 0)
   1165        1.1       cgd 	stderror(ERR_SILENT);
   1166        1.1       cgd }
   1167        1.1       cgd 
   1168        1.1       cgd static  RLIM_TYPE
   1169       1.21       wiz getval(const struct limits *lp, Char **v)
   1170        1.1       cgd {
   1171       1.21       wiz     Char *cp;
   1172       1.40  christos     double d;
   1173        1.1       cgd 
   1174       1.21       wiz     cp = *v++;
   1175       1.40  christos     d = atof(short2str(cp));
   1176        1.1       cgd 
   1177        1.1       cgd     while (Isdigit(*cp) || *cp == '.' || *cp == 'e' || *cp == 'E')
   1178        1.1       cgd 	cp++;
   1179        1.1       cgd     if (*cp == 0) {
   1180        1.1       cgd 	if (*v == 0)
   1181       1.40  christos 	    return ((RLIM_TYPE)((d + 0.5) * lp->limdiv));
   1182        1.1       cgd 	cp = *v;
   1183        1.1       cgd     }
   1184        1.1       cgd     switch (*cp) {
   1185        1.1       cgd     case ':':
   1186        1.1       cgd 	if (lp->limconst != RLIMIT_CPU)
   1187        1.1       cgd 	    goto badscal;
   1188       1.40  christos 	return ((RLIM_TYPE)(d * 60.0 + atof(short2str(cp + 1))));
   1189       1.21       wiz     case 'M':
   1190       1.21       wiz 	if (lp->limconst == RLIMIT_CPU)
   1191       1.21       wiz 	    goto badscal;
   1192       1.21       wiz 	*cp = 'm';
   1193       1.21       wiz 	limtail(cp, "megabytes");
   1194       1.40  christos 	d *= 1024.0 * 1024.0;
   1195       1.21       wiz 	break;
   1196        1.1       cgd     case 'h':
   1197        1.1       cgd 	if (lp->limconst != RLIMIT_CPU)
   1198        1.1       cgd 	    goto badscal;
   1199        1.1       cgd 	limtail(cp, "hours");
   1200       1.40  christos 	d *= 3600.0;
   1201        1.1       cgd 	break;
   1202       1.21       wiz     case 'k':
   1203       1.21       wiz 	if (lp->limconst == RLIMIT_CPU)
   1204       1.21       wiz 	    goto badscal;
   1205       1.21       wiz 	limtail(cp, "kbytes");
   1206       1.40  christos 	d *= 1024.0;
   1207       1.21       wiz 	break;
   1208        1.1       cgd     case 'm':
   1209        1.1       cgd 	if (lp->limconst == RLIMIT_CPU) {
   1210        1.1       cgd 	    limtail(cp, "minutes");
   1211       1.40  christos 	    d *= 60.0;
   1212        1.1       cgd 	    break;
   1213        1.1       cgd 	}
   1214        1.1       cgd 	*cp = 'm';
   1215        1.1       cgd 	limtail(cp, "megabytes");
   1216       1.40  christos 	d *= 1024.0 * 1024.0;
   1217        1.1       cgd 	break;
   1218        1.1       cgd     case 's':
   1219        1.1       cgd 	if (lp->limconst != RLIMIT_CPU)
   1220        1.1       cgd 	    goto badscal;
   1221        1.1       cgd 	limtail(cp, "seconds");
   1222        1.1       cgd 	break;
   1223        1.1       cgd     case 'u':
   1224        1.1       cgd 	limtail(cp, "unlimited");
   1225        1.1       cgd 	return (RLIM_INFINITY);
   1226        1.1       cgd     default:
   1227       1.14   mycroft     badscal:
   1228        1.1       cgd 	stderror(ERR_NAME | ERR_SCALEF);
   1229       1.14   mycroft 	/* NOTREACHED */
   1230        1.1       cgd     }
   1231       1.40  christos     d += 0.5;
   1232       1.40  christos     if (d > (double) RLIM_INFINITY)
   1233        1.7   mycroft 	return RLIM_INFINITY;
   1234        1.7   mycroft     else
   1235       1.40  christos 	return ((RLIM_TYPE)d);
   1236        1.1       cgd }
   1237        1.1       cgd 
   1238        1.1       cgd static void
   1239       1.30  christos limtail(Char *cp, const char *str)
   1240        1.1       cgd {
   1241        1.1       cgd     while (*cp && *cp == *str)
   1242        1.1       cgd 	cp++, str++;
   1243       1.16   mycroft     if (*cp)
   1244        1.1       cgd 	stderror(ERR_BADSCALE, str);
   1245        1.1       cgd }
   1246        1.1       cgd 
   1247        1.1       cgd 
   1248        1.1       cgd /*ARGSUSED*/
   1249        1.1       cgd static void
   1250       1.21       wiz plim(const struct limits *lp, Char hard)
   1251        1.1       cgd {
   1252        1.1       cgd     struct rlimit rlim;
   1253        1.1       cgd     RLIM_TYPE limit;
   1254        1.1       cgd 
   1255       1.28  christos     (void)fprintf(cshout, "%-13.13s", lp->limname);
   1256        1.1       cgd 
   1257       1.21       wiz     (void)getrlimit(lp->limconst, &rlim);
   1258        1.1       cgd     limit = hard ? rlim.rlim_max : rlim.rlim_cur;
   1259        1.1       cgd 
   1260        1.1       cgd     if (limit == RLIM_INFINITY)
   1261       1.21       wiz 	(void)fprintf(cshout, "unlimited");
   1262        1.1       cgd     else if (lp->limconst == RLIMIT_CPU)
   1263        1.1       cgd 	psecs((long) limit);
   1264        1.1       cgd     else
   1265       1.40  christos 	(void)fprintf(cshout, "%jd %s",
   1266       1.40  christos 	    (intmax_t) (limit / (RLIM_TYPE)lp->limdiv), lp->limscale);
   1267       1.21       wiz     (void)fputc('\n', cshout);
   1268        1.1       cgd }
   1269        1.1       cgd 
   1270        1.1       cgd void
   1271        1.7   mycroft /*ARGSUSED*/
   1272       1.21       wiz dounlimit(Char **v, struct command *t)
   1273        1.1       cgd {
   1274       1.14   mycroft     const struct limits *lp;
   1275       1.21       wiz     int lerr;
   1276       1.21       wiz     Char hard;
   1277        1.1       cgd 
   1278       1.21       wiz     lerr = 0;
   1279       1.21       wiz     hard = 0;
   1280        1.1       cgd     v++;
   1281        1.1       cgd     if (*v && eq(*v, STRmh)) {
   1282        1.1       cgd 	hard = 1;
   1283        1.1       cgd 	v++;
   1284        1.1       cgd     }
   1285        1.1       cgd     if (*v == 0) {
   1286        1.1       cgd 	for (lp = limits; lp->limconst >= 0; lp++)
   1287       1.21       wiz 	    if (setlim(lp, hard, (RLIM_TYPE)RLIM_INFINITY) < 0)
   1288        1.1       cgd 		lerr++;
   1289       1.16   mycroft 	if (lerr)
   1290        1.1       cgd 	    stderror(ERR_SILENT);
   1291        1.1       cgd 	return;
   1292        1.1       cgd     }
   1293        1.1       cgd     while (*v) {
   1294        1.1       cgd 	lp = findlim(*v++);
   1295       1.21       wiz 	if (setlim(lp, hard, (RLIM_TYPE)RLIM_INFINITY) < 0)
   1296        1.1       cgd 	    stderror(ERR_SILENT);
   1297        1.1       cgd     }
   1298        1.1       cgd }
   1299        1.1       cgd 
   1300        1.1       cgd static int
   1301       1.21       wiz setlim(const struct limits *lp, Char hard, RLIM_TYPE limit)
   1302        1.1       cgd {
   1303        1.1       cgd     struct rlimit rlim;
   1304        1.1       cgd 
   1305       1.21       wiz     (void)getrlimit(lp->limconst, &rlim);
   1306        1.1       cgd 
   1307        1.1       cgd     if (hard)
   1308        1.1       cgd 	rlim.rlim_max = limit;
   1309        1.1       cgd     else if (limit == RLIM_INFINITY && geteuid() != 0)
   1310        1.1       cgd 	rlim.rlim_cur = rlim.rlim_max;
   1311        1.1       cgd     else
   1312        1.1       cgd 	rlim.rlim_cur = limit;
   1313        1.1       cgd 
   1314       1.22  christos     if (rlim.rlim_max < rlim.rlim_cur)
   1315       1.22  christos 	rlim.rlim_max = rlim.rlim_cur;
   1316       1.22  christos 
   1317        1.1       cgd     if (setrlimit(lp->limconst, &rlim) < 0) {
   1318       1.22  christos 	(void)fprintf(csherr, "%s: %s: Can't %s%s limit (%s)\n", bname,
   1319       1.22  christos 	    lp->limname, limit == RLIM_INFINITY ? "remove" : "set",
   1320       1.22  christos 	    hard ? " hard" : "", strerror(errno));
   1321        1.1       cgd 	return (-1);
   1322        1.1       cgd     }
   1323        1.1       cgd     return (0);
   1324        1.1       cgd }
   1325        1.1       cgd 
   1326        1.1       cgd void
   1327        1.7   mycroft /*ARGSUSED*/
   1328       1.21       wiz dosuspend(Char **v, struct command *t)
   1329        1.1       cgd {
   1330        1.1       cgd     int     ctpgrp;
   1331       1.21       wiz     void    (*old)(int);
   1332        1.1       cgd 
   1333       1.16   mycroft     if (loginsh)
   1334        1.1       cgd 	stderror(ERR_SUSPLOG);
   1335        1.1       cgd     untty();
   1336        1.1       cgd 
   1337        1.1       cgd     old = signal(SIGTSTP, SIG_DFL);
   1338       1.21       wiz     (void)kill(0, SIGTSTP);
   1339        1.1       cgd     /* the shell stops here */
   1340       1.21       wiz     (void)signal(SIGTSTP, old);
   1341        1.1       cgd 
   1342        1.1       cgd     if (tpgrp != -1) {
   1343       1.11  christos retry:
   1344        1.1       cgd 	ctpgrp = tcgetpgrp(FSHTTY);
   1345       1.11  christos 	if  (ctpgrp != opgrp) {
   1346        1.1       cgd 	    old = signal(SIGTTIN, SIG_DFL);
   1347       1.21       wiz 	    (void)kill(0, SIGTTIN);
   1348       1.21       wiz 	    (void)signal(SIGTTIN, old);
   1349       1.11  christos 	    goto retry;
   1350        1.1       cgd 	}
   1351       1.21       wiz 	(void)setpgid(0, shpgrp);
   1352       1.21       wiz 	(void)tcsetpgrp(FSHTTY, shpgrp);
   1353        1.1       cgd     }
   1354        1.1       cgd }
   1355        1.1       cgd 
   1356        1.1       cgd /* This is the dreaded EVAL built-in.
   1357        1.1       cgd  *   If you don't fiddle with file descriptors, and reset didfds,
   1358        1.1       cgd  *   this command will either ignore redirection inside or outside
   1359       1.32   msaitoh  *   its arguments, e.g. eval "date >x"  vs.  eval "date" >x
   1360        1.1       cgd  *   The stuff here seems to work, but I did it by trial and error rather
   1361        1.1       cgd  *   than really knowing what was going on.  If tpgrp is zero, we are
   1362        1.1       cgd  *   probably a background eval, e.g. "eval date &", and we want to
   1363        1.1       cgd  *   make sure that any processes we start stay in our pgrp.
   1364        1.1       cgd  *   This is also the case for "time eval date" -- stay in same pgrp.
   1365        1.1       cgd  *   Otherwise, under stty tostop, processes will stop in the wrong
   1366        1.1       cgd  *   pgrp, with no way for the shell to get them going again.  -IAN!
   1367        1.1       cgd  */
   1368        1.7   mycroft static Char **gv = NULL;
   1369       1.21       wiz 
   1370        1.1       cgd void
   1371        1.7   mycroft /*ARGSUSED*/
   1372       1.21       wiz doeval(Char **v, struct command *t)
   1373       1.21       wiz {
   1374        1.1       cgd     jmp_buf osetexit;
   1375       1.21       wiz     Char *oevalp, **oevalvec, **savegv;
   1376       1.21       wiz     int my_reenter, odidfds, oSHERR, oSHIN, oSHOUT, saveERR, saveIN, saveOUT;
   1377        1.7   mycroft 
   1378       1.21       wiz     savegv = gv;
   1379        1.7   mycroft     UNREGISTER(v);
   1380        1.1       cgd 
   1381        1.1       cgd     oevalvec = evalvec;
   1382        1.1       cgd     oevalp = evalp;
   1383        1.1       cgd     odidfds = didfds;
   1384        1.1       cgd     oSHIN = SHIN;
   1385        1.1       cgd     oSHOUT = SHOUT;
   1386        1.7   mycroft     oSHERR = SHERR;
   1387        1.1       cgd 
   1388        1.1       cgd     v++;
   1389        1.1       cgd     if (*v == 0)
   1390        1.1       cgd 	return;
   1391        1.1       cgd     gflag = 0, tglob(v);
   1392        1.1       cgd     if (gflag) {
   1393        1.1       cgd 	gv = v = globall(v);
   1394        1.1       cgd 	gargv = 0;
   1395       1.16   mycroft 	if (v == 0)
   1396        1.1       cgd 	    stderror(ERR_NOMATCH);
   1397        1.1       cgd 	v = copyblk(v);
   1398        1.1       cgd     }
   1399        1.1       cgd     else {
   1400        1.1       cgd 	gv = NULL;
   1401        1.1       cgd 	v = copyblk(v);
   1402        1.1       cgd 	trim(v);
   1403        1.1       cgd     }
   1404        1.1       cgd 
   1405        1.1       cgd     saveIN = dcopy(SHIN, -1);
   1406        1.1       cgd     saveOUT = dcopy(SHOUT, -1);
   1407        1.7   mycroft     saveERR = dcopy(SHERR, -1);
   1408        1.1       cgd 
   1409        1.1       cgd     getexit(osetexit);
   1410        1.1       cgd 
   1411        1.1       cgd     if ((my_reenter = setexit()) == 0) {
   1412        1.1       cgd 	evalvec = v;
   1413        1.1       cgd 	evalp = 0;
   1414        1.1       cgd 	SHIN = dcopy(0, -1);
   1415        1.1       cgd 	SHOUT = dcopy(1, -1);
   1416        1.7   mycroft 	SHERR = dcopy(2, -1);
   1417        1.1       cgd 	didfds = 0;
   1418        1.1       cgd 	process(0);
   1419        1.1       cgd     }
   1420        1.1       cgd 
   1421        1.1       cgd     evalvec = oevalvec;
   1422        1.1       cgd     evalp = oevalp;
   1423        1.1       cgd     doneinp = 0;
   1424        1.1       cgd     didfds = odidfds;
   1425       1.31  christos     if (SHIN != -1)
   1426       1.31  christos 	(void)close(SHIN);
   1427       1.31  christos     if (SHOUT != -1)
   1428       1.31  christos 	(void)close(SHOUT);
   1429       1.31  christos     if (SHERR != -1)
   1430       1.31  christos 	(void)close(SHERR);
   1431        1.1       cgd     SHIN = dmove(saveIN, oSHIN);
   1432        1.1       cgd     SHOUT = dmove(saveOUT, oSHOUT);
   1433        1.7   mycroft     SHERR = dmove(saveERR, oSHERR);
   1434        1.1       cgd     if (gv)
   1435        1.7   mycroft 	blkfree(gv), gv = NULL;
   1436        1.1       cgd     resexit(osetexit);
   1437        1.7   mycroft     gv = savegv;
   1438       1.16   mycroft     if (my_reenter)
   1439        1.7   mycroft 	stderror(ERR_SILENT);
   1440        1.7   mycroft }
   1441        1.7   mycroft 
   1442        1.7   mycroft void
   1443        1.7   mycroft /*ARGSUSED*/
   1444       1.21       wiz doprintf(Char **v, struct command *t)
   1445        1.7   mycroft {
   1446        1.7   mycroft     char **c;
   1447        1.7   mycroft     int ret;
   1448        1.7   mycroft 
   1449        1.7   mycroft     ret = progprintf(blklen(v), c = short2blk(v));
   1450       1.21       wiz     (void)fflush(cshout);
   1451       1.21       wiz     (void)fflush(csherr);
   1452        1.7   mycroft 
   1453  1.40.26.1  pgoyette     blkfree((Char **)c);
   1454       1.16   mycroft     if (ret)
   1455        1.1       cgd 	stderror(ERR_SILENT);
   1456        1.1       cgd }
   1457