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