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