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