xref: /titanic_44/usr/src/cmd/csh/sh.func.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
1 /*
2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved.  The Berkeley Software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include "sh.h"
18 #include <locale.h>	/* For LC_ALL */
19 #include "sh.tconst.h"
20 #include <sys/types.h>
21 #include <stdlib.h>
22 
23 /*
24  * N.B.: Some of the limits change from SunOS 4.x to SunOS 5.0.  In
25  * particular, RLIMIT_RSS is gone and RLIMIT_VMEM is new.  Beware of consusing
26  * the keywords that the command prints for these two.  The old one was
27  * "memoryuse" and the new one is "memorysize".  Note also that a given limit
28  * doesn't necessarily appear in the same position in the two releases.
29  */
30 struct limits {
31 	int	limconst;
32 	tchar *limname;
33 	int	limdiv;
34 	tchar *limscale;
35 } limits[] = {
36 	RLIMIT_CPU,	S_cputime,	/* "cputime" */
37 		1,	S_seconds,	/* "seconds" */
38 	RLIMIT_FSIZE,	S_filesize,	/* "filesize" */
39 		1024,	S_kbytes,	/* "kbytes" */
40 	RLIMIT_DATA,	S_datasize,	/* "datasize" */
41 		1024,	S_kbytes,	/* "kbytes" */
42 	RLIMIT_STACK,	S_stacksize,	/* "stacksize" */
43 		1024,	S_kbytes,	/* "kbytes" */
44 	RLIMIT_CORE,	S_coredumpsize, /* "coredumpsize" */
45 		1024,	S_kbytes,	/* "kbytes" */
46 	RLIMIT_NOFILE,	S_descriptors,	/* "descriptors" */
47 		1,	S_,		/* "" */
48 	RLIMIT_VMEM,	S_memorysize,	/* "memorysize" */
49 		1024,	S_kbytes,	/* "kbytes" */
50 	-1,		0,
51 };
52 
53 static int getval(struct limits *lp, tchar **v, rlim_t *);
54 void islogin();
55 int dolabel();
56 void reexecute(struct command *kp);
57 void preread_();
58 void doagain();
59 void toend();
60 void wfree();
61 void echo(tchar sep, tchar **v);
62 void local_setenv(tchar *name, tchar *val);
63 void local_unsetenv(tchar *name);
64 void limtail(tchar *cp, tchar *str0);
65 void plim(struct limits *lp, tchar hard);
66 void search();
67 
68 #define	BUFSZ	1028
69 
70 /*
71  * C shell
72  */
73 
74 struct
75 biltins *
76 isbfunc(struct command *t)
77 {
78 	tchar *cp = t->t_dcom[0];
79 	struct biltins *bp, *bp1, *bp2;
80 	int dofg1(), dobg1();
81 
82 	static struct biltins label = { S_, dolabel, 0, 0 };
83 	static struct biltins foregnd = { S_Pjob, dofg1, 0, 0 };
84 	static struct biltins backgnd = { S_PjobAND, dobg1, 0, 0 };
85 #ifdef TRACE
86 	tprintf("TRACE- isbfunc()\n");
87 #endif
88 	if (lastchr(cp) == ':') {
89 		label.bname = cp;
90 		return (&label);
91 	}
92 	if (*cp == '%') {
93 		if (t->t_dflg & FAND) {
94 			t->t_dflg &= ~FAND;
95 			backgnd.bname = cp;
96 			return (&backgnd);
97 		}
98 		foregnd.bname = cp;
99 		return (&foregnd);
100 	}
101 	/*
102 	 * Binary search
103 	 * Bp1 is the beginning of the current search range.
104 	 * Bp2 is one past the end.
105 	 */
106 	for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2; ) {
107 		int i;
108 
109 		bp = bp1 + (bp2 - bp1 >> 1);
110 		if ((i = *cp - *bp->bname) == 0 &&
111 		    (i = strcmp_(cp, bp->bname)) == 0) {
112 			return (bp);
113 		}
114 		if (i < 0) {
115 			bp2 = bp;
116 		} else {
117 			bp1 = bp + 1;
118 		}
119 	}
120 	return (0);
121 }
122 
123 void
124 func(struct command *t, struct biltins *bp)
125 {
126 	int i;
127 
128 #ifdef TRACE
129 	tprintf("TRACE- func()\n");
130 #endif
131 	xechoit(t->t_dcom);
132 	setname(bp->bname);
133 	i = blklen(t->t_dcom) - 1;
134 	if (i < bp->minargs) {
135 		bferr("Too few arguments");
136 	}
137 	if (i > bp->maxargs) {
138 		bferr("Too many arguments");
139 	}
140 	(*bp->bfunct)(t->t_dcom, t);
141 }
142 
143 int
144 dolabel()
145 {
146 #ifdef TRACE
147 	tprintf("TRACE- dolabel()\n");
148 #endif
149 
150 }
151 
152 void
153 doonintr(tchar **v)
154 {
155 	tchar *cp;
156 	tchar *vv = v[1];
157 
158 #ifdef TRACE
159 	tprintf("TRACE- doonintr()\n");
160 #endif
161 	if (parintr == SIG_IGN) {
162 		return;
163 	}
164 	if (setintr && intty) {
165 		bferr("Can't from terminal");
166 	}
167 	cp = gointr, gointr = 0, xfree(cp);
168 	if (vv == 0) {
169 		if (setintr) {
170 			(void) sigblock(sigmask(SIGINT));
171 		} else {
172 			(void) signal(SIGINT, SIG_DFL);
173 		}
174 		gointr = 0;
175 	} else if (eq((vv = strip(vv)), S_MINUS)) {
176 		(void) signal(SIGINT, SIG_IGN);
177 		gointr = S_MINUS;
178 	} else {
179 		gointr = savestr(vv);
180 		(void) signal(SIGINT, pintr);
181 	}
182 }
183 
184 void
185 donohup()
186 {
187 
188 #ifdef TRACE
189 	tprintf("TRACE- donohup()\n");
190 #endif
191 	if (intty) {
192 		bferr("Can't from terminal");
193 	}
194 	if (setintr == 0) {
195 		(void) signal(SIGHUP, SIG_IGN);
196 #ifdef CC
197 		submit(getpid());
198 #endif
199 	}
200 }
201 
202 void
203 dozip()
204 {
205 	;
206 }
207 
208 void
209 prvars()
210 {
211 #ifdef TRACE
212 	tprintf("TRACE- prvars()\n");
213 #endif
214 
215 	plist(&shvhed);
216 }
217 
218 void
219 doalias(tchar **v)
220 {
221 	struct varent *vp;
222 	tchar *p;
223 
224 #ifdef TRACE
225 	tprintf("TRACE- doalias()\n");
226 #endif
227 	v++;
228 	p = *v++;
229 	if (p == 0) {
230 		plist(&aliases);
231 	} else if (*v == 0) {
232 		vp = adrof1(strip(p), &aliases);
233 		if (vp) {
234 			blkpr(vp->vec), printf("\n");
235 		}
236 	} else {
237 		if (eq(p, S_alias) ||
238 		    eq(p, S_unalias)) {
239 			setname(p);
240 			bferr("Too dangerous to alias that");
241 		}
242 		set1(strip(p), saveblk(v), &aliases);
243 	}
244 }
245 
246 void
247 unalias(tchar **v)
248 {
249 
250 #ifdef TRACE
251 	tprintf("TRACE- unalias()\n");
252 #endif
253 	unset1(v, &aliases);
254 }
255 
256 void
257 dologout()
258 {
259 
260 #ifdef TRACE
261 	tprintf("TRACE- dologout()\n");
262 #endif
263 	islogin();
264 	goodbye();
265 }
266 
267 void
268 dologin(tchar **v)
269 {
270 
271 	char *v_;	/* work */
272 #ifdef TRACE
273 	tprintf("TRACE- dologin()\n");
274 #endif
275 	islogin();
276 	rechist();
277 	(void) signal(SIGTERM, parterm);
278 	if (v[1] != NULL) {
279 		v_ = tstostr(NULL, v[1]);	/* No need to free */
280 	} else {
281 		v_ = 0;
282 	}
283 	execl("/bin/login", "login", v_, 0);
284 	untty();
285 	exit(1);
286 }
287 
288 #ifdef NEWGRP
289 void
290 donewgrp(tchar **v)
291 {
292 
293 	char *v_;	/* work */
294 #ifdef TRACE
295 	tprintf("TRACE- donewgrp()\n");
296 #endif
297 	if (chkstop == 0 && setintr) {
298 		panystop(0);
299 	}
300 	(void) signal(SIGTERM, parterm);
301 
302 	if (v[1] != NULL) {
303 		v_ = tstostr(NOSTR, v[1]);	/* No need to free */
304 	} else {
305 		v_ = 0;
306 	}
307 	execl("/bin/newgrp", "newgrp", v_, 0);
308 	execl("/usr/bin/newgrp", "newgrp", v_, 0);
309 	untty();
310 	exit(1);
311 }
312 #endif
313 
314 void
315 islogin()
316 {
317 
318 #ifdef TRACE
319 	tprintf("TRACE- islogin()\n");
320 #endif
321 	if (chkstop == 0 && setintr) {
322 		panystop(0);
323 	}
324 	if (loginsh) {
325 		return;
326 	}
327 	error("Not login shell");
328 }
329 
330 void
331 doif(tchar **v, struct command *kp)
332 {
333 	int i;
334 	tchar **vv;
335 
336 #ifdef TRACE
337 	tprintf("TRACE- doif()\n");
338 #endif
339 	v++;
340 	i = exp(&v);
341 	vv = v;
342 	if (*vv == NOSTR) {
343 		bferr("Empty if");
344 	}
345 	if (eq(*vv, S_then)) {
346 		if (*++vv) {
347 			bferr("Improper then");
348 		}
349 		setname(S_then);
350 		/*
351 		 * If expression was zero, then scan to else,
352 		 * otherwise just fall into following code.
353 		 */
354 		if (!i) {
355 			search(ZIF, 0);
356 		}
357 		return;
358 	}
359 	/*
360 	 * Simple command attached to this if.
361 	 * Left shift the node in this tree, munging it
362 	 * so we can reexecute it.
363 	 */
364 	if (i) {
365 		lshift(kp->t_dcom, vv - kp->t_dcom);
366 		reexecute(kp);
367 		donefds();
368 	}
369 }
370 
371 /*
372  * Reexecute a command, being careful not
373  * to redo i/o redirection, which is already set up.
374  */
375 void
376 reexecute(struct command *kp)
377 {
378 
379 #ifdef TRACE
380 	tprintf("TRACE- reexecute()\n");
381 #endif
382 	kp->t_dflg &= FSAVE;
383 	kp->t_dflg |= FREDO;
384 	/*
385 	 * If tty is still ours to arbitrate, arbitrate it;
386 	 * otherwise dont even set pgrp's as the jobs would
387 	 * then have no way to get the tty (we can't give it
388 	 * to them, and our parent wouldn't know their pgrp, etc.
389 	 */
390 	execute(kp, tpgrp > 0 ? tpgrp : -1);
391 }
392 
393 void
394 doelse()
395 {
396 
397 #ifdef TRACE
398 	tprintf("TRACE- doelse()\n");
399 #endif
400 	search(ZELSE, 0);
401 }
402 
403 void
404 dogoto(tchar **v)
405 {
406 	struct whyle *wp;
407 	tchar *lp;
408 #ifdef TRACE
409 	tprintf("TRACE- dogoto()\n");
410 #endif
411 
412 	/*
413 	 * While we still can, locate any unknown ends of existing loops.
414 	 * This obscure code is the WORST result of the fact that we
415 	 * don't really parse.
416 	 */
417 	for (wp = whyles; wp; wp = wp->w_next) {
418 		if (wp->w_end == 0) {
419 			search(ZBREAK, 0);
420 			wp->w_end = btell();
421 		} else {
422 			bseek(wp->w_end);
423 		}
424 	}
425 	search(ZGOTO, 0, lp = globone(v[1]));
426 	xfree(lp);
427 	/*
428 	 * Eliminate loops which were exited.
429 	 */
430 	wfree();
431 }
432 
433 void
434 doswitch(tchar **v)
435 {
436 	tchar *cp, *lp;
437 
438 #ifdef TRACE
439 	tprintf("TRACE- doswitch()\n");
440 #endif
441 	v++;
442 	if (!*v || *(*v++) != '(') {
443 		goto syntax;
444 	}
445 	cp = **v == ')' ? S_ : *v++;
446 	if (*(*v++) != ')') {
447 		v--;
448 	}
449 	if (*v) {
450 syntax:
451 		error("Syntax error");
452 	}
453 	search(ZSWITCH, 0, lp = globone(cp));
454 	xfree(lp);
455 }
456 
457 void
458 dobreak()
459 {
460 
461 #ifdef TRACE
462 	tprintf("TRACE- dobreak()\n");
463 #endif
464 	if (whyles) {
465 		toend();
466 	} else {
467 		bferr("Not in while/foreach");
468 	}
469 }
470 
471 void
472 doexit(tchar **v)
473 {
474 
475 #ifdef TRACE
476 	tprintf("TRACE- doexit()\n");
477 #endif
478 	if (chkstop == 0) {
479 		panystop(0);
480 	}
481 	/*
482 	 * Don't DEMAND parentheses here either.
483 	 */
484 	v++;
485 	if (*v) {
486 		set(S_status, putn(exp(&v)));
487 		if (*v) {
488 			bferr("Expression syntax");
489 		}
490 	}
491 	btoeof();
492 	if (intty) {
493 		(void) close(SHIN);
494 		unsetfd(SHIN);
495 	}
496 }
497 
498 void
499 doforeach(tchar **v)
500 {
501 	tchar *cp;
502 	struct whyle *nwp;
503 
504 #ifdef TRACE
505 	tprintf("TRACE- doforeach()\n");
506 #endif
507 	v++;
508 	cp = strip(*v);
509 	while (*cp && alnum(*cp)) {
510 		cp++;
511 	}
512 	if (*cp || strlen_(*v) >= 20 || !letter(**v)) {
513 		bferr("Invalid variable");
514 	}
515 	cp = *v++;
516 	if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')') {
517 		bferr("Words not ()'ed");
518 	}
519 	v++;
520 	gflag = 0, tglob(v);
521 	v = glob(v);
522 	if (v == 0) {
523 		bferr("No match");
524 	}
525 	nwp = (struct whyle *)calloc(1, sizeof (*nwp));
526 	nwp->w_fe = nwp->w_fe0 = v; gargv = 0;
527 	nwp->w_start = btell();
528 	nwp->w_fename = savestr(cp);
529 	nwp->w_next = whyles;
530 	whyles = nwp;
531 	/*
532 	 * Pre-read the loop so as to be more
533 	 * comprehensible to a terminal user.
534 	 */
535 	if (intty) {
536 		preread_();
537 	}
538 	doagain();
539 }
540 
541 void
542 dowhile(tchar **v)
543 {
544 	int status;
545 	bool again = whyles != 0 && whyles->w_start == lineloc &&
546 	    whyles->w_fename == 0;
547 
548 #ifdef TRACE
549 	tprintf("TRACE- dowhile()\n");
550 #endif
551 	v++;
552 	/*
553 	 * Implement prereading here also, taking care not to
554 	 * evaluate the expression before the loop has been read up
555 	 * from a terminal.
556 	 */
557 	if (intty && !again) {
558 		status = !exp0(&v, 1);
559 	} else {
560 		status = !exp(&v);
561 	}
562 	if (*v) {
563 		bferr("Expression syntax");
564 	}
565 	if (!again) {
566 		struct whyle *nwp = (struct whyle *)calloc(1, sizeof (*nwp));
567 
568 		nwp->w_start = lineloc;
569 		nwp->w_end = 0;
570 		nwp->w_next = whyles;
571 		whyles = nwp;
572 		if (intty) {
573 			/*
574 			 * The tty preread
575 			 */
576 			preread_();
577 			doagain();
578 			return;
579 		}
580 	}
581 	if (status) {
582 		/* We ain't gonna loop no more, no more! */
583 		toend();
584 	}
585 }
586 
587 void
588 preread_()
589 {
590 #ifdef TRACE
591 	tprintf("TRACE- preread()\n");
592 #endif
593 
594 	whyles->w_end = -1;
595 	if (setintr) {
596 		(void) sigsetmask(sigblock(0) & ~sigmask(SIGINT));
597 	}
598 	search(ZBREAK, 0);
599 	if (setintr) {
600 		(void) sigblock(sigmask(SIGINT));
601 	}
602 	whyles->w_end = btell();
603 }
604 
605 void
606 doend()
607 {
608 
609 #ifdef TRACE
610 	tprintf("TRACE- doend()\n");
611 #endif
612 	if (!whyles) {
613 		bferr("Not in while/foreach");
614 	}
615 	whyles->w_end = btell();
616 	doagain();
617 }
618 
619 void
620 docontin()
621 {
622 #ifdef TRACE
623 	tprintf("TRACE- docontin()\n");
624 #endif
625 
626 	if (!whyles) {
627 		bferr("Not in while/foreach");
628 	}
629 	doagain();
630 }
631 
632 void
633 doagain()
634 {
635 
636 #ifdef TRACE
637 	tprintf("TRACE- doagain()\n");
638 #endif
639 	/* Repeating a while is simple */
640 	if (whyles->w_fename == 0) {
641 		bseek(whyles->w_start);
642 		return;
643 	}
644 	/*
645 	 * The foreach variable list actually has a spurious word
646 	 * ")" at the end of the w_fe list.  Thus we are at the
647 	 * of the list if one word beyond this is 0.
648 	 */
649 	if (!whyles->w_fe[1]) {
650 		dobreak();
651 		return;
652 	}
653 	set(whyles->w_fename, savestr(*whyles->w_fe++));
654 	bseek(whyles->w_start);
655 }
656 
657 void
658 dorepeat(tchar **v, struct command *kp)
659 {
660 	int i, omask;
661 
662 #ifdef TRACE
663 	tprintf("TRACE- dorepeat()\n");
664 #endif
665 	i = getn(v[1]);
666 	if (setintr) {
667 		omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
668 	}
669 	lshift(v, 2);
670 	while (i > 0) {
671 		if (setintr) {
672 			(void) sigsetmask(omask);
673 		}
674 		reexecute(kp);
675 		--i;
676 	}
677 	donefds();
678 	if (setintr) {
679 		(void) sigsetmask(omask);
680 	}
681 }
682 
683 void
684 doswbrk()
685 {
686 
687 #ifdef TRACE
688 	tprintf("TRACE- doswbrk()\n");
689 #endif
690 	search(ZBRKSW, 0);
691 }
692 
693 int
694 srchx(tchar *cp)
695 {
696 	struct srch *sp, *sp1, *sp2;
697 	int i;
698 
699 #ifdef TRACE
700 	tprintf("TRACE- srchx()\n");
701 #endif
702 	/*
703 	 * Binary search
704 	 * Sp1 is the beginning of the current search range.
705 	 * Sp2 is one past the end.
706 	 */
707 	for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2; ) {
708 		sp = sp1 + (sp2 - sp1 >> 1);
709 		if ((i = *cp - *sp->s_name) == 0 &&
710 		    (i = strcmp_(cp, sp->s_name)) == 0) {
711 			return (sp->s_value);
712 		}
713 		if (i < 0) {
714 			sp2 = sp;
715 		} else {
716 			sp1 = sp + 1;
717 		}
718 	}
719 	return (-1);
720 }
721 
722 tchar Stype;
723 tchar *Sgoal;
724 
725 /*VARARGS2*/
726 void
727 search(type, level, goal)
728 	int type; int level; tchar *goal;
729 {
730 	tchar wordbuf[BUFSIZ];
731 	tchar *aword = wordbuf;
732 	tchar *cp;
733 
734 #ifdef TRACE
735 	tprintf("TRACE- search()\n");
736 #endif
737 	Stype = type; Sgoal = goal;
738 	if (type == ZGOTO) {
739 		bseek((off_t)0);
740 	}
741 	do {
742 		if (intty && fseekp == feobp) {
743 			printf("? "), flush();
744 		}
745 		aword[0] = 0;
746 		(void) getword(aword);
747 
748 		switch (srchx(aword)) {
749 
750 		case ZELSE:
751 			if (level == 0 && type == ZIF) {
752 				return;
753 			}
754 			break;
755 
756 		case ZIF:
757 			while (getword(aword)) {
758 				continue;
759 			}
760 			if ((type == ZIF || type == ZELSE) &&
761 			    eq(aword, S_then)) {
762 				level++;
763 			}
764 			break;
765 
766 		case ZENDIF:
767 			if (type == ZIF || type == ZELSE) {
768 				level--;
769 			}
770 			break;
771 
772 		case ZFOREACH:
773 		case ZWHILE:
774 			if (type == ZBREAK) {
775 				level++;
776 			}
777 			break;
778 
779 		case ZEND:
780 			if (type == ZBREAK) {
781 				level--;
782 			}
783 			break;
784 
785 		case ZSWITCH:
786 			if (type == ZSWITCH || type == ZBRKSW) {
787 				level++;
788 			}
789 			break;
790 
791 		case ZENDSW:
792 			if (type == ZSWITCH || type == ZBRKSW) {
793 				level--;
794 			}
795 			break;
796 
797 		case ZLABEL:
798 			if (type == ZGOTO && getword(aword) &&
799 			    eq(aword, goal)) {
800 				level = -1;
801 			}
802 			break;
803 
804 		default:
805 			if (type != ZGOTO && (type != ZSWITCH || level != 0)) {
806 				break;
807 			}
808 			if (lastchr(aword) != ':') {
809 				break;
810 			}
811 			aword[strlen_(aword) - 1] = 0;
812 			if (type == ZGOTO && eq(aword, goal) ||
813 			    type == ZSWITCH && eq(aword, S_default)) {
814 				level = -1;
815 			}
816 			break;
817 
818 		case ZCASE:
819 			if (type != ZSWITCH || level != 0) {
820 				break;
821 			}
822 			(void) getword(aword);
823 			if (lastchr(aword) == ':') {
824 				aword[strlen_(aword) - 1] = 0;
825 			}
826 			cp = strip(Dfix1(aword));
827 			if (Gmatch(goal, cp)) {
828 				level = -1;
829 			}
830 			xfree(cp);
831 			break;
832 
833 		case ZDEFAULT:
834 			if (type == ZSWITCH && level == 0) {
835 				level = -1;
836 			}
837 			break;
838 		}
839 		(void) getword(NOSTR);
840 	} while (level >= 0);
841 }
842 
843 int
844 getword(tchar *wp)
845 {
846 	int found = 0;
847 	int c, d;
848 #ifdef TRACE
849 	tprintf("TRACE- getword()\n");
850 #endif
851 
852 	c = readc(1);
853 	d = 0;
854 	do {
855 		while (issp(c)) {
856 			c = readc(1);
857 		}
858 		if (c == '#') {
859 			do {
860 				c = readc(1);
861 			} while (c >= 0 && c != '\n');
862 		}
863 		if (c < 0) {
864 			goto past;
865 		}
866 		if (c == '\n') {
867 			if (wp) {
868 				break;
869 			}
870 			return (0);
871 		}
872 
873 		/* ( and ) form separate words */
874 		if (c == '(' || c == ')') {
875 			return (1);
876 		}
877 
878 		unreadc(c);
879 		found = 1;
880 		do {
881 			c = readc(1);
882 			if (c == '\\' && (c = readc(1)) == '\n') {
883 				c = ' ';
884 			}
885 			if (c == '\'' || c == '"') {
886 				if (d == 0) {
887 					d = c;
888 				} else if (d == c) {
889 					d = 0;
890 				}
891 			}
892 			if (c < 0) {
893 				goto past;
894 			}
895 			if (wp) {
896 				*wp++ = c;
897 			}
898 		} while ((d || !issp(c) && c != '(' && c != ')') && c != '\n');
899 	} while (wp == 0);
900 	unreadc(c);
901 	if (found) {
902 		*--wp = 0;
903 	}
904 	return (found);
905 
906 past:
907 	switch (Stype) {
908 
909 	case ZIF:
910 		bferr("then/endif not found");
911 
912 	case ZELSE:
913 		bferr("endif not found");
914 
915 	case ZBRKSW:
916 	case ZSWITCH:
917 		bferr("endsw not found");
918 
919 	case ZBREAK:
920 		bferr("end not found");
921 
922 	case ZGOTO:
923 		setname(Sgoal);
924 		bferr("label not found");
925 	}
926 	/*NOTREACHED*/
927 }
928 
929 void
930 toend()
931 {
932 
933 #ifdef TRACE
934 	tprintf("TRACE- toend()\n");
935 #endif
936 	if (whyles->w_end == 0) {
937 		search(ZBREAK, 0);
938 		whyles->w_end = btell() - 1;
939 	} else {
940 		bseek(whyles->w_end);
941 	}
942 	wfree();
943 }
944 
945 void
946 wfree()
947 {
948 	long o = btell();
949 
950 #ifdef TRACE
951 	tprintf("TRACE- wfree()\n");
952 #endif
953 	while (whyles) {
954 		struct whyle *wp = whyles;
955 		struct whyle *nwp = wp->w_next;
956 
957 		if (o >= wp->w_start && (wp->w_end == 0 || o < wp->w_end)) {
958 			break;
959 		}
960 		if (wp->w_fe0) {
961 			blkfree(wp->w_fe0);
962 		}
963 		if (wp->w_fename) {
964 			xfree(wp->w_fename);
965 		}
966 		xfree((char *)wp);
967 		whyles = nwp;
968 	}
969 }
970 
971 void
972 doecho(tchar **v)
973 {
974 
975 #ifdef TRACE
976 	tprintf("TRACE- doecho()\n");
977 #endif
978 	echo(' ', v);
979 }
980 
981 void
982 doglob(tchar **v)
983 {
984 
985 #ifdef TRACE
986 	tprintf("TRACE- doglob()\n");
987 #endif
988 	echo(0, v);
989 	flush();
990 }
991 
992 void
993 echo(tchar sep, tchar **v)
994 {
995 	tchar *cp;
996 	int nonl = 0;
997 
998 #ifdef TRACE
999 	tprintf("TRACE- echo()\n");
1000 #endif
1001 	if (setintr) {
1002 		(void) sigsetmask(sigblock(0) & ~sigmask(SIGINT));
1003 	}
1004 	v++;
1005 	if (*v == 0) {
1006 		/*
1007 		 * echo command needs to have newline when there are no
1008 		 * flags or arguments.  glob should have no newline.  If
1009 		 * the separator is a blank, we are doing an echo.  If the
1010 		 * separator is zero, we are globbing.
1011 		 */
1012 		if (sep == (tchar)' ')
1013 			Putchar('\n');
1014 		return;
1015 	}
1016 	gflag = 0, tglob(v);
1017 	if (gflag) {
1018 		v = glob(v);
1019 		if (v == 0) {
1020 			bferr("No match");
1021 		}
1022 	}
1023 	/* check for -n arg, NOTE: it might be quoted */
1024 	if (sep == ' ' && *v && strlen_(*v) == 2 &&
1025 	    ((**v&TRIM) == '-' && (*(*v + 1) & TRIM) == 'n' &&
1026 	    (*(*v+2)&TRIM) == 0)) {
1027 		nonl++, v++;
1028 	}
1029 	while (cp = *v++) {
1030 		int c;
1031 
1032 		while (c = *cp++) {
1033 			Putchar(c | QUOTE);
1034 		}
1035 		if (*v) {
1036 			Putchar(sep | QUOTE);
1037 		}
1038 	}
1039 	if (sep && nonl == 0) {
1040 		Putchar('\n');
1041 	} else {
1042 		flush();
1043 	}
1044 	if (setintr) {
1045 		(void) sigblock(sigmask(SIGINT));
1046 	}
1047 	if (gargv) {
1048 		blkfree(gargv), gargv = 0;
1049 	}
1050 }
1051 
1052 extern char **environ;
1053 
1054 /*
1055  * Check if the environment variable vp affects this csh's behavior
1056  * and therefore we should call setlocale() or not.
1057  * This function has two side effects when it returns 1:
1058  *	variable islocalevar_catnum is set to the LC_xxx value.
1059  *	variable islocalevar_catname is set to the string "LC_xxx"
1060  */
1061 static int	islocalevar_catnum;
1062 static char	*islocalevar_catname;
1063 
1064 static
1065 bool
1066 islocalevar(tchar *vp)
1067 {
1068 	static struct lcinfo {
1069 		tchar *	evname; /* The name of the env. var. */
1070 	} categories_we_care[] = {
1071 	    S_LANG, S_LC_ALL, S_LC_CTYPE, S_LC_MESSAGES,
1072 	    NOSTR		/* assumption: LC_xxx >= 0 */
1073 	};
1074 	struct lcinfo *p = categories_we_care;
1075 
1076 	do {
1077 		if (strcmp_(vp, p->evname) == 0) {
1078 			return (1);
1079 		}
1080 	} while (((++p)->evname) != NOSTR);
1081 	return (0);
1082 }
1083 
1084 void
1085 dosetenv(tchar **v)
1086 {
1087 	tchar *vp, *lp;
1088 
1089 #ifdef TRACE
1090 	tprintf("TRACE- dosetenv()\n");
1091 #endif
1092 	v++;
1093 	if ((vp = *v++) == 0) {
1094 		char **ep;
1095 
1096 		if (setintr) {
1097 			(void) sigsetmask(sigblock(0) & ~ sigmask(SIGINT));
1098 		}
1099 		for (ep = environ; *ep; ep++) {
1100 			printf("%s\n", *ep);
1101 		}
1102 		return;
1103 	}
1104 
1105 	if ((lp = *v++) == 0) {
1106 		lp = S_;	/* "" */
1107 	}
1108 	local_setenv(vp, lp = globone(lp));
1109 	if (eq(vp, S_PATH)) {
1110 		importpath(lp);
1111 		dohash(xhash);
1112 	} else if (islocalevar(vp)) {
1113 		if (!setlocale(LC_ALL, "")) {
1114 			error("Locale could not be set properly");
1115 		}
1116 	}
1117 
1118 	xfree(lp);
1119 }
1120 
1121 void
1122 dounsetenv(tchar **v)
1123 {
1124 #ifdef TRACE
1125 	tprintf("TRACE- dounsetenv()\n");
1126 #endif
1127 	v++;
1128 	do {
1129 		local_unsetenv(*v);
1130 		if (islocalevar(*v++)) {
1131 			setlocale(LC_ALL, "");	/* Hope no error! */
1132 		}
1133 	} while (*v);
1134 }
1135 
1136 void
1137 local_setenv(tchar *name, tchar *val)
1138 {
1139 	char **ep = environ;
1140 	tchar *cp;
1141 	char *dp;
1142 	tchar *ep_;	/* temporary */
1143 	char *blk[2], **oep = ep;
1144 
1145 #ifdef TRACE
1146 	/* tprintf("TRACE- local_setenv(%t, %t)\n", name, val); */
1147 	/* printf("IN local_setenv args = (%t)\n", val); */
1148 #endif
1149 	for (; *ep; ep++) {
1150 #ifdef MBCHAR
1151 		for (cp = name, dp = *ep; *cp && *dp; cp++) {
1152 			/*
1153 			 * This loop compares two chars in different
1154 			 * representations, EUC (as char *) and wchar_t
1155 			 * (in tchar), and ends when they are different.
1156 			 */
1157 			wchar_t	dwc;
1158 			int	n;
1159 
1160 			n = mbtowc(&dwc, dp, MB_CUR_MAX);
1161 			if (n <= 0) {
1162 				break; /* Illegal multibyte. */
1163 			}
1164 			dp += n; /* Advance to next multibyte char. */
1165 			if (dwc == (wchar_t)(*cp & TRIM)) {
1166 				continue;
1167 			} else  {
1168 				break;
1169 			}
1170 		}
1171 #else /* !MBCHAR */
1172 		for (cp = name, dp = *ep; *cp && (char)*cp == *dp; cp++, dp++) {
1173 			continue;
1174 		}
1175 #endif /* !MBCHAR */
1176 		if (*cp != 0 || *dp != '=') {
1177 			continue;
1178 		}
1179 		cp = strspl(S_EQ, val);
1180 		xfree(*ep);
1181 		ep_ = strspl(name, cp);		/* ep_ is xalloc'ed */
1182 		xfree(cp);
1183 		/*
1184 		 * Trimming is not needed here.
1185 		 * trim();
1186 		 */
1187 		*ep = tstostr(NULL, ep_);
1188 		xfree(ep_);			/* because temp.  use */
1189 		return;
1190 	}
1191 	ep_ = strspl(name, S_EQ);		/* ep_ is xalloc'ed */
1192 	blk[0] = tstostr(NULL, ep_);
1193 	blk[1] = 0;
1194 	xfree(ep_);
1195 	environ = (char **)blkspl_((unsigned char **)environ, blk);
1196 	xfree((void *)oep);
1197 	local_setenv(name, val);
1198 }
1199 
1200 void
1201 local_unsetenv(tchar *name)
1202 {
1203 	char **ep = environ;
1204 	tchar *cp;
1205 	char *dp;
1206 	char **oep = ep;
1207 	char *cp_;	/* tmp use */
1208 	static cnt = 0;	/* delete counter */
1209 
1210 #ifdef TRACE
1211 	tprintf("TRACE- local_unsetenv()\n");
1212 #endif
1213 	for (; *ep; ep++) {
1214 #ifdef MBCHAR
1215 		for (cp = name, dp = *ep; *cp && *dp; cp++) {
1216 			/*
1217 			 * This loop compares two chars in different
1218 			 * representations, EUC (as char *) and wchar_t
1219 			 * (in tchar), and ends when they are different.
1220 			 */
1221 			wchar_t	dwc;
1222 			int	n;
1223 
1224 			n = mbtowc(&dwc, dp, MB_CUR_MAX);
1225 			if (n <= 0) {
1226 				break; /* Illegal multibyte. */
1227 			}
1228 			dp += n; /* Advance to next multibyte char. */
1229 			if (dwc == (wchar_t)(*cp & TRIM)) {
1230 				continue;
1231 			} else {
1232 				break;
1233 			}
1234 		}
1235 #else /* !MBCHAR */
1236 		for (cp = name, dp = *ep; *cp && (char)*cp == *dp; cp++, dp++) {
1237 			continue;
1238 		}
1239 #endif /* !MBCHAR */
1240 		if (*cp != 0 || *dp != '=') {
1241 			continue;
1242 		}
1243 		cp_ = *ep;
1244 		*ep = 0;
1245 		environ = (char **)blkspl_((unsigned char **)environ, ep+1);
1246 		*ep = cp_;
1247 		xfree(cp_);
1248 		xfree((void *)oep);
1249 		return;
1250 	}
1251 }
1252 
1253 void
1254 doumask(tchar **v)
1255 {
1256 	tchar *cp = v[1];
1257 	int i;
1258 
1259 #ifdef TRACE
1260 	tprintf("TRACE- dounmask()\n");
1261 #endif
1262 	if (cp == 0) {
1263 		i = umask(0);
1264 		(void) umask(i);
1265 		printf("%o\n", i);
1266 		return;
1267 	}
1268 	i = 0;
1269 	while (digit(*cp) && *cp != '8' && *cp != '9') {
1270 		i = i * 8 + *cp++ - '0';
1271 	}
1272 	if (*cp || i < 0 || i > 0777) {
1273 		bferr("Improper mask");
1274 	}
1275 	(void) umask(i);
1276 }
1277 
1278 
1279 struct limits *
1280 findlim(tchar *cp)
1281 {
1282 	struct limits *lp, *res;
1283 
1284 #ifdef TRACE
1285 	tprintf("TRACE- findlim()\n");
1286 #endif
1287 	res = 0;
1288 	for (lp = limits; lp->limconst >= 0; lp++) {
1289 		if (prefix(cp, lp->limname)) {
1290 			if (res) {
1291 				bferr("Ambiguous");
1292 			}
1293 			res = lp;
1294 		}
1295 	}
1296 	if (res) {
1297 		return (res);
1298 	}
1299 	bferr("No such limit");
1300 	/*NOTREACHED*/
1301 }
1302 
1303 void
1304 dolimit(tchar **v)
1305 {
1306 	struct limits *lp;
1307 	rlim_t limit;
1308 	tchar hard = 0;
1309 
1310 #ifdef TRACE
1311 	tprintf("TRACE- dolimit()\n");
1312 #endif
1313 	v++;
1314 	if (*v && eq(*v, S_h)) {
1315 		hard = 1;
1316 		v++;
1317 	}
1318 	if (*v == 0) {
1319 		for (lp = limits; lp->limconst >= 0; lp++) {
1320 			plim(lp, hard);
1321 		}
1322 		return;
1323 	}
1324 	lp = findlim(v[0]);
1325 	if (v[1] == 0) {
1326 		plim(lp,  hard);
1327 		return;
1328 	}
1329 	switch (getval(lp, v+1, &limit)) {
1330 	case 0:
1331 		error("Value specified for limit is too large");
1332 		return;
1333 	case (-1):
1334 		error("Numeric conversion failed");
1335 		return;
1336 	default:
1337 		if (setlim(lp, hard, limit) < 0) {
1338 			error(NOSTR);
1339 		}
1340 	}
1341 }
1342 
1343 static int
1344 getval(struct limits *lp, tchar **v, rlim_t *retval)
1345 {
1346 	rlim_t value, tmp, tmp2;
1347 	tchar *cp = *v++;
1348 	char chbuf[BUFSIZ * MB_LEN_MAX];
1349 
1350 #ifdef TRACE
1351 	tprintf("TRACE- getval()\n");
1352 #endif
1353 
1354 	tstostr(chbuf, cp);
1355 	errno = 0;
1356 	value = strtoull(chbuf, NULL, 0);
1357 /*
1358  * we must accept zero, but the conversion can fail and give us
1359  * zero as well...try to deal with it as gracefully as possible
1360  * by checking for EINVAL
1361  */
1362 	if (value == 0 && errno == EINVAL)
1363 		return (-1);
1364 
1365 	while (digit(*cp) || *cp == '.' || *cp == 'e' || *cp == 'E') {
1366 		cp++;
1367 	}
1368 	if (*cp == 0) {
1369 		if (*v == 0) {
1370 			tmp = value * (rlim_t)lp->limdiv;
1371 			/* Check for overflow */
1372 			if (tmp >= value) {
1373 				*retval = tmp;
1374 				return (1);
1375 			} else {
1376 				return (0);
1377 			}
1378 		}
1379 		cp = *v;
1380 	}
1381 	switch (*cp) {
1382 
1383 	case ':':
1384 		if (lp->limconst != RLIMIT_CPU) {
1385 			goto badscal;
1386 		}
1387 		tstostr(chbuf, cp + 1);
1388 		tmp = strtoull(chbuf, NULL, 0);
1389 		tmp2 = value * 60 + tmp;
1390 		if (tmp2 >= value) {
1391 			*retval = tmp2;
1392 			return (1);
1393 		} else {
1394 			return (0);
1395 		}
1396 
1397 	case 'h':
1398 		if (lp->limconst != RLIMIT_CPU) {
1399 			goto badscal;
1400 		}
1401 		limtail(cp, S_hours);
1402 		tmp = value * 3600;
1403 		if (tmp < value) {
1404 			return (0);
1405 		}
1406 		value = tmp;
1407 		break;
1408 
1409 	case 'm':
1410 		if (lp->limconst == RLIMIT_CPU) {
1411 			limtail(cp, S_minutes);
1412 			tmp = value * 60;
1413 			if (tmp < value) {
1414 				return (0);
1415 			}
1416 			value = tmp;
1417 			break;
1418 		}
1419 	case 'M':
1420 		if (lp->limconst == RLIMIT_CPU) {
1421 			goto badscal;
1422 		}
1423 		*cp = 'm';
1424 		limtail(cp, S_megabytes);
1425 		tmp = value * 1024 * 1024;
1426 		if (tmp < value) {
1427 			return (0);
1428 		}
1429 		value = tmp;
1430 		break;
1431 
1432 	case 's':
1433 		if (lp->limconst != RLIMIT_CPU) {
1434 			goto badscal;
1435 		}
1436 		limtail(cp, S_seconds);
1437 		break;
1438 
1439 	case 'k':
1440 		if (lp->limconst == RLIMIT_CPU) {
1441 			goto badscal;
1442 		}
1443 		limtail(cp, S_kbytes);
1444 		tmp = value * 1024;
1445 		if (tmp < value) {
1446 			return (0);
1447 		}
1448 		value = tmp;
1449 		break;
1450 
1451 	case 'u':
1452 		limtail(cp, S_unlimited);
1453 		*retval = RLIM_INFINITY;
1454 		return (1);
1455 
1456 	default:
1457 badscal:
1458 		bferr("Improper or unknown scale factor");
1459 	}
1460 	*retval = value;
1461 	return (1);
1462 }
1463 
1464 void
1465 limtail(tchar *cp, tchar *str0)
1466 {
1467 	tchar *str = str0;
1468 #ifdef TRACE
1469 	tprintf("TRACE- limtail()\n");
1470 #endif
1471 
1472 	while (*cp && *cp == *str) {
1473 		cp++, str++;
1474 	}
1475 	if (*cp) {
1476 		error("Bad scaling; did you mean ``%t''?", str0);
1477 	}
1478 }
1479 
1480 void
1481 plim(struct limits *lp, tchar hard)
1482 {
1483 	struct rlimit rlim;
1484 	char buf[BUFSZ];
1485 	char *pbuf;
1486 	rlim_t limit;
1487 
1488 #ifdef TRACE
1489 	tprintf("TRACE- plim()\n");
1490 #endif
1491 	printf("%t \t", lp->limname);
1492 	(void) getrlimit(lp->limconst, &rlim);
1493 	limit = hard ? rlim.rlim_max : rlim.rlim_cur;
1494 	if (limit == RLIM_INFINITY) {
1495 		printf("unlimited");
1496 	} else if (lp->limconst == RLIMIT_CPU) {
1497 		psecs_ull(limit);
1498 	} else {
1499 		buf[BUFSZ - 1] = '\0';
1500 		pbuf = ulltostr((limit / lp->limdiv), &buf[BUFSZ - 1]);
1501 		printf("%s %t", pbuf, lp->limscale);
1502 	}
1503 	printf("\n");
1504 }
1505 
1506 void
1507 dounlimit(tchar **v)
1508 {
1509 	struct limits *lp;
1510 	int err = 0;
1511 	tchar hard = 0;
1512 #ifdef TRACE
1513 	tprintf("TRACE- dounlimit()\n");
1514 #endif
1515 
1516 	v++;
1517 	if (*v && eq(*v, S_h)) {
1518 		hard = 1;
1519 		v++;
1520 	}
1521 	if (*v == 0) {
1522 		for (lp = limits; lp->limconst >= 0; lp++) {
1523 			if (setlim(lp, hard, RLIM_INFINITY) < 0) {
1524 				err++;
1525 			}
1526 		}
1527 		if (err) {
1528 			error(NULL);
1529 		}
1530 		return;
1531 	}
1532 	while (*v) {
1533 		lp = findlim(*v++);
1534 		if (setlim(lp, hard, RLIM_INFINITY) < 0) {
1535 			error(NULL);
1536 		}
1537 	}
1538 }
1539 
1540 int
1541 setlim(struct limits *lp, tchar hard, rlim_t limit)
1542 {
1543 	struct rlimit rlim;
1544 
1545 #ifdef TRACE
1546 	tprintf("TRACE- setlim()\n");
1547 #endif
1548 	(void) getrlimit(lp->limconst, &rlim);
1549 	if (hard) {
1550 		rlim.rlim_max = limit;
1551 	} else if (limit == RLIM_INFINITY && geteuid() != 0) {
1552 		rlim.rlim_cur = rlim.rlim_max;
1553 	} else {
1554 		rlim.rlim_cur = limit;
1555 	}
1556 	if (setrlimit(lp->limconst, &rlim) < 0) {
1557 		printf("%t: %t: Can't %s%s limit\n", bname, lp->limname,
1558 		    limit == RLIM_INFINITY ? "remove" : "set",
1559 		    hard ? " hard" : "");
1560 		return (-1);
1561 	}
1562 	return (0);
1563 }
1564 
1565 void
1566 dosuspend()
1567 {
1568 	int ctpgrp;
1569 	void (*old)();
1570 
1571 #ifdef TRACE
1572 	tprintf("TRACE- dosuspend()\n");
1573 #endif
1574 	if (loginsh) {
1575 		error("Can't suspend a login shell (yet)");
1576 	}
1577 	if (getpid() == getsid(0)) {
1578 		error("Can't suspend this shell");
1579 	}
1580 	untty();
1581 	old = (void (*)())signal(SIGTSTP, SIG_DFL);
1582 	(void) kill(0, SIGTSTP);
1583 	/* the shell stops here */
1584 	(void) signal(SIGTSTP, old);
1585 	if (tpgrp != -1) {
1586 retry:
1587 		(void) ioctl(FSHTTY, TIOCGPGRP,  (char *)&ctpgrp);
1588 		if (ctpgrp != opgrp) {
1589 			old = (void (*)())signal(SIGTTIN, SIG_DFL);
1590 			(void) kill(0, SIGTTIN);
1591 			(void) signal(SIGTTIN, old);
1592 			goto retry;
1593 		}
1594 		(void) setpgid(0, shpgrp);
1595 		(void) ioctl(FSHTTY, TIOCSPGRP, (char *)&shpgrp);
1596 	}
1597 }
1598 
1599 void
1600 doeval(tchar **v)
1601 {
1602 	tchar **oevalvec = evalvec;
1603 	tchar *oevalp = evalp;
1604 	jmp_buf osetexit;
1605 	int reenter;
1606 	tchar **gv = 0;
1607 
1608 #ifdef TRACE
1609 	tprintf("TRACE- doeval()\n");
1610 #endif
1611 	v++;
1612 	if (*v == 0) {
1613 		return;
1614 	}
1615 	gflag = 0, tglob(v);
1616 	if (gflag) {
1617 		gv = v = glob(v);
1618 		gargv = 0;
1619 		if (v == 0) {
1620 			error("No match");
1621 		}
1622 		v = copyblk(v);
1623 	} else {
1624 		trim(v);
1625 	}
1626 	getexit(osetexit);
1627 	reenter = 0;
1628 	setexit();
1629 	reenter++;
1630 	if (reenter == 1) {
1631 		evalvec = v;
1632 		evalp = 0;
1633 		process(0);
1634 	}
1635 	evalvec = oevalvec;
1636 	evalp = oevalp;
1637 	doneinp = 0;
1638 	if (gv) {
1639 		blkfree(gv);
1640 	}
1641 	resexit(osetexit);
1642 	if (reenter >= 2) {
1643 		error(NULL);
1644 	}
1645 }
1646