eval.c revision 1.153.2.7 1 /* $NetBSD: eval.c,v 1.153.2.7 2019/01/18 08:48:24 pgoyette 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
39 #else
40 __RCSID("$NetBSD: eval.c,v 1.153.2.7 2019/01/18 08:48:24 pgoyette Exp $");
41 #endif
42 #endif /* not lint */
43
44 #include <stdbool.h>
45 #include <stdlib.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <limits.h>
51 #include <unistd.h>
52 #include <sys/fcntl.h>
53 #include <sys/stat.h>
54 #include <sys/times.h>
55 #include <sys/param.h>
56 #include <sys/types.h>
57 #include <sys/wait.h>
58 #include <sys/sysctl.h>
59
60 /*
61 * Evaluate a command.
62 */
63
64 #include "shell.h"
65 #include "nodes.h"
66 #include "syntax.h"
67 #include "expand.h"
68 #include "parser.h"
69 #include "jobs.h"
70 #include "eval.h"
71 #include "builtins.h"
72 #include "options.h"
73 #include "exec.h"
74 #include "redir.h"
75 #include "input.h"
76 #include "output.h"
77 #include "trap.h"
78 #include "var.h"
79 #include "memalloc.h"
80 #include "error.h"
81 #include "show.h"
82 #include "mystring.h"
83 #include "main.h"
84 #ifndef SMALL
85 #include "nodenames.h"
86 #include "myhistedit.h"
87 #endif
88
89
90 STATIC struct skipsave s_k_i_p;
91 #define evalskip (s_k_i_p.state)
92 #define skipcount (s_k_i_p.count)
93
94 STATIC int loopnest; /* current loop nesting level */
95 STATIC int funcnest; /* depth of function calls */
96 STATIC int builtin_flags; /* evalcommand flags for builtins */
97 /*
98 * Base function nesting level inside a dot command. Set to 0 initially
99 * and to (funcnest + 1) before every dot command to enable
100 * 1) detection of being in a file sourced by a dot command and
101 * 2) counting of function nesting in that file for the implementation
102 * of the return command.
103 * The value is reset to its previous value after the dot command.
104 */
105 STATIC int dot_funcnest;
106
107
108 const char *commandname;
109 struct strlist *cmdenviron;
110 int exitstatus; /* exit status of last command */
111 int back_exitstatus; /* exit status of backquoted command */
112
113
114 STATIC void evalloop(union node *, int);
115 STATIC void evalfor(union node *, int);
116 STATIC void evalcase(union node *, int);
117 STATIC void evalsubshell(union node *, int);
118 STATIC void expredir(union node *);
119 STATIC void evalredir(union node *, int);
120 STATIC void evalpipe(union node *);
121 STATIC void evalcommand(union node *, int, struct backcmd *);
122 STATIC void prehash(union node *);
123
124 STATIC char *find_dot_file(char *);
125
126 /*
127 * Called to reset things after an exception.
128 */
129
130 #ifdef mkinit
131 INCLUDE "eval.h"
132
133 RESET {
134 reset_eval();
135 }
136
137 SHELLPROC {
138 exitstatus = 0;
139 }
140 #endif
141
142 void
143 reset_eval(void)
144 {
145 evalskip = SKIPNONE;
146 dot_funcnest = 0;
147 loopnest = 0;
148 funcnest = 0;
149 }
150
151 static int
152 sh_pipe(int fds[2])
153 {
154 int nfd;
155
156 if (pipe(fds))
157 return -1;
158
159 if (fds[0] < 3) {
160 nfd = fcntl(fds[0], F_DUPFD, 3);
161 if (nfd != -1) {
162 close(fds[0]);
163 fds[0] = nfd;
164 }
165 }
166
167 if (fds[1] < 3) {
168 nfd = fcntl(fds[1], F_DUPFD, 3);
169 if (nfd != -1) {
170 close(fds[1]);
171 fds[1] = nfd;
172 }
173 }
174 return 0;
175 }
176
177
178 /*
179 * The eval commmand.
180 */
181
182 int
183 evalcmd(int argc, char **argv)
184 {
185 char *p;
186 char *concat;
187 char **ap;
188
189 if (argc > 1) {
190 p = argv[1];
191 if (argc > 2) {
192 STARTSTACKSTR(concat);
193 ap = argv + 2;
194 for (;;) {
195 while (*p)
196 STPUTC(*p++, concat);
197 if ((p = *ap++) == NULL)
198 break;
199 STPUTC(' ', concat);
200 }
201 STPUTC('\0', concat);
202 p = grabstackstr(concat);
203 }
204 evalstring(p, builtin_flags & EV_TESTED);
205 } else
206 exitstatus = 0;
207 return exitstatus;
208 }
209
210
211 /*
212 * Execute a command or commands contained in a string.
213 */
214
215 void
216 evalstring(char *s, int flag)
217 {
218 union node *n;
219 struct stackmark smark;
220 int last;
221 int any;
222
223 last = flag & EV_EXIT;
224 flag &= ~EV_EXIT;
225
226 setstackmark(&smark);
227 setinputstring(s, 1, line_number);
228
229 any = 0; /* to determine if exitstatus will have been set */
230 while ((n = parsecmd(0)) != NEOF) {
231 XTRACE(DBG_EVAL, ("evalstring: "), showtree(n));
232 if (n && nflag == 0) {
233 if (last && at_eof())
234 evaltree(n, flag | EV_EXIT);
235 else
236 evaltree(n, flag);
237 any = 1;
238 if (evalskip)
239 break;
240 }
241 rststackmark(&smark);
242 }
243 popfile();
244 popstackmark(&smark);
245 if (!any)
246 exitstatus = 0;
247 if (last)
248 exraise(EXEXIT);
249 }
250
251
252
253 /*
254 * Evaluate a parse tree. The value is left in the global variable
255 * exitstatus.
256 */
257
258 void
259 evaltree(union node *n, int flags)
260 {
261 bool do_etest;
262 int sflags = flags & ~EV_EXIT;
263 union node *next;
264 struct stackmark smark;
265
266 do_etest = false;
267 if (n == NULL || nflag) {
268 VTRACE(DBG_EVAL, ("evaltree(%s) called\n",
269 n == NULL ? "NULL" : "-n"));
270 if (nflag == 0)
271 exitstatus = 0;
272 goto out2;
273 }
274
275 setstackmark(&smark);
276 do {
277 #ifndef SMALL
278 displayhist = 1; /* show history substitutions done with fc */
279 #endif
280 next = NULL;
281 CTRACE(DBG_EVAL, ("pid %d, evaltree(%p: %s(%d), %#x) called\n",
282 getpid(), n, NODETYPENAME(n->type), n->type, flags));
283 if (n->type != NCMD && traps_invalid)
284 free_traps();
285 switch (n->type) {
286 case NSEMI:
287 evaltree(n->nbinary.ch1, sflags);
288 if (nflag || evalskip)
289 goto out1;
290 next = n->nbinary.ch2;
291 break;
292 case NAND:
293 evaltree(n->nbinary.ch1, EV_TESTED);
294 if (nflag || evalskip || exitstatus != 0)
295 goto out1;
296 next = n->nbinary.ch2;
297 break;
298 case NOR:
299 evaltree(n->nbinary.ch1, EV_TESTED);
300 if (nflag || evalskip || exitstatus == 0)
301 goto out1;
302 next = n->nbinary.ch2;
303 break;
304 case NREDIR:
305 evalredir(n, flags);
306 break;
307 case NSUBSHELL:
308 evalsubshell(n, flags);
309 do_etest = !(flags & EV_TESTED);
310 break;
311 case NBACKGND:
312 evalsubshell(n, flags);
313 break;
314 case NIF: {
315 evaltree(n->nif.test, EV_TESTED);
316 if (nflag || evalskip)
317 goto out1;
318 if (exitstatus == 0)
319 next = n->nif.ifpart;
320 else if (n->nif.elsepart)
321 next = n->nif.elsepart;
322 else
323 exitstatus = 0;
324 break;
325 }
326 case NWHILE:
327 case NUNTIL:
328 evalloop(n, sflags);
329 break;
330 case NFOR:
331 evalfor(n, sflags);
332 break;
333 case NCASE:
334 evalcase(n, sflags);
335 break;
336 case NDEFUN:
337 CTRACE(DBG_EVAL, ("Defining fn %s @%d%s\n",
338 n->narg.text, n->narg.lineno,
339 fnline1 ? " LINENO=1" : ""));
340 defun(n->narg.text, n->narg.next, n->narg.lineno);
341 exitstatus = 0;
342 break;
343 case NNOT:
344 evaltree(n->nnot.com, EV_TESTED);
345 exitstatus = !exitstatus;
346 break;
347 case NDNOT:
348 evaltree(n->nnot.com, EV_TESTED);
349 if (exitstatus != 0)
350 exitstatus = 1;
351 break;
352 case NPIPE:
353 evalpipe(n);
354 do_etest = !(flags & EV_TESTED);
355 break;
356 case NCMD:
357 evalcommand(n, flags, NULL);
358 do_etest = !(flags & EV_TESTED);
359 break;
360 default:
361 #ifdef NODETYPENAME
362 out1fmt("Node type = %d(%s)\n",
363 n->type, NODETYPENAME(n->type));
364 #else
365 out1fmt("Node type = %d\n", n->type);
366 #endif
367 flushout(&output);
368 break;
369 }
370 n = next;
371 rststackmark(&smark);
372 } while(n != NULL);
373 out1:
374 popstackmark(&smark);
375 out2:
376 if (pendingsigs)
377 dotrap();
378 if (eflag && exitstatus != 0 && do_etest)
379 exitshell(exitstatus);
380 if (flags & EV_EXIT)
381 exraise(EXEXIT);
382 }
383
384
385 STATIC void
386 evalloop(union node *n, int flags)
387 {
388 int status;
389
390 loopnest++;
391 status = 0;
392
393 CTRACE(DBG_EVAL, ("evalloop %s:", NODETYPENAME(n->type)));
394 VXTRACE(DBG_EVAL, (" "), showtree(n->nbinary.ch1));
395 VXTRACE(DBG_EVAL, ("evalloop do: "), showtree(n->nbinary.ch2));
396 VTRACE(DBG_EVAL, ("evalloop done\n"));
397 CTRACE(DBG_EVAL, ("\n"));
398
399 for (;;) {
400 evaltree(n->nbinary.ch1, EV_TESTED);
401 if (nflag)
402 break;
403 if (evalskip) {
404 skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
405 evalskip = SKIPNONE;
406 continue;
407 }
408 if (evalskip == SKIPBREAK && --skipcount <= 0)
409 evalskip = SKIPNONE;
410 break;
411 }
412 if (n->type == NWHILE) {
413 if (exitstatus != 0)
414 break;
415 } else {
416 if (exitstatus == 0)
417 break;
418 }
419 evaltree(n->nbinary.ch2, flags & EV_TESTED);
420 status = exitstatus;
421 if (evalskip)
422 goto skipping;
423 }
424 loopnest--;
425 exitstatus = status;
426 }
427
428
429
430 STATIC void
431 evalfor(union node *n, int flags)
432 {
433 struct arglist arglist;
434 union node *argp;
435 struct strlist *sp;
436 struct stackmark smark;
437 int status;
438
439 status = nflag ? exitstatus : 0;
440
441 setstackmark(&smark);
442 arglist.lastp = &arglist.list;
443 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
444 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
445 if (evalskip)
446 goto out;
447 }
448 *arglist.lastp = NULL;
449
450 loopnest++;
451 for (sp = arglist.list ; sp ; sp = sp->next) {
452 if (xflag) {
453 outxstr(expandstr(ps4val(), line_number));
454 outxstr("for ");
455 outxstr(n->nfor.var);
456 outxc('=');
457 outxshstr(sp->text);
458 outxc('\n');
459 flushout(outx);
460 }
461
462 setvar(n->nfor.var, sp->text, 0);
463 evaltree(n->nfor.body, flags & EV_TESTED);
464 status = exitstatus;
465 if (nflag)
466 break;
467 if (evalskip) {
468 if (evalskip == SKIPCONT && --skipcount <= 0) {
469 evalskip = SKIPNONE;
470 continue;
471 }
472 if (evalskip == SKIPBREAK && --skipcount <= 0)
473 evalskip = SKIPNONE;
474 break;
475 }
476 }
477 loopnest--;
478 exitstatus = status;
479 out:
480 popstackmark(&smark);
481 }
482
483
484
485 STATIC void
486 evalcase(union node *n, int flags)
487 {
488 union node *cp, *ncp;
489 union node *patp;
490 struct arglist arglist;
491 struct stackmark smark;
492 int status = 0;
493
494 setstackmark(&smark);
495 arglist.lastp = &arglist.list;
496 line_number = n->ncase.lineno;
497 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
498 for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
499 for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
500 line_number = patp->narg.lineno;
501 if (casematch(patp, arglist.list->text)) {
502 while (cp != NULL && evalskip == 0 &&
503 nflag == 0) {
504 if (cp->type == NCLISTCONT)
505 ncp = cp->nclist.next;
506 else
507 ncp = NULL;
508 line_number = cp->nclist.lineno;
509 evaltree(cp->nclist.body, flags);
510 status = exitstatus;
511 cp = ncp;
512 }
513 goto out;
514 }
515 }
516 }
517 out:
518 exitstatus = status;
519 popstackmark(&smark);
520 }
521
522
523
524 /*
525 * Kick off a subshell to evaluate a tree.
526 */
527
528 STATIC void
529 evalsubshell(union node *n, int flags)
530 {
531 struct job *jp= NULL;
532 int backgnd = (n->type == NBACKGND);
533
534 expredir(n->nredir.redirect);
535 if (xflag && n->nredir.redirect) {
536 union node *rn;
537
538 outxstr(expandstr(ps4val(), line_number));
539 outxstr("using redirections:");
540 for (rn = n->nredir.redirect; rn; rn = rn->nfile.next)
541 (void) outredir(outx, rn, ' ');
542 outxstr(" do subshell ("/*)*/);
543 if (backgnd)
544 outxstr(/*(*/") &");
545 outxc('\n');
546 flushout(outx);
547 }
548 if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
549 forkshell(jp = makejob(n, 1), n, backgnd?FORK_BG:FORK_FG) == 0) {
550 INTON;
551 if (backgnd)
552 flags &=~ EV_TESTED;
553 redirect(n->nredir.redirect, REDIR_KEEP);
554 evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
555 } else if (!backgnd) {
556 INTOFF;
557 exitstatus = waitforjob(jp);
558 INTON;
559 } else
560 exitstatus = 0;
561
562 if (!backgnd && xflag && n->nredir.redirect) {
563 outxstr(expandstr(ps4val(), line_number));
564 outxstr(/*(*/") done subshell\n");
565 flushout(outx);
566 }
567 }
568
569
570
571 /*
572 * Compute the names of the files in a redirection list.
573 */
574
575 STATIC void
576 expredir(union node *n)
577 {
578 union node *redir;
579
580 for (redir = n ; redir ; redir = redir->nfile.next) {
581 struct arglist fn;
582
583 fn.lastp = &fn.list;
584 switch (redir->type) {
585 case NFROMTO:
586 case NFROM:
587 case NTO:
588 case NCLOBBER:
589 case NAPPEND:
590 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
591 redir->nfile.expfname = fn.list->text;
592 break;
593 case NFROMFD:
594 case NTOFD:
595 if (redir->ndup.vname) {
596 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
597 fixredir(redir, fn.list->text, 1);
598 }
599 break;
600 }
601 }
602 }
603
604 /*
605 * Perform redirections for a compound command, and then do it (and restore)
606 */
607 STATIC void
608 evalredir(union node *n, int flags)
609 {
610 struct jmploc jmploc;
611 struct jmploc * const savehandler = handler;
612 volatile int in_redirect = 1;
613 const char * volatile PS4 = NULL;
614
615 expredir(n->nredir.redirect);
616
617 if (xflag && n->nredir.redirect) {
618 union node *rn;
619
620 outxstr(PS4 = expandstr(ps4val(), line_number));
621 outxstr("using redirections:");
622 for (rn = n->nredir.redirect; rn != NULL; rn = rn->nfile.next)
623 (void) outredir(outx, rn, ' ');
624 outxstr(" do {\n"); /* } */
625 flushout(outx);
626 }
627
628 if (setjmp(jmploc.loc)) {
629 int e;
630
631 handler = savehandler;
632 e = exception;
633 popredir();
634 if (PS4 != NULL) {
635 outxstr(PS4);
636 /* { */ outxstr("} failed\n");
637 flushout(outx);
638 }
639 if (e == EXERROR || e == EXEXEC) {
640 if (in_redirect) {
641 exitstatus = 2;
642 return;
643 }
644 }
645 longjmp(handler->loc, 1);
646 } else {
647 INTOFF;
648 handler = &jmploc;
649 redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP);
650 in_redirect = 0;
651 INTON;
652 evaltree(n->nredir.n, flags);
653 }
654 INTOFF;
655 handler = savehandler;
656 popredir();
657 INTON;
658
659 if (PS4 != NULL) {
660 outxstr(PS4);
661 /* { */ outxstr("} done\n");
662 flushout(outx);
663 }
664 }
665
666
667 /*
668 * Evaluate a pipeline. All the processes in the pipeline are children
669 * of the process creating the pipeline. (This differs from some versions
670 * of the shell, which make the last process in a pipeline the parent
671 * of all the rest.)
672 */
673
674 STATIC void
675 evalpipe(union node *n)
676 {
677 struct job *jp;
678 struct nodelist *lp;
679 int pipelen;
680 int prevfd;
681 int pip[2];
682
683 CTRACE(DBG_EVAL, ("evalpipe(%p) called\n", n));
684 pipelen = 0;
685 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
686 pipelen++;
687 INTOFF;
688 jp = makejob(n, pipelen);
689 prevfd = -1;
690 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
691 prehash(lp->n);
692 pip[1] = -1;
693 if (lp->next) {
694 if (sh_pipe(pip) < 0) {
695 if (prevfd >= 0)
696 close(prevfd);
697 error("Pipe call failed: %s", strerror(errno));
698 }
699 }
700 if (forkshell(jp, lp->n,
701 n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
702 INTON;
703 if (prevfd > 0)
704 movefd(prevfd, 0);
705 if (pip[1] >= 0) {
706 close(pip[0]);
707 movefd(pip[1], 1);
708 }
709 evaltree(lp->n, EV_EXIT);
710 }
711 if (prevfd >= 0)
712 close(prevfd);
713 prevfd = pip[0];
714 close(pip[1]);
715 }
716 if (n->npipe.backgnd == 0) {
717 INTOFF;
718 exitstatus = waitforjob(jp);
719 CTRACE(DBG_EVAL, ("evalpipe: job done exit status %d\n",
720 exitstatus));
721 INTON;
722 } else
723 exitstatus = 0;
724 INTON;
725 }
726
727
728
729 /*
730 * Execute a command inside back quotes. If it's a builtin command, we
731 * want to save its output in a block obtained from malloc. Otherwise
732 * we fork off a subprocess and get the output of the command via a pipe.
733 * Should be called with interrupts off.
734 */
735
736 void
737 evalbackcmd(union node *n, struct backcmd *result)
738 {
739 int pip[2];
740 struct job *jp;
741 struct stackmark smark; /* unnecessary (because we fork) */
742
743 result->fd = -1;
744 result->buf = NULL;
745 result->nleft = 0;
746 result->jp = NULL;
747
748 if (nflag || n == NULL)
749 goto out;
750
751 setstackmark(&smark);
752
753 #ifdef notyet
754 /*
755 * For now we disable executing builtins in the same
756 * context as the shell, because we are not keeping
757 * enough state to recover from changes that are
758 * supposed only to affect subshells. eg. echo "`cd /`"
759 */
760 if (n->type == NCMD) {
761 exitstatus = oexitstatus; /* XXX o... no longer exists */
762 evalcommand(n, EV_BACKCMD, result);
763 } else
764 #endif
765 {
766 INTOFF;
767 if (sh_pipe(pip) < 0)
768 error("Pipe call failed");
769 jp = makejob(n, 1);
770 if (forkshell(jp, n, FORK_NOJOB) == 0) {
771 FORCEINTON;
772 close(pip[0]);
773 movefd(pip[1], 1);
774 eflag = 0;
775 evaltree(n, EV_EXIT);
776 /* NOTREACHED */
777 }
778 close(pip[1]);
779 result->fd = pip[0];
780 result->jp = jp;
781 INTON;
782 }
783 popstackmark(&smark);
784 out:
785 CTRACE(DBG_EVAL, ("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
786 result->fd, result->buf, result->nleft, result->jp));
787 }
788
789 const char *
790 syspath(void)
791 {
792 static char *sys_path = NULL;
793 static int mib[] = {CTL_USER, USER_CS_PATH};
794 static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
795 size_t len;
796
797 if (sys_path == NULL) {
798 if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
799 (sys_path = ckmalloc(len + 5)) != NULL &&
800 sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
801 memcpy(sys_path, "PATH=", 5);
802 } else {
803 ckfree(sys_path);
804 /* something to keep things happy */
805 sys_path = def_path;
806 }
807 }
808 return sys_path;
809 }
810
811 static int
812 parse_command_args(int argc, char **argv, int *use_syspath)
813 {
814 int sv_argc = argc;
815 char *cp, c;
816
817 *use_syspath = 0;
818
819 for (;;) {
820 argv++;
821 if (--argc == 0)
822 break;
823 cp = *argv;
824 if (*cp++ != '-')
825 break;
826 if (*cp == '-' && cp[1] == 0) {
827 argv++;
828 argc--;
829 break;
830 }
831 while ((c = *cp++)) {
832 switch (c) {
833 case 'p':
834 *use_syspath = 1;
835 break;
836 default:
837 /* run 'typecmd' for other options */
838 return 0;
839 }
840 }
841 }
842 return sv_argc - argc;
843 }
844
845 int vforked = 0;
846 extern char *trap[];
847
848 /*
849 * Execute a simple command.
850 */
851
852 STATIC void
853 evalcommand(union node *cmd, int flgs, struct backcmd *backcmd)
854 {
855 struct stackmark smark;
856 union node *argp;
857 struct arglist arglist;
858 struct arglist varlist;
859 volatile int flags = flgs;
860 char ** volatile argv;
861 volatile int argc;
862 char **envp;
863 int varflag;
864 struct strlist *sp;
865 volatile int mode;
866 int pip[2];
867 struct cmdentry cmdentry;
868 struct job * volatile jp;
869 struct jmploc jmploc;
870 struct jmploc *volatile savehandler = NULL;
871 const char *volatile savecmdname;
872 volatile struct shparam saveparam;
873 struct localvar *volatile savelocalvars;
874 struct parsefile *volatile savetopfile;
875 volatile int e;
876 char * volatile lastarg;
877 const char * volatile path = pathval();
878 volatile int temp_path;
879 const int savefuncline = funclinebase;
880 const int savefuncabs = funclineabs;
881 volatile int cmd_flags = 0;
882
883 vforked = 0;
884 /* First expand the arguments. */
885 CTRACE(DBG_EVAL, ("evalcommand(%p, %d) called [%s]\n", cmd, flags,
886 cmd->ncmd.args ? cmd->ncmd.args->narg.text : ""));
887 setstackmark(&smark);
888 back_exitstatus = 0;
889
890 line_number = cmd->ncmd.lineno;
891
892 arglist.lastp = &arglist.list;
893 varflag = 1;
894 /* Expand arguments, ignoring the initial 'name=value' ones */
895 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
896 if (varflag && isassignment(argp->narg.text))
897 continue;
898 varflag = 0;
899 line_number = argp->narg.lineno;
900 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
901 }
902 *arglist.lastp = NULL;
903
904 expredir(cmd->ncmd.redirect);
905
906 /* Now do the initial 'name=value' ones we skipped above */
907 varlist.lastp = &varlist.list;
908 for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
909 line_number = argp->narg.lineno;
910 if (!isassignment(argp->narg.text))
911 break;
912 expandarg(argp, &varlist, EXP_VARTILDE);
913 }
914 *varlist.lastp = NULL;
915
916 argc = 0;
917 for (sp = arglist.list ; sp ; sp = sp->next)
918 argc++;
919 argv = stalloc(sizeof (char *) * (argc + 1));
920
921 for (sp = arglist.list ; sp ; sp = sp->next) {
922 VTRACE(DBG_EVAL, ("evalcommand arg: %s\n", sp->text));
923 *argv++ = sp->text;
924 }
925 *argv = NULL;
926 lastarg = NULL;
927 if (iflag && funcnest == 0 && argc > 0)
928 lastarg = argv[-1];
929 argv -= argc;
930
931 /* Print the command if xflag is set. */
932 if (xflag) {
933 char sep = 0;
934 union node *rn;
935
936 outxstr(expandstr(ps4val(), line_number));
937 for (sp = varlist.list ; sp ; sp = sp->next) {
938 char *p;
939
940 if (sep != 0)
941 outxc(sep);
942
943 /*
944 * The "var=" part should not be quoted, regardless
945 * of the value, or it would not represent an
946 * assignment, but rather a command
947 */
948 p = strchr(sp->text, '=');
949 if (p != NULL) {
950 *p = '\0'; /*XXX*/
951 outxshstr(sp->text);
952 outxc('=');
953 *p++ = '='; /*XXX*/
954 } else
955 p = sp->text;
956 outxshstr(p);
957 sep = ' ';
958 }
959 for (sp = arglist.list ; sp ; sp = sp->next) {
960 if (sep != 0)
961 outxc(sep);
962 outxshstr(sp->text);
963 sep = ' ';
964 }
965 for (rn = cmd->ncmd.redirect; rn; rn = rn->nfile.next)
966 if (outredir(outx, rn, sep))
967 sep = ' ';
968 outxc('\n');
969 flushout(outx);
970 }
971
972 /* Now locate the command. */
973 if (argc == 0) {
974 /*
975 * the empty command begins as a normal builtin, and
976 * remains that way while redirects are processed, then
977 * will become special before we get to doing the
978 * var assigns.
979 */
980 cmdentry.cmdtype = CMDBUILTIN;
981 cmdentry.u.bltin = bltincmd;
982 } else {
983 static const char PATH[] = "PATH=";
984
985 /*
986 * Modify the command lookup path, if a PATH= assignment
987 * is present
988 */
989 for (sp = varlist.list; sp; sp = sp->next)
990 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
991 path = sp->text + sizeof(PATH) - 1;
992
993 do {
994 int argsused, use_syspath;
995
996 find_command(argv[0], &cmdentry, cmd_flags, path);
997 #if 0
998 /*
999 * This short circuits all of the processing that
1000 * should be done (including processing the
1001 * redirects), so just don't ...
1002 *
1003 * (eventually this whole #if'd block will vanish)
1004 */
1005 if (cmdentry.cmdtype == CMDUNKNOWN) {
1006 exitstatus = 127;
1007 flushout(&errout);
1008 goto out;
1009 }
1010 #endif
1011
1012 /* implement the 'command' builtin here */
1013 if (cmdentry.cmdtype != CMDBUILTIN ||
1014 cmdentry.u.bltin != bltincmd)
1015 break;
1016 cmd_flags |= DO_NOFUNC;
1017 argsused = parse_command_args(argc, argv, &use_syspath);
1018 if (argsused == 0) {
1019 /* use 'type' builtin to display info */
1020 cmdentry.u.bltin = typecmd;
1021 break;
1022 }
1023 argc -= argsused;
1024 argv += argsused;
1025 if (use_syspath)
1026 path = syspath() + 5;
1027 } while (argc != 0);
1028 if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
1029 /* posix mandates that 'command <splbltin>' act as if
1030 <splbltin> was a normal builtin */
1031 cmdentry.cmdtype = CMDBUILTIN;
1032 }
1033
1034 if (traps_invalid && cmdentry.cmdtype != CMDSPLBLTIN)
1035 free_traps();
1036
1037 /* Fork off a child process if necessary. */
1038 if (cmd->ncmd.backgnd || (have_traps() && (flags & EV_EXIT) != 0)
1039 || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN)
1040 && (flags & EV_EXIT) == 0)
1041 || ((flags & EV_BACKCMD) != 0 &&
1042 ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN)
1043 || cmdentry.u.bltin == dotcmd
1044 || cmdentry.u.bltin == evalcmd))) {
1045 INTOFF;
1046 jp = makejob(cmd, 1);
1047 mode = cmd->ncmd.backgnd;
1048 if (flags & EV_BACKCMD) {
1049 mode = FORK_NOJOB;
1050 if (sh_pipe(pip) < 0)
1051 error("Pipe call failed");
1052 }
1053 #ifdef DO_SHAREDVFORK
1054 /* It is essential that if DO_SHAREDVFORK is defined that the
1055 * child's address space is actually shared with the parent as
1056 * we rely on this.
1057 */
1058 if (usefork == 0 && cmdentry.cmdtype == CMDNORMAL) {
1059 pid_t pid;
1060 int serrno;
1061
1062 savelocalvars = localvars;
1063 localvars = NULL;
1064 vforked = 1;
1065 VFORK_BLOCK
1066 switch (pid = vfork()) {
1067 case -1:
1068 serrno = errno;
1069 VTRACE(DBG_EVAL, ("vfork() failed, errno=%d\n",
1070 serrno));
1071 INTON;
1072 error("Cannot vfork (%s)", strerror(serrno));
1073 break;
1074 case 0:
1075 /* Make sure that exceptions only unwind to
1076 * after the vfork(2)
1077 */
1078 SHELL_FORKED();
1079 if (setjmp(jmploc.loc)) {
1080 if (exception == EXSHELLPROC) {
1081 /*
1082 * We can't progress with the
1083 * vfork, so, set vforked = 2
1084 * so the parent knows,
1085 * and _exit();
1086 */
1087 vforked = 2;
1088 _exit(0);
1089 } else {
1090 _exit(exception == EXEXIT ?
1091 exitstatus : exerrno);
1092 }
1093 }
1094 savehandler = handler;
1095 handler = &jmploc;
1096 listmklocal(varlist.list,
1097 VDOEXPORT | VEXPORT | VNOFUNC);
1098 forkchild(jp, cmd, mode, vforked);
1099 break;
1100 default:
1101 VFORK_UNDO();
1102 /* restore from vfork(2) */
1103 handler = savehandler;
1104 poplocalvars();
1105 localvars = savelocalvars;
1106 if (vforked == 2) {
1107 vforked = 0;
1108
1109 (void)waitpid(pid, NULL, 0);
1110 /*
1111 * We need to progress in a
1112 * normal fork fashion
1113 */
1114 goto normal_fork;
1115 }
1116 /*
1117 * Here the child has left home,
1118 * getting on with its life, so
1119 * so must we...
1120 */
1121 vforked = 0;
1122 forkparent(jp, cmd, mode, pid);
1123 goto parent;
1124 }
1125 VFORK_END
1126 } else {
1127 normal_fork:
1128 #endif
1129 if (forkshell(jp, cmd, mode) != 0)
1130 goto parent; /* at end of routine */
1131 flags |= EV_EXIT;
1132 FORCEINTON;
1133 #ifdef DO_SHAREDVFORK
1134 }
1135 #endif
1136 if (flags & EV_BACKCMD) {
1137 if (!vforked) {
1138 FORCEINTON;
1139 }
1140 close(pip[0]);
1141 movefd(pip[1], 1);
1142 }
1143 flags |= EV_EXIT;
1144 }
1145
1146 /* This is the child process if a fork occurred. */
1147 /* Execute the command. */
1148 switch (cmdentry.cmdtype) {
1149 volatile int saved;
1150
1151 case CMDFUNCTION:
1152 VXTRACE(DBG_EVAL, ("Shell function%s: ",vforked?" VF":""),
1153 trargs(argv));
1154 redirect(cmd->ncmd.redirect, saved =
1155 !(flags & EV_EXIT) || have_traps() ? REDIR_PUSH : 0);
1156 saveparam = shellparam;
1157 shellparam.malloc = 0;
1158 shellparam.reset = 1;
1159 shellparam.nparam = argc - 1;
1160 shellparam.p = argv + 1;
1161 shellparam.optnext = NULL;
1162 INTOFF;
1163 savelocalvars = localvars;
1164 localvars = NULL;
1165 reffunc(cmdentry.u.func);
1166 INTON;
1167 if (setjmp(jmploc.loc)) {
1168 if (exception == EXSHELLPROC) {
1169 freeparam((volatile struct shparam *)
1170 &saveparam);
1171 } else {
1172 freeparam(&shellparam);
1173 shellparam = saveparam;
1174 }
1175 if (saved)
1176 popredir();;
1177 unreffunc(cmdentry.u.func);
1178 poplocalvars();
1179 localvars = savelocalvars;
1180 funclinebase = savefuncline;
1181 funclineabs = savefuncabs;
1182 handler = savehandler;
1183 longjmp(handler->loc, 1);
1184 }
1185 savehandler = handler;
1186 handler = &jmploc;
1187 if (cmdentry.u.func) {
1188 if (cmdentry.lno_frel)
1189 funclinebase = cmdentry.lineno - 1;
1190 else
1191 funclinebase = 0;
1192 funclineabs = cmdentry.lineno;
1193
1194 VTRACE(DBG_EVAL,
1195 ("function: node: %d '%s' # %d%s; funclinebase=%d\n",
1196 getfuncnode(cmdentry.u.func)->type,
1197 NODETYPENAME(getfuncnode(cmdentry.u.func)->type),
1198 cmdentry.lineno, cmdentry.lno_frel?" (=1)":"",
1199 funclinebase));
1200 }
1201 listmklocal(varlist.list, VDOEXPORT | VEXPORT);
1202 /* stop shell blowing its stack */
1203 if (++funcnest > 1000)
1204 error("too many nested function calls");
1205 evaltree(getfuncnode(cmdentry.u.func),
1206 flags & (EV_TESTED|EV_EXIT));
1207 funcnest--;
1208 INTOFF;
1209 unreffunc(cmdentry.u.func);
1210 poplocalvars();
1211 localvars = savelocalvars;
1212 funclinebase = savefuncline;
1213 funclineabs = savefuncabs;
1214 freeparam(&shellparam);
1215 shellparam = saveparam;
1216 handler = savehandler;
1217 if (saved)
1218 popredir();
1219 INTON;
1220 if (evalskip == SKIPFUNC) {
1221 evalskip = SKIPNONE;
1222 skipcount = 0;
1223 }
1224 if (flags & EV_EXIT)
1225 exitshell(exitstatus);
1226 break;
1227
1228 case CMDSPLBLTIN:
1229 VTRACE(DBG_EVAL, ("special "));
1230 case CMDBUILTIN:
1231 VXTRACE(DBG_EVAL, ("builtin command [%d]%s: ", argc,
1232 vforked ? " VF" : ""), trargs(argv));
1233 mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
1234 if (flags == EV_BACKCMD) {
1235 memout.nleft = 0;
1236 memout.nextc = memout.buf;
1237 memout.bufsize = 64;
1238 mode |= REDIR_BACKQ;
1239 }
1240 e = -1;
1241 savecmdname = commandname;
1242 savetopfile = getcurrentfile();
1243 savehandler = handler;
1244 temp_path = 0;
1245 if (!setjmp(jmploc.loc)) {
1246 handler = &jmploc;
1247
1248 /*
1249 * We need to ensure the command hash table isn't
1250 * corrupted by temporary PATH assignments.
1251 * However we must ensure the 'local' command works!
1252 */
1253 if (path != pathval() && (cmdentry.u.bltin == hashcmd ||
1254 cmdentry.u.bltin == typecmd)) {
1255 savelocalvars = localvars;
1256 localvars = 0;
1257 temp_path = 1;
1258 mklocal(path - 5 /* PATH= */, 0);
1259 }
1260 redirect(cmd->ncmd.redirect, mode);
1261
1262 /*
1263 * the empty command is regarded as a normal
1264 * builtin for the purposes of redirects, but
1265 * is a special builtin for var assigns.
1266 * (unless we are the "command" command.)
1267 */
1268 if (argc == 0 && !(cmd_flags & DO_NOFUNC))
1269 cmdentry.cmdtype = CMDSPLBLTIN;
1270
1271 /* exec is a special builtin, but needs this list... */
1272 cmdenviron = varlist.list;
1273 /* we must check 'readonly' flag for all builtins */
1274 listsetvar(varlist.list,
1275 cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
1276 commandname = argv[0];
1277 /* initialize nextopt */
1278 argptr = argv + 1;
1279 optptr = NULL;
1280 /* and getopt */
1281 optreset = 1;
1282 optind = 1;
1283 builtin_flags = flags;
1284 exitstatus = cmdentry.u.bltin(argc, argv);
1285 } else {
1286 e = exception;
1287 if (e == EXINT)
1288 exitstatus = SIGINT + 128;
1289 else if (e == EXEXEC)
1290 exitstatus = exerrno;
1291 else if (e != EXEXIT)
1292 exitstatus = 2;
1293 }
1294 handler = savehandler;
1295 flushall();
1296 out1 = &output;
1297 out2 = &errout;
1298 freestdout();
1299 if (temp_path) {
1300 poplocalvars();
1301 localvars = savelocalvars;
1302 }
1303 cmdenviron = NULL;
1304 if (e != EXSHELLPROC) {
1305 commandname = savecmdname;
1306 if (flags & EV_EXIT)
1307 exitshell(exitstatus);
1308 }
1309 if (e != -1) {
1310 if ((e != EXERROR && e != EXEXEC)
1311 || cmdentry.cmdtype == CMDSPLBLTIN)
1312 exraise(e);
1313 popfilesupto(savetopfile);
1314 FORCEINTON;
1315 }
1316 if (cmdentry.u.bltin != execcmd)
1317 popredir();
1318 if (flags == EV_BACKCMD) {
1319 backcmd->buf = memout.buf;
1320 backcmd->nleft = memout.nextc - memout.buf;
1321 memout.buf = NULL;
1322 }
1323 break;
1324
1325 default:
1326 VXTRACE(DBG_EVAL, ("normal command%s: ", vforked?" VF":""),
1327 trargs(argv));
1328 redirect(cmd->ncmd.redirect,
1329 (vforked ? REDIR_VFORK : 0) | REDIR_KEEP);
1330 if (!vforked)
1331 for (sp = varlist.list ; sp ; sp = sp->next)
1332 setvareq(sp->text, VDOEXPORT|VEXPORT|VSTACK);
1333 envp = environment();
1334 shellexec(argv, envp, path, cmdentry.u.index, vforked);
1335 break;
1336 }
1337 goto out;
1338
1339 parent: /* parent process gets here (if we forked) */
1340
1341 exitstatus = 0; /* if not altered just below */
1342 if (mode == FORK_FG) { /* argument to fork */
1343 exitstatus = waitforjob(jp);
1344 } else if (mode == FORK_NOJOB) {
1345 backcmd->fd = pip[0];
1346 close(pip[1]);
1347 backcmd->jp = jp;
1348 }
1349 FORCEINTON;
1350
1351 out:
1352 if (lastarg)
1353 /* implement $_ for whatever use that really is */
1354 (void) setvarsafe("_", lastarg, VNOERROR);
1355 popstackmark(&smark);
1356 }
1357
1358
1359 /*
1360 * Search for a command. This is called before we fork so that the
1361 * location of the command will be available in the parent as well as
1362 * the child. The check for "goodname" is an overly conservative
1363 * check that the name will not be subject to expansion.
1364 */
1365
1366 STATIC void
1367 prehash(union node *n)
1368 {
1369 struct cmdentry entry;
1370
1371 if (n && n->type == NCMD && n->ncmd.args)
1372 if (goodname(n->ncmd.args->narg.text))
1373 find_command(n->ncmd.args->narg.text, &entry, 0,
1374 pathval());
1375 }
1376
1377 int
1378 in_function(void)
1379 {
1380 return funcnest;
1381 }
1382
1383 enum skipstate
1384 current_skipstate(void)
1385 {
1386 return evalskip;
1387 }
1388
1389 void
1390 save_skipstate(struct skipsave *p)
1391 {
1392 *p = s_k_i_p;
1393 }
1394
1395 void
1396 restore_skipstate(const struct skipsave *p)
1397 {
1398 s_k_i_p = *p;
1399 }
1400
1401 void
1402 stop_skipping(void)
1403 {
1404 evalskip = SKIPNONE;
1405 skipcount = 0;
1406 }
1407
1408 /*
1409 * Builtin commands. Builtin commands whose functions are closely
1410 * tied to evaluation are implemented here.
1411 */
1412
1413 /*
1414 * No command given.
1415 */
1416
1417 int
1418 bltincmd(int argc, char **argv)
1419 {
1420 /*
1421 * Preserve exitstatus of a previous possible redirection
1422 * as POSIX mandates
1423 */
1424 return back_exitstatus;
1425 }
1426
1427
1428 /*
1429 * Handle break and continue commands. Break, continue, and return are
1430 * all handled by setting the evalskip flag. The evaluation routines
1431 * above all check this flag, and if it is set they start skipping
1432 * commands rather than executing them. The variable skipcount is
1433 * the number of loops to break/continue, or the number of function
1434 * levels to return. (The latter is always 1.) It should probably
1435 * be an error to break out of more loops than exist, but it isn't
1436 * in the standard shell so we don't make it one here.
1437 */
1438
1439 int
1440 breakcmd(int argc, char **argv)
1441 {
1442 int n = argc > 1 ? number(argv[1]) : 1;
1443
1444 if (n <= 0)
1445 error("invalid count: %d", n);
1446 if (n > loopnest)
1447 n = loopnest;
1448 if (n > 0) {
1449 evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1450 skipcount = n;
1451 }
1452 return 0;
1453 }
1454
1455 int
1456 dotcmd(int argc, char **argv)
1457 {
1458 exitstatus = 0;
1459
1460 if (argc >= 2) { /* That's what SVR2 does */
1461 char *fullname;
1462 /*
1463 * dot_funcnest needs to be 0 when not in a dotcmd, so it
1464 * cannot be restored with (funcnest + 1).
1465 */
1466 int dot_funcnest_old;
1467 struct stackmark smark;
1468
1469 setstackmark(&smark);
1470 fullname = find_dot_file(argv[1]);
1471 setinputfile(fullname, 1);
1472 commandname = fullname;
1473 dot_funcnest_old = dot_funcnest;
1474 dot_funcnest = funcnest + 1;
1475 cmdloop(0);
1476 dot_funcnest = dot_funcnest_old;
1477 popfile();
1478 popstackmark(&smark);
1479 }
1480 return exitstatus;
1481 }
1482
1483 /*
1484 * Take commands from a file. To be compatible we should do a path
1485 * search for the file, which is necessary to find sub-commands.
1486 */
1487
1488 STATIC char *
1489 find_dot_file(char *basename)
1490 {
1491 char *fullname;
1492 const char *path = pathval();
1493 struct stat statb;
1494
1495 /* don't try this for absolute or relative paths */
1496 if (strchr(basename, '/')) {
1497 if (stat(basename, &statb) == 0) {
1498 if (S_ISDIR(statb.st_mode))
1499 error("%s: is a directory", basename);
1500 if (S_ISBLK(statb.st_mode))
1501 error("%s: is a block device", basename);
1502 return basename;
1503 }
1504 } else while ((fullname = padvance(&path, basename, 1)) != NULL) {
1505 if ((stat(fullname, &statb) == 0)) {
1506 /* weird format is to ease future code... */
1507 if (S_ISDIR(statb.st_mode) || S_ISBLK(statb.st_mode))
1508 ;
1509 #if notyet
1510 else if (unreadable()) {
1511 /*
1512 * testing this via st_mode is ugly to get
1513 * correct (and would ignore ACLs).
1514 * better way is just to open the file.
1515 * But doing that here would (currently)
1516 * mean opening the file twice, which
1517 * might not be safe. So, defer this
1518 * test until code is restructures so
1519 * we can return a fd. Then we also
1520 * get to fix the mem leak just below...
1521 */
1522 }
1523 #endif
1524 else {
1525 /*
1526 * Don't bother freeing here, since
1527 * it will be freed by the caller.
1528 * XXX no it won't - a bug for later.
1529 */
1530 return fullname;
1531 }
1532 }
1533 stunalloc(fullname);
1534 }
1535
1536 /* not found in the PATH */
1537 error("%s: not found", basename);
1538 /* NOTREACHED */
1539 }
1540
1541
1542
1543 /*
1544 * The return command.
1545 *
1546 * Quoth the POSIX standard:
1547 * The return utility shall cause the shell to stop executing the current
1548 * function or dot script. If the shell is not currently executing
1549 * a function or dot script, the results are unspecified.
1550 *
1551 * As for the unspecified part, there seems to be no de-facto standard: bash
1552 * ignores the return with a warning, zsh ignores the return in interactive
1553 * mode but seems to liken it to exit in a script. (checked May 2014)
1554 *
1555 * We choose to silently ignore the return. Older versions of this shell
1556 * set evalskip to SKIPFILE causing the shell to (indirectly) exit. This
1557 * had at least the problem of circumventing the check for stopped jobs,
1558 * which would occur for exit or ^D.
1559 */
1560
1561 int
1562 returncmd(int argc, char **argv)
1563 {
1564 int ret = argc > 1 ? number(argv[1]) : exitstatus;
1565
1566 if ((dot_funcnest == 0 && funcnest)
1567 || (dot_funcnest > 0 && funcnest - (dot_funcnest - 1) > 0)) {
1568 evalskip = SKIPFUNC;
1569 skipcount = 1;
1570 } else if (dot_funcnest > 0) {
1571 evalskip = SKIPFILE;
1572 skipcount = 1;
1573 } else {
1574 /* XXX: should a warning be issued? */
1575 ret = 0;
1576 }
1577
1578 return ret;
1579 }
1580
1581
1582 int
1583 falsecmd(int argc, char **argv)
1584 {
1585 return 1;
1586 }
1587
1588
1589 int
1590 truecmd(int argc, char **argv)
1591 {
1592 return 0;
1593 }
1594
1595
1596 int
1597 execcmd(int argc, char **argv)
1598 {
1599 if (argc > 1) {
1600 struct strlist *sp;
1601
1602 iflag = 0; /* exit on error */
1603 mflag = 0;
1604 optschanged();
1605 for (sp = cmdenviron; sp; sp = sp->next)
1606 setvareq(sp->text, VDOEXPORT|VEXPORT|VSTACK);
1607 shellexec(argv + 1, environment(), pathval(), 0, 0);
1608 }
1609 return 0;
1610 }
1611
1612 static int
1613 conv_time(clock_t ticks, char *seconds, size_t l)
1614 {
1615 static clock_t tpm = 0;
1616 clock_t mins;
1617 int i;
1618
1619 if (!tpm)
1620 tpm = sysconf(_SC_CLK_TCK) * 60;
1621
1622 mins = ticks / tpm;
1623 snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
1624
1625 if (seconds[0] == '6' && seconds[1] == '0') {
1626 /* 59.99995 got rounded up... */
1627 mins++;
1628 strlcpy(seconds, "0.0", l);
1629 return mins;
1630 }
1631
1632 /* suppress trailing zeros */
1633 i = strlen(seconds) - 1;
1634 for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
1635 seconds[i] = 0;
1636 return mins;
1637 }
1638
1639 int
1640 timescmd(int argc, char **argv)
1641 {
1642 struct tms tms;
1643 int u, s, cu, cs;
1644 char us[8], ss[8], cus[8], css[8];
1645
1646 nextopt("");
1647
1648 times(&tms);
1649
1650 u = conv_time(tms.tms_utime, us, sizeof(us));
1651 s = conv_time(tms.tms_stime, ss, sizeof(ss));
1652 cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
1653 cs = conv_time(tms.tms_cstime, css, sizeof(css));
1654
1655 outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
1656 u, us, s, ss, cu, cus, cs, css);
1657
1658 return 0;
1659 }
1660