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