eval.c revision 1.63 1 /* $NetBSD: eval.c,v 1.63 2002/09/27 21:32:25 mycroft 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.63 2002/09/27 21:32:25 mycroft Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <signal.h>
49 #include <unistd.h>
50
51 #include <sys/types.h>
52 #include <sys/wait.h>
53
54 /*
55 * Evaluate a command.
56 */
57
58 #include "shell.h"
59 #include "nodes.h"
60 #include "syntax.h"
61 #include "expand.h"
62 #include "parser.h"
63 #include "jobs.h"
64 #include "eval.h"
65 #include "builtins.h"
66 #include "options.h"
67 #include "exec.h"
68 #include "redir.h"
69 #include "input.h"
70 #include "output.h"
71 #include "trap.h"
72 #include "var.h"
73 #include "memalloc.h"
74 #include "error.h"
75 #include "show.h"
76 #include "mystring.h"
77 #ifndef SMALL
78 #include "myhistedit.h"
79 #endif
80
81
82 /* flags in argument to evaltree */
83 #define EV_EXIT 01 /* exit after evaluating tree */
84 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
85 #define EV_BACKCMD 04 /* command executing within back quotes */
86
87 int evalskip; /* set if we are skipping commands */
88 STATIC int skipcount; /* number of levels to skip */
89 MKINIT int loopnest; /* current loop nesting level */
90 int funcnest; /* depth of function calls */
91
92
93 char *commandname;
94 struct strlist *cmdenviron;
95 int exitstatus; /* exit status of last command */
96 int oexitstatus; /* saved exit status */
97
98
99 STATIC void evalloop __P((union node *, int));
100 STATIC void evalfor __P((union node *, int));
101 STATIC void evalcase __P((union node *, int));
102 STATIC void evalsubshell __P((union node *, int));
103 STATIC void expredir __P((union node *));
104 STATIC void evalpipe __P((union node *));
105 STATIC void evalcommand __P((union node *, int, struct backcmd *));
106 STATIC void prehash __P((union node *));
107
108
109 /*
110 * Called to reset things after an exception.
111 */
112
113 #ifdef mkinit
114 INCLUDE "eval.h"
115
116 RESET {
117 evalskip = 0;
118 loopnest = 0;
119 funcnest = 0;
120 }
121
122 SHELLPROC {
123 exitstatus = 0;
124 }
125 #endif
126
127
128
129 /*
130 * The eval commmand.
131 */
132
133 int
134 evalcmd(argc, argv)
135 int argc;
136 char **argv;
137 {
138 char *p;
139 char *concat;
140 char **ap;
141
142 if (argc > 1) {
143 p = argv[1];
144 if (argc > 2) {
145 STARTSTACKSTR(concat);
146 ap = argv + 2;
147 for (;;) {
148 while (*p)
149 STPUTC(*p++, concat);
150 if ((p = *ap++) == NULL)
151 break;
152 STPUTC(' ', concat);
153 }
154 STPUTC('\0', concat);
155 p = grabstackstr(concat);
156 }
157 evalstring(p, EV_TESTED);
158 }
159 return exitstatus;
160 }
161
162
163 /*
164 * Execute a command or commands contained in a string.
165 */
166
167 void
168 evalstring(s, flag)
169 char *s;
170 int flag;
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, flag);
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 SMALL
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, flags & EV_TESTED);
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 /* don't bomb out on "set -e; false && true" */
217 flags |= EV_TESTED;
218 goto out;
219 }
220 evaltree(n->nbinary.ch2, flags | EV_TESTED);
221 break;
222 case NOR:
223 evaltree(n->nbinary.ch1, EV_TESTED);
224 if (evalskip || exitstatus == 0)
225 goto out;
226 evaltree(n->nbinary.ch2, flags | EV_TESTED);
227 break;
228 case NREDIR:
229 expredir(n->nredir.redirect);
230 redirect(n->nredir.redirect, REDIR_PUSH);
231 evaltree(n->nredir.n, flags);
232 popredir();
233 break;
234 case NSUBSHELL:
235 evalsubshell(n, flags);
236 break;
237 case NBACKGND:
238 evalsubshell(n, flags);
239 break;
240 case NIF: {
241 evaltree(n->nif.test, EV_TESTED);
242 if (evalskip)
243 goto out;
244 if (exitstatus == 0)
245 evaltree(n->nif.ifpart, flags);
246 else if (n->nif.elsepart)
247 evaltree(n->nif.elsepart, flags);
248 else
249 exitstatus = 0;
250 break;
251 }
252 case NWHILE:
253 case NUNTIL:
254 evalloop(n, flags);
255 break;
256 case NFOR:
257 evalfor(n, flags);
258 break;
259 case NCASE:
260 evalcase(n, flags);
261 break;
262 case NDEFUN:
263 defun(n->narg.text, n->narg.next);
264 exitstatus = 0;
265 break;
266 case NNOT:
267 evaltree(n->nnot.com, EV_TESTED);
268 exitstatus = !exitstatus;
269 break;
270
271 case NPIPE:
272 evalpipe(n);
273 break;
274 case NCMD:
275 evalcommand(n, flags, (struct backcmd *)NULL);
276 break;
277 default:
278 out1fmt("Node type = %d\n", n->type);
279 flushout(&output);
280 break;
281 }
282 out:
283 if (pendingsigs)
284 dotrap();
285 if ((flags & EV_EXIT) != 0)
286 exitshell(exitstatus);
287 }
288
289
290 STATIC void
291 evalloop(n, flags)
292 union node *n;
293 int flags;
294 {
295 int status;
296
297 loopnest++;
298 status = 0;
299 for (;;) {
300 evaltree(n->nbinary.ch1, EV_TESTED);
301 if (evalskip) {
302 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
303 evalskip = 0;
304 continue;
305 }
306 if (evalskip == SKIPBREAK && --skipcount <= 0)
307 evalskip = 0;
308 break;
309 }
310 if (n->type == NWHILE) {
311 if (exitstatus != 0)
312 break;
313 } else {
314 if (exitstatus == 0)
315 break;
316 }
317 evaltree(n->nbinary.ch2, flags & EV_TESTED);
318 status = exitstatus;
319 if (evalskip)
320 goto skipping;
321 }
322 loopnest--;
323 exitstatus = status;
324 }
325
326
327
328 STATIC void
329 evalfor(n, flags)
330 union node *n;
331 int flags;
332 {
333 struct arglist arglist;
334 union node *argp;
335 struct strlist *sp;
336 struct stackmark smark;
337
338 setstackmark(&smark);
339 arglist.lastp = &arglist.list;
340 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
341 oexitstatus = exitstatus;
342 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD);
343 if (evalskip)
344 goto out;
345 }
346 *arglist.lastp = NULL;
347
348 exitstatus = 0;
349 loopnest++;
350 for (sp = arglist.list ; sp ; sp = sp->next) {
351 setvar(n->nfor.var, sp->text, 0);
352 evaltree(n->nfor.body, flags & EV_TESTED);
353 if (evalskip) {
354 if (evalskip == SKIPCONT && --skipcount <= 0) {
355 evalskip = 0;
356 continue;
357 }
358 if (evalskip == SKIPBREAK && --skipcount <= 0)
359 evalskip = 0;
360 break;
361 }
362 }
363 loopnest--;
364 out:
365 popstackmark(&smark);
366 }
367
368
369
370 STATIC void
371 evalcase(n, flags)
372 union node *n;
373 int flags;
374 {
375 union node *cp;
376 union node *patp;
377 struct arglist arglist;
378 struct stackmark smark;
379
380 setstackmark(&smark);
381 arglist.lastp = &arglist.list;
382 oexitstatus = exitstatus;
383 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
384 for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
385 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
386 if (casematch(patp, arglist.list->text)) {
387 if (evalskip == 0) {
388 evaltree(cp->nclist.body, flags);
389 }
390 goto out;
391 }
392 }
393 }
394 out:
395 popstackmark(&smark);
396 }
397
398
399
400 /*
401 * Kick off a subshell to evaluate a tree.
402 */
403
404 STATIC void
405 evalsubshell(n, flags)
406 union node *n;
407 int flags;
408 {
409 struct job *jp;
410 int backgnd = (n->type == NBACKGND);
411
412 expredir(n->nredir.redirect);
413 INTOFF;
414 jp = makejob(n, 1);
415 if (forkshell(jp, n, backgnd) == 0) {
416 INTON;
417 if (backgnd)
418 flags &=~ EV_TESTED;
419 redirect(n->nredir.redirect, 0);
420 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
421 }
422 if (! backgnd)
423 exitstatus = waitforjob(jp);
424 INTON;
425 }
426
427
428
429 /*
430 * Compute the names of the files in a redirection list.
431 */
432
433 STATIC void
434 expredir(n)
435 union node *n;
436 {
437 union node *redir;
438
439 for (redir = n ; redir ; redir = redir->nfile.next) {
440 struct arglist fn;
441 fn.lastp = &fn.list;
442 oexitstatus = exitstatus;
443 switch (redir->type) {
444 case NFROMTO:
445 case NFROM:
446 case NTO:
447 case NCLOBBER:
448 case NAPPEND:
449 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
450 redir->nfile.expfname = fn.list->text;
451 break;
452 case NFROMFD:
453 case NTOFD:
454 if (redir->ndup.vname) {
455 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
456 fixredir(redir, fn.list->text, 1);
457 }
458 break;
459 }
460 }
461 }
462
463
464
465 /*
466 * Evaluate a pipeline. All the processes in the pipeline are children
467 * of the process creating the pipeline. (This differs from some versions
468 * of the shell, which make the last process in a pipeline the parent
469 * of all the rest.)
470 */
471
472 STATIC void
473 evalpipe(n)
474 union node *n;
475 {
476 struct job *jp;
477 struct nodelist *lp;
478 int pipelen;
479 int prevfd;
480 int pip[2];
481
482 TRACE(("evalpipe(0x%lx) called\n", (long)n));
483 pipelen = 0;
484 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
485 pipelen++;
486 INTOFF;
487 jp = makejob(n, pipelen);
488 prevfd = -1;
489 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
490 prehash(lp->n);
491 pip[1] = -1;
492 if (lp->next) {
493 if (pipe(pip) < 0) {
494 close(prevfd);
495 error("Pipe call failed");
496 }
497 }
498 if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
499 INTON;
500 if (prevfd > 0) {
501 close(0);
502 copyfd(prevfd, 0);
503 close(prevfd);
504 }
505 if (pip[1] >= 0) {
506 close(pip[0]);
507 if (pip[1] != 1) {
508 close(1);
509 copyfd(pip[1], 1);
510 close(pip[1]);
511 }
512 }
513 evaltree(lp->n, EV_EXIT);
514 }
515 if (prevfd >= 0)
516 close(prevfd);
517 prevfd = pip[0];
518 close(pip[1]);
519 }
520 if (n->npipe.backgnd == 0) {
521 exitstatus = waitforjob(jp);
522 TRACE(("evalpipe: job done exit status %d\n", exitstatus));
523 }
524 INTON;
525 }
526
527
528
529 /*
530 * Execute a command inside back quotes. If it's a builtin command, we
531 * want to save its output in a block obtained from malloc. Otherwise
532 * we fork off a subprocess and get the output of the command via a pipe.
533 * Should be called with interrupts off.
534 */
535
536 void
537 evalbackcmd(n, result)
538 union node *n;
539 struct backcmd *result;
540 {
541 int pip[2];
542 struct job *jp;
543 struct stackmark smark; /* unnecessary */
544
545 setstackmark(&smark);
546 result->fd = -1;
547 result->buf = NULL;
548 result->nleft = 0;
549 result->jp = NULL;
550 if (n == NULL) {
551 exitstatus = 0;
552 goto out;
553 }
554 #ifdef notyet
555 /*
556 * For now we disable executing builtins in the same
557 * context as the shell, because we are not keeping
558 * enough state to recover from changes that are
559 * supposed only to affect subshells. eg. echo "`cd /`"
560 */
561 if (n->type == NCMD) {
562 exitstatus = oexitstatus;
563 evalcommand(n, EV_BACKCMD, result);
564 } else
565 #endif
566 {
567 exitstatus = 0;
568 INTOFF;
569 if (pipe(pip) < 0)
570 error("Pipe call failed");
571 jp = makejob(n, 1);
572 if (forkshell(jp, n, FORK_NOJOB) == 0) {
573 FORCEINTON;
574 close(pip[0]);
575 if (pip[1] != 1) {
576 close(1);
577 copyfd(pip[1], 1);
578 close(pip[1]);
579 }
580 eflag = 0;
581 evaltree(n, EV_EXIT);
582 }
583 close(pip[1]);
584 result->fd = pip[0];
585 result->jp = jp;
586 INTON;
587 }
588 out:
589 popstackmark(&smark);
590 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
591 result->fd, result->buf, result->nleft, result->jp));
592 }
593
594
595 int vforked = 0;
596
597 /*
598 * Execute a simple command.
599 */
600
601 STATIC void
602 evalcommand(cmd, flags, backcmd)
603 union node *cmd;
604 int flags;
605 struct backcmd *backcmd;
606 {
607 struct stackmark smark;
608 union node *argp;
609 struct arglist arglist;
610 struct arglist varlist;
611 char **argv;
612 int argc;
613 char **envp;
614 int varflag;
615 struct strlist *sp;
616 int mode;
617 int pip[2];
618 struct cmdentry cmdentry;
619 struct job *jp;
620 struct jmploc jmploc;
621 struct jmploc *volatile savehandler;
622 char *volatile savecmdname;
623 volatile struct shparam saveparam;
624 struct localvar *volatile savelocalvars;
625 volatile int e;
626 char *lastarg;
627 #if __GNUC__
628 /* Avoid longjmp clobbering */
629 (void) &argv;
630 (void) &argc;
631 (void) &lastarg;
632 (void) &flags;
633 #endif
634
635 vforked = 0;
636 /* First expand the arguments. */
637 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
638 setstackmark(&smark);
639 arglist.lastp = &arglist.list;
640 varlist.lastp = &varlist.list;
641 varflag = 1;
642 oexitstatus = exitstatus;
643 exitstatus = 0;
644 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
645 char *p = argp->narg.text;
646 if (varflag && is_name(*p)) {
647 do {
648 p++;
649 } while (is_in_name(*p));
650 if (*p == '=') {
651 expandarg(argp, &varlist, EXP_VARTILDE);
652 continue;
653 }
654 }
655 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
656 varflag = 0;
657 }
658 *arglist.lastp = NULL;
659 *varlist.lastp = NULL;
660 expredir(cmd->ncmd.redirect);
661 argc = 0;
662 for (sp = arglist.list ; sp ; sp = sp->next)
663 argc++;
664 argv = stalloc(sizeof (char *) * (argc + 1));
665
666 for (sp = arglist.list ; sp ; sp = sp->next) {
667 TRACE(("evalcommand arg: %s\n", sp->text));
668 *argv++ = sp->text;
669 }
670 *argv = NULL;
671 lastarg = NULL;
672 if (iflag && funcnest == 0 && argc > 0)
673 lastarg = argv[-1];
674 argv -= argc;
675
676 /* Print the command if xflag is set. */
677 if (xflag) {
678 outc('+', &errout);
679 for (sp = varlist.list ; sp ; sp = sp->next) {
680 outc(' ', &errout);
681 out2str(sp->text);
682 }
683 for (sp = arglist.list ; sp ; sp = sp->next) {
684 outc(' ', &errout);
685 out2str(sp->text);
686 }
687 outc('\n', &errout);
688 flushout(&errout);
689 }
690
691 /* Now locate the command. */
692 if (argc == 0) {
693 cmdentry.cmdtype = CMDBUILTIN;
694 cmdentry.u.index = BLTINCMD;
695 } else {
696 static const char PATH[] = "PATH=";
697 const char *path = pathval();
698
699 /*
700 * Modify the command lookup path, if a PATH= assignment
701 * is present
702 */
703 for (sp = varlist.list ; sp ; sp = sp->next)
704 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
705 path = sp->text + sizeof(PATH) - 1;
706
707 find_command(argv[0], &cmdentry, DO_ERR, path);
708 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
709 exitstatus = 127;
710 flushout(&errout);
711 return;
712 }
713 /* implement the bltin builtin here */
714 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
715 for (;;) {
716 argv++;
717 if (--argc == 0)
718 break;
719 if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
720 outfmt(&errout, "%s: not found\n", *argv);
721 exitstatus = 127;
722 flushout(&errout);
723 return;
724 }
725 if (cmdentry.u.index != BLTINCMD)
726 break;
727 }
728 }
729 }
730
731 /* Fork off a child process if necessary. */
732 if (cmd->ncmd.backgnd
733 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
734 || ((flags & EV_BACKCMD) != 0
735 && (cmdentry.cmdtype != CMDBUILTIN
736 || cmdentry.u.index == DOTCMD
737 || cmdentry.u.index == EVALCMD))) {
738 INTOFF;
739 jp = makejob(cmd, 1);
740 mode = cmd->ncmd.backgnd;
741 if (flags & EV_BACKCMD) {
742 mode = FORK_NOJOB;
743 if (pipe(pip) < 0)
744 error("Pipe call failed");
745 }
746 #ifdef DO_SHAREDVFORK
747 /* It is essential that if DO_SHAREDVFORK is defined that the
748 * child's address space is actually shared with the parent as
749 * we rely on this.
750 */
751 if (cmdentry.cmdtype == CMDNORMAL) {
752 pid_t pid;
753
754 savelocalvars = localvars;
755 localvars = NULL;
756 for (sp = varlist.list ; sp ; sp = sp->next)
757 mklocal(sp->text, VEXPORT);
758 vforked = 1;
759 switch (pid = vfork()) {
760 case -1:
761 TRACE(("Vfork failed, errno=%d", errno));
762 INTON;
763 error("Cannot vfork");
764 break;
765 case 0:
766 /* Make sure that exceptions only unwind to
767 * after the vfork(2)
768 */
769 if (setjmp(jmploc.loc)) {
770 if (exception == EXSHELLPROC) {
771 /* We can't progress with the vfork,
772 * so, set vforked = 2 so the parent
773 * knows, and _exit();
774 */
775 vforked = 2;
776 _exit(0);
777 } else {
778 _exit(exerrno);
779 }
780 }
781 savehandler = handler;
782 handler = &jmploc;
783 forkchild(jp, cmd, mode, vforked);
784 break;
785 default:
786 handler = savehandler; /* restore from vfork(2) */
787 poplocalvars();
788 localvars = savelocalvars;
789 if (vforked == 2) {
790 vforked = 0;
791
792 (void)waitpid(pid, NULL, 0);
793 /* We need to progress in a normal fork fashion */
794 goto normal_fork;
795 }
796 vforked = 0;
797 forkparent(jp, cmd, mode, pid);
798 goto parent;
799 }
800 } else {
801 normal_fork:
802 #endif
803 if (forkshell(jp, cmd, mode) != 0)
804 goto parent; /* at end of routine */
805 INTON;
806 #ifdef DO_SHAREDVFORK
807 }
808 #endif
809 if (flags & EV_BACKCMD) {
810 if (!vforked) {
811 FORCEINTON;
812 }
813 close(pip[0]);
814 if (pip[1] != 1) {
815 close(1);
816 copyfd(pip[1], 1);
817 close(pip[1]);
818 }
819 }
820 flags |= EV_EXIT;
821 }
822
823 /* This is the child process if a fork occurred. */
824 /* Execute the command. */
825 if (cmdentry.cmdtype == CMDFUNCTION) {
826 #ifdef DEBUG
827 trputs("Shell function: "); trargs(argv);
828 #endif
829 redirect(cmd->ncmd.redirect, REDIR_PUSH);
830 saveparam = shellparam;
831 shellparam.malloc = 0;
832 shellparam.reset = 1;
833 shellparam.nparam = argc - 1;
834 shellparam.p = argv + 1;
835 shellparam.optnext = NULL;
836 INTOFF;
837 savelocalvars = localvars;
838 localvars = NULL;
839 INTON;
840 if (setjmp(jmploc.loc)) {
841 if (exception == EXSHELLPROC) {
842 freeparam((volatile struct shparam *)
843 &saveparam);
844 } else {
845 freeparam(&shellparam);
846 shellparam = saveparam;
847 }
848 poplocalvars();
849 localvars = savelocalvars;
850 handler = savehandler;
851 longjmp(handler->loc, 1);
852 }
853 savehandler = handler;
854 handler = &jmploc;
855 for (sp = varlist.list ; sp ; sp = sp->next)
856 mklocal(sp->text, 0);
857 funcnest++;
858 evaltree(cmdentry.u.func, flags & EV_TESTED);
859 funcnest--;
860 INTOFF;
861 poplocalvars();
862 localvars = savelocalvars;
863 freeparam(&shellparam);
864 shellparam = saveparam;
865 handler = savehandler;
866 popredir();
867 INTON;
868 if (evalskip == SKIPFUNC) {
869 evalskip = 0;
870 skipcount = 0;
871 }
872 if (flags & EV_EXIT)
873 exitshell(exitstatus);
874 } else if (cmdentry.cmdtype == CMDBUILTIN) {
875 #ifdef DEBUG
876 trputs("builtin command: "); trargs(argv);
877 #endif
878 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
879 if (flags == EV_BACKCMD) {
880 memout.nleft = 0;
881 memout.nextc = memout.buf;
882 memout.bufsize = 64;
883 mode |= REDIR_BACKQ;
884 }
885 redirect(cmd->ncmd.redirect, mode);
886 savecmdname = commandname;
887 cmdenviron = varlist.list;
888 e = -1;
889 if (setjmp(jmploc.loc)) {
890 e = exception;
891 exitstatus = (e == EXINT)? SIGINT+128 : 2;
892 goto cmddone;
893 }
894 savehandler = handler;
895 handler = &jmploc;
896 commandname = argv[0];
897 argptr = argv + 1;
898 optptr = NULL; /* initialize nextopt */
899 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
900 flushall();
901 cmddone:
902 out1 = &output;
903 out2 = &errout;
904 freestdout();
905 cmdenviron = NULL;
906 if (e != EXSHELLPROC) {
907 commandname = savecmdname;
908 if (flags & EV_EXIT)
909 exitshell(exitstatus);
910 }
911 handler = savehandler;
912 if (e != -1) {
913 if ((e != EXERROR && e != EXEXEC)
914 || cmdentry.u.index == BLTINCMD
915 || cmdentry.u.index == DOTCMD
916 || cmdentry.u.index == EVALCMD
917 #ifndef SMALL
918 || cmdentry.u.index == HISTCMD
919 #endif
920 || cmdentry.u.index == EXECCMD)
921 exraise(e);
922 FORCEINTON;
923 }
924 if (cmdentry.u.index != EXECCMD)
925 popredir();
926 if (flags == EV_BACKCMD) {
927 backcmd->buf = memout.buf;
928 backcmd->nleft = memout.nextc - memout.buf;
929 memout.buf = NULL;
930 }
931 } else {
932 #ifdef DEBUG
933 trputs("normal command: "); trargs(argv);
934 #endif
935 clearredir(vforked);
936 redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0);
937 if (!vforked)
938 for (sp = varlist.list ; sp ; sp = sp->next)
939 setvareq(sp->text, VEXPORT|VSTACK);
940 envp = environment();
941 shellexec(argv, envp, pathval(), cmdentry.u.index, vforked);
942 }
943 goto out;
944
945 parent: /* parent process gets here (if we forked) */
946 if (mode == 0) { /* argument to fork */
947 exitstatus = waitforjob(jp);
948 } else if (mode == 2) {
949 backcmd->fd = pip[0];
950 close(pip[1]);
951 backcmd->jp = jp;
952 }
953 INTON;
954
955 out:
956 if (lastarg)
957 setvar("_", lastarg, 0);
958 popstackmark(&smark);
959
960 if (eflag && exitstatus && !(flags & EV_TESTED))
961 exitshell(exitstatus);
962 }
963
964
965 /*
966 * Search for a command. This is called before we fork so that the
967 * location of the command will be available in the parent as well as
968 * the child. The check for "goodname" is an overly conservative
969 * check that the name will not be subject to expansion.
970 */
971
972 STATIC void
973 prehash(n)
974 union node *n;
975 {
976 struct cmdentry entry;
977
978 if (n->type == NCMD && n->ncmd.args)
979 if (goodname(n->ncmd.args->narg.text))
980 find_command(n->ncmd.args->narg.text, &entry, 0,
981 pathval());
982 }
983
984
985
986 /*
987 * Builtin commands. Builtin commands whose functions are closely
988 * tied to evaluation are implemented here.
989 */
990
991 /*
992 * No command given, or a bltin command with no arguments. Set the
993 * specified variables.
994 */
995
996 int
997 bltincmd(argc, argv)
998 int argc;
999 char **argv;
1000 {
1001 listsetvar(cmdenviron);
1002 /*
1003 * Preserve exitstatus of a previous possible redirection
1004 * as POSIX mandates
1005 */
1006 return exitstatus;
1007 }
1008
1009
1010 /*
1011 * Handle break and continue commands. Break, continue, and return are
1012 * all handled by setting the evalskip flag. The evaluation routines
1013 * above all check this flag, and if it is set they start skipping
1014 * commands rather than executing them. The variable skipcount is
1015 * the number of loops to break/continue, or the number of function
1016 * levels to return. (The latter is always 1.) It should probably
1017 * be an error to break out of more loops than exist, but it isn't
1018 * in the standard shell so we don't make it one here.
1019 */
1020
1021 int
1022 breakcmd(argc, argv)
1023 int argc;
1024 char **argv;
1025 {
1026 int n = argc > 1 ? number(argv[1]) : 1;
1027
1028 if (n > loopnest)
1029 n = loopnest;
1030 if (n > 0) {
1031 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1032 skipcount = n;
1033 }
1034 return 0;
1035 }
1036
1037
1038 /*
1039 * The return command.
1040 */
1041
1042 int
1043 returncmd(argc, argv)
1044 int argc;
1045 char **argv;
1046 {
1047 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1048
1049 if (funcnest) {
1050 evalskip = SKIPFUNC;
1051 skipcount = 1;
1052 return ret;
1053 }
1054 else {
1055 /* Do what ksh does; skip the rest of the file */
1056 evalskip = SKIPFILE;
1057 skipcount = 1;
1058 return ret;
1059 }
1060 }
1061
1062
1063 int
1064 falsecmd(argc, argv)
1065 int argc;
1066 char **argv;
1067 {
1068 return 1;
1069 }
1070
1071
1072 int
1073 truecmd(argc, argv)
1074 int argc;
1075 char **argv;
1076 {
1077 return 0;
1078 }
1079
1080
1081 int
1082 execcmd(argc, argv)
1083 int argc;
1084 char **argv;
1085 {
1086 if (argc > 1) {
1087 struct strlist *sp;
1088
1089 iflag = 0; /* exit on error */
1090 mflag = 0;
1091 optschanged();
1092 for (sp = cmdenviron; sp ; sp = sp->next)
1093 setvareq(sp->text, VEXPORT|VSTACK);
1094 shellexec(argv + 1, environment(), pathval(), 0, 0);
1095 }
1096 return 0;
1097 }
1098