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