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