add opendir alias
[minix.git] / commands / ash / eval.c
blob4c1df9c55997f4a64bf1c109443288e4b8bcba46
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/eval.c,v 1.42 2004/04/06 20:06:51 markm Exp $");
43 #ifndef NO_PATHS_H
44 #include <paths.h>
45 #endif
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <sys/wait.h> /* For WIFSIGNALED(status) */
50 #include <errno.h>
53 * Evaluate a command.
56 #include "shell.h"
57 #include "nodes.h"
58 #include "syntax.h"
59 #include "expand.h"
60 #include "parser.h"
61 #include "jobs.h"
62 #include "eval.h"
63 #include "builtins.h"
64 #include "options.h"
65 #include "exec.h"
66 #include "redir.h"
67 #include "input.h"
68 #include "output.h"
69 #include "trap.h"
70 #include "var.h"
71 #include "memalloc.h"
72 #include "error.h"
73 #include "show.h"
74 #include "mystring.h"
75 #if !defined(NO_HISTORY)
76 #include "myhistedit.h"
77 #endif
79 #ifndef _PATH_STDPATH
80 #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin:"
81 #endif
83 /* flags in argument to evaltree */
84 #define EV_EXIT 01 /* exit after evaluating tree */
85 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
86 #define EV_BACKCMD 04 /* command executing within back quotes */
88 MKINIT int evalskip; /* set if we are skipping commands */
89 STATIC int skipcount; /* number of levels to skip */
90 MKINIT int loopnest; /* current loop nesting level */
91 int funcnest; /* depth of function calls */
94 char *commandname;
95 struct strlist *cmdenviron;
96 int exitstatus; /* exit status of last command */
97 int oexitstatus; /* saved exit status */
100 STATIC void evalloop(union node *);
101 STATIC void evalfor(union node *);
102 STATIC void evalcase(union node *, int);
103 STATIC void evalsubshell(union node *, int);
104 STATIC void expredir(union node *);
105 STATIC void evalpipe(union node *);
106 STATIC void evalcommand(union node *, int, struct backcmd *);
107 STATIC void prehash(union node *);
111 * Called to reset things after an exception.
114 #ifdef mkinit
115 INCLUDE "eval.h"
117 RESET {
118 evalskip = 0;
119 loopnest = 0;
120 funcnest = 0;
123 SHELLPROC {
124 exitstatus = 0;
126 #endif
131 * The eval command.
135 evalcmd(int argc, char **argv)
137 char *p;
138 char *concat;
139 char **ap;
141 if (argc > 1) {
142 p = argv[1];
143 if (argc > 2) {
144 STARTSTACKSTR(concat);
145 ap = argv + 2;
146 for (;;) {
147 while (*p)
148 STPUTC(*p++, concat);
149 if ((p = *ap++) == NULL)
150 break;
151 STPUTC(' ', concat);
153 STPUTC('\0', concat);
154 p = grabstackstr(concat);
156 evalstring(p);
158 return exitstatus;
163 * Execute a command or commands contained in a string.
166 void
167 evalstring(char *s)
169 union node *n;
170 struct stackmark smark;
172 setstackmark(&smark);
173 setinputstring(s, 1);
174 while ((n = parsecmd(0)) != NEOF) {
175 evaltree(n, 0);
176 popstackmark(&smark);
178 popfile();
179 popstackmark(&smark);
185 * Evaluate a parse tree. The value is left in the global variable
186 * exitstatus.
189 void
190 evaltree(union node *n, int flags)
192 if (n == NULL) {
193 TRACE(("evaltree(NULL) called\n"));
194 exitstatus = 0;
195 goto out;
197 #if !defined(NO_HISTORY)
198 displayhist = 1; /* show history substitutions done with fc */
199 #endif
200 TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
201 switch (n->type) {
202 case NSEMI:
203 evaltree(n->nbinary.ch1, 0);
204 if (evalskip)
205 goto out;
206 evaltree(n->nbinary.ch2, flags);
207 break;
208 case NAND:
209 evaltree(n->nbinary.ch1, EV_TESTED);
210 if (evalskip || exitstatus != 0) {
211 goto out;
213 evaltree(n->nbinary.ch2, flags);
214 break;
215 case NOR:
216 evaltree(n->nbinary.ch1, EV_TESTED);
217 if (evalskip || exitstatus == 0)
218 goto out;
219 evaltree(n->nbinary.ch2, flags);
220 break;
221 case NREDIR:
222 expredir(n->nredir.redirect);
223 redirect(n->nredir.redirect, REDIR_PUSH);
224 evaltree(n->nredir.n, flags);
225 popredir();
226 break;
227 case NSUBSHELL:
228 evalsubshell(n, flags);
229 break;
230 case NBACKGND:
231 evalsubshell(n, flags);
232 break;
233 case NIF: {
234 evaltree(n->nif.test, EV_TESTED);
235 if (evalskip)
236 goto out;
237 if (exitstatus == 0)
238 evaltree(n->nif.ifpart, flags);
239 else if (n->nif.elsepart)
240 evaltree(n->nif.elsepart, flags);
241 else
242 exitstatus = 0;
243 break;
245 case NWHILE:
246 case NUNTIL:
247 evalloop(n);
248 break;
249 case NFOR:
250 evalfor(n);
251 break;
252 case NCASE:
253 evalcase(n, flags);
254 break;
255 case NDEFUN:
256 defun(n->narg.text, n->narg.next);
257 exitstatus = 0;
258 break;
259 case NNOT:
260 evaltree(n->nnot.com, EV_TESTED);
261 exitstatus = !exitstatus;
262 break;
264 case NPIPE:
265 evalpipe(n);
266 break;
267 case NCMD:
268 evalcommand(n, flags, (struct backcmd *)NULL);
269 break;
270 default:
271 out1fmt("Node type = %d\n", n->type);
272 flushout(&output);
273 break;
275 out:
276 if (pendingsigs)
277 dotrap();
278 if ((flags & EV_EXIT) || (eflag && exitstatus
279 && !(flags & EV_TESTED) && (n->type == NCMD ||
280 n->type == NSUBSHELL)))
281 exitshell(exitstatus);
285 STATIC void
286 evalloop(union node *n)
288 int status;
290 loopnest++;
291 status = 0;
292 for (;;) {
293 evaltree(n->nbinary.ch1, EV_TESTED);
294 if (evalskip) {
295 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
296 evalskip = 0;
297 continue;
299 if (evalskip == SKIPBREAK && --skipcount <= 0)
300 evalskip = 0;
301 break;
303 if (n->type == NWHILE) {
304 if (exitstatus != 0)
305 break;
306 } else {
307 if (exitstatus == 0)
308 break;
310 evaltree(n->nbinary.ch2, 0);
311 status = exitstatus;
312 if (evalskip)
313 goto skipping;
315 loopnest--;
316 exitstatus = status;
321 STATIC void
322 evalfor(union node *n)
324 struct arglist arglist;
325 union node *argp;
326 struct strlist *sp;
327 struct stackmark smark;
329 setstackmark(&smark);
330 arglist.lastp = &arglist.list;
331 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
332 oexitstatus = exitstatus;
333 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
334 if (evalskip)
335 goto out;
337 *arglist.lastp = NULL;
339 exitstatus = 0;
340 loopnest++;
341 for (sp = arglist.list ; sp ; sp = sp->next) {
342 setvar(n->nfor.var, sp->text, 0);
343 evaltree(n->nfor.body, 0);
344 if (evalskip) {
345 if (evalskip == SKIPCONT && --skipcount <= 0) {
346 evalskip = 0;
347 continue;
349 if (evalskip == SKIPBREAK && --skipcount <= 0)
350 evalskip = 0;
351 break;
354 loopnest--;
355 out:
356 popstackmark(&smark);
361 STATIC void
362 evalcase(union node *n, int flags)
364 union node *cp;
365 union node *patp;
366 struct arglist arglist;
367 struct stackmark smark;
369 setstackmark(&smark);
370 arglist.lastp = &arglist.list;
371 oexitstatus = exitstatus;
372 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
373 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
374 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
375 if (casematch(patp, arglist.list->text)) {
376 if (evalskip == 0) {
377 evaltree(cp->nclist.body, flags);
379 goto out;
383 out:
384 popstackmark(&smark);
390 * Kick off a subshell to evaluate a tree.
393 STATIC void
394 evalsubshell(union node *n, int flags)
396 struct job *jp;
397 int backgnd = (n->type == NBACKGND);
399 expredir(n->nredir.redirect);
400 jp = makejob(n, 1);
401 if (forkshell(jp, n, backgnd) == 0) {
402 if (backgnd)
403 flags &=~ EV_TESTED;
404 redirect(n->nredir.redirect, 0);
405 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
407 if (! backgnd) {
408 INTOFF;
409 exitstatus = waitforjob(jp, (int *)NULL);
410 INTON;
417 * Compute the names of the files in a redirection list.
420 STATIC void
421 expredir(union node *n)
423 union node *redir;
425 for (redir = n ; redir ; redir = redir->nfile.next) {
426 struct arglist fn;
427 fn.lastp = &fn.list;
428 oexitstatus = exitstatus;
429 switch (redir->type) {
430 case NFROM:
431 case NTO:
432 case NFROMTO:
433 case NAPPEND:
434 case NCLOBBER:
435 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
436 redir->nfile.expfname = fn.list->text;
437 break;
438 case NFROMFD:
439 case NTOFD:
440 if (redir->ndup.vname) {
441 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
442 fixredir(redir, fn.list->text, 1);
444 break;
452 * Evaluate a pipeline. All the processes in the pipeline are children
453 * of the process creating the pipeline. (This differs from some versions
454 * of the shell, which make the last process in a pipeline the parent
455 * of all the rest.)
458 STATIC void
459 evalpipe(union node *n)
461 struct job *jp;
462 struct nodelist *lp;
463 int pipelen;
464 int prevfd;
465 int pip[2];
467 TRACE(("evalpipe(0x%lx) called\n", (long)n));
468 pipelen = 0;
469 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
470 pipelen++;
471 INTOFF;
472 jp = makejob(n, pipelen);
473 prevfd = -1;
474 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
475 prehash(lp->n);
476 pip[1] = -1;
477 if (lp->next) {
478 if (pipe(pip) < 0) {
479 close(prevfd);
480 error("Pipe call failed: %s", strerror(errno));
483 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
484 INTON;
485 if (prevfd > 0) {
486 dup2(prevfd, 0);
487 close(prevfd);
489 if (pip[1] >= 0) {
490 if (!(prevfd >= 0 && pip[0] == 0))
491 close(pip[0]);
492 if (pip[1] != 1) {
493 dup2(pip[1], 1);
494 close(pip[1]);
497 evaltree(lp->n, EV_EXIT);
499 if (prevfd >= 0)
500 close(prevfd);
501 prevfd = pip[0];
502 close(pip[1]);
504 INTON;
505 if (n->npipe.backgnd == 0) {
506 INTOFF;
507 exitstatus = waitforjob(jp, (int *)NULL);
508 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
509 INTON;
516 * Execute a command inside back quotes. If it's a builtin command, we
517 * want to save its output in a block obtained from malloc. Otherwise
518 * we fork off a subprocess and get the output of the command via a pipe.
519 * Should be called with interrupts off.
522 void
523 evalbackcmd(union node *n, struct backcmd *result)
525 int pip[2];
526 struct job *jp;
527 struct stackmark smark; /* unnecessary */
529 setstackmark(&smark);
530 result->fd = -1;
531 result->buf = NULL;
532 result->nleft = 0;
533 result->jp = NULL;
534 if (n == NULL) {
535 exitstatus = 0;
536 goto out;
538 if (n->type == NCMD) {
539 exitstatus = oexitstatus;
540 evalcommand(n, EV_BACKCMD, result);
541 } else {
542 exitstatus = 0;
543 if (pipe(pip) < 0)
544 error("Pipe call failed: %s", strerror(errno));
545 jp = makejob(n, 1);
546 if (forkshell(jp, n, FORK_NOJOB) == 0) {
547 FORCEINTON;
548 close(pip[0]);
549 if (pip[1] != 1) {
550 dup2(pip[1], 1);
551 close(pip[1]);
553 evaltree(n, EV_EXIT);
555 close(pip[1]);
556 result->fd = pip[0];
557 result->jp = jp;
559 out:
560 popstackmark(&smark);
561 TRACE(("evalbackcmd done: fd=%d buf=%p nleft=%d jp=%p\n",
562 result->fd, result->buf, result->nleft, result->jp));
568 * Execute a simple command.
571 STATIC void
572 evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
574 struct stackmark smark;
575 union node *argp;
576 struct arglist arglist;
577 struct arglist varlist;
578 char **argv;
579 int argc;
580 char **envp;
581 int varflag;
582 struct strlist *sp;
583 int mode;
584 int pip[2];
585 struct cmdentry cmdentry;
586 struct job *jp;
587 struct jmploc jmploc;
588 struct jmploc *volatile savehandler;
589 char *volatile savecmdname;
590 volatile struct shparam saveparam;
591 struct localvar *volatile savelocalvars;
592 volatile int e;
593 char *lastarg;
594 int realstatus;
595 int do_clearcmdentry;
596 #if __GNUC__
597 /* Avoid longjmp clobbering */
598 (void) &argv;
599 (void) &argc;
600 (void) &lastarg;
601 (void) &flags;
602 (void) &do_clearcmdentry;
603 #endif
605 /* First expand the arguments. */
606 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
607 setstackmark(&smark);
608 arglist.lastp = &arglist.list;
609 varlist.lastp = &varlist.list;
610 varflag = 1;
611 do_clearcmdentry = 0;
612 oexitstatus = exitstatus;
613 exitstatus = 0;
614 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
615 char *p = argp->narg.text;
616 if (varflag && is_name(*p)) {
617 do {
618 p++;
619 } while (is_in_name(*p));
620 if (*p == '=') {
621 expandarg(argp, &varlist, EXP_VARTILDE);
622 continue;
625 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
626 varflag = 0;
628 *arglist.lastp = NULL;
629 *varlist.lastp = NULL;
630 expredir(cmd->ncmd.redirect);
631 argc = 0;
632 for (sp = arglist.list ; sp ; sp = sp->next)
633 argc++;
634 argv = stalloc(sizeof (char *) * (argc + 1));
636 for (sp = arglist.list ; sp ; sp = sp->next) {
637 TRACE(("evalcommand arg: %s\n", sp->text));
638 *argv++ = sp->text;
640 *argv = NULL;
641 lastarg = NULL;
642 if (iflag && funcnest == 0 && argc > 0)
643 lastarg = argv[-1];
644 argv -= argc;
646 /* Print the command if xflag is set. */
647 if (xflag) {
648 outc('+', &errout);
649 for (sp = varlist.list ; sp ; sp = sp->next) {
650 outc(' ', &errout);
651 out2str(sp->text);
653 for (sp = arglist.list ; sp ; sp = sp->next) {
654 outc(' ', &errout);
655 out2str(sp->text);
657 outc('\n', &errout);
658 flushout(&errout);
661 /* Now locate the command. */
662 if (argc == 0) {
663 cmdentry.cmdtype = CMDBUILTIN;
664 cmdentry.u.index = BLTINCMD;
665 } else {
666 static const char PATH[] = "PATH=";
667 char *path = pathval();
670 * Modify the command lookup path, if a PATH= assignment
671 * is present
673 for (sp = varlist.list ; sp ; sp = sp->next)
674 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) {
675 path = sp->text + sizeof(PATH) - 1;
677 * On `PATH=... command`, we need to make
678 * sure that the command isn't using the
679 * non-updated hash table of the outer PATH
680 * setting and we need to make sure that
681 * the hash table isn't filled with items
682 * from the temporary setting.
684 * It would be better to forbit using and
685 * updating the table while this command
686 * runs, by the command finding mechanism
687 * is heavily integrated with hash handling,
688 * so we just delete the hash before and after
689 * the command runs. Partly deleting like
690 * changepatch() does doesn't seem worth the
691 * bookinging effort, since most such runs add
692 * directories in front of the new PATH.
694 clearcmdentry(0);
695 do_clearcmdentry = 1;
698 find_command(argv[0], &cmdentry, 1, path);
699 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
700 exitstatus = 127;
701 flushout(&errout);
702 return;
704 /* implement the bltin builtin here */
705 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
706 for (;;) {
707 argv++;
708 if (--argc == 0)
709 break;
710 if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
711 outfmt(&errout, "%s: not found\n", *argv);
712 exitstatus = 127;
713 flushout(&errout);
714 return;
716 if (cmdentry.u.index != BLTINCMD)
717 break;
722 /* Fork off a child process if necessary. */
723 if (cmd->ncmd.backgnd
724 || (cmdentry.cmdtype == CMDNORMAL
725 && ((flags & EV_EXIT) == 0 || Tflag))
726 || ((flags & EV_BACKCMD) != 0
727 && (cmdentry.cmdtype != CMDBUILTIN
728 || cmdentry.u.index == CDCMD
729 || cmdentry.u.index == DOTCMD
730 || cmdentry.u.index == EVALCMD))
731 || (cmdentry.cmdtype == CMDBUILTIN &&
732 cmdentry.u.index == COMMANDCMD)) {
733 jp = makejob(cmd, 1);
734 mode = cmd->ncmd.backgnd;
735 if (flags & EV_BACKCMD) {
736 mode = FORK_NOJOB;
737 if (pipe(pip) < 0)
738 error("Pipe call failed: %s", strerror(errno));
740 if (forkshell(jp, cmd, mode) != 0)
741 goto parent; /* at end of routine */
742 if (flags & EV_BACKCMD) {
743 FORCEINTON;
744 close(pip[0]);
745 if (pip[1] != 1) {
746 dup2(pip[1], 1);
747 close(pip[1]);
750 flags |= EV_EXIT;
753 /* This is the child process if a fork occurred. */
754 /* Execute the command. */
755 if (cmdentry.cmdtype == CMDFUNCTION) {
756 #if DEBUG
757 trputs("Shell function: "); trargs(argv);
758 #endif
759 redirect(cmd->ncmd.redirect, REDIR_PUSH);
760 saveparam = shellparam;
761 shellparam.malloc = 0;
762 shellparam.reset = 1;
763 shellparam.nparam = argc - 1;
764 shellparam.p = argv + 1;
765 shellparam.optnext = NULL;
766 INTOFF;
767 savelocalvars = localvars;
768 localvars = NULL;
769 INTON;
770 if (setjmp(jmploc.loc)) {
771 if (exception == EXSHELLPROC)
772 freeparam((struct shparam *)&saveparam);
773 else {
774 freeparam(&shellparam);
775 shellparam = saveparam;
777 poplocalvars();
778 localvars = savelocalvars;
779 handler = savehandler;
780 longjmp(handler->loc, 1);
782 savehandler = handler;
783 handler = &jmploc;
784 for (sp = varlist.list ; sp ; sp = sp->next)
785 mklocal(sp->text);
786 funcnest++;
787 if (flags & EV_TESTED)
788 evaltree(cmdentry.u.func, EV_TESTED);
789 else
790 evaltree(cmdentry.u.func, 0);
791 funcnest--;
792 INTOFF;
793 poplocalvars();
794 localvars = savelocalvars;
795 freeparam(&shellparam);
796 shellparam = saveparam;
797 handler = savehandler;
798 popredir();
799 INTON;
800 if (evalskip == SKIPFUNC) {
801 evalskip = 0;
802 skipcount = 0;
804 if (flags & EV_EXIT)
805 exitshell(exitstatus);
806 } else if (cmdentry.cmdtype == CMDBUILTIN) {
807 #if DEBUG
808 trputs("builtin command: "); trargs(argv);
809 #endif
810 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
811 if (flags == EV_BACKCMD) {
812 memout.nleft = 0;
813 memout.nextc = memout.buf;
814 memout.bufsize = 64;
815 mode |= REDIR_BACKQ;
817 redirect(cmd->ncmd.redirect, mode);
818 savecmdname = commandname;
819 cmdenviron = varlist.list;
820 e = -1;
821 if (setjmp(jmploc.loc)) {
822 e = exception;
823 exitstatus = (e == EXINT)? SIGINT+128 : 2;
824 goto cmddone;
826 savehandler = handler;
827 handler = &jmploc;
828 commandname = argv[0];
829 argptr = argv + 1;
830 optptr = NULL; /* initialize nextopt */
831 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
832 flushall();
833 cmddone:
834 cmdenviron = NULL;
835 out1 = &output;
836 out2 = &errout;
837 freestdout();
838 if (e != EXSHELLPROC) {
839 commandname = savecmdname;
840 if (flags & EV_EXIT) {
841 exitshell(exitstatus);
844 handler = savehandler;
845 if (e != -1) {
846 if ((e != EXERROR && e != EXEXEC)
847 || cmdentry.u.index == BLTINCMD
848 || cmdentry.u.index == DOTCMD
849 || cmdentry.u.index == EVALCMD
850 #ifndef NO_HISTORY
851 || cmdentry.u.index == HISTCMD
852 #endif
853 || cmdentry.u.index == EXECCMD
854 || cmdentry.u.index == COMMANDCMD)
855 exraise(e);
856 FORCEINTON;
858 if (cmdentry.u.index != EXECCMD)
859 popredir();
860 if (flags == EV_BACKCMD) {
861 backcmd->buf = memout.buf;
862 backcmd->nleft = memout.nextc - memout.buf;
863 memout.buf = NULL;
865 } else {
866 #if DEBUG
867 trputs("normal command: "); trargs(argv);
868 #endif
869 clearredir();
870 redirect(cmd->ncmd.redirect, 0);
871 for (sp = varlist.list ; sp ; sp = sp->next)
872 setvareq(sp->text, VEXPORT|VSTACK);
873 envp = environment();
874 shellexec(argv, envp, pathval(), cmdentry.u.index);
875 /*NOTREACHED*/
877 goto out;
879 parent: /* parent process gets here (if we forked) */
880 if (mode == 0) { /* argument to fork */
881 INTOFF;
882 exitstatus = waitforjob(jp, &realstatus);
883 INTON;
884 if (iflag && loopnest > 0 && WIFSIGNALED(realstatus)) {
885 evalskip = SKIPBREAK;
886 skipcount = loopnest;
888 } else if (mode == 2) {
889 backcmd->fd = pip[0];
890 close(pip[1]);
891 backcmd->jp = jp;
894 out:
895 if (lastarg)
896 setvar("_", lastarg, 0);
897 if (do_clearcmdentry)
898 clearcmdentry(0);
899 popstackmark(&smark);
905 * Search for a command. This is called before we fork so that the
906 * location of the command will be available in the parent as well as
907 * the child. The check for "goodname" is an overly conservative
908 * check that the name will not be subject to expansion.
911 STATIC void
912 prehash(union node *n)
914 struct cmdentry entry;
916 if (n->type == NCMD && n->ncmd.args)
917 if (goodname(n->ncmd.args->narg.text))
918 find_command(n->ncmd.args->narg.text, &entry, 0,
919 pathval());
925 * Builtin commands. Builtin commands whose functions are closely
926 * tied to evaluation are implemented here.
930 * No command given, or a bltin command with no arguments. Set the
931 * specified variables.
935 bltincmd(int argc __unused, char **argv __unused)
937 listsetvar(cmdenviron);
939 * Preserve exitstatus of a previous possible redirection
940 * as POSIX mandates
942 return exitstatus;
947 * Handle break and continue commands. Break, continue, and return are
948 * all handled by setting the evalskip flag. The evaluation routines
949 * above all check this flag, and if it is set they start skipping
950 * commands rather than executing them. The variable skipcount is
951 * the number of loops to break/continue, or the number of function
952 * levels to return. (The latter is always 1.) It should probably
953 * be an error to break out of more loops than exist, but it isn't
954 * in the standard shell so we don't make it one here.
958 breakcmd(int argc, char **argv)
960 int n = argc > 1 ? number(argv[1]) : 1;
962 if (n > loopnest)
963 n = loopnest;
964 if (n > 0) {
965 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
966 skipcount = n;
968 return 0;
972 * The `command' command.
975 commandcmd(int argc, char **argv)
977 static char stdpath[] = _PATH_STDPATH;
978 struct jmploc loc, *old;
979 struct strlist *sp;
980 char *path;
981 int ch;
983 for (sp = cmdenviron; sp ; sp = sp->next)
984 setvareq(sp->text, VEXPORT|VSTACK);
985 path = pathval();
987 optind = optreset = 1;
988 opterr = 0;
989 while ((ch = getopt(argc, argv, "p")) != -1) {
990 switch (ch) {
991 case 'p':
992 path = stdpath;
993 break;
994 case '?':
995 default:
996 error("unknown option: -%c", optopt);
999 argc -= optind;
1000 argv += optind;
1002 if (argc != 0) {
1003 old = handler;
1004 handler = &loc;
1005 if (setjmp(handler->loc) == 0)
1006 shellexec(argv, environment(), path, 0);
1007 handler = old;
1008 if (exception == EXEXEC)
1009 exit(exerrno);
1010 exraise(exception);
1014 * Do nothing successfully if no command was specified;
1015 * ksh also does this.
1017 exit(0);
1022 * The return command.
1026 returncmd(int argc, char **argv)
1028 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1030 if (funcnest) {
1031 evalskip = SKIPFUNC;
1032 skipcount = 1;
1033 } else {
1034 /* skip the rest of the file */
1035 evalskip = SKIPFILE;
1036 skipcount = 1;
1038 return ret;
1043 falsecmd(int argc __unused, char **argv __unused)
1045 return 1;
1050 truecmd(int argc __unused, char **argv __unused)
1052 return 0;
1057 execcmd(int argc, char **argv)
1059 if (argc > 1) {
1060 struct strlist *sp;
1062 iflag = 0; /* exit on error */
1063 mflag = 0;
1064 optschanged();
1065 for (sp = cmdenviron; sp ; sp = sp->next)
1066 setvareq(sp->text, VEXPORT|VSTACK);
1067 shellexec(argv + 1, environment(), pathval(), 0);
1070 return 0;
1074 * $PchId: eval.c,v 1.7 2006/04/10 14:46:14 philip Exp $