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