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