1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)jobs.c 8.4 (Berkeley) 04/28/95"; 13 #endif /* not lint */ 14 15 #include "shell.h" 16 #if JOBS 17 #include "sgtty.h" 18 #undef CEOF /* syntax.h redefines this */ 19 #endif 20 #include "main.h" 21 #include "parser.h" 22 #include "nodes.h" 23 #include "jobs.h" 24 #include "options.h" 25 #include "trap.h" 26 #include "syntax.h" 27 #include "input.h" 28 #include "output.h" 29 #include "memalloc.h" 30 #include "error.h" 31 #include "mystring.h" 32 #include <fcntl.h> 33 #include <signal.h> 34 #include <errno.h> 35 #include <unistd.h> 36 #ifdef BSD 37 #include <sys/types.h> 38 #include <sys/wait.h> 39 #include <sys/time.h> 40 #include <sys/resource.h> 41 #endif 42 43 44 45 struct job *jobtab; /* array of jobs */ 46 int njobs; /* size of array */ 47 MKINIT short backgndpid = -1; /* pid of last background process */ 48 #if JOBS 49 int initialpgrp; /* pgrp of shell on invocation */ 50 short curjob; /* current job */ 51 #endif 52 53 #ifdef __STDC__ 54 STATIC void restartjob(struct job *); 55 STATIC struct job *getjob(char *); 56 STATIC void freejob(struct job *); 57 STATIC int procrunning(int); 58 STATIC int dowait(int, struct job *); 59 STATIC int waitproc(int, int *); 60 #else 61 STATIC void restartjob(); 62 STATIC struct job *getjob(); 63 STATIC void freejob(); 64 STATIC int procrunning(); 65 STATIC int dowait(); 66 STATIC int waitproc(); 67 #endif 68 69 70 71 /* 72 * Turn job control on and off. 73 * 74 * Note: This code assumes that the third arg to ioctl is a character 75 * pointer, which is true on Berkeley systems but not System V. Since 76 * System V doesn't have job control yet, this isn't a problem now. 77 */ 78 79 MKINIT int jobctl; 80 81 void 82 setjobctl(on) { 83 #ifdef OLD_TTY_DRIVER 84 int ldisc; 85 #endif 86 87 if (on == jobctl || rootshell == 0) 88 return; 89 if (on) { 90 do { /* while we are in the background */ 91 if (ioctl(2, TIOCGPGRP, (char *)&initialpgrp) < 0) { 92 out2str("sh: can't access tty; job control turned off\n"); 93 mflag = 0; 94 return; 95 } 96 if (initialpgrp == -1) 97 initialpgrp = getpgrp(); 98 else if (initialpgrp != getpgrp()) { 99 killpg(initialpgrp, SIGTTIN); 100 continue; 101 } 102 } while (0); 103 #ifdef OLD_TTY_DRIVER 104 if (ioctl(2, TIOCGETD, (char *)&ldisc) < 0 || ldisc != NTTYDISC) { 105 out2str("sh: need new tty driver to run job control; job control turned off\n"); 106 mflag = 0; 107 return; 108 } 109 #endif 110 setsignal(SIGTSTP); 111 setsignal(SIGTTOU); 112 setsignal(SIGTTIN); 113 setpgid(0, rootpid); 114 ioctl(2, TIOCSPGRP, (char *)&rootpid); 115 } else { /* turning job control off */ 116 setpgid(0, initialpgrp); 117 ioctl(2, TIOCSPGRP, (char *)&initialpgrp); 118 setsignal(SIGTSTP); 119 setsignal(SIGTTOU); 120 setsignal(SIGTTIN); 121 } 122 jobctl = on; 123 } 124 125 126 #ifdef mkinit 127 128 SHELLPROC { 129 backgndpid = -1; 130 #if JOBS 131 jobctl = 0; 132 #endif 133 } 134 135 #endif 136 137 138 139 #if JOBS 140 fgcmd(argc, argv) char **argv; { 141 struct job *jp; 142 int pgrp; 143 int status; 144 145 jp = getjob(argv[1]); 146 if (jp->jobctl == 0) 147 error("job not created under job control"); 148 pgrp = jp->ps[0].pid; 149 ioctl(2, TIOCSPGRP, (char *)&pgrp); 150 restartjob(jp); 151 INTOFF; 152 status = waitforjob(jp); 153 INTON; 154 return status; 155 } 156 157 158 bgcmd(argc, argv) char **argv; { 159 struct job *jp; 160 161 do { 162 jp = getjob(*++argv); 163 if (jp->jobctl == 0) 164 error("job not created under job control"); 165 restartjob(jp); 166 } while (--argc > 1); 167 return 0; 168 } 169 170 171 STATIC void 172 restartjob(jp) 173 struct job *jp; 174 { 175 struct procstat *ps; 176 int i; 177 178 if (jp->state == JOBDONE) 179 return; 180 INTOFF; 181 killpg(jp->ps[0].pid, SIGCONT); 182 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) { 183 if ((ps->status & 0377) == 0177) { 184 ps->status = -1; 185 jp->state = 0; 186 } 187 } 188 INTON; 189 } 190 #endif 191 192 193 int 194 jobscmd(argc, argv) char **argv; { 195 showjobs(0); 196 return 0; 197 } 198 199 200 /* 201 * Print a list of jobs. If "change" is nonzero, only print jobs whose 202 * statuses have changed since the last call to showjobs. 203 * 204 * If the shell is interrupted in the process of creating a job, the 205 * result may be a job structure containing zero processes. Such structures 206 * will be freed here. 207 */ 208 209 void 210 showjobs(change) { 211 int jobno; 212 int procno; 213 int i; 214 struct job *jp; 215 struct procstat *ps; 216 int col; 217 char s[64]; 218 219 TRACE(("showjobs(%d) called\n", change)); 220 while (dowait(0, (struct job *)NULL) > 0); 221 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) { 222 if (! jp->used) 223 continue; 224 if (jp->nprocs == 0) { 225 freejob(jp); 226 continue; 227 } 228 if (change && ! jp->changed) 229 continue; 230 procno = jp->nprocs; 231 for (ps = jp->ps ; ; ps++) { /* for each process */ 232 if (ps == jp->ps) 233 fmtstr(s, 64, "[%d] %d ", jobno, ps->pid); 234 else 235 fmtstr(s, 64, " %d ", ps->pid); 236 out1str(s); 237 col = strlen(s); 238 s[0] = '\0'; 239 if (ps->status == -1) { 240 /* don't print anything */ 241 } else if ((ps->status & 0xFF) == 0) { 242 fmtstr(s, 64, "Exit %d", ps->status >> 8); 243 } else { 244 i = ps->status; 245 #if JOBS 246 if ((i & 0xFF) == 0177) 247 i >>= 8; 248 #endif 249 if ((i & 0x7F) < NSIG && sys_siglist[i & 0x7F]) 250 scopy(sys_siglist[i & 0x7F], s); 251 else 252 fmtstr(s, 64, "Signal %d", i & 0x7F); 253 if (i & 0x80) 254 strcat(s, " (core dumped)"); 255 } 256 out1str(s); 257 col += strlen(s); 258 do { 259 out1c(' '); 260 col++; 261 } while (col < 30); 262 out1str(ps->cmd); 263 out1c('\n'); 264 if (--procno <= 0) 265 break; 266 } 267 jp->changed = 0; 268 if (jp->state == JOBDONE) { 269 freejob(jp); 270 } 271 } 272 } 273 274 275 /* 276 * Mark a job structure as unused. 277 */ 278 279 STATIC void 280 freejob(jp) 281 struct job *jp; 282 { 283 struct procstat *ps; 284 int i; 285 286 INTOFF; 287 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) { 288 if (ps->cmd != nullstr) 289 ckfree(ps->cmd); 290 } 291 if (jp->ps != &jp->ps0) 292 ckfree(jp->ps); 293 jp->used = 0; 294 #if JOBS 295 if (curjob == jp - jobtab + 1) 296 curjob = 0; 297 #endif 298 INTON; 299 } 300 301 302 303 int 304 waitcmd(argc, argv) char **argv; { 305 struct job *job; 306 int status; 307 struct job *jp; 308 309 if (argc > 1) { 310 job = getjob(argv[1]); 311 } else { 312 job = NULL; 313 } 314 for (;;) { /* loop until process terminated or stopped */ 315 if (job != NULL) { 316 if (job->state) { 317 status = job->ps[job->nprocs - 1].status; 318 if ((status & 0xFF) == 0) 319 status = status >> 8 & 0xFF; 320 #if JOBS 321 else if ((status & 0xFF) == 0177) 322 status = (status >> 8 & 0x7F) + 128; 323 #endif 324 else 325 status = (status & 0x7F) + 128; 326 if (! iflag) 327 freejob(job); 328 return status; 329 } 330 } else { 331 for (jp = jobtab ; ; jp++) { 332 if (jp >= jobtab + njobs) { /* no running procs */ 333 return 0; 334 } 335 if (jp->used && jp->state == 0) 336 break; 337 } 338 } 339 dowait(1, (struct job *)NULL); 340 } 341 } 342 343 344 345 jobidcmd(argc, argv) char **argv; { 346 struct job *jp; 347 int i; 348 349 jp = getjob(argv[1]); 350 for (i = 0 ; i < jp->nprocs ; ) { 351 out1fmt("%d", jp->ps[i].pid); 352 out1c(++i < jp->nprocs? ' ' : '\n'); 353 } 354 return 0; 355 } 356 357 358 359 /* 360 * Convert a job name to a job structure. 361 */ 362 363 STATIC struct job * 364 getjob(name) 365 char *name; 366 { 367 int jobno; 368 register struct job *jp; 369 int pid; 370 int i; 371 372 if (name == NULL) { 373 #if JOBS 374 currentjob: 375 if ((jobno = curjob) == 0 || jobtab[jobno - 1].used == 0) 376 error("No current job"); 377 return &jobtab[jobno - 1]; 378 #else 379 error("No current job"); 380 #endif 381 } else if (name[0] == '%') { 382 if (is_digit(name[1])) { 383 jobno = number(name + 1); 384 if (jobno > 0 && jobno <= njobs 385 && jobtab[jobno - 1].used != 0) 386 return &jobtab[jobno - 1]; 387 #if JOBS 388 } else if (name[1] == '%' && name[2] == '\0') { 389 goto currentjob; 390 #endif 391 } else { 392 register struct job *found = NULL; 393 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 394 if (jp->used && jp->nprocs > 0 395 && prefix(name + 1, jp->ps[0].cmd)) { 396 if (found) 397 error("%s: ambiguous", name); 398 found = jp; 399 } 400 } 401 if (found) 402 return found; 403 } 404 } else if (is_number(name)) { 405 pid = number(name); 406 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) { 407 if (jp->used && jp->nprocs > 0 408 && jp->ps[jp->nprocs - 1].pid == pid) 409 return jp; 410 } 411 } 412 error("No such job: %s", name); 413 } 414 415 416 417 /* 418 * Return a new job structure, 419 */ 420 421 struct job * 422 makejob(node, nprocs) 423 union node *node; 424 { 425 int i; 426 struct job *jp; 427 428 for (i = njobs, jp = jobtab ; ; jp++) { 429 if (--i < 0) { 430 INTOFF; 431 if (njobs == 0) { 432 jobtab = ckmalloc(4 * sizeof jobtab[0]); 433 } else { 434 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]); 435 memmove(jp, jobtab, njobs * sizeof jp[0]); 436 ckfree(jobtab); 437 jobtab = jp; 438 } 439 jp = jobtab + njobs; 440 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0); 441 INTON; 442 break; 443 } 444 if (jp->used == 0) 445 break; 446 } 447 INTOFF; 448 jp->state = 0; 449 jp->used = 1; 450 jp->changed = 0; 451 jp->nprocs = 0; 452 #if JOBS 453 jp->jobctl = jobctl; 454 #endif 455 if (nprocs > 1) { 456 jp->ps = ckmalloc(nprocs * sizeof (struct procstat)); 457 } else { 458 jp->ps = &jp->ps0; 459 } 460 INTON; 461 TRACE(("makejob(0x%x, %d) returns %%%d\n", (int)node, nprocs, jp - jobtab + 1)); 462 return jp; 463 } 464 465 466 /* 467 * Fork of a subshell. If we are doing job control, give the subshell its 468 * own process group. Jp is a job structure that the job is to be added to. 469 * N is the command that will be evaluated by the child. Both jp and n may 470 * be NULL. The mode parameter can be one of the following: 471 * FORK_FG - Fork off a foreground process. 472 * FORK_BG - Fork off a background process. 473 * FORK_NOJOB - Like FORK_FG, but don't give the process its own 474 * process group even if job control is on. 475 * 476 * When job control is turned off, background processes have their standard 477 * input redirected to /dev/null (except for the second and later processes 478 * in a pipeline). 479 */ 480 481 int 482 forkshell(jp, n, mode) 483 union node *n; 484 struct job *jp; 485 { 486 int pid; 487 int pgrp; 488 489 TRACE(("forkshell(%%%d, 0x%x, %d) called\n", jp - jobtab, (int)n, mode)); 490 INTOFF; 491 pid = fork(); 492 if (pid == -1) { 493 TRACE(("Fork failed, errno=%d\n", errno)); 494 INTON; 495 error("Cannot fork"); 496 } 497 if (pid == 0) { 498 struct job *p; 499 int wasroot; 500 int i; 501 502 TRACE(("Child shell %d\n", getpid())); 503 wasroot = rootshell; 504 rootshell = 0; 505 for (i = njobs, p = jobtab ; --i >= 0 ; p++) 506 if (p->used) 507 freejob(p); 508 closescript(); 509 INTON; 510 clear_traps(); 511 #if JOBS 512 jobctl = 0; /* do job control only in root shell */ 513 if (wasroot && mode != FORK_NOJOB && mflag) { 514 if (jp == NULL || jp->nprocs == 0) 515 pgrp = getpid(); 516 else 517 pgrp = jp->ps[0].pid; 518 setpgid(0, pgrp); 519 if (mode == FORK_FG) { 520 /*** this causes superfluous TIOCSPGRPS ***/ 521 if (ioctl(2, TIOCSPGRP, (char *)&pgrp) < 0) 522 error("TIOCSPGRP failed, errno=%d\n", errno); 523 } 524 setsignal(SIGTSTP); 525 setsignal(SIGTTOU); 526 } else if (mode == FORK_BG) { 527 ignoresig(SIGINT); 528 ignoresig(SIGQUIT); 529 if ((jp == NULL || jp->nprocs == 0) && 530 ! fd0_redirected_p ()) { 531 close(0); 532 if (open("/dev/null", O_RDONLY) != 0) 533 error("Can't open /dev/null"); 534 } 535 } 536 #else 537 if (mode == FORK_BG) { 538 ignoresig(SIGINT); 539 ignoresig(SIGQUIT); 540 if ((jp == NULL || jp->nprocs == 0) && 541 ! fd0_redirected_p ()) { 542 close(0); 543 if (open("/dev/null", O_RDONLY) != 0) 544 error("Can't open /dev/null"); 545 } 546 } 547 #endif 548 if (wasroot && iflag) { 549 setsignal(SIGINT); 550 setsignal(SIGQUIT); 551 setsignal(SIGTERM); 552 } 553 return pid; 554 } 555 if (rootshell && mode != FORK_NOJOB && mflag) { 556 if (jp == NULL || jp->nprocs == 0) 557 pgrp = pid; 558 else 559 pgrp = jp->ps[0].pid; 560 setpgid(pid, pgrp); 561 } 562 if (mode == FORK_BG) 563 backgndpid = pid; /* set $! */ 564 if (jp) { 565 struct procstat *ps = &jp->ps[jp->nprocs++]; 566 ps->pid = pid; 567 ps->status = -1; 568 ps->cmd = nullstr; 569 if (iflag && rootshell && n) 570 ps->cmd = commandtext(n); 571 } 572 INTON; 573 TRACE(("In parent shell: child = %d\n", pid)); 574 return pid; 575 } 576 577 578 579 /* 580 * Wait for job to finish. 581 * 582 * Under job control we have the problem that while a child process is 583 * running interrupts generated by the user are sent to the child but not 584 * to the shell. This means that an infinite loop started by an inter- 585 * active user may be hard to kill. With job control turned off, an 586 * interactive user may place an interactive program inside a loop. If 587 * the interactive program catches interrupts, the user doesn't want 588 * these interrupts to also abort the loop. The approach we take here 589 * is to have the shell ignore interrupt signals while waiting for a 590 * forground process to terminate, and then send itself an interrupt 591 * signal if the child process was terminated by an interrupt signal. 592 * Unfortunately, some programs want to do a bit of cleanup and then 593 * exit on interrupt; unless these processes terminate themselves by 594 * sending a signal to themselves (instead of calling exit) they will 595 * confuse this approach. 596 */ 597 598 int 599 waitforjob(jp) 600 register struct job *jp; 601 { 602 #if JOBS 603 int mypgrp = getpgrp(); 604 #endif 605 int status; 606 int st; 607 608 INTOFF; 609 TRACE(("waitforjob(%%%d) called\n", jp - jobtab + 1)); 610 while (jp->state == 0) { 611 dowait(1, jp); 612 } 613 #if JOBS 614 if (jp->jobctl) { 615 if (ioctl(2, TIOCSPGRP, (char *)&mypgrp) < 0) 616 error("TIOCSPGRP failed, errno=%d\n", errno); 617 } 618 if (jp->state == JOBSTOPPED) 619 curjob = jp - jobtab + 1; 620 #endif 621 status = jp->ps[jp->nprocs - 1].status; 622 /* convert to 8 bits */ 623 if ((status & 0xFF) == 0) 624 st = status >> 8 & 0xFF; 625 #if JOBS 626 else if ((status & 0xFF) == 0177) 627 st = (status >> 8 & 0x7F) + 128; 628 #endif 629 else 630 st = (status & 0x7F) + 128; 631 if (! JOBS || jp->state == JOBDONE) 632 freejob(jp); 633 CLEAR_PENDING_INT; 634 if ((status & 0x7F) == SIGINT) 635 kill(getpid(), SIGINT); 636 INTON; 637 return st; 638 } 639 640 641 642 /* 643 * Wait for a process to terminate. 644 */ 645 646 STATIC int 647 dowait(block, job) 648 struct job *job; 649 { 650 int pid; 651 int status; 652 struct procstat *sp; 653 struct job *jp; 654 struct job *thisjob; 655 int done; 656 int stopped; 657 int core; 658 659 TRACE(("dowait(%d) called\n", block)); 660 do { 661 pid = waitproc(block, &status); 662 TRACE(("wait returns %d, status=%d\n", pid, status)); 663 } while (pid == -1 && errno == EINTR); 664 if (pid <= 0) 665 return pid; 666 INTOFF; 667 thisjob = NULL; 668 for (jp = jobtab ; jp < jobtab + njobs ; jp++) { 669 if (jp->used) { 670 done = 1; 671 stopped = 1; 672 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) { 673 if (sp->pid == -1) 674 continue; 675 if (sp->pid == pid) { 676 TRACE(("Changin status of proc %d from 0x%x to 0x%x\n", pid, sp->status, status)); 677 sp->status = status; 678 thisjob = jp; 679 } 680 if (sp->status == -1) 681 stopped = 0; 682 else if ((sp->status & 0377) == 0177) 683 done = 0; 684 } 685 if (stopped) { /* stopped or done */ 686 int state = done? JOBDONE : JOBSTOPPED; 687 if (jp->state != state) { 688 TRACE(("Job %d: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state)); 689 jp->state = state; 690 #if JOBS 691 if (done && curjob == jp - jobtab + 1) 692 curjob = 0; /* no current job */ 693 #endif 694 } 695 } 696 } 697 } 698 INTON; 699 if (! rootshell || ! iflag || (job && thisjob == job)) { 700 #if JOBS 701 if ((status & 0xFF) == 0177) 702 status >>= 8; 703 #endif 704 core = status & 0x80; 705 status &= 0x7F; 706 if (status != 0 && status != SIGINT && status != SIGPIPE) { 707 if (thisjob != job) 708 outfmt(out2, "%d: ", pid); 709 #if JOBS 710 if (status == SIGTSTP && rootshell && iflag) 711 outfmt(out2, "%%%d ", job - jobtab + 1); 712 #endif 713 if (status < NSIG && sys_siglist[status]) 714 out2str(sys_siglist[status]); 715 else 716 outfmt(out2, "Signal %d", status); 717 if (core) 718 out2str(" - core dumped"); 719 out2c('\n'); 720 flushout(&errout); 721 } else { 722 TRACE(("Not printing status: status=%d\n", status)); 723 } 724 } else { 725 TRACE(("Not printing status, rootshell=%d, job=0x%x\n", rootshell, job)); 726 if (thisjob) 727 thisjob->changed = 1; 728 } 729 return pid; 730 } 731 732 733 734 /* 735 * Do a wait system call. If job control is compiled in, we accept 736 * stopped processes. If block is zero, we return a value of zero 737 * rather than blocking. 738 * 739 * System V doesn't have a non-blocking wait system call. It does 740 * have a SIGCLD signal that is sent to a process when one of it's 741 * children dies. The obvious way to use SIGCLD would be to install 742 * a handler for SIGCLD which simply bumped a counter when a SIGCLD 743 * was received, and have waitproc bump another counter when it got 744 * the status of a process. Waitproc would then know that a wait 745 * system call would not block if the two counters were different. 746 * This approach doesn't work because if a process has children that 747 * have not been waited for, System V will send it a SIGCLD when it 748 * installs a signal handler for SIGCLD. What this means is that when 749 * a child exits, the shell will be sent SIGCLD signals continuously 750 * until is runs out of stack space, unless it does a wait call before 751 * restoring the signal handler. The code below takes advantage of 752 * this (mis)feature by installing a signal handler for SIGCLD and 753 * then checking to see whether it was called. If there are any 754 * children to be waited for, it will be. 755 * 756 * If neither SYSV nor BSD is defined, we don't implement nonblocking 757 * waits at all. In this case, the user will not be informed when 758 * a background process until the next time she runs a real program 759 * (as opposed to running a builtin command or just typing return), 760 * and the jobs command may give out of date information. 761 */ 762 763 #ifdef SYSV 764 STATIC int gotsigchild; 765 766 STATIC int onsigchild() { 767 gotsigchild = 1; 768 } 769 #endif 770 771 772 STATIC int 773 waitproc(block, status) 774 int *status; 775 { 776 #ifdef BSD 777 int flags; 778 779 #if JOBS 780 flags = WUNTRACED; 781 #else 782 flags = 0; 783 #endif 784 if (block == 0) 785 flags |= WNOHANG; 786 return wait3(status, flags, (struct rusage *)NULL); 787 #else 788 #ifdef SYSV 789 int (*save)(); 790 791 if (block == 0) { 792 gotsigchild = 0; 793 save = signal(SIGCLD, onsigchild); 794 signal(SIGCLD, save); 795 if (gotsigchild == 0) 796 return 0; 797 } 798 return wait(status); 799 #else 800 if (block == 0) 801 return 0; 802 return wait(status); 803 #endif 804 #endif 805 } 806 807 /* 808 * return 1 if there are stopped jobs, otherwise 0 809 */ 810 int job_warning = 0; 811 int 812 stoppedjobs() 813 { 814 register int jobno; 815 register struct job *jp; 816 817 if (job_warning) 818 return (0); 819 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) { 820 if (jp->used == 0) 821 continue; 822 if (jp->state == JOBSTOPPED) { 823 out2str("You have stopped jobs.\n"); 824 job_warning = 2; 825 return (1); 826 } 827 } 828 829 return (0); 830 } 831 832 /* 833 * Return a string identifying a command (to be printed by the 834 * jobs command. 835 */ 836 837 STATIC char *cmdnextc; 838 STATIC int cmdnleft; 839 STATIC void cmdtxt(), cmdputs(); 840 #define MAXCMDTEXT 200 841 842 char * 843 commandtext(n) 844 union node *n; 845 { 846 char *name; 847 848 cmdnextc = name = ckmalloc(MAXCMDTEXT); 849 cmdnleft = MAXCMDTEXT - 4; 850 cmdtxt(n); 851 *cmdnextc = '\0'; 852 return name; 853 } 854 855 856 STATIC void 857 cmdtxt(n) 858 union node *n; 859 { 860 union node *np; 861 struct nodelist *lp; 862 char *p; 863 int i; 864 char s[2]; 865 866 if (n == NULL) 867 return; 868 switch (n->type) { 869 case NSEMI: 870 cmdtxt(n->nbinary.ch1); 871 cmdputs("; "); 872 cmdtxt(n->nbinary.ch2); 873 break; 874 case NAND: 875 cmdtxt(n->nbinary.ch1); 876 cmdputs(" && "); 877 cmdtxt(n->nbinary.ch2); 878 break; 879 case NOR: 880 cmdtxt(n->nbinary.ch1); 881 cmdputs(" || "); 882 cmdtxt(n->nbinary.ch2); 883 break; 884 case NPIPE: 885 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) { 886 cmdtxt(lp->n); 887 if (lp->next) 888 cmdputs(" | "); 889 } 890 break; 891 case NSUBSHELL: 892 cmdputs("("); 893 cmdtxt(n->nredir.n); 894 cmdputs(")"); 895 break; 896 case NREDIR: 897 case NBACKGND: 898 cmdtxt(n->nredir.n); 899 break; 900 case NIF: 901 cmdputs("if "); 902 cmdtxt(n->nif.test); 903 cmdputs("; then "); 904 cmdtxt(n->nif.ifpart); 905 cmdputs("..."); 906 break; 907 case NWHILE: 908 cmdputs("while "); 909 goto until; 910 case NUNTIL: 911 cmdputs("until "); 912 until: 913 cmdtxt(n->nbinary.ch1); 914 cmdputs("; do "); 915 cmdtxt(n->nbinary.ch2); 916 cmdputs("; done"); 917 break; 918 case NFOR: 919 cmdputs("for "); 920 cmdputs(n->nfor.var); 921 cmdputs(" in ..."); 922 break; 923 case NCASE: 924 cmdputs("case "); 925 cmdputs(n->ncase.expr->narg.text); 926 cmdputs(" in ..."); 927 break; 928 case NDEFUN: 929 cmdputs(n->narg.text); 930 cmdputs("() ..."); 931 break; 932 case NCMD: 933 for (np = n->ncmd.args ; np ; np = np->narg.next) { 934 cmdtxt(np); 935 if (np->narg.next) 936 cmdputs(" "); 937 } 938 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) { 939 cmdputs(" "); 940 cmdtxt(np); 941 } 942 break; 943 case NARG: 944 cmdputs(n->narg.text); 945 break; 946 case NTO: 947 p = ">"; i = 1; goto redir; 948 case NAPPEND: 949 p = ">>"; i = 1; goto redir; 950 case NTOFD: 951 p = ">&"; i = 1; goto redir; 952 case NFROM: 953 p = "<"; i = 0; goto redir; 954 case NFROMFD: 955 p = "<&"; i = 0; goto redir; 956 redir: 957 if (n->nfile.fd != i) { 958 s[0] = n->nfile.fd + '0'; 959 s[1] = '\0'; 960 cmdputs(s); 961 } 962 cmdputs(p); 963 if (n->type == NTOFD || n->type == NFROMFD) { 964 s[0] = n->ndup.dupfd + '0'; 965 s[1] = '\0'; 966 cmdputs(s); 967 } else { 968 cmdtxt(n->nfile.fname); 969 } 970 break; 971 case NHERE: 972 case NXHERE: 973 cmdputs("<<..."); 974 break; 975 default: 976 cmdputs("???"); 977 break; 978 } 979 } 980 981 982 983 STATIC void 984 cmdputs(s) 985 char *s; 986 { 987 register char *p, *q; 988 register char c; 989 int subtype = 0; 990 991 if (cmdnleft <= 0) 992 return; 993 p = s; 994 q = cmdnextc; 995 while ((c = *p++) != '\0') { 996 if (c == CTLESC) 997 *q++ = *p++; 998 else if (c == CTLVAR) { 999 *q++ = '$'; 1000 if (--cmdnleft > 0) 1001 *q++ = '{'; 1002 subtype = *p++; 1003 } else if (c == '=' && subtype != 0) { 1004 *q++ = "}-+?="[(subtype & VSTYPE) - VSNORMAL]; 1005 subtype = 0; 1006 } else if (c == CTLENDVAR) { 1007 *q++ = '}'; 1008 } else if (c == CTLBACKQ | c == CTLBACKQ+CTLQUOTE) 1009 cmdnleft++; /* ignore it */ 1010 else 1011 *q++ = c; 1012 if (--cmdnleft <= 0) { 1013 *q++ = '.'; 1014 *q++ = '.'; 1015 *q++ = '.'; 1016 break; 1017 } 1018 } 1019 cmdnextc = q; 1020 } 1021