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