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