source: trunk/src/kash/eval.c@ 2423

Last change on this file since 2423 was 2423, checked in by bird, 15 years ago

kash: made the SHFILE_IN_USE mode work on unix (because openbsd has a very hackish pthread library).

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