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