eval.c revision 1.14 1 1.1 cgd /*-
2 1.7 jtc * Copyright (c) 1993
3 1.7 jtc * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Kenneth Almquist.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.12 mycroft /*static char sccsid[] = "from: @(#)eval.c 8.1 (Berkeley) 5/31/93";*/
39 1.14 jtc static char *rcsid = "$Id: eval.c,v 1.14 1994/06/14 05:49:19 jtc Exp $";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd /*
43 1.1 cgd * Evaluate a command.
44 1.1 cgd */
45 1.1 cgd
46 1.1 cgd #include "shell.h"
47 1.1 cgd #include "nodes.h"
48 1.1 cgd #include "syntax.h"
49 1.1 cgd #include "expand.h"
50 1.1 cgd #include "parser.h"
51 1.1 cgd #include "jobs.h"
52 1.1 cgd #include "eval.h"
53 1.1 cgd #include "builtins.h"
54 1.1 cgd #include "options.h"
55 1.1 cgd #include "exec.h"
56 1.1 cgd #include "redir.h"
57 1.1 cgd #include "input.h"
58 1.1 cgd #include "output.h"
59 1.1 cgd #include "trap.h"
60 1.1 cgd #include "var.h"
61 1.1 cgd #include "memalloc.h"
62 1.1 cgd #include "error.h"
63 1.1 cgd #include "mystring.h"
64 1.11 cgd #ifndef NO_HISTORY
65 1.7 jtc #include "myhistedit.h"
66 1.11 cgd #endif
67 1.1 cgd #include <signal.h>
68 1.9 jtc #include <unistd.h>
69 1.1 cgd
70 1.1 cgd
71 1.1 cgd /* flags in argument to evaltree */
72 1.1 cgd #define EV_EXIT 01 /* exit after evaluating tree */
73 1.1 cgd #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
74 1.1 cgd #define EV_BACKCMD 04 /* command executing within back quotes */
75 1.1 cgd
76 1.1 cgd
77 1.1 cgd /* reasons for skipping commands (see comment on breakcmd routine) */
78 1.1 cgd #define SKIPBREAK 1
79 1.1 cgd #define SKIPCONT 2
80 1.1 cgd #define SKIPFUNC 3
81 1.1 cgd
82 1.1 cgd MKINIT int evalskip; /* set if we are skipping commands */
83 1.1 cgd STATIC int skipcount; /* number of levels to skip */
84 1.1 cgd MKINIT int loopnest; /* current loop nesting level */
85 1.1 cgd int funcnest; /* depth of function calls */
86 1.1 cgd
87 1.1 cgd
88 1.1 cgd char *commandname;
89 1.1 cgd struct strlist *cmdenviron;
90 1.1 cgd int exitstatus; /* exit status of last command */
91 1.1 cgd
92 1.1 cgd
93 1.1 cgd #ifdef __STDC__
94 1.1 cgd STATIC void evalloop(union node *);
95 1.1 cgd STATIC void evalfor(union node *);
96 1.1 cgd STATIC void evalcase(union node *, int);
97 1.1 cgd STATIC void evalsubshell(union node *, int);
98 1.1 cgd STATIC void expredir(union node *);
99 1.1 cgd STATIC void evalpipe(union node *);
100 1.1 cgd STATIC void evalcommand(union node *, int, struct backcmd *);
101 1.1 cgd STATIC void prehash(union node *);
102 1.1 cgd #else
103 1.1 cgd STATIC void evalloop();
104 1.1 cgd STATIC void evalfor();
105 1.1 cgd STATIC void evalcase();
106 1.1 cgd STATIC void evalsubshell();
107 1.1 cgd STATIC void expredir();
108 1.1 cgd STATIC void evalpipe();
109 1.1 cgd STATIC void evalcommand();
110 1.1 cgd STATIC void prehash();
111 1.1 cgd #endif
112 1.1 cgd
113 1.1 cgd
114 1.1 cgd
115 1.1 cgd /*
116 1.1 cgd * Called to reset things after an exception.
117 1.1 cgd */
118 1.1 cgd
119 1.1 cgd #ifdef mkinit
120 1.1 cgd INCLUDE "eval.h"
121 1.1 cgd
122 1.1 cgd RESET {
123 1.1 cgd evalskip = 0;
124 1.1 cgd loopnest = 0;
125 1.1 cgd funcnest = 0;
126 1.1 cgd }
127 1.1 cgd
128 1.1 cgd SHELLPROC {
129 1.1 cgd exitstatus = 0;
130 1.1 cgd }
131 1.1 cgd #endif
132 1.1 cgd
133 1.1 cgd
134 1.1 cgd
135 1.1 cgd /*
136 1.1 cgd * The eval commmand.
137 1.1 cgd */
138 1.1 cgd
139 1.1 cgd evalcmd(argc, argv)
140 1.1 cgd char **argv;
141 1.1 cgd {
142 1.1 cgd char *p;
143 1.1 cgd char *concat;
144 1.1 cgd char **ap;
145 1.1 cgd
146 1.1 cgd if (argc > 1) {
147 1.1 cgd p = argv[1];
148 1.1 cgd if (argc > 2) {
149 1.1 cgd STARTSTACKSTR(concat);
150 1.1 cgd ap = argv + 2;
151 1.1 cgd for (;;) {
152 1.1 cgd while (*p)
153 1.1 cgd STPUTC(*p++, concat);
154 1.1 cgd if ((p = *ap++) == NULL)
155 1.1 cgd break;
156 1.1 cgd STPUTC(' ', concat);
157 1.1 cgd }
158 1.1 cgd STPUTC('\0', concat);
159 1.1 cgd p = grabstackstr(concat);
160 1.1 cgd }
161 1.1 cgd evalstring(p);
162 1.1 cgd }
163 1.1 cgd return exitstatus;
164 1.1 cgd }
165 1.1 cgd
166 1.1 cgd
167 1.1 cgd /*
168 1.1 cgd * Execute a command or commands contained in a string.
169 1.1 cgd */
170 1.1 cgd
171 1.1 cgd void
172 1.1 cgd evalstring(s)
173 1.1 cgd char *s;
174 1.1 cgd {
175 1.1 cgd union node *n;
176 1.1 cgd struct stackmark smark;
177 1.1 cgd
178 1.1 cgd setstackmark(&smark);
179 1.1 cgd setinputstring(s, 1);
180 1.1 cgd while ((n = parsecmd(0)) != NEOF) {
181 1.1 cgd evaltree(n, 0);
182 1.1 cgd popstackmark(&smark);
183 1.1 cgd }
184 1.1 cgd popfile();
185 1.1 cgd popstackmark(&smark);
186 1.1 cgd }
187 1.1 cgd
188 1.1 cgd
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * Evaluate a parse tree. The value is left in the global variable
192 1.1 cgd * exitstatus.
193 1.1 cgd */
194 1.1 cgd
195 1.1 cgd void
196 1.1 cgd evaltree(n, flags)
197 1.1 cgd union node *n;
198 1.1 cgd {
199 1.1 cgd if (n == NULL) {
200 1.1 cgd TRACE(("evaltree(NULL) called\n"));
201 1.7 jtc exitstatus = 0;
202 1.7 jtc goto out;
203 1.1 cgd }
204 1.10 cgd #ifndef NO_HISTORY
205 1.7 jtc displayhist = 1; /* show history substitutions done with fc */
206 1.10 cgd #endif
207 1.1 cgd TRACE(("evaltree(0x%x: %d) called\n", (int)n, n->type));
208 1.1 cgd switch (n->type) {
209 1.1 cgd case NSEMI:
210 1.1 cgd evaltree(n->nbinary.ch1, 0);
211 1.1 cgd if (evalskip)
212 1.1 cgd goto out;
213 1.1 cgd evaltree(n->nbinary.ch2, flags);
214 1.1 cgd break;
215 1.1 cgd case NAND:
216 1.1 cgd evaltree(n->nbinary.ch1, EV_TESTED);
217 1.1 cgd if (evalskip || exitstatus != 0)
218 1.1 cgd goto out;
219 1.1 cgd evaltree(n->nbinary.ch2, flags);
220 1.1 cgd break;
221 1.1 cgd case NOR:
222 1.1 cgd evaltree(n->nbinary.ch1, EV_TESTED);
223 1.1 cgd if (evalskip || exitstatus == 0)
224 1.1 cgd goto out;
225 1.1 cgd evaltree(n->nbinary.ch2, flags);
226 1.1 cgd break;
227 1.1 cgd case NREDIR:
228 1.1 cgd expredir(n->nredir.redirect);
229 1.1 cgd redirect(n->nredir.redirect, REDIR_PUSH);
230 1.1 cgd evaltree(n->nredir.n, flags);
231 1.1 cgd popredir();
232 1.1 cgd break;
233 1.1 cgd case NSUBSHELL:
234 1.1 cgd evalsubshell(n, flags);
235 1.1 cgd break;
236 1.1 cgd case NBACKGND:
237 1.1 cgd evalsubshell(n, flags);
238 1.1 cgd break;
239 1.1 cgd case NIF: {
240 1.1 cgd int status = 0;
241 1.1 cgd
242 1.1 cgd evaltree(n->nif.test, EV_TESTED);
243 1.1 cgd if (evalskip)
244 1.1 cgd goto out;
245 1.1 cgd if (exitstatus == 0) {
246 1.1 cgd evaltree(n->nif.ifpart, flags);
247 1.1 cgd status = exitstatus;
248 1.1 cgd } else if (n->nif.elsepart) {
249 1.1 cgd evaltree(n->nif.elsepart, flags);
250 1.1 cgd status = exitstatus;
251 1.1 cgd }
252 1.1 cgd exitstatus = status;
253 1.1 cgd break;
254 1.1 cgd }
255 1.1 cgd case NWHILE:
256 1.1 cgd case NUNTIL:
257 1.1 cgd evalloop(n);
258 1.1 cgd break;
259 1.1 cgd case NFOR:
260 1.1 cgd evalfor(n);
261 1.1 cgd break;
262 1.1 cgd case NCASE:
263 1.1 cgd evalcase(n, flags);
264 1.1 cgd break;
265 1.1 cgd case NDEFUN:
266 1.1 cgd defun(n->narg.text, n->narg.next);
267 1.1 cgd exitstatus = 0;
268 1.1 cgd break;
269 1.7 jtc case NNOT:
270 1.7 jtc evaltree(n->nnot.com, EV_TESTED);
271 1.7 jtc exitstatus = !exitstatus;
272 1.7 jtc break;
273 1.7 jtc
274 1.1 cgd case NPIPE:
275 1.1 cgd evalpipe(n);
276 1.1 cgd break;
277 1.1 cgd case NCMD:
278 1.1 cgd evalcommand(n, flags, (struct backcmd *)NULL);
279 1.1 cgd break;
280 1.1 cgd default:
281 1.1 cgd out1fmt("Node type = %d\n", n->type);
282 1.1 cgd flushout(&output);
283 1.1 cgd break;
284 1.1 cgd }
285 1.1 cgd out:
286 1.1 cgd if (pendingsigs)
287 1.1 cgd dotrap();
288 1.1 cgd if ((flags & EV_EXIT) || (eflag && exitstatus && !(flags & EV_TESTED)))
289 1.1 cgd exitshell(exitstatus);
290 1.1 cgd }
291 1.1 cgd
292 1.1 cgd
293 1.1 cgd STATIC void
294 1.1 cgd evalloop(n)
295 1.1 cgd union node *n;
296 1.1 cgd {
297 1.1 cgd int status;
298 1.1 cgd
299 1.1 cgd loopnest++;
300 1.1 cgd status = 0;
301 1.1 cgd for (;;) {
302 1.1 cgd evaltree(n->nbinary.ch1, EV_TESTED);
303 1.1 cgd if (evalskip) {
304 1.1 cgd skipping: if (evalskip == SKIPCONT && --skipcount <= 0) {
305 1.1 cgd evalskip = 0;
306 1.1 cgd continue;
307 1.1 cgd }
308 1.1 cgd if (evalskip == SKIPBREAK && --skipcount <= 0)
309 1.1 cgd evalskip = 0;
310 1.1 cgd break;
311 1.1 cgd }
312 1.1 cgd if (n->type == NWHILE) {
313 1.1 cgd if (exitstatus != 0)
314 1.1 cgd break;
315 1.1 cgd } else {
316 1.1 cgd if (exitstatus == 0)
317 1.1 cgd break;
318 1.1 cgd }
319 1.1 cgd evaltree(n->nbinary.ch2, 0);
320 1.1 cgd status = exitstatus;
321 1.1 cgd if (evalskip)
322 1.1 cgd goto skipping;
323 1.1 cgd }
324 1.1 cgd loopnest--;
325 1.1 cgd exitstatus = status;
326 1.1 cgd }
327 1.1 cgd
328 1.1 cgd
329 1.1 cgd
330 1.1 cgd STATIC void
331 1.1 cgd evalfor(n)
332 1.1 cgd union node *n;
333 1.1 cgd {
334 1.1 cgd struct arglist arglist;
335 1.1 cgd union node *argp;
336 1.1 cgd struct strlist *sp;
337 1.1 cgd struct stackmark smark;
338 1.1 cgd
339 1.1 cgd setstackmark(&smark);
340 1.1 cgd arglist.lastp = &arglist.list;
341 1.1 cgd for (argp = n->nfor.args ; argp ; argp = argp->narg.next) {
342 1.7 jtc expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
343 1.1 cgd if (evalskip)
344 1.1 cgd goto out;
345 1.1 cgd }
346 1.1 cgd *arglist.lastp = NULL;
347 1.1 cgd
348 1.1 cgd exitstatus = 0;
349 1.1 cgd loopnest++;
350 1.1 cgd for (sp = arglist.list ; sp ; sp = sp->next) {
351 1.1 cgd setvar(n->nfor.var, sp->text, 0);
352 1.1 cgd evaltree(n->nfor.body, 0);
353 1.1 cgd if (evalskip) {
354 1.1 cgd if (evalskip == SKIPCONT && --skipcount <= 0) {
355 1.1 cgd evalskip = 0;
356 1.1 cgd continue;
357 1.1 cgd }
358 1.1 cgd if (evalskip == SKIPBREAK && --skipcount <= 0)
359 1.1 cgd evalskip = 0;
360 1.1 cgd break;
361 1.1 cgd }
362 1.1 cgd }
363 1.1 cgd loopnest--;
364 1.1 cgd out:
365 1.1 cgd popstackmark(&smark);
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd
369 1.1 cgd
370 1.1 cgd STATIC void
371 1.1 cgd evalcase(n, flags)
372 1.1 cgd union node *n;
373 1.1 cgd {
374 1.1 cgd union node *cp;
375 1.1 cgd union node *patp;
376 1.1 cgd struct arglist arglist;
377 1.1 cgd struct stackmark smark;
378 1.1 cgd
379 1.1 cgd setstackmark(&smark);
380 1.1 cgd arglist.lastp = &arglist.list;
381 1.7 jtc expandarg(n->ncase.expr, &arglist, EXP_TILDE);
382 1.1 cgd for (cp = n->ncase.cases ; cp && evalskip == 0 ; cp = cp->nclist.next) {
383 1.1 cgd for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) {
384 1.1 cgd if (casematch(patp, arglist.list->text)) {
385 1.1 cgd if (evalskip == 0) {
386 1.1 cgd evaltree(cp->nclist.body, flags);
387 1.1 cgd }
388 1.1 cgd goto out;
389 1.1 cgd }
390 1.1 cgd }
391 1.1 cgd }
392 1.1 cgd out:
393 1.1 cgd popstackmark(&smark);
394 1.1 cgd }
395 1.1 cgd
396 1.1 cgd
397 1.1 cgd
398 1.1 cgd /*
399 1.1 cgd * Kick off a subshell to evaluate a tree.
400 1.1 cgd */
401 1.1 cgd
402 1.1 cgd STATIC void
403 1.1 cgd evalsubshell(n, flags)
404 1.1 cgd union node *n;
405 1.1 cgd {
406 1.1 cgd struct job *jp;
407 1.1 cgd int backgnd = (n->type == NBACKGND);
408 1.1 cgd
409 1.1 cgd expredir(n->nredir.redirect);
410 1.1 cgd jp = makejob(n, 1);
411 1.1 cgd if (forkshell(jp, n, backgnd) == 0) {
412 1.1 cgd if (backgnd)
413 1.1 cgd flags &=~ EV_TESTED;
414 1.1 cgd redirect(n->nredir.redirect, 0);
415 1.1 cgd evaltree(n->nredir.n, flags | EV_EXIT); /* never returns */
416 1.1 cgd }
417 1.1 cgd if (! backgnd) {
418 1.1 cgd INTOFF;
419 1.1 cgd exitstatus = waitforjob(jp);
420 1.1 cgd INTON;
421 1.1 cgd }
422 1.1 cgd }
423 1.1 cgd
424 1.1 cgd
425 1.1 cgd
426 1.1 cgd /*
427 1.1 cgd * Compute the names of the files in a redirection list.
428 1.1 cgd */
429 1.1 cgd
430 1.1 cgd STATIC void
431 1.1 cgd expredir(n)
432 1.1 cgd union node *n;
433 1.1 cgd {
434 1.1 cgd register union node *redir;
435 1.1 cgd
436 1.1 cgd for (redir = n ; redir ; redir = redir->nfile.next) {
437 1.14 jtc struct arglist fn;
438 1.14 jtc fn.lastp = &fn.list;
439 1.14 jtc switch (redir->type) {
440 1.14 jtc case NFROM:
441 1.14 jtc case NTO:
442 1.14 jtc case NAPPEND:
443 1.7 jtc expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
444 1.1 cgd redir->nfile.expfname = fn.list->text;
445 1.14 jtc break;
446 1.14 jtc case NFROMFD:
447 1.14 jtc case NTOFD:
448 1.14 jtc if (redir->ndup.vname) {
449 1.14 jtc expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
450 1.14 jtc fixredir(redir, fn.list->text, 1);
451 1.14 jtc }
452 1.14 jtc break;
453 1.1 cgd }
454 1.1 cgd }
455 1.1 cgd }
456 1.1 cgd
457 1.1 cgd
458 1.1 cgd
459 1.1 cgd /*
460 1.1 cgd * Evaluate a pipeline. All the processes in the pipeline are children
461 1.1 cgd * of the process creating the pipeline. (This differs from some versions
462 1.1 cgd * of the shell, which make the last process in a pipeline the parent
463 1.1 cgd * of all the rest.)
464 1.1 cgd */
465 1.1 cgd
466 1.1 cgd STATIC void
467 1.1 cgd evalpipe(n)
468 1.1 cgd union node *n;
469 1.1 cgd {
470 1.1 cgd struct job *jp;
471 1.1 cgd struct nodelist *lp;
472 1.1 cgd int pipelen;
473 1.1 cgd int prevfd;
474 1.1 cgd int pip[2];
475 1.1 cgd
476 1.1 cgd TRACE(("evalpipe(0x%x) called\n", (int)n));
477 1.1 cgd pipelen = 0;
478 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next)
479 1.1 cgd pipelen++;
480 1.1 cgd INTOFF;
481 1.1 cgd jp = makejob(n, pipelen);
482 1.1 cgd prevfd = -1;
483 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
484 1.1 cgd prehash(lp->n);
485 1.1 cgd pip[1] = -1;
486 1.1 cgd if (lp->next) {
487 1.1 cgd if (pipe(pip) < 0) {
488 1.1 cgd close(prevfd);
489 1.1 cgd error("Pipe call failed");
490 1.1 cgd }
491 1.1 cgd }
492 1.1 cgd if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
493 1.1 cgd INTON;
494 1.1 cgd if (prevfd > 0) {
495 1.1 cgd close(0);
496 1.1 cgd copyfd(prevfd, 0);
497 1.1 cgd close(prevfd);
498 1.1 cgd }
499 1.1 cgd if (pip[1] >= 0) {
500 1.1 cgd close(pip[0]);
501 1.1 cgd if (pip[1] != 1) {
502 1.1 cgd close(1);
503 1.1 cgd copyfd(pip[1], 1);
504 1.1 cgd close(pip[1]);
505 1.1 cgd }
506 1.1 cgd }
507 1.1 cgd evaltree(lp->n, EV_EXIT);
508 1.1 cgd }
509 1.1 cgd if (prevfd >= 0)
510 1.1 cgd close(prevfd);
511 1.1 cgd prevfd = pip[0];
512 1.1 cgd close(pip[1]);
513 1.1 cgd }
514 1.1 cgd INTON;
515 1.1 cgd if (n->npipe.backgnd == 0) {
516 1.1 cgd INTOFF;
517 1.1 cgd exitstatus = waitforjob(jp);
518 1.1 cgd TRACE(("evalpipe: job done exit status %d\n", exitstatus));
519 1.1 cgd INTON;
520 1.1 cgd }
521 1.1 cgd }
522 1.1 cgd
523 1.1 cgd
524 1.1 cgd
525 1.1 cgd /*
526 1.1 cgd * Execute a command inside back quotes. If it's a builtin command, we
527 1.1 cgd * want to save its output in a block obtained from malloc. Otherwise
528 1.1 cgd * we fork off a subprocess and get the output of the command via a pipe.
529 1.1 cgd * Should be called with interrupts off.
530 1.1 cgd */
531 1.1 cgd
532 1.1 cgd void
533 1.1 cgd evalbackcmd(n, result)
534 1.1 cgd union node *n;
535 1.1 cgd struct backcmd *result;
536 1.1 cgd {
537 1.1 cgd int pip[2];
538 1.1 cgd struct job *jp;
539 1.1 cgd struct stackmark smark; /* unnecessary */
540 1.1 cgd
541 1.1 cgd setstackmark(&smark);
542 1.1 cgd result->fd = -1;
543 1.1 cgd result->buf = NULL;
544 1.1 cgd result->nleft = 0;
545 1.1 cgd result->jp = NULL;
546 1.7 jtc exitstatus = 0;
547 1.6 cgd if (n == NULL)
548 1.7 jtc goto out;
549 1.7 jtc if (n->type == NCMD) {
550 1.1 cgd evalcommand(n, EV_BACKCMD, result);
551 1.1 cgd } else {
552 1.1 cgd if (pipe(pip) < 0)
553 1.1 cgd error("Pipe call failed");
554 1.1 cgd jp = makejob(n, 1);
555 1.1 cgd if (forkshell(jp, n, FORK_NOJOB) == 0) {
556 1.1 cgd FORCEINTON;
557 1.1 cgd close(pip[0]);
558 1.1 cgd if (pip[1] != 1) {
559 1.1 cgd close(1);
560 1.1 cgd copyfd(pip[1], 1);
561 1.1 cgd close(pip[1]);
562 1.1 cgd }
563 1.1 cgd evaltree(n, EV_EXIT);
564 1.1 cgd }
565 1.1 cgd close(pip[1]);
566 1.1 cgd result->fd = pip[0];
567 1.1 cgd result->jp = jp;
568 1.1 cgd }
569 1.7 jtc out:
570 1.1 cgd popstackmark(&smark);
571 1.1 cgd TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
572 1.1 cgd result->fd, result->buf, result->nleft, result->jp));
573 1.1 cgd }
574 1.1 cgd
575 1.1 cgd
576 1.1 cgd
577 1.1 cgd /*
578 1.1 cgd * Execute a simple command.
579 1.1 cgd */
580 1.1 cgd
581 1.1 cgd STATIC void
582 1.1 cgd evalcommand(cmd, flags, backcmd)
583 1.1 cgd union node *cmd;
584 1.1 cgd struct backcmd *backcmd;
585 1.1 cgd {
586 1.1 cgd struct stackmark smark;
587 1.1 cgd union node *argp;
588 1.1 cgd struct arglist arglist;
589 1.1 cgd struct arglist varlist;
590 1.1 cgd char **argv;
591 1.1 cgd int argc;
592 1.1 cgd char **envp;
593 1.1 cgd int varflag;
594 1.1 cgd struct strlist *sp;
595 1.1 cgd register char *p;
596 1.1 cgd int mode;
597 1.1 cgd int pip[2];
598 1.1 cgd struct cmdentry cmdentry;
599 1.1 cgd struct job *jp;
600 1.1 cgd struct jmploc jmploc;
601 1.1 cgd struct jmploc *volatile savehandler;
602 1.1 cgd char *volatile savecmdname;
603 1.1 cgd volatile struct shparam saveparam;
604 1.1 cgd struct localvar *volatile savelocalvars;
605 1.1 cgd volatile int e;
606 1.1 cgd char *lastarg;
607 1.1 cgd
608 1.1 cgd /* First expand the arguments. */
609 1.1 cgd TRACE(("evalcommand(0x%x, %d) called\n", (int)cmd, flags));
610 1.1 cgd setstackmark(&smark);
611 1.1 cgd arglist.lastp = &arglist.list;
612 1.1 cgd varlist.lastp = &varlist.list;
613 1.1 cgd varflag = 1;
614 1.1 cgd for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
615 1.1 cgd p = argp->narg.text;
616 1.1 cgd if (varflag && is_name(*p)) {
617 1.1 cgd do {
618 1.1 cgd p++;
619 1.1 cgd } while (is_in_name(*p));
620 1.1 cgd if (*p == '=') {
621 1.7 jtc expandarg(argp, &varlist, EXP_VARTILDE);
622 1.1 cgd continue;
623 1.1 cgd }
624 1.1 cgd }
625 1.7 jtc expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
626 1.1 cgd varflag = 0;
627 1.1 cgd }
628 1.1 cgd *arglist.lastp = NULL;
629 1.1 cgd *varlist.lastp = NULL;
630 1.1 cgd expredir(cmd->ncmd.redirect);
631 1.1 cgd argc = 0;
632 1.1 cgd for (sp = arglist.list ; sp ; sp = sp->next)
633 1.1 cgd argc++;
634 1.1 cgd argv = stalloc(sizeof (char *) * (argc + 1));
635 1.7 jtc
636 1.7 jtc for (sp = arglist.list ; sp ; sp = sp->next) {
637 1.7 jtc TRACE(("evalcommand arg: %s\n", sp->text));
638 1.1 cgd *argv++ = sp->text;
639 1.7 jtc }
640 1.1 cgd *argv = NULL;
641 1.1 cgd lastarg = NULL;
642 1.1 cgd if (iflag && funcnest == 0 && argc > 0)
643 1.1 cgd lastarg = argv[-1];
644 1.1 cgd argv -= argc;
645 1.1 cgd
646 1.1 cgd /* Print the command if xflag is set. */
647 1.1 cgd if (xflag) {
648 1.1 cgd outc('+', &errout);
649 1.1 cgd for (sp = varlist.list ; sp ; sp = sp->next) {
650 1.1 cgd outc(' ', &errout);
651 1.1 cgd out2str(sp->text);
652 1.1 cgd }
653 1.1 cgd for (sp = arglist.list ; sp ; sp = sp->next) {
654 1.1 cgd outc(' ', &errout);
655 1.1 cgd out2str(sp->text);
656 1.1 cgd }
657 1.1 cgd outc('\n', &errout);
658 1.1 cgd flushout(&errout);
659 1.1 cgd }
660 1.1 cgd
661 1.1 cgd /* Now locate the command. */
662 1.1 cgd if (argc == 0) {
663 1.1 cgd cmdentry.cmdtype = CMDBUILTIN;
664 1.1 cgd cmdentry.u.index = BLTINCMD;
665 1.1 cgd } else {
666 1.1 cgd find_command(argv[0], &cmdentry, 1);
667 1.1 cgd if (cmdentry.cmdtype == CMDUNKNOWN) { /* command not found */
668 1.1 cgd exitstatus = 2;
669 1.1 cgd flushout(&errout);
670 1.1 cgd return;
671 1.1 cgd }
672 1.1 cgd /* implement the bltin builtin here */
673 1.1 cgd if (cmdentry.cmdtype == CMDBUILTIN && cmdentry.u.index == BLTINCMD) {
674 1.1 cgd for (;;) {
675 1.1 cgd argv++;
676 1.1 cgd if (--argc == 0)
677 1.1 cgd break;
678 1.1 cgd if ((cmdentry.u.index = find_builtin(*argv)) < 0) {
679 1.1 cgd outfmt(&errout, "%s: not found\n", *argv);
680 1.1 cgd exitstatus = 2;
681 1.1 cgd flushout(&errout);
682 1.1 cgd return;
683 1.1 cgd }
684 1.1 cgd if (cmdentry.u.index != BLTINCMD)
685 1.1 cgd break;
686 1.1 cgd }
687 1.1 cgd }
688 1.1 cgd }
689 1.1 cgd
690 1.1 cgd /* Fork off a child process if necessary. */
691 1.1 cgd if (cmd->ncmd.backgnd
692 1.1 cgd || cmdentry.cmdtype == CMDNORMAL && (flags & EV_EXIT) == 0
693 1.1 cgd || (flags & EV_BACKCMD) != 0
694 1.1 cgd && (cmdentry.cmdtype != CMDBUILTIN
695 1.1 cgd || cmdentry.u.index == DOTCMD
696 1.1 cgd || cmdentry.u.index == EVALCMD)) {
697 1.1 cgd jp = makejob(cmd, 1);
698 1.1 cgd mode = cmd->ncmd.backgnd;
699 1.1 cgd if (flags & EV_BACKCMD) {
700 1.1 cgd mode = FORK_NOJOB;
701 1.1 cgd if (pipe(pip) < 0)
702 1.1 cgd error("Pipe call failed");
703 1.1 cgd }
704 1.1 cgd if (forkshell(jp, cmd, mode) != 0)
705 1.1 cgd goto parent; /* at end of routine */
706 1.1 cgd if (flags & EV_BACKCMD) {
707 1.1 cgd FORCEINTON;
708 1.1 cgd close(pip[0]);
709 1.1 cgd if (pip[1] != 1) {
710 1.1 cgd close(1);
711 1.1 cgd copyfd(pip[1], 1);
712 1.1 cgd close(pip[1]);
713 1.1 cgd }
714 1.1 cgd }
715 1.1 cgd flags |= EV_EXIT;
716 1.1 cgd }
717 1.1 cgd
718 1.1 cgd /* This is the child process if a fork occurred. */
719 1.1 cgd /* Execute the command. */
720 1.1 cgd if (cmdentry.cmdtype == CMDFUNCTION) {
721 1.1 cgd trputs("Shell function: "); trargs(argv);
722 1.1 cgd redirect(cmd->ncmd.redirect, REDIR_PUSH);
723 1.1 cgd saveparam = shellparam;
724 1.1 cgd shellparam.malloc = 0;
725 1.1 cgd shellparam.nparam = argc - 1;
726 1.1 cgd shellparam.p = argv + 1;
727 1.1 cgd shellparam.optnext = NULL;
728 1.1 cgd INTOFF;
729 1.1 cgd savelocalvars = localvars;
730 1.1 cgd localvars = NULL;
731 1.1 cgd INTON;
732 1.1 cgd if (setjmp(jmploc.loc)) {
733 1.1 cgd if (exception == EXSHELLPROC)
734 1.1 cgd freeparam((struct shparam *)&saveparam);
735 1.1 cgd else {
736 1.1 cgd freeparam(&shellparam);
737 1.1 cgd shellparam = saveparam;
738 1.1 cgd }
739 1.1 cgd poplocalvars();
740 1.1 cgd localvars = savelocalvars;
741 1.1 cgd handler = savehandler;
742 1.1 cgd longjmp(handler->loc, 1);
743 1.1 cgd }
744 1.1 cgd savehandler = handler;
745 1.1 cgd handler = &jmploc;
746 1.1 cgd for (sp = varlist.list ; sp ; sp = sp->next)
747 1.1 cgd mklocal(sp->text);
748 1.1 cgd funcnest++;
749 1.1 cgd evaltree(cmdentry.u.func, 0);
750 1.1 cgd funcnest--;
751 1.1 cgd INTOFF;
752 1.1 cgd poplocalvars();
753 1.1 cgd localvars = savelocalvars;
754 1.1 cgd freeparam(&shellparam);
755 1.1 cgd shellparam = saveparam;
756 1.1 cgd handler = savehandler;
757 1.1 cgd popredir();
758 1.1 cgd INTON;
759 1.1 cgd if (evalskip == SKIPFUNC) {
760 1.1 cgd evalskip = 0;
761 1.1 cgd skipcount = 0;
762 1.1 cgd }
763 1.1 cgd if (flags & EV_EXIT)
764 1.1 cgd exitshell(exitstatus);
765 1.1 cgd } else if (cmdentry.cmdtype == CMDBUILTIN) {
766 1.1 cgd trputs("builtin command: "); trargs(argv);
767 1.1 cgd mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH;
768 1.1 cgd if (flags == EV_BACKCMD) {
769 1.1 cgd memout.nleft = 0;
770 1.1 cgd memout.nextc = memout.buf;
771 1.1 cgd memout.bufsize = 64;
772 1.1 cgd mode |= REDIR_BACKQ;
773 1.1 cgd }
774 1.1 cgd redirect(cmd->ncmd.redirect, mode);
775 1.1 cgd savecmdname = commandname;
776 1.1 cgd cmdenviron = varlist.list;
777 1.1 cgd e = -1;
778 1.1 cgd if (setjmp(jmploc.loc)) {
779 1.1 cgd e = exception;
780 1.1 cgd exitstatus = (e == EXINT)? SIGINT+128 : 2;
781 1.1 cgd goto cmddone;
782 1.1 cgd }
783 1.1 cgd savehandler = handler;
784 1.1 cgd handler = &jmploc;
785 1.1 cgd commandname = argv[0];
786 1.1 cgd argptr = argv + 1;
787 1.1 cgd optptr = NULL; /* initialize nextopt */
788 1.1 cgd exitstatus = (*builtinfunc[cmdentry.u.index])(argc, argv);
789 1.1 cgd flushall();
790 1.1 cgd cmddone:
791 1.1 cgd out1 = &output;
792 1.1 cgd out2 = &errout;
793 1.1 cgd freestdout();
794 1.1 cgd if (e != EXSHELLPROC) {
795 1.1 cgd commandname = savecmdname;
796 1.1 cgd if (flags & EV_EXIT) {
797 1.1 cgd exitshell(exitstatus);
798 1.1 cgd }
799 1.1 cgd }
800 1.1 cgd handler = savehandler;
801 1.1 cgd if (e != -1) {
802 1.1 cgd if (e != EXERROR || cmdentry.u.index == BLTINCMD
803 1.1 cgd || cmdentry.u.index == DOTCMD
804 1.1 cgd || cmdentry.u.index == EVALCMD
805 1.10 cgd #ifndef NO_HISTORY
806 1.7 jtc || cmdentry.u.index == HISTCMD
807 1.10 cgd #endif
808 1.1 cgd || cmdentry.u.index == EXECCMD)
809 1.1 cgd exraise(e);
810 1.1 cgd FORCEINTON;
811 1.1 cgd }
812 1.1 cgd if (cmdentry.u.index != EXECCMD)
813 1.1 cgd popredir();
814 1.1 cgd if (flags == EV_BACKCMD) {
815 1.1 cgd backcmd->buf = memout.buf;
816 1.1 cgd backcmd->nleft = memout.nextc - memout.buf;
817 1.1 cgd memout.buf = NULL;
818 1.1 cgd }
819 1.1 cgd } else {
820 1.1 cgd trputs("normal command: "); trargs(argv);
821 1.1 cgd clearredir();
822 1.1 cgd redirect(cmd->ncmd.redirect, 0);
823 1.1 cgd if (varlist.list) {
824 1.1 cgd p = stalloc(strlen(pathval()) + 1);
825 1.1 cgd scopy(pathval(), p);
826 1.1 cgd } else {
827 1.1 cgd p = pathval();
828 1.1 cgd }
829 1.1 cgd for (sp = varlist.list ; sp ; sp = sp->next)
830 1.1 cgd setvareq(sp->text, VEXPORT|VSTACK);
831 1.1 cgd envp = environment();
832 1.1 cgd shellexec(argv, envp, p, cmdentry.u.index);
833 1.1 cgd /*NOTREACHED*/
834 1.1 cgd }
835 1.1 cgd goto out;
836 1.1 cgd
837 1.1 cgd parent: /* parent process gets here (if we forked) */
838 1.1 cgd if (mode == 0) { /* argument to fork */
839 1.1 cgd INTOFF;
840 1.1 cgd exitstatus = waitforjob(jp);
841 1.1 cgd INTON;
842 1.1 cgd } else if (mode == 2) {
843 1.1 cgd backcmd->fd = pip[0];
844 1.1 cgd close(pip[1]);
845 1.1 cgd backcmd->jp = jp;
846 1.1 cgd }
847 1.1 cgd
848 1.1 cgd out:
849 1.1 cgd if (lastarg)
850 1.1 cgd setvar("_", lastarg, 0);
851 1.1 cgd popstackmark(&smark);
852 1.1 cgd }
853 1.1 cgd
854 1.1 cgd
855 1.1 cgd
856 1.1 cgd /*
857 1.1 cgd * Search for a command. This is called before we fork so that the
858 1.1 cgd * location of the command will be available in the parent as well as
859 1.1 cgd * the child. The check for "goodname" is an overly conservative
860 1.1 cgd * check that the name will not be subject to expansion.
861 1.1 cgd */
862 1.1 cgd
863 1.1 cgd STATIC void
864 1.1 cgd prehash(n)
865 1.1 cgd union node *n;
866 1.1 cgd {
867 1.1 cgd struct cmdentry entry;
868 1.1 cgd
869 1.1 cgd if (n->type == NCMD && goodname(n->ncmd.args->narg.text))
870 1.1 cgd find_command(n->ncmd.args->narg.text, &entry, 0);
871 1.1 cgd }
872 1.1 cgd
873 1.1 cgd
874 1.1 cgd
875 1.1 cgd /*
876 1.1 cgd * Builtin commands. Builtin commands whose functions are closely
877 1.1 cgd * tied to evaluation are implemented here.
878 1.1 cgd */
879 1.1 cgd
880 1.1 cgd /*
881 1.1 cgd * No command given, or a bltin command with no arguments. Set the
882 1.1 cgd * specified variables.
883 1.1 cgd */
884 1.1 cgd
885 1.1 cgd bltincmd(argc, argv) char **argv; {
886 1.1 cgd listsetvar(cmdenviron);
887 1.13 jtc return 0;
888 1.1 cgd }
889 1.1 cgd
890 1.1 cgd
891 1.1 cgd /*
892 1.1 cgd * Handle break and continue commands. Break, continue, and return are
893 1.1 cgd * all handled by setting the evalskip flag. The evaluation routines
894 1.1 cgd * above all check this flag, and if it is set they start skipping
895 1.1 cgd * commands rather than executing them. The variable skipcount is
896 1.1 cgd * the number of loops to break/continue, or the number of function
897 1.1 cgd * levels to return. (The latter is always 1.) It should probably
898 1.1 cgd * be an error to break out of more loops than exist, but it isn't
899 1.1 cgd * in the standard shell so we don't make it one here.
900 1.1 cgd */
901 1.1 cgd
902 1.1 cgd breakcmd(argc, argv) char **argv; {
903 1.1 cgd int n;
904 1.1 cgd
905 1.1 cgd n = 1;
906 1.1 cgd if (argc > 1)
907 1.1 cgd n = number(argv[1]);
908 1.1 cgd if (n > loopnest)
909 1.1 cgd n = loopnest;
910 1.1 cgd if (n > 0) {
911 1.1 cgd evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK;
912 1.1 cgd skipcount = n;
913 1.1 cgd }
914 1.1 cgd return 0;
915 1.1 cgd }
916 1.1 cgd
917 1.1 cgd
918 1.1 cgd /*
919 1.1 cgd * The return command.
920 1.1 cgd */
921 1.1 cgd
922 1.1 cgd returncmd(argc, argv) char **argv; {
923 1.1 cgd int ret;
924 1.1 cgd
925 1.1 cgd ret = exitstatus;
926 1.1 cgd if (argc > 1)
927 1.1 cgd ret = number(argv[1]);
928 1.1 cgd if (funcnest) {
929 1.1 cgd evalskip = SKIPFUNC;
930 1.1 cgd skipcount = 1;
931 1.1 cgd }
932 1.1 cgd return ret;
933 1.1 cgd }
934 1.1 cgd
935 1.1 cgd
936 1.8 jtc falsecmd(argc, argv) char **argv; {
937 1.8 jtc return 1;
938 1.8 jtc }
939 1.8 jtc
940 1.8 jtc
941 1.1 cgd truecmd(argc, argv) char **argv; {
942 1.1 cgd return 0;
943 1.1 cgd }
944 1.1 cgd
945 1.1 cgd
946 1.1 cgd execcmd(argc, argv) char **argv; {
947 1.1 cgd if (argc > 1) {
948 1.1 cgd iflag = 0; /* exit on error */
949 1.7 jtc mflag = 0;
950 1.7 jtc optschanged();
951 1.1 cgd shellexec(argv + 1, environment(), pathval(), 0);
952 1.1 cgd
953 1.1 cgd }
954 1.1 cgd return 0;
955 1.1 cgd }
956