VirtualBox

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

Last change on this file since 2773 was 2654, checked in by bird, 12 years ago

eval.c/evalcommand: tiny optimization wrt name=value handling.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 29.2 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
416/*
417 * Kick off a subshell to evaluate a tree.
418 */
419
420STATIC void
421evalsubshell(shinstance *psh, union node *n, int flags)
422{
423 struct job *jp;
424 int backgnd = (n->type == NBACKGND);
425
426 expredir(psh, n->nredir.redirect);
427 INTOFF;
428 jp = makejob(psh, n, 1);
429 if (forkshell(psh, jp, n, backgnd ? FORK_BG : FORK_FG) == 0) {
430 INTON;
431 if (backgnd)
432 flags &=~ EV_TESTED;
433 redirect(psh, n->nredir.redirect, 0);
434 /* never returns */
435 evaltree(psh, n->nredir.n, flags | EV_EXIT);
436 }
437 if (! backgnd)
438 psh->exitstatus = waitforjob(psh, jp);
439 INTON;
440}
441
442
443
444/*
445 * Compute the names of the files in a redirection list.
446 */
447
448STATIC void
449expredir(shinstance *psh, union node *n)
450{
451 union node *redir;
452
453 for (redir = n ; redir ; redir = redir->nfile.next) {
454 struct arglist fn;
455 fn.lastp = &fn.list;
456 switch (redir->type) {
457 case NFROMTO:
458 case NFROM:
459 case NTO:
460 case NCLOBBER:
461 case NAPPEND:
462 expandarg(psh, redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
463 redir->nfile.expfname = fn.list->text;
464 break;
465 case NFROMFD:
466 case NTOFD:
467 if (redir->ndup.vname) {
468 expandarg(psh, redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
469 fixredir(psh, redir, fn.list->text, 1);
470 }
471 break;
472 }
473 }
474}
475
476
477
478/*
479 * Evaluate a pipeline. All the processes in the pipeline are children
480 * of the process creating the pipeline. (This differs from some versions
481 * of the shell, which make the last process in a pipeline the parent
482 * of all the rest.)
483 */
484
485STATIC void
486evalpipe(shinstance *psh, union node *n)
487{
488 struct job *jp;
489 struct nodelist *lp;
490 int pipelen;
491 int prevfd;
492 int pip[2];
493
494 TRACE((psh, "evalpipe(0x%lx) called\n", (long)n));
495 pipelen = 0;
496 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
497 pipelen++;
498 INTOFF;
499 jp = makejob(psh, n, pipelen);
500 prevfd = -1;
501 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
502 prehash(psh, lp->n);
503 pip[1] = -1;
504 if (lp->next) {
505 if (sh_pipe(psh, pip) < 0) {
506 shfile_close(&psh->fdtab, prevfd);
507 error(psh, "Pipe call failed");
508 }
509 }
510 if (forkshell(psh, jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) {
511 INTON;
512 if (prevfd > 0) {
513 movefd(psh, prevfd, 0);
514 }
515 if (pip[1] >= 0) {
516 shfile_close(&psh->fdtab, pip[0]);
517 if (pip[1] != 1) {
518 movefd(psh, pip[1], 1);
519 }
520 }
521 evaltree(psh, lp->n, EV_EXIT);
522 }
523 if (prevfd >= 0)
524 shfile_close(&psh->fdtab, prevfd);
525 prevfd = pip[0];
526 shfile_close(&psh->fdtab, pip[1]);
527 }
528 if (n->npipe.backgnd == 0) {
529 psh->exitstatus = waitforjob(psh, jp);
530 TRACE((psh, "evalpipe: job done exit status %d\n", psh->exitstatus));
531 }
532 INTON;
533}
534
535
536
537/*
538 * Execute a command inside back quotes. If it's a builtin command, we
539 * want to save its output in a block obtained from malloc. Otherwise
540 * we fork off a subprocess and get the output of the command via a pipe.
541 * Should be called with interrupts off.
542 */
543
544void
545evalbackcmd(shinstance *psh, union node *n, struct backcmd *result)
546{
547 int pip[2];
548 struct job *jp;
549 struct stackmark smark; /* unnecessary */
550
551 setstackmark(psh, &smark);
552 result->fd = -1;
553 result->buf = NULL;
554 result->nleft = 0;
555 result->jp = NULL;
556 if (n == NULL) {
557 goto out;
558 }
559#ifdef notyet
560 /*
561 * For now we disable executing builtins in the same
562 * context as the shell, because we are not keeping
563 * enough state to recover from changes that are
564 * supposed only to affect subshells. eg. echo "`cd /`"
565 */
566 if (n->type == NCMD) {
567 psh->exitstatus = opsh->exitstatus;
568 evalcommand(psh, n, EV_BACKCMD, result);
569 } else
570#endif
571 {
572 INTOFF;
573 if (sh_pipe(psh, pip) < 0)
574 error(psh, "Pipe call failed");
575 jp = makejob(psh, n, 1);
576 if (forkshell(psh, jp, n, FORK_NOJOB) == 0) {
577 FORCEINTON;
578 shfile_close(&psh->fdtab, pip[0]);
579 if (pip[1] != 1) {
580 movefd(psh, pip[1], 1);
581 }
582 eflag(psh) = 0;
583 evaltree(psh, n, EV_EXIT);
584 /* NOTREACHED */
585 }
586 shfile_close(&psh->fdtab, pip[1]);
587 result->fd = pip[0];
588 result->jp = jp;
589 INTON;
590 }
591out:
592 popstackmark(psh, &smark);
593 TRACE((psh, "evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
594 result->fd, result->buf, result->nleft, result->jp));
595}
596
597static const char *
598syspath(shinstance *psh)
599{
600#ifdef CTL_USER
601 static char *sys_path = NULL;
602 static int mib[] = {CTL_USER, USER_CS_PATH};
603#endif
604#ifdef PC_PATH_SEP
605 static char def_path[] = "PATH=/usr/bin;/bin;/usr/sbin;/sbin";
606#else
607 static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin";
608#endif
609#ifdef CTL_USER
610 size_t len;
611
612 if (sys_path == NULL) {
613 if (sysctl(mib, 2, 0, &len, 0, 0) != -1 &&
614 (sys_path = ckmalloc(psh, len + 5)) != NULL &&
615 sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) {
616 memcpy(sys_path, "PATH=", 5);
617 } else {
618 ckfree(psh, sys_path);
619 /* something to keep things happy */
620 sys_path = def_path;
621 }
622 }
623 return sys_path;
624#else
625 return def_path;
626#endif
627}
628
629static int
630parse_command_args(shinstance *psh, int argc, char **argv, int *use_syspath)
631{
632 int sv_argc = argc;
633 char *cp, c;
634
635 *use_syspath = 0;
636
637 for (;;) {
638 argv++;
639 if (--argc == 0)
640 break;
641 cp = *argv;
642 if (*cp++ != '-')
643 break;
644 if (*cp == '-' && cp[1] == 0) {
645 argv++;
646 argc--;
647 break;
648 }
649 while ((c = *cp++)) {
650 switch (c) {
651 case 'p':
652 *use_syspath = 1;
653 break;
654 default:
655 /* run 'typecmd' for other options */
656 return 0;
657 }
658 }
659 }
660 return sv_argc - argc;
661}
662
663/*int vforked = 0;*/
664
665/*
666 * Execute a simple command.
667 */
668
669STATIC void
670evalcommand(shinstance *psh, union node *cmd, int flags, struct backcmd *backcmd)
671{
672 struct stackmark smark;
673 union node *argp;
674 struct arglist arglist;
675 struct arglist varlist;
676 char **argv;
677 int argc;
678 char **envp;
679 int numvars;
680 struct strlist *sp;
681 int mode;
682 int pip[2];
683 struct cmdentry cmdentry;
684 struct job *jp;
685 struct jmploc jmploc;
686 struct jmploc *volatile savehandler;
687 char *volatile savecmdname;
688 volatile struct shparam saveparam;
689 struct localvar *volatile savelocalvars;
690 volatile int e;
691 char *lastarg;
692 const char *path = pathval(psh);
693 volatile int temp_path;
694#if __GNUC__
695 /* Avoid longjmp clobbering */
696 (void) &argv;
697 (void) &argc;
698 (void) &lastarg;
699 (void) &flags;
700#endif
701
702 psh->vforked = 0;
703 /* First expand the arguments. */
704 TRACE((psh, "evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
705 setstackmark(psh, &smark);
706 psh->back_exitstatus = 0;
707
708 arglist.lastp = &arglist.list;
709 /* Expand arguments, ignoring the initial 'name=value' ones */
710 for (argp = cmd->ncmd.args, numvars = 0 ; argp ; argp = argp->narg.next, numvars++) {
711 char *p = argp->narg.text;
712 char ch = *p;
713 if (is_name(ch)) {
714 do ch = *++p;
715 while (is_in_name(ch));
716 if (ch == '=')
717 continue;
718 }
719 break;
720 }
721 for (/*continue on argp from above. */ ; argp ; argp = argp->narg.next)
722 expandarg(psh, argp, &arglist, EXP_FULL | EXP_TILDE);
723 *arglist.lastp = NULL;
724
725 expredir(psh, cmd->ncmd.redirect);
726
727 /* Now do the initial 'name=value' ones we skipped above */
728 varlist.lastp = &varlist.list;
729 for (argp = cmd->ncmd.args ; numvars > 0 && argp ; argp = argp->narg.next, numvars--)
730 expandarg(psh, argp, &varlist, EXP_VARTILDE);
731 *varlist.lastp = NULL;
732
733 argc = 0;
734 for (sp = arglist.list ; sp ; sp = sp->next)
735 argc++;
736 argv = stalloc(psh, sizeof (char *) * (argc + 1));
737
738 for (sp = arglist.list ; sp ; sp = sp->next) {
739 TRACE((psh, "evalcommand arg: %s\n", sp->text));
740 *argv++ = sp->text;
741 }
742 *argv = NULL;
743 lastarg = NULL;
744 if (iflag(psh) && psh->funcnest == 0 && argc > 0)
745 lastarg = argv[-1];
746 argv -= argc;
747
748 /* Print the command if xflag is set. */
749 if (xflag(psh)) {
750 char sep = 0;
751 out2str(psh, ps4val(psh));
752 for (sp = varlist.list ; sp ; sp = sp->next) {
753 if (sep != 0)
754 outc(sep, &psh->errout);
755 out2str(psh, sp->text);
756 sep = ' ';
757 }
758 for (sp = arglist.list ; sp ; sp = sp->next) {
759 if (sep != 0)
760 outc(sep, &psh->errout);
761 out2str(psh, sp->text);
762 sep = ' ';
763 }
764 outc('\n', &psh->errout);
765 flushout(&psh->errout);
766 }
767
768 /* Now locate the command. */
769 if (argc == 0) {
770 cmdentry.cmdtype = CMDSPLBLTIN;
771 cmdentry.u.bltin = bltincmd;
772 } else {
773 static const char PATH[] = "PATH=";
774 int cmd_flags = DO_ERR;
775
776 /*
777 * Modify the command lookup path, if a PATH= assignment
778 * is present
779 */
780 for (sp = varlist.list; sp; sp = sp->next)
781 if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
782 path = sp->text + sizeof(PATH) - 1;
783
784 do {
785 int argsused, use_syspath;
786 find_command(psh, argv[0], &cmdentry, cmd_flags, path);
787 if (cmdentry.cmdtype == CMDUNKNOWN) {
788 psh->exitstatus = 127;
789 flushout(&psh->errout);
790 goto out;
791 }
792
793 /* implement the 'command' builtin here */
794 if (cmdentry.cmdtype != CMDBUILTIN ||
795 cmdentry.u.bltin != bltincmd)
796 break;
797 cmd_flags |= DO_NOFUNC;
798 argsused = parse_command_args(psh, argc, argv, &use_syspath);
799 if (argsused == 0) {
800 /* use 'type' builting to display info */
801 cmdentry.u.bltin = typecmd;
802 break;
803 }
804 argc -= argsused;
805 argv += argsused;
806 if (use_syspath)
807 path = syspath(psh) + 5;
808 } while (argc != 0);
809 if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC)
810 /* posix mandates that 'command <splbltin>' act as if
811 <splbltin> was a normal builtin */
812 cmdentry.cmdtype = CMDBUILTIN;
813 }
814
815 /* Fork off a child process if necessary. */
816 if (cmd->ncmd.backgnd
817 || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
818 || ((flags & EV_BACKCMD) != 0
819 && ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN)
820 || cmdentry.u.bltin == dotcmd
821 || cmdentry.u.bltin == evalcmd))) {
822 INTOFF;
823 jp = makejob(psh, cmd, 1);
824 mode = cmd->ncmd.backgnd;
825 if (flags & EV_BACKCMD) {
826 mode = FORK_NOJOB;
827 if (sh_pipe(psh, pip) < 0)
828 error(psh, "Pipe call failed");
829 }
830#ifdef DO_SHAREDVFORK
831 /* It is essential that if DO_SHAREDVFORK is defined that the
832 * child's address space is actually shared with the parent as
833 * we rely on this.
834 */
835 if (cmdentry.cmdtype == CMDNORMAL) {
836 pid_t pid;
837
838 savelocalvars = psh->localvars;
839 psh->localvars = NULL;
840 psh->vforked = 1;
841 switch (pid = vfork()) {
842 case -1:
843 TRACE((psh, "Vfork failed, errno=%d\n", errno));
844 INTON;
845 error(psh, "Cannot vfork");
846 break;
847 case 0:
848 /* Make sure that exceptions only unwind to
849 * after the vfork(2)
850 */
851 if (setjmp(jmploc.loc)) {
852 if (psh->exception == EXSHELLPROC) {
853 /* We can't progress with the vfork,
854 * so, set vforked = 2 so the parent
855 * knows, and _exit();
856 */
857 psh->vforked = 2;
858 sh__exit(psh, 0);
859 } else {
860 sh__exit(psh, psh->exerrno);
861 }
862 }
863 savehandler = psh->handler;
864 psh->handler = &jmploc;
865 listmklocal(psh, varlist.list, VEXPORT | VNOFUNC);
866 forkchild(psh, jp, cmd, mode, psh->vforked);
867 break;
868 default:
869 psh->handler = savehandler; /* restore from vfork(2) */
870 poplocalvars(psh);
871 psh->localvars = savelocalvars;
872 if (psh->vforked == 2) {
873 psh->vforked = 0;
874
875 (void)sh_waitpid(psh, pid, NULL, 0);
876 /* We need to progress in a normal fork fashion */
877 goto normal_fork;
878 }
879 psh->vforked = 0;
880 forkparent(psh, jp, cmd, mode, pid);
881 goto parent;
882 }
883 } else {
884normal_fork:
885#endif
886 if (forkshell(psh, jp, cmd, mode) != 0)
887 goto parent; /* at end of routine */
888 FORCEINTON;
889#ifdef DO_SHAREDVFORK
890 }
891#endif
892 if (flags & EV_BACKCMD) {
893 if (!psh->vforked) {
894 FORCEINTON;
895 }
896 shfile_close(&psh->fdtab, pip[0]);
897 if (pip[1] != 1) {
898 movefd(psh, pip[1], 1);
899 }
900 }
901 flags |= EV_EXIT;
902 }
903
904 /* This is the child process if a fork occurred. */
905 /* Execute the command. */
906 switch (cmdentry.cmdtype) {
907 case CMDFUNCTION:
908#ifdef DEBUG
909 trputs(psh, "Shell function: "); trargs(psh, argv);
910#endif
911 redirect(psh, cmd->ncmd.redirect, REDIR_PUSH);
912 saveparam = psh->shellparam;
913 psh->shellparam.malloc = 0;
914 psh->shellparam.reset = 1;
915 psh->shellparam.nparam = argc - 1;
916 psh->shellparam.p = argv + 1;
917 psh->shellparam.optnext = NULL;
918 INTOFF;
919 savelocalvars = psh->localvars;
920 psh->localvars = NULL;
921 INTON;
922 if (setjmp(jmploc.loc)) {
923 if (psh->exception == EXSHELLPROC) {
924 freeparam(psh, (volatile struct shparam *)
925 &saveparam);
926 } else {
927 freeparam(psh, &psh->shellparam);
928 psh->shellparam = saveparam;
929 }
930 poplocalvars(psh);
931 psh->localvars = savelocalvars;
932 psh->handler = savehandler;
933 longjmp(psh->handler->loc, 1);
934 }
935 savehandler = psh->handler;
936 psh->handler = &jmploc;
937 listmklocal(psh, varlist.list, 0);
938 /* stop shell blowing its stack */
939 if (++psh->funcnest > 1000)
940 error(psh, "too many nested function calls");
941 evaltree(psh, cmdentry.u.func, flags & EV_TESTED);
942 psh->funcnest--;
943 INTOFF;
944 poplocalvars(psh);
945 psh->localvars = savelocalvars;
946 freeparam(psh, &psh->shellparam);
947 psh->shellparam = saveparam;
948 psh->handler = savehandler;
949 popredir(psh);
950 INTON;
951 if (psh->evalskip == SKIPFUNC) {
952 psh->evalskip = 0;
953 psh->skipcount = 0;
954 }
955 if (flags & EV_EXIT)
956 exitshell(psh, psh->exitstatus);
957 break;
958
959 case CMDBUILTIN:
960 case CMDSPLBLTIN:
961#ifdef DEBUG
962 trputs(psh, "builtin command: "); trargs(psh, argv);
963#endif
964 mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
965 if (flags == EV_BACKCMD) {
966 psh->memout.nleft = 0;
967 psh->memout.nextc = psh->memout.buf;
968 psh->memout.bufsize = 64;
969 mode |= REDIR_BACKQ;
970 }
971 e = -1;
972 savehandler = psh->handler;
973 savecmdname = psh->commandname;
974 psh->handler = &jmploc;
975 if (!setjmp(jmploc.loc)) {
976 /* We need to ensure the command hash table isn't
977 * corruped by temporary PATH assignments.
978 * However we must ensure the 'local' command works!
979 */
980 if (path != pathval(psh) && (cmdentry.u.bltin == hashcmd ||
981 cmdentry.u.bltin == typecmd)) {
982 savelocalvars = psh->localvars;
983 psh->localvars = 0;
984 mklocal(psh, path - 5 /* PATH= */, 0);
985 temp_path = 1;
986 } else
987 temp_path = 0;
988 redirect(psh, cmd->ncmd.redirect, mode);
989
990 /* exec is a special builtin, but needs this list... */
991 psh->cmdenviron = varlist.list;
992 /* we must check 'readonly' flag for all builtins */
993 listsetvar(psh, varlist.list,
994 cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
995 psh->commandname = argv[0];
996 /* initialize nextopt */
997 psh->argptr = argv + 1;
998 psh->optptr = NULL;
999 /* and getopt */
1000#if 0 /** @todo fix getop usage! */
1001#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
1002 optreset = 1;
1003 optind = 1;
1004#else
1005 optind = 0; /* init */
1006#endif
1007#endif
1008
1009 psh->exitstatus = cmdentry.u.bltin(psh, argc, argv);
1010 } else {
1011 e = psh->exception;
1012 psh->exitstatus = e == EXINT ? SIGINT + 128 :
1013 e == EXEXEC ? psh->exerrno : 2;
1014 }
1015 psh->handler = savehandler;
1016 output_flushall(psh);
1017 psh->out1 = &psh->output;
1018 psh->out2 = &psh->errout;
1019 freestdout(psh);
1020 if (temp_path) {
1021 poplocalvars(psh);
1022 psh->localvars = savelocalvars;
1023 }
1024 psh->cmdenviron = NULL;
1025 if (e != EXSHELLPROC) {
1026 psh->commandname = savecmdname;
1027 if (flags & EV_EXIT)
1028 exitshell(psh, psh->exitstatus);
1029 }
1030 if (e != -1) {
1031 if ((e != EXERROR && e != EXEXEC)
1032 || cmdentry.cmdtype == CMDSPLBLTIN)
1033 exraise(psh, e);
1034 FORCEINTON;
1035 }
1036 if (cmdentry.u.bltin != execcmd)
1037 popredir(psh);
1038 if (flags == EV_BACKCMD) {
1039 backcmd->buf = psh->memout.buf;
1040 backcmd->nleft = (int)(psh->memout.nextc - psh->memout.buf);
1041 psh->memout.buf = NULL;
1042 }
1043 break;
1044
1045 default:
1046#ifdef DEBUG
1047 trputs(psh, "normal command: "); trargs(psh, argv);
1048#endif
1049 clearredir(psh, psh->vforked);
1050 redirect(psh, cmd->ncmd.redirect, psh->vforked ? REDIR_VFORK : 0);
1051 if (!psh->vforked)
1052 for (sp = varlist.list ; sp ; sp = sp->next)
1053 setvareq(psh, sp->text, VEXPORT|VSTACK);
1054 envp = environment(psh);
1055 shellexec(psh, argv, envp, path, cmdentry.u.index, psh->vforked);
1056 break;
1057 }
1058 goto out;
1059
1060parent: /* parent process gets here (if we forked) */
1061 if (mode == FORK_FG) { /* argument to fork */
1062 psh->exitstatus = waitforjob(psh, jp);
1063 } else if (mode == FORK_NOJOB) {
1064 backcmd->fd = pip[0];
1065 shfile_close(&psh->fdtab, pip[1]);
1066 backcmd->jp = jp;
1067 }
1068 FORCEINTON;
1069
1070out:
1071 if (lastarg)
1072 /* dsl: I think this is intended to be used to support
1073 * '_' in 'vi' command mode during line editing...
1074 * However I implemented that within libedit itself.
1075 */
1076 setvar(psh, "_", lastarg, 0);
1077 popstackmark(psh, &smark);
1078
1079 if (eflag(psh) && psh->exitstatus && !(flags & EV_TESTED))
1080 exitshell(psh, psh->exitstatus);
1081}
1082
1083
1084/*
1085 * Search for a command. This is called before we fork so that the
1086 * location of the command will be available in the parent as well as
1087 * the child. The check for "goodname" is an overly conservative
1088 * check that the name will not be subject to expansion.
1089 */
1090
1091STATIC void
1092prehash(shinstance *psh, union node *n)
1093{
1094 struct cmdentry entry;
1095
1096 if (n->type == NCMD && n->ncmd.args)
1097 if (goodname(n->ncmd.args->narg.text))
1098 find_command(psh, n->ncmd.args->narg.text, &entry, 0,
1099 pathval(psh));
1100}
1101
1102
1103
1104/*
1105 * Builtin commands. Builtin commands whose functions are closely
1106 * tied to evaluation are implemented here.
1107 */
1108
1109/*
1110 * No command given.
1111 */
1112
1113int
1114bltincmd(shinstance *psh, int argc, char **argv)
1115{
1116 /*
1117 * Preserve psh->exitstatus of a previous possible redirection
1118 * as POSIX mandates
1119 */
1120 return psh->back_exitstatus;
1121}
1122
1123
1124/*
1125 * Handle break and continue commands. Break, continue, and return are
1126 * all handled by setting the psh->evalskip flag. The evaluation routines
1127 * above all check this flag, and if it is set they start skipping
1128 * commands rather than executing them. The variable skipcount is
1129 * the number of loops to break/continue, or the number of function
1130 * levels to return. (The latter is always 1.) It should probably
1131 * be an error to break out of more loops than exist, but it isn't
1132 * in the standard shell so we don't make it one here.
1133 */
1134
1135int
1136breakcmd(shinstance *psh, int argc, char **argv)
1137{
1138 int n = argc > 1 ? number(psh, argv[1]) : 1;
1139
1140 if (n > psh->loopnest)
1141 n = psh->loopnest;
1142 if (n > 0) {
1143 psh->evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1144 psh->skipcount = n;
1145 }
1146 return 0;
1147}
1148
1149
1150/*
1151 * The return command.
1152 */
1153
1154int
1155returncmd(shinstance *psh, int argc, char **argv)
1156{
1157#if 0
1158 int ret = argc > 1 ? number(psh, argv[1]) : psh->exitstatus;
1159#else
1160 int ret;
1161 if (argc > 1) {
1162 /* make return -1 and VSC lite work ... */
1163 if (argv[1][0] != '-' || !is_number(&argv[1][1]))
1164 ret = number(psh, argv[1]);
1165 else
1166 ret = -number(psh, &argv[1][1]) & 255; /* take the bash approach */
1167 } else {
1168 ret = psh->exitstatus;
1169 }
1170#endif
1171
1172 if (psh->funcnest) {
1173 psh->evalskip = SKIPFUNC;
1174 psh->skipcount = 1;
1175 return ret;
1176 }
1177 else {
1178 /* Do what ksh does; skip the rest of the file */
1179 psh->evalskip = SKIPFILE;
1180 psh->skipcount = 1;
1181 return ret;
1182 }
1183}
1184
1185
1186int
1187falsecmd(shinstance *psh, int argc, char **argv)
1188{
1189 return 1;
1190}
1191
1192
1193int
1194truecmd(shinstance *psh, int argc, char **argv)
1195{
1196 return 0;
1197}
1198
1199
1200int
1201execcmd(shinstance *psh, int argc, char **argv)
1202{
1203 if (argc > 1) {
1204 struct strlist *sp;
1205
1206 iflag(psh) = 0; /* exit on error */
1207 mflag(psh) = 0;
1208 optschanged(psh);
1209 for (sp = psh->cmdenviron; sp; sp = sp->next)
1210 setvareq(psh, sp->text, VEXPORT|VSTACK);
1211 shellexec(psh, argv + 1, environment(psh), pathval(psh), 0, 0);
1212 }
1213 return 0;
1214}
1215
1216static int
1217conv_time(clock_t ticks, char *seconds, size_t l)
1218{
1219 static clock_t tpm = 0;
1220 clock_t mins;
1221 size_t i;
1222
1223 if (!tpm)
1224 tpm = /*sysconf(_SC_CLK_TCK)*/sh_sysconf_clk_tck() * 60;
1225
1226 mins = ticks / tpm;
1227#ifdef _MSC_VER
1228 {
1229 char tmp[64];
1230 sprintf(tmp, "%.4f", (ticks - mins * tpm) * 60.0 / tpm);
1231 strlcpy(seconds, tmp, l);
1232 }
1233#else
1234 snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
1235#endif
1236
1237 if (seconds[0] == '6' && seconds[1] == '0') {
1238 /* 59.99995 got rounded up... */
1239 mins++;
1240 strlcpy(seconds, "0.0", l);
1241 return mins;
1242 }
1243
1244 /* suppress trailing zeros */
1245 i = strlen(seconds) - 1;
1246 for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
1247 seconds[i] = 0;
1248 return mins;
1249}
1250
1251int
1252timescmd(shinstance *psh, int argc, char **argv)
1253{
1254 shtms tms;
1255 int u, s, cu, cs;
1256 char us[8], ss[8], cus[8], css[8];
1257
1258 nextopt(psh, "");
1259
1260 sh_times(psh, &tms);
1261
1262 u = conv_time(tms.tms_utime, us, sizeof(us));
1263 s = conv_time(tms.tms_stime, ss, sizeof(ss));
1264 cu = conv_time(tms.tms_cutime, cus, sizeof(cus));
1265 cs = conv_time(tms.tms_cstime, css, sizeof(css));
1266
1267 outfmt(psh->out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
1268 u, us, s, ss, cu, cus, cs, css);
1269
1270 return 0;
1271}
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