eval.c revision 1.36 1 /* $NetBSD: eval.c,v 1.36 1997/07/04 21:01:56 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.36 1997/07/04 21:01:56 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 varflag = 1;
622 oexitstatus = exitstatus;
623 exitstatus = 0;
624 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
625 char *p = argp->narg.text;
626 if (varflag && is_name(*p)) {
627 do {
628 p++;
629 } while (is_in_name(*p));
630 if (*p == '=') {
631 expandarg(argp, &varlist, EXP_VARTILDE);
632 continue;
633 }
634 }
635 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
636 varflag = 0;
637 }
638 *arglist.lastp = NULL;
639 *varlist.lastp = NULL;
640 expredir(cmd->ncmd.redirect);
641 argc = 0;
642 for (sp = arglist.list ; sp ; sp = sp->next)
643 argc++;
644 argv = stalloc(sizeof (char *) * (argc + 1));
645
646 for (sp = arglist.list ; sp ; sp = sp->next) {
647 TRACE(("evalcommand arg: %s\n", sp->text));
648 *argv++ = sp->text;
649 }
650 *argv = NULL;
651 lastarg = NULL;
652 if (iflag && funcnest == 0 && argc > 0)
653 lastarg = argv[-1];
654 argv -= argc;
655
656 /* Print the command if xflag is set. */
657 if (xflag) {
658 outc('+', &errout);
659 for (sp = varlist.list ; sp ; sp = sp->next) {
660 outc(' ', &errout);
661 out2str(sp->text);
662 }
663 for (sp = arglist.list ; sp ; sp = sp->next) {
664 outc(' ', &errout);
665 out2str(sp->text);
666 }
667 outc('\n', &errout);
668 flushout(&errout);
669 }
670
671 /* Now locate the command. */
672 if (argc == 0) {
673 cmdentry.cmdtype = CMDBUILTIN;
674 cmdentry.u.index = BLTINCMD;
675 } else {
676 static const char PATH[] = "PATH=";
677 char *path = pathval();
678
679 /*
680 * Modify the command lookup path, if a PATH= assignment
681 * is present
682 */
683 for (sp = varlist.list ; sp ; sp = sp->next)
684 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
685 path = sp->text + sizeof(PATH) - 1;
686
687 find_command(argv[0], &cmdentry, 1, path);
688 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
689 exitstatus = 127;
690 flushout(&errout);
691 return;
692 }
693 /* implement the bltin builtin here */
694 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
695 for (;;) {
696 argv++;
697 if (--argc == 0)
698 break;
699 if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
700 outfmt(&errout, "%s: not found\n", *argv);
701 exitstatus = 127;
702 flushout(&errout);
703 return;
704 }
705 if (cmdentry.u.index != BLTINCMD)
706 break;
707 }
708 }
709 }
710
711 /* Fork off a child process if necessary. */
712 if (cmd->ncmd.backgnd
713 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
714 || ((flags & EV_BACKCMD) != 0
715 && (cmdentry.cmdtype != CMDBUILTIN
716 || cmdentry.u.index == DOTCMD
717 || cmdentry.u.index == EVALCMD))) {
718 jp = makejob(cmd, 1);
719 mode = cmd->ncmd.backgnd;
720 if (flags & EV_BACKCMD) {
721 mode = FORK_NOJOB;
722 if (pipe(pip) < 0)
723 error("Pipe call failed");
724 }
725 if (forkshell(jp, cmd, mode) != 0)
726 goto parent; /* at end of routine */
727 if (flags & EV_BACKCMD) {
728 FORCEINTON;
729 close(pip[0]);
730 if (pip[1] != 1) {
731 close(1);
732 copyfd(pip[1], 1);
733 close(pip[1]);
734 }
735 }
736 flags |= EV_EXIT;
737 }
738
739 /* This is the child process if a fork occurred. */
740 /* Execute the command. */
741 if (cmdentry.cmdtype == CMDFUNCTION) {
742 #ifdef DEBUG
743 trputs("Shell function: "); trargs(argv);
744 #endif
745 redirect(cmd->ncmd.redirect, REDIR_PUSH);
746 saveparam = shellparam;
747 shellparam.malloc = 0;
748 shellparam.reset = 1;
749 shellparam.nparam = argc - 1;
750 shellparam.p = argv + 1;
751 shellparam.optnext = NULL;
752 INTOFF;
753 savelocalvars = localvars;
754 localvars = NULL;
755 INTON;
756 if (setjmp(jmploc.loc)) {
757 if (exception == EXSHELLPROC)
758 freeparam((struct shparam *)&saveparam);
759 else {
760 freeparam(&shellparam);
761 shellparam = saveparam;
762 }
763 poplocalvars();
764 localvars = savelocalvars;
765 handler = savehandler;
766 longjmp(handler->loc, 1);
767 }
768 savehandler = handler;
769 handler = &jmploc;
770 for (sp = varlist.list ; sp ; sp = sp->next)
771 mklocal(sp->text);
772 funcnest++;
773 evaltree(cmdentry.u.func, 0);
774 funcnest--;
775 INTOFF;
776 poplocalvars();
777 localvars = savelocalvars;
778 freeparam(&shellparam);
779 shellparam = saveparam;
780 handler = savehandler;
781 popredir();
782 INTON;
783 if (evalskip == SKIPFUNC) {
784 evalskip = 0;
785 skipcount = 0;
786 }
787 if (flags & EV_EXIT)
788 exitshell(exitstatus);
789 } else if (cmdentry.cmdtype == CMDBUILTIN) {
790 #ifdef DEBUG
791 trputs("builtin command: "); trargs(argv);
792 #endif
793 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
794 if (flags == EV_BACKCMD) {
795 memout.nleft = 0;
796 memout.nextc = memout.buf;
797 memout.bufsize = 64;
798 mode |= REDIR_BACKQ;
799 }
800 redirect(cmd->ncmd.redirect, mode);
801 savecmdname = commandname;
802 cmdenviron = varlist.list;
803 e = -1;
804 if (setjmp(jmploc.loc)) {
805 e = exception;
806 exitstatus = (e == EXINT)? SIGINT+128 : 2;
807 goto cmddone;
808 }
809 savehandler = handler;
810 handler = &jmploc;
811 commandname = argv[0];
812 argptr = argv + 1;
813 optptr = NULL; /* initialize nextopt */
814 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
815 flushall();
816 cmddone:
817 out1 = &output;
818 out2 = &errout;
819 freestdout();
820 if (e != EXSHELLPROC) {
821 commandname = savecmdname;
822 if (flags & EV_EXIT) {
823 exitshell(exitstatus);
824 }
825 }
826 handler = savehandler;
827 if (e != -1) {
828 if ((e != EXERROR && e != EXEXEC)
829 || cmdentry.u.index == BLTINCMD
830 || cmdentry.u.index == DOTCMD
831 || cmdentry.u.index == EVALCMD
832 #ifndef SMALL
833 || cmdentry.u.index == HISTCMD
834 #endif
835 || cmdentry.u.index == EXECCMD)
836 exraise(e);
837 FORCEINTON;
838 }
839 if (cmdentry.u.index != EXECCMD)
840 popredir();
841 if (flags == EV_BACKCMD) {
842 backcmd->buf = memout.buf;
843 backcmd->nleft = memout.nextc - memout.buf;
844 memout.buf = NULL;
845 }
846 } else {
847 #ifdef DEBUG
848 trputs("normal command: "); trargs(argv);
849 #endif
850 clearredir();
851 redirect(cmd->ncmd.redirect, 0);
852 for (sp = varlist.list ; sp ; sp = sp->next)
853 setvareq(sp->text, VEXPORT|VSTACK);
854 envp = environment();
855 shellexec(argv, envp, pathval(), cmdentry.u.index);
856 /*NOTREACHED*/
857 }
858 goto out;
859
860 parent: /* parent process gets here (if we forked) */
861 if (mode == 0) { /* argument to fork */
862 INTOFF;
863 exitstatus = waitforjob(jp);
864 INTON;
865 } else if (mode == 2) {
866 backcmd->fd = pip[0];
867 close(pip[1]);
868 backcmd->jp = jp;
869 }
870
871 out:
872 if (lastarg)
873 setvar("_", lastarg, 0);
874 popstackmark(&smark);
875 }
876
877
878
879 /*
880 * Search for a command. This is called before we fork so that the
881 * location of the command will be available in the parent as well as
882 * the child. The check for "goodname" is an overly conservative
883 * check that the name will not be subject to expansion.
884 */
885
886 STATIC void
887 prehash(n)
888 union node *n;
889 {
890 struct cmdentry entry;
891
892 if (n->type == NCMD && n->ncmd.args)
893 if (goodname(n->ncmd.args->narg.text))
894 find_command(n->ncmd.args->narg.text, &entry, 0,
895 pathval());
896 }
897
898
899
900 /*
901 * Builtin commands. Builtin commands whose functions are closely
902 * tied to evaluation are implemented here.
903 */
904
905 /*
906 * No command given, or a bltin command with no arguments. Set the
907 * specified variables.
908 */
909
910 int
911 bltincmd(argc, argv)
912 int argc;
913 char **argv;
914 {
915 listsetvar(cmdenviron);
916 /*
917 * Preserve exitstatus of a previous possible redirection
918 * as POSIX mandates
919 */
920 return exitstatus;
921 }
922
923
924 /*
925 * Handle break and continue commands. Break, continue, and return are
926 * all handled by setting the evalskip flag. The evaluation routines
927 * above all check this flag, and if it is set they start skipping
928 * commands rather than executing them. The variable skipcount is
929 * the number of loops to break/continue, or the number of function
930 * levels to return. (The latter is always 1.) It should probably
931 * be an error to break out of more loops than exist, but it isn't
932 * in the standard shell so we don't make it one here.
933 */
934
935 int
936 breakcmd(argc, argv)
937 int argc;
938 char **argv;
939 {
940 int n = argc > 1 ? number(argv[1]) : 1;
941
942 if (n > loopnest)
943 n = loopnest;
944 if (n > 0) {
945 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
946 skipcount = n;
947 }
948 return 0;
949 }
950
951
952 /*
953 * The return command.
954 */
955
956 int
957 returncmd(argc, argv)
958 int argc;
959 char **argv;
960 {
961 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
962
963 if (funcnest) {
964 evalskip = SKIPFUNC;
965 skipcount = 1;
966 return ret;
967 }
968 else {
969 /* Do what ksh does; skip the rest of the file */
970 evalskip = SKIPFILE;
971 skipcount = 1;
972 return ret;
973 }
974 }
975
976
977 int
978 falsecmd(argc, argv)
979 int argc;
980 char **argv;
981 {
982 return 1;
983 }
984
985
986 int
987 truecmd(argc, argv)
988 int argc;
989 char **argv;
990 {
991 return 0;
992 }
993
994
995 int
996 execcmd(argc, argv)
997 int argc;
998 char **argv;
999 {
1000 if (argc > 1) {
1001 struct strlist *sp;
1002
1003 iflag = 0; /* exit on error */
1004 mflag = 0;
1005 optschanged();
1006 for (sp = cmdenviron; sp ; sp = sp->next)
1007 setvareq(sp->text, VEXPORT|VSTACK);
1008 shellexec(argv + 1, environment(), pathval(), 0);
1009
1010 }
1011 return 0;
1012 }
1013