[626] | 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 |
|
---|
[1214] | 35 | #if 0
|
---|
[626] | 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 $");
|
---|
[1214] | 40 | #endif /* not lint */
|
---|
[626] | 41 | #endif
|
---|
| 42 |
|
---|
[3449] | 43 | #include <stddef.h>
|
---|
[626] | 44 | #include <stdlib.h>
|
---|
| 45 | #include <stdio.h>
|
---|
| 46 | #include <sys/types.h>
|
---|
[629] | 47 | #ifdef HAVE_SYSCTL_H
|
---|
[2423] | 48 | # ifdef __OpenBSD__ /* joyful crap */
|
---|
| 49 | # include <sys/param.h>
|
---|
| 50 | # undef psh
|
---|
| 51 | # endif
|
---|
[1214] | 52 | # include <sys/sysctl.h>
|
---|
[629] | 53 | #endif
|
---|
[626] | 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
|
---|
[1214] | 80 | # include "myhistedit.h"
|
---|
[626] | 81 | #endif
|
---|
[884] | 82 | #include "shinstance.h"
|
---|
[626] | 83 |
|
---|
| 84 |
|
---|
| 85 | /* flags in argument to evaltree */
|
---|
| 86 | #define EV_EXIT 01 /* exit after evaluating tree */
|
---|
| 87 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
|
---|
| 88 | #define EV_BACKCMD 04 /* command executing within back quotes */
|
---|
| 89 |
|
---|
[884] | 90 | /*int evalskip;*/ /* set if we are skipping commands */
|
---|
| 91 | /*STATIC int skipcount;*/ /* number of levels to skip */
|
---|
| 92 | /*MKINIT int loopnest;*/ /* current loop nesting level */
|
---|
| 93 | /*int funcnest;*/ /* depth of function calls */
|
---|
[626] | 94 |
|
---|
| 95 |
|
---|
[883] | 96 | /*char *commandname;*/
|
---|
[884] | 97 | /*struct strlist *cmdenviron;*/
|
---|
| 98 | /*int exitstatus;*/ /* exit status of last command */
|
---|
| 99 | /*int back_exitstatus;*/ /* exit status of backquoted command */
|
---|
[626] | 100 |
|
---|
| 101 |
|
---|
[884] | 102 | STATIC void evalloop(shinstance *, union node *, int);
|
---|
| 103 | STATIC void evalfor(shinstance *, union node *, int);
|
---|
| 104 | STATIC void evalcase(shinstance *, union node *, int);
|
---|
| 105 | STATIC void evalsubshell(shinstance *, union node *, int);
|
---|
[3449] | 106 | STATIC unsigned expredir(shinstance *, union node *);
|
---|
[884] | 107 | STATIC void evalpipe(shinstance *, union node *);
|
---|
| 108 | STATIC void evalcommand(shinstance *, union node *, int, struct backcmd *);
|
---|
| 109 | STATIC void prehash(shinstance *, union node *);
|
---|
[626] | 110 |
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
| 113 | * Called to reset things after an exception.
|
---|
| 114 | */
|
---|
| 115 |
|
---|
| 116 | #ifdef mkinit
|
---|
| 117 | INCLUDE "eval.h"
|
---|
| 118 |
|
---|
| 119 | RESET {
|
---|
[1198] | 120 | psh->evalskip = 0;
|
---|
| 121 | psh->loopnest = 0;
|
---|
| 122 | psh->funcnest = 0;
|
---|
[626] | 123 | }
|
---|
| 124 |
|
---|
| 125 | SHELLPROC {
|
---|
[1198] | 126 | psh->exitstatus = 0;
|
---|
[626] | 127 | }
|
---|
| 128 | #endif
|
---|
| 129 |
|
---|
| 130 | static int
|
---|
[884] | 131 | sh_pipe(shinstance *psh, int fds[2])
|
---|
[626] | 132 | {
|
---|
| 133 | int nfd;
|
---|
| 134 |
|
---|
[884] | 135 | if (shfile_pipe(&psh->fdtab, fds))
|
---|
[626] | 136 | return -1;
|
---|
| 137 |
|
---|
| 138 | if (fds[0] < 3) {
|
---|
[884] | 139 | nfd = shfile_fcntl(&psh->fdtab, fds[0], F_DUPFD, 3);
|
---|
[626] | 140 | if (nfd != -1) {
|
---|
[884] | 141 | shfile_close(&psh->fdtab, fds[0]);
|
---|
[626] | 142 | fds[0] = nfd;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | if (fds[1] < 3) {
|
---|
[884] | 147 | nfd = shfile_fcntl(&psh->fdtab, fds[1], F_DUPFD, 3);
|
---|
[626] | 148 | if (nfd != -1) {
|
---|
[884] | 149 | shfile_close(&psh->fdtab, fds[1]);
|
---|
[626] | 150 | fds[1] = nfd;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | return 0;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /*
|
---|
| 158 | * The eval commmand.
|
---|
| 159 | */
|
---|
| 160 |
|
---|
| 161 | int
|
---|
[884] | 162 | evalcmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 163 | {
|
---|
[1214] | 164 | char *p;
|
---|
| 165 | char *concat;
|
---|
| 166 | char **ap;
|
---|
[626] | 167 |
|
---|
[1214] | 168 | if (argc > 1) {
|
---|
| 169 | p = argv[1];
|
---|
| 170 | if (argc > 2) {
|
---|
| 171 | STARTSTACKSTR(psh, concat);
|
---|
| 172 | ap = argv + 2;
|
---|
| 173 | for (;;) {
|
---|
| 174 | while (*p)
|
---|
| 175 | STPUTC(psh, *p++, concat);
|
---|
| 176 | if ((p = *ap++) == NULL)
|
---|
| 177 | break;
|
---|
| 178 | STPUTC(psh, ' ', concat);
|
---|
| 179 | }
|
---|
| 180 | STPUTC(psh, '\0', concat);
|
---|
| 181 | p = grabstackstr(psh, concat);
|
---|
| 182 | }
|
---|
| 183 | evalstring(psh, p, EV_TESTED);
|
---|
| 184 | }
|
---|
| 185 | return psh->exitstatus;
|
---|
[626] | 186 | }
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | /*
|
---|
| 190 | * Execute a command or commands contained in a string.
|
---|
| 191 | */
|
---|
| 192 |
|
---|
| 193 | void
|
---|
[884] | 194 | evalstring(shinstance *psh, char *s, int flag)
|
---|
[626] | 195 | {
|
---|
| 196 | union node *n;
|
---|
| 197 | struct stackmark smark;
|
---|
| 198 |
|
---|
[884] | 199 | setstackmark(psh, &smark);
|
---|
| 200 | setinputstring(psh, s, 1);
|
---|
[626] | 201 |
|
---|
[884] | 202 | while ((n = parsecmd(psh, 0)) != NEOF) {
|
---|
| 203 | evaltree(psh, n, flag);
|
---|
| 204 | popstackmark(psh, &smark);
|
---|
[626] | 205 | }
|
---|
[884] | 206 | popfile(psh);
|
---|
| 207 | popstackmark(psh, &smark);
|
---|
[626] | 208 | }
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 |
|
---|
| 212 | /*
|
---|
| 213 | * Evaluate a parse tree. The value is left in the global variable
|
---|
| 214 | * exitstatus.
|
---|
| 215 | */
|
---|
| 216 |
|
---|
| 217 | void
|
---|
[884] | 218 | evaltree(shinstance *psh, union node *n, int flags)
|
---|
[626] | 219 | {
|
---|
| 220 | if (n == NULL) {
|
---|
[1199] | 221 | TRACE((psh, "evaltree(NULL) called\n"));
|
---|
[884] | 222 | psh->exitstatus = 0;
|
---|
[626] | 223 | goto out;
|
---|
| 224 | }
|
---|
| 225 | #ifndef SMALL
|
---|
[884] | 226 | psh->displayhist = 1; /* show history substitutions done with fc */
|
---|
[626] | 227 | #endif
|
---|
[3438] | 228 | TRACE((psh, "pid %" SHPID_PRI ", evaltree(%p: %d, %d) called\n",
|
---|
[1207] | 229 | sh_getpid(psh), n, n->type, flags));
|
---|
[626] | 230 | switch (n->type) {
|
---|
| 231 | case NSEMI:
|
---|
[884] | 232 | evaltree(psh, n->nbinary.ch1, flags & EV_TESTED);
|
---|
| 233 | if (psh->evalskip)
|
---|
[626] | 234 | goto out;
|
---|
[884] | 235 | evaltree(psh, n->nbinary.ch2, flags);
|
---|
[626] | 236 | break;
|
---|
| 237 | case NAND:
|
---|
[884] | 238 | evaltree(psh, n->nbinary.ch1, EV_TESTED);
|
---|
| 239 | if (psh->evalskip || psh->exitstatus != 0)
|
---|
[626] | 240 | goto out;
|
---|
[884] | 241 | evaltree(psh, n->nbinary.ch2, flags);
|
---|
[626] | 242 | break;
|
---|
| 243 | case NOR:
|
---|
[884] | 244 | evaltree(psh, n->nbinary.ch1, EV_TESTED);
|
---|
| 245 | if (psh->evalskip || psh->exitstatus == 0)
|
---|
[626] | 246 | goto out;
|
---|
[884] | 247 | evaltree(psh, n->nbinary.ch2, flags);
|
---|
[626] | 248 | break;
|
---|
[3449] | 249 | case NREDIR: {
|
---|
| 250 | unsigned const oldfnames = expredir(psh, n->nredir.redirect);
|
---|
[884] | 251 | redirect(psh, n->nredir.redirect, REDIR_PUSH);
|
---|
| 252 | evaltree(psh, n->nredir.n, flags);
|
---|
| 253 | popredir(psh);
|
---|
[3449] | 254 | expredircleanup(psh, oldfnames);
|
---|
[626] | 255 | break;
|
---|
[3449] | 256 | }
|
---|
[626] | 257 | case NSUBSHELL:
|
---|
[884] | 258 | evalsubshell(psh, n, flags);
|
---|
[626] | 259 | break;
|
---|
| 260 | case NBACKGND:
|
---|
[884] | 261 | evalsubshell(psh, n, flags);
|
---|
[626] | 262 | break;
|
---|
| 263 | case NIF: {
|
---|
[884] | 264 | evaltree(psh, n->nif.test, EV_TESTED);
|
---|
| 265 | if (psh->evalskip)
|
---|
[626] | 266 | goto out;
|
---|
[884] | 267 | if (psh->exitstatus == 0)
|
---|
| 268 | evaltree(psh, n->nif.ifpart, flags);
|
---|
[626] | 269 | else if (n->nif.elsepart)
|
---|
[884] | 270 | evaltree(psh, n->nif.elsepart, flags);
|
---|
[626] | 271 | else
|
---|
[884] | 272 | psh->exitstatus = 0;
|
---|
[626] | 273 | break;
|
---|
| 274 | }
|
---|
| 275 | case NWHILE:
|
---|
| 276 | case NUNTIL:
|
---|
[884] | 277 | evalloop(psh, n, flags);
|
---|
[626] | 278 | break;
|
---|
| 279 | case NFOR:
|
---|
[884] | 280 | evalfor(psh, n, flags);
|
---|
[626] | 281 | break;
|
---|
| 282 | case NCASE:
|
---|
[884] | 283 | evalcase(psh, n, flags);
|
---|
[626] | 284 | break;
|
---|
| 285 | case NDEFUN:
|
---|
[884] | 286 | defun(psh, n->narg.text, n->narg.next);
|
---|
| 287 | psh->exitstatus = 0;
|
---|
[626] | 288 | break;
|
---|
| 289 | case NNOT:
|
---|
[884] | 290 | evaltree(psh, n->nnot.com, EV_TESTED);
|
---|
| 291 | psh->exitstatus = !psh->exitstatus;
|
---|
[626] | 292 | break;
|
---|
| 293 | case NPIPE:
|
---|
[884] | 294 | evalpipe(psh, n);
|
---|
[626] | 295 | break;
|
---|
| 296 | case NCMD:
|
---|
[884] | 297 | evalcommand(psh, n, flags, (struct backcmd *)NULL);
|
---|
[626] | 298 | break;
|
---|
| 299 | default:
|
---|
[884] | 300 | out1fmt(psh, "Node type = %d\n", n->type);
|
---|
| 301 | flushout(&psh->output);
|
---|
[626] | 302 | break;
|
---|
| 303 | }
|
---|
| 304 | out:
|
---|
[884] | 305 | if (psh->pendingsigs)
|
---|
| 306 | dotrap(psh);
|
---|
[626] | 307 | if ((flags & EV_EXIT) != 0)
|
---|
[884] | 308 | exitshell(psh, psh->exitstatus);
|
---|
[626] | 309 | }
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 | STATIC void
|
---|
[884] | 313 | evalloop(shinstance *psh, union node *n, int flags)
|
---|
[626] | 314 | {
|
---|
| 315 | int status;
|
---|
| 316 |
|
---|
[884] | 317 | psh->loopnest++;
|
---|
[626] | 318 | status = 0;
|
---|
| 319 | for (;;) {
|
---|
[884] | 320 | evaltree(psh, n->nbinary.ch1, EV_TESTED);
|
---|
| 321 | if (psh->evalskip) {
|
---|
| 322 | skipping: if (psh->evalskip == SKIPCONT && --psh->skipcount <= 0) {
|
---|
| 323 | psh->evalskip = 0;
|
---|
[626] | 324 | continue;
|
---|
| 325 | }
|
---|
[884] | 326 | if (psh->evalskip == SKIPBREAK && --psh->skipcount <= 0)
|
---|
| 327 | psh->evalskip = 0;
|
---|
[626] | 328 | break;
|
---|
| 329 | }
|
---|
| 330 | if (n->type == NWHILE) {
|
---|
[884] | 331 | if (psh->exitstatus != 0)
|
---|
[626] | 332 | break;
|
---|
| 333 | } else {
|
---|
[884] | 334 | if (psh->exitstatus == 0)
|
---|
[626] | 335 | break;
|
---|
| 336 | }
|
---|
[884] | 337 | evaltree(psh, n->nbinary.ch2, flags & EV_TESTED);
|
---|
| 338 | status = psh->exitstatus;
|
---|
| 339 | if (psh->evalskip)
|
---|
[626] | 340 | goto skipping;
|
---|
| 341 | }
|
---|
[884] | 342 | psh->loopnest--;
|
---|
| 343 | psh->exitstatus = status;
|
---|
[626] | 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 | STATIC void
|
---|
[884] | 349 | evalfor(shinstance *psh, union node *n, int flags)
|
---|
[626] | 350 | {
|
---|
| 351 | struct arglist arglist;
|
---|
| 352 | union node *argp;
|
---|
| 353 | struct strlist *sp;
|
---|
| 354 | struct stackmark smark;
|
---|
| 355 | int status = 0;
|
---|
| 356 |
|
---|
[884] | 357 | setstackmark(psh, &smark);
|
---|
[626] | 358 | arglist.lastp = &arglist.list;
|
---|
| 359 | for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
|
---|
[884] | 360 | expandarg(psh, argp, &arglist, EXP_FULL | EXP_TILDE);
|
---|
| 361 | if (psh->evalskip)
|
---|
[626] | 362 | goto out;
|
---|
| 363 | }
|
---|
| 364 | *arglist.lastp = NULL;
|
---|
| 365 |
|
---|
[884] | 366 | psh->loopnest++;
|
---|
[626] | 367 | for (sp = arglist.list ; sp ; sp = sp->next) {
|
---|
[884] | 368 | setvar(psh, n->nfor.var, sp->text, 0);
|
---|
| 369 | evaltree(psh, n->nfor.body, flags & EV_TESTED);
|
---|
| 370 | status = psh->exitstatus;
|
---|
| 371 | if (psh->evalskip) {
|
---|
| 372 | if (psh->evalskip == SKIPCONT && --psh->skipcount <= 0) {
|
---|
| 373 | psh->evalskip = 0;
|
---|
[626] | 374 | continue;
|
---|
| 375 | }
|
---|
[884] | 376 | if (psh->evalskip == SKIPBREAK && --psh->skipcount <= 0)
|
---|
| 377 | psh->evalskip = 0;
|
---|
[626] | 378 | break;
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
[884] | 381 | psh->loopnest--;
|
---|
| 382 | psh->exitstatus = status;
|
---|
[626] | 383 | out:
|
---|
[884] | 384 | popstackmark(psh, &smark);
|
---|
[626] | 385 | }
|
---|
| 386 |
|
---|
| 387 |
|
---|
| 388 |
|
---|
| 389 | STATIC void
|
---|
[884] | 390 | evalcase(shinstance *psh, union node *n, int flags)
|
---|
[626] | 391 | {
|
---|
| 392 | union node *cp;
|
---|
| 393 | union node *patp;
|
---|
| 394 | struct arglist arglist;
|
---|
| 395 | struct stackmark smark;
|
---|
| 396 | int status = 0;
|
---|
| 397 |
|
---|
[884] | 398 | setstackmark(psh, &smark);
|
---|
[626] | 399 | arglist.lastp = &arglist.list;
|
---|
[884] | 400 | expandarg(psh, n->ncase.expr, &arglist, EXP_TILDE);
|
---|
| 401 | for (cp = n->ncase.cases ; cp && psh->evalskip == 0 ; cp = cp->nclist.next) {
|
---|
[626] | 402 | for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
|
---|
[884] | 403 | if (casematch(psh, patp, arglist.list->text)) {
|
---|
| 404 | if (psh->evalskip == 0) {
|
---|
| 405 | evaltree(psh, cp->nclist.body, flags);
|
---|
| 406 | status = psh->exitstatus;
|
---|
[626] | 407 | }
|
---|
| 408 | goto out;
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 | }
|
---|
| 412 | out:
|
---|
[884] | 413 | psh->exitstatus = status;
|
---|
| 414 | popstackmark(psh, &smark);
|
---|
[626] | 415 | }
|
---|
| 416 |
|
---|
| 417 |
|
---|
[3433] | 418 | #ifdef KASH_USE_FORKSHELL2
|
---|
| 419 | /*
|
---|
| 420 | * Child of evalsubshell.
|
---|
| 421 | */
|
---|
| 422 | struct evalsubshellchild
|
---|
| 423 | {
|
---|
| 424 | int flags;
|
---|
| 425 | int backgnd;
|
---|
| 426 | };
|
---|
[626] | 427 |
|
---|
[3433] | 428 | static int evalsubshell_child(shinstance *psh, union node *n, void *argp)
|
---|
| 429 | {
|
---|
| 430 | struct evalsubshellchild args = *(struct evalsubshellchild *)argp;
|
---|
| 431 |
|
---|
| 432 | INTON;
|
---|
| 433 | if (args.backgnd)
|
---|
| 434 | args.flags &=~ EV_TESTED;
|
---|
| 435 | redirect(psh, n->nredir.redirect, 0);
|
---|
| 436 | /* never returns */
|
---|
| 437 | evaltree(psh, n->nredir.n, args.flags | EV_EXIT);
|
---|
| 438 | /** @todo make us return here. */
|
---|
| 439 | return 0;
|
---|
| 440 | }
|
---|
| 441 | #endif /* KASH_USE_FORKSHELL2 */
|
---|
| 442 |
|
---|
| 443 |
|
---|
[626] | 444 | /*
|
---|
| 445 | * Kick off a subshell to evaluate a tree.
|
---|
| 446 | */
|
---|
| 447 |
|
---|
| 448 | STATIC void
|
---|
[884] | 449 | evalsubshell(shinstance *psh, union node *n, int flags)
|
---|
[626] | 450 | {
|
---|
| 451 | struct job *jp;
|
---|
| 452 | int backgnd = (n->type == NBACKGND);
|
---|
[3449] | 453 | unsigned expfnamedepth;
|
---|
[626] | 454 |
|
---|
[3449] | 455 | expfnamedepth = expredir(psh, n->nredir.redirect);
|
---|
[626] | 456 | INTOFF;
|
---|
[884] | 457 | jp = makejob(psh, n, 1);
|
---|
[3433] | 458 | #ifdef KASH_USE_FORKSHELL2
|
---|
| 459 | {
|
---|
| 460 | struct evalsubshellchild args;
|
---|
| 461 | args.flags = flags;
|
---|
| 462 | args.backgnd = backgnd;
|
---|
| 463 | forkshell2(psh, jp, n, backgnd ? FORK_BG : FORK_FG,
|
---|
[3438] | 464 | evalsubshell_child, n, &args, sizeof(args), NULL);
|
---|
[3433] | 465 | }
|
---|
| 466 | #else
|
---|
[884] | 467 | if (forkshell(psh, jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
|
---|
[626] | 468 | INTON;
|
---|
| 469 | if (backgnd)
|
---|
| 470 | flags &=~ EV_TESTED;
|
---|
[884] | 471 | redirect(psh, n->nredir.redirect, 0);
|
---|
[626] | 472 | /* never returns */
|
---|
[884] | 473 | evaltree(psh, n->nredir.n, flags | EV_EXIT);
|
---|
[626] | 474 | }
|
---|
[3433] | 475 | #endif
|
---|
[626] | 476 | if (! backgnd)
|
---|
[884] | 477 | psh->exitstatus = waitforjob(psh, jp);
|
---|
[3449] | 478 | expredircleanup(psh, expfnamedepth);
|
---|
[626] | 479 | INTON;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 |
|
---|
| 483 |
|
---|
| 484 | /*
|
---|
| 485 | * Compute the names of the files in a redirection list.
|
---|
| 486 | */
|
---|
| 487 |
|
---|
[3449] | 488 | STATIC unsigned
|
---|
[884] | 489 | expredir(shinstance *psh, union node *n)
|
---|
[626] | 490 | {
|
---|
| 491 | union node *redir;
|
---|
[3449] | 492 | redirexpfnames *expfnames;
|
---|
| 493 | unsigned i;
|
---|
[626] | 494 |
|
---|
[3449] | 495 | /* We typically end up here w/o redirections. */
|
---|
| 496 | if (!n)
|
---|
| 497 | return !(expfnames = psh->expfnames) ? 0 : expfnames->depth + 1;
|
---|
| 498 |
|
---|
| 499 | /* Prepare a table for the expanded names. */
|
---|
| 500 | i = 0;
|
---|
| 501 | for (redir = n; redir ; redir = redir->nfile.next)
|
---|
| 502 | i++;
|
---|
| 503 | expfnames = stalloc(psh, offsetof(redirexpfnames, names) + sizeof(expfnames->names[0]) * i);
|
---|
| 504 | expfnames->count = i;
|
---|
| 505 | TRACE2((psh, "expredir: %p: count=%u\n", expfnames, i));
|
---|
| 506 |
|
---|
| 507 | /* Do the expansion. */
|
---|
| 508 | for (redir = n, i = 0 ; redir ; redir = redir->nfile.next, i++) {
|
---|
[626] | 509 | struct arglist fn;
|
---|
| 510 | fn.lastp = &fn.list;
|
---|
| 511 | switch (redir->type) {
|
---|
| 512 | case NFROMTO:
|
---|
| 513 | case NFROM:
|
---|
| 514 | case NTO:
|
---|
| 515 | case NCLOBBER:
|
---|
| 516 | case NAPPEND:
|
---|
[884] | 517 | expandarg(psh, redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
|
---|
[3449] | 518 | expfnames->names[i] = fn.list->text;
|
---|
[626] | 519 | break;
|
---|
| 520 | case NFROMFD:
|
---|
| 521 | case NTOFD:
|
---|
| 522 | if (redir->ndup.vname) {
|
---|
[884] | 523 | expandarg(psh, redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
|
---|
| 524 | fixredir(psh, redir, fn.list->text, 1);
|
---|
[626] | 525 | }
|
---|
[3449] | 526 | expfnames->names[i] = NULL;
|
---|
[626] | 527 | break;
|
---|
[3449] | 528 | default:
|
---|
[3477] | 529 | kHlpAssert(redir->type == NHERE || redir->type == NXHERE);
|
---|
[3449] | 530 | expfnames->names[i] = NULL;
|
---|
| 531 | break;
|
---|
[626] | 532 | }
|
---|
| 533 | }
|
---|
[3477] | 534 | kHlpAssert(i == expfnames->count);
|
---|
[3449] | 535 |
|
---|
| 536 | /* Do the linking at the end, as nesting happens when we expand backtick arguments. */
|
---|
| 537 | expfnames->prev = psh->expfnames;
|
---|
| 538 | psh->expfnames = expfnames;
|
---|
| 539 | return expfnames->depth = psh->expfnames ? psh->expfnames->depth + 1 : 1;
|
---|
[626] | 540 | }
|
---|
| 541 |
|
---|
[3440] | 542 | STATIC void
|
---|
[3449] | 543 | expredircleanup(shinstance *psh, unsigned depth)
|
---|
[3440] | 544 | {
|
---|
[3449] | 545 | redirexpfnames *expfnames = psh->expfnames;
|
---|
[3477] | 546 | kHlpAssert(expfnames == NULL ? depth == 0 : expfnames->depth == depth || expfnames->depth + 1 == depth);
|
---|
[3449] | 547 | while (expfnames && expfnames->depth >= depth)
|
---|
| 548 | expfnames = psh->expfnames = expfnames->prev;
|
---|
[3440] | 549 | }
|
---|
[626] | 550 |
|
---|
[3440] | 551 |
|
---|
[3433] | 552 | #ifdef KASH_USE_FORKSHELL2
|
---|
| 553 | /*
|
---|
| 554 | * Child of evalpipe.
|
---|
| 555 | */
|
---|
| 556 | struct evalpipechild
|
---|
| 557 | {
|
---|
| 558 | int prevfd;
|
---|
| 559 | int pip[2];
|
---|
| 560 | };
|
---|
[626] | 561 |
|
---|
[3433] | 562 | static int evalpipe_child(shinstance *psh, union node *n, void *argp)
|
---|
| 563 | {
|
---|
| 564 | struct evalpipechild args = *(struct evalpipechild *)argp;
|
---|
| 565 |
|
---|
| 566 | if (args.prevfd > 0) {
|
---|
| 567 | movefd(psh, args.prevfd, 0);
|
---|
| 568 | }
|
---|
| 569 | if (args.pip[1] >= 0) {
|
---|
| 570 | shfile_close(&psh->fdtab, args.pip[0]);
|
---|
| 571 | if (args.pip[1] != 1) {
|
---|
| 572 | movefd(psh, args.pip[1], 1);
|
---|
| 573 | }
|
---|
| 574 | }
|
---|
| 575 | evaltree(psh, n, EV_EXIT);
|
---|
| 576 | /** @todo make it return thru here. */
|
---|
| 577 | return 0;
|
---|
| 578 | }
|
---|
| 579 | #endif /* KASH_USE_FORKSHELL2 */
|
---|
| 580 |
|
---|
[626] | 581 | /*
|
---|
| 582 | * Evaluate a pipeline. All the processes in the pipeline are children
|
---|
| 583 | * of the process creating the pipeline. (This differs from some versions
|
---|
| 584 | * of the shell, which make the last process in a pipeline the parent
|
---|
| 585 | * of all the rest.)
|
---|
| 586 | */
|
---|
| 587 |
|
---|
| 588 | STATIC void
|
---|
[884] | 589 | evalpipe(shinstance *psh, union node *n)
|
---|
[626] | 590 | {
|
---|
| 591 | struct job *jp;
|
---|
| 592 | struct nodelist *lp;
|
---|
| 593 | int pipelen;
|
---|
| 594 | int prevfd;
|
---|
| 595 | int pip[2];
|
---|
| 596 |
|
---|
[1199] | 597 | TRACE((psh, "evalpipe(0x%lx) called\n", (long)n));
|
---|
[626] | 598 | pipelen = 0;
|
---|
| 599 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
|
---|
| 600 | pipelen++;
|
---|
| 601 | INTOFF;
|
---|
[884] | 602 | jp = makejob(psh, n, pipelen);
|
---|
[626] | 603 | prevfd = -1;
|
---|
| 604 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
|
---|
[884] | 605 | prehash(psh, lp->n);
|
---|
[626] | 606 | pip[1] = -1;
|
---|
| 607 | if (lp->next) {
|
---|
[884] | 608 | if (sh_pipe(psh, pip) < 0) {
|
---|
| 609 | shfile_close(&psh->fdtab, prevfd);
|
---|
| 610 | error(psh, "Pipe call failed");
|
---|
[626] | 611 | }
|
---|
| 612 | }
|
---|
[3433] | 613 | #ifdef KASH_USE_FORKSHELL2
|
---|
| 614 | {
|
---|
| 615 | struct evalpipechild args;
|
---|
| 616 | args.prevfd = prevfd;
|
---|
| 617 | args.pip[0] = pip[0];
|
---|
| 618 | args.pip[1] = pip[1];
|
---|
| 619 | forkshell2(psh, jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG,
|
---|
[3438] | 620 | evalpipe_child, lp->n, &args, sizeof(args), NULL);
|
---|
[3433] | 621 | }
|
---|
| 622 | #else
|
---|
[884] | 623 | if (forkshell(psh, jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
|
---|
[626] | 624 | INTON;
|
---|
| 625 | if (prevfd > 0) {
|
---|
[2424] | 626 | movefd(psh, prevfd, 0);
|
---|
[626] | 627 | }
|
---|
| 628 | if (pip[1] >= 0) {
|
---|
[884] | 629 | shfile_close(&psh->fdtab, pip[0]);
|
---|
[626] | 630 | if (pip[1] != 1) {
|
---|
[2424] | 631 | movefd(psh, pip[1], 1);
|
---|
[626] | 632 | }
|
---|
| 633 | }
|
---|
[884] | 634 | evaltree(psh, lp->n, EV_EXIT);
|
---|
[626] | 635 | }
|
---|
[3433] | 636 | #endif
|
---|
[626] | 637 | if (prevfd >= 0)
|
---|
[884] | 638 | shfile_close(&psh->fdtab, prevfd);
|
---|
[626] | 639 | prevfd = pip[0];
|
---|
[884] | 640 | shfile_close(&psh->fdtab, pip[1]);
|
---|
[626] | 641 | }
|
---|
| 642 | if (n->npipe.backgnd == 0) {
|
---|
[884] | 643 | psh->exitstatus = waitforjob(psh, jp);
|
---|
[1199] | 644 | TRACE((psh, "evalpipe: job done exit status %d\n", psh->exitstatus));
|
---|
[626] | 645 | }
|
---|
| 646 | INTON;
|
---|
| 647 | }
|
---|
| 648 |
|
---|
[3433] | 649 | #ifdef KASH_USE_FORKSHELL2
|
---|
| 650 | /*
|
---|
| 651 | * evalbackcmd child.
|
---|
| 652 | */
|
---|
| 653 | struct evalbackcmdchild
|
---|
| 654 | {
|
---|
| 655 | int pip[2];
|
---|
| 656 | };
|
---|
[626] | 657 |
|
---|
[3433] | 658 | static int evalbackcmd_child(shinstance *psh, union node *n, void *argp)
|
---|
| 659 | {
|
---|
| 660 | struct evalbackcmdchild args = *(struct evalbackcmdchild *)argp;
|
---|
[626] | 661 |
|
---|
[3433] | 662 | FORCEINTON;
|
---|
| 663 | shfile_close(&psh->fdtab, args.pip[0]);
|
---|
| 664 | if (args.pip[1] != 1) {
|
---|
| 665 | movefd(psh, args.pip[1], 1);
|
---|
| 666 | }
|
---|
| 667 | eflag(psh) = 0;
|
---|
| 668 | evaltree(psh, n, EV_EXIT);
|
---|
| 669 | /* NOTREACHED */ /** @todo make it return here to simplify thread handling (no need for setjmp). */
|
---|
| 670 | return 0;
|
---|
| 671 | }
|
---|
| 672 | #endif /* KASH_USE_FORKSHELL2 */
|
---|
| 673 |
|
---|
[626] | 674 | /*
|
---|
| 675 | * Execute a command inside back quotes. If it's a builtin command, we
|
---|
| 676 | * want to save its output in a block obtained from malloc. Otherwise
|
---|
| 677 | * we fork off a subprocess and get the output of the command via a pipe.
|
---|
| 678 | * Should be called with interrupts off.
|
---|
| 679 | */
|
---|
| 680 |
|
---|
| 681 | void
|
---|
[884] | 682 | evalbackcmd(shinstance *psh, union node *n, struct backcmd *result)
|
---|
[626] | 683 | {
|
---|
| 684 | int pip[2];
|
---|
| 685 | struct job *jp;
|
---|
| 686 | struct stackmark smark; /* unnecessary */
|
---|
| 687 |
|
---|
[884] | 688 | setstackmark(psh, &smark);
|
---|
[626] | 689 | result->fd = -1;
|
---|
| 690 | result->buf = NULL;
|
---|
| 691 | result->nleft = 0;
|
---|
| 692 | result->jp = NULL;
|
---|
| 693 | if (n == NULL) {
|
---|
| 694 | goto out;
|
---|
| 695 | }
|
---|
| 696 | #ifdef notyet
|
---|
| 697 | /*
|
---|
| 698 | * For now we disable executing builtins in the same
|
---|
| 699 | * context as the shell, because we are not keeping
|
---|
| 700 | * enough state to recover from changes that are
|
---|
| 701 | * supposed only to affect subshells. eg. echo "`cd /`"
|
---|
| 702 | */
|
---|
| 703 | if (n->type == NCMD) {
|
---|
[884] | 704 | psh->exitstatus = opsh->exitstatus;
|
---|
| 705 | evalcommand(psh, n, EV_BACKCMD, result);
|
---|
[626] | 706 | } else
|
---|
| 707 | #endif
|
---|
| 708 | {
|
---|
| 709 | INTOFF;
|
---|
[884] | 710 | if (sh_pipe(psh, pip) < 0)
|
---|
| 711 | error(psh, "Pipe call failed");
|
---|
| 712 | jp = makejob(psh, n, 1);
|
---|
[3433] | 713 | #ifdef KASH_USE_FORKSHELL2
|
---|
| 714 | {
|
---|
| 715 | struct evalbackcmdchild args;
|
---|
| 716 | args.pip[0] = pip[0];
|
---|
| 717 | args.pip[1] = pip[1];
|
---|
| 718 | forkshell2(psh, jp, n, FORK_NOJOB,
|
---|
[3438] | 719 | evalbackcmd_child, n, &args, sizeof(args), NULL);
|
---|
[3433] | 720 | }
|
---|
| 721 | #else
|
---|
[884] | 722 | if (forkshell(psh, jp, n, FORK_NOJOB) == 0) {
|
---|
[626] | 723 | FORCEINTON;
|
---|
[884] | 724 | shfile_close(&psh->fdtab, pip[0]);
|
---|
[626] | 725 | if (pip[1] != 1) {
|
---|
[2424] | 726 | movefd(psh, pip[1], 1);
|
---|
[626] | 727 | }
|
---|
[1198] | 728 | eflag(psh) = 0;
|
---|
[884] | 729 | evaltree(psh, n, EV_EXIT);
|
---|
[626] | 730 | /* NOTREACHED */
|
---|
| 731 | }
|
---|
[3433] | 732 | #endif
|
---|
[884] | 733 | shfile_close(&psh->fdtab, pip[1]);
|
---|
[626] | 734 | result->fd = pip[0];
|
---|
| 735 | result->jp = jp;
|
---|
| 736 | INTON;
|
---|
| 737 | }
|
---|
| 738 | out:
|
---|
[884] | 739 | popstackmark(psh, &smark);
|
---|
[1199] | 740 | TRACE((psh, "evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
|
---|
[626] | 741 | result->fd, result->buf, result->nleft, result->jp));
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | static const char *
|
---|
[884] | 745 | syspath(shinstance *psh)
|
---|
[626] | 746 | {
|
---|
[629] | 747 | #ifdef CTL_USER
|
---|
[626] | 748 | static char *sys_path = NULL;
|
---|
| 749 | static int mib[] = {CTL_USER, USER_CS_PATH};
|
---|
[809] | 750 | #endif
|
---|
[626] | 751 | #ifdef PC_PATH_SEP
|
---|
| 752 | static char def_path[] = "PATH=/usr/bin;/bin;/usr/sbin;/sbin";
|
---|
| 753 | #else
|
---|
| 754 | static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
|
---|
| 755 | #endif
|
---|
[629] | 756 | #ifdef CTL_USER
|
---|
[626] | 757 | size_t len;
|
---|
| 758 |
|
---|
| 759 | if (sys_path == NULL) {
|
---|
| 760 | if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
|
---|
[2290] | 761 | (sys_path = ckmalloc(psh, len + 5)) != NULL &&
|
---|
[626] | 762 | sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
|
---|
| 763 | memcpy(sys_path, "PATH=", 5);
|
---|
| 764 | } else {
|
---|
[2290] | 765 | ckfree(psh, sys_path);
|
---|
[626] | 766 | /* something to keep things happy */
|
---|
| 767 | sys_path = def_path;
|
---|
| 768 | }
|
---|
| 769 | }
|
---|
| 770 | return sys_path;
|
---|
[629] | 771 | #else
|
---|
[1214] | 772 | return def_path;
|
---|
[809] | 773 | #endif
|
---|
[626] | 774 | }
|
---|
| 775 |
|
---|
| 776 | static int
|
---|
[884] | 777 | parse_command_args(shinstance *psh, int argc, char **argv, int *use_syspath)
|
---|
[626] | 778 | {
|
---|
| 779 | int sv_argc = argc;
|
---|
| 780 | char *cp, c;
|
---|
| 781 |
|
---|
| 782 | *use_syspath = 0;
|
---|
| 783 |
|
---|
| 784 | for (;;) {
|
---|
| 785 | argv++;
|
---|
| 786 | if (--argc == 0)
|
---|
| 787 | break;
|
---|
| 788 | cp = *argv;
|
---|
| 789 | if (*cp++ != '-')
|
---|
| 790 | break;
|
---|
| 791 | if (*cp == '-' && cp[1] == 0) {
|
---|
| 792 | argv++;
|
---|
| 793 | argc--;
|
---|
| 794 | break;
|
---|
| 795 | }
|
---|
| 796 | while ((c = *cp++)) {
|
---|
| 797 | switch (c) {
|
---|
| 798 | case 'p':
|
---|
| 799 | *use_syspath = 1;
|
---|
| 800 | break;
|
---|
| 801 | default:
|
---|
| 802 | /* run 'typecmd' for other options */
|
---|
| 803 | return 0;
|
---|
| 804 | }
|
---|
| 805 | }
|
---|
| 806 | }
|
---|
| 807 | return sv_argc - argc;
|
---|
| 808 | }
|
---|
| 809 |
|
---|
[3434] | 810 |
|
---|
| 811 | /*
|
---|
| 812 | * The split up evalcommand code:
|
---|
| 813 | * evalcommand_out, evalcommand_parent, evalcommand_doit, evalcommand_child
|
---|
| 814 | */
|
---|
[3435] | 815 | /*int vforked = 0; - obsolete */
|
---|
[626] | 816 |
|
---|
[3434] | 817 | /* Both child and parent exits thru here. */
|
---|
| 818 | STATIC void
|
---|
[3453] | 819 | evalcommand_out(shinstance *psh, int flags, char *lastarg, unsigned expfnamedepth, struct stackmark *smarkp)
|
---|
[3434] | 820 | {
|
---|
| 821 | if (lastarg)
|
---|
| 822 | /* dsl: I think this is intended to be used to support
|
---|
| 823 | * '_' in 'vi' command mode during line editing...
|
---|
| 824 | * However I implemented that within libedit itself.
|
---|
| 825 | */
|
---|
| 826 | setvar(psh, "_", lastarg, 0);
|
---|
[3453] | 827 | expredircleanup(psh, expfnamedepth);
|
---|
[3434] | 828 | popstackmark(psh, smarkp);
|
---|
| 829 |
|
---|
| 830 | if (eflag(psh) && psh->exitstatus && !(flags & EV_TESTED))
|
---|
| 831 | exitshell(psh, psh->exitstatus);
|
---|
| 832 | }
|
---|
| 833 |
|
---|
| 834 |
|
---|
| 835 | /* Called if we forkshell(). */
|
---|
| 836 | STATIC void
|
---|
[3453] | 837 | evalcommand_parent(shinstance *psh, int flags, char *lastarg, unsigned expfnamedepth, struct stackmark *smarkp,
|
---|
[3434] | 838 | int mode, struct job *jp, int pip[2], struct backcmd *backcmd)
|
---|
| 839 | {
|
---|
| 840 | if (mode == FORK_FG) { /* argument to fork */
|
---|
| 841 | psh->exitstatus = waitforjob(psh, jp);
|
---|
| 842 | } else if (mode == FORK_NOJOB) {
|
---|
| 843 | backcmd->fd = pip[0];
|
---|
| 844 | shfile_close(&psh->fdtab, pip[1]);
|
---|
| 845 | backcmd->jp = jp;
|
---|
| 846 | }
|
---|
| 847 | FORCEINTON;
|
---|
| 848 |
|
---|
[3453] | 849 | evalcommand_out(psh, flags, lastarg, expfnamedepth, smarkp);
|
---|
[3434] | 850 | }
|
---|
| 851 |
|
---|
| 852 | struct evalcommanddoit
|
---|
| 853 | {
|
---|
[3437] | 854 | struct stackmark smark;
|
---|
[3453] | 855 | unsigned expfnamedepth;
|
---|
[3437] | 856 |
|
---|
[3434] | 857 | struct backcmd *backcmd;
|
---|
| 858 | int flags;
|
---|
| 859 | int argc;
|
---|
| 860 | char **argv;
|
---|
[3437] | 861 | char *lastarg;
|
---|
[3434] | 862 | struct arglist varlist;
|
---|
[3437] | 863 | const char *path;
|
---|
| 864 | struct cmdentry cmdentry;
|
---|
| 865 |
|
---|
| 866 | /* for child stuff only: */
|
---|
| 867 | int pip[2];
|
---|
[3434] | 868 | };
|
---|
| 869 |
|
---|
| 870 | STATIC void
|
---|
| 871 | evalcommand_doit(shinstance *psh, union node *cmd, struct evalcommanddoit *args)
|
---|
| 872 | {
|
---|
| 873 | struct jmploc jmploc;
|
---|
| 874 | struct jmploc *volatile savehandler;
|
---|
| 875 | struct localvar *volatile savelocalvars;
|
---|
| 876 |
|
---|
| 877 | /* This is the child process if a fork occurred. */
|
---|
| 878 | /* Execute the command. */
|
---|
| 879 | switch (args->cmdentry.cmdtype) {
|
---|
| 880 | case CMDFUNCTION: {
|
---|
| 881 | volatile struct shparam saveparam;
|
---|
| 882 | #ifdef DEBUG
|
---|
| 883 | trputs(psh, "Shell function: "); trargs(psh, args->argv);
|
---|
| 884 | #endif
|
---|
| 885 | redirect(psh, cmd->ncmd.redirect, REDIR_PUSH);
|
---|
| 886 | saveparam = psh->shellparam;
|
---|
| 887 | psh->shellparam.malloc = 0;
|
---|
| 888 | psh->shellparam.reset = 1;
|
---|
| 889 | psh->shellparam.nparam = args->argc - 1;
|
---|
| 890 | psh->shellparam.p = args->argv + 1;
|
---|
| 891 | psh->shellparam.optnext = NULL;
|
---|
| 892 | INTOFF;
|
---|
| 893 | savelocalvars = psh->localvars;
|
---|
| 894 | psh->localvars = NULL;
|
---|
| 895 | INTON;
|
---|
| 896 | if (setjmp(jmploc.loc)) {
|
---|
| 897 | if (psh->exception == EXSHELLPROC) {
|
---|
| 898 | freeparam(psh, (volatile struct shparam *)
|
---|
| 899 | &saveparam);
|
---|
| 900 | } else {
|
---|
| 901 | freeparam(psh, &psh->shellparam);
|
---|
| 902 | psh->shellparam = saveparam;
|
---|
| 903 | }
|
---|
| 904 | poplocalvars(psh);
|
---|
| 905 | psh->localvars = savelocalvars;
|
---|
| 906 | psh->handler = savehandler;
|
---|
| 907 | longjmp(psh->handler->loc, 1);
|
---|
| 908 | }
|
---|
| 909 | savehandler = psh->handler;
|
---|
| 910 | psh->handler = &jmploc;
|
---|
| 911 | listmklocal(psh, args->varlist.list, 0);
|
---|
| 912 | /* stop shell blowing its stack */
|
---|
| 913 | if (++psh->funcnest > 1000)
|
---|
| 914 | error(psh, "too many nested function calls");
|
---|
| 915 | evaltree(psh, args->cmdentry.u.func, args->flags & EV_TESTED);
|
---|
| 916 | psh->funcnest--;
|
---|
| 917 | INTOFF;
|
---|
| 918 | poplocalvars(psh);
|
---|
| 919 | psh->localvars = savelocalvars;
|
---|
| 920 | freeparam(psh, &psh->shellparam);
|
---|
| 921 | psh->shellparam = saveparam;
|
---|
| 922 | psh->handler = savehandler;
|
---|
| 923 | popredir(psh);
|
---|
| 924 | INTON;
|
---|
| 925 | if (psh->evalskip == SKIPFUNC) {
|
---|
| 926 | psh->evalskip = 0;
|
---|
| 927 | psh->skipcount = 0;
|
---|
| 928 | }
|
---|
| 929 | if (args->flags & EV_EXIT)
|
---|
| 930 | exitshell(psh, psh->exitstatus);
|
---|
| 931 | break;
|
---|
| 932 | }
|
---|
| 933 |
|
---|
| 934 | case CMDBUILTIN:
|
---|
| 935 | case CMDSPLBLTIN: {
|
---|
| 936 | volatile int temp_path = 0;
|
---|
| 937 | char *volatile savecmdname;
|
---|
[3438] | 938 | int volatile savecmdnamemalloc;
|
---|
[3434] | 939 | volatile int e;
|
---|
| 940 | int mode;
|
---|
| 941 | #ifdef DEBUG
|
---|
| 942 | trputs(psh, "builtin command: "); trargs(psh, args->argv);
|
---|
| 943 | #endif
|
---|
| 944 | mode = (args->cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
|
---|
| 945 | if (args->flags == EV_BACKCMD) {
|
---|
| 946 | psh->memout.nleft = 0;
|
---|
| 947 | psh->memout.nextc = psh->memout.buf;
|
---|
| 948 | psh->memout.bufsize = 64;
|
---|
| 949 | mode |= REDIR_BACKQ;
|
---|
| 950 | }
|
---|
| 951 | e = -1;
|
---|
| 952 | savehandler = psh->handler;
|
---|
| 953 | savecmdname = psh->commandname;
|
---|
[3438] | 954 | savecmdnamemalloc = psh->commandnamemalloc;
|
---|
[3434] | 955 | psh->handler = &jmploc;
|
---|
| 956 | if (!setjmp(jmploc.loc)) {
|
---|
| 957 | /* We need to ensure the command hash table isn't
|
---|
| 958 | * corruped by temporary PATH assignments.
|
---|
| 959 | * However we must ensure the 'local' command works!
|
---|
| 960 | */
|
---|
| 961 | if (args->path != pathval(psh) && (args->cmdentry.u.bltin == hashcmd ||
|
---|
| 962 | args->cmdentry.u.bltin == typecmd)) {
|
---|
| 963 | savelocalvars = psh->localvars;
|
---|
| 964 | psh->localvars = 0;
|
---|
| 965 | mklocal(psh, args->path - 5 /* PATH= */, 0);
|
---|
| 966 | temp_path = 1;
|
---|
| 967 | } else
|
---|
| 968 | temp_path = 0;
|
---|
| 969 | redirect(psh, cmd->ncmd.redirect, mode);
|
---|
| 970 |
|
---|
| 971 | /* exec is a special builtin, but needs this list... */
|
---|
| 972 | psh->cmdenviron = args->varlist.list;
|
---|
| 973 | /* we must check 'readonly' flag for all builtins */
|
---|
| 974 | listsetvar(psh, args->varlist.list,
|
---|
| 975 | args->cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
|
---|
[3438] | 976 | psh->commandnamemalloc = 0;
|
---|
[3434] | 977 | psh->commandname = args->argv[0];
|
---|
| 978 | /* initialize nextopt */
|
---|
| 979 | psh->argptr = args->argv + 1;
|
---|
| 980 | psh->optptr = NULL;
|
---|
| 981 | /* and getopt */
|
---|
| 982 | #if 0 /** @todo fix getop usage! */
|
---|
| 983 | #if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
|
---|
| 984 | optreset = 1;
|
---|
| 985 | optind = 1;
|
---|
| 986 | #else
|
---|
| 987 | optind = 0; /* init */
|
---|
| 988 | #endif
|
---|
| 989 | #endif
|
---|
| 990 |
|
---|
| 991 | psh->exitstatus = args->cmdentry.u.bltin(psh, args->argc, args->argv);
|
---|
| 992 | } else {
|
---|
| 993 | e = psh->exception;
|
---|
| 994 | psh->exitstatus = e == EXINT ? SIGINT + 128 :
|
---|
| 995 | e == EXEXEC ? psh->exerrno : 2;
|
---|
| 996 | }
|
---|
| 997 | psh->handler = savehandler;
|
---|
| 998 | output_flushall(psh);
|
---|
| 999 | psh->out1 = &psh->output;
|
---|
| 1000 | psh->out2 = &psh->errout;
|
---|
| 1001 | freestdout(psh);
|
---|
| 1002 | if (temp_path) {
|
---|
| 1003 | poplocalvars(psh);
|
---|
| 1004 | psh->localvars = savelocalvars;
|
---|
| 1005 | }
|
---|
| 1006 | psh->cmdenviron = NULL;
|
---|
| 1007 | if (e != EXSHELLPROC) {
|
---|
| 1008 | psh->commandname = savecmdname;
|
---|
[3438] | 1009 | psh->commandnamemalloc = savecmdnamemalloc;
|
---|
[3434] | 1010 | if (args->flags & EV_EXIT)
|
---|
| 1011 | exitshell(psh, psh->exitstatus);
|
---|
| 1012 | }
|
---|
[3475] | 1013 | else if (savecmdnamemalloc)
|
---|
[3438] | 1014 | sh_free(psh, savecmdname);
|
---|
[3434] | 1015 | if (e != -1) {
|
---|
| 1016 | if ((e != EXERROR && e != EXEXEC)
|
---|
| 1017 | || args->cmdentry.cmdtype == CMDSPLBLTIN)
|
---|
| 1018 | exraise(psh, e);
|
---|
| 1019 | FORCEINTON;
|
---|
| 1020 | }
|
---|
| 1021 | if (args->cmdentry.u.bltin != execcmd)
|
---|
| 1022 | popredir(psh);
|
---|
| 1023 | if (args->flags == EV_BACKCMD) {
|
---|
| 1024 | args->backcmd->buf = psh->memout.buf;
|
---|
| 1025 | args->backcmd->nleft = (int)(psh->memout.nextc - psh->memout.buf);
|
---|
| 1026 | psh->memout.buf = NULL;
|
---|
| 1027 | }
|
---|
| 1028 | break;
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
| 1031 | default: {
|
---|
| 1032 | struct strlist *sp;
|
---|
| 1033 | char **envp;
|
---|
| 1034 | #ifdef DEBUG
|
---|
| 1035 | trputs(psh, "normal command: "); trargs(psh, args->argv);
|
---|
| 1036 | #endif
|
---|
[3435] | 1037 | clearredir(psh);
|
---|
| 1038 | redirect(psh, cmd->ncmd.redirect, 0);
|
---|
| 1039 | for (sp = args->varlist.list ; sp ; sp = sp->next)
|
---|
| 1040 | setvareq(psh, sp->text, VEXPORT|VSTACK);
|
---|
[3434] | 1041 | envp = environment(psh);
|
---|
[3475] | 1042 | shellexec(psh, args->argv, envp, args->path,
|
---|
| 1043 | args->cmdentry.u.n.index, args->cmdentry.u.n.suffix);
|
---|
[3434] | 1044 | break;
|
---|
| 1045 | }
|
---|
| 1046 | }
|
---|
| 1047 |
|
---|
[3453] | 1048 | evalcommand_out(psh, args->flags, args->lastarg, args->expfnamedepth, &args->smark);
|
---|
[3434] | 1049 | }
|
---|
| 1050 |
|
---|
[3437] | 1051 | /* child callback. */
|
---|
| 1052 | static int evalcommand_child(shinstance *psh, union node *cmd, void *argp)
|
---|
| 1053 | {
|
---|
| 1054 | struct evalcommanddoit *args = (struct evalcommanddoit *)argp;
|
---|
| 1055 |
|
---|
| 1056 | if (args->flags & EV_BACKCMD) {
|
---|
| 1057 | FORCEINTON;
|
---|
| 1058 | shfile_close(&psh->fdtab, args->pip[0]);
|
---|
| 1059 | if (args->pip[1] != 1) {
|
---|
| 1060 | movefd(psh, args->pip[1], 1);
|
---|
| 1061 | }
|
---|
| 1062 | }
|
---|
| 1063 | args->flags |= EV_EXIT;
|
---|
| 1064 |
|
---|
| 1065 | evalcommand_doit(psh, cmd, args);
|
---|
| 1066 | /* not reached */ /** @todo make it return here */
|
---|
| 1067 | return 0;
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
[3573] | 1070 | #ifdef KASH_USE_FORKSHELL2
|
---|
[3437] | 1071 | /* Copies data in the argument structure from parent to child. */
|
---|
| 1072 | static void evalcommand_setup_child(shinstance *pshchild, shinstance *pshparent, void *argp)
|
---|
| 1073 | {
|
---|
| 1074 | struct evalcommanddoit *args = (struct evalcommanddoit *)argp;
|
---|
| 1075 | char **argv;
|
---|
| 1076 | char **srcargv;
|
---|
| 1077 | struct strlist *sp;
|
---|
| 1078 | int argc, i;
|
---|
| 1079 |
|
---|
| 1080 | setstackmark(pshchild, &args->smark);
|
---|
| 1081 |
|
---|
| 1082 | /* copy arguments. */
|
---|
| 1083 | srcargv = args->argv;
|
---|
| 1084 | argc = args->argc;
|
---|
| 1085 | args->argv = argv = stalloc(pshchild, sizeof(char *) * (argc + 1));
|
---|
| 1086 | for (i = 0; i < argc; i++)
|
---|
| 1087 | argv[i] = stsavestr(pshchild, srcargv[i]);
|
---|
| 1088 | argv[argc] = NULL;
|
---|
| 1089 | if (args->lastarg)
|
---|
| 1090 | args->lastarg = argv[argc - 1];
|
---|
| 1091 |
|
---|
| 1092 | /* copy variable list, checking for the 'path'. */
|
---|
| 1093 | sp = args->varlist.list;
|
---|
| 1094 | args->varlist.list = NULL;
|
---|
| 1095 | args->varlist.lastp = &args->varlist.list;
|
---|
| 1096 | for (; sp; sp = sp->next) {
|
---|
| 1097 | struct strlist *snew = (struct strlist *)stalloc(pshchild, sizeof(*snew));
|
---|
| 1098 | char *text;
|
---|
| 1099 | snew->next = NULL;
|
---|
| 1100 | snew->text = text = stsavestr(pshchild, sp->text);
|
---|
| 1101 |
|
---|
| 1102 | if (&text[5] == args->path)
|
---|
| 1103 | args->path = &text[sizeof("PATH=") - 1];
|
---|
| 1104 |
|
---|
| 1105 | *args->varlist.lastp = snew;
|
---|
| 1106 | args->varlist.lastp = &snew->next;
|
---|
| 1107 | }
|
---|
| 1108 |
|
---|
| 1109 | if (args->path == pathval(pshparent))
|
---|
| 1110 | args->path = pathval(pshchild);
|
---|
| 1111 |
|
---|
| 1112 | /* back tick command should be ignored in this codepath
|
---|
| 1113 | (flags != EV_BACKCMD as EV_EXIT is ORed in). */
|
---|
| 1114 |
|
---|
[3458] | 1115 | /* If cmdentry references an internal function, we must duplicates (reference) it's nodes. */
|
---|
[3437] | 1116 | if (args->cmdentry.cmdtype == CMDFUNCTION)
|
---|
| 1117 | args->cmdentry.u.func = copyparsetree(pshchild, args->cmdentry.u.func); /** @todo isn't this duplicated already? */
|
---|
| 1118 | }
|
---|
[3573] | 1119 | #endif /* KASH_USE_FORKSHELL2 */
|
---|
[3437] | 1120 |
|
---|
[626] | 1121 | /*
|
---|
| 1122 | * Execute a simple command.
|
---|
| 1123 | */
|
---|
| 1124 |
|
---|
| 1125 | STATIC void
|
---|
[884] | 1126 | evalcommand(shinstance *psh, union node *cmd, int flags, struct backcmd *backcmd)
|
---|
[626] | 1127 | {
|
---|
[3434] | 1128 | struct evalcommanddoit args;
|
---|
[626] | 1129 | char **argv;
|
---|
| 1130 | int argc;
|
---|
[3434] | 1131 |
|
---|
| 1132 | union node *argp;
|
---|
[2654] | 1133 | int numvars;
|
---|
[3434] | 1134 | struct arglist arglist;
|
---|
[626] | 1135 | struct strlist *sp;
|
---|
[884] | 1136 | const char *path = pathval(psh);
|
---|
[626] | 1137 |
|
---|
| 1138 | /* First expand the arguments. */
|
---|
[1199] | 1139 | TRACE((psh, "evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
|
---|
[3434] | 1140 | setstackmark(psh, &args.smark);
|
---|
[2386] | 1141 | psh->back_exitstatus = 0;
|
---|
[626] | 1142 |
|
---|
| 1143 | arglist.lastp = &arglist.list;
|
---|
| 1144 | /* Expand arguments, ignoring the initial 'name=value' ones */
|
---|
[2654] | 1145 | for (argp = cmd->ncmd.args, numvars = 0 ; argp ; argp = argp->narg.next, numvars++) {
|
---|
| 1146 | char *p = argp->narg.text;
|
---|
| 1147 | char ch = *p;
|
---|
| 1148 | if (is_name(ch)) {
|
---|
| 1149 | do ch = *++p;
|
---|
| 1150 | while (is_in_name(ch));
|
---|
| 1151 | if (ch == '=')
|
---|
| 1152 | continue;
|
---|
| 1153 | }
|
---|
| 1154 | break;
|
---|
| 1155 | }
|
---|
| 1156 | for (/*continue on argp from above. */ ; argp ; argp = argp->narg.next)
|
---|
[884] | 1157 | expandarg(psh, argp, &arglist, EXP_FULL | EXP_TILDE);
|
---|
[626] | 1158 | *arglist.lastp = NULL;
|
---|
| 1159 |
|
---|
[3453] | 1160 | args.expfnamedepth = expredir(psh, cmd->ncmd.redirect);
|
---|
[626] | 1161 |
|
---|
| 1162 | /* Now do the initial 'name=value' ones we skipped above */
|
---|
[3434] | 1163 | args.varlist.lastp = &args.varlist.list;
|
---|
[2654] | 1164 | for (argp = cmd->ncmd.args ; numvars > 0 && argp ; argp = argp->narg.next, numvars--)
|
---|
[3434] | 1165 | expandarg(psh, argp, &args.varlist, EXP_VARTILDE);
|
---|
| 1166 | *args.varlist.lastp = NULL;
|
---|
[626] | 1167 |
|
---|
| 1168 | argc = 0;
|
---|
| 1169 | for (sp = arglist.list ; sp ; sp = sp->next)
|
---|
| 1170 | argc++;
|
---|
[3434] | 1171 | args.argc = argc;
|
---|
| 1172 | args.argv = argv = stalloc(psh, sizeof (char *) * (argc + 1));
|
---|
[626] | 1173 |
|
---|
| 1174 | for (sp = arglist.list ; sp ; sp = sp->next) {
|
---|
[1199] | 1175 | TRACE((psh, "evalcommand arg: %s\n", sp->text));
|
---|
[626] | 1176 | *argv++ = sp->text;
|
---|
| 1177 | }
|
---|
| 1178 | *argv = NULL;
|
---|
[3434] | 1179 | args.lastarg = NULL;
|
---|
[1198] | 1180 | if (iflag(psh) && psh->funcnest == 0 && argc > 0)
|
---|
[3434] | 1181 | args.lastarg = argv[-1];
|
---|
[626] | 1182 | argv -= argc;
|
---|
| 1183 |
|
---|
| 1184 | /* Print the command if xflag is set. */
|
---|
[1198] | 1185 | if (xflag(psh)) {
|
---|
[626] | 1186 | char sep = 0;
|
---|
[884] | 1187 | out2str(psh, ps4val(psh));
|
---|
[3434] | 1188 | for (sp = args.varlist.list ; sp ; sp = sp->next) {
|
---|
[626] | 1189 | if (sep != 0)
|
---|
[884] | 1190 | outc(sep, &psh->errout);
|
---|
| 1191 | out2str(psh, sp->text);
|
---|
[626] | 1192 | sep = ' ';
|
---|
| 1193 | }
|
---|
| 1194 | for (sp = arglist.list ; sp ; sp = sp->next) {
|
---|
| 1195 | if (sep != 0)
|
---|
[884] | 1196 | outc(sep, &psh->errout);
|
---|
| 1197 | out2str(psh, sp->text);
|
---|
[626] | 1198 | sep = ' ';
|
---|
| 1199 | }
|
---|
[884] | 1200 | outc('\n', &psh->errout);
|
---|
| 1201 | flushout(&psh->errout);
|
---|
[626] | 1202 | }
|
---|
| 1203 |
|
---|
| 1204 | /* Now locate the command. */
|
---|
| 1205 | if (argc == 0) {
|
---|
[3434] | 1206 | args.cmdentry.cmdtype = CMDSPLBLTIN;
|
---|
| 1207 | args.cmdentry.u.bltin = bltincmd;
|
---|
[626] | 1208 | } else {
|
---|
| 1209 | static const char PATH[] = "PATH=";
|
---|
| 1210 | int cmd_flags = DO_ERR;
|
---|
| 1211 |
|
---|
| 1212 | /*
|
---|
| 1213 | * Modify the command lookup path, if a PATH= assignment
|
---|
| 1214 | * is present
|
---|
| 1215 | */
|
---|
[3434] | 1216 | for (sp = args.varlist.list; sp; sp = sp->next)
|
---|
[626] | 1217 | if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
|
---|
| 1218 | path = sp->text + sizeof(PATH) - 1;
|
---|
| 1219 |
|
---|
| 1220 | do {
|
---|
| 1221 | int argsused, use_syspath;
|
---|
[3434] | 1222 | find_command(psh, argv[0], &args.cmdentry, cmd_flags, path);
|
---|
| 1223 | if (args.cmdentry.cmdtype == CMDUNKNOWN) {
|
---|
[884] | 1224 | psh->exitstatus = 127;
|
---|
| 1225 | flushout(&psh->errout);
|
---|
[3453] | 1226 | evalcommand_out(psh, flags, args.lastarg, args.expfnamedepth, &args.smark);
|
---|
[3434] | 1227 | return;
|
---|
[626] | 1228 | }
|
---|
| 1229 |
|
---|
| 1230 | /* implement the 'command' builtin here */
|
---|
[3434] | 1231 | if (args.cmdentry.cmdtype != CMDBUILTIN ||
|
---|
| 1232 | args.cmdentry.u.bltin != bltincmd)
|
---|
[626] | 1233 | break;
|
---|
| 1234 | cmd_flags |= DO_NOFUNC;
|
---|
[884] | 1235 | argsused = parse_command_args(psh, argc, argv, &use_syspath);
|
---|
[626] | 1236 | if (argsused == 0) {
|
---|
| 1237 | /* use 'type' builting to display info */
|
---|
[3434] | 1238 | args.cmdentry.u.bltin = typecmd;
|
---|
[626] | 1239 | break;
|
---|
| 1240 | }
|
---|
| 1241 | argc -= argsused;
|
---|
| 1242 | argv += argsused;
|
---|
| 1243 | if (use_syspath)
|
---|
[884] | 1244 | path = syspath(psh) + 5;
|
---|
[626] | 1245 | } while (argc != 0);
|
---|
[3434] | 1246 | if (args.cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
|
---|
[626] | 1247 | /* posix mandates that 'command <splbltin>' act as if
|
---|
| 1248 | <splbltin> was a normal builtin */
|
---|
[3434] | 1249 | args.cmdentry.cmdtype = CMDBUILTIN;
|
---|
[626] | 1250 | }
|
---|
| 1251 |
|
---|
| 1252 | /* Fork off a child process if necessary. */
|
---|
| 1253 | if (cmd->ncmd.backgnd
|
---|
[3434] | 1254 | || (args.cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
|
---|
| 1255 | || ( (flags & EV_BACKCMD) != 0
|
---|
| 1256 | && ( (args.cmdentry.cmdtype != CMDBUILTIN && args.cmdentry.cmdtype != CMDSPLBLTIN)
|
---|
| 1257 | || args.cmdentry.u.bltin == dotcmd
|
---|
| 1258 | || args.cmdentry.u.bltin == evalcmd))) {
|
---|
| 1259 | struct job *jp;
|
---|
| 1260 | int mode;
|
---|
[3437] | 1261 |
|
---|
[626] | 1262 | INTOFF;
|
---|
[884] | 1263 | jp = makejob(psh, cmd, 1);
|
---|
[626] | 1264 | mode = cmd->ncmd.backgnd;
|
---|
[3437] | 1265 | args.pip[0] = -1;
|
---|
| 1266 | args.pip[1] = -1;
|
---|
[626] | 1267 | if (flags & EV_BACKCMD) {
|
---|
| 1268 | mode = FORK_NOJOB;
|
---|
[3437] | 1269 | if (sh_pipe(psh, args.pip) < 0)
|
---|
[884] | 1270 | error(psh, "Pipe call failed");
|
---|
[626] | 1271 | }
|
---|
| 1272 |
|
---|
[3434] | 1273 | args.backcmd = backcmd;
|
---|
| 1274 | args.flags = flags;
|
---|
| 1275 | args.path = path;
|
---|
[3437] | 1276 | #ifdef KASH_USE_FORKSHELL2
|
---|
| 1277 | forkshell2(psh, jp, cmd, mode, evalcommand_child, cmd,
|
---|
| 1278 | &args, sizeof(args), evalcommand_setup_child);
|
---|
[3453] | 1279 | evalcommand_parent(psh, flags, args.lastarg, args.expfnamedepth,
|
---|
| 1280 | &args.smark, mode, jp, args.pip, backcmd);
|
---|
[3437] | 1281 | #else
|
---|
| 1282 | if (forkshell(psh, jp, cmd, mode) != 0) {
|
---|
[3453] | 1283 | evalcommand_parent(psh, flags, args.lastarg, args.expfnamedepth,
|
---|
| 1284 | &args.smark, mode, jp, args.pip, backcmd);
|
---|
[3437] | 1285 | return; /* at end of routine */
|
---|
| 1286 | }
|
---|
| 1287 | evalcommand_child(psh, cmd, &args);
|
---|
| 1288 | #endif
|
---|
| 1289 | } else {
|
---|
[3434] | 1290 | args.backcmd = backcmd;
|
---|
| 1291 | args.flags = flags;
|
---|
| 1292 | args.path = path;
|
---|
| 1293 | evalcommand_doit(psh, cmd, &args);
|
---|
[626] | 1294 | }
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
| 1297 |
|
---|
| 1298 | /*
|
---|
| 1299 | * Search for a command. This is called before we fork so that the
|
---|
| 1300 | * location of the command will be available in the parent as well as
|
---|
| 1301 | * the child. The check for "goodname" is an overly conservative
|
---|
| 1302 | * check that the name will not be subject to expansion.
|
---|
| 1303 | */
|
---|
| 1304 |
|
---|
| 1305 | STATIC void
|
---|
[884] | 1306 | prehash(shinstance *psh, union node *n)
|
---|
[626] | 1307 | {
|
---|
| 1308 | struct cmdentry entry;
|
---|
| 1309 |
|
---|
| 1310 | if (n->type == NCMD && n->ncmd.args)
|
---|
| 1311 | if (goodname(n->ncmd.args->narg.text))
|
---|
[884] | 1312 | find_command(psh, n->ncmd.args->narg.text, &entry, 0,
|
---|
[3437] | 1313 | pathval(psh));
|
---|
[626] | 1314 | }
|
---|
| 1315 |
|
---|
| 1316 |
|
---|
| 1317 |
|
---|
| 1318 | /*
|
---|
| 1319 | * Builtin commands. Builtin commands whose functions are closely
|
---|
| 1320 | * tied to evaluation are implemented here.
|
---|
| 1321 | */
|
---|
| 1322 |
|
---|
| 1323 | /*
|
---|
| 1324 | * No command given.
|
---|
| 1325 | */
|
---|
| 1326 |
|
---|
| 1327 | int
|
---|
[884] | 1328 | bltincmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 1329 | {
|
---|
| 1330 | /*
|
---|
[884] | 1331 | * Preserve psh->exitstatus of a previous possible redirection
|
---|
[626] | 1332 | * as POSIX mandates
|
---|
| 1333 | */
|
---|
[2387] | 1334 | return psh->back_exitstatus;
|
---|
[626] | 1335 | }
|
---|
| 1336 |
|
---|
| 1337 |
|
---|
| 1338 | /*
|
---|
| 1339 | * Handle break and continue commands. Break, continue, and return are
|
---|
[884] | 1340 | * all handled by setting the psh->evalskip flag. The evaluation routines
|
---|
[626] | 1341 | * above all check this flag, and if it is set they start skipping
|
---|
| 1342 | * commands rather than executing them. The variable skipcount is
|
---|
| 1343 | * the number of loops to break/continue, or the number of function
|
---|
| 1344 | * levels to return. (The latter is always 1.) It should probably
|
---|
| 1345 | * be an error to break out of more loops than exist, but it isn't
|
---|
| 1346 | * in the standard shell so we don't make it one here.
|
---|
| 1347 | */
|
---|
| 1348 |
|
---|
| 1349 | int
|
---|
[884] | 1350 | breakcmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 1351 | {
|
---|
[1198] | 1352 | int n = argc > 1 ? number(psh, argv[1]) : 1;
|
---|
[626] | 1353 |
|
---|
[884] | 1354 | if (n > psh->loopnest)
|
---|
| 1355 | n = psh->loopnest;
|
---|
[626] | 1356 | if (n > 0) {
|
---|
[884] | 1357 | psh->evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
|
---|
| 1358 | psh->skipcount = n;
|
---|
[626] | 1359 | }
|
---|
| 1360 | return 0;
|
---|
| 1361 | }
|
---|
| 1362 |
|
---|
| 1363 |
|
---|
| 1364 | /*
|
---|
| 1365 | * The return command.
|
---|
| 1366 | */
|
---|
| 1367 |
|
---|
| 1368 | int
|
---|
[884] | 1369 | returncmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 1370 | {
|
---|
[2384] | 1371 | #if 0
|
---|
[1198] | 1372 | int ret = argc > 1 ? number(psh, argv[1]) : psh->exitstatus;
|
---|
[2384] | 1373 | #else
|
---|
| 1374 | int ret;
|
---|
| 1375 | if (argc > 1) {
|
---|
| 1376 | /* make return -1 and VSC lite work ... */
|
---|
| 1377 | if (argv[1][0] != '-' || !is_number(&argv[1][1]))
|
---|
| 1378 | ret = number(psh, argv[1]);
|
---|
| 1379 | else
|
---|
| 1380 | ret = -number(psh, &argv[1][1]) & 255; /* take the bash approach */
|
---|
| 1381 | } else {
|
---|
| 1382 | ret = psh->exitstatus;
|
---|
| 1383 | }
|
---|
| 1384 | #endif
|
---|
[626] | 1385 |
|
---|
[884] | 1386 | if (psh->funcnest) {
|
---|
| 1387 | psh->evalskip = SKIPFUNC;
|
---|
| 1388 | psh->skipcount = 1;
|
---|
[626] | 1389 | return ret;
|
---|
| 1390 | }
|
---|
| 1391 | else {
|
---|
| 1392 | /* Do what ksh does; skip the rest of the file */
|
---|
[884] | 1393 | psh->evalskip = SKIPFILE;
|
---|
| 1394 | psh->skipcount = 1;
|
---|
[626] | 1395 | return ret;
|
---|
| 1396 | }
|
---|
| 1397 | }
|
---|
| 1398 |
|
---|
| 1399 |
|
---|
| 1400 | int
|
---|
[884] | 1401 | falsecmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 1402 | {
|
---|
| 1403 | return 1;
|
---|
| 1404 | }
|
---|
| 1405 |
|
---|
| 1406 |
|
---|
| 1407 | int
|
---|
[884] | 1408 | truecmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 1409 | {
|
---|
| 1410 | return 0;
|
---|
| 1411 | }
|
---|
| 1412 |
|
---|
| 1413 |
|
---|
| 1414 | int
|
---|
[884] | 1415 | execcmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 1416 | {
|
---|
| 1417 | if (argc > 1) {
|
---|
| 1418 | struct strlist *sp;
|
---|
| 1419 |
|
---|
[1198] | 1420 | iflag(psh) = 0; /* exit on error */
|
---|
| 1421 | mflag(psh) = 0;
|
---|
[884] | 1422 | optschanged(psh);
|
---|
| 1423 | for (sp = psh->cmdenviron; sp; sp = sp->next)
|
---|
| 1424 | setvareq(psh, sp->text, VEXPORT|VSTACK);
|
---|
[3475] | 1425 | shellexec(psh, argv + 1, environment(psh), pathval(psh), 0, -1);
|
---|
[626] | 1426 | }
|
---|
| 1427 | return 0;
|
---|
| 1428 | }
|
---|
| 1429 |
|
---|
| 1430 | static int
|
---|
| 1431 | conv_time(clock_t ticks, char *seconds, size_t l)
|
---|
| 1432 | {
|
---|
| 1433 | static clock_t tpm = 0;
|
---|
| 1434 | clock_t mins;
|
---|
[1198] | 1435 | size_t i;
|
---|
[626] | 1436 |
|
---|
| 1437 | if (!tpm)
|
---|
[884] | 1438 | tpm = /*sysconf(_SC_CLK_TCK)*/sh_sysconf_clk_tck() * 60;
|
---|
[626] | 1439 |
|
---|
| 1440 | mins = ticks / tpm;
|
---|
[1214] | 1441 | #ifdef _MSC_VER
|
---|
| 1442 | {
|
---|
| 1443 | char tmp[64];
|
---|
| 1444 | sprintf(tmp, "%.4f", (ticks - mins * tpm) * 60.0 / tpm);
|
---|
| 1445 | strlcpy(seconds, tmp, l);
|
---|
| 1446 | }
|
---|
| 1447 | #else
|
---|
[626] | 1448 | snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
|
---|
[1214] | 1449 | #endif
|
---|
[626] | 1450 |
|
---|
| 1451 | if (seconds[0] == '6' && seconds[1] == '0') {
|
---|
| 1452 | /* 59.99995 got rounded up... */
|
---|
| 1453 | mins++;
|
---|
| 1454 | strlcpy(seconds, "0.0", l);
|
---|
| 1455 | return mins;
|
---|
| 1456 | }
|
---|
| 1457 |
|
---|
| 1458 | /* suppress trailing zeros */
|
---|
| 1459 | i = strlen(seconds) - 1;
|
---|
| 1460 | for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
|
---|
| 1461 | seconds[i] = 0;
|
---|
| 1462 | return mins;
|
---|
| 1463 | }
|
---|
| 1464 |
|
---|
| 1465 | int
|
---|
[884] | 1466 | timescmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 1467 | {
|
---|
[1216] | 1468 | shtms tms;
|
---|
[626] | 1469 | int u, s, cu, cs;
|
---|
| 1470 | char us[8], ss[8], cus[8], css[8];
|
---|
| 1471 |
|
---|
[884] | 1472 | nextopt(psh, "");
|
---|
[626] | 1473 |
|
---|
[1216] | 1474 | sh_times(psh, &tms);
|
---|
[626] | 1475 |
|
---|
| 1476 | u = conv_time(tms.tms_utime, us, sizeof(us));
|
---|
| 1477 | s = conv_time(tms.tms_stime, ss, sizeof(ss));
|
---|
| 1478 | cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
|
---|
| 1479 | cs = conv_time(tms.tms_cstime, css, sizeof(css));
|
---|
| 1480 |
|
---|
[884] | 1481 | outfmt(psh->out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
|
---|
[626] | 1482 | u, us, s, ss, cu, cus, cs, css);
|
---|
| 1483 |
|
---|
| 1484 | return 0;
|
---|
| 1485 | }
|
---|