eval.c revision 1.47 1 /* $NetBSD: eval.c,v 1.47 1999/07/09 03:05:49 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.47 1999/07/09 03:05:49 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 | EXP_RECORD);
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 NFROMTO:
439 case NFROM:
440 case NTO:
441 case NAPPEND:
442 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
443 redir->nfile.expfname = fn.list->text;
444 break;
445 case NFROMFD:
446 case NTOFD:
447 if (redir->ndup.vname) {
448 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
449 fixredir(redir, fn.list->text, 1);
450 }
451 break;
452 }
453 }
454 }
455
456
457
458 /*
459 * Evaluate a pipeline. All the processes in the pipeline are children
460 * of the process creating the pipeline. (This differs from some versions
461 * of the shell, which make the last process in a pipeline the parent
462 * of all the rest.)
463 */
464
465 STATIC void
466 evalpipe(n)
467 union node *n;
468 {
469 struct job *jp;
470 struct nodelist *lp;
471 int pipelen;
472 int prevfd;
473 int pip[2];
474
475 TRACE(("evalpipe(0x%lx) called\n", (long)n));
476 pipelen = 0;
477 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
478 pipelen++;
479 INTOFF;
480 jp = makejob(n, pipelen);
481 prevfd = -1;
482 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
483 prehash(lp->n);
484 pip[1] = -1;
485 if (lp->next) {
486 if (pipe(pip) < 0) {
487 close(prevfd);
488 error("Pipe call failed");
489 }
490 }
491 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
492 INTON;
493 if (prevfd > 0) {
494 close(0);
495 copyfd(prevfd, 0);
496 close(prevfd);
497 }
498 if (pip[1] >= 0) {
499 close(pip[0]);
500 if (pip[1] != 1) {
501 close(1);
502 copyfd(pip[1], 1);
503 close(pip[1]);
504 }
505 }
506 evaltree(lp->n, EV_EXIT);
507 }
508 if (prevfd >= 0)
509 close(prevfd);
510 prevfd = pip[0];
511 close(pip[1]);
512 }
513 INTON;
514 if (n->npipe.backgnd == 0) {
515 INTOFF;
516 exitstatus = waitforjob(jp);
517 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
518 INTON;
519 }
520 }
521
522
523
524 /*
525 * Execute a command inside back quotes. If it's a builtin command, we
526 * want to save its output in a block obtained from malloc. Otherwise
527 * we fork off a subprocess and get the output of the command via a pipe.
528 * Should be called with interrupts off.
529 */
530
531 void
532 evalbackcmd(n, result)
533 union node *n;
534 struct backcmd *result;
535 {
536 int pip[2];
537 struct job *jp;
538 struct stackmark smark; /* unnecessary */
539
540 setstackmark(&smark);
541 result->fd = -1;
542 result->buf = NULL;
543 result->nleft = 0;
544 result->jp = NULL;
545 if (n == NULL) {
546 exitstatus = 0;
547 goto out;
548 }
549 #ifdef notyet
550 /*
551 * For now we disable executing builtins in the same
552 * context as the shell, because we are not keeping
553 * enough state to recover from changes that are
554 * supposed only to affect subshells. eg. echo "`cd /`"
555 */
556 if (n->type == NCMD) {
557 exitstatus = oexitstatus;
558 evalcommand(n, EV_BACKCMD, result);
559 } else
560 #endif
561 {
562 exitstatus = 0;
563 if (pipe(pip) < 0)
564 error("Pipe call failed");
565 jp = makejob(n, 1);
566 if (forkshell(jp, n, FORK_NOJOB) == 0) {
567 FORCEINTON;
568 close(pip[0]);
569 if (pip[1] != 1) {
570 close(1);
571 copyfd(pip[1], 1);
572 close(pip[1]);
573 }
574 evaltree(n, EV_EXIT);
575 }
576 close(pip[1]);
577 result->fd = pip[0];
578 result->jp = jp;
579 }
580 out:
581 popstackmark(&smark);
582 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
583 result->fd, result->buf, result->nleft, result->jp));
584 }
585
586
587
588 /*
589 * Execute a simple command.
590 */
591
592 STATIC void
593 evalcommand(cmd, flags, backcmd)
594 union node *cmd;
595 int flags;
596 struct backcmd *backcmd;
597 {
598 struct stackmark smark;
599 union node *argp;
600 struct arglist arglist;
601 struct arglist varlist;
602 char **argv;
603 int argc;
604 char **envp;
605 int varflag;
606 struct strlist *sp;
607 int mode;
608 int pip[2];
609 struct cmdentry cmdentry;
610 struct job *jp;
611 struct jmploc jmploc;
612 struct jmploc *volatile savehandler;
613 char *volatile savecmdname;
614 volatile struct shparam saveparam;
615 struct localvar *volatile savelocalvars;
616 volatile int e;
617 char *lastarg;
618 #if __GNUC__
619 /* Avoid longjmp clobbering */
620 (void) &argv;
621 (void) &argc;
622 (void) &lastarg;
623 (void) &flags;
624 #endif
625
626 /* First expand the arguments. */
627 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
628 setstackmark(&smark);
629 arglist.lastp = &arglist.list;
630 varlist.lastp = &varlist.list;
631 varflag = 1;
632 oexitstatus = exitstatus;
633 exitstatus = 0;
634 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
635 char *p = argp->narg.text;
636 if (varflag && is_name(*p)) {
637 do {
638 p++;
639 } while (is_in_name(*p));
640 if (*p == '=') {
641 expandarg(argp, &varlist, EXP_VARTILDE);
642 continue;
643 }
644 }
645 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
646 varflag = 0;
647 }
648 *arglist.lastp = NULL;
649 *varlist.lastp = NULL;
650 expredir(cmd->ncmd.redirect);
651 argc = 0;
652 for (sp = arglist.list ; sp ; sp = sp->next)
653 argc++;
654 argv = stalloc(sizeof (char *) * (argc + 1));
655
656 for (sp = arglist.list ; sp ; sp = sp->next) {
657 TRACE(("evalcommand arg: %s\n", sp->text));
658 *argv++ = sp->text;
659 }
660 *argv = NULL;
661 lastarg = NULL;
662 if (iflag && funcnest == 0 && argc > 0)
663 lastarg = argv[-1];
664 argv -= argc;
665
666 /* Print the command if xflag is set. */
667 if (xflag) {
668 outc('+', &errout);
669 for (sp = varlist.list ; sp ; sp = sp->next) {
670 outc(' ', &errout);
671 out2str(sp->text);
672 }
673 for (sp = arglist.list ; sp ; sp = sp->next) {
674 outc(' ', &errout);
675 out2str(sp->text);
676 }
677 outc('\n', &errout);
678 flushout(&errout);
679 }
680
681 /* Now locate the command. */
682 if (argc == 0) {
683 cmdentry.cmdtype = CMDBUILTIN;
684 cmdentry.u.index = BLTINCMD;
685 } else {
686 static const char PATH[] = "PATH=";
687 const char *path = pathval();
688
689 /*
690 * Modify the command lookup path, if a PATH= assignment
691 * is present
692 */
693 for (sp = varlist.list ; sp ; sp = sp->next)
694 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
695 path = sp->text + sizeof(PATH) - 1;
696
697 find_command(argv[0], &cmdentry, DO_ERR, path);
698 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
699 exitstatus = 127;
700 flushout(&errout);
701 return;
702 }
703 /* implement the bltin builtin here */
704 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
705 for (;;) {
706 argv++;
707 if (--argc == 0)
708 break;
709 if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
710 outfmt(&errout, "%s: not found\n", *argv);
711 exitstatus = 127;
712 flushout(&errout);
713 return;
714 }
715 if (cmdentry.u.index != BLTINCMD)
716 break;
717 }
718 }
719 }
720
721 /* Fork off a child process if necessary. */
722 if (cmd->ncmd.backgnd
723 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
724 || ((flags & EV_BACKCMD) != 0
725 && (cmdentry.cmdtype != CMDBUILTIN
726 || cmdentry.u.index == DOTCMD
727 || cmdentry.u.index == EVALCMD))) {
728 jp = makejob(cmd, 1);
729 mode = cmd->ncmd.backgnd;
730 if (flags & EV_BACKCMD) {
731 mode = FORK_NOJOB;
732 if (pipe(pip) < 0)
733 error("Pipe call failed");
734 }
735 if (forkshell(jp, cmd, mode) != 0)
736 goto parent; /* at end of routine */
737 if (flags & EV_BACKCMD) {
738 FORCEINTON;
739 close(pip[0]);
740 if (pip[1] != 1) {
741 close(1);
742 copyfd(pip[1], 1);
743 close(pip[1]);
744 }
745 }
746 flags |= EV_EXIT;
747 }
748
749 /* This is the child process if a fork occurred. */
750 /* Execute the command. */
751 if (cmdentry.cmdtype == CMDFUNCTION) {
752 #ifdef DEBUG
753 trputs("Shell function: "); trargs(argv);
754 #endif
755 redirect(cmd->ncmd.redirect, REDIR_PUSH);
756 saveparam = shellparam;
757 shellparam.malloc = 0;
758 shellparam.reset = 1;
759 shellparam.nparam = argc - 1;
760 shellparam.p = argv + 1;
761 shellparam.optnext = NULL;
762 INTOFF;
763 savelocalvars = localvars;
764 localvars = NULL;
765 INTON;
766 if (setjmp(jmploc.loc)) {
767 if (exception == EXSHELLPROC) {
768 freeparam((volatile struct shparam *)
769 &saveparam);
770 } else {
771 freeparam(&shellparam);
772 shellparam = saveparam;
773 }
774 poplocalvars();
775 localvars = savelocalvars;
776 handler = savehandler;
777 longjmp(handler->loc, 1);
778 }
779 savehandler = handler;
780 handler = &jmploc;
781 for (sp = varlist.list ; sp ; sp = sp->next)
782 mklocal(sp->text);
783 funcnest++;
784 evaltree(cmdentry.u.func, 0);
785 funcnest--;
786 INTOFF;
787 poplocalvars();
788 localvars = savelocalvars;
789 freeparam(&shellparam);
790 shellparam = saveparam;
791 handler = savehandler;
792 popredir();
793 INTON;
794 if (evalskip == SKIPFUNC) {
795 evalskip = 0;
796 skipcount = 0;
797 }
798 if (flags & EV_EXIT)
799 exitshell(exitstatus);
800 } else if (cmdentry.cmdtype == CMDBUILTIN) {
801 #ifdef DEBUG
802 trputs("builtin command: "); trargs(argv);
803 #endif
804 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
805 if (flags == EV_BACKCMD) {
806 memout.nleft = 0;
807 memout.nextc = memout.buf;
808 memout.bufsize = 64;
809 mode |= REDIR_BACKQ;
810 }
811 redirect(cmd->ncmd.redirect, mode);
812 savecmdname = commandname;
813 cmdenviron = varlist.list;
814 e = -1;
815 if (setjmp(jmploc.loc)) {
816 e = exception;
817 exitstatus = (e == EXINT)? SIGINT+128 : 2;
818 goto cmddone;
819 }
820 savehandler = handler;
821 handler = &jmploc;
822 commandname = argv[0];
823 argptr = argv + 1;
824 optptr = NULL; /* initialize nextopt */
825 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
826 flushall();
827 cmddone:
828 out1 = &output;
829 out2 = &errout;
830 freestdout();
831 cmdenviron = NULL;
832 if (e != EXSHELLPROC) {
833 commandname = savecmdname;
834 if (flags & EV_EXIT)
835 exitshell(exitstatus);
836 }
837 handler = savehandler;
838 if (e != -1) {
839 if ((e != EXERROR && e != EXEXEC)
840 || cmdentry.u.index == BLTINCMD
841 || cmdentry.u.index == DOTCMD
842 || cmdentry.u.index == EVALCMD
843 #ifndef SMALL
844 || cmdentry.u.index == HISTCMD
845 #endif
846 || cmdentry.u.index == EXECCMD)
847 exraise(e);
848 FORCEINTON;
849 }
850 if (cmdentry.u.index != EXECCMD)
851 popredir();
852 if (flags == EV_BACKCMD) {
853 backcmd->buf = memout.buf;
854 backcmd->nleft = memout.nextc - memout.buf;
855 memout.buf = NULL;
856 }
857 } else {
858 #ifdef DEBUG
859 trputs("normal command: "); trargs(argv);
860 #endif
861 clearredir();
862 redirect(cmd->ncmd.redirect, 0);
863 for (sp = varlist.list ; sp ; sp = sp->next)
864 setvareq(sp->text, VEXPORT|VSTACK);
865 envp = environment();
866 shellexec(argv, envp, pathval(), cmdentry.u.index);
867 }
868 goto out;
869
870 parent: /* parent process gets here (if we forked) */
871 if (mode == 0) { /* argument to fork */
872 INTOFF;
873 exitstatus = waitforjob(jp);
874 INTON;
875 } else if (mode == 2) {
876 backcmd->fd = pip[0];
877 close(pip[1]);
878 backcmd->jp = jp;
879 }
880
881 out:
882 if (lastarg)
883 setvar("_", lastarg, 0);
884 popstackmark(&smark);
885 }
886
887
888
889 /*
890 * Search for a command. This is called before we fork so that the
891 * location of the command will be available in the parent as well as
892 * the child. The check for "goodname" is an overly conservative
893 * check that the name will not be subject to expansion.
894 */
895
896 STATIC void
897 prehash(n)
898 union node *n;
899 {
900 struct cmdentry entry;
901
902 if (n->type == NCMD && n->ncmd.args)
903 if (goodname(n->ncmd.args->narg.text))
904 find_command(n->ncmd.args->narg.text, &entry, 0,
905 pathval());
906 }
907
908
909
910 /*
911 * Builtin commands. Builtin commands whose functions are closely
912 * tied to evaluation are implemented here.
913 */
914
915 /*
916 * No command given, or a bltin command with no arguments. Set the
917 * specified variables.
918 */
919
920 int
921 bltincmd(argc, argv)
922 int argc;
923 char **argv;
924 {
925 listsetvar(cmdenviron);
926 /*
927 * Preserve exitstatus of a previous possible redirection
928 * as POSIX mandates
929 */
930 return exitstatus;
931 }
932
933
934 /*
935 * Handle break and continue commands. Break, continue, and return are
936 * all handled by setting the evalskip flag. The evaluation routines
937 * above all check this flag, and if it is set they start skipping
938 * commands rather than executing them. The variable skipcount is
939 * the number of loops to break/continue, or the number of function
940 * levels to return. (The latter is always 1.) It should probably
941 * be an error to break out of more loops than exist, but it isn't
942 * in the standard shell so we don't make it one here.
943 */
944
945 int
946 breakcmd(argc, argv)
947 int argc;
948 char **argv;
949 {
950 int n = argc > 1 ? number(argv[1]) : 1;
951
952 if (n > loopnest)
953 n = loopnest;
954 if (n > 0) {
955 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
956 skipcount = n;
957 }
958 return 0;
959 }
960
961
962 /*
963 * The return command.
964 */
965
966 int
967 returncmd(argc, argv)
968 int argc;
969 char **argv;
970 {
971 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
972
973 if (funcnest) {
974 evalskip = SKIPFUNC;
975 skipcount = 1;
976 return ret;
977 }
978 else {
979 /* Do what ksh does; skip the rest of the file */
980 evalskip = SKIPFILE;
981 skipcount = 1;
982 return ret;
983 }
984 }
985
986
987 int
988 falsecmd(argc, argv)
989 int argc;
990 char **argv;
991 {
992 return 1;
993 }
994
995
996 int
997 truecmd(argc, argv)
998 int argc;
999 char **argv;
1000 {
1001 return 0;
1002 }
1003
1004
1005 int
1006 execcmd(argc, argv)
1007 int argc;
1008 char **argv;
1009 {
1010 if (argc > 1) {
1011 struct strlist *sp;
1012
1013 iflag = 0; /* exit on error */
1014 mflag = 0;
1015 optschanged();
1016 for (sp = cmdenviron; sp ; sp = sp->next)
1017 setvareq(sp->text, VEXPORT|VSTACK);
1018 shellexec(argv + 1, environment(), pathval(), 0);
1019 }
1020 return 0;
1021 }
1022