source: trunk/src/kash/eval.c@ 3433

Last change on this file since 3433 was 3433, checked in by bird, 5 years ago

kash: refactoring forkshell(); simple stuff.

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