1 /* $OpenBSD: func.c,v 1.13 2002/02/16 21:27:06 millert Exp $ */ 2 /* $NetBSD: func.c,v 1.11 1996/02/09 02:28:29 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1980, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)func.c 8.1 (Berkeley) 5/31/93"; 40 #else 41 static char rcsid[] = "$OpenBSD: func.c,v 1.13 2002/02/16 21:27:06 millert 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 #ifdef __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 int zlast = -1; 65 static void islogin(void); 66 static void reexecute(struct command *); 67 static void preread(void); 68 static void doagain(void); 69 static void search(int, int, Char *); 70 static int getword(Char *); 71 static int keyword(Char *); 72 static void toend(void); 73 static void xecho(int, Char **); 74 static void Unsetenv(Char *); 75 76 struct biltins * 77 isbfunc(t) 78 struct command *t; 79 { 80 register Char *cp = t->t_dcom[0]; 81 register 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 register 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 register struct command *t; 121 register 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 register Char *cp; 142 register 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 sigaddset(&sigset, SIGINT); 156 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 register struct varent *vp; 206 register 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 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]), (char *)NULL); 257 untty(); 258 xexit(1); 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 } 270 271 void 272 doif(v, kp) 273 Char **v; 274 struct command *kp; 275 { 276 register int i; 277 register Char **vv; 278 279 v++; 280 i = expr(&v); 281 vv = v; 282 if (*vv == NULL) 283 stderror(ERR_NAME | ERR_EMPTYIF); 284 if (eq(*vv, STRthen)) { 285 if (*++vv) 286 stderror(ERR_NAME | ERR_IMPRTHEN); 287 setname(vis_str(STRthen)); 288 /* 289 * If expression was zero, then scan to else, otherwise just fall into 290 * following code. 291 */ 292 if (!i) 293 search(T_IF, 0, NULL); 294 return; 295 } 296 /* 297 * Simple command attached to this if. Left shift the node in this tree, 298 * munging it so we can reexecute it. 299 */ 300 if (i) { 301 lshift(kp->t_dcom, vv - kp->t_dcom); 302 reexecute(kp); 303 donefds(); 304 } 305 } 306 307 /* 308 * Reexecute a command, being careful not 309 * to redo i/o redirection, which is already set up. 310 */ 311 static void 312 reexecute(kp) 313 register struct command *kp; 314 { 315 kp->t_dflg &= F_SAVE; 316 kp->t_dflg |= F_REPEAT; 317 /* 318 * If tty is still ours to arbitrate, arbitrate it; otherwise dont even set 319 * pgrp's as the jobs would then have no way to get the tty (we can't give 320 * it to them, and our parent wouldn't know their pgrp, etc. 321 */ 322 execute(kp, (tpgrp > 0 ? tpgrp : -1), NULL, NULL); 323 } 324 325 void 326 /*ARGSUSED*/ 327 doelse(v, t) 328 Char **v; 329 struct command *t; 330 { 331 search(T_ELSE, 0, NULL); 332 } 333 334 void 335 /*ARGSUSED*/ 336 dogoto(v, t) 337 Char **v; 338 struct command *t; 339 { 340 Char *lp; 341 342 gotolab(lp = globone(v[1], G_ERROR)); 343 xfree((ptr_t) lp); 344 } 345 346 void 347 gotolab(lab) 348 Char *lab; 349 { 350 register struct whyle *wp; 351 /* 352 * While we still can, locate any unknown ends of existing loops. This 353 * obscure code is the WORST result of the fact that we don't really parse. 354 */ 355 zlast = T_GOTO; 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 register 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 register Char *cp, *sp; 431 register 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 zlast = T_FOREACH; 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 register int status; 475 register 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 register 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 zlast = T_WHILE; 499 if (intty) { 500 /* 501 * The tty preread 502 */ 503 preread(); 504 doagain(); 505 return; 506 } 507 } 508 if (status) 509 /* We ain't gonna loop no more, no more! */ 510 toend(); 511 } 512 513 static void 514 preread() 515 { 516 sigset_t sigset; 517 518 whyles->w_end.type = I_SEEK; 519 if (setintr) { 520 sigemptyset(&sigset); 521 sigaddset(&sigset, SIGINT); 522 sigprocmask(SIG_UNBLOCK, &sigset, NULL); 523 } 524 525 search(T_BREAK, 0, NULL); /* read the expression in */ 526 if (setintr) 527 sigprocmask(SIG_BLOCK, &sigset, NULL); 528 btell(&whyles->w_end); 529 } 530 531 void 532 /*ARGSUSED*/ 533 doend(v, t) 534 Char **v; 535 struct command *t; 536 { 537 if (!whyles) 538 stderror(ERR_NAME | ERR_NOTWHILE); 539 btell(&whyles->w_end); 540 doagain(); 541 } 542 543 void 544 /*ARGSUSED*/ 545 docontin(v, t) 546 Char **v; 547 struct command *t; 548 { 549 if (!whyles) 550 stderror(ERR_NAME | ERR_NOTWHILE); 551 doagain(); 552 } 553 554 static void 555 doagain() 556 { 557 /* Repeating a while is simple */ 558 if (whyles->w_fename == 0) { 559 bseek(&whyles->w_start); 560 return; 561 } 562 /* 563 * The foreach variable list actually has a spurious word ")" at the end of 564 * the w_fe list. Thus we are at the of the list if one word beyond this 565 * is 0. 566 */ 567 if (!whyles->w_fe[1]) { 568 dobreak(NULL, NULL); 569 return; 570 } 571 set(whyles->w_fename, Strsave(*whyles->w_fe++)); 572 bseek(&whyles->w_start); 573 } 574 575 void 576 dorepeat(v, kp) 577 Char **v; 578 struct command *kp; 579 { 580 register int i; 581 sigset_t sigset; 582 583 i = getn(v[1]); 584 if (setintr) { 585 sigemptyset(&sigset); 586 sigaddset(&sigset, SIGINT); 587 sigprocmask(SIG_BLOCK, &sigset, NULL); 588 } 589 lshift(v, 2); 590 while (i > 0) { 591 if (setintr) 592 sigprocmask(SIG_UNBLOCK, &sigset, NULL); 593 reexecute(kp); 594 --i; 595 } 596 donefds(); 597 if (setintr) 598 sigprocmask(SIG_UNBLOCK, &sigset, NULL); 599 } 600 601 void 602 /*ARGSUSED*/ 603 doswbrk(v, t) 604 Char **v; 605 struct command *t; 606 { 607 search(T_BRKSW, 0, NULL); 608 } 609 610 int 611 srchx(cp) 612 register Char *cp; 613 { 614 register struct srch *sp, *sp1, *sp2; 615 register int i; 616 617 /* 618 * Binary search Sp1 is the beginning of the current search range. Sp2 is 619 * one past the end. 620 */ 621 for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2;) { 622 sp = sp1 + ((sp2 - sp1) >> 1); 623 if ((i = *cp - *sp->s_name) == 0 && 624 (i = Strcmp(cp, str2short(sp->s_name))) == 0) 625 return sp->s_value; 626 if (i < 0) 627 sp2 = sp; 628 else 629 sp1 = sp + 1; 630 } 631 return (-1); 632 } 633 634 static Char Stype; 635 static Char *Sgoal; 636 637 /*VARARGS2*/ 638 static void 639 search(type, level, goal) 640 int type; 641 register int level; 642 Char *goal; 643 { 644 Char wordbuf[BUFSIZ]; 645 register Char *aword = wordbuf; 646 register Char *cp; 647 648 Stype = type; 649 Sgoal = goal; 650 if (type == T_GOTO) { 651 struct Ain a; 652 a.type = F_SEEK; 653 a.f_seek = 0; 654 bseek(&a); 655 } 656 do { 657 if (intty && fseekp == feobp && aret == F_SEEK) 658 (void) fprintf(cshout, "? "), (void) fflush(cshout); 659 aword[0] = 0; 660 (void) getword(aword); 661 switch (srchx(aword)) { 662 663 case T_ELSE: 664 if (level == 0 && type == T_IF) 665 return; 666 break; 667 668 case T_IF: 669 while (getword(aword)) 670 continue; 671 if ((type == T_IF || type == T_ELSE) && 672 eq(aword, STRthen)) 673 level++; 674 break; 675 676 case T_ENDIF: 677 if (type == T_IF || type == T_ELSE) 678 level--; 679 break; 680 681 case T_FOREACH: 682 case T_WHILE: 683 if (type == T_BREAK) 684 level++; 685 break; 686 687 case T_END: 688 if (type == T_BREAK) 689 level--; 690 break; 691 692 case T_SWITCH: 693 if (type == T_SWITCH || type == T_BRKSW) 694 level++; 695 break; 696 697 case T_ENDSW: 698 if (type == T_SWITCH || type == T_BRKSW) 699 level--; 700 break; 701 702 case T_LABEL: 703 if (type == T_GOTO && getword(aword) && eq(aword, goal)) 704 level = -1; 705 break; 706 707 default: 708 if (type != T_GOTO && (type != T_SWITCH || level != 0)) 709 break; 710 if (lastchr(aword) != ':') 711 break; 712 aword[Strlen(aword) - 1] = 0; 713 if ((type == T_GOTO && eq(aword, goal)) || 714 (type == T_SWITCH && eq(aword, STRdefault))) 715 level = -1; 716 break; 717 718 case T_CASE: 719 if (type != T_SWITCH || level != 0) 720 break; 721 (void) getword(aword); 722 if (lastchr(aword) == ':') 723 aword[Strlen(aword) - 1] = 0; 724 cp = strip(Dfix1(aword)); 725 if (Gmatch(goal, cp)) 726 level = -1; 727 xfree((ptr_t) cp); 728 break; 729 730 case T_DEFAULT: 731 if (type == T_SWITCH && level == 0) 732 level = -1; 733 break; 734 } 735 (void) getword(NULL); 736 } while (level >= 0); 737 } 738 739 static int 740 getword(wp) 741 register Char *wp; 742 { 743 register int found = 0; 744 register int c, d; 745 int kwd = 0; 746 Char *owp = wp; 747 748 c = readc(1); 749 d = 0; 750 do { 751 while (c == ' ' || c == '\t') 752 c = readc(1); 753 if (c == '#') 754 do 755 c = readc(1); 756 while (c >= 0 && c != '\n'); 757 if (c < 0) 758 goto past; 759 if (c == '\n') { 760 if (wp) 761 break; 762 return (0); 763 } 764 unreadc(c); 765 found = 1; 766 do { 767 c = readc(1); 768 if (c == '\\' && (c = readc(1)) == '\n') 769 c = ' '; 770 if (c == '\'' || c == '"') { 771 if (d == 0) 772 d = c; 773 else if (d == c) 774 d = 0; 775 } 776 if (c < 0) 777 goto past; 778 if (wp) { 779 *wp++ = c; 780 *wp = 0; /* end the string b4 test */ 781 } 782 } while ((d || (!(kwd = keyword(owp)) && c != ' ' 783 && c != '\t')) && c != '\n'); 784 } while (wp == 0); 785 786 /* 787 * if we have read a keyword ( "if", "switch" or "while" ) then we do not 788 * need to unreadc the look-ahead char 789 */ 790 if (!kwd) { 791 unreadc(c); 792 if (found) 793 *--wp = 0; 794 } 795 796 return (found); 797 798 past: 799 switch (Stype) { 800 801 case T_IF: 802 stderror(ERR_NAME | ERR_NOTFOUND, "then/endif"); 803 804 case T_ELSE: 805 stderror(ERR_NAME | ERR_NOTFOUND, "endif"); 806 807 case T_BRKSW: 808 case T_SWITCH: 809 stderror(ERR_NAME | ERR_NOTFOUND, "endsw"); 810 811 case T_BREAK: 812 stderror(ERR_NAME | ERR_NOTFOUND, "end"); 813 814 case T_GOTO: 815 setname(vis_str(Sgoal)); 816 stderror(ERR_NAME | ERR_NOTFOUND, "label"); 817 } 818 /* NOTREACHED */ 819 return (0); 820 } 821 822 /* 823 * keyword(wp) determines if wp is one of the built-n functions if, 824 * switch or while. It seems that when an if statement looks like 825 * "if(" then getword above sucks in the '(' and so the search routine 826 * never finds what it is scanning for. Rather than rewrite doword, I hack 827 * in a test to see if the string forms a keyword. Then doword stops 828 * and returns the word "if" -strike 829 */ 830 831 static int 832 keyword(wp) 833 Char *wp; 834 { 835 static Char STRif[] = {'i', 'f', '\0'}; 836 static Char STRwhile[] = {'w', 'h', 'i', 'l', 'e', '\0'}; 837 static Char STRswitch[] = {'s', 'w', 'i', 't', 'c', 'h', '\0'}; 838 839 if (!wp) 840 return (0); 841 842 if ((Strcmp(wp, STRif) == 0) || (Strcmp(wp, STRwhile) == 0) 843 || (Strcmp(wp, STRswitch) == 0)) 844 return (1); 845 846 return (0); 847 } 848 849 static void 850 toend() 851 { 852 if (whyles->w_end.type == F_SEEK && whyles->w_end.f_seek == 0) { 853 search(T_BREAK, 0, NULL); 854 btell(&whyles->w_end); 855 whyles->w_end.f_seek--; 856 } 857 else 858 bseek(&whyles->w_end); 859 wfree(); 860 } 861 862 void 863 wfree() 864 { 865 struct Ain o; 866 struct whyle *nwp; 867 868 btell(&o); 869 870 for (; whyles; whyles = nwp) { 871 register struct whyle *wp = whyles; 872 nwp = wp->w_next; 873 874 /* 875 * We free loops that have different seek types. 876 */ 877 if (wp->w_end.type != I_SEEK && wp->w_start.type == wp->w_end.type && 878 wp->w_start.type == o.type) { 879 if (wp->w_end.type == F_SEEK) { 880 if (o.f_seek >= wp->w_start.f_seek && 881 (wp->w_end.f_seek == 0 || o.f_seek < wp->w_end.f_seek)) 882 break; 883 } 884 else { 885 if (o.a_seek >= wp->w_start.a_seek && 886 (wp->w_end.a_seek == 0 || o.a_seek < wp->w_end.a_seek)) 887 break; 888 } 889 } 890 891 if (wp->w_fe0) 892 blkfree(wp->w_fe0); 893 if (wp->w_fename) 894 xfree((ptr_t) wp->w_fename); 895 xfree((ptr_t) wp); 896 } 897 } 898 899 void 900 /*ARGSUSED*/ 901 doecho(v, t) 902 Char **v; 903 struct command *t; 904 { 905 xecho(' ', v); 906 } 907 908 void 909 /*ARGSUSED*/ 910 doglob(v, t) 911 Char **v; 912 struct command *t; 913 { 914 xecho(0, v); 915 (void) fflush(cshout); 916 } 917 918 static void 919 xecho(sep, v) 920 int sep; 921 register Char **v; 922 { 923 register Char *cp; 924 int nonl = 0; 925 sigset_t sigset; 926 927 if (setintr) { 928 sigemptyset(&sigset); 929 sigaddset(&sigset, SIGINT); 930 sigprocmask(SIG_UNBLOCK, &sigset, NULL); 931 } 932 v++; 933 if (*v == 0) 934 return; 935 gflag = 0, tglob(v); 936 if (gflag) { 937 v = globall(v); 938 if (v == 0) 939 stderror(ERR_NAME | ERR_NOMATCH); 940 } 941 else { 942 v = gargv = saveblk(v); 943 trim(v); 944 } 945 if (sep == ' ' && *v && eq(*v, STRmn)) 946 nonl++, v++; 947 while ((cp = *v++) != NULL) { 948 register int c; 949 950 while ((c = *cp++) != '\0') 951 (void) vis_fputc(c | QUOTE, cshout); 952 953 if (*v) 954 (void) vis_fputc(sep | QUOTE, cshout); 955 } 956 if (sep && nonl == 0) 957 (void) fputc('\n', cshout); 958 else 959 (void) fflush(cshout); 960 if (setintr) 961 sigprocmask(SIG_BLOCK, &sigset, NULL); 962 if (gargv) 963 blkfree(gargv), gargv = 0; 964 } 965 966 void 967 /*ARGSUSED*/ 968 dosetenv(v, t) 969 Char **v; 970 struct command *t; 971 { 972 Char *vp, *lp; 973 sigset_t sigset; 974 975 v++; 976 if ((vp = *v++) == 0) { 977 register Char **ep; 978 979 if (setintr) { 980 sigemptyset(&sigset); 981 sigaddset(&sigset, SIGINT); 982 sigprocmask(SIG_UNBLOCK, &sigset, NULL); 983 } 984 for (ep = STR_environ; *ep; ep++) 985 (void) fprintf(cshout, "%s\n", vis_str(*ep)); 986 return; 987 } 988 if ((lp = *v++) == 0) 989 lp = STRNULL; 990 Setenv(vp, lp = globone(lp, G_APPEND)); 991 if (eq(vp, STRPATH)) { 992 importpath(lp); 993 dohash(NULL, NULL); 994 } 995 else if (eq(vp, STRLANG) || eq(vp, STRLC_CTYPE)) { 996 #ifdef NLS 997 int k; 998 999 (void) setlocale(LC_ALL, ""); 1000 for (k = 0200; k <= 0377 && !Isprint(k); k++) 1001 continue; 1002 AsciiOnly = k > 0377; 1003 #else 1004 AsciiOnly = 0; 1005 #endif /* NLS */ 1006 } 1007 xfree((ptr_t) lp); 1008 } 1009 1010 void 1011 /*ARGSUSED*/ 1012 dounsetenv(v, t) 1013 Char **v; 1014 struct command *t; 1015 { 1016 Char **ep, *p, *n; 1017 int i, maxi; 1018 static Char *name = NULL; 1019 1020 if (name) 1021 xfree((ptr_t) name); 1022 /* 1023 * Find the longest environment variable 1024 */ 1025 for (maxi = 0, ep = STR_environ; *ep; ep++) { 1026 for (i = 0, p = *ep; *p && *p != '='; p++, i++) 1027 continue; 1028 if (i > maxi) 1029 maxi = i; 1030 } 1031 1032 name = (Char *) xmalloc((size_t) (maxi + 1) * sizeof(Char)); 1033 1034 while (++v && *v) 1035 for (maxi = 1; maxi;) 1036 for (maxi = 0, ep = STR_environ; *ep; ep++) { 1037 for (n = name, p = *ep; *p && *p != '='; *n++ = *p++) 1038 continue; 1039 *n = '\0'; 1040 if (!Gmatch(name, *v)) 1041 continue; 1042 maxi = 1; 1043 if (eq(name, STRLANG) || eq(name, STRLC_CTYPE)) { 1044 #ifdef NLS 1045 int k; 1046 1047 (void) setlocale(LC_ALL, ""); 1048 for (k = 0200; k <= 0377 && !Isprint(k); k++) 1049 continue; 1050 AsciiOnly = k > 0377; 1051 #else 1052 AsciiOnly = getenv("LANG") == NULL && 1053 getenv("LC_CTYPE") == NULL; 1054 #endif /* NLS */ 1055 } 1056 /* 1057 * Delete name, and start again cause the environment changes 1058 */ 1059 Unsetenv(name); 1060 break; 1061 } 1062 xfree((ptr_t) name); 1063 name = NULL; 1064 } 1065 1066 void 1067 Setenv(name, val) 1068 Char *name, *val; 1069 { 1070 register Char **ep = STR_environ; 1071 register Char *cp, *dp; 1072 Char *blk[2]; 1073 Char **oep = ep; 1074 1075 1076 for (; *ep; ep++) { 1077 for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++) 1078 continue; 1079 if (*cp != 0 || *dp != '=') 1080 continue; 1081 cp = Strspl(STRequal, val); 1082 xfree((ptr_t) * ep); 1083 *ep = strip(Strspl(name, cp)); 1084 xfree((ptr_t) cp); 1085 blkfree((Char **) environ); 1086 environ = short2blk(STR_environ); 1087 return; 1088 } 1089 cp = Strspl(name, STRequal); 1090 blk[0] = strip(Strspl(cp, val)); 1091 xfree((ptr_t) cp); 1092 blk[1] = 0; 1093 STR_environ = blkspl(STR_environ, blk); 1094 blkfree((Char **) environ); 1095 environ = short2blk(STR_environ); 1096 xfree((ptr_t) oep); 1097 } 1098 1099 static void 1100 Unsetenv(name) 1101 Char *name; 1102 { 1103 register Char **ep = STR_environ; 1104 register Char *cp, *dp; 1105 Char **oep = ep; 1106 1107 for (; *ep; ep++) { 1108 for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++) 1109 continue; 1110 if (*cp != 0 || *dp != '=') 1111 continue; 1112 cp = *ep; 1113 *ep = 0; 1114 STR_environ = blkspl(STR_environ, ep + 1); 1115 environ = short2blk(STR_environ); 1116 *ep = cp; 1117 xfree((ptr_t) cp); 1118 xfree((ptr_t) oep); 1119 return; 1120 } 1121 } 1122 1123 void 1124 /*ARGSUSED*/ 1125 doumask(v, t) 1126 Char **v; 1127 struct command *t; 1128 { 1129 register Char *cp = v[1]; 1130 register int i; 1131 1132 if (cp == 0) { 1133 i = umask(0); 1134 (void) umask(i); 1135 (void) fprintf(cshout, "%o\n", i); 1136 return; 1137 } 1138 i = 0; 1139 while (Isdigit(*cp) && *cp != '8' && *cp != '9') 1140 i = i * 8 + *cp++ - '0'; 1141 if (*cp || i < 0 || i > 0777) 1142 stderror(ERR_NAME | ERR_MASK); 1143 (void) umask(i); 1144 } 1145 1146 typedef quad_t RLIM_TYPE; 1147 1148 static struct limits { 1149 int limconst; 1150 char *limname; 1151 int limdiv; 1152 char *limscale; 1153 } limits[] = { 1154 { RLIMIT_CPU, "cputime", 1, "seconds" }, 1155 { RLIMIT_FSIZE, "filesize", 1024, "kbytes" }, 1156 { RLIMIT_DATA, "datasize", 1024, "kbytes" }, 1157 { RLIMIT_STACK, "stacksize", 1024, "kbytes" }, 1158 { RLIMIT_CORE, "coredumpsize", 1024, "kbytes" }, 1159 { RLIMIT_RSS, "memoryuse", 1024, "kbytes" }, 1160 { RLIMIT_MEMLOCK, "memorylocked", 1024, "kbytes" }, 1161 { RLIMIT_NPROC, "maxproc", 1, "" }, 1162 { RLIMIT_NOFILE, "openfiles", 1, "" }, 1163 { -1, NULL, 0, NULL } 1164 }; 1165 1166 static struct limits *findlim(Char *); 1167 static RLIM_TYPE getval(struct limits *, Char **); 1168 static void limtail(Char *, char *); 1169 static void plim(struct limits *, Char); 1170 static int setlim(struct limits *, Char, RLIM_TYPE); 1171 1172 static struct limits * 1173 findlim(cp) 1174 Char *cp; 1175 { 1176 register struct limits *lp, *res; 1177 1178 res = NULL; 1179 for (lp = limits; lp->limconst >= 0; lp++) 1180 if (prefix(cp, str2short(lp->limname))) { 1181 if (res) 1182 stderror(ERR_NAME | ERR_AMBIG); 1183 res = lp; 1184 } 1185 if (res) 1186 return (res); 1187 stderror(ERR_NAME | ERR_LIMIT); 1188 /* NOTREACHED */ 1189 return (0); 1190 } 1191 1192 void 1193 /*ARGSUSED*/ 1194 dolimit(v, t) 1195 Char **v; 1196 struct command *t; 1197 { 1198 register struct limits *lp; 1199 register 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 register struct limits *lp; 1225 Char **v; 1226 { 1227 register float f; 1228 double atof(); 1229 Char *cp = *v++; 1230 1231 f = atof(short2str(cp)); 1232 1233 while (Isdigit(*cp) || *cp == '.' || *cp == 'e' || *cp == 'E') 1234 cp++; 1235 if (*cp == 0) { 1236 if (*v == 0) 1237 return ((RLIM_TYPE) ((f + 0.5) * lp->limdiv)); 1238 cp = *v; 1239 } 1240 switch (*cp) { 1241 case ':': 1242 if (lp->limconst != RLIMIT_CPU) 1243 goto badscal; 1244 return ((RLIM_TYPE) (f * 60.0 + atof(short2str(cp + 1)))); 1245 case 'h': 1246 if (lp->limconst != RLIMIT_CPU) 1247 goto badscal; 1248 limtail(cp, "hours"); 1249 f *= 3600.0; 1250 break; 1251 case 'm': 1252 if (lp->limconst == RLIMIT_CPU) { 1253 limtail(cp, "minutes"); 1254 f *= 60.0; 1255 break; 1256 } 1257 *cp = 'm'; 1258 limtail(cp, "megabytes"); 1259 f *= 1024.0 * 1024.0; 1260 break; 1261 case 's': 1262 if (lp->limconst != RLIMIT_CPU) 1263 goto badscal; 1264 limtail(cp, "seconds"); 1265 break; 1266 case 'M': 1267 if (lp->limconst == RLIMIT_CPU) 1268 goto badscal; 1269 *cp = 'm'; 1270 limtail(cp, "megabytes"); 1271 f *= 1024.0 * 1024.0; 1272 break; 1273 case 'k': 1274 if (lp->limconst == RLIMIT_CPU) 1275 goto badscal; 1276 limtail(cp, "kbytes"); 1277 f *= 1024.0; 1278 break; 1279 case 'u': 1280 limtail(cp, "unlimited"); 1281 return (RLIM_INFINITY); 1282 default: 1283 badscal: 1284 stderror(ERR_NAME | ERR_SCALEF); 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 register 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 register 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 register 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) (); 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 arguments, 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 **newv; 1517 char **c; 1518 extern int progprintf(int, char **); 1519 int ret; 1520 1521 gflag = 0; 1522 tglob(v); 1523 if (gflag) { 1524 newv = globall(v); 1525 if (newv == 0) { 1526 stderror(ERR_NAME | ERR_NOMATCH); 1527 return; 1528 } 1529 v = newv; 1530 gargv = 0; 1531 } 1532 1533 ret = progprintf(blklen(v), c = short2blk(v)); 1534 (void) fflush(cshout); 1535 (void) fflush(csherr); 1536 1537 blkfree((Char **) c); 1538 if (ret) 1539 stderror(ERR_SILENT); 1540 } 1541