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