eval.c revision 1.62 1 /* $NetBSD: eval.c,v 1.62 2002/09/27 20:24:36 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
43 #else
44 __RCSID("$NetBSD: eval.c,v 1.62 2002/09/27 20:24:36 christos 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 jp = makejob(n, 1);
414 if (forkshell(jp, n, backgnd) == 0) {
415 if (backgnd)
416 flags &=~ EV_TESTED;
417 redirect(n->nredir.redirect, 0);
418 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
419 }
420 if (! backgnd) {
421 INTOFF;
422 exitstatus = waitforjob(jp);
423 INTON;
424 }
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 if (pipe(pip) < 0)
569 error("Pipe call failed");
570 jp = makejob(n, 1);
571 if (forkshell(jp, n, FORK_NOJOB) == 0) {
572 FORCEINTON;
573 close(pip[0]);
574 if (pip[1] != 1) {
575 close(1);
576 copyfd(pip[1], 1);
577 close(pip[1]);
578 }
579 eflag = 0;
580 evaltree(n, EV_EXIT);
581 }
582 close(pip[1]);
583 result->fd = pip[0];
584 result->jp = jp;
585 }
586 out:
587 popstackmark(&smark);
588 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
589 result->fd, result->buf, result->nleft, result->jp));
590 }
591
592
593 int vforked = 0;
594
595 /*
596 * Execute a simple command.
597 */
598
599 STATIC void
600 evalcommand(cmd, flags, backcmd)
601 union node *cmd;
602 int flags;
603 struct backcmd *backcmd;
604 {
605 struct stackmark smark;
606 union node *argp;
607 struct arglist arglist;
608 struct arglist varlist;
609 char **argv;
610 int argc;
611 char **envp;
612 int varflag;
613 struct strlist *sp;
614 int mode;
615 int pip[2];
616 struct cmdentry cmdentry;
617 struct job *jp;
618 struct jmploc jmploc;
619 struct jmploc *volatile savehandler;
620 char *volatile savecmdname;
621 volatile struct shparam saveparam;
622 struct localvar *volatile savelocalvars;
623 volatile int e;
624 char *lastarg;
625 #if __GNUC__
626 /* Avoid longjmp clobbering */
627 (void) &argv;
628 (void) &argc;
629 (void) &lastarg;
630 (void) &flags;
631 #endif
632
633 vforked = 0;
634 /* First expand the arguments. */
635 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
636 setstackmark(&smark);
637 arglist.lastp = &arglist.list;
638 varlist.lastp = &varlist.list;
639 varflag = 1;
640 oexitstatus = exitstatus;
641 exitstatus = 0;
642 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
643 char *p = argp->narg.text;
644 if (varflag && is_name(*p)) {
645 do {
646 p++;
647 } while (is_in_name(*p));
648 if (*p == '=') {
649 expandarg(argp, &varlist, EXP_VARTILDE);
650 continue;
651 }
652 }
653 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
654 varflag = 0;
655 }
656 *arglist.lastp = NULL;
657 *varlist.lastp = NULL;
658 expredir(cmd->ncmd.redirect);
659 argc = 0;
660 for (sp = arglist.list ; sp ; sp = sp->next)
661 argc++;
662 argv = stalloc(sizeof (char *) * (argc + 1));
663
664 for (sp = arglist.list ; sp ; sp = sp->next) {
665 TRACE(("evalcommand arg: %s\n", sp->text));
666 *argv++ = sp->text;
667 }
668 *argv = NULL;
669 lastarg = NULL;
670 if (iflag && funcnest == 0 && argc > 0)
671 lastarg = argv[-1];
672 argv -= argc;
673
674 /* Print the command if xflag is set. */
675 if (xflag) {
676 outc('+', &errout);
677 for (sp = varlist.list ; sp ; sp = sp->next) {
678 outc(' ', &errout);
679 out2str(sp->text);
680 }
681 for (sp = arglist.list ; sp ; sp = sp->next) {
682 outc(' ', &errout);
683 out2str(sp->text);
684 }
685 outc('\n', &errout);
686 flushout(&errout);
687 }
688
689 /* Now locate the command. */
690 if (argc == 0) {
691 cmdentry.cmdtype = CMDBUILTIN;
692 cmdentry.u.index = BLTINCMD;
693 } else {
694 static const char PATH[] = "PATH=";
695 const char *path = pathval();
696
697 /*
698 * Modify the command lookup path, if a PATH= assignment
699 * is present
700 */
701 for (sp = varlist.list ; sp ; sp = sp->next)
702 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
703 path = sp->text + sizeof(PATH) - 1;
704
705 find_command(argv[0], &cmdentry, DO_ERR, path);
706 if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
707 exitstatus = 127;
708 flushout(&errout);
709 return;
710 }
711 /* implement the bltin builtin here */
712 if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
713 for (;;) {
714 argv++;
715 if (--argc == 0)
716 break;
717 if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
718 outfmt(&errout, "%s: not found\n", *argv);
719 exitstatus = 127;
720 flushout(&errout);
721 return;
722 }
723 if (cmdentry.u.index != BLTINCMD)
724 break;
725 }
726 }
727 }
728
729 /* Fork off a child process if necessary. */
730 if (cmd->ncmd.backgnd
731 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
732 || ((flags & EV_BACKCMD) != 0
733 && (cmdentry.cmdtype != CMDBUILTIN
734 || cmdentry.u.index == DOTCMD
735 || cmdentry.u.index == EVALCMD))) {
736 INTOFF;
737 jp = makejob(cmd, 1);
738 mode = cmd->ncmd.backgnd;
739 if (flags & EV_BACKCMD) {
740 mode = FORK_NOJOB;
741 if (pipe(pip) < 0)
742 error("Pipe call failed");
743 }
744 #ifdef DO_SHAREDVFORK
745 /* It is essential that if DO_SHAREDVFORK is defined that the
746 * child's address space is actually shared with the parent as
747 * we rely on this.
748 */
749 if (cmdentry.cmdtype == CMDNORMAL) {
750 pid_t pid;
751
752 INTOFF;
753 savelocalvars = localvars;
754 localvars = NULL;
755 for (sp = varlist.list ; sp ; sp = sp->next)
756 mklocal(sp->text, VEXPORT);
757 vforked = 1;
758 switch (pid = vfork()) {
759 case -1:
760 TRACE(("Vfork failed, errno=%d", errno));
761 INTON;
762 error("Cannot vfork");
763 break;
764 case 0:
765 /* Make sure that exceptions only unwind to
766 * after the vfork(2)
767 */
768 if (setjmp(jmploc.loc)) {
769 if (exception == EXSHELLPROC) {
770 /* We can't progress with the vfork,
771 * so, set vforked = 2 so the parent
772 * knows, and _exit();
773 */
774 vforked = 2;
775 _exit(0);
776 } else {
777 _exit(exerrno);
778 }
779 }
780 savehandler = handler;
781 handler = &jmploc;
782 forkchild(jp, cmd, mode, vforked);
783 break;
784 default:
785 handler = savehandler; /* restore from vfork(2) */
786 poplocalvars();
787 localvars = savelocalvars;
788 if (vforked == 2) {
789 vforked = 0;
790
791 (void)waitpid(pid, NULL, 0);
792 /* We need to progress in a normal fork fashion */
793 goto normal_fork;
794 }
795 vforked = 0;
796 forkparent(jp, cmd, mode, pid);
797 goto parent;
798 }
799 } else {
800 normal_fork:
801 #endif
802 if (forkshell(jp, cmd, mode) != 0)
803 goto parent; /* at end of routine */
804 INTON;
805 #ifdef DO_SHAREDVFORK
806 }
807 #endif
808 if (flags & EV_BACKCMD) {
809 if (!vforked) {
810 FORCEINTON;
811 }
812 close(pip[0]);
813 if (pip[1] != 1) {
814 close(1);
815 copyfd(pip[1], 1);
816 close(pip[1]);
817 }
818 }
819 flags |= EV_EXIT;
820 }
821
822 /* This is the child process if a fork occurred. */
823 /* Execute the command. */
824 if (cmdentry.cmdtype == CMDFUNCTION) {
825 #ifdef DEBUG
826 trputs("Shell function: "); trargs(argv);
827 #endif
828 redirect(cmd->ncmd.redirect, REDIR_PUSH);
829 saveparam = shellparam;
830 shellparam.malloc = 0;
831 shellparam.reset = 1;
832 shellparam.nparam = argc - 1;
833 shellparam.p = argv + 1;
834 shellparam.optnext = NULL;
835 INTOFF;
836 savelocalvars = localvars;
837 localvars = NULL;
838 INTON;
839 if (setjmp(jmploc.loc)) {
840 if (exception == EXSHELLPROC) {
841 freeparam((volatile struct shparam *)
842 &saveparam);
843 } else {
844 freeparam(&shellparam);
845 shellparam = saveparam;
846 }
847 poplocalvars();
848 localvars = savelocalvars;
849 handler = savehandler;
850 longjmp(handler->loc, 1);
851 }
852 savehandler = handler;
853 handler = &jmploc;
854 for (sp = varlist.list ; sp ; sp = sp->next)
855 mklocal(sp->text, 0);
856 funcnest++;
857 evaltree(cmdentry.u.func, flags & EV_TESTED);
858 funcnest--;
859 INTOFF;
860 poplocalvars();
861 localvars = savelocalvars;
862 freeparam(&shellparam);
863 shellparam = saveparam;
864 handler = savehandler;
865 popredir();
866 INTON;
867 if (evalskip == SKIPFUNC) {
868 evalskip = 0;
869 skipcount = 0;
870 }
871 if (flags & EV_EXIT)
872 exitshell(exitstatus);
873 } else if (cmdentry.cmdtype == CMDBUILTIN) {
874 #ifdef DEBUG
875 trputs("builtin command: "); trargs(argv);
876 #endif
877 mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
878 if (flags == EV_BACKCMD) {
879 memout.nleft = 0;
880 memout.nextc = memout.buf;
881 memout.bufsize = 64;
882 mode |= REDIR_BACKQ;
883 }
884 redirect(cmd->ncmd.redirect, mode);
885 savecmdname = commandname;
886 cmdenviron = varlist.list;
887 e = -1;
888 if (setjmp(jmploc.loc)) {
889 e = exception;
890 exitstatus = (e == EXINT)? SIGINT+128 : 2;
891 goto cmddone;
892 }
893 savehandler = handler;
894 handler = &jmploc;
895 commandname = argv[0];
896 argptr = argv + 1;
897 optptr = NULL; /* initialize nextopt */
898 exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
899 flushall();
900 cmddone:
901 out1 = &output;
902 out2 = &errout;
903 freestdout();
904 cmdenviron = NULL;
905 if (e != EXSHELLPROC) {
906 commandname = savecmdname;
907 if (flags & EV_EXIT)
908 exitshell(exitstatus);
909 }
910 handler = savehandler;
911 if (e != -1) {
912 if ((e != EXERROR && e != EXEXEC)
913 || cmdentry.u.index == BLTINCMD
914 || cmdentry.u.index == DOTCMD
915 || cmdentry.u.index == EVALCMD
916 #ifndef SMALL
917 || cmdentry.u.index == HISTCMD
918 #endif
919 || cmdentry.u.index == EXECCMD)
920 exraise(e);
921 FORCEINTON;
922 }
923 if (cmdentry.u.index != EXECCMD)
924 popredir();
925 if (flags == EV_BACKCMD) {
926 backcmd->buf = memout.buf;
927 backcmd->nleft = memout.nextc - memout.buf;
928 memout.buf = NULL;
929 }
930 } else {
931 #ifdef DEBUG
932 trputs("normal command: "); trargs(argv);
933 #endif
934 clearredir(vforked);
935 redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0);
936 if (!vforked)
937 for (sp = varlist.list ; sp ; sp = sp->next)
938 setvareq(sp->text, VEXPORT|VSTACK);
939 envp = environment();
940 shellexec(argv, envp, pathval(), cmdentry.u.index, vforked);
941 }
942 goto out;
943
944 parent: /* parent process gets here (if we forked) */
945 if (mode == 0) { /* argument to fork */
946 exitstatus = waitforjob(jp);
947 } else if (mode == 2) {
948 backcmd->fd = pip[0];
949 close(pip[1]);
950 backcmd->jp = jp;
951 }
952 INTON;
953
954 out:
955 if (lastarg)
956 setvar("_", lastarg, 0);
957 popstackmark(&smark);
958
959 if (eflag && exitstatus && !(flags & EV_TESTED))
960 exitshell(exitstatus);
961 }
962
963
964 /*
965 * Search for a command. This is called before we fork so that the
966 * location of the command will be available in the parent as well as
967 * the child. The check for "goodname" is an overly conservative
968 * check that the name will not be subject to expansion.
969 */
970
971 STATIC void
972 prehash(n)
973 union node *n;
974 {
975 struct cmdentry entry;
976
977 if (n->type == NCMD && n->ncmd.args)
978 if (goodname(n->ncmd.args->narg.text))
979 find_command(n->ncmd.args->narg.text, &entry, 0,
980 pathval());
981 }
982
983
984
985 /*
986 * Builtin commands. Builtin commands whose functions are closely
987 * tied to evaluation are implemented here.
988 */
989
990 /*
991 * No command given, or a bltin command with no arguments. Set the
992 * specified variables.
993 */
994
995 int
996 bltincmd(argc, argv)
997 int argc;
998 char **argv;
999 {
1000 listsetvar(cmdenviron);
1001 /*
1002 * Preserve exitstatus of a previous possible redirection
1003 * as POSIX mandates
1004 */
1005 return exitstatus;
1006 }
1007
1008
1009 /*
1010 * Handle break and continue commands. Break, continue, and return are
1011 * all handled by setting the evalskip flag. The evaluation routines
1012 * above all check this flag, and if it is set they start skipping
1013 * commands rather than executing them. The variable skipcount is
1014 * the number of loops to break/continue, or the number of function
1015 * levels to return. (The latter is always 1.) It should probably
1016 * be an error to break out of more loops than exist, but it isn't
1017 * in the standard shell so we don't make it one here.
1018 */
1019
1020 int
1021 breakcmd(argc, argv)
1022 int argc;
1023 char **argv;
1024 {
1025 int n = argc > 1 ? number(argv[1]) : 1;
1026
1027 if (n > loopnest)
1028 n = loopnest;
1029 if (n > 0) {
1030 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1031 skipcount = n;
1032 }
1033 return 0;
1034 }
1035
1036
1037 /*
1038 * The return command.
1039 */
1040
1041 int
1042 returncmd(argc, argv)
1043 int argc;
1044 char **argv;
1045 {
1046 int ret = argc > 1 ? number(argv[1]) : oexitstatus;
1047
1048 if (funcnest) {
1049 evalskip = SKIPFUNC;
1050 skipcount = 1;
1051 return ret;
1052 }
1053 else {
1054 /* Do what ksh does; skip the rest of the file */
1055 evalskip = SKIPFILE;
1056 skipcount = 1;
1057 return ret;
1058 }
1059 }
1060
1061
1062 int
1063 falsecmd(argc, argv)
1064 int argc;
1065 char **argv;
1066 {
1067 return 1;
1068 }
1069
1070
1071 int
1072 truecmd(argc, argv)
1073 int argc;
1074 char **argv;
1075 {
1076 return 0;
1077 }
1078
1079
1080 int
1081 execcmd(argc, argv)
1082 int argc;
1083 char **argv;
1084 {
1085 if (argc > 1) {
1086 struct strlist *sp;
1087
1088 iflag = 0; /* exit on error */
1089 mflag = 0;
1090 optschanged();
1091 for (sp = cmdenviron; sp ; sp = sp->next)
1092 setvareq(sp->text, VEXPORT|VSTACK);
1093 shellexec(argv + 1, environment(), pathval(), 0, 0);
1094 }
1095 return 0;
1096 }
1097