1 /* $NetBSD: eval.c,v 1.76 2004/04/30 06:27:59 dsl Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; 39 #else 40 __RCSID("$NetBSD: eval.c,v 1.76 2004/04/30 06:27:59 dsl Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 #include <stdlib.h> 45 #include <signal.h> 46 #include <stdio.h> 47 #include <unistd.h> 48 #include <sys/fcntl.h> 49 #include <sys/times.h> 50 #include <sys/param.h> 51 #include <sys/types.h> 52 #include <sys/wait.h> 53 #include <sys/sysctl.h> 54 55 /* 56 * Evaluate a command. 57 */ 58 59 #include "shell.h" 60 #include "nodes.h" 61 #include "syntax.h" 62 #include "expand.h" 63 #include "parser.h" 64 #include "jobs.h" 65 #include "eval.h" 66 #include "builtins.h" 67 #include "options.h" 68 #include "exec.h" 69 #include "redir.h" 70 #include "input.h" 71 #include "output.h" 72 #include "trap.h" 73 #include "var.h" 74 #include "memalloc.h" 75 #include "error.h" 76 #include "show.h" 77 #include "mystring.h" 78 #include "main.h" 79 #ifndef SMALL 80 #include "myhistedit.h" 81 #endif 82 83 84 /* flags in argument to evaltree */ 85 #define EV_EXIT 01 /* exit after evaluating tree */ 86 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ 87 #define EV_BACKCMD 04 /* command executing within back quotes */ 88 89 int evalskip; /* set if we are skipping commands */ 90 STATIC int skipcount; /* number of levels to skip */ 91 MKINIT int loopnest; /* current loop nesting level */ 92 int funcnest; /* depth of function calls */ 93 94 95 char *commandname; 96 struct strlist *cmdenviron; 97 int exitstatus; /* exit status of last command */ 98 int back_exitstatus; /* exit status of backquoted command */ 99 100 101 STATIC void evalloop(union node *, int); 102 STATIC void evalfor(union node *, int); 103 STATIC void evalcase(union node *, int); 104 STATIC void evalsubshell(union node *, int); 105 STATIC void expredir(union node *); 106 STATIC void evalpipe(union node *); 107 STATIC void evalcommand(union node *, int, struct backcmd *); 108 STATIC void prehash(union node *); 109 110 111 /* 112 * Called to reset things after an exception. 113 */ 114 115 #ifdef mkinit 116 INCLUDE "eval.h" 117 118 RESET { 119 evalskip = 0; 120 loopnest = 0; 121 funcnest = 0; 122 } 123 124 SHELLPROC { 125 exitstatus = 0; 126 } 127 #endif 128 129 static int 130 sh_pipe(int fds[2]) 131 { 132 int nfd; 133 134 if (pipe(fds)) 135 return -1; 136 137 if (fds[0] < 3) { 138 nfd = fcntl(fds[0], F_DUPFD, 3); 139 if (nfd != -1) { 140 close(fds[0]); 141 fds[0] = nfd; 142 } 143 } 144 145 if (fds[1] < 3) { 146 nfd = fcntl(fds[1], F_DUPFD, 3); 147 if (nfd != -1) { 148 close(fds[1]); 149 fds[1] = nfd; 150 } 151 } 152 return 0; 153 } 154 155 156 /* 157 * The eval commmand. 158 */ 159 160 int 161 evalcmd(int argc, char **argv) 162 { 163 char *p; 164 char *concat; 165 char **ap; 166 167 if (argc > 1) { 168 p = argv[1]; 169 if (argc > 2) { 170 STARTSTACKSTR(concat); 171 ap = argv + 2; 172 for (;;) { 173 while (*p) 174 STPUTC(*p++, concat); 175 if ((p = *ap++) == NULL) 176 break; 177 STPUTC(' ', concat); 178 } 179 STPUTC('\0', concat); 180 p = grabstackstr(concat); 181 } 182 evalstring(p, EV_TESTED); 183 } 184 return exitstatus; 185 } 186 187 188 /* 189 * Execute a command or commands contained in a string. 190 */ 191 192 void 193 evalstring(char *s, int flag) 194 { 195 union node *n; 196 struct stackmark smark; 197 198 setstackmark(&smark); 199 setinputstring(s, 1); 200 201 while ((n = parsecmd(0)) != NEOF) { 202 evaltree(n, flag); 203 popstackmark(&smark); 204 } 205 popfile(); 206 popstackmark(&smark); 207 } 208 209 210 211 /* 212 * Evaluate a parse tree. The value is left in the global variable 213 * exitstatus. 214 */ 215 216 void 217 evaltree(union node *n, int flags) 218 { 219 if (n == NULL) { 220 TRACE(("evaltree(NULL) called\n")); 221 exitstatus = 0; 222 goto out; 223 } 224 #ifndef SMALL 225 displayhist = 1; /* show history substitutions done with fc */ 226 #endif 227 TRACE(("pid %d, evaltree(%p: %d, %d) called\n", 228 getpid(), n, n->type, flags)); 229 switch (n->type) { 230 case NSEMI: 231 evaltree(n->nbinary.ch1, flags & EV_TESTED); 232 if (evalskip) 233 goto out; 234 evaltree(n->nbinary.ch2, flags); 235 break; 236 case NAND: 237 evaltree(n->nbinary.ch1, EV_TESTED); 238 if (evalskip || exitstatus != 0) 239 goto out; 240 evaltree(n->nbinary.ch2, flags | EV_TESTED); 241 break; 242 case NOR: 243 evaltree(n->nbinary.ch1, EV_TESTED); 244 if (evalskip || exitstatus == 0) 245 goto out; 246 evaltree(n->nbinary.ch2, flags | EV_TESTED); 247 break; 248 case NREDIR: 249 expredir(n->nredir.redirect); 250 redirect(n->nredir.redirect, REDIR_PUSH); 251 evaltree(n->nredir.n, flags); 252 popredir(); 253 break; 254 case NSUBSHELL: 255 evalsubshell(n, flags); 256 break; 257 case NBACKGND: 258 evalsubshell(n, flags); 259 break; 260 case NIF: { 261 evaltree(n->nif.test, EV_TESTED); 262 if (evalskip) 263 goto out; 264 if (exitstatus == 0) 265 evaltree(n->nif.ifpart, flags); 266 else if (n->nif.elsepart) 267 evaltree(n->nif.elsepart, flags); 268 else 269 exitstatus = 0; 270 break; 271 } 272 case NWHILE: 273 case NUNTIL: 274 evalloop(n, flags); 275 break; 276 case NFOR: 277 evalfor(n, flags); 278 break; 279 case NCASE: 280 evalcase(n, flags); 281 break; 282 case NDEFUN: 283 defun(n->narg.text, n->narg.next); 284 exitstatus = 0; 285 break; 286 case NNOT: 287 evaltree(n->nnot.com, EV_TESTED); 288 exitstatus = !exitstatus; 289 break; 290 case NPIPE: 291 evalpipe(n); 292 break; 293 case NCMD: 294 evalcommand(n, flags, (struct backcmd *)NULL); 295 break; 296 default: 297 out1fmt("Node type = %d\n", n->type); 298 flushout(&output); 299 break; 300 } 301 out: 302 if (pendingsigs) 303 dotrap(); 304 if ((flags & EV_EXIT) != 0) 305 exitshell(exitstatus); 306 } 307 308 309 STATIC void 310 evalloop(union node *n, int flags) 311 { 312 int status; 313 314 loopnest++; 315 status = 0; 316 for (;;) { 317 evaltree(n->nbinary.ch1, EV_TESTED); 318 if (evalskip) { 319 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) { 320 evalskip = 0; 321 continue; 322 } 323 if (evalskip == SKIPBREAK && --skipcount <= 0) 324 evalskip = 0; 325 break; 326 } 327 if (n->type == NWHILE) { 328 if (exitstatus != 0) 329 break; 330 } else { 331 if (exitstatus == 0) 332 break; 333 } 334 evaltree(n->nbinary.ch2, flags & EV_TESTED); 335 status = exitstatus; 336 if (evalskip) 337 goto skipping; 338 } 339 loopnest--; 340 exitstatus = status; 341 } 342 343 344 345 STATIC void 346 evalfor(union node *n, int flags) 347 { 348 struct arglist arglist; 349 union node *argp; 350 struct strlist *sp; 351 struct stackmark smark; 352 int status = 0; 353 354 setstackmark(&smark); 355 arglist.lastp = &arglist.list; 356 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { 357 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); 358 if (evalskip) 359 goto out; 360 } 361 *arglist.lastp = NULL; 362 363 loopnest++; 364 for (sp = arglist.list ; sp ; sp = sp->next) { 365 setvar(n->nfor.var, sp->text, 0); 366 evaltree(n->nfor.body, flags & EV_TESTED); 367 status = exitstatus; 368 if (evalskip) { 369 if (evalskip == SKIPCONT && --skipcount <= 0) { 370 evalskip = 0; 371 continue; 372 } 373 if (evalskip == SKIPBREAK && --skipcount <= 0) 374 evalskip = 0; 375 break; 376 } 377 } 378 loopnest--; 379 exitstatus = status; 380 out: 381 popstackmark(&smark); 382 } 383 384 385 386 STATIC void 387 evalcase(union node *n, int flags) 388 { 389 union node *cp; 390 union node *patp; 391 struct arglist arglist; 392 struct stackmark smark; 393 int status = 0; 394 395 setstackmark(&smark); 396 arglist.lastp = &arglist.list; 397 expandarg(n->ncase.expr, &arglist, EXP_TILDE); 398 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) { 399 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { 400 if (casematch(patp, arglist.list->text)) { 401 if (evalskip == 0) { 402 evaltree(cp->nclist.body, flags); 403 status = exitstatus; 404 } 405 goto out; 406 } 407 } 408 } 409 out: 410 exitstatus = status; 411 popstackmark(&smark); 412 } 413 414 415 416 /* 417 * Kick off a subshell to evaluate a tree. 418 */ 419 420 STATIC void 421 evalsubshell(union node *n, int flags) 422 { 423 struct job *jp; 424 int backgnd = (n->type == NBACKGND); 425 426 expredir(n->nredir.redirect); 427 INTOFF; 428 jp = makejob(n, 1); 429 if (forkshell(jp, n, backgnd) == 0) { 430 INTON; 431 if (backgnd) 432 flags &=~ EV_TESTED; 433 redirect(n->nredir.redirect, 0); 434 /* never returns */ 435 evaltree(n->nredir.n, flags | EV_EXIT); 436 } 437 if (! backgnd) 438 exitstatus = waitforjob(jp); 439 INTON; 440 } 441 442 443 444 /* 445 * Compute the names of the files in a redirection list. 446 */ 447 448 STATIC void 449 expredir(union node *n) 450 { 451 union node *redir; 452 453 for (redir = n ; redir ; redir = redir->nfile.next) { 454 struct arglist fn; 455 fn.lastp = &fn.list; 456 switch (redir->type) { 457 case NFROMTO: 458 case NFROM: 459 case NTO: 460 case NCLOBBER: 461 case NAPPEND: 462 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); 463 redir->nfile.expfname = fn.list->text; 464 break; 465 case NFROMFD: 466 case NTOFD: 467 if (redir->ndup.vname) { 468 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); 469 fixredir(redir, fn.list->text, 1); 470 } 471 break; 472 } 473 } 474 } 475 476 477 478 /* 479 * Evaluate a pipeline. All the processes in the pipeline are children 480 * of the process creating the pipeline. (This differs from some versions 481 * of the shell, which make the last process in a pipeline the parent 482 * of all the rest.) 483 */ 484 485 STATIC void 486 evalpipe(union node *n) 487 { 488 struct job *jp; 489 struct nodelist *lp; 490 int pipelen; 491 int prevfd; 492 int pip[2]; 493 494 TRACE(("evalpipe(0x%lx) called\n", (long)n)); 495 pipelen = 0; 496 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) 497 pipelen++; 498 INTOFF; 499 jp = makejob(n, pipelen); 500 prevfd = -1; 501 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 502 prehash(lp->n); 503 pip[1] = -1; 504 if (lp->next) { 505 if (sh_pipe(pip) < 0) { 506 close(prevfd); 507 error("Pipe call failed"); 508 } 509 } 510 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) { 511 INTON; 512 if (prevfd > 0) { 513 close(0); 514 copyfd(prevfd, 0); 515 close(prevfd); 516 } 517 if (pip[1] >= 0) { 518 close(pip[0]); 519 if (pip[1] != 1) { 520 close(1); 521 copyfd(pip[1], 1); 522 close(pip[1]); 523 } 524 } 525 evaltree(lp->n, EV_EXIT); 526 } 527 if (prevfd >= 0) 528 close(prevfd); 529 prevfd = pip[0]; 530 close(pip[1]); 531 } 532 if (n->npipe.backgnd == 0) { 533 exitstatus = waitforjob(jp); 534 TRACE(("evalpipe: job done exit status %d\n", exitstatus)); 535 } 536 INTON; 537 } 538 539 540 541 /* 542 * Execute a command inside back quotes. If it's a builtin command, we 543 * want to save its output in a block obtained from malloc. Otherwise 544 * we fork off a subprocess and get the output of the command via a pipe. 545 * Should be called with interrupts off. 546 */ 547 548 void 549 evalbackcmd(union node *n, struct backcmd *result) 550 { 551 int pip[2]; 552 struct job *jp; 553 struct stackmark smark; /* unnecessary */ 554 555 setstackmark(&smark); 556 result->fd = -1; 557 result->buf = NULL; 558 result->nleft = 0; 559 result->jp = NULL; 560 if (n == NULL) { 561 goto out; 562 } 563 #ifdef notyet 564 /* 565 * For now we disable executing builtins in the same 566 * context as the shell, because we are not keeping 567 * enough state to recover from changes that are 568 * supposed only to affect subshells. eg. echo "`cd /`" 569 */ 570 if (n->type == NCMD) { 571 exitstatus = oexitstatus; 572 evalcommand(n, EV_BACKCMD, result); 573 } else 574 #endif 575 { 576 INTOFF; 577 if (sh_pipe(pip) < 0) 578 error("Pipe call failed"); 579 jp = makejob(n, 1); 580 if (forkshell(jp, n, FORK_NOJOB) == 0) { 581 FORCEINTON; 582 close(pip[0]); 583 if (pip[1] != 1) { 584 close(1); 585 copyfd(pip[1], 1); 586 close(pip[1]); 587 } 588 eflag = 0; 589 evaltree(n, EV_EXIT); 590 /* NOTREACHED */ 591 } 592 close(pip[1]); 593 result->fd = pip[0]; 594 result->jp = jp; 595 INTON; 596 } 597 out: 598 popstackmark(&smark); 599 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", 600 result->fd, result->buf, result->nleft, result->jp)); 601 } 602 603 static const char * 604 syspath(void) 605 { 606 static char *sys_path = NULL; 607 static int mib[] = {CTL_USER, USER_CS_PATH}; 608 size_t len; 609 610 if (sys_path == NULL) { 611 if (sysctl(mib, 2, 0, &len, 0, 0) != -1 && 612 (sys_path = ckmalloc(len + 5)) != NULL && 613 sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) { 614 memcpy(sys_path, "PATH=", 5); 615 } else { 616 ckfree(sys_path); 617 /* something to keep things happy */ 618 sys_path = "PATH=/usr/bin:/bin:/usr/sbin:/sbin"; 619 } 620 } 621 return sys_path; 622 } 623 624 static int 625 parse_command_args(int argc, char **argv, int *use_syspath) 626 { 627 int sv_argc = argc; 628 char *cp, c; 629 630 *use_syspath = 0; 631 632 for (;;) { 633 argv++; 634 if (--argc == 0) 635 break; 636 cp = *argv; 637 if (*cp++ != '-') 638 break; 639 if (*cp == '-' && cp[1] == 0) { 640 argv++; 641 argc--; 642 break; 643 } 644 while ((c = *cp++)) { 645 switch (c) { 646 case 'p': 647 *use_syspath = 1; 648 break; 649 default: 650 /* run 'typecmd' for other options */ 651 return 0; 652 } 653 } 654 } 655 return sv_argc - argc; 656 } 657 658 int vforked = 0; 659 660 /* 661 * Execute a simple command. 662 */ 663 664 STATIC void 665 evalcommand(union node *cmd, int flags, struct backcmd *backcmd) 666 { 667 struct stackmark smark; 668 union node *argp; 669 struct arglist arglist; 670 struct arglist varlist; 671 char **argv; 672 int argc; 673 char **envp; 674 int varflag; 675 struct strlist *sp; 676 int mode; 677 int pip[2]; 678 struct cmdentry cmdentry; 679 struct job *jp; 680 struct jmploc jmploc; 681 struct jmploc *volatile savehandler; 682 char *volatile savecmdname; 683 volatile struct shparam saveparam; 684 struct localvar *volatile savelocalvars; 685 volatile int e; 686 char *lastarg; 687 const char *path = pathval(); 688 int temp_path; 689 #if __GNUC__ 690 /* Avoid longjmp clobbering */ 691 (void) &argv; 692 (void) &argc; 693 (void) &lastarg; 694 (void) &flags; 695 #endif 696 697 vforked = 0; 698 /* First expand the arguments. */ 699 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); 700 setstackmark(&smark); 701 back_exitstatus = 0; 702 703 arglist.lastp = &arglist.list; 704 varflag = 1; 705 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 706 char *p = argp->narg.text; 707 if (varflag && is_name(*p)) { 708 do { 709 p++; 710 } while (is_in_name(*p)); 711 if (*p == '=') 712 continue; 713 } 714 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 715 varflag = 0; 716 } 717 *arglist.lastp = NULL; 718 719 expredir(cmd->ncmd.redirect); 720 721 varlist.lastp = &varlist.list; 722 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 723 char *p = argp->narg.text; 724 if (!is_name(*p)) 725 break; 726 do 727 p++; 728 while (is_in_name(*p)); 729 if (*p != '=') 730 break; 731 expandarg(argp, &varlist, EXP_VARTILDE); 732 } 733 *varlist.lastp = NULL; 734 735 argc = 0; 736 for (sp = arglist.list ; sp ; sp = sp->next) 737 argc++; 738 argv = stalloc(sizeof (char *) * (argc + 1)); 739 740 for (sp = arglist.list ; sp ; sp = sp->next) { 741 TRACE(("evalcommand arg: %s\n", sp->text)); 742 *argv++ = sp->text; 743 } 744 *argv = NULL; 745 lastarg = NULL; 746 if (iflag && funcnest == 0 && argc > 0) 747 lastarg = argv[-1]; 748 argv -= argc; 749 750 /* Print the command if xflag is set. */ 751 if (xflag) { 752 char sep = 0; 753 out2str(ps4val()); 754 for (sp = varlist.list ; sp ; sp = sp->next) { 755 if (sep != 0) 756 outc(sep, &errout); 757 out2str(sp->text); 758 sep = ' '; 759 } 760 for (sp = arglist.list ; sp ; sp = sp->next) { 761 if (sep != 0) 762 outc(sep, &errout); 763 out2str(sp->text); 764 sep = ' '; 765 } 766 outc('\n', &errout); 767 flushout(&errout); 768 } 769 770 /* Now locate the command. */ 771 if (argc == 0) { 772 cmdentry.cmdtype = CMDSPLBLTIN; 773 cmdentry.u.bltin = bltincmd; 774 } else { 775 static const char PATH[] = "PATH="; 776 int cmd_flags = DO_ERR; 777 778 /* 779 * Modify the command lookup path, if a PATH= assignment 780 * is present 781 */ 782 for (sp = varlist.list; sp; sp = sp->next) 783 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) 784 path = sp->text + sizeof(PATH) - 1; 785 786 do { 787 int argsused, use_syspath; 788 find_command(argv[0], &cmdentry, cmd_flags, path); 789 if (cmdentry.cmdtype == CMDUNKNOWN) { 790 exitstatus = 127; 791 flushout(&errout); 792 goto out; 793 } 794 795 /* implement the 'command' builtin here */ 796 if (cmdentry.cmdtype != CMDBUILTIN || 797 cmdentry.u.bltin != bltincmd) 798 break; 799 cmd_flags |= DO_NOFUNC; 800 argsused = parse_command_args(argc, argv, &use_syspath); 801 if (argsused == 0) { 802 /* use 'type' builting to display info */ 803 cmdentry.u.bltin = typecmd; 804 break; 805 } 806 argc -= argsused; 807 argv += argsused; 808 if (use_syspath) 809 path = syspath() + 5; 810 } while (argc != 0); 811 if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC) 812 /* posix mandates that 'command <splbltin>' act as if 813 <splbltin> was a normal builtin */ 814 cmdentry.cmdtype = CMDBUILTIN; 815 } 816 817 /* Fork off a child process if necessary. */ 818 if (cmd->ncmd.backgnd 819 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0) 820 || ((flags & EV_BACKCMD) != 0 821 && ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN) 822 || cmdentry.u.bltin == dotcmd 823 || cmdentry.u.bltin == evalcmd))) { 824 INTOFF; 825 jp = makejob(cmd, 1); 826 mode = cmd->ncmd.backgnd; 827 if (flags & EV_BACKCMD) { 828 mode = FORK_NOJOB; 829 if (sh_pipe(pip) < 0) 830 error("Pipe call failed"); 831 } 832 #ifdef DO_SHAREDVFORK 833 /* It is essential that if DO_SHAREDVFORK is defined that the 834 * child's address space is actually shared with the parent as 835 * we rely on this. 836 */ 837 if (cmdentry.cmdtype == CMDNORMAL) { 838 pid_t pid; 839 840 savelocalvars = localvars; 841 localvars = NULL; 842 vforked = 1; 843 switch (pid = vfork()) { 844 case -1: 845 TRACE(("Vfork failed, errno=%d\n", errno)); 846 INTON; 847 error("Cannot vfork"); 848 break; 849 case 0: 850 /* Make sure that exceptions only unwind to 851 * after the vfork(2) 852 */ 853 if (setjmp(jmploc.loc)) { 854 if (exception == EXSHELLPROC) { 855 /* We can't progress with the vfork, 856 * so, set vforked = 2 so the parent 857 * knows, and _exit(); 858 */ 859 vforked = 2; 860 _exit(0); 861 } else { 862 _exit(exerrno); 863 } 864 } 865 savehandler = handler; 866 handler = &jmploc; 867 listmklocal(varlist.list, VEXPORT | VNOFUNC); 868 forkchild(jp, cmd, mode, vforked); 869 break; 870 default: 871 handler = savehandler; /* restore from vfork(2) */ 872 poplocalvars(); 873 localvars = savelocalvars; 874 if (vforked == 2) { 875 vforked = 0; 876 877 (void)waitpid(pid, NULL, 0); 878 /* We need to progress in a normal fork fashion */ 879 goto normal_fork; 880 } 881 vforked = 0; 882 forkparent(jp, cmd, mode, pid); 883 goto parent; 884 } 885 } else { 886 normal_fork: 887 #endif 888 if (forkshell(jp, cmd, mode) != 0) 889 goto parent; /* at end of routine */ 890 FORCEINTON; 891 #ifdef DO_SHAREDVFORK 892 } 893 #endif 894 if (flags & EV_BACKCMD) { 895 if (!vforked) { 896 FORCEINTON; 897 } 898 close(pip[0]); 899 if (pip[1] != 1) { 900 close(1); 901 copyfd(pip[1], 1); 902 close(pip[1]); 903 } 904 } 905 flags |= EV_EXIT; 906 } 907 908 /* This is the child process if a fork occurred. */ 909 /* Execute the command. */ 910 switch (cmdentry.cmdtype) { 911 case CMDFUNCTION: 912 #ifdef DEBUG 913 trputs("Shell function: "); trargs(argv); 914 #endif 915 redirect(cmd->ncmd.redirect, REDIR_PUSH); 916 saveparam = shellparam; 917 shellparam.malloc = 0; 918 shellparam.reset = 1; 919 shellparam.nparam = argc - 1; 920 shellparam.p = argv + 1; 921 shellparam.optnext = NULL; 922 INTOFF; 923 savelocalvars = localvars; 924 localvars = NULL; 925 INTON; 926 if (setjmp(jmploc.loc)) { 927 if (exception == EXSHELLPROC) { 928 freeparam((volatile struct shparam *) 929 &saveparam); 930 } else { 931 freeparam(&shellparam); 932 shellparam = saveparam; 933 } 934 poplocalvars(); 935 localvars = savelocalvars; 936 handler = savehandler; 937 longjmp(handler->loc, 1); 938 } 939 savehandler = handler; 940 handler = &jmploc; 941 listmklocal(varlist.list, 0); 942 /* stop shell blowing its stack */ 943 if (++funcnest > 1000) 944 error("too many nested function calls"); 945 evaltree(cmdentry.u.func, flags & EV_TESTED); 946 funcnest--; 947 INTOFF; 948 poplocalvars(); 949 localvars = savelocalvars; 950 freeparam(&shellparam); 951 shellparam = saveparam; 952 handler = savehandler; 953 popredir(); 954 INTON; 955 if (evalskip == SKIPFUNC) { 956 evalskip = 0; 957 skipcount = 0; 958 } 959 if (flags & EV_EXIT) 960 exitshell(exitstatus); 961 break; 962 963 case CMDBUILTIN: 964 case CMDSPLBLTIN: 965 #ifdef DEBUG 966 trputs("builtin command: "); trargs(argv); 967 #endif 968 mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH; 969 if (flags == EV_BACKCMD) { 970 memout.nleft = 0; 971 memout.nextc = memout.buf; 972 memout.bufsize = 64; 973 mode |= REDIR_BACKQ; 974 } 975 e = -1; 976 savehandler = handler; 977 handler = &jmploc; 978 if (!setjmp(jmploc.loc)) { 979 /* We need to ensure the command hash table isn't 980 * corruped by temporary PATH assignments. 981 * However we must ensure the 'local' command works! 982 */ 983 if (path != pathval() && (cmdentry.u.bltin == hashcmd || 984 cmdentry.u.bltin == typecmd)) { 985 savelocalvars = localvars; 986 localvars = 0; 987 mklocal(path - 5 /* PATH= */, 0); 988 temp_path = 1; 989 } else 990 temp_path = 0; 991 redirect(cmd->ncmd.redirect, mode); 992 993 savecmdname = commandname; 994 /* exec is a special builtin, but needs this list... */ 995 cmdenviron = varlist.list; 996 /* we must check 'readonly' flag for all builtins */ 997 listsetvar(varlist.list, 998 cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET); 999 commandname = argv[0]; 1000 /* initialize nextopt */ 1001 argptr = argv + 1; 1002 optptr = NULL; 1003 /* and getopt */ 1004 optreset = 1; 1005 optind = 1; 1006 exitstatus = cmdentry.u.bltin(argc, argv); 1007 } else { 1008 e = exception; 1009 exitstatus = e == EXINT ? SIGINT + 128 : 1010 e == EXEXEC ? exerrno : 2; 1011 } 1012 handler = savehandler; 1013 flushall(); 1014 out1 = &output; 1015 out2 = &errout; 1016 freestdout(); 1017 if (temp_path) { 1018 poplocalvars(); 1019 localvars = savelocalvars; 1020 } 1021 cmdenviron = NULL; 1022 if (e != EXSHELLPROC) { 1023 commandname = savecmdname; 1024 if (flags & EV_EXIT) 1025 exitshell(exitstatus); 1026 } 1027 if (e != -1) { 1028 if ((e != EXERROR && e != EXEXEC) 1029 || cmdentry.cmdtype == CMDSPLBLTIN) 1030 exraise(e); 1031 FORCEINTON; 1032 } 1033 if (cmdentry.u.bltin != execcmd) 1034 popredir(); 1035 if (flags == EV_BACKCMD) { 1036 backcmd->buf = memout.buf; 1037 backcmd->nleft = memout.nextc - memout.buf; 1038 memout.buf = NULL; 1039 } 1040 break; 1041 1042 default: 1043 #ifdef DEBUG 1044 trputs("normal command: "); trargs(argv); 1045 #endif 1046 clearredir(vforked); 1047 redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0); 1048 if (!vforked) 1049 for (sp = varlist.list ; sp ; sp = sp->next) 1050 setvareq(sp->text, VEXPORT|VSTACK); 1051 envp = environment(); 1052 shellexec(argv, envp, path, cmdentry.u.index, vforked); 1053 break; 1054 } 1055 goto out; 1056 1057 parent: /* parent process gets here (if we forked) */ 1058 if (mode == FORK_FG) { /* argument to fork */ 1059 exitstatus = waitforjob(jp); 1060 } else if (mode == FORK_NOJOB) { 1061 backcmd->fd = pip[0]; 1062 close(pip[1]); 1063 backcmd->jp = jp; 1064 } 1065 FORCEINTON; 1066 1067 out: 1068 if (lastarg) 1069 /* dsl: I think this is intended to be used to support 1070 * '_' in 'vi' command mode during line editing... 1071 * However I implemented that within libedit itself. 1072 */ 1073 setvar("_", lastarg, 0); 1074 popstackmark(&smark); 1075 1076 if (eflag && exitstatus && !(flags & EV_TESTED)) 1077 exitshell(exitstatus); 1078 } 1079 1080 1081 /* 1082 * Search for a command. This is called before we fork so that the 1083 * location of the command will be available in the parent as well as 1084 * the child. The check for "goodname" is an overly conservative 1085 * check that the name will not be subject to expansion. 1086 */ 1087 1088 STATIC void 1089 prehash(union node *n) 1090 { 1091 struct cmdentry entry; 1092 1093 if (n->type == NCMD && n->ncmd.args) 1094 if (goodname(n->ncmd.args->narg.text)) 1095 find_command(n->ncmd.args->narg.text, &entry, 0, 1096 pathval()); 1097 } 1098 1099 1100 1101 /* 1102 * Builtin commands. Builtin commands whose functions are closely 1103 * tied to evaluation are implemented here. 1104 */ 1105 1106 /* 1107 * No command given. 1108 */ 1109 1110 int 1111 bltincmd(int argc, char **argv) 1112 { 1113 /* 1114 * Preserve exitstatus of a previous possible redirection 1115 * as POSIX mandates 1116 */ 1117 return back_exitstatus; 1118 } 1119 1120 1121 /* 1122 * Handle break and continue commands. Break, continue, and return are 1123 * all handled by setting the evalskip flag. The evaluation routines 1124 * above all check this flag, and if it is set they start skipping 1125 * commands rather than executing them. The variable skipcount is 1126 * the number of loops to break/continue, or the number of function 1127 * levels to return. (The latter is always 1.) It should probably 1128 * be an error to break out of more loops than exist, but it isn't 1129 * in the standard shell so we don't make it one here. 1130 */ 1131 1132 int 1133 breakcmd(int argc, char **argv) 1134 { 1135 int n = argc > 1 ? number(argv[1]) : 1; 1136 1137 if (n > loopnest) 1138 n = loopnest; 1139 if (n > 0) { 1140 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 1141 skipcount = n; 1142 } 1143 return 0; 1144 } 1145 1146 1147 /* 1148 * The return command. 1149 */ 1150 1151 int 1152 returncmd(int argc, char **argv) 1153 { 1154 int ret = argc > 1 ? number(argv[1]) : exitstatus; 1155 1156 if (funcnest) { 1157 evalskip = SKIPFUNC; 1158 skipcount = 1; 1159 return ret; 1160 } 1161 else { 1162 /* Do what ksh does; skip the rest of the file */ 1163 evalskip = SKIPFILE; 1164 skipcount = 1; 1165 return ret; 1166 } 1167 } 1168 1169 1170 int 1171 falsecmd(int argc, char **argv) 1172 { 1173 return 1; 1174 } 1175 1176 1177 int 1178 truecmd(int argc, char **argv) 1179 { 1180 return 0; 1181 } 1182 1183 1184 int 1185 execcmd(int argc, char **argv) 1186 { 1187 if (argc > 1) { 1188 struct strlist *sp; 1189 1190 iflag = 0; /* exit on error */ 1191 mflag = 0; 1192 optschanged(); 1193 for (sp = cmdenviron; sp; sp = sp->next) 1194 setvareq(sp->text, VEXPORT|VSTACK); 1195 shellexec(argv + 1, environment(), pathval(), 0, 0); 1196 } 1197 return 0; 1198 } 1199 1200 static int 1201 conv_time(clock_t ticks, char *seconds, size_t l) 1202 { 1203 static clock_t tpm = 0; 1204 clock_t mins; 1205 int i; 1206 1207 if (!tpm) 1208 tpm = sysconf(_SC_CLK_TCK) * 60; 1209 1210 mins = ticks / tpm; 1211 snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm ); 1212 1213 if (seconds[0] == '6' && seconds[1] == '0') { 1214 /* 59.99995 got rounded up... */ 1215 mins++; 1216 strlcpy(seconds, "0.0", l); 1217 return mins; 1218 } 1219 1220 /* suppress trailing zeros */ 1221 i = strlen(seconds) - 1; 1222 for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--) 1223 seconds[i] = 0; 1224 return mins; 1225 } 1226 1227 int 1228 timescmd(int argc, char **argv) 1229 { 1230 struct tms tms; 1231 int u, s, cu, cs; 1232 char us[8], ss[8], cus[8], css[8]; 1233 1234 nextopt(""); 1235 1236 times(&tms); 1237 1238 u = conv_time(tms.tms_utime, us, sizeof(us)); 1239 s = conv_time(tms.tms_stime, ss, sizeof(ss)); 1240 cu = conv_time(tms.tms_cutime, cus, sizeof(cus)); 1241 cs = conv_time(tms.tms_cstime, css, sizeof(css)); 1242 1243 outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n", 1244 u, us, s, ss, cu, cus, cs, css); 1245 1246 return 0; 1247 } 1248