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