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