eval.c revision 1.37 1 /* $NetBSD: eval.c,v 1.37 1997/07/15 17:49:15 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. 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.37 1997/07/15 17:49:15 christos 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 flags |= EV_TESTED;
214 goto out;
215 }
216 evaltree(n->nbinary.ch2, flags);
217 break;
218 case NOR:
219 evaltree(n->nbinary.ch1, EV_TESTED);
220 if (evalskip || exitstatus == 0)
221 goto out;
222 evaltree(n->nbinary.ch2, flags);
223 break;
224 case NREDIR:
225 expredir(n->nredir.redirect);
226 redirect(n->nredir.redirect, REDIR_PUSH);
227 evaltree(n->nredir.n, flags);
228 popredir();
229 break;
230 case NSUBSHELL:
231 evalsubshell(n, flags);
232 break;
233 case NBACKGND:
234 evalsubshell(n, flags);
235 break;
236 case NIF: {
237 evaltree(n->nif.test, EV_TESTED);
238 if (evalskip)
239 goto out;
240 if (exitstatus == 0)
241 evaltree(n->nif.ifpart, flags);
242 else if (n->nif.elsepart)
243 evaltree(n->nif.elsepart, flags);
244 else
245 exitstatus = 0;
246 break;
247 }
248 case NWHILE:
249 case NUNTIL:
250 evalloop(n);
251 break;
252 case NFOR:
253 evalfor(n);
254 break;
255 case NCASE:
256 evalcase(n, flags);
257 break;
258 case NDEFUN:
259 defun(n->narg.text, n->narg.next);
260 exitstatus = 0;
261 break;
262 case NNOT:
263 evaltree(n->nnot.com, EV_TESTED);
264 exitstatus = !exitstatus;
265 break;
266
267 case NPIPE:
268 evalpipe(n);
269 break;
270 case NCMD:
271 evalcommand(n, flags, (struct backcmd *)NULL);
272 break;
273 default:
274 out1fmt("Node type = %d\n", n->type);
275 flushout(&output);
276 break;
277 }
278 out:
279 if (pendingsigs)
280 dotrap();
281 if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
282 exitshell(exitstatus);
283 }
284
285
286 STATIC void
287 evalloop(n)
288 union node *n;
289 {
290 int status;
291
292 loopnest++;
293 status = 0;
294 for (;;) {
295 evaltree(n->nbinary.ch1, EV_TESTED);
296 if (evalskip) {
297 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
298 evalskip = 0;
299 continue;
300 }
301 if (evalskip == SKIPBREAK && --skipcount <= 0)
302 evalskip = 0;
303 break;
304 }
305 if (n->type == NWHILE) {
306 if (exitstatus != 0)
307 break;
308 } else {
309 if (exitstatus == 0)
310 break;
311 }
312 evaltree(n->nbinary.ch2, 0);
313 status = exitstatus;
314 if (evalskip)
315 goto skipping;
316 }
317 loopnest--;
318 exitstatus = status;
319 }
320
321
322
323 STATIC void
324 evalfor(n)
325 union node *n;
326 {
327 struct arglist arglist;
328 union node *argp;
329 struct strlist *sp;
330 struct stackmark smark;
331
332 setstackmark(&smark);
333 arglist.lastp = &arglist.list;
334 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
335 oexitstatus = exitstatus;
336 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
337 if (evalskip)
338 goto out;
339 }
340 *arglist.lastp = NULL;
341
342 exitstatus = 0;
343 loopnest++;
344 for (sp = arglist.list ; sp ; sp = sp->next) {
345 setvar(n->nfor.var, sp->text, 0);
346 evaltree(n->nfor.body, 0);
347 if (evalskip) {
348 if (evalskip == SKIPCONT && --skipcount <= 0) {
349 evalskip = 0;
350 continue;
351 }
352 if (evalskip == SKIPBREAK && --skipcount <= 0)
353 evalskip = 0;
354 break;
355 }
356 }
357 loopnest--;
358 out:
359 popstackmark(&smark);
360 }
361
362
363
364 STATIC void
365 evalcase(n, flags)
366 union node *n;
367 int flags;
368 {
369 union node *cp;
370 union node *patp;
371 struct arglist arglist;
372 struct stackmark smark;
373
374 setstackmark(&smark);
375 arglist.lastp = &arglist.list;
376 oexitstatus = exitstatus;
377 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
378 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
379 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
380 if (casematch(patp, arglist.list->text)) {
381 if (evalskip == 0) {
382 evaltree(cp->nclist.body, flags);
383 }
384 goto out;
385 }
386 }
387 }
388 out:
389 popstackmark(&smark);
390 }
391
392
393
394 /*
395 * Kick off a subshell to evaluate a tree.
396 */
397
398 STATIC void
399 evalsubshell(n, flags)
400 union node *n;
401 int flags;
402 {
403 struct job *jp;
404 int backgnd = (n->type == NBACKGND);
405
406 expredir(n->nredir.redirect);
407 jp = makejob(n, 1);
408 if (forkshell(jp, n, backgnd) == 0) {
409 if (backgnd)
410 flags &=~ EV_TESTED;
411 redirect(n->nredir.redirect, 0);
412 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
413 }
414 if (! backgnd) {
415 INTOFF;
416 exitstatus = waitforjob(jp);
417 INTON;
418 }
419 }
420
421
422
423 /*
424 * Compute the names of the files in a redirection list.
425 */
426
427 STATIC void
428 expredir(n)
429 union node *n;
430 {
431 union node *redir;
432
433 for (redir = n ; redir ; redir = redir->nfile.next) {
434 struct arglist fn;
435 fn.lastp = &fn.list;
436 oexitstatus = exitstatus;
437 switch (redir->type) {
438 case NFROM:
439 case NTO:
440 case NAPPEND:
441 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
442 redir->nfile.expfname = fn.list->text;
443 break;
444 case NFROMFD:
445 case NTOFD:
446 if (redir->ndup.vname) {
447 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
448 fixredir(redir, fn.list->text, 1);
449 }
450 break;
451 }
452 }
453 }
454
455
456
457 /*
458 * Evaluate a pipeline. All the processes in the pipeline are children
459 * of the process creating the pipeline. (This differs from some versions
460 * of the shell, which make the last process in a pipeline the parent
461 * of all the rest.)
462 */
463
464 STATIC void
465 evalpipe(n)
466 union node *n;
467 {
468 struct job *jp;
469 struct nodelist *lp;
470 int pipelen;
471 int prevfd;
472 int pip[2];
473
474 TRACE(("evalpipe(0x%lx) called\n", (long)n));
475 pipelen = 0;
476 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
477 pipelen++;
478 INTOFF;
479 jp = makejob(n, pipelen);
480 prevfd = -1;
481 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
482 prehash(lp->n);
483 pip[1] = -1;
484 if (lp->next) {
485 if (pipe(pip) < 0) {
486 close(prevfd);
487 error("Pipe call failed");
488 }
489 }
490 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
491 INTON;
492 if (prevfd > 0) {
493 close(0);
494 copyfd(prevfd, 0);
495 close(prevfd);
496 }
497 if (pip[1] >= 0) {
498 close(pip[0]);
499 if (pip[1] != 1) {
500 close(1);
501 copyfd(pip[1], 1);
502 close(pip[1]);
503 }
504 }
505 evaltree(lp->n, EV_EXIT);
506 }
507 if (prevfd >= 0)
508 close(prevfd);
509 prevfd = pip[0];
510 close(pip[1]);
511 }
512 INTON;
513 if (n->npipe.backgnd == 0) {
514 INTOFF;
515 exitstatus = waitforjob(jp);
516 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
517 INTON;
518 }
519 }
520
521
522
523 /*
524 * Execute a command inside back quotes. If it's a builtin command, we
525 * want to save its output in a block obtained from malloc. Otherwise
526 * we fork off a subprocess and get the output of the command via a pipe.
527 * Should be called with interrupts off.
528 */
529
530 void
531 evalbackcmd(n, result)
532 union node *n;
533 struct backcmd *result;
534 {
535 int pip[2];
536 struct job *jp;
537 struct stackmark smark; /* unnecessary */
538
539 setstackmark(&smark);
540 result->fd = -1;
541 result->buf = NULL;
542 result->nleft = 0;
543 result->jp = NULL;
544 if (n == NULL) {
545 exitstatus = 0;
546 goto out;
547 }
548 if (n->type == NCMD) {
549 exitstatus = oexitstatus;
550 evalcommand(n, EV_BACKCMD, result);
551 } else {
552 exitstatus = 0;
553 if (pipe(pip) < 0)
554 error("Pipe call failed");
555 jp = makejob(n, 1);
556 if (forkshell(jp, n, FORK_NOJOB) == 0) {
557 FORCEINTON;
558 close(pip[0]);
559 if (pip[1] != 1) {
560 close(1);
561 copyfd(pip[1], 1);
562 close(pip[1]);
563 }
564 evaltree(n, EV_EXIT);
565 }
566 close(pip[1]);
567 result->fd = pip[0];
568 result->jp = jp;
569 }
570 out:
571 popstackmark(&smark);
572 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
573 result->fd, result->buf, result->nleft, result->jp));
574 }
575
576
577
578 /*
579 * Execute a simple command.
580 */
581
582 STATIC void
583 evalcommand(cmd, flags, backcmd)
584 union node *cmd;
585 int flags;
586 struct backcmd *backcmd;
587 {
588 struct stackmark smark;
589 union node *argp;
590 struct arglist arglist;
591 struct arglist varlist;
592 char **argv;
593 int argc;
594 char **envp;
595 int varflag;
596 struct strlist *sp;
597 int mode;
598 int pip[2];
599 struct cmdentry cmdentry;
600 struct job *jp;
601 struct jmploc jmploc;
602 struct jmploc *volatile savehandler;
603 char *volatile savecmdname;
604 volatile struct shparam saveparam;
605 struct localvar *volatile savelocalvars;
606 volatile int e;
607 char *lastarg;
608 #if __GNUC__
609 /* Avoid longjmp clobbering */
610 (void) &argv;
611 (void) &argc;
612 (void) &lastarg;
613 (void) &flags;
614 #endif
615
616 /* First expand the arguments. */
617 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
618 setstackmark(&smark);
619 arglist.lastp = &arglist.list;
620 varlist.lastp = &varlist.list;
621 cmdenviron = NULL;
622 varflag = 1;
623 oexitstatus = exitstatus;
624 exitstatus = 0;
625 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
626 char *p = argp->narg.text;
627 if (varflag && is_name(*p)) {
628 do {
629 p++;
630 } while (is_in_name(*p));
631 if (*p == '=') {
632 expandarg(argp, &varlist, EXP_VARTILDE);
633 continue;
634 }
635 }
636 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
637 varflag = 0;
638 }
639 *arglist.lastp = NULL;
640 *varlist.lastp = NULL;
641 expredir(cmd->ncmd.redirect);
642 argc = 0;
643 for (sp = arglist.list ; sp ; sp = sp->next)
644 argc++;
645 argv = stalloc(sizeof (char *) * (argc + 1));
646
647 for (sp = arglist.list ; sp ; sp = sp->next) {
648 TRACE(("evalcommand arg: %s\n", sp->text));
649 *argv++ = sp->text;
650 }
651 *argv = NULL;
652 lastarg = NULL;
653 if (iflag && funcnest == 0 && argc > 0)
654 lastarg = argv[-1];
655 argv -= argc;
656
657 /* Print the command if xflag is set. */
658 if (xflag) {
659 outc('+', &errout);
660 for (sp = varlist.list ; sp ; sp = sp->next) {
661 outc(' ', &errout);
662 out2str(sp->text);
663 }
664 for (sp = arglist.list ; sp ; sp = sp->next) {
665 outc(' ', &errout);
666 out2str(sp->text);
667 }
668 outc('\n', &errout);
669 flushout(&errout);
670 }
671
672 /* Now locate the command. */
673 if (argc == 0) {
674 cmdentry.cmdtype = CMDBUILTIN;
675 cmdentry.u.index = BLTINCMD;
676 } else {
677 static const char PATH[] = "PATH=";
678 char *path = pathval();
679
680 /*
681 * Modify the command lookup path, if a PATH= assignment
682 * is present
683 */
684 for (sp = varlist.list ; sp ; sp = sp->next)
685 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
686 path = sp->text + sizeof(PATH) - 1;
687
688 find_command(argv[0], &cmdentry, 1, path);
689 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
690 exitstatus = 127;
691 flushout(&errout);
692 return;
693 }
694 /* implement the bltin builtin here */
695 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
696 for (;;) {
697 argv++;
698 if (--argc == 0)
699 break;
700 if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
701 outfmt(&errout, "%s: not found\n", *argv);
702 exitstatus = 127;
703 flushout(&errout);
704 return;
705 }
706 if (cmdentry.u.index != BLTINCMD)
707 break;
708 }
709 }
710 }
711
712 /* Fork off a child process if necessary. */
713 if (cmd->ncmd.backgnd
714 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
715 || ((flags & EV_BACKCMD) != 0
716 && (cmdentry.cmdtype != CMDBUILTIN
717 || cmdentry.u.index == DOTCMD
718 || cmdentry.u.index == EVALCMD))) {
719 jp = makejob(cmd, 1);
720 mode = cmd->ncmd.backgnd;
721 if (flags & EV_BACKCMD) {
722 mode = FORK_NOJOB;
723 if (pipe(pip) < 0)
724 error("Pipe call failed");
725 }
726 if (forkshell(jp, cmd, mode) != 0)
727 goto parent; /* at end of routine */
728 if (flags & EV_BACKCMD) {
729 FORCEINTON;
730 close(pip[0]);
731 if (pip[1] != 1) {
732 close(1);
733 copyfd(pip[1], 1);
734 close(pip[1]);
735 }
736 }
737 flags |= EV_EXIT;
738 }
739
740 /* This is the child process if a fork occurred. */
741 /* Execute the command. */
742 if (cmdentry.cmdtype == CMDFUNCTION) {
743 #ifdef DEBUG
744 trputs("Shell function: "); trargs(argv);
745 #endif
746 redirect(cmd->ncmd.redirect, REDIR_PUSH);
747 saveparam = shellparam;
748 shellparam.malloc = 0;
749 shellparam.reset = 1;
750 shellparam.nparam = argc - 1;
751 shellparam.p = argv + 1;
752 shellparam.optnext = NULL;
753 INTOFF;
754 savelocalvars = localvars;
755 localvars = NULL;
756 INTON;
757 if (setjmp(jmploc.loc)) {
758 if (exception == EXSHELLPROC)
759 freeparam((struct shparam *)&saveparam);
760 else {
761 freeparam(&shellparam);
762 shellparam = saveparam;
763 }
764 poplocalvars();
765 localvars = savelocalvars;
766 handler = savehandler;
767 longjmp(handler->loc, 1);
768 }
769 savehandler = handler;
770 handler = &jmploc;
771 for (sp = varlist.list ; sp ; sp = sp->next)
772 mklocal(sp->text);
773 funcnest++;
774 evaltree(cmdentry.u.func, 0);
775 funcnest--;
776 INTOFF;
777 poplocalvars();
778 localvars = savelocalvars;
779 freeparam(&shellparam);
780 shellparam = saveparam;
781 handler = savehandler;
782 popredir();
783 INTON;
784 if (evalskip == SKIPFUNC) {
785 evalskip = 0;
786 skipcount = 0;
787 }
788 if (flags & EV_EXIT)
789 exitshell(exitstatus);
790 } else if (cmdentry.cmdtype == CMDBUILTIN) {
791 #ifdef DEBUG
792 trputs("builtin command: "); trargs(argv);
793 #endif
794 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
795 if (flags == EV_BACKCMD) {
796 memout.nleft = 0;
797 memout.nextc = memout.buf;
798 memout.bufsize = 64;
799 mode |= REDIR_BACKQ;
800 }
801 redirect(cmd->ncmd.redirect, mode);
802 savecmdname = commandname;
803 cmdenviron = varlist.list;
804 e = -1;
805 if (setjmp(jmploc.loc)) {
806 e = exception;
807 exitstatus = (e == EXINT)? SIGINT+128 : 2;
808 goto cmddone;
809 }
810 savehandler = handler;
811 handler = &jmploc;
812 commandname = argv[0];
813 argptr = argv + 1;
814 optptr = NULL; /* initialize nextopt */
815 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
816 flushall();
817 cmddone:
818 out1 = &output;
819 out2 = &errout;
820 freestdout();
821 if (e != EXSHELLPROC) {
822 commandname = savecmdname;
823 if (flags & EV_EXIT) {
824 exitshell(exitstatus);
825 }
826 }
827 handler = savehandler;
828 if (e != -1) {
829 if ((e != EXERROR && e != EXEXEC)
830 || cmdentry.u.index == BLTINCMD
831 || cmdentry.u.index == DOTCMD
832 || cmdentry.u.index == EVALCMD
833 #ifndef SMALL
834 || cmdentry.u.index == HISTCMD
835 #endif
836 || cmdentry.u.index == EXECCMD)
837 exraise(e);
838 FORCEINTON;
839 }
840 if (cmdentry.u.index != EXECCMD)
841 popredir();
842 if (flags == EV_BACKCMD) {
843 backcmd->buf = memout.buf;
844 backcmd->nleft = memout.nextc - memout.buf;
845 memout.buf = NULL;
846 }
847 } else {
848 #ifdef DEBUG
849 trputs("normal command: "); trargs(argv);
850 #endif
851 clearredir();
852 redirect(cmd->ncmd.redirect, 0);
853 for (sp = varlist.list ; sp ; sp = sp->next)
854 setvareq(sp->text, VEXPORT|VSTACK);
855 envp = environment();
856 shellexec(argv, envp, pathval(), cmdentry.u.index);
857 /*NOTREACHED*/
858 }
859 goto out;
860
861 parent: /* parent process gets here (if we forked) */
862 if (mode == 0) { /* argument to fork */
863 INTOFF;
864 exitstatus = waitforjob(jp);
865 INTON;
866 } else if (mode == 2) {
867 backcmd->fd = pip[0];
868 close(pip[1]);
869 backcmd->jp = jp;
870 }
871
872 out:
873 if (lastarg)
874 setvar("_", lastarg, 0);
875 popstackmark(&smark);
876 }
877
878
879
880 /*
881 * Search for a command. This is called before we fork so that the
882 * location of the command will be available in the parent as well as
883 * the child. The check for "goodname" is an overly conservative
884 * check that the name will not be subject to expansion.
885 */
886
887 STATIC void
888 prehash(n)
889 union node *n;
890 {
891 struct cmdentry entry;
892
893 if (n->type == NCMD && n->ncmd.args)
894 if (goodname(n->ncmd.args->narg.text))
895 find_command(n->ncmd.args->narg.text, &entry, 0,
896 pathval());
897 }
898
899
900
901 /*
902 * Builtin commands. Builtin commands whose functions are closely
903 * tied to evaluation are implemented here.
904 */
905
906 /*
907 * No command given, or a bltin command with no arguments. Set the
908 * specified variables.
909 */
910
911 int
912 bltincmd(argc, argv)
913 int argc;
914 char **argv;
915 {
916 listsetvar(cmdenviron);
917 /*
918 * Preserve exitstatus of a previous possible redirection
919 * as POSIX mandates
920 */
921 return exitstatus;
922 }
923
924
925 /*
926 * Handle break and continue commands. Break, continue, and return are
927 * all handled by setting the evalskip flag. The evaluation routines
928 * above all check this flag, and if it is set they start skipping
929 * commands rather than executing them. The variable skipcount is
930 * the number of loops to break/continue, or the number of function
931 * levels to return. (The latter is always 1.) It should probably
932 * be an error to break out of more loops than exist, but it isn't
933 * in the standard shell so we don't make it one here.
934 */
935
936 int
937 breakcmd(argc, argv)
938 int argc;
939 char **argv;
940 {
941 int n = argc > 1 ? number(argv[1]) : 1;
942
943 if (n > loopnest)
944 n = loopnest;
945 if (n > 0) {
946 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
947 skipcount = n;
948 }
949 return 0;
950 }
951
952
953 /*
954 * The return command.
955 */
956
957 int
958 returncmd(argc, argv)
959 int argc;
960 char **argv;
961 {
962 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
963
964 if (funcnest) {
965 evalskip = SKIPFUNC;
966 skipcount = 1;
967 return ret;
968 }
969 else {
970 /* Do what ksh does; skip the rest of the file */
971 evalskip = SKIPFILE;
972 skipcount = 1;
973 return ret;
974 }
975 }
976
977
978 int
979 falsecmd(argc, argv)
980 int argc;
981 char **argv;
982 {
983 return 1;
984 }
985
986
987 int
988 truecmd(argc, argv)
989 int argc;
990 char **argv;
991 {
992 return 0;
993 }
994
995
996 int
997 execcmd(argc, argv)
998 int argc;
999 char **argv;
1000 {
1001 if (argc > 1) {
1002 struct strlist *sp;
1003
1004 iflag = 0; /* exit on error */
1005 mflag = 0;
1006 optschanged();
1007 for (sp = cmdenviron; sp ; sp = sp->next)
1008 setvareq(sp->text, VEXPORT|VSTACK);
1009 shellexec(argv + 1, environment(), pathval(), 0);
1010
1011 }
1012 return 0;
1013 }
1014