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