1 /* $NetBSD: eval.c,v 1.78 2004/06/26 22:09:49 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.78 2004/06/26 22:09:49 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); 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 /* Expand arguments, ignoring the initial 'name=value' ones */ 706 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 707 char *p = argp->narg.text; 708 if (varflag && is_name(*p)) { 709 do { 710 p++; 711 } while (is_in_name(*p)); 712 if (*p == '=') 713 continue; 714 } 715 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); 716 varflag = 0; 717 } 718 *arglist.lastp = NULL; 719 720 expredir(cmd->ncmd.redirect); 721 722 /* Now do the initial 'name=value' ones we skipped above */ 723 varlist.lastp = &varlist.list; 724 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { 725 char *p = argp->narg.text; 726 if (!is_name(*p)) 727 break; 728 do 729 p++; 730 while (is_in_name(*p)); 731 if (*p != '=') 732 break; 733 expandarg(argp, &varlist, EXP_VARTILDE); 734 } 735 *varlist.lastp = NULL; 736 737 argc = 0; 738 for (sp = arglist.list ; sp ; sp = sp->next) 739 argc++; 740 argv = stalloc(sizeof (char *) * (argc + 1)); 741 742 for (sp = arglist.list ; sp ; sp = sp->next) { 743 TRACE(("evalcommand arg: %s\n", sp->text)); 744 *argv++ = sp->text; 745 } 746 *argv = NULL; 747 lastarg = NULL; 748 if (iflag && funcnest == 0 && argc > 0) 749 lastarg = argv[-1]; 750 argv -= argc; 751 752 /* Print the command if xflag is set. */ 753 if (xflag) { 754 char sep = 0; 755 out2str(ps4val()); 756 for (sp = varlist.list ; sp ; sp = sp->next) { 757 if (sep != 0) 758 outc(sep, &errout); 759 out2str(sp->text); 760 sep = ' '; 761 } 762 for (sp = arglist.list ; sp ; sp = sp->next) { 763 if (sep != 0) 764 outc(sep, &errout); 765 out2str(sp->text); 766 sep = ' '; 767 } 768 outc('\n', &errout); 769 flushout(&errout); 770 } 771 772 /* Now locate the command. */ 773 if (argc == 0) { 774 cmdentry.cmdtype = CMDSPLBLTIN; 775 cmdentry.u.bltin = bltincmd; 776 } else { 777 static const char PATH[] = "PATH="; 778 int cmd_flags = DO_ERR; 779 780 /* 781 * Modify the command lookup path, if a PATH= assignment 782 * is present 783 */ 784 for (sp = varlist.list; sp; sp = sp->next) 785 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) 786 path = sp->text + sizeof(PATH) - 1; 787 788 do { 789 int argsused, use_syspath; 790 find_command(argv[0], &cmdentry, cmd_flags, path); 791 if (cmdentry.cmdtype == CMDUNKNOWN) { 792 exitstatus = 127; 793 flushout(&errout); 794 goto out; 795 } 796 797 /* implement the 'command' builtin here */ 798 if (cmdentry.cmdtype != CMDBUILTIN || 799 cmdentry.u.bltin != bltincmd) 800 break; 801 cmd_flags |= DO_NOFUNC; 802 argsused = parse_command_args(argc, argv, &use_syspath); 803 if (argsused == 0) { 804 /* use 'type' builting to display info */ 805 cmdentry.u.bltin = typecmd; 806 break; 807 } 808 argc -= argsused; 809 argv += argsused; 810 if (use_syspath) 811 path = syspath() + 5; 812 } while (argc != 0); 813 if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC) 814 /* posix mandates that 'command <splbltin>' act as if 815 <splbltin> was a normal builtin */ 816 cmdentry.cmdtype = CMDBUILTIN; 817 } 818 819 /* Fork off a child process if necessary. */ 820 if (cmd->ncmd.backgnd 821 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0) 822 || ((flags & EV_BACKCMD) != 0 823 && ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN) 824 || cmdentry.u.bltin == dotcmd 825 || cmdentry.u.bltin == evalcmd))) { 826 INTOFF; 827 jp = makejob(cmd, 1); 828 mode = cmd->ncmd.backgnd; 829 if (flags & EV_BACKCMD) { 830 mode = FORK_NOJOB; 831 if (sh_pipe(pip) < 0) 832 error("Pipe call failed"); 833 } 834 #ifdef DO_SHAREDVFORK 835 /* It is essential that if DO_SHAREDVFORK is defined that the 836 * child's address space is actually shared with the parent as 837 * we rely on this. 838 */ 839 if (cmdentry.cmdtype == CMDNORMAL) { 840 pid_t pid; 841 842 savelocalvars = localvars; 843 localvars = NULL; 844 vforked = 1; 845 switch (pid = vfork()) { 846 case -1: 847 TRACE(("Vfork failed, errno=%d\n", errno)); 848 INTON; 849 error("Cannot vfork"); 850 break; 851 case 0: 852 /* Make sure that exceptions only unwind to 853 * after the vfork(2) 854 */ 855 if (setjmp(jmploc.loc)) { 856 if (exception == EXSHELLPROC) { 857 /* We can't progress with the vfork, 858 * so, set vforked = 2 so the parent 859 * knows, and _exit(); 860 */ 861 vforked = 2; 862 _exit(0); 863 } else { 864 _exit(exerrno); 865 } 866 } 867 savehandler = handler; 868 handler = &jmploc; 869 listmklocal(varlist.list, VEXPORT | VNOFUNC); 870 forkchild(jp, cmd, mode, vforked); 871 break; 872 default: 873 handler = savehandler; /* restore from vfork(2) */ 874 poplocalvars(); 875 localvars = savelocalvars; 876 if (vforked == 2) { 877 vforked = 0; 878 879 (void)waitpid(pid, NULL, 0); 880 /* We need to progress in a normal fork fashion */ 881 goto normal_fork; 882 } 883 vforked = 0; 884 forkparent(jp, cmd, mode, pid); 885 goto parent; 886 } 887 } else { 888 normal_fork: 889 #endif 890 if (forkshell(jp, cmd, mode) != 0) 891 goto parent; /* at end of routine */ 892 FORCEINTON; 893 #ifdef DO_SHAREDVFORK 894 } 895 #endif 896 if (flags & EV_BACKCMD) { 897 if (!vforked) { 898 FORCEINTON; 899 } 900 close(pip[0]); 901 if (pip[1] != 1) { 902 close(1); 903 copyfd(pip[1], 1); 904 close(pip[1]); 905 } 906 } 907 flags |= EV_EXIT; 908 } 909 910 /* This is the child process if a fork occurred. */ 911 /* Execute the command. */ 912 switch (cmdentry.cmdtype) { 913 case CMDFUNCTION: 914 #ifdef DEBUG 915 trputs("Shell function: "); trargs(argv); 916 #endif 917 redirect(cmd->ncmd.redirect, REDIR_PUSH); 918 saveparam = shellparam; 919 shellparam.malloc = 0; 920 shellparam.reset = 1; 921 shellparam.nparam = argc - 1; 922 shellparam.p = argv + 1; 923 shellparam.optnext = NULL; 924 INTOFF; 925 savelocalvars = localvars; 926 localvars = NULL; 927 INTON; 928 if (setjmp(jmploc.loc)) { 929 if (exception == EXSHELLPROC) { 930 freeparam((volatile struct shparam *) 931 &saveparam); 932 } else { 933 freeparam(&shellparam); 934 shellparam = saveparam; 935 } 936 poplocalvars(); 937 localvars = savelocalvars; 938 handler = savehandler; 939 longjmp(handler->loc, 1); 940 } 941 savehandler = handler; 942 handler = &jmploc; 943 listmklocal(varlist.list, 0); 944 /* stop shell blowing its stack */ 945 if (++funcnest > 1000) 946 error("too many nested function calls"); 947 evaltree(cmdentry.u.func, flags & EV_TESTED); 948 funcnest--; 949 INTOFF; 950 poplocalvars(); 951 localvars = savelocalvars; 952 freeparam(&shellparam); 953 shellparam = saveparam; 954 handler = savehandler; 955 popredir(); 956 INTON; 957 if (evalskip == SKIPFUNC) { 958 evalskip = 0; 959 skipcount = 0; 960 } 961 if (flags & EV_EXIT) 962 exitshell(exitstatus); 963 break; 964 965 case CMDBUILTIN: 966 case CMDSPLBLTIN: 967 #ifdef DEBUG 968 trputs("builtin command: "); trargs(argv); 969 #endif 970 mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH; 971 if (flags == EV_BACKCMD) { 972 memout.nleft = 0; 973 memout.nextc = memout.buf; 974 memout.bufsize = 64; 975 mode |= REDIR_BACKQ; 976 } 977 e = -1; 978 savehandler = handler; 979 handler = &jmploc; 980 if (!setjmp(jmploc.loc)) { 981 /* We need to ensure the command hash table isn't 982 * corruped by temporary PATH assignments. 983 * However we must ensure the 'local' command works! 984 */ 985 if (path != pathval() && (cmdentry.u.bltin == hashcmd || 986 cmdentry.u.bltin == typecmd)) { 987 savelocalvars = localvars; 988 localvars = 0; 989 mklocal(path - 5 /* PATH= */, 0); 990 temp_path = 1; 991 } else 992 temp_path = 0; 993 redirect(cmd->ncmd.redirect, mode); 994 995 savecmdname = commandname; 996 /* exec is a special builtin, but needs this list... */ 997 cmdenviron = varlist.list; 998 /* we must check 'readonly' flag for all builtins */ 999 listsetvar(varlist.list, 1000 cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET); 1001 commandname = argv[0]; 1002 /* initialize nextopt */ 1003 argptr = argv + 1; 1004 optptr = NULL; 1005 /* and getopt */ 1006 optreset = 1; 1007 optind = 1; 1008 exitstatus = cmdentry.u.bltin(argc, argv); 1009 } else { 1010 e = exception; 1011 exitstatus = e == EXINT ? SIGINT + 128 : 1012 e == EXEXEC ? exerrno : 2; 1013 } 1014 handler = savehandler; 1015 flushall(); 1016 out1 = &output; 1017 out2 = &errout; 1018 freestdout(); 1019 if (temp_path) { 1020 poplocalvars(); 1021 localvars = savelocalvars; 1022 } 1023 cmdenviron = NULL; 1024 if (e != EXSHELLPROC) { 1025 commandname = savecmdname; 1026 if (flags & EV_EXIT) 1027 exitshell(exitstatus); 1028 } 1029 if (e != -1) { 1030 if ((e != EXERROR && e != EXEXEC) 1031 || cmdentry.cmdtype == CMDSPLBLTIN) 1032 exraise(e); 1033 FORCEINTON; 1034 } 1035 if (cmdentry.u.bltin != execcmd) 1036 popredir(); 1037 if (flags == EV_BACKCMD) { 1038 backcmd->buf = memout.buf; 1039 backcmd->nleft = memout.nextc - memout.buf; 1040 memout.buf = NULL; 1041 } 1042 break; 1043 1044 default: 1045 #ifdef DEBUG 1046 trputs("normal command: "); trargs(argv); 1047 #endif 1048 clearredir(vforked); 1049 redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0); 1050 if (!vforked) 1051 for (sp = varlist.list ; sp ; sp = sp->next) 1052 setvareq(sp->text, VEXPORT|VSTACK); 1053 envp = environment(); 1054 shellexec(argv, envp, path, cmdentry.u.index, vforked); 1055 break; 1056 } 1057 goto out; 1058 1059 parent: /* parent process gets here (if we forked) */ 1060 if (mode == FORK_FG) { /* argument to fork */ 1061 exitstatus = waitforjob(jp); 1062 } else if (mode == FORK_NOJOB) { 1063 backcmd->fd = pip[0]; 1064 close(pip[1]); 1065 backcmd->jp = jp; 1066 } 1067 FORCEINTON; 1068 1069 out: 1070 if (lastarg) 1071 /* dsl: I think this is intended to be used to support 1072 * '_' in 'vi' command mode during line editing... 1073 * However I implemented that within libedit itself. 1074 */ 1075 setvar("_", lastarg, 0); 1076 popstackmark(&smark); 1077 1078 if (eflag && exitstatus && !(flags & EV_TESTED)) 1079 exitshell(exitstatus); 1080 } 1081 1082 1083 /* 1084 * Search for a command. This is called before we fork so that the 1085 * location of the command will be available in the parent as well as 1086 * the child. The check for "goodname" is an overly conservative 1087 * check that the name will not be subject to expansion. 1088 */ 1089 1090 STATIC void 1091 prehash(union node *n) 1092 { 1093 struct cmdentry entry; 1094 1095 if (n->type == NCMD && n->ncmd.args) 1096 if (goodname(n->ncmd.args->narg.text)) 1097 find_command(n->ncmd.args->narg.text, &entry, 0, 1098 pathval()); 1099 } 1100 1101 1102 1103 /* 1104 * Builtin commands. Builtin commands whose functions are closely 1105 * tied to evaluation are implemented here. 1106 */ 1107 1108 /* 1109 * No command given. 1110 */ 1111 1112 int 1113 bltincmd(int argc, char **argv) 1114 { 1115 /* 1116 * Preserve exitstatus of a previous possible redirection 1117 * as POSIX mandates 1118 */ 1119 return back_exitstatus; 1120 } 1121 1122 1123 /* 1124 * Handle break and continue commands. Break, continue, and return are 1125 * all handled by setting the evalskip flag. The evaluation routines 1126 * above all check this flag, and if it is set they start skipping 1127 * commands rather than executing them. The variable skipcount is 1128 * the number of loops to break/continue, or the number of function 1129 * levels to return. (The latter is always 1.) It should probably 1130 * be an error to break out of more loops than exist, but it isn't 1131 * in the standard shell so we don't make it one here. 1132 */ 1133 1134 int 1135 breakcmd(int argc, char **argv) 1136 { 1137 int n = argc > 1 ? number(argv[1]) : 1; 1138 1139 if (n > loopnest) 1140 n = loopnest; 1141 if (n > 0) { 1142 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; 1143 skipcount = n; 1144 } 1145 return 0; 1146 } 1147 1148 1149 /* 1150 * The return command. 1151 */ 1152 1153 int 1154 returncmd(int argc, char **argv) 1155 { 1156 int ret = argc > 1 ? number(argv[1]) : exitstatus; 1157 1158 if (funcnest) { 1159 evalskip = SKIPFUNC; 1160 skipcount = 1; 1161 return ret; 1162 } 1163 else { 1164 /* Do what ksh does; skip the rest of the file */ 1165 evalskip = SKIPFILE; 1166 skipcount = 1; 1167 return ret; 1168 } 1169 } 1170 1171 1172 int 1173 falsecmd(int argc, char **argv) 1174 { 1175 return 1; 1176 } 1177 1178 1179 int 1180 truecmd(int argc, char **argv) 1181 { 1182 return 0; 1183 } 1184 1185 1186 int 1187 execcmd(int argc, char **argv) 1188 { 1189 if (argc > 1) { 1190 struct strlist *sp; 1191 1192 iflag = 0; /* exit on error */ 1193 mflag = 0; 1194 optschanged(); 1195 for (sp = cmdenviron; sp; sp = sp->next) 1196 setvareq(sp->text, VEXPORT|VSTACK); 1197 shellexec(argv + 1, environment(), pathval(), 0, 0); 1198 } 1199 return 0; 1200 } 1201 1202 static int 1203 conv_time(clock_t ticks, char *seconds, size_t l) 1204 { 1205 static clock_t tpm = 0; 1206 clock_t mins; 1207 int i; 1208 1209 if (!tpm) 1210 tpm = sysconf(_SC_CLK_TCK) * 60; 1211 1212 mins = ticks / tpm; 1213 snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm ); 1214 1215 if (seconds[0] == '6' && seconds[1] == '0') { 1216 /* 59.99995 got rounded up... */ 1217 mins++; 1218 strlcpy(seconds, "0.0", l); 1219 return mins; 1220 } 1221 1222 /* suppress trailing zeros */ 1223 i = strlen(seconds) - 1; 1224 for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--) 1225 seconds[i] = 0; 1226 return mins; 1227 } 1228 1229 int 1230 timescmd(int argc, char **argv) 1231 { 1232 struct tms tms; 1233 int u, s, cu, cs; 1234 char us[8], ss[8], cus[8], css[8]; 1235 1236 nextopt(""); 1237 1238 times(&tms); 1239 1240 u = conv_time(tms.tms_utime, us, sizeof(us)); 1241 s = conv_time(tms.tms_stime, ss, sizeof(ss)); 1242 cu = conv_time(tms.tms_cutime, cus, sizeof(cus)); 1243 cs = conv_time(tms.tms_cstime, css, sizeof(css)); 1244 1245 outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n", 1246 u, us, s, ss, cu, cus, cs, css); 1247 1248 return 0; 1249 } 1250