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