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