eval.c revision 1.48 1 /* $NetBSD: eval.c,v 1.48 1999/10/10 21:22:24 pk 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
43 #else
44 __RCSID("$NetBSD: eval.c,v 1.48 1999/10/10 21:22:24 pk Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <signal.h>
49 #include <unistd.h>
50
51 /*
52 * Evaluate a command.
53 */
54
55 #include "shell.h"
56 #include "nodes.h"
57 #include "syntax.h"
58 #include "expand.h"
59 #include "parser.h"
60 #include "jobs.h"
61 #include "eval.h"
62 #include "builtins.h"
63 #include "options.h"
64 #include "exec.h"
65 #include "redir.h"
66 #include "input.h"
67 #include "output.h"
68 #include "trap.h"
69 #include "var.h"
70 #include "memalloc.h"
71 #include "error.h"
72 #include "show.h"
73 #include "mystring.h"
74 #ifndef SMALL
75 #include "myhistedit.h"
76 #endif
77
78
79 /* flags in argument to evaltree */
80 #define EV_EXIT 01 /* exit after evaluating tree */
81 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
82 #define EV_BACKCMD 04 /* command executing within back quotes */
83
84 MKINIT int evalskip; /* set if we are skipping commands */
85 STATIC int skipcount; /* number of levels to skip */
86 MKINIT int loopnest; /* current loop nesting level */
87 int funcnest; /* depth of function calls */
88
89
90 char *commandname;
91 struct strlist *cmdenviron;
92 int exitstatus; /* exit status of last command */
93 int oexitstatus; /* saved exit status */
94
95
96 STATIC void evalloop __P((union node *));
97 STATIC void evalfor __P((union node *));
98 STATIC void evalcase __P((union node *, int));
99 STATIC void evalsubshell __P((union node *, int));
100 STATIC void expredir __P((union node *));
101 STATIC void evalpipe __P((union node *));
102 STATIC void evalcommand __P((union node *, int, struct backcmd *));
103 STATIC void prehash __P((union node *));
104
105
106 /*
107 * Called to reset things after an exception.
108 */
109
110 #ifdef mkinit
111 INCLUDE "eval.h"
112
113 RESET {
114 evalskip = 0;
115 loopnest = 0;
116 funcnest = 0;
117 }
118
119 SHELLPROC {
120 exitstatus = 0;
121 }
122 #endif
123
124
125
126 /*
127 * The eval commmand.
128 */
129
130 int
131 evalcmd(argc, argv)
132 int argc;
133 char **argv;
134 {
135 char *p;
136 char *concat;
137 char **ap;
138
139 if (argc > 1) {
140 p = argv[1];
141 if (argc > 2) {
142 STARTSTACKSTR(concat);
143 ap = argv + 2;
144 for (;;) {
145 while (*p)
146 STPUTC(*p++, concat);
147 if ((p = *ap++) == NULL)
148 break;
149 STPUTC(' ', concat);
150 }
151 STPUTC('\0', concat);
152 p = grabstackstr(concat);
153 }
154 evalstring(p);
155 }
156 return exitstatus;
157 }
158
159
160 /*
161 * Execute a command or commands contained in a string.
162 */
163
164 void
165 evalstring(s)
166 char *s;
167 {
168 union node *n;
169 struct stackmark smark;
170
171 setstackmark(&smark);
172 setinputstring(s, 1);
173 while ((n = parsecmd(0)) != NEOF) {
174 evaltree(n, 0);
175 popstackmark(&smark);
176 }
177 popfile();
178 popstackmark(&smark);
179 }
180
181
182
183 /*
184 * Evaluate a parse tree. The value is left in the global variable
185 * exitstatus.
186 */
187
188 void
189 evaltree(n, flags)
190 union node *n;
191 int flags;
192 {
193 if (n == NULL) {
194 TRACE(("evaltree(NULL) called\n"));
195 exitstatus = 0;
196 goto out;
197 }
198 #ifndef SMALL
199 displayhist = 1; /* show history substitutions done with fc */
200 #endif
201 TRACE(("evaltree(0x%lx: %d) called\n", (long)n, n->type));
202 switch (n->type) {
203 case NSEMI:
204 evaltree(n->nbinary.ch1, 0);
205 if (evalskip)
206 goto out;
207 evaltree(n->nbinary.ch2, flags);
208 break;
209 case NAND:
210 evaltree(n->nbinary.ch1, EV_TESTED);
211 if (evalskip || exitstatus != 0) {
212 /* don't bomb out on "set -e; false && true" */
213 exitstatus = oexitstatus;
214 flags |= EV_TESTED;
215 goto out;
216 }
217 evaltree(n->nbinary.ch2, flags);
218 break;
219 case NOR:
220 evaltree(n->nbinary.ch1, EV_TESTED);
221 if (evalskip || exitstatus == 0)
222 goto out;
223 evaltree(n->nbinary.ch2, flags);
224 break;
225 case NREDIR:
226 expredir(n->nredir.redirect);
227 redirect(n->nredir.redirect, REDIR_PUSH);
228 evaltree(n->nredir.n, flags);
229 popredir();
230 break;
231 case NSUBSHELL:
232 evalsubshell(n, flags);
233 break;
234 case NBACKGND:
235 evalsubshell(n, flags);
236 break;
237 case NIF: {
238 evaltree(n->nif.test, EV_TESTED);
239 if (evalskip)
240 goto out;
241 if (exitstatus == 0)
242 evaltree(n->nif.ifpart, flags);
243 else if (n->nif.elsepart)
244 evaltree(n->nif.elsepart, flags);
245 else
246 exitstatus = 0;
247 break;
248 }
249 case NWHILE:
250 case NUNTIL:
251 evalloop(n);
252 break;
253 case NFOR:
254 evalfor(n);
255 break;
256 case NCASE:
257 evalcase(n, flags);
258 break;
259 case NDEFUN:
260 defun(n->narg.text, n->narg.next);
261 exitstatus = 0;
262 break;
263 case NNOT:
264 evaltree(n->nnot.com, EV_TESTED);
265 exitstatus = !exitstatus;
266 break;
267
268 case NPIPE:
269 evalpipe(n);
270 break;
271 case NCMD:
272 evalcommand(n, flags, (struct backcmd *)NULL);
273 break;
274 default:
275 out1fmt("Node type = %d\n", n->type);
276 flushout(&output);
277 break;
278 }
279 out:
280 if (pendingsigs)
281 dotrap();
282 if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
283 exitshell(exitstatus);
284 }
285
286
287 STATIC void
288 evalloop(n)
289 union node *n;
290 {
291 int status;
292
293 loopnest++;
294 status = 0;
295 for (;;) {
296 evaltree(n->nbinary.ch1, EV_TESTED);
297 if (evalskip) {
298 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
299 evalskip = 0;
300 continue;
301 }
302 if (evalskip == SKIPBREAK && --skipcount <= 0)
303 evalskip = 0;
304 break;
305 }
306 if (n->type == NWHILE) {
307 if (exitstatus != 0)
308 break;
309 } else {
310 if (exitstatus == 0)
311 break;
312 }
313 evaltree(n->nbinary.ch2, 0);
314 status = exitstatus;
315 if (evalskip)
316 goto skipping;
317 }
318 loopnest--;
319 exitstatus = status;
320 }
321
322
323
324 STATIC void
325 evalfor(n)
326 union node *n;
327 {
328 struct arglist arglist;
329 union node *argp;
330 struct strlist *sp;
331 struct stackmark smark;
332
333 setstackmark(&smark);
334 arglist.lastp = &arglist.list;
335 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
336 oexitstatus = exitstatus;
337 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD);
338 if (evalskip)
339 goto out;
340 }
341 *arglist.lastp = NULL;
342
343 exitstatus = 0;
344 loopnest++;
345 for (sp = arglist.list ; sp ; sp = sp->next) {
346 setvar(n->nfor.var, sp->text, 0);
347 evaltree(n->nfor.body, 0);
348 if (evalskip) {
349 if (evalskip == SKIPCONT && --skipcount <= 0) {
350 evalskip = 0;
351 continue;
352 }
353 if (evalskip == SKIPBREAK && --skipcount <= 0)
354 evalskip = 0;
355 break;
356 }
357 }
358 loopnest--;
359 out:
360 popstackmark(&smark);
361 }
362
363
364
365 STATIC void
366 evalcase(n, flags)
367 union node *n;
368 int flags;
369 {
370 union node *cp;
371 union node *patp;
372 struct arglist arglist;
373 struct stackmark smark;
374
375 setstackmark(&smark);
376 arglist.lastp = &arglist.list;
377 oexitstatus = exitstatus;
378 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
379 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
380 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
381 if (casematch(patp, arglist.list->text)) {
382 if (evalskip == 0) {
383 evaltree(cp->nclist.body, flags);
384 }
385 goto out;
386 }
387 }
388 }
389 out:
390 popstackmark(&smark);
391 }
392
393
394
395 /*
396 * Kick off a subshell to evaluate a tree.
397 */
398
399 STATIC void
400 evalsubshell(n, flags)
401 union node *n;
402 int flags;
403 {
404 struct job *jp;
405 int backgnd = (n->type == NBACKGND);
406
407 expredir(n->nredir.redirect);
408 jp = makejob(n, 1);
409 if (forkshell(jp, n, backgnd) == 0) {
410 if (backgnd)
411 flags &=~ EV_TESTED;
412 redirect(n->nredir.redirect, 0);
413 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
414 }
415 if (! backgnd) {
416 INTOFF;
417 exitstatus = waitforjob(jp);
418 INTON;
419 }
420 }
421
422
423
424 /*
425 * Compute the names of the files in a redirection list.
426 */
427
428 STATIC void
429 expredir(n)
430 union node *n;
431 {
432 union node *redir;
433
434 for (redir = n ; redir ; redir = redir->nfile.next) {
435 struct arglist fn;
436 fn.lastp = &fn.list;
437 oexitstatus = exitstatus;
438 switch (redir->type) {
439 case NFROMTO:
440 case NFROM:
441 case NTO:
442 case NAPPEND:
443 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
444 redir->nfile.expfname = fn.list->text;
445 break;
446 case NFROMFD:
447 case NTOFD:
448 if (redir->ndup.vname) {
449 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
450 fixredir(redir, fn.list->text, 1);
451 }
452 break;
453 }
454 }
455 }
456
457
458
459 /*
460 * Evaluate a pipeline. All the processes in the pipeline are children
461 * of the process creating the pipeline. (This differs from some versions
462 * of the shell, which make the last process in a pipeline the parent
463 * of all the rest.)
464 */
465
466 STATIC void
467 evalpipe(n)
468 union node *n;
469 {
470 struct job *jp;
471 struct nodelist *lp;
472 int pipelen;
473 int prevfd;
474 int pip[2];
475
476 TRACE(("evalpipe(0x%lx) called\n", (long)n));
477 pipelen = 0;
478 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
479 pipelen++;
480 INTOFF;
481 jp = makejob(n, pipelen);
482 prevfd = -1;
483 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
484 prehash(lp->n);
485 pip[1] = -1;
486 if (lp->next) {
487 if (pipe(pip) < 0) {
488 close(prevfd);
489 error("Pipe call failed");
490 }
491 }
492 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
493 INTON;
494 if (prevfd > 0) {
495 close(0);
496 copyfd(prevfd, 0);
497 close(prevfd);
498 }
499 if (pip[1] >= 0) {
500 close(pip[0]);
501 if (pip[1] != 1) {
502 close(1);
503 copyfd(pip[1], 1);
504 close(pip[1]);
505 }
506 }
507 evaltree(lp->n, EV_EXIT);
508 }
509 if (prevfd >= 0)
510 close(prevfd);
511 prevfd = pip[0];
512 close(pip[1]);
513 }
514 INTON;
515 if (n->npipe.backgnd == 0) {
516 INTOFF;
517 exitstatus = waitforjob(jp);
518 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
519 INTON;
520 }
521 }
522
523
524
525 /*
526 * Execute a command inside back quotes. If it's a builtin command, we
527 * want to save its output in a block obtained from malloc. Otherwise
528 * we fork off a subprocess and get the output of the command via a pipe.
529 * Should be called with interrupts off.
530 */
531
532 void
533 evalbackcmd(n, result)
534 union node *n;
535 struct backcmd *result;
536 {
537 int pip[2];
538 struct job *jp;
539 struct stackmark smark; /* unnecessary */
540
541 setstackmark(&smark);
542 result->fd = -1;
543 result->buf = NULL;
544 result->nleft = 0;
545 result->jp = NULL;
546 if (n == NULL) {
547 exitstatus = 0;
548 goto out;
549 }
550 #ifdef notyet
551 /*
552 * For now we disable executing builtins in the same
553 * context as the shell, because we are not keeping
554 * enough state to recover from changes that are
555 * supposed only to affect subshells. eg. echo "`cd /`"
556 */
557 if (n->type == NCMD) {
558 exitstatus = oexitstatus;
559 evalcommand(n, EV_BACKCMD, result);
560 } else
561 #endif
562 {
563 exitstatus = 0;
564 if (pipe(pip) < 0)
565 error("Pipe call failed");
566 jp = makejob(n, 1);
567 if (forkshell(jp, n, FORK_NOJOB) == 0) {
568 FORCEINTON;
569 close(pip[0]);
570 if (pip[1] != 1) {
571 close(1);
572 copyfd(pip[1], 1);
573 close(pip[1]);
574 }
575 evaltree(n, EV_EXIT);
576 }
577 close(pip[1]);
578 result->fd = pip[0];
579 result->jp = jp;
580 }
581 out:
582 popstackmark(&smark);
583 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
584 result->fd, result->buf, result->nleft, result->jp));
585 }
586
587
588
589 /*
590 * Execute a simple command.
591 */
592
593 STATIC void
594 evalcommand(cmd, flags, backcmd)
595 union node *cmd;
596 int flags;
597 struct backcmd *backcmd;
598 {
599 struct stackmark smark;
600 union node *argp;
601 struct arglist arglist;
602 struct arglist varlist;
603 char **argv;
604 int argc;
605 char **envp;
606 int varflag;
607 struct strlist *sp;
608 int mode;
609 int pip[2];
610 struct cmdentry cmdentry;
611 struct job *jp;
612 struct jmploc jmploc;
613 struct jmploc *volatile savehandler;
614 char *volatile savecmdname;
615 volatile struct shparam saveparam;
616 struct localvar *volatile savelocalvars;
617 volatile int e;
618 char *lastarg;
619 #if __GNUC__
620 /* Avoid longjmp clobbering */
621 (void) &argv;
622 (void) &argc;
623 (void) &lastarg;
624 (void) &flags;
625 #endif
626
627 /* First expand the arguments. */
628 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
629 setstackmark(&smark);
630 arglist.lastp = &arglist.list;
631 varlist.lastp = &varlist.list;
632 varflag = 1;
633 oexitstatus = exitstatus;
634 exitstatus = 0;
635 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
636 char *p = argp->narg.text;
637 if (varflag && is_name(*p)) {
638 do {
639 p++;
640 } while (is_in_name(*p));
641 if (*p == '=') {
642 expandarg(argp, &varlist, EXP_VARTILDE);
643 continue;
644 }
645 }
646 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
647 varflag = 0;
648 }
649 *arglist.lastp = NULL;
650 *varlist.lastp = NULL;
651 expredir(cmd->ncmd.redirect);
652 argc = 0;
653 for (sp = arglist.list ; sp ; sp = sp->next)
654 argc++;
655 argv = stalloc(sizeof (char *) * (argc + 1));
656
657 for (sp = arglist.list ; sp ; sp = sp->next) {
658 TRACE(("evalcommand arg: %s\n", sp->text));
659 *argv++ = sp->text;
660 }
661 *argv = NULL;
662 lastarg = NULL;
663 if (iflag && funcnest == 0 && argc > 0)
664 lastarg = argv[-1];
665 argv -= argc;
666
667 /* Print the command if xflag is set. */
668 if (xflag) {
669 outc('+', &errout);
670 for (sp = varlist.list ; sp ; sp = sp->next) {
671 outc(' ', &errout);
672 out2str(sp->text);
673 }
674 for (sp = arglist.list ; sp ; sp = sp->next) {
675 outc(' ', &errout);
676 out2str(sp->text);
677 }
678 outc('\n', &errout);
679 flushout(&errout);
680 }
681
682 /* Now locate the command. */
683 if (argc == 0) {
684 cmdentry.cmdtype = CMDBUILTIN;
685 cmdentry.u.index = BLTINCMD;
686 } else {
687 static const char PATH[] = "PATH=";
688 const char *path = pathval();
689
690 /*
691 * Modify the command lookup path, if a PATH= assignment
692 * is present
693 */
694 for (sp = varlist.list ; sp ; sp = sp->next)
695 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
696 path = sp->text + sizeof(PATH) - 1;
697
698 find_command(argv[0], &cmdentry, DO_ERR, path);
699 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
700 exitstatus = 127;
701 flushout(&errout);
702 return;
703 }
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;
715 }
716 if (cmdentry.u.index != BLTINCMD)
717 break;
718 }
719 }
720 }
721
722 /* Fork off a child process if necessary. */
723 if (cmd->ncmd.backgnd
724 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
725 || ((flags & EV_BACKCMD) != 0
726 && (cmdentry.cmdtype != CMDBUILTIN
727 || cmdentry.u.index == DOTCMD
728 || cmdentry.u.index == EVALCMD))) {
729 jp = makejob(cmd, 1);
730 mode = cmd->ncmd.backgnd;
731 if (flags & EV_BACKCMD) {
732 mode = FORK_NOJOB;
733 if (pipe(pip) < 0)
734 error("Pipe call failed");
735 }
736 if (forkshell(jp, cmd, mode) != 0)
737 goto parent; /* at end of routine */
738 if (flags & EV_BACKCMD) {
739 FORCEINTON;
740 close(pip[0]);
741 if (pip[1] != 1) {
742 close(1);
743 copyfd(pip[1], 1);
744 close(pip[1]);
745 }
746 }
747 flags |= EV_EXIT;
748 }
749
750 /* This is the child process if a fork occurred. */
751 /* Execute the command. */
752 if (cmdentry.cmdtype == CMDFUNCTION) {
753 #ifdef DEBUG
754 trputs("Shell function: "); trargs(argv);
755 #endif
756 redirect(cmd->ncmd.redirect, REDIR_PUSH);
757 saveparam = shellparam;
758 shellparam.malloc = 0;
759 shellparam.reset = 1;
760 shellparam.nparam = argc - 1;
761 shellparam.p = argv + 1;
762 shellparam.optnext = NULL;
763 INTOFF;
764 savelocalvars = localvars;
765 localvars = NULL;
766 INTON;
767 if (setjmp(jmploc.loc)) {
768 if (exception == EXSHELLPROC) {
769 freeparam((volatile struct shparam *)
770 &saveparam);
771 } else {
772 freeparam(&shellparam);
773 shellparam = saveparam;
774 }
775 poplocalvars();
776 localvars = savelocalvars;
777 handler = savehandler;
778 longjmp(handler->loc, 1);
779 }
780 savehandler = handler;
781 handler = &jmploc;
782 for (sp = varlist.list ; sp ; sp = sp->next)
783 mklocal(sp->text);
784 funcnest++;
785 evaltree(cmdentry.u.func, 0);
786 funcnest--;
787 INTOFF;
788 poplocalvars();
789 localvars = savelocalvars;
790 freeparam(&shellparam);
791 shellparam = saveparam;
792 handler = savehandler;
793 popredir();
794 INTON;
795 if (evalskip == SKIPFUNC) {
796 evalskip = 0;
797 skipcount = 0;
798 }
799 if (flags & EV_EXIT)
800 exitshell(exitstatus);
801 } else if (cmdentry.cmdtype == CMDBUILTIN) {
802 #ifdef DEBUG
803 trputs("builtin command: "); trargs(argv);
804 #endif
805 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
806 if (flags == EV_BACKCMD) {
807 memout.nleft = 0;
808 memout.nextc = memout.buf;
809 memout.bufsize = 64;
810 mode |= REDIR_BACKQ;
811 }
812 redirect(cmd->ncmd.redirect, mode);
813 savecmdname = commandname;
814 cmdenviron = varlist.list;
815 e = -1;
816 if (setjmp(jmploc.loc)) {
817 e = exception;
818 exitstatus = (e == EXINT)? SIGINT+128 : 2;
819 goto cmddone;
820 }
821 savehandler = handler;
822 handler = &jmploc;
823 commandname = argv[0];
824 argptr = argv + 1;
825 optptr = NULL; /* initialize nextopt */
826 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
827 flushall();
828 cmddone:
829 out1 = &output;
830 out2 = &errout;
831 freestdout();
832 cmdenviron = NULL;
833 if (e != EXSHELLPROC) {
834 commandname = savecmdname;
835 if (flags & EV_EXIT)
836 exitshell(exitstatus);
837 }
838 handler = savehandler;
839 if (e != -1) {
840 if ((e != EXERROR && e != EXEXEC)
841 || cmdentry.u.index == BLTINCMD
842 || cmdentry.u.index == DOTCMD
843 || cmdentry.u.index == EVALCMD
844 #ifndef SMALL
845 || cmdentry.u.index == HISTCMD
846 #endif
847 || cmdentry.u.index == EXECCMD)
848 exraise(e);
849 FORCEINTON;
850 }
851 if (cmdentry.u.index != EXECCMD)
852 popredir();
853 if (flags == EV_BACKCMD) {
854 backcmd->buf = memout.buf;
855 backcmd->nleft = memout.nextc - memout.buf;
856 memout.buf = NULL;
857 }
858 } else {
859 #ifdef DEBUG
860 trputs("normal command: "); trargs(argv);
861 #endif
862 clearredir();
863 redirect(cmd->ncmd.redirect, 0);
864 for (sp = varlist.list ; sp ; sp = sp->next)
865 setvareq(sp->text, VEXPORT|VSTACK);
866 envp = environment();
867 shellexec(argv, envp, pathval(), cmdentry.u.index);
868 }
869 goto out;
870
871 parent: /* parent process gets here (if we forked) */
872 if (mode == 0) { /* argument to fork */
873 INTOFF;
874 exitstatus = waitforjob(jp);
875 INTON;
876 } else if (mode == 2) {
877 backcmd->fd = pip[0];
878 close(pip[1]);
879 backcmd->jp = jp;
880 }
881
882 out:
883 if (lastarg)
884 setvar("_", lastarg, 0);
885 popstackmark(&smark);
886 }
887
888
889
890 /*
891 * Search for a command. This is called before we fork so that the
892 * location of the command will be available in the parent as well as
893 * the child. The check for "goodname" is an overly conservative
894 * check that the name will not be subject to expansion.
895 */
896
897 STATIC void
898 prehash(n)
899 union node *n;
900 {
901 struct cmdentry entry;
902
903 if (n->type == NCMD && n->ncmd.args)
904 if (goodname(n->ncmd.args->narg.text))
905 find_command(n->ncmd.args->narg.text, &entry, 0,
906 pathval());
907 }
908
909
910
911 /*
912 * Builtin commands. Builtin commands whose functions are closely
913 * tied to evaluation are implemented here.
914 */
915
916 /*
917 * No command given, or a bltin command with no arguments. Set the
918 * specified variables.
919 */
920
921 int
922 bltincmd(argc, argv)
923 int argc;
924 char **argv;
925 {
926 listsetvar(cmdenviron);
927 /*
928 * Preserve exitstatus of a previous possible redirection
929 * as POSIX mandates
930 */
931 return exitstatus;
932 }
933
934
935 /*
936 * Handle break and continue commands. Break, continue, and return are
937 * all handled by setting the evalskip flag. The evaluation routines
938 * above all check this flag, and if it is set they start skipping
939 * commands rather than executing them. The variable skipcount is
940 * the number of loops to break/continue, or the number of function
941 * levels to return. (The latter is always 1.) It should probably
942 * be an error to break out of more loops than exist, but it isn't
943 * in the standard shell so we don't make it one here.
944 */
945
946 int
947 breakcmd(argc, argv)
948 int argc;
949 char **argv;
950 {
951 int n = argc > 1 ? number(argv[1]) : 1;
952
953 if (n > loopnest)
954 n = loopnest;
955 if (n > 0) {
956 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
957 skipcount = n;
958 }
959 return 0;
960 }
961
962
963 /*
964 * The return command.
965 */
966
967 int
968 returncmd(argc, argv)
969 int argc;
970 char **argv;
971 {
972 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
973
974 if (funcnest) {
975 evalskip = SKIPFUNC;
976 skipcount = 1;
977 return ret;
978 }
979 else {
980 /* Do what ksh does; skip the rest of the file */
981 evalskip = SKIPFILE;
982 skipcount = 1;
983 return ret;
984 }
985 }
986
987
988 int
989 falsecmd(argc, argv)
990 int argc;
991 char **argv;
992 {
993 return 1;
994 }
995
996
997 int
998 truecmd(argc, argv)
999 int argc;
1000 char **argv;
1001 {
1002 return 0;
1003 }
1004
1005
1006 int
1007 execcmd(argc, argv)
1008 int argc;
1009 char **argv;
1010 {
1011 if (argc > 1) {
1012 struct strlist *sp;
1013
1014 iflag = 0; /* exit on error */
1015 mflag = 0;
1016 optschanged();
1017 for (sp = cmdenviron; sp ; sp = sp->next)
1018 setvareq(sp->text, VEXPORT|VSTACK);
1019 shellexec(argv + 1, environment(), pathval(), 0);
1020 }
1021 return 0;
1022 }
1023