- Fix some duplicate commenting for struct Job, struct GNode and
[dragonfly.git] / usr.bin / make / job.h
1 /*-
2  * Copyright (c) 1988, 1989, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1988, 1989 by Adam de Boor
5  * Copyright (c) 1989 by Berkeley Softworks
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Adam de Boor.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      from: @(#)job.h 8.1 (Berkeley) 6/6/93
40  * $FreeBSD: src/usr.bin/make/job.h,v 1.11 2000/01/17 06:43:41 kris Exp $
41  * $DragonFly: src/usr.bin/make/job.h,v 1.19 2005/02/01 22:05:36 okumoto Exp $
42  */
43
44 #ifndef job_h_4678dfd1
45 #define job_h_4678dfd1
46
47 /*-
48  * job.h --
49  *      Definitions pertaining to the running of jobs in parallel mode.
50  */
51
52 #include <stdio.h>
53
54 #include "sprite.h"
55
56 struct GNode;
57 struct LstNode;
58
59 #define TMPPAT  "/tmp/makeXXXXXXXXXX"
60
61 #ifndef USE_KQUEUE
62 /*
63  * The SEL_ constants determine the maximum amount of time spent in select
64  * before coming out to see if a child has finished. SEL_SEC is the number of
65  * seconds and SEL_USEC is the number of micro-seconds
66  */
67 #define SEL_SEC         2
68 #define SEL_USEC        0
69 #endif /* !USE_KQUEUE */
70
71 /*
72  * Job Table definitions.
73  *
74  * The job "table" is kept as a linked Lst in 'jobs', with the number of
75  * active jobs maintained in the 'nJobs' variable. At no time will this
76  * exceed the value of 'maxJobs', initialized by the Job_Init function.
77  *
78  * When a job is finished, the Make_Update function is called on each of the
79  * parents of the node which was just remade. This takes care of the upward
80  * traversal of the dependency graph.
81  */
82 #define JOB_BUFSIZE     1024
83 typedef struct Job {
84         int             pid;    /* The child's process ID */
85
86         /* Temporary file to use for job */
87         char            tfile[sizeof(TMPPAT)];
88
89         struct GNode    *node;  /* The target the child is making */
90
91         /*
92          * A LstNode for the first command to be saved after the job completes.
93          * This is NULL if there was no "..." in the job's commands.
94          */
95         LstNode         *tailCmds;
96
97         /*
98          * An FILE* for writing out the commands. This is only
99          * used before the job is actually started.
100          */
101         FILE            *cmdFILE;
102
103         /*
104          * A word of flags which determine how the module handles errors,
105          * echoing, etc. for the job
106          */
107         short           flags;  /* Flags to control treatment of job */
108 #define JOB_IGNERR      0x001   /* Ignore non-zero exits */
109 #define JOB_SILENT      0x002   /* no output */
110 #define JOB_SPECIAL     0x004   /* Target is a special one. i.e. run it locally
111                                  * if we can't export it and maxLocal is 0 */
112 #define JOB_IGNDOTS     0x008   /* Ignore "..." lines when processing
113                                  * commands */
114 #define JOB_FIRST       0x020   /* Job is first job for the node */
115 #define JOB_RESTART     0x080   /* Job needs to be completely restarted */
116 #define JOB_RESUME      0x100   /* Job needs to be resumed b/c it stopped,
117                                  * for some reason */
118 #define JOB_CONTINUING  0x200   /* We are in the process of resuming this job.
119                                  * Used to avoid infinite recursion between
120                                  * JobFinish and JobRestart */
121
122         /* union for handling shell's output */
123         union {
124                 /*
125                  * This part is used when usePipes is true.
126                  * The output is being caught via a pipe and the descriptors
127                  * of our pipe, an array in which output is line buffered and
128                  * the current position in that buffer are all maintained for
129                  * each job.
130                  */
131                 struct {
132                         /*
133                          * Input side of pipe associated with
134                          * job's output channel
135                          */
136                         int     op_inPipe;
137
138                         /*
139                          * Output side of pipe associated with job's
140                          * output channel
141                          */
142                         int     op_outPipe;
143
144                         /*
145                          * Buffer for storing the output of the
146                          * job, line by line
147                          */
148                         char    op_outBuf[JOB_BUFSIZE + 1];
149
150                         /* Current position in op_outBuf */
151                         int     op_curPos;
152                 }       o_pipe;
153
154                 /*
155                  * If usePipes is false the output is routed to a temporary
156                  * file and all that is kept is the name of the file and the
157                  * descriptor open to the file.
158                  */
159                 struct {
160                         /* Name of file to which shell output was rerouted */
161                         char    of_outFile[sizeof(TMPPAT)];
162
163                         /*
164                          * Stream open to the output file. Used to funnel all
165                          * from a single job to one file while still allowing
166                          * multiple shell invocations
167                          */
168                         int     of_outFd;
169                 }       o_file;
170
171         }       output;     /* Data for tracking a shell's output */
172 } Job;
173
174 #define outPipe         output.o_pipe.op_outPipe
175 #define inPipe          output.o_pipe.op_inPipe
176 #define outBuf          output.o_pipe.op_outBuf
177 #define curPos          output.o_pipe.op_curPos
178 #define outFile         output.o_file.of_outFile
179 #define outFd           output.o_file.of_outFd
180
181 /*-
182  * Shell Specifications:
183  *
184  * Some special stuff goes on if a shell doesn't have error control. In such
185  * a case, errCheck becomes a printf template for echoing the command,
186  * should echoing be on and ignErr becomes another printf template for
187  * executing the command while ignoring the return status. If either of these
188  * strings is empty when hasErrCtl is FALSE, the command will be executed
189  * anyway as is and if it causes an error, so be it.
190  */
191 #define DEF_SHELL_STRUCT(TAG, CONST)                                    \
192 struct TAG {                                                            \
193         /*                                                              \
194          * the name of the shell. For Bourne and C shells, this is used \
195          * only to find the shell description when used as the single   \
196          * source of a .SHELL target. For user-defined shells, this is  \
197          * the full path of the shell.                                  \
198          */                                                             \
199         CONST char      *name;                                          \
200                                                                         \
201         /* True if both echoOff and echoOn defined */                   \
202         Boolean         hasEchoCtl;                                     \
203                                                                         \
204         CONST char      *echoOff;       /* command to turn off echo */  \
205         CONST char      *echoOn;        /* command to turn it back on */\
206                                                                         \
207         /*                                                              \
208          * What the shell prints, and its length, when given the        \
209          * echo-off command. This line will not be printed when         \
210          * received from the shell. This is usually the command which   \
211          * was executed to turn off echoing                             \
212          */                                                             \
213         CONST char      *noPrint;                                       \
214         int             noPLen;         /* length of noPrint command */ \
215                                                                         \
216         /* set if can control error checking for individual commands */ \
217         Boolean         hasErrCtl;                                      \
218                                                                         \
219         /* string to turn error checking on */                          \
220         CONST char      *errCheck;                                      \
221                                                                         \
222         /* string to turn off error checking */                         \
223         CONST char      *ignErr;                                        \
224                                                                         \
225         CONST char      *echo;  /* command line flag: echo commands */  \
226         CONST char      *exit;  /* command line flag: exit on error */  \
227 }
228
229 typedef DEF_SHELL_STRUCT(Shell,) Shell;
230
231 extern char *shellPath;
232 extern char *shellName;
233 extern int      maxJobs;        /* Number of jobs that may run */
234
235 void Shell_Init(void);
236 void Job_Touch(struct GNode *, Boolean);
237 Boolean Job_CheckCommands(struct GNode *, void (*abortProc)(const char *, ...));
238 void Job_CatchChildren(Boolean);
239 void Job_CatchOutput(int flag);
240 void Job_Make(struct GNode *);
241 void Job_Init(int);
242 Boolean Job_Full(void);
243 Boolean Job_Empty(void);
244 ReturnStatus Job_ParseShell(char *);
245 int Job_Finish(void);
246 void Job_Wait(void);
247 void Job_AbortAll(void);
248
249 #endif /* job_h_4678dfd1 */