VirtualBox

source: kBuild/trunk/src/kash/eval.c@ 3434

Last change on this file since 3434 was 3434, checked in by bird, 4 years ago

kash: refactoring evalcommand - complicated, part I.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 31.6 KB
Line 
1/* $NetBSD: eval.c,v 1.84 2005/06/23 23:05:29 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. 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#if 0
36#ifndef lint
37static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
38#else
39__RCSID("$NetBSD: eval.c,v 1.84 2005/06/23 23:05:29 christos Exp $");
40#endif /* not lint */
41#endif
42
43#include <stdlib.h>
44#include <stdio.h>
45#include <sys/types.h>
46#ifdef HAVE_SYSCTL_H
47# ifdef __OpenBSD__ /* joyful crap */
48# include <sys/param.h>
49# undef psh
50# endif
51# include <sys/sysctl.h>
52#endif
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#include "main.h"
78#ifndef SMALL
79# include "myhistedit.h"
80#endif
81#include "shinstance.h"
82
83
84/* flags in argument to evaltree */
85#define EV_EXIT 01 /* exit after evaluating tree */
86#define EV_TESTED 02 /* exit status is checked; ignore -e flag */
87#define EV_BACKCMD 04 /* command executing within back quotes */
88
89/*int evalskip;*/ /* set if we are skipping commands */
90/*STATIC int skipcount;*/ /* number of levels to skip */
91/*MKINIT int loopnest;*/ /* current loop nesting level */
92/*int funcnest;*/ /* depth of function calls */
93
94
95/*char *commandname;*/
96/*struct strlist *cmdenviron;*/
97/*int exitstatus;*/ /* exit status of last command */
98/*int back_exitstatus;*/ /* exit status of backquoted command */
99
100
101STATIC void evalloop(shinstance *, union node *, int);
102STATIC void evalfor(shinstance *, union node *, int);
103STATIC void evalcase(shinstance *, union node *, int);
104STATIC void evalsubshell(shinstance *, union node *, int);
105STATIC void expredir(shinstance *, union node *);
106STATIC void evalpipe(shinstance *, union node *);
107STATIC void evalcommand(shinstance *, union node *, int, struct backcmd *);
108STATIC void prehash(shinstance *, union node *);
109
110
111/*
112 * Called to reset things after an exception.
113 */
114
115#ifdef mkinit
116INCLUDE "eval.h"
117
118RESET {
119 psh->evalskip = 0;
120 psh->loopnest = 0;
121 psh->funcnest = 0;
122}
123
124SHELLPROC {
125 psh->exitstatus = 0;
126}
127#endif
128
129static int
130sh_pipe(shinstance *psh, int fds[2])
131{
132 int nfd;
133
134 if (shfile_pipe(&psh->fdtab, fds))
135 return -1;
136
137 if (fds[0] < 3) {
138 nfd = shfile_fcntl(&psh->fdtab, fds[0], F_DUPFD, 3);
139 if (nfd != -1) {
140 shfile_close(&psh->fdtab, fds[0]);
141 fds[0] = nfd;
142 }
143 }
144
145 if (fds[1] < 3) {
146 nfd = shfile_fcntl(&psh->fdtab, fds[1], F_DUPFD, 3);
147 if (nfd != -1) {
148 shfile_close(&psh->fdtab, fds[1]);
149 fds[1] = nfd;
150 }
151 }
152 return 0;
153}
154
155
156/*
157 * The eval commmand.
158 */
159
160int
161evalcmd(shinstance *psh, int argc, char **argv)
162{
163 char *p;
164 char *concat;
165 char **ap;
166
167 if (argc > 1) {
168 p = argv[1];
169 if (argc > 2) {
170 STARTSTACKSTR(psh, concat);
171 ap = argv + 2;
172 for (;;) {
173 while (*p)
174 STPUTC(psh, *p++, concat);
175 if ((p = *ap++) == NULL)
176 break;
177 STPUTC(psh, ' ', concat);
178 }
179 STPUTC(psh, '\0', concat);
180 p = grabstackstr(psh, concat);
181 }
182 evalstring(psh, p, EV_TESTED);
183 }
184 return psh->exitstatus;
185}
186
187
188/*
189 * Execute a command or commands contained in a string.
190 */
191
192void
193evalstring(shinstance *psh, char *s, int flag)
194{
195 union node *n;
196 struct stackmark smark;
197
198 setstackmark(psh, &smark);
199 setinputstring(psh, s, 1);
200
201 while ((n = parsecmd(psh, 0)) != NEOF) {
202 evaltree(psh, n, flag);
203 popstackmark(psh, &smark);
204 }
205 popfile(psh);
206 popstackmark(psh, &smark);
207}
208
209
210
211/*
212 * Evaluate a parse tree. The value is left in the global variable
213 * exitstatus.
214 */
215
216void
217evaltree(shinstance *psh, union node *n, int flags)
218{
219 if (n == NULL) {
220 TRACE((psh, "evaltree(NULL) called\n"));
221 psh->exitstatus = 0;
222 goto out;
223 }
224#ifndef SMALL
225 psh->displayhist = 1; /* show history substitutions done with fc */
226#endif
227 TRACE((psh, "pid %d, evaltree(%p: %d, %d) called\n",
228 sh_getpid(psh), n, n->type, flags));
229 switch (n->type) {
230 case NSEMI:
231 evaltree(psh, n->nbinary.ch1, flags & EV_TESTED);
232 if (psh->evalskip)
233 goto out;
234 evaltree(psh, n->nbinary.ch2, flags);
235 break;
236 case NAND:
237 evaltree(psh, n->nbinary.ch1, EV_TESTED);
238 if (psh->evalskip || psh->exitstatus != 0)
239 goto out;
240 evaltree(psh, n->nbinary.ch2, flags);
241 break;
242 case NOR:
243 evaltree(psh, n->nbinary.ch1, EV_TESTED);
244 if (psh->evalskip || psh->exitstatus == 0)
245 goto out;
246 evaltree(psh, n->nbinary.ch2, flags);
247 break;
248 case NREDIR:
249 expredir(psh, n->nredir.redirect);
250 redirect(psh, n->nredir.redirect, REDIR_PUSH);
251 evaltree(psh, n->nredir.n, flags);
252 popredir(psh);
253 break;
254 case NSUBSHELL:
255 evalsubshell(psh, n, flags);
256 break;
257 case NBACKGND:
258 evalsubshell(psh, n, flags);
259 break;
260 case NIF: {
261 evaltree(psh, n->nif.test, EV_TESTED);
262 if (psh->evalskip)
263 goto out;
264 if (psh->exitstatus == 0)
265 evaltree(psh, n->nif.ifpart, flags);
266 else if (n->nif.elsepart)
267 evaltree(psh, n->nif.elsepart, flags);
268 else
269 psh->exitstatus = 0;
270 break;
271 }
272 case NWHILE:
273 case NUNTIL:
274 evalloop(psh, n, flags);
275 break;
276 case NFOR:
277 evalfor(psh, n, flags);
278 break;
279 case NCASE:
280 evalcase(psh, n, flags);
281 break;
282 case NDEFUN:
283 defun(psh, n->narg.text, n->narg.next);
284 psh->exitstatus = 0;
285 break;
286 case NNOT:
287 evaltree(psh, n->nnot.com, EV_TESTED);
288 psh->exitstatus = !psh->exitstatus;
289 break;
290 case NPIPE:
291 evalpipe(psh, n);
292 break;
293 case NCMD:
294 evalcommand(psh, n, flags, (struct backcmd *)NULL);
295 break;
296 default:
297 out1fmt(psh, "Node type = %d\n", n->type);
298 flushout(&psh->output);
299 break;
300 }
301out:
302 if (psh->pendingsigs)
303 dotrap(psh);
304 if ((flags & EV_EXIT) != 0)
305 exitshell(psh, psh->exitstatus);
306}
307
308
309STATIC void
310evalloop(shinstance *psh, union node *n, int flags)
311{
312 int status;
313
314 psh->loopnest++;
315 status = 0;
316 for (;;) {
317 evaltree(psh, n->nbinary.ch1, EV_TESTED);
318 if (psh->evalskip) {
319skipping: if (psh->evalskip == SKIPCONT && --psh->skipcount <= 0) {
320 psh->evalskip = 0;
321 continue;
322 }
323 if (psh->evalskip == SKIPBREAK && --psh->skipcount <= 0)
324 psh->evalskip = 0;
325 break;
326 }
327 if (n->type == NWHILE) {
328 if (psh->exitstatus != 0)
329 break;
330 } else {
331 if (psh->exitstatus == 0)
332 break;
333 }
334 evaltree(psh, n->nbinary.ch2, flags & EV_TESTED);
335 status = psh->exitstatus;
336 if (psh->evalskip)
337 goto skipping;
338 }
339 psh->loopnest--;
340 psh->exitstatus = status;
341}
342
343
344
345STATIC void
346evalfor(shinstance *psh, union node *n, int flags)
347{
348 struct arglist arglist;
349 union node *argp;
350 struct strlist *sp;
351 struct stackmark smark;
352 int status = 0;
353
354 setstackmark(psh, &smark);
355 arglist.lastp = &arglist.list;
356 for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
357 expandarg(psh, argp, &arglist, EXP_FULL | EXP_TILDE);
358 if (psh->evalskip)
359 goto out;
360 }
361 *arglist.lastp = NULL;
362
363 psh->loopnest++;
364 for (sp = arglist.list ; sp ; sp = sp->next) {
365 setvar(psh, n->nfor.var, sp->text, 0);
366 evaltree(psh, n->nfor.body, flags & EV_TESTED);
367 status = psh->exitstatus;
368 if (psh->evalskip) {
369 if (psh->evalskip == SKIPCONT && --psh->skipcount <= 0) {
370 psh->evalskip = 0;
371 continue;
372 }
373 if (psh->evalskip == SKIPBREAK && --psh->skipcount <= 0)
374 psh->evalskip = 0;
375 break;
376 }
377 }
378 psh->loopnest--;
379 psh->exitstatus = status;
380out:
381 popstackmark(psh, &smark);
382}
383
384
385
386STATIC void
387evalcase(shinstance *psh, union node *n, int flags)
388{
389 union node *cp;
390 union node *patp;
391 struct arglist arglist;
392 struct stackmark smark;
393 int status = 0;
394
395 setstackmark(psh, &smark);
396 arglist.lastp = &arglist.list;
397 expandarg(psh, n->ncase.expr, &arglist, EXP_TILDE);
398 for (cp = n->ncase.cases ; cp && psh->evalskip == 0 ; cp = cp->nclist.next) {
399 for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
400 if (casematch(psh, patp, arglist.list->text)) {
401 if (psh->evalskip == 0) {
402 evaltree(psh, cp->nclist.body, flags);
403 status = psh->exitstatus;
404 }
405 goto out;
406 }
407 }
408 }
409out:
410 psh->exitstatus = status;
411 popstackmark(psh, &smark);
412}
413
414
415#ifdef KASH_USE_FORKSHELL2
416/*
417 * Child of evalsubshell.
418 */
419struct evalsubshellchild
420{
421 int flags;
422 int backgnd;
423};
424
425static int evalsubshell_child(shinstance *psh, union node *n, void *argp)
426{
427 struct evalsubshellchild args = *(struct evalsubshellchild *)argp;
428
429 INTON;
430 if (args.backgnd)
431 args.flags &=~ EV_TESTED;
432 redirect(psh, n->nredir.redirect, 0);
433 /* never returns */
434 evaltree(psh, n->nredir.n, args.flags | EV_EXIT);
435 /** @todo make us return here. */
436 return 0;
437}
438#endif /* KASH_USE_FORKSHELL2 */
439
440
441/*
442 * Kick off a subshell to evaluate a tree.
443 */
444
445STATIC void
446evalsubshell(shinstance *psh, union node *n, int flags)
447{
448 struct job *jp;
449 int backgnd = (n->type == NBACKGND);
450
451 expredir(psh, n->nredir.redirect);
452 INTOFF;
453 jp = makejob(psh, n, 1);
454#ifdef KASH_USE_FORKSHELL2
455 {
456 struct evalsubshellchild args;
457 args.flags = flags;
458 args.backgnd = backgnd;
459 forkshell2(psh, jp, n, backgnd ? FORK_BG : FORK_FG,
460 evalsubshell_child, n, &args, sizeof(args));
461 }
462#else
463 if (forkshell(psh, jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
464 INTON;
465 if (backgnd)
466 flags &=~ EV_TESTED;
467 redirect(psh, n->nredir.redirect, 0);
468 /* never returns */
469 evaltree(psh, n->nredir.n, flags | EV_EXIT);
470 }
471#endif
472 if (! backgnd)
473 psh->exitstatus = waitforjob(psh, jp);
474 INTON;
475}
476
477
478
479/*
480 * Compute the names of the files in a redirection list.
481 */
482
483STATIC void
484expredir(shinstance *psh, union node *n)
485{
486 union node *redir;
487
488 for (redir = n ; redir ; redir = redir->nfile.next) {
489 struct arglist fn;
490 fn.lastp = &fn.list;
491 switch (redir->type) {
492 case NFROMTO:
493 case NFROM:
494 case NTO:
495 case NCLOBBER:
496 case NAPPEND:
497 expandarg(psh, redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
498 redir->nfile.expfname = fn.list->text;
499 break;
500 case NFROMFD:
501 case NTOFD:
502 if (redir->ndup.vname) {
503 expandarg(psh, redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
504 fixredir(psh, redir, fn.list->text, 1);
505 }
506 break;
507 }
508 }
509}
510
511
512#ifdef KASH_USE_FORKSHELL2
513/*
514 * Child of evalpipe.
515 */
516struct evalpipechild
517{
518 int prevfd;
519 int pip[2];
520};
521
522static int evalpipe_child(shinstance *psh, union node *n, void *argp)
523{
524 struct evalpipechild args = *(struct evalpipechild *)argp;
525
526 if (args.prevfd > 0) {
527 movefd(psh, args.prevfd, 0);
528 }
529 if (args.pip[1] >= 0) {
530 shfile_close(&psh->fdtab, args.pip[0]);
531 if (args.pip[1] != 1) {
532 movefd(psh, args.pip[1], 1);
533 }
534 }
535 evaltree(psh, n, EV_EXIT);
536 /** @todo make it return thru here. */
537 return 0;
538}
539#endif /* KASH_USE_FORKSHELL2 */
540
541/*
542 * Evaluate a pipeline. All the processes in the pipeline are children
543 * of the process creating the pipeline. (This differs from some versions
544 * of the shell, which make the last process in a pipeline the parent
545 * of all the rest.)
546 */
547
548STATIC void
549evalpipe(shinstance *psh, union node *n)
550{
551 struct job *jp;
552 struct nodelist *lp;
553 int pipelen;
554 int prevfd;
555 int pip[2];
556
557 TRACE((psh, "evalpipe(0x%lx) called\n", (long)n));
558 pipelen = 0;
559 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
560 pipelen++;
561 INTOFF;
562 jp = makejob(psh, n, pipelen);
563 prevfd = -1;
564 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
565 prehash(psh, lp->n);
566 pip[1] = -1;
567 if (lp->next) {
568 if (sh_pipe(psh, pip) < 0) {
569 shfile_close(&psh->fdtab, prevfd);
570 error(psh, "Pipe call failed");
571 }
572 }
573#ifdef KASH_USE_FORKSHELL2
574 {
575 struct evalpipechild args;
576 args.prevfd = prevfd;
577 args.pip[0] = pip[0];
578 args.pip[1] = pip[1];
579 forkshell2(psh, jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG,
580 evalpipe_child, lp->n, &args, sizeof(args));
581 }
582#else
583 if (forkshell(psh, jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
584 INTON;
585 if (prevfd > 0) {
586 movefd(psh, prevfd, 0);
587 }
588 if (pip[1] >= 0) {
589 shfile_close(&psh->fdtab, pip[0]);
590 if (pip[1] != 1) {
591 movefd(psh, pip[1], 1);
592 }
593 }
594 evaltree(psh, lp->n, EV_EXIT);
595 }
596#endif
597 if (prevfd >= 0)
598 shfile_close(&psh->fdtab, prevfd);
599 prevfd = pip[0];
600 shfile_close(&psh->fdtab, pip[1]);
601 }
602 if (n->npipe.backgnd == 0) {
603 psh->exitstatus = waitforjob(psh, jp);
604 TRACE((psh, "evalpipe: job done exit status %d\n", psh->exitstatus));
605 }
606 INTON;
607}
608
609#ifdef KASH_USE_FORKSHELL2
610/*
611 * evalbackcmd child.
612 */
613struct evalbackcmdchild
614{
615 int pip[2];
616};
617
618static int evalbackcmd_child(shinstance *psh, union node *n, void *argp)
619{
620 struct evalbackcmdchild args = *(struct evalbackcmdchild *)argp;
621
622 FORCEINTON;
623 shfile_close(&psh->fdtab, args.pip[0]);
624 if (args.pip[1] != 1) {
625 movefd(psh, args.pip[1], 1);
626 }
627 eflag(psh) = 0;
628 evaltree(psh, n, EV_EXIT);
629 /* NOTREACHED */ /** @todo make it return here to simplify thread handling (no need for setjmp). */
630 return 0;
631}
632#endif /* KASH_USE_FORKSHELL2 */
633
634/*
635 * Execute a command inside back quotes. If it's a builtin command, we
636 * want to save its output in a block obtained from malloc. Otherwise
637 * we fork off a subprocess and get the output of the command via a pipe.
638 * Should be called with interrupts off.
639 */
640
641void
642evalbackcmd(shinstance *psh, union node *n, struct backcmd *result)
643{
644 int pip[2];
645 struct job *jp;
646 struct stackmark smark; /* unnecessary */
647
648 setstackmark(psh, &smark);
649 result->fd = -1;
650 result->buf = NULL;
651 result->nleft = 0;
652 result->jp = NULL;
653 if (n == NULL) {
654 goto out;
655 }
656#ifdef notyet
657 /*
658 * For now we disable executing builtins in the same
659 * context as the shell, because we are not keeping
660 * enough state to recover from changes that are
661 * supposed only to affect subshells. eg. echo "`cd /`"
662 */
663 if (n->type == NCMD) {
664 psh->exitstatus = opsh->exitstatus;
665 evalcommand(psh, n, EV_BACKCMD, result);
666 } else
667#endif
668 {
669 INTOFF;
670 if (sh_pipe(psh, pip) < 0)
671 error(psh, "Pipe call failed");
672 jp = makejob(psh, n, 1);
673#ifdef KASH_USE_FORKSHELL2
674 {
675 struct evalbackcmdchild args;
676 args.pip[0] = pip[0];
677 args.pip[1] = pip[1];
678 forkshell2(psh, jp, n, FORK_NOJOB,
679 evalbackcmd_child, n, &args, sizeof(args));
680 }
681#else
682 if (forkshell(psh, jp, n, FORK_NOJOB) == 0) {
683 FORCEINTON;
684 shfile_close(&psh->fdtab, pip[0]);
685 if (pip[1] != 1) {
686 movefd(psh, pip[1], 1);
687 }
688 eflag(psh) = 0;
689 evaltree(psh, n, EV_EXIT);
690 /* NOTREACHED */
691 }
692#endif
693 shfile_close(&psh->fdtab, pip[1]);
694 result->fd = pip[0];
695 result->jp = jp;
696 INTON;
697 }
698out:
699 popstackmark(psh, &smark);
700 TRACE((psh, "evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
701 result->fd, result->buf, result->nleft, result->jp));
702}
703
704static const char *
705syspath(shinstance *psh)
706{
707#ifdef CTL_USER
708 static char *sys_path = NULL;
709 static int mib[] = {CTL_USER, USER_CS_PATH};
710#endif
711#ifdef PC_PATH_SEP
712 static char def_path[] = "PATH=/usr/bin;/bin;/usr/sbin;/sbin";
713#else
714 static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
715#endif
716#ifdef CTL_USER
717 size_t len;
718
719 if (sys_path == NULL) {
720 if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
721 (sys_path = ckmalloc(psh, len + 5)) != NULL &&
722 sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
723 memcpy(sys_path, "PATH=", 5);
724 } else {
725 ckfree(psh, sys_path);
726 /* something to keep things happy */
727 sys_path = def_path;
728 }
729 }
730 return sys_path;
731#else
732 return def_path;
733#endif
734}
735
736static int
737parse_command_args(shinstance *psh, int argc, char **argv, int *use_syspath)
738{
739 int sv_argc = argc;
740 char *cp, c;
741
742 *use_syspath = 0;
743
744 for (;;) {
745 argv++;
746 if (--argc == 0)
747 break;
748 cp = *argv;
749 if (*cp++ != '-')
750 break;
751 if (*cp == '-' && cp[1] == 0) {
752 argv++;
753 argc--;
754 break;
755 }
756 while ((c = *cp++)) {
757 switch (c) {
758 case 'p':
759 *use_syspath = 1;
760 break;
761 default:
762 /* run 'typecmd' for other options */
763 return 0;
764 }
765 }
766 }
767 return sv_argc - argc;
768}
769
770
771/*
772 * The split up evalcommand code:
773 * evalcommand_out, evalcommand_parent, evalcommand_doit, evalcommand_child
774 */
775/*int vforked = 0;*/
776
777/* Both child and parent exits thru here. */
778STATIC void
779evalcommand_out(shinstance *psh, int flags, char *lastarg, struct stackmark *smarkp)
780{
781 if (lastarg)
782 /* dsl: I think this is intended to be used to support
783 * '_' in 'vi' command mode during line editing...
784 * However I implemented that within libedit itself.
785 */
786 setvar(psh, "_", lastarg, 0);
787 popstackmark(psh, smarkp);
788
789 if (eflag(psh) && psh->exitstatus && !(flags & EV_TESTED))
790 exitshell(psh, psh->exitstatus);
791}
792
793
794/* Called if we forkshell(). */
795STATIC void
796evalcommand_parent(shinstance *psh, int flags, char *lastarg, struct stackmark *smarkp,
797 int mode, struct job *jp, int pip[2], struct backcmd *backcmd)
798{
799 if (mode == FORK_FG) { /* argument to fork */
800 psh->exitstatus = waitforjob(psh, jp);
801 } else if (mode == FORK_NOJOB) {
802 backcmd->fd = pip[0];
803 shfile_close(&psh->fdtab, pip[1]);
804 backcmd->jp = jp;
805 }
806 FORCEINTON;
807
808 evalcommand_out(psh, flags, lastarg, smarkp);
809}
810
811struct evalcommanddoit
812{
813 struct cmdentry cmdentry;
814 char *lastarg;
815 const char *path;
816 struct backcmd *backcmd;
817 int flags;
818 int argc;
819 char **argv;
820 struct arglist varlist;
821 struct stackmark smark;
822};
823
824STATIC void
825evalcommand_doit(shinstance *psh, union node *cmd, struct evalcommanddoit *args)
826{
827 struct jmploc jmploc;
828 struct jmploc *volatile savehandler;
829 struct localvar *volatile savelocalvars;
830
831 /* This is the child process if a fork occurred. */
832 /* Execute the command. */
833 switch (args->cmdentry.cmdtype) {
834 case CMDFUNCTION: {
835 volatile struct shparam saveparam;
836#ifdef DEBUG
837 trputs(psh, "Shell function: "); trargs(psh, args->argv);
838#endif
839 redirect(psh, cmd->ncmd.redirect, REDIR_PUSH);
840 saveparam = psh->shellparam;
841 psh->shellparam.malloc = 0;
842 psh->shellparam.reset = 1;
843 psh->shellparam.nparam = args->argc - 1;
844 psh->shellparam.p = args->argv + 1;
845 psh->shellparam.optnext = NULL;
846 INTOFF;
847 savelocalvars = psh->localvars;
848 psh->localvars = NULL;
849 INTON;
850 if (setjmp(jmploc.loc)) {
851 if (psh->exception == EXSHELLPROC) {
852 freeparam(psh, (volatile struct shparam *)
853 &saveparam);
854 } else {
855 freeparam(psh, &psh->shellparam);
856 psh->shellparam = saveparam;
857 }
858 poplocalvars(psh);
859 psh->localvars = savelocalvars;
860 psh->handler = savehandler;
861 longjmp(psh->handler->loc, 1);
862 }
863 savehandler = psh->handler;
864 psh->handler = &jmploc;
865 listmklocal(psh, args->varlist.list, 0);
866 /* stop shell blowing its stack */
867 if (++psh->funcnest > 1000)
868 error(psh, "too many nested function calls");
869 evaltree(psh, args->cmdentry.u.func, args->flags & EV_TESTED);
870 psh->funcnest--;
871 INTOFF;
872 poplocalvars(psh);
873 psh->localvars = savelocalvars;
874 freeparam(psh, &psh->shellparam);
875 psh->shellparam = saveparam;
876 psh->handler = savehandler;
877 popredir(psh);
878 INTON;
879 if (psh->evalskip == SKIPFUNC) {
880 psh->evalskip = 0;
881 psh->skipcount = 0;
882 }
883 if (args->flags & EV_EXIT)
884 exitshell(psh, psh->exitstatus);
885 break;
886 }
887
888 case CMDBUILTIN:
889 case CMDSPLBLTIN: {
890 volatile int temp_path = 0;
891 char *volatile savecmdname;
892 volatile int e;
893 int mode;
894#ifdef DEBUG
895 trputs(psh, "builtin command: "); trargs(psh, args->argv);
896#endif
897 mode = (args->cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
898 if (args->flags == EV_BACKCMD) {
899 psh->memout.nleft = 0;
900 psh->memout.nextc = psh->memout.buf;
901 psh->memout.bufsize = 64;
902 mode |= REDIR_BACKQ;
903 }
904 e = -1;
905 savehandler = psh->handler;
906 savecmdname = psh->commandname;
907 psh->handler = &jmploc;
908 if (!setjmp(jmploc.loc)) {
909 /* We need to ensure the command hash table isn't
910 * corruped by temporary PATH assignments.
911 * However we must ensure the 'local' command works!
912 */
913 if (args->path != pathval(psh) && (args->cmdentry.u.bltin == hashcmd ||
914 args->cmdentry.u.bltin == typecmd)) {
915 savelocalvars = psh->localvars;
916 psh->localvars = 0;
917 mklocal(psh, args->path - 5 /* PATH= */, 0);
918 temp_path = 1;
919 } else
920 temp_path = 0;
921 redirect(psh, cmd->ncmd.redirect, mode);
922
923 /* exec is a special builtin, but needs this list... */
924 psh->cmdenviron = args->varlist.list;
925 /* we must check 'readonly' flag for all builtins */
926 listsetvar(psh, args->varlist.list,
927 args->cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
928 psh->commandname = args->argv[0];
929 /* initialize nextopt */
930 psh->argptr = args->argv + 1;
931 psh->optptr = NULL;
932 /* and getopt */
933#if 0 /** @todo fix getop usage! */
934#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
935 optreset = 1;
936 optind = 1;
937#else
938 optind = 0; /* init */
939#endif
940#endif
941
942 psh->exitstatus = args->cmdentry.u.bltin(psh, args->argc, args->argv);
943 } else {
944 e = psh->exception;
945 psh->exitstatus = e == EXINT ? SIGINT + 128 :
946 e == EXEXEC ? psh->exerrno : 2;
947 }
948 psh->handler = savehandler;
949 output_flushall(psh);
950 psh->out1 = &psh->output;
951 psh->out2 = &psh->errout;
952 freestdout(psh);
953 if (temp_path) {
954 poplocalvars(psh);
955 psh->localvars = savelocalvars;
956 }
957 psh->cmdenviron = NULL;
958 if (e != EXSHELLPROC) {
959 psh->commandname = savecmdname;
960 if (args->flags & EV_EXIT)
961 exitshell(psh, psh->exitstatus);
962 }
963 if (e != -1) {
964 if ((e != EXERROR && e != EXEXEC)
965 || args->cmdentry.cmdtype == CMDSPLBLTIN)
966 exraise(psh, e);
967 FORCEINTON;
968 }
969 if (args->cmdentry.u.bltin != execcmd)
970 popredir(psh);
971 if (args->flags == EV_BACKCMD) {
972 args->backcmd->buf = psh->memout.buf;
973 args->backcmd->nleft = (int)(psh->memout.nextc - psh->memout.buf);
974 psh->memout.buf = NULL;
975 }
976 break;
977 }
978
979 default: {
980 struct strlist *sp;
981 char **envp;
982#ifdef DEBUG
983 trputs(psh, "normal command: "); trargs(psh, args->argv);
984#endif
985 clearredir(psh, psh->vforked);
986 redirect(psh, cmd->ncmd.redirect, psh->vforked ? REDIR_VFORK : 0);
987 if (!psh->vforked)
988 for (sp = args->varlist.list ; sp ; sp = sp->next)
989 setvareq(psh, sp->text, VEXPORT|VSTACK);
990 envp = environment(psh);
991 shellexec(psh, args->argv, envp, args->path, args->cmdentry.u.index, psh->vforked);
992 break;
993 }
994 }
995
996 evalcommand_out(psh, args->flags, args->lastarg, &args->smark);
997}
998
999/*
1000 * Execute a simple command.
1001 */
1002
1003STATIC void
1004evalcommand(shinstance *psh, union node *cmd, int flags, struct backcmd *backcmd)
1005{
1006 struct evalcommanddoit args;
1007 char **argv;
1008 int argc;
1009
1010 union node *argp;
1011 int numvars;
1012 struct arglist arglist;
1013 struct strlist *sp;
1014 const char *path = pathval(psh);
1015
1016 psh->vforked = 0;
1017 /* First expand the arguments. */
1018 TRACE((psh, "evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
1019 setstackmark(psh, &args.smark);
1020 psh->back_exitstatus = 0;
1021
1022 arglist.lastp = &arglist.list;
1023 /* Expand arguments, ignoring the initial 'name=value' ones */
1024 for (argp = cmd->ncmd.args, numvars = 0 ; argp ; argp = argp->narg.next, numvars++) {
1025 char *p = argp->narg.text;
1026 char ch = *p;
1027 if (is_name(ch)) {
1028 do ch = *++p;
1029 while (is_in_name(ch));
1030 if (ch == '=')
1031 continue;
1032 }
1033 break;
1034 }
1035 for (/*continue on argp from above. */ ; argp ; argp = argp->narg.next)
1036 expandarg(psh, argp, &arglist, EXP_FULL | EXP_TILDE);
1037 *arglist.lastp = NULL;
1038
1039 expredir(psh, cmd->ncmd.redirect);
1040
1041 /* Now do the initial 'name=value' ones we skipped above */
1042 args.varlist.lastp = &args.varlist.list;
1043 for (argp = cmd->ncmd.args ; numvars > 0 && argp ; argp = argp->narg.next, numvars--)
1044 expandarg(psh, argp, &args.varlist, EXP_VARTILDE);
1045 *args.varlist.lastp = NULL;
1046
1047 argc = 0;
1048 for (sp = arglist.list ; sp ; sp = sp->next)
1049 argc++;
1050 args.argc = argc;
1051 args.argv = argv = stalloc(psh, sizeof (char *) * (argc + 1));
1052
1053 for (sp = arglist.list ; sp ; sp = sp->next) {
1054 TRACE((psh, "evalcommand arg: %s\n", sp->text));
1055 *argv++ = sp->text;
1056 }
1057 *argv = NULL;
1058 args.lastarg = NULL;
1059 if (iflag(psh) && psh->funcnest == 0 && argc > 0)
1060 args.lastarg = argv[-1];
1061 argv -= argc;
1062
1063 /* Print the command if xflag is set. */
1064 if (xflag(psh)) {
1065 char sep = 0;
1066 out2str(psh, ps4val(psh));
1067 for (sp = args.varlist.list ; sp ; sp = sp->next) {
1068 if (sep != 0)
1069 outc(sep, &psh->errout);
1070 out2str(psh, sp->text);
1071 sep = ' ';
1072 }
1073 for (sp = arglist.list ; sp ; sp = sp->next) {
1074 if (sep != 0)
1075 outc(sep, &psh->errout);
1076 out2str(psh, sp->text);
1077 sep = ' ';
1078 }
1079 outc('\n', &psh->errout);
1080 flushout(&psh->errout);
1081 }
1082
1083 /* Now locate the command. */
1084 if (argc == 0) {
1085 args.cmdentry.cmdtype = CMDSPLBLTIN;
1086 args.cmdentry.u.bltin = bltincmd;
1087 } else {
1088 static const char PATH[] = "PATH=";
1089 int cmd_flags = DO_ERR;
1090
1091 /*
1092 * Modify the command lookup path, if a PATH= assignment
1093 * is present
1094 */
1095 for (sp = args.varlist.list; sp; sp = sp->next)
1096 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
1097 path = sp->text + sizeof(PATH) - 1;
1098
1099 do {
1100 int argsused, use_syspath;
1101 find_command(psh, argv[0], &args.cmdentry, cmd_flags, path);
1102 if (args.cmdentry.cmdtype == CMDUNKNOWN) {
1103 psh->exitstatus = 127;
1104 flushout(&psh->errout);
1105 evalcommand_out(psh, flags, args.lastarg, &args.smark);
1106 return;
1107 }
1108
1109 /* implement the 'command' builtin here */
1110 if (args.cmdentry.cmdtype != CMDBUILTIN ||
1111 args.cmdentry.u.bltin != bltincmd)
1112 break;
1113 cmd_flags |= DO_NOFUNC;
1114 argsused = parse_command_args(psh, argc, argv, &use_syspath);
1115 if (argsused == 0) {
1116 /* use 'type' builting to display info */
1117 args.cmdentry.u.bltin = typecmd;
1118 break;
1119 }
1120 argc -= argsused;
1121 argv += argsused;
1122 if (use_syspath)
1123 path = syspath(psh) + 5;
1124 } while (argc != 0);
1125 if (args.cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
1126 /* posix mandates that 'command <splbltin>' act as if
1127 <splbltin> was a normal builtin */
1128 args.cmdentry.cmdtype = CMDBUILTIN;
1129 }
1130
1131 /* Fork off a child process if necessary. */
1132 if (cmd->ncmd.backgnd
1133 || (args.cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
1134 || ( (flags & EV_BACKCMD) != 0
1135 && ( (args.cmdentry.cmdtype != CMDBUILTIN && args.cmdentry.cmdtype != CMDSPLBLTIN)
1136 || args.cmdentry.u.bltin == dotcmd
1137 || args.cmdentry.u.bltin == evalcmd))) {
1138 struct job *jp;
1139 int pip[2];
1140 int mode;
1141 INTOFF;
1142 jp = makejob(psh, cmd, 1);
1143 mode = cmd->ncmd.backgnd;
1144 if (flags & EV_BACKCMD) {
1145 mode = FORK_NOJOB;
1146 if (sh_pipe(psh, pip) < 0)
1147 error(psh, "Pipe call failed");
1148 }
1149 if (forkshell(psh, jp, cmd, mode) != 0) {
1150 evalcommand_parent(psh, flags, args.lastarg, &args.smark, mode, jp, pip, backcmd);
1151 return; /* at end of routine */
1152 }
1153
1154 if (flags & EV_BACKCMD) {
1155 if (!psh->vforked) {
1156 FORCEINTON;
1157 }
1158 shfile_close(&psh->fdtab, pip[0]);
1159 if (pip[1] != 1) {
1160 movefd(psh, pip[1], 1);
1161 }
1162 }
1163 flags |= EV_EXIT;
1164
1165 args.backcmd = backcmd;
1166 args.flags = flags;
1167 args.path = path;
1168 evalcommand_doit(psh, cmd, &args);
1169 }
1170 else {
1171 args.backcmd = backcmd;
1172 args.flags = flags;
1173 args.path = path;
1174 evalcommand_doit(psh, cmd, &args);
1175 }
1176}
1177
1178
1179/*
1180 * Search for a command. This is called before we fork so that the
1181 * location of the command will be available in the parent as well as
1182 * the child. The check for "goodname" is an overly conservative
1183 * check that the name will not be subject to expansion.
1184 */
1185
1186STATIC void
1187prehash(shinstance *psh, union node *n)
1188{
1189 struct cmdentry entry;
1190
1191 if (n->type == NCMD && n->ncmd.args)
1192 if (goodname(n->ncmd.args->narg.text))
1193 find_command(psh, n->ncmd.args->narg.text, &entry, 0,
1194 pathval(psh));
1195}
1196
1197
1198
1199/*
1200 * Builtin commands. Builtin commands whose functions are closely
1201 * tied to evaluation are implemented here.
1202 */
1203
1204/*
1205 * No command given.
1206 */
1207
1208int
1209bltincmd(shinstance *psh, int argc, char **argv)
1210{
1211 /*
1212 * Preserve psh->exitstatus of a previous possible redirection
1213 * as POSIX mandates
1214 */
1215 return psh->back_exitstatus;
1216}
1217
1218
1219/*
1220 * Handle break and continue commands. Break, continue, and return are
1221 * all handled by setting the psh->evalskip flag. The evaluation routines
1222 * above all check this flag, and if it is set they start skipping
1223 * commands rather than executing them. The variable skipcount is
1224 * the number of loops to break/continue, or the number of function
1225 * levels to return. (The latter is always 1.) It should probably
1226 * be an error to break out of more loops than exist, but it isn't
1227 * in the standard shell so we don't make it one here.
1228 */
1229
1230int
1231breakcmd(shinstance *psh, int argc, char **argv)
1232{
1233 int n = argc > 1 ? number(psh, argv[1]) : 1;
1234
1235 if (n > psh->loopnest)
1236 n = psh->loopnest;
1237 if (n > 0) {
1238 psh->evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1239 psh->skipcount = n;
1240 }
1241 return 0;
1242}
1243
1244
1245/*
1246 * The return command.
1247 */
1248
1249int
1250returncmd(shinstance *psh, int argc, char **argv)
1251{
1252#if 0
1253 int ret = argc > 1 ? number(psh, argv[1]) : psh->exitstatus;
1254#else
1255 int ret;
1256 if (argc > 1) {
1257 /* make return -1 and VSC lite work ... */
1258 if (argv[1][0] != '-' || !is_number(&argv[1][1]))
1259 ret = number(psh, argv[1]);
1260 else
1261 ret = -number(psh, &argv[1][1]) & 255; /* take the bash approach */
1262 } else {
1263 ret = psh->exitstatus;
1264 }
1265#endif
1266
1267 if (psh->funcnest) {
1268 psh->evalskip = SKIPFUNC;
1269 psh->skipcount = 1;
1270 return ret;
1271 }
1272 else {
1273 /* Do what ksh does; skip the rest of the file */
1274 psh->evalskip = SKIPFILE;
1275 psh->skipcount = 1;
1276 return ret;
1277 }
1278}
1279
1280
1281int
1282falsecmd(shinstance *psh, int argc, char **argv)
1283{
1284 return 1;
1285}
1286
1287
1288int
1289truecmd(shinstance *psh, int argc, char **argv)
1290{
1291 return 0;
1292}
1293
1294
1295int
1296execcmd(shinstance *psh, int argc, char **argv)
1297{
1298 if (argc > 1) {
1299 struct strlist *sp;
1300
1301 iflag(psh) = 0; /* exit on error */
1302 mflag(psh) = 0;
1303 optschanged(psh);
1304 for (sp = psh->cmdenviron; sp; sp = sp->next)
1305 setvareq(psh, sp->text, VEXPORT|VSTACK);
1306 shellexec(psh, argv + 1, environment(psh), pathval(psh), 0, 0);
1307 }
1308 return 0;
1309}
1310
1311static int
1312conv_time(clock_t ticks, char *seconds, size_t l)
1313{
1314 static clock_t tpm = 0;
1315 clock_t mins;
1316 size_t i;
1317
1318 if (!tpm)
1319 tpm = /*sysconf(_SC_CLK_TCK)*/sh_sysconf_clk_tck() * 60;
1320
1321 mins = ticks / tpm;
1322#ifdef _MSC_VER
1323 {
1324 char tmp[64];
1325 sprintf(tmp, "%.4f", (ticks - mins * tpm) * 60.0 / tpm);
1326 strlcpy(seconds, tmp, l);
1327 }
1328#else
1329 snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
1330#endif
1331
1332 if (seconds[0] == '6' && seconds[1] == '0') {
1333 /* 59.99995 got rounded up... */
1334 mins++;
1335 strlcpy(seconds, "0.0", l);
1336 return mins;
1337 }
1338
1339 /* suppress trailing zeros */
1340 i = strlen(seconds) - 1;
1341 for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
1342 seconds[i] = 0;
1343 return mins;
1344}
1345
1346int
1347timescmd(shinstance *psh, int argc, char **argv)
1348{
1349 shtms tms;
1350 int u, s, cu, cs;
1351 char us[8], ss[8], cus[8], css[8];
1352
1353 nextopt(psh, "");
1354
1355 sh_times(psh, &tms);
1356
1357 u = conv_time(tms.tms_utime, us, sizeof(us));
1358 s = conv_time(tms.tms_stime, ss, sizeof(ss));
1359 cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
1360 cs = conv_time(tms.tms_cstime, css, sizeof(css));
1361
1362 outfmt(psh->out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
1363 u, us, s, ss, cu, cus, cs, css);
1364
1365 return 0;
1366}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette