xref: /netbsd-src/bin/csh/func.c (revision 76adbe2bd34c2af7966b51430e8e3b51bf57c4ab)
1 /*	$NetBSD: func.c,v 1.12 1997/01/13 17:53:22 tls 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)func.c	8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: func.c,v 1.12 1997/01/13 17:53:22 tls Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <signal.h>
47 #include <locale.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #if __STDC__
52 # include <stdarg.h>
53 #else
54 # include <varargs.h>
55 #endif
56 
57 #include "csh.h"
58 #include "extern.h"
59 #include "pathnames.h"
60 
61 extern char **environ;
62 
63 static int zlast = -1;
64 static void	islogin __P((void));
65 static void	reexecute __P((struct command *));
66 static void	preread __P((void));
67 static void	doagain __P((void));
68 static void	search __P((int, int, Char *));
69 static int	getword __P((Char *));
70 static int	keyword __P((Char *));
71 static void	toend __P((void));
72 static void	xecho __P((int, Char **));
73 static void	Unsetenv __P((Char *));
74 
75 struct biltins *
76 isbfunc(t)
77     struct command *t;
78 {
79     Char *cp = t->t_dcom[0];
80     struct biltins *bp, *bp1, *bp2;
81     static struct biltins label = {"", dozip, 0, 0};
82     static struct biltins foregnd = {"%job", dofg1, 0, 0};
83     static struct biltins backgnd = {"%job &", dobg1, 0, 0};
84 
85     if (lastchr(cp) == ':') {
86 	label.bname = short2str(cp);
87 	return (&label);
88     }
89     if (*cp == '%') {
90 	if (t->t_dflg & F_AMPERSAND) {
91 	    t->t_dflg &= ~F_AMPERSAND;
92 	    backgnd.bname = short2str(cp);
93 	    return (&backgnd);
94 	}
95 	foregnd.bname = short2str(cp);
96 	return (&foregnd);
97     }
98     /*
99      * Binary search Bp1 is the beginning of the current search range. Bp2 is
100      * one past the end.
101      */
102     for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2;) {
103 	int i;
104 
105 	bp = bp1 + ((bp2 - bp1) >> 1);
106 	if ((i = *cp - *bp->bname) == 0 &&
107 	    (i = Strcmp(cp, str2short(bp->bname))) == 0)
108 	    return bp;
109 	if (i < 0)
110 	    bp2 = bp;
111 	else
112 	    bp1 = bp + 1;
113     }
114     return (0);
115 }
116 
117 void
118 func(t, bp)
119     struct command *t;
120     struct biltins *bp;
121 {
122     int     i;
123 
124     xechoit(t->t_dcom);
125     setname(bp->bname);
126     i = blklen(t->t_dcom) - 1;
127     if (i < bp->minargs)
128 	stderror(ERR_NAME | ERR_TOOFEW);
129     if (i > bp->maxargs)
130 	stderror(ERR_NAME | ERR_TOOMANY);
131     (*bp->bfunct) (t->t_dcom, t);
132 }
133 
134 void
135 /*ARGSUSED*/
136 doonintr(v, t)
137     Char **v;
138     struct command *t;
139 {
140     Char *cp;
141     Char *vv = v[1];
142     sigset_t sigset;
143 
144     if (parintr == SIG_IGN)
145 	return;
146     if (setintr && intty)
147 	stderror(ERR_NAME | ERR_TERMINAL);
148     cp = gointr;
149     gointr = 0;
150     xfree((ptr_t) cp);
151     if (vv == 0) {
152 	if (setintr) {
153 	    sigemptyset(&sigset);
154 	    sigaddset(&sigset, SIGINT);
155 	    sigprocmask(SIG_BLOCK, &sigset, NULL);
156 	} else
157 	    (void) signal(SIGINT, SIG_DFL);
158 	gointr = 0;
159     }
160     else if (eq((vv = strip(vv)), STRminus)) {
161 	(void) signal(SIGINT, SIG_IGN);
162 	gointr = Strsave(STRminus);
163     }
164     else {
165 	gointr = Strsave(vv);
166 	(void) signal(SIGINT, pintr);
167     }
168 }
169 
170 void
171 /*ARGSUSED*/
172 donohup(v, t)
173     Char **v;
174     struct command *t;
175 {
176     if (intty)
177 	stderror(ERR_NAME | ERR_TERMINAL);
178     if (setintr == 0) {
179 	(void) signal(SIGHUP, SIG_IGN);
180     }
181 }
182 
183 void
184 /*ARGSUSED*/
185 dozip(v, t)
186     Char **v;
187     struct command *t;
188 {
189     ;
190 }
191 
192 void
193 prvars()
194 {
195     plist(&shvhed);
196 }
197 
198 void
199 /*ARGSUSED*/
200 doalias(v, t)
201     Char **v;
202     struct command *t;
203 {
204     struct varent *vp;
205     Char *p;
206 
207     v++;
208     p = *v++;
209     if (p == 0)
210 	plist(&aliases);
211     else if (*v == 0) {
212 	vp = adrof1(strip(p), &aliases);
213 	if (vp) {
214 	    blkpr(cshout, vp->vec);
215 	    fputc('\n', cshout);
216 	}
217     }
218     else {
219 	if (eq(p, STRalias) || eq(p, STRunalias)) {
220 	    setname(vis_str(p));
221 	    stderror(ERR_NAME | ERR_DANGER);
222 	}
223 	set1(strip(p), saveblk(v), &aliases);
224     }
225 }
226 
227 void
228 /*ARGSUSED*/
229 unalias(v, t)
230     Char **v;
231     struct command *t;
232 {
233     unset1(v, &aliases);
234 }
235 
236 void
237 /*ARGSUSED*/
238 dologout(v, t)
239     Char **v;
240     struct command *t;
241 {
242     islogin();
243     goodbye();
244 }
245 
246 void
247 /*ARGSUSED*/
248 dologin(v, t)
249     Char **v;
250     struct command *t;
251 {
252     islogin();
253     rechist();
254     (void) signal(SIGTERM, parterm);
255     (void) execl(_PATH_LOGIN, "login", short2str(v[1]), NULL);
256     untty();
257     xexit(1);
258 }
259 
260 static void
261 islogin()
262 {
263     if (chkstop == 0 && setintr)
264 	panystop(0);
265     if (loginsh)
266 	return;
267     stderror(ERR_NOTLOGIN);
268 }
269 
270 void
271 doif(v, kp)
272     Char  **v;
273     struct command *kp;
274 {
275     int i;
276     Char **vv;
277 
278     v++;
279     i = expr(&v);
280     vv = v;
281     if (*vv == NULL)
282 	stderror(ERR_NAME | ERR_EMPTYIF);
283     if (eq(*vv, STRthen)) {
284 	if (*++vv)
285 	    stderror(ERR_NAME | ERR_IMPRTHEN);
286 	setname(vis_str(STRthen));
287 	/*
288 	 * If expression was zero, then scan to else, otherwise just fall into
289 	 * following code.
290 	 */
291 	if (!i)
292 	    search(T_IF, 0, NULL);
293 	return;
294     }
295     /*
296      * Simple command attached to this if. Left shift the node in this tree,
297      * munging it so we can reexecute it.
298      */
299     if (i) {
300 	lshift(kp->t_dcom, vv - kp->t_dcom);
301 	reexecute(kp);
302 	donefds();
303     }
304 }
305 
306 /*
307  * Reexecute a command, being careful not
308  * to redo i/o redirection, which is already set up.
309  */
310 static void
311 reexecute(kp)
312     struct command *kp;
313 {
314     kp->t_dflg &= F_SAVE;
315     kp->t_dflg |= F_REPEAT;
316     /*
317      * If tty is still ours to arbitrate, arbitrate it; otherwise dont even set
318      * pgrp's as the jobs would then have no way to get the tty (we can't give
319      * it to them, and our parent wouldn't know their pgrp, etc.
320      */
321     execute(kp, (tpgrp > 0 ? tpgrp : -1), NULL, NULL);
322 }
323 
324 void
325 /*ARGSUSED*/
326 doelse(v, t)
327     Char **v;
328     struct command *t;
329 {
330     search(T_ELSE, 0, NULL);
331 }
332 
333 void
334 /*ARGSUSED*/
335 dogoto(v, t)
336     Char **v;
337     struct command *t;
338 {
339     Char   *lp;
340 
341     gotolab(lp = globone(v[1], G_ERROR));
342     xfree((ptr_t) lp);
343 }
344 
345 void
346 gotolab(lab)
347     Char *lab;
348 {
349     struct whyle *wp;
350     /*
351      * While we still can, locate any unknown ends of existing loops. This
352      * obscure code is the WORST result of the fact that we don't really parse.
353      */
354     zlast = T_GOTO;
355     for (wp = whyles; wp; wp = wp->w_next)
356 	if (wp->w_end.type == F_SEEK && wp->w_end.f_seek == 0) {
357 	    search(T_BREAK, 0, NULL);
358 	    btell(&wp->w_end);
359 	}
360 	else
361 	    bseek(&wp->w_end);
362     search(T_GOTO, 0, lab);
363     /*
364      * Eliminate loops which were exited.
365      */
366     wfree();
367 }
368 
369 void
370 /*ARGSUSED*/
371 doswitch(v, t)
372     Char **v;
373     struct command *t;
374 {
375     Char *cp, *lp;
376 
377     v++;
378     if (!*v || *(*v++) != '(')
379 	stderror(ERR_SYNTAX);
380     cp = **v == ')' ? STRNULL : *v++;
381     if (*(*v++) != ')')
382 	v--;
383     if (*v)
384 	stderror(ERR_SYNTAX);
385     search(T_SWITCH, 0, lp = globone(cp, G_ERROR));
386     xfree((ptr_t) lp);
387 }
388 
389 void
390 /*ARGSUSED*/
391 dobreak(v, t)
392     Char **v;
393     struct command *t;
394 {
395     if (whyles)
396 	toend();
397     else
398 	stderror(ERR_NAME | ERR_NOTWHILE);
399 }
400 
401 void
402 /*ARGSUSED*/
403 doexit(v, t)
404     Char **v;
405     struct command *t;
406 {
407     if (chkstop == 0 && (intty || intact) && evalvec == 0)
408 	panystop(0);
409     /*
410      * Don't DEMAND parentheses here either.
411      */
412     v++;
413     if (*v) {
414 	set(STRstatus, putn(expr(&v)));
415 	if (*v)
416 	    stderror(ERR_NAME | ERR_EXPRESSION);
417     }
418     btoeof();
419     if (intty)
420 	(void) close(SHIN);
421 }
422 
423 void
424 /*ARGSUSED*/
425 doforeach(v, t)
426     Char **v;
427     struct command *t;
428 {
429     Char *cp, *sp;
430     struct whyle *nwp;
431 
432     v++;
433     sp = cp = strip(*v);
434     if (!letter(*sp))
435 	stderror(ERR_NAME | ERR_VARBEGIN);
436     while (*cp && alnum(*cp))
437 	cp++;
438     if (*cp)
439 	stderror(ERR_NAME | ERR_VARALNUM);
440     if ((cp - sp) > MAXVARLEN)
441 	stderror(ERR_NAME | ERR_VARTOOLONG);
442     cp = *v++;
443     if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')')
444 	stderror(ERR_NAME | ERR_NOPAREN);
445     v++;
446     gflag = 0, tglob(v);
447     v = globall(v);
448     if (v == 0)
449 	stderror(ERR_NAME | ERR_NOMATCH);
450     nwp = (struct whyle *) xcalloc(1, sizeof *nwp);
451     nwp->w_fe = nwp->w_fe0 = v;
452     gargv = 0;
453     btell(&nwp->w_start);
454     nwp->w_fename = Strsave(cp);
455     nwp->w_next = whyles;
456     nwp->w_end.type = F_SEEK;
457     whyles = nwp;
458     /*
459      * Pre-read the loop so as to be more comprehensible to a terminal user.
460      */
461     zlast = T_FOREACH;
462     if (intty)
463 	preread();
464     doagain();
465 }
466 
467 void
468 /*ARGSUSED*/
469 dowhile(v, t)
470     Char **v;
471     struct command *t;
472 {
473     int status;
474     bool again = whyles != 0 && SEEKEQ(&whyles->w_start, &lineloc) &&
475     whyles->w_fename == 0;
476 
477     v++;
478     /*
479      * Implement prereading here also, taking care not to evaluate the
480      * expression before the loop has been read up from a terminal.
481      */
482     if (intty && !again)
483 	status = !exp0(&v, 1);
484     else
485 	status = !expr(&v);
486     if (*v)
487 	stderror(ERR_NAME | ERR_EXPRESSION);
488     if (!again) {
489 	struct whyle *nwp =
490 	(struct whyle *) xcalloc(1, sizeof(*nwp));
491 
492 	nwp->w_start = lineloc;
493 	nwp->w_end.type = F_SEEK;
494 	nwp->w_end.f_seek = 0;
495 	nwp->w_next = whyles;
496 	whyles = nwp;
497 	zlast = T_WHILE;
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 	sigaddset(&sigset, SIGINT);
521 	sigprocmask(SIG_UNBLOCK, &sigset, NULL);
522     }
523 
524     search(T_BREAK, 0, NULL);		/* read the expression in */
525     if (setintr)
526 	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 	sigaddset(&sigset, SIGINT);
586 	sigprocmask(SIG_BLOCK, &sigset, NULL);
587     }
588     lshift(v, 2);
589     while (i > 0) {
590 	if (setintr)
591 	    sigprocmask(SIG_UNBLOCK, &sigset, NULL);
592 	reexecute(kp);
593 	--i;
594     }
595     donefds();
596     if (setintr)
597 	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[BUFSIZ];
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 	    if (c < 0)
775 		goto past;
776 	    if (wp) {
777 		*wp++ = c;
778 		*wp = 0;	/* end the string b4 test */
779 	    }
780 	} while ((d || (!(kwd = keyword(owp)) && c != ' '
781 		  && c != '\t')) && c != '\n');
782     } while (wp == 0);
783 
784     /*
785      * if we have read a keyword ( "if", "switch" or "while" ) then we do not
786      * need to unreadc the look-ahead char
787      */
788     if (!kwd) {
789 	unreadc(c);
790 	if (found)
791 	    *--wp = 0;
792     }
793 
794     return (found);
795 
796 past:
797     switch (Stype) {
798 
799     case T_IF:
800 	stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
801 
802     case T_ELSE:
803 	stderror(ERR_NAME | ERR_NOTFOUND, "endif");
804 
805     case T_BRKSW:
806     case T_SWITCH:
807 	stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
808 
809     case T_BREAK:
810 	stderror(ERR_NAME | ERR_NOTFOUND, "end");
811 
812     case T_GOTO:
813 	setname(vis_str(Sgoal));
814 	stderror(ERR_NAME | ERR_NOTFOUND, "label");
815     }
816     /* NOTREACHED */
817     return (0);
818 }
819 
820 /*
821  * keyword(wp) determines if wp is one of the built-n functions if,
822  * switch or while. It seems that when an if statement looks like
823  * "if(" then getword above sucks in the '(' and so the search routine
824  * never finds what it is scanning for. Rather than rewrite doword, I hack
825  * in a test to see if the string forms a keyword. Then doword stops
826  * and returns the word "if" -strike
827  */
828 
829 static int
830 keyword(wp)
831     Char   *wp;
832 {
833     static Char STRif[] = {'i', 'f', '\0'};
834     static Char STRwhile[] = {'w', 'h', 'i', 'l', 'e', '\0'};
835     static Char STRswitch[] = {'s', 'w', 'i', 't', 'c', 'h', '\0'};
836 
837     if (!wp)
838 	return (0);
839 
840     if ((Strcmp(wp, STRif) == 0) || (Strcmp(wp, STRwhile) == 0)
841 	|| (Strcmp(wp, STRswitch) == 0))
842 	return (1);
843 
844     return (0);
845 }
846 
847 static void
848 toend()
849 {
850     if (whyles->w_end.type == F_SEEK && whyles->w_end.f_seek == 0) {
851 	search(T_BREAK, 0, NULL);
852 	btell(&whyles->w_end);
853 	whyles->w_end.f_seek--;
854     }
855     else
856 	bseek(&whyles->w_end);
857     wfree();
858 }
859 
860 void
861 wfree()
862 {
863     struct Ain    o;
864     struct whyle *nwp;
865 
866     btell(&o);
867 
868     for (; whyles; whyles = nwp) {
869 	struct whyle *wp = whyles;
870 	nwp = wp->w_next;
871 
872 	/*
873 	 * We free loops that have different seek types.
874 	 */
875 	if (wp->w_end.type != I_SEEK && wp->w_start.type == wp->w_end.type &&
876 	    wp->w_start.type == o.type) {
877 	    if (wp->w_end.type == F_SEEK) {
878 		if (o.f_seek >= wp->w_start.f_seek &&
879 		    (wp->w_end.f_seek == 0 || o.f_seek < wp->w_end.f_seek))
880 		    break;
881 	    }
882 	    else {
883 		if (o.a_seek >= wp->w_start.a_seek &&
884 		    (wp->w_end.a_seek == 0 || o.a_seek < wp->w_end.a_seek))
885 		    break;
886 	    }
887 	}
888 
889 	if (wp->w_fe0)
890 	    blkfree(wp->w_fe0);
891 	if (wp->w_fename)
892 	    xfree((ptr_t) wp->w_fename);
893 	xfree((ptr_t) wp);
894     }
895 }
896 
897 void
898 /*ARGSUSED*/
899 doecho(v, t)
900     Char **v;
901     struct command *t;
902 {
903     xecho(' ', v);
904 }
905 
906 void
907 /*ARGSUSED*/
908 doglob(v, t)
909     Char **v;
910     struct command *t;
911 {
912     xecho(0, v);
913     (void) fflush(cshout);
914 }
915 
916 static void
917 xecho(sep, v)
918     int    sep;
919     Char **v;
920 {
921     Char *cp;
922     int     nonl = 0;
923     sigset_t sigset;
924 
925     if (setintr) {
926 	sigemptyset(&sigset);
927 	sigaddset(&sigset, SIGINT);
928 	sigprocmask(SIG_UNBLOCK, &sigset, NULL);
929     }
930     v++;
931     if (*v == 0)
932 	return;
933     gflag = 0, tglob(v);
934     if (gflag) {
935 	v = globall(v);
936 	if (v == 0)
937 	    stderror(ERR_NAME | ERR_NOMATCH);
938     }
939     else {
940 	v = gargv = saveblk(v);
941 	trim(v);
942     }
943     if (sep == ' ' && *v && eq(*v, STRmn))
944 	nonl++, v++;
945     while ((cp = *v++) != NULL) {
946 	int c;
947 
948 	while ((c = *cp++) != '\0')
949 	    (void) vis_fputc(c | QUOTE, cshout);
950 
951 	if (*v)
952 	    (void) vis_fputc(sep | QUOTE, cshout);
953     }
954     if (sep && nonl == 0)
955 	(void) fputc('\n', cshout);
956     else
957 	(void) fflush(cshout);
958     if (setintr)
959 	sigprocmask(SIG_BLOCK, &sigset, NULL);
960     if (gargv)
961 	blkfree(gargv), gargv = 0;
962 }
963 
964 void
965 /*ARGSUSED*/
966 dosetenv(v, t)
967     Char **v;
968     struct command *t;
969 {
970     Char   *vp, *lp;
971     sigset_t sigset;
972 
973     v++;
974     if ((vp = *v++) == 0) {
975 	Char **ep;
976 
977 	if (setintr) {
978 	    sigemptyset(&sigset);
979 	    sigaddset(&sigset, SIGINT);
980 	    sigprocmask(SIG_UNBLOCK, &sigset, NULL);
981 	}
982 	for (ep = STR_environ; *ep; ep++)
983 	    (void) fprintf(cshout, "%s\n", vis_str(*ep));
984 	return;
985     }
986     if ((lp = *v++) == 0)
987 	lp = STRNULL;
988     Setenv(vp, lp = globone(lp, G_APPEND));
989     if (eq(vp, STRPATH)) {
990 	importpath(lp);
991 	dohash(NULL, NULL);
992     }
993     else if (eq(vp, STRLANG) || eq(vp, STRLC_CTYPE)) {
994 #ifdef NLS
995 	int     k;
996 
997 	(void) setlocale(LC_ALL, "");
998 	for (k = 0200; k <= 0377 && !Isprint(k); k++)
999 		continue;
1000 	AsciiOnly = k > 0377;
1001 #else
1002 	AsciiOnly = 0;
1003 #endif				/* NLS */
1004     }
1005     xfree((ptr_t) lp);
1006 }
1007 
1008 void
1009 /*ARGSUSED*/
1010 dounsetenv(v, t)
1011     Char **v;
1012     struct command *t;
1013 {
1014     Char  **ep, *p, *n;
1015     int     i, maxi;
1016     static Char *name = NULL;
1017 
1018     if (name)
1019 	xfree((ptr_t) name);
1020     /*
1021      * Find the longest environment variable
1022      */
1023     for (maxi = 0, ep = STR_environ; *ep; ep++) {
1024 	for (i = 0, p = *ep; *p && *p != '='; p++, i++)
1025 	    continue;
1026 	if (i > maxi)
1027 	    maxi = i;
1028     }
1029 
1030     name = (Char *) xmalloc((size_t) (maxi + 1) * sizeof(Char));
1031 
1032     while (++v && *v)
1033 	for (maxi = 1; maxi;)
1034 	    for (maxi = 0, ep = STR_environ; *ep; ep++) {
1035 		for (n = name, p = *ep; *p && *p != '='; *n++ = *p++)
1036 		    continue;
1037 		*n = '\0';
1038 		if (!Gmatch(name, *v))
1039 		    continue;
1040 		maxi = 1;
1041 		if (eq(name, STRLANG) || eq(name, STRLC_CTYPE)) {
1042 #ifdef NLS
1043 		    int     k;
1044 
1045 		    (void) setlocale(LC_ALL, "");
1046 		    for (k = 0200; k <= 0377 && !Isprint(k); k++)
1047 			continue;
1048 		    AsciiOnly = k > 0377;
1049 #else
1050 		    AsciiOnly = getenv("LANG") == NULL &&
1051 			getenv("LC_CTYPE") == NULL;
1052 #endif				/* NLS */
1053 		}
1054 		/*
1055 		 * Delete name, and start again cause the environment changes
1056 		 */
1057 		Unsetenv(name);
1058 		break;
1059 	    }
1060     xfree((ptr_t) name);
1061     name = NULL;
1062 }
1063 
1064 void
1065 Setenv(name, val)
1066     Char   *name, *val;
1067 {
1068     Char **ep = STR_environ;
1069     Char *cp, *dp;
1070     Char   *blk[2];
1071     Char  **oep = ep;
1072 
1073 
1074     for (; *ep; ep++) {
1075 	for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
1076 	    continue;
1077 	if (*cp != 0 || *dp != '=')
1078 	    continue;
1079 	cp = Strspl(STRequal, val);
1080 	xfree((ptr_t) * ep);
1081 	*ep = strip(Strspl(name, cp));
1082 	xfree((ptr_t) cp);
1083 	blkfree((Char **) environ);
1084 	environ = short2blk(STR_environ);
1085 	return;
1086     }
1087     cp = Strspl(name, STRequal);
1088     blk[0] = strip(Strspl(cp, val));
1089     xfree((ptr_t) cp);
1090     blk[1] = 0;
1091     STR_environ = blkspl(STR_environ, blk);
1092     blkfree((Char **) environ);
1093     environ = short2blk(STR_environ);
1094     xfree((ptr_t) oep);
1095 }
1096 
1097 static void
1098 Unsetenv(name)
1099     Char   *name;
1100 {
1101     Char **ep = STR_environ;
1102     Char *cp, *dp;
1103     Char  **oep = ep;
1104 
1105     for (; *ep; ep++) {
1106 	for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
1107 	    continue;
1108 	if (*cp != 0 || *dp != '=')
1109 	    continue;
1110 	cp = *ep;
1111 	*ep = 0;
1112 	STR_environ = blkspl(STR_environ, ep + 1);
1113 	environ = short2blk(STR_environ);
1114 	*ep = cp;
1115 	xfree((ptr_t) cp);
1116 	xfree((ptr_t) oep);
1117 	return;
1118     }
1119 }
1120 
1121 void
1122 /*ARGSUSED*/
1123 doumask(v, t)
1124     Char **v;
1125     struct command *t;
1126 {
1127     Char *cp = v[1];
1128     int i;
1129 
1130     if (cp == 0) {
1131 	i = umask(0);
1132 	(void) umask(i);
1133 	(void) fprintf(cshout, "%o\n", i);
1134 	return;
1135     }
1136     i = 0;
1137     while (Isdigit(*cp) && *cp != '8' && *cp != '9')
1138 	i = i * 8 + *cp++ - '0';
1139     if (*cp || i < 0 || i > 0777)
1140 	stderror(ERR_NAME | ERR_MASK);
1141     (void) umask(i);
1142 }
1143 
1144 typedef quad_t RLIM_TYPE;
1145 
1146 static struct limits {
1147     int     limconst;
1148     char   *limname;
1149     int     limdiv;
1150     char   *limscale;
1151 }       limits[] = {
1152     { RLIMIT_CPU,	"cputime",	1,	"seconds" },
1153     { RLIMIT_FSIZE,	"filesize",	1024,	"kbytes" },
1154     { RLIMIT_DATA,	"datasize",	1024,	"kbytes" },
1155     { RLIMIT_STACK,	"stacksize",	1024,	"kbytes" },
1156     { RLIMIT_CORE,	"coredumpsize", 1024,	"kbytes" },
1157     { RLIMIT_RSS,	"memoryuse",	1024,	"kbytes" },
1158     { RLIMIT_MEMLOCK,	"memorylocked",	1024,	"kbytes" },
1159     { RLIMIT_NPROC,	"maxproc",	1,	"" },
1160     { RLIMIT_NOFILE,	"openfiles",	1,	"" },
1161     { -1,		NULL,		0,	NULL }
1162 };
1163 
1164 static struct limits *findlim __P((Char *));
1165 static RLIM_TYPE getval __P((struct limits *, Char **));
1166 static void limtail __P((Char *, char *));
1167 static void plim __P((struct limits *, Char));
1168 static int setlim __P((struct limits *, Char, RLIM_TYPE));
1169 
1170 static struct limits *
1171 findlim(cp)
1172     Char   *cp;
1173 {
1174     struct limits *lp, *res;
1175 
1176     res = (struct limits *) NULL;
1177     for (lp = limits; lp->limconst >= 0; lp++)
1178 	if (prefix(cp, str2short(lp->limname))) {
1179 	    if (res)
1180 		stderror(ERR_NAME | ERR_AMBIG);
1181 	    res = lp;
1182 	}
1183     if (res)
1184 	return (res);
1185     stderror(ERR_NAME | ERR_LIMIT);
1186     /* NOTREACHED */
1187     return (0);
1188 }
1189 
1190 void
1191 /*ARGSUSED*/
1192 dolimit(v, t)
1193     Char **v;
1194     struct command *t;
1195 {
1196     struct limits *lp;
1197     RLIM_TYPE limit;
1198     char    hard = 0;
1199 
1200     v++;
1201     if (*v && eq(*v, STRmh)) {
1202 	hard = 1;
1203 	v++;
1204     }
1205     if (*v == 0) {
1206 	for (lp = limits; lp->limconst >= 0; lp++)
1207 	    plim(lp, hard);
1208 	return;
1209     }
1210     lp = findlim(v[0]);
1211     if (v[1] == 0) {
1212 	plim(lp, hard);
1213 	return;
1214     }
1215     limit = getval(lp, v + 1);
1216     if (setlim(lp, hard, limit) < 0)
1217 	stderror(ERR_SILENT);
1218 }
1219 
1220 static  RLIM_TYPE
1221 getval(lp, v)
1222     struct limits *lp;
1223     Char  **v;
1224 {
1225     float f;
1226     double  atof();
1227     Char   *cp = *v++;
1228 
1229     f = atof(short2str(cp));
1230 
1231     while (Isdigit(*cp) || *cp == '.' || *cp == 'e' || *cp == 'E')
1232 	cp++;
1233     if (*cp == 0) {
1234 	if (*v == 0)
1235 	    return ((RLIM_TYPE) ((f + 0.5) * lp->limdiv));
1236 	cp = *v;
1237     }
1238     switch (*cp) {
1239     case ':':
1240 	if (lp->limconst != RLIMIT_CPU)
1241 	    goto badscal;
1242 	return ((RLIM_TYPE) (f * 60.0 + atof(short2str(cp + 1))));
1243     case 'h':
1244 	if (lp->limconst != RLIMIT_CPU)
1245 	    goto badscal;
1246 	limtail(cp, "hours");
1247 	f *= 3600.0;
1248 	break;
1249     case 'm':
1250 	if (lp->limconst == RLIMIT_CPU) {
1251 	    limtail(cp, "minutes");
1252 	    f *= 60.0;
1253 	    break;
1254 	}
1255 	*cp = 'm';
1256 	limtail(cp, "megabytes");
1257 	f *= 1024.0 * 1024.0;
1258 	break;
1259     case 's':
1260 	if (lp->limconst != RLIMIT_CPU)
1261 	    goto badscal;
1262 	limtail(cp, "seconds");
1263 	break;
1264     case 'M':
1265 	if (lp->limconst == RLIMIT_CPU)
1266 	    goto badscal;
1267 	*cp = 'm';
1268 	limtail(cp, "megabytes");
1269 	f *= 1024.0 * 1024.0;
1270 	break;
1271     case 'k':
1272 	if (lp->limconst == RLIMIT_CPU)
1273 	    goto badscal;
1274 	limtail(cp, "kbytes");
1275 	f *= 1024.0;
1276 	break;
1277     case 'u':
1278 	limtail(cp, "unlimited");
1279 	return (RLIM_INFINITY);
1280     default:
1281 badscal:
1282 	stderror(ERR_NAME | ERR_SCALEF);
1283     }
1284     f += 0.5;
1285     if (f > (float) RLIM_INFINITY)
1286 	return RLIM_INFINITY;
1287     else
1288 	return ((RLIM_TYPE) f);
1289 }
1290 
1291 static void
1292 limtail(cp, str)
1293     Char   *cp;
1294     char   *str;
1295 {
1296     while (*cp && *cp == *str)
1297 	cp++, str++;
1298     if (*cp)
1299 	stderror(ERR_BADSCALE, str);
1300 }
1301 
1302 
1303 /*ARGSUSED*/
1304 static void
1305 plim(lp, hard)
1306     struct limits *lp;
1307     Char    hard;
1308 {
1309     struct rlimit rlim;
1310     RLIM_TYPE limit;
1311 
1312     (void) fprintf(cshout, "%s \t", lp->limname);
1313 
1314     (void) getrlimit(lp->limconst, &rlim);
1315     limit = hard ? rlim.rlim_max : rlim.rlim_cur;
1316 
1317     if (limit == RLIM_INFINITY)
1318 	(void) fprintf(cshout, "unlimited");
1319     else if (lp->limconst == RLIMIT_CPU)
1320 	psecs((long) limit);
1321     else
1322 	(void) fprintf(cshout, "%ld %s", (long) (limit / lp->limdiv),
1323 		       lp->limscale);
1324     (void) fputc('\n', cshout);
1325 }
1326 
1327 void
1328 /*ARGSUSED*/
1329 dounlimit(v, t)
1330     Char **v;
1331     struct command *t;
1332 {
1333     struct limits *lp;
1334     int     lerr = 0;
1335     Char    hard = 0;
1336 
1337     v++;
1338     if (*v && eq(*v, STRmh)) {
1339 	hard = 1;
1340 	v++;
1341     }
1342     if (*v == 0) {
1343 	for (lp = limits; lp->limconst >= 0; lp++)
1344 	    if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0)
1345 		lerr++;
1346 	if (lerr)
1347 	    stderror(ERR_SILENT);
1348 	return;
1349     }
1350     while (*v) {
1351 	lp = findlim(*v++);
1352 	if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0)
1353 	    stderror(ERR_SILENT);
1354     }
1355 }
1356 
1357 static int
1358 setlim(lp, hard, limit)
1359     struct limits *lp;
1360     Char    hard;
1361     RLIM_TYPE limit;
1362 {
1363     struct rlimit rlim;
1364 
1365     (void) getrlimit(lp->limconst, &rlim);
1366 
1367     if (hard)
1368 	rlim.rlim_max = limit;
1369     else if (limit == RLIM_INFINITY && geteuid() != 0)
1370 	rlim.rlim_cur = rlim.rlim_max;
1371     else
1372 	rlim.rlim_cur = limit;
1373 
1374     if (setrlimit(lp->limconst, &rlim) < 0) {
1375 	(void) fprintf(csherr, "%s: %s: Can't %s%s limit\n", bname, lp->limname,
1376 		       limit == RLIM_INFINITY ? "remove" : "set",
1377 		       hard ? " hard" : "");
1378 	return (-1);
1379     }
1380     return (0);
1381 }
1382 
1383 void
1384 /*ARGSUSED*/
1385 dosuspend(v, t)
1386     Char **v;
1387     struct command *t;
1388 {
1389     int     ctpgrp;
1390 
1391     void    (*old) ();
1392 
1393     if (loginsh)
1394 	stderror(ERR_SUSPLOG);
1395     untty();
1396 
1397     old = signal(SIGTSTP, SIG_DFL);
1398     (void) kill(0, SIGTSTP);
1399     /* the shell stops here */
1400     (void) signal(SIGTSTP, old);
1401 
1402     if (tpgrp != -1) {
1403 retry:
1404 	ctpgrp = tcgetpgrp(FSHTTY);
1405 	if  (ctpgrp != opgrp) {
1406 	    old = signal(SIGTTIN, SIG_DFL);
1407 	    (void) kill(0, SIGTTIN);
1408 	    (void) signal(SIGTTIN, old);
1409 	    goto retry;
1410 	}
1411 	(void) setpgid(0, shpgrp);
1412 	(void) tcsetpgrp(FSHTTY, shpgrp);
1413     }
1414 }
1415 
1416 /* This is the dreaded EVAL built-in.
1417  *   If you don't fiddle with file descriptors, and reset didfds,
1418  *   this command will either ignore redirection inside or outside
1419  *   its aguments, e.g. eval "date >x"  vs.  eval "date" >x
1420  *   The stuff here seems to work, but I did it by trial and error rather
1421  *   than really knowing what was going on.  If tpgrp is zero, we are
1422  *   probably a background eval, e.g. "eval date &", and we want to
1423  *   make sure that any processes we start stay in our pgrp.
1424  *   This is also the case for "time eval date" -- stay in same pgrp.
1425  *   Otherwise, under stty tostop, processes will stop in the wrong
1426  *   pgrp, with no way for the shell to get them going again.  -IAN!
1427  */
1428 static Char **gv = NULL;
1429 void
1430 /*ARGSUSED*/
1431 doeval(v, t)
1432     Char **v;
1433     struct command *t;
1434 {
1435     Char  **oevalvec;
1436     Char   *oevalp;
1437     int     odidfds;
1438     jmp_buf osetexit;
1439     int     my_reenter;
1440     Char  **savegv = gv;
1441     int     saveIN;
1442     int     saveOUT;
1443     int     saveERR;
1444     int     oSHIN;
1445     int     oSHOUT;
1446     int     oSHERR;
1447 
1448     UNREGISTER(v);
1449 
1450     oevalvec = evalvec;
1451     oevalp = evalp;
1452     odidfds = didfds;
1453     oSHIN = SHIN;
1454     oSHOUT = SHOUT;
1455     oSHERR = SHERR;
1456 
1457     v++;
1458     if (*v == 0)
1459 	return;
1460     gflag = 0, tglob(v);
1461     if (gflag) {
1462 	gv = v = globall(v);
1463 	gargv = 0;
1464 	if (v == 0)
1465 	    stderror(ERR_NOMATCH);
1466 	v = copyblk(v);
1467     }
1468     else {
1469 	gv = NULL;
1470 	v = copyblk(v);
1471 	trim(v);
1472     }
1473 
1474     saveIN = dcopy(SHIN, -1);
1475     saveOUT = dcopy(SHOUT, -1);
1476     saveERR = dcopy(SHERR, -1);
1477 
1478     getexit(osetexit);
1479 
1480     if ((my_reenter = setexit()) == 0) {
1481 	evalvec = v;
1482 	evalp = 0;
1483 	SHIN = dcopy(0, -1);
1484 	SHOUT = dcopy(1, -1);
1485 	SHERR = dcopy(2, -1);
1486 	didfds = 0;
1487 	process(0);
1488     }
1489 
1490     evalvec = oevalvec;
1491     evalp = oevalp;
1492     doneinp = 0;
1493     didfds = odidfds;
1494     (void) close(SHIN);
1495     (void) close(SHOUT);
1496     (void) close(SHERR);
1497     SHIN = dmove(saveIN, oSHIN);
1498     SHOUT = dmove(saveOUT, oSHOUT);
1499     SHERR = dmove(saveERR, oSHERR);
1500     if (gv)
1501 	blkfree(gv), gv = NULL;
1502     resexit(osetexit);
1503     gv = savegv;
1504     if (my_reenter)
1505 	stderror(ERR_SILENT);
1506 }
1507 
1508 void
1509 /*ARGSUSED*/
1510 doprintf(v, t)
1511     Char **v;
1512     struct command *t;
1513 {
1514     char **c;
1515     extern int progprintf __P((int, char **));
1516     int ret;
1517 
1518     ret = progprintf(blklen(v), c = short2blk(v));
1519     (void) fflush(cshout);
1520     (void) fflush(csherr);
1521 
1522     blkfree((Char **) c);
1523     if (ret)
1524 	stderror(ERR_SILENT);
1525 }
1526