eval.c revision 1.68 1 1.68 christos /* $NetBSD: eval.c,v 1.68 2002/11/24 22:35:39 christos Exp $ */
2 1.19 cgd
3 1.1 cgd /*-
4 1.7 jtc * Copyright (c) 1993
5 1.7 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.36 christos #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.19 cgd #if 0
42 1.26 christos static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
43 1.19 cgd #else
44 1.68 christos __RCSID("$NetBSD: eval.c,v 1.68 2002/11/24 22:35:39 christos Exp $");
45 1.24 cgd #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.21 christos #include <signal.h>
49 1.21 christos #include <unistd.h>
50 1.68 christos #include <sys/times.h>
51 1.53 elric
52 1.61 christos #include <sys/types.h>
53 1.61 christos #include <sys/wait.h>
54 1.61 christos
55 1.1 cgd /*
56 1.1 cgd * Evaluate a command.
57 1.1 cgd */
58 1.1 cgd
59 1.1 cgd #include "shell.h"
60 1.1 cgd #include "nodes.h"
61 1.1 cgd #include "syntax.h"
62 1.1 cgd #include "expand.h"
63 1.1 cgd #include "parser.h"
64 1.1 cgd #include "jobs.h"
65 1.1 cgd #include "eval.h"
66 1.1 cgd #include "builtins.h"
67 1.1 cgd #include "options.h"
68 1.1 cgd #include "exec.h"
69 1.1 cgd #include "redir.h"
70 1.1 cgd #include "input.h"
71 1.1 cgd #include "output.h"
72 1.1 cgd #include "trap.h"
73 1.1 cgd #include "var.h"
74 1.1 cgd #include "memalloc.h"
75 1.1 cgd #include "error.h"
76 1.21 christos #include "show.h"
77 1.1 cgd #include "mystring.h"
78 1.64 christos #include "main.h"
79 1.35 christos #ifndef SMALL
80 1.7 jtc #include "myhistedit.h"
81 1.11 cgd #endif
82 1.1 cgd
83 1.1 cgd
84 1.1 cgd /* flags in argument to evaltree */
85 1.1 cgd #define EV_EXIT 01 /* exit after evaluating tree */
86 1.1 cgd #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
87 1.1 cgd #define EV_BACKCMD 04 /* command executing within back quotes */
88 1.1 cgd
89 1.57 christos int evalskip; /* set if we are skipping commands */
90 1.1 cgd STATIC int skipcount; /* number of levels to skip */
91 1.1 cgd MKINIT int loopnest; /* current loop nesting level */
92 1.1 cgd int funcnest; /* depth of function calls */
93 1.1 cgd
94 1.1 cgd
95 1.1 cgd char *commandname;
96 1.1 cgd struct strlist *cmdenviron;
97 1.1 cgd int exitstatus; /* exit status of last command */
98 1.68 christos int back_exitstatus; /* exit status of backquoted command */
99 1.1 cgd
100 1.1 cgd
101 1.68 christos STATIC void evalloop(union node *, int);
102 1.68 christos STATIC void evalfor(union node *, int);
103 1.68 christos STATIC void evalcase(union node *, int);
104 1.68 christos STATIC void evalsubshell(union node *, int);
105 1.68 christos STATIC void expredir(union node *);
106 1.68 christos STATIC void evalpipe(union node *);
107 1.68 christos STATIC void evalcommand(union node *, int, struct backcmd *);
108 1.68 christos STATIC void prehash(union node *);
109 1.1 cgd
110 1.1 cgd
111 1.1 cgd /*
112 1.1 cgd * Called to reset things after an exception.
113 1.1 cgd */
114 1.1 cgd
115 1.1 cgd #ifdef mkinit
116 1.1 cgd INCLUDE "eval.h"
117 1.1 cgd
118 1.1 cgd RESET {
119 1.1 cgd evalskip = 0;
120 1.1 cgd loopnest = 0;
121 1.1 cgd funcnest = 0;
122 1.1 cgd }
123 1.1 cgd
124 1.1 cgd SHELLPROC {
125 1.1 cgd exitstatus = 0;
126 1.1 cgd }
127 1.1 cgd #endif
128 1.1 cgd
129 1.1 cgd
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * The eval commmand.
133 1.1 cgd */
134 1.1 cgd
135 1.16 cgd int
136 1.68 christos evalcmd(int argc, char **argv)
137 1.1 cgd {
138 1.1 cgd char *p;
139 1.1 cgd char *concat;
140 1.1 cgd char **ap;
141 1.1 cgd
142 1.1 cgd if (argc > 1) {
143 1.1 cgd p = argv[1];
144 1.1 cgd if (argc > 2) {
145 1.1 cgd STARTSTACKSTR(concat);
146 1.1 cgd ap = argv + 2;
147 1.1 cgd for (;;) {
148 1.1 cgd while (*p)
149 1.1 cgd STPUTC(*p++, concat);
150 1.1 cgd if ((p = *ap++) == NULL)
151 1.1 cgd break;
152 1.1 cgd STPUTC(' ', concat);
153 1.1 cgd }
154 1.1 cgd STPUTC('\0', concat);
155 1.1 cgd p = grabstackstr(concat);
156 1.1 cgd }
157 1.50 christos evalstring(p, EV_TESTED);
158 1.1 cgd }
159 1.1 cgd return exitstatus;
160 1.1 cgd }
161 1.1 cgd
162 1.1 cgd
163 1.1 cgd /*
164 1.1 cgd * Execute a command or commands contained in a string.
165 1.1 cgd */
166 1.1 cgd
167 1.1 cgd void
168 1.68 christos evalstring(char *s, int flag)
169 1.68 christos {
170 1.1 cgd union node *n;
171 1.1 cgd struct stackmark smark;
172 1.1 cgd
173 1.1 cgd setstackmark(&smark);
174 1.1 cgd setinputstring(s, 1);
175 1.64 christos
176 1.1 cgd while ((n = parsecmd(0)) != NEOF) {
177 1.65 christos evaltree(n, flag);
178 1.1 cgd popstackmark(&smark);
179 1.1 cgd }
180 1.1 cgd popfile();
181 1.1 cgd popstackmark(&smark);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd
185 1.1 cgd
186 1.1 cgd /*
187 1.1 cgd * Evaluate a parse tree. The value is left in the global variable
188 1.1 cgd * exitstatus.
189 1.1 cgd */
190 1.1 cgd
191 1.1 cgd void
192 1.68 christos evaltree(union node *n, int flags)
193 1.17 cgd {
194 1.1 cgd if (n == NULL) {
195 1.1 cgd TRACE(("evaltree(NULL) called\n"));
196 1.7 jtc exitstatus = 0;
197 1.7 jtc goto out;
198 1.1 cgd }
199 1.35 christos #ifndef SMALL
200 1.7 jtc displayhist = 1; /* show history substitutions done with fc */
201 1.10 cgd #endif
202 1.68 christos TRACE(("pid %d, evaltree(%p: %d) called\n", getpid(), n, n->type));
203 1.1 cgd switch (n->type) {
204 1.1 cgd case NSEMI:
205 1.65 christos evaltree(n->nbinary.ch1, flags & EV_TESTED);
206 1.1 cgd if (evalskip)
207 1.1 cgd goto out;
208 1.65 christos evaltree(n->nbinary.ch2, flags);
209 1.1 cgd break;
210 1.1 cgd case NAND:
211 1.65 christos evaltree(n->nbinary.ch1, EV_TESTED);
212 1.31 christos if (evalskip || exitstatus != 0) {
213 1.31 christos /* don't bomb out on "set -e; false && true" */
214 1.31 christos flags |= EV_TESTED;
215 1.1 cgd goto out;
216 1.31 christos }
217 1.65 christos evaltree(n->nbinary.ch2, flags | EV_TESTED);
218 1.1 cgd break;
219 1.1 cgd case NOR:
220 1.65 christos evaltree(n->nbinary.ch1, EV_TESTED);
221 1.1 cgd if (evalskip || exitstatus == 0)
222 1.1 cgd goto out;
223 1.65 christos evaltree(n->nbinary.ch2, flags | EV_TESTED);
224 1.1 cgd break;
225 1.1 cgd case NREDIR:
226 1.1 cgd expredir(n->nredir.redirect);
227 1.1 cgd redirect(n->nredir.redirect, REDIR_PUSH);
228 1.65 christos evaltree(n->nredir.n, flags);
229 1.1 cgd popredir();
230 1.1 cgd break;
231 1.1 cgd case NSUBSHELL:
232 1.65 christos evalsubshell(n, flags);
233 1.1 cgd break;
234 1.1 cgd case NBACKGND:
235 1.65 christos evalsubshell(n, flags);
236 1.1 cgd break;
237 1.1 cgd case NIF: {
238 1.65 christos evaltree(n->nif.test, EV_TESTED);
239 1.1 cgd if (evalskip)
240 1.1 cgd goto out;
241 1.28 christos if (exitstatus == 0)
242 1.65 christos evaltree(n->nif.ifpart, flags);
243 1.22 christos else if (n->nif.elsepart)
244 1.65 christos evaltree(n->nif.elsepart, flags);
245 1.29 pk else
246 1.29 pk exitstatus = 0;
247 1.1 cgd break;
248 1.1 cgd }
249 1.1 cgd case NWHILE:
250 1.1 cgd case NUNTIL:
251 1.65 christos evalloop(n, flags);
252 1.1 cgd break;
253 1.1 cgd case NFOR:
254 1.65 christos evalfor(n, flags);
255 1.1 cgd break;
256 1.1 cgd case NCASE:
257 1.65 christos evalcase(n, flags);
258 1.1 cgd break;
259 1.1 cgd case NDEFUN:
260 1.1 cgd defun(n->narg.text, n->narg.next);
261 1.1 cgd exitstatus = 0;
262 1.1 cgd break;
263 1.7 jtc case NNOT:
264 1.65 christos evaltree(n->nnot.com, EV_TESTED);
265 1.7 jtc exitstatus = !exitstatus;
266 1.7 jtc break;
267 1.7 jtc
268 1.1 cgd case NPIPE:
269 1.65 christos evalpipe(n);
270 1.1 cgd break;
271 1.1 cgd case NCMD:
272 1.65 christos evalcommand(n, flags, (struct backcmd *)NULL);
273 1.1 cgd break;
274 1.1 cgd default:
275 1.1 cgd out1fmt("Node type = %d\n", n->type);
276 1.1 cgd flushout(&output);
277 1.1 cgd break;
278 1.1 cgd }
279 1.1 cgd out:
280 1.1 cgd if (pendingsigs)
281 1.1 cgd dotrap();
282 1.58 christos if ((flags & EV_EXIT) != 0)
283 1.1 cgd exitshell(exitstatus);
284 1.1 cgd }
285 1.1 cgd
286 1.1 cgd
287 1.1 cgd STATIC void
288 1.68 christos evalloop(union node *n, int flags)
289 1.22 christos {
290 1.1 cgd int status;
291 1.1 cgd
292 1.1 cgd loopnest++;
293 1.1 cgd status = 0;
294 1.1 cgd for (;;) {
295 1.65 christos evaltree(n->nbinary.ch1, EV_TESTED);
296 1.1 cgd if (evalskip) {
297 1.1 cgd skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
298 1.1 cgd evalskip = 0;
299 1.1 cgd continue;
300 1.1 cgd }
301 1.1 cgd if (evalskip == SKIPBREAK && --skipcount <= 0)
302 1.1 cgd evalskip = 0;
303 1.1 cgd break;
304 1.1 cgd }
305 1.1 cgd if (n->type == NWHILE) {
306 1.1 cgd if (exitstatus != 0)
307 1.1 cgd break;
308 1.1 cgd } else {
309 1.1 cgd if (exitstatus == 0)
310 1.1 cgd break;
311 1.1 cgd }
312 1.65 christos evaltree(n->nbinary.ch2, flags & EV_TESTED);
313 1.1 cgd status = exitstatus;
314 1.1 cgd if (evalskip)
315 1.1 cgd goto skipping;
316 1.1 cgd }
317 1.1 cgd loopnest--;
318 1.1 cgd exitstatus = status;
319 1.1 cgd }
320 1.1 cgd
321 1.1 cgd
322 1.1 cgd
323 1.1 cgd STATIC void
324 1.68 christos evalfor(union node *n, int flags)
325 1.22 christos {
326 1.1 cgd struct arglist arglist;
327 1.1 cgd union node *argp;
328 1.1 cgd struct strlist *sp;
329 1.1 cgd struct stackmark smark;
330 1.68 christos int status = 0;
331 1.1 cgd
332 1.1 cgd setstackmark(&smark);
333 1.1 cgd arglist.lastp = &arglist.list;
334 1.1 cgd for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
335 1.42 christos expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD);
336 1.1 cgd if (evalskip)
337 1.1 cgd goto out;
338 1.1 cgd }
339 1.1 cgd *arglist.lastp = NULL;
340 1.1 cgd
341 1.1 cgd loopnest++;
342 1.1 cgd for (sp = arglist.list ; sp ; sp = sp->next) {
343 1.1 cgd setvar(n->nfor.var, sp->text, 0);
344 1.65 christos evaltree(n->nfor.body, flags & EV_TESTED);
345 1.68 christos status = exitstatus;
346 1.1 cgd if (evalskip) {
347 1.1 cgd if (evalskip == SKIPCONT && --skipcount <= 0) {
348 1.1 cgd evalskip = 0;
349 1.1 cgd continue;
350 1.1 cgd }
351 1.1 cgd if (evalskip == SKIPBREAK && --skipcount <= 0)
352 1.1 cgd evalskip = 0;
353 1.1 cgd break;
354 1.1 cgd }
355 1.1 cgd }
356 1.1 cgd loopnest--;
357 1.68 christos exitstatus = status;
358 1.1 cgd out:
359 1.1 cgd popstackmark(&smark);
360 1.1 cgd }
361 1.1 cgd
362 1.1 cgd
363 1.1 cgd
364 1.1 cgd STATIC void
365 1.68 christos evalcase(union node *n, int flags)
366 1.16 cgd {
367 1.1 cgd union node *cp;
368 1.1 cgd union node *patp;
369 1.1 cgd struct arglist arglist;
370 1.1 cgd struct stackmark smark;
371 1.68 christos int status = 0;
372 1.1 cgd
373 1.1 cgd setstackmark(&smark);
374 1.1 cgd arglist.lastp = &arglist.list;
375 1.7 jtc expandarg(n->ncase.expr, &arglist, EXP_TILDE);
376 1.1 cgd for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
377 1.1 cgd for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
378 1.1 cgd if (casematch(patp, arglist.list->text)) {
379 1.1 cgd if (evalskip == 0) {
380 1.65 christos evaltree(cp->nclist.body, flags);
381 1.68 christos status = exitstatus;
382 1.1 cgd }
383 1.1 cgd goto out;
384 1.1 cgd }
385 1.1 cgd }
386 1.1 cgd }
387 1.1 cgd out:
388 1.68 christos exitstatus = status;
389 1.1 cgd popstackmark(&smark);
390 1.1 cgd }
391 1.1 cgd
392 1.1 cgd
393 1.1 cgd
394 1.1 cgd /*
395 1.1 cgd * Kick off a subshell to evaluate a tree.
396 1.1 cgd */
397 1.1 cgd
398 1.1 cgd STATIC void
399 1.68 christos evalsubshell(union node *n, int flags)
400 1.16 cgd {
401 1.1 cgd struct job *jp;
402 1.1 cgd int backgnd = (n->type == NBACKGND);
403 1.1 cgd
404 1.1 cgd expredir(n->nredir.redirect);
405 1.63 mycroft INTOFF;
406 1.1 cgd jp = makejob(n, 1);
407 1.65 christos if (forkshell(jp, n, backgnd) == 0) {
408 1.63 mycroft INTON;
409 1.1 cgd if (backgnd)
410 1.1 cgd flags &=~ EV_TESTED;
411 1.1 cgd redirect(n->nredir.redirect, 0);
412 1.64 christos /* never returns */
413 1.65 christos evaltree(n->nredir.n, flags | EV_EXIT);
414 1.1 cgd }
415 1.63 mycroft if (! backgnd)
416 1.1 cgd exitstatus = waitforjob(jp);
417 1.63 mycroft INTON;
418 1.1 cgd }
419 1.1 cgd
420 1.1 cgd
421 1.1 cgd
422 1.1 cgd /*
423 1.1 cgd * Compute the names of the files in a redirection list.
424 1.1 cgd */
425 1.1 cgd
426 1.1 cgd STATIC void
427 1.68 christos expredir(union node *n)
428 1.22 christos {
429 1.34 tls union node *redir;
430 1.1 cgd
431 1.1 cgd for (redir = n ; redir ; redir = redir->nfile.next) {
432 1.14 jtc struct arglist fn;
433 1.14 jtc fn.lastp = &fn.list;
434 1.14 jtc switch (redir->type) {
435 1.45 christos case NFROMTO:
436 1.14 jtc case NFROM:
437 1.14 jtc case NTO:
438 1.59 christos case NCLOBBER:
439 1.14 jtc case NAPPEND:
440 1.7 jtc expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
441 1.1 cgd redir->nfile.expfname = fn.list->text;
442 1.14 jtc break;
443 1.14 jtc case NFROMFD:
444 1.14 jtc case NTOFD:
445 1.14 jtc if (redir->ndup.vname) {
446 1.14 jtc expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
447 1.14 jtc fixredir(redir, fn.list->text, 1);
448 1.14 jtc }
449 1.14 jtc break;
450 1.1 cgd }
451 1.1 cgd }
452 1.1 cgd }
453 1.1 cgd
454 1.1 cgd
455 1.1 cgd
456 1.1 cgd /*
457 1.1 cgd * Evaluate a pipeline. All the processes in the pipeline are children
458 1.1 cgd * of the process creating the pipeline. (This differs from some versions
459 1.1 cgd * of the shell, which make the last process in a pipeline the parent
460 1.1 cgd * of all the rest.)
461 1.1 cgd */
462 1.1 cgd
463 1.1 cgd STATIC void
464 1.68 christos evalpipe(union node *n)
465 1.22 christos {
466 1.1 cgd struct job *jp;
467 1.1 cgd struct nodelist *lp;
468 1.1 cgd int pipelen;
469 1.1 cgd int prevfd;
470 1.1 cgd int pip[2];
471 1.1 cgd
472 1.18 cgd TRACE(("evalpipe(0x%lx) called\n", (long)n));
473 1.1 cgd pipelen = 0;
474 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
475 1.1 cgd pipelen++;
476 1.1 cgd INTOFF;
477 1.1 cgd jp = makejob(n, pipelen);
478 1.1 cgd prevfd = -1;
479 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
480 1.1 cgd prehash(lp->n);
481 1.1 cgd pip[1] = -1;
482 1.1 cgd if (lp->next) {
483 1.1 cgd if (pipe(pip) < 0) {
484 1.1 cgd close(prevfd);
485 1.1 cgd error("Pipe call failed");
486 1.1 cgd }
487 1.1 cgd }
488 1.65 christos if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
489 1.1 cgd INTON;
490 1.1 cgd if (prevfd > 0) {
491 1.1 cgd close(0);
492 1.1 cgd copyfd(prevfd, 0);
493 1.1 cgd close(prevfd);
494 1.1 cgd }
495 1.1 cgd if (pip[1] >= 0) {
496 1.1 cgd close(pip[0]);
497 1.1 cgd if (pip[1] != 1) {
498 1.1 cgd close(1);
499 1.1 cgd copyfd(pip[1], 1);
500 1.1 cgd close(pip[1]);
501 1.1 cgd }
502 1.1 cgd }
503 1.65 christos evaltree(lp->n, EV_EXIT);
504 1.1 cgd }
505 1.1 cgd if (prevfd >= 0)
506 1.1 cgd close(prevfd);
507 1.1 cgd prevfd = pip[0];
508 1.1 cgd close(pip[1]);
509 1.1 cgd }
510 1.1 cgd if (n->npipe.backgnd == 0) {
511 1.1 cgd exitstatus = waitforjob(jp);
512 1.1 cgd TRACE(("evalpipe: job done exit status %d\n", exitstatus));
513 1.1 cgd }
514 1.60 mycroft INTON;
515 1.1 cgd }
516 1.1 cgd
517 1.1 cgd
518 1.1 cgd
519 1.1 cgd /*
520 1.1 cgd * Execute a command inside back quotes. If it's a builtin command, we
521 1.1 cgd * want to save its output in a block obtained from malloc. Otherwise
522 1.1 cgd * we fork off a subprocess and get the output of the command via a pipe.
523 1.1 cgd * Should be called with interrupts off.
524 1.1 cgd */
525 1.1 cgd
526 1.1 cgd void
527 1.68 christos evalbackcmd(union node *n, struct backcmd *result)
528 1.22 christos {
529 1.1 cgd int pip[2];
530 1.1 cgd struct job *jp;
531 1.1 cgd struct stackmark smark; /* unnecessary */
532 1.1 cgd
533 1.1 cgd setstackmark(&smark);
534 1.1 cgd result->fd = -1;
535 1.1 cgd result->buf = NULL;
536 1.1 cgd result->nleft = 0;
537 1.1 cgd result->jp = NULL;
538 1.23 christos if (n == NULL) {
539 1.7 jtc goto out;
540 1.23 christos }
541 1.46 christos #ifdef notyet
542 1.46 christos /*
543 1.46 christos * For now we disable executing builtins in the same
544 1.46 christos * context as the shell, because we are not keeping
545 1.46 christos * enough state to recover from changes that are
546 1.46 christos * supposed only to affect subshells. eg. echo "`cd /`"
547 1.46 christos */
548 1.7 jtc if (n->type == NCMD) {
549 1.23 christos exitstatus = oexitstatus;
550 1.65 christos evalcommand(n, EV_BACKCMD, result);
551 1.46 christos } else
552 1.46 christos #endif
553 1.46 christos {
554 1.63 mycroft INTOFF;
555 1.1 cgd if (pipe(pip) < 0)
556 1.1 cgd error("Pipe call failed");
557 1.1 cgd jp = makejob(n, 1);
558 1.65 christos if (forkshell(jp, n, FORK_NOJOB) == 0) {
559 1.1 cgd FORCEINTON;
560 1.1 cgd close(pip[0]);
561 1.1 cgd if (pip[1] != 1) {
562 1.1 cgd close(1);
563 1.1 cgd copyfd(pip[1], 1);
564 1.1 cgd close(pip[1]);
565 1.1 cgd }
566 1.50 christos eflag = 0;
567 1.65 christos evaltree(n, EV_EXIT);
568 1.68 christos /* NOTREACHED */
569 1.1 cgd }
570 1.1 cgd close(pip[1]);
571 1.1 cgd result->fd = pip[0];
572 1.1 cgd result->jp = jp;
573 1.63 mycroft INTON;
574 1.1 cgd }
575 1.7 jtc out:
576 1.1 cgd popstackmark(&smark);
577 1.1 cgd TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
578 1.1 cgd result->fd, result->buf, result->nleft, result->jp));
579 1.1 cgd }
580 1.1 cgd
581 1.1 cgd
582 1.61 christos int vforked = 0;
583 1.1 cgd
584 1.1 cgd /*
585 1.1 cgd * Execute a simple command.
586 1.1 cgd */
587 1.1 cgd
588 1.1 cgd STATIC void
589 1.68 christos evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
590 1.16 cgd {
591 1.1 cgd struct stackmark smark;
592 1.1 cgd union node *argp;
593 1.1 cgd struct arglist arglist;
594 1.1 cgd struct arglist varlist;
595 1.1 cgd char **argv;
596 1.1 cgd int argc;
597 1.1 cgd char **envp;
598 1.1 cgd int varflag;
599 1.1 cgd struct strlist *sp;
600 1.1 cgd int mode;
601 1.1 cgd int pip[2];
602 1.1 cgd struct cmdentry cmdentry;
603 1.1 cgd struct job *jp;
604 1.1 cgd struct jmploc jmploc;
605 1.1 cgd struct jmploc *volatile savehandler;
606 1.1 cgd char *volatile savecmdname;
607 1.1 cgd volatile struct shparam saveparam;
608 1.1 cgd struct localvar *volatile savelocalvars;
609 1.1 cgd volatile int e;
610 1.1 cgd char *lastarg;
611 1.21 christos #if __GNUC__
612 1.21 christos /* Avoid longjmp clobbering */
613 1.21 christos (void) &argv;
614 1.21 christos (void) &argc;
615 1.21 christos (void) &lastarg;
616 1.21 christos (void) &flags;
617 1.21 christos #endif
618 1.1 cgd
619 1.61 christos vforked = 0;
620 1.1 cgd /* First expand the arguments. */
621 1.65 christos TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
622 1.1 cgd setstackmark(&smark);
623 1.68 christos back_exitstatus = 0;
624 1.68 christos
625 1.1 cgd arglist.lastp = &arglist.list;
626 1.1 cgd varflag = 1;
627 1.1 cgd for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
628 1.20 christos char *p = argp->narg.text;
629 1.1 cgd if (varflag && is_name(*p)) {
630 1.1 cgd do {
631 1.1 cgd p++;
632 1.1 cgd } while (is_in_name(*p));
633 1.68 christos if (*p == '=')
634 1.1 cgd continue;
635 1.1 cgd }
636 1.7 jtc expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
637 1.1 cgd varflag = 0;
638 1.1 cgd }
639 1.1 cgd *arglist.lastp = NULL;
640 1.68 christos
641 1.68 christos expredir(cmd->ncmd.redirect);
642 1.68 christos
643 1.68 christos varlist.lastp = &varlist.list;
644 1.68 christos for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
645 1.68 christos char *p = argp->narg.text;
646 1.68 christos if (!is_name(*p))
647 1.68 christos break;
648 1.68 christos do
649 1.68 christos p++;
650 1.68 christos while (is_in_name(*p));
651 1.68 christos if (*p != '=')
652 1.68 christos break;
653 1.68 christos expandarg(argp, &varlist, EXP_VARTILDE);
654 1.68 christos }
655 1.1 cgd *varlist.lastp = NULL;
656 1.68 christos
657 1.1 cgd argc = 0;
658 1.1 cgd for (sp = arglist.list ; sp ; sp = sp->next)
659 1.1 cgd argc++;
660 1.1 cgd argv = stalloc(sizeof (char *) * (argc + 1));
661 1.7 jtc
662 1.7 jtc for (sp = arglist.list ; sp ; sp = sp->next) {
663 1.7 jtc TRACE(("evalcommand arg: %s\n", sp->text));
664 1.1 cgd *argv++ = sp->text;
665 1.7 jtc }
666 1.1 cgd *argv = NULL;
667 1.1 cgd lastarg = NULL;
668 1.1 cgd if (iflag && funcnest == 0 && argc > 0)
669 1.1 cgd lastarg = argv[-1];
670 1.1 cgd argv -= argc;
671 1.1 cgd
672 1.1 cgd /* Print the command if xflag is set. */
673 1.1 cgd if (xflag) {
674 1.1 cgd outc('+', &errout);
675 1.1 cgd for (sp = varlist.list ; sp ; sp = sp->next) {
676 1.1 cgd outc(' ', &errout);
677 1.1 cgd out2str(sp->text);
678 1.1 cgd }
679 1.1 cgd for (sp = arglist.list ; sp ; sp = sp->next) {
680 1.1 cgd outc(' ', &errout);
681 1.1 cgd out2str(sp->text);
682 1.1 cgd }
683 1.1 cgd outc('\n', &errout);
684 1.1 cgd flushout(&errout);
685 1.1 cgd }
686 1.1 cgd
687 1.1 cgd /* Now locate the command. */
688 1.1 cgd if (argc == 0) {
689 1.1 cgd cmdentry.cmdtype = CMDBUILTIN;
690 1.68 christos cmdentry.u.bltin = bltincmd;
691 1.1 cgd } else {
692 1.26 christos static const char PATH[] = "PATH=";
693 1.47 christos const char *path = pathval();
694 1.26 christos
695 1.26 christos /*
696 1.26 christos * Modify the command lookup path, if a PATH= assignment
697 1.26 christos * is present
698 1.26 christos */
699 1.26 christos for (sp = varlist.list ; sp ; sp = sp->next)
700 1.26 christos if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0)
701 1.26 christos path = sp->text + sizeof(PATH) - 1;
702 1.26 christos
703 1.38 christos find_command(argv[0], &cmdentry, DO_ERR, path);
704 1.1 cgd if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
705 1.31 christos exitstatus = 127;
706 1.1 cgd flushout(&errout);
707 1.1 cgd return;
708 1.1 cgd }
709 1.68 christos /* implement the 'command' (was bltin) builtin here */
710 1.68 christos while (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.bltin == bltincmd) {
711 1.68 christos argv++;
712 1.68 christos if (--argc == 0)
713 1.68 christos break;
714 1.68 christos if ((cmdentry.u.bltin = find_builtin(*argv)) != 0)
715 1.68 christos continue;
716 1.68 christos if ((cmdentry.u.bltin = find_splbltin(*argv)) == 0) {
717 1.68 christos outfmt(&errout, "%s: not found\n", *argv);
718 1.68 christos exitstatus = 127;
719 1.68 christos flushout(&errout);
720 1.68 christos return;
721 1.1 cgd }
722 1.68 christos cmdentry.cmdtype = CMDSPLBLTIN;
723 1.68 christos break;
724 1.1 cgd }
725 1.1 cgd }
726 1.1 cgd
727 1.1 cgd /* Fork off a child process if necessary. */
728 1.1 cgd if (cmd->ncmd.backgnd
729 1.21 christos || (cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0)
730 1.21 christos || ((flags & EV_BACKCMD) != 0
731 1.68 christos && ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN)
732 1.68 christos || cmdentry.u.bltin == dotcmd
733 1.68 christos || cmdentry.u.bltin == evalcmd))) {
734 1.62 christos INTOFF;
735 1.1 cgd jp = makejob(cmd, 1);
736 1.1 cgd mode = cmd->ncmd.backgnd;
737 1.1 cgd if (flags & EV_BACKCMD) {
738 1.1 cgd mode = FORK_NOJOB;
739 1.1 cgd if (pipe(pip) < 0)
740 1.1 cgd error("Pipe call failed");
741 1.1 cgd }
742 1.61 christos #ifdef DO_SHAREDVFORK
743 1.61 christos /* It is essential that if DO_SHAREDVFORK is defined that the
744 1.61 christos * child's address space is actually shared with the parent as
745 1.61 christos * we rely on this.
746 1.61 christos */
747 1.61 christos if (cmdentry.cmdtype == CMDNORMAL) {
748 1.61 christos pid_t pid;
749 1.61 christos
750 1.61 christos savelocalvars = localvars;
751 1.61 christos localvars = NULL;
752 1.61 christos vforked = 1;
753 1.61 christos switch (pid = vfork()) {
754 1.61 christos case -1:
755 1.61 christos TRACE(("Vfork failed, errno=%d", errno));
756 1.61 christos INTON;
757 1.61 christos error("Cannot vfork");
758 1.61 christos break;
759 1.61 christos case 0:
760 1.61 christos /* Make sure that exceptions only unwind to
761 1.61 christos * after the vfork(2)
762 1.61 christos */
763 1.61 christos if (setjmp(jmploc.loc)) {
764 1.61 christos if (exception == EXSHELLPROC) {
765 1.61 christos /* We can't progress with the vfork,
766 1.61 christos * so, set vforked = 2 so the parent
767 1.61 christos * knows, and _exit();
768 1.61 christos */
769 1.61 christos vforked = 2;
770 1.61 christos _exit(0);
771 1.61 christos } else {
772 1.61 christos _exit(exerrno);
773 1.61 christos }
774 1.61 christos }
775 1.61 christos savehandler = handler;
776 1.61 christos handler = &jmploc;
777 1.67 christos for (sp = varlist.list; sp; sp = sp->next)
778 1.67 christos mklocal(sp->text, VEXPORT);
779 1.65 christos forkchild(jp, cmd, mode, vforked);
780 1.61 christos break;
781 1.61 christos default:
782 1.61 christos handler = savehandler; /* restore from vfork(2) */
783 1.61 christos poplocalvars();
784 1.61 christos localvars = savelocalvars;
785 1.61 christos if (vforked == 2) {
786 1.61 christos vforked = 0;
787 1.61 christos
788 1.61 christos (void)waitpid(pid, NULL, 0);
789 1.61 christos /* We need to progress in a normal fork fashion */
790 1.61 christos goto normal_fork;
791 1.61 christos }
792 1.61 christos vforked = 0;
793 1.65 christos forkparent(jp, cmd, mode, pid);
794 1.61 christos goto parent;
795 1.61 christos }
796 1.61 christos } else {
797 1.61 christos normal_fork:
798 1.61 christos #endif
799 1.65 christos if (forkshell(jp, cmd, mode) != 0)
800 1.61 christos goto parent; /* at end of routine */
801 1.66 christos FORCEINTON;
802 1.61 christos #ifdef DO_SHAREDVFORK
803 1.61 christos }
804 1.61 christos #endif
805 1.1 cgd if (flags & EV_BACKCMD) {
806 1.61 christos if (!vforked) {
807 1.61 christos FORCEINTON;
808 1.61 christos }
809 1.1 cgd close(pip[0]);
810 1.1 cgd if (pip[1] != 1) {
811 1.1 cgd close(1);
812 1.1 cgd copyfd(pip[1], 1);
813 1.1 cgd close(pip[1]);
814 1.1 cgd }
815 1.1 cgd }
816 1.1 cgd flags |= EV_EXIT;
817 1.1 cgd }
818 1.1 cgd
819 1.1 cgd /* This is the child process if a fork occurred. */
820 1.1 cgd /* Execute the command. */
821 1.1 cgd if (cmdentry.cmdtype == CMDFUNCTION) {
822 1.31 christos #ifdef DEBUG
823 1.1 cgd trputs("Shell function: "); trargs(argv);
824 1.31 christos #endif
825 1.1 cgd redirect(cmd->ncmd.redirect, REDIR_PUSH);
826 1.1 cgd saveparam = shellparam;
827 1.1 cgd shellparam.malloc = 0;
828 1.32 christos shellparam.reset = 1;
829 1.1 cgd shellparam.nparam = argc - 1;
830 1.1 cgd shellparam.p = argv + 1;
831 1.1 cgd shellparam.optnext = NULL;
832 1.1 cgd INTOFF;
833 1.1 cgd savelocalvars = localvars;
834 1.1 cgd localvars = NULL;
835 1.1 cgd INTON;
836 1.1 cgd if (setjmp(jmploc.loc)) {
837 1.47 christos if (exception == EXSHELLPROC) {
838 1.47 christos freeparam((volatile struct shparam *)
839 1.47 christos &saveparam);
840 1.47 christos } else {
841 1.1 cgd freeparam(&shellparam);
842 1.1 cgd shellparam = saveparam;
843 1.1 cgd }
844 1.1 cgd poplocalvars();
845 1.1 cgd localvars = savelocalvars;
846 1.1 cgd handler = savehandler;
847 1.1 cgd longjmp(handler->loc, 1);
848 1.1 cgd }
849 1.1 cgd savehandler = handler;
850 1.1 cgd handler = &jmploc;
851 1.1 cgd for (sp = varlist.list ; sp ; sp = sp->next)
852 1.61 christos mklocal(sp->text, 0);
853 1.1 cgd funcnest++;
854 1.65 christos evaltree(cmdentry.u.func, flags & EV_TESTED);
855 1.1 cgd funcnest--;
856 1.1 cgd INTOFF;
857 1.1 cgd poplocalvars();
858 1.1 cgd localvars = savelocalvars;
859 1.1 cgd freeparam(&shellparam);
860 1.1 cgd shellparam = saveparam;
861 1.1 cgd handler = savehandler;
862 1.1 cgd popredir();
863 1.1 cgd INTON;
864 1.1 cgd if (evalskip == SKIPFUNC) {
865 1.1 cgd evalskip = 0;
866 1.1 cgd skipcount = 0;
867 1.1 cgd }
868 1.1 cgd if (flags & EV_EXIT)
869 1.1 cgd exitshell(exitstatus);
870 1.68 christos } else if (cmdentry.cmdtype == CMDBUILTIN || cmdentry.cmdtype == CMDSPLBLTIN) {
871 1.31 christos #ifdef DEBUG
872 1.1 cgd trputs("builtin command: "); trargs(argv);
873 1.31 christos #endif
874 1.68 christos mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH;
875 1.1 cgd if (flags == EV_BACKCMD) {
876 1.1 cgd memout.nleft = 0;
877 1.1 cgd memout.nextc = memout.buf;
878 1.1 cgd memout.bufsize = 64;
879 1.1 cgd mode |= REDIR_BACKQ;
880 1.1 cgd }
881 1.68 christos savehandler = handler;
882 1.68 christos handler = &jmploc;
883 1.1 cgd e = -1;
884 1.68 christos if (!setjmp(jmploc.loc)) {
885 1.68 christos redirect(cmd->ncmd.redirect, mode);
886 1.68 christos
887 1.68 christos savecmdname = commandname;
888 1.68 christos /* exec is a special builtin, but needs this list... */
889 1.68 christos cmdenviron = varlist.list;
890 1.68 christos /* we must check 'readonly' flag for all builtins */
891 1.68 christos listsetvar(varlist.list,
892 1.68 christos cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET);
893 1.68 christos commandname = argv[0];
894 1.68 christos /* initialize nextopt */
895 1.68 christos argptr = argv + 1;
896 1.68 christos optptr = NULL;
897 1.68 christos /* and getopt */
898 1.68 christos optreset = 1;
899 1.68 christos optind = 1;
900 1.68 christos exitstatus = cmdentry.u.bltin(argc, argv);
901 1.68 christos } else {
902 1.1 cgd e = exception;
903 1.68 christos exitstatus = e == EXINT ? SIGINT+128 :
904 1.68 christos e == EXEXEC ? exerrno : 2;
905 1.1 cgd }
906 1.1 cgd flushall();
907 1.1 cgd out1 = &output;
908 1.1 cgd out2 = &errout;
909 1.1 cgd freestdout();
910 1.39 thorpej cmdenviron = NULL;
911 1.1 cgd if (e != EXSHELLPROC) {
912 1.1 cgd commandname = savecmdname;
913 1.44 mycroft if (flags & EV_EXIT)
914 1.1 cgd exitshell(exitstatus);
915 1.1 cgd }
916 1.1 cgd handler = savehandler;
917 1.1 cgd if (e != -1) {
918 1.31 christos if ((e != EXERROR && e != EXEXEC)
919 1.68 christos || cmdentry.cmdtype == CMDSPLBLTIN
920 1.68 christos || cmdentry.u.bltin == bltincmd)
921 1.68 christos #if 0
922 1.68 christos || cmdentry.u.bltin == dotcmd
923 1.68 christos || cmdentry.u.bltin == evalcmd
924 1.35 christos #ifndef SMALL
925 1.68 christos || cmdentry.u.bltin == histcmd
926 1.68 christos #endif
927 1.68 christos || cmdentry.u.bltin == execcmd)
928 1.10 cgd #endif
929 1.1 cgd exraise(e);
930 1.1 cgd FORCEINTON;
931 1.1 cgd }
932 1.68 christos if (cmdentry.u.bltin != execcmd)
933 1.1 cgd popredir();
934 1.1 cgd if (flags == EV_BACKCMD) {
935 1.1 cgd backcmd->buf = memout.buf;
936 1.1 cgd backcmd->nleft = memout.nextc - memout.buf;
937 1.1 cgd memout.buf = NULL;
938 1.1 cgd }
939 1.1 cgd } else {
940 1.31 christos #ifdef DEBUG
941 1.1 cgd trputs("normal command: "); trargs(argv);
942 1.31 christos #endif
943 1.61 christos clearredir(vforked);
944 1.61 christos redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0);
945 1.61 christos if (!vforked)
946 1.61 christos for (sp = varlist.list ; sp ; sp = sp->next)
947 1.61 christos setvareq(sp->text, VEXPORT|VSTACK);
948 1.1 cgd envp = environment();
949 1.61 christos shellexec(argv, envp, pathval(), cmdentry.u.index, vforked);
950 1.1 cgd }
951 1.1 cgd goto out;
952 1.1 cgd
953 1.1 cgd parent: /* parent process gets here (if we forked) */
954 1.68 christos if (mode == FORK_FG) { /* argument to fork */
955 1.1 cgd exitstatus = waitforjob(jp);
956 1.68 christos } else if (mode == FORK_NOJOB) {
957 1.1 cgd backcmd->fd = pip[0];
958 1.1 cgd close(pip[1]);
959 1.1 cgd backcmd->jp = jp;
960 1.1 cgd }
961 1.66 christos FORCEINTON;
962 1.1 cgd
963 1.1 cgd out:
964 1.1 cgd if (lastarg)
965 1.1 cgd setvar("_", lastarg, 0);
966 1.1 cgd popstackmark(&smark);
967 1.50 christos
968 1.50 christos if (eflag && exitstatus && !(flags & EV_TESTED))
969 1.68 christos exitshell(exitstatus);
970 1.1 cgd }
971 1.1 cgd
972 1.1 cgd
973 1.1 cgd /*
974 1.1 cgd * Search for a command. This is called before we fork so that the
975 1.1 cgd * location of the command will be available in the parent as well as
976 1.1 cgd * the child. The check for "goodname" is an overly conservative
977 1.1 cgd * check that the name will not be subject to expansion.
978 1.1 cgd */
979 1.1 cgd
980 1.1 cgd STATIC void
981 1.68 christos prehash(union node *n)
982 1.22 christos {
983 1.1 cgd struct cmdentry entry;
984 1.1 cgd
985 1.15 mycroft if (n->type == NCMD && n->ncmd.args)
986 1.15 mycroft if (goodname(n->ncmd.args->narg.text))
987 1.26 christos find_command(n->ncmd.args->narg.text, &entry, 0,
988 1.26 christos pathval());
989 1.1 cgd }
990 1.1 cgd
991 1.1 cgd
992 1.1 cgd
993 1.1 cgd /*
994 1.1 cgd * Builtin commands. Builtin commands whose functions are closely
995 1.1 cgd * tied to evaluation are implemented here.
996 1.1 cgd */
997 1.1 cgd
998 1.1 cgd /*
999 1.1 cgd * No command given, or a bltin command with no arguments. Set the
1000 1.1 cgd * specified variables.
1001 1.1 cgd */
1002 1.1 cgd
1003 1.16 cgd int
1004 1.68 christos bltincmd(int argc, char **argv)
1005 1.16 cgd {
1006 1.68 christos /* "command" isn't a special builtin.... */
1007 1.68 christos listsetvar(cmdenviron, 0);
1008 1.31 christos /*
1009 1.22 christos * Preserve exitstatus of a previous possible redirection
1010 1.31 christos * as POSIX mandates
1011 1.22 christos */
1012 1.68 christos return back_exitstatus;
1013 1.1 cgd }
1014 1.1 cgd
1015 1.1 cgd
1016 1.1 cgd /*
1017 1.1 cgd * Handle break and continue commands. Break, continue, and return are
1018 1.1 cgd * all handled by setting the evalskip flag. The evaluation routines
1019 1.1 cgd * above all check this flag, and if it is set they start skipping
1020 1.1 cgd * commands rather than executing them. The variable skipcount is
1021 1.1 cgd * the number of loops to break/continue, or the number of function
1022 1.1 cgd * levels to return. (The latter is always 1.) It should probably
1023 1.1 cgd * be an error to break out of more loops than exist, but it isn't
1024 1.1 cgd * in the standard shell so we don't make it one here.
1025 1.1 cgd */
1026 1.1 cgd
1027 1.16 cgd int
1028 1.68 christos breakcmd(int argc, char **argv)
1029 1.16 cgd {
1030 1.30 christos int n = argc > 1 ? number(argv[1]) : 1;
1031 1.1 cgd
1032 1.1 cgd if (n > loopnest)
1033 1.1 cgd n = loopnest;
1034 1.1 cgd if (n > 0) {
1035 1.1 cgd evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
1036 1.1 cgd skipcount = n;
1037 1.1 cgd }
1038 1.1 cgd return 0;
1039 1.1 cgd }
1040 1.1 cgd
1041 1.1 cgd
1042 1.1 cgd /*
1043 1.1 cgd * The return command.
1044 1.1 cgd */
1045 1.1 cgd
1046 1.16 cgd int
1047 1.68 christos returncmd(int argc, char **argv)
1048 1.16 cgd {
1049 1.68 christos int ret = argc > 1 ? number(argv[1]) : exitstatus;
1050 1.1 cgd
1051 1.1 cgd if (funcnest) {
1052 1.1 cgd evalskip = SKIPFUNC;
1053 1.1 cgd skipcount = 1;
1054 1.27 christos return ret;
1055 1.27 christos }
1056 1.27 christos else {
1057 1.27 christos /* Do what ksh does; skip the rest of the file */
1058 1.27 christos evalskip = SKIPFILE;
1059 1.27 christos skipcount = 1;
1060 1.27 christos return ret;
1061 1.1 cgd }
1062 1.1 cgd }
1063 1.1 cgd
1064 1.1 cgd
1065 1.16 cgd int
1066 1.68 christos falsecmd(int argc, char **argv)
1067 1.16 cgd {
1068 1.8 jtc return 1;
1069 1.8 jtc }
1070 1.8 jtc
1071 1.8 jtc
1072 1.16 cgd int
1073 1.68 christos truecmd(int argc, char **argv)
1074 1.16 cgd {
1075 1.1 cgd return 0;
1076 1.1 cgd }
1077 1.1 cgd
1078 1.1 cgd
1079 1.16 cgd int
1080 1.68 christos execcmd(int argc, char **argv)
1081 1.16 cgd {
1082 1.1 cgd if (argc > 1) {
1083 1.20 christos struct strlist *sp;
1084 1.20 christos
1085 1.1 cgd iflag = 0; /* exit on error */
1086 1.7 jtc mflag = 0;
1087 1.7 jtc optschanged();
1088 1.20 christos for (sp = cmdenviron; sp ; sp = sp->next)
1089 1.20 christos setvareq(sp->text, VEXPORT|VSTACK);
1090 1.61 christos shellexec(argv + 1, environment(), pathval(), 0, 0);
1091 1.1 cgd }
1092 1.68 christos return 0;
1093 1.68 christos }
1094 1.68 christos
1095 1.68 christos static int
1096 1.68 christos conv_time(clock_t ticks, char *seconds)
1097 1.68 christos {
1098 1.68 christos static clock_t tpm = 0;
1099 1.68 christos clock_t mins;
1100 1.68 christos int i;
1101 1.68 christos
1102 1.68 christos if (!tpm)
1103 1.68 christos tpm = sysconf(_SC_CLK_TCK) * 60;
1104 1.68 christos
1105 1.68 christos mins = ticks / tpm;
1106 1.68 christos sprintf(seconds, "%.4f", (ticks - mins * tpm) * 60.0 / tpm );
1107 1.68 christos
1108 1.68 christos if (seconds[0] == '6' && seconds[1] == '0') {
1109 1.68 christos /* 59.99995 got rounded up... */
1110 1.68 christos mins++;
1111 1.68 christos strcpy(seconds, "0.0");
1112 1.68 christos return mins;
1113 1.68 christos }
1114 1.68 christos
1115 1.68 christos /* suppress trailing zeros */
1116 1.68 christos i = strlen(seconds) - 1;
1117 1.68 christos for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--)
1118 1.68 christos seconds[i] = 0;
1119 1.68 christos return mins;
1120 1.68 christos }
1121 1.68 christos
1122 1.68 christos int
1123 1.68 christos timescmd(int argc, char **argv)
1124 1.68 christos {
1125 1.68 christos struct tms tms;
1126 1.68 christos int u, s, cu, cs;
1127 1.68 christos char us[8], ss[8], cus[8], css[8];
1128 1.68 christos
1129 1.68 christos nextopt("");
1130 1.68 christos
1131 1.68 christos times(&tms);
1132 1.68 christos
1133 1.68 christos u = conv_time( tms.tms_utime, us );
1134 1.68 christos s = conv_time( tms.tms_stime, ss );
1135 1.68 christos cu = conv_time( tms.tms_cutime, cus );
1136 1.68 christos cs = conv_time( tms.tms_cstime, css );
1137 1.68 christos
1138 1.68 christos outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n",
1139 1.68 christos u, us, s, ss, cu, cus, cs, css);
1140 1.68 christos
1141 1.1 cgd return 0;
1142 1.1 cgd }
1143