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 * 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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
39 #include <sys/ioctl.h>
40 #include <sys/param.h>
41 #include <sys/resource.h>
42 #include <sys/time.h>
43 #include <sys/wait.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <paths.h>
47 #include <signal.h>
48 #include <stddef.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51
52 #include "shell.h"
53 #if JOBS
54 #include <termios.h>
55 #undef CEOF /* syntax.h redefines this */
56 #endif
57 #include "redir.h"
58 #include "exec.h"
59 #include "show.h"
60 #include "main.h"
61 #include "parser.h"
62 #include "nodes.h"
63 #include "jobs.h"
64 #include "options.h"
65 #include "trap.h"
66 #include "syntax.h"
67 #include "input.h"
68 #include "output.h"
69 #include "memalloc.h"
70 #include "error.h"
71 #include "mystring.h"
72 #include "var.h"
73 #include "builtins.h"
74 #include "eval.h"
75
76
77 /*
78 * A job structure contains information about a job. A job is either a
79 * single process or a set of processes contained in a pipeline. In the
80 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
81 * array of pids.
82 */
83
84 struct procstat {
85 pid_t pid; /* process id */
86 int status; /* status flags (defined above) */
87 char *cmd; /* text of command being run */
88 };
89
90
91 /* states */
92 #define JOBSTOPPED 1 /* all procs are stopped */
93 #define JOBDONE 2 /* all procs are completed */
94
95
96 struct job {
97 struct procstat ps0; /* status of process */
98 struct procstat *ps; /* status or processes when more than one */
99 short nprocs; /* number of processes */
100 pid_t pgrp; /* process group of this job */
101 char state; /* true if job is finished */
102 char used; /* true if this entry is in used */
103 char changed; /* true if status has changed */
104 char foreground; /* true if running in the foreground */
105 char remembered; /* true if $! referenced */
106 char pipefail; /* pass any non-zero status */
107 #if JOBS
108 char jobctl; /* job running under job control */
109 struct job *next; /* job used after this one */
110 #endif
111 };
112
113
114 static struct job *jobtab; /* array of jobs */
115 static int njobs; /* size of array */
116 static pid_t backgndpid = -1; /* pid of last background process */
117 static struct job *bgjob = NULL; /* last background process */
118 #if JOBS
119 static struct job *jobmru; /* most recently used job list */
120 static pid_t initialpgrp; /* pgrp of shell on invocation */
121 #endif
122 static int ttyfd = -1;
123
124 /* mode flags for dowait */
125 #define DOWAIT_BLOCK 0x1 /* wait until a child exits */
126 #define DOWAIT_SIG 0x2 /* if DOWAIT_BLOCK, abort on signal */
127 #define DOWAIT_SIG_TRAP 0x4 /* if DOWAIT_SIG, abort on trapped signal only */
128
129 #if JOBS
130 static void restartjob(struct job *);
131 #endif
132 static void freejob(struct job *);
133 static int waitcmdloop(struct job *);
134 static struct job *getjob_nonotfound(const char *);
135 static struct job *getjob(const char *);
136 pid_t killjob(const char *, int);
137 static pid_t dowait(int, struct job *);
138 static void checkzombies(void);
139 static void cmdtxt(union node *);
140 static void cmdputs(const char *);
141 #if JOBS
142 static void setcurjob(struct job *);
143 static void deljob(struct job *);
144 static struct job *getcurjob(struct job *);
145 #endif
146 static int getjobstatus(const struct job *);
147 static void printjobcmd(struct job *);
148 static void showjob(struct job *, int);
149
150
151 /*
152 * Turn job control on and off.
153 */
154
155 static int jobctl;
156
157 #if JOBS
158 static void
jobctl_notty(void)159 jobctl_notty(void)
160 {
161 if (ttyfd >= 0) {
162 close(ttyfd);
163 ttyfd = -1;
164 }
165 if (!iflag) {
166 setsignal(SIGTSTP);
167 setsignal(SIGTTOU);
168 setsignal(SIGTTIN);
169 jobctl = 1;
170 return;
171 }
172 out2fmt_flush("sh: can't access tty; job control turned off\n");
173 mflag = 0;
174 }
175
176 void
setjobctl(int on)177 setjobctl(int on)
178 {
179 int i;
180
181 if (on == jobctl || rootshell == 0)
182 return;
183 if (on) {
184 if (ttyfd != -1)
185 close(ttyfd);
186 if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
187 i = 0;
188 while (i <= 2 && !isatty(i))
189 i++;
190 if (i > 2 ||
191 (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
192 jobctl_notty();
193 return;
194 }
195 }
196 if (ttyfd < 10) {
197 /*
198 * Keep our TTY file descriptor out of the way of
199 * the user's redirections.
200 */
201 if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
202 jobctl_notty();
203 return;
204 }
205 close(ttyfd);
206 ttyfd = i;
207 }
208 do { /* while we are in the background */
209 initialpgrp = tcgetpgrp(ttyfd);
210 if (initialpgrp < 0) {
211 jobctl_notty();
212 return;
213 }
214 if (initialpgrp != getpgrp()) {
215 if (!iflag) {
216 initialpgrp = -1;
217 jobctl_notty();
218 return;
219 }
220 kill(0, SIGTTIN);
221 continue;
222 }
223 } while (0);
224 setsignal(SIGTSTP);
225 setsignal(SIGTTOU);
226 setsignal(SIGTTIN);
227 setpgid(0, rootpid);
228 tcsetpgrp(ttyfd, rootpid);
229 } else { /* turning job control off */
230 setpgid(0, initialpgrp);
231 if (ttyfd >= 0) {
232 tcsetpgrp(ttyfd, initialpgrp);
233 close(ttyfd);
234 ttyfd = -1;
235 }
236 setsignal(SIGTSTP);
237 setsignal(SIGTTOU);
238 setsignal(SIGTTIN);
239 }
240 jobctl = on;
241 }
242 #endif
243
244
245 #if JOBS
246 int
fgcmd(int argc __unused,char ** argv __unused)247 fgcmd(int argc __unused, char **argv __unused)
248 {
249 struct job *jp;
250 pid_t pgrp;
251 int status;
252
253 nextopt("");
254 jp = getjob(*argptr);
255 if (jp->jobctl == 0)
256 error("job not created under job control");
257 printjobcmd(jp);
258 flushout(&output);
259 pgrp = jp->ps[0].pid;
260 if (ttyfd >= 0)
261 tcsetpgrp(ttyfd, pgrp);
262 restartjob(jp);
263 jp->foreground = 1;
264 INTOFF;
265 status = waitforjob(jp, (int *)NULL);
266 INTON;
267 return status;
268 }
269
270
271 int
bgcmd(int argc __unused,char ** argv __unused)272 bgcmd(int argc __unused, char **argv __unused)
273 {
274 struct job *jp;
275
276 nextopt("");
277 do {
278 jp = getjob(*argptr);
279 if (jp->jobctl == 0)
280 error("job not created under job control");
281 if (jp->state == JOBDONE)
282 continue;
283 restartjob(jp);
284 jp->foreground = 0;
285 out1fmt("[%td] ", jp - jobtab + 1);
286 printjobcmd(jp);
287 } while (*argptr != NULL && *++argptr != NULL);
288 return 0;
289 }
290
291
292 static void
restartjob(struct job * jp)293 restartjob(struct job *jp)
294 {
295 struct procstat *ps;
296 int i;
297
298 if (jp->state == JOBDONE)
299 return;
300 setcurjob(jp);
301 INTOFF;
302 kill(-jp->ps[0].pid, SIGCONT);
303 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
304 if (WIFSTOPPED(ps->status)) {
305 ps->status = -1;
306 jp->state = 0;
307 }
308 }
309 INTON;
310 }
311 #endif
312
313
314 int
jobscmd(int argc __unused,char * argv[]__unused)315 jobscmd(int argc __unused, char *argv[] __unused)
316 {
317 char *id;
318 int ch, mode;
319
320 mode = SHOWJOBS_DEFAULT;
321 while ((ch = nextopt("lps")) != '\0') {
322 switch (ch) {
323 case 'l':
324 mode = SHOWJOBS_VERBOSE;
325 break;
326 case 'p':
327 mode = SHOWJOBS_PGIDS;
328 break;
329 case 's':
330 mode = SHOWJOBS_PIDS;
331 break;
332 }
333 }
334
335 if (*argptr == NULL)
336 showjobs(0, mode);
337 else
338 while ((id = *argptr++) != NULL)
339 showjob(getjob(id), mode);
340
341 return (0);
342 }
343
getjobstatus(const struct job * jp)344 static int getjobstatus(const struct job *jp)
345 {
346 int i, status;
347
348 if (!jp->pipefail)
349 return (jp->ps[jp->nprocs - 1].status);
350 for (i = jp->nprocs - 1; i >= 0; i--) {
351 status = jp->ps[i].status;
352 if (status != 0)
353 return (status);
354 }
355 return (0);
356 }
357
358 static void
printjobcmd(struct job * jp)359 printjobcmd(struct job *jp)
360 {
361 struct procstat *ps;
362 int i;
363
364 for (ps = jp->ps, i = jp->nprocs ; --i >= 0 ; ps++) {
365 out1str(ps->cmd);
366 if (i > 0)
367 out1str(" | ");
368 }
369 out1c('\n');
370 }
371
372 static void
showjob(struct job * jp,int mode)373 showjob(struct job *jp, int mode)
374 {
375 char s[64];
376 char statebuf[16];
377 const char *statestr, *coredump;
378 struct procstat *ps;
379 struct job *j;
380 int col, curr, i, jobno, prev, procno, status;
381 char c;
382
383 procno = (mode == SHOWJOBS_PGIDS) ? 1 : jp->nprocs;
384 jobno = jp - jobtab + 1;
385 curr = prev = 0;
386 #if JOBS
387 if ((j = getcurjob(NULL)) != NULL) {
388 curr = j - jobtab + 1;
389 if ((j = getcurjob(j)) != NULL)
390 prev = j - jobtab + 1;
391 }
392 #endif
393 coredump = "";
394 status = getjobstatus(jp);
395 if (jp->state == 0) {
396 statestr = "Running";
397 #if JOBS
398 } else if (jp->state == JOBSTOPPED) {
399 ps = jp->ps + jp->nprocs - 1;
400 while (!WIFSTOPPED(ps->status) && ps > jp->ps)
401 ps--;
402 if (WIFSTOPPED(ps->status))
403 i = WSTOPSIG(ps->status);
404 else
405 i = -1;
406 statestr = strsignal(i);
407 if (statestr == NULL)
408 statestr = "Suspended";
409 #endif
410 } else if (WIFEXITED(status)) {
411 if (WEXITSTATUS(status) == 0)
412 statestr = "Done";
413 else {
414 fmtstr(statebuf, sizeof(statebuf), "Done(%d)",
415 WEXITSTATUS(status));
416 statestr = statebuf;
417 }
418 } else {
419 i = WTERMSIG(status);
420 statestr = strsignal(i);
421 if (statestr == NULL)
422 statestr = "Unknown signal";
423 if (WCOREDUMP(status))
424 coredump = " (core dumped)";
425 }
426
427 for (ps = jp->ps ; procno > 0 ; ps++, procno--) { /* for each process */
428 if (mode == SHOWJOBS_PIDS || mode == SHOWJOBS_PGIDS) {
429 out1fmt("%d\n", (int)ps->pid);
430 continue;
431 }
432 if (mode != SHOWJOBS_VERBOSE && ps != jp->ps)
433 continue;
434 if (jobno == curr && ps == jp->ps)
435 c = '+';
436 else if (jobno == prev && ps == jp->ps)
437 c = '-';
438 else
439 c = ' ';
440 if (ps == jp->ps)
441 fmtstr(s, 64, "[%d] %c ", jobno, c);
442 else
443 fmtstr(s, 64, " %c ", c);
444 out1str(s);
445 col = strlen(s);
446 if (mode == SHOWJOBS_VERBOSE) {
447 fmtstr(s, 64, "%d ", (int)ps->pid);
448 out1str(s);
449 col += strlen(s);
450 }
451 if (ps == jp->ps) {
452 out1str(statestr);
453 out1str(coredump);
454 col += strlen(statestr) + strlen(coredump);
455 }
456 do {
457 out1c(' ');
458 col++;
459 } while (col < 30);
460 if (mode == SHOWJOBS_VERBOSE) {
461 out1str(ps->cmd);
462 out1c('\n');
463 } else
464 printjobcmd(jp);
465 }
466 }
467
468 /*
469 * Print a list of jobs. If "change" is nonzero, only print jobs whose
470 * statuses have changed since the last call to showjobs.
471 *
472 * If the shell is interrupted in the process of creating a job, the
473 * result may be a job structure containing zero processes. Such structures
474 * will be freed here.
475 */
476
477 void
showjobs(int change,int mode)478 showjobs(int change, int mode)
479 {
480 int jobno;
481 struct job *jp;
482
483 TRACE(("showjobs(%d) called\n", change));
484 checkzombies();
485 for (jobno = 1, jp = jobtab ; jobno <= njobs ; jobno++, jp++) {
486 if (! jp->used)
487 continue;
488 if (jp->nprocs == 0) {
489 freejob(jp);
490 continue;
491 }
492 if (change && ! jp->changed)
493 continue;
494 showjob(jp, mode);
495 if (mode == SHOWJOBS_DEFAULT || mode == SHOWJOBS_VERBOSE) {
496 jp->changed = 0;
497 /* Hack: discard jobs for which $! has not been
498 * referenced in interactive mode when they terminate.
499 */
500 if (jp->state == JOBDONE && !jp->remembered &&
501 (iflag || jp != bgjob)) {
502 freejob(jp);
503 }
504 }
505 }
506 }
507
508
509 /*
510 * Mark a job structure as unused.
511 */
512
513 static void
freejob(struct job * jp)514 freejob(struct job *jp)
515 {
516 struct procstat *ps;
517 int i;
518
519 INTOFF;
520 if (bgjob == jp)
521 bgjob = NULL;
522 for (i = jp->nprocs, ps = jp->ps ; --i >= 0 ; ps++) {
523 if (ps->cmd != nullstr)
524 ckfree(ps->cmd);
525 }
526 if (jp->ps != &jp->ps0)
527 ckfree(jp->ps);
528 jp->used = 0;
529 #if JOBS
530 deljob(jp);
531 #endif
532 INTON;
533 }
534
535
536
537 int
waitcmd(int argc __unused,char ** argv __unused)538 waitcmd(int argc __unused, char **argv __unused)
539 {
540 struct job *job;
541 int retval;
542
543 nextopt("");
544 if (*argptr == NULL)
545 return (waitcmdloop(NULL));
546
547 do {
548 job = getjob_nonotfound(*argptr);
549 if (job == NULL)
550 retval = 127;
551 else
552 retval = waitcmdloop(job);
553 argptr++;
554 } while (*argptr != NULL);
555
556 return (retval);
557 }
558
559 static int
waitcmdloop(struct job * job)560 waitcmdloop(struct job *job)
561 {
562 int status, retval, sig;
563 struct job *jp;
564
565 /*
566 * Loop until a process is terminated or stopped, or a SIGINT is
567 * received.
568 */
569
570 do {
571 if (job != NULL) {
572 if (job->state == JOBDONE) {
573 status = getjobstatus(job);
574 if (WIFEXITED(status))
575 retval = WEXITSTATUS(status);
576 else
577 retval = WTERMSIG(status) + 128;
578 if (! iflag || ! job->changed)
579 freejob(job);
580 else {
581 job->remembered = 0;
582 if (job == bgjob)
583 bgjob = NULL;
584 }
585 return retval;
586 }
587 } else {
588 for (jp = jobtab ; jp < jobtab + njobs; jp++)
589 if (jp->used && jp->state == JOBDONE) {
590 if (! iflag || ! jp->changed)
591 freejob(jp);
592 else {
593 jp->remembered = 0;
594 if (jp == bgjob)
595 bgjob = NULL;
596 }
597 }
598 for (jp = jobtab ; ; jp++) {
599 if (jp >= jobtab + njobs) { /* no running procs */
600 return 0;
601 }
602 if (jp->used && jp->state == 0)
603 break;
604 }
605 }
606 } while (dowait(DOWAIT_BLOCK | DOWAIT_SIG, (struct job *)NULL) != -1);
607
608 sig = pendingsig_waitcmd;
609 pendingsig_waitcmd = 0;
610 return sig + 128;
611 }
612
613
614
615 int
jobidcmd(int argc __unused,char ** argv __unused)616 jobidcmd(int argc __unused, char **argv __unused)
617 {
618 struct job *jp;
619 int i;
620
621 nextopt("");
622 jp = getjob(*argptr);
623 for (i = 0 ; i < jp->nprocs ; ) {
624 out1fmt("%d", (int)jp->ps[i].pid);
625 out1c(++i < jp->nprocs? ' ' : '\n');
626 }
627 return 0;
628 }
629
630
631
632 /*
633 * Convert a job name to a job structure.
634 */
635
636 static struct job *
getjob_nonotfound(const char * name)637 getjob_nonotfound(const char *name)
638 {
639 int jobno;
640 struct job *found, *jp;
641 size_t namelen;
642 pid_t pid;
643 int i;
644
645 if (name == NULL) {
646 #if JOBS
647 name = "%+";
648 #else
649 error("No current job");
650 #endif
651 }
652 if (name[0] == '%') {
653 if (is_digit(name[1])) {
654 jobno = number(name + 1);
655 if (jobno > 0 && jobno <= njobs
656 && jobtab[jobno - 1].used != 0)
657 return &jobtab[jobno - 1];
658 #if JOBS
659 } else if ((name[1] == '%' || name[1] == '+') &&
660 name[2] == '\0') {
661 if ((jp = getcurjob(NULL)) == NULL)
662 error("No current job");
663 return (jp);
664 } else if (name[1] == '-' && name[2] == '\0') {
665 if ((jp = getcurjob(NULL)) == NULL ||
666 (jp = getcurjob(jp)) == NULL)
667 error("No previous job");
668 return (jp);
669 #endif
670 } else if (name[1] == '?') {
671 found = NULL;
672 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
673 if (jp->used && jp->nprocs > 0
674 && strstr(jp->ps[0].cmd, name + 2) != NULL) {
675 if (found)
676 error("%s: ambiguous", name);
677 found = jp;
678 }
679 }
680 if (found != NULL)
681 return (found);
682 } else {
683 namelen = strlen(name);
684 found = NULL;
685 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
686 if (jp->used && jp->nprocs > 0
687 && strncmp(jp->ps[0].cmd, name + 1,
688 namelen - 1) == 0) {
689 if (found)
690 error("%s: ambiguous", name);
691 found = jp;
692 }
693 }
694 if (found)
695 return found;
696 }
697 } else if (is_number(name)) {
698 pid = (pid_t)number(name);
699 for (jp = jobtab, i = njobs ; --i >= 0 ; jp++) {
700 if (jp->used && jp->nprocs > 0
701 && jp->ps[jp->nprocs - 1].pid == pid)
702 return jp;
703 }
704 }
705 return NULL;
706 }
707
708
709 static struct job *
getjob(const char * name)710 getjob(const char *name)
711 {
712 struct job *jp;
713
714 jp = getjob_nonotfound(name);
715 if (jp == NULL)
716 error("No such job: %s", name);
717 return (jp);
718 }
719
720
721 int
killjob(const char * name,int sig)722 killjob(const char *name, int sig)
723 {
724 struct job *jp;
725 int i, ret;
726
727 jp = getjob(name);
728 if (jp->state == JOBDONE)
729 return 0;
730 if (jp->jobctl)
731 return kill(-jp->ps[0].pid, sig);
732 ret = -1;
733 errno = ESRCH;
734 for (i = 0; i < jp->nprocs; i++)
735 if (jp->ps[i].status == -1 || WIFSTOPPED(jp->ps[i].status)) {
736 if (kill(jp->ps[i].pid, sig) == 0)
737 ret = 0;
738 } else
739 ret = 0;
740 return ret;
741 }
742
743 /*
744 * Return a new job structure,
745 */
746
747 struct job *
makejob(union node * node __unused,int nprocs)748 makejob(union node *node __unused, int nprocs)
749 {
750 int i;
751 struct job *jp;
752
753 for (i = njobs, jp = jobtab ; ; jp++) {
754 if (--i < 0) {
755 INTOFF;
756 if (njobs == 0) {
757 jobtab = ckmalloc(4 * sizeof jobtab[0]);
758 #if JOBS
759 jobmru = NULL;
760 #endif
761 } else {
762 jp = ckmalloc((njobs + 4) * sizeof jobtab[0]);
763 memcpy(jp, jobtab, njobs * sizeof jp[0]);
764 #if JOBS
765 /* Relocate `next' pointers and list head */
766 if (jobmru != NULL)
767 jobmru = &jp[jobmru - jobtab];
768 for (i = 0; i < njobs; i++)
769 if (jp[i].next != NULL)
770 jp[i].next = &jp[jp[i].next -
771 jobtab];
772 #endif
773 if (bgjob != NULL)
774 bgjob = &jp[bgjob - jobtab];
775 /* Relocate `ps' pointers */
776 for (i = 0; i < njobs; i++)
777 if (jp[i].ps == &jobtab[i].ps0)
778 jp[i].ps = &jp[i].ps0;
779 ckfree(jobtab);
780 jobtab = jp;
781 }
782 jp = jobtab + njobs;
783 for (i = 4 ; --i >= 0 ; jobtab[njobs++].used = 0)
784 ;
785 INTON;
786 break;
787 }
788 if (jp->used == 0)
789 break;
790 }
791 INTOFF;
792 jp->state = 0;
793 jp->used = 1;
794 jp->changed = 0;
795 jp->nprocs = 0;
796 jp->foreground = 0;
797 jp->remembered = 0;
798 jp->pipefail = pipefailflag;
799 #if JOBS
800 jp->jobctl = jobctl;
801 jp->next = NULL;
802 #endif
803 if (nprocs > 1) {
804 jp->ps = ckmalloc(nprocs * sizeof (struct procstat));
805 } else {
806 jp->ps = &jp->ps0;
807 }
808 INTON;
809 TRACE(("makejob(%p, %d) returns %%%td\n", (void *)node, nprocs,
810 jp - jobtab + 1));
811 return jp;
812 }
813
814 #if JOBS
815 static void
setcurjob(struct job * cj)816 setcurjob(struct job *cj)
817 {
818 struct job *jp, *prev;
819
820 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
821 if (jp == cj) {
822 if (prev != NULL)
823 prev->next = jp->next;
824 else
825 jobmru = jp->next;
826 jp->next = jobmru;
827 jobmru = cj;
828 return;
829 }
830 }
831 cj->next = jobmru;
832 jobmru = cj;
833 }
834
835 static void
deljob(struct job * j)836 deljob(struct job *j)
837 {
838 struct job *jp, *prev;
839
840 for (prev = NULL, jp = jobmru; jp != NULL; prev = jp, jp = jp->next) {
841 if (jp == j) {
842 if (prev != NULL)
843 prev->next = jp->next;
844 else
845 jobmru = jp->next;
846 return;
847 }
848 }
849 }
850
851 /*
852 * Return the most recently used job that isn't `nj', and preferably one
853 * that is stopped.
854 */
855 static struct job *
getcurjob(struct job * nj)856 getcurjob(struct job *nj)
857 {
858 struct job *jp;
859
860 /* Try to find a stopped one.. */
861 for (jp = jobmru; jp != NULL; jp = jp->next)
862 if (jp->used && jp != nj && jp->state == JOBSTOPPED)
863 return (jp);
864 /* Otherwise the most recently used job that isn't `nj' */
865 for (jp = jobmru; jp != NULL; jp = jp->next)
866 if (jp->used && jp != nj)
867 return (jp);
868
869 return (NULL);
870 }
871
872 #endif
873
874 /*
875 * Fork of a subshell. If we are doing job control, give the subshell its
876 * own process group. Jp is a job structure that the job is to be added to.
877 * N is the command that will be evaluated by the child. Both jp and n may
878 * be NULL. The mode parameter can be one of the following:
879 * FORK_FG - Fork off a foreground process.
880 * FORK_BG - Fork off a background process.
881 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
882 * process group even if job control is on.
883 *
884 * When job control is turned off, background processes have their standard
885 * input redirected to /dev/null (except for the second and later processes
886 * in a pipeline).
887 */
888
889 pid_t
forkshell(struct job * jp,union node * n,int mode)890 forkshell(struct job *jp, union node *n, int mode)
891 {
892 pid_t pid;
893 pid_t pgrp;
894
895 TRACE(("forkshell(%%%td, %p, %d) called\n", jp - jobtab, (void *)n,
896 mode));
897 INTOFF;
898 if (mode == FORK_BG && (jp == NULL || jp->nprocs == 0))
899 checkzombies();
900 flushall();
901 pid = fork();
902 if (pid == -1) {
903 TRACE(("Fork failed, errno=%d\n", errno));
904 INTON;
905 error("Cannot fork: %s", strerror(errno));
906 }
907 if (pid == 0) {
908 struct job *p;
909 int wasroot;
910 int i;
911
912 TRACE(("Child shell %d\n", (int)getpid()));
913 wasroot = rootshell;
914 rootshell = 0;
915 handler = &main_handler;
916 closescript();
917 INTON;
918 forcelocal = 0;
919 clear_traps();
920 #if JOBS
921 jobctl = 0; /* do job control only in root shell */
922 if (wasroot && mode != FORK_NOJOB && mflag) {
923 if (jp == NULL || jp->nprocs == 0)
924 pgrp = getpid();
925 else
926 pgrp = jp->ps[0].pid;
927 if (setpgid(0, pgrp) == 0 && mode == FORK_FG &&
928 ttyfd >= 0) {
929 /*
930 * Each process in a pipeline must have the tty
931 * pgrp set before running its code.
932 * Only for pipelines of three or more processes
933 * could this be reduced to two calls.
934 */
935 if (tcsetpgrp(ttyfd, pgrp) < 0)
936 error("tcsetpgrp failed, errno=%d", errno);
937 }
938 setsignal(SIGTSTP);
939 setsignal(SIGTTOU);
940 } else if (mode == FORK_BG) {
941 ignoresig(SIGINT);
942 ignoresig(SIGQUIT);
943 if ((jp == NULL || jp->nprocs == 0) &&
944 ! fd0_redirected_p ()) {
945 close(0);
946 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
947 error("cannot open %s: %s",
948 _PATH_DEVNULL, strerror(errno));
949 }
950 }
951 #else
952 if (mode == FORK_BG) {
953 ignoresig(SIGINT);
954 ignoresig(SIGQUIT);
955 if ((jp == NULL || jp->nprocs == 0) &&
956 ! fd0_redirected_p ()) {
957 close(0);
958 if (open(_PATH_DEVNULL, O_RDONLY) != 0)
959 error("cannot open %s: %s",
960 _PATH_DEVNULL, strerror(errno));
961 }
962 }
963 #endif
964 INTOFF;
965 for (i = njobs, p = jobtab ; --i >= 0 ; p++)
966 if (p->used)
967 freejob(p);
968 INTON;
969 if (wasroot && iflag) {
970 setsignal(SIGINT);
971 setsignal(SIGQUIT);
972 setsignal(SIGTERM);
973 }
974 return pid;
975 }
976 if (rootshell && mode != FORK_NOJOB && mflag) {
977 if (jp == NULL || jp->nprocs == 0)
978 pgrp = pid;
979 else
980 pgrp = jp->ps[0].pid;
981 setpgid(pid, pgrp);
982 }
983 if (mode == FORK_BG) {
984 if (bgjob != NULL && bgjob->state == JOBDONE &&
985 !bgjob->remembered && !iflag)
986 freejob(bgjob);
987 backgndpid = pid; /* set $! */
988 bgjob = jp;
989 }
990 if (jp) {
991 struct procstat *ps = &jp->ps[jp->nprocs++];
992 ps->pid = pid;
993 ps->status = -1;
994 ps->cmd = nullstr;
995 if (iflag && rootshell && n)
996 ps->cmd = commandtext(n);
997 jp->foreground = mode == FORK_FG;
998 #if JOBS
999 setcurjob(jp);
1000 #endif
1001 }
1002 INTON;
1003 TRACE(("In parent shell: child = %d\n", (int)pid));
1004 return pid;
1005 }
1006
1007
1008 pid_t
vforkexecshell(struct job * jp,char ** argv,char ** envp,const char * path,int idx,int pip[2])1009 vforkexecshell(struct job *jp, char **argv, char **envp, const char *path, int idx, int pip[2])
1010 {
1011 pid_t pid;
1012 struct jmploc jmploc;
1013 struct jmploc *savehandler;
1014 int inton;
1015
1016 TRACE(("vforkexecshell(%%%td, %s, %p) called\n", jp - jobtab, argv[0],
1017 (void *)pip));
1018 inton = is_int_on();
1019 INTOFF;
1020 flushall();
1021 savehandler = handler;
1022 pid = vfork();
1023 if (pid == -1) {
1024 TRACE(("Vfork failed, errno=%d\n", errno));
1025 INTON;
1026 error("Cannot fork: %s", strerror(errno));
1027 }
1028 if (pid == 0) {
1029 TRACE(("Child shell %d\n", (int)getpid()));
1030 if (setjmp(jmploc.loc))
1031 _exit(exitstatus);
1032 if (pip != NULL) {
1033 close(pip[0]);
1034 if (pip[1] != 1) {
1035 dup2(pip[1], 1);
1036 close(pip[1]);
1037 }
1038 }
1039 handler = &jmploc;
1040 shellexec(argv, envp, path, idx);
1041 }
1042 handler = savehandler;
1043 if (jp) {
1044 struct procstat *ps = &jp->ps[jp->nprocs++];
1045 ps->pid = pid;
1046 ps->status = -1;
1047 ps->cmd = nullstr;
1048 jp->foreground = 1;
1049 #if JOBS
1050 setcurjob(jp);
1051 #endif
1052 }
1053 SETINTON(inton);
1054 TRACE(("In parent shell: child = %d\n", (int)pid));
1055 return pid;
1056 }
1057
1058
1059 /*
1060 * Wait for job to finish.
1061 *
1062 * Under job control we have the problem that while a child process is
1063 * running interrupts generated by the user are sent to the child but not
1064 * to the shell. This means that an infinite loop started by an inter-
1065 * active user may be hard to kill. With job control turned off, an
1066 * interactive user may place an interactive program inside a loop. If
1067 * the interactive program catches interrupts, the user doesn't want
1068 * these interrupts to also abort the loop. The approach we take here
1069 * is to have the shell ignore interrupt signals while waiting for a
1070 * foreground process to terminate, and then send itself an interrupt
1071 * signal if the child process was terminated by an interrupt signal.
1072 * Unfortunately, some programs want to do a bit of cleanup and then
1073 * exit on interrupt; unless these processes terminate themselves by
1074 * sending a signal to themselves (instead of calling exit) they will
1075 * confuse this approach.
1076 */
1077
1078 int
waitforjob(struct job * jp,int * signaled)1079 waitforjob(struct job *jp, int *signaled)
1080 {
1081 #if JOBS
1082 int propagate_int = jp->jobctl && jp->foreground;
1083 #endif
1084 int status;
1085 int st;
1086
1087 INTOFF;
1088 TRACE(("waitforjob(%%%td) called\n", jp - jobtab + 1));
1089 while (jp->state == 0)
1090 if (dowait(DOWAIT_BLOCK | (Tflag ? DOWAIT_SIG |
1091 DOWAIT_SIG_TRAP : 0), jp) == -1)
1092 dotrap();
1093 #if JOBS
1094 if (jp->jobctl) {
1095 if (ttyfd >= 0 && tcsetpgrp(ttyfd, rootpid) < 0)
1096 error("tcsetpgrp failed, errno=%d\n", errno);
1097 }
1098 if (jp->state == JOBSTOPPED)
1099 setcurjob(jp);
1100 #endif
1101 status = getjobstatus(jp);
1102 if (signaled != NULL)
1103 *signaled = WIFSIGNALED(status);
1104 /* convert to 8 bits */
1105 if (WIFEXITED(status))
1106 st = WEXITSTATUS(status);
1107 #if JOBS
1108 else if (WIFSTOPPED(status))
1109 st = WSTOPSIG(status) + 128;
1110 #endif
1111 else
1112 st = WTERMSIG(status) + 128;
1113 if (! JOBS || jp->state == JOBDONE)
1114 freejob(jp);
1115 if (int_pending()) {
1116 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGINT)
1117 CLEAR_PENDING_INT;
1118 }
1119 #if JOBS
1120 else if (rootshell && propagate_int &&
1121 WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
1122 kill(getpid(), SIGINT);
1123 #endif
1124 INTON;
1125 return st;
1126 }
1127
1128
1129 static void
dummy_handler(int sig __unused)1130 dummy_handler(int sig __unused)
1131 {
1132 }
1133
1134 /*
1135 * Wait for a process to terminate.
1136 */
1137
1138 static pid_t
dowait(int mode,struct job * job)1139 dowait(int mode, struct job *job)
1140 {
1141 struct sigaction sa, osa;
1142 sigset_t mask, omask;
1143 pid_t pid;
1144 int status;
1145 struct procstat *sp;
1146 struct job *jp;
1147 struct job *thisjob;
1148 const char *sigstr;
1149 int done;
1150 int stopped;
1151 int sig;
1152 int coredump;
1153 int wflags;
1154 int restore_sigchld;
1155
1156 TRACE(("dowait(%d, %p) called\n", mode, job));
1157 restore_sigchld = 0;
1158 if ((mode & DOWAIT_SIG) != 0) {
1159 sigfillset(&mask);
1160 sigprocmask(SIG_BLOCK, &mask, &omask);
1161 INTOFF;
1162 if (!issigchldtrapped()) {
1163 restore_sigchld = 1;
1164 sa.sa_handler = dummy_handler;
1165 sa.sa_flags = 0;
1166 sigemptyset(&sa.sa_mask);
1167 sigaction(SIGCHLD, &sa, &osa);
1168 }
1169 }
1170 do {
1171 #if JOBS
1172 if (iflag)
1173 wflags = WUNTRACED | WCONTINUED;
1174 else
1175 #endif
1176 wflags = 0;
1177 if ((mode & (DOWAIT_BLOCK | DOWAIT_SIG)) != DOWAIT_BLOCK)
1178 wflags |= WNOHANG;
1179 pid = wait3(&status, wflags, (struct rusage *)NULL);
1180 TRACE(("wait returns %d, status=%d\n", (int)pid, status));
1181 if (pid == 0 && (mode & DOWAIT_SIG) != 0) {
1182 pid = -1;
1183 if (((mode & DOWAIT_SIG_TRAP) != 0 ?
1184 pendingsig : pendingsig_waitcmd) != 0) {
1185 errno = EINTR;
1186 break;
1187 }
1188 sigsuspend(&omask);
1189 if (int_pending())
1190 break;
1191 }
1192 } while (pid == -1 && errno == EINTR);
1193 if (pid == -1 && errno == ECHILD && job != NULL)
1194 job->state = JOBDONE;
1195 if ((mode & DOWAIT_SIG) != 0) {
1196 if (restore_sigchld)
1197 sigaction(SIGCHLD, &osa, NULL);
1198 sigprocmask(SIG_SETMASK, &omask, NULL);
1199 INTON;
1200 }
1201 if (pid <= 0)
1202 return pid;
1203 INTOFF;
1204 thisjob = NULL;
1205 for (jp = jobtab ; jp < jobtab + njobs ; jp++) {
1206 if (jp->used && jp->nprocs > 0) {
1207 done = 1;
1208 stopped = 1;
1209 for (sp = jp->ps ; sp < jp->ps + jp->nprocs ; sp++) {
1210 if (sp->pid == -1)
1211 continue;
1212 if (sp->pid == pid && (sp->status == -1 ||
1213 WIFSTOPPED(sp->status))) {
1214 TRACE(("Changing status of proc %d from 0x%x to 0x%x\n",
1215 (int)pid, sp->status,
1216 status));
1217 if (WIFCONTINUED(status)) {
1218 sp->status = -1;
1219 jp->state = 0;
1220 } else
1221 sp->status = status;
1222 thisjob = jp;
1223 }
1224 if (sp->status == -1)
1225 stopped = 0;
1226 else if (WIFSTOPPED(sp->status))
1227 done = 0;
1228 }
1229 if (stopped) { /* stopped or done */
1230 int state = done? JOBDONE : JOBSTOPPED;
1231 if (jp->state != state) {
1232 TRACE(("Job %td: changing state from %d to %d\n", jp - jobtab + 1, jp->state, state));
1233 jp->state = state;
1234 if (jp != job) {
1235 if (done && !jp->remembered &&
1236 !iflag && jp != bgjob)
1237 freejob(jp);
1238 #if JOBS
1239 else if (done)
1240 deljob(jp);
1241 #endif
1242 }
1243 }
1244 }
1245 }
1246 }
1247 INTON;
1248 if (!thisjob || thisjob->state == 0)
1249 ;
1250 else if ((!rootshell || !iflag || thisjob == job) &&
1251 thisjob->foreground && thisjob->state != JOBSTOPPED) {
1252 sig = 0;
1253 coredump = 0;
1254 for (sp = thisjob->ps; sp < thisjob->ps + thisjob->nprocs; sp++)
1255 if (WIFSIGNALED(sp->status)) {
1256 sig = WTERMSIG(sp->status);
1257 coredump = WCOREDUMP(sp->status);
1258 }
1259 if (sig > 0 && sig != SIGINT && sig != SIGPIPE) {
1260 sigstr = strsignal(sig);
1261 if (sigstr != NULL)
1262 out2str(sigstr);
1263 else
1264 out2str("Unknown signal");
1265 if (coredump)
1266 out2str(" (core dumped)");
1267 out2c('\n');
1268 flushout(out2);
1269 }
1270 } else {
1271 TRACE(("Not printing status, rootshell=%d, job=%p\n", rootshell, job));
1272 thisjob->changed = 1;
1273 }
1274 return pid;
1275 }
1276
1277
1278
1279 /*
1280 * return 1 if there are stopped jobs, otherwise 0
1281 */
1282 int job_warning = 0;
1283 int
stoppedjobs(void)1284 stoppedjobs(void)
1285 {
1286 int jobno;
1287 struct job *jp;
1288
1289 if (job_warning)
1290 return (0);
1291 for (jobno = 1, jp = jobtab; jobno <= njobs; jobno++, jp++) {
1292 if (jp->used == 0)
1293 continue;
1294 if (jp->state == JOBSTOPPED) {
1295 out2fmt_flush("You have stopped jobs.\n");
1296 job_warning = 2;
1297 return (1);
1298 }
1299 }
1300
1301 return (0);
1302 }
1303
1304
1305 static void
checkzombies(void)1306 checkzombies(void)
1307 {
1308 while (njobs > 0 && dowait(0, NULL) > 0)
1309 ;
1310 }
1311
1312
1313 int
backgndpidset(void)1314 backgndpidset(void)
1315 {
1316 return backgndpid != -1;
1317 }
1318
1319
1320 pid_t
backgndpidval(void)1321 backgndpidval(void)
1322 {
1323 if (bgjob != NULL && !forcelocal)
1324 bgjob->remembered = 1;
1325 return backgndpid;
1326 }
1327
1328 /*
1329 * Return a string identifying a command (to be printed by the
1330 * jobs command.
1331 */
1332
1333 static char *cmdnextc;
1334 static int cmdnleft;
1335 #define MAXCMDTEXT 200
1336
1337 char *
commandtext(union node * n)1338 commandtext(union node *n)
1339 {
1340 char *name;
1341
1342 cmdnextc = name = ckmalloc(MAXCMDTEXT);
1343 cmdnleft = MAXCMDTEXT - 4;
1344 cmdtxt(n);
1345 *cmdnextc = '\0';
1346 return name;
1347 }
1348
1349
1350 static void
cmdtxtdogroup(union node * n)1351 cmdtxtdogroup(union node *n)
1352 {
1353 cmdputs("; do ");
1354 cmdtxt(n);
1355 cmdputs("; done");
1356 }
1357
1358
1359 static void
cmdtxtredir(union node * n,const char * op,int deffd)1360 cmdtxtredir(union node *n, const char *op, int deffd)
1361 {
1362 char s[2];
1363
1364 if (n->nfile.fd != deffd) {
1365 s[0] = n->nfile.fd + '0';
1366 s[1] = '\0';
1367 cmdputs(s);
1368 }
1369 cmdputs(op);
1370 if (n->type == NTOFD || n->type == NFROMFD) {
1371 if (n->ndup.dupfd >= 0)
1372 s[0] = n->ndup.dupfd + '0';
1373 else
1374 s[0] = '-';
1375 s[1] = '\0';
1376 cmdputs(s);
1377 } else {
1378 cmdtxt(n->nfile.fname);
1379 }
1380 }
1381
1382
1383 static void
cmdtxt(union node * n)1384 cmdtxt(union node *n)
1385 {
1386 union node *np;
1387 struct nodelist *lp;
1388
1389 if (n == NULL)
1390 return;
1391 switch (n->type) {
1392 case NSEMI:
1393 cmdtxt(n->nbinary.ch1);
1394 cmdputs("; ");
1395 cmdtxt(n->nbinary.ch2);
1396 break;
1397 case NAND:
1398 cmdtxt(n->nbinary.ch1);
1399 cmdputs(" && ");
1400 cmdtxt(n->nbinary.ch2);
1401 break;
1402 case NOR:
1403 cmdtxt(n->nbinary.ch1);
1404 cmdputs(" || ");
1405 cmdtxt(n->nbinary.ch2);
1406 break;
1407 case NPIPE:
1408 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1409 cmdtxt(lp->n);
1410 if (lp->next)
1411 cmdputs(" | ");
1412 }
1413 break;
1414 case NSUBSHELL:
1415 cmdputs("(");
1416 cmdtxt(n->nredir.n);
1417 cmdputs(")");
1418 break;
1419 case NREDIR:
1420 case NBACKGND:
1421 cmdtxt(n->nredir.n);
1422 break;
1423 case NIF:
1424 cmdputs("if ");
1425 cmdtxt(n->nif.test);
1426 cmdputs("; then ");
1427 cmdtxt(n->nif.ifpart);
1428 cmdputs("...");
1429 break;
1430 case NWHILE:
1431 cmdputs("while ");
1432 cmdtxt(n->nbinary.ch1);
1433 cmdtxtdogroup(n->nbinary.ch2);
1434 break;
1435 case NUNTIL:
1436 cmdputs("until ");
1437 cmdtxt(n->nbinary.ch1);
1438 cmdtxtdogroup(n->nbinary.ch2);
1439 break;
1440 case NFOR:
1441 cmdputs("for ");
1442 cmdputs(n->nfor.var);
1443 cmdputs(" in ...");
1444 break;
1445 case NCASE:
1446 cmdputs("case ");
1447 cmdputs(n->ncase.expr->narg.text);
1448 cmdputs(" in ...");
1449 break;
1450 case NDEFUN:
1451 cmdputs(n->narg.text);
1452 cmdputs("() ...");
1453 break;
1454 case NNOT:
1455 cmdputs("! ");
1456 cmdtxt(n->nnot.com);
1457 break;
1458 case NCMD:
1459 for (np = n->ncmd.args ; np ; np = np->narg.next) {
1460 cmdtxt(np);
1461 if (np->narg.next)
1462 cmdputs(" ");
1463 }
1464 for (np = n->ncmd.redirect ; np ; np = np->nfile.next) {
1465 cmdputs(" ");
1466 cmdtxt(np);
1467 }
1468 break;
1469 case NARG:
1470 cmdputs(n->narg.text);
1471 break;
1472 case NTO:
1473 cmdtxtredir(n, ">", 1);
1474 break;
1475 case NAPPEND:
1476 cmdtxtredir(n, ">>", 1);
1477 break;
1478 case NTOFD:
1479 cmdtxtredir(n, ">&", 1);
1480 break;
1481 case NCLOBBER:
1482 cmdtxtredir(n, ">|", 1);
1483 break;
1484 case NFROM:
1485 cmdtxtredir(n, "<", 0);
1486 break;
1487 case NFROMTO:
1488 cmdtxtredir(n, "<>", 0);
1489 break;
1490 case NFROMFD:
1491 cmdtxtredir(n, "<&", 0);
1492 break;
1493 case NHERE:
1494 case NXHERE:
1495 cmdputs("<<...");
1496 break;
1497 default:
1498 cmdputs("???");
1499 break;
1500 }
1501 }
1502
1503
1504
1505 static void
cmdputs(const char * s)1506 cmdputs(const char *s)
1507 {
1508 const char *p;
1509 char *q;
1510 char c;
1511 int subtype = 0;
1512
1513 if (cmdnleft <= 0)
1514 return;
1515 p = s;
1516 q = cmdnextc;
1517 while ((c = *p++) != '\0') {
1518 if (c == CTLESC)
1519 *q++ = *p++;
1520 else if (c == CTLVAR) {
1521 *q++ = '$';
1522 if (--cmdnleft > 0)
1523 *q++ = '{';
1524 subtype = *p++;
1525 if ((subtype & VSTYPE) == VSLENGTH && --cmdnleft > 0)
1526 *q++ = '#';
1527 } else if (c == '=' && subtype != 0) {
1528 *q = "}-+?=##%%\0X"[(subtype & VSTYPE) - VSNORMAL];
1529 if (*q)
1530 q++;
1531 else
1532 cmdnleft++;
1533 if (((subtype & VSTYPE) == VSTRIMLEFTMAX ||
1534 (subtype & VSTYPE) == VSTRIMRIGHTMAX) &&
1535 --cmdnleft > 0)
1536 *q = q[-1], q++;
1537 subtype = 0;
1538 } else if (c == CTLENDVAR) {
1539 *q++ = '}';
1540 } else if (c == CTLBACKQ || c == CTLBACKQ+CTLQUOTE) {
1541 cmdnleft -= 5;
1542 if (cmdnleft > 0) {
1543 *q++ = '$';
1544 *q++ = '(';
1545 *q++ = '.';
1546 *q++ = '.';
1547 *q++ = '.';
1548 *q++ = ')';
1549 }
1550 } else if (c == CTLARI) {
1551 cmdnleft -= 2;
1552 if (cmdnleft > 0) {
1553 *q++ = '$';
1554 *q++ = '(';
1555 *q++ = '(';
1556 }
1557 p++;
1558 } else if (c == CTLENDARI) {
1559 if (--cmdnleft > 0) {
1560 *q++ = ')';
1561 *q++ = ')';
1562 }
1563 } else if (c == CTLQUOTEMARK || c == CTLQUOTEEND)
1564 cmdnleft++; /* ignore */
1565 else
1566 *q++ = c;
1567 if (--cmdnleft <= 0) {
1568 *q++ = '.';
1569 *q++ = '.';
1570 *q++ = '.';
1571 break;
1572 }
1573 }
1574 cmdnextc = q;
1575 }
1576