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