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