| @@ -1,1531 +1,1531 @@ | | | @@ -1,1531 +1,1531 @@ |
1 | /* $NetBSD: eval.c,v 1.140.2.5 2018/08/25 14:22:49 martin Exp $ */ | | 1 | /* $NetBSD: eval.c,v 1.140.2.6 2018/08/25 14:48:22 martin Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1993 | | 4 | * Copyright (c) 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to Berkeley by | | 7 | * This code is derived from software contributed to Berkeley by |
8 | * Kenneth Almquist. | | 8 | * Kenneth Almquist. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * 3. Neither the name of the University nor the names of its contributors | | 18 | * 3. Neither the name of the University nor the names of its contributors |
19 | * may be used to endorse or promote products derived from this software | | 19 | * may be used to endorse or promote products derived from this software |
20 | * without specific prior written permission. | | 20 | * without specific prior written permission. |
21 | * | | 21 | * |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | | 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * SUCH DAMAGE. | | 32 | * SUCH DAMAGE. |
33 | */ | | 33 | */ |
34 | | | 34 | |
35 | #include <sys/cdefs.h> | | 35 | #include <sys/cdefs.h> |
36 | #ifndef lint | | 36 | #ifndef lint |
37 | #if 0 | | 37 | #if 0 |
38 | static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; | | 38 | static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95"; |
39 | #else | | 39 | #else |
40 | __RCSID("$NetBSD: eval.c,v 1.140.2.5 2018/08/25 14:22:49 martin Exp $"); | | 40 | __RCSID("$NetBSD: eval.c,v 1.140.2.6 2018/08/25 14:48:22 martin Exp $"); |
41 | #endif | | 41 | #endif |
42 | #endif /* not lint */ | | 42 | #endif /* not lint */ |
43 | | | 43 | |
44 | #include <stdbool.h> | | 44 | #include <stdbool.h> |
45 | #include <stdlib.h> | | 45 | #include <stdlib.h> |
46 | #include <signal.h> | | 46 | #include <signal.h> |
47 | #include <stdio.h> | | 47 | #include <stdio.h> |
48 | #include <string.h> | | 48 | #include <string.h> |
49 | #include <errno.h> | | 49 | #include <errno.h> |
50 | #include <limits.h> | | 50 | #include <limits.h> |
51 | #include <unistd.h> | | 51 | #include <unistd.h> |
52 | #include <sys/fcntl.h> | | 52 | #include <sys/fcntl.h> |
53 | #include <sys/stat.h> | | 53 | #include <sys/stat.h> |
54 | #include <sys/times.h> | | 54 | #include <sys/times.h> |
55 | #include <sys/param.h> | | 55 | #include <sys/param.h> |
56 | #include <sys/types.h> | | 56 | #include <sys/types.h> |
57 | #include <sys/wait.h> | | 57 | #include <sys/wait.h> |
58 | #include <sys/sysctl.h> | | 58 | #include <sys/sysctl.h> |
59 | | | 59 | |
60 | /* | | 60 | /* |
61 | * Evaluate a command. | | 61 | * Evaluate a command. |
62 | */ | | 62 | */ |
63 | | | 63 | |
64 | #include "shell.h" | | 64 | #include "shell.h" |
65 | #include "nodes.h" | | 65 | #include "nodes.h" |
66 | #include "syntax.h" | | 66 | #include "syntax.h" |
67 | #include "expand.h" | | 67 | #include "expand.h" |
68 | #include "parser.h" | | 68 | #include "parser.h" |
69 | #include "jobs.h" | | 69 | #include "jobs.h" |
70 | #include "eval.h" | | 70 | #include "eval.h" |
71 | #include "builtins.h" | | 71 | #include "builtins.h" |
72 | #include "options.h" | | 72 | #include "options.h" |
73 | #include "exec.h" | | 73 | #include "exec.h" |
74 | #include "redir.h" | | 74 | #include "redir.h" |
75 | #include "input.h" | | 75 | #include "input.h" |
76 | #include "output.h" | | 76 | #include "output.h" |
77 | #include "trap.h" | | 77 | #include "trap.h" |
78 | #include "var.h" | | 78 | #include "var.h" |
79 | #include "memalloc.h" | | 79 | #include "memalloc.h" |
80 | #include "error.h" | | 80 | #include "error.h" |
81 | #include "show.h" | | 81 | #include "show.h" |
82 | #include "mystring.h" | | 82 | #include "mystring.h" |
83 | #include "main.h" | | 83 | #include "main.h" |
84 | #ifndef SMALL | | 84 | #ifndef SMALL |
85 | #include "nodenames.h" | | 85 | #include "nodenames.h" |
86 | #include "myhistedit.h" | | 86 | #include "myhistedit.h" |
87 | #endif | | 87 | #endif |
88 | | | 88 | |
89 | | | 89 | |
90 | STATIC enum skipstate evalskip; /* != SKIPNONE if we are skipping commands */ | | 90 | STATIC enum skipstate evalskip; /* != SKIPNONE if we are skipping commands */ |
91 | STATIC int skipcount; /* number of levels to skip */ | | 91 | STATIC int skipcount; /* number of levels to skip */ |
92 | STATIC int loopnest; /* current loop nesting level */ | | 92 | STATIC int loopnest; /* current loop nesting level */ |
93 | STATIC int funcnest; /* depth of function calls */ | | 93 | STATIC int funcnest; /* depth of function calls */ |
94 | STATIC int builtin_flags; /* evalcommand flags for builtins */ | | 94 | STATIC int builtin_flags; /* evalcommand flags for builtins */ |
95 | /* | | 95 | /* |
96 | * Base function nesting level inside a dot command. Set to 0 initially | | 96 | * Base function nesting level inside a dot command. Set to 0 initially |
97 | * and to (funcnest + 1) before every dot command to enable | | 97 | * and to (funcnest + 1) before every dot command to enable |
98 | * 1) detection of being in a file sourced by a dot command and | | 98 | * 1) detection of being in a file sourced by a dot command and |
99 | * 2) counting of function nesting in that file for the implementation | | 99 | * 2) counting of function nesting in that file for the implementation |
100 | * of the return command. | | 100 | * of the return command. |
101 | * The value is reset to its previous value after the dot command. | | 101 | * The value is reset to its previous value after the dot command. |
102 | */ | | 102 | */ |
103 | STATIC int dot_funcnest; | | 103 | STATIC int dot_funcnest; |
104 | | | 104 | |
105 | | | 105 | |
106 | const char *commandname; | | 106 | const char *commandname; |
107 | struct strlist *cmdenviron; | | 107 | struct strlist *cmdenviron; |
108 | int exitstatus; /* exit status of last command */ | | 108 | int exitstatus; /* exit status of last command */ |
109 | int back_exitstatus; /* exit status of backquoted command */ | | 109 | int back_exitstatus; /* exit status of backquoted command */ |
110 | | | 110 | |
111 | | | 111 | |
112 | STATIC void evalloop(union node *, int); | | 112 | STATIC void evalloop(union node *, int); |
113 | STATIC void evalfor(union node *, int); | | 113 | STATIC void evalfor(union node *, int); |
114 | STATIC void evalcase(union node *, int); | | 114 | STATIC void evalcase(union node *, int); |
115 | STATIC void evalsubshell(union node *, int); | | 115 | STATIC void evalsubshell(union node *, int); |
116 | STATIC void expredir(union node *); | | 116 | STATIC void expredir(union node *); |
117 | STATIC void evalpipe(union node *); | | 117 | STATIC void evalpipe(union node *); |
118 | STATIC void evalcommand(union node *, int, struct backcmd *); | | 118 | STATIC void evalcommand(union node *, int, struct backcmd *); |
119 | STATIC void prehash(union node *); | | 119 | STATIC void prehash(union node *); |
120 | | | 120 | |
121 | STATIC char *find_dot_file(char *); | | 121 | STATIC char *find_dot_file(char *); |
122 | | | 122 | |
123 | /* | | 123 | /* |
124 | * Called to reset things after an exception. | | 124 | * Called to reset things after an exception. |
125 | */ | | 125 | */ |
126 | | | 126 | |
127 | #ifdef mkinit | | 127 | #ifdef mkinit |
128 | INCLUDE "eval.h" | | 128 | INCLUDE "eval.h" |
129 | | | 129 | |
130 | RESET { | | 130 | RESET { |
131 | reset_eval(); | | 131 | reset_eval(); |
132 | } | | 132 | } |
133 | | | 133 | |
134 | SHELLPROC { | | 134 | SHELLPROC { |
135 | exitstatus = 0; | | 135 | exitstatus = 0; |
136 | } | | 136 | } |
137 | #endif | | 137 | #endif |
138 | | | 138 | |
139 | void | | 139 | void |
140 | reset_eval(void) | | 140 | reset_eval(void) |
141 | { | | 141 | { |
142 | evalskip = SKIPNONE; | | 142 | evalskip = SKIPNONE; |
143 | dot_funcnest = 0; | | 143 | dot_funcnest = 0; |
144 | loopnest = 0; | | 144 | loopnest = 0; |
145 | funcnest = 0; | | 145 | funcnest = 0; |
146 | } | | 146 | } |
147 | | | 147 | |
148 | static int | | 148 | static int |
149 | sh_pipe(int fds[2]) | | 149 | sh_pipe(int fds[2]) |
150 | { | | 150 | { |
151 | int nfd; | | 151 | int nfd; |
152 | | | 152 | |
153 | if (pipe(fds)) | | 153 | if (pipe(fds)) |
154 | return -1; | | 154 | return -1; |
155 | | | 155 | |
156 | if (fds[0] < 3) { | | 156 | if (fds[0] < 3) { |
157 | nfd = fcntl(fds[0], F_DUPFD, 3); | | 157 | nfd = fcntl(fds[0], F_DUPFD, 3); |
158 | if (nfd != -1) { | | 158 | if (nfd != -1) { |
159 | close(fds[0]); | | 159 | close(fds[0]); |
160 | fds[0] = nfd; | | 160 | fds[0] = nfd; |
161 | } | | 161 | } |
162 | } | | 162 | } |
163 | | | 163 | |
164 | if (fds[1] < 3) { | | 164 | if (fds[1] < 3) { |
165 | nfd = fcntl(fds[1], F_DUPFD, 3); | | 165 | nfd = fcntl(fds[1], F_DUPFD, 3); |
166 | if (nfd != -1) { | | 166 | if (nfd != -1) { |
167 | close(fds[1]); | | 167 | close(fds[1]); |
168 | fds[1] = nfd; | | 168 | fds[1] = nfd; |
169 | } | | 169 | } |
170 | } | | 170 | } |
171 | return 0; | | 171 | return 0; |
172 | } | | 172 | } |
173 | | | 173 | |
174 | | | 174 | |
175 | /* | | 175 | /* |
176 | * The eval commmand. | | 176 | * The eval commmand. |
177 | */ | | 177 | */ |
178 | | | 178 | |
179 | int | | 179 | int |
180 | evalcmd(int argc, char **argv) | | 180 | evalcmd(int argc, char **argv) |
181 | { | | 181 | { |
182 | char *p; | | 182 | char *p; |
183 | char *concat; | | 183 | char *concat; |
184 | char **ap; | | 184 | char **ap; |
185 | | | 185 | |
186 | if (argc > 1) { | | 186 | if (argc > 1) { |
187 | p = argv[1]; | | 187 | p = argv[1]; |
188 | if (argc > 2) { | | 188 | if (argc > 2) { |
189 | STARTSTACKSTR(concat); | | 189 | STARTSTACKSTR(concat); |
190 | ap = argv + 2; | | 190 | ap = argv + 2; |
191 | for (;;) { | | 191 | for (;;) { |
192 | while (*p) | | 192 | while (*p) |
193 | STPUTC(*p++, concat); | | 193 | STPUTC(*p++, concat); |
194 | if ((p = *ap++) == NULL) | | 194 | if ((p = *ap++) == NULL) |
195 | break; | | 195 | break; |
196 | STPUTC(' ', concat); | | 196 | STPUTC(' ', concat); |
197 | } | | 197 | } |
198 | STPUTC('\0', concat); | | 198 | STPUTC('\0', concat); |
199 | p = grabstackstr(concat); | | 199 | p = grabstackstr(concat); |
200 | } | | 200 | } |
201 | evalstring(p, builtin_flags & EV_TESTED); | | 201 | evalstring(p, builtin_flags & EV_TESTED); |
202 | } else | | 202 | } else |
203 | exitstatus = 0; | | 203 | exitstatus = 0; |
204 | return exitstatus; | | 204 | return exitstatus; |
205 | } | | 205 | } |
206 | | | 206 | |
207 | | | 207 | |
208 | /* | | 208 | /* |
209 | * Execute a command or commands contained in a string. | | 209 | * Execute a command or commands contained in a string. |
210 | */ | | 210 | */ |
211 | | | 211 | |
212 | void | | 212 | void |
213 | evalstring(char *s, int flag) | | 213 | evalstring(char *s, int flag) |
214 | { | | 214 | { |
215 | union node *n; | | 215 | union node *n; |
216 | struct stackmark smark; | | 216 | struct stackmark smark; |
217 | | | 217 | |
218 | setstackmark(&smark); | | 218 | setstackmark(&smark); |
219 | setinputstring(s, 1, line_number); | | 219 | setinputstring(s, 1, line_number); |
220 | | | 220 | |
221 | while ((n = parsecmd(0)) != NEOF) { | | 221 | while ((n = parsecmd(0)) != NEOF) { |
222 | XTRACE(DBG_EVAL, ("evalstring: "), showtree(n)); | | 222 | XTRACE(DBG_EVAL, ("evalstring: "), showtree(n)); |
223 | if (n && nflag == 0) | | 223 | if (n && nflag == 0) |
224 | evaltree(n, flag); | | 224 | evaltree(n, flag); |
225 | popstackmark(&smark); | | 225 | popstackmark(&smark); |
226 | } | | 226 | } |
227 | popfile(); | | 227 | popfile(); |
228 | popstackmark(&smark); | | 228 | popstackmark(&smark); |
229 | } | | 229 | } |
230 | | | 230 | |
231 | | | 231 | |
232 | | | 232 | |
233 | /* | | 233 | /* |
234 | * Evaluate a parse tree. The value is left in the global variable | | 234 | * Evaluate a parse tree. The value is left in the global variable |
235 | * exitstatus. | | 235 | * exitstatus. |
236 | */ | | 236 | */ |
237 | | | 237 | |
238 | void | | 238 | void |
239 | evaltree(union node *n, int flags) | | 239 | evaltree(union node *n, int flags) |
240 | { | | 240 | { |
241 | bool do_etest; | | 241 | bool do_etest; |
242 | int sflags = flags & ~EV_EXIT; | | 242 | int sflags = flags & ~EV_EXIT; |
243 | | | 243 | |
244 | do_etest = false; | | 244 | do_etest = false; |
245 | if (n == NULL || nflag) { | | 245 | if (n == NULL || nflag) { |
246 | VTRACE(DBG_EVAL, ("evaltree(%s) called\n", | | 246 | VTRACE(DBG_EVAL, ("evaltree(%s) called\n", |
247 | n == NULL ? "NULL" : "-n")); | | 247 | n == NULL ? "NULL" : "-n")); |
248 | if (nflag == 0) | | 248 | if (nflag == 0) |
249 | exitstatus = 0; | | 249 | exitstatus = 0; |
250 | goto out; | | 250 | goto out; |
251 | } | | 251 | } |
252 | #ifndef SMALL | | 252 | #ifndef SMALL |
253 | displayhist = 1; /* show history substitutions done with fc */ | | 253 | displayhist = 1; /* show history substitutions done with fc */ |
254 | #endif | | 254 | #endif |
255 | CTRACE(DBG_EVAL, ("pid %d, evaltree(%p: %s(%d), %#x) called\n", | | 255 | CTRACE(DBG_EVAL, ("pid %d, evaltree(%p: %s(%d), %#x) called\n", |
256 | getpid(), n, NODETYPENAME(n->type), n->type, flags)); | | 256 | getpid(), n, NODETYPENAME(n->type), n->type, flags)); |
257 | switch (n->type) { | | 257 | switch (n->type) { |
258 | case NSEMI: | | 258 | case NSEMI: |
259 | evaltree(n->nbinary.ch1, flags & EV_TESTED); | | 259 | evaltree(n->nbinary.ch1, flags & EV_TESTED); |
260 | if (nflag || evalskip) | | 260 | if (nflag || evalskip) |
261 | goto out; | | 261 | goto out; |
262 | evaltree(n->nbinary.ch2, flags); | | 262 | evaltree(n->nbinary.ch2, flags); |
263 | break; | | 263 | break; |
264 | case NAND: | | 264 | case NAND: |
265 | evaltree(n->nbinary.ch1, EV_TESTED); | | 265 | evaltree(n->nbinary.ch1, EV_TESTED); |
266 | if (nflag || evalskip || exitstatus != 0) | | 266 | if (nflag || evalskip || exitstatus != 0) |
267 | goto out; | | 267 | goto out; |
268 | evaltree(n->nbinary.ch2, flags); | | 268 | evaltree(n->nbinary.ch2, flags); |
269 | break; | | 269 | break; |
270 | case NOR: | | 270 | case NOR: |
271 | evaltree(n->nbinary.ch1, EV_TESTED); | | 271 | evaltree(n->nbinary.ch1, EV_TESTED); |
272 | if (nflag || evalskip || exitstatus == 0) | | 272 | if (nflag || evalskip || exitstatus == 0) |
273 | goto out; | | 273 | goto out; |
274 | evaltree(n->nbinary.ch2, flags); | | 274 | evaltree(n->nbinary.ch2, flags); |
275 | break; | | 275 | break; |
276 | case NREDIR: | | 276 | case NREDIR: |
277 | expredir(n->nredir.redirect); | | 277 | expredir(n->nredir.redirect); |
278 | if (xflag && n->nredir.redirect) { | | 278 | if (xflag && n->nredir.redirect) { |
279 | union node *rn; | | 279 | union node *rn; |
280 | | | 280 | |
281 | out2str(expandstr(ps4val(), line_number)); | | 281 | out2str(expandstr(ps4val(), line_number)); |
282 | out2str("using redirections:"); | | 282 | out2str("using redirections:"); |
283 | for (rn = n->nredir.redirect; rn; rn = rn->nfile.next) | | 283 | for (rn = n->nredir.redirect; rn; rn = rn->nfile.next) |
284 | (void) outredir(&errout, rn, ' '); | | 284 | (void) outredir(&errout, rn, ' '); |
285 | out2str(" do\n"); | | 285 | out2str(" do\n"); |
286 | flushout(&errout); | | 286 | flushout(&errout); |
287 | } | | 287 | } |
288 | redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP); | | 288 | redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP); |
289 | evaltree(n->nredir.n, flags); | | 289 | evaltree(n->nredir.n, flags); |
290 | popredir(); | | 290 | popredir(); |
291 | if (xflag && n->nredir.redirect) { | | 291 | if (xflag && n->nredir.redirect) { |
292 | out2str(expandstr(ps4val(), line_number)); | | 292 | out2str(expandstr(ps4val(), line_number)); |
293 | out2str("done\n"); | | 293 | out2str("done\n"); |
294 | flushout(&errout); | | 294 | flushout(&errout); |
295 | } | | 295 | } |
296 | break; | | 296 | break; |
297 | case NSUBSHELL: | | 297 | case NSUBSHELL: |
298 | evalsubshell(n, flags); | | 298 | evalsubshell(n, flags); |
299 | do_etest = !(flags & EV_TESTED); | | 299 | do_etest = !(flags & EV_TESTED); |
300 | break; | | 300 | break; |
301 | case NBACKGND: | | 301 | case NBACKGND: |
302 | evalsubshell(n, flags); | | 302 | evalsubshell(n, flags); |
303 | break; | | 303 | break; |
304 | case NIF: { | | 304 | case NIF: { |
305 | evaltree(n->nif.test, EV_TESTED); | | 305 | evaltree(n->nif.test, EV_TESTED); |
306 | if (nflag || evalskip) | | 306 | if (nflag || evalskip) |
307 | goto out; | | 307 | goto out; |
308 | if (exitstatus == 0) | | 308 | if (exitstatus == 0) |
309 | evaltree(n->nif.ifpart, flags); | | 309 | evaltree(n->nif.ifpart, flags); |
310 | else if (n->nif.elsepart) | | 310 | else if (n->nif.elsepart) |
311 | evaltree(n->nif.elsepart, flags); | | 311 | evaltree(n->nif.elsepart, flags); |
312 | else | | 312 | else |
313 | exitstatus = 0; | | 313 | exitstatus = 0; |
314 | break; | | 314 | break; |
315 | } | | 315 | } |
316 | case NWHILE: | | 316 | case NWHILE: |
317 | case NUNTIL: | | 317 | case NUNTIL: |
318 | evalloop(n, sflags); | | 318 | evalloop(n, sflags); |
319 | break; | | 319 | break; |
320 | case NFOR: | | 320 | case NFOR: |
321 | evalfor(n, sflags); | | 321 | evalfor(n, sflags); |
322 | break; | | 322 | break; |
323 | case NCASE: | | 323 | case NCASE: |
324 | evalcase(n, sflags); | | 324 | evalcase(n, sflags); |
325 | break; | | 325 | break; |
326 | case NDEFUN: | | 326 | case NDEFUN: |
327 | CTRACE(DBG_EVAL, ("Defining fn %s @%d%s\n", n->narg.text, | | 327 | CTRACE(DBG_EVAL, ("Defining fn %s @%d%s\n", n->narg.text, |
328 | n->narg.lineno, fnline1 ? " LINENO=1" : "")); | | 328 | n->narg.lineno, fnline1 ? " LINENO=1" : "")); |
329 | defun(n->narg.text, n->narg.next, n->narg.lineno); | | 329 | defun(n->narg.text, n->narg.next, n->narg.lineno); |
330 | exitstatus = 0; | | 330 | exitstatus = 0; |
331 | break; | | 331 | break; |
332 | case NNOT: | | 332 | case NNOT: |
333 | evaltree(n->nnot.com, EV_TESTED); | | 333 | evaltree(n->nnot.com, EV_TESTED); |
334 | exitstatus = !exitstatus; | | 334 | exitstatus = !exitstatus; |
335 | break; | | 335 | break; |
336 | case NDNOT: | | 336 | case NDNOT: |
337 | evaltree(n->nnot.com, EV_TESTED); | | 337 | evaltree(n->nnot.com, EV_TESTED); |
338 | if (exitstatus != 0) | | 338 | if (exitstatus != 0) |
339 | exitstatus = 1; | | 339 | exitstatus = 1; |
340 | break; | | 340 | break; |
341 | case NPIPE: | | 341 | case NPIPE: |
342 | evalpipe(n); | | 342 | evalpipe(n); |
343 | do_etest = !(flags & EV_TESTED); | | 343 | do_etest = !(flags & EV_TESTED); |
344 | break; | | 344 | break; |
345 | case NCMD: | | 345 | case NCMD: |
346 | evalcommand(n, flags, NULL); | | 346 | evalcommand(n, flags, NULL); |
347 | do_etest = !(flags & EV_TESTED); | | 347 | do_etest = !(flags & EV_TESTED); |
348 | break; | | 348 | break; |
349 | default: | | 349 | default: |
350 | #ifdef NODETYPENAME | | 350 | #ifdef NODETYPENAME |
351 | out1fmt("Node type = %d(%s)\n", n->type, NODETYPENAME(n->type)); | | 351 | out1fmt("Node type = %d(%s)\n", n->type, NODETYPENAME(n->type)); |
352 | #else | | 352 | #else |
353 | out1fmt("Node type = %d\n", n->type); | | 353 | out1fmt("Node type = %d\n", n->type); |
354 | #endif | | 354 | #endif |
355 | flushout(&output); | | 355 | flushout(&output); |
356 | break; | | 356 | break; |
357 | } | | 357 | } |
358 | out: | | 358 | out: |
359 | if (pendingsigs) | | 359 | if (pendingsigs) |
360 | dotrap(); | | 360 | dotrap(); |
361 | if ((flags & EV_EXIT) != 0 || (eflag && exitstatus != 0 && do_etest)) | | 361 | if ((flags & EV_EXIT) != 0 || (eflag && exitstatus != 0 && do_etest)) |
362 | exitshell(exitstatus); | | 362 | exitshell(exitstatus); |
363 | } | | 363 | } |
364 | | | 364 | |
365 | | | 365 | |
366 | STATIC void | | 366 | STATIC void |
367 | evalloop(union node *n, int flags) | | 367 | evalloop(union node *n, int flags) |
368 | { | | 368 | { |
369 | int status; | | 369 | int status; |
370 | | | 370 | |
371 | loopnest++; | | 371 | loopnest++; |
372 | status = 0; | | 372 | status = 0; |
373 | | | 373 | |
374 | CTRACE(DBG_EVAL, ("evalloop %s:", NODETYPENAME(n->type))); | | 374 | CTRACE(DBG_EVAL, ("evalloop %s:", NODETYPENAME(n->type))); |
375 | VXTRACE(DBG_EVAL, (" "), showtree(n->nbinary.ch1)); | | 375 | VXTRACE(DBG_EVAL, (" "), showtree(n->nbinary.ch1)); |
376 | VXTRACE(DBG_EVAL, ("evalloop do: "), showtree(n->nbinary.ch2)); | | 376 | VXTRACE(DBG_EVAL, ("evalloop do: "), showtree(n->nbinary.ch2)); |
377 | VTRACE(DBG_EVAL, ("evalloop done\n")); | | 377 | VTRACE(DBG_EVAL, ("evalloop done\n")); |
378 | CTRACE(DBG_EVAL, ("\n")); | | 378 | CTRACE(DBG_EVAL, ("\n")); |
379 | | | 379 | |
380 | for (;;) { | | 380 | for (;;) { |
381 | evaltree(n->nbinary.ch1, EV_TESTED); | | 381 | evaltree(n->nbinary.ch1, EV_TESTED); |
382 | if (nflag) | | 382 | if (nflag) |
383 | break; | | 383 | break; |
384 | if (evalskip) { | | 384 | if (evalskip) { |
385 | skipping: if (evalskip == SKIPCONT && --skipcount <= 0) { | | 385 | skipping: if (evalskip == SKIPCONT && --skipcount <= 0) { |
386 | evalskip = SKIPNONE; | | 386 | evalskip = SKIPNONE; |
387 | continue; | | 387 | continue; |
388 | } | | 388 | } |
389 | if (evalskip == SKIPBREAK && --skipcount <= 0) | | 389 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
390 | evalskip = SKIPNONE; | | 390 | evalskip = SKIPNONE; |
391 | break; | | 391 | break; |
392 | } | | 392 | } |
393 | if (n->type == NWHILE) { | | 393 | if (n->type == NWHILE) { |
394 | if (exitstatus != 0) | | 394 | if (exitstatus != 0) |
395 | break; | | 395 | break; |
396 | } else { | | 396 | } else { |
397 | if (exitstatus == 0) | | 397 | if (exitstatus == 0) |
398 | break; | | 398 | break; |
399 | } | | 399 | } |
400 | evaltree(n->nbinary.ch2, flags & EV_TESTED); | | 400 | evaltree(n->nbinary.ch2, flags & EV_TESTED); |
401 | status = exitstatus; | | 401 | status = exitstatus; |
402 | if (evalskip) | | 402 | if (evalskip) |
403 | goto skipping; | | 403 | goto skipping; |
404 | } | | 404 | } |
405 | loopnest--; | | 405 | loopnest--; |
406 | exitstatus = status; | | 406 | exitstatus = status; |
407 | } | | 407 | } |
408 | | | 408 | |
409 | | | 409 | |
410 | | | 410 | |
411 | STATIC void | | 411 | STATIC void |
412 | evalfor(union node *n, int flags) | | 412 | evalfor(union node *n, int flags) |
413 | { | | 413 | { |
414 | struct arglist arglist; | | 414 | struct arglist arglist; |
415 | union node *argp; | | 415 | union node *argp; |
416 | struct strlist *sp; | | 416 | struct strlist *sp; |
417 | struct stackmark smark; | | 417 | struct stackmark smark; |
418 | int status; | | 418 | int status; |
419 | | | 419 | |
420 | status = nflag ? exitstatus : 0; | | 420 | status = nflag ? exitstatus : 0; |
421 | | | 421 | |
422 | setstackmark(&smark); | | 422 | setstackmark(&smark); |
423 | arglist.lastp = &arglist.list; | | 423 | arglist.lastp = &arglist.list; |
424 | for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { | | 424 | for (argp = n->nfor.args ; argp ; argp = argp->narg.next) { |
425 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); | | 425 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
426 | if (evalskip) | | 426 | if (evalskip) |
427 | goto out; | | 427 | goto out; |
428 | } | | 428 | } |
429 | *arglist.lastp = NULL; | | 429 | *arglist.lastp = NULL; |
430 | | | 430 | |
431 | loopnest++; | | 431 | loopnest++; |
432 | for (sp = arglist.list ; sp ; sp = sp->next) { | | 432 | for (sp = arglist.list ; sp ; sp = sp->next) { |
433 | if (xflag) { | | 433 | if (xflag) { |
434 | out2str(expandstr(ps4val(), line_number)); | | 434 | out2str(expandstr(ps4val(), line_number)); |
435 | out2str("for "); | | 435 | out2str("for "); |
436 | out2str(n->nfor.var); | | 436 | out2str(n->nfor.var); |
437 | out2c('='); | | 437 | out2c('='); |
438 | out2shstr(sp->text); | | 438 | out2shstr(sp->text); |
439 | out2c('\n'); | | 439 | out2c('\n'); |
440 | flushout(&errout); | | 440 | flushout(&errout); |
441 | } | | 441 | } |
442 | | | 442 | |
443 | setvar(n->nfor.var, sp->text, 0); | | 443 | setvar(n->nfor.var, sp->text, 0); |
444 | evaltree(n->nfor.body, flags & EV_TESTED); | | 444 | evaltree(n->nfor.body, flags & EV_TESTED); |
445 | status = exitstatus; | | 445 | status = exitstatus; |
446 | if (nflag) | | 446 | if (nflag) |
447 | break; | | 447 | break; |
448 | if (evalskip) { | | 448 | if (evalskip) { |
449 | if (evalskip == SKIPCONT && --skipcount <= 0) { | | 449 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
450 | evalskip = SKIPNONE; | | 450 | evalskip = SKIPNONE; |
451 | continue; | | 451 | continue; |
452 | } | | 452 | } |
453 | if (evalskip == SKIPBREAK && --skipcount <= 0) | | 453 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
454 | evalskip = SKIPNONE; | | 454 | evalskip = SKIPNONE; |
455 | break; | | 455 | break; |
456 | } | | 456 | } |
457 | } | | 457 | } |
458 | loopnest--; | | 458 | loopnest--; |
459 | exitstatus = status; | | 459 | exitstatus = status; |
460 | out: | | 460 | out: |
461 | popstackmark(&smark); | | 461 | popstackmark(&smark); |
462 | } | | 462 | } |
463 | | | 463 | |
464 | | | 464 | |
465 | | | 465 | |
466 | STATIC void | | 466 | STATIC void |
467 | evalcase(union node *n, int flags) | | 467 | evalcase(union node *n, int flags) |
468 | { | | 468 | { |
469 | union node *cp, *ncp; | | 469 | union node *cp, *ncp; |
470 | union node *patp; | | 470 | union node *patp; |
471 | struct arglist arglist; | | 471 | struct arglist arglist; |
472 | struct stackmark smark; | | 472 | struct stackmark smark; |
473 | int status = 0; | | 473 | int status = 0; |
474 | | | 474 | |
475 | setstackmark(&smark); | | 475 | setstackmark(&smark); |
476 | arglist.lastp = &arglist.list; | | 476 | arglist.lastp = &arglist.list; |
477 | line_number = n->ncase.lineno; | | 477 | line_number = n->ncase.lineno; |
478 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); | | 478 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
479 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { | | 479 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
480 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { | | 480 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
481 | line_number = patp->narg.lineno; | | 481 | line_number = patp->narg.lineno; |
482 | if (casematch(patp, arglist.list->text)) { | | 482 | if (casematch(patp, arglist.list->text)) { |
483 | while (cp != NULL && evalskip == 0 && | | 483 | while (cp != NULL && evalskip == 0 && |
484 | nflag == 0) { | | 484 | nflag == 0) { |
485 | if (cp->type == NCLISTCONT) | | 485 | if (cp->type == NCLISTCONT) |
486 | ncp = cp->nclist.next; | | 486 | ncp = cp->nclist.next; |
487 | else | | 487 | else |
488 | ncp = NULL; | | 488 | ncp = NULL; |
489 | line_number = cp->nclist.lineno; | | 489 | line_number = cp->nclist.lineno; |
490 | evaltree(cp->nclist.body, flags); | | 490 | evaltree(cp->nclist.body, flags); |
491 | status = exitstatus; | | 491 | status = exitstatus; |
492 | cp = ncp; | | 492 | cp = ncp; |
493 | } | | 493 | } |
494 | goto out; | | 494 | goto out; |
495 | } | | 495 | } |
496 | } | | 496 | } |
497 | } | | 497 | } |
498 | out: | | 498 | out: |
499 | exitstatus = status; | | 499 | exitstatus = status; |
500 | popstackmark(&smark); | | 500 | popstackmark(&smark); |
501 | } | | 501 | } |
502 | | | 502 | |
503 | | | 503 | |
504 | | | 504 | |
505 | /* | | 505 | /* |
506 | * Kick off a subshell to evaluate a tree. | | 506 | * Kick off a subshell to evaluate a tree. |
507 | */ | | 507 | */ |
508 | | | 508 | |
509 | STATIC void | | 509 | STATIC void |
510 | evalsubshell(union node *n, int flags) | | 510 | evalsubshell(union node *n, int flags) |
511 | { | | 511 | { |
512 | struct job *jp; | | 512 | struct job *jp; |
513 | int backgnd = (n->type == NBACKGND); | | 513 | int backgnd = (n->type == NBACKGND); |
514 | | | 514 | |
515 | expredir(n->nredir.redirect); | | 515 | expredir(n->nredir.redirect); |
516 | if (xflag && n->nredir.redirect) { | | 516 | if (xflag && n->nredir.redirect) { |
517 | union node *rn; | | 517 | union node *rn; |
518 | | | 518 | |
519 | out2str(expandstr(ps4val(), line_number)); | | 519 | out2str(expandstr(ps4val(), line_number)); |
520 | out2str("using redirections:"); | | 520 | out2str("using redirections:"); |
521 | for (rn = n->nredir.redirect; rn; rn = rn->nfile.next) | | 521 | for (rn = n->nredir.redirect; rn; rn = rn->nfile.next) |
522 | (void) outredir(&errout, rn, ' '); | | 522 | (void) outredir(&errout, rn, ' '); |
523 | out2str(" do subshell\n"); | | 523 | out2str(" do subshell\n"); |
524 | flushout(&errout); | | 524 | flushout(&errout); |
525 | } | | 525 | } |
526 | INTOFF; | | 526 | INTOFF; |
527 | jp = makejob(n, 1); | | 527 | jp = makejob(n, 1); |
528 | if (forkshell(jp, n, backgnd ? FORK_BG : FORK_FG) == 0) { | | 528 | if (forkshell(jp, n, backgnd ? FORK_BG : FORK_FG) == 0) { |
529 | INTON; | | 529 | INTON; |
530 | if (backgnd) | | 530 | if (backgnd) |
531 | flags &=~ EV_TESTED; | | 531 | flags &=~ EV_TESTED; |
532 | redirect(n->nredir.redirect, REDIR_KEEP); | | 532 | redirect(n->nredir.redirect, REDIR_KEEP); |
533 | /* never returns */ | | 533 | /* never returns */ |
534 | evaltree(n->nredir.n, flags | EV_EXIT); | | 534 | evaltree(n->nredir.n, flags | EV_EXIT); |
535 | } | | 535 | } |
536 | exitstatus = backgnd ? 0 : waitforjob(jp); | | 536 | exitstatus = backgnd ? 0 : waitforjob(jp); |
537 | INTON; | | 537 | INTON; |
538 | if (!backgnd && xflag && n->nredir.redirect) { | | 538 | if (!backgnd && xflag && n->nredir.redirect) { |
539 | out2str(expandstr(ps4val(), line_number)); | | 539 | out2str(expandstr(ps4val(), line_number)); |
540 | out2str("done subshell\n"); | | 540 | out2str("done subshell\n"); |
541 | flushout(&errout); | | 541 | flushout(&errout); |
542 | } | | 542 | } |
543 | } | | 543 | } |
544 | | | 544 | |
545 | | | 545 | |
546 | | | 546 | |
547 | /* | | 547 | /* |
548 | * Compute the names of the files in a redirection list. | | 548 | * Compute the names of the files in a redirection list. |
549 | */ | | 549 | */ |
550 | | | 550 | |
551 | STATIC void | | 551 | STATIC void |
552 | expredir(union node *n) | | 552 | expredir(union node *n) |
553 | { | | 553 | { |
554 | union node *redir; | | 554 | union node *redir; |
555 | | | 555 | |
556 | for (redir = n ; redir ; redir = redir->nfile.next) { | | 556 | for (redir = n ; redir ; redir = redir->nfile.next) { |
557 | struct arglist fn; | | 557 | struct arglist fn; |
558 | | | 558 | |
559 | fn.lastp = &fn.list; | | 559 | fn.lastp = &fn.list; |
560 | switch (redir->type) { | | 560 | switch (redir->type) { |
561 | case NFROMTO: | | 561 | case NFROMTO: |
562 | case NFROM: | | 562 | case NFROM: |
563 | case NTO: | | 563 | case NTO: |
564 | case NCLOBBER: | | 564 | case NCLOBBER: |
565 | case NAPPEND: | | 565 | case NAPPEND: |
566 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); | | 566 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
567 | redir->nfile.expfname = fn.list->text; | | 567 | redir->nfile.expfname = fn.list->text; |
568 | break; | | 568 | break; |
569 | case NFROMFD: | | 569 | case NFROMFD: |
570 | case NTOFD: | | 570 | case NTOFD: |
571 | if (redir->ndup.vname) { | | 571 | if (redir->ndup.vname) { |
572 | expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); | | 572 | expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR); |
573 | fixredir(redir, fn.list->text, 1); | | 573 | fixredir(redir, fn.list->text, 1); |
574 | } | | 574 | } |
575 | break; | | 575 | break; |
576 | } | | 576 | } |
577 | } | | 577 | } |
578 | } | | 578 | } |
579 | | | 579 | |
580 | | | 580 | |
581 | | | 581 | |
582 | /* | | 582 | /* |
583 | * Evaluate a pipeline. All the processes in the pipeline are children | | 583 | * Evaluate a pipeline. All the processes in the pipeline are children |
584 | * of the process creating the pipeline. (This differs from some versions | | 584 | * of the process creating the pipeline. (This differs from some versions |
585 | * of the shell, which make the last process in a pipeline the parent | | 585 | * of the shell, which make the last process in a pipeline the parent |
586 | * of all the rest.) | | 586 | * of all the rest.) |
587 | */ | | 587 | */ |
588 | | | 588 | |
589 | STATIC void | | 589 | STATIC void |
590 | evalpipe(union node *n) | | 590 | evalpipe(union node *n) |
591 | { | | 591 | { |
592 | struct job *jp; | | 592 | struct job *jp; |
593 | struct nodelist *lp; | | 593 | struct nodelist *lp; |
594 | int pipelen; | | 594 | int pipelen; |
595 | int prevfd; | | 595 | int prevfd; |
596 | int pip[2]; | | 596 | int pip[2]; |
597 | | | 597 | |
598 | CTRACE(DBG_EVAL, ("evalpipe(%p) called\n", n)); | | 598 | CTRACE(DBG_EVAL, ("evalpipe(%p) called\n", n)); |
599 | pipelen = 0; | | 599 | pipelen = 0; |
600 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) | | 600 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) |
601 | pipelen++; | | 601 | pipelen++; |
602 | INTOFF; | | 602 | INTOFF; |
603 | jp = makejob(n, pipelen); | | 603 | jp = makejob(n, pipelen); |
604 | prevfd = -1; | | 604 | prevfd = -1; |
605 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { | | 605 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { |
606 | prehash(lp->n); | | 606 | prehash(lp->n); |
607 | pip[1] = -1; | | 607 | pip[1] = -1; |
608 | if (lp->next) { | | 608 | if (lp->next) { |
609 | if (sh_pipe(pip) < 0) { | | 609 | if (sh_pipe(pip) < 0) { |
610 | if (prevfd >= 0) | | 610 | if (prevfd >= 0) |
611 | close(prevfd); | | 611 | close(prevfd); |
612 | error("Pipe call failed"); | | 612 | error("Pipe call failed"); |
613 | } | | 613 | } |
614 | } | | 614 | } |
615 | if (forkshell(jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) { | | 615 | if (forkshell(jp, lp->n, n->npipe.backgnd ? FORK_BG : FORK_FG) == 0) { |
616 | INTON; | | 616 | INTON; |
617 | if (prevfd > 0) | | 617 | if (prevfd > 0) |
618 | movefd(prevfd, 0); | | 618 | movefd(prevfd, 0); |
619 | if (pip[1] >= 0) { | | 619 | if (pip[1] >= 0) { |
620 | close(pip[0]); | | 620 | close(pip[0]); |
621 | movefd(pip[1], 1); | | 621 | movefd(pip[1], 1); |
622 | } | | 622 | } |
623 | evaltree(lp->n, EV_EXIT); | | 623 | evaltree(lp->n, EV_EXIT); |
624 | } | | 624 | } |
625 | if (prevfd >= 0) | | 625 | if (prevfd >= 0) |
626 | close(prevfd); | | 626 | close(prevfd); |
627 | prevfd = pip[0]; | | 627 | prevfd = pip[0]; |
628 | close(pip[1]); | | 628 | close(pip[1]); |
629 | } | | 629 | } |
630 | if (n->npipe.backgnd == 0) { | | 630 | if (n->npipe.backgnd == 0) { |
631 | exitstatus = waitforjob(jp); | | 631 | exitstatus = waitforjob(jp); |
632 | CTRACE(DBG_EVAL, ("evalpipe: job done exit status %d\n", | | 632 | CTRACE(DBG_EVAL, ("evalpipe: job done exit status %d\n", |
633 | exitstatus)); | | 633 | exitstatus)); |
634 | } else | | 634 | } else |
635 | exitstatus = 0; | | 635 | exitstatus = 0; |
636 | INTON; | | 636 | INTON; |
637 | } | | 637 | } |
638 | | | 638 | |
639 | | | 639 | |
640 | | | 640 | |
641 | /* | | 641 | /* |
642 | * Execute a command inside back quotes. If it's a builtin command, we | | 642 | * Execute a command inside back quotes. If it's a builtin command, we |
643 | * want to save its output in a block obtained from malloc. Otherwise | | 643 | * want to save its output in a block obtained from malloc. Otherwise |
644 | * we fork off a subprocess and get the output of the command via a pipe. | | 644 | * we fork off a subprocess and get the output of the command via a pipe. |
645 | * Should be called with interrupts off. | | 645 | * Should be called with interrupts off. |
646 | */ | | 646 | */ |
647 | | | 647 | |
648 | void | | 648 | void |
649 | evalbackcmd(union node *n, struct backcmd *result) | | 649 | evalbackcmd(union node *n, struct backcmd *result) |
650 | { | | 650 | { |
651 | int pip[2]; | | 651 | int pip[2]; |
652 | struct job *jp; | | 652 | struct job *jp; |
653 | struct stackmark smark; /* unnecessary */ | | 653 | struct stackmark smark; /* unnecessary */ |
654 | | | 654 | |
655 | setstackmark(&smark); | | 655 | setstackmark(&smark); |
656 | result->fd = -1; | | 656 | result->fd = -1; |
657 | result->buf = NULL; | | 657 | result->buf = NULL; |
658 | result->nleft = 0; | | 658 | result->nleft = 0; |
659 | result->jp = NULL; | | 659 | result->jp = NULL; |
660 | if (nflag || n == NULL) { | | 660 | if (nflag || n == NULL) { |
661 | goto out; | | 661 | goto out; |
662 | } | | 662 | } |
663 | #ifdef notyet | | 663 | #ifdef notyet |
664 | /* | | 664 | /* |
665 | * For now we disable executing builtins in the same | | 665 | * For now we disable executing builtins in the same |
666 | * context as the shell, because we are not keeping | | 666 | * context as the shell, because we are not keeping |
667 | * enough state to recover from changes that are | | 667 | * enough state to recover from changes that are |
668 | * supposed only to affect subshells. eg. echo "`cd /`" | | 668 | * supposed only to affect subshells. eg. echo "`cd /`" |
669 | */ | | 669 | */ |
670 | if (n->type == NCMD) { | | 670 | if (n->type == NCMD) { |
671 | exitstatus = oexitstatus; | | 671 | exitstatus = oexitstatus; |
672 | evalcommand(n, EV_BACKCMD, result); | | 672 | evalcommand(n, EV_BACKCMD, result); |
673 | } else | | 673 | } else |
674 | #endif | | 674 | #endif |
675 | { | | 675 | { |
676 | INTOFF; | | 676 | INTOFF; |
677 | if (sh_pipe(pip) < 0) | | 677 | if (sh_pipe(pip) < 0) |
678 | error("Pipe call failed"); | | 678 | error("Pipe call failed"); |
679 | jp = makejob(n, 1); | | 679 | jp = makejob(n, 1); |
680 | if (forkshell(jp, n, FORK_NOJOB) == 0) { | | 680 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
681 | FORCEINTON; | | 681 | FORCEINTON; |
682 | close(pip[0]); | | 682 | close(pip[0]); |
683 | movefd(pip[1], 1); | | 683 | movefd(pip[1], 1); |
684 | eflag = 0; | | 684 | eflag = 0; |
685 | evaltree(n, EV_EXIT); | | 685 | evaltree(n, EV_EXIT); |
686 | /* NOTREACHED */ | | 686 | /* NOTREACHED */ |
687 | } | | 687 | } |
688 | close(pip[1]); | | 688 | close(pip[1]); |
689 | result->fd = pip[0]; | | 689 | result->fd = pip[0]; |
690 | result->jp = jp; | | 690 | result->jp = jp; |
691 | INTON; | | 691 | INTON; |
692 | } | | 692 | } |
693 | out: | | 693 | out: |
694 | popstackmark(&smark); | | 694 | popstackmark(&smark); |
695 | CTRACE(DBG_EVAL, ("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", | | 695 | CTRACE(DBG_EVAL, ("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
696 | result->fd, result->buf, result->nleft, result->jp)); | | 696 | result->fd, result->buf, result->nleft, result->jp)); |
697 | } | | 697 | } |
698 | | | 698 | |
699 | static const char * | | 699 | const char * |
700 | syspath(void) | | 700 | syspath(void) |
701 | { | | 701 | { |
702 | static char *sys_path = NULL; | | 702 | static char *sys_path = NULL; |
703 | static int mib[] = {CTL_USER, USER_CS_PATH}; | | 703 | static int mib[] = {CTL_USER, USER_CS_PATH}; |
704 | static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin"; | | 704 | static char def_path[] = "PATH=/usr/bin:/bin:/usr/sbin:/sbin"; |
705 | size_t len; | | 705 | size_t len; |
706 | | | 706 | |
707 | if (sys_path == NULL) { | | 707 | if (sys_path == NULL) { |
708 | if (sysctl(mib, 2, 0, &len, 0, 0) != -1 && | | 708 | if (sysctl(mib, 2, 0, &len, 0, 0) != -1 && |
709 | (sys_path = ckmalloc(len + 5)) != NULL && | | 709 | (sys_path = ckmalloc(len + 5)) != NULL && |
710 | sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) { | | 710 | sysctl(mib, 2, sys_path + 5, &len, 0, 0) != -1) { |
711 | memcpy(sys_path, "PATH=", 5); | | 711 | memcpy(sys_path, "PATH=", 5); |
712 | } else { | | 712 | } else { |
713 | ckfree(sys_path); | | 713 | ckfree(sys_path); |
714 | /* something to keep things happy */ | | 714 | /* something to keep things happy */ |
715 | sys_path = def_path; | | 715 | sys_path = def_path; |
716 | } | | 716 | } |
717 | } | | 717 | } |
718 | return sys_path; | | 718 | return sys_path; |
719 | } | | 719 | } |
720 | | | 720 | |
721 | static int | | 721 | static int |
722 | parse_command_args(int argc, char **argv, int *use_syspath) | | 722 | parse_command_args(int argc, char **argv, int *use_syspath) |
723 | { | | 723 | { |
724 | int sv_argc = argc; | | 724 | int sv_argc = argc; |
725 | char *cp, c; | | 725 | char *cp, c; |
726 | | | 726 | |
727 | *use_syspath = 0; | | 727 | *use_syspath = 0; |
728 | | | 728 | |
729 | for (;;) { | | 729 | for (;;) { |
730 | argv++; | | 730 | argv++; |
731 | if (--argc == 0) | | 731 | if (--argc == 0) |
732 | break; | | 732 | break; |
733 | cp = *argv; | | 733 | cp = *argv; |
734 | if (*cp++ != '-') | | 734 | if (*cp++ != '-') |
735 | break; | | 735 | break; |
736 | if (*cp == '-' && cp[1] == 0) { | | 736 | if (*cp == '-' && cp[1] == 0) { |
737 | argv++; | | 737 | argv++; |
738 | argc--; | | 738 | argc--; |
739 | break; | | 739 | break; |
740 | } | | 740 | } |
741 | while ((c = *cp++)) { | | 741 | while ((c = *cp++)) { |
742 | switch (c) { | | 742 | switch (c) { |
743 | case 'p': | | 743 | case 'p': |
744 | *use_syspath = 1; | | 744 | *use_syspath = 1; |
745 | break; | | 745 | break; |
746 | default: | | 746 | default: |
747 | /* run 'typecmd' for other options */ | | 747 | /* run 'typecmd' for other options */ |
748 | return 0; | | 748 | return 0; |
749 | } | | 749 | } |
750 | } | | 750 | } |
751 | } | | 751 | } |
752 | return sv_argc - argc; | | 752 | return sv_argc - argc; |
753 | } | | 753 | } |
754 | | | 754 | |
755 | int vforked = 0; | | 755 | int vforked = 0; |
756 | extern char *trap[]; | | 756 | extern char *trap[]; |
757 | | | 757 | |
758 | /* | | 758 | /* |
759 | * Execute a simple command. | | 759 | * Execute a simple command. |
760 | */ | | 760 | */ |
761 | | | 761 | |
762 | STATIC void | | 762 | STATIC void |
763 | evalcommand(union node *cmd, int flgs, struct backcmd *backcmd) | | 763 | evalcommand(union node *cmd, int flgs, struct backcmd *backcmd) |
764 | { | | 764 | { |
765 | struct stackmark smark; | | 765 | struct stackmark smark; |
766 | union node *argp; | | 766 | union node *argp; |
767 | struct arglist arglist; | | 767 | struct arglist arglist; |
768 | struct arglist varlist; | | 768 | struct arglist varlist; |
769 | volatile int flags = flgs; | | 769 | volatile int flags = flgs; |
770 | char ** volatile argv; | | 770 | char ** volatile argv; |
771 | volatile int argc; | | 771 | volatile int argc; |
772 | char **envp; | | 772 | char **envp; |
773 | int varflag; | | 773 | int varflag; |
774 | struct strlist *sp; | | 774 | struct strlist *sp; |
775 | volatile int mode; | | 775 | volatile int mode; |
776 | int pip[2]; | | 776 | int pip[2]; |
777 | struct cmdentry cmdentry; | | 777 | struct cmdentry cmdentry; |
778 | struct job * volatile jp; | | 778 | struct job * volatile jp; |
779 | struct jmploc jmploc; | | 779 | struct jmploc jmploc; |
780 | struct jmploc *volatile savehandler = NULL; | | 780 | struct jmploc *volatile savehandler = NULL; |
781 | const char *volatile savecmdname; | | 781 | const char *volatile savecmdname; |
782 | volatile struct shparam saveparam; | | 782 | volatile struct shparam saveparam; |
783 | struct localvar *volatile savelocalvars; | | 783 | struct localvar *volatile savelocalvars; |
784 | volatile int e; | | 784 | volatile int e; |
785 | char * volatile lastarg; | | 785 | char * volatile lastarg; |
786 | const char * volatile path = pathval(); | | 786 | const char * volatile path = pathval(); |
787 | volatile int temp_path; | | 787 | volatile int temp_path; |
788 | const int savefuncline = funclinebase; | | 788 | const int savefuncline = funclinebase; |
789 | const int savefuncabs = funclineabs; | | 789 | const int savefuncabs = funclineabs; |
790 | | | 790 | |
791 | vforked = 0; | | 791 | vforked = 0; |
792 | /* First expand the arguments. */ | | 792 | /* First expand the arguments. */ |
793 | CTRACE(DBG_EVAL, ("evalcommand(%p, %d) called\n", cmd, flags)); | | 793 | CTRACE(DBG_EVAL, ("evalcommand(%p, %d) called\n", cmd, flags)); |
794 | setstackmark(&smark); | | 794 | setstackmark(&smark); |
795 | back_exitstatus = 0; | | 795 | back_exitstatus = 0; |
796 | | | 796 | |
797 | line_number = cmd->ncmd.lineno; | | 797 | line_number = cmd->ncmd.lineno; |
798 | | | 798 | |
799 | arglist.lastp = &arglist.list; | | 799 | arglist.lastp = &arglist.list; |
800 | varflag = 1; | | 800 | varflag = 1; |
801 | /* Expand arguments, ignoring the initial 'name=value' ones */ | | 801 | /* Expand arguments, ignoring the initial 'name=value' ones */ |
802 | for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { | | 802 | for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { |
803 | char *p = argp->narg.text; | | 803 | char *p = argp->narg.text; |
804 | | | 804 | |
805 | line_number = argp->narg.lineno; | | 805 | line_number = argp->narg.lineno; |
806 | if (varflag && is_name(*p)) { | | 806 | if (varflag && is_name(*p)) { |
807 | do { | | 807 | do { |
808 | p++; | | 808 | p++; |
809 | } while (is_in_name(*p)); | | 809 | } while (is_in_name(*p)); |
810 | if (*p == '=') | | 810 | if (*p == '=') |
811 | continue; | | 811 | continue; |
812 | } | | 812 | } |
813 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); | | 813 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
814 | varflag = 0; | | 814 | varflag = 0; |
815 | } | | 815 | } |
816 | *arglist.lastp = NULL; | | 816 | *arglist.lastp = NULL; |
817 | | | 817 | |
818 | expredir(cmd->ncmd.redirect); | | 818 | expredir(cmd->ncmd.redirect); |
819 | | | 819 | |
820 | /* Now do the initial 'name=value' ones we skipped above */ | | 820 | /* Now do the initial 'name=value' ones we skipped above */ |
821 | varlist.lastp = &varlist.list; | | 821 | varlist.lastp = &varlist.list; |
822 | for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { | | 822 | for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) { |
823 | char *p = argp->narg.text; | | 823 | char *p = argp->narg.text; |
824 | | | 824 | |
825 | line_number = argp->narg.lineno; | | 825 | line_number = argp->narg.lineno; |
826 | if (!is_name(*p)) | | 826 | if (!is_name(*p)) |
827 | break; | | 827 | break; |
828 | do | | 828 | do |
829 | p++; | | 829 | p++; |
830 | while (is_in_name(*p)); | | 830 | while (is_in_name(*p)); |
831 | if (*p != '=') | | 831 | if (*p != '=') |
832 | break; | | 832 | break; |
833 | expandarg(argp, &varlist, EXP_VARTILDE); | | 833 | expandarg(argp, &varlist, EXP_VARTILDE); |
834 | } | | 834 | } |
835 | *varlist.lastp = NULL; | | 835 | *varlist.lastp = NULL; |
836 | | | 836 | |
837 | argc = 0; | | 837 | argc = 0; |
838 | for (sp = arglist.list ; sp ; sp = sp->next) | | 838 | for (sp = arglist.list ; sp ; sp = sp->next) |
839 | argc++; | | 839 | argc++; |
840 | argv = stalloc(sizeof (char *) * (argc + 1)); | | 840 | argv = stalloc(sizeof (char *) * (argc + 1)); |
841 | | | 841 | |
842 | for (sp = arglist.list ; sp ; sp = sp->next) { | | 842 | for (sp = arglist.list ; sp ; sp = sp->next) { |
843 | VTRACE(DBG_EVAL, ("evalcommand arg: %s\n", sp->text)); | | 843 | VTRACE(DBG_EVAL, ("evalcommand arg: %s\n", sp->text)); |
844 | *argv++ = sp->text; | | 844 | *argv++ = sp->text; |
845 | } | | 845 | } |
846 | *argv = NULL; | | 846 | *argv = NULL; |
847 | lastarg = NULL; | | 847 | lastarg = NULL; |
848 | if (iflag && funcnest == 0 && argc > 0) | | 848 | if (iflag && funcnest == 0 && argc > 0) |
849 | lastarg = argv[-1]; | | 849 | lastarg = argv[-1]; |
850 | argv -= argc; | | 850 | argv -= argc; |
851 | | | 851 | |
852 | /* Print the command if xflag is set. */ | | 852 | /* Print the command if xflag is set. */ |
853 | if (xflag) { | | 853 | if (xflag) { |
854 | char sep = 0; | | 854 | char sep = 0; |
855 | union node *rn; | | 855 | union node *rn; |
856 | | | 856 | |
857 | out2str(expandstr(ps4val(), line_number)); | | 857 | out2str(expandstr(ps4val(), line_number)); |
858 | for (sp = varlist.list ; sp ; sp = sp->next) { | | 858 | for (sp = varlist.list ; sp ; sp = sp->next) { |
859 | char *p; | | 859 | char *p; |
860 | | | 860 | |
861 | if (sep != 0) | | 861 | if (sep != 0) |
862 | outc(sep, &errout); | | 862 | outc(sep, &errout); |
863 | | | 863 | |
864 | /* | | 864 | /* |
865 | * The "var=" part should not be quoted, regardless | | 865 | * The "var=" part should not be quoted, regardless |
866 | * of the value, or it would not represent an | | 866 | * of the value, or it would not represent an |
867 | * assignment, but rather a command | | 867 | * assignment, but rather a command |
868 | */ | | 868 | */ |
869 | p = strchr(sp->text, '='); | | 869 | p = strchr(sp->text, '='); |
870 | if (p != NULL) { | | 870 | if (p != NULL) { |
871 | *p = '\0'; /*XXX*/ | | 871 | *p = '\0'; /*XXX*/ |
872 | out2shstr(sp->text); | | 872 | out2shstr(sp->text); |
873 | out2c('='); | | 873 | out2c('='); |
874 | *p++ = '='; /*XXX*/ | | 874 | *p++ = '='; /*XXX*/ |
875 | } else | | 875 | } else |
876 | p = sp->text; | | 876 | p = sp->text; |
877 | out2shstr(p); | | 877 | out2shstr(p); |
878 | sep = ' '; | | 878 | sep = ' '; |
879 | } | | 879 | } |
880 | for (sp = arglist.list ; sp ; sp = sp->next) { | | 880 | for (sp = arglist.list ; sp ; sp = sp->next) { |
881 | if (sep != 0) | | 881 | if (sep != 0) |
882 | outc(sep, &errout); | | 882 | outc(sep, &errout); |
883 | out2shstr(sp->text); | | 883 | out2shstr(sp->text); |
884 | sep = ' '; | | 884 | sep = ' '; |
885 | } | | 885 | } |
886 | for (rn = cmd->ncmd.redirect; rn; rn = rn->nfile.next) | | 886 | for (rn = cmd->ncmd.redirect; rn; rn = rn->nfile.next) |
887 | if (outredir(&errout, rn, sep)) | | 887 | if (outredir(&errout, rn, sep)) |
888 | sep = ' '; | | 888 | sep = ' '; |
889 | outc('\n', &errout); | | 889 | outc('\n', &errout); |
890 | flushout(&errout); | | 890 | flushout(&errout); |
891 | } | | 891 | } |
892 | | | 892 | |
893 | /* Now locate the command. */ | | 893 | /* Now locate the command. */ |
894 | if (argc == 0) { | | 894 | if (argc == 0) { |
895 | cmdentry.cmdtype = CMDSPLBLTIN; | | 895 | cmdentry.cmdtype = CMDSPLBLTIN; |
896 | cmdentry.u.bltin = bltincmd; | | 896 | cmdentry.u.bltin = bltincmd; |
897 | } else { | | 897 | } else { |
898 | static const char PATH[] = "PATH="; | | 898 | static const char PATH[] = "PATH="; |
899 | int cmd_flags = 0; | | 899 | int cmd_flags = 0; |
900 | | | 900 | |
901 | /* | | 901 | /* |
902 | * Modify the command lookup path, if a PATH= assignment | | 902 | * Modify the command lookup path, if a PATH= assignment |
903 | * is present | | 903 | * is present |
904 | */ | | 904 | */ |
905 | for (sp = varlist.list; sp; sp = sp->next) | | 905 | for (sp = varlist.list; sp; sp = sp->next) |
906 | if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) | | 906 | if (strncmp(sp->text, PATH, sizeof(PATH) - 1) == 0) |
907 | path = sp->text + sizeof(PATH) - 1; | | 907 | path = sp->text + sizeof(PATH) - 1; |
908 | | | 908 | |
909 | do { | | 909 | do { |
910 | int argsused, use_syspath; | | 910 | int argsused, use_syspath; |
911 | | | 911 | |
912 | find_command(argv[0], &cmdentry, cmd_flags, path); | | 912 | find_command(argv[0], &cmdentry, cmd_flags, path); |
913 | #if 0 | | 913 | #if 0 |
914 | /* | | 914 | /* |
915 | * This short circuits all of the processing that | | 915 | * This short circuits all of the processing that |
916 | * should be done (including processing the | | 916 | * should be done (including processing the |
917 | * redirects), so just don't ... | | 917 | * redirects), so just don't ... |
918 | * | | 918 | * |
919 | * (eventually this whole #if'd block will vanish) | | 919 | * (eventually this whole #if'd block will vanish) |
920 | */ | | 920 | */ |
921 | if (cmdentry.cmdtype == CMDUNKNOWN) { | | 921 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
922 | exitstatus = 127; | | 922 | exitstatus = 127; |
923 | flushout(&errout); | | 923 | flushout(&errout); |
924 | goto out; | | 924 | goto out; |
925 | } | | 925 | } |
926 | #endif | | 926 | #endif |
927 | | | 927 | |
928 | /* implement the 'command' builtin here */ | | 928 | /* implement the 'command' builtin here */ |
929 | if (cmdentry.cmdtype != CMDBUILTIN || | | 929 | if (cmdentry.cmdtype != CMDBUILTIN || |
930 | cmdentry.u.bltin != bltincmd) | | 930 | cmdentry.u.bltin != bltincmd) |
931 | break; | | 931 | break; |
932 | cmd_flags |= DO_NOFUNC; | | 932 | cmd_flags |= DO_NOFUNC; |
933 | argsused = parse_command_args(argc, argv, &use_syspath); | | 933 | argsused = parse_command_args(argc, argv, &use_syspath); |
934 | if (argsused == 0) { | | 934 | if (argsused == 0) { |
935 | /* use 'type' builting to display info */ | | 935 | /* use 'type' builting to display info */ |
936 | cmdentry.u.bltin = typecmd; | | 936 | cmdentry.u.bltin = typecmd; |
937 | break; | | 937 | break; |
938 | } | | 938 | } |
939 | argc -= argsused; | | 939 | argc -= argsused; |
940 | argv += argsused; | | 940 | argv += argsused; |
941 | if (use_syspath) | | 941 | if (use_syspath) |
942 | path = syspath() + 5; | | 942 | path = syspath() + 5; |
943 | } while (argc != 0); | | 943 | } while (argc != 0); |
944 | if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC) | | 944 | if (cmdentry.cmdtype == CMDSPLBLTIN && cmd_flags & DO_NOFUNC) |
945 | /* posix mandates that 'command <splbltin>' act as if | | 945 | /* posix mandates that 'command <splbltin>' act as if |
946 | <splbltin> was a normal builtin */ | | 946 | <splbltin> was a normal builtin */ |
947 | cmdentry.cmdtype = CMDBUILTIN; | | 947 | cmdentry.cmdtype = CMDBUILTIN; |
948 | } | | 948 | } |
949 | | | 949 | |
950 | /* Fork off a child process if necessary. */ | | 950 | /* Fork off a child process if necessary. */ |
951 | if (cmd->ncmd.backgnd || (trap[0] && (flags & EV_EXIT) != 0) | | 951 | if (cmd->ncmd.backgnd || (trap[0] && (flags & EV_EXIT) != 0) |
952 | || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) | | 952 | || ((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) |
953 | && (flags & EV_EXIT) == 0) | | 953 | && (flags & EV_EXIT) == 0) |
954 | || ((flags & EV_BACKCMD) != 0 && | | 954 | || ((flags & EV_BACKCMD) != 0 && |
955 | ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN) | | 955 | ((cmdentry.cmdtype != CMDBUILTIN && cmdentry.cmdtype != CMDSPLBLTIN) |
956 | || cmdentry.u.bltin == dotcmd | | 956 | || cmdentry.u.bltin == dotcmd |
957 | || cmdentry.u.bltin == evalcmd))) { | | 957 | || cmdentry.u.bltin == evalcmd))) { |
958 | INTOFF; | | 958 | INTOFF; |
959 | jp = makejob(cmd, 1); | | 959 | jp = makejob(cmd, 1); |
960 | mode = cmd->ncmd.backgnd; | | 960 | mode = cmd->ncmd.backgnd; |
961 | if (flags & EV_BACKCMD) { | | 961 | if (flags & EV_BACKCMD) { |
962 | mode = FORK_NOJOB; | | 962 | mode = FORK_NOJOB; |
963 | if (sh_pipe(pip) < 0) | | 963 | if (sh_pipe(pip) < 0) |
964 | error("Pipe call failed"); | | 964 | error("Pipe call failed"); |
965 | } | | 965 | } |
966 | #ifdef DO_SHAREDVFORK | | 966 | #ifdef DO_SHAREDVFORK |
967 | /* It is essential that if DO_SHAREDVFORK is defined that the | | 967 | /* It is essential that if DO_SHAREDVFORK is defined that the |
968 | * child's address space is actually shared with the parent as | | 968 | * child's address space is actually shared with the parent as |
969 | * we rely on this. | | 969 | * we rely on this. |
970 | */ | | 970 | */ |
971 | if (usefork == 0 && cmdentry.cmdtype == CMDNORMAL) { | | 971 | if (usefork == 0 && cmdentry.cmdtype == CMDNORMAL) { |
972 | pid_t pid; | | 972 | pid_t pid; |
973 | int serrno; | | 973 | int serrno; |
974 | | | 974 | |
975 | savelocalvars = localvars; | | 975 | savelocalvars = localvars; |
976 | localvars = NULL; | | 976 | localvars = NULL; |
977 | vforked = 1; | | 977 | vforked = 1; |
978 | VFORK_BLOCK | | 978 | VFORK_BLOCK |
979 | switch (pid = vfork()) { | | 979 | switch (pid = vfork()) { |
980 | case -1: | | 980 | case -1: |
981 | serrno = errno; | | 981 | serrno = errno; |
982 | VTRACE(DBG_EVAL, ("vfork() failed, errno=%d\n", | | 982 | VTRACE(DBG_EVAL, ("vfork() failed, errno=%d\n", |
983 | serrno)); | | 983 | serrno)); |
984 | INTON; | | 984 | INTON; |
985 | error("Cannot vfork (%s)", strerror(serrno)); | | 985 | error("Cannot vfork (%s)", strerror(serrno)); |
986 | break; | | 986 | break; |
987 | case 0: | | 987 | case 0: |
988 | /* Make sure that exceptions only unwind to | | 988 | /* Make sure that exceptions only unwind to |
989 | * after the vfork(2) | | 989 | * after the vfork(2) |
990 | */ | | 990 | */ |
991 | SHELL_FORKED(); | | 991 | SHELL_FORKED(); |
992 | if (setjmp(jmploc.loc)) { | | 992 | if (setjmp(jmploc.loc)) { |
993 | if (exception == EXSHELLPROC) { | | 993 | if (exception == EXSHELLPROC) { |
994 | /* | | 994 | /* |
995 | * We can't progress with the | | 995 | * We can't progress with the |
996 | * vfork, so, set vforked = 2 | | 996 | * vfork, so, set vforked = 2 |
997 | * so the parent knows, | | 997 | * so the parent knows, |
998 | * and _exit(); | | 998 | * and _exit(); |
999 | */ | | 999 | */ |
1000 | vforked = 2; | | 1000 | vforked = 2; |
1001 | _exit(0); | | 1001 | _exit(0); |
1002 | } else { | | 1002 | } else { |
1003 | _exit(exerrno); | | 1003 | _exit(exerrno); |
1004 | } | | 1004 | } |
1005 | } | | 1005 | } |
1006 | savehandler = handler; | | 1006 | savehandler = handler; |
1007 | handler = &jmploc; | | 1007 | handler = &jmploc; |
1008 | listmklocal(varlist.list, VEXPORT | VNOFUNC); | | 1008 | listmklocal(varlist.list, VEXPORT | VNOFUNC); |
1009 | forkchild(jp, cmd, mode, vforked); | | 1009 | forkchild(jp, cmd, mode, vforked); |
1010 | break; | | 1010 | break; |
1011 | default: | | 1011 | default: |
1012 | VFORK_UNDO(); | | 1012 | VFORK_UNDO(); |
1013 | /* restore from vfork(2) */ | | 1013 | /* restore from vfork(2) */ |
1014 | handler = savehandler; | | 1014 | handler = savehandler; |
1015 | poplocalvars(); | | 1015 | poplocalvars(); |
1016 | localvars = savelocalvars; | | 1016 | localvars = savelocalvars; |
1017 | if (vforked == 2) { | | 1017 | if (vforked == 2) { |
1018 | vforked = 0; | | 1018 | vforked = 0; |
1019 | | | 1019 | |
1020 | (void)waitpid(pid, NULL, 0); | | 1020 | (void)waitpid(pid, NULL, 0); |
1021 | /* | | 1021 | /* |
1022 | * We need to progress in a | | 1022 | * We need to progress in a |
1023 | * normal fork fashion | | 1023 | * normal fork fashion |
1024 | */ | | 1024 | */ |
1025 | goto normal_fork; | | 1025 | goto normal_fork; |
1026 | } | | 1026 | } |
1027 | /* | | 1027 | /* |
1028 | * Here the child has left home, | | 1028 | * Here the child has left home, |
1029 | * getting on with its life, so | | 1029 | * getting on with its life, so |
1030 | * so must we... | | 1030 | * so must we... |
1031 | */ | | 1031 | */ |
1032 | vforked = 0; | | 1032 | vforked = 0; |
1033 | forkparent(jp, cmd, mode, pid); | | 1033 | forkparent(jp, cmd, mode, pid); |
1034 | goto parent; | | 1034 | goto parent; |
1035 | } | | 1035 | } |
1036 | VFORK_END | | 1036 | VFORK_END |
1037 | } else { | | 1037 | } else { |
1038 | normal_fork: | | 1038 | normal_fork: |
1039 | #endif | | 1039 | #endif |
1040 | if (forkshell(jp, cmd, mode) != 0) | | 1040 | if (forkshell(jp, cmd, mode) != 0) |
1041 | goto parent; /* at end of routine */ | | 1041 | goto parent; /* at end of routine */ |
1042 | FORCEINTON; | | 1042 | FORCEINTON; |
1043 | #ifdef DO_SHAREDVFORK | | 1043 | #ifdef DO_SHAREDVFORK |
1044 | } | | 1044 | } |
1045 | #endif | | 1045 | #endif |
1046 | if (flags & EV_BACKCMD) { | | 1046 | if (flags & EV_BACKCMD) { |
1047 | if (!vforked) { | | 1047 | if (!vforked) { |
1048 | FORCEINTON; | | 1048 | FORCEINTON; |
1049 | } | | 1049 | } |
1050 | close(pip[0]); | | 1050 | close(pip[0]); |
1051 | movefd(pip[1], 1); | | 1051 | movefd(pip[1], 1); |
1052 | } | | 1052 | } |
1053 | flags |= EV_EXIT; | | 1053 | flags |= EV_EXIT; |
1054 | } | | 1054 | } |
1055 | | | 1055 | |
1056 | /* This is the child process if a fork occurred. */ | | 1056 | /* This is the child process if a fork occurred. */ |
1057 | /* Execute the command. */ | | 1057 | /* Execute the command. */ |
1058 | switch (cmdentry.cmdtype) { | | 1058 | switch (cmdentry.cmdtype) { |
1059 | case CMDFUNCTION: | | 1059 | case CMDFUNCTION: |
1060 | VXTRACE(DBG_EVAL, ("Shell function: "), trargs(argv)); | | 1060 | VXTRACE(DBG_EVAL, ("Shell function: "), trargs(argv)); |
1061 | redirect(cmd->ncmd.redirect, flags & EV_MORE ? REDIR_PUSH : 0); | | 1061 | redirect(cmd->ncmd.redirect, flags & EV_MORE ? REDIR_PUSH : 0); |
1062 | redirect(cmd->ncmd.redirect, REDIR_PUSH); | | 1062 | redirect(cmd->ncmd.redirect, REDIR_PUSH); |
1063 | saveparam = shellparam; | | 1063 | saveparam = shellparam; |
1064 | shellparam.malloc = 0; | | 1064 | shellparam.malloc = 0; |
1065 | shellparam.reset = 1; | | 1065 | shellparam.reset = 1; |
1066 | shellparam.nparam = argc - 1; | | 1066 | shellparam.nparam = argc - 1; |
1067 | shellparam.p = argv + 1; | | 1067 | shellparam.p = argv + 1; |
1068 | shellparam.optnext = NULL; | | 1068 | shellparam.optnext = NULL; |
1069 | INTOFF; | | 1069 | INTOFF; |
1070 | savelocalvars = localvars; | | 1070 | savelocalvars = localvars; |
1071 | localvars = NULL; | | 1071 | localvars = NULL; |
1072 | reffunc(cmdentry.u.func); | | 1072 | reffunc(cmdentry.u.func); |
1073 | INTON; | | 1073 | INTON; |
1074 | if (setjmp(jmploc.loc)) { | | 1074 | if (setjmp(jmploc.loc)) { |
1075 | if (exception == EXSHELLPROC) { | | 1075 | if (exception == EXSHELLPROC) { |
1076 | freeparam((volatile struct shparam *) | | 1076 | freeparam((volatile struct shparam *) |
1077 | &saveparam); | | 1077 | &saveparam); |
1078 | } else { | | 1078 | } else { |
1079 | freeparam(&shellparam); | | 1079 | freeparam(&shellparam); |
1080 | shellparam = saveparam; | | 1080 | shellparam = saveparam; |
1081 | } | | 1081 | } |
1082 | unreffunc(cmdentry.u.func); | | 1082 | unreffunc(cmdentry.u.func); |
1083 | poplocalvars(); | | 1083 | poplocalvars(); |
1084 | localvars = savelocalvars; | | 1084 | localvars = savelocalvars; |
1085 | funclinebase = savefuncline; | | 1085 | funclinebase = savefuncline; |
1086 | funclineabs = savefuncabs; | | 1086 | funclineabs = savefuncabs; |
1087 | handler = savehandler; | | 1087 | handler = savehandler; |
1088 | longjmp(handler->loc, 1); | | 1088 | longjmp(handler->loc, 1); |
1089 | } | | 1089 | } |
1090 | savehandler = handler; | | 1090 | savehandler = handler; |
1091 | handler = &jmploc; | | 1091 | handler = &jmploc; |
1092 | if (cmdentry.u.func) { | | 1092 | if (cmdentry.u.func) { |
1093 | if (cmdentry.lno_frel) | | 1093 | if (cmdentry.lno_frel) |
1094 | funclinebase = cmdentry.lineno - 1; | | 1094 | funclinebase = cmdentry.lineno - 1; |
1095 | else | | 1095 | else |
1096 | funclinebase = 0; | | 1096 | funclinebase = 0; |
1097 | funclineabs = cmdentry.lineno; | | 1097 | funclineabs = cmdentry.lineno; |
1098 | | | 1098 | |
1099 | VTRACE(DBG_EVAL, | | 1099 | VTRACE(DBG_EVAL, |
1100 | ("function: node: %d '%s' # %d%s; funclinebase=%d\n", | | 1100 | ("function: node: %d '%s' # %d%s; funclinebase=%d\n", |
1101 | getfuncnode(cmdentry.u.func)->type, | | 1101 | getfuncnode(cmdentry.u.func)->type, |
1102 | NODETYPENAME(getfuncnode(cmdentry.u.func)->type), | | 1102 | NODETYPENAME(getfuncnode(cmdentry.u.func)->type), |
1103 | cmdentry.lineno, cmdentry.lno_frel?" (=1)":"", | | 1103 | cmdentry.lineno, cmdentry.lno_frel?" (=1)":"", |
1104 | funclinebase)); | | 1104 | funclinebase)); |
1105 | } | | 1105 | } |
1106 | listmklocal(varlist.list, VEXPORT); | | 1106 | listmklocal(varlist.list, VEXPORT); |
1107 | /* stop shell blowing its stack */ | | 1107 | /* stop shell blowing its stack */ |
1108 | if (++funcnest > 1000) | | 1108 | if (++funcnest > 1000) |
1109 | error("too many nested function calls"); | | 1109 | error("too many nested function calls"); |
1110 | evaltree(getfuncnode(cmdentry.u.func), flags & EV_TESTED); | | 1110 | evaltree(getfuncnode(cmdentry.u.func), flags & EV_TESTED); |
1111 | funcnest--; | | 1111 | funcnest--; |
1112 | INTOFF; | | 1112 | INTOFF; |
1113 | unreffunc(cmdentry.u.func); | | 1113 | unreffunc(cmdentry.u.func); |
1114 | poplocalvars(); | | 1114 | poplocalvars(); |
1115 | localvars = savelocalvars; | | 1115 | localvars = savelocalvars; |
1116 | funclinebase = savefuncline; | | 1116 | funclinebase = savefuncline; |
1117 | funclineabs = savefuncabs; | | 1117 | funclineabs = savefuncabs; |
1118 | freeparam(&shellparam); | | 1118 | freeparam(&shellparam); |
1119 | shellparam = saveparam; | | 1119 | shellparam = saveparam; |
1120 | handler = savehandler; | | 1120 | handler = savehandler; |
1121 | popredir(); | | 1121 | popredir(); |
1122 | INTON; | | 1122 | INTON; |
1123 | if (evalskip == SKIPFUNC) { | | 1123 | if (evalskip == SKIPFUNC) { |
1124 | evalskip = SKIPNONE; | | 1124 | evalskip = SKIPNONE; |
1125 | skipcount = 0; | | 1125 | skipcount = 0; |
1126 | } | | 1126 | } |
1127 | if (flags & EV_EXIT) | | 1127 | if (flags & EV_EXIT) |
1128 | exitshell(exitstatus); | | 1128 | exitshell(exitstatus); |
1129 | break; | | 1129 | break; |
1130 | | | 1130 | |
1131 | case CMDBUILTIN: | | 1131 | case CMDBUILTIN: |
1132 | case CMDSPLBLTIN: | | 1132 | case CMDSPLBLTIN: |
1133 | VXTRACE(DBG_EVAL, ("builtin command: "), trargs(argv)); | | 1133 | VXTRACE(DBG_EVAL, ("builtin command: "), trargs(argv)); |
1134 | mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH; | | 1134 | mode = (cmdentry.u.bltin == execcmd) ? 0 : REDIR_PUSH; |
1135 | if (flags == EV_BACKCMD) { | | 1135 | if (flags == EV_BACKCMD) { |
1136 | memout.nleft = 0; | | 1136 | memout.nleft = 0; |
1137 | memout.nextc = memout.buf; | | 1137 | memout.nextc = memout.buf; |
1138 | memout.bufsize = 64; | | 1138 | memout.bufsize = 64; |
1139 | mode |= REDIR_BACKQ; | | 1139 | mode |= REDIR_BACKQ; |
1140 | } | | 1140 | } |
1141 | e = -1; | | 1141 | e = -1; |
1142 | savehandler = handler; | | 1142 | savehandler = handler; |
1143 | savecmdname = commandname; | | 1143 | savecmdname = commandname; |
1144 | handler = &jmploc; | | 1144 | handler = &jmploc; |
1145 | temp_path = 0; | | 1145 | temp_path = 0; |
1146 | if (!setjmp(jmploc.loc)) { | | 1146 | if (!setjmp(jmploc.loc)) { |
1147 | /* | | 1147 | /* |
1148 | * We need to ensure the command hash table isn't | | 1148 | * We need to ensure the command hash table isn't |
1149 | * corrupted by temporary PATH assignments. | | 1149 | * corrupted by temporary PATH assignments. |
1150 | * However we must ensure the 'local' command works! | | 1150 | * However we must ensure the 'local' command works! |
1151 | */ | | 1151 | */ |
1152 | if (path != pathval() && (cmdentry.u.bltin == hashcmd || | | 1152 | if (path != pathval() && (cmdentry.u.bltin == hashcmd || |
1153 | cmdentry.u.bltin == typecmd)) { | | 1153 | cmdentry.u.bltin == typecmd)) { |
1154 | savelocalvars = localvars; | | 1154 | savelocalvars = localvars; |
1155 | localvars = 0; | | 1155 | localvars = 0; |
1156 | temp_path = 1; | | 1156 | temp_path = 1; |
1157 | mklocal(path - 5 /* PATH= */, 0); | | 1157 | mklocal(path - 5 /* PATH= */, 0); |
1158 | } | | 1158 | } |
1159 | redirect(cmd->ncmd.redirect, mode); | | 1159 | redirect(cmd->ncmd.redirect, mode); |
1160 | | | 1160 | |
1161 | /* exec is a special builtin, but needs this list... */ | | 1161 | /* exec is a special builtin, but needs this list... */ |
1162 | cmdenviron = varlist.list; | | 1162 | cmdenviron = varlist.list; |
1163 | /* we must check 'readonly' flag for all builtins */ | | 1163 | /* we must check 'readonly' flag for all builtins */ |
1164 | listsetvar(varlist.list, | | 1164 | listsetvar(varlist.list, |
1165 | cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET); | | 1165 | cmdentry.cmdtype == CMDSPLBLTIN ? 0 : VNOSET); |
1166 | commandname = argv[0]; | | 1166 | commandname = argv[0]; |
1167 | /* initialize nextopt */ | | 1167 | /* initialize nextopt */ |
1168 | argptr = argv + 1; | | 1168 | argptr = argv + 1; |
1169 | optptr = NULL; | | 1169 | optptr = NULL; |
1170 | /* and getopt */ | | 1170 | /* and getopt */ |
1171 | optreset = 1; | | 1171 | optreset = 1; |
1172 | optind = 1; | | 1172 | optind = 1; |
1173 | builtin_flags = flags; | | 1173 | builtin_flags = flags; |
1174 | exitstatus = cmdentry.u.bltin(argc, argv); | | 1174 | exitstatus = cmdentry.u.bltin(argc, argv); |
1175 | } else { | | 1175 | } else { |
1176 | e = exception; | | 1176 | e = exception; |
1177 | exitstatus = e == EXINT ? SIGINT + 128 : | | 1177 | exitstatus = e == EXINT ? SIGINT + 128 : |
1178 | e == EXEXEC ? exerrno : 2; | | 1178 | e == EXEXEC ? exerrno : 2; |
1179 | } | | 1179 | } |
1180 | handler = savehandler; | | 1180 | handler = savehandler; |
1181 | flushall(); | | 1181 | flushall(); |
1182 | out1 = &output; | | 1182 | out1 = &output; |
1183 | out2 = &errout; | | 1183 | out2 = &errout; |
1184 | freestdout(); | | 1184 | freestdout(); |
1185 | if (temp_path) { | | 1185 | if (temp_path) { |
1186 | poplocalvars(); | | 1186 | poplocalvars(); |
1187 | localvars = savelocalvars; | | 1187 | localvars = savelocalvars; |
1188 | } | | 1188 | } |
1189 | cmdenviron = NULL; | | 1189 | cmdenviron = NULL; |
1190 | if (e != EXSHELLPROC) { | | 1190 | if (e != EXSHELLPROC) { |
1191 | commandname = savecmdname; | | 1191 | commandname = savecmdname; |
1192 | if (flags & EV_EXIT) | | 1192 | if (flags & EV_EXIT) |
1193 | exitshell(exitstatus); | | 1193 | exitshell(exitstatus); |
1194 | } | | 1194 | } |
1195 | if (e != -1) { | | 1195 | if (e != -1) { |
1196 | if ((e != EXERROR && e != EXEXEC) | | 1196 | if ((e != EXERROR && e != EXEXEC) |
1197 | || cmdentry.cmdtype == CMDSPLBLTIN) | | 1197 | || cmdentry.cmdtype == CMDSPLBLTIN) |
1198 | exraise(e); | | 1198 | exraise(e); |
1199 | FORCEINTON; | | 1199 | FORCEINTON; |
1200 | } | | 1200 | } |
1201 | if (cmdentry.u.bltin != execcmd) | | 1201 | if (cmdentry.u.bltin != execcmd) |
1202 | popredir(); | | 1202 | popredir(); |
1203 | if (flags == EV_BACKCMD) { | | 1203 | if (flags == EV_BACKCMD) { |
1204 | backcmd->buf = memout.buf; | | 1204 | backcmd->buf = memout.buf; |
1205 | backcmd->nleft = memout.nextc - memout.buf; | | 1205 | backcmd->nleft = memout.nextc - memout.buf; |
1206 | memout.buf = NULL; | | 1206 | memout.buf = NULL; |
1207 | } | | 1207 | } |
1208 | break; | | 1208 | break; |
1209 | | | 1209 | |
1210 | default: | | 1210 | default: |
1211 | VXTRACE(DBG_EVAL, ("normal command: "), trargs(argv)); | | 1211 | VXTRACE(DBG_EVAL, ("normal command: "), trargs(argv)); |
1212 | redirect(cmd->ncmd.redirect, | | 1212 | redirect(cmd->ncmd.redirect, |
1213 | (vforked ? REDIR_VFORK : 0) | REDIR_KEEP); | | 1213 | (vforked ? REDIR_VFORK : 0) | REDIR_KEEP); |
1214 | if (!vforked) | | 1214 | if (!vforked) |
1215 | for (sp = varlist.list ; sp ; sp = sp->next) | | 1215 | for (sp = varlist.list ; sp ; sp = sp->next) |
1216 | setvareq(sp->text, VEXPORT|VSTACK); | | 1216 | setvareq(sp->text, VEXPORT|VSTACK); |
1217 | envp = environment(); | | 1217 | envp = environment(); |
1218 | shellexec(argv, envp, path, cmdentry.u.index, vforked); | | 1218 | shellexec(argv, envp, path, cmdentry.u.index, vforked); |
1219 | break; | | 1219 | break; |
1220 | } | | 1220 | } |
1221 | goto out; | | 1221 | goto out; |
1222 | | | 1222 | |
1223 | parent: /* parent process gets here (if we forked) */ | | 1223 | parent: /* parent process gets here (if we forked) */ |
1224 | | | 1224 | |
1225 | exitstatus = 0; /* if not altered just below */ | | 1225 | exitstatus = 0; /* if not altered just below */ |
1226 | if (mode == FORK_FG) { /* argument to fork */ | | 1226 | if (mode == FORK_FG) { /* argument to fork */ |
1227 | exitstatus = waitforjob(jp); | | 1227 | exitstatus = waitforjob(jp); |
1228 | } else if (mode == FORK_NOJOB) { | | 1228 | } else if (mode == FORK_NOJOB) { |
1229 | backcmd->fd = pip[0]; | | 1229 | backcmd->fd = pip[0]; |
1230 | close(pip[1]); | | 1230 | close(pip[1]); |
1231 | backcmd->jp = jp; | | 1231 | backcmd->jp = jp; |
1232 | } | | 1232 | } |
1233 | FORCEINTON; | | 1233 | FORCEINTON; |
1234 | | | 1234 | |
1235 | out: | | 1235 | out: |
1236 | if (lastarg) | | 1236 | if (lastarg) |
1237 | /* implement $_ for whatever use that really is */ | | 1237 | /* implement $_ for whatever use that really is */ |
1238 | (void) setvarsafe("_", lastarg, VNOERROR); | | 1238 | (void) setvarsafe("_", lastarg, VNOERROR); |
1239 | popstackmark(&smark); | | 1239 | popstackmark(&smark); |
1240 | } | | 1240 | } |
1241 | | | 1241 | |
1242 | | | 1242 | |
1243 | /* | | 1243 | /* |
1244 | * Search for a command. This is called before we fork so that the | | 1244 | * Search for a command. This is called before we fork so that the |
1245 | * location of the command will be available in the parent as well as | | 1245 | * location of the command will be available in the parent as well as |
1246 | * the child. The check for "goodname" is an overly conservative | | 1246 | * the child. The check for "goodname" is an overly conservative |
1247 | * check that the name will not be subject to expansion. | | 1247 | * check that the name will not be subject to expansion. |
1248 | */ | | 1248 | */ |
1249 | | | 1249 | |
1250 | STATIC void | | 1250 | STATIC void |
1251 | prehash(union node *n) | | 1251 | prehash(union node *n) |
1252 | { | | 1252 | { |
1253 | struct cmdentry entry; | | 1253 | struct cmdentry entry; |
1254 | | | 1254 | |
1255 | if (n && n->type == NCMD && n->ncmd.args) | | 1255 | if (n && n->type == NCMD && n->ncmd.args) |
1256 | if (goodname(n->ncmd.args->narg.text)) | | 1256 | if (goodname(n->ncmd.args->narg.text)) |
1257 | find_command(n->ncmd.args->narg.text, &entry, 0, | | 1257 | find_command(n->ncmd.args->narg.text, &entry, 0, |
1258 | pathval()); | | 1258 | pathval()); |
1259 | } | | 1259 | } |
1260 | | | 1260 | |
1261 | int | | 1261 | int |
1262 | in_function(void) | | 1262 | in_function(void) |
1263 | { | | 1263 | { |
1264 | return funcnest; | | 1264 | return funcnest; |
1265 | } | | 1265 | } |
1266 | | | 1266 | |
1267 | enum skipstate | | 1267 | enum skipstate |
1268 | current_skipstate(void) | | 1268 | current_skipstate(void) |
1269 | { | | 1269 | { |
1270 | return evalskip; | | 1270 | return evalskip; |
1271 | } | | 1271 | } |
1272 | | | 1272 | |
1273 | void | | 1273 | void |
1274 | stop_skipping(void) | | 1274 | stop_skipping(void) |
1275 | { | | 1275 | { |
1276 | evalskip = SKIPNONE; | | 1276 | evalskip = SKIPNONE; |
1277 | skipcount = 0; | | 1277 | skipcount = 0; |
1278 | } | | 1278 | } |
1279 | | | 1279 | |
1280 | /* | | 1280 | /* |
1281 | * Builtin commands. Builtin commands whose functions are closely | | 1281 | * Builtin commands. Builtin commands whose functions are closely |
1282 | * tied to evaluation are implemented here. | | 1282 | * tied to evaluation are implemented here. |
1283 | */ | | 1283 | */ |
1284 | | | 1284 | |
1285 | /* | | 1285 | /* |
1286 | * No command given. | | 1286 | * No command given. |
1287 | */ | | 1287 | */ |
1288 | | | 1288 | |
1289 | int | | 1289 | int |
1290 | bltincmd(int argc, char **argv) | | 1290 | bltincmd(int argc, char **argv) |
1291 | { | | 1291 | { |
1292 | /* | | 1292 | /* |
1293 | * Preserve exitstatus of a previous possible redirection | | 1293 | * Preserve exitstatus of a previous possible redirection |
1294 | * as POSIX mandates | | 1294 | * as POSIX mandates |
1295 | */ | | 1295 | */ |
1296 | return back_exitstatus; | | 1296 | return back_exitstatus; |
1297 | } | | 1297 | } |
1298 | | | 1298 | |
1299 | | | 1299 | |
1300 | /* | | 1300 | /* |
1301 | * Handle break and continue commands. Break, continue, and return are | | 1301 | * Handle break and continue commands. Break, continue, and return are |
1302 | * all handled by setting the evalskip flag. The evaluation routines | | 1302 | * all handled by setting the evalskip flag. The evaluation routines |
1303 | * above all check this flag, and if it is set they start skipping | | 1303 | * above all check this flag, and if it is set they start skipping |
1304 | * commands rather than executing them. The variable skipcount is | | 1304 | * commands rather than executing them. The variable skipcount is |
1305 | * the number of loops to break/continue, or the number of function | | 1305 | * the number of loops to break/continue, or the number of function |
1306 | * levels to return. (The latter is always 1.) It should probably | | 1306 | * levels to return. (The latter is always 1.) It should probably |
1307 | * be an error to break out of more loops than exist, but it isn't | | 1307 | * be an error to break out of more loops than exist, but it isn't |
1308 | * in the standard shell so we don't make it one here. | | 1308 | * in the standard shell so we don't make it one here. |
1309 | */ | | 1309 | */ |
1310 | | | 1310 | |
1311 | int | | 1311 | int |
1312 | breakcmd(int argc, char **argv) | | 1312 | breakcmd(int argc, char **argv) |
1313 | { | | 1313 | { |
1314 | int n = argc > 1 ? number(argv[1]) : 1; | | 1314 | int n = argc > 1 ? number(argv[1]) : 1; |
1315 | | | 1315 | |
1316 | if (n <= 0) | | 1316 | if (n <= 0) |
1317 | error("invalid count: %d", n); | | 1317 | error("invalid count: %d", n); |
1318 | if (n > loopnest) | | 1318 | if (n > loopnest) |
1319 | n = loopnest; | | 1319 | n = loopnest; |
1320 | if (n > 0) { | | 1320 | if (n > 0) { |
1321 | evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; | | 1321 | evalskip = (**argv == 'c')? SKIPCONT : SKIPBREAK; |
1322 | skipcount = n; | | 1322 | skipcount = n; |
1323 | } | | 1323 | } |
1324 | return 0; | | 1324 | return 0; |
1325 | } | | 1325 | } |
1326 | | | 1326 | |
1327 | int | | 1327 | int |
1328 | dotcmd(int argc, char **argv) | | 1328 | dotcmd(int argc, char **argv) |
1329 | { | | 1329 | { |
1330 | exitstatus = 0; | | 1330 | exitstatus = 0; |
1331 | | | 1331 | |
1332 | if (argc >= 2) { /* That's what SVR2 does */ | | 1332 | if (argc >= 2) { /* That's what SVR2 does */ |
1333 | char *fullname; | | 1333 | char *fullname; |
1334 | /* | | 1334 | /* |
1335 | * dot_funcnest needs to be 0 when not in a dotcmd, so it | | 1335 | * dot_funcnest needs to be 0 when not in a dotcmd, so it |
1336 | * cannot be restored with (funcnest + 1). | | 1336 | * cannot be restored with (funcnest + 1). |
1337 | */ | | 1337 | */ |
1338 | int dot_funcnest_old; | | 1338 | int dot_funcnest_old; |
1339 | struct stackmark smark; | | 1339 | struct stackmark smark; |
1340 | | | 1340 | |
1341 | setstackmark(&smark); | | 1341 | setstackmark(&smark); |
1342 | fullname = find_dot_file(argv[1]); | | 1342 | fullname = find_dot_file(argv[1]); |
1343 | setinputfile(fullname, 1); | | 1343 | setinputfile(fullname, 1); |
1344 | commandname = fullname; | | 1344 | commandname = fullname; |
1345 | dot_funcnest_old = dot_funcnest; | | 1345 | dot_funcnest_old = dot_funcnest; |
1346 | dot_funcnest = funcnest + 1; | | 1346 | dot_funcnest = funcnest + 1; |
1347 | cmdloop(0); | | 1347 | cmdloop(0); |
1348 | dot_funcnest = dot_funcnest_old; | | 1348 | dot_funcnest = dot_funcnest_old; |
1349 | popfile(); | | 1349 | popfile(); |
1350 | popstackmark(&smark); | | 1350 | popstackmark(&smark); |
1351 | } | | 1351 | } |
1352 | return exitstatus; | | 1352 | return exitstatus; |
1353 | } | | 1353 | } |
1354 | | | 1354 | |
1355 | /* | | 1355 | /* |
1356 | * Take commands from a file. To be compatible we should do a path | | 1356 | * Take commands from a file. To be compatible we should do a path |
1357 | * search for the file, which is necessary to find sub-commands. | | 1357 | * search for the file, which is necessary to find sub-commands. |
1358 | */ | | 1358 | */ |
1359 | | | 1359 | |
1360 | STATIC char * | | 1360 | STATIC char * |
1361 | find_dot_file(char *basename) | | 1361 | find_dot_file(char *basename) |
1362 | { | | 1362 | { |
1363 | char *fullname; | | 1363 | char *fullname; |
1364 | const char *path = pathval(); | | 1364 | const char *path = pathval(); |
1365 | struct stat statb; | | 1365 | struct stat statb; |
1366 | | | 1366 | |
1367 | /* don't try this for absolute or relative paths */ | | 1367 | /* don't try this for absolute or relative paths */ |
1368 | if (strchr(basename, '/')) { | | 1368 | if (strchr(basename, '/')) { |
1369 | if (stat(basename, &statb) == 0) { | | 1369 | if (stat(basename, &statb) == 0) { |
1370 | if (S_ISDIR(statb.st_mode)) | | 1370 | if (S_ISDIR(statb.st_mode)) |
1371 | error("%s: is a directory", basename); | | 1371 | error("%s: is a directory", basename); |
1372 | if (S_ISBLK(statb.st_mode)) | | 1372 | if (S_ISBLK(statb.st_mode)) |
1373 | error("%s: is a block device", basename); | | 1373 | error("%s: is a block device", basename); |
1374 | return basename; | | 1374 | return basename; |
1375 | } | | 1375 | } |
1376 | } else while ((fullname = padvance(&path, basename, 1)) != NULL) { | | 1376 | } else while ((fullname = padvance(&path, basename, 1)) != NULL) { |
1377 | if ((stat(fullname, &statb) == 0)) { | | 1377 | if ((stat(fullname, &statb) == 0)) { |
1378 | /* weird format is to ease future code... */ | | 1378 | /* weird format is to ease future code... */ |
1379 | if (S_ISDIR(statb.st_mode) || S_ISBLK(statb.st_mode)) | | 1379 | if (S_ISDIR(statb.st_mode) || S_ISBLK(statb.st_mode)) |
1380 | ; | | 1380 | ; |
1381 | #if notyet | | 1381 | #if notyet |
1382 | else if (unreadable()) { | | 1382 | else if (unreadable()) { |
1383 | /* | | 1383 | /* |
1384 | * testing this via st_mode is ugly to get | | 1384 | * testing this via st_mode is ugly to get |
1385 | * correct (and would ignore ACLs). | | 1385 | * correct (and would ignore ACLs). |
1386 | * better way is just to open the file. | | 1386 | * better way is just to open the file. |
1387 | * But doing that here would (currently) | | 1387 | * But doing that here would (currently) |
1388 | * mean opening the file twice, which | | 1388 | * mean opening the file twice, which |
1389 | * might not be safe. So, defer this | | 1389 | * might not be safe. So, defer this |
1390 | * test until code is restructures so | | 1390 | * test until code is restructures so |
1391 | * we can return a fd. Then we also | | 1391 | * we can return a fd. Then we also |
1392 | * get to fix the mem leak just below... | | 1392 | * get to fix the mem leak just below... |
1393 | */ | | 1393 | */ |
1394 | } | | 1394 | } |
1395 | #endif | | 1395 | #endif |
1396 | else { | | 1396 | else { |
1397 | /* | | 1397 | /* |
1398 | * Don't bother freeing here, since | | 1398 | * Don't bother freeing here, since |
1399 | * it will be freed by the caller. | | 1399 | * it will be freed by the caller. |
1400 | * XXX no it won't - a bug for later. | | 1400 | * XXX no it won't - a bug for later. |
1401 | */ | | 1401 | */ |
1402 | return fullname; | | 1402 | return fullname; |
1403 | } | | 1403 | } |
1404 | } | | 1404 | } |
1405 | stunalloc(fullname); | | 1405 | stunalloc(fullname); |
1406 | } | | 1406 | } |
1407 | | | 1407 | |
1408 | /* not found in the PATH */ | | 1408 | /* not found in the PATH */ |
1409 | error("%s: not found", basename); | | 1409 | error("%s: not found", basename); |
1410 | /* NOTREACHED */ | | 1410 | /* NOTREACHED */ |
1411 | } | | 1411 | } |
1412 | | | 1412 | |
1413 | | | 1413 | |
1414 | | | 1414 | |
1415 | /* | | 1415 | /* |
1416 | * The return command. | | 1416 | * The return command. |
1417 | * | | 1417 | * |
1418 | * Quoth the POSIX standard: | | 1418 | * Quoth the POSIX standard: |
1419 | * The return utility shall cause the shell to stop executing the current | | 1419 | * The return utility shall cause the shell to stop executing the current |
1420 | * function or dot script. If the shell is not currently executing | | 1420 | * function or dot script. If the shell is not currently executing |
1421 | * a function or dot script, the results are unspecified. | | 1421 | * a function or dot script, the results are unspecified. |
1422 | * | | 1422 | * |
1423 | * As for the unspecified part, there seems to be no de-facto standard: bash | | 1423 | * As for the unspecified part, there seems to be no de-facto standard: bash |
1424 | * ignores the return with a warning, zsh ignores the return in interactive | | 1424 | * ignores the return with a warning, zsh ignores the return in interactive |
1425 | * mode but seems to liken it to exit in a script. (checked May 2014) | | 1425 | * mode but seems to liken it to exit in a script. (checked May 2014) |
1426 | * | | 1426 | * |
1427 | * We choose to silently ignore the return. Older versions of this shell | | 1427 | * We choose to silently ignore the return. Older versions of this shell |
1428 | * set evalskip to SKIPFILE causing the shell to (indirectly) exit. This | | 1428 | * set evalskip to SKIPFILE causing the shell to (indirectly) exit. This |
1429 | * had at least the problem of circumventing the check for stopped jobs, | | 1429 | * had at least the problem of circumventing the check for stopped jobs, |
1430 | * which would occur for exit or ^D. | | 1430 | * which would occur for exit or ^D. |
1431 | */ | | 1431 | */ |
1432 | | | 1432 | |
1433 | int | | 1433 | int |
1434 | returncmd(int argc, char **argv) | | 1434 | returncmd(int argc, char **argv) |
1435 | { | | 1435 | { |
1436 | int ret = argc > 1 ? number(argv[1]) : exitstatus; | | 1436 | int ret = argc > 1 ? number(argv[1]) : exitstatus; |
1437 | | | 1437 | |
1438 | if ((dot_funcnest == 0 && funcnest) | | 1438 | if ((dot_funcnest == 0 && funcnest) |
1439 | || (dot_funcnest > 0 && funcnest - (dot_funcnest - 1) > 0)) { | | 1439 | || (dot_funcnest > 0 && funcnest - (dot_funcnest - 1) > 0)) { |
1440 | evalskip = SKIPFUNC; | | 1440 | evalskip = SKIPFUNC; |
1441 | skipcount = 1; | | 1441 | skipcount = 1; |
1442 | } else if (dot_funcnest > 0) { | | 1442 | } else if (dot_funcnest > 0) { |
1443 | evalskip = SKIPFILE; | | 1443 | evalskip = SKIPFILE; |
1444 | skipcount = 1; | | 1444 | skipcount = 1; |
1445 | } else { | | 1445 | } else { |
1446 | /* XXX: should a warning be issued? */ | | 1446 | /* XXX: should a warning be issued? */ |
1447 | ret = 0; | | 1447 | ret = 0; |
1448 | } | | 1448 | } |
1449 | | | 1449 | |
1450 | return ret; | | 1450 | return ret; |
1451 | } | | 1451 | } |
1452 | | | 1452 | |
1453 | | | 1453 | |
1454 | int | | 1454 | int |
1455 | falsecmd(int argc, char **argv) | | 1455 | falsecmd(int argc, char **argv) |
1456 | { | | 1456 | { |
1457 | return 1; | | 1457 | return 1; |
1458 | } | | 1458 | } |
1459 | | | 1459 | |
1460 | | | 1460 | |
1461 | int | | 1461 | int |
1462 | truecmd(int argc, char **argv) | | 1462 | truecmd(int argc, char **argv) |
1463 | { | | 1463 | { |
1464 | return 0; | | 1464 | return 0; |
1465 | } | | 1465 | } |
1466 | | | 1466 | |
1467 | | | 1467 | |
1468 | int | | 1468 | int |
1469 | execcmd(int argc, char **argv) | | 1469 | execcmd(int argc, char **argv) |
1470 | { | | 1470 | { |
1471 | if (argc > 1) { | | 1471 | if (argc > 1) { |
1472 | struct strlist *sp; | | 1472 | struct strlist *sp; |
1473 | | | 1473 | |
1474 | iflag = 0; /* exit on error */ | | 1474 | iflag = 0; /* exit on error */ |
1475 | mflag = 0; | | 1475 | mflag = 0; |
1476 | optschanged(); | | 1476 | optschanged(); |
1477 | for (sp = cmdenviron; sp; sp = sp->next) | | 1477 | for (sp = cmdenviron; sp; sp = sp->next) |
1478 | setvareq(sp->text, VEXPORT|VSTACK); | | 1478 | setvareq(sp->text, VEXPORT|VSTACK); |
1479 | shellexec(argv + 1, environment(), pathval(), 0, 0); | | 1479 | shellexec(argv + 1, environment(), pathval(), 0, 0); |
1480 | } | | 1480 | } |
1481 | return 0; | | 1481 | return 0; |
1482 | } | | 1482 | } |
1483 | | | 1483 | |
1484 | static int | | 1484 | static int |
1485 | conv_time(clock_t ticks, char *seconds, size_t l) | | 1485 | conv_time(clock_t ticks, char *seconds, size_t l) |
1486 | { | | 1486 | { |
1487 | static clock_t tpm = 0; | | 1487 | static clock_t tpm = 0; |
1488 | clock_t mins; | | 1488 | clock_t mins; |
1489 | int i; | | 1489 | int i; |
1490 | | | 1490 | |
1491 | if (!tpm) | | 1491 | if (!tpm) |
1492 | tpm = sysconf(_SC_CLK_TCK) * 60; | | 1492 | tpm = sysconf(_SC_CLK_TCK) * 60; |
1493 | | | 1493 | |
1494 | mins = ticks / tpm; | | 1494 | mins = ticks / tpm; |
1495 | snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm ); | | 1495 | snprintf(seconds, l, "%.4f", (ticks - mins * tpm) * 60.0 / tpm ); |
1496 | | | 1496 | |
1497 | if (seconds[0] == '6' && seconds[1] == '0') { | | 1497 | if (seconds[0] == '6' && seconds[1] == '0') { |
1498 | /* 59.99995 got rounded up... */ | | 1498 | /* 59.99995 got rounded up... */ |
1499 | mins++; | | 1499 | mins++; |
1500 | strlcpy(seconds, "0.0", l); | | 1500 | strlcpy(seconds, "0.0", l); |
1501 | return mins; | | 1501 | return mins; |
1502 | } | | 1502 | } |
1503 | | | 1503 | |
1504 | /* suppress trailing zeros */ | | 1504 | /* suppress trailing zeros */ |
1505 | i = strlen(seconds) - 1; | | 1505 | i = strlen(seconds) - 1; |
1506 | for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--) | | 1506 | for (; seconds[i] == '0' && seconds[i - 1] != '.'; i--) |
1507 | seconds[i] = 0; | | 1507 | seconds[i] = 0; |
1508 | return mins; | | 1508 | return mins; |
1509 | } | | 1509 | } |
1510 | | | 1510 | |
1511 | int | | 1511 | int |
1512 | timescmd(int argc, char **argv) | | 1512 | timescmd(int argc, char **argv) |
1513 | { | | 1513 | { |
1514 | struct tms tms; | | 1514 | struct tms tms; |
1515 | int u, s, cu, cs; | | 1515 | int u, s, cu, cs; |
1516 | char us[8], ss[8], cus[8], css[8]; | | 1516 | char us[8], ss[8], cus[8], css[8]; |
1517 | | | 1517 | |
1518 | nextopt(""); | | 1518 | nextopt(""); |
1519 | | | 1519 | |
1520 | times(&tms); | | 1520 | times(&tms); |
1521 | | | 1521 | |
1522 | u = conv_time(tms.tms_utime, us, sizeof(us)); | | 1522 | u = conv_time(tms.tms_utime, us, sizeof(us)); |
1523 | s = conv_time(tms.tms_stime, ss, sizeof(ss)); | | 1523 | s = conv_time(tms.tms_stime, ss, sizeof(ss)); |
1524 | cu = conv_time(tms.tms_cutime, cus, sizeof(cus)); | | 1524 | cu = conv_time(tms.tms_cutime, cus, sizeof(cus)); |
1525 | cs = conv_time(tms.tms_cstime, css, sizeof(css)); | | 1525 | cs = conv_time(tms.tms_cstime, css, sizeof(css)); |
1526 | | | 1526 | |
1527 | outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n", | | 1527 | outfmt(out1, "%dm%ss %dm%ss\n%dm%ss %dm%ss\n", |
1528 | u, us, s, ss, cu, cus, cs, css); | | 1528 | u, us, s, ss, cu, cus, cs, css); |
1529 | | | 1529 | |
1530 | return 0; | | 1530 | return 0; |
1531 | } | | 1531 | } |