| 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 | #if 0
|
|---|
| 36 | #ifndef lint
|
|---|
| 37 | static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
|
|---|
| 38 | #else
|
|---|
| 39 | __RCSID("$NetBSD: eval.c,v 1.84 2005/06/23 23:05:29 christos Exp $");
|
|---|
| 40 | #endif /* not lint */
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 | #include <sys/types.h>
|
|---|
| 46 | #ifdef HAVE_SYSCTL_H
|
|---|
| 47 | # ifdef __OpenBSD__ /* joyful crap */
|
|---|
| 48 | # include <sys/param.h>
|
|---|
| 49 | # undef psh
|
|---|
| 50 | # endif
|
|---|
| 51 | # include <sys/sysctl.h>
|
|---|
| 52 | #endif
|
|---|
| 53 |
|
|---|
| 54 | /*
|
|---|
| 55 | * Evaluate a command.
|
|---|
| 56 | */
|
|---|
| 57 |
|
|---|
| 58 | #include "shell.h"
|
|---|
| 59 | #include "nodes.h"
|
|---|
| 60 | #include "syntax.h"
|
|---|
| 61 | #include "expand.h"
|
|---|
| 62 | #include "parser.h"
|
|---|
| 63 | #include "jobs.h"
|
|---|
| 64 | #include "eval.h"
|
|---|
| 65 | #include "builtins.h"
|
|---|
| 66 | #include "options.h"
|
|---|
| 67 | #include "exec.h"
|
|---|
| 68 | #include "redir.h"
|
|---|
| 69 | #include "input.h"
|
|---|
| 70 | #include "output.h"
|
|---|
| 71 | #include "trap.h"
|
|---|
| 72 | #include "var.h"
|
|---|
| 73 | #include "memalloc.h"
|
|---|
| 74 | #include "error.h"
|
|---|
| 75 | #include "show.h"
|
|---|
| 76 | #include "mystring.h"
|
|---|
| 77 | #include "main.h"
|
|---|
| 78 | #ifndef SMALL
|
|---|
| 79 | # include "myhistedit.h"
|
|---|
| 80 | #endif
|
|---|
| 81 | #include "shinstance.h"
|
|---|
| 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(shinstance *, union node *, int);
|
|---|
| 102 | STATIC void evalfor(shinstance *, union node *, int);
|
|---|
| 103 | STATIC void evalcase(shinstance *, union node *, int);
|
|---|
| 104 | STATIC void evalsubshell(shinstance *, union node *, int);
|
|---|
| 105 | STATIC void expredir(shinstance *, union node *);
|
|---|
| 106 | STATIC void evalpipe(shinstance *, union node *);
|
|---|
| 107 | STATIC void evalcommand(shinstance *, union node *, int, struct backcmd *);
|
|---|
| 108 | STATIC void prehash(shinstance *, 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 | psh->evalskip = 0;
|
|---|
| 120 | psh->loopnest = 0;
|
|---|
| 121 | psh->funcnest = 0;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | SHELLPROC {
|
|---|
| 125 | psh->exitstatus = 0;
|
|---|
| 126 | }
|
|---|
| 127 | #endif
|
|---|
| 128 |
|
|---|
| 129 | static int
|
|---|
| 130 | sh_pipe(shinstance *psh, int fds[2])
|
|---|
| 131 | {
|
|---|
| 132 | int nfd;
|
|---|
| 133 |
|
|---|
| 134 | if (shfile_pipe(&psh->fdtab, fds))
|
|---|
| 135 | return -1;
|
|---|
| 136 |
|
|---|
| 137 | if (fds[0] < 3) {
|
|---|
| 138 | nfd = shfile_fcntl(&psh->fdtab, fds[0], F_DUPFD, 3);
|
|---|
| 139 | if (nfd != -1) {
|
|---|
| 140 | shfile_close(&psh->fdtab, fds[0]);
|
|---|
| 141 | fds[0] = nfd;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | if (fds[1] < 3) {
|
|---|
| 146 | nfd = shfile_fcntl(&psh->fdtab, fds[1], F_DUPFD, 3);
|
|---|
| 147 | if (nfd != -1) {
|
|---|
| 148 | shfile_close(&psh->fdtab, 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(shinstance *psh, 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(psh, concat);
|
|---|
| 171 | ap = argv + 2;
|
|---|
| 172 | for (;;) {
|
|---|
| 173 | while (*p)
|
|---|
| 174 | STPUTC(psh, *p++, concat);
|
|---|
| 175 | if ((p = *ap++) == NULL)
|
|---|
| 176 | break;
|
|---|
| 177 | STPUTC(psh, ' ', concat);
|
|---|
| 178 | }
|
|---|
| 179 | STPUTC(psh, '\0', concat);
|
|---|
| 180 | p = grabstackstr(psh, concat);
|
|---|
| 181 | }
|
|---|
| 182 | evalstring(psh, p, EV_TESTED);
|
|---|
| 183 | }
|
|---|
| 184 | return psh->exitstatus;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 | /*
|
|---|
| 189 | * Execute a command or commands contained in a string.
|
|---|
| 190 | */
|
|---|
| 191 |
|
|---|
| 192 | void
|
|---|
| 193 | evalstring(shinstance *psh, char *s, int flag)
|
|---|
| 194 | {
|
|---|
| 195 | union node *n;
|
|---|
| 196 | struct stackmark smark;
|
|---|
| 197 |
|
|---|
| 198 | setstackmark(psh, &smark);
|
|---|
| 199 | setinputstring(psh, s, 1);
|
|---|
| 200 |
|
|---|
| 201 | while ((n = parsecmd(psh, 0)) != NEOF) {
|
|---|
| 202 | evaltree(psh, n, flag);
|
|---|
| 203 | popstackmark(psh, &smark);
|
|---|
| 204 | }
|
|---|
| 205 | popfile(psh);
|
|---|
| 206 | popstackmark(psh, &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(shinstance *psh, union node *n, int flags)
|
|---|
| 218 | {
|
|---|
| 219 | if (n == NULL) {
|
|---|
| 220 | TRACE((psh, "evaltree(NULL) called\n"));
|
|---|
| 221 | psh->exitstatus = 0;
|
|---|
| 222 | goto out;
|
|---|
| 223 | }
|
|---|
| 224 | #ifndef SMALL
|
|---|
| 225 | psh->displayhist = 1; /* show history substitutions done with fc */
|
|---|
| 226 | #endif
|
|---|
| 227 | TRACE((psh, "pid %d, evaltree(%p: %d, %d) called\n",
|
|---|
| 228 | sh_getpid(psh), n, n->type, flags));
|
|---|
| 229 | switch (n->type) {
|
|---|
| 230 | case NSEMI:
|
|---|
| 231 | evaltree(psh, n->nbinary.ch1, flags & EV_TESTED);
|
|---|
| 232 | if (psh->evalskip)
|
|---|
| 233 | goto out;
|
|---|
| 234 | evaltree(psh, n->nbinary.ch2, flags);
|
|---|
| 235 | break;
|
|---|
| 236 | case NAND:
|
|---|
| 237 | evaltree(psh, n->nbinary.ch1, EV_TESTED);
|
|---|
| 238 | if (psh->evalskip || psh->exitstatus != 0)
|
|---|
| 239 | goto out;
|
|---|
| 240 | evaltree(psh, n->nbinary.ch2, flags);
|
|---|
| 241 | break;
|
|---|
| 242 | case NOR:
|
|---|
| 243 | evaltree(psh, n->nbinary.ch1, EV_TESTED);
|
|---|
| 244 | if (psh->evalskip || psh->exitstatus == 0)
|
|---|
| 245 | goto out;
|
|---|
| 246 | evaltree(psh, n->nbinary.ch2, flags);
|
|---|
| 247 | break;
|
|---|
| 248 | case NREDIR:
|
|---|
| 249 | expredir(psh, n->nredir.redirect);
|
|---|
| 250 | redirect(psh, n->nredir.redirect, REDIR_PUSH);
|
|---|
| 251 | evaltree(psh, n->nredir.n, flags);
|
|---|
| 252 | popredir(psh);
|
|---|
| 253 | break;
|
|---|
| 254 | case NSUBSHELL:
|
|---|
| 255 | evalsubshell(psh, n, flags);
|
|---|
| 256 | break;
|
|---|
| 257 | case NBACKGND:
|
|---|
| 258 | evalsubshell(psh, n, flags);
|
|---|
| 259 | break;
|
|---|
| 260 | case NIF: {
|
|---|
| 261 | evaltree(psh, n->nif.test, EV_TESTED);
|
|---|
| 262 | if (psh->evalskip)
|
|---|
| 263 | goto out;
|
|---|
| 264 | if (psh->exitstatus == 0)
|
|---|
| 265 | evaltree(psh, n->nif.ifpart, flags);
|
|---|
| 266 | else if (n->nif.elsepart)
|
|---|
| 267 | evaltree(psh, n->nif.elsepart, flags);
|
|---|
| 268 | else
|
|---|
| 269 | psh->exitstatus = 0;
|
|---|
| 270 | break;
|
|---|
| 271 | }
|
|---|
| 272 | case NWHILE:
|
|---|
| 273 | case NUNTIL:
|
|---|
| 274 | evalloop(psh, n, flags);
|
|---|
| 275 | break;
|
|---|
| 276 | case NFOR:
|
|---|
| 277 | evalfor(psh, n, flags);
|
|---|
| 278 | break;
|
|---|
| 279 | case NCASE:
|
|---|
| 280 | evalcase(psh, n, flags);
|
|---|
| 281 | break;
|
|---|
| 282 | case NDEFUN:
|
|---|
| 283 | defun(psh, n->narg.text, n->narg.next);
|
|---|
| 284 | psh->exitstatus = 0;
|
|---|
| 285 | break;
|
|---|
| 286 | case NNOT:
|
|---|
| 287 | evaltree(psh, n->nnot.com, EV_TESTED);
|
|---|
| 288 | psh->exitstatus = !psh->exitstatus;
|
|---|
| 289 | break;
|
|---|
| 290 | case NPIPE:
|
|---|
| 291 | evalpipe(psh, n);
|
|---|
| 292 | break;
|
|---|
| 293 | case NCMD:
|
|---|
| 294 | evalcommand(psh, n, flags, (struct backcmd *)NULL);
|
|---|
| 295 | break;
|
|---|
| 296 | default:
|
|---|
| 297 | out1fmt(psh, "Node type = %d\n", n->type);
|
|---|
| 298 | flushout(&psh->output);
|
|---|
| 299 | break;
|
|---|
| 300 | }
|
|---|
| 301 | out:
|
|---|
| 302 | if (psh->pendingsigs)
|
|---|
| 303 | dotrap(psh);
|
|---|
| 304 | if ((flags & EV_EXIT) != 0)
|
|---|
| 305 | exitshell(psh, psh->exitstatus);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 | STATIC void
|
|---|
| 310 | evalloop(shinstance *psh, union node *n, int flags)
|
|---|
| 311 | {
|
|---|
| 312 | int status;
|
|---|
| 313 |
|
|---|
| 314 | psh->loopnest++;
|
|---|
| 315 | status = 0;
|
|---|
| 316 | for (;;) {
|
|---|
| 317 | evaltree(psh, n->nbinary.ch1, EV_TESTED);
|
|---|
| 318 | if (psh->evalskip) {
|
|---|
| 319 | skipping: if (psh->evalskip == SKIPCONT && --psh->skipcount <= 0) {
|
|---|
| 320 | psh->evalskip = 0;
|
|---|
| 321 | continue;
|
|---|
| 322 | }
|
|---|
| 323 | if (psh->evalskip == SKIPBREAK && --psh->skipcount <= 0)
|
|---|
| 324 | psh->evalskip = 0;
|
|---|
| 325 | break;
|
|---|
| 326 | }
|
|---|
| 327 | if (n->type == NWHILE) {
|
|---|
| 328 | if (psh->exitstatus != 0)
|
|---|
| 329 | break;
|
|---|
| 330 | } else {
|
|---|
| 331 | if (psh->exitstatus == 0)
|
|---|
| 332 | break;
|
|---|
| 333 | }
|
|---|
| 334 | evaltree(psh, n->nbinary.ch2, flags & EV_TESTED);
|
|---|
| 335 | status = psh->exitstatus;
|
|---|
| 336 | if (psh->evalskip)
|
|---|
| 337 | goto skipping;
|
|---|
| 338 | }
|
|---|
| 339 | psh->loopnest--;
|
|---|
| 340 | psh->exitstatus = status;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 | STATIC void
|
|---|
| 346 | evalfor(shinstance *psh, 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(psh, &smark);
|
|---|
| 355 | arglist.lastp = &arglist.list;
|
|---|
| 356 | for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
|
|---|
| 357 | expandarg(psh, argp, &arglist, EXP_FULL | EXP_TILDE);
|
|---|
| 358 | if (psh->evalskip)
|
|---|
| 359 | goto out;
|
|---|
| 360 | }
|
|---|
| 361 | *arglist.lastp = NULL;
|
|---|
| 362 |
|
|---|
| 363 | psh->loopnest++;
|
|---|
| 364 | for (sp = arglist.list ; sp ; sp = sp->next) {
|
|---|
| 365 | setvar(psh, n->nfor.var, sp->text, 0);
|
|---|
| 366 | evaltree(psh, n->nfor.body, flags & EV_TESTED);
|
|---|
| 367 | status = psh->exitstatus;
|
|---|
| 368 | if (psh->evalskip) {
|
|---|
| 369 | if (psh->evalskip == SKIPCONT && --psh->skipcount <= 0) {
|
|---|
| 370 | psh->evalskip = 0;
|
|---|
| 371 | continue;
|
|---|
| 372 | }
|
|---|
| 373 | if (psh->evalskip == SKIPBREAK && --psh->skipcount <= 0)
|
|---|
| 374 | psh->evalskip = 0;
|
|---|
| 375 | break;
|
|---|
| 376 | }
|
|---|
| 377 | }
|
|---|
| 378 | psh->loopnest--;
|
|---|
| 379 | psh->exitstatus = status;
|
|---|
| 380 | out:
|
|---|
| 381 | popstackmark(psh, &smark);
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 | STATIC void
|
|---|
| 387 | evalcase(shinstance *psh, 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(psh, &smark);
|
|---|
| 396 | arglist.lastp = &arglist.list;
|
|---|
| 397 | expandarg(psh, n->ncase.expr, &arglist, EXP_TILDE);
|
|---|
| 398 | for (cp = n->ncase.cases ; cp && psh->evalskip == 0 ; cp = cp->nclist.next) {
|
|---|
| 399 | for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
|
|---|
| 400 | if (casematch(psh, patp, arglist.list->text)) {
|
|---|
| 401 | if (psh->evalskip == 0) {
|
|---|
| 402 | evaltree(psh, cp->nclist.body, flags);
|
|---|
| 403 | status = psh->exitstatus;
|
|---|
| 404 | }
|
|---|
| 405 | goto out;
|
|---|
| 406 | }
|
|---|
| 407 | }
|
|---|
| 408 | }
|
|---|
| 409 | out:
|
|---|
| 410 | psh->exitstatus = status;
|
|---|
| 411 | popstackmark(psh, &smark);
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 | /*
|
|---|
| 417 | * Kick off a subshell to evaluate a tree.
|
|---|
| 418 | */
|
|---|
| 419 |
|
|---|
| 420 | STATIC void
|
|---|
| 421 | evalsubshell(shinstance *psh, union node *n, int flags)
|
|---|
| 422 | {
|
|---|
| 423 | struct job *jp;
|
|---|
| 424 | int backgnd = (n->type == NBACKGND);
|
|---|
| 425 |
|
|---|
| 426 | expredir(psh, n->nredir.redirect);
|
|---|
| 427 | INTOFF;
|
|---|
| 428 | jp = makejob(psh, n, 1);
|
|---|
| 429 | if (forkshell(psh, jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
|
|---|
| 430 | INTON;
|
|---|
| 431 | if (backgnd)
|
|---|
| 432 | flags &=~ EV_TESTED;
|
|---|
| 433 | redirect(psh, n->nredir.redirect, 0);
|
|---|
| 434 | /* never returns */
|
|---|
| 435 | evaltree(psh, n->nredir.n, flags | EV_EXIT);
|
|---|
| 436 | }
|
|---|
| 437 | if (! backgnd)
|
|---|
| 438 | psh->exitstatus = waitforjob(psh, 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(shinstance *psh, 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(psh, 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(psh, redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
|
|---|
| 469 | fixredir(psh, 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(shinstance *psh, 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((psh, "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(psh, n, pipelen);
|
|---|
| 500 | prevfd = -1;
|
|---|
| 501 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
|
|---|
| 502 | prehash(psh, lp->n);
|
|---|
| 503 | pip[1] = -1;
|
|---|
| 504 | if (lp->next) {
|
|---|
| 505 | if (sh_pipe(psh, pip) < 0) {
|
|---|
| 506 | shfile_close(&psh->fdtab, prevfd);
|
|---|
| 507 | error(psh, "Pipe call failed");
|
|---|
| 508 | }
|
|---|
| 509 | }
|
|---|
| 510 | if (forkshell(psh, jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
|
|---|
| 511 | INTON;
|
|---|
| 512 | if (prevfd > 0) {
|
|---|
| 513 | movefd(psh, prevfd, 0);
|
|---|
| 514 | }
|
|---|
| 515 | if (pip[1] >= 0) {
|
|---|
| 516 | shfile_close(&psh->fdtab, pip[0]);
|
|---|
| 517 | if (pip[1] != 1) {
|
|---|
| 518 | movefd(psh, pip[1], 1);
|
|---|
| 519 | }
|
|---|
| 520 | }
|
|---|
| 521 | evaltree(psh, lp->n, EV_EXIT);
|
|---|
| 522 | }
|
|---|
| 523 | if (prevfd >= 0)
|
|---|
| 524 | shfile_close(&psh->fdtab, prevfd);
|
|---|
| 525 | prevfd = pip[0];
|
|---|
| 526 | shfile_close(&psh->fdtab, pip[1]);
|
|---|
| 527 | }
|
|---|
| 528 | if (n->npipe.backgnd == 0) {
|
|---|
| 529 | psh->exitstatus = waitforjob(psh, jp);
|
|---|
| 530 | TRACE((psh, "evalpipe: job done exit status %d\n", psh->exitstatus));
|
|---|
| 531 | }
|
|---|
| 532 | INTON;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 | /*
|
|---|
| 538 | * Execute a command inside back quotes. If it's a builtin command, we
|
|---|
| 539 | * want to save its output in a block obtained from malloc. Otherwise
|
|---|
| 540 | * we fork off a subprocess and get the output of the command via a pipe.
|
|---|
| 541 | * Should be called with interrupts off.
|
|---|
| 542 | */
|
|---|
| 543 |
|
|---|
| 544 | void
|
|---|
| 545 | evalbackcmd(shinstance *psh, union node *n, struct backcmd *result)
|
|---|
| 546 | {
|
|---|
| 547 | int pip[2];
|
|---|
| 548 | struct job *jp;
|
|---|
| 549 | struct stackmark smark; /* unnecessary */
|
|---|
| 550 |
|
|---|
| 551 | setstackmark(psh, &smark);
|
|---|
| 552 | result->fd = -1;
|
|---|
| 553 | result->buf = NULL;
|
|---|
| 554 | result->nleft = 0;
|
|---|
| 555 | result->jp = NULL;
|
|---|
| 556 | if (n == NULL) {
|
|---|
| 557 | goto out;
|
|---|
| 558 | }
|
|---|
| 559 | #ifdef notyet
|
|---|
| 560 | /*
|
|---|
| 561 | * For now we disable executing builtins in the same
|
|---|
| 562 | * context as the shell, because we are not keeping
|
|---|
| 563 | * enough state to recover from changes that are
|
|---|
| 564 | * supposed only to affect subshells. eg. echo "`cd /`"
|
|---|
| 565 | */
|
|---|
| 566 | if (n->type == NCMD) {
|
|---|
| 567 | psh->exitstatus = opsh->exitstatus;
|
|---|
| 568 | evalcommand(psh, n, EV_BACKCMD, result);
|
|---|
| 569 | } else
|
|---|
| 570 | #endif
|
|---|
| 571 | {
|
|---|
| 572 | INTOFF;
|
|---|
| 573 | if (sh_pipe(psh, pip) < 0)
|
|---|
| 574 | error(psh, "Pipe call failed");
|
|---|
| 575 | jp = makejob(psh, n, 1);
|
|---|
| 576 | if (forkshell(psh, jp, n, FORK_NOJOB) == 0) {
|
|---|
| 577 | FORCEINTON;
|
|---|
| 578 | shfile_close(&psh->fdtab, pip[0]);
|
|---|
| 579 | if (pip[1] != 1) {
|
|---|
| 580 | movefd(psh, pip[1], 1);
|
|---|
| 581 | }
|
|---|
| 582 | eflag(psh) = 0;
|
|---|
| 583 | evaltree(psh, n, EV_EXIT);
|
|---|
| 584 | /* NOTREACHED */
|
|---|
| 585 | }
|
|---|
| 586 | shfile_close(&psh->fdtab, pip[1]);
|
|---|
| 587 | result->fd = pip[0];
|
|---|
| 588 | result->jp = jp;
|
|---|
| 589 | INTON;
|
|---|
| 590 | }
|
|---|
| 591 | out:
|
|---|
| 592 | popstackmark(psh, &smark);
|
|---|
| 593 | TRACE((psh, "evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
|
|---|
| 594 | result->fd, result->buf, result->nleft, result->jp));
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | static const char *
|
|---|
| 598 | syspath(shinstance *psh)
|
|---|
| 599 | {
|
|---|
| 600 | #ifdef CTL_USER
|
|---|
| 601 | static char *sys_path = NULL;
|
|---|
| 602 | static int mib[] = {CTL_USER, USER_CS_PATH};
|
|---|
| 603 | #endif
|
|---|
| 604 | #ifdef PC_PATH_SEP
|
|---|
| 605 | static char def_path[] = "PATH=/usr/bin;/bin;/usr/sbin;/sbin";
|
|---|
| 606 | #else
|
|---|
| 607 | static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
|
|---|
| 608 | #endif
|
|---|
| 609 | #ifdef CTL_USER
|
|---|
| 610 | size_t len;
|
|---|
| 611 |
|
|---|
| 612 | if (sys_path == NULL) {
|
|---|
| 613 | if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
|
|---|
| 614 | (sys_path = ckmalloc(psh, len + 5)) != NULL &&
|
|---|
| 615 | sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
|
|---|
| 616 | memcpy(sys_path, "PATH=", 5);
|
|---|
| 617 | } else {
|
|---|
| 618 | ckfree(psh, sys_path);
|
|---|
| 619 | /* something to keep things happy */
|
|---|
| 620 | sys_path = def_path;
|
|---|
| 621 | }
|
|---|
| 622 | }
|
|---|
| 623 | return sys_path;
|
|---|
| 624 | #else
|
|---|
| 625 | return def_path;
|
|---|
| 626 | #endif
|
|---|
| 627 | }
|
|---|
| 628 |
|
|---|
| 629 | static int
|
|---|
| 630 | parse_command_args(shinstance *psh, int argc, char **argv, int *use_syspath)
|
|---|
| 631 | {
|
|---|
| 632 | int sv_argc = argc;
|
|---|
| 633 | char *cp, c;
|
|---|
| 634 |
|
|---|
| 635 | *use_syspath = 0;
|
|---|
| 636 |
|
|---|
| 637 | for (;;) {
|
|---|
| 638 | argv++;
|
|---|
| 639 | if (--argc == 0)
|
|---|
| 640 | break;
|
|---|
| 641 | cp = *argv;
|
|---|
| 642 | if (*cp++ != '-')
|
|---|
| 643 | break;
|
|---|
| 644 | if (*cp == '-' && cp[1] == 0) {
|
|---|
| 645 | argv++;
|
|---|
| 646 | argc--;
|
|---|
| 647 | break;
|
|---|
| 648 | }
|
|---|
| 649 | while ((c = *cp++)) {
|
|---|
| 650 | switch (c) {
|
|---|
| 651 | case 'p':
|
|---|
| 652 | *use_syspath = 1;
|
|---|
| 653 | break;
|
|---|
| 654 | default:
|
|---|
| 655 | /* run 'typecmd' for other options */
|
|---|
| 656 | return 0;
|
|---|
| 657 | }
|
|---|
| 658 | }
|
|---|
| 659 | }
|
|---|
| 660 | return sv_argc - argc;
|
|---|
| 661 | }
|
|---|
| 662 |
|
|---|
| 663 | /*int vforked = 0;*/
|
|---|
| 664 |
|
|---|
| 665 | /*
|
|---|
| 666 | * Execute a simple command.
|
|---|
| 667 | */
|
|---|
| 668 |
|
|---|
| 669 | STATIC void
|
|---|
| 670 | evalcommand(shinstance *psh, union node *cmd, int flags, struct backcmd *backcmd)
|
|---|
| 671 | {
|
|---|
| 672 | struct stackmark smark;
|
|---|
| 673 | union node *argp;
|
|---|
| 674 | struct arglist arglist;
|
|---|
| 675 | struct arglist varlist;
|
|---|
| 676 | char **argv;
|
|---|
| 677 | int argc;
|
|---|
| 678 | char **envp;
|
|---|
| 679 | int numvars;
|
|---|
| 680 | struct strlist *sp;
|
|---|
| 681 | int mode = 0;
|
|---|
| 682 | int pip[2];
|
|---|
| 683 | struct cmdentry cmdentry;
|
|---|
| 684 | struct job *jp;
|
|---|
| 685 | struct jmploc jmploc;
|
|---|
| 686 | struct jmploc *volatile savehandler;
|
|---|
| 687 | char *volatile savecmdname;
|
|---|
| 688 | volatile struct shparam saveparam;
|
|---|
| 689 | struct localvar *volatile savelocalvars;
|
|---|
| 690 | volatile int e;
|
|---|
| 691 | char *lastarg;
|
|---|
| 692 | const char *path = pathval(psh);
|
|---|
| 693 | volatile int temp_path;
|
|---|
| 694 | #if __GNUC__
|
|---|
| 695 | /* Try avoid longjmp clobbering */
|
|---|
| 696 | (void) &argv;
|
|---|
| 697 | (void) &argc;
|
|---|
| 698 | (void) &lastarg;
|
|---|
| 699 | (void) &flags;
|
|---|
| 700 | (void) &path;
|
|---|
| 701 | (void) &mode;
|
|---|
| 702 | #endif
|
|---|
| 703 |
|
|---|
| 704 | psh->vforked = 0;
|
|---|
| 705 | /* First expand the arguments. */
|
|---|
| 706 | TRACE((psh, "evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
|
|---|
| 707 | setstackmark(psh, &smark);
|
|---|
| 708 | psh->back_exitstatus = 0;
|
|---|
| 709 |
|
|---|
| 710 | arglist.lastp = &arglist.list;
|
|---|
| 711 | /* Expand arguments, ignoring the initial 'name=value' ones */
|
|---|
| 712 | for (argp = cmd->ncmd.args, numvars = 0 ; argp ; argp = argp->narg.next, numvars++) {
|
|---|
| 713 | char *p = argp->narg.text;
|
|---|
| 714 | char ch = *p;
|
|---|
| 715 | if (is_name(ch)) {
|
|---|
| 716 | do ch = *++p;
|
|---|
| 717 | while (is_in_name(ch));
|
|---|
| 718 | if (ch == '=')
|
|---|
| 719 | continue;
|
|---|
| 720 | }
|
|---|
| 721 | break;
|
|---|
| 722 | }
|
|---|
| 723 | for (/*continue on argp from above. */ ; argp ; argp = argp->narg.next)
|
|---|
| 724 | expandarg(psh, argp, &arglist, EXP_FULL | EXP_TILDE);
|
|---|
| 725 | *arglist.lastp = NULL;
|
|---|
| 726 |
|
|---|
| 727 | expredir(psh, cmd->ncmd.redirect);
|
|---|
| 728 |
|
|---|
| 729 | /* Now do the initial 'name=value' ones we skipped above */
|
|---|
| 730 | varlist.lastp = &varlist.list;
|
|---|
| 731 | for (argp = cmd->ncmd.args ; numvars > 0 && argp ; argp = argp->narg.next, numvars--)
|
|---|
| 732 | expandarg(psh, argp, &varlist, EXP_VARTILDE);
|
|---|
| 733 | *varlist.lastp = NULL;
|
|---|
| 734 |
|
|---|
| 735 | argc = 0;
|
|---|
| 736 | for (sp = arglist.list ; sp ; sp = sp->next)
|
|---|
| 737 | argc++;
|
|---|
| 738 | argv = stalloc(psh, sizeof (char *) * (argc + 1));
|
|---|
| 739 |
|
|---|
| 740 | for (sp = arglist.list ; sp ; sp = sp->next) {
|
|---|
| 741 | TRACE((psh, "evalcommand arg: %s\n", sp->text));
|
|---|
| 742 | *argv++ = sp->text;
|
|---|
| 743 | }
|
|---|
| 744 | *argv = NULL;
|
|---|
| 745 | lastarg = NULL;
|
|---|
| 746 | if (iflag(psh) && psh->funcnest == 0 && argc > 0)
|
|---|
| 747 | lastarg = argv[-1];
|
|---|
| 748 | argv -= argc;
|
|---|
| 749 |
|
|---|
| 750 | /* Print the command if xflag is set. */
|
|---|
| 751 | if (xflag(psh)) {
|
|---|
| 752 | char sep = 0;
|
|---|
| 753 | out2str(psh, ps4val(psh));
|
|---|
| 754 | for (sp = varlist.list ; sp ; sp = sp->next) {
|
|---|
| 755 | if (sep != 0)
|
|---|
| 756 | outc(sep, &psh->errout);
|
|---|
| 757 | out2str(psh, sp->text);
|
|---|
| 758 | sep = ' ';
|
|---|
| 759 | }
|
|---|
| 760 | for (sp = arglist.list ; sp ; sp = sp->next) {
|
|---|
| 761 | if (sep != 0)
|
|---|
| 762 | outc(sep, &psh->errout);
|
|---|
| 763 | out2str(psh, sp->text);
|
|---|
| 764 | sep = ' ';
|
|---|
| 765 | }
|
|---|
| 766 | outc('\n', &psh->errout);
|
|---|
| 767 | flushout(&psh->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(psh, argv[0], &cmdentry, cmd_flags, path);
|
|---|
| 789 | if (cmdentry.cmdtype == CMDUNKNOWN) {
|
|---|
| 790 | psh->exitstatus = 127;
|
|---|
| 791 | flushout(&psh->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(psh, 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(psh) + 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(psh, cmd, 1);
|
|---|
| 826 | mode = cmd->ncmd.backgnd;
|
|---|
| 827 | if (flags & EV_BACKCMD) {
|
|---|
| 828 | mode = FORK_NOJOB;
|
|---|
| 829 | if (sh_pipe(psh, pip) < 0)
|
|---|
| 830 | error(psh, "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 = psh->localvars;
|
|---|
| 841 | psh->localvars = NULL;
|
|---|
| 842 | psh->vforked = 1;
|
|---|
| 843 | switch (pid = vfork()) {
|
|---|
| 844 | case -1:
|
|---|
| 845 | TRACE((psh, "Vfork failed, errno=%d\n", errno));
|
|---|
| 846 | INTON;
|
|---|
| 847 | error(psh, "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 (psh->exception == EXSHELLPROC) {
|
|---|
| 855 | /* We can't progress with the vfork,
|
|---|
| 856 | * so, set vforked = 2 so the parent
|
|---|
| 857 | * knows, and _exit();
|
|---|
| 858 | */
|
|---|
| 859 | psh->vforked = 2;
|
|---|
| 860 | sh__exit(psh, 0);
|
|---|
| 861 | } else {
|
|---|
| 862 | sh__exit(psh, psh->exerrno);
|
|---|
| 863 | }
|
|---|
| 864 | }
|
|---|
| 865 | savehandler = psh->handler;
|
|---|
| 866 | psh->handler = &jmploc;
|
|---|
| 867 | listmklocal(psh, varlist.list, VEXPORT | VNOFUNC);
|
|---|
| 868 | forkchild(psh, jp, cmd, mode, psh->vforked);
|
|---|
| 869 | break;
|
|---|
| 870 | default:
|
|---|
| 871 | psh->handler = savehandler; /* restore from vfork(2) */
|
|---|
| 872 | poplocalvars(psh);
|
|---|
| 873 | psh->localvars = savelocalvars;
|
|---|
| 874 | if (psh->vforked == 2) {
|
|---|
| 875 | psh->vforked = 0;
|
|---|
| 876 |
|
|---|
| 877 | (void)sh_waitpid(psh, pid, NULL, 0);
|
|---|
| 878 | /* We need to progress in a normal fork fashion */
|
|---|
| 879 | goto normal_fork;
|
|---|
| 880 | }
|
|---|
| 881 | psh->vforked = 0;
|
|---|
| 882 | forkparent(psh, jp, cmd, mode, pid);
|
|---|
| 883 | goto parent;
|
|---|
| 884 | }
|
|---|
| 885 | } else {
|
|---|
| 886 | normal_fork:
|
|---|
| 887 | #endif
|
|---|
| 888 | if (forkshell(psh, 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 (!psh->vforked) {
|
|---|
| 896 | FORCEINTON;
|
|---|
| 897 | }
|
|---|
| 898 | shfile_close(&psh->fdtab, pip[0]);
|
|---|
| 899 | if (pip[1] != 1) {
|
|---|
| 900 | movefd(psh, pip[1], 1);
|
|---|
| 901 | }
|
|---|
| 902 | }
|
|---|
| 903 | flags |= EV_EXIT;
|
|---|
| 904 | }
|
|---|
| 905 |
|
|---|
| 906 | /* This is the child process if a fork occurred. */
|
|---|
| 907 | /* Execute the command. */
|
|---|
| 908 | switch (cmdentry.cmdtype) {
|
|---|
| 909 | case CMDFUNCTION:
|
|---|
| 910 | #ifdef DEBUG
|
|---|
| 911 | trputs(psh, "Shell function: "); trargs(psh, argv);
|
|---|
| 912 | #endif
|
|---|
| 913 | redirect(psh, cmd->ncmd.redirect, REDIR_PUSH);
|
|---|
| 914 | saveparam = psh->shellparam;
|
|---|
| 915 | psh->shellparam.malloc = 0;
|
|---|
| 916 | psh->shellparam.reset = 1;
|
|---|
| 917 | psh->shellparam.nparam = argc - 1;
|
|---|
| 918 | psh->shellparam.p = argv + 1;
|
|---|
| 919 | psh->shellparam.optnext = NULL;
|
|---|
| 920 | INTOFF;
|
|---|
| 921 | savelocalvars = psh->localvars;
|
|---|
| 922 | psh->localvars = NULL;
|
|---|
| 923 | INTON;
|
|---|
| 924 | if (setjmp(jmploc.loc)) {
|
|---|
| 925 | if (psh->exception == EXSHELLPROC) {
|
|---|
| 926 | freeparam(psh, (volatile struct shparam *)
|
|---|
| 927 | &saveparam);
|
|---|
| 928 | } else {
|
|---|
| 929 | freeparam(psh, &psh->shellparam);
|
|---|
| 930 | psh->shellparam = saveparam;
|
|---|
| 931 | }
|
|---|
| 932 | poplocalvars(psh);
|
|---|
| 933 | psh->localvars = savelocalvars;
|
|---|
| 934 | psh->handler = savehandler;
|
|---|
| 935 | longjmp(psh->handler->loc, 1);
|
|---|
| 936 | }
|
|---|
| 937 | savehandler = psh->handler;
|
|---|
| 938 | psh->handler = &jmploc;
|
|---|
| 939 | listmklocal(psh, varlist.list, 0);
|
|---|
| 940 | /* stop shell blowing its stack */
|
|---|
| 941 | if (++psh->funcnest > 1000)
|
|---|
| 942 | error(psh, "too many nested function calls");
|
|---|
| 943 | evaltree(psh, cmdentry.u.func, flags & EV_TESTED);
|
|---|
| 944 | psh->funcnest--;
|
|---|
| 945 | INTOFF;
|
|---|
| 946 | poplocalvars(psh);
|
|---|
| 947 | psh->localvars = savelocalvars;
|
|---|
| 948 | freeparam(psh, &psh->shellparam);
|
|---|
| 949 | psh->shellparam = saveparam;
|
|---|
| 950 | psh->handler = savehandler;
|
|---|
| 951 | popredir(psh);
|
|---|
| 952 | INTON;
|
|---|
| 953 | if (psh->evalskip == SKIPFUNC) {
|
|---|
| 954 | psh->evalskip = 0;
|
|---|
| 955 | psh->skipcount = 0;
|
|---|
| 956 | }
|
|---|
| 957 | if (flags & EV_EXIT)
|
|---|
| 958 | exitshell(psh, psh->exitstatus);
|
|---|
| 959 | break;
|
|---|
| 960 |
|
|---|
| 961 | case CMDBUILTIN:
|
|---|
| 962 | case CMDSPLBLTIN:
|
|---|
| 963 | #ifdef DEBUG
|
|---|
| 964 | trputs(psh, "builtin command: "); trargs(psh, argv);
|
|---|
| 965 | #endif
|
|---|
| 966 | mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
|
|---|
| 967 | if (flags == EV_BACKCMD) {
|
|---|
| 968 | psh->memout.nleft = 0;
|
|---|
| 969 | psh->memout.nextc = psh->memout.buf;
|
|---|
| 970 | psh->memout.bufsize = 64;
|
|---|
| 971 | mode |= REDIR_BACKQ;
|
|---|
| 972 | }
|
|---|
| 973 | e = -1;
|
|---|
| 974 | savehandler = psh->handler;
|
|---|
| 975 | savecmdname = psh->commandname;
|
|---|
| 976 | psh->handler = &jmploc;
|
|---|
| 977 | if (!setjmp(jmploc.loc)) {
|
|---|
| 978 | /* We need to ensure the command hash table isn't
|
|---|
| 979 | * corruped by temporary PATH assignments.
|
|---|
| 980 | * However we must ensure the 'local' command works!
|
|---|
| 981 | */
|
|---|
| 982 | if (path != pathval(psh) && (cmdentry.u.bltin == hashcmd ||
|
|---|
| 983 | cmdentry.u.bltin == typecmd)) {
|
|---|
| 984 | savelocalvars = psh->localvars;
|
|---|
| 985 | psh->localvars = 0;
|
|---|
| 986 | mklocal(psh, path - 5 /* PATH= */, 0);
|
|---|
| 987 | temp_path = 1;
|
|---|
| 988 | } else
|
|---|
| 989 | temp_path = 0;
|
|---|
| 990 | redirect(psh, cmd->ncmd.redirect, mode);
|
|---|
| 991 |
|
|---|
| 992 | /* exec is a special builtin, but needs this list... */
|
|---|
| 993 | psh->cmdenviron = varlist.list;
|
|---|
| 994 | /* we must check 'readonly' flag for all builtins */
|
|---|
| 995 | listsetvar(psh, varlist.list,
|
|---|
| 996 | cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
|
|---|
| 997 | psh->commandname = argv[0];
|
|---|
| 998 | /* initialize nextopt */
|
|---|
| 999 | psh->argptr = argv + 1;
|
|---|
| 1000 | psh->optptr = NULL;
|
|---|
| 1001 | /* and getopt */
|
|---|
| 1002 | #if 0 /** @todo fix getop usage! */
|
|---|
| 1003 | #if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
|
|---|
| 1004 | optreset = 1;
|
|---|
| 1005 | optind = 1;
|
|---|
| 1006 | #else
|
|---|
| 1007 | optind = 0; /* init */
|
|---|
| 1008 | #endif
|
|---|
| 1009 | #endif
|
|---|
| 1010 |
|
|---|
| 1011 | psh->exitstatus = cmdentry.u.bltin(psh, argc, argv);
|
|---|
| 1012 | } else {
|
|---|
| 1013 | e = psh->exception;
|
|---|
| 1014 | psh->exitstatus = e == EXINT ? SIGINT + 128 :
|
|---|
| 1015 | e == EXEXEC ? psh->exerrno : 2;
|
|---|
| 1016 | }
|
|---|
| 1017 | psh->handler = savehandler;
|
|---|
| 1018 | output_flushall(psh);
|
|---|
| 1019 | psh->out1 = &psh->output;
|
|---|
| 1020 | psh->out2 = &psh->errout;
|
|---|
| 1021 | freestdout(psh);
|
|---|
| 1022 | if (temp_path) {
|
|---|
| 1023 | poplocalvars(psh);
|
|---|
| 1024 | psh->localvars = savelocalvars;
|
|---|
| 1025 | }
|
|---|
| 1026 | psh->cmdenviron = NULL;
|
|---|
| 1027 | if (e != EXSHELLPROC) {
|
|---|
| 1028 | psh->commandname = savecmdname;
|
|---|
| 1029 | if (flags & EV_EXIT)
|
|---|
| 1030 | exitshell(psh, psh->exitstatus);
|
|---|
| 1031 | }
|
|---|
| 1032 | if (e != -1) {
|
|---|
| 1033 | if ((e != EXERROR && e != EXEXEC)
|
|---|
| 1034 | || cmdentry.cmdtype == CMDSPLBLTIN)
|
|---|
| 1035 | exraise(psh, e);
|
|---|
| 1036 | FORCEINTON;
|
|---|
| 1037 | }
|
|---|
| 1038 | if (cmdentry.u.bltin != execcmd)
|
|---|
| 1039 | popredir(psh);
|
|---|
| 1040 | if (flags == EV_BACKCMD) {
|
|---|
| 1041 | backcmd->buf = psh->memout.buf;
|
|---|
| 1042 | backcmd->nleft = (int)(psh->memout.nextc - psh->memout.buf);
|
|---|
| 1043 | psh->memout.buf = NULL;
|
|---|
| 1044 | }
|
|---|
| 1045 | break;
|
|---|
| 1046 |
|
|---|
| 1047 | default:
|
|---|
| 1048 | #ifdef DEBUG
|
|---|
| 1049 | trputs(psh, "normal command: "); trargs(psh, argv);
|
|---|
| 1050 | #endif
|
|---|
| 1051 | clearredir(psh, psh->vforked);
|
|---|
| 1052 | redirect(psh, cmd->ncmd.redirect, psh->vforked ? REDIR_VFORK : 0);
|
|---|
| 1053 | if (!psh->vforked)
|
|---|
| 1054 | for (sp = varlist.list ; sp ; sp = sp->next)
|
|---|
| 1055 | setvareq(psh, sp->text, VEXPORT|VSTACK);
|
|---|
| 1056 | envp = environment(psh);
|
|---|
| 1057 | shellexec(psh, argv, envp, path, cmdentry.u.index, psh->vforked);
|
|---|
| 1058 | break;
|
|---|
| 1059 | }
|
|---|
| 1060 | goto out;
|
|---|
| 1061 |
|
|---|
| 1062 | parent: /* parent process gets here (if we forked) */
|
|---|
| 1063 | if (mode == FORK_FG) { /* argument to fork */
|
|---|
| 1064 | psh->exitstatus = waitforjob(psh, jp);
|
|---|
| 1065 | } else if (mode == FORK_NOJOB) {
|
|---|
| 1066 | backcmd->fd = pip[0];
|
|---|
| 1067 | shfile_close(&psh->fdtab, pip[1]);
|
|---|
| 1068 | backcmd->jp = jp;
|
|---|
| 1069 | }
|
|---|
| 1070 | FORCEINTON;
|
|---|
| 1071 |
|
|---|
| 1072 | out:
|
|---|
| 1073 | if (lastarg)
|
|---|
| 1074 | /* dsl: I think this is intended to be used to support
|
|---|
| 1075 | * '_' in 'vi' command mode during line editing...
|
|---|
| 1076 | * However I implemented that within libedit itself.
|
|---|
| 1077 | */
|
|---|
| 1078 | setvar(psh, "_", lastarg, 0);
|
|---|
| 1079 | popstackmark(psh, &smark);
|
|---|
| 1080 |
|
|---|
| 1081 | if (eflag(psh) && psh->exitstatus && !(flags & EV_TESTED))
|
|---|
| 1082 | exitshell(psh, psh->exitstatus);
|
|---|
| 1083 | }
|
|---|
| 1084 |
|
|---|
| 1085 |
|
|---|
| 1086 | /*
|
|---|
| 1087 | * Search for a command. This is called before we fork so that the
|
|---|
| 1088 | * location of the command will be available in the parent as well as
|
|---|
| 1089 | * the child. The check for "goodname" is an overly conservative
|
|---|
| 1090 | * check that the name will not be subject to expansion.
|
|---|
| 1091 | */
|
|---|
| 1092 |
|
|---|
| 1093 | STATIC void
|
|---|
| 1094 | prehash(shinstance *psh, union node *n)
|
|---|
| 1095 | {
|
|---|
| 1096 | struct cmdentry entry;
|
|---|
| 1097 |
|
|---|
| 1098 | if (n->type == NCMD && n->ncmd.args)
|
|---|
| 1099 | if (goodname(n->ncmd.args->narg.text))
|
|---|
| 1100 | find_command(psh, n->ncmd.args->narg.text, &entry, 0,
|
|---|
| 1101 | pathval(psh));
|
|---|
| 1102 | }
|
|---|
| 1103 |
|
|---|
| 1104 |
|
|---|
| 1105 |
|
|---|
| 1106 | /*
|
|---|
| 1107 | * Builtin commands. Builtin commands whose functions are closely
|
|---|
| 1108 | * tied to evaluation are implemented here.
|
|---|
| 1109 | */
|
|---|
| 1110 |
|
|---|
| 1111 | /*
|
|---|
| 1112 | * No command given.
|
|---|
| 1113 | */
|
|---|
| 1114 |
|
|---|
| 1115 | int
|
|---|
| 1116 | bltincmd(shinstance *psh, int argc, char **argv)
|
|---|
| 1117 | {
|
|---|
| 1118 | /*
|
|---|
| 1119 | * Preserve psh->exitstatus of a previous possible redirection
|
|---|
| 1120 | * as POSIX mandates
|
|---|
| 1121 | */
|
|---|
| 1122 | return psh->back_exitstatus;
|
|---|
| 1123 | }
|
|---|
| 1124 |
|
|---|
| 1125 |
|
|---|
| 1126 | /*
|
|---|
| 1127 | * Handle break and continue commands. Break, continue, and return are
|
|---|
| 1128 | * all handled by setting the psh->evalskip flag. The evaluation routines
|
|---|
| 1129 | * above all check this flag, and if it is set they start skipping
|
|---|
| 1130 | * commands rather than executing them. The variable skipcount is
|
|---|
| 1131 | * the number of loops to break/continue, or the number of function
|
|---|
| 1132 | * levels to return. (The latter is always 1.) It should probably
|
|---|
| 1133 | * be an error to break out of more loops than exist, but it isn't
|
|---|
| 1134 | * in the standard shell so we don't make it one here.
|
|---|
| 1135 | */
|
|---|
| 1136 |
|
|---|
| 1137 | int
|
|---|
| 1138 | breakcmd(shinstance *psh, int argc, char **argv)
|
|---|
| 1139 | {
|
|---|
| 1140 | int n = argc > 1 ? number(psh, argv[1]) : 1;
|
|---|
| 1141 |
|
|---|
| 1142 | if (n > psh->loopnest)
|
|---|
| 1143 | n = psh->loopnest;
|
|---|
| 1144 | if (n > 0) {
|
|---|
| 1145 | psh->evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
|
|---|
| 1146 | psh->skipcount = n;
|
|---|
| 1147 | }
|
|---|
| 1148 | return 0;
|
|---|
| 1149 | }
|
|---|
| 1150 |
|
|---|
| 1151 |
|
|---|
| 1152 | /*
|
|---|
| 1153 | * The return command.
|
|---|
| 1154 | */
|
|---|
| 1155 |
|
|---|
| 1156 | int
|
|---|
| 1157 | returncmd(shinstance *psh, int argc, char **argv)
|
|---|
| 1158 | {
|
|---|
| 1159 | #if 0
|
|---|
| 1160 | int ret = argc > 1 ? number(psh, argv[1]) : psh->exitstatus;
|
|---|
| 1161 | #else
|
|---|
| 1162 | int ret;
|
|---|
| 1163 | if (argc > 1) {
|
|---|
| 1164 | /* make return -1 and VSC lite work ... */
|
|---|
| 1165 | if (argv[1][0] != '-' || !is_number(&argv[1][1]))
|
|---|
| 1166 | ret = number(psh, argv[1]);
|
|---|
| 1167 | else
|
|---|
| 1168 | ret = -number(psh, &argv[1][1]) & 255; /* take the bash approach */
|
|---|
| 1169 | } else {
|
|---|
| 1170 | ret = psh->exitstatus;
|
|---|
| 1171 | }
|
|---|
| 1172 | #endif
|
|---|
| 1173 |
|
|---|
| 1174 | if (psh->funcnest) {
|
|---|
| 1175 | psh->evalskip = SKIPFUNC;
|
|---|
| 1176 | psh->skipcount = 1;
|
|---|
| 1177 | return ret;
|
|---|
| 1178 | }
|
|---|
| 1179 | else {
|
|---|
| 1180 | /* Do what ksh does; skip the rest of the file */
|
|---|
| 1181 | psh->evalskip = SKIPFILE;
|
|---|
| 1182 | psh->skipcount = 1;
|
|---|
| 1183 | return ret;
|
|---|
| 1184 | }
|
|---|
| 1185 | }
|
|---|
| 1186 |
|
|---|
| 1187 |
|
|---|
| 1188 | int
|
|---|
| 1189 | falsecmd(shinstance *psh, int argc, char **argv)
|
|---|
| 1190 | {
|
|---|
| 1191 | return 1;
|
|---|
| 1192 | }
|
|---|
| 1193 |
|
|---|
| 1194 |
|
|---|
| 1195 | int
|
|---|
| 1196 | truecmd(shinstance *psh, int argc, char **argv)
|
|---|
| 1197 | {
|
|---|
| 1198 | return 0;
|
|---|
| 1199 | }
|
|---|
| 1200 |
|
|---|
| 1201 |
|
|---|
| 1202 | int
|
|---|
| 1203 | execcmd(shinstance *psh, int argc, char **argv)
|
|---|
| 1204 | {
|
|---|
| 1205 | if (argc > 1) {
|
|---|
| 1206 | struct strlist *sp;
|
|---|
| 1207 |
|
|---|
| 1208 | iflag(psh) = 0; /* exit on error */
|
|---|
| 1209 | mflag(psh) = 0;
|
|---|
| 1210 | optschanged(psh);
|
|---|
| 1211 | for (sp = psh->cmdenviron; sp; sp = sp->next)
|
|---|
| 1212 | setvareq(psh, sp->text, VEXPORT|VSTACK);
|
|---|
| 1213 | shellexec(psh, argv + 1, environment(psh), pathval(psh), 0, 0);
|
|---|
| 1214 | }
|
|---|
| 1215 | return 0;
|
|---|
| 1216 | }
|
|---|
| 1217 |
|
|---|
| 1218 | static int
|
|---|
| 1219 | conv_time(clock_t ticks, char *seconds, size_t l)
|
|---|
| 1220 | {
|
|---|
| 1221 | static clock_t tpm = 0;
|
|---|
| 1222 | clock_t mins;
|
|---|
| 1223 | size_t i;
|
|---|
| 1224 |
|
|---|
| 1225 | if (!tpm)
|
|---|
| 1226 | tpm = /*sysconf(_SC_CLK_TCK)*/sh_sysconf_clk_tck() * 60;
|
|---|
| 1227 |
|
|---|
| 1228 | mins = ticks / tpm;
|
|---|
| 1229 | #ifdef _MSC_VER
|
|---|
| 1230 | {
|
|---|
| 1231 | char tmp[64];
|
|---|
| 1232 | sprintf(tmp, "%.4f", (ticks - mins * tpm) * 60.0 / tpm);
|
|---|
| 1233 | strlcpy(seconds, tmp, l);
|
|---|
| 1234 | }
|
|---|
| 1235 | #else
|
|---|
| 1236 | snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
|
|---|
| 1237 | #endif
|
|---|
| 1238 |
|
|---|
| 1239 | if (seconds[0] == '6' && seconds[1] == '0') {
|
|---|
| 1240 | /* 59.99995 got rounded up... */
|
|---|
| 1241 | mins++;
|
|---|
| 1242 | strlcpy(seconds, "0.0", l);
|
|---|
| 1243 | return mins;
|
|---|
| 1244 | }
|
|---|
| 1245 |
|
|---|
| 1246 | /* suppress trailing zeros */
|
|---|
| 1247 | i = strlen(seconds) - 1;
|
|---|
| 1248 | for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
|
|---|
| 1249 | seconds[i] = 0;
|
|---|
| 1250 | return mins;
|
|---|
| 1251 | }
|
|---|
| 1252 |
|
|---|
| 1253 | int
|
|---|
| 1254 | timescmd(shinstance *psh, int argc, char **argv)
|
|---|
| 1255 | {
|
|---|
| 1256 | shtms tms;
|
|---|
| 1257 | int u, s, cu, cs;
|
|---|
| 1258 | char us[8], ss[8], cus[8], css[8];
|
|---|
| 1259 |
|
|---|
| 1260 | nextopt(psh, "");
|
|---|
| 1261 |
|
|---|
| 1262 | sh_times(psh, &tms);
|
|---|
| 1263 |
|
|---|
| 1264 | u = conv_time(tms.tms_utime, us, sizeof(us));
|
|---|
| 1265 | s = conv_time(tms.tms_stime, ss, sizeof(ss));
|
|---|
| 1266 | cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
|
|---|
| 1267 | cs = conv_time(tms.tms_cstime, css, sizeof(css));
|
|---|
| 1268 |
|
|---|
| 1269 | outfmt(psh->out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
|
|---|
| 1270 | u, us, s, ss, cu, cus, cs, css);
|
|---|
| 1271 |
|
|---|
| 1272 | return 0;
|
|---|
| 1273 | }
|
|---|