exec.c revision 1.7 1 1.7 provos /* $NetBSD: exec.c,v 1.7 2002/09/25 02:41:11 provos Exp $ */
2 1.2 tls
3 1.1 jtc /*
4 1.1 jtc * execute command tree
5 1.1 jtc */
6 1.1 jtc
7 1.1 jtc #include "sh.h"
8 1.1 jtc #include "c_test.h"
9 1.1 jtc #include <ctype.h>
10 1.1 jtc #include "ksh_stat.h"
11 1.1 jtc
12 1.1 jtc /* Does ps4 get parameter substitutions done? */
13 1.1 jtc #ifdef KSH
14 1.1 jtc # define PS4_SUBSTITUTE(s) substitute((s), 0)
15 1.1 jtc #else
16 1.1 jtc # define PS4_SUBSTITUTE(s) (s)
17 1.1 jtc #endif /* KSH */
18 1.1 jtc
19 1.1 jtc static int comexec ARGS((struct op *t, struct tbl *volatile tp, char **ap,
20 1.1 jtc int volatile flags));
21 1.1 jtc static void scriptexec ARGS((struct op *tp, char **ap));
22 1.1 jtc static int call_builtin ARGS((struct tbl *tp, char **wp));
23 1.1 jtc static int iosetup ARGS((struct ioword *iop, struct tbl *tp));
24 1.5 hubertf static int herein ARGS((const char *content, int sub));
25 1.1 jtc #ifdef KSH
26 1.1 jtc static char *do_selectargs ARGS((char **ap, bool_t print_menu));
27 1.1 jtc #endif /* KSH */
28 1.1 jtc #ifdef KSH
29 1.1 jtc static int dbteste_isa ARGS((Test_env *te, Test_meta meta));
30 1.1 jtc static const char *dbteste_getopnd ARGS((Test_env *te, Test_op op,
31 1.1 jtc int do_eval));
32 1.1 jtc static int dbteste_eval ARGS((Test_env *te, Test_op op, const char *opnd1,
33 1.1 jtc const char *opnd2, int do_eval));
34 1.1 jtc static void dbteste_error ARGS((Test_env *te, int offset, const char *msg));
35 1.1 jtc #endif /* KSH */
36 1.1 jtc #ifdef OS2
37 1.2 tls static int search_access1 ARGS((const char *path, int mode, int *errnop));
38 1.1 jtc #endif /* OS2 */
39 1.1 jtc
40 1.1 jtc
41 1.1 jtc /*
42 1.1 jtc * handle systems that don't have F_SETFD
43 1.1 jtc */
44 1.1 jtc #ifndef F_SETFD
45 1.1 jtc # ifndef MAXFD
46 1.1 jtc # define MAXFD 64
47 1.1 jtc # endif
48 1.1 jtc /* a bit field would be smaller, but this will work */
49 1.1 jtc static char clexec_tab[MAXFD+1];
50 1.1 jtc #endif
51 1.1 jtc
52 1.1 jtc /*
53 1.1 jtc * we now use this function always.
54 1.1 jtc */
55 1.1 jtc int
56 1.1 jtc fd_clexec(fd)
57 1.1 jtc int fd;
58 1.1 jtc {
59 1.1 jtc #ifndef F_SETFD
60 1.1 jtc if (fd >= 0 && fd < sizeof(clexec_tab)) {
61 1.1 jtc clexec_tab[fd] = 1;
62 1.1 jtc return 0;
63 1.1 jtc }
64 1.1 jtc return -1;
65 1.1 jtc #else
66 1.1 jtc return fcntl(fd, F_SETFD, 1);
67 1.1 jtc #endif
68 1.1 jtc }
69 1.1 jtc
70 1.1 jtc
71 1.1 jtc /*
72 1.1 jtc * execute command tree
73 1.1 jtc */
74 1.1 jtc int
75 1.1 jtc execute(t, flags)
76 1.1 jtc struct op * volatile t;
77 1.1 jtc volatile int flags; /* if XEXEC don't fork */
78 1.1 jtc {
79 1.1 jtc int i;
80 1.1 jtc volatile int rv = 0;
81 1.6 wiz volatile int rv_prop = 0; /* rv being propogated or newly generated? */
82 1.1 jtc int pv[2];
83 1.1 jtc char ** volatile ap;
84 1.1 jtc char *s, *cp;
85 1.1 jtc struct ioword **iowp;
86 1.1 jtc struct tbl *tp = NULL;
87 1.1 jtc
88 1.1 jtc if (t == NULL)
89 1.1 jtc return 0;
90 1.1 jtc
91 1.1 jtc /* Is this the end of a pipeline? If so, we want to evaluate the
92 1.1 jtc * command arguments
93 1.1 jtc bool_t eval_done = FALSE;
94 1.1 jtc if ((flags&XFORK) && !(flags&XEXEC) && (flags&XPCLOSE)) {
95 1.1 jtc eval_done = TRUE;
96 1.1 jtc tp = eval_execute_args(t, &ap);
97 1.1 jtc }
98 1.1 jtc */
99 1.1 jtc if ((flags&XFORK) && !(flags&XEXEC) && t->type != TPIPE)
100 1.5 hubertf return exchild(t, flags & ~XTIME, -1); /* run in sub-process */
101 1.1 jtc
102 1.1 jtc newenv(E_EXEC);
103 1.1 jtc if (trap)
104 1.1 jtc runtraps(0);
105 1.1 jtc
106 1.1 jtc if (t->type == TCOM) {
107 1.1 jtc /* Clear subst_exstat before argument expansion. Used by
108 1.5 hubertf * null commands (see comexec() and c_eval()) and by c_set().
109 1.1 jtc */
110 1.1 jtc subst_exstat = 0;
111 1.1 jtc
112 1.5 hubertf current_lineno = t->lineno; /* for $LINENO */
113 1.5 hubertf
114 1.1 jtc /* POSIX says expand command words first, then redirections,
115 1.1 jtc * and assignments last..
116 1.1 jtc */
117 1.1 jtc ap = eval(t->args, t->u.evalflags | DOBLANK | DOGLOB | DOTILDE);
118 1.5 hubertf if (flags & XTIME)
119 1.5 hubertf /* Allow option parsing (bizarre, but POSIX) */
120 1.5 hubertf timex_hook(t, &ap);
121 1.1 jtc if (Flag(FXTRACE) && ap[0]) {
122 1.1 jtc shf_fprintf(shl_out, "%s",
123 1.1 jtc PS4_SUBSTITUTE(str_val(global("PS4"))));
124 1.1 jtc for (i = 0; ap[i]; i++)
125 1.1 jtc shf_fprintf(shl_out, "%s%s", ap[i],
126 1.1 jtc ap[i + 1] ? space : newline);
127 1.1 jtc shf_flush(shl_out);
128 1.1 jtc }
129 1.1 jtc if (ap[0])
130 1.1 jtc tp = findcom(ap[0], FC_BI|FC_FUNC);
131 1.1 jtc }
132 1.5 hubertf flags &= ~XTIME;
133 1.1 jtc
134 1.1 jtc if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) {
135 1.1 jtc e->savefd = (short *) alloc(sizeofN(short, NUFILE), ATEMP);
136 1.1 jtc /* initialize to not redirected */
137 1.1 jtc memset(e->savefd, 0, sizeofN(short, NUFILE));
138 1.1 jtc }
139 1.1 jtc
140 1.1 jtc /* do redirection, to be restored in quitenv() */
141 1.1 jtc if (t->ioact != NULL)
142 1.1 jtc for (iowp = t->ioact; *iowp != NULL; iowp++) {
143 1.1 jtc if (iosetup(*iowp, tp) < 0) {
144 1.1 jtc exstat = rv = 1;
145 1.1 jtc /* Redirection failures for special commands
146 1.1 jtc * cause (non-interactive) shell to exit.
147 1.1 jtc */
148 1.1 jtc if (tp && tp->type == CSHELL
149 1.1 jtc && (tp->flag & SPEC_BI))
150 1.1 jtc errorf(null);
151 1.1 jtc /* Deal with FERREXIT, quitenv(), etc. */
152 1.1 jtc goto Break;
153 1.1 jtc }
154 1.1 jtc }
155 1.1 jtc
156 1.1 jtc switch(t->type) {
157 1.1 jtc case TCOM:
158 1.1 jtc rv = comexec(t, tp, ap, flags);
159 1.1 jtc break;
160 1.1 jtc
161 1.1 jtc case TPAREN:
162 1.1 jtc rv = execute(t->left, flags|XFORK);
163 1.6 wiz rv_prop = 1;
164 1.1 jtc break;
165 1.1 jtc
166 1.1 jtc case TPIPE:
167 1.1 jtc flags |= XFORK;
168 1.1 jtc flags &= ~XEXEC;
169 1.1 jtc e->savefd[0] = savefd(0, 0);
170 1.1 jtc (void) ksh_dup2(e->savefd[0], 0, FALSE); /* stdin of first */
171 1.1 jtc e->savefd[1] = savefd(1, 0);
172 1.1 jtc while (t->type == TPIPE) {
173 1.1 jtc openpipe(pv);
174 1.1 jtc (void) ksh_dup2(pv[1], 1, FALSE); /* stdout of curr */
175 1.1 jtc /* Let exchild() close pv[0] in child
176 1.1 jtc * (if this isn't done, commands like
177 1.1 jtc * (: ; cat /etc/termcap) | sleep 1
178 1.1 jtc * will hang forever).
179 1.1 jtc */
180 1.1 jtc exchild(t->left, flags|XPIPEO|XCCLOSE, pv[0]);
181 1.1 jtc (void) ksh_dup2(pv[0], 0, FALSE); /* stdin of next */
182 1.1 jtc closepipe(pv);
183 1.1 jtc flags |= XPIPEI;
184 1.1 jtc t = t->right;
185 1.1 jtc }
186 1.1 jtc restfd(1, e->savefd[1]); /* stdout of last */
187 1.1 jtc e->savefd[1] = 0; /* no need to re-restore this */
188 1.1 jtc /* Let exchild() close 0 in parent, after fork, before wait */
189 1.1 jtc i = exchild(t, flags|XPCLOSE, 0);
190 1.1 jtc if (!(flags&XBGND) && !(flags&XXCOM))
191 1.1 jtc rv = i;
192 1.1 jtc break;
193 1.1 jtc
194 1.1 jtc case TLIST:
195 1.1 jtc while (t->type == TLIST) {
196 1.1 jtc execute(t->left, flags & XERROK);
197 1.1 jtc t = t->right;
198 1.1 jtc }
199 1.1 jtc rv = execute(t, flags & XERROK);
200 1.1 jtc break;
201 1.1 jtc
202 1.1 jtc #ifdef KSH
203 1.1 jtc case TCOPROC:
204 1.1 jtc {
205 1.1 jtc # ifdef JOB_SIGS
206 1.1 jtc sigset_t omask;
207 1.1 jtc # endif /* JOB_SIGS */
208 1.1 jtc
209 1.1 jtc # ifdef JOB_SIGS
210 1.1 jtc /* Block sigchild as we are using things changed in the
211 1.1 jtc * signal handler
212 1.1 jtc */
213 1.1 jtc sigprocmask(SIG_BLOCK, &sm_sigchld, &omask);
214 1.1 jtc e->type = E_ERRH;
215 1.1 jtc i = ksh_sigsetjmp(e->jbuf, 0);
216 1.1 jtc if (i) {
217 1.1 jtc sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
218 1.1 jtc quitenv();
219 1.1 jtc unwind(i);
220 1.1 jtc /*NOTREACHED*/
221 1.1 jtc }
222 1.1 jtc # endif /* JOB_SIGS */
223 1.1 jtc /* Already have a (live) co-process? */
224 1.1 jtc if (coproc.job && coproc.write >= 0)
225 1.1 jtc errorf("coprocess already exists");
226 1.1 jtc
227 1.1 jtc /* Can we re-use the existing co-process pipe? */
228 1.1 jtc coproc_cleanup(TRUE);
229 1.1 jtc
230 1.1 jtc /* do this before opening pipes, in case these fail */
231 1.1 jtc e->savefd[0] = savefd(0, 0);
232 1.1 jtc e->savefd[1] = savefd(1, 0);
233 1.1 jtc
234 1.1 jtc openpipe(pv);
235 1.1 jtc ksh_dup2(pv[0], 0, FALSE);
236 1.1 jtc close(pv[0]);
237 1.1 jtc coproc.write = pv[1];
238 1.1 jtc coproc.job = (void *) 0;
239 1.1 jtc
240 1.1 jtc if (coproc.readw >= 0)
241 1.1 jtc ksh_dup2(coproc.readw, 1, FALSE);
242 1.1 jtc else {
243 1.1 jtc openpipe(pv);
244 1.1 jtc coproc.read = pv[0];
245 1.1 jtc ksh_dup2(pv[1], 1, FALSE);
246 1.1 jtc coproc.readw = pv[1]; /* closed before first read */
247 1.1 jtc coproc.njobs = 0;
248 1.1 jtc /* create new coprocess id */
249 1.1 jtc ++coproc.id;
250 1.1 jtc }
251 1.1 jtc # ifdef JOB_SIGS
252 1.1 jtc sigprocmask(SIG_SETMASK, &omask, (sigset_t *) 0);
253 1.1 jtc e->type = E_EXEC; /* no more need for error handler */
254 1.1 jtc # endif /* JOB_SIGS */
255 1.1 jtc
256 1.1 jtc /* exchild() closes coproc.* in child after fork,
257 1.1 jtc * will also increment coproc.njobs when the
258 1.1 jtc * job is actually created.
259 1.1 jtc */
260 1.1 jtc flags &= ~XEXEC;
261 1.1 jtc exchild(t->left, flags|XBGND|XFORK|XCOPROC|XCCLOSE,
262 1.1 jtc coproc.readw);
263 1.1 jtc break;
264 1.1 jtc }
265 1.1 jtc #endif /* KSH */
266 1.1 jtc
267 1.1 jtc case TASYNC:
268 1.1 jtc /* XXX non-optimal, I think - "(foo &)", forks for (),
269 1.1 jtc * forks again for async... parent should optimize
270 1.1 jtc * this to "foo &"...
271 1.1 jtc */
272 1.1 jtc rv = execute(t->left, (flags&~XEXEC)|XBGND|XFORK);
273 1.1 jtc break;
274 1.1 jtc
275 1.1 jtc case TOR:
276 1.1 jtc case TAND:
277 1.1 jtc rv = execute(t->left, XERROK);
278 1.1 jtc if (t->right != NULL && (rv == 0) == (t->type == TAND))
279 1.1 jtc rv = execute(t->right, flags & XERROK);
280 1.1 jtc else
281 1.1 jtc flags |= XERROK;
282 1.6 wiz rv_prop = 1;
283 1.1 jtc break;
284 1.1 jtc
285 1.1 jtc case TBANG:
286 1.1 jtc rv = !execute(t->right, XERROK);
287 1.1 jtc break;
288 1.1 jtc
289 1.1 jtc #ifdef KSH
290 1.1 jtc case TDBRACKET:
291 1.1 jtc {
292 1.1 jtc Test_env te;
293 1.1 jtc
294 1.1 jtc te.flags = TEF_DBRACKET;
295 1.1 jtc te.pos.wp = t->args;
296 1.1 jtc te.isa = dbteste_isa;
297 1.1 jtc te.getopnd = dbteste_getopnd;
298 1.1 jtc te.eval = dbteste_eval;
299 1.1 jtc te.error = dbteste_error;
300 1.1 jtc
301 1.1 jtc rv = test_parse(&te);
302 1.1 jtc break;
303 1.1 jtc }
304 1.1 jtc #endif /* KSH */
305 1.1 jtc
306 1.1 jtc case TFOR:
307 1.1 jtc #ifdef KSH
308 1.1 jtc case TSELECT:
309 1.1 jtc {
310 1.1 jtc volatile bool_t is_first = TRUE;
311 1.1 jtc #endif /* KSH */
312 1.1 jtc ap = (t->vars != NULL) ?
313 1.1 jtc eval(t->vars, DOBLANK|DOGLOB|DOTILDE)
314 1.1 jtc : e->loc->argv + 1;
315 1.1 jtc e->type = E_LOOP;
316 1.1 jtc while (1) {
317 1.1 jtc i = ksh_sigsetjmp(e->jbuf, 0);
318 1.1 jtc if (!i)
319 1.1 jtc break;
320 1.1 jtc if ((e->flags&EF_BRKCONT_PASS)
321 1.1 jtc || (i != LBREAK && i != LCONTIN))
322 1.1 jtc {
323 1.1 jtc quitenv();
324 1.1 jtc unwind(i);
325 1.1 jtc } else if (i == LBREAK) {
326 1.1 jtc rv = 0;
327 1.1 jtc goto Break;
328 1.1 jtc }
329 1.1 jtc }
330 1.1 jtc rv = 0; /* in case of a continue */
331 1.6 wiz rv_prop = 1;
332 1.1 jtc if (t->type == TFOR) {
333 1.1 jtc while (*ap != NULL) {
334 1.5 hubertf setstr(global(t->str), *ap++, KSH_UNWIND_ERROR);
335 1.1 jtc rv = execute(t->left, flags & XERROK);
336 1.1 jtc }
337 1.1 jtc }
338 1.1 jtc #ifdef KSH
339 1.1 jtc else { /* TSELECT */
340 1.1 jtc for (;;) {
341 1.1 jtc if (!(cp = do_selectargs(ap, is_first))) {
342 1.1 jtc rv = 1;
343 1.6 wiz rv_prop = 0;
344 1.1 jtc break;
345 1.1 jtc }
346 1.1 jtc is_first = FALSE;
347 1.5 hubertf setstr(global(t->str), cp, KSH_UNWIND_ERROR);
348 1.1 jtc rv = execute(t->left, flags & XERROK);
349 1.1 jtc }
350 1.1 jtc }
351 1.1 jtc }
352 1.1 jtc #endif /* KSH */
353 1.1 jtc break;
354 1.1 jtc
355 1.1 jtc case TWHILE:
356 1.1 jtc case TUNTIL:
357 1.1 jtc e->type = E_LOOP;
358 1.1 jtc while (1) {
359 1.1 jtc i = ksh_sigsetjmp(e->jbuf, 0);
360 1.1 jtc if (!i)
361 1.1 jtc break;
362 1.1 jtc if ((e->flags&EF_BRKCONT_PASS)
363 1.1 jtc || (i != LBREAK && i != LCONTIN))
364 1.1 jtc {
365 1.1 jtc quitenv();
366 1.1 jtc unwind(i);
367 1.1 jtc } else if (i == LBREAK) {
368 1.1 jtc rv = 0;
369 1.1 jtc goto Break;
370 1.1 jtc }
371 1.1 jtc }
372 1.1 jtc rv = 0; /* in case of a continue */
373 1.1 jtc while ((execute(t->left, XERROK) == 0) == (t->type == TWHILE))
374 1.1 jtc rv = execute(t->right, flags & XERROK);
375 1.6 wiz rv_prop = 1;
376 1.1 jtc break;
377 1.1 jtc
378 1.1 jtc case TIF:
379 1.1 jtc case TELIF:
380 1.1 jtc if (t->right == NULL)
381 1.1 jtc break; /* should be error */
382 1.1 jtc rv = execute(t->left, XERROK) == 0 ?
383 1.1 jtc execute(t->right->left, flags & XERROK) :
384 1.1 jtc execute(t->right->right, flags & XERROK);
385 1.6 wiz rv_prop = 1;
386 1.1 jtc break;
387 1.1 jtc
388 1.1 jtc case TCASE:
389 1.1 jtc cp = evalstr(t->str, DOTILDE);
390 1.1 jtc for (t = t->left; t != NULL && t->type == TPAT; t = t->right)
391 1.1 jtc for (ap = t->vars; *ap; ap++)
392 1.1 jtc if ((s = evalstr(*ap, DOTILDE|DOPAT))
393 1.1 jtc && gmatch(cp, s, FALSE))
394 1.1 jtc goto Found;
395 1.1 jtc break;
396 1.1 jtc Found:
397 1.1 jtc rv = execute(t->left, flags & XERROK);
398 1.6 wiz rv_prop = 1;
399 1.1 jtc break;
400 1.1 jtc
401 1.1 jtc case TBRACE:
402 1.1 jtc rv = execute(t->left, flags & XERROK);
403 1.6 wiz rv_prop = 1;
404 1.1 jtc break;
405 1.1 jtc
406 1.1 jtc case TFUNCT:
407 1.1 jtc rv = define(t->str, t);
408 1.1 jtc break;
409 1.1 jtc
410 1.1 jtc case TTIME:
411 1.5 hubertf /* Clear XEXEC so nested execute() call doesn't exit
412 1.5 hubertf * (allows "ls -l | time grep foo").
413 1.5 hubertf */
414 1.5 hubertf rv = timex(t, flags & ~XEXEC);
415 1.6 wiz rv_prop = 1;
416 1.1 jtc break;
417 1.1 jtc
418 1.1 jtc case TEXEC: /* an eval'd TCOM */
419 1.1 jtc s = t->args[0];
420 1.1 jtc ap = makenv();
421 1.1 jtc #ifndef F_SETFD
422 1.1 jtc for (i = 0; i < sizeof(clexec_tab); i++)
423 1.1 jtc if (clexec_tab[i]) {
424 1.1 jtc close(i);
425 1.1 jtc clexec_tab[i] = 0;
426 1.1 jtc }
427 1.1 jtc #endif
428 1.1 jtc restoresigs();
429 1.2 tls cleanup_proc_env();
430 1.5 hubertf /* XINTACT bit is for OS2 */
431 1.5 hubertf ksh_execve(t->str, t->args, ap, (flags & XINTACT) ? 1 : 0);
432 1.1 jtc if (errno == ENOEXEC)
433 1.1 jtc scriptexec(t, ap);
434 1.1 jtc else
435 1.1 jtc errorf("%s: %s", s, strerror(errno));
436 1.1 jtc }
437 1.1 jtc Break:
438 1.1 jtc exstat = rv;
439 1.1 jtc
440 1.1 jtc quitenv(); /* restores IO */
441 1.4 mycroft if ((flags&XEXEC)) {
442 1.5 hubertf unwind(LEXIT); /* exit child */
443 1.4 mycroft /* NOTREACHED */
444 1.4 mycroft }
445 1.6 wiz if (rv != 0 && !rv_prop && !(flags & XERROK)) {
446 1.1 jtc if (Flag(FERREXIT))
447 1.1 jtc unwind(LERROR);
448 1.1 jtc trapsig(SIGERR_);
449 1.1 jtc }
450 1.1 jtc return rv;
451 1.1 jtc }
452 1.1 jtc
453 1.1 jtc /*
454 1.1 jtc * execute simple command
455 1.1 jtc */
456 1.1 jtc
457 1.1 jtc static int
458 1.1 jtc comexec(t, tp, ap, flags)
459 1.1 jtc struct op *t;
460 1.1 jtc struct tbl *volatile tp;
461 1.1 jtc register char **ap;
462 1.1 jtc int volatile flags;
463 1.1 jtc {
464 1.1 jtc int i;
465 1.1 jtc int rv = 0;
466 1.1 jtc register char *cp;
467 1.1 jtc register char **lastp;
468 1.1 jtc static struct op texec; /* Must be static (XXX but why?) */
469 1.1 jtc int type_flags;
470 1.1 jtc int keepasn_ok;
471 1.1 jtc int fcflags = FC_BI|FC_FUNC|FC_PATH;
472 1.3 christos #ifdef __GNUC__
473 1.3 christos (void) &rv;
474 1.3 christos #endif
475 1.1 jtc
476 1.5 hubertf #ifdef KSH
477 1.1 jtc /* snag the last argument for $_ XXX not the same as at&t ksh,
478 1.1 jtc * which only seems to set $_ after a newline (but not in
479 1.1 jtc * functions/dot scripts, but in interactive and scipt) -
480 1.1 jtc * perhaps save last arg here and set it in shell()?.
481 1.1 jtc */
482 1.5 hubertf if (Flag(FTALKING) && *(lastp = ap)) {
483 1.1 jtc while (*++lastp)
484 1.1 jtc ;
485 1.5 hubertf /* setstr() can't fail here */
486 1.5 hubertf setstr(typeset("_", LOCAL, 0, INTEGER, 0), *--lastp,
487 1.5 hubertf KSH_RETURN_ERROR);
488 1.1 jtc }
489 1.5 hubertf #endif /* KSH */
490 1.1 jtc
491 1.1 jtc /* Deal with the shell builtins builtin, exec and command since
492 1.1 jtc * they can be followed by other commands. This must be done before
493 1.1 jtc * we know if we should create a local block, which must be done
494 1.1 jtc * before we can do a path search (in case the assignments change
495 1.1 jtc * PATH).
496 1.1 jtc * Odd cases:
497 1.1 jtc * FOO=bar exec > /dev/null FOO is kept but not exported
498 1.1 jtc * FOO=bar exec foobar FOO is exported
499 1.1 jtc * FOO=bar command exec > /dev/null FOO is neither kept nor exported
500 1.1 jtc * FOO=bar command FOO is neither kept nor exported
501 1.1 jtc * PATH=... foobar use new PATH in foobar search
502 1.1 jtc */
503 1.1 jtc keepasn_ok = 1;
504 1.1 jtc while (tp && tp->type == CSHELL) {
505 1.1 jtc fcflags = FC_BI|FC_FUNC|FC_PATH;/* undo effects of command */
506 1.1 jtc if (tp->val.f == c_builtin) {
507 1.1 jtc if ((cp = *++ap) == NULL) {
508 1.1 jtc tp = NULL;
509 1.1 jtc break;
510 1.1 jtc }
511 1.1 jtc tp = findcom(cp, FC_BI);
512 1.1 jtc if (tp == NULL)
513 1.1 jtc errorf("builtin: %s: not a builtin", cp);
514 1.1 jtc continue;
515 1.1 jtc } else if (tp->val.f == c_exec) {
516 1.1 jtc if (ap[1] == NULL)
517 1.1 jtc break;
518 1.1 jtc ap++;
519 1.1 jtc flags |= XEXEC;
520 1.1 jtc } else if (tp->val.f == c_command) {
521 1.1 jtc int optc, saw_p = 0;
522 1.1 jtc
523 1.1 jtc /* Ugly dealing with options in two places (here and
524 1.1 jtc * in c_command(), but such is life)
525 1.1 jtc */
526 1.1 jtc ksh_getopt_reset(&builtin_opt, 0);
527 1.1 jtc while ((optc = ksh_getopt(ap, &builtin_opt, ":p"))
528 1.1 jtc == 'p')
529 1.1 jtc saw_p = 1;
530 1.1 jtc if (optc != EOF)
531 1.1 jtc break; /* command -vV or something */
532 1.1 jtc /* don't look for functions */
533 1.1 jtc fcflags = FC_BI|FC_PATH;
534 1.1 jtc if (saw_p) {
535 1.1 jtc if (Flag(FRESTRICTED)) {
536 1.1 jtc warningf(TRUE,
537 1.1 jtc "command -p: restricted");
538 1.1 jtc rv = 1;
539 1.1 jtc goto Leave;
540 1.1 jtc }
541 1.1 jtc fcflags |= FC_DEFPATH;
542 1.1 jtc }
543 1.1 jtc ap += builtin_opt.optind;
544 1.2 tls /* POSIX says special builtins lose their status
545 1.1 jtc * if accessed using command.
546 1.1 jtc */
547 1.1 jtc keepasn_ok = 0;
548 1.1 jtc if (!ap[0]) {
549 1.1 jtc /* ensure command with no args exits with 0 */
550 1.1 jtc subst_exstat = 0;
551 1.1 jtc break;
552 1.1 jtc }
553 1.1 jtc } else
554 1.1 jtc break;
555 1.1 jtc tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC));
556 1.1 jtc }
557 1.1 jtc if (keepasn_ok && (!ap[0] || (tp && (tp->flag & KEEPASN))))
558 1.1 jtc type_flags = 0;
559 1.1 jtc else {
560 1.1 jtc /* create new variable/function block */
561 1.1 jtc newblock();
562 1.1 jtc /* ksh functions don't keep assignments, POSIX functions do. */
563 1.1 jtc if (keepasn_ok && tp && tp->type == CFUNC
564 1.1 jtc && !(tp->flag & FKSH))
565 1.1 jtc type_flags = 0;
566 1.1 jtc else
567 1.1 jtc type_flags = LOCAL|LOCAL_COPY|EXPORT;
568 1.1 jtc }
569 1.1 jtc if (Flag(FEXPORT))
570 1.1 jtc type_flags |= EXPORT;
571 1.1 jtc for (i = 0; t->vars[i]; i++) {
572 1.1 jtc cp = evalstr(t->vars[i], DOASNTILDE);
573 1.1 jtc if (Flag(FXTRACE)) {
574 1.1 jtc if (i == 0)
575 1.1 jtc shf_fprintf(shl_out, "%s",
576 1.1 jtc PS4_SUBSTITUTE(str_val(global("PS4"))));
577 1.1 jtc shf_fprintf(shl_out, "%s%s", cp,
578 1.1 jtc t->vars[i + 1] ? space : newline);
579 1.1 jtc if (!t->vars[i + 1])
580 1.1 jtc shf_flush(shl_out);
581 1.1 jtc }
582 1.1 jtc typeset(cp, type_flags, 0, 0, 0);
583 1.1 jtc }
584 1.1 jtc
585 1.1 jtc if ((cp = *ap) == NULL) {
586 1.1 jtc rv = subst_exstat;
587 1.1 jtc goto Leave;
588 1.1 jtc } else if (!tp) {
589 1.1 jtc if (Flag(FRESTRICTED) && ksh_strchr_dirsep(cp)) {
590 1.1 jtc warningf(TRUE, "%s: restricted", cp);
591 1.1 jtc rv = 1;
592 1.1 jtc goto Leave;
593 1.1 jtc }
594 1.1 jtc tp = findcom(cp, fcflags);
595 1.1 jtc }
596 1.1 jtc
597 1.1 jtc switch (tp->type) {
598 1.1 jtc case CSHELL: /* shell built-in */
599 1.1 jtc rv = call_builtin(tp, ap);
600 1.1 jtc break;
601 1.1 jtc
602 1.1 jtc case CFUNC: /* function call */
603 1.1 jtc {
604 1.1 jtc volatile int old_xflag;
605 1.1 jtc volatile Tflag old_inuse;
606 1.1 jtc const char *volatile old_kshname;
607 1.1 jtc
608 1.1 jtc if (!(tp->flag & ISSET)) {
609 1.1 jtc struct tbl *ftp;
610 1.1 jtc
611 1.1 jtc if (!tp->u.fpath) {
612 1.1 jtc if (tp->u2.errno_) {
613 1.1 jtc warningf(TRUE,
614 1.1 jtc "%s: can't find function definition file - %s",
615 1.1 jtc cp, strerror(tp->u2.errno_));
616 1.1 jtc rv = 126;
617 1.1 jtc } else {
618 1.1 jtc warningf(TRUE,
619 1.1 jtc "%s: can't find function definition file", cp);
620 1.1 jtc rv = 127;
621 1.1 jtc }
622 1.1 jtc break;
623 1.1 jtc }
624 1.1 jtc if (include(tp->u.fpath, 0, (char **) 0, 0) < 0) {
625 1.1 jtc warningf(TRUE,
626 1.1 jtc "%s: can't open function definition file %s - %s",
627 1.1 jtc cp, tp->u.fpath, strerror(errno));
628 1.1 jtc rv = 127;
629 1.1 jtc break;
630 1.1 jtc }
631 1.1 jtc if (!(ftp = findfunc(cp, hash(cp), FALSE))
632 1.1 jtc || !(ftp->flag & ISSET))
633 1.1 jtc {
634 1.1 jtc warningf(TRUE,
635 1.1 jtc "%s: function not defined by %s",
636 1.1 jtc cp, tp->u.fpath);
637 1.1 jtc rv = 127;
638 1.1 jtc break;
639 1.1 jtc }
640 1.1 jtc tp = ftp;
641 1.1 jtc }
642 1.1 jtc
643 1.1 jtc /* ksh functions set $0 to function name, POSIX functions leave
644 1.1 jtc * $0 unchanged.
645 1.1 jtc */
646 1.1 jtc old_kshname = kshname;
647 1.1 jtc if (tp->flag & FKSH)
648 1.1 jtc kshname = ap[0];
649 1.5 hubertf else
650 1.5 hubertf ap[0] = (char *) kshname;
651 1.1 jtc e->loc->argv = ap;
652 1.1 jtc for (i = 0; *ap++ != NULL; i++)
653 1.1 jtc ;
654 1.1 jtc e->loc->argc = i - 1;
655 1.5 hubertf /* ksh-style functions handle getopts sanely,
656 1.5 hubertf * bourne/posix functions are insane...
657 1.5 hubertf */
658 1.5 hubertf if (tp->flag & FKSH) {
659 1.5 hubertf e->loc->flags |= BF_DOGETOPTS;
660 1.5 hubertf e->loc->getopts_state = user_opt;
661 1.5 hubertf getopts_reset(1);
662 1.5 hubertf }
663 1.1 jtc
664 1.1 jtc old_xflag = Flag(FXTRACE);
665 1.1 jtc Flag(FXTRACE) = tp->flag & TRACE ? TRUE : FALSE;
666 1.1 jtc
667 1.1 jtc old_inuse = tp->flag & FINUSE;
668 1.1 jtc tp->flag |= FINUSE;
669 1.1 jtc
670 1.1 jtc e->type = E_FUNC;
671 1.1 jtc i = ksh_sigsetjmp(e->jbuf, 0);
672 1.1 jtc if (i == 0) {
673 1.1 jtc /* seems odd to pass XERROK here, but at&t ksh does */
674 1.1 jtc exstat = execute(tp->val.t, flags & XERROK);
675 1.1 jtc i = LRETURN;
676 1.1 jtc }
677 1.1 jtc kshname = old_kshname;
678 1.1 jtc Flag(FXTRACE) = old_xflag;
679 1.1 jtc tp->flag = (tp->flag & ~FINUSE) | old_inuse;
680 1.1 jtc /* Were we deleted while executing? If so, free the execution
681 1.1 jtc * tree. todo: Unfortunately, the table entry is never re-used
682 1.1 jtc * until the lookup table is expanded.
683 1.1 jtc */
684 1.1 jtc if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) {
685 1.1 jtc if (tp->flag & ALLOC) {
686 1.1 jtc tp->flag &= ~ALLOC;
687 1.1 jtc tfree(tp->val.t, tp->areap);
688 1.1 jtc }
689 1.1 jtc tp->flag = 0;
690 1.1 jtc }
691 1.1 jtc switch (i) {
692 1.1 jtc case LRETURN:
693 1.1 jtc case LERROR:
694 1.1 jtc rv = exstat;
695 1.1 jtc break;
696 1.1 jtc case LINTR:
697 1.1 jtc case LEXIT:
698 1.1 jtc case LLEAVE:
699 1.1 jtc case LSHELL:
700 1.1 jtc quitenv();
701 1.1 jtc unwind(i);
702 1.1 jtc /*NOTREACHED*/
703 1.1 jtc default:
704 1.1 jtc quitenv();
705 1.1 jtc internal_errorf(1, "CFUNC %d", i);
706 1.1 jtc }
707 1.1 jtc break;
708 1.1 jtc }
709 1.1 jtc
710 1.1 jtc case CEXEC: /* executable command */
711 1.1 jtc case CTALIAS: /* tracked alias */
712 1.1 jtc if (!(tp->flag&ISSET)) {
713 1.1 jtc /* errno_ will be set if the named command was found
714 1.1 jtc * but could not be executed (permissions, no execute
715 1.1 jtc * bit, directory, etc). Print out a (hopefully)
716 1.1 jtc * useful error message and set the exit status to 126.
717 1.1 jtc */
718 1.1 jtc if (tp->u2.errno_) {
719 1.1 jtc warningf(TRUE, "%s: cannot execute - %s", cp,
720 1.1 jtc strerror(tp->u2.errno_));
721 1.1 jtc rv = 126; /* POSIX */
722 1.1 jtc } else {
723 1.1 jtc warningf(TRUE, "%s: not found", cp);
724 1.1 jtc rv = 127;
725 1.1 jtc }
726 1.1 jtc break;
727 1.1 jtc }
728 1.1 jtc
729 1.5 hubertf #ifdef KSH
730 1.1 jtc /* set $_ to program's full path */
731 1.5 hubertf /* setstr() can't fail here */
732 1.5 hubertf setstr(typeset("_", LOCAL|EXPORT, 0, INTEGER, 0), tp->val.s,
733 1.5 hubertf KSH_RETURN_ERROR);
734 1.5 hubertf #endif /* KSH */
735 1.1 jtc
736 1.1 jtc if (flags&XEXEC) {
737 1.1 jtc j_exit();
738 1.1 jtc if (!(flags&XBGND) || Flag(FMONITOR)) {
739 1.1 jtc setexecsig(&sigtraps[SIGINT], SS_RESTORE_ORIG);
740 1.1 jtc setexecsig(&sigtraps[SIGQUIT], SS_RESTORE_ORIG);
741 1.1 jtc }
742 1.1 jtc }
743 1.1 jtc
744 1.1 jtc /* to fork we set up a TEXEC node and call execute */
745 1.1 jtc texec.type = TEXEC;
746 1.1 jtc texec.left = t; /* for tprint */
747 1.1 jtc texec.str = tp->val.s;
748 1.1 jtc texec.args = ap;
749 1.1 jtc rv = exchild(&texec, flags, -1);
750 1.1 jtc break;
751 1.1 jtc }
752 1.1 jtc Leave:
753 1.1 jtc if (flags & XEXEC) {
754 1.1 jtc exstat = rv;
755 1.1 jtc unwind(LLEAVE);
756 1.1 jtc }
757 1.1 jtc return rv;
758 1.1 jtc }
759 1.1 jtc
760 1.1 jtc static void
761 1.1 jtc scriptexec(tp, ap)
762 1.1 jtc register struct op *tp;
763 1.1 jtc register char **ap;
764 1.1 jtc {
765 1.1 jtc char *shell;
766 1.1 jtc
767 1.1 jtc shell = str_val(global(EXECSHELL_STR));
768 1.1 jtc if (shell && *shell)
769 1.1 jtc shell = search(shell, path, X_OK, (int *) 0);
770 1.1 jtc if (!shell || !*shell)
771 1.1 jtc shell = EXECSHELL;
772 1.1 jtc
773 1.1 jtc *tp->args-- = tp->str;
774 1.1 jtc #ifdef SHARPBANG
775 1.1 jtc {
776 1.1 jtc char buf[LINE];
777 1.1 jtc register char *cp;
778 1.1 jtc register int fd, n;
779 1.1 jtc
780 1.1 jtc buf[0] = '\0';
781 1.1 jtc if ((fd = open(tp->str, O_RDONLY)) >= 0) {
782 1.1 jtc if ((n = read(fd, buf, LINE - 1)) > 0)
783 1.1 jtc buf[n] = '\0';
784 1.1 jtc (void) close(fd);
785 1.1 jtc }
786 1.1 jtc if ((buf[0] == '#' && buf[1] == '!' && (cp = &buf[2]))
787 1.1 jtc # ifdef OS2
788 1.1 jtc || (strncmp(buf, "extproc", 7) == 0 && isspace(buf[7])
789 1.1 jtc && (cp = &buf[7]))
790 1.1 jtc # endif /* OS2 */
791 1.1 jtc )
792 1.1 jtc {
793 1.1 jtc while (*cp && (*cp == ' ' || *cp == '\t'))
794 1.1 jtc cp++;
795 1.1 jtc if (*cp && *cp != '\n') {
796 1.1 jtc char *a0 = cp, *a1 = (char *) 0;
797 1.1 jtc # ifdef OS2
798 1.1 jtc char *a2 = cp;
799 1.1 jtc # endif /* OS2 */
800 1.1 jtc
801 1.1 jtc while (*cp && *cp != '\n' && *cp != ' '
802 1.1 jtc && *cp != '\t')
803 1.1 jtc {
804 1.1 jtc # ifdef OS2
805 1.1 jtc /* Allow shell search without prepended path
806 1.1 jtc * if shell with / in pathname cannot be found.
807 1.1 jtc * Use / explicitly so \ can be used if explicit
808 1.1 jtc * needs to be forced.
809 1.1 jtc */
810 1.1 jtc if (*cp == '/')
811 1.1 jtc a2 = cp + 1;
812 1.1 jtc # endif /* OS2 */
813 1.1 jtc cp++;
814 1.1 jtc }
815 1.1 jtc if (*cp && *cp != '\n') {
816 1.1 jtc *cp++ = '\0';
817 1.1 jtc while (*cp
818 1.1 jtc && (*cp == ' ' || *cp == '\t'))
819 1.1 jtc cp++;
820 1.1 jtc if (*cp && *cp != '\n') {
821 1.1 jtc a1 = cp;
822 1.1 jtc /* all one argument */
823 1.1 jtc while (*cp && *cp != '\n')
824 1.1 jtc cp++;
825 1.1 jtc }
826 1.1 jtc }
827 1.1 jtc if (*cp == '\n') {
828 1.1 jtc *cp = '\0';
829 1.1 jtc if (a1)
830 1.1 jtc *tp->args-- = a1;
831 1.1 jtc # ifdef OS2
832 1.5 hubertf if (a0 != a2) {
833 1.5 hubertf char *tmp_a0 = str_nsave(a0,
834 1.5 hubertf strlen(a0) + 5, ATEMP);
835 1.5 hubertf if (search_access(tmp_a0, X_OK,
836 1.5 hubertf (int *) 0))
837 1.5 hubertf a0 = a2;
838 1.5 hubertf afree(tmp_a0, ATEMP);
839 1.5 hubertf }
840 1.1 jtc # endif /* OS2 */
841 1.1 jtc shell = a0;
842 1.1 jtc }
843 1.1 jtc }
844 1.1 jtc # ifdef OS2
845 1.1 jtc } else {
846 1.1 jtc /* Use ksh documented shell default if present
847 1.1 jtc * else use OS2_SHELL which is assumed to need
848 1.1 jtc * the /c option and '\' as dir separater.
849 1.1 jtc */
850 1.1 jtc char *p = shell;
851 1.1 jtc
852 1.1 jtc shell = str_val(global("EXECSHELL"));
853 1.1 jtc if (shell && *shell)
854 1.1 jtc shell = search(shell, path, X_OK, (int *) 0);
855 1.1 jtc if (!shell || !*shell) {
856 1.1 jtc shell = p;
857 1.1 jtc *tp->args-- = "/c";
858 1.1 jtc for (p = tp->str; *p; p++)
859 1.1 jtc if (*p == '/')
860 1.1 jtc *p = '\\';
861 1.1 jtc }
862 1.1 jtc # endif /* OS2 */
863 1.1 jtc }
864 1.1 jtc }
865 1.1 jtc #endif /* SHARPBANG */
866 1.1 jtc *tp->args = shell;
867 1.1 jtc
868 1.5 hubertf ksh_execve(tp->args[0], tp->args, ap, 0);
869 1.1 jtc
870 1.1 jtc /* report both the program that was run and the bogus shell */
871 1.1 jtc errorf("%s: %s: %s", tp->str, shell, strerror(errno));
872 1.1 jtc }
873 1.1 jtc
874 1.1 jtc int
875 1.1 jtc shcomexec(wp)
876 1.1 jtc register char **wp;
877 1.1 jtc {
878 1.1 jtc register struct tbl *tp;
879 1.1 jtc
880 1.1 jtc tp = tsearch(&builtins, *wp, hash(*wp));
881 1.1 jtc if (tp == NULL)
882 1.1 jtc internal_errorf(1, "shcomexec: %s", *wp);
883 1.1 jtc return call_builtin(tp, wp);
884 1.1 jtc }
885 1.1 jtc
886 1.1 jtc /*
887 1.1 jtc * Search function tables for a function. If create set, a table entry
888 1.1 jtc * is created if none is found.
889 1.1 jtc */
890 1.1 jtc struct tbl *
891 1.1 jtc findfunc(name, h, create)
892 1.1 jtc const char *name;
893 1.5 hubertf unsigned int h;
894 1.5 hubertf int create;
895 1.1 jtc {
896 1.1 jtc struct block *l;
897 1.1 jtc struct tbl *tp = (struct tbl *) 0;
898 1.1 jtc
899 1.1 jtc for (l = e->loc; l; l = l->next) {
900 1.1 jtc tp = tsearch(&l->funs, name, h);
901 1.1 jtc if (tp)
902 1.1 jtc break;
903 1.1 jtc if (!l->next && create) {
904 1.1 jtc tp = tenter(&l->funs, name, h);
905 1.1 jtc tp->flag = DEFINED;
906 1.1 jtc tp->type = CFUNC;
907 1.1 jtc tp->val.t = (struct op *) 0;
908 1.1 jtc break;
909 1.1 jtc }
910 1.1 jtc }
911 1.1 jtc return tp;
912 1.1 jtc }
913 1.1 jtc
914 1.1 jtc /*
915 1.1 jtc * define function. Returns 1 if function is being undefined (t == 0) and
916 1.1 jtc * function did not exist, returns 0 otherwise.
917 1.1 jtc */
918 1.1 jtc int
919 1.1 jtc define(name, t)
920 1.1 jtc const char *name;
921 1.1 jtc struct op *t;
922 1.1 jtc {
923 1.1 jtc struct tbl *tp;
924 1.1 jtc int was_set = 0;
925 1.1 jtc
926 1.1 jtc while (1) {
927 1.1 jtc tp = findfunc(name, hash(name), TRUE);
928 1.1 jtc
929 1.1 jtc if (tp->flag & ISSET)
930 1.1 jtc was_set = 1;
931 1.1 jtc /* If this function is currently being executed, we zap this
932 1.1 jtc * table entry so findfunc() won't see it
933 1.1 jtc */
934 1.1 jtc if (tp->flag & FINUSE) {
935 1.1 jtc tp->name[0] = '\0';
936 1.1 jtc tp->flag &= ~DEFINED; /* ensure it won't be found */
937 1.1 jtc tp->flag |= FDELETE;
938 1.1 jtc } else
939 1.1 jtc break;
940 1.1 jtc }
941 1.1 jtc
942 1.1 jtc if (tp->flag & ALLOC) {
943 1.1 jtc tp->flag &= ~(ISSET|ALLOC);
944 1.1 jtc tfree(tp->val.t, tp->areap);
945 1.1 jtc }
946 1.1 jtc
947 1.1 jtc if (t == NULL) { /* undefine */
948 1.1 jtc tdelete(tp);
949 1.1 jtc return was_set ? 0 : 1;
950 1.1 jtc }
951 1.1 jtc
952 1.1 jtc tp->val.t = tcopy(t->left, tp->areap);
953 1.1 jtc tp->flag |= (ISSET|ALLOC);
954 1.1 jtc if (t->u.ksh_func)
955 1.5 hubertf tp->flag |= FKSH;
956 1.1 jtc
957 1.1 jtc return 0;
958 1.1 jtc }
959 1.1 jtc
960 1.1 jtc /*
961 1.1 jtc * add builtin
962 1.1 jtc */
963 1.1 jtc void
964 1.1 jtc builtin(name, func)
965 1.1 jtc const char *name;
966 1.1 jtc int (*func) ARGS((char **));
967 1.1 jtc {
968 1.1 jtc register struct tbl *tp;
969 1.1 jtc Tflag flag;
970 1.1 jtc
971 1.1 jtc /* see if any flags should be set for this builtin */
972 1.1 jtc for (flag = 0; ; name++) {
973 1.1 jtc if (*name == '=') /* command does variable assignment */
974 1.1 jtc flag |= KEEPASN;
975 1.1 jtc else if (*name == '*') /* POSIX special builtin */
976 1.1 jtc flag |= SPEC_BI;
977 1.1 jtc else if (*name == '+') /* POSIX regular builtin */
978 1.1 jtc flag |= REG_BI;
979 1.1 jtc else
980 1.1 jtc break;
981 1.1 jtc }
982 1.1 jtc
983 1.1 jtc tp = tenter(&builtins, name, hash(name));
984 1.1 jtc tp->flag = DEFINED | flag;
985 1.1 jtc tp->type = CSHELL;
986 1.1 jtc tp->val.f = func;
987 1.1 jtc }
988 1.1 jtc
989 1.1 jtc /*
990 1.1 jtc * find command
991 1.1 jtc * either function, hashed command, or built-in (in that order)
992 1.1 jtc */
993 1.1 jtc struct tbl *
994 1.1 jtc findcom(name, flags)
995 1.1 jtc const char *name;
996 1.1 jtc int flags; /* FC_* */
997 1.1 jtc {
998 1.1 jtc static struct tbl temp;
999 1.1 jtc unsigned int h = hash(name);
1000 1.1 jtc struct tbl *tp = NULL, *tbi;
1001 1.1 jtc int insert = Flag(FTRACKALL); /* insert if not found */
1002 1.1 jtc char *fpath; /* for function autoloading */
1003 1.1 jtc char *npath;
1004 1.1 jtc
1005 1.1 jtc if (ksh_strchr_dirsep(name) != NULL) {
1006 1.1 jtc insert = 0;
1007 1.1 jtc /* prevent FPATH search below */
1008 1.1 jtc flags &= ~FC_FUNC;
1009 1.1 jtc goto Search;
1010 1.1 jtc }
1011 1.1 jtc tbi = (flags & FC_BI) ? tsearch(&builtins, name, h) : NULL;
1012 1.1 jtc /* POSIX says special builtins first, then functions, then
1013 1.1 jtc * POSIX regular builtins, then search path...
1014 1.1 jtc */
1015 1.1 jtc if ((flags & FC_SPECBI) && tbi && (tbi->flag & SPEC_BI))
1016 1.1 jtc tp = tbi;
1017 1.1 jtc if (!tp && (flags & FC_FUNC)) {
1018 1.1 jtc tp = findfunc(name, h, FALSE);
1019 1.1 jtc if (tp && !(tp->flag & ISSET)) {
1020 1.1 jtc if ((fpath = str_val(global("FPATH"))) == null) {
1021 1.1 jtc tp->u.fpath = (char *) 0;
1022 1.1 jtc tp->u2.errno_ = 0;
1023 1.1 jtc } else
1024 1.1 jtc tp->u.fpath = search(name, fpath, R_OK,
1025 1.1 jtc &tp->u2.errno_);
1026 1.1 jtc }
1027 1.1 jtc }
1028 1.1 jtc if (!tp && (flags & FC_REGBI) && tbi && (tbi->flag & REG_BI))
1029 1.1 jtc tp = tbi;
1030 1.1 jtc /* todo: posix says non-special/non-regular builtins must
1031 1.1 jtc * be triggered by some user-controllable means like a
1032 1.1 jtc * special directory in PATH. Requires modifications to
1033 1.1 jtc * the search() function. Tracked aliases should be
1034 1.1 jtc * modified to allow tracking of builtin commands.
1035 1.1 jtc * This should be under control of the FPOSIX flag.
1036 1.1 jtc * If this is changed, also change c_whence...
1037 1.1 jtc */
1038 1.1 jtc if (!tp && (flags & FC_UNREGBI) && tbi)
1039 1.1 jtc tp = tbi;
1040 1.1 jtc if (!tp && (flags & FC_PATH) && !(flags & FC_DEFPATH)) {
1041 1.1 jtc tp = tsearch(&taliases, name, h);
1042 1.1 jtc if (tp && (tp->flag & ISSET) && eaccess(tp->val.s, X_OK) != 0) {
1043 1.1 jtc if (tp->flag & ALLOC) {
1044 1.1 jtc tp->flag &= ~ALLOC;
1045 1.1 jtc afree(tp->val.s, APERM);
1046 1.1 jtc }
1047 1.1 jtc tp->flag &= ~ISSET;
1048 1.1 jtc }
1049 1.1 jtc }
1050 1.1 jtc
1051 1.1 jtc Search:
1052 1.1 jtc if ((!tp || (tp->type == CTALIAS && !(tp->flag&ISSET)))
1053 1.1 jtc && (flags & FC_PATH))
1054 1.1 jtc {
1055 1.1 jtc if (!tp) {
1056 1.1 jtc if (insert && !(flags & FC_DEFPATH)) {
1057 1.1 jtc tp = tenter(&taliases, name, h);
1058 1.1 jtc tp->type = CTALIAS;
1059 1.1 jtc } else {
1060 1.1 jtc tp = &temp;
1061 1.1 jtc tp->type = CEXEC;
1062 1.1 jtc }
1063 1.1 jtc tp->flag = DEFINED; /* make ~ISSET */
1064 1.1 jtc }
1065 1.1 jtc npath = search(name, flags & FC_DEFPATH ? def_path : path,
1066 1.1 jtc X_OK, &tp->u2.errno_);
1067 1.1 jtc if (npath) {
1068 1.1 jtc tp->val.s = tp == &temp ? npath : str_save(npath, APERM);
1069 1.1 jtc tp->flag |= ISSET|ALLOC;
1070 1.1 jtc } else if ((flags & FC_FUNC)
1071 1.1 jtc && (fpath = str_val(global("FPATH"))) != null
1072 1.1 jtc && (npath = search(name, fpath, R_OK,
1073 1.1 jtc &tp->u2.errno_)) != (char *) 0)
1074 1.1 jtc {
1075 1.1 jtc /* An undocumented feature of at&t ksh is that it
1076 1.1 jtc * searches FPATH if a command is not found, even
1077 1.1 jtc * if the command hasn't been set up as an autoloaded
1078 1.1 jtc * function (ie, no typeset -uf).
1079 1.1 jtc */
1080 1.1 jtc tp = &temp;
1081 1.1 jtc tp->type = CFUNC;
1082 1.1 jtc tp->flag = DEFINED; /* make ~ISSET */
1083 1.1 jtc tp->u.fpath = npath;
1084 1.1 jtc }
1085 1.1 jtc }
1086 1.1 jtc return tp;
1087 1.1 jtc }
1088 1.1 jtc
1089 1.1 jtc /*
1090 1.1 jtc * flush executable commands with relative paths
1091 1.1 jtc */
1092 1.1 jtc void
1093 1.1 jtc flushcom(all)
1094 1.1 jtc int all; /* just relative or all */
1095 1.1 jtc {
1096 1.1 jtc struct tbl *tp;
1097 1.1 jtc struct tstate ts;
1098 1.1 jtc
1099 1.1 jtc for (twalk(&ts, &taliases); (tp = tnext(&ts)) != NULL; )
1100 1.1 jtc if ((tp->flag&ISSET) && (all || !ISDIRSEP(tp->val.s[0]))) {
1101 1.1 jtc if (tp->flag&ALLOC) {
1102 1.1 jtc tp->flag &= ~(ALLOC|ISSET);
1103 1.1 jtc afree(tp->val.s, APERM);
1104 1.1 jtc }
1105 1.5 hubertf tp->flag &= ~ISSET;
1106 1.1 jtc }
1107 1.1 jtc }
1108 1.1 jtc
1109 1.1 jtc /* Check if path is something we want to find. Returns -1 for failure. */
1110 1.1 jtc int
1111 1.1 jtc search_access(path, mode, errnop)
1112 1.1 jtc const char *path;
1113 1.1 jtc int mode;
1114 1.1 jtc int *errnop; /* set if candidate found, but not suitable */
1115 1.1 jtc {
1116 1.1 jtc #ifndef OS2
1117 1.1 jtc int ret, err = 0;
1118 1.1 jtc struct stat statb;
1119 1.1 jtc
1120 1.1 jtc if (stat(path, &statb) < 0)
1121 1.1 jtc return -1;
1122 1.1 jtc ret = eaccess(path, mode);
1123 1.1 jtc if (ret < 0)
1124 1.1 jtc err = errno; /* File exists, but we can't access it */
1125 1.5 hubertf else if (mode == X_OK
1126 1.5 hubertf && (!S_ISREG(statb.st_mode)
1127 1.5 hubertf /* This 'cause access() says root can execute everything */
1128 1.5 hubertf || !(statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))))
1129 1.1 jtc {
1130 1.1 jtc ret = -1;
1131 1.1 jtc err = S_ISDIR(statb.st_mode) ? EISDIR : EACCES;
1132 1.1 jtc }
1133 1.5 hubertf if (err && errnop && !*errnop)
1134 1.1 jtc *errnop = err;
1135 1.1 jtc return ret;
1136 1.1 jtc #else /* !OS2 */
1137 1.1 jtc /*
1138 1.1 jtc * NOTE: ASSUMES path can be modified and has enough room at the
1139 1.1 jtc * end of the string for a suffix (ie, 4 extra characters).
1140 1.1 jtc * Certain code knows this (eg, eval.c(globit()),
1141 1.1 jtc * exec.c(search())).
1142 1.1 jtc */
1143 1.1 jtc static char *xsuffixes[] = { ".ksh", ".exe", ".", ".sh", ".cmd",
1144 1.1 jtc ".com", ".bat", (char *) 0
1145 1.1 jtc };
1146 1.1 jtc static char *rsuffixes[] = { ".ksh", ".", ".sh", ".cmd", ".bat",
1147 1.1 jtc (char *) 0
1148 1.1 jtc };
1149 1.1 jtc int i;
1150 1.1 jtc char *mpath = (char *) path;
1151 1.1 jtc char *tp = mpath + strlen(mpath);
1152 1.1 jtc char *p;
1153 1.1 jtc char **sfx;
1154 1.1 jtc
1155 1.1 jtc /* If a suffix has been specified, check if it is one of the
1156 1.1 jtc * suffixes that indicate the file is executable - if so, change
1157 1.1 jtc * the access test to R_OK...
1158 1.1 jtc * This code assumes OS/2 files can have only one suffix...
1159 1.1 jtc */
1160 1.1 jtc if ((p = strrchr((p = ksh_strrchr_dirsep(mpath)) ? p : mpath, '.'))) {
1161 1.1 jtc if (mode == X_OK)
1162 1.1 jtc mode = R_OK;
1163 1.1 jtc return search_access1(mpath, mode, errnop);
1164 1.1 jtc }
1165 1.1 jtc /* Try appending the various suffixes. Different suffixes for
1166 1.1 jtc * read and execute 'cause we don't want to read an executable...
1167 1.1 jtc */
1168 1.1 jtc sfx = mode == R_OK ? rsuffixes : xsuffixes;
1169 1.1 jtc for (i = 0; sfx[i]; i++) {
1170 1.1 jtc strcpy(tp, p = sfx[i]);
1171 1.1 jtc if (search_access1(mpath, R_OK, errnop) == 0)
1172 1.1 jtc return 0;
1173 1.1 jtc *tp = '\0';
1174 1.1 jtc }
1175 1.1 jtc return -1;
1176 1.1 jtc #endif /* !OS2 */
1177 1.1 jtc }
1178 1.1 jtc
1179 1.1 jtc #ifdef OS2
1180 1.1 jtc static int
1181 1.1 jtc search_access1(path, mode, errnop)
1182 1.1 jtc const char *path;
1183 1.1 jtc int mode;
1184 1.1 jtc int *errnop; /* set if candidate found, but not suitable */
1185 1.1 jtc {
1186 1.1 jtc int ret, err = 0;
1187 1.1 jtc struct stat statb;
1188 1.1 jtc
1189 1.1 jtc if (stat(path, &statb) < 0)
1190 1.1 jtc return -1;
1191 1.1 jtc ret = eaccess(path, mode);
1192 1.1 jtc if (ret < 0)
1193 1.1 jtc err = errno; /* File exists, but we can't access it */
1194 1.1 jtc else if (!S_ISREG(statb.st_mode)) {
1195 1.1 jtc ret = -1;
1196 1.1 jtc err = S_ISDIR(statb.st_mode) ? EISDIR : EACCES;
1197 1.1 jtc }
1198 1.5 hubertf if (err && errnop && !*errnop)
1199 1.1 jtc *errnop = err;
1200 1.1 jtc return ret;
1201 1.1 jtc }
1202 1.1 jtc #endif /* OS2 */
1203 1.1 jtc
1204 1.1 jtc /*
1205 1.1 jtc * search for command with PATH
1206 1.1 jtc */
1207 1.1 jtc char *
1208 1.1 jtc search(name, path, mode, errnop)
1209 1.1 jtc const char *name;
1210 1.1 jtc const char *path;
1211 1.1 jtc int mode; /* R_OK or X_OK */
1212 1.1 jtc int *errnop; /* set if candidate found, but not suitable */
1213 1.1 jtc {
1214 1.1 jtc const char *sp, *p;
1215 1.1 jtc char *xp;
1216 1.1 jtc XString xs;
1217 1.1 jtc int namelen;
1218 1.1 jtc
1219 1.1 jtc if (errnop)
1220 1.1 jtc *errnop = 0;
1221 1.1 jtc #ifdef OS2
1222 1.1 jtc /* Xinit() allocates 8 additional bytes, so appended suffixes won't
1223 1.1 jtc * overflow the memory.
1224 1.1 jtc */
1225 1.1 jtc namelen = strlen(name) + 1;
1226 1.1 jtc Xinit(xs, xp, namelen, ATEMP);
1227 1.1 jtc memcpy(Xstring(xs, xp), name, namelen);
1228 1.1 jtc
1229 1.1 jtc if (ksh_strchr_dirsep(name)) {
1230 1.1 jtc if (search_access(Xstring(xs, xp), mode, errnop) >= 0)
1231 1.1 jtc return Xstring(xs, xp); /* not Xclose() - see above */
1232 1.1 jtc Xfree(xs, xp);
1233 1.1 jtc return NULL;
1234 1.1 jtc }
1235 1.1 jtc
1236 1.1 jtc /* Look in current context always. (os2 style) */
1237 1.1 jtc if (search_access(Xstring(xs, xp), mode, errnop) == 0)
1238 1.1 jtc return Xstring(xs, xp); /* not Xclose() - xp may be wrong */
1239 1.1 jtc #else /* OS2 */
1240 1.1 jtc if (ksh_strchr_dirsep(name)) {
1241 1.1 jtc if (search_access(name, mode, errnop) == 0)
1242 1.1 jtc return (char *) name;
1243 1.1 jtc return NULL;
1244 1.1 jtc }
1245 1.1 jtc
1246 1.1 jtc namelen = strlen(name) + 1;
1247 1.1 jtc Xinit(xs, xp, 128, ATEMP);
1248 1.1 jtc #endif /* OS2 */
1249 1.1 jtc
1250 1.1 jtc sp = path;
1251 1.1 jtc while (sp != NULL) {
1252 1.1 jtc xp = Xstring(xs, xp);
1253 1.1 jtc if (!(p = strchr(sp, PATHSEP)))
1254 1.1 jtc p = sp + strlen(sp);
1255 1.1 jtc if (p != sp) {
1256 1.1 jtc XcheckN(xs, xp, p - sp);
1257 1.1 jtc memcpy(xp, sp, p - sp);
1258 1.1 jtc xp += p - sp;
1259 1.1 jtc *xp++ = DIRSEP;
1260 1.1 jtc }
1261 1.1 jtc sp = p;
1262 1.1 jtc XcheckN(xs, xp, namelen);
1263 1.1 jtc memcpy(xp, name, namelen);
1264 1.1 jtc if (search_access(Xstring(xs, xp), mode, errnop) == 0)
1265 1.1 jtc #ifdef OS2
1266 1.1 jtc return Xstring(xs, xp); /* Not Xclose() - see above */
1267 1.1 jtc #else /* OS2 */
1268 1.1 jtc return Xclose(xs, xp + namelen);
1269 1.1 jtc #endif /* OS2 */
1270 1.1 jtc if (*sp++ == '\0')
1271 1.1 jtc sp = NULL;
1272 1.1 jtc }
1273 1.1 jtc Xfree(xs, xp);
1274 1.1 jtc return NULL;
1275 1.1 jtc }
1276 1.1 jtc
1277 1.1 jtc static int
1278 1.1 jtc call_builtin(tp, wp)
1279 1.1 jtc struct tbl *tp;
1280 1.1 jtc char **wp;
1281 1.1 jtc {
1282 1.1 jtc int rv;
1283 1.1 jtc
1284 1.1 jtc builtin_argv0 = wp[0];
1285 1.1 jtc builtin_flag = tp->flag;
1286 1.1 jtc shf_reopen(1, SHF_WR, shl_stdout);
1287 1.1 jtc shl_stdout_ok = 1;
1288 1.1 jtc ksh_getopt_reset(&builtin_opt, GF_ERROR);
1289 1.1 jtc rv = (*tp->val.f)(wp);
1290 1.1 jtc shf_flush(shl_stdout);
1291 1.1 jtc shl_stdout_ok = 0;
1292 1.1 jtc builtin_flag = 0;
1293 1.1 jtc builtin_argv0 = (char *) 0;
1294 1.1 jtc return rv;
1295 1.1 jtc }
1296 1.1 jtc
1297 1.1 jtc /*
1298 1.1 jtc * set up redirection, saving old fd's in e->savefd
1299 1.1 jtc */
1300 1.1 jtc static int
1301 1.1 jtc iosetup(iop, tp)
1302 1.1 jtc register struct ioword *iop;
1303 1.1 jtc struct tbl *tp;
1304 1.1 jtc {
1305 1.1 jtc register int u = -1;
1306 1.1 jtc char *cp = iop->name;
1307 1.1 jtc int iotype = iop->flag & IOTYPE;
1308 1.1 jtc int do_open = 1, do_close = 0, UNINITIALIZED(flags);
1309 1.1 jtc struct ioword iotmp;
1310 1.1 jtc struct stat statb;
1311 1.1 jtc
1312 1.1 jtc if (iotype != IOHERE)
1313 1.5 hubertf cp = evalonestr(cp, DOTILDE|(Flag(FTALKING_I) ? DOGLOB : 0));
1314 1.1 jtc
1315 1.1 jtc /* Used for tracing and error messages to print expanded cp */
1316 1.1 jtc iotmp = *iop;
1317 1.1 jtc iotmp.name = (iotype == IOHERE) ? (char *) 0 : cp;
1318 1.1 jtc iotmp.flag |= IONAMEXP;
1319 1.1 jtc
1320 1.1 jtc if (Flag(FXTRACE))
1321 1.1 jtc shellf("%s%s\n",
1322 1.1 jtc PS4_SUBSTITUTE(str_val(global("PS4"))),
1323 1.1 jtc snptreef((char *) 0, 32, "%R", &iotmp));
1324 1.1 jtc
1325 1.1 jtc switch (iotype) {
1326 1.1 jtc case IOREAD:
1327 1.1 jtc flags = O_RDONLY;
1328 1.1 jtc break;
1329 1.1 jtc
1330 1.1 jtc case IOCAT:
1331 1.1 jtc flags = O_WRONLY | O_APPEND | O_CREAT;
1332 1.1 jtc break;
1333 1.1 jtc
1334 1.1 jtc case IOWRITE:
1335 1.1 jtc flags = O_WRONLY | O_CREAT | O_TRUNC;
1336 1.5 hubertf /* The stat() is here to allow redirections to
1337 1.5 hubertf * things like /dev/null without error.
1338 1.5 hubertf */
1339 1.1 jtc if (Flag(FNOCLOBBER) && !(iop->flag & IOCLOB)
1340 1.1 jtc && (stat(cp, &statb) < 0 || S_ISREG(statb.st_mode)))
1341 1.1 jtc flags |= O_EXCL;
1342 1.1 jtc break;
1343 1.1 jtc
1344 1.1 jtc case IORDWR:
1345 1.1 jtc flags = O_RDWR | O_CREAT;
1346 1.1 jtc break;
1347 1.1 jtc
1348 1.1 jtc case IOHERE:
1349 1.1 jtc do_open = 0;
1350 1.1 jtc /* herein() returns -2 if error has been printed */
1351 1.5 hubertf u = herein(iop->heredoc, iop->flag & IOEVAL);
1352 1.1 jtc /* cp may have wrong name */
1353 1.1 jtc break;
1354 1.1 jtc
1355 1.1 jtc case IODUP:
1356 1.1 jtc {
1357 1.1 jtc const char *emsg;
1358 1.1 jtc
1359 1.1 jtc do_open = 0;
1360 1.1 jtc if (*cp == '-' && !cp[1]) {
1361 1.1 jtc u = 1009; /* prevent error return below */
1362 1.1 jtc do_close = 1;
1363 1.1 jtc } else if ((u = check_fd(cp,
1364 1.1 jtc X_OK | ((iop->flag & IORDUP) ? R_OK : W_OK),
1365 1.1 jtc &emsg)) < 0)
1366 1.1 jtc {
1367 1.1 jtc warningf(TRUE, "%s: %s",
1368 1.1 jtc snptreef((char *) 0, 32, "%R", &iotmp), emsg);
1369 1.1 jtc return -1;
1370 1.1 jtc }
1371 1.1 jtc break;
1372 1.1 jtc }
1373 1.1 jtc }
1374 1.1 jtc if (do_open) {
1375 1.1 jtc if (Flag(FRESTRICTED) && (flags & O_CREAT)) {
1376 1.1 jtc warningf(TRUE, "%s: restricted", cp);
1377 1.1 jtc return -1;
1378 1.1 jtc }
1379 1.1 jtc u = open(cp, flags, 0666);
1380 1.1 jtc #ifdef OS2
1381 1.1 jtc if (u < 0 && strcmp(cp, "/dev/null") == 0)
1382 1.1 jtc u = open("nul", flags, 0666);
1383 1.1 jtc #endif /* OS2 */
1384 1.1 jtc }
1385 1.1 jtc if (u < 0) {
1386 1.1 jtc /* herein() may already have printed message */
1387 1.1 jtc if (u == -1)
1388 1.1 jtc warningf(TRUE, "cannot %s %s: %s",
1389 1.1 jtc iotype == IODUP ? "dup"
1390 1.1 jtc : (iotype == IOREAD || iotype == IOHERE) ?
1391 1.1 jtc "open" : "create", cp, strerror(errno));
1392 1.1 jtc return -1;
1393 1.1 jtc }
1394 1.1 jtc /* Do not save if it has already been redirected (i.e. "cat >x >y"). */
1395 1.1 jtc if (e->savefd[iop->unit] == 0)
1396 1.1 jtc /* c_exec() assumes e->savefd[fd] set for any redirections.
1397 1.1 jtc * Ask savefd() not to close iop->unit - allows error messages
1398 1.1 jtc * to be seen if iop->unit is 2; also means we can't lose
1399 1.1 jtc * the fd (eg, both dup2 below and dup2 in restfd() failing).
1400 1.1 jtc */
1401 1.1 jtc e->savefd[iop->unit] = savefd(iop->unit, 1);
1402 1.1 jtc
1403 1.1 jtc if (do_close)
1404 1.1 jtc close(iop->unit);
1405 1.1 jtc else if (u != iop->unit) {
1406 1.1 jtc if (ksh_dup2(u, iop->unit, TRUE) < 0) {
1407 1.1 jtc warningf(TRUE,
1408 1.1 jtc "could not finish (dup) redirection %s: %s",
1409 1.1 jtc snptreef((char *) 0, 32, "%R", &iotmp),
1410 1.1 jtc strerror(errno));
1411 1.1 jtc if (iotype != IODUP)
1412 1.1 jtc close(u);
1413 1.1 jtc return -1;
1414 1.1 jtc }
1415 1.1 jtc if (iotype != IODUP)
1416 1.1 jtc close(u);
1417 1.1 jtc #ifdef KSH
1418 1.1 jtc /* Touching any co-process fd in an empty exec
1419 1.1 jtc * causes the shell to close its copies
1420 1.1 jtc */
1421 1.1 jtc else if (tp && tp->type == CSHELL && tp->val.f == c_exec) {
1422 1.1 jtc if (iop->flag & IORDUP) /* possible exec <&p */
1423 1.1 jtc coproc_read_close(u);
1424 1.1 jtc else /* possible exec >&p */
1425 1.1 jtc coproc_write_close(u);
1426 1.1 jtc }
1427 1.1 jtc #endif /* KSH */
1428 1.1 jtc }
1429 1.1 jtc if (u == 2) /* Clear any write errors */
1430 1.1 jtc shf_reopen(2, SHF_WR, shl_out);
1431 1.1 jtc return 0;
1432 1.1 jtc }
1433 1.1 jtc
1434 1.1 jtc /*
1435 1.1 jtc * open here document temp file.
1436 1.1 jtc * if unquoted here, expand here temp file into second temp file.
1437 1.1 jtc */
1438 1.1 jtc static int
1439 1.5 hubertf herein(content, sub)
1440 1.5 hubertf const char *content;
1441 1.1 jtc int sub;
1442 1.1 jtc {
1443 1.5 hubertf volatile int fd = -1;
1444 1.5 hubertf struct source *s, *volatile osource;
1445 1.5 hubertf struct shf *volatile shf;
1446 1.5 hubertf struct temp *h;
1447 1.5 hubertf int i;
1448 1.1 jtc
1449 1.1 jtc /* ksh -c 'cat << EOF' can cause this... */
1450 1.5 hubertf if (content == (char *) 0) {
1451 1.1 jtc warningf(TRUE, "here document missing");
1452 1.1 jtc return -2; /* special to iosetup(): don't print error */
1453 1.1 jtc }
1454 1.5 hubertf
1455 1.5 hubertf /* Create temp file to hold content (done before newenv so temp
1456 1.5 hubertf * doesn't get removed too soon).
1457 1.5 hubertf */
1458 1.5 hubertf h = maketemp(ATEMP, TT_HEREDOC_EXP, &e->temps);
1459 1.5 hubertf if (!(shf = h->shf) || (fd = open(h->name, O_RDONLY, 0)) < 0) {
1460 1.5 hubertf warningf(TRUE, "can't %s temporary file %s: %s",
1461 1.5 hubertf !shf ? "create" : "open",
1462 1.5 hubertf h->name, strerror(errno));
1463 1.5 hubertf if (shf)
1464 1.5 hubertf shf_close(shf);
1465 1.5 hubertf return -2 /* special to iosetup(): don't print error */;
1466 1.5 hubertf }
1467 1.5 hubertf
1468 1.5 hubertf osource = source;
1469 1.5 hubertf newenv(E_ERRH);
1470 1.5 hubertf i = ksh_sigsetjmp(e->jbuf, 0);
1471 1.5 hubertf if (i) {
1472 1.5 hubertf source = osource;
1473 1.5 hubertf quitenv();
1474 1.5 hubertf shf_close(shf); /* after quitenv */
1475 1.5 hubertf close(fd);
1476 1.5 hubertf return -2; /* special to iosetup(): don't print error */
1477 1.5 hubertf }
1478 1.1 jtc if (sub) {
1479 1.5 hubertf /* Do substitutions on the content of heredoc */
1480 1.5 hubertf s = pushs(SSTRING, ATEMP);
1481 1.5 hubertf s->start = s->str = content;
1482 1.1 jtc source = s;
1483 1.1 jtc if (yylex(ONEWORD) != LWORD)
1484 1.1 jtc internal_errorf(1, "herein: yylex");
1485 1.5 hubertf source = osource;
1486 1.5 hubertf shf_puts(evalstr(yylval.cp, 0), shf);
1487 1.5 hubertf } else
1488 1.5 hubertf shf_puts(content, shf);
1489 1.5 hubertf
1490 1.5 hubertf quitenv();
1491 1.5 hubertf
1492 1.5 hubertf if (shf_close(shf) == EOF) {
1493 1.5 hubertf close(fd);
1494 1.5 hubertf warningf(TRUE, "error writing %s: %s", h->name,
1495 1.5 hubertf strerror(errno));
1496 1.5 hubertf return -2; /* special to iosetup(): don't print error */
1497 1.1 jtc }
1498 1.1 jtc
1499 1.1 jtc return fd;
1500 1.1 jtc }
1501 1.1 jtc
1502 1.1 jtc #ifdef KSH
1503 1.1 jtc /*
1504 1.1 jtc * ksh special - the select command processing section
1505 1.1 jtc * print the args in column form - assuming that we can
1506 1.1 jtc */
1507 1.1 jtc static char *
1508 1.1 jtc do_selectargs(ap, print_menu)
1509 1.1 jtc register char **ap;
1510 1.1 jtc bool_t print_menu;
1511 1.1 jtc {
1512 1.1 jtc static const char *const read_args[] = {
1513 1.1 jtc "read", "-r", "REPLY", (char *) 0
1514 1.1 jtc };
1515 1.1 jtc char *s;
1516 1.1 jtc int i, argct;
1517 1.1 jtc
1518 1.1 jtc for (argct = 0; ap[argct]; argct++)
1519 1.1 jtc ;
1520 1.1 jtc while (1) {
1521 1.1 jtc /* Menu is printed if
1522 1.1 jtc * - this is the first time around the select loop
1523 1.1 jtc * - the user enters a blank line
1524 1.1 jtc * - the REPLY parameter is empty
1525 1.1 jtc */
1526 1.1 jtc if (print_menu || !*str_val(global("REPLY")))
1527 1.1 jtc pr_menu(ap);
1528 1.1 jtc shellf("%s", str_val(global("PS3")));
1529 1.1 jtc if (call_builtin(findcom("read", FC_BI), (char **) read_args))
1530 1.1 jtc return (char *) 0;
1531 1.1 jtc s = str_val(global("REPLY"));
1532 1.1 jtc if (*s) {
1533 1.1 jtc i = atoi(s);
1534 1.1 jtc return (i >= 1 && i <= argct) ? ap[i - 1] : null;
1535 1.1 jtc }
1536 1.1 jtc print_menu = 1;
1537 1.1 jtc }
1538 1.1 jtc }
1539 1.1 jtc
1540 1.1 jtc struct select_menu_info {
1541 1.1 jtc char *const *args;
1542 1.1 jtc int arg_width;
1543 1.1 jtc int num_width;
1544 1.1 jtc } info;
1545 1.1 jtc
1546 1.1 jtc static char *select_fmt_entry ARGS((void *arg, int i, char *buf, int buflen));
1547 1.1 jtc
1548 1.1 jtc /* format a single select menu item */
1549 1.1 jtc static char *
1550 1.1 jtc select_fmt_entry(arg, i, buf, buflen)
1551 1.1 jtc void *arg;
1552 1.1 jtc int i;
1553 1.1 jtc char *buf;
1554 1.1 jtc int buflen;
1555 1.1 jtc {
1556 1.1 jtc struct select_menu_info *smi = (struct select_menu_info *) arg;
1557 1.1 jtc
1558 1.1 jtc shf_snprintf(buf, buflen, "%*d) %s",
1559 1.1 jtc smi->num_width, i + 1, smi->args[i]);
1560 1.1 jtc return buf;
1561 1.1 jtc }
1562 1.1 jtc
1563 1.1 jtc /*
1564 1.1 jtc * print a select style menu
1565 1.1 jtc */
1566 1.1 jtc int
1567 1.1 jtc pr_menu(ap)
1568 1.1 jtc char *const *ap;
1569 1.1 jtc {
1570 1.1 jtc struct select_menu_info smi;
1571 1.1 jtc char *const *pp;
1572 1.1 jtc int nwidth, dwidth;
1573 1.1 jtc int i, n;
1574 1.1 jtc
1575 1.1 jtc /* Width/column calculations were done once and saved, but this
1576 1.1 jtc * means select can't be used recursively so we re-calculate each
1577 1.1 jtc * time (could save in a structure that is returned, but its probably
1578 1.1 jtc * not worth the bother).
1579 1.1 jtc */
1580 1.1 jtc
1581 1.1 jtc /*
1582 1.1 jtc * get dimensions of the list
1583 1.1 jtc */
1584 1.1 jtc for (n = 0, nwidth = 0, pp = ap; *pp; n++, pp++) {
1585 1.1 jtc i = strlen(*pp);
1586 1.1 jtc nwidth = (i > nwidth) ? i : nwidth;
1587 1.1 jtc }
1588 1.1 jtc /*
1589 1.1 jtc * we will print an index of the form
1590 1.1 jtc * %d)
1591 1.1 jtc * in front of each entry
1592 1.1 jtc * get the max width of this
1593 1.1 jtc */
1594 1.1 jtc for (i = n, dwidth = 1; i >= 10; i /= 10)
1595 1.1 jtc dwidth++;
1596 1.1 jtc
1597 1.1 jtc smi.args = ap;
1598 1.1 jtc smi.arg_width = nwidth;
1599 1.1 jtc smi.num_width = dwidth;
1600 1.1 jtc print_columns(shl_out, n, select_fmt_entry, (void *) &smi,
1601 1.7 provos dwidth + nwidth + 2, 1);
1602 1.7 provos
1603 1.7 provos return n;
1604 1.7 provos }
1605 1.7 provos
1606 1.7 provos /* XXX: horrible kludge to fit within the framework */
1607 1.7 provos
1608 1.7 provos static char *plain_fmt_entry ARGS((void *arg, int i, char *buf, int buflen));
1609 1.7 provos
1610 1.7 provos static char *
1611 1.7 provos plain_fmt_entry(arg, i, buf, buflen)
1612 1.7 provos void *arg;
1613 1.7 provos int i;
1614 1.7 provos char *buf;
1615 1.7 provos int buflen;
1616 1.7 provos {
1617 1.7 provos shf_snprintf(buf, buflen, "%s", ((char *const *)arg)[i]);
1618 1.7 provos return buf;
1619 1.7 provos }
1620 1.7 provos
1621 1.7 provos int
1622 1.7 provos pr_list(ap)
1623 1.7 provos char *const *ap;
1624 1.7 provos {
1625 1.7 provos char *const *pp;
1626 1.7 provos int nwidth;
1627 1.7 provos int i, n;
1628 1.7 provos
1629 1.7 provos for (n = 0, nwidth = 0, pp = ap; *pp; n++, pp++) {
1630 1.7 provos i = strlen(*pp);
1631 1.7 provos nwidth = (i > nwidth) ? i : nwidth;
1632 1.7 provos }
1633 1.7 provos print_columns(shl_out, n, plain_fmt_entry, (void *) ap, nwidth + 1, 0);
1634 1.1 jtc
1635 1.1 jtc return n;
1636 1.1 jtc }
1637 1.1 jtc #endif /* KSH */
1638 1.1 jtc #ifdef KSH
1639 1.1 jtc
1640 1.1 jtc /*
1641 1.1 jtc * [[ ... ]] evaluation routines
1642 1.1 jtc */
1643 1.1 jtc
1644 1.1 jtc extern const char *const dbtest_tokens[];
1645 1.1 jtc extern const char db_close[];
1646 1.1 jtc
1647 1.1 jtc /* Test if the current token is a whatever. Accepts the current token if
1648 1.1 jtc * it is. Returns 0 if it is not, non-zero if it is (in the case of
1649 1.1 jtc * TM_UNOP and TM_BINOP, the returned value is a Test_op).
1650 1.1 jtc */
1651 1.1 jtc static int
1652 1.1 jtc dbteste_isa(te, meta)
1653 1.1 jtc Test_env *te;
1654 1.1 jtc Test_meta meta;
1655 1.1 jtc {
1656 1.1 jtc int ret = 0;
1657 1.1 jtc int uqword;
1658 1.1 jtc char *p;
1659 1.1 jtc
1660 1.1 jtc if (!*te->pos.wp)
1661 1.1 jtc return meta == TM_END;
1662 1.1 jtc
1663 1.1 jtc /* unquoted word? */
1664 1.1 jtc for (p = *te->pos.wp; *p == CHAR; p += 2)
1665 1.1 jtc ;
1666 1.1 jtc uqword = *p == EOS;
1667 1.1 jtc
1668 1.1 jtc if (meta == TM_UNOP || meta == TM_BINOP) {
1669 1.1 jtc if (uqword) {
1670 1.1 jtc char buf[8]; /* longer than the longest operator */
1671 1.1 jtc char *q = buf;
1672 1.1 jtc for (p = *te->pos.wp; *p == CHAR
1673 1.1 jtc && q < &buf[sizeof(buf) - 1];
1674 1.1 jtc p += 2)
1675 1.1 jtc *q++ = p[1];
1676 1.1 jtc *q = '\0';
1677 1.1 jtc ret = (int) test_isop(te, meta, buf);
1678 1.1 jtc }
1679 1.1 jtc } else if (meta == TM_END)
1680 1.1 jtc ret = 0;
1681 1.1 jtc else
1682 1.1 jtc ret = uqword
1683 1.1 jtc && strcmp(*te->pos.wp, dbtest_tokens[(int) meta]) == 0;
1684 1.1 jtc
1685 1.1 jtc /* Accept the token? */
1686 1.1 jtc if (ret)
1687 1.1 jtc te->pos.wp++;
1688 1.1 jtc
1689 1.1 jtc return ret;
1690 1.1 jtc }
1691 1.1 jtc
1692 1.1 jtc static const char *
1693 1.1 jtc dbteste_getopnd(te, op, do_eval)
1694 1.1 jtc Test_env *te;
1695 1.1 jtc Test_op op;
1696 1.1 jtc int do_eval;
1697 1.1 jtc {
1698 1.1 jtc char *s = *te->pos.wp;
1699 1.1 jtc
1700 1.1 jtc if (!s)
1701 1.1 jtc return (char *) 0;
1702 1.1 jtc
1703 1.1 jtc te->pos.wp++;
1704 1.1 jtc
1705 1.1 jtc if (!do_eval)
1706 1.1 jtc return null;
1707 1.1 jtc
1708 1.1 jtc if (op == TO_STEQL || op == TO_STNEQ)
1709 1.1 jtc s = evalstr(s, DOTILDE | DOPAT);
1710 1.1 jtc else
1711 1.1 jtc s = evalstr(s, DOTILDE);
1712 1.1 jtc
1713 1.1 jtc return s;
1714 1.1 jtc }
1715 1.1 jtc
1716 1.1 jtc static int
1717 1.1 jtc dbteste_eval(te, op, opnd1, opnd2, do_eval)
1718 1.1 jtc Test_env *te;
1719 1.1 jtc Test_op op;
1720 1.1 jtc const char *opnd1;
1721 1.1 jtc const char *opnd2;
1722 1.1 jtc int do_eval;
1723 1.1 jtc {
1724 1.1 jtc return test_eval(te, op, opnd1, opnd2, do_eval);
1725 1.1 jtc }
1726 1.1 jtc
1727 1.1 jtc static void
1728 1.1 jtc dbteste_error(te, offset, msg)
1729 1.1 jtc Test_env *te;
1730 1.1 jtc int offset;
1731 1.1 jtc const char *msg;
1732 1.1 jtc {
1733 1.1 jtc te->flags |= TEF_ERROR;
1734 1.1 jtc internal_errorf(0, "dbteste_error: %s (offset %d)", msg, offset);
1735 1.1 jtc }
1736 1.1 jtc #endif /* KSH */
1737