sh.func.c 58.1 KB
Newer Older
1
/* $Header: /p/tcsh/cvsroot/tcsh/sh.func.c,v 3.176 2016/10/18 17:26:42 christos Exp $ */
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * sh.func.c: csh builtin functions
 */
/*-
 * Copyright (c) 1980, 1991 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include "sh.h"

35
RCSID("$tcsh: sh.func.c,v 3.176 2016/10/18 17:26:42 christos Exp $")
36 37 38 39 40 41 42 43

#include "ed.h"
#include "tw.h"
#include "tc.h"
#ifdef WINNT_NATIVE
#include "nt.const.h"
#endif /* WINNT_NATIVE */

44
#if defined (NLS_CATALOGS) && defined(HAVE_ICONV)
45 46 47 48 49 50 51 52 53 54 55 56 57
static iconv_t catgets_iconv; /* Or (iconv_t)-1 */
#endif

/*
 * C shell
 */

extern int MapsAreInited;
extern int NLSMapsAreInited;
extern int GotTermCaps;

static int zlast = -1;

58 59 60 61 62 63
static	void	islogin		(void);
static	void	preread		(void);
static	void	doagain		(void);
static  const char *isrchx	(int);
static	void	search		(int, int, Char *);
static	int	getword		(struct Strbuf *);
64
static	struct wordent	*histgetword	(struct wordent *);
65 66 67 68 69 70 71
static	void	toend		(void);
static	void	xecho		(int, Char **);
static	int	islocale_var	(Char *);
static	void	wpfree		(struct whyle *);

const struct biltins *
isbfunc(struct command *t)
72 73
{
    Char *cp = t->t_dcom[0];
74
    const struct biltins *bp, *bp1, *bp2;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    static struct biltins label = {"", dozip, 0, 0};
    static struct biltins foregnd = {"%job", dofg1, 0, 0};
    static struct biltins backgnd = {"%job &", dobg1, 0, 0};

    /*
     * We never match a builtin that has quoted the first
     * character; this has been the traditional way to escape 
     * builtin commands.
     */
    if (*cp & QUOTE)
	return NULL;

    if (*cp != ':' && lastchr(cp) == ':') {
	label.bname = short2str(cp);
	return (&label);
    }
    if (*cp == '%') {
	if (t->t_dflg & F_AMPERSAND) {
	    t->t_dflg &= ~F_AMPERSAND;
	    backgnd.bname = short2str(cp);
	    return (&backgnd);
	}
	foregnd.bname = short2str(cp);
	return (&foregnd);
    }
#ifdef WARP
    /*
     * This is a perhaps kludgy way to determine if the warp builtin is to be
     * acknowledged or not.  If checkwarp() fails, then we are to assume that
     * the warp command is invalid, and carry on as we would handle any other
     * non-builtin command.         -- JDK 2/4/88
     */
    if (eq(STRwarp, cp) && !checkwarp()) {
	return (0);		/* this builtin disabled */
    }
#endif /* WARP */
    /*
     * Binary search Bp1 is the beginning of the current search range. Bp2 is
     * one past the end.
     */
    for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2;) {
	int i;

	bp = bp1 + ((bp2 - bp1) >> 1);
	if ((i = ((char) *cp) - *bp->bname) == 0 &&
	    (i = StrQcmp(cp, str2short(bp->bname))) == 0)
	    return bp;
	if (i < 0)
	    bp2 = bp;
	else
	    bp1 = bp + 1;
    }
#ifdef WINNT_NATIVE
    return nt_check_additional_builtins(cp);
#endif /*WINNT_NATIVE*/
    return (0);
}

void
134
func(struct command *t, const struct biltins *bp)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{
    int     i;

    xechoit(t->t_dcom);
    setname(bp->bname);
    i = blklen(t->t_dcom) - 1;
    if (i < bp->minargs)
	stderror(ERR_NAME | ERR_TOOFEW);
    if (i > bp->maxargs)
	stderror(ERR_NAME | ERR_TOOMANY);
    (*bp->bfunct) (t->t_dcom, t);
}

/*ARGSUSED*/
void
150
doonintr(Char **v, struct command *c)
151 152 153 154 155
{
    Char *cp;
    Char *vv = v[1];

    USE(c);
156
    if (parintr.sa_handler == SIG_IGN)
157 158 159 160 161
	return;
    if (setintr && intty)
	stderror(ERR_NAME | ERR_TERMINAL);
    cp = gointr;
    gointr = 0;
162
    xfree(cp);
163
    if (vv == 0) {
164 165
	if (setintr)
	    sigset_interrupting(SIGINT, queue_pintr);
166
	else
167
	    (void) signal(SIGINT, SIG_DFL);
168 169 170 171 172 173 174 175
	gointr = 0;
    }
    else if (eq((vv = strip(vv)), STRminus)) {
	(void) signal(SIGINT, SIG_IGN);
	gointr = Strsave(STRminus);
    }
    else {
	gointr = Strsave(vv);
176
	sigset_interrupting(SIGINT, queue_pintr);
177 178 179 180 181
    }
}

/*ARGSUSED*/
void
182
donohup(Char **v, struct command *c)
183 184 185 186 187 188 189
{
    USE(c);
    USE(v);
    if (intty)
	stderror(ERR_NAME | ERR_TERMINAL);
    if (setintr == 0) {
	(void) signal(SIGHUP, SIG_IGN);
190
	phup_disabled = 1;
191 192 193 194 195 196 197 198
#ifdef CC
	submit(getpid());
#endif /* CC */
    }
}

/*ARGSUSED*/
void
199
dohup(Char **v, struct command *c)
200 201 202 203 204 205 206 207 208 209 210 211
{
    USE(c);
    USE(v);
    if (intty)
	stderror(ERR_NAME | ERR_TERMINAL);
    if (setintr == 0)
	(void) signal(SIGHUP, SIG_DFL);
}


/*ARGSUSED*/
void
212
dozip(Char **v, struct command *c)
213 214 215 216 217 218 219
{
    USE(c);
    USE(v);
}

/*ARGSUSED*/
void
220
dofiletest(Char **v, struct command *c)
221
{
222
    Char **globbed, **fileptr, *ftest, *res;
223 224 225 226 227 228

    USE(c);
    if (*(ftest = *++v) != '-')
	stderror(ERR_NAME | ERR_FILEINQ);
    ++v;

229 230 231
    v = glob_all_or_error(v);
    globbed = v;
    cleanup_push(globbed, blk_cleanup);
232 233

    while (*(fileptr = v++) != '\0') {
234 235 236 237
	res = filetest(ftest, &fileptr, 0);
	cleanup_push(res, xfree);
	xprintf("%S", res);
	cleanup_until(res);
238 239 240 241 242
	if (*v)
	    xprintf(" ");
    }
    xprintf("\n");

243
    cleanup_until(globbed);
244 245 246
}

void
247
prvars(void)
248 249 250 251 252 253
{
    plist(&shvhed, VAR_ALL);
}

/*ARGSUSED*/
void
254
doalias(Char **v, struct command *c)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
{
    struct varent *vp;
    Char *p;

    USE(c);
    v++;
    p = *v++;
    if (p == 0)
	plist(&aliases, VAR_ALL);
    else if (*v == 0) {
	vp = adrof1(strip(p), &aliases);
	if (vp && vp->vec)
	    blkpr(vp->vec), xputchar('\n');
    }
    else {
	if (eq(p, STRalias) || eq(p, STRunalias)) {
	    setname(short2str(p));
	    stderror(ERR_NAME | ERR_DANGER);
	}
	set1(strip(p), saveblk(v), &aliases, VAR_READWRITE);
	tw_cmd_free();
    }
}

