1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1982-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                  David Korn <dgk@research.att.com>                   *
18*4887Schin *                                                                      *
19*4887Schin ***********************************************************************/
20*4887Schin #pragma prototyped
21*4887Schin #ifndef JOB_NFLAG
22*4887Schin /*
23*4887Schin  *	Interface to job control for shell
24*4887Schin  *	written by David Korn
25*4887Schin  *
26*4887Schin  */
27*4887Schin 
28*4887Schin #define JOBTTY	2
29*4887Schin 
30*4887Schin #include	<ast.h>
31*4887Schin #include	<sfio.h>
32*4887Schin #ifndef SIGINT
33*4887Schin #   include	<signal.h>
34*4887Schin #endif /* !SIGINT */
35*4887Schin #include	"FEATURE/options"
36*4887Schin 
37*4887Schin #undef JOBS
38*4887Schin #if defined(SIGCLD) && !defined(SIGCHLD)
39*4887Schin #   define SIGCHLD	SIGCLD
40*4887Schin #endif
41*4887Schin #ifdef SIGCHLD
42*4887Schin #   define JOBS	1
43*4887Schin #   include	"terminal.h"
44*4887Schin #   ifdef FIOLOOKLD
45*4887Schin 	/* Ninth edition */
46*4887Schin 	extern int tty_ld, ntty_ld;
47*4887Schin #	define OTTYDISC	tty_ld
48*4887Schin #	define NTTYDISC	ntty_ld
49*4887Schin #   endif	/* FIOLOOKLD */
50*4887Schin #else
51*4887Schin #   undef SIGTSTP
52*4887Schin #   undef SH_MONITOR
53*4887Schin #   define SH_MONITOR	0
54*4887Schin #   define job_set(x)
55*4887Schin #   define job_reset(x)
56*4887Schin #endif
57*4887Schin 
58*4887Schin struct process
59*4887Schin {
60*4887Schin 	struct process *p_nxtjob;	/* next job structure */
61*4887Schin 	struct process *p_nxtproc;	/* next process in current job */
62*4887Schin 	pid_t		p_pid;		/* process id */
63*4887Schin 	pid_t		p_pgrp;		/* process group */
64*4887Schin 	pid_t		p_fgrp;		/* process group when stopped */
65*4887Schin 	short		p_job;		/* job number of process */
66*4887Schin 	unsigned short	p_exit;		/* exit value or signal number */
67*4887Schin 	unsigned short	p_flag;		/* flags - see below */
68*4887Schin 	int		p_env;		/* subshell environment number */
69*4887Schin #ifdef JOBS
70*4887Schin 	off_t		p_name;		/* history file offset for command */
71*4887Schin 	struct termios	p_stty;		/* terminal state for job */
72*4887Schin #endif /* JOBS */
73*4887Schin };
74*4887Schin 
75*4887Schin struct jobs
76*4887Schin {
77*4887Schin 	struct process	*pwlist;	/* head of process list */
78*4887Schin 	pid_t		curpgid;	/* current process gid id */
79*4887Schin 	pid_t		parent;		/* set by fork() */
80*4887Schin 	pid_t		mypid;		/* process id of shell */
81*4887Schin 	pid_t		mypgid;		/* process group id of shell */
82*4887Schin 	pid_t		mytgid;		/* terminal group id of shell */
83*4887Schin 	unsigned int	in_critical;	/* >0 => in critical region */
84*4887Schin 	int		savesig;	/* active signal */
85*4887Schin 	int		numpost;	/* number of posted jobs */
86*4887Schin 	short		fd;		/* tty descriptor number */
87*4887Schin #ifdef JOBS
88*4887Schin 	int		suspend;	/* suspend character */
89*4887Schin 	int		linedisc;	/* line dicipline */
90*4887Schin #endif /* JOBS */
91*4887Schin 	char		jobcontrol;	/* turned on for real job control */
92*4887Schin 	char		waitsafe;	/* wait will not block */
93*4887Schin 	char		waitall;	/* wait for all jobs in pipe */
94*4887Schin 	char		toclear;	/* job table needs clearing */
95*4887Schin 	unsigned char	*freejobs;	/* free jobs numbers */
96*4887Schin };
97*4887Schin 
98*4887Schin /* flags for joblist */
99*4887Schin #define JOB_LFLAG	1
100*4887Schin #define JOB_NFLAG	2
101*4887Schin #define JOB_PFLAG	4
102*4887Schin #define JOB_NLFLAG	8
103*4887Schin 
104*4887Schin extern struct jobs job;
105*4887Schin 
106*4887Schin #ifdef JOBS
107*4887Schin 
108*4887Schin #define job_lock()	(job.in_critical++)
109*4887Schin #define job_unlock()	do{if(!--job.in_critical&&job.savesig)job_reap(job.savesig);}while(0)
110*4887Schin 
111*4887Schin extern const char	e_jobusage[];
112*4887Schin extern const char	e_done[];
113*4887Schin extern const char	e_running[];
114*4887Schin extern const char	e_coredump[];
115*4887Schin extern const char	e_no_proc[];
116*4887Schin extern const char	e_no_job[];
117*4887Schin extern const char	e_jobsrunning[];
118*4887Schin extern const char	e_nlspace[];
119*4887Schin extern const char	e_access[];
120*4887Schin extern const char	e_terminate[];
121*4887Schin extern const char	e_no_jctl[];
122*4887Schin extern const char	e_signo[];
123*4887Schin #ifdef SIGTSTP
124*4887Schin    extern const char	e_no_start[];
125*4887Schin #endif /* SIGTSTP */
126*4887Schin #ifdef NTTYDISC
127*4887Schin    extern const char	e_newtty[];
128*4887Schin    extern const char	e_oldtty[];
129*4887Schin #endif /* NTTYDISC */
130*4887Schin #endif	/* JOBS */
131*4887Schin 
132*4887Schin /*
133*4887Schin  * The following are defined in jobs.c
134*4887Schin  */
135*4887Schin 
136*4887Schin extern void	job_clear(void);
137*4887Schin extern void	job_bwait(char**);
138*4887Schin extern int	job_walk(Sfio_t*,int(*)(struct process*,int),int,char*[]);
139*4887Schin extern int	job_kill(struct process*,int);
140*4887Schin extern void	job_wait(pid_t);
141*4887Schin extern int	job_post(pid_t,pid_t);
142*4887Schin extern void	*job_subsave(void);
143*4887Schin extern void	job_subrestore(void*);
144*4887Schin #ifdef JOBS
145*4887Schin 	extern void	job_init(int);
146*4887Schin 	extern int	job_close(void);
147*4887Schin 	extern int	job_list(struct process*,int);
148*4887Schin 	extern int	job_terminate(struct process*,int);
149*4887Schin 	extern int	job_switch(struct process*,int);
150*4887Schin 	extern void	job_fork(pid_t);
151*4887Schin 	extern int	job_reap(int);
152*4887Schin #else
153*4887Schin #	define job_init(flag)
154*4887Schin #	define job_close()	(0)
155*4887Schin #	define job_fork(p)
156*4887Schin #endif	/* JOBS */
157*4887Schin 
158*4887Schin 
159*4887Schin #endif /* !JOB_NFLAG */
160