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