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