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