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