1253883Ssjg/*	$NetBSD: job.h,v 1.42 2013/07/05 22:14:56 sjg Exp $	*/
2236769Sobrien
3236769Sobrien/*
4236769Sobrien * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5236769Sobrien * All rights reserved.
6236769Sobrien *
7236769Sobrien * This code is derived from software contributed to Berkeley by
8236769Sobrien * Adam de Boor.
9236769Sobrien *
10236769Sobrien * Redistribution and use in source and binary forms, with or without
11236769Sobrien * modification, are permitted provided that the following conditions
12236769Sobrien * are met:
13236769Sobrien * 1. Redistributions of source code must retain the above copyright
14236769Sobrien *    notice, this list of conditions and the following disclaimer.
15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
16236769Sobrien *    notice, this list of conditions and the following disclaimer in the
17236769Sobrien *    documentation and/or other materials provided with the distribution.
18236769Sobrien * 3. Neither the name of the University nor the names of its contributors
19236769Sobrien *    may be used to endorse or promote products derived from this software
20236769Sobrien *    without specific prior written permission.
21236769Sobrien *
22236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32236769Sobrien * SUCH DAMAGE.
33236769Sobrien *
34236769Sobrien *	from: @(#)job.h	8.1 (Berkeley) 6/6/93
35236769Sobrien */
36236769Sobrien
37236769Sobrien/*
38236769Sobrien * Copyright (c) 1988, 1989 by Adam de Boor
39236769Sobrien * Copyright (c) 1989 by Berkeley Softworks
40236769Sobrien * All rights reserved.
41236769Sobrien *
42236769Sobrien * This code is derived from software contributed to Berkeley by
43236769Sobrien * Adam de Boor.
44236769Sobrien *
45236769Sobrien * Redistribution and use in source and binary forms, with or without
46236769Sobrien * modification, are permitted provided that the following conditions
47236769Sobrien * are met:
48236769Sobrien * 1. Redistributions of source code must retain the above copyright
49236769Sobrien *    notice, this list of conditions and the following disclaimer.
50236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
51236769Sobrien *    notice, this list of conditions and the following disclaimer in the
52236769Sobrien *    documentation and/or other materials provided with the distribution.
53236769Sobrien * 3. All advertising materials mentioning features or use of this software
54236769Sobrien *    must display the following acknowledgement:
55236769Sobrien *	This product includes software developed by the University of
56236769Sobrien *	California, Berkeley and its contributors.
57236769Sobrien * 4. Neither the name of the University nor the names of its contributors
58236769Sobrien *    may be used to endorse or promote products derived from this software
59236769Sobrien *    without specific prior written permission.
60236769Sobrien *
61236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71236769Sobrien * SUCH DAMAGE.
72236769Sobrien *
73236769Sobrien *	from: @(#)job.h	8.1 (Berkeley) 6/6/93
74236769Sobrien */
75236769Sobrien
76236769Sobrien/*-
77236769Sobrien * job.h --
78236769Sobrien *	Definitions pertaining to the running of jobs in parallel mode.
79236769Sobrien */
80236769Sobrien#ifndef _JOB_H_
81236769Sobrien#define _JOB_H_
82236769Sobrien
83236769Sobrien#define TMPPAT	"makeXXXXXX"		/* relative to tmpdir */
84236769Sobrien
85236769Sobrien#ifdef USE_SELECT
86236769Sobrien/*
87236769Sobrien * Emulate poll() in terms of select().  This is not a complete
88236769Sobrien * emulation but it is sufficient for make's purposes.
89236769Sobrien */
90236769Sobrien
91236769Sobrien#define poll emul_poll
92236769Sobrien#define pollfd emul_pollfd
93236769Sobrien
94236769Sobrienstruct emul_pollfd {
95236769Sobrien    int fd;
96236769Sobrien    short events;
97236769Sobrien    short revents;
98236769Sobrien};
99236769Sobrien
100236769Sobrien#define	POLLIN		0x0001
101236769Sobrien#define	POLLOUT		0x0004
102236769Sobrien
103236769Sobrienint
104236769Sobrienemul_poll(struct pollfd *fd, int nfd, int timeout);
105236769Sobrien#endif
106236769Sobrien
107236769Sobrien/*
108236769Sobrien * The POLL_MSEC constant determines the maximum number of milliseconds spent
109236769Sobrien * in poll before coming out to see if a child has finished.
110236769Sobrien */
111236769Sobrien#define POLL_MSEC	5000
112236769Sobrien
113236769Sobrien
114236769Sobrien/*-
115236769Sobrien * Job Table definitions.
116236769Sobrien *
117236769Sobrien * Each job has several things associated with it:
118236769Sobrien *	1) The process id of the child shell
119236769Sobrien *	2) The graph node describing the target being made by this job
120236769Sobrien *	3) A LstNode for the first command to be saved after the job
121236769Sobrien *	   completes. This is NULL if there was no "..." in the job's
122236769Sobrien *	   commands.
123236769Sobrien *	4) An FILE* for writing out the commands. This is only
124236769Sobrien *	   used before the job is actually started.
125236769Sobrien *	5) The output is being caught via a pipe and
126236769Sobrien *	   the descriptors of our pipe, an array in which output is line
127236769Sobrien *	   buffered and the current position in that buffer are all
128236769Sobrien *	   maintained for each job.
129236769Sobrien *	6) A word of flags which determine how the module handles errors,
130236769Sobrien *	   echoing, etc. for the job
131236769Sobrien *
132236769Sobrien * When a job is finished, the Make_Update function is called on each of the
133236769Sobrien * parents of the node which was just remade. This takes care of the upward
134236769Sobrien * traversal of the dependency graph.
135236769Sobrien */
136236769Sobrienstruct pollfd;
137236769Sobrien
138236769Sobrien
139236769Sobrien#ifdef USE_META
140236769Sobrien# include "meta.h"
141236769Sobrien#endif
142236769Sobrien
143236769Sobrien#define JOB_BUFSIZE	1024
144236769Sobrientypedef struct Job {
145236769Sobrien    int       	pid;	    /* The child's process ID */
146236769Sobrien    GNode    	*node;      /* The target the child is making */
147236769Sobrien    LstNode 	tailCmds;   /* The node of the first command to be
148236769Sobrien			     * saved when the job has been run */
149236769Sobrien    FILE 	*cmdFILE;   /* When creating the shell script, this is
150236769Sobrien			     * where the commands go */
151236769Sobrien    int		exit_status; /* from wait4() in signal handler */
152236769Sobrien    char        job_state;  /* status of the job entry */
153236769Sobrien#define JOB_ST_FREE	0	/* Job is available */
154236769Sobrien#define JOB_ST_SETUP	1	/* Job is allocated but otherwise invalid */
155236769Sobrien#define JOB_ST_RUNNING	3	/* Job is running, pid valid */
156236769Sobrien#define JOB_ST_FINISHED	4	/* Job is done (ie after SIGCHILD) */
157236769Sobrien    char        job_suspended;
158236769Sobrien    short      	flags;	    /* Flags to control treatment of job */
159236769Sobrien#define	JOB_IGNERR	0x001	/* Ignore non-zero exits */
160236769Sobrien#define	JOB_SILENT	0x002	/* no output */
161236769Sobrien#define JOB_SPECIAL	0x004	/* Target is a special one. i.e. run it locally
162236769Sobrien				 * if we can't export it and maxLocal is 0 */
163236769Sobrien#define JOB_IGNDOTS	0x008  	/* Ignore "..." lines when processing
164236769Sobrien				 * commands */
165236769Sobrien#define JOB_TRACED	0x400	/* we've sent 'set -x' */
166236769Sobrien
167236769Sobrien    int	  	 jobPipe[2];	/* Pipe for readind output from job */
168236769Sobrien    struct pollfd *inPollfd;	/* pollfd associated with inPipe */
169236769Sobrien    char  	outBuf[JOB_BUFSIZE + 1];
170236769Sobrien				/* Buffer for storing the output of the
171236769Sobrien				 * job, line by line */
172236769Sobrien    int   	curPos;	/* Current position in op_outBuf */
173236769Sobrien
174236769Sobrien#ifdef USE_META
175236769Sobrien    struct BuildMon	bm;
176236769Sobrien#endif
177236769Sobrien} Job;
178236769Sobrien
179236769Sobrien#define inPipe jobPipe[0]
180236769Sobrien#define outPipe jobPipe[1]
181236769Sobrien
182236769Sobrien
183236769Sobrien/*-
184236769Sobrien * Shell Specifications:
185236769Sobrien * Each shell type has associated with it the following information:
186236769Sobrien *	1) The string which must match the last character of the shell name
187236769Sobrien *	   for the shell to be considered of this type. The longest match
188236769Sobrien *	   wins.
189236769Sobrien *	2) A command to issue to turn off echoing of command lines
190236769Sobrien *	3) A command to issue to turn echoing back on again
191236769Sobrien *	4) What the shell prints, and its length, when given the echo-off
192236769Sobrien *	   command. This line will not be printed when received from the shell
193236769Sobrien *	5) A boolean to tell if the shell has the ability to control
194236769Sobrien *	   error checking for individual commands.
195236769Sobrien *	6) The string to turn this checking on.
196236769Sobrien *	7) The string to turn it off.
197236769Sobrien *	8) The command-flag to give to cause the shell to start echoing
198236769Sobrien *	   commands right away.
199236769Sobrien *	9) The command-flag to cause the shell to Lib_Exit when an error is
200236769Sobrien *	   detected in one of the commands.
201236769Sobrien *
202236769Sobrien * Some special stuff goes on if a shell doesn't have error control. In such
203236769Sobrien * a case, errCheck becomes a printf template for echoing the command,
204236769Sobrien * should echoing be on and ignErr becomes another printf template for
205236769Sobrien * executing the command while ignoring the return status. Finally errOut
206236769Sobrien * is a printf template for running the command and causing the shell to
207236769Sobrien * exit on error. If any of these strings are empty when hasErrCtl is FALSE,
208236769Sobrien * the command will be executed anyway as is and if it causes an error, so be
209236769Sobrien * it. Any templates setup to echo the command will escape any '$ ` \ "'i
210236769Sobrien * characters in the command string to avoid common problems with
211236769Sobrien * echo "%s\n" as a template.
212236769Sobrien */
213236769Sobrientypedef struct Shell {
214236769Sobrien    const char	 *name;		/* the name of the shell. For Bourne and C
215236769Sobrien				 * shells, this is used only to find the
216236769Sobrien				 * shell description when used as the single
217236769Sobrien				 * source of a .SHELL target. For user-defined
218236769Sobrien				 * shells, this is the full path of the shell.
219236769Sobrien				 */
220236769Sobrien    Boolean 	  hasEchoCtl;	/* True if both echoOff and echoOn defined */
221236769Sobrien    const char   *echoOff;	/* command to turn off echo */
222236769Sobrien    const char   *echoOn;	/* command to turn it back on again */
223236769Sobrien    const char   *noPrint;	/* command to skip when printing output from
224236769Sobrien				 * shell. This is usually the command which
225236769Sobrien				 * was executed to turn off echoing */
226236769Sobrien    int           noPLen;	/* length of noPrint command */
227236769Sobrien    Boolean	  hasErrCtl;	/* set if can control error checking for
228236769Sobrien				 * individual commands */
229236769Sobrien    const char	 *errCheck;	/* string to turn error checking on */
230236769Sobrien    const char	 *ignErr;	/* string to turn off error checking */
231236769Sobrien    const char	 *errOut;	/* string to use for testing exit code */
232236769Sobrien    const char	 *newline;	/* string literal that results in a newline
233236769Sobrien				 * character when it appears outside of any
234236769Sobrien				 * 'quote' or "quote" characters */
235236769Sobrien    char   commentChar;		/* character used by shell for comment lines */
236236769Sobrien
237236769Sobrien    /*
238236769Sobrien     * command-line flags
239236769Sobrien     */
240236769Sobrien    const char   *echo;		/* echo commands */
241236769Sobrien    const char   *exit;		/* exit on error */
242236769Sobrien}               Shell;
243236769Sobrien
244236769Sobrienextern const char *shellPath;
245236769Sobrienextern const char *shellName;
246253883Ssjgextern char *shellErrFlag;
247236769Sobrien
248236769Sobrienextern int	jobTokensRunning; /* tokens currently "out" */
249236769Sobrienextern int	maxJobs;	/* Max jobs we can run */
250236769Sobrien
251236769Sobrienvoid Shell_Init(void);
252236769Sobrienconst char *Shell_GetNewline(void);
253236769Sobrienvoid Job_Touch(GNode *, Boolean);
254236769SobrienBoolean Job_CheckCommands(GNode *, void (*abortProc )(const char *, ...));
255236769Sobrien#define CATCH_BLOCK	1
256236769Sobrienvoid Job_CatchChildren(void);
257236769Sobrienvoid Job_CatchOutput(void);
258236769Sobrienvoid Job_Make(GNode *);
259236769Sobrienvoid Job_Init(void);
260236769SobrienBoolean Job_Full(void);
261236769SobrienBoolean Job_Empty(void);
262236769SobrienReturnStatus Job_ParseShell(char *);
263236769Sobrienint Job_Finish(void);
264236769Sobrienvoid Job_End(void);
265236769Sobrienvoid Job_Wait(void);
266236769Sobrienvoid Job_AbortAll(void);
267236769Sobrienvoid JobFlagForMigration(int);
268236769Sobrienvoid Job_TokenReturn(void);
269236769SobrienBoolean Job_TokenWithdraw(void);
270236769Sobrienvoid Job_ServerStart(int, int, int);
271236769Sobrienvoid Job_SetPrefix(void);
272249033SsjgBoolean Job_RunTarget(const char *, const char *);
273236769Sobrien
274236769Sobrien#endif /* _JOB_H_ */
275