/*ARGSUSED*/
void
281
unalias(Char **v, struct command *c)
282 283 284 285 286 287 288 289
{
    USE(c);
    unset1(v, &aliases);
    tw_cmd_free();
}

/*ARGSUSED*/
void
290
dologout(Char **v, struct command *c)
291 292 293 294 295 296 297 298 299
{
    USE(c);
    USE(v);
    islogin();
    goodbye(NULL, NULL);
}

/*ARGSUSED*/
void
300
dologin(Char **v, struct command *c)
301 302 303 304 305 306
{
#ifdef WINNT_NATIVE
    USE(c);
    USE(v);
#else /* !WINNT_NATIVE */
    char **p = short2blk(v);
307

308
    USE(c);
309
    cleanup_push((Char **)p, blk_cleanup);
310 311
    islogin();
    rechist(NULL, adrof(STRsavehist) != NULL);
312
    sigaction(SIGTERM, &parterm, NULL);
313 314
    (void) execv(_PATH_BIN_LOGIN, p);
    (void) execv(_PATH_USRBIN_LOGIN, p);
315
    cleanup_until((Char **)p);
316 317 318 319 320 321 322 323 324
    untty();
    xexit(1);
#endif /* !WINNT_NATIVE */
}


#ifdef NEWGRP
/*ARGSUSED*/
void
325
donewgrp(Char **v, struct command *c)
326 327 328 329
{
    char **p;
    if (chkstop == 0 && setintr)
	panystop(0);
330
    sigaction(SIGTERM, &parterm, NULL);
331 332 333 334 335 336 337 338 339 340 341 342 343 344
    p = short2blk(v);
    /*
     * From Beto Appleton (beto@aixwiz.austin.ibm.com)
     * Newgrp can take 2 arguments...
     */
    (void) execv(_PATH_BIN_NEWGRP, p);
    (void) execv(_PATH_USRBIN_NEWGRP, p);
    blkfree((Char **) p);
    untty();
    xexit(1);
}
#endif /* NEWGRP */

static void
345
islogin(void)
346 347 348 349 350 351 352 353 354
{
    if (chkstop == 0 && setintr)
	panystop(0);
    if (loginsh)
	return;
    stderror(ERR_NOTLOGIN);
}

void
355
doif(Char **v, struct command *kp)
356 357 358 359 360
{
    int i;
    Char **vv;

    v++;
361
    i = noexec ? 1 : expr(&v);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    vv = v;
    if (*vv == NULL)
	stderror(ERR_NAME | ERR_EMPTYIF);
    if (eq(*vv, STRthen)) {
	if (*++vv)
	    stderror(ERR_NAME | ERR_IMPRTHEN);
	setname(short2str(STRthen));
	/*
	 * If expression was zero, then scan to else , otherwise just fall into
	 * following code.
	 */
	if (!i)
	    search(TC_IF, 0, NULL);
	return;
    }
    /*
     * Simple command attached to this if. Left shift the node in this tree,
     * munging it so we can reexecute it.
     */
    if (i) {
	lshift(kp->t_dcom, vv - kp->t_dcom);
	reexecute(kp);
	donefds();
    }
}

/*
 * Reexecute a command, being careful not
 * to redo i/o redirection, which is already set up.
 */
void
393
reexecute(struct command *kp)
394 395 396 397 398 399 400 401 402 403 404 405 406
{
    kp->t_dflg &= F_SAVE;
    kp->t_dflg |= F_REPEAT;
    /*
     * If tty is still ours to arbitrate, arbitrate it; otherwise dont even set
     * pgrp's as the jobs would then have no way to get the tty (we can't give
     * it to them, and our parent wouldn't know their pgrp, etc.
     */
    execute(kp, (tpgrp > 0 ? tpgrp : -1), NULL, NULL, TRUE);
}

/*ARGSUSED*/
void
407
doelse (Char **v, struct command *c)
408 409 410
{
    USE(c);
    USE(v);
411 412
    if (!noexec)
	search(TC_ELSE, 0, NULL);
413 414 415 416
}

/*ARGSUSED*/
void
417
dogoto(Char **v, struct command *c)
418 419 420 421
{
    Char   *lp;

    USE(c);
422 423 424 425 426
    lp = globone(v[1], G_ERROR);
    cleanup_push(lp, xfree);
    if (!noexec)
	gotolab(lp);
    cleanup_until(lp);
427 428 429
}

void
430
gotolab(Char *lab)
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
{
    struct whyle *wp;
    /*
     * While we still can, locate any unknown ends of existing loops. This
     * obscure code is the WORST result of the fact that we don't really parse.
     */
    zlast = TC_GOTO;
    for (wp = whyles; wp; wp = wp->w_next)
	if (wp->w_end.type == TCSH_F_SEEK && wp->w_end.f_seek == 0) {
	    search(TC_BREAK, 0, NULL);
	    btell(&wp->w_end);
	}
	else {
	    bseek(&wp->w_end);
	}
    search(TC_GOTO, 0, lab);
    /*
     * Eliminate loops which were exited.
     */
    wfree();
}

/*ARGSUSED*/
void
455
doswitch(Char **v, struct command *c)
456 457 458 459 460 461 462 463 464 465 466 467
{
    Char *cp, *lp;

    USE(c);
    v++;
    if (!*v || *(*v++) != '(')
	stderror(ERR_SYNTAX);
    cp = **v == ')' ? STRNULL : *v++;
    if (*(*v++) != ')')
	v--;
    if (*v)
	stderror(ERR_SYNTAX);
468 469 470 471 472
    lp = globone(cp, G_ERROR);
    cleanup_push(lp, xfree);
    if (!noexec)
	search(TC_SWITCH, 0, lp);
    cleanup_until(lp);
473 474 475 476
}

/*ARGSUSED*/
void
477
dobreak(Char **v, struct command *c)
478 479 480
{
    USE(v);
    USE(c);
481
    if (whyles == NULL)
482
	stderror(ERR_NAME | ERR_NOTWHILE);
483 484
    if (!noexec)
	toend();
485 486 487 488
}

/*ARGSUSED*/
void
489
doexit(Char **v, struct command *c)
490 491 492 493 494 495 496 497 498 499
{
    USE(c);

    if (chkstop == 0 && (intty || intact) && evalvec == 0)
	panystop(0);
    /*
     * Don't DEMAND parentheses here either.
     */
    v++;
    if (*v) {
500
	setv(STRstatus, putn(expr(&v)), VAR_READWRITE);
501 502 503 504 505 506 507 508
	if (*v)
	    stderror(ERR_NAME | ERR_EXPRESSION);
    }
    btoeof();
#if 0
    if (intty)
#endif
    /* Always close, why only on ttys? */
509
	xclose(SHIN);
510 511 512 513
}

/*ARGSUSED*/
void
514
doforeach(Char **v, struct command *c)
515 516 517
{
    Char *cp, *sp;
    struct whyle *nwp;
518
    int gflag;
519 520 521

    USE(c);
    v++;
522 523
    cp = sp = strip(*v);
    if (!letter(*cp))
524
	stderror(ERR_NAME | ERR_VARBEGIN);
525
    do {
526
	cp++;
527 528
    } while (alnum(*cp));
    if (*cp != '\0')
529 530 531 532 533
	stderror(ERR_NAME | ERR_VARALNUM);
    cp = *v++;
    if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')')
	stderror(ERR_NAME | ERR_NOPAREN);
    v++;
