job.h revision 1.47
11.47Srillig/*	$NetBSD: job.h,v 1.47 2020/08/29 12:20:17 rillig Exp $	*/
21.4Schristos
31.1Scgd/*
41.1Scgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
51.20Sagc * All rights reserved.
61.20Sagc *
71.20Sagc * This code is derived from software contributed to Berkeley by
81.20Sagc * Adam de Boor.
91.20Sagc *
101.20Sagc * Redistribution and use in source and binary forms, with or without
111.20Sagc * modification, are permitted provided that the following conditions
121.20Sagc * are met:
131.20Sagc * 1. Redistributions of source code must retain the above copyright
141.20Sagc *    notice, this list of conditions and the following disclaimer.
151.20Sagc * 2. Redistributions in binary form must reproduce the above copyright
161.20Sagc *    notice, this list of conditions and the following disclaimer in the
171.20Sagc *    documentation and/or other materials provided with the distribution.
181.20Sagc * 3. Neither the name of the University nor the names of its contributors
191.20Sagc *    may be used to endorse or promote products derived from this software
201.20Sagc *    without specific prior written permission.
211.20Sagc *
221.20Sagc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.20Sagc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.20Sagc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.20Sagc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.20Sagc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.20Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.20Sagc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.20Sagc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.20Sagc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.20Sagc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.20Sagc * SUCH DAMAGE.
331.20Sagc *
341.20Sagc *	from: @(#)job.h	8.1 (Berkeley) 6/6/93
351.20Sagc */
361.20Sagc
371.20Sagc/*
381.1Scgd * Copyright (c) 1988, 1989 by Adam de Boor
391.1Scgd * Copyright (c) 1989 by Berkeley Softworks
401.1Scgd * All rights reserved.
411.1Scgd *
421.1Scgd * This code is derived from software contributed to Berkeley by
431.1Scgd * Adam de Boor.
441.1Scgd *
451.1Scgd * Redistribution and use in source and binary forms, with or without
461.1Scgd * modification, are permitted provided that the following conditions
471.1Scgd * are met:
481.1Scgd * 1. Redistributions of source code must retain the above copyright
491.1Scgd *    notice, this list of conditions and the following disclaimer.
501.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
511.1Scgd *    notice, this list of conditions and the following disclaimer in the
521.1Scgd *    documentation and/or other materials provided with the distribution.
531.1Scgd * 3. All advertising materials mentioning features or use of this software
541.1Scgd *    must display the following acknowledgement:
551.1Scgd *	This product includes software developed by the University of
561.1Scgd *	California, Berkeley and its contributors.
571.1Scgd * 4. Neither the name of the University nor the names of its contributors
581.1Scgd *    may be used to endorse or promote products derived from this software
591.1Scgd *    without specific prior written permission.
601.1Scgd *
611.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
621.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
631.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
641.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
651.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
661.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
671.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
681.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
691.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
701.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
711.1Scgd * SUCH DAMAGE.
721.1Scgd *
731.5Schristos *	from: @(#)job.h	8.1 (Berkeley) 6/6/93
741.1Scgd */
751.1Scgd
761.1Scgd/*-
771.1Scgd * job.h --
781.1Scgd *	Definitions pertaining to the running of jobs in parallel mode.
791.1Scgd */
801.44Srillig#ifndef MAKE_JOB_H
811.44Srillig#define MAKE_JOB_H
821.1Scgd
831.39Sapb#define TMPPAT	"makeXXXXXX"		/* relative to tmpdir */
841.1Scgd
851.10Ssommerfe#ifdef USE_SELECT
861.1Scgd/*
871.17Sgson * Emulate poll() in terms of select().  This is not a complete
881.17Sgson * emulation but it is sufficient for make's purposes.
891.1Scgd */
901.17Sgson
911.17Sgson#define poll emul_poll
921.17Sgson#define pollfd emul_pollfd
931.17Sgson
941.17Sgsonstruct emul_pollfd {
951.17Sgson    int fd;
961.17Sgson    short events;
971.17Sgson    short revents;
981.17Sgson};
991.17Sgson
1001.17Sgson#define	POLLIN		0x0001
1011.17Sgson#define	POLLOUT		0x0004
1021.17Sgson
1031.17Sgsonint
1041.17Sgsonemul_poll(struct pollfd *fd, int nfd, int timeout);
1051.17Sgson#endif
1061.17Sgson
1071.10Ssommerfe/*
1081.10Ssommerfe * The POLL_MSEC constant determines the maximum number of milliseconds spent
1091.43Srillig * in poll before coming out to see if a child has finished.
1101.10Ssommerfe */
1111.10Ssommerfe#define POLL_MSEC	5000
1121.1Scgd
1131.1Scgd/*-
1141.5Schristos * Job Table definitions.
1151.1Scgd *
1161.1Scgd * Each job has several things associated with it:
1171.1Scgd *	1) The process id of the child shell
1181.1Scgd *	2) The graph node describing the target being made by this job
1191.1Scgd *	3) A LstNode for the first command to be saved after the job
1201.38Sdsl *	   completes. This is NULL if there was no "..." in the job's
1211.1Scgd *	   commands.
1221.1Scgd *	4) An FILE* for writing out the commands. This is only
1231.1Scgd *	   used before the job is actually started.
1241.32Sdsl *	5) The output is being caught via a pipe and
1251.1Scgd *	   the descriptors of our pipe, an array in which output is line
1261.1Scgd *	   buffered and the current position in that buffer are all
1271.32Sdsl *	   maintained for each job.
1281.25Sdsl *	6) A word of flags which determine how the module handles errors,
1291.5Schristos *	   echoing, etc. for the job
1301.1Scgd *
1311.1Scgd * When a job is finished, the Make_Update function is called on each of the
1321.1Scgd * parents of the node which was just remade. This takes care of the upward
1331.1Scgd * traversal of the dependency graph.
1341.1Scgd */
1351.9Schristosstruct pollfd;
1361.9Schristos
1371.40Ssjg
1381.40Ssjg#ifdef USE_META
1391.40Ssjg# include "meta.h"
1401.40Ssjg#endif
1411.40Ssjg
1421.1Scgd#define JOB_BUFSIZE	1024
1431.1Scgdtypedef struct Job {
1441.37Schristos    int       	pid;	    /* The child's process ID */
1451.1Scgd    GNode    	*node;      /* The target the child is making */
1461.1Scgd    LstNode 	tailCmds;   /* The node of the first command to be
1471.1Scgd			     * saved when the job has been run */
1481.1Scgd    FILE 	*cmdFILE;   /* When creating the shell script, this is
1491.1Scgd			     * where the commands go */
1501.30Sdsl    int		exit_status; /* from wait4() in signal handler */
1511.30Sdsl    char        job_state;  /* status of the job entry */
1521.29Sdsl#define JOB_ST_FREE	0	/* Job is available */
1531.29Sdsl#define JOB_ST_SETUP	1	/* Job is allocated but otherwise invalid */
1541.30Sdsl#define JOB_ST_RUNNING	3	/* Job is running, pid valid */
1551.29Sdsl#define JOB_ST_FINISHED	4	/* Job is done (ie after SIGCHILD) */
1561.30Sdsl    char        job_suspended;
1571.1Scgd    short      	flags;	    /* Flags to control treatment of job */
1581.1Scgd#define	JOB_IGNERR	0x001	/* Ignore non-zero exits */
1591.1Scgd#define	JOB_SILENT	0x002	/* no output */
1601.1Scgd#define JOB_SPECIAL	0x004	/* Target is a special one. i.e. run it locally
1611.1Scgd				 * if we can't export it and maxLocal is 0 */
1621.1Scgd#define JOB_IGNDOTS	0x008  	/* Ignore "..." lines when processing
1631.1Scgd				 * commands */
1641.15Ssjg#define JOB_TRACED	0x400	/* we've sent 'set -x' */
1651.15Ssjg
1661.46Srillig    int	  	 jobPipe[2];	/* Pipe for reading output from job */
1671.32Sdsl    struct pollfd *inPollfd;	/* pollfd associated with inPipe */
1681.32Sdsl    char  	outBuf[JOB_BUFSIZE + 1];
1691.32Sdsl				/* Buffer for storing the output of the
1701.32Sdsl				 * job, line by line */
1711.37Schristos    int   	curPos;	/* Current position in op_outBuf */
1721.40Ssjg
1731.40Ssjg#ifdef USE_META
1741.40Ssjg    struct BuildMon	bm;
1751.40Ssjg#endif
1761.1Scgd} Job;
1771.1Scgd
1781.33Sdsl#define inPipe jobPipe[0]
1791.33Sdsl#define outPipe jobPipe[1]
1801.33Sdsl
1811.1Scgd/*-
1821.1Scgd * Shell Specifications:
1831.1Scgd * Each shell type has associated with it the following information:
1841.1Scgd *	1) The string which must match the last character of the shell name
1851.1Scgd *	   for the shell to be considered of this type. The longest match
1861.1Scgd *	   wins.
1871.1Scgd *	2) A command to issue to turn off echoing of command lines
1881.1Scgd *	3) A command to issue to turn echoing back on again
1891.1Scgd *	4) What the shell prints, and its length, when given the echo-off
1901.1Scgd *	   command. This line will not be printed when received from the shell
1911.1Scgd *	5) A boolean to tell if the shell has the ability to control
1921.1Scgd *	   error checking for individual commands.
1931.1Scgd *	6) The string to turn this checking on.
1941.1Scgd *	7) The string to turn it off.
1951.1Scgd *	8) The command-flag to give to cause the shell to start echoing
1961.1Scgd *	   commands right away.
1971.1Scgd *	9) The command-flag to cause the shell to Lib_Exit when an error is
1981.1Scgd *	   detected in one of the commands.
1991.1Scgd *
2001.1Scgd * Some special stuff goes on if a shell doesn't have error control. In such
2011.1Scgd * a case, errCheck becomes a printf template for echoing the command,
2021.1Scgd * should echoing be on and ignErr becomes another printf template for
2031.21Sjmc * executing the command while ignoring the return status. Finally errOut
2041.43Srillig * is a printf template for running the command and causing the shell to
2051.43Srillig * exit on error. If any of these strings are empty when hasErrCtl is FALSE,
2061.43Srillig * the command will be executed anyway as is and if it causes an error, so be
2071.21Sjmc * it. Any templates setup to echo the command will escape any '$ ` \ "'i
2081.43Srillig * characters in the command string to avoid common problems with
2091.21Sjmc * echo "%s\n" as a template.
2101.1Scgd */
2111.1Scgdtypedef struct Shell {
2121.18Schristos    const char	 *name;		/* the name of the shell. For Bourne and C
2131.1Scgd				 * shells, this is used only to find the
2141.1Scgd				 * shell description when used as the single
2151.1Scgd				 * source of a .SHELL target. For user-defined
2161.1Scgd				 * shells, this is the full path of the shell.
2171.1Scgd				 */
2181.1Scgd    Boolean 	  hasEchoCtl;	/* True if both echoOff and echoOn defined */
2191.18Schristos    const char   *echoOff;	/* command to turn off echo */
2201.18Schristos    const char   *echoOn;	/* command to turn it back on again */
2211.18Schristos    const char   *noPrint;	/* command to skip when printing output from
2221.1Scgd				 * shell. This is usually the command which
2231.1Scgd				 * was executed to turn off echoing */
2241.1Scgd    int           noPLen;	/* length of noPrint command */
2251.1Scgd    Boolean	  hasErrCtl;	/* set if can control error checking for
2261.1Scgd				 * individual commands */
2271.18Schristos    const char	 *errCheck;	/* string to turn error checking on */
2281.18Schristos    const char	 *ignErr;	/* string to turn off error checking */
2291.21Sjmc    const char	 *errOut;	/* string to use for testing exit code */
2301.28Srillig    const char	 *newline;	/* string literal that results in a newline
2311.28Srillig				 * character when it appears outside of any
2321.28Srillig				 * 'quote' or "quote" characters */
2331.21Sjmc    char   commentChar;		/* character used by shell for comment lines */
2341.21Sjmc
2351.1Scgd    /*
2361.5Schristos     * command-line flags
2371.1Scgd     */
2381.18Schristos    const char   *echo;		/* echo commands */
2391.18Schristos    const char   *exit;		/* exit on error */
2401.1Scgd}               Shell;
2411.1Scgd
2421.19Ssjgextern const char *shellPath;
2431.19Ssjgextern const char *shellName;
2441.42Ssjgextern char *shellErrFlag;
2451.19Ssjg
2461.14Ssommerfeextern int	jobTokensRunning; /* tokens currently "out" */
2471.27Sdslextern int	maxJobs;	/* Max jobs we can run */
2481.11Ssommerfe
2491.19Ssjgvoid Shell_Init(void);
2501.28Srilligconst char *Shell_GetNewline(void);
2511.16Swizvoid Job_Touch(GNode *, Boolean);
2521.18SchristosBoolean Job_CheckCommands(GNode *, void (*abortProc )(const char *, ...));
2531.32Sdslvoid Job_CatchChildren(void);
2541.16Swizvoid Job_CatchOutput(void);
2551.16Swizvoid Job_Make(GNode *);
2561.27Sdslvoid Job_Init(void);
2571.16SwizBoolean Job_Empty(void);
2581.47SrilligBoolean Job_ParseShell(char *);
2591.16Swizint Job_Finish(void);
2601.16Swizvoid Job_End(void);
2611.16Swizvoid Job_Wait(void);
2621.16Swizvoid Job_AbortAll(void);
2631.16Swizvoid Job_TokenReturn(void);
2641.16SwizBoolean Job_TokenWithdraw(void);
2651.33Sdslvoid Job_ServerStart(int, int, int);
2661.34Ssjgvoid Job_SetPrefix(void);
2671.41SchristosBoolean Job_RunTarget(const char *, const char *);
2681.3Scgd
2691.44Srillig#endif /* MAKE_JOB_H */
270