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