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