Home | History | Annotate | Line # | Download | only in make
main.c revision 1.414
      1  1.414    rillig /*	$NetBSD: main.c,v 1.414 2020/10/31 09:20:07 rillig Exp $	*/
      2   1.19  christos 
      3    1.1       cgd /*
      4   1.31  christos  * Copyright (c) 1988, 1989, 1990, 1993
      5   1.31  christos  *	The Regents of the University of California.  All rights reserved.
      6   1.91       agc  *
      7   1.91       agc  * This code is derived from software contributed to Berkeley by
      8   1.91       agc  * Adam de Boor.
      9   1.91       agc  *
     10   1.91       agc  * Redistribution and use in source and binary forms, with or without
     11   1.91       agc  * modification, are permitted provided that the following conditions
     12   1.91       agc  * are met:
     13   1.91       agc  * 1. Redistributions of source code must retain the above copyright
     14   1.91       agc  *    notice, this list of conditions and the following disclaimer.
     15   1.91       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.91       agc  *    notice, this list of conditions and the following disclaimer in the
     17   1.91       agc  *    documentation and/or other materials provided with the distribution.
     18   1.91       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.91       agc  *    may be used to endorse or promote products derived from this software
     20   1.91       agc  *    without specific prior written permission.
     21   1.91       agc  *
     22   1.91       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.91       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.91       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.91       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.91       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.91       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.91       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.91       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.91       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.91       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.91       agc  * SUCH DAMAGE.
     33   1.91       agc  */
     34   1.91       agc 
     35   1.91       agc /*
     36    1.1       cgd  * Copyright (c) 1989 by Berkeley Softworks
     37    1.1       cgd  * All rights reserved.
     38    1.1       cgd  *
     39    1.1       cgd  * This code is derived from software contributed to Berkeley by
     40    1.1       cgd  * Adam de Boor.
     41    1.1       cgd  *
     42    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     43    1.1       cgd  * modification, are permitted provided that the following conditions
     44    1.1       cgd  * are met:
     45    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     46    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     47    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     48    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     49    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     50    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     51    1.1       cgd  *    must display the following acknowledgement:
     52    1.1       cgd  *	This product includes software developed by the University of
     53    1.1       cgd  *	California, Berkeley and its contributors.
     54    1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     55    1.1       cgd  *    may be used to endorse or promote products derived from this software
     56    1.1       cgd  *    without specific prior written permission.
     57    1.1       cgd  *
     58    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68    1.1       cgd  * SUCH DAMAGE.
     69    1.1       cgd  */
     70    1.1       cgd 
     71    1.1       cgd /*-
     72    1.1       cgd  * main.c --
     73    1.1       cgd  *	The main file for this entire program. Exit routines etc
     74    1.1       cgd  *	reside here.
     75    1.1       cgd  *
     76    1.1       cgd  * Utility functions defined in this file:
     77    1.1       cgd  *	Main_ParseArgLine	Takes a line of arguments, breaks them and
     78    1.1       cgd  *				treats them as if they were given when first
     79    1.1       cgd  *				invoked. Used by the parse module to implement
     80    1.1       cgd  *				the .MFLAGS target.
     81    1.1       cgd  *
     82    1.1       cgd  *	Error			Print a tagged error message. The global
     83    1.1       cgd  *				MAKE variable must have been defined. This
     84  1.227      gson  *				takes a format string and optional arguments
     85  1.227      gson  *				for it.
     86    1.1       cgd  *
     87    1.1       cgd  *	Fatal			Print an error message and exit. Also takes
     88  1.227      gson  *				a format string and arguments for it.
     89    1.1       cgd  *
     90    1.1       cgd  *	Punt			Aborts all jobs and exits with a message. Also
     91  1.227      gson  *				takes a format string and arguments for it.
     92    1.1       cgd  *
     93    1.1       cgd  *	Finish			Finish things up by printing the number of
     94   1.72       wiz  *				errors which occurred, as passed to it, and
     95    1.1       cgd  *				exiting.
     96    1.1       cgd  */
     97    1.1       cgd 
     98   1.13       cgd #include <sys/types.h>
     99   1.13       cgd #include <sys/time.h>
    100    1.1       cgd #include <sys/param.h>
    101   1.13       cgd #include <sys/resource.h>
    102    1.1       cgd #include <sys/stat.h>
    103  1.225      matt #ifdef MAKE_NATIVE
    104  1.225      matt #include <sys/sysctl.h>
    105  1.225      matt #endif
    106   1.17       gwr #include <sys/utsname.h>
    107   1.29  christos #include <sys/wait.h>
    108   1.85       wiz 
    109    1.1       cgd #include <errno.h>
    110  1.215  christos #include <signal.h>
    111   1.85       wiz #include <stdarg.h>
    112   1.42    kleink #include <time.h>
    113   1.85       wiz 
    114    1.1       cgd #include "make.h"
    115   1.13       cgd #include "dir.h"
    116   1.13       cgd #include "job.h"
    117    1.1       cgd #include "pathnames.h"
    118   1.58  sommerfe #include "trace.h"
    119    1.1       cgd 
    120  1.337    rillig /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
    121  1.414    rillig MAKE_RCSID("$NetBSD: main.c,v 1.414 2020/10/31 09:20:07 rillig Exp $");
    122  1.337    rillig #if defined(MAKE_NATIVE) && !defined(lint)
    123  1.369    rillig __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
    124  1.369    rillig 	    "The Regents of the University of California.  "
    125  1.369    rillig 	    "All rights reserved.");
    126  1.337    rillig #endif
    127  1.337    rillig 
    128    1.1       cgd #ifndef	DEFMAXLOCAL
    129    1.1       cgd #define	DEFMAXLOCAL DEFMAXJOBS
    130  1.336    rillig #endif
    131    1.1       cgd 
    132  1.391    rillig CmdOpts opts;
    133    1.1       cgd time_t			now;		/* Time at start of make */
    134    1.1       cgd GNode			*DEFAULT;	/* .DEFAULT node */
    135    1.1       cgd Boolean			allPrecious;	/* .PRECIOUS given on line by itself */
    136  1.251  dholland Boolean			deleteOnError;	/* .DELETE_ON_ERROR: set */
    137    1.1       cgd 
    138  1.132       dsl static int		maxJobTokens;	/* -j argument */
    139  1.233       sjg Boolean			enterFlagObj;	/* -w and objdir != srcdir */
    140  1.391    rillig 
    141    1.1       cgd Boolean			oldVars;	/* variable substitution style */
    142  1.132       dsl static int jp_0 = -1, jp_1 = -1;	/* ends of parent job pipe */
    143  1.145       dsl Boolean			doing_depend;	/* Set while reading .depend */
    144    1.1       cgd static Boolean		jobsRunning;	/* TRUE if the jobs might be running */
    145   1.58  sommerfe static const char *	tracefile;
    146  1.325    rillig static int		ReadMakefile(const char *);
    147  1.200     joerg static void		usage(void) MAKE_ATTR_DEAD;
    148  1.262  riastrad static void		purge_cached_realpaths(void);
    149    1.1       cgd 
    150  1.174       sjg static Boolean		ignorePWD;	/* if we use -C, PWD is meaningless */
    151   1.74        tv static char objdir[MAXPATHLEN + 1];	/* where we chdir'ed to */
    152  1.196     joerg char curdir[MAXPATHLEN + 1];		/* Startup directory */
    153   1.63  christos char *progname;				/* the program name */
    154  1.176       sjg char *makeDependfile;
    155  1.183       sjg pid_t myPid;
    156  1.220  christos int makelevel;
    157    1.1       cgd 
    158   1.58  sommerfe Boolean forceJobs = FALSE;
    159   1.58  sommerfe 
    160  1.338    rillig extern SearchPath *parseIncPath;
    161   1.63  christos 
    162  1.220  christos /*
    163  1.220  christos  * For compatibility with the POSIX version of MAKEFLAGS that includes
    164  1.220  christos  * all the options with out -, convert flags to -f -l -a -g -s.
    165  1.220  christos  */
    166  1.220  christos static char *
    167  1.220  christos explode(const char *flags)
    168  1.220  christos {
    169  1.220  christos     size_t len;
    170  1.220  christos     char *nf, *st;
    171  1.220  christos     const char *f;
    172  1.220  christos 
    173  1.220  christos     if (flags == NULL)
    174  1.220  christos 	return NULL;
    175  1.220  christos 
    176  1.220  christos     for (f = flags; *f; f++)
    177  1.332    rillig 	if (!ch_isalpha(*f))
    178  1.220  christos 	    break;
    179  1.220  christos 
    180  1.220  christos     if (*f)
    181  1.221  christos 	return bmake_strdup(flags);
    182  1.220  christos 
    183  1.220  christos     len = strlen(flags);
    184  1.221  christos     st = nf = bmake_malloc(len * 3 + 1);
    185  1.220  christos     while (*flags) {
    186  1.220  christos 	*nf++ = '-';
    187  1.220  christos 	*nf++ = *flags++;
    188  1.220  christos 	*nf++ = ' ';
    189  1.220  christos     }
    190  1.220  christos     *nf = '\0';
    191  1.220  christos     return st;
    192  1.220  christos }
    193  1.279    rillig 
    194  1.133       dsl static void
    195  1.344    rillig parse_debug_option_F(const char *modules)
    196  1.344    rillig {
    197  1.344    rillig     const char *mode;
    198  1.344    rillig     size_t len;
    199  1.344    rillig     char *fname;
    200  1.344    rillig 
    201  1.391    rillig     if (opts.debug_file != stdout && opts.debug_file != stderr)
    202  1.391    rillig 	fclose(opts.debug_file);
    203  1.344    rillig 
    204  1.344    rillig     if (*modules == '+') {
    205  1.344    rillig 	modules++;
    206  1.344    rillig 	mode = "a";
    207  1.344    rillig     } else
    208  1.344    rillig 	mode = "w";
    209  1.344    rillig 
    210  1.344    rillig     if (strcmp(modules, "stdout") == 0) {
    211  1.391    rillig 	opts.debug_file = stdout;
    212  1.344    rillig 	return;
    213  1.344    rillig     }
    214  1.344    rillig     if (strcmp(modules, "stderr") == 0) {
    215  1.391    rillig 	opts.debug_file = stderr;
    216  1.344    rillig 	return;
    217  1.344    rillig     }
    218  1.344    rillig 
    219  1.344    rillig     len = strlen(modules);
    220  1.344    rillig     fname = bmake_malloc(len + 20);
    221  1.344    rillig     memcpy(fname, modules, len + 1);
    222  1.344    rillig 
    223  1.344    rillig     /* Let the filename be modified by the pid */
    224  1.344    rillig     if (strcmp(fname + len - 3, ".%d") == 0)
    225  1.344    rillig 	snprintf(fname + len - 2, 20, "%d", getpid());
    226  1.344    rillig 
    227  1.391    rillig     opts.debug_file = fopen(fname, mode);
    228  1.391    rillig     if (!opts.debug_file) {
    229  1.344    rillig 	fprintf(stderr, "Cannot open debug file %s\n",
    230  1.344    rillig 		fname);
    231  1.344    rillig 	usage();
    232  1.344    rillig     }
    233  1.344    rillig     free(fname);
    234  1.344    rillig }
    235  1.344    rillig 
    236  1.344    rillig static void
    237  1.133       dsl parse_debug_options(const char *argvalue)
    238  1.133       dsl {
    239  1.133       dsl 	const char *modules;
    240  1.133       dsl 
    241  1.133       dsl 	for (modules = argvalue; *modules; ++modules) {
    242  1.133       dsl 		switch (*modules) {
    243  1.369    rillig 		case '0':	/* undocumented, only intended for tests */
    244  1.391    rillig 			opts.debug &= DEBUG_LINT;
    245  1.369    rillig 			break;
    246  1.133       dsl 		case 'A':
    247  1.391    rillig 			opts.debug = ~(0|DEBUG_LINT);
    248  1.133       dsl 			break;
    249  1.133       dsl 		case 'a':
    250  1.391    rillig 			opts.debug |= DEBUG_ARCH;
    251  1.133       dsl 			break;
    252  1.155  christos 		case 'C':
    253  1.391    rillig 			opts.debug |= DEBUG_CWD;
    254  1.155  christos 			break;
    255  1.133       dsl 		case 'c':
    256  1.391    rillig 			opts.debug |= DEBUG_COND;
    257  1.133       dsl 			break;
    258  1.133       dsl 		case 'd':
    259  1.391    rillig 			opts.debug |= DEBUG_DIR;
    260  1.133       dsl 			break;
    261  1.133       dsl 		case 'e':
    262  1.391    rillig 			opts.debug |= DEBUG_ERROR;
    263  1.133       dsl 			break;
    264  1.133       dsl 		case 'f':
    265  1.391    rillig 			opts.debug |= DEBUG_FOR;
    266  1.133       dsl 			break;
    267  1.133       dsl 		case 'g':
    268  1.133       dsl 			if (modules[1] == '1') {
    269  1.391    rillig 				opts.debug |= DEBUG_GRAPH1;
    270  1.133       dsl 				++modules;
    271  1.133       dsl 			}
    272  1.133       dsl 			else if (modules[1] == '2') {
    273  1.391    rillig 				opts.debug |= DEBUG_GRAPH2;
    274  1.133       dsl 				++modules;
    275  1.133       dsl 			}
    276  1.133       dsl 			else if (modules[1] == '3') {
    277  1.391    rillig 				opts.debug |= DEBUG_GRAPH3;
    278  1.133       dsl 				++modules;
    279  1.133       dsl 			}
    280  1.133       dsl 			break;
    281  1.280       sjg 		case 'h':
    282  1.391    rillig 			opts.debug |= DEBUG_HASH;
    283  1.280       sjg 			break;
    284  1.133       dsl 		case 'j':
    285  1.391    rillig 			opts.debug |= DEBUG_JOB;
    286  1.133       dsl 			break;
    287  1.285       sjg 		case 'L':
    288  1.391    rillig 			opts.debug |= DEBUG_LINT;
    289  1.285       sjg 			break;
    290  1.146       sjg 		case 'l':
    291  1.391    rillig 			opts.debug |= DEBUG_LOUD;
    292  1.146       sjg 			break;
    293  1.190       sjg 		case 'M':
    294  1.391    rillig 			opts.debug |= DEBUG_META;
    295  1.190       sjg 			break;
    296  1.133       dsl 		case 'm':
    297  1.391    rillig 			opts.debug |= DEBUG_MAKE;
    298  1.133       dsl 			break;
    299  1.133       dsl 		case 'n':
    300  1.391    rillig 			opts.debug |= DEBUG_SCRIPT;
    301  1.133       dsl 			break;
    302  1.133       dsl 		case 'p':
    303  1.391    rillig 			opts.debug |= DEBUG_PARSE;
    304  1.133       dsl 			break;
    305  1.133       dsl 		case 's':
    306  1.391    rillig 			opts.debug |= DEBUG_SUFF;
    307  1.133       dsl 			break;
    308  1.133       dsl 		case 't':
    309  1.391    rillig 			opts.debug |= DEBUG_TARG;
    310  1.133       dsl 			break;
    311  1.201       sjg 		case 'V':
    312  1.394    rillig 			opts.debugVflag = TRUE;
    313  1.201       sjg 			break;
    314  1.133       dsl 		case 'v':
    315  1.391    rillig 			opts.debug |= DEBUG_VAR;
    316  1.133       dsl 			break;
    317  1.133       dsl 		case 'x':
    318  1.391    rillig 			opts.debug |= DEBUG_SHELL;
    319  1.133       dsl 			break;
    320  1.133       dsl 		case 'F':
    321  1.344    rillig 			parse_debug_option_F(modules + 1);
    322  1.153       apb 			goto debug_setbuf;
    323  1.133       dsl 		default:
    324  1.133       dsl 			(void)fprintf(stderr,
    325  1.133       dsl 			    "%s: illegal argument to d option -- %c\n",
    326  1.133       dsl 			    progname, *modules);
    327  1.133       dsl 			usage();
    328  1.133       dsl 		}
    329  1.133       dsl 	}
    330  1.153       apb debug_setbuf:
    331  1.153       apb 	/*
    332  1.153       apb 	 * Make the debug_file unbuffered, and make
    333  1.153       apb 	 * stdout line buffered (unless debugfile == stdout).
    334  1.153       apb 	 */
    335  1.391    rillig 	setvbuf(opts.debug_file, NULL, _IONBF, 0);
    336  1.391    rillig 	if (opts.debug_file != stdout) {
    337  1.153       apb 		setvbuf(stdout, NULL, _IOLBF, 0);
    338  1.153       apb 	}
    339  1.133       dsl }
    340  1.133       dsl 
    341  1.264       sjg /*
    342  1.264       sjg  * does path contain any relative components
    343  1.264       sjg  */
    344  1.286    rillig static Boolean
    345  1.264       sjg is_relpath(const char *path)
    346  1.264       sjg {
    347  1.264       sjg 	const char *cp;
    348  1.264       sjg 
    349  1.264       sjg 	if (path[0] != '/')
    350  1.264       sjg 		return TRUE;
    351  1.264       sjg 	cp = path;
    352  1.286    rillig 	while ((cp = strstr(cp, "/.")) != NULL) {
    353  1.264       sjg 		cp += 2;
    354  1.264       sjg 		if (cp[0] == '/' || cp[0] == '\0')
    355  1.264       sjg 			return TRUE;
    356  1.264       sjg 		else if (cp[0] == '.') {
    357  1.264       sjg 			if (cp[1] == '/' || cp[1] == '\0')
    358  1.264       sjg 				return TRUE;
    359  1.264       sjg 		}
    360  1.286    rillig 	}
    361  1.264       sjg 	return FALSE;
    362  1.264       sjg }
    363  1.264       sjg 
    364  1.355    rillig static void
    365  1.355    rillig MainParseArgChdir(const char *argvalue)
    366  1.355    rillig {
    367  1.355    rillig 	struct stat sa, sb;
    368  1.355    rillig 
    369  1.355    rillig 	if (chdir(argvalue) == -1) {
    370  1.355    rillig 		(void)fprintf(stderr, "%s: chdir %s: %s\n",
    371  1.355    rillig 		    progname, argvalue, strerror(errno));
    372  1.355    rillig 		exit(1);
    373  1.355    rillig 	}
    374  1.355    rillig 	if (getcwd(curdir, MAXPATHLEN) == NULL) {
    375  1.355    rillig 		(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
    376  1.355    rillig 		exit(2);
    377  1.355    rillig 	}
    378  1.355    rillig 	if (!is_relpath(argvalue) &&
    379  1.355    rillig 	    stat(argvalue, &sa) != -1 &&
    380  1.355    rillig 	    stat(curdir, &sb) != -1 &&
    381  1.355    rillig 	    sa.st_ino == sb.st_ino &&
    382  1.355    rillig 	    sa.st_dev == sb.st_dev)
    383  1.355    rillig 		strncpy(curdir, argvalue, MAXPATHLEN);
    384  1.355    rillig 	ignorePWD = TRUE;
    385  1.355    rillig }
    386  1.355    rillig 
    387  1.356    rillig static void
    388  1.356    rillig MainParseArgJobsInternal(const char *argvalue)
    389  1.356    rillig {
    390  1.356    rillig 	if (sscanf(argvalue, "%d,%d", &jp_0, &jp_1) != 2) {
    391  1.356    rillig 		(void)fprintf(stderr,
    392  1.356    rillig 			      "%s: internal error -- J option malformed (%s)\n",
    393  1.356    rillig 			      progname, argvalue);
    394  1.356    rillig 		usage();
    395  1.356    rillig 	}
    396  1.356    rillig 	if ((fcntl(jp_0, F_GETFD, 0) < 0) ||
    397  1.356    rillig 	    (fcntl(jp_1, F_GETFD, 0) < 0)) {
    398  1.356    rillig #if 0
    399  1.356    rillig 		(void)fprintf(stderr,
    400  1.356    rillig 		    "%s: ###### warning -- J descriptors were closed!\n",
    401  1.356    rillig 		    progname);
    402  1.356    rillig 		exit(2);
    403  1.356    rillig #endif
    404  1.356    rillig 		jp_0 = -1;
    405  1.356    rillig 		jp_1 = -1;
    406  1.391    rillig 		opts.compatMake = TRUE;
    407  1.356    rillig 	} else {
    408  1.356    rillig 		Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
    409  1.356    rillig 		Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
    410  1.356    rillig 	}
    411  1.356    rillig }
    412  1.356    rillig 
    413  1.356    rillig static void
    414  1.356    rillig MainParseArgJobs(const char *argvalue)
    415  1.356    rillig {
    416  1.356    rillig 	char *p;
    417  1.356    rillig 
    418  1.356    rillig 	forceJobs = TRUE;
    419  1.391    rillig 	opts.maxJobs = (int)strtol(argvalue, &p, 0);
    420  1.391    rillig 	if (*p != '\0' || opts.maxJobs < 1) {
    421  1.356    rillig 		(void)fprintf(stderr,
    422  1.356    rillig 		    "%s: illegal argument to -j -- must be positive integer!\n",
    423  1.356    rillig 		    progname);
    424  1.356    rillig 		exit(1);	/* XXX: why not 2? */
    425  1.356    rillig 	}
    426  1.356    rillig 	Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
    427  1.356    rillig 	Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
    428  1.356    rillig 	Var_Set(".MAKE.JOBS", argvalue, VAR_GLOBAL);
    429  1.391    rillig 	maxJobTokens = opts.maxJobs;
    430  1.356    rillig }
    431  1.356    rillig 
    432  1.357    rillig static void
    433  1.357    rillig MainParseArgSysInc(const char *argvalue)
    434  1.357    rillig {
    435  1.357    rillig 	/* look for magic parent directory search string */
    436  1.357    rillig 	if (strncmp(".../", argvalue, 4) == 0) {
    437  1.371    rillig 		char *found_path = Dir_FindHereOrAbove(curdir, argvalue + 4);
    438  1.371    rillig 		if (found_path == NULL)
    439  1.357    rillig 			return;
    440  1.357    rillig 		(void)Dir_AddDir(sysIncPath, found_path);
    441  1.371    rillig 		free(found_path);
    442  1.357    rillig 	} else {
    443  1.357    rillig 		(void)Dir_AddDir(sysIncPath, argvalue);
    444  1.357    rillig 	}
    445  1.357    rillig 	Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
    446  1.357    rillig 	Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
    447  1.357    rillig }
    448  1.357    rillig 
    449  1.354    rillig static Boolean
    450  1.360    rillig MainParseArg(char c, const char *argvalue)
    451  1.354    rillig {
    452  1.354    rillig 	switch (c) {
    453  1.354    rillig 	case '\0':
    454  1.354    rillig 		break;
    455  1.354    rillig 	case 'B':
    456  1.391    rillig 		opts.compatMake = TRUE;
    457  1.354    rillig 		Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
    458  1.354    rillig 		Var_Set(MAKE_MODE, "compat", VAR_GLOBAL);
    459  1.354    rillig 		break;
    460  1.354    rillig 	case 'C':
    461  1.355    rillig 		MainParseArgChdir(argvalue);
    462  1.354    rillig 		break;
    463  1.354    rillig 	case 'D':
    464  1.359    rillig 		if (argvalue[0] == '\0') return FALSE;
    465  1.354    rillig 		Var_Set(argvalue, "1", VAR_GLOBAL);
    466  1.354    rillig 		Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
    467  1.354    rillig 		Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
    468  1.354    rillig 		break;
    469  1.354    rillig 	case 'I':
    470  1.354    rillig 		Parse_AddIncludeDir(argvalue);
    471  1.354    rillig 		Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
    472  1.354    rillig 		Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
    473  1.354    rillig 		break;
    474  1.354    rillig 	case 'J':
    475  1.356    rillig 		MainParseArgJobsInternal(argvalue);
    476  1.354    rillig 		break;
    477  1.354    rillig 	case 'N':
    478  1.391    rillig 		opts.noExecute = TRUE;
    479  1.391    rillig 		opts.noRecursiveExecute = TRUE;
    480  1.354    rillig 		Var_Append(MAKEFLAGS, "-N", VAR_GLOBAL);
    481  1.354    rillig 		break;
    482  1.354    rillig 	case 'S':
    483  1.391    rillig 		opts.keepgoing = FALSE;
    484  1.354    rillig 		Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
    485  1.354    rillig 		break;
    486  1.354    rillig 	case 'T':
    487  1.354    rillig 		tracefile = bmake_strdup(argvalue);
    488  1.354    rillig 		Var_Append(MAKEFLAGS, "-T", VAR_GLOBAL);
    489  1.354    rillig 		Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
    490  1.354    rillig 		break;
    491  1.354    rillig 	case 'V':
    492  1.354    rillig 	case 'v':
    493  1.391    rillig 		opts.printVars = c == 'v' ? EXPAND_VARS : COMPAT_VARS;
    494  1.391    rillig 		Lst_Append(opts.variables, bmake_strdup(argvalue));
    495  1.391    rillig 		/* XXX: Why always -V? */
    496  1.354    rillig 		Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
    497  1.354    rillig 		Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
    498  1.354    rillig 		break;
    499  1.354    rillig 	case 'W':
    500  1.391    rillig 		opts.parseWarnFatal = TRUE;
    501  1.354    rillig 		break;
    502  1.354    rillig 	case 'X':
    503  1.391    rillig 		opts.varNoExportEnv = TRUE;
    504  1.354    rillig 		Var_Append(MAKEFLAGS, "-X", VAR_GLOBAL);
    505  1.354    rillig 		break;
    506  1.354    rillig 	case 'd':
    507  1.354    rillig 		/* If '-d-opts' don't pass to children */
    508  1.354    rillig 		if (argvalue[0] == '-')
    509  1.354    rillig 			argvalue++;
    510  1.354    rillig 		else {
    511  1.354    rillig 			Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
    512  1.354    rillig 			Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
    513  1.354    rillig 		}
    514  1.354    rillig 		parse_debug_options(argvalue);
    515  1.354    rillig 		break;
    516  1.354    rillig 	case 'e':
    517  1.391    rillig 		opts.checkEnvFirst = TRUE;
    518  1.354    rillig 		Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
    519  1.354    rillig 		break;
    520  1.354    rillig 	case 'f':
    521  1.391    rillig 		Lst_Append(opts.makefiles, bmake_strdup(argvalue));
    522  1.354    rillig 		break;
    523  1.354    rillig 	case 'i':
    524  1.391    rillig 		opts.ignoreErrors = TRUE;
    525  1.354    rillig 		Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
    526  1.354    rillig 		break;
    527  1.354    rillig 	case 'j':
    528  1.356    rillig 		MainParseArgJobs(argvalue);
    529  1.354    rillig 		break;
    530  1.354    rillig 	case 'k':
    531  1.391    rillig 		opts.keepgoing = TRUE;
    532  1.354    rillig 		Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
    533  1.354    rillig 		break;
    534  1.354    rillig 	case 'm':
    535  1.357    rillig 		MainParseArgSysInc(argvalue);
    536  1.354    rillig 		break;
    537  1.354    rillig 	case 'n':
    538  1.391    rillig 		opts.noExecute = TRUE;
    539  1.354    rillig 		Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
    540  1.354    rillig 		break;
    541  1.354    rillig 	case 'q':
    542  1.391    rillig 		opts.queryFlag = TRUE;
    543  1.354    rillig 		/* Kind of nonsensical, wot? */
    544  1.354    rillig 		Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
    545  1.354    rillig 		break;
    546  1.354    rillig 	case 'r':
    547  1.391    rillig 		opts.noBuiltins = TRUE;
    548  1.354    rillig 		Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
    549  1.354    rillig 		break;
    550  1.354    rillig 	case 's':
    551  1.391    rillig 		opts.beSilent = TRUE;
    552  1.354    rillig 		Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
    553  1.354    rillig 		break;
    554  1.354    rillig 	case 't':
    555  1.391    rillig 		opts.touchFlag = TRUE;
    556  1.354    rillig 		Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
    557  1.354    rillig 		break;
    558  1.354    rillig 	case 'w':
    559  1.391    rillig 		opts.enterFlag = TRUE;
    560  1.354    rillig 		Var_Append(MAKEFLAGS, "-w", VAR_GLOBAL);
    561  1.354    rillig 		break;
    562  1.354    rillig 	default:
    563  1.354    rillig 	case '?':
    564  1.354    rillig 		usage();
    565  1.354    rillig 	}
    566  1.354    rillig 	return TRUE;
    567  1.354    rillig }
    568  1.354    rillig 
    569  1.345    rillig /* Parse the given arguments.  Called from main() and from
    570  1.345    rillig  * Main_ParseArgLine() when the .MAKEFLAGS target is used.
    571    1.1       cgd  *
    572  1.345    rillig  * The arguments must be treated as read-only and will be freed after the
    573  1.345    rillig  * call.
    574    1.1       cgd  *
    575  1.345    rillig  * XXX: Deal with command line overriding .MAKEFLAGS in makefile */
    576    1.1       cgd static void
    577  1.149  christos MainParseArgs(int argc, char **argv)
    578    1.1       cgd {
    579  1.360    rillig 	char c;
    580   1.98      ross 	int arginc;
    581  1.133       dsl 	char *argvalue;
    582   1.98      ross 	char *optscan;
    583  1.101      ross 	Boolean inOption, dashDash = FALSE;
    584    1.1       cgd 
    585  1.359    rillig 	const char *optspecs = "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstv:w";
    586   1.98      ross /* Can't actually use getopt(3) because rescanning is not portable */
    587   1.98      ross 
    588  1.279    rillig rearg:
    589   1.98      ross 	inOption = FALSE;
    590  1.108     lukem 	optscan = NULL;
    591  1.358    rillig 	while (argc > 1) {
    592  1.359    rillig 		const char *optspec;
    593  1.358    rillig 		if (!inOption)
    594   1.98      ross 			optscan = argv[1];
    595   1.98      ross 		c = *optscan++;
    596   1.98      ross 		arginc = 0;
    597  1.358    rillig 		if (inOption) {
    598  1.358    rillig 			if (c == '\0') {
    599   1.98      ross 				++argv;
    600   1.98      ross 				--argc;
    601   1.98      ross 				inOption = FALSE;
    602   1.98      ross 				continue;
    603   1.98      ross 			}
    604   1.98      ross 		} else {
    605  1.101      ross 			if (c != '-' || dashDash)
    606   1.98      ross 				break;
    607   1.98      ross 			inOption = TRUE;
    608   1.98      ross 			c = *optscan++;
    609   1.98      ross 		}
    610   1.98      ross 		/* '-' found at some earlier point */
    611  1.359    rillig 		optspec = strchr(optspecs, c);
    612  1.359    rillig 		if (c != '\0' && optspec != NULL && optspec[1] == ':') {
    613   1.98      ross 			/* -<something> found, and <something> should have an arg */
    614   1.98      ross 			inOption = FALSE;
    615   1.98      ross 			arginc = 1;
    616   1.98      ross 			argvalue = optscan;
    617  1.358    rillig 			if (*argvalue == '\0') {
    618  1.122  christos 				if (argc < 3)
    619  1.122  christos 					goto noarg;
    620   1.98      ross 				argvalue = argv[2];
    621   1.98      ross 				arginc = 2;
    622   1.98      ross 			}
    623  1.108     lukem 		} else {
    624  1.279    rillig 			argvalue = NULL;
    625   1.98      ross 		}
    626  1.354    rillig 		switch (c) {
    627   1.98      ross 		case '\0':
    628  1.358    rillig 			arginc = 1;
    629  1.358    rillig 			inOption = FALSE;
    630  1.358    rillig 			break;
    631  1.101      ross 		case '-':
    632  1.358    rillig 			dashDash = TRUE;
    633  1.358    rillig 			break;
    634    1.1       cgd 		default:
    635  1.358    rillig 			if (!MainParseArg(c, argvalue))
    636  1.358    rillig 				goto noarg;
    637    1.1       cgd 		}
    638   1.98      ross 		argv += arginc;
    639   1.98      ross 		argc -= arginc;
    640    1.1       cgd 	}
    641    1.1       cgd 
    642    1.1       cgd 	oldVars = TRUE;
    643    1.1       cgd 
    644    1.1       cgd 	/*
    645    1.1       cgd 	 * See if the rest of the arguments are variable assignments and
    646    1.1       cgd 	 * perform them if so. Else take them to be targets and stuff them
    647    1.1       cgd 	 * on the end of the "create" list.
    648    1.1       cgd 	 */
    649  1.369    rillig 	for (; argc > 1; ++argv, --argc) {
    650  1.369    rillig 		VarAssign var;
    651  1.369    rillig 		if (Parse_IsVar(argv[1], &var)) {
    652  1.410    rillig 			Parse_DoVar(&var, VAR_CMDLINE);
    653   1.67       sjg 		} else {
    654   1.98      ross 			if (!*argv[1])
    655    1.1       cgd 				Punt("illegal (null) argument.");
    656  1.101      ross 			if (*argv[1] == '-' && !dashDash)
    657    1.1       cgd 				goto rearg;
    658  1.391    rillig 			Lst_Append(opts.create, bmake_strdup(argv[1]));
    659    1.1       cgd 		}
    660  1.369    rillig 	}
    661  1.122  christos 
    662  1.122  christos 	return;
    663  1.122  christos noarg:
    664  1.122  christos 	(void)fprintf(stderr, "%s: option requires an argument -- %c\n",
    665  1.122  christos 	    progname, c);
    666  1.122  christos 	usage();
    667    1.1       cgd }
    668    1.1       cgd 
    669  1.361    rillig /* Break a line of arguments into words and parse them.
    670   1.85       wiz  *
    671  1.361    rillig  * Used when a .MFLAGS or .MAKEFLAGS target is encountered during parsing and
    672  1.361    rillig  * by main() when reading the MAKEFLAGS environment variable. */
    673    1.1       cgd void
    674  1.161       dsl Main_ParseArgLine(const char *line)
    675    1.1       cgd {
    676  1.331    rillig 	Words words;
    677  1.412    rillig 	void *p1;
    678  1.289    rillig 	const char *argv0 = Var_Value(".MAKE", VAR_GLOBAL, &p1);
    679  1.296    rillig 	char *buf;
    680    1.1       cgd 
    681    1.1       cgd 	if (line == NULL)
    682    1.1       cgd 		return;
    683   1.13       cgd 	for (; *line == ' '; ++line)
    684   1.13       cgd 		continue;
    685    1.1       cgd 	if (!*line)
    686    1.1       cgd 		return;
    687    1.1       cgd 
    688  1.303    rillig 	buf = str_concat3(argv0, " ", line);
    689  1.238  christos 	free(p1);
    690   1.40  christos 
    691  1.331    rillig 	words = Str_Words(buf, TRUE);
    692  1.331    rillig 	if (words.words == NULL) {
    693  1.159  christos 		Error("Unterminated quoted string [%s]", buf);
    694  1.159  christos 		free(buf);
    695  1.159  christos 		return;
    696  1.159  christos 	}
    697   1.45     itohy 	free(buf);
    698  1.331    rillig 	MainParseArgs((int)words.len, words.words);
    699   1.40  christos 
    700  1.331    rillig 	Words_Free(words);
    701    1.1       cgd }
    702    1.1       cgd 
    703   1.74        tv Boolean
    704  1.252  christos Main_SetObjdir(const char *fmt, ...)
    705   1.31  christos {
    706   1.31  christos 	struct stat sb;
    707  1.260  christos 	char *path;
    708  1.260  christos 	char buf[MAXPATHLEN + 1];
    709  1.265       sjg 	char buf2[MAXPATHLEN + 1];
    710   1.76        tv 	Boolean rc = FALSE;
    711  1.252  christos 	va_list ap;
    712  1.252  christos 
    713  1.252  christos 	va_start(ap, fmt);
    714  1.260  christos 	vsnprintf(path = buf, MAXPATHLEN, fmt, ap);
    715  1.252  christos 	va_end(ap);
    716   1.31  christos 
    717   1.74        tv 	if (path[0] != '/') {
    718  1.263       sjg 		snprintf(buf2, MAXPATHLEN, "%s/%s", curdir, path);
    719  1.263       sjg 		path = buf2;
    720   1.74        tv 	}
    721   1.74        tv 
    722   1.74        tv 	/* look for the directory and try to chdir there */
    723   1.31  christos 	if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
    724  1.413       sjg 		if (access(path, W_OK) || chdir(path)) {
    725   1.31  christos 			(void)fprintf(stderr, "make warning: %s: %s.\n",
    726   1.31  christos 				      path, strerror(errno));
    727   1.74        tv 		} else {
    728  1.295    rillig 			snprintf(objdir, sizeof objdir, "%s", path);
    729  1.277    rillig 			Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
    730   1.74        tv 			setenv("PWD", objdir, 1);
    731   1.79        tv 			Dir_InitDot();
    732  1.262  riastrad 			purge_cached_realpaths();
    733   1.74        tv 			rc = TRUE;
    734  1.391    rillig 			if (opts.enterFlag && strcmp(objdir, curdir) != 0)
    735  1.233       sjg 				enterFlagObj = TRUE;
    736   1.31  christos 		}
    737   1.31  christos 	}
    738   1.31  christos 
    739   1.74        tv 	return rc;
    740   1.31  christos }
    741   1.31  christos 
    742  1.252  christos static Boolean
    743  1.253  christos Main_SetVarObjdir(const char *var, const char *suffix)
    744  1.252  christos {
    745  1.412    rillig 	void *path_freeIt;
    746  1.410    rillig 	const char *path = Var_Value(var, VAR_CMDLINE, &path_freeIt);
    747  1.296    rillig 	const char *xpath;
    748  1.296    rillig 	char *xpath_freeIt;
    749  1.296    rillig 
    750  1.288    rillig 	if (path == NULL || path[0] == '\0') {
    751  1.290    rillig 		bmake_free(path_freeIt);
    752  1.252  christos 		return FALSE;
    753  1.288    rillig 	}
    754  1.252  christos 
    755  1.260  christos 	/* expand variable substitutions */
    756  1.296    rillig 	xpath = path;
    757  1.296    rillig 	xpath_freeIt = NULL;
    758  1.341    rillig 	if (strchr(path, '$') != 0) {
    759  1.341    rillig 		(void)Var_Subst(path, VAR_GLOBAL, VARE_WANTRES, &xpath_freeIt);
    760  1.341    rillig 		/* TODO: handle errors */
    761  1.341    rillig 		xpath = xpath_freeIt;
    762  1.341    rillig 	}
    763  1.260  christos 
    764  1.260  christos 	(void)Main_SetObjdir("%s%s", xpath, suffix);
    765  1.260  christos 
    766  1.290    rillig 	bmake_free(xpath_freeIt);
    767  1.290    rillig 	bmake_free(path_freeIt);
    768  1.252  christos 	return TRUE;
    769  1.252  christos }
    770  1.252  christos 
    771  1.325    rillig /* Read and parse the makefile.
    772  1.387    rillig  * Return TRUE if reading the makefile succeeded. */
    773  1.386    rillig static int
    774  1.386    rillig ReadMakefileSucceeded(void *fname, void *unused)
    775  1.325    rillig {
    776  1.325    rillig 	return ReadMakefile(fname) == 0;
    777  1.325    rillig }
    778  1.325    rillig 
    779  1.197       sjg int
    780  1.338    rillig str2Lst_Append(StringList *lp, char *str, const char *sep)
    781  1.176       sjg {
    782  1.176       sjg     char *cp;
    783  1.176       sjg     int n;
    784  1.176       sjg 
    785  1.176       sjg     if (!sep)
    786  1.176       sjg 	sep = " \t";
    787  1.176       sjg 
    788  1.176       sjg     for (n = 0, cp = strtok(str, sep); cp; cp = strtok(NULL, sep)) {
    789  1.320    rillig 	Lst_Append(lp, cp);
    790  1.176       sjg 	n++;
    791  1.176       sjg     }
    792  1.278    rillig     return n;
    793  1.176       sjg }
    794  1.176       sjg 
    795  1.167  christos #ifdef SIGINFO
    796  1.167  christos /*ARGSUSED*/
    797  1.167  christos static void
    798  1.200     joerg siginfo(int signo MAKE_ATTR_UNUSED)
    799  1.167  christos {
    800  1.167  christos 	char dir[MAXPATHLEN];
    801  1.167  christos 	char str[2 * MAXPATHLEN];
    802  1.167  christos 	int len;
    803  1.167  christos 	if (getcwd(dir, sizeof(dir)) == NULL)
    804  1.167  christos 		return;
    805  1.168  christos 	len = snprintf(str, sizeof(str), "%s: Working in: %s\n", progname, dir);
    806  1.167  christos 	if (len > 0)
    807  1.167  christos 		(void)write(STDERR_FILENO, str, (size_t)len);
    808  1.167  christos }
    809  1.167  christos #endif
    810  1.167  christos 
    811  1.176       sjg /*
    812  1.176       sjg  * Allow makefiles some control over the mode we run in.
    813  1.176       sjg  */
    814  1.176       sjg void
    815  1.176       sjg MakeMode(const char *mode)
    816  1.176       sjg {
    817  1.322    rillig     char *mode_freeIt = NULL;
    818  1.176       sjg 
    819  1.341    rillig     if (mode == NULL) {
    820  1.341    rillig 	(void)Var_Subst("${" MAKE_MODE ":tl}",
    821  1.341    rillig 		  VAR_GLOBAL, VARE_WANTRES, &mode_freeIt);
    822  1.341    rillig 	/* TODO: handle errors */
    823  1.341    rillig 	mode = mode_freeIt;
    824  1.341    rillig     }
    825  1.176       sjg 
    826  1.322    rillig     if (mode[0] != '\0') {
    827  1.176       sjg 	if (strstr(mode, "compat")) {
    828  1.391    rillig 	    opts.compatMake = TRUE;
    829  1.176       sjg 	    forceJobs = FALSE;
    830  1.176       sjg 	}
    831  1.190       sjg #if USE_META
    832  1.190       sjg 	if (strstr(mode, "meta"))
    833  1.210       sjg 	    meta_mode_init(mode);
    834  1.190       sjg #endif
    835  1.176       sjg     }
    836  1.238  christos 
    837  1.322    rillig     free(mode_freeIt);
    838  1.176       sjg }
    839  1.176       sjg 
    840  1.266  christos static void
    841  1.383    rillig PrintVar(const char *varname, Boolean expandVars)
    842  1.383    rillig {
    843  1.383    rillig 	if (strchr(varname, '$')) {
    844  1.384    rillig 		char *evalue;
    845  1.384    rillig 		(void)Var_Subst(varname, VAR_GLOBAL, VARE_WANTRES, &evalue);
    846  1.383    rillig 		/* TODO: handle errors */
    847  1.384    rillig 		printf("%s\n", evalue);
    848  1.384    rillig 		bmake_free(evalue);
    849  1.384    rillig 
    850  1.383    rillig 	} else if (expandVars) {
    851  1.383    rillig 		char *expr = str_concat3("${", varname, "}");
    852  1.384    rillig 		char *evalue;
    853  1.384    rillig 		(void)Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES, &evalue);
    854  1.383    rillig 		/* TODO: handle errors */
    855  1.383    rillig 		free(expr);
    856  1.384    rillig 		printf("%s\n", evalue);
    857  1.384    rillig 		bmake_free(evalue);
    858  1.384    rillig 
    859  1.383    rillig 	} else {
    860  1.412    rillig 		void *freeIt;
    861  1.384    rillig 		const char *value = Var_Value(varname, VAR_GLOBAL, &freeIt);
    862  1.384    rillig 		printf("%s\n", value ? value : "");
    863  1.384    rillig 		bmake_free(freeIt);
    864  1.383    rillig 	}
    865  1.383    rillig }
    866  1.383    rillig 
    867  1.383    rillig static void
    868  1.266  christos doPrintVars(void)
    869  1.266  christos {
    870  1.338    rillig 	StringListNode *ln;
    871  1.266  christos 	Boolean expandVars;
    872  1.266  christos 
    873  1.391    rillig 	if (opts.printVars == EXPAND_VARS)
    874  1.272  christos 		expandVars = TRUE;
    875  1.394    rillig 	else if (opts.debugVflag)
    876  1.266  christos 		expandVars = FALSE;
    877  1.266  christos 	else
    878  1.272  christos 		expandVars = getBoolean(".MAKE.EXPAND_VARIABLES", FALSE);
    879  1.266  christos 
    880  1.391    rillig 	for (ln = opts.variables->first; ln != NULL; ln = ln->next) {
    881  1.383    rillig 		const char *varname = ln->datum;
    882  1.383    rillig 		PrintVar(varname, expandVars);
    883  1.266  christos 	}
    884  1.266  christos }
    885  1.266  christos 
    886  1.266  christos static Boolean
    887  1.266  christos runTargets(void)
    888  1.266  christos {
    889  1.338    rillig 	GNodeList *targs; /* target nodes to create -- passed to Make_Init */
    890  1.351    rillig 	Boolean outOfDate;	/* FALSE if all targets up to date */
    891  1.266  christos 
    892  1.266  christos 	/*
    893  1.266  christos 	 * Have now read the entire graph and need to make a list of
    894  1.266  christos 	 * targets to create. If none was given on the command line,
    895  1.266  christos 	 * we consult the parsing module to find the main target(s)
    896  1.266  christos 	 * to create.
    897  1.266  christos 	 */
    898  1.391    rillig 	if (Lst_IsEmpty(opts.create))
    899  1.266  christos 		targs = Parse_MainName();
    900  1.266  christos 	else
    901  1.391    rillig 		targs = Targ_FindList(opts.create);
    902  1.266  christos 
    903  1.391    rillig 	if (!opts.compatMake) {
    904  1.266  christos 		/*
    905  1.266  christos 		 * Initialize job module before traversing the graph
    906  1.266  christos 		 * now that any .BEGIN and .END targets have been read.
    907  1.266  christos 		 * This is done only if the -q flag wasn't given
    908  1.266  christos 		 * (to prevent the .BEGIN from being executed should
    909  1.266  christos 		 * it exist).
    910  1.266  christos 		 */
    911  1.391    rillig 		if (!opts.queryFlag) {
    912  1.266  christos 			Job_Init();
    913  1.266  christos 			jobsRunning = TRUE;
    914  1.266  christos 		}
    915  1.266  christos 
    916  1.266  christos 		/* Traverse the graph, checking on all the targets */
    917  1.266  christos 		outOfDate = Make_Run(targs);
    918  1.266  christos 	} else {
    919  1.266  christos 		/*
    920  1.266  christos 		 * Compat_Init will take care of creating all the
    921  1.266  christos 		 * targets as well as initializing the module.
    922  1.266  christos 		 */
    923  1.266  christos 		Compat_Run(targs);
    924  1.266  christos 		outOfDate = FALSE;
    925  1.266  christos 	}
    926  1.320    rillig 	Lst_Free(targs);
    927  1.266  christos 	return outOfDate;
    928  1.266  christos }
    929  1.266  christos 
    930  1.343    rillig /*
    931  1.343    rillig  * Set up the .TARGETS variable to contain the list of targets to be
    932  1.343    rillig  * created. If none specified, make the variable empty -- the parser
    933  1.343    rillig  * will fill the thing in with the default or .MAIN target.
    934  1.343    rillig  */
    935  1.343    rillig static void
    936  1.343    rillig InitVarTargets(void)
    937  1.343    rillig {
    938  1.343    rillig 	StringListNode *ln;
    939  1.343    rillig 
    940  1.391    rillig 	if (Lst_IsEmpty(opts.create)) {
    941  1.343    rillig 		Var_Set(".TARGETS", "", VAR_GLOBAL);
    942  1.343    rillig 		return;
    943  1.343    rillig 	}
    944  1.343    rillig 
    945  1.391    rillig 	for (ln = opts.create->first; ln != NULL; ln = ln->next) {
    946  1.343    rillig 		char *name = ln->datum;
    947  1.343    rillig 		Var_Append(".TARGETS", name, VAR_GLOBAL);
    948  1.343    rillig 	}
    949  1.343    rillig }
    950  1.343    rillig 
    951  1.404    rillig static void
    952  1.404    rillig InitRandom(void)
    953  1.404    rillig {
    954  1.404    rillig 	struct timeval tv;
    955  1.404    rillig 
    956  1.404    rillig 	gettimeofday(&tv, NULL);
    957  1.404    rillig 	srandom((unsigned int)(tv.tv_sec + tv.tv_usec));
    958  1.404    rillig }
    959  1.404    rillig 
    960  1.369    rillig static const char *
    961  1.369    rillig init_machine(const struct utsname *utsname)
    962  1.369    rillig {
    963  1.369    rillig 	const char *machine = getenv("MACHINE");
    964  1.369    rillig 	if (machine != NULL)
    965  1.369    rillig 		return machine;
    966  1.369    rillig 
    967  1.369    rillig #ifdef MAKE_NATIVE
    968  1.369    rillig 	return utsname->machine;
    969  1.369    rillig #else
    970  1.369    rillig #ifdef MAKE_MACHINE
    971  1.369    rillig 	return MAKE_MACHINE;
    972  1.369    rillig #else
    973  1.369    rillig 	return "unknown";
    974  1.369    rillig #endif
    975  1.369    rillig #endif
    976  1.369    rillig }
    977  1.369    rillig 
    978  1.369    rillig static const char *
    979  1.369    rillig init_machine_arch(void)
    980  1.369    rillig {
    981  1.369    rillig 	const char *env = getenv("MACHINE_ARCH");
    982  1.369    rillig 	if (env != NULL)
    983  1.369    rillig 		return env;
    984  1.369    rillig 
    985  1.369    rillig #ifdef MAKE_NATIVE
    986  1.369    rillig 	{
    987  1.369    rillig 		struct utsname utsname;
    988  1.369    rillig 		static char machine_arch_buf[sizeof(utsname.machine)];
    989  1.369    rillig 		const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
    990  1.369    rillig 		size_t len = sizeof(machine_arch_buf);
    991  1.369    rillig 
    992  1.369    rillig 		if (sysctl(mib, __arraycount(mib), machine_arch_buf,
    993  1.369    rillig 			&len, NULL, 0) < 0) {
    994  1.369    rillig 		    (void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
    995  1.369    rillig 			strerror(errno));
    996  1.369    rillig 		    exit(2);
    997  1.369    rillig 		}
    998  1.369    rillig 
    999  1.369    rillig 		return machine_arch_buf;
   1000  1.369    rillig 	}
   1001  1.369    rillig #else
   1002  1.369    rillig #ifndef MACHINE_ARCH
   1003  1.369    rillig #ifdef MAKE_MACHINE_ARCH
   1004  1.369    rillig 	return MAKE_MACHINE_ARCH;
   1005  1.369    rillig #else
   1006  1.369    rillig 	return "unknown";
   1007  1.369    rillig #endif
   1008  1.369    rillig #else
   1009  1.369    rillig 	return MACHINE_ARCH;
   1010  1.369    rillig #endif
   1011  1.369    rillig #endif
   1012  1.369    rillig }
   1013  1.369    rillig 
   1014  1.381    rillig #ifndef NO_PWD_OVERRIDE
   1015  1.381    rillig /*
   1016  1.381    rillig  * All this code is so that we know where we are when we start up
   1017  1.381    rillig  * on a different machine with pmake.
   1018  1.382    rillig  *
   1019  1.381    rillig  * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
   1020  1.381    rillig  * since the value of curdir can vary depending on how we got
   1021  1.381    rillig  * here.  Ie sitting at a shell prompt (shell that provides $PWD)
   1022  1.381    rillig  * or via subdir.mk in which case its likely a shell which does
   1023  1.381    rillig  * not provide it.
   1024  1.382    rillig  *
   1025  1.381    rillig  * So, to stop it breaking this case only, we ignore PWD if
   1026  1.382    rillig  * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a variable expression.
   1027  1.381    rillig  */
   1028  1.381    rillig static void
   1029  1.382    rillig HandlePWD(const struct stat *curdir_st)
   1030  1.381    rillig {
   1031  1.382    rillig 	char *pwd;
   1032  1.412    rillig 	void *prefix_freeIt, *makeobjdir_freeIt;
   1033  1.382    rillig 	const char *makeobjdir;
   1034  1.382    rillig 	struct stat pwd_st;
   1035  1.381    rillig 
   1036  1.381    rillig 	if (ignorePWD || (pwd = getenv("PWD")) == NULL)
   1037  1.381    rillig 		return;
   1038  1.381    rillig 
   1039  1.410    rillig 	if (Var_Value("MAKEOBJDIRPREFIX", VAR_CMDLINE, &prefix_freeIt) != NULL) {
   1040  1.382    rillig 		bmake_free(prefix_freeIt);
   1041  1.382    rillig 		return;
   1042  1.381    rillig 	}
   1043  1.382    rillig 
   1044  1.410    rillig 	makeobjdir = Var_Value("MAKEOBJDIR", VAR_CMDLINE, &makeobjdir_freeIt);
   1045  1.382    rillig 	if (makeobjdir != NULL && strchr(makeobjdir, '$') != NULL)
   1046  1.382    rillig 		goto ignore_pwd;
   1047  1.382    rillig 
   1048  1.382    rillig 	if (stat(pwd, &pwd_st) == 0 &&
   1049  1.382    rillig 	    curdir_st->st_ino == pwd_st.st_ino &&
   1050  1.382    rillig 	    curdir_st->st_dev == pwd_st.st_dev)
   1051  1.382    rillig 		(void)strncpy(curdir, pwd, MAXPATHLEN);
   1052  1.382    rillig 
   1053  1.382    rillig ignore_pwd:
   1054  1.382    rillig 	bmake_free(makeobjdir_freeIt);
   1055  1.381    rillig }
   1056  1.381    rillig #endif
   1057  1.381    rillig 
   1058  1.407    rillig /*
   1059  1.407    rillig  * Find the .OBJDIR.  If MAKEOBJDIRPREFIX, or failing that,
   1060  1.407    rillig  * MAKEOBJDIR is set in the environment, try only that value
   1061  1.407    rillig  * and fall back to .CURDIR if it does not exist.
   1062  1.407    rillig  *
   1063  1.407    rillig  * Otherwise, try _PATH_OBJDIR.MACHINE-MACHINE_ARCH, _PATH_OBJDIR.MACHINE,
   1064  1.407    rillig  * and * finally _PATH_OBJDIRPREFIX`pwd`, in that order.  If none
   1065  1.407    rillig  * of these paths exist, just use .CURDIR.
   1066  1.407    rillig  */
   1067  1.407    rillig static void
   1068  1.407    rillig InitObjdir(const char *machine, const char *machine_arch)
   1069  1.407    rillig {
   1070  1.407    rillig 	Dir_InitDir(curdir);
   1071  1.407    rillig 	(void)Main_SetObjdir("%s", curdir);
   1072  1.407    rillig 
   1073  1.407    rillig 	if (!Main_SetVarObjdir("MAKEOBJDIRPREFIX", curdir) &&
   1074  1.407    rillig 	    !Main_SetVarObjdir("MAKEOBJDIR", "") &&
   1075  1.407    rillig 	    !Main_SetObjdir("%s.%s-%s", _PATH_OBJDIR, machine, machine_arch) &&
   1076  1.407    rillig 	    !Main_SetObjdir("%s.%s", _PATH_OBJDIR, machine) &&
   1077  1.407    rillig 	    !Main_SetObjdir("%s", _PATH_OBJDIR))
   1078  1.407    rillig 		(void)Main_SetObjdir("%s%s", _PATH_OBJDIRPREFIX, curdir);
   1079  1.407    rillig }
   1080  1.407    rillig 
   1081  1.393    rillig /* get rid of resource limit on file descriptors */
   1082  1.393    rillig static void
   1083  1.393    rillig UnlimitFiles(void)
   1084  1.393    rillig {
   1085  1.393    rillig #if defined(MAKE_NATIVE) || (defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE))
   1086  1.393    rillig 	struct rlimit rl;
   1087  1.393    rillig 	if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
   1088  1.393    rillig 	    rl.rlim_cur != rl.rlim_max) {
   1089  1.393    rillig 		rl.rlim_cur = rl.rlim_max;
   1090  1.393    rillig 		(void)setrlimit(RLIMIT_NOFILE, &rl);
   1091  1.393    rillig 	}
   1092  1.393    rillig #endif
   1093  1.393    rillig }
   1094  1.393    rillig 
   1095  1.395    rillig static void
   1096  1.395    rillig CmdOpts_Init(void)
   1097  1.395    rillig {
   1098  1.395    rillig 	opts.compatMake = FALSE;	/* No compat mode */
   1099  1.395    rillig 	opts.debug = 0;			/* No debug verbosity, please. */
   1100  1.395    rillig 	/* opts.debug_file has been initialized earlier */
   1101  1.395    rillig 	opts.debugVflag = FALSE;
   1102  1.396    rillig 	opts.checkEnvFirst = FALSE;
   1103  1.395    rillig 	opts.makefiles = Lst_New();
   1104  1.395    rillig 	opts.ignoreErrors = FALSE;	/* Pay attention to non-zero returns */
   1105  1.395    rillig 	opts.maxJobs = DEFMAXLOCAL;	/* Set default local max concurrency */
   1106  1.395    rillig 	opts.keepgoing = FALSE;		/* Stop on error */
   1107  1.395    rillig 	opts.noRecursiveExecute = FALSE; /* Execute all .MAKE targets */
   1108  1.395    rillig 	opts.noExecute = FALSE;		/* Execute all commands */
   1109  1.395    rillig 	opts.queryFlag = FALSE;		/* This is not just a check-run */
   1110  1.395    rillig 	opts.noBuiltins = FALSE;	/* Read the built-in rules */
   1111  1.395    rillig 	opts.beSilent = FALSE;		/* Print commands as executed */
   1112  1.395    rillig 	opts.touchFlag = FALSE;		/* Actually update targets */
   1113  1.395    rillig 	opts.printVars = 0;
   1114  1.395    rillig 	opts.variables = Lst_New();
   1115  1.396    rillig 	opts.parseWarnFatal = FALSE;
   1116  1.396    rillig 	opts.enterFlag = FALSE;
   1117  1.396    rillig 	opts.varNoExportEnv = FALSE;
   1118  1.395    rillig 	opts.create = Lst_New();
   1119  1.395    rillig }
   1120  1.395    rillig 
   1121  1.406    rillig /* Initialize MAKE and .MAKE to the path of the executable, so that it can be
   1122  1.406    rillig  * found by execvp(3) and the shells, even after a chdir.
   1123  1.406    rillig  *
   1124  1.406    rillig  * If it's a relative path and contains a '/', resolve it to an absolute path.
   1125  1.406    rillig  * Otherwise keep it as is, assuming it will be found in the PATH. */
   1126  1.397    rillig static void
   1127  1.406    rillig InitVarMake(const char *argv0)
   1128  1.405    rillig {
   1129  1.406    rillig 	const char *make = argv0;
   1130  1.405    rillig 
   1131  1.406    rillig 	if (argv0[0] != '/' && strchr(argv0, '/') != NULL) {
   1132  1.406    rillig 		char pathbuf[MAXPATHLEN];
   1133  1.406    rillig 		const char *abs = cached_realpath(argv0, pathbuf);
   1134  1.406    rillig 		struct stat st;
   1135  1.406    rillig 		if (abs != NULL && abs[0] == '/' && stat(make, &st) == 0)
   1136  1.406    rillig 			make = abs;
   1137  1.405    rillig 	}
   1138  1.406    rillig 
   1139  1.406    rillig 	Var_Set("MAKE", make, VAR_GLOBAL);
   1140  1.406    rillig 	Var_Set(".MAKE", make, VAR_GLOBAL);
   1141  1.405    rillig }
   1142  1.405    rillig 
   1143  1.405    rillig static void
   1144  1.409    rillig InitDefSysIncPath(char *syspath)
   1145  1.397    rillig {
   1146  1.397    rillig 	static char defsyspath[] = _PATH_DEFSYSPATH;
   1147  1.397    rillig 	char *start, *cp;
   1148  1.397    rillig 
   1149  1.397    rillig 	/*
   1150  1.397    rillig 	 * If no user-supplied system path was given (through the -m option)
   1151  1.397    rillig 	 * add the directories from the DEFSYSPATH (more than one may be given
   1152  1.397    rillig 	 * as dir1:...:dirn) to the system include path.
   1153  1.397    rillig 	 */
   1154  1.397    rillig 	/* XXX: mismatch: the -m option sets sysIncPath, not syspath */
   1155  1.397    rillig 	if (syspath == NULL || syspath[0] == '\0')
   1156  1.397    rillig 		syspath = defsyspath;
   1157  1.397    rillig 	else
   1158  1.397    rillig 		syspath = bmake_strdup(syspath);
   1159  1.397    rillig 
   1160  1.397    rillig 	for (start = syspath; *start != '\0'; start = cp) {
   1161  1.397    rillig 		for (cp = start; *cp != '\0' && *cp != ':'; cp++)
   1162  1.397    rillig 			continue;
   1163  1.397    rillig 		if (*cp == ':') {
   1164  1.397    rillig 			*cp++ = '\0';
   1165  1.397    rillig 		}
   1166  1.397    rillig 		/* look for magic parent directory search string */
   1167  1.397    rillig 		if (strncmp(".../", start, 4) != 0) {
   1168  1.409    rillig 			(void)Dir_AddDir(defSysIncPath, start);
   1169  1.397    rillig 		} else {
   1170  1.397    rillig 			char *dir = Dir_FindHereOrAbove(curdir, start + 4);
   1171  1.397    rillig 			if (dir != NULL) {
   1172  1.409    rillig 				(void)Dir_AddDir(defSysIncPath, dir);
   1173  1.397    rillig 				free(dir);
   1174  1.397    rillig 			}
   1175  1.397    rillig 		}
   1176  1.397    rillig 	}
   1177  1.397    rillig 
   1178  1.397    rillig 	if (syspath != defsyspath)
   1179  1.397    rillig 		free(syspath);
   1180  1.397    rillig }
   1181  1.397    rillig 
   1182  1.398    rillig static void
   1183  1.398    rillig ReadBuiltinRules(void)
   1184  1.398    rillig {
   1185  1.398    rillig 	StringList *sysMkPath = Lst_New();
   1186  1.398    rillig 	Dir_Expand(_PATH_DEFSYSMK,
   1187  1.409    rillig 		   Lst_IsEmpty(sysIncPath) ? defSysIncPath : sysIncPath,
   1188  1.398    rillig 		   sysMkPath);
   1189  1.398    rillig 	if (Lst_IsEmpty(sysMkPath))
   1190  1.398    rillig 		Fatal("%s: no system rules (%s).", progname, _PATH_DEFSYSMK);
   1191  1.398    rillig 	if (!Lst_ForEachUntil(sysMkPath, ReadMakefileSucceeded, NULL))
   1192  1.398    rillig 		Fatal("%s: cannot open %s.", progname,
   1193  1.398    rillig 		      (char *)sysMkPath->first->datum);
   1194  1.398    rillig 	/* XXX: sysMkPath is not freed */
   1195  1.398    rillig }
   1196  1.398    rillig 
   1197  1.408    rillig static void
   1198  1.408    rillig InitMaxJobs(void)
   1199  1.408    rillig {
   1200  1.408    rillig 	char *value;
   1201  1.408    rillig 	int n;
   1202  1.408    rillig 
   1203  1.408    rillig 	if (forceJobs || opts.compatMake ||
   1204  1.408    rillig 	    !Var_Exists(".MAKE.JOBS", VAR_GLOBAL))
   1205  1.408    rillig 		return;
   1206  1.408    rillig 
   1207  1.408    rillig 	(void)Var_Subst("${.MAKE.JOBS}", VAR_GLOBAL, VARE_WANTRES, &value);
   1208  1.408    rillig 	/* TODO: handle errors */
   1209  1.408    rillig 	n = (int)strtol(value, NULL, 0);
   1210  1.408    rillig 	if (n < 1) {
   1211  1.408    rillig 		(void)fprintf(stderr,
   1212  1.408    rillig 			      "%s: illegal value for .MAKE.JOBS "
   1213  1.408    rillig 			      "-- must be positive integer!\n",
   1214  1.408    rillig 			      progname);
   1215  1.408    rillig 		exit(1);
   1216  1.408    rillig 	}
   1217  1.408    rillig 
   1218  1.408    rillig 	if (n != opts.maxJobs) {
   1219  1.408    rillig 		Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
   1220  1.408    rillig 		Var_Append(MAKEFLAGS, value, VAR_GLOBAL);
   1221  1.408    rillig 	}
   1222  1.408    rillig 
   1223  1.408    rillig 	opts.maxJobs = n;
   1224  1.408    rillig 	maxJobTokens = opts.maxJobs;
   1225  1.408    rillig 	forceJobs = TRUE;
   1226  1.408    rillig 	free(value);
   1227  1.408    rillig }
   1228  1.408    rillig 
   1229  1.400    rillig /*
   1230  1.400    rillig  * For compatibility, look at the directories in the VPATH variable
   1231  1.400    rillig  * and add them to the search path, if the variable is defined. The
   1232  1.400    rillig  * variable's value is in the same format as the PATH environment
   1233  1.400    rillig  * variable, i.e. <directory>:<directory>:<directory>...
   1234  1.400    rillig  */
   1235  1.400    rillig static void
   1236  1.400    rillig InitVpath(void)
   1237  1.400    rillig {
   1238  1.400    rillig 	char *vpath, savec, *path;
   1239  1.410    rillig 	if (!Var_Exists("VPATH", VAR_CMDLINE))
   1240  1.400    rillig 		return;
   1241  1.400    rillig 
   1242  1.410    rillig 	(void)Var_Subst("${VPATH}", VAR_CMDLINE, VARE_WANTRES, &vpath);
   1243  1.400    rillig 	/* TODO: handle errors */
   1244  1.400    rillig 	path = vpath;
   1245  1.400    rillig 	do {
   1246  1.400    rillig 		char *cp;
   1247  1.400    rillig 		/* skip to end of directory */
   1248  1.400    rillig 		for (cp = path; *cp != ':' && *cp != '\0'; cp++)
   1249  1.400    rillig 			continue;
   1250  1.400    rillig 		/* Save terminator character so know when to stop */
   1251  1.400    rillig 		savec = *cp;
   1252  1.400    rillig 		*cp = '\0';
   1253  1.400    rillig 		/* Add directory to search path */
   1254  1.400    rillig 		(void)Dir_AddDir(dirSearchPath, path);
   1255  1.400    rillig 		*cp = savec;
   1256  1.400    rillig 		path = cp + 1;
   1257  1.400    rillig 	} while (savec == ':');
   1258  1.400    rillig 	free(vpath);
   1259  1.400    rillig }
   1260  1.400    rillig 
   1261  1.402    rillig static void
   1262  1.403    rillig ReadMakefiles(void)
   1263  1.403    rillig {
   1264  1.403    rillig 	if (opts.makefiles->first != NULL) {
   1265  1.403    rillig 		StringListNode *ln;
   1266  1.403    rillig 
   1267  1.403    rillig 		for (ln = opts.makefiles->first; ln != NULL; ln = ln->next) {
   1268  1.403    rillig 			if (ReadMakefile(ln->datum) != 0)
   1269  1.403    rillig 				Fatal("%s: cannot open %s.",
   1270  1.403    rillig 				      progname, (char *)ln->datum);
   1271  1.403    rillig 		}
   1272  1.403    rillig 	} else {
   1273  1.403    rillig 		char *p1;
   1274  1.403    rillig 		(void)Var_Subst("${" MAKEFILE_PREFERENCE "}",
   1275  1.410    rillig 				VAR_CMDLINE, VARE_WANTRES, &p1);
   1276  1.403    rillig 		/* TODO: handle errors */
   1277  1.403    rillig 		(void)str2Lst_Append(opts.makefiles, p1, NULL);
   1278  1.403    rillig 		(void)Lst_ForEachUntil(opts.makefiles,
   1279  1.403    rillig 				       ReadMakefileSucceeded, NULL);
   1280  1.403    rillig 		free(p1);
   1281  1.403    rillig 	}
   1282  1.403    rillig }
   1283  1.403    rillig 
   1284  1.403    rillig static void
   1285  1.402    rillig CleanUp(void)
   1286  1.402    rillig {
   1287  1.402    rillig #ifdef CLEANUP
   1288  1.402    rillig 	Lst_Destroy(opts.variables, free);
   1289  1.402    rillig 	Lst_Free(opts.makefiles);	/* don't free, may be used in GNodes */
   1290  1.402    rillig 	Lst_Destroy(opts.create, free);
   1291  1.402    rillig #endif
   1292  1.402    rillig 
   1293  1.402    rillig 	/* print the graph now it's been processed if the user requested it */
   1294  1.402    rillig 	if (DEBUG(GRAPH2))
   1295  1.402    rillig 		Targ_PrintGraph(2);
   1296  1.402    rillig 
   1297  1.402    rillig 	Trace_Log(MAKEEND, 0);
   1298  1.402    rillig 
   1299  1.402    rillig 	if (enterFlagObj)
   1300  1.402    rillig 		printf("%s: Leaving directory `%s'\n", progname, objdir);
   1301  1.402    rillig 	if (opts.enterFlag)
   1302  1.402    rillig 		printf("%s: Leaving directory `%s'\n", progname, curdir);
   1303  1.402    rillig 
   1304  1.402    rillig #ifdef USE_META
   1305  1.402    rillig 	meta_finish();
   1306  1.402    rillig #endif
   1307  1.402    rillig 	Suff_End();
   1308  1.402    rillig 	Targ_End();
   1309  1.402    rillig 	Arch_End();
   1310  1.402    rillig 	Var_End();
   1311  1.402    rillig 	Parse_End();
   1312  1.402    rillig 	Dir_End();
   1313  1.402    rillig 	Job_End();
   1314  1.402    rillig 	Trace_End();
   1315  1.402    rillig }
   1316  1.402    rillig 
   1317    1.1       cgd /*-
   1318    1.1       cgd  * main --
   1319    1.1       cgd  *	The main function, for obvious reasons. Initializes variables
   1320    1.1       cgd  *	and a few modules, then parses the arguments give it in the
   1321    1.1       cgd  *	environment and on the command line. Reads the system makefile
   1322    1.1       cgd  *	followed by either Makefile, makefile or the file given by the
   1323    1.1       cgd  *	-f argument. Sets the .MAKEFLAGS PMake variable based on all the
   1324    1.1       cgd  *	flags it has received by then uses either the Make or the Compat
   1325    1.1       cgd  *	module to create the initial list of targets.
   1326    1.1       cgd  *
   1327    1.1       cgd  * Results:
   1328    1.1       cgd  *	If -q was given, exits -1 if anything was out-of-date. Else it exits
   1329    1.1       cgd  *	0.
   1330    1.1       cgd  *
   1331    1.1       cgd  * Side Effects:
   1332    1.1       cgd  *	The program exits when done. Targets are created. etc. etc. etc.
   1333    1.1       cgd  */
   1334   1.13       cgd int
   1335   1.85       wiz main(int argc, char **argv)
   1336    1.1       cgd {
   1337  1.351    rillig 	Boolean outOfDate;	/* FALSE if all targets up to date */
   1338  1.381    rillig 	struct stat sa;
   1339  1.369    rillig 	const char *machine;
   1340  1.369    rillig 	const char *machine_arch;
   1341   1.55       sjg 	char *syspath = getenv("MAKESYSPATH");
   1342  1.119   tsutsui 	struct utsname utsname;
   1343  1.107       sjg 
   1344  1.154       apb 	/* default to writing debug to stderr */
   1345  1.391    rillig 	opts.debug_file = stderr;
   1346  1.133       dsl 
   1347  1.167  christos #ifdef SIGINFO
   1348  1.188       sjg 	(void)bmake_signal(SIGINFO, siginfo);
   1349  1.167  christos #endif
   1350  1.404    rillig 
   1351  1.404    rillig 	InitRandom();
   1352  1.277    rillig 
   1353   1.53  christos 	if ((progname = strrchr(argv[0], '/')) != NULL)
   1354   1.53  christos 		progname++;
   1355   1.53  christos 	else
   1356   1.53  christos 		progname = argv[0];
   1357  1.393    rillig 
   1358  1.393    rillig 	UnlimitFiles();
   1359   1.13       cgd 
   1360  1.206  christos 	if (uname(&utsname) == -1) {
   1361  1.206  christos 	    (void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
   1362  1.206  christos 		strerror(errno));
   1363  1.206  christos 	    exit(2);
   1364  1.206  christos 	}
   1365  1.206  christos 
   1366   1.17       gwr 	/*
   1367   1.17       gwr 	 * Get the name of this type of MACHINE from utsname
   1368   1.17       gwr 	 * so we can share an executable for similar machines.
   1369   1.17       gwr 	 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
   1370   1.17       gwr 	 *
   1371   1.33  christos 	 * Note that both MACHINE and MACHINE_ARCH are decided at
   1372   1.33  christos 	 * run-time.
   1373   1.17       gwr 	 */
   1374  1.369    rillig 	machine = init_machine(&utsname);
   1375  1.369    rillig 	machine_arch = init_machine_arch();
   1376   1.33  christos 
   1377  1.183       sjg 	myPid = getpid();		/* remember this for vFork() */
   1378  1.183       sjg 
   1379    1.1       cgd 	/*
   1380   1.51       sjg 	 * Just in case MAKEOBJDIR wants us to do something tricky.
   1381   1.51       sjg 	 */
   1382   1.51       sjg 	Var_Init();		/* Initialize the lists of variables for
   1383   1.51       sjg 				 * parsing arguments */
   1384  1.277    rillig 	Var_Set(".MAKE.OS", utsname.sysname, VAR_GLOBAL);
   1385  1.277    rillig 	Var_Set("MACHINE", machine, VAR_GLOBAL);
   1386  1.277    rillig 	Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
   1387  1.170     perry #ifdef MAKE_VERSION
   1388  1.277    rillig 	Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL);
   1389  1.170     perry #endif
   1390  1.277    rillig 	Var_Set(".newline", "\n", VAR_GLOBAL); /* handy for :@ loops */
   1391  1.176       sjg 	/*
   1392  1.176       sjg 	 * This is the traditional preference for makefiles.
   1393  1.176       sjg 	 */
   1394  1.176       sjg #ifndef MAKEFILE_PREFERENCE_LIST
   1395  1.176       sjg # define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
   1396  1.176       sjg #endif
   1397  1.176       sjg 	Var_Set(MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST,
   1398  1.277    rillig 		VAR_GLOBAL);
   1399  1.277    rillig 	Var_Set(MAKE_DEPENDFILE, ".depend", VAR_GLOBAL);
   1400   1.51       sjg 
   1401  1.395    rillig 	CmdOpts_Init();
   1402    1.1       cgd 	allPrecious = FALSE;		/* Remove targets when interrupted */
   1403  1.251  dholland 	deleteOnError = FALSE;		/* Historical default behavior */
   1404    1.1       cgd 	jobsRunning = FALSE;
   1405    1.1       cgd 
   1406  1.391    rillig 	maxJobTokens = opts.maxJobs;
   1407  1.174       sjg 	ignorePWD = FALSE;
   1408   1.13       cgd 
   1409    1.1       cgd 	/*
   1410    1.1       cgd 	 * Initialize the parsing, directory and variable modules to prepare
   1411    1.1       cgd 	 * for the reading of inclusion paths and variable settings on the
   1412    1.1       cgd 	 * command line
   1413    1.1       cgd 	 */
   1414   1.36       gwr 
   1415   1.36       gwr 	/*
   1416    1.1       cgd 	 * Initialize various variables.
   1417    1.1       cgd 	 *	MAKE also gets this name, for compatibility
   1418    1.1       cgd 	 *	.MAKEFLAGS gets set to the empty string just in case.
   1419    1.1       cgd 	 *	MFLAGS also gets initialized empty, for compatibility.
   1420    1.1       cgd 	 */
   1421   1.79        tv 	Parse_Init();
   1422  1.406    rillig 	InitVarMake(argv[0]);
   1423  1.277    rillig 	Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
   1424  1.277    rillig 	Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL);
   1425  1.277    rillig 	Var_Set("MFLAGS", "", VAR_GLOBAL);
   1426  1.277    rillig 	Var_Set(".ALLTARGETS", "", VAR_GLOBAL);
   1427  1.217       sjg 	/* some makefiles need to know this */
   1428  1.410    rillig 	Var_Set(MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV, VAR_CMDLINE);
   1429    1.1       cgd 
   1430    1.1       cgd 	/*
   1431  1.142       sjg 	 * Set some other useful macros
   1432  1.142       sjg 	 */
   1433  1.142       sjg 	{
   1434  1.213  christos 	    char tmp[64], *ep;
   1435  1.142       sjg 
   1436  1.220  christos 	    makelevel = ((ep = getenv(MAKE_LEVEL_ENV)) && *ep) ? atoi(ep) : 0;
   1437  1.220  christos 	    if (makelevel < 0)
   1438  1.220  christos 		makelevel = 0;
   1439  1.220  christos 	    snprintf(tmp, sizeof(tmp), "%d", makelevel);
   1440  1.277    rillig 	    Var_Set(MAKE_LEVEL, tmp, VAR_GLOBAL);
   1441  1.183       sjg 	    snprintf(tmp, sizeof(tmp), "%u", myPid);
   1442  1.277    rillig 	    Var_Set(".MAKE.PID", tmp, VAR_GLOBAL);
   1443  1.149  christos 	    snprintf(tmp, sizeof(tmp), "%u", getppid());
   1444  1.277    rillig 	    Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL);
   1445  1.142       sjg 	}
   1446  1.220  christos 	if (makelevel > 0) {
   1447  1.220  christos 		char pn[1024];
   1448  1.220  christos 		snprintf(pn, sizeof(pn), "%s[%d]", progname, makelevel);
   1449  1.221  christos 		progname = bmake_strdup(pn);
   1450  1.220  christos 	}
   1451  1.142       sjg 
   1452  1.210       sjg #ifdef USE_META
   1453  1.210       sjg 	meta_init();
   1454  1.210       sjg #endif
   1455  1.306    rillig 	Dir_Init();
   1456  1.255       sjg 
   1457  1.142       sjg 	/*
   1458    1.1       cgd 	 * First snag any flags out of the MAKE environment variable.
   1459    1.1       cgd 	 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
   1460    1.1       cgd 	 * in a different format).
   1461    1.1       cgd 	 */
   1462    1.1       cgd #ifdef POSIX
   1463  1.412    rillig 	{
   1464  1.412    rillig 	    char *p1 = explode(getenv("MAKEFLAGS"));
   1465  1.412    rillig 	    Main_ParseArgLine(p1);
   1466  1.412    rillig 	    free(p1);
   1467  1.412    rillig 	}
   1468    1.1       cgd #else
   1469    1.1       cgd 	Main_ParseArgLine(getenv("MAKE"));
   1470    1.1       cgd #endif
   1471   1.31  christos 
   1472    1.1       cgd 	/*
   1473  1.175       sjg 	 * Find where we are (now).
   1474  1.175       sjg 	 * We take care of PWD for the automounter below...
   1475  1.174       sjg 	 */
   1476  1.174       sjg 	if (getcwd(curdir, MAXPATHLEN) == NULL) {
   1477  1.194  dholland 		(void)fprintf(stderr, "%s: getcwd: %s.\n",
   1478  1.194  dholland 		    progname, strerror(errno));
   1479  1.174       sjg 		exit(2);
   1480  1.174       sjg 	}
   1481  1.174       sjg 
   1482  1.175       sjg 	MainParseArgs(argc, argv);
   1483  1.175       sjg 
   1484  1.391    rillig 	if (opts.enterFlag)
   1485  1.220  christos 		printf("%s: Entering directory `%s'\n", progname, curdir);
   1486  1.220  christos 
   1487  1.175       sjg 	/*
   1488  1.175       sjg 	 * Verify that cwd is sane.
   1489  1.175       sjg 	 */
   1490  1.174       sjg 	if (stat(curdir, &sa) == -1) {
   1491  1.174       sjg 	    (void)fprintf(stderr, "%s: %s: %s.\n",
   1492  1.174       sjg 		 progname, curdir, strerror(errno));
   1493  1.174       sjg 	    exit(2);
   1494  1.174       sjg 	}
   1495  1.174       sjg 
   1496  1.222       sjg #ifndef NO_PWD_OVERRIDE
   1497  1.381    rillig 	HandlePWD(&sa);
   1498  1.222       sjg #endif
   1499  1.277    rillig 	Var_Set(".CURDIR", curdir, VAR_GLOBAL);
   1500  1.174       sjg 
   1501  1.407    rillig 	InitObjdir(machine, machine_arch);
   1502  1.174       sjg 
   1503  1.174       sjg 	/*
   1504    1.1       cgd 	 * Initialize archive, target and suffix modules in preparation for
   1505    1.1       cgd 	 * parsing the makefile(s)
   1506    1.1       cgd 	 */
   1507    1.1       cgd 	Arch_Init();
   1508    1.1       cgd 	Targ_Init();
   1509    1.1       cgd 	Suff_Init();
   1510   1.58  sommerfe 	Trace_Init(tracefile);
   1511    1.1       cgd 
   1512  1.157       dsl 	DEFAULT = NULL;
   1513    1.1       cgd 	(void)time(&now);
   1514    1.1       cgd 
   1515   1.58  sommerfe 	Trace_Log(MAKESTART, NULL);
   1516  1.279    rillig 
   1517  1.343    rillig 	InitVarTargets();
   1518   1.25  christos 
   1519  1.409    rillig 	InitDefSysIncPath(syspath);
   1520   1.25  christos 
   1521   1.25  christos 	/*
   1522   1.25  christos 	 * Read in the built-in rules first, followed by the specified
   1523  1.325    rillig 	 * makefiles, or the default makefile and Makefile, in that order,
   1524  1.325    rillig 	 * if no makefiles were given on the command line.
   1525   1.25  christos 	 */
   1526  1.398    rillig 	if (!opts.noBuiltins)
   1527  1.398    rillig 		ReadBuiltinRules();
   1528  1.403    rillig 	ReadMakefiles();
   1529  1.403    rillig 
   1530  1.138       dsl 	/* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
   1531  1.391    rillig 	if (!opts.noBuiltins || !opts.printVars) {
   1532  1.389       sjg 	    /* ignore /dev/null and anything starting with "no" */
   1533  1.389       sjg 	    (void)Var_Subst("${.MAKE.DEPENDFILE:N/dev/null:Nno*:T}",
   1534  1.410    rillig 			    VAR_CMDLINE, VARE_WANTRES, &makeDependfile);
   1535  1.389       sjg 	    if (makeDependfile[0] != '\0') {
   1536  1.389       sjg 		/* TODO: handle errors */
   1537  1.389       sjg 		doing_depend = TRUE;
   1538  1.389       sjg 		(void)ReadMakefile(makeDependfile);
   1539  1.389       sjg 		doing_depend = FALSE;
   1540  1.389       sjg 	    }
   1541  1.145       dsl 	}
   1542    1.1       cgd 
   1543  1.233       sjg 	if (enterFlagObj)
   1544  1.233       sjg 		printf("%s: Entering directory `%s'\n", progname, objdir);
   1545  1.279    rillig 
   1546  1.176       sjg 	MakeMode(NULL);
   1547  1.176       sjg 
   1548  1.412    rillig 	{
   1549  1.412    rillig 	    void *freeIt;
   1550  1.412    rillig 	    Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &freeIt),
   1551  1.412    rillig 		       VAR_GLOBAL);
   1552  1.412    rillig 	    bmake_free(freeIt);
   1553  1.412    rillig 
   1554  1.412    rillig 	}
   1555    1.1       cgd 
   1556  1.408    rillig 	InitMaxJobs();
   1557  1.243  matthias 
   1558  1.243  matthias 	/*
   1559  1.243  matthias 	 * Be compatible if user did not specify -j and did not explicitly
   1560  1.243  matthias 	 * turned compatibility on
   1561  1.243  matthias 	 */
   1562  1.391    rillig 	if (!opts.compatMake && !forceJobs) {
   1563  1.391    rillig 	    opts.compatMake = TRUE;
   1564  1.243  matthias 	}
   1565  1.243  matthias 
   1566  1.391    rillig 	if (!opts.compatMake)
   1567  1.132       dsl 	    Job_ServerStart(maxJobTokens, jp_0, jp_1);
   1568  1.352    rillig 	DEBUG5(JOB, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
   1569  1.391    rillig 	       jp_0, jp_1, opts.maxJobs, maxJobTokens, opts.compatMake ? 1 : 0);
   1570   1.58  sommerfe 
   1571  1.391    rillig 	if (!opts.printVars)
   1572  1.255       sjg 	    Main_ExportMAKEFLAGS(TRUE);	/* initial export */
   1573  1.279    rillig 
   1574  1.400    rillig 	InitVpath();
   1575    1.1       cgd 
   1576    1.1       cgd 	/*
   1577    1.1       cgd 	 * Now that all search paths have been read for suffixes et al, it's
   1578    1.1       cgd 	 * time to add the default search path to their lists...
   1579    1.1       cgd 	 */
   1580    1.1       cgd 	Suff_DoPaths();
   1581   1.50   mycroft 
   1582   1.50   mycroft 	/*
   1583   1.50   mycroft 	 * Propagate attributes through :: dependency lists.
   1584   1.50   mycroft 	 */
   1585   1.50   mycroft 	Targ_Propagate();
   1586    1.1       cgd 
   1587    1.1       cgd 	/* print the initial graph, if the user requested it */
   1588    1.1       cgd 	if (DEBUG(GRAPH1))
   1589    1.1       cgd 		Targ_PrintGraph(1);
   1590    1.1       cgd 
   1591   1.31  christos 	/* print the values of any variables requested by the user */
   1592  1.391    rillig 	if (opts.printVars) {
   1593  1.266  christos 		doPrintVars();
   1594  1.266  christos 		outOfDate = FALSE;
   1595  1.138       dsl 	} else {
   1596  1.266  christos 		outOfDate = runTargets();
   1597   1.31  christos 	}
   1598   1.31  christos 
   1599  1.402    rillig 	CleanUp();
   1600   1.14       jtc 
   1601  1.137       dsl 	return outOfDate ? 1 : 0;
   1602    1.1       cgd }
   1603    1.1       cgd 
   1604  1.321    rillig /* Open and parse the given makefile, with all its side effects.
   1605    1.1       cgd  *
   1606    1.1       cgd  * Results:
   1607  1.124       sjg  *	0 if ok. -1 if couldn't open file.
   1608    1.1       cgd  */
   1609  1.124       sjg static int
   1610  1.325    rillig ReadMakefile(const char *fname)
   1611    1.1       cgd {
   1612  1.140       dsl 	int fd;
   1613  1.304    rillig 	char *name, *path = NULL;
   1614    1.1       cgd 
   1615    1.1       cgd 	if (!strcmp(fname, "-")) {
   1616  1.193  dholland 		Parse_File(NULL /*stdin*/, -1);
   1617  1.277    rillig 		Var_Set("MAKEFILE", "", VAR_INTERNAL);
   1618    1.1       cgd 	} else {
   1619    1.1       cgd 		/* if we've chdir'd, rebuild the path name */
   1620   1.74        tv 		if (strcmp(curdir, objdir) && *fname != '/') {
   1621  1.304    rillig 			path = str_concat3(curdir, "/", fname);
   1622  1.140       dsl 			fd = open(path, O_RDONLY);
   1623  1.140       dsl 			if (fd != -1) {
   1624    1.1       cgd 				fname = path;
   1625    1.1       cgd 				goto found;
   1626    1.1       cgd 			}
   1627  1.309    rillig 			free(path);
   1628  1.279    rillig 
   1629   1.95       jmc 			/* If curdir failed, try objdir (ala .depend) */
   1630  1.304    rillig 			path = str_concat3(objdir, "/", fname);
   1631  1.140       dsl 			fd = open(path, O_RDONLY);
   1632  1.140       dsl 			if (fd != -1) {
   1633   1.95       jmc 				fname = path;
   1634   1.95       jmc 				goto found;
   1635   1.95       jmc 			}
   1636  1.140       dsl 		} else {
   1637  1.140       dsl 			fd = open(fname, O_RDONLY);
   1638  1.140       dsl 			if (fd != -1)
   1639  1.140       dsl 				goto found;
   1640  1.140       dsl 		}
   1641    1.1       cgd 		/* look in -I and system include directories. */
   1642    1.1       cgd 		name = Dir_FindFile(fname, parseIncPath);
   1643  1.409    rillig 		if (!name) {
   1644  1.409    rillig 			SearchPath *sysInc = Lst_IsEmpty(sysIncPath)
   1645  1.409    rillig 					     ? defSysIncPath : sysIncPath;
   1646  1.409    rillig 			name = Dir_FindFile(fname, sysInc);
   1647  1.409    rillig 		}
   1648  1.140       dsl 		if (!name || (fd = open(name, O_RDONLY)) == -1) {
   1649  1.238  christos 			free(name);
   1650   1.48  christos 			free(path);
   1651  1.278    rillig 			return -1;
   1652   1.48  christos 		}
   1653    1.1       cgd 		fname = name;
   1654    1.1       cgd 		/*
   1655    1.1       cgd 		 * set the MAKEFILE variable desired by System V fans -- the
   1656    1.1       cgd 		 * placement of the setting here means it gets set to the last
   1657    1.1       cgd 		 * makefile specified, as it is set by SysV make.
   1658    1.1       cgd 		 */
   1659   1.67       sjg found:
   1660  1.176       sjg 		if (!doing_depend)
   1661  1.277    rillig 			Var_Set("MAKEFILE", fname, VAR_INTERNAL);
   1662  1.140       dsl 		Parse_File(fname, fd);
   1663    1.1       cgd 	}
   1664   1.48  christos 	free(path);
   1665  1.278    rillig 	return 0;
   1666   1.54       sjg }
   1667   1.54       sjg 
   1668   1.54       sjg 
   1669   1.29  christos 
   1670   1.29  christos /*-
   1671   1.29  christos  * Cmd_Exec --
   1672   1.29  christos  *	Execute the command in cmd, and return the output of that command
   1673  1.302    rillig  *	in a string.  In the output, newlines are replaced with spaces.
   1674   1.29  christos  *
   1675   1.29  christos  * Results:
   1676  1.293    rillig  *	A string containing the output of the command, or the empty string.
   1677  1.293    rillig  *	*errfmt returns a format string describing the command failure,
   1678  1.293    rillig  *	if any, using a single %s conversion specification.
   1679   1.29  christos  *
   1680   1.29  christos  * Side Effects:
   1681   1.29  christos  *	The string must be freed by the caller.
   1682   1.29  christos  */
   1683   1.29  christos char *
   1684  1.293    rillig Cmd_Exec(const char *cmd, const char **errfmt)
   1685   1.29  christos {
   1686  1.351    rillig     const char	*args[4];	/* Args for invoking the shell */
   1687  1.351    rillig     int		fds[2];		/* Pipe streams */
   1688  1.351    rillig     int		cpid;		/* Child PID */
   1689  1.351    rillig     int		pid;		/* PID from wait() */
   1690   1.29  christos     int		status;		/* command exit status */
   1691   1.29  christos     Buffer	buf;		/* buffer to store the result */
   1692  1.298    rillig     ssize_t	bytes_read;
   1693  1.298    rillig     char	*res;		/* result */
   1694  1.298    rillig     size_t	res_len;
   1695   1.29  christos     char	*cp;
   1696  1.231  dholland     int		savederr;	/* saved errno */
   1697   1.29  christos 
   1698  1.293    rillig     *errfmt = NULL;
   1699   1.29  christos 
   1700   1.90       sjg     if (!shellName)
   1701   1.90       sjg 	Shell_Init();
   1702   1.29  christos     /*
   1703   1.29  christos      * Set up arguments for shell
   1704   1.29  christos      */
   1705   1.90       sjg     args[0] = shellName;
   1706   1.29  christos     args[1] = "-c";
   1707   1.29  christos     args[2] = cmd;
   1708   1.29  christos     args[3] = NULL;
   1709   1.29  christos 
   1710   1.29  christos     /*
   1711   1.29  christos      * Open a pipe for fetching its output
   1712   1.29  christos      */
   1713   1.29  christos     if (pipe(fds) == -1) {
   1714  1.293    rillig 	*errfmt = "Couldn't create pipe for \"%s\"";
   1715   1.29  christos 	goto bad;
   1716   1.29  christos     }
   1717   1.29  christos 
   1718   1.29  christos     /*
   1719   1.29  christos      * Fork
   1720   1.29  christos      */
   1721  1.183       sjg     switch (cpid = vFork()) {
   1722   1.29  christos     case 0:
   1723   1.29  christos 	/*
   1724   1.29  christos 	 * Close input side of pipe
   1725   1.29  christos 	 */
   1726  1.112  christos 	(void)close(fds[0]);
   1727   1.29  christos 
   1728   1.29  christos 	/*
   1729   1.29  christos 	 * Duplicate the output stream to the shell's output, then
   1730   1.29  christos 	 * shut the extra thing down. Note we don't fetch the error
   1731   1.29  christos 	 * stream...why not? Why?
   1732   1.29  christos 	 */
   1733  1.112  christos 	(void)dup2(fds[1], 1);
   1734  1.112  christos 	(void)close(fds[1]);
   1735   1.31  christos 
   1736  1.143       sjg 	Var_ExportVars();
   1737  1.143       sjg 
   1738  1.112  christos 	(void)execv(shellPath, UNCONST(args));
   1739   1.29  christos 	_exit(1);
   1740   1.29  christos 	/*NOTREACHED*/
   1741   1.29  christos 
   1742   1.29  christos     case -1:
   1743  1.293    rillig 	*errfmt = "Couldn't exec \"%s\"";
   1744   1.29  christos 	goto bad;
   1745   1.29  christos 
   1746   1.29  christos     default:
   1747   1.29  christos 	/*
   1748   1.29  christos 	 * No need for the writing half
   1749   1.29  christos 	 */
   1750  1.112  christos 	(void)close(fds[1]);
   1751   1.31  christos 
   1752  1.232       sjg 	savederr = 0;
   1753  1.297    rillig 	Buf_Init(&buf, 0);
   1754   1.29  christos 
   1755   1.29  christos 	do {
   1756   1.29  christos 	    char   result[BUFSIZ];
   1757  1.298    rillig 	    bytes_read = read(fds[0], result, sizeof(result));
   1758  1.298    rillig 	    if (bytes_read > 0)
   1759  1.298    rillig 		Buf_AddBytes(&buf, result, (size_t)bytes_read);
   1760   1.29  christos 	}
   1761  1.298    rillig 	while (bytes_read > 0 || (bytes_read == -1 && errno == EINTR));
   1762  1.298    rillig 	if (bytes_read == -1)
   1763  1.231  dholland 	    savederr = errno;
   1764   1.29  christos 
   1765   1.29  christos 	/*
   1766   1.29  christos 	 * Close the input side of the pipe.
   1767   1.29  christos 	 */
   1768  1.112  christos 	(void)close(fds[0]);
   1769   1.29  christos 
   1770   1.29  christos 	/*
   1771   1.29  christos 	 * Wait for the process to exit.
   1772   1.29  christos 	 */
   1773  1.189       sjg 	while(((pid = waitpid(cpid, &status, 0)) != cpid) && (pid >= 0)) {
   1774  1.189       sjg 	    JobReapChild(pid, status, FALSE);
   1775   1.29  christos 	    continue;
   1776  1.189       sjg 	}
   1777  1.350    rillig 	res_len = Buf_Len(&buf);
   1778  1.163       dsl 	res = Buf_Destroy(&buf, FALSE);
   1779   1.29  christos 
   1780  1.231  dholland 	if (savederr != 0)
   1781  1.293    rillig 	    *errfmt = "Couldn't read shell's output for \"%s\"";
   1782   1.29  christos 
   1783  1.172  dholland 	if (WIFSIGNALED(status))
   1784  1.293    rillig 	    *errfmt = "\"%s\" exited on a signal";
   1785  1.172  dholland 	else if (WEXITSTATUS(status) != 0)
   1786  1.293    rillig 	    *errfmt = "\"%s\" returned non-zero status";
   1787   1.29  christos 
   1788  1.299    rillig 	/* Convert newlines to spaces.  A final newline is just stripped */
   1789  1.300    rillig 	if (res_len > 0 && res[res_len - 1] == '\n')
   1790  1.300    rillig 	    res[res_len - 1] = '\0';
   1791  1.300    rillig 	for (cp = res; *cp != '\0'; cp++)
   1792  1.299    rillig 	    if (*cp == '\n')
   1793   1.29  christos 		*cp = ' ';
   1794   1.29  christos 	break;
   1795   1.29  christos     }
   1796   1.29  christos     return res;
   1797   1.29  christos bad:
   1798  1.293    rillig     return bmake_strdup("");
   1799    1.1       cgd }
   1800    1.1       cgd 
   1801  1.369    rillig /* Print a printf-style error message.
   1802    1.1       cgd  *
   1803  1.369    rillig  * This error message has no consequences, in particular it does not affect
   1804  1.369    rillig  * the exit status. */
   1805    1.1       cgd void
   1806   1.89  christos Error(const char *fmt, ...)
   1807    1.1       cgd {
   1808    1.1       cgd 	va_list ap;
   1809  1.162       dsl 	FILE *err_file;
   1810   1.85       wiz 
   1811  1.391    rillig 	err_file = opts.debug_file;
   1812  1.162       dsl 	if (err_file == stdout)
   1813  1.162       dsl 		err_file = stderr;
   1814  1.184       sjg 	(void)fflush(stdout);
   1815  1.162       dsl 	for (;;) {
   1816  1.162       dsl 		va_start(ap, fmt);
   1817  1.162       dsl 		fprintf(err_file, "%s: ", progname);
   1818  1.162       dsl 		(void)vfprintf(err_file, fmt, ap);
   1819  1.162       dsl 		va_end(ap);
   1820  1.162       dsl 		(void)fprintf(err_file, "\n");
   1821  1.162       dsl 		(void)fflush(err_file);
   1822  1.162       dsl 		if (err_file == stderr)
   1823  1.162       dsl 			break;
   1824  1.162       dsl 		err_file = stderr;
   1825  1.162       dsl 	}
   1826    1.1       cgd }
   1827    1.1       cgd 
   1828  1.334    rillig /* Produce a Fatal error message, then exit immediately.
   1829    1.1       cgd  *
   1830  1.334    rillig  * If jobs are running, waits for them to finish. */
   1831    1.1       cgd void
   1832   1.89  christos Fatal(const char *fmt, ...)
   1833    1.1       cgd {
   1834    1.1       cgd 	va_list ap;
   1835   1.85       wiz 
   1836   1.13       cgd 	va_start(ap, fmt);
   1837    1.1       cgd 	if (jobsRunning)
   1838    1.1       cgd 		Job_Wait();
   1839    1.1       cgd 
   1840  1.184       sjg 	(void)fflush(stdout);
   1841    1.1       cgd 	(void)vfprintf(stderr, fmt, ap);
   1842    1.1       cgd 	va_end(ap);
   1843    1.1       cgd 	(void)fprintf(stderr, "\n");
   1844    1.1       cgd 	(void)fflush(stderr);
   1845    1.1       cgd 
   1846  1.176       sjg 	PrintOnError(NULL, NULL);
   1847   1.67       sjg 
   1848   1.94       dsl 	if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
   1849    1.1       cgd 		Targ_PrintGraph(2);
   1850   1.58  sommerfe 	Trace_Log(MAKEERROR, 0);
   1851    1.1       cgd 	exit(2);		/* Not 1 so -q can distinguish error */
   1852    1.1       cgd }
   1853    1.1       cgd 
   1854  1.392    rillig /* Major exception once jobs are being created.
   1855  1.392    rillig  * Kills all jobs, prints a message and exits. */
   1856    1.1       cgd void
   1857   1.89  christos Punt(const char *fmt, ...)
   1858    1.1       cgd {
   1859    1.1       cgd 	va_list ap;
   1860   1.85       wiz 
   1861   1.13       cgd 	va_start(ap, fmt);
   1862  1.184       sjg 	(void)fflush(stdout);
   1863   1.63  christos 	(void)fprintf(stderr, "%s: ", progname);
   1864    1.1       cgd 	(void)vfprintf(stderr, fmt, ap);
   1865    1.1       cgd 	va_end(ap);
   1866    1.1       cgd 	(void)fprintf(stderr, "\n");
   1867    1.1       cgd 	(void)fflush(stderr);
   1868    1.1       cgd 
   1869  1.176       sjg 	PrintOnError(NULL, NULL);
   1870   1.67       sjg 
   1871    1.1       cgd 	DieHorribly();
   1872    1.1       cgd }
   1873    1.1       cgd 
   1874  1.392    rillig /* Exit without giving a message. */
   1875    1.1       cgd void
   1876   1.85       wiz DieHorribly(void)
   1877    1.1       cgd {
   1878    1.1       cgd 	if (jobsRunning)
   1879    1.1       cgd 		Job_AbortAll();
   1880    1.1       cgd 	if (DEBUG(GRAPH2))
   1881    1.1       cgd 		Targ_PrintGraph(2);
   1882   1.58  sommerfe 	Trace_Log(MAKEERROR, 0);
   1883    1.1       cgd 	exit(2);		/* Not 1, so -q can distinguish error */
   1884    1.1       cgd }
   1885    1.1       cgd 
   1886  1.392    rillig /* Called when aborting due to errors in child shell to signal abnormal exit.
   1887  1.392    rillig  * The program exits.
   1888  1.392    rillig  * Errors is the number of errors encountered in Make_Make. */
   1889    1.1       cgd void
   1890   1.85       wiz Finish(int errors)
   1891    1.1       cgd {
   1892  1.275       sjg 	if (dieQuietly(NULL, -1))
   1893  1.275       sjg 		exit(2);
   1894    1.1       cgd 	Fatal("%d error%s", errors, errors == 1 ? "" : "s");
   1895    1.1       cgd }
   1896    1.1       cgd 
   1897   1.21  christos /*
   1898  1.208  dholland  * eunlink --
   1899   1.21  christos  *	Remove a file carefully, avoiding directories.
   1900   1.21  christos  */
   1901   1.21  christos int
   1902   1.85       wiz eunlink(const char *file)
   1903   1.21  christos {
   1904   1.21  christos 	struct stat st;
   1905   1.21  christos 
   1906   1.21  christos 	if (lstat(file, &st) == -1)
   1907   1.21  christos 		return -1;
   1908   1.21  christos 
   1909   1.21  christos 	if (S_ISDIR(st.st_mode)) {
   1910   1.21  christos 		errno = EISDIR;
   1911   1.21  christos 		return -1;
   1912   1.21  christos 	}
   1913   1.21  christos 	return unlink(file);
   1914    1.1       cgd }
   1915    1.1       cgd 
   1916  1.373    rillig static void
   1917  1.373    rillig write_all(int fd, const void *data, size_t n)
   1918  1.373    rillig {
   1919  1.373    rillig 	const char *mem = data;
   1920  1.373    rillig 
   1921  1.373    rillig 	while (n > 0) {
   1922  1.373    rillig 		ssize_t written = write(fd, mem, n);
   1923  1.373    rillig 		if (written == -1 && errno == EAGAIN)
   1924  1.373    rillig 			continue;
   1925  1.373    rillig 		if (written == -1)
   1926  1.373    rillig 			break;
   1927  1.373    rillig 		mem += written;
   1928  1.375    rillig 		n -= (size_t)written;
   1929  1.373    rillig 	}
   1930  1.373    rillig }
   1931  1.373    rillig 
   1932    1.1       cgd /*
   1933  1.372    rillig  * execDie --
   1934   1.66  christos  *	Print why exec failed, avoiding stdio.
   1935   1.66  christos  */
   1936  1.372    rillig void MAKE_ATTR_DEAD
   1937  1.372    rillig execDie(const char *af, const char *av)
   1938   1.66  christos {
   1939  1.373    rillig 	Buffer buf;
   1940  1.373    rillig 
   1941  1.373    rillig 	Buf_Init(&buf, 0);
   1942  1.373    rillig 	Buf_AddStr(&buf, progname);
   1943  1.373    rillig 	Buf_AddStr(&buf, ": ");
   1944  1.373    rillig 	Buf_AddStr(&buf, af);
   1945  1.373    rillig 	Buf_AddStr(&buf, "(");
   1946  1.373    rillig 	Buf_AddStr(&buf, av);
   1947  1.373    rillig 	Buf_AddStr(&buf, ") failed (");
   1948  1.373    rillig 	Buf_AddStr(&buf, strerror(errno));
   1949  1.373    rillig 	Buf_AddStr(&buf, ")\n");
   1950   1.66  christos 
   1951  1.373    rillig 	write_all(STDERR_FILENO, Buf_GetAll(&buf, NULL), Buf_Len(&buf));
   1952   1.66  christos 
   1953  1.373    rillig 	Buf_Destroy(&buf, TRUE);
   1954  1.372    rillig 	_exit(1);
   1955   1.66  christos }
   1956   1.66  christos 
   1957   1.66  christos /*
   1958    1.1       cgd  * usage --
   1959    1.1       cgd  *	exit with usage message
   1960    1.1       cgd  */
   1961   1.13       cgd static void
   1962   1.85       wiz usage(void)
   1963    1.1       cgd {
   1964  1.220  christos 	char *p;
   1965  1.220  christos 	if ((p = strchr(progname, '[')) != NULL)
   1966  1.292    rillig 		*p = '\0';
   1967  1.220  christos 
   1968    1.1       cgd 	(void)fprintf(stderr,
   1969  1.292    rillig "usage: %s [-BeikNnqrstWwX] \n"
   1970  1.292    rillig "            [-C directory] [-D variable] [-d flags] [-f makefile]\n"
   1971  1.292    rillig "            [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n"
   1972  1.292    rillig "            [-V variable] [-v variable] [variable=value] [target ...]\n",
   1973  1.272  christos 	    progname);
   1974    1.1       cgd 	exit(2);
   1975   1.14       jtc }
   1976   1.14       jtc 
   1977  1.245       sjg /*
   1978  1.245       sjg  * realpath(3) can get expensive, cache results...
   1979  1.245       sjg  */
   1980  1.262  riastrad static GNode *cached_realpaths = NULL;
   1981  1.262  riastrad 
   1982  1.262  riastrad static GNode *
   1983  1.262  riastrad get_cached_realpaths(void)
   1984  1.262  riastrad {
   1985  1.262  riastrad 
   1986  1.262  riastrad     if (!cached_realpaths) {
   1987  1.262  riastrad 	cached_realpaths = Targ_NewGN("Realpath");
   1988  1.262  riastrad #ifndef DEBUG_REALPATH_CACHE
   1989  1.262  riastrad 	cached_realpaths->flags = INTERNAL;
   1990  1.262  riastrad #endif
   1991  1.262  riastrad     }
   1992  1.262  riastrad 
   1993  1.262  riastrad     return cached_realpaths;
   1994  1.262  riastrad }
   1995  1.262  riastrad 
   1996  1.262  riastrad /* purge any relative paths */
   1997  1.262  riastrad static void
   1998  1.262  riastrad purge_cached_realpaths(void)
   1999  1.262  riastrad {
   2000  1.262  riastrad     GNode *cache = get_cached_realpaths();
   2001  1.376    rillig     HashEntry *he, *nhe;
   2002  1.374    rillig     HashIter hi;
   2003  1.262  riastrad 
   2004  1.374    rillig     HashIter_Init(&hi, &cache->context);
   2005  1.374    rillig     he = HashIter_Next(&hi);
   2006  1.374    rillig     while (he != NULL) {
   2007  1.374    rillig 	nhe = HashIter_Next(&hi);
   2008  1.377    rillig 	if (he->key[0] != '/') {
   2009  1.262  riastrad 	    if (DEBUG(DIR))
   2010  1.377    rillig 		fprintf(stderr, "cached_realpath: purging %s\n", he->key);
   2011  1.390    rillig 	    HashTable_DeleteEntry(&cache->context, he);
   2012  1.262  riastrad 	}
   2013  1.262  riastrad 	he = nhe;
   2014  1.262  riastrad     }
   2015  1.262  riastrad }
   2016  1.262  riastrad 
   2017  1.245       sjg char *
   2018  1.245       sjg cached_realpath(const char *pathname, char *resolved)
   2019  1.245       sjg {
   2020  1.262  riastrad     GNode *cache;
   2021  1.289    rillig     const char *rp;
   2022  1.412    rillig     void *freeIt;
   2023  1.245       sjg 
   2024  1.245       sjg     if (!pathname || !pathname[0])
   2025  1.245       sjg 	return NULL;
   2026  1.245       sjg 
   2027  1.262  riastrad     cache = get_cached_realpaths();
   2028  1.262  riastrad 
   2029  1.412    rillig     if ((rp = Var_Value(pathname, cache, &freeIt)) != NULL) {
   2030  1.245       sjg 	/* a hit */
   2031  1.248  christos 	strncpy(resolved, rp, MAXPATHLEN);
   2032  1.248  christos 	resolved[MAXPATHLEN - 1] = '\0';
   2033  1.248  christos     } else if ((rp = realpath(pathname, resolved)) != NULL) {
   2034  1.277    rillig 	Var_Set(pathname, rp, cache);
   2035  1.248  christos     } /* else should we negative-cache? */
   2036  1.248  christos 
   2037  1.412    rillig     bmake_free(freeIt);
   2038  1.245       sjg     return rp ? resolved : NULL;
   2039  1.245       sjg }
   2040  1.245       sjg 
   2041  1.275       sjg /*
   2042  1.275       sjg  * Return true if we should die without noise.
   2043  1.275       sjg  * For example our failing child was a sub-make
   2044  1.275       sjg  * or failure happend elsewhere.
   2045  1.275       sjg  */
   2046  1.275       sjg int
   2047  1.275       sjg dieQuietly(GNode *gn, int bf)
   2048  1.275       sjg {
   2049  1.275       sjg     static int quietly = -1;
   2050  1.275       sjg 
   2051  1.275       sjg     if (quietly < 0) {
   2052  1.275       sjg 	if (DEBUG(JOB) || getBoolean(".MAKE.DIE_QUIETLY", 1) == 0)
   2053  1.275       sjg 	    quietly = 0;
   2054  1.275       sjg 	else if (bf >= 0)
   2055  1.275       sjg 	    quietly = bf;
   2056  1.275       sjg 	else
   2057  1.380    rillig 	    quietly = gn != NULL ? ((gn->type  & (OP_MAKE)) != 0) : 0;
   2058  1.275       sjg     }
   2059  1.275       sjg     return quietly;
   2060  1.275       sjg }
   2061  1.275       sjg 
   2062  1.385    rillig static void
   2063  1.385    rillig SetErrorVars(GNode *gn)
   2064  1.385    rillig {
   2065  1.385    rillig     StringListNode *ln;
   2066  1.385    rillig 
   2067  1.385    rillig     /*
   2068  1.385    rillig      * We can print this even if there is no .ERROR target.
   2069  1.385    rillig      */
   2070  1.385    rillig     Var_Set(".ERROR_TARGET", gn->name, VAR_GLOBAL);
   2071  1.385    rillig     Var_Delete(".ERROR_CMD", VAR_GLOBAL);
   2072  1.385    rillig 
   2073  1.385    rillig     for (ln = gn->commands->first; ln != NULL; ln = ln->next) {
   2074  1.385    rillig 	const char *cmd = ln->datum;
   2075  1.385    rillig 
   2076  1.385    rillig 	if (cmd == NULL)
   2077  1.385    rillig 	    break;
   2078  1.385    rillig 	Var_Append(".ERROR_CMD", cmd, VAR_GLOBAL);
   2079  1.385    rillig     }
   2080  1.385    rillig }
   2081  1.385    rillig 
   2082   1.67       sjg void
   2083  1.176       sjg PrintOnError(GNode *gn, const char *s)
   2084   1.67       sjg {
   2085  1.177       sjg     static GNode *en = NULL;
   2086  1.296    rillig     const char *expr;
   2087  1.296    rillig     char *cp;
   2088  1.128       sjg 
   2089  1.283       sjg     if (DEBUG(HASH)) {
   2090  1.283       sjg 	Targ_Stats();
   2091  1.283       sjg 	Var_Stats();
   2092  1.283       sjg     }
   2093  1.283       sjg 
   2094  1.275       sjg     /* we generally want to keep quiet if a sub-make died */
   2095  1.275       sjg     if (dieQuietly(gn, -1))
   2096  1.275       sjg 	return;
   2097  1.275       sjg 
   2098   1.67       sjg     if (s)
   2099  1.176       sjg 	printf("%s", s);
   2100  1.279    rillig 
   2101   1.67       sjg     printf("\n%s: stopped in %s\n", progname, curdir);
   2102  1.176       sjg 
   2103  1.177       sjg     if (en)
   2104  1.177       sjg 	return;				/* we've been here! */
   2105  1.385    rillig     if (gn)
   2106  1.411    rillig 	SetErrorVars(gn);
   2107  1.296    rillig     expr = "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}";
   2108  1.341    rillig     (void)Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES, &cp);
   2109  1.341    rillig     /* TODO: handle errors */
   2110  1.329    rillig     printf("%s", cp);
   2111  1.329    rillig     free(cp);
   2112  1.235       sjg     fflush(stdout);
   2113  1.235       sjg 
   2114  1.176       sjg     /*
   2115  1.186       sjg      * Finally, see if there is a .ERROR target, and run it if so.
   2116  1.176       sjg      */
   2117  1.346    rillig     en = Targ_FindNode(".ERROR");
   2118  1.176       sjg     if (en) {
   2119  1.176       sjg 	en->type |= OP_SPECIAL;
   2120  1.176       sjg 	Compat_Make(en, en);
   2121  1.176       sjg     }
   2122   1.68       sjg }
   2123   1.68       sjg 
   2124   1.68       sjg void
   2125   1.85       wiz Main_ExportMAKEFLAGS(Boolean first)
   2126   1.68       sjg {
   2127  1.322    rillig     static Boolean once = TRUE;
   2128  1.296    rillig     const char *expr;
   2129  1.296    rillig     char *s;
   2130   1.68       sjg 
   2131   1.68       sjg     if (once != first)
   2132   1.68       sjg 	return;
   2133  1.322    rillig     once = FALSE;
   2134  1.279    rillig 
   2135  1.296    rillig     expr = "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}";
   2136  1.410    rillig     (void)Var_Subst(expr, VAR_CMDLINE, VARE_WANTRES, &s);
   2137  1.341    rillig     /* TODO: handle errors */
   2138  1.322    rillig     if (s[0] != '\0') {
   2139   1.68       sjg #ifdef POSIX
   2140   1.68       sjg 	setenv("MAKEFLAGS", s, 1);
   2141   1.68       sjg #else
   2142   1.68       sjg 	setenv("MAKE", s, 1);
   2143   1.68       sjg #endif
   2144   1.68       sjg     }
   2145    1.1       cgd }
   2146  1.180       sjg 
   2147  1.191       sjg char *
   2148  1.191       sjg getTmpdir(void)
   2149  1.180       sjg {
   2150  1.180       sjg     static char *tmpdir = NULL;
   2151  1.180       sjg 
   2152  1.180       sjg     if (!tmpdir) {
   2153  1.180       sjg 	struct stat st;
   2154  1.180       sjg 
   2155  1.180       sjg 	/*
   2156  1.180       sjg 	 * Honor $TMPDIR but only if it is valid.
   2157  1.180       sjg 	 * Ensure it ends with /.
   2158  1.180       sjg 	 */
   2159  1.341    rillig 	(void)Var_Subst("${TMPDIR:tA:U" _PATH_TMP "}/", VAR_GLOBAL,
   2160  1.341    rillig 			VARE_WANTRES, &tmpdir);
   2161  1.341    rillig 	/* TODO: handle errors */
   2162  1.180       sjg 	if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
   2163  1.180       sjg 	    free(tmpdir);
   2164  1.180       sjg 	    tmpdir = bmake_strdup(_PATH_TMP);
   2165  1.180       sjg 	}
   2166  1.180       sjg     }
   2167  1.191       sjg     return tmpdir;
   2168  1.191       sjg }
   2169  1.191       sjg 
   2170  1.191       sjg /*
   2171  1.191       sjg  * Create and open a temp file using "pattern".
   2172  1.414    rillig  * If out_fname is provided, set it to a copy of the filename created.
   2173  1.191       sjg  * Otherwise unlink the file once open.
   2174  1.191       sjg  */
   2175  1.191       sjg int
   2176  1.414    rillig mkTempFile(const char *pattern, char **out_fname)
   2177  1.191       sjg {
   2178  1.191       sjg     static char *tmpdir = NULL;
   2179  1.191       sjg     char tfile[MAXPATHLEN];
   2180  1.191       sjg     int fd;
   2181  1.279    rillig 
   2182  1.414    rillig     if (pattern != NULL)
   2183  1.191       sjg 	pattern = TMPPAT;
   2184  1.414    rillig     if (tmpdir == NULL)
   2185  1.191       sjg 	tmpdir = getTmpdir();
   2186  1.180       sjg     if (pattern[0] == '/') {
   2187  1.181  dholland 	snprintf(tfile, sizeof(tfile), "%s", pattern);
   2188  1.180       sjg     } else {
   2189  1.180       sjg 	snprintf(tfile, sizeof(tfile), "%s%s", tmpdir, pattern);
   2190  1.180       sjg     }
   2191  1.180       sjg     if ((fd = mkstemp(tfile)) < 0)
   2192  1.180       sjg 	Punt("Could not create temporary file %s: %s", tfile, strerror(errno));
   2193  1.414    rillig     if (out_fname) {
   2194  1.414    rillig 	*out_fname = bmake_strdup(tfile);
   2195  1.180       sjg     } else {
   2196  1.180       sjg 	unlink(tfile);			/* we just want the descriptor */
   2197  1.180       sjg     }
   2198  1.180       sjg     return fd;
   2199  1.180       sjg }
   2200  1.201       sjg 
   2201  1.240       sjg /*
   2202  1.240       sjg  * Convert a string representation of a boolean.
   2203  1.240       sjg  * Anything that looks like "No", "False", "Off", "0" etc,
   2204  1.240       sjg  * is FALSE, otherwise TRUE.
   2205  1.240       sjg  */
   2206  1.240       sjg Boolean
   2207  1.240       sjg s2Boolean(const char *s, Boolean bf)
   2208  1.240       sjg {
   2209  1.240       sjg     if (s) {
   2210  1.240       sjg 	switch(*s) {
   2211  1.240       sjg 	case '\0':			/* not set - the default wins */
   2212  1.240       sjg 	    break;
   2213  1.240       sjg 	case '0':
   2214  1.240       sjg 	case 'F':
   2215  1.240       sjg 	case 'f':
   2216  1.240       sjg 	case 'N':
   2217  1.240       sjg 	case 'n':
   2218  1.240       sjg 	    bf = FALSE;
   2219  1.240       sjg 	    break;
   2220  1.240       sjg 	case 'O':
   2221  1.240       sjg 	case 'o':
   2222  1.240       sjg 	    switch (s[1]) {
   2223  1.240       sjg 	    case 'F':
   2224  1.240       sjg 	    case 'f':
   2225  1.240       sjg 		bf = FALSE;
   2226  1.240       sjg 		break;
   2227  1.240       sjg 	    default:
   2228  1.240       sjg 		bf = TRUE;
   2229  1.240       sjg 		break;
   2230  1.240       sjg 	    }
   2231  1.240       sjg 	    break;
   2232  1.240       sjg 	default:
   2233  1.240       sjg 	    bf = TRUE;
   2234  1.240       sjg 	    break;
   2235  1.240       sjg 	}
   2236  1.240       sjg     }
   2237  1.278    rillig     return bf;
   2238  1.240       sjg }
   2239  1.201       sjg 
   2240  1.201       sjg /*
   2241  1.201       sjg  * Return a Boolean based on setting of a knob.
   2242  1.201       sjg  *
   2243  1.201       sjg  * If the knob is not set, the supplied default is the return value.
   2244  1.201       sjg  * If set, anything that looks or smells like "No", "False", "Off", "0" etc,
   2245  1.201       sjg  * is FALSE, otherwise TRUE.
   2246  1.201       sjg  */
   2247  1.201       sjg Boolean
   2248  1.328    rillig getBoolean(const char *name, Boolean fallback)
   2249  1.201       sjg {
   2250  1.328    rillig     char *expr = str_concat3("${", name, ":U:tl}");
   2251  1.341    rillig     char *value;
   2252  1.341    rillig     Boolean res;
   2253  1.341    rillig 
   2254  1.341    rillig     (void)Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES, &value);
   2255  1.341    rillig     /* TODO: handle errors */
   2256  1.341    rillig     res = s2Boolean(value, fallback);
   2257  1.328    rillig     free(value);
   2258  1.328    rillig     free(expr);
   2259  1.328    rillig     return res;
   2260  1.201       sjg }
   2261