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