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