VirtualBox

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

Last change on this file since 1214 was 1214, checked in by bird, 17 years ago

some more cleanup.

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