534
    gflag = tglob(v);
535
    if (gflag) {
536 537
	v = globall(v, gflag);
	if (v == 0 && !noexec)
538 539 540
	    stderror(ERR_NAME | ERR_NOMATCH);
    }
    else {
541
	v = saveblk(v);
542 543
	trim(v);
    }
544
    nwp = xcalloc(1, sizeof *nwp);
545 546 547 548 549 550 551 552 553 554 555 556
    nwp->w_fe = nwp->w_fe0 = v;
    btell(&nwp->w_start);
    nwp->w_fename = Strsave(cp);
    nwp->w_next = whyles;
    nwp->w_end.type = TCSH_F_SEEK;
    whyles = nwp;
    /*
     * Pre-read the loop so as to be more comprehensible to a terminal user.
     */
    zlast = TC_FOREACH;
    if (intty)
	preread();
557 558
    if (!noexec)
	doagain();
559 560 561 562
}

/*ARGSUSED*/
void
563
dowhile(Char **v, struct command *c)
564 565 566 567 568 569 570 571 572 573 574 575
{
    int status;
    int again = whyles != 0 && 
			  SEEKEQ(&whyles->w_start, &lineloc) &&
			  whyles->w_fename == 0;

    USE(c);
    v++;
    /*
     * Implement prereading here also, taking care not to evaluate the
     * expression before the loop has been read up from a terminal.
     */
576 577 578
    if (noexec)
	status = 0;
    else if (intty && !again)
579 580 581
	status = !exp0(&v, 1);
    else
	status = !expr(&v);
582
    if (*v && !noexec)
583 584
	stderror(ERR_NAME | ERR_EXPRESSION);
    if (!again) {
585
	struct whyle *nwp = xcalloc(1, sizeof(*nwp));
586 587 588 589

	nwp->w_start = lineloc;
	nwp->w_end.type = TCSH_F_SEEK;
	nwp->w_end.f_seek = 0;
590
	nwp->w_end.a_seek = 0;
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
	nwp->w_next = whyles;
	whyles = nwp;
	zlast = TC_WHILE;
	if (intty) {
	    /*
	     * The tty preread
	     */
	    preread();
	    doagain();
	    return;
	}
    }
    if (status)
	/* We ain't gonna loop no more, no more! */
	toend();
}

static void
609
preread(void)
610
{
611 612
    int old_pintr_disabled;

613 614
    whyles->w_end.type = TCSH_I_SEEK;
    if (setintr)
615
	pintr_push_enable(&old_pintr_disabled);
616 617
    search(TC_BREAK, 0, NULL);		/* read the expression in */
    if (setintr)
618
	cleanup_until(&old_pintr_disabled);
619 620 621 622 623
    btell(&whyles->w_end);
}

/*ARGSUSED*/
void
624
doend(Char **v, struct command *c)
625 626 627 628 629 630
{
    USE(v);
    USE(c);
    if (!whyles)
	stderror(ERR_NAME | ERR_NOTWHILE);
    btell(&whyles->w_end);
631 632
    if (!noexec)
	doagain();
633 634 635 636
}

/*ARGSUSED*/
void
637
docontin(Char **v, struct command *c)
638 639 640 641 642
{
    USE(v);
    USE(c);
    if (!whyles)
	stderror(ERR_NAME | ERR_NOTWHILE);
643 644
    if (!noexec)
	doagain();
645 646 647
}

static void
648
doagain(void)
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
{
    /* Repeating a while is simple */
    if (whyles->w_fename == 0) {
	bseek(&whyles->w_start);
	return;
    }
    /*
     * The foreach variable list actually has a spurious word ")" at the end of
     * the w_fe list.  Thus we are at the of the list if one word beyond this
     * is 0.
     */
    if (!whyles->w_fe[1]) {
	dobreak(NULL, NULL);
	return;
    }
664
    setv(whyles->w_fename, quote(Strsave(*whyles->w_fe++)), VAR_READWRITE);
665 666 667 668
    bseek(&whyles->w_start);
}

void
669
dorepeat(Char **v, struct command *kp)
670 671 672 673 674 675 676
{
    int i = 1;

    do {
	i *= getn(v[1]);
	lshift(v, 2);
    } while (v[0] != NULL && Strcmp(v[0], STRrepeat) == 0);
677 678
    if (noexec)
	i = 1;
679

680 681 682 683
    if (setintr) {
	pintr_disabled++;
	cleanup_push(&pintr_disabled, disabled_cleanup);
    }
684
    while (i > 0) {
685 686 687 688 689
	if (setintr && pintr_disabled == 1) {
	    cleanup_until(&pintr_disabled);
	    pintr_disabled++;
	    cleanup_push(&pintr_disabled, disabled_cleanup);
	}
690 691 692
	reexecute(kp);
	--i;
    }
693 694
    if (setintr && pintr_disabled == 1)
        cleanup_until(&pintr_disabled);
695 696 697 698 699
    donefds();
}

/*ARGSUSED*/
void
700
doswbrk(Char **v, struct command *c)
701 702 703
{
    USE(v);
    USE(c);
704 705
    if (!noexec)
	search(TC_BRKSW, 0, NULL);
706 707 708
}

int
709
srchx(Char *cp)
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
{
    struct srch *sp, *sp1, *sp2;
    int i;

    /*
     * Ignore keywords inside heredocs
     */
    if (inheredoc)
	return -1;

    /*
     * Binary search Sp1 is the beginning of the current search range. Sp2 is
     * one past the end.
     */
    for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2;) {
	sp = sp1 + ((sp2 - sp1) >> 1);
	if ((i = *cp - *sp->s_name) == 0 &&
	    (i = Strcmp(cp, str2short(sp->s_name))) == 0)
	    return sp->s_value;
	if (i < 0)
	    sp2 = sp;
	else
	    sp1 = sp + 1;
    }
    return (-1);
}

static const char *
738
isrchx(int n)
739 740 741 742 743 744 745 746 747 748
{
    struct srch *sp, *sp2;

    for (sp = srchn, sp2 = srchn + nsrchn; sp < sp2; sp++)
	if (sp->s_value == n)
	    return (sp->s_name);
    return ("");
}


749
static int Stype;
750 751 752
static Char *Sgoal;

static void
753
search(int type, int level, Char *goal)
754
{
755
    struct Strbuf word = Strbuf_INIT;
756 757 758
    Char *cp;
    struct whyle *wp;
    int wlevel = 0;
759
    struct wordent *histent = NULL, *ohistent = NULL;
760

761
    Stype = type;
762 763 764 765 766
    Sgoal = goal;
    if (type == TC_GOTO) {
	struct Ain a;
	a.type = TCSH_F_SEEK;
	a.f_seek = 0;
767
	a.a_seek = 0;
768 769
	bseek(&a);
    }
770
    cleanup_push(&word, Strbuf_cleanup);
771
    do {
772 773 774 775 776 777 778 779 780
	    
	if (intty) {
	    histent = xmalloc(sizeof(*histent));
	    ohistent = xmalloc(sizeof(*histent));
	    ohistent->word = STRNULL;
	    ohistent->next = histent;
	    histent->prev = ohistent;
	}

781 782 783
	if (intty && fseekp == feobp && aret == TCSH_F_SEEK)
	    printprompt(1, isrchx(type == TC_BREAK ? zlast : type));
	/* xprintf("? "), flush(); */
784 785
	(void) getword(&word);
	Strbuf_terminate(&word);
786 787 788 789 790 791 792 793

	if (intty && Strlen(word.s) > 0) {
	    histent->word = Strsave(word.s);
	    histent->next = xmalloc(sizeof(*histent));
	    histent->next->prev = histent;
	    histent = histent->next;
	}

794
	switch (srchx(word.s)) {
795 796 797

	case TC_ELSE:
	    if (level == 0 && type == TC_IF)
798
		goto end;
799 800 801
	    break;

	case TC_IF:
802 803 804 805 806 807 808
	    while (getword(&word)) {
		if (intty) {
		    histent->word = Strsave(word.s);
		    histent->next = xmalloc(sizeof(*histent));
		    histent->next->prev = histent;
		    histent = histent->next;
		}
809
		continue;
810 811
	    }
	    
812
	    if ((type == TC_IF || type == TC_ELSE) &&
813
		eq(word.s, STRthen))
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
		level++;
	    break;

	case TC_ENDIF:
	    if (type == TC_IF || type == TC_ELSE)
		level--;
	    break;

	case TC_FOREACH:
	case TC_WHILE:
	    wlevel++;
	    if (type == TC_BREAK)
		level++;
	    break;

	case TC_END:
	    if (type == TC_BRKSW) {
		if (wlevel == 0) {
		    wp = whyles;
		    if (wp) {
			    whyles = wp->w_next;
			    wpfree(wp);
		    }
		}
	    }
	    if (type == TC_BREAK)
		level--;
	    wlevel--;
	    break;

	case TC_SWITCH:
	    if (type == TC_SWITCH || type == TC_BRKSW)
		level++;
	    break;

	case TC_ENDSW:
	    if (type == TC_SWITCH || type == TC_BRKSW)
		level--;
	    break;

	case TC_LABEL:
855
	    if (type == TC_GOTO && getword(&word) && eq(word.s, goal))
856 857 858 859 860 861
		level = -1;
	    break;

	default:
	    if (type != TC_GOTO && (type != TC_SWITCH || level != 0))
		break;
862
	    if (word.len == 0 || word.s[word.len - 1] != ':')
863
		break;
864 865 866
	    word.s[--word.len] = 0;
	    if ((type == TC_GOTO && eq(word.s, goal)) ||
		(type == TC_SWITCH && eq(word.s, STRdefault)))
867 868 869 870 871 872
		level = -1;
	    break;

	case TC_CASE:
	    if (type != TC_SWITCH || level != 0)
		break;
873 874 875 876 877
	    (void) getword(&word);
	    if (word.len != 0 && word.s[word.len - 1] == ':')
		word.s[--word.len] = 0;
	    cp = strip(Dfix1(word.s));
	    cleanup_push(cp, xfree);
878 879
	    if (Gmatch(goal, cp))
		level = -1;
880
	    cleanup_until(cp);
881 882 883 884 885 886 887
	    break;

	case TC_DEFAULT:
	    if (type == TC_SWITCH && level == 0)
		level = -1;
	    break;
	}
888 889 890 891 892 893 894 895
	if (intty) {
	    ohistent->prev = histgetword(histent);
	    ohistent->prev->next = ohistent;
	    savehist(ohistent, 0);
	    freelex(ohistent);
	    xfree(ohistent);
	} else 
	    (void) getword(NULL);
896
    } while (level >= 0);
897 898
 end:
    cleanup_until(&word);
899 900
}

901 902 903
static struct wordent *
histgetword(struct wordent *histent) 
{
904
    int first;
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    eChar c, d;
    int e;
    struct Strbuf *tmp;
    tmp = xmalloc(sizeof(*tmp));
    tmp->size = 0;
    tmp->s = NULL;
    c = readc(1);
    d = 0;
    e = 0;
    for (;;) {
	tmp->len = 0;
	Strbuf_terminate (tmp);
	while (c == ' ' || c == '\t')
	    c = readc(1);
	if (c == '#')
	    do
		c = readc(1);
	    while (c != CHAR_ERR && c != '\n');
	if (c == CHAR_ERR)
	    goto past;
	if (c == '\n') 
	    goto nl;
	unreadc(c);
	first = 1;
	do {
	    e = (c == '\\');
	    c = readc(1);
	    if (c == '\\' && !e) {
		if ((c = readc(1)) == '\n') {
		    e = 1;
		    c = ' ';
		} else {
		    unreadc(c);
		    c = '\\';
		}
	    }
	    if ((c == '\'' || c == '"') && !e) {
		if (d == 0)
		    d = c;
		else if (d == c)
		    d = 0;
	    }
	    if (c == CHAR_ERR)
		goto past;
	    
	    Strbuf_append1(tmp, (Char) c);
	    
	    if (!first && !d && c == '(' && !e) {
		break;
	    }
	    first = 0;
	} while (d || e || (c != ' ' && c != '\t' && c != '\n'));
	tmp->len--;
	if (tmp->len) {
	    Strbuf_terminate(tmp);
	    histent->word = Strsave(tmp->s);
	    histent->next = xmalloc(sizeof (*histent));
	    histent->next->prev = histent;
	    histent = histent->next;
	}
	if (c == '\n') {
	nl:
	    tmp->len = 0;
	    Strbuf_append1(tmp, (Char) c);
	    Strbuf_terminate(tmp);
	    histent->word = Strsave(tmp->s);
	    return histent;
	}
    }
    
past:
    switch (Stype) {

    case TC_IF:
	stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
	break;

    case TC_ELSE:
	stderror(ERR_NAME | ERR_NOTFOUND, "endif");
	break;

    case TC_BRKSW:
    case TC_SWITCH:
	stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
	break;

    case TC_BREAK:
	stderror(ERR_NAME | ERR_NOTFOUND, "end");
	break;

    case TC_GOTO:
	setname(short2str(Sgoal));
	stderror(ERR_NAME | ERR_NOTFOUND, "label");
	break;

    default:
	break;
    }
    /* NOTREACHED */
    return NULL;
}

1007
static int
1008
getword(struct Strbuf *wp)
1009 1010 1011 1012
{
    int found = 0, first;
    eChar c, d;

1013 1014
    if (wp)
	wp->len = 0;
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    c = readc(1);
    d = 0;
    do {
	while (c == ' ' || c == '\t')
	    c = readc(1);
	if (c == '#')
	    do
		c = readc(1);
	    while (c != CHAR_ERR && c != '\n');
	if (c == CHAR_ERR)
	    goto past;
	if (c == '\n') {
	    if (wp)
		break;
	    return (0);
	}
	unreadc(c);
	found = 1;
	first = 1;
	do {
	    c = readc(1);
	    if (c == '\\' && (c = readc(1)) == '\n')
		c = ' ';
	    if (c == '\'' || c == '"') {
		if (d == 0)
		    d = c;
		else if (d == c)
		    d = 0;
	    }
	    if (c == CHAR_ERR)
		goto past;
1046 1047
	    if (wp)
		Strbuf_append1(wp, (Char) c);
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	    if (!d && c == ')') {
		if (!first && wp) {
		    goto past_word_end;
		} else {
		    if (wp) {
			wp->len = 1;
			Strbuf_terminate(wp);
		    }
		    return found;
		}
	    }
1059
	    if (!first && !d && c == '(') {
1060 1061
		if (wp)
		    goto past_word_end;
1062 1063 1064 1065 1066 1067 1068
		else 
		    break;
	    }
	    first = 0;
	} while ((d || (c != ' ' && c != '\t')) && c != '\n');
    } while (wp == 0);

1069
 past_word_end:
1070
    unreadc(c);
1071 1072 1073 1074
    if (found) {
	wp->len--;
	Strbuf_terminate(wp);
    }
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

    return (found);

past:
    switch (Stype) {

    case TC_IF:
	stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
	break;

    case TC_ELSE:
	stderror(ERR_NAME | ERR_NOTFOUND, "endif");
	break;

    case TC_BRKSW:
    case TC_SWITCH:
	stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
	break;

    case TC_BREAK:
	stderror(ERR_NAME | ERR_NOTFOUND, "end");
	break;

    case TC_GOTO:
	setname(short2str(Sgoal));
	stderror(ERR_NAME | ERR_NOTFOUND, "label");
	break;

    default:
	break;
    }
    /* NOTREACHED */
    return (0);
}

static void
1111
toend(void)
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
{
    if (whyles->w_end.type == TCSH_F_SEEK && whyles->w_end.f_seek == 0) {
	search(TC_BREAK, 0, NULL);
	btell(&whyles->w_end);
	whyles->w_end.f_seek--;
    }
    else {
	bseek(&whyles->w_end);
    }
    wfree();
}

static void
1125
wpfree(struct whyle *wp)
1126 1127 1128
{
	if (wp->w_fe0)
	    blkfree(wp->w_fe0);
1129 1130
	xfree(wp->w_fename);
	xfree(wp);
1131 1132 1133
}

void
1134
wfree(void)
1135 1136 1137 1138 1139 1140 1141 1142
{
    struct Ain    o;
    struct whyle *nwp;
#ifdef lint
    nwp = NULL;	/* sun lint is dumb! */
#endif

#ifdef FDEBUG
1143
    static const char foo[] = "IAFE";
1144 1145 1146 1147 1148
#endif /* FDEBUG */

    btell(&o);

#ifdef FDEBUG
1149
    xprintf("o->type %c o->a_seek %d o->f_seek %d\n",
1150 1151 1152 1153 1154 1155 1156 1157
	    foo[o.type + 1], o.a_seek, o.f_seek);
#endif /* FDEBUG */

    for (; whyles; whyles = nwp) {
	struct whyle *wp = whyles;
	nwp = wp->w_next;

#ifdef FDEBUG
1158
	xprintf("start->type %c start->a_seek %d start->f_seek %d\n",
1159 1160
		foo[wp->w_start.type+1], 
		wp->w_start.a_seek, wp->w_start.f_seek);
1161
	xprintf("end->type %c end->a_seek %d end->f_seek %d\n",
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		foo[wp->w_end.type + 1], wp->w_end.a_seek, wp->w_end.f_seek);
#endif /* FDEBUG */

	/*
	 * XXX: We free loops that have different seek types.
	 */
	if (wp->w_end.type != TCSH_I_SEEK && wp->w_start.type == wp->w_end.type &&
	    wp->w_start.type == o.type) {
	    if (wp->w_end.type == TCSH_F_SEEK) {
		if (o.f_seek >= wp->w_start.f_seek && 
		    (wp->w_end.f_seek == 0 || o.f_seek < wp->w_end.f_seek))
		    break;
	    }
	    else {
		if (o.a_seek >= wp->w_start.a_seek && 
		    (wp->w_end.a_seek == 0 || o.a_seek < wp->w_end.a_seek))
		    break;
	    }
	}

	wpfree(wp);
    }
}

/*ARGSUSED*/
void
1188
doecho(Char **v, struct command *c)
1189 1190 1191 1192 1193 1194 1195
{
    USE(c);
    xecho(' ', v);
}

/*ARGSUSED*/
void
1196
doglob(Char **v, struct command *c)
1197 1198 1199 1200 1201 1202 1203
{
    USE(c);
    xecho(0, v);
    flush();
}

static void
1204
xecho(int sep, Char **v)
1205
{
1206
    Char *cp, **globbed = NULL;
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
    int     nonl = 0;
    int	    echo_style = ECHO_STYLE;
    struct varent *vp;

    if ((vp = adrof(STRecho_style)) != NULL && vp->vec != NULL &&
	vp->vec[0] != NULL) {
	if (Strcmp(vp->vec[0], STRbsd) == 0)
	    echo_style = BSD_ECHO;
	else if (Strcmp(vp->vec[0], STRsysv) == 0)
	    echo_style = SYSV_ECHO;
	else if (Strcmp(vp->vec[0], STRboth) == 0)
	    echo_style = BOTH_ECHO;
	else if (Strcmp(vp->vec[0], STRnone) == 0)
	    echo_style = NONE_ECHO;
    }

    v++;
    if (*v == 0)
	goto done;
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
    if (setintr) {
	int old_pintr_disabled;
	pintr_push_enable(&old_pintr_disabled);
	v = glob_all_or_error(v);
	cleanup_until(&old_pintr_disabled);
    } else {
	v = glob_all_or_error(v);
    }
    globbed = v;
    if (globbed != NULL)
	cleanup_push(globbed, blk_cleanup);
1237 1238 1239 1240 1241 1242 1243

    if ((echo_style & BSD_ECHO) != 0 && sep == ' ' && *v && eq(*v, STRmn))
	nonl++, v++;

    while ((cp = *v++) != 0) {
	Char c;

1244 1245 1246 1247 1248 1249
	if (setintr) {
	    int old_pintr_disabled;

	    pintr_push_enable(&old_pintr_disabled);
	    cleanup_until(&old_pintr_disabled);
	}
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	while ((c = *cp++) != 0) {
	    if ((echo_style & SYSV_ECHO) != 0 && c == '\\') {
		switch (c = *cp++) {
		case 'a':
		    c = '\a';
		    break;
		case 'b':
		    c = '\b';
		    break;
		case 'c':
		    nonl = 1;
		    goto done;
		case 'e':
#if 0			/* Windows does not understand \e */
		    c = '\e';
#else
1266
		    c = CTL_ESC('\033');
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
#endif
		    break;
		case 'f':
		    c = '\f';
		    break;
		case 'n':
		    c = '\n';
		    break;
		case 'r':
		    c = '\r';
		    break;
		case 't':
		    c = '\t';
		    break;
		case 'v':
		    c = '\v';
		    break;
		case '\\':
		    c = '\\';
		    break;
		case '0':
		    c = 0;
		    if (*cp >= '0' && *cp < '8')
			c = c * 8 + *cp++ - '0';
		    if (*cp >= '0' && *cp < '8')
			c = c * 8 + *cp++ - '0';
		    if (*cp >= '0' && *cp < '8')
			c = c * 8 + *cp++ - '0';
		    break;
		case '\0':
		    c = '\\';
		    cp--;
		    break;
		default:
		    xputchar('\\' | QUOTE);
		    break;
		}
	    }
	    xputwchar(c | QUOTE);

	}
	if (*v)
	    xputchar(sep | QUOTE);
    }
done:
    if (sep && nonl == 0)
	xputchar('\n');
    else
	flush();
1316 1317
    if (globbed != NULL)
	cleanup_until(globbed);
1318 1319 1320 1321
}

/* check whether an environment variable should invoke 'set_locale()' */
static int
1322
islocale_var(Char *var)
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
{
    static Char *locale_vars[] = {
	STRLANG,	STRLC_ALL, 	STRLC_CTYPE,	STRLC_NUMERIC,
	STRLC_TIME,	STRLC_COLLATE,	STRLC_MESSAGES,	STRLC_MONETARY, 0
    };
    Char **v;

    for (v = locale_vars; *v; ++v)
	if (eq(var, *v))
	    return 1;
    return 0;
}

1336 1337 1338 1339 1340 1341 1342
static void
xlate_cr_cleanup(void *dummy)
{
    USE(dummy);
    xlate_cr = 0;
}

1343 1344
/*ARGSUSED*/
void
1345
doprintenv(Char **v, struct command *c) 
1346 1347 1348 1349 1350 1351 1352 1353 1354
{
    Char   *e;

    USE(c);
    v++;
    if (*v == 0) {
	Char **ep;

	xlate_cr = 1;
1355 1356 1357 1358 1359 1360 1361 1362
	cleanup_push(&xlate_cr, xlate_cr_cleanup);
	for (ep = STR_environ; *ep; ep++) {
	    if (setintr) {
		int old_pintr_disabled;

		pintr_push_enable(&old_pintr_disabled);
		cleanup_until(&old_pintr_disabled);
	    }
1363
	    xprintf("%S\n", *ep);
1364 1365
	}
	cleanup_until(&xlate_cr);
1366 1367
    }
    else if ((e = tgetenv(*v)) != NULL) {
1368 1369 1370
	int old_output_raw;

	old_output_raw = output_raw;
1371
	output_raw = 1;
1372
	cleanup_push(&old_output_raw, output_raw_restore);
1373
	xprintf("%S\n", e);
1374
	cleanup_until(&old_output_raw);
1375 1376
    }
    else
1377
	setcopy(STRstatus, STR1, VAR_READWRITE);
1378 1379 1380 1381 1382 1383 1384
}

/* from "Karl Berry." <karl%mote.umb.edu@relay.cs.net> -- for NeXT things
   (and anything else with a modern compiler) */

/*ARGSUSED*/
void
1385
dosetenv(Char **v, struct command *c)
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
{
    Char   *vp, *lp;

    USE(c);
    if (*++v == 0) {
	doprintenv(--v, 0);
	return;
    }

    vp = *v++;
    lp = vp;
1397 1398 1399 1400 1401

    if (!letter(*lp))
	stderror(ERR_NAME | ERR_VARBEGIN);
    do {
	lp++;
1402
    } while (alnum(*lp) || *lp == '.');
1403 1404 1405
    if (*lp != '\0')
	stderror(ERR_NAME | ERR_VARALNUM);

1406 1407 1408
    if ((lp = *v++) == 0)
	lp = STRNULL;

1409 1410 1411
    lp = globone(lp, G_APPEND);
    cleanup_push(lp, xfree);
    tsetenv(vp, lp);
1412
    if (eq(vp, STRKPATH)) {
1413
        importpath(lp);
1414
	dohash(NULL, NULL);
1415
	cleanup_until(lp);
1416 1417 1418 1419 1420 1421
	return;
    }

#ifdef apollo
    if (eq(vp, STRSYSTYPE)) {
	dohash(NULL, NULL);
1422
	cleanup_until(lp);
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
	return;
    }
#endif /* apollo */

    /* dspkanji/dspmbyte autosetting */
    /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
#if defined(DSPMBYTE)
    if(eq(vp, STRLANG) && !adrof(CHECK_MBYTEVAR)) {
	autoset_dspmbyte(lp);
    }
#endif

    if (islocale_var(vp)) {
#ifdef NLS
	int     k;

# ifdef SETLOCALEBUG
	dont_free = 1;
# endif /* SETLOCALEBUG */
	(void) setlocale(LC_ALL, "");
# ifdef LC_COLLATE
	(void) setlocale(LC_COLLATE, "");
# endif
# ifdef LC_CTYPE
	(void) setlocale(LC_CTYPE, ""); /* for iscntrl */
# endif /* LC_CTYPE */
1449 1450 1451
# if defined(AUTOSET_KANJI)
        autoset_kanji();
# endif /* AUTOSET_KANJI */
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
# ifdef NLS_CATALOGS
#  ifdef LC_MESSAGES
	(void) setlocale(LC_MESSAGES, "");
#  endif /* LC_MESSAGES */
	nlsclose();
	nlsinit();
# endif /* NLS_CATALOGS */
# ifdef SETLOCALEBUG
	dont_free = 0;
# endif /* SETLOCALEBUG */
# ifdef STRCOLLBUG
	fix_strcoll_bug();
# endif /* STRCOLLBUG */
	tw_cmd_free();	/* since the collation sequence has changed */
1466
	for (k = 0200; k <= 0377 && !Isprint(CTL_ESC(k)); k++)
1467 1468 1469 1470 1471 1472 1473 1474 1475
	    continue;
	AsciiOnly = MB_CUR_MAX == 1 && k > 0377;
#else /* !NLS */
	AsciiOnly = 0;
#endif /* NLS */
	NLSMapsAreInited = 0;
	ed_Init();
	if (MapsAreInited && !NLSMapsAreInited)
	    ed_InitNLSMaps();
1476
	cleanup_until(lp);
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
	return;
    }

#ifdef NLS_CATALOGS
    if (eq(vp, STRNLSPATH)) {
	nlsclose();
	nlsinit();
    }
#endif

    if (eq(vp, STRNOREBIND)) {
	NoNLSRebind = 1;
	MapsAreInited = 0;
	NLSMapsAreInited = 0;
	ed_InitMaps();
1492
	cleanup_until(lp);
1493 1494 1495 1496 1497
	return;
    }
#ifdef WINNT_NATIVE
    if (eq(vp, STRtcshlang)) {
	nlsinit();
1498
	cleanup_until(lp);
1499 1500 1501 1502 1503
	return;
    }
#endif /* WINNT_NATIVE */
    if (eq(vp, STRKTERM)) {
	char *t;
1504 1505 1506 1507

	setv(STRterm, quote(lp), VAR_READWRITE);	/* lp memory used here */
	cleanup_ignore(lp);
	cleanup_until(lp);
1508 1509 1510 1511
	t = short2str(lp);
	if (noediting && strcmp(t, "unknown") != 0 && strcmp(t,"dumb") != 0) {
	    editing = 1;
	    noediting = 0;
1512
	    setNS(STRedit);
1513 1514 1515 1516 1517 1518 1519
	}
	GotTermCaps = 0;
	ed_Init();
	return;
    }

    if (eq(vp, STRKHOME)) {
1520
	Char *canon;
1521 1522 1523
	/*
	 * convert to canonical pathname (possibly resolving symlinks)
	 */
1524 1525 1526 1527 1528 1529 1530
	canon = dcanon(lp, lp);
	cleanup_ignore(lp);
	cleanup_until(lp);
	cleanup_push(canon, xfree);
	setv(STRhome, quote(canon), VAR_READWRITE); /* lp memory used here */
	cleanup_ignore(canon);
	cleanup_until(canon);
1531 1532 1533 1534 1535 1536 1537

	/* fix directory stack for new tilde home */
	dtilde();
	return;
    }

    if (eq(vp, STRKSHLVL)) {
1538 1539 1540
	setv(STRshlvl, quote(lp), VAR_READWRITE); /* lp memory used here */
	cleanup_ignore(lp);
	cleanup_until(lp);
1541 1542 1543 1544
	return;
    }

    if (eq(vp, STRKUSER)) {
1545 1546 1547
	setv(STRuser, quote(lp), VAR_READWRITE);	/* lp memory used here */
	cleanup_ignore(lp);
	cleanup_until(lp);
1548 1549 1550 1551
	return;
    }

    if (eq(vp, STRKGROUP)) {
1552 1553 1554
	setv(STRgroup, quote(lp), VAR_READWRITE); /* lp memory used here */
	cleanup_ignore(lp);
	cleanup_until(lp);
1555 1556 1557 1558 1559 1560
	return;
    }

#ifdef COLOR_LS_F
    if (eq(vp, STRLS_COLORS)) {
        parseLS_COLORS(lp);
1561
	cleanup_until(lp);
1562 1563
	return;
    }
1564 1565 1566 1567 1568
    if (eq(vp, STRLSCOLORS)) {
        parseLSCOLORS(lp);
	cleanup_until(lp);
	return;
    }
1569 1570 1571 1572 1573 1574 1575 1576
#endif /* COLOR_LS_F */

#ifdef SIG_WINDOW
    /*
     * Load/Update $LINES $COLUMNS
     */
    if ((eq(lp, STRNULL) && (eq(vp, STRLINES) || eq(vp, STRCOLUMNS))) ||
	eq(vp, STRTERMCAP)) {
1577
	cleanup_until(lp);
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
	check_window_size(1);
	return;
    }

    /*
     * Change the size to the one directed by $LINES and $COLUMNS
     */
    if (eq(vp, STRLINES) || eq(vp, STRCOLUMNS)) {
#if 0
	GotTermCaps = 0;
#endif
1589
	cleanup_until(lp);
1590 1591 1592 1593
	ed_Init();
	return;
    }
#endif /* SIG_WINDOW */
1594
    cleanup_until(lp);
1595 1596 1597 1598
}

/*ARGSUSED*/
void
1599
dounsetenv(Char **v, struct command *c)
1600
{
1601
    Char  **ep, *p, *n, *name;
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
    int     i, maxi;

    USE(c);
    /*
     * Find the longest environment variable
     */
    for (maxi = 0, ep = STR_environ; *ep; ep++) {
	for (i = 0, p = *ep; *p && *p != '='; p++, i++)
	    continue;
	if (i > maxi)
	    maxi = i;
    }

1615 1616
    name = xmalloc((maxi + 1) * sizeof(Char));
    cleanup_push(name, xfree);
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657

    while (++v && *v) 
	for (maxi = 1; maxi;)
	    for (maxi = 0, ep = STR_environ; *ep; ep++) {
		for (n = name, p = *ep; *p && *p != '='; *n++ = *p++)
		    continue;
		*n = '\0';
		if (!Gmatch(name, *v))
		    continue;
		maxi = 1;

		/* Unset the name. This wasn't being done until
		 * later but most of the stuff following won't
		 * work (particularly the setlocale() and getenv()
		 * stuff) as intended until the name is actually
		 * removed. (sg)
		 */
		Unsetenv(name);

		if (eq(name, STRNOREBIND)) {
		    NoNLSRebind = 0;
		    MapsAreInited = 0;
		    NLSMapsAreInited = 0;
		    ed_InitMaps();
		}
#ifdef apollo
		else if (eq(name, STRSYSTYPE))
		    dohash(NULL, NULL);
#endif /* apollo */
		else if (islocale_var(name)) {
#ifdef NLS
		    int     k;

# ifdef SETLOCALEBUG
		    dont_free = 1;
# endif /* SETLOCALEBUG */
		    (void) setlocale(LC_ALL, "");
# ifdef LC_COLLATE
		    (void) setlocale(LC_COLLATE, "");
# endif
# ifdef LC_CTYPE
1658
		    (void) setlocale(LC_CTYPE, ""); /* for iscntrl */
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
# endif /* LC_CTYPE */
# ifdef NLS_CATALOGS
#  ifdef LC_MESSAGES
		    (void) setlocale(LC_MESSAGES, "");
#  endif /* LC_MESSAGES */
		    nlsclose();
		    nlsinit();
# endif /* NLS_CATALOGS */
# ifdef SETLOCALEBUG
		    dont_free = 0;
# endif /* SETLOCALEBUG */
# ifdef STRCOLLBUG
		    fix_strcoll_bug();
# endif /* STRCOLLBUG */
		    tw_cmd_free();/* since the collation sequence has changed */
1674
		    for (k = 0200; k <= 0377 && !Isprint(CTL_ESC(k)); k++)
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
			continue;
		    AsciiOnly = MB_CUR_MAX == 1 && k > 0377;
#else /* !NLS */
		    AsciiOnly = getenv("LANG") == NULL &&
			getenv("LC_CTYPE") == NULL;
#endif /* NLS */
		    NLSMapsAreInited = 0;
		    ed_Init();
		    if (MapsAreInited && !NLSMapsAreInited)
			ed_InitNLSMaps();

		}
#ifdef WINNT_NATIVE
		else if (eq(name,(STRtcshlang))) {
		    nls_dll_unload();
		    nlsinit();
		}
#endif /* WINNT_NATIVE */
#ifdef COLOR_LS_F
		else if (eq(name, STRLS_COLORS))
		    parseLS_COLORS(n);
1696 1697
		else if (eq(name, STRLSCOLORS))
		    parseLSCOLORS(n);
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
#endif /* COLOR_LS_F */
#ifdef NLS_CATALOGS
		else if (eq(name, STRNLSPATH)) {
		    nlsclose();
		    nlsinit();
		}
#endif
		/*
		 * start again cause the environment changes
		 */
		break;
	    }
1710
    cleanup_until(name);
1711 1712 1713
}

void
1714
tsetenv(const Char *name, const Char *val)
1715 1716 1717 1718 1719 1720 1721 1722 1723
{
#ifdef SETENV_IN_LIB
/*
 * XXX: This does not work right, since tcsh cannot track changes to
 * the environment this way. (the builtin setenv without arguments does
 * not print the right stuff neither does unsetenv). This was for Mach,
 * it is not needed anymore.
 */
#undef setenv
1724
    char   *cname;
1725

1726
    if (name == NULL)
1727
	return;
1728 1729 1730
    cname = strsave(short2str(name));
    setenv(cname, short2str(val), 1);
    xfree(cname);
1731 1732 1733 1734 1735 1736 1737 1738
#else /* !SETENV_IN_LIB */
    Char **ep = STR_environ;
    const Char *ccp;
    Char *cp, *dp;
    Char   *blk[2];
    Char  **oep = ep;

#ifdef WINNT_NATIVE
1739
    nt_set_env(name,val);
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
#endif /* WINNT_NATIVE */
    for (; *ep; ep++) {
#ifdef WINNT_NATIVE
	for (ccp = name, dp = *ep; *ccp && Tolower(*ccp & TRIM) == Tolower(*dp);
				ccp++, dp++)
#else
	for (ccp = name, dp = *ep; *ccp && (*ccp & TRIM) == *dp; ccp++, dp++)
#endif /* WINNT_NATIVE */
	    continue;
	if (*ccp != 0 || *dp != '=')
	    continue;
	cp = Strspl(STRequal, val);
1752
	xfree(*ep);
1753
	*ep = strip(Strspl(name, cp));
1754
	xfree(cp);
1755 1756 1757 1758 1759 1760
	blkfree((Char **) environ);
	environ = short2blk(STR_environ);
	return;
    }
    cp = Strspl(name, STRequal);
    blk[0] = strip(Strspl(cp, val));
1761
    xfree(cp);
1762 1763 1764 1765
    blk[1] = 0;
    STR_environ = blkspl(STR_environ, blk);
    blkfree((Char **) environ);
    environ = short2blk(STR_environ);
1766
    xfree(oep);
1767 1768 1769 1770
#endif /* SETENV_IN_LIB */
}

void
1771
Unsetenv(Char *name)
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
{
    Char **ep = STR_environ;
    Char *cp, *dp;
    Char **oep = ep;

#ifdef WINNT_NATIVE
	nt_set_env(name,NULL);
#endif /*WINNT_NATIVE */
    for (; *ep; ep++) {
	for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
	    continue;
	if (*cp != 0 || *dp != '=')
	    continue;
	cp = *ep;
	*ep = 0;
	STR_environ = blkspl(STR_environ, ep + 1);
	blkfree((Char **) environ);
	environ = short2blk(STR_environ);
	*ep = cp;
1791 1792
	xfree(cp);
	xfree(oep);
1793 1794 1795 1796 1797 1798
	return;
    }
}

/*ARGSUSED*/
void
1799
doumask(Char **v, struct command *c)
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
{
    Char *cp = v[1];
    int i;

    USE(c);
    if (cp == 0) {
	i = (int)umask(0);
	(void) umask(i);
	xprintf("%o\n", i);
	return;
    }
    i = 0;
    while (Isdigit(*cp) && *cp != '8' && *cp != '9')
	i = i * 8 + *cp++ - '0';
    if (*cp || i < 0 || i > 0777)
	stderror(ERR_NAME | ERR_MASK);
    (void) umask(i);
}

#ifndef HAVENOLIMIT
# ifndef BSDLIMIT
   typedef long RLIM_TYPE;
#  ifdef _OSD_POSIX /* BS2000 */
#   include <ulimit.h>
#  endif
#  ifndef RLIM_INFINITY
#   if !defined(_MINIX) && !defined(__clipper__) && !defined(_CRAY)
    extern RLIM_TYPE ulimit();
#   endif /* ! _MINIX && !__clipper__ */
#   define RLIM_INFINITY 0x003fffff
#   define RLIMIT_FSIZE 1
#  endif /* RLIM_INFINITY */
#  ifdef aiws
#   define toset(a) (((a) == 3) ? 1004 : (a) + 1)
#   define RLIMIT_DATA	3
#   define RLIMIT_STACK 1005
#  else /* aiws */
#   define toset(a) ((a) + 1)
#  endif /* aiws */
# else /* BSDLIMIT */
#  if (defined(BSD4_4) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || (HPUXVERSION >= 1100)) && !defined(__386BSD__)
    typedef rlim_t RLIM_TYPE;
#  else
#   if defined(SOLARIS2) || (defined(sgi) && SYSVREL > 3)
     typedef rlim_t RLIM_TYPE;
#   else
#    if defined(_SX)
      typedef long long RLIM_TYPE;
#    else /* !_SX */
      typedef unsigned long RLIM_TYPE;
#    endif /* _SX */
#   endif /* SOLARIS2 || (sgi && SYSVREL > 3) */
#  endif /* BSD4_4 && !__386BSD__  */
# endif /* BSDLIMIT */

# if (HPUXVERSION > 700) && (HPUXVERSION < 1100) && defined(BSDLIMIT)
/* Yes hpux8.0 has limits but <sys/resource.h> does not make them public */
/* Yes, we could have defined _KERNEL, and -I/etc/conf/h, but is that better? */
#  ifndef RLIMIT_CPU
#   define RLIMIT_CPU		0
#   define RLIMIT_FSIZE		1
#   define RLIMIT_DATA		2
#   define RLIMIT_STACK		3
#   define RLIMIT_CORE		4
#   define RLIMIT_RSS		5
#   define RLIMIT_NOFILE	6
#  endif /* RLIMIT_CPU */
#  ifndef RLIM_INFINITY
#   define RLIM_INFINITY	0x7fffffff
#  endif /* RLIM_INFINITY */
   /*
    * old versions of HP/UX counted limits in 512 bytes
    */
#  ifndef SIGRTMIN
#   define FILESIZE512
#  endif /* SIGRTMIN */
# endif /* (HPUXVERSION > 700) && (HPUXVERSION < 1100) && BSDLIMIT */

# if SYSVREL > 3 && defined(BSDLIMIT) && !defined(_SX)
/* In order to use rusage, we included "/usr/ucbinclude/sys/resource.h" in */
/* sh.h.  However, some SVR4 limits are defined in <sys/resource.h>.  Rather */
/* than include both and get warnings, we define the extra SVR4 limits here. */
/* XXX: I don't understand if RLIMIT_AS is defined, why don't we define */
/* RLIMIT_VMEM based on it? */
#  ifndef RLIMIT_VMEM
#   define RLIMIT_VMEM	6
#  endif
#  ifndef RLIMIT_AS
#   define RLIMIT_AS	RLIMIT_VMEM
#  endif
# endif /* SYSVREL > 3 && BSDLIMIT */

1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
# if (defined(__linux__) || defined(__GNU__) || defined(__GLIBC__))
#  if defined(RLIMIT_AS) && !defined(RLIMIT_VMEM)
#   define RLIMIT_VMEM	RLIMIT_AS
#  endif
/*
 * Oh well, <asm-generic/resource.h> has it, but <bits/resource.h> does not
 * Linux headers: When the left hand does not know what the right hand does.
 */
#  if defined(RLIMIT_RTPRIO) && !defined(RLIMIT_RTTIME)
#   define RLIMIT_RTTIME (RLIMIT_RTPRIO + 1)
#  endif
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
# endif

struct limits limits[] = 
{
# ifdef RLIMIT_CPU
    { RLIMIT_CPU, 	"cputime",	1,	"seconds"	},
# endif /* RLIMIT_CPU */

# ifdef RLIMIT_FSIZE
#  ifndef aiws
    { RLIMIT_FSIZE, 	"filesize",	1024,	"kbytes"	},
#  else
    { RLIMIT_FSIZE, 	"filesize",	512,	"blocks"	},
#  endif /* aiws */
# endif /* RLIMIT_FSIZE */

# ifdef RLIMIT_DATA
    { RLIMIT_DATA, 	"datasize",	1024,	"kbytes"	},
# endif /* RLIMIT_DATA */

# ifdef RLIMIT_STACK
#  ifndef aiws
    { RLIMIT_STACK, 	"stacksize",	1024,	"kbytes"	},
#  else
    { RLIMIT_STACK, 	"stacksize",	1024 * 1024,	"kbytes"},
#  endif /* aiws */
# endif /* RLIMIT_STACK */

# ifdef RLIMIT_CORE
    { RLIMIT_CORE, 	"coredumpsize",	1024,	"kbytes"	},
# endif /* RLIMIT_CORE */

# ifdef RLIMIT_RSS
    { RLIMIT_RSS, 	"memoryuse",	1024,	"kbytes"	},
# endif /* RLIMIT_RSS */

# ifdef RLIMIT_UMEM
    { RLIMIT_UMEM, 	"memoryuse",	1024,	"kbytes"	},
# endif /* RLIMIT_UMEM */

# ifdef RLIMIT_VMEM