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