eval.c revision 1.22 1 /* $NetBSD: eval.c,v 1.22 1995/05/14 04:19:18 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.6 (Berkeley) 5/14/95";
42 #else
43 static char rcsid[] = "$NetBSD: eval.c,v 1.22 1995/05/14 04:19:18 christos Exp $";
44 #endif
45 #endif /* not lint */
46
47 #include <signal.h>
48 #include <unistd.h>
49
50 /*
51 * Evaluate a command.
52 */
53
54 #include "shell.h"
55 #include "nodes.h"
56 #include "syntax.h"
57 #include "expand.h"
58 #include "parser.h"
59 #include "jobs.h"
60 #include "eval.h"
61 #include "builtins.h"
62 #include "options.h"
63 #include "exec.h"
64 #include "redir.h"
65 #include "input.h"
66 #include "output.h"
67 #include "trap.h"
68 #include "var.h"
69 #include "memalloc.h"
70 #include "error.h"
71 #include "show.h"
72 #include "mystring.h"
73 #ifndef NO_HISTORY
74 #include "myhistedit.h"
75 #endif
76
77
78 /* flags in argument to evaltree */
79 #define EV_EXIT 01 /* exit after evaluating tree */
80 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
81 #define EV_BACKCMD 04 /* command executing within back quotes */
82
83
84 /* reasons for skipping commands (see comment on breakcmd routine) */
85 #define SKIPBREAK 1
86 #define SKIPCONT 2
87 #define SKIPFUNC 3
88
89 MKINIT int evalskip; /* set if we are skipping commands */
90 STATIC int skipcount; /* number of levels to skip */
91 MKINIT int loopnest; /* current loop nesting level */
92 int funcnest; /* depth of function calls */
93
94
95 char *commandname;
96 struct strlist *cmdenviron;
97 int exitstatus; /* exit status of last command */
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 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
339 if (evalskip)
340 goto out;
341 }
342 *arglist.lastp = NULL;
343
344 exitstatus = 0;
345 loopnest++;
346 for (sp = arglist.list ; sp ; sp = sp->next) {
347 setvar(n->nfor.var, sp->text, 0);
348 evaltree(n->nfor.body, 0);
349 if (evalskip) {
350 if (evalskip == SKIPCONT && --skipcount <= 0) {
351 evalskip = 0;
352 continue;
353 }
354 if (evalskip == SKIPBREAK && --skipcount <= 0)
355 evalskip = 0;
356 break;
357 }
358 }
359 loopnest--;
360 out:
361 popstackmark(&smark);
362 }
363
364
365
366 STATIC void
367 evalcase(n, flags)
368 union node *n;
369 int flags;
370 {
371 union node *cp;
372 union node *patp;
373 struct arglist arglist;
374 struct stackmark smark;
375
376 setstackmark(&smark);
377 arglist.lastp = &arglist.list;
378 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
379 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
380 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
381 if (casematch(patp, arglist.list->text)) {
382 if (evalskip == 0) {
383 evaltree(cp->nclist.body, flags);
384 }
385 goto out;
386 }
387 }
388 }
389 out:
390 popstackmark(&smark);
391 }
392
393
394
395 /*
396 * Kick off a subshell to evaluate a tree.
397 */
398
399 STATIC void
400 evalsubshell(n, flags)
401 union node *n;
402 int flags;
403 {
404 struct job *jp;
405 int backgnd = (n->type == NBACKGND);
406
407 expredir(n->nredir.redirect);
408 jp = makejob(n, 1);
409 if (forkshell(jp, n, backgnd) == 0) {
410 if (backgnd)
411 flags &=~ EV_TESTED;
412 redirect(n->nredir.redirect, 0);
413 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
414 }
415 if (! backgnd) {
416 INTOFF;
417 exitstatus = waitforjob(jp);
418 INTON;
419 }
420 }
421
422
423
424 /*
425 * Compute the names of the files in a redirection list.
426 */
427
428 STATIC void
429 expredir(n)
430 union node *n;
431 {
432 register union node *redir;
433
434 for (redir = n ; redir ; redir = redir->nfile.next) {
435 struct arglist fn;
436 fn.lastp = &fn.list;
437 switch (redir->type) {
438 case NFROM:
439 case NTO:
440 case NAPPEND:
441 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
442 redir->nfile.expfname = fn.list->text;
443 break;
444 case NFROMFD:
445 case NTOFD:
446 if (redir->ndup.vname) {
447 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
448 fixredir(redir, fn.list->text, 1);
449 }
450 break;
451 }
452 }
453 }
454
455
456
457 /*
458 * Evaluate a pipeline. All the processes in the pipeline are children
459 * of the process creating the pipeline. (This differs from some versions
460 * of the shell, which make the last process in a pipeline the parent
461 * of all the rest.)
462 */
463
464 STATIC void
465 evalpipe(n)
466 union node *n;
467 {
468 struct job *jp;
469 struct nodelist *lp;
470 int pipelen;
471 int prevfd;
472 int pip[2];
473
474 TRACE(("evalpipe(0x%lx) called\n", (long)n));
475 pipelen = 0;
476 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
477 pipelen++;
478 INTOFF;
479 jp = makejob(n, pipelen);
480 prevfd = -1;
481 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
482 prehash(lp->n);
483 pip[1] = -1;
484 if (lp->next) {
485 if (pipe(pip) < 0) {
486 close(prevfd);
487 error("Pipe call failed");
488 }
489 }
490 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
491 INTON;
492 if (prevfd > 0) {
493 close(0);
494 copyfd(prevfd, 0);
495 close(prevfd);
496 }
497 if (pip[1] >= 0) {
498 close(pip[0]);
499 if (pip[1] != 1) {
500 close(1);
501 copyfd(pip[1], 1);
502 close(pip[1]);
503 }
504 }
505 evaltree(lp->n, EV_EXIT);
506 }
507 if (prevfd >= 0)
508 close(prevfd);
509 prevfd = pip[0];
510 close(pip[1]);
511 }
512 INTON;
513 if (n->npipe.backgnd == 0) {
514 INTOFF;
515 exitstatus = waitforjob(jp);
516 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
517 INTON;
518 }
519 }
520
521
522
523 /*
524 * Execute a command inside back quotes. If it's a builtin command, we
525 * want to save its output in a block obtained from malloc. Otherwise
526 * we fork off a subprocess and get the output of the command via a pipe.
527 * Should be called with interrupts off.
528 */
529
530 void
531 evalbackcmd(n, result)
532 union node *n;
533 struct backcmd *result;
534 {
535 int pip[2];
536 struct job *jp;
537 struct stackmark smark; /* unnecessary */
538
539 setstackmark(&smark);
540 result->fd = -1;
541 result->buf = NULL;
542 result->nleft = 0;
543 result->jp = NULL;
544 exitstatus = 0;
545 if (n == NULL)
546 goto out;
547 if (n->type == NCMD) {
548 evalcommand(n, EV_BACKCMD, result);
549 } else {
550 if (pipe(pip) < 0)
551 error("Pipe call failed");
552 jp = makejob(n, 1);
553 if (forkshell(jp, n, FORK_NOJOB) == 0) {
554 FORCEINTON;
555 close(pip[0]);
556 if (pip[1] != 1) {
557 close(1);
558 copyfd(pip[1], 1);
559 close(pip[1]);
560 }
561 evaltree(n, EV_EXIT);
562 }
563 close(pip[1]);
564 result->fd = pip[0];
565 result->jp = jp;
566 }
567 out:
568 popstackmark(&smark);
569 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
570 result->fd, result->buf, result->nleft, result->jp));
571 }
572
573
574
575 /*
576 * Execute a simple command.
577 */
578
579 STATIC void
580 evalcommand(cmd, flags, backcmd)
581 union node *cmd;
582 int flags;
583 struct backcmd *backcmd;
584 {
585 struct stackmark smark;
586 union node *argp;
587 struct arglist arglist;
588 struct arglist varlist;
589 char **argv;
590 int argc;
591 char **envp;
592 int varflag;
593 struct strlist *sp;
594 int mode;
595 int pip[2];
596 struct cmdentry cmdentry;
597 struct job *jp;
598 struct jmploc jmploc;
599 struct jmploc *volatile savehandler;
600 char *volatile savecmdname;
601 volatile struct shparam saveparam;
602 struct localvar *volatile savelocalvars;
603 volatile int e;
604 char *lastarg;
605 #if __GNUC__
606 /* Avoid longjmp clobbering */
607 (void) &argv;
608 (void) &argc;
609 (void) &lastarg;
610 (void) &flags;
611 #endif
612
613 /* First expand the arguments. */
614 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
615 setstackmark(&smark);
616 arglist.lastp = &arglist.list;
617 varlist.lastp = &varlist.list;
618 varflag = 1;
619 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
620 char *p = argp->narg.text;
621 if (varflag && is_name(*p)) {
622 do {
623 p++;
624 } while (is_in_name(*p));
625 if (*p == '=') {
626 expandarg(argp, &varlist, EXP_VARTILDE);
627 continue;
628 }
629 }
630 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
631 varflag = 0;
632 }
633 *arglist.lastp = NULL;
634 *varlist.lastp = NULL;
635 expredir(cmd->ncmd.redirect);
636 argc = 0;
637 for (sp = arglist.list ; sp ; sp = sp->next)
638 argc++;
639 argv = stalloc(sizeof (char *) * (argc + 1));
640
641 for (sp = arglist.list ; sp ; sp = sp->next) {
642 TRACE(("evalcommand arg: %s\n", sp->text));
643 *argv++ = sp->text;
644 }
645 *argv = NULL;
646 lastarg = NULL;
647 if (iflag && funcnest == 0 && argc > 0)
648 lastarg = argv[-1];
649 argv -= argc;
650
651 /* Print the command if xflag is set. */
652 if (xflag) {
653 outc('+', &errout);
654 for (sp = varlist.list ; sp ; sp = sp->next) {
655 outc(' ', &errout);
656 out2str(sp->text);
657 }
658 for (sp = arglist.list ; sp ; sp = sp->next) {
659 outc(' ', &errout);
660 out2str(sp->text);
661 }
662 outc('\n', &errout);
663 flushout(&errout);
664 }
665
666 /* Now locate the command. */
667 if (argc == 0) {
668 cmdentry.cmdtype = CMDBUILTIN;
669 cmdentry.u.index = BLTINCMD;
670 } else {
671 find_command(argv[0], &cmdentry, 1);
672 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
673 exitstatus = 2;
674 flushout(&errout);
675 return;
676 }
677 /* implement the bltin builtin here */
678 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
679 for (;;) {
680 argv++;
681 if (--argc == 0)
682 break;
683 if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
684 outfmt(&errout, "%s: not found\n", *argv);
685 exitstatus = 2;
686 flushout(&errout);
687 return;
688 }
689 if (cmdentry.u.index != BLTINCMD)
690 break;
691 }
692 }
693 }
694
695 /* Fork off a child process if necessary. */
696 if (cmd->ncmd.backgnd
697 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
698 || ((flags & EV_BACKCMD) != 0
699 && (cmdentry.cmdtype != CMDBUILTIN
700 || cmdentry.u.index == DOTCMD
701 || cmdentry.u.index == EVALCMD))) {
702 jp = makejob(cmd, 1);
703 mode = cmd->ncmd.backgnd;
704 if (flags & EV_BACKCMD) {
705 mode = FORK_NOJOB;
706 if (pipe(pip) < 0)
707 error("Pipe call failed");
708 }
709 if (forkshell(jp, cmd, mode) != 0)
710 goto parent; /* at end of routine */
711 if (flags & EV_BACKCMD) {
712 FORCEINTON;
713 close(pip[0]);
714 if (pip[1] != 1) {
715 close(1);
716 copyfd(pip[1], 1);
717 close(pip[1]);
718 }
719 }
720 flags |= EV_EXIT;
721 }
722
723 /* This is the child process if a fork occurred. */
724 /* Execute the command. */
725 if (cmdentry.cmdtype == CMDFUNCTION) {
726 trputs("Shell function: "); trargs(argv);
727 redirect(cmd->ncmd.redirect, REDIR_PUSH);
728 saveparam = shellparam;
729 shellparam.malloc = 0;
730 shellparam.nparam = argc - 1;
731 shellparam.p = argv + 1;
732 shellparam.optnext = NULL;
733 INTOFF;
734 savelocalvars = localvars;
735 localvars = NULL;
736 INTON;
737 if (setjmp(jmploc.loc)) {
738 if (exception == EXSHELLPROC)
739 freeparam((struct shparam *)&saveparam);
740 else {
741 freeparam(&shellparam);
742 shellparam = saveparam;
743 }
744 poplocalvars();
745 localvars = savelocalvars;
746 handler = savehandler;
747 longjmp(handler->loc, 1);
748 }
749 savehandler = handler;
750 handler = &jmploc;
751 for (sp = varlist.list ; sp ; sp = sp->next)
752 mklocal(sp->text);
753 funcnest++;
754 evaltree(cmdentry.u.func, 0);
755 funcnest--;
756 INTOFF;
757 poplocalvars();
758 localvars = savelocalvars;
759 freeparam(&shellparam);
760 shellparam = saveparam;
761 handler = savehandler;
762 popredir();
763 INTON;
764 if (evalskip == SKIPFUNC) {
765 evalskip = 0;
766 skipcount = 0;
767 }
768 if (flags & EV_EXIT)
769 exitshell(exitstatus);
770 } else if (cmdentry.cmdtype == CMDBUILTIN) {
771 trputs("builtin command: "); trargs(argv);
772 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
773 if (flags == EV_BACKCMD) {
774 memout.nleft = 0;
775 memout.nextc = memout.buf;
776 memout.bufsize = 64;
777 mode |= REDIR_BACKQ;
778 }
779 redirect(cmd->ncmd.redirect, mode);
780 savecmdname = commandname;
781 cmdenviron = varlist.list;
782 e = -1;
783 if (setjmp(jmploc.loc)) {
784 e = exception;
785 exitstatus = (e == EXINT)? SIGINT+128 : 2;
786 goto cmddone;
787 }
788 savehandler = handler;
789 handler = &jmploc;
790 commandname = argv[0];
791 argptr = argv + 1;
792 optptr = NULL; /* initialize nextopt */
793 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
794 flushall();
795 cmddone:
796 out1 = &output;
797 out2 = &errout;
798 freestdout();
799 if (e != EXSHELLPROC) {
800 commandname = savecmdname;
801 if (flags & EV_EXIT) {
802 exitshell(exitstatus);
803 }
804 }
805 handler = savehandler;
806 if (e != -1) {
807 if (e != EXERROR || cmdentry.u.index == BLTINCMD
808 || cmdentry.u.index == DOTCMD
809 || cmdentry.u.index == EVALCMD
810 #ifndef NO_HISTORY
811 || cmdentry.u.index == HISTCMD
812 #endif
813 || cmdentry.u.index == EXECCMD)
814 exraise(e);
815 FORCEINTON;
816 }
817 if (cmdentry.u.index != EXECCMD)
818 popredir();
819 if (flags == EV_BACKCMD) {
820 backcmd->buf = memout.buf;
821 backcmd->nleft = memout.nextc - memout.buf;
822 memout.buf = NULL;
823 }
824 } else {
825 trputs("normal command: "); trargs(argv);
826 clearredir();
827 redirect(cmd->ncmd.redirect, 0);
828 for (sp = varlist.list ; sp ; sp = sp->next)
829 setvareq(sp->text, VEXPORT|VSTACK);
830 envp = environment();
831 shellexec(argv, envp, pathval(), cmdentry.u.index);
832 /*NOTREACHED*/
833 }
834 goto out;
835
836 parent: /* parent process gets here (if we forked) */
837 if (mode == 0) { /* argument to fork */
838 INTOFF;
839 exitstatus = waitforjob(jp);
840 INTON;
841 } else if (mode == 2) {
842 backcmd->fd = pip[0];
843 close(pip[1]);
844 backcmd->jp = jp;
845 }
846
847 out:
848 if (lastarg)
849 setvar("_", lastarg, 0);
850 popstackmark(&smark);
851 }
852
853
854
855 /*
856 * Search for a command. This is called before we fork so that the
857 * location of the command will be available in the parent as well as
858 * the child. The check for "goodname" is an overly conservative
859 * check that the name will not be subject to expansion.
860 */
861
862 STATIC void
863 prehash(n)
864 union node *n;
865 {
866 struct cmdentry entry;
867
868 if (n->type == NCMD && n->ncmd.args)
869 if (goodname(n->ncmd.args->narg.text))
870 find_command(n->ncmd.args->narg.text, &entry, 0);
871 }
872
873
874
875 /*
876 * Builtin commands. Builtin commands whose functions are closely
877 * tied to evaluation are implemented here.
878 */
879
880 /*
881 * No command given, or a bltin command with no arguments. Set the
882 * specified variables.
883 */
884
885 int
886 bltincmd(argc, argv)
887 int argc;
888 char **argv;
889 {
890 listsetvar(cmdenviron);
891 /*
892 * Preserve exitstatus of a previous possible redirection
893 * as POSIX mandates
894 */
895 return exitstatus;
896 }
897
898
899 /*
900 * Handle break and continue commands. Break, continue, and return are
901 * all handled by setting the evalskip flag. The evaluation routines
902 * above all check this flag, and if it is set they start skipping
903 * commands rather than executing them. The variable skipcount is
904 * the number of loops to break/continue, or the number of function
905 * levels to return. (The latter is always 1.) It should probably
906 * be an error to break out of more loops than exist, but it isn't
907 * in the standard shell so we don't make it one here.
908 */
909
910 int
911 breakcmd(argc, argv)
912 int argc;
913 char **argv;
914 {
915 int n;
916
917 n = 1;
918 if (argc > 1)
919 n = number(argv[1]);
920 if (n > loopnest)
921 n = loopnest;
922 if (n > 0) {
923 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
924 skipcount = n;
925 }
926 return 0;
927 }
928
929
930 /*
931 * The return command.
932 */
933
934 int
935 returncmd(argc, argv)
936 int argc;
937 char **argv;
938 {
939 int ret;
940
941 ret = exitstatus;
942 if (argc > 1)
943 ret = number(argv[1]);
944 if (funcnest) {
945 evalskip = SKIPFUNC;
946 skipcount = 1;
947 }
948 return ret;
949 }
950
951
952 int
953 falsecmd(argc, argv)
954 int argc;
955 char **argv;
956 {
957 return 1;
958 }
959
960
961 int
962 truecmd(argc, argv)
963 int argc;
964 char **argv;
965 {
966 return 0;
967 }
968
969
970 int
971 execcmd(argc, argv)
972 int argc;
973 char **argv;
974 {
975 if (argc > 1) {
976 struct strlist *sp;
977
978 iflag = 0; /* exit on error */
979 mflag = 0;
980 optschanged();
981 for (sp = cmdenviron; sp ; sp = sp->next)
982 setvareq(sp->text, VEXPORT|VSTACK);
983 shellexec(argv + 1, environment(), pathval(), 0);
984
985 }
986 return 0;
987 }
988