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