xref: /minix3/usr.bin/make/main.c (revision d2532d3d42d764c9ef9816851cdb17eda7e08d36)
10a6a1f1dSLionel Sambuc /*	$NetBSD: main.c,v 1.233 2015/09/10 17:15:11 sjg Exp $	*/
22e2caf59SThomas Veerman 
32e2caf59SThomas Veerman /*
42e2caf59SThomas Veerman  * Copyright (c) 1988, 1989, 1990, 1993
52e2caf59SThomas Veerman  *	The Regents of the University of California.  All rights reserved.
62e2caf59SThomas Veerman  *
72e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
82e2caf59SThomas Veerman  * Adam de Boor.
92e2caf59SThomas Veerman  *
102e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
112e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
122e2caf59SThomas Veerman  * are met:
132e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
142e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
152e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
162e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
172e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
182e2caf59SThomas Veerman  * 3. Neither the name of the University nor the names of its contributors
192e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
202e2caf59SThomas Veerman  *    without specific prior written permission.
212e2caf59SThomas Veerman  *
222e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322e2caf59SThomas Veerman  * SUCH DAMAGE.
332e2caf59SThomas Veerman  */
342e2caf59SThomas Veerman 
352e2caf59SThomas Veerman /*
362e2caf59SThomas Veerman  * Copyright (c) 1989 by Berkeley Softworks
372e2caf59SThomas Veerman  * All rights reserved.
382e2caf59SThomas Veerman  *
392e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
402e2caf59SThomas Veerman  * Adam de Boor.
412e2caf59SThomas Veerman  *
422e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
432e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
442e2caf59SThomas Veerman  * are met:
452e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
462e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
472e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
482e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
492e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
502e2caf59SThomas Veerman  * 3. All advertising materials mentioning features or use of this software
512e2caf59SThomas Veerman  *    must display the following acknowledgement:
522e2caf59SThomas Veerman  *	This product includes software developed by the University of
532e2caf59SThomas Veerman  *	California, Berkeley and its contributors.
542e2caf59SThomas Veerman  * 4. Neither the name of the University nor the names of its contributors
552e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
562e2caf59SThomas Veerman  *    without specific prior written permission.
572e2caf59SThomas Veerman  *
582e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
592e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
602e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
612e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
622e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
632e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
642e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
652e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
662e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
672e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
682e2caf59SThomas Veerman  * SUCH DAMAGE.
692e2caf59SThomas Veerman  */
702e2caf59SThomas Veerman 
712e2caf59SThomas Veerman #ifndef MAKE_NATIVE
720a6a1f1dSLionel Sambuc static char rcsid[] = "$NetBSD: main.c,v 1.233 2015/09/10 17:15:11 sjg Exp $";
732e2caf59SThomas Veerman #else
742e2caf59SThomas Veerman #include <sys/cdefs.h>
752e2caf59SThomas Veerman #ifndef lint
762e2caf59SThomas Veerman __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\
772e2caf59SThomas Veerman  The Regents of the University of California.  All rights reserved.");
782e2caf59SThomas Veerman #endif /* not lint */
792e2caf59SThomas Veerman 
802e2caf59SThomas Veerman #ifndef lint
812e2caf59SThomas Veerman #if 0
822e2caf59SThomas Veerman static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 3/19/94";
832e2caf59SThomas Veerman #else
840a6a1f1dSLionel Sambuc __RCSID("$NetBSD: main.c,v 1.233 2015/09/10 17:15:11 sjg Exp $");
852e2caf59SThomas Veerman #endif
862e2caf59SThomas Veerman #endif /* not lint */
872e2caf59SThomas Veerman #endif
882e2caf59SThomas Veerman 
892e2caf59SThomas Veerman /*-
902e2caf59SThomas Veerman  * main.c --
912e2caf59SThomas Veerman  *	The main file for this entire program. Exit routines etc
922e2caf59SThomas Veerman  *	reside here.
932e2caf59SThomas Veerman  *
942e2caf59SThomas Veerman  * Utility functions defined in this file:
952e2caf59SThomas Veerman  *	Main_ParseArgLine	Takes a line of arguments, breaks them and
962e2caf59SThomas Veerman  *				treats them as if they were given when first
972e2caf59SThomas Veerman  *				invoked. Used by the parse module to implement
982e2caf59SThomas Veerman  *				the .MFLAGS target.
992e2caf59SThomas Veerman  *
1002e2caf59SThomas Veerman  *	Error			Print a tagged error message. The global
1012e2caf59SThomas Veerman  *				MAKE variable must have been defined. This
1020a6a1f1dSLionel Sambuc  *				takes a format string and optional arguments
1030a6a1f1dSLionel Sambuc  *				for it.
1042e2caf59SThomas Veerman  *
1052e2caf59SThomas Veerman  *	Fatal			Print an error message and exit. Also takes
1060a6a1f1dSLionel Sambuc  *				a format string and arguments for it.
1072e2caf59SThomas Veerman  *
1082e2caf59SThomas Veerman  *	Punt			Aborts all jobs and exits with a message. Also
1090a6a1f1dSLionel Sambuc  *				takes a format string and arguments for it.
1102e2caf59SThomas Veerman  *
1112e2caf59SThomas Veerman  *	Finish			Finish things up by printing the number of
1122e2caf59SThomas Veerman  *				errors which occurred, as passed to it, and
1132e2caf59SThomas Veerman  *				exiting.
1142e2caf59SThomas Veerman  */
1152e2caf59SThomas Veerman 
1162e2caf59SThomas Veerman #include <sys/types.h>
1172e2caf59SThomas Veerman #include <sys/time.h>
1182e2caf59SThomas Veerman #include <sys/param.h>
1192e2caf59SThomas Veerman #include <sys/resource.h>
1202e2caf59SThomas Veerman #include <sys/stat.h>
1212e2caf59SThomas Veerman #ifdef MAKE_NATIVE
12284d9c625SLionel Sambuc #include <sys/sysctl.h>
1232e2caf59SThomas Veerman #endif
12484d9c625SLionel Sambuc #include <sys/utsname.h>
1252e2caf59SThomas Veerman #include <sys/wait.h>
1262e2caf59SThomas Veerman 
1272e2caf59SThomas Veerman #include <errno.h>
1282e2caf59SThomas Veerman #include <fcntl.h>
12984d9c625SLionel Sambuc #include <signal.h>
1302e2caf59SThomas Veerman #include <stdarg.h>
1312e2caf59SThomas Veerman #include <stdio.h>
1322e2caf59SThomas Veerman #include <stdlib.h>
1332e2caf59SThomas Veerman #include <time.h>
13484d9c625SLionel Sambuc #include <ctype.h>
1352e2caf59SThomas Veerman 
1362e2caf59SThomas Veerman #include "make.h"
1372e2caf59SThomas Veerman #include "hash.h"
1382e2caf59SThomas Veerman #include "dir.h"
1392e2caf59SThomas Veerman #include "job.h"
1402e2caf59SThomas Veerman #include "pathnames.h"
1412e2caf59SThomas Veerman #include "trace.h"
1422e2caf59SThomas Veerman 
1432e2caf59SThomas Veerman #ifdef USE_IOVEC
1442e2caf59SThomas Veerman #include <sys/uio.h>
1452e2caf59SThomas Veerman #endif
1462e2caf59SThomas Veerman 
1472e2caf59SThomas Veerman #ifndef	DEFMAXLOCAL
1482e2caf59SThomas Veerman #define	DEFMAXLOCAL DEFMAXJOBS
1492e2caf59SThomas Veerman #endif	/* DEFMAXLOCAL */
1502e2caf59SThomas Veerman 
1512e2caf59SThomas Veerman Lst			create;		/* Targets to be made */
1522e2caf59SThomas Veerman time_t			now;		/* Time at start of make */
1532e2caf59SThomas Veerman GNode			*DEFAULT;	/* .DEFAULT node */
1542e2caf59SThomas Veerman Boolean			allPrecious;	/* .PRECIOUS given on line by itself */
1552e2caf59SThomas Veerman 
1562e2caf59SThomas Veerman static Boolean		noBuiltins;	/* -r flag */
1572e2caf59SThomas Veerman static Lst		makefiles;	/* ordered list of makefiles to read */
1582e2caf59SThomas Veerman static Boolean		printVars;	/* print value of one or more vars */
1592e2caf59SThomas Veerman static Lst		variables;	/* list of variables to print */
1602e2caf59SThomas Veerman int			maxJobs;	/* -j argument */
1612e2caf59SThomas Veerman static int		maxJobTokens;	/* -j argument */
1622e2caf59SThomas Veerman Boolean			compatMake;	/* -B argument */
1632e2caf59SThomas Veerman int			debug;		/* -d argument */
1642bc7c627SLionel Sambuc Boolean			debugVflag;	/* -dV */
1652e2caf59SThomas Veerman Boolean			noExecute;	/* -n flag */
1662e2caf59SThomas Veerman Boolean			noRecursiveExecute;	/* -N flag */
1672e2caf59SThomas Veerman Boolean			keepgoing;	/* -k flag */
1682e2caf59SThomas Veerman Boolean			queryFlag;	/* -q flag */
1692e2caf59SThomas Veerman Boolean			touchFlag;	/* -t flag */
17084d9c625SLionel Sambuc Boolean			enterFlag;	/* -w flag */
1710a6a1f1dSLionel Sambuc Boolean			enterFlagObj;	/* -w and objdir != srcdir */
1722e2caf59SThomas Veerman Boolean			ignoreErrors;	/* -i flag */
1732e2caf59SThomas Veerman Boolean			beSilent;	/* -s flag */
1742e2caf59SThomas Veerman Boolean			oldVars;	/* variable substitution style */
1752e2caf59SThomas Veerman Boolean			checkEnvFirst;	/* -e flag */
1762e2caf59SThomas Veerman Boolean			parseWarnFatal;	/* -W flag */
1772e2caf59SThomas Veerman Boolean			jobServer; 	/* -J flag */
1782e2caf59SThomas Veerman static int jp_0 = -1, jp_1 = -1;	/* ends of parent job pipe */
1792e2caf59SThomas Veerman Boolean			varNoExportEnv;	/* -X flag */
1802e2caf59SThomas Veerman Boolean			doing_depend;	/* Set while reading .depend */
1812e2caf59SThomas Veerman static Boolean		jobsRunning;	/* TRUE if the jobs might be running */
1822e2caf59SThomas Veerman static const char *	tracefile;
1832e2caf59SThomas Veerman static void		MainParseArgs(int, char **);
1842e2caf59SThomas Veerman static int		ReadMakefile(const void *, const void *);
1852bc7c627SLionel Sambuc static void		usage(void) MAKE_ATTR_DEAD;
1862e2caf59SThomas Veerman 
1872e2caf59SThomas Veerman static Boolean		ignorePWD;	/* if we use -C, PWD is meaningless */
1882e2caf59SThomas Veerman static char objdir[MAXPATHLEN + 1];	/* where we chdir'ed to */
1892e2caf59SThomas Veerman char curdir[MAXPATHLEN + 1];		/* Startup directory */
1902e2caf59SThomas Veerman char *progname;				/* the program name */
1912e2caf59SThomas Veerman char *makeDependfile;
1922e2caf59SThomas Veerman pid_t myPid;
19384d9c625SLionel Sambuc int makelevel;
1942e2caf59SThomas Veerman 
1952e2caf59SThomas Veerman Boolean forceJobs = FALSE;
1962e2caf59SThomas Veerman 
1972e2caf59SThomas Veerman extern Lst parseIncPath;
1982e2caf59SThomas Veerman 
19984d9c625SLionel Sambuc /*
20084d9c625SLionel Sambuc  * For compatibility with the POSIX version of MAKEFLAGS that includes
20184d9c625SLionel Sambuc  * all the options with out -, convert flags to -f -l -a -g -s.
20284d9c625SLionel Sambuc  */
20384d9c625SLionel Sambuc static char *
explode(const char * flags)20484d9c625SLionel Sambuc explode(const char *flags)
20584d9c625SLionel Sambuc {
20684d9c625SLionel Sambuc     size_t len;
20784d9c625SLionel Sambuc     char *nf, *st;
20884d9c625SLionel Sambuc     const char *f;
20984d9c625SLionel Sambuc 
21084d9c625SLionel Sambuc     if (flags == NULL)
21184d9c625SLionel Sambuc 	return NULL;
21284d9c625SLionel Sambuc 
21384d9c625SLionel Sambuc     for (f = flags; *f; f++)
21484d9c625SLionel Sambuc 	if (!isalpha((unsigned char)*f))
21584d9c625SLionel Sambuc 	    break;
21684d9c625SLionel Sambuc 
21784d9c625SLionel Sambuc     if (*f)
21884d9c625SLionel Sambuc 	return bmake_strdup(flags);
21984d9c625SLionel Sambuc 
22084d9c625SLionel Sambuc     len = strlen(flags);
22184d9c625SLionel Sambuc     st = nf = bmake_malloc(len * 3 + 1);
22284d9c625SLionel Sambuc     while (*flags) {
22384d9c625SLionel Sambuc 	*nf++ = '-';
22484d9c625SLionel Sambuc 	*nf++ = *flags++;
22584d9c625SLionel Sambuc 	*nf++ = ' ';
22684d9c625SLionel Sambuc     }
22784d9c625SLionel Sambuc     *nf = '\0';
22884d9c625SLionel Sambuc     return st;
22984d9c625SLionel Sambuc }
23084d9c625SLionel Sambuc 
2312e2caf59SThomas Veerman static void
parse_debug_options(const char * argvalue)2322e2caf59SThomas Veerman parse_debug_options(const char *argvalue)
2332e2caf59SThomas Veerman {
2342e2caf59SThomas Veerman 	const char *modules;
2352e2caf59SThomas Veerman 	const char *mode;
2362e2caf59SThomas Veerman 	char *fname;
2372e2caf59SThomas Veerman 	int len;
2382e2caf59SThomas Veerman 
2392e2caf59SThomas Veerman 	for (modules = argvalue; *modules; ++modules) {
2402e2caf59SThomas Veerman 		switch (*modules) {
2412e2caf59SThomas Veerman 		case 'A':
2422e2caf59SThomas Veerman 			debug = ~0;
2432e2caf59SThomas Veerman 			break;
2442e2caf59SThomas Veerman 		case 'a':
2452e2caf59SThomas Veerman 			debug |= DEBUG_ARCH;
2462e2caf59SThomas Veerman 			break;
2472e2caf59SThomas Veerman 		case 'C':
2482e2caf59SThomas Veerman 			debug |= DEBUG_CWD;
2492e2caf59SThomas Veerman 			break;
2502e2caf59SThomas Veerman 		case 'c':
2512e2caf59SThomas Veerman 			debug |= DEBUG_COND;
2522e2caf59SThomas Veerman 			break;
2532e2caf59SThomas Veerman 		case 'd':
2542e2caf59SThomas Veerman 			debug |= DEBUG_DIR;
2552e2caf59SThomas Veerman 			break;
2562e2caf59SThomas Veerman 		case 'e':
2572e2caf59SThomas Veerman 			debug |= DEBUG_ERROR;
2582e2caf59SThomas Veerman 			break;
2592e2caf59SThomas Veerman 		case 'f':
2602e2caf59SThomas Veerman 			debug |= DEBUG_FOR;
2612e2caf59SThomas Veerman 			break;
2622e2caf59SThomas Veerman 		case 'g':
2632e2caf59SThomas Veerman 			if (modules[1] == '1') {
2642e2caf59SThomas Veerman 				debug |= DEBUG_GRAPH1;
2652e2caf59SThomas Veerman 				++modules;
2662e2caf59SThomas Veerman 			}
2672e2caf59SThomas Veerman 			else if (modules[1] == '2') {
2682e2caf59SThomas Veerman 				debug |= DEBUG_GRAPH2;
2692e2caf59SThomas Veerman 				++modules;
2702e2caf59SThomas Veerman 			}
2712e2caf59SThomas Veerman 			else if (modules[1] == '3') {
2722e2caf59SThomas Veerman 				debug |= DEBUG_GRAPH3;
2732e2caf59SThomas Veerman 				++modules;
2742e2caf59SThomas Veerman 			}
2752e2caf59SThomas Veerman 			break;
2762e2caf59SThomas Veerman 		case 'j':
2772e2caf59SThomas Veerman 			debug |= DEBUG_JOB;
2782e2caf59SThomas Veerman 			break;
2792e2caf59SThomas Veerman 		case 'l':
2802e2caf59SThomas Veerman 			debug |= DEBUG_LOUD;
2812e2caf59SThomas Veerman 			break;
2822e2caf59SThomas Veerman 		case 'M':
2832e2caf59SThomas Veerman 			debug |= DEBUG_META;
2842e2caf59SThomas Veerman 			break;
2852e2caf59SThomas Veerman 		case 'm':
2862e2caf59SThomas Veerman 			debug |= DEBUG_MAKE;
2872e2caf59SThomas Veerman 			break;
2882e2caf59SThomas Veerman 		case 'n':
2892e2caf59SThomas Veerman 			debug |= DEBUG_SCRIPT;
2902e2caf59SThomas Veerman 			break;
2912e2caf59SThomas Veerman 		case 'p':
2922e2caf59SThomas Veerman 			debug |= DEBUG_PARSE;
2932e2caf59SThomas Veerman 			break;
2942e2caf59SThomas Veerman 		case 's':
2952e2caf59SThomas Veerman 			debug |= DEBUG_SUFF;
2962e2caf59SThomas Veerman 			break;
2972e2caf59SThomas Veerman 		case 't':
2982e2caf59SThomas Veerman 			debug |= DEBUG_TARG;
2992e2caf59SThomas Veerman 			break;
3002bc7c627SLionel Sambuc 		case 'V':
3012bc7c627SLionel Sambuc 			debugVflag = TRUE;
3022bc7c627SLionel Sambuc 			break;
3032e2caf59SThomas Veerman 		case 'v':
3042e2caf59SThomas Veerman 			debug |= DEBUG_VAR;
3052e2caf59SThomas Veerman 			break;
3062e2caf59SThomas Veerman 		case 'x':
3072e2caf59SThomas Veerman 			debug |= DEBUG_SHELL;
3082e2caf59SThomas Veerman 			break;
3092e2caf59SThomas Veerman 		case 'F':
3102e2caf59SThomas Veerman 			if (debug_file != stdout && debug_file != stderr)
3112e2caf59SThomas Veerman 				fclose(debug_file);
3122bc7c627SLionel Sambuc 			if (*++modules == '+') {
3132bc7c627SLionel Sambuc 				modules++;
3142e2caf59SThomas Veerman 				mode = "a";
3152bc7c627SLionel Sambuc 			} else
3162e2caf59SThomas Veerman 				mode = "w";
3172e2caf59SThomas Veerman 			if (strcmp(modules, "stdout") == 0) {
3182e2caf59SThomas Veerman 				debug_file = stdout;
3192e2caf59SThomas Veerman 				goto debug_setbuf;
3202e2caf59SThomas Veerman 			}
3212e2caf59SThomas Veerman 			if (strcmp(modules, "stderr") == 0) {
3222e2caf59SThomas Veerman 				debug_file = stderr;
3232e2caf59SThomas Veerman 				goto debug_setbuf;
3242e2caf59SThomas Veerman 			}
3252e2caf59SThomas Veerman 			len = strlen(modules);
3262e2caf59SThomas Veerman 			fname = malloc(len + 20);
3272e2caf59SThomas Veerman 			memcpy(fname, modules, len + 1);
3282e2caf59SThomas Veerman 			/* Let the filename be modified by the pid */
3292e2caf59SThomas Veerman 			if (strcmp(fname + len - 3, ".%d") == 0)
3302e2caf59SThomas Veerman 				snprintf(fname + len - 2, 20, "%d", getpid());
3312e2caf59SThomas Veerman 			debug_file = fopen(fname, mode);
3322e2caf59SThomas Veerman 			if (!debug_file) {
3332e2caf59SThomas Veerman 				fprintf(stderr, "Cannot open debug file %s\n",
3342e2caf59SThomas Veerman 				    fname);
3352e2caf59SThomas Veerman 				usage();
3362e2caf59SThomas Veerman 			}
3372e2caf59SThomas Veerman 			free(fname);
3382e2caf59SThomas Veerman 			goto debug_setbuf;
3392e2caf59SThomas Veerman 		default:
3402e2caf59SThomas Veerman 			(void)fprintf(stderr,
3412e2caf59SThomas Veerman 			    "%s: illegal argument to d option -- %c\n",
3422e2caf59SThomas Veerman 			    progname, *modules);
3432e2caf59SThomas Veerman 			usage();
3442e2caf59SThomas Veerman 		}
3452e2caf59SThomas Veerman 	}
3462e2caf59SThomas Veerman debug_setbuf:
3472e2caf59SThomas Veerman 	/*
3482e2caf59SThomas Veerman 	 * Make the debug_file unbuffered, and make
3492e2caf59SThomas Veerman 	 * stdout line buffered (unless debugfile == stdout).
3502e2caf59SThomas Veerman 	 */
3512e2caf59SThomas Veerman 	setvbuf(debug_file, NULL, _IONBF, 0);
3522e2caf59SThomas Veerman 	if (debug_file != stdout) {
3532e2caf59SThomas Veerman 		setvbuf(stdout, NULL, _IOLBF, 0);
3542e2caf59SThomas Veerman 	}
3552e2caf59SThomas Veerman }
3562e2caf59SThomas Veerman 
3572e2caf59SThomas Veerman /*-
3582e2caf59SThomas Veerman  * MainParseArgs --
3592e2caf59SThomas Veerman  *	Parse a given argument vector. Called from main() and from
3602e2caf59SThomas Veerman  *	Main_ParseArgLine() when the .MAKEFLAGS target is used.
3612e2caf59SThomas Veerman  *
3622e2caf59SThomas Veerman  *	XXX: Deal with command line overriding .MAKEFLAGS in makefile
3632e2caf59SThomas Veerman  *
3642e2caf59SThomas Veerman  * Results:
3652e2caf59SThomas Veerman  *	None
3662e2caf59SThomas Veerman  *
3672e2caf59SThomas Veerman  * Side Effects:
3682e2caf59SThomas Veerman  *	Various global and local flags will be set depending on the flags
3692e2caf59SThomas Veerman  *	given
3702e2caf59SThomas Veerman  */
3712e2caf59SThomas Veerman static void
MainParseArgs(int argc,char ** argv)3722e2caf59SThomas Veerman MainParseArgs(int argc, char **argv)
3732e2caf59SThomas Veerman {
3742e2caf59SThomas Veerman 	char *p;
3752e2caf59SThomas Veerman 	int c = '?';
3762e2caf59SThomas Veerman 	int arginc;
3772e2caf59SThomas Veerman 	char *argvalue;
3782e2caf59SThomas Veerman 	const char *getopt_def;
3792e2caf59SThomas Veerman 	char *optscan;
3802e2caf59SThomas Veerman 	Boolean inOption, dashDash = FALSE;
3812e2caf59SThomas Veerman 	char found_path[MAXPATHLEN + 1];	/* for searching for sys.mk */
3822e2caf59SThomas Veerman 
38384d9c625SLionel Sambuc #define OPTFLAGS "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstw"
3842e2caf59SThomas Veerman /* Can't actually use getopt(3) because rescanning is not portable */
3852e2caf59SThomas Veerman 
3862e2caf59SThomas Veerman 	getopt_def = OPTFLAGS;
3872e2caf59SThomas Veerman rearg:
3882e2caf59SThomas Veerman 	inOption = FALSE;
3892e2caf59SThomas Veerman 	optscan = NULL;
3902e2caf59SThomas Veerman 	while(argc > 1) {
3912e2caf59SThomas Veerman 		char *getopt_spec;
3922e2caf59SThomas Veerman 		if(!inOption)
3932e2caf59SThomas Veerman 			optscan = argv[1];
3942e2caf59SThomas Veerman 		c = *optscan++;
3952e2caf59SThomas Veerman 		arginc = 0;
3962e2caf59SThomas Veerman 		if(inOption) {
3972e2caf59SThomas Veerman 			if(c == '\0') {
3982e2caf59SThomas Veerman 				++argv;
3992e2caf59SThomas Veerman 				--argc;
4002e2caf59SThomas Veerman 				inOption = FALSE;
4012e2caf59SThomas Veerman 				continue;
4022e2caf59SThomas Veerman 			}
4032e2caf59SThomas Veerman 		} else {
4042e2caf59SThomas Veerman 			if (c != '-' || dashDash)
4052e2caf59SThomas Veerman 				break;
4062e2caf59SThomas Veerman 			inOption = TRUE;
4072e2caf59SThomas Veerman 			c = *optscan++;
4082e2caf59SThomas Veerman 		}
4092e2caf59SThomas Veerman 		/* '-' found at some earlier point */
4102e2caf59SThomas Veerman 		getopt_spec = strchr(getopt_def, c);
4112e2caf59SThomas Veerman 		if(c != '\0' && getopt_spec != NULL && getopt_spec[1] == ':') {
4122e2caf59SThomas Veerman 			/* -<something> found, and <something> should have an arg */
4132e2caf59SThomas Veerman 			inOption = FALSE;
4142e2caf59SThomas Veerman 			arginc = 1;
4152e2caf59SThomas Veerman 			argvalue = optscan;
4162e2caf59SThomas Veerman 			if(*argvalue == '\0') {
4172e2caf59SThomas Veerman 				if (argc < 3)
4182e2caf59SThomas Veerman 					goto noarg;
4192e2caf59SThomas Veerman 				argvalue = argv[2];
4202e2caf59SThomas Veerman 				arginc = 2;
4212e2caf59SThomas Veerman 			}
4222e2caf59SThomas Veerman 		} else {
4232e2caf59SThomas Veerman 			argvalue = NULL;
4242e2caf59SThomas Veerman 		}
4252e2caf59SThomas Veerman 		switch(c) {
4262e2caf59SThomas Veerman 		case '\0':
4272e2caf59SThomas Veerman 			arginc = 1;
4282e2caf59SThomas Veerman 			inOption = FALSE;
4292e2caf59SThomas Veerman 			break;
4302e2caf59SThomas Veerman 		case 'B':
4312e2caf59SThomas Veerman 			compatMake = TRUE;
4322e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
4332e2caf59SThomas Veerman 			Var_Set(MAKE_MODE, "compat", VAR_GLOBAL, 0);
4342e2caf59SThomas Veerman 			break;
4352e2caf59SThomas Veerman 		case 'C':
4362e2caf59SThomas Veerman 			if (chdir(argvalue) == -1) {
4372e2caf59SThomas Veerman 				(void)fprintf(stderr,
4382e2caf59SThomas Veerman 					      "%s: chdir %s: %s\n",
4392e2caf59SThomas Veerman 					      progname, argvalue,
4402e2caf59SThomas Veerman 					      strerror(errno));
4412e2caf59SThomas Veerman 				exit(1);
4422e2caf59SThomas Veerman 			}
4432e2caf59SThomas Veerman 			if (getcwd(curdir, MAXPATHLEN) == NULL) {
4442e2caf59SThomas Veerman 				(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
4452e2caf59SThomas Veerman 				exit(2);
4462e2caf59SThomas Veerman 			}
4472e2caf59SThomas Veerman 			ignorePWD = TRUE;
4482e2caf59SThomas Veerman 			break;
4492e2caf59SThomas Veerman 		case 'D':
4502e2caf59SThomas Veerman 			if (argvalue == NULL || argvalue[0] == 0) goto noarg;
4512e2caf59SThomas Veerman 			Var_Set(argvalue, "1", VAR_GLOBAL, 0);
4522e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
4532e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
4542e2caf59SThomas Veerman 			break;
4552e2caf59SThomas Veerman 		case 'I':
4562e2caf59SThomas Veerman 			if (argvalue == NULL) goto noarg;
4572e2caf59SThomas Veerman 			Parse_AddIncludeDir(argvalue);
4582e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
4592e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
4602e2caf59SThomas Veerman 			break;
4612e2caf59SThomas Veerman 		case 'J':
4622e2caf59SThomas Veerman 			if (argvalue == NULL) goto noarg;
4632e2caf59SThomas Veerman 			if (sscanf(argvalue, "%d,%d", &jp_0, &jp_1) != 2) {
4642e2caf59SThomas Veerman 			    (void)fprintf(stderr,
4652e2caf59SThomas Veerman 				"%s: internal error -- J option malformed (%s)\n",
4662e2caf59SThomas Veerman 				progname, argvalue);
4672e2caf59SThomas Veerman 				usage();
4682e2caf59SThomas Veerman 			}
4692e2caf59SThomas Veerman 			if ((fcntl(jp_0, F_GETFD, 0) < 0) ||
4702e2caf59SThomas Veerman 			    (fcntl(jp_1, F_GETFD, 0) < 0)) {
4712e2caf59SThomas Veerman #if 0
4722e2caf59SThomas Veerman 			    (void)fprintf(stderr,
4732e2caf59SThomas Veerman 				"%s: ###### warning -- J descriptors were closed!\n",
4742e2caf59SThomas Veerman 				progname);
4752e2caf59SThomas Veerman 			    exit(2);
4762e2caf59SThomas Veerman #endif
4772e2caf59SThomas Veerman 			    jp_0 = -1;
4782e2caf59SThomas Veerman 			    jp_1 = -1;
4792e2caf59SThomas Veerman 			    compatMake = TRUE;
4802e2caf59SThomas Veerman 			} else {
4812e2caf59SThomas Veerman 			    Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
4822e2caf59SThomas Veerman 			    Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
4832e2caf59SThomas Veerman 			    jobServer = TRUE;
4842e2caf59SThomas Veerman 			}
4852e2caf59SThomas Veerman 			break;
4862e2caf59SThomas Veerman 		case 'N':
4872e2caf59SThomas Veerman 			noExecute = TRUE;
4882e2caf59SThomas Veerman 			noRecursiveExecute = TRUE;
4892e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-N", VAR_GLOBAL);
4902e2caf59SThomas Veerman 			break;
4912e2caf59SThomas Veerman 		case 'S':
4922e2caf59SThomas Veerman 			keepgoing = FALSE;
4932e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
4942e2caf59SThomas Veerman 			break;
4952e2caf59SThomas Veerman 		case 'T':
4962e2caf59SThomas Veerman 			if (argvalue == NULL) goto noarg;
4972e2caf59SThomas Veerman 			tracefile = bmake_strdup(argvalue);
4982e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-T", VAR_GLOBAL);
4992e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
5002e2caf59SThomas Veerman 			break;
5012e2caf59SThomas Veerman 		case 'V':
5022e2caf59SThomas Veerman 			if (argvalue == NULL) goto noarg;
5032e2caf59SThomas Veerman 			printVars = TRUE;
5042e2caf59SThomas Veerman 			(void)Lst_AtEnd(variables, argvalue);
5052e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
5062e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
5072e2caf59SThomas Veerman 			break;
5082e2caf59SThomas Veerman 		case 'W':
5092e2caf59SThomas Veerman 			parseWarnFatal = TRUE;
5102e2caf59SThomas Veerman 			break;
5112e2caf59SThomas Veerman 		case 'X':
5122e2caf59SThomas Veerman 			varNoExportEnv = TRUE;
5132e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-X", VAR_GLOBAL);
5142e2caf59SThomas Veerman 			break;
5152e2caf59SThomas Veerman 		case 'd':
5162e2caf59SThomas Veerman 			if (argvalue == NULL) goto noarg;
5172e2caf59SThomas Veerman 			/* If '-d-opts' don't pass to children */
5182e2caf59SThomas Veerman 			if (argvalue[0] == '-')
5192e2caf59SThomas Veerman 			    argvalue++;
5202e2caf59SThomas Veerman 			else {
5212e2caf59SThomas Veerman 			    Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
5222e2caf59SThomas Veerman 			    Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
5232e2caf59SThomas Veerman 			}
5242e2caf59SThomas Veerman 			parse_debug_options(argvalue);
5252e2caf59SThomas Veerman 			break;
5262e2caf59SThomas Veerman 		case 'e':
5272e2caf59SThomas Veerman 			checkEnvFirst = TRUE;
5282e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
5292e2caf59SThomas Veerman 			break;
5302e2caf59SThomas Veerman 		case 'f':
5312e2caf59SThomas Veerman 			if (argvalue == NULL) goto noarg;
5322e2caf59SThomas Veerman 			(void)Lst_AtEnd(makefiles, argvalue);
5332e2caf59SThomas Veerman 			break;
5342e2caf59SThomas Veerman 		case 'i':
5352e2caf59SThomas Veerman 			ignoreErrors = TRUE;
5362e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
5372e2caf59SThomas Veerman 			break;
5382e2caf59SThomas Veerman 		case 'j':
5392e2caf59SThomas Veerman 			if (argvalue == NULL) goto noarg;
5402e2caf59SThomas Veerman 			forceJobs = TRUE;
5412e2caf59SThomas Veerman 			maxJobs = strtol(argvalue, &p, 0);
5422e2caf59SThomas Veerman 			if (*p != '\0' || maxJobs < 1) {
5432e2caf59SThomas Veerman 				(void)fprintf(stderr, "%s: illegal argument to -j -- must be positive integer!\n",
5442e2caf59SThomas Veerman 				    progname);
5452e2caf59SThomas Veerman 				exit(1);
5462e2caf59SThomas Veerman 			}
5472e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
5482e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
5492e2caf59SThomas Veerman 			Var_Set(".MAKE.JOBS", argvalue, VAR_GLOBAL, 0);
5502e2caf59SThomas Veerman 			maxJobTokens = maxJobs;
5512e2caf59SThomas Veerman 			break;
5522e2caf59SThomas Veerman 		case 'k':
5532e2caf59SThomas Veerman 			keepgoing = TRUE;
5542e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
5552e2caf59SThomas Veerman 			break;
5562e2caf59SThomas Veerman 		case 'm':
5572e2caf59SThomas Veerman 			if (argvalue == NULL) goto noarg;
5582e2caf59SThomas Veerman 			/* look for magic parent directory search string */
5592e2caf59SThomas Veerman 			if (strncmp(".../", argvalue, 4) == 0) {
5602e2caf59SThomas Veerman 				if (!Dir_FindHereOrAbove(curdir, argvalue+4,
5612e2caf59SThomas Veerman 				    found_path, sizeof(found_path)))
5622e2caf59SThomas Veerman 					break;		/* nothing doing */
5632e2caf59SThomas Veerman 				(void)Dir_AddDir(sysIncPath, found_path);
5642e2caf59SThomas Veerman 			} else {
5652e2caf59SThomas Veerman 				(void)Dir_AddDir(sysIncPath, argvalue);
5662e2caf59SThomas Veerman 			}
5672e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
5682e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
5692e2caf59SThomas Veerman 			break;
5702e2caf59SThomas Veerman 		case 'n':
5712e2caf59SThomas Veerman 			noExecute = TRUE;
5722e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
5732e2caf59SThomas Veerman 			break;
5742e2caf59SThomas Veerman 		case 'q':
5752e2caf59SThomas Veerman 			queryFlag = TRUE;
5762e2caf59SThomas Veerman 			/* Kind of nonsensical, wot? */
5772e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
5782e2caf59SThomas Veerman 			break;
5792e2caf59SThomas Veerman 		case 'r':
5802e2caf59SThomas Veerman 			noBuiltins = TRUE;
5812e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
5822e2caf59SThomas Veerman 			break;
5832e2caf59SThomas Veerman 		case 's':
5842e2caf59SThomas Veerman 			beSilent = TRUE;
5852e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
5862e2caf59SThomas Veerman 			break;
5872e2caf59SThomas Veerman 		case 't':
5882e2caf59SThomas Veerman 			touchFlag = TRUE;
5892e2caf59SThomas Veerman 			Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
5902e2caf59SThomas Veerman 			break;
59184d9c625SLionel Sambuc 		case 'w':
59284d9c625SLionel Sambuc 			enterFlag = TRUE;
59384d9c625SLionel Sambuc 			Var_Append(MAKEFLAGS, "-w", VAR_GLOBAL);
59484d9c625SLionel Sambuc 			break;
5952e2caf59SThomas Veerman 		case '-':
5962e2caf59SThomas Veerman 			dashDash = TRUE;
5972e2caf59SThomas Veerman 			break;
5982e2caf59SThomas Veerman 		default:
5992e2caf59SThomas Veerman 		case '?':
6002e2caf59SThomas Veerman 			usage();
6012e2caf59SThomas Veerman 		}
6022e2caf59SThomas Veerman 		argv += arginc;
6032e2caf59SThomas Veerman 		argc -= arginc;
6042e2caf59SThomas Veerman 	}
6052e2caf59SThomas Veerman 
6062e2caf59SThomas Veerman 	oldVars = TRUE;
6072e2caf59SThomas Veerman 
6082e2caf59SThomas Veerman 	/*
6092e2caf59SThomas Veerman 	 * See if the rest of the arguments are variable assignments and
6102e2caf59SThomas Veerman 	 * perform them if so. Else take them to be targets and stuff them
6112e2caf59SThomas Veerman 	 * on the end of the "create" list.
6122e2caf59SThomas Veerman 	 */
6132e2caf59SThomas Veerman 	for (; argc > 1; ++argv, --argc)
6142e2caf59SThomas Veerman 		if (Parse_IsVar(argv[1])) {
6152e2caf59SThomas Veerman 			Parse_DoVar(argv[1], VAR_CMD);
6162e2caf59SThomas Veerman 		} else {
6172e2caf59SThomas Veerman 			if (!*argv[1])
6182e2caf59SThomas Veerman 				Punt("illegal (null) argument.");
6192e2caf59SThomas Veerman 			if (*argv[1] == '-' && !dashDash)
6202e2caf59SThomas Veerman 				goto rearg;
6212e2caf59SThomas Veerman 			(void)Lst_AtEnd(create, bmake_strdup(argv[1]));
6222e2caf59SThomas Veerman 		}
6232e2caf59SThomas Veerman 
6242e2caf59SThomas Veerman 	return;
6252e2caf59SThomas Veerman noarg:
6262e2caf59SThomas Veerman 	(void)fprintf(stderr, "%s: option requires an argument -- %c\n",
6272e2caf59SThomas Veerman 	    progname, c);
6282e2caf59SThomas Veerman 	usage();
6292e2caf59SThomas Veerman }
6302e2caf59SThomas Veerman 
6312e2caf59SThomas Veerman /*-
6322e2caf59SThomas Veerman  * Main_ParseArgLine --
6332e2caf59SThomas Veerman  *  	Used by the parse module when a .MFLAGS or .MAKEFLAGS target
6342e2caf59SThomas Veerman  *	is encountered and by main() when reading the .MAKEFLAGS envariable.
6352e2caf59SThomas Veerman  *	Takes a line of arguments and breaks it into its
6362e2caf59SThomas Veerman  * 	component words and passes those words and the number of them to the
6372e2caf59SThomas Veerman  *	MainParseArgs function.
6382e2caf59SThomas Veerman  *	The line should have all its leading whitespace removed.
6392e2caf59SThomas Veerman  *
6402e2caf59SThomas Veerman  * Input:
6412e2caf59SThomas Veerman  *	line		Line to fracture
6422e2caf59SThomas Veerman  *
6432e2caf59SThomas Veerman  * Results:
6442e2caf59SThomas Veerman  *	None
6452e2caf59SThomas Veerman  *
6462e2caf59SThomas Veerman  * Side Effects:
6472e2caf59SThomas Veerman  *	Only those that come from the various arguments.
6482e2caf59SThomas Veerman  */
6492e2caf59SThomas Veerman void
Main_ParseArgLine(const char * line)6502e2caf59SThomas Veerman Main_ParseArgLine(const char *line)
6512e2caf59SThomas Veerman {
6522e2caf59SThomas Veerman 	char **argv;			/* Manufactured argument vector */
6532e2caf59SThomas Veerman 	int argc;			/* Number of arguments in argv */
6542e2caf59SThomas Veerman 	char *args;			/* Space used by the args */
6552e2caf59SThomas Veerman 	char *buf, *p1;
6562e2caf59SThomas Veerman 	char *argv0 = Var_Value(".MAKE", VAR_GLOBAL, &p1);
6572e2caf59SThomas Veerman 	size_t len;
6582e2caf59SThomas Veerman 
6592e2caf59SThomas Veerman 	if (line == NULL)
6602e2caf59SThomas Veerman 		return;
6612e2caf59SThomas Veerman 	for (; *line == ' '; ++line)
6622e2caf59SThomas Veerman 		continue;
6632e2caf59SThomas Veerman 	if (!*line)
6642e2caf59SThomas Veerman 		return;
6652e2caf59SThomas Veerman 
6662e2caf59SThomas Veerman 	buf = bmake_malloc(len = strlen(line) + strlen(argv0) + 2);
6672e2caf59SThomas Veerman 	(void)snprintf(buf, len, "%s %s", argv0, line);
6682e2caf59SThomas Veerman 	if (p1)
6692e2caf59SThomas Veerman 		free(p1);
6702e2caf59SThomas Veerman 
6712e2caf59SThomas Veerman 	argv = brk_string(buf, &argc, TRUE, &args);
6722e2caf59SThomas Veerman 	if (argv == NULL) {
6732e2caf59SThomas Veerman 		Error("Unterminated quoted string [%s]", buf);
6742e2caf59SThomas Veerman 		free(buf);
6752e2caf59SThomas Veerman 		return;
6762e2caf59SThomas Veerman 	}
6772e2caf59SThomas Veerman 	free(buf);
6782e2caf59SThomas Veerman 	MainParseArgs(argc, argv);
6792e2caf59SThomas Veerman 
6802e2caf59SThomas Veerman 	free(args);
6812e2caf59SThomas Veerman 	free(argv);
6822e2caf59SThomas Veerman }
6832e2caf59SThomas Veerman 
6842e2caf59SThomas Veerman Boolean
Main_SetObjdir(const char * path)6852e2caf59SThomas Veerman Main_SetObjdir(const char *path)
6862e2caf59SThomas Veerman {
6872e2caf59SThomas Veerman 	struct stat sb;
6882e2caf59SThomas Veerman 	char *p = NULL;
6892e2caf59SThomas Veerman 	char buf[MAXPATHLEN + 1];
6902e2caf59SThomas Veerman 	Boolean rc = FALSE;
6912e2caf59SThomas Veerman 
6922e2caf59SThomas Veerman 	/* expand variable substitutions */
6932e2caf59SThomas Veerman 	if (strchr(path, '$') != 0) {
6942e2caf59SThomas Veerman 		snprintf(buf, MAXPATHLEN, "%s", path);
6952e2caf59SThomas Veerman 		path = p = Var_Subst(NULL, buf, VAR_GLOBAL, 0);
6962e2caf59SThomas Veerman 	}
6972e2caf59SThomas Veerman 
6982e2caf59SThomas Veerman 	if (path[0] != '/') {
6992e2caf59SThomas Veerman 		snprintf(buf, MAXPATHLEN, "%s/%s", curdir, path);
7002e2caf59SThomas Veerman 		path = buf;
7012e2caf59SThomas Veerman 	}
7022e2caf59SThomas Veerman 
7032e2caf59SThomas Veerman 	/* look for the directory and try to chdir there */
7042e2caf59SThomas Veerman 	if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
7052e2caf59SThomas Veerman 		if (chdir(path)) {
7062e2caf59SThomas Veerman 			(void)fprintf(stderr, "make warning: %s: %s.\n",
7072e2caf59SThomas Veerman 				      path, strerror(errno));
7082e2caf59SThomas Veerman 		} else {
7092e2caf59SThomas Veerman 			strncpy(objdir, path, MAXPATHLEN);
7102e2caf59SThomas Veerman 			Var_Set(".OBJDIR", objdir, VAR_GLOBAL, 0);
7112e2caf59SThomas Veerman 			setenv("PWD", objdir, 1);
7122e2caf59SThomas Veerman 			Dir_InitDot();
7132e2caf59SThomas Veerman 			rc = TRUE;
7140a6a1f1dSLionel Sambuc 			if (enterFlag && strcmp(objdir, curdir) != 0)
7150a6a1f1dSLionel Sambuc 				enterFlagObj = TRUE;
7162e2caf59SThomas Veerman 		}
7172e2caf59SThomas Veerman 	}
7182e2caf59SThomas Veerman 
7192e2caf59SThomas Veerman 	if (p)
7202e2caf59SThomas Veerman 		free(p);
7212e2caf59SThomas Veerman 	return rc;
7222e2caf59SThomas Veerman }
7232e2caf59SThomas Veerman 
7242e2caf59SThomas Veerman /*-
7252e2caf59SThomas Veerman  * ReadAllMakefiles --
7262e2caf59SThomas Veerman  *	wrapper around ReadMakefile() to read all.
7272e2caf59SThomas Veerman  *
7282e2caf59SThomas Veerman  * Results:
7292e2caf59SThomas Veerman  *	TRUE if ok, FALSE on error
7302e2caf59SThomas Veerman  */
7312e2caf59SThomas Veerman static int
ReadAllMakefiles(const void * p,const void * q)7322e2caf59SThomas Veerman ReadAllMakefiles(const void *p, const void *q)
7332e2caf59SThomas Veerman {
7342e2caf59SThomas Veerman 	return (ReadMakefile(p, q) == 0);
7352e2caf59SThomas Veerman }
7362e2caf59SThomas Veerman 
7372e2caf59SThomas Veerman int
str2Lst_Append(Lst lp,char * str,const char * sep)7382e2caf59SThomas Veerman str2Lst_Append(Lst lp, char *str, const char *sep)
7392e2caf59SThomas Veerman {
7402e2caf59SThomas Veerman     char *cp;
7412e2caf59SThomas Veerman     int n;
7422e2caf59SThomas Veerman 
7432e2caf59SThomas Veerman     if (!sep)
7442e2caf59SThomas Veerman 	sep = " \t";
7452e2caf59SThomas Veerman 
7462e2caf59SThomas Veerman     for (n = 0, cp = strtok(str, sep); cp; cp = strtok(NULL, sep)) {
7472e2caf59SThomas Veerman 	(void)Lst_AtEnd(lp, cp);
7482e2caf59SThomas Veerman 	n++;
7492e2caf59SThomas Veerman     }
7502e2caf59SThomas Veerman     return (n);
7512e2caf59SThomas Veerman }
7522e2caf59SThomas Veerman 
7532e2caf59SThomas Veerman #ifdef SIGINFO
7542e2caf59SThomas Veerman /*ARGSUSED*/
7552e2caf59SThomas Veerman static void
siginfo(int signo MAKE_ATTR_UNUSED)7562bc7c627SLionel Sambuc siginfo(int signo MAKE_ATTR_UNUSED)
7572e2caf59SThomas Veerman {
7582e2caf59SThomas Veerman 	char dir[MAXPATHLEN];
7592e2caf59SThomas Veerman 	char str[2 * MAXPATHLEN];
7602e2caf59SThomas Veerman 	int len;
7612e2caf59SThomas Veerman 	if (getcwd(dir, sizeof(dir)) == NULL)
7622e2caf59SThomas Veerman 		return;
7632e2caf59SThomas Veerman 	len = snprintf(str, sizeof(str), "%s: Working in: %s\n", progname, dir);
7642e2caf59SThomas Veerman 	if (len > 0)
7652e2caf59SThomas Veerman 		(void)write(STDERR_FILENO, str, (size_t)len);
7662e2caf59SThomas Veerman }
7672e2caf59SThomas Veerman #endif
7682e2caf59SThomas Veerman 
7692e2caf59SThomas Veerman /*
7702e2caf59SThomas Veerman  * Allow makefiles some control over the mode we run in.
7712e2caf59SThomas Veerman  */
7722e2caf59SThomas Veerman void
MakeMode(const char * mode)7732e2caf59SThomas Veerman MakeMode(const char *mode)
7742e2caf59SThomas Veerman {
7752e2caf59SThomas Veerman     char *mp = NULL;
7762e2caf59SThomas Veerman 
7772e2caf59SThomas Veerman     if (!mode)
7782e2caf59SThomas Veerman 	mode = mp = Var_Subst(NULL, "${" MAKE_MODE ":tl}", VAR_GLOBAL, 0);
7792e2caf59SThomas Veerman 
7802e2caf59SThomas Veerman     if (mode && *mode) {
7812e2caf59SThomas Veerman 	if (strstr(mode, "compat")) {
7822e2caf59SThomas Veerman 	    compatMake = TRUE;
7832e2caf59SThomas Veerman 	    forceJobs = FALSE;
7842e2caf59SThomas Veerman 	}
7852e2caf59SThomas Veerman #if USE_META
7862e2caf59SThomas Veerman 	if (strstr(mode, "meta"))
78784d9c625SLionel Sambuc 	    meta_mode_init(mode);
7882e2caf59SThomas Veerman #endif
7892e2caf59SThomas Veerman     }
7902e2caf59SThomas Veerman     if (mp)
7912e2caf59SThomas Veerman 	free(mp);
7922e2caf59SThomas Veerman }
7932e2caf59SThomas Veerman 
7942e2caf59SThomas Veerman /*-
7952e2caf59SThomas Veerman  * main --
7962e2caf59SThomas Veerman  *	The main function, for obvious reasons. Initializes variables
7972e2caf59SThomas Veerman  *	and a few modules, then parses the arguments give it in the
7982e2caf59SThomas Veerman  *	environment and on the command line. Reads the system makefile
7992e2caf59SThomas Veerman  *	followed by either Makefile, makefile or the file given by the
8002e2caf59SThomas Veerman  *	-f argument. Sets the .MAKEFLAGS PMake variable based on all the
8012e2caf59SThomas Veerman  *	flags it has received by then uses either the Make or the Compat
8022e2caf59SThomas Veerman  *	module to create the initial list of targets.
8032e2caf59SThomas Veerman  *
8042e2caf59SThomas Veerman  * Results:
8052e2caf59SThomas Veerman  *	If -q was given, exits -1 if anything was out-of-date. Else it exits
8062e2caf59SThomas Veerman  *	0.
8072e2caf59SThomas Veerman  *
8082e2caf59SThomas Veerman  * Side Effects:
8092e2caf59SThomas Veerman  *	The program exits when done. Targets are created. etc. etc. etc.
8102e2caf59SThomas Veerman  */
8112e2caf59SThomas Veerman int
main(int argc,char ** argv)8122e2caf59SThomas Veerman main(int argc, char **argv)
8132e2caf59SThomas Veerman {
8142e2caf59SThomas Veerman 	Lst targs;	/* target nodes to create -- passed to Make_Init */
8152e2caf59SThomas Veerman 	Boolean outOfDate = FALSE; 	/* FALSE if all targets up to date */
8162e2caf59SThomas Veerman 	struct stat sb, sa;
81784d9c625SLionel Sambuc 	char *p1, *path;
8182e2caf59SThomas Veerman 	char mdpath[MAXPATHLEN];
8192e2caf59SThomas Veerman     	const char *machine = getenv("MACHINE");
8202e2caf59SThomas Veerman 	const char *machine_arch = getenv("MACHINE_ARCH");
8212e2caf59SThomas Veerman 	char *syspath = getenv("MAKESYSPATH");
8222e2caf59SThomas Veerman 	Lst sysMkPath;			/* Path of sys.mk */
8232e2caf59SThomas Veerman 	char *cp = NULL, *start;
8242e2caf59SThomas Veerman 					/* avoid faults on read-only strings */
8252e2caf59SThomas Veerman 	static char defsyspath[] = _PATH_DEFSYSPATH;
8262e2caf59SThomas Veerman 	char found_path[MAXPATHLEN + 1];	/* for searching for sys.mk */
8272e2caf59SThomas Veerman 	struct timeval rightnow;		/* to initialize random seed */
8282e2caf59SThomas Veerman 	struct utsname utsname;
8292e2caf59SThomas Veerman 
8302e2caf59SThomas Veerman 	/* default to writing debug to stderr */
8312e2caf59SThomas Veerman 	debug_file = stderr;
8322e2caf59SThomas Veerman 
8332e2caf59SThomas Veerman #ifdef SIGINFO
8342e2caf59SThomas Veerman 	(void)bmake_signal(SIGINFO, siginfo);
8352e2caf59SThomas Veerman #endif
8362e2caf59SThomas Veerman 	/*
8372e2caf59SThomas Veerman 	 * Set the seed to produce a different random sequence
8382e2caf59SThomas Veerman 	 * on each program execution.
8392e2caf59SThomas Veerman 	 */
8402e2caf59SThomas Veerman 	gettimeofday(&rightnow, NULL);
8412e2caf59SThomas Veerman 	srandom(rightnow.tv_sec + rightnow.tv_usec);
8422e2caf59SThomas Veerman 
8432e2caf59SThomas Veerman 	if ((progname = strrchr(argv[0], '/')) != NULL)
8442e2caf59SThomas Veerman 		progname++;
8452e2caf59SThomas Veerman 	else
8462e2caf59SThomas Veerman 		progname = argv[0];
847ac5b3e53SLionel Sambuc #if defined(MAKE_NATIVE) || (defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE))
8482e2caf59SThomas Veerman 	/*
8492e2caf59SThomas Veerman 	 * get rid of resource limit on file descriptors
8502e2caf59SThomas Veerman 	 */
8512e2caf59SThomas Veerman 	{
8522e2caf59SThomas Veerman 		struct rlimit rl;
8532e2caf59SThomas Veerman 		if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
8542e2caf59SThomas Veerman 		    rl.rlim_cur != rl.rlim_max) {
8552e2caf59SThomas Veerman 			rl.rlim_cur = rl.rlim_max;
8562e2caf59SThomas Veerman 			(void)setrlimit(RLIMIT_NOFILE, &rl);
8572e2caf59SThomas Veerman 		}
8582e2caf59SThomas Veerman 	}
8592e2caf59SThomas Veerman #endif
8602e2caf59SThomas Veerman 
86184d9c625SLionel Sambuc 	if (uname(&utsname) == -1) {
86284d9c625SLionel Sambuc 	    (void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
86384d9c625SLionel Sambuc 		strerror(errno));
86484d9c625SLionel Sambuc 	    exit(2);
86584d9c625SLionel Sambuc 	}
86684d9c625SLionel Sambuc 
8672e2caf59SThomas Veerman 	/*
8682e2caf59SThomas Veerman 	 * Get the name of this type of MACHINE from utsname
8692e2caf59SThomas Veerman 	 * so we can share an executable for similar machines.
8702e2caf59SThomas Veerman 	 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
8712e2caf59SThomas Veerman 	 *
8722e2caf59SThomas Veerman 	 * Note that both MACHINE and MACHINE_ARCH are decided at
8732e2caf59SThomas Veerman 	 * run-time.
8742e2caf59SThomas Veerman 	 */
8752e2caf59SThomas Veerman 	if (!machine) {
8762e2caf59SThomas Veerman #ifdef MAKE_NATIVE
8772e2caf59SThomas Veerman 	    machine = utsname.machine;
8782e2caf59SThomas Veerman #else
8792e2caf59SThomas Veerman #ifdef MAKE_MACHINE
8802e2caf59SThomas Veerman 	    machine = MAKE_MACHINE;
8812e2caf59SThomas Veerman #else
8822e2caf59SThomas Veerman 	    machine = "unknown";
8832e2caf59SThomas Veerman #endif
8842e2caf59SThomas Veerman #endif
8852e2caf59SThomas Veerman 	}
8862e2caf59SThomas Veerman 
8872e2caf59SThomas Veerman 	if (!machine_arch) {
888*d2532d3dSDavid van Moolenbroek #if defined(MAKE_NATIVE)
88984d9c625SLionel Sambuc 	    static char machine_arch_buf[sizeof(utsname.machine)];
89084d9c625SLionel Sambuc 	    const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
89184d9c625SLionel Sambuc 	    size_t len = sizeof(machine_arch_buf);
89284d9c625SLionel Sambuc 
89384d9c625SLionel Sambuc 	    if (sysctl(mib, __arraycount(mib), machine_arch_buf,
89484d9c625SLionel Sambuc 		    &len, NULL, 0) < 0) {
89584d9c625SLionel Sambuc 		(void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
89684d9c625SLionel Sambuc 		    strerror(errno));
89784d9c625SLionel Sambuc 		exit(2);
89884d9c625SLionel Sambuc 	    }
89984d9c625SLionel Sambuc 
90084d9c625SLionel Sambuc 	    machine_arch = machine_arch_buf;
90184d9c625SLionel Sambuc #else
9022e2caf59SThomas Veerman #ifndef MACHINE_ARCH
9032e2caf59SThomas Veerman #ifdef MAKE_MACHINE_ARCH
9042e2caf59SThomas Veerman             machine_arch = MAKE_MACHINE_ARCH;
9052e2caf59SThomas Veerman #else
9062e2caf59SThomas Veerman 	    machine_arch = "unknown";
9072e2caf59SThomas Veerman #endif
9082e2caf59SThomas Veerman #else
9092e2caf59SThomas Veerman 	    machine_arch = MACHINE_ARCH;
9102e2caf59SThomas Veerman #endif
91184d9c625SLionel Sambuc #endif
9122e2caf59SThomas Veerman 	}
9132e2caf59SThomas Veerman 
9142e2caf59SThomas Veerman 	myPid = getpid();		/* remember this for vFork() */
9152e2caf59SThomas Veerman 
9162e2caf59SThomas Veerman 	/*
9172e2caf59SThomas Veerman 	 * Just in case MAKEOBJDIR wants us to do something tricky.
9182e2caf59SThomas Veerman 	 */
9192e2caf59SThomas Veerman 	Var_Init();		/* Initialize the lists of variables for
9202e2caf59SThomas Veerman 				 * parsing arguments */
92184d9c625SLionel Sambuc 	Var_Set(".MAKE.OS", utsname.sysname, VAR_GLOBAL, 0);
9222e2caf59SThomas Veerman 	Var_Set("MACHINE", machine, VAR_GLOBAL, 0);
9232e2caf59SThomas Veerman 	Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL, 0);
9242e2caf59SThomas Veerman #ifdef MAKE_VERSION
9252e2caf59SThomas Veerman 	Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL, 0);
9262e2caf59SThomas Veerman #endif
9272e2caf59SThomas Veerman 	Var_Set(".newline", "\n", VAR_GLOBAL, 0); /* handy for :@ loops */
9282e2caf59SThomas Veerman 	/*
9292e2caf59SThomas Veerman 	 * This is the traditional preference for makefiles.
9302e2caf59SThomas Veerman 	 */
9312e2caf59SThomas Veerman #ifndef MAKEFILE_PREFERENCE_LIST
9322e2caf59SThomas Veerman # define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
9332e2caf59SThomas Veerman #endif
9342e2caf59SThomas Veerman 	Var_Set(MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST,
9352e2caf59SThomas Veerman 		VAR_GLOBAL, 0);
9362e2caf59SThomas Veerman 	Var_Set(MAKE_DEPENDFILE, ".depend", VAR_GLOBAL, 0);
9372e2caf59SThomas Veerman 
9382e2caf59SThomas Veerman 	create = Lst_Init(FALSE);
9392e2caf59SThomas Veerman 	makefiles = Lst_Init(FALSE);
9402e2caf59SThomas Veerman 	printVars = FALSE;
9412bc7c627SLionel Sambuc 	debugVflag = FALSE;
9422e2caf59SThomas Veerman 	variables = Lst_Init(FALSE);
9432e2caf59SThomas Veerman 	beSilent = FALSE;		/* Print commands as executed */
9442e2caf59SThomas Veerman 	ignoreErrors = FALSE;		/* Pay attention to non-zero returns */
9452e2caf59SThomas Veerman 	noExecute = FALSE;		/* Execute all commands */
9462e2caf59SThomas Veerman 	noRecursiveExecute = FALSE;	/* Execute all .MAKE targets */
9472e2caf59SThomas Veerman 	keepgoing = FALSE;		/* Stop on error */
9482e2caf59SThomas Veerman 	allPrecious = FALSE;		/* Remove targets when interrupted */
9492e2caf59SThomas Veerman 	queryFlag = FALSE;		/* This is not just a check-run */
9502e2caf59SThomas Veerman 	noBuiltins = FALSE;		/* Read the built-in rules */
9512e2caf59SThomas Veerman 	touchFlag = FALSE;		/* Actually update targets */
9522e2caf59SThomas Veerman 	debug = 0;			/* No debug verbosity, please. */
9532e2caf59SThomas Veerman 	jobsRunning = FALSE;
9542e2caf59SThomas Veerman 
9552e2caf59SThomas Veerman 	maxJobs = DEFMAXLOCAL;		/* Set default local max concurrency */
9562e2caf59SThomas Veerman 	maxJobTokens = maxJobs;
9572e2caf59SThomas Veerman 	compatMake = FALSE;		/* No compat mode */
9582e2caf59SThomas Veerman 	ignorePWD = FALSE;
9592e2caf59SThomas Veerman 
9602e2caf59SThomas Veerman 	/*
9612e2caf59SThomas Veerman 	 * Initialize the parsing, directory and variable modules to prepare
9622e2caf59SThomas Veerman 	 * for the reading of inclusion paths and variable settings on the
9632e2caf59SThomas Veerman 	 * command line
9642e2caf59SThomas Veerman 	 */
9652e2caf59SThomas Veerman 
9662e2caf59SThomas Veerman 	/*
9672e2caf59SThomas Veerman 	 * Initialize various variables.
9682e2caf59SThomas Veerman 	 *	MAKE also gets this name, for compatibility
9692e2caf59SThomas Veerman 	 *	.MAKEFLAGS gets set to the empty string just in case.
9702e2caf59SThomas Veerman 	 *	MFLAGS also gets initialized empty, for compatibility.
9712e2caf59SThomas Veerman 	 */
9722e2caf59SThomas Veerman 	Parse_Init();
9732e2caf59SThomas Veerman 	if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) {
9742e2caf59SThomas Veerman 	    /*
9752e2caf59SThomas Veerman 	     * Leave alone if it is an absolute path, or if it does
9762e2caf59SThomas Veerman 	     * not contain a '/' in which case we need to find it in
9772e2caf59SThomas Veerman 	     * the path, like execvp(3) and the shells do.
9782e2caf59SThomas Veerman 	     */
9792e2caf59SThomas Veerman 	    p1 = argv[0];
9802e2caf59SThomas Veerman 	} else {
9812e2caf59SThomas Veerman 	    /*
9822e2caf59SThomas Veerman 	     * A relative path, canonicalize it.
9832e2caf59SThomas Veerman 	     */
9842e2caf59SThomas Veerman 	    p1 = realpath(argv[0], mdpath);
9852e2caf59SThomas Veerman 	    if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) {
9862e2caf59SThomas Veerman 		p1 = argv[0];		/* realpath failed */
9872e2caf59SThomas Veerman 	    }
9882e2caf59SThomas Veerman 	}
9892e2caf59SThomas Veerman 	Var_Set("MAKE", p1, VAR_GLOBAL, 0);
9902e2caf59SThomas Veerman 	Var_Set(".MAKE", p1, VAR_GLOBAL, 0);
9912e2caf59SThomas Veerman 	Var_Set(MAKEFLAGS, "", VAR_GLOBAL, 0);
9922e2caf59SThomas Veerman 	Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL, 0);
9932e2caf59SThomas Veerman 	Var_Set("MFLAGS", "", VAR_GLOBAL, 0);
9942e2caf59SThomas Veerman 	Var_Set(".ALLTARGETS", "", VAR_GLOBAL, 0);
99584d9c625SLionel Sambuc 	/* some makefiles need to know this */
99684d9c625SLionel Sambuc 	Var_Set(MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV, VAR_CMD, 0);
9972e2caf59SThomas Veerman 
9982e2caf59SThomas Veerman 	/*
9992e2caf59SThomas Veerman 	 * Set some other useful macros
10002e2caf59SThomas Veerman 	 */
10012e2caf59SThomas Veerman 	{
100284d9c625SLionel Sambuc 	    char tmp[64], *ep;
10032e2caf59SThomas Veerman 
100484d9c625SLionel Sambuc 	    makelevel = ((ep = getenv(MAKE_LEVEL_ENV)) && *ep) ? atoi(ep) : 0;
100584d9c625SLionel Sambuc 	    if (makelevel < 0)
100684d9c625SLionel Sambuc 		makelevel = 0;
100784d9c625SLionel Sambuc 	    snprintf(tmp, sizeof(tmp), "%d", makelevel);
100884d9c625SLionel Sambuc 	    Var_Set(MAKE_LEVEL, tmp, VAR_GLOBAL, 0);
10092e2caf59SThomas Veerman 	    snprintf(tmp, sizeof(tmp), "%u", myPid);
10102e2caf59SThomas Veerman 	    Var_Set(".MAKE.PID", tmp, VAR_GLOBAL, 0);
10112e2caf59SThomas Veerman 	    snprintf(tmp, sizeof(tmp), "%u", getppid());
10122e2caf59SThomas Veerman 	    Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL, 0);
10132e2caf59SThomas Veerman 	}
101484d9c625SLionel Sambuc 	if (makelevel > 0) {
101584d9c625SLionel Sambuc 		char pn[1024];
101684d9c625SLionel Sambuc 		snprintf(pn, sizeof(pn), "%s[%d]", progname, makelevel);
101784d9c625SLionel Sambuc 		progname = bmake_strdup(pn);
101884d9c625SLionel Sambuc 	}
10192e2caf59SThomas Veerman 
102084d9c625SLionel Sambuc #ifdef USE_META
102184d9c625SLionel Sambuc 	meta_init();
102284d9c625SLionel Sambuc #endif
10232e2caf59SThomas Veerman 	/*
10242e2caf59SThomas Veerman 	 * First snag any flags out of the MAKE environment variable.
10252e2caf59SThomas Veerman 	 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
10262e2caf59SThomas Veerman 	 * in a different format).
10272e2caf59SThomas Veerman 	 */
10282e2caf59SThomas Veerman #ifdef POSIX
102984d9c625SLionel Sambuc 	p1 = explode(getenv("MAKEFLAGS"));
103084d9c625SLionel Sambuc 	Main_ParseArgLine(p1);
103184d9c625SLionel Sambuc 	free(p1);
10322e2caf59SThomas Veerman #else
10332e2caf59SThomas Veerman 	Main_ParseArgLine(getenv("MAKE"));
10342e2caf59SThomas Veerman #endif
10352e2caf59SThomas Veerman 
10362e2caf59SThomas Veerman 	/*
10372e2caf59SThomas Veerman 	 * Find where we are (now).
10382e2caf59SThomas Veerman 	 * We take care of PWD for the automounter below...
10392e2caf59SThomas Veerman 	 */
10402e2caf59SThomas Veerman 	if (getcwd(curdir, MAXPATHLEN) == NULL) {
10412e2caf59SThomas Veerman 		(void)fprintf(stderr, "%s: getcwd: %s.\n",
10422e2caf59SThomas Veerman 		    progname, strerror(errno));
10432e2caf59SThomas Veerman 		exit(2);
10442e2caf59SThomas Veerman 	}
10452e2caf59SThomas Veerman 
10462e2caf59SThomas Veerman 	MainParseArgs(argc, argv);
10472e2caf59SThomas Veerman 
104884d9c625SLionel Sambuc 	if (enterFlag)
104984d9c625SLionel Sambuc 		printf("%s: Entering directory `%s'\n", progname, curdir);
105084d9c625SLionel Sambuc 
10512e2caf59SThomas Veerman 	/*
10522e2caf59SThomas Veerman 	 * Verify that cwd is sane.
10532e2caf59SThomas Veerman 	 */
10542e2caf59SThomas Veerman 	if (stat(curdir, &sa) == -1) {
10552e2caf59SThomas Veerman 	    (void)fprintf(stderr, "%s: %s: %s.\n",
10562e2caf59SThomas Veerman 		 progname, curdir, strerror(errno));
10572e2caf59SThomas Veerman 	    exit(2);
10582e2caf59SThomas Veerman 	}
10592e2caf59SThomas Veerman 
10602e2caf59SThomas Veerman 	/*
10612e2caf59SThomas Veerman 	 * All this code is so that we know where we are when we start up
10622e2caf59SThomas Veerman 	 * on a different machine with pmake.
10632e2caf59SThomas Veerman 	 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
10642e2caf59SThomas Veerman 	 * since the value of curdir can vary depending on how we got
10652e2caf59SThomas Veerman 	 * here.  Ie sitting at a shell prompt (shell that provides $PWD)
10662e2caf59SThomas Veerman 	 * or via subdir.mk in which case its likely a shell which does
10672e2caf59SThomas Veerman 	 * not provide it.
10682e2caf59SThomas Veerman 	 * So, to stop it breaking this case only, we ignore PWD if
10692e2caf59SThomas Veerman 	 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform.
10702e2caf59SThomas Veerman 	 */
107184d9c625SLionel Sambuc #ifndef NO_PWD_OVERRIDE
107284d9c625SLionel Sambuc 	if (!ignorePWD) {
10730a6a1f1dSLionel Sambuc 		char *pwd, *ptmp1 = NULL, *ptmp2 = NULL;
107484d9c625SLionel Sambuc 
107584d9c625SLionel Sambuc 		if ((pwd = getenv("PWD")) != NULL &&
10760a6a1f1dSLionel Sambuc 		    Var_Value("MAKEOBJDIRPREFIX", VAR_CMD, &ptmp1) == NULL) {
10770a6a1f1dSLionel Sambuc 			const char *makeobjdir = Var_Value("MAKEOBJDIR",
10780a6a1f1dSLionel Sambuc 			    VAR_CMD, &ptmp2);
10792e2caf59SThomas Veerman 
10802e2caf59SThomas Veerman 			if (makeobjdir == NULL || !strchr(makeobjdir, '$')) {
108184d9c625SLionel Sambuc 				if (stat(pwd, &sb) == 0 &&
108284d9c625SLionel Sambuc 				    sa.st_ino == sb.st_ino &&
10832e2caf59SThomas Veerman 				    sa.st_dev == sb.st_dev)
10842e2caf59SThomas Veerman 					(void)strncpy(curdir, pwd, MAXPATHLEN);
10852e2caf59SThomas Veerman 			}
10862e2caf59SThomas Veerman 		}
10870a6a1f1dSLionel Sambuc 		free(ptmp1);
10880a6a1f1dSLionel Sambuc 		free(ptmp2);
108984d9c625SLionel Sambuc 	}
109084d9c625SLionel Sambuc #endif
10912e2caf59SThomas Veerman 	Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0);
10922e2caf59SThomas Veerman 
10932e2caf59SThomas Veerman 	/*
10942e2caf59SThomas Veerman 	 * Find the .OBJDIR.  If MAKEOBJDIRPREFIX, or failing that,
10952e2caf59SThomas Veerman 	 * MAKEOBJDIR is set in the environment, try only that value
10962e2caf59SThomas Veerman 	 * and fall back to .CURDIR if it does not exist.
10972e2caf59SThomas Veerman 	 *
10982e2caf59SThomas Veerman 	 * Otherwise, try _PATH_OBJDIR.MACHINE, _PATH_OBJDIR, and
10992e2caf59SThomas Veerman 	 * finally _PATH_OBJDIRPREFIX`pwd`, in that order.  If none
11002e2caf59SThomas Veerman 	 * of these paths exist, just use .CURDIR.
11012e2caf59SThomas Veerman 	 */
11022e2caf59SThomas Veerman 	Dir_Init(curdir);
11032e2caf59SThomas Veerman 	(void)Main_SetObjdir(curdir);
11042e2caf59SThomas Veerman 
11050a6a1f1dSLionel Sambuc 	if ((path = Var_Value("MAKEOBJDIRPREFIX", VAR_CMD, &p1)) != NULL) {
11062e2caf59SThomas Veerman 		(void)snprintf(mdpath, MAXPATHLEN, "%s%s", path, curdir);
11072e2caf59SThomas Veerman 		(void)Main_SetObjdir(mdpath);
11080a6a1f1dSLionel Sambuc 		free(p1);
11090a6a1f1dSLionel Sambuc 	} else if ((path = Var_Value("MAKEOBJDIR", VAR_CMD, &p1)) != NULL) {
11102e2caf59SThomas Veerman 		(void)Main_SetObjdir(path);
11110a6a1f1dSLionel Sambuc 		free(p1);
11122e2caf59SThomas Veerman 	} else {
11132e2caf59SThomas Veerman 		(void)snprintf(mdpath, MAXPATHLEN, "%s.%s", _PATH_OBJDIR, machine);
11142e2caf59SThomas Veerman 		if (!Main_SetObjdir(mdpath) && !Main_SetObjdir(_PATH_OBJDIR)) {
11152e2caf59SThomas Veerman 			(void)snprintf(mdpath, MAXPATHLEN, "%s%s",
11162e2caf59SThomas Veerman 					_PATH_OBJDIRPREFIX, curdir);
11172e2caf59SThomas Veerman 			(void)Main_SetObjdir(mdpath);
11182e2caf59SThomas Veerman 		}
11192e2caf59SThomas Veerman 	}
11202e2caf59SThomas Veerman 
11212e2caf59SThomas Veerman 	/*
11222e2caf59SThomas Veerman 	 * Be compatible if user did not specify -j and did not explicitly
11232e2caf59SThomas Veerman 	 * turned compatibility on
11242e2caf59SThomas Veerman 	 */
11252e2caf59SThomas Veerman 	if (!compatMake && !forceJobs) {
11262e2caf59SThomas Veerman 		compatMake = TRUE;
11272e2caf59SThomas Veerman 	}
11282e2caf59SThomas Veerman 
11292e2caf59SThomas Veerman 	/*
11302e2caf59SThomas Veerman 	 * Initialize archive, target and suffix modules in preparation for
11312e2caf59SThomas Veerman 	 * parsing the makefile(s)
11322e2caf59SThomas Veerman 	 */
11332e2caf59SThomas Veerman 	Arch_Init();
11342e2caf59SThomas Veerman 	Targ_Init();
11352e2caf59SThomas Veerman 	Suff_Init();
11362e2caf59SThomas Veerman 	Trace_Init(tracefile);
11372e2caf59SThomas Veerman 
11382e2caf59SThomas Veerman 	DEFAULT = NULL;
11392e2caf59SThomas Veerman 	(void)time(&now);
11402e2caf59SThomas Veerman 
11412e2caf59SThomas Veerman 	Trace_Log(MAKESTART, NULL);
11422e2caf59SThomas Veerman 
11432e2caf59SThomas Veerman 	/*
11442e2caf59SThomas Veerman 	 * Set up the .TARGETS variable to contain the list of targets to be
11452e2caf59SThomas Veerman 	 * created. If none specified, make the variable empty -- the parser
11462e2caf59SThomas Veerman 	 * will fill the thing in with the default or .MAIN target.
11472e2caf59SThomas Veerman 	 */
11482e2caf59SThomas Veerman 	if (!Lst_IsEmpty(create)) {
11492e2caf59SThomas Veerman 		LstNode ln;
11502e2caf59SThomas Veerman 
11512e2caf59SThomas Veerman 		for (ln = Lst_First(create); ln != NULL;
11522e2caf59SThomas Veerman 		    ln = Lst_Succ(ln)) {
11532e2caf59SThomas Veerman 			char *name = (char *)Lst_Datum(ln);
11542e2caf59SThomas Veerman 
11552e2caf59SThomas Veerman 			Var_Append(".TARGETS", name, VAR_GLOBAL);
11562e2caf59SThomas Veerman 		}
11572e2caf59SThomas Veerman 	} else
11582e2caf59SThomas Veerman 		Var_Set(".TARGETS", "", VAR_GLOBAL, 0);
11592e2caf59SThomas Veerman 
11602e2caf59SThomas Veerman 
11612e2caf59SThomas Veerman 	/*
11622e2caf59SThomas Veerman 	 * If no user-supplied system path was given (through the -m option)
11632e2caf59SThomas Veerman 	 * add the directories from the DEFSYSPATH (more than one may be given
11642e2caf59SThomas Veerman 	 * as dir1:...:dirn) to the system include path.
11652e2caf59SThomas Veerman 	 */
11662e2caf59SThomas Veerman 	if (syspath == NULL || *syspath == '\0')
11672e2caf59SThomas Veerman 		syspath = defsyspath;
11682e2caf59SThomas Veerman 	else
11692e2caf59SThomas Veerman 		syspath = bmake_strdup(syspath);
11702e2caf59SThomas Veerman 
11712e2caf59SThomas Veerman 	for (start = syspath; *start != '\0'; start = cp) {
11722e2caf59SThomas Veerman 		for (cp = start; *cp != '\0' && *cp != ':'; cp++)
11732e2caf59SThomas Veerman 			continue;
11742e2caf59SThomas Veerman 		if (*cp == ':') {
11752e2caf59SThomas Veerman 			*cp++ = '\0';
11762e2caf59SThomas Veerman 		}
11772e2caf59SThomas Veerman 		/* look for magic parent directory search string */
11782e2caf59SThomas Veerman 		if (strncmp(".../", start, 4) != 0) {
11792e2caf59SThomas Veerman 			(void)Dir_AddDir(defIncPath, start);
11802e2caf59SThomas Veerman 		} else {
11812e2caf59SThomas Veerman 			if (Dir_FindHereOrAbove(curdir, start+4,
11822e2caf59SThomas Veerman 			    found_path, sizeof(found_path))) {
11832e2caf59SThomas Veerman 				(void)Dir_AddDir(defIncPath, found_path);
11842e2caf59SThomas Veerman 			}
11852e2caf59SThomas Veerman 		}
11862e2caf59SThomas Veerman 	}
11872e2caf59SThomas Veerman 	if (syspath != defsyspath)
11882e2caf59SThomas Veerman 		free(syspath);
11892e2caf59SThomas Veerman 
11902e2caf59SThomas Veerman 	/*
11912e2caf59SThomas Veerman 	 * Read in the built-in rules first, followed by the specified
11922e2caf59SThomas Veerman 	 * makefile, if it was (makefile != NULL), or the default
11932e2caf59SThomas Veerman 	 * makefile and Makefile, in that order, if it wasn't.
11942e2caf59SThomas Veerman 	 */
11952e2caf59SThomas Veerman 	if (!noBuiltins) {
11962e2caf59SThomas Veerman 		LstNode ln;
11972e2caf59SThomas Veerman 
11982e2caf59SThomas Veerman 		sysMkPath = Lst_Init(FALSE);
11992e2caf59SThomas Veerman 		Dir_Expand(_PATH_DEFSYSMK,
12002e2caf59SThomas Veerman 			   Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath,
12012e2caf59SThomas Veerman 			   sysMkPath);
12022e2caf59SThomas Veerman 		if (Lst_IsEmpty(sysMkPath))
12032e2caf59SThomas Veerman 			Fatal("%s: no system rules (%s).", progname,
12042e2caf59SThomas Veerman 			    _PATH_DEFSYSMK);
12052e2caf59SThomas Veerman 		ln = Lst_Find(sysMkPath, NULL, ReadMakefile);
12062e2caf59SThomas Veerman 		if (ln == NULL)
12072e2caf59SThomas Veerman 			Fatal("%s: cannot open %s.", progname,
12082e2caf59SThomas Veerman 			    (char *)Lst_Datum(ln));
12092e2caf59SThomas Veerman 	}
12102e2caf59SThomas Veerman 
12112e2caf59SThomas Veerman 	if (!Lst_IsEmpty(makefiles)) {
12122e2caf59SThomas Veerman 		LstNode ln;
12132e2caf59SThomas Veerman 
12142e2caf59SThomas Veerman 		ln = Lst_Find(makefiles, NULL, ReadAllMakefiles);
12152e2caf59SThomas Veerman 		if (ln != NULL)
12162e2caf59SThomas Veerman 			Fatal("%s: cannot open %s.", progname,
12172e2caf59SThomas Veerman 			    (char *)Lst_Datum(ln));
12182e2caf59SThomas Veerman 	} else {
12192e2caf59SThomas Veerman 	    p1 = Var_Subst(NULL, "${" MAKEFILE_PREFERENCE "}",
12202e2caf59SThomas Veerman 		VAR_CMD, 0);
12212e2caf59SThomas Veerman 	    if (p1) {
12222e2caf59SThomas Veerman 		(void)str2Lst_Append(makefiles, p1, NULL);
12232e2caf59SThomas Veerman 		(void)Lst_Find(makefiles, NULL, ReadMakefile);
12242e2caf59SThomas Veerman 		free(p1);
12252e2caf59SThomas Veerman 	    }
12262e2caf59SThomas Veerman 	}
12272e2caf59SThomas Veerman 
12282e2caf59SThomas Veerman 	/* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
12292e2caf59SThomas Veerman 	if (!noBuiltins || !printVars) {
12302e2caf59SThomas Veerman 	    makeDependfile = Var_Subst(NULL, "${.MAKE.DEPENDFILE:T}",
12312e2caf59SThomas Veerman 		VAR_CMD, 0);
12322e2caf59SThomas Veerman 	    doing_depend = TRUE;
12332e2caf59SThomas Veerman 	    (void)ReadMakefile(makeDependfile, NULL);
12342e2caf59SThomas Veerman 	    doing_depend = FALSE;
12352e2caf59SThomas Veerman 	}
12362e2caf59SThomas Veerman 
12370a6a1f1dSLionel Sambuc 	if (enterFlagObj)
12380a6a1f1dSLionel Sambuc 		printf("%s: Entering directory `%s'\n", progname, objdir);
12390a6a1f1dSLionel Sambuc 
12402e2caf59SThomas Veerman 	MakeMode(NULL);
12412e2caf59SThomas Veerman 
12422e2caf59SThomas Veerman 	Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
12432e2caf59SThomas Veerman 	if (p1)
12442e2caf59SThomas Veerman 	    free(p1);
12452e2caf59SThomas Veerman 
12462e2caf59SThomas Veerman 	if (!compatMake)
12472e2caf59SThomas Veerman 	    Job_ServerStart(maxJobTokens, jp_0, jp_1);
12482e2caf59SThomas Veerman 	if (DEBUG(JOB))
12492e2caf59SThomas Veerman 	    fprintf(debug_file, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
12502e2caf59SThomas Veerman 		jp_0, jp_1, maxJobs, maxJobTokens, compatMake);
12512e2caf59SThomas Veerman 
12522e2caf59SThomas Veerman 	Main_ExportMAKEFLAGS(TRUE);	/* initial export */
12532e2caf59SThomas Veerman 
12542e2caf59SThomas Veerman 
12552e2caf59SThomas Veerman 	/*
12562e2caf59SThomas Veerman 	 * For compatibility, look at the directories in the VPATH variable
12572e2caf59SThomas Veerman 	 * and add them to the search path, if the variable is defined. The
12582e2caf59SThomas Veerman 	 * variable's value is in the same format as the PATH envariable, i.e.
12592e2caf59SThomas Veerman 	 * <directory>:<directory>:<directory>...
12602e2caf59SThomas Veerman 	 */
12612e2caf59SThomas Veerman 	if (Var_Exists("VPATH", VAR_CMD)) {
12622e2caf59SThomas Veerman 		char *vpath, savec;
12632e2caf59SThomas Veerman 		/*
12642e2caf59SThomas Veerman 		 * GCC stores string constants in read-only memory, but
12652e2caf59SThomas Veerman 		 * Var_Subst will want to write this thing, so store it
12662e2caf59SThomas Veerman 		 * in an array
12672e2caf59SThomas Veerman 		 */
12682e2caf59SThomas Veerman 		static char VPATH[] = "${VPATH}";
12692e2caf59SThomas Veerman 
12702e2caf59SThomas Veerman 		vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
12712e2caf59SThomas Veerman 		path = vpath;
12722e2caf59SThomas Veerman 		do {
12732e2caf59SThomas Veerman 			/* skip to end of directory */
12742e2caf59SThomas Veerman 			for (cp = path; *cp != ':' && *cp != '\0'; cp++)
12752e2caf59SThomas Veerman 				continue;
12762e2caf59SThomas Veerman 			/* Save terminator character so know when to stop */
12772e2caf59SThomas Veerman 			savec = *cp;
12782e2caf59SThomas Veerman 			*cp = '\0';
12792e2caf59SThomas Veerman 			/* Add directory to search path */
12802e2caf59SThomas Veerman 			(void)Dir_AddDir(dirSearchPath, path);
12812e2caf59SThomas Veerman 			*cp = savec;
12822e2caf59SThomas Veerman 			path = cp + 1;
12832e2caf59SThomas Veerman 		} while (savec == ':');
12842e2caf59SThomas Veerman 		free(vpath);
12852e2caf59SThomas Veerman 	}
12862e2caf59SThomas Veerman 
12872e2caf59SThomas Veerman 	/*
12882e2caf59SThomas Veerman 	 * Now that all search paths have been read for suffixes et al, it's
12892e2caf59SThomas Veerman 	 * time to add the default search path to their lists...
12902e2caf59SThomas Veerman 	 */
12912e2caf59SThomas Veerman 	Suff_DoPaths();
12922e2caf59SThomas Veerman 
12932e2caf59SThomas Veerman 	/*
12942e2caf59SThomas Veerman 	 * Propagate attributes through :: dependency lists.
12952e2caf59SThomas Veerman 	 */
12962e2caf59SThomas Veerman 	Targ_Propagate();
12972e2caf59SThomas Veerman 
12982e2caf59SThomas Veerman 	/* print the initial graph, if the user requested it */
12992e2caf59SThomas Veerman 	if (DEBUG(GRAPH1))
13002e2caf59SThomas Veerman 		Targ_PrintGraph(1);
13012e2caf59SThomas Veerman 
13022e2caf59SThomas Veerman 	/* print the values of any variables requested by the user */
13032e2caf59SThomas Veerman 	if (printVars) {
13042e2caf59SThomas Veerman 		LstNode ln;
13052bc7c627SLionel Sambuc 		Boolean expandVars;
13062e2caf59SThomas Veerman 
13072bc7c627SLionel Sambuc 		if (debugVflag)
13082bc7c627SLionel Sambuc 			expandVars = FALSE;
13092bc7c627SLionel Sambuc 		else
13102bc7c627SLionel Sambuc 			expandVars = getBoolean(".MAKE.EXPAND_VARIABLES", FALSE);
13112e2caf59SThomas Veerman 		for (ln = Lst_First(variables); ln != NULL;
13122e2caf59SThomas Veerman 		    ln = Lst_Succ(ln)) {
13132e2caf59SThomas Veerman 			char *var = (char *)Lst_Datum(ln);
13142e2caf59SThomas Veerman 			char *value;
13152e2caf59SThomas Veerman 
13162e2caf59SThomas Veerman 			if (strchr(var, '$')) {
13172e2caf59SThomas Veerman 				value = p1 = Var_Subst(NULL, var, VAR_GLOBAL, 0);
13182bc7c627SLionel Sambuc 			} else if (expandVars) {
13192bc7c627SLionel Sambuc 				char tmp[128];
13202bc7c627SLionel Sambuc 
13212bc7c627SLionel Sambuc 				if (snprintf(tmp, sizeof(tmp), "${%s}", var) >= (int)(sizeof(tmp)))
13222bc7c627SLionel Sambuc 					Fatal("%s: variable name too big: %s",
13232bc7c627SLionel Sambuc 					      progname, var);
13242bc7c627SLionel Sambuc 				value = p1 = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
13252e2caf59SThomas Veerman 			} else {
13262e2caf59SThomas Veerman 				value = Var_Value(var, VAR_GLOBAL, &p1);
13272e2caf59SThomas Veerman 			}
13282e2caf59SThomas Veerman 			printf("%s\n", value ? value : "");
13292e2caf59SThomas Veerman 			if (p1)
13302e2caf59SThomas Veerman 				free(p1);
13312e2caf59SThomas Veerman 		}
13322e2caf59SThomas Veerman 	} else {
13332e2caf59SThomas Veerman 		/*
13342e2caf59SThomas Veerman 		 * Have now read the entire graph and need to make a list of
13352e2caf59SThomas Veerman 		 * targets to create. If none was given on the command line,
13362e2caf59SThomas Veerman 		 * we consult the parsing module to find the main target(s)
13372e2caf59SThomas Veerman 		 * to create.
13382e2caf59SThomas Veerman 		 */
13392e2caf59SThomas Veerman 		if (Lst_IsEmpty(create))
13402e2caf59SThomas Veerman 			targs = Parse_MainName();
13412e2caf59SThomas Veerman 		else
13422e2caf59SThomas Veerman 			targs = Targ_FindList(create, TARG_CREATE);
13432e2caf59SThomas Veerman 
13442e2caf59SThomas Veerman 		if (!compatMake) {
13452e2caf59SThomas Veerman 			/*
13462e2caf59SThomas Veerman 			 * Initialize job module before traversing the graph
13472e2caf59SThomas Veerman 			 * now that any .BEGIN and .END targets have been read.
13482e2caf59SThomas Veerman 			 * This is done only if the -q flag wasn't given
13492e2caf59SThomas Veerman 			 * (to prevent the .BEGIN from being executed should
13502e2caf59SThomas Veerman 			 * it exist).
13512e2caf59SThomas Veerman 			 */
13522e2caf59SThomas Veerman 			if (!queryFlag) {
13532e2caf59SThomas Veerman 				Job_Init();
13542e2caf59SThomas Veerman 				jobsRunning = TRUE;
13552e2caf59SThomas Veerman 			}
13562e2caf59SThomas Veerman 
13572e2caf59SThomas Veerman 			/* Traverse the graph, checking on all the targets */
13582e2caf59SThomas Veerman 			outOfDate = Make_Run(targs);
13592e2caf59SThomas Veerman 		} else {
13602e2caf59SThomas Veerman 			/*
13612e2caf59SThomas Veerman 			 * Compat_Init will take care of creating all the
13622e2caf59SThomas Veerman 			 * targets as well as initializing the module.
13632e2caf59SThomas Veerman 			 */
13642e2caf59SThomas Veerman 			Compat_Run(targs);
13652e2caf59SThomas Veerman 		}
13662e2caf59SThomas Veerman 	}
13672e2caf59SThomas Veerman 
13682e2caf59SThomas Veerman #ifdef CLEANUP
13692e2caf59SThomas Veerman 	Lst_Destroy(targs, NULL);
13702e2caf59SThomas Veerman 	Lst_Destroy(variables, NULL);
13712e2caf59SThomas Veerman 	Lst_Destroy(makefiles, NULL);
13722e2caf59SThomas Veerman 	Lst_Destroy(create, (FreeProc *)free);
13732e2caf59SThomas Veerman #endif
13742e2caf59SThomas Veerman 
13752e2caf59SThomas Veerman 	/* print the graph now it's been processed if the user requested it */
13762e2caf59SThomas Veerman 	if (DEBUG(GRAPH2))
13772e2caf59SThomas Veerman 		Targ_PrintGraph(2);
13782e2caf59SThomas Veerman 
13792e2caf59SThomas Veerman 	Trace_Log(MAKEEND, 0);
13802e2caf59SThomas Veerman 
13810a6a1f1dSLionel Sambuc 	if (enterFlagObj)
13820a6a1f1dSLionel Sambuc 		printf("%s: Leaving directory `%s'\n", progname, objdir);
138384d9c625SLionel Sambuc 	if (enterFlag)
138484d9c625SLionel Sambuc 		printf("%s: Leaving directory `%s'\n", progname, curdir);
138584d9c625SLionel Sambuc 
13862e2caf59SThomas Veerman 	Suff_End();
13872e2caf59SThomas Veerman         Targ_End();
13882e2caf59SThomas Veerman 	Arch_End();
13892e2caf59SThomas Veerman 	Var_End();
13902e2caf59SThomas Veerman 	Parse_End();
13912e2caf59SThomas Veerman 	Dir_End();
13922e2caf59SThomas Veerman 	Job_End();
13932e2caf59SThomas Veerman 	Trace_End();
13942e2caf59SThomas Veerman 
13952e2caf59SThomas Veerman 	return outOfDate ? 1 : 0;
13962e2caf59SThomas Veerman }
13972e2caf59SThomas Veerman 
13982e2caf59SThomas Veerman /*-
13992e2caf59SThomas Veerman  * ReadMakefile  --
14002e2caf59SThomas Veerman  *	Open and parse the given makefile.
14012e2caf59SThomas Veerman  *
14022e2caf59SThomas Veerman  * Results:
14032e2caf59SThomas Veerman  *	0 if ok. -1 if couldn't open file.
14042e2caf59SThomas Veerman  *
14052e2caf59SThomas Veerman  * Side Effects:
14062e2caf59SThomas Veerman  *	lots
14072e2caf59SThomas Veerman  */
14082e2caf59SThomas Veerman static int
ReadMakefile(const void * p,const void * q MAKE_ATTR_UNUSED)14092bc7c627SLionel Sambuc ReadMakefile(const void *p, const void *q MAKE_ATTR_UNUSED)
14102e2caf59SThomas Veerman {
14112e2caf59SThomas Veerman 	const char *fname = p;		/* makefile to read */
14122e2caf59SThomas Veerman 	int fd;
14132e2caf59SThomas Veerman 	size_t len = MAXPATHLEN;
14142e2caf59SThomas Veerman 	char *name, *path = bmake_malloc(len);
14152e2caf59SThomas Veerman 
14162e2caf59SThomas Veerman 	if (!strcmp(fname, "-")) {
14172e2caf59SThomas Veerman 		Parse_File(NULL /*stdin*/, -1);
141884d9c625SLionel Sambuc 		Var_Set("MAKEFILE", "", VAR_INTERNAL, 0);
14192e2caf59SThomas Veerman 	} else {
14202e2caf59SThomas Veerman 		/* if we've chdir'd, rebuild the path name */
14212e2caf59SThomas Veerman 		if (strcmp(curdir, objdir) && *fname != '/') {
14222e2caf59SThomas Veerman 			size_t plen = strlen(curdir) + strlen(fname) + 2;
14232e2caf59SThomas Veerman 			if (len < plen)
14242e2caf59SThomas Veerman 				path = bmake_realloc(path, len = 2 * plen);
14252e2caf59SThomas Veerman 
14262e2caf59SThomas Veerman 			(void)snprintf(path, len, "%s/%s", curdir, fname);
14272e2caf59SThomas Veerman 			fd = open(path, O_RDONLY);
14282e2caf59SThomas Veerman 			if (fd != -1) {
14292e2caf59SThomas Veerman 				fname = path;
14302e2caf59SThomas Veerman 				goto found;
14312e2caf59SThomas Veerman 			}
14322e2caf59SThomas Veerman 
14332e2caf59SThomas Veerman 			/* If curdir failed, try objdir (ala .depend) */
14342e2caf59SThomas Veerman 			plen = strlen(objdir) + strlen(fname) + 2;
14352e2caf59SThomas Veerman 			if (len < plen)
14362e2caf59SThomas Veerman 				path = bmake_realloc(path, len = 2 * plen);
14372e2caf59SThomas Veerman 			(void)snprintf(path, len, "%s/%s", objdir, fname);
14382e2caf59SThomas Veerman 			fd = open(path, O_RDONLY);
14392e2caf59SThomas Veerman 			if (fd != -1) {
14402e2caf59SThomas Veerman 				fname = path;
14412e2caf59SThomas Veerman 				goto found;
14422e2caf59SThomas Veerman 			}
14432e2caf59SThomas Veerman 		} else {
14442e2caf59SThomas Veerman 			fd = open(fname, O_RDONLY);
14452e2caf59SThomas Veerman 			if (fd != -1)
14462e2caf59SThomas Veerman 				goto found;
14472e2caf59SThomas Veerman 		}
14482e2caf59SThomas Veerman 		/* look in -I and system include directories. */
14492e2caf59SThomas Veerman 		name = Dir_FindFile(fname, parseIncPath);
14502e2caf59SThomas Veerman 		if (!name)
14512e2caf59SThomas Veerman 			name = Dir_FindFile(fname,
14522e2caf59SThomas Veerman 				Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
14532e2caf59SThomas Veerman 		if (!name || (fd = open(name, O_RDONLY)) == -1) {
14542e2caf59SThomas Veerman 			if (name)
14552e2caf59SThomas Veerman 				free(name);
14562e2caf59SThomas Veerman 			free(path);
14572e2caf59SThomas Veerman 			return(-1);
14582e2caf59SThomas Veerman 		}
14592e2caf59SThomas Veerman 		fname = name;
14602e2caf59SThomas Veerman 		/*
14612e2caf59SThomas Veerman 		 * set the MAKEFILE variable desired by System V fans -- the
14622e2caf59SThomas Veerman 		 * placement of the setting here means it gets set to the last
14632e2caf59SThomas Veerman 		 * makefile specified, as it is set by SysV make.
14642e2caf59SThomas Veerman 		 */
14652e2caf59SThomas Veerman found:
14662e2caf59SThomas Veerman 		if (!doing_depend)
146784d9c625SLionel Sambuc 			Var_Set("MAKEFILE", fname, VAR_INTERNAL, 0);
14682e2caf59SThomas Veerman 		Parse_File(fname, fd);
14692e2caf59SThomas Veerman 	}
14702e2caf59SThomas Veerman 	free(path);
14712e2caf59SThomas Veerman 	return(0);
14722e2caf59SThomas Veerman }
14732e2caf59SThomas Veerman 
14742e2caf59SThomas Veerman 
14752e2caf59SThomas Veerman 
14762e2caf59SThomas Veerman /*-
14772e2caf59SThomas Veerman  * Cmd_Exec --
14782e2caf59SThomas Veerman  *	Execute the command in cmd, and return the output of that command
14792e2caf59SThomas Veerman  *	in a string.
14802e2caf59SThomas Veerman  *
14812e2caf59SThomas Veerman  * Results:
14822e2caf59SThomas Veerman  *	A string containing the output of the command, or the empty string
14832e2caf59SThomas Veerman  *	If errnum is not NULL, it contains the reason for the command failure
14842e2caf59SThomas Veerman  *
14852e2caf59SThomas Veerman  * Side Effects:
14862e2caf59SThomas Veerman  *	The string must be freed by the caller.
14872e2caf59SThomas Veerman  */
14882e2caf59SThomas Veerman char *
Cmd_Exec(const char * cmd,const char ** errnum)14892e2caf59SThomas Veerman Cmd_Exec(const char *cmd, const char **errnum)
14902e2caf59SThomas Veerman {
14912e2caf59SThomas Veerman     const char	*args[4];   	/* Args for invoking the shell */
14922e2caf59SThomas Veerman     int 	fds[2];	    	/* Pipe streams */
14932e2caf59SThomas Veerman     int 	cpid;	    	/* Child PID */
14942e2caf59SThomas Veerman     int 	pid;	    	/* PID from wait() */
14952e2caf59SThomas Veerman     char	*res;		/* result */
14962e2caf59SThomas Veerman     int		status;		/* command exit status */
14972e2caf59SThomas Veerman     Buffer	buf;		/* buffer to store the result */
14982e2caf59SThomas Veerman     char	*cp;
14990a6a1f1dSLionel Sambuc     int		cc;		/* bytes read, or -1 */
15000a6a1f1dSLionel Sambuc     int		savederr;	/* saved errno */
15012e2caf59SThomas Veerman 
15022e2caf59SThomas Veerman 
15032e2caf59SThomas Veerman     *errnum = NULL;
15042e2caf59SThomas Veerman 
15052e2caf59SThomas Veerman     if (!shellName)
15062e2caf59SThomas Veerman 	Shell_Init();
15072e2caf59SThomas Veerman     /*
15082e2caf59SThomas Veerman      * Set up arguments for shell
15092e2caf59SThomas Veerman      */
15102e2caf59SThomas Veerman     args[0] = shellName;
15112e2caf59SThomas Veerman     args[1] = "-c";
15122e2caf59SThomas Veerman     args[2] = cmd;
15132e2caf59SThomas Veerman     args[3] = NULL;
15142e2caf59SThomas Veerman 
15152e2caf59SThomas Veerman     /*
15162e2caf59SThomas Veerman      * Open a pipe for fetching its output
15172e2caf59SThomas Veerman      */
15182e2caf59SThomas Veerman     if (pipe(fds) == -1) {
15192e2caf59SThomas Veerman 	*errnum = "Couldn't create pipe for \"%s\"";
15202e2caf59SThomas Veerman 	goto bad;
15212e2caf59SThomas Veerman     }
15222e2caf59SThomas Veerman 
15232e2caf59SThomas Veerman     /*
15242e2caf59SThomas Veerman      * Fork
15252e2caf59SThomas Veerman      */
15262e2caf59SThomas Veerman     switch (cpid = vFork()) {
15272e2caf59SThomas Veerman     case 0:
15282e2caf59SThomas Veerman 	/*
15292e2caf59SThomas Veerman 	 * Close input side of pipe
15302e2caf59SThomas Veerman 	 */
15312e2caf59SThomas Veerman 	(void)close(fds[0]);
15322e2caf59SThomas Veerman 
15332e2caf59SThomas Veerman 	/*
15342e2caf59SThomas Veerman 	 * Duplicate the output stream to the shell's output, then
15352e2caf59SThomas Veerman 	 * shut the extra thing down. Note we don't fetch the error
15362e2caf59SThomas Veerman 	 * stream...why not? Why?
15372e2caf59SThomas Veerman 	 */
15382e2caf59SThomas Veerman 	(void)dup2(fds[1], 1);
15392e2caf59SThomas Veerman 	(void)close(fds[1]);
15402e2caf59SThomas Veerman 
15412e2caf59SThomas Veerman 	Var_ExportVars();
15422e2caf59SThomas Veerman 
15432e2caf59SThomas Veerman 	(void)execv(shellPath, UNCONST(args));
15442e2caf59SThomas Veerman 	_exit(1);
15452e2caf59SThomas Veerman 	/*NOTREACHED*/
15462e2caf59SThomas Veerman 
15472e2caf59SThomas Veerman     case -1:
15482e2caf59SThomas Veerman 	*errnum = "Couldn't exec \"%s\"";
15492e2caf59SThomas Veerman 	goto bad;
15502e2caf59SThomas Veerman 
15512e2caf59SThomas Veerman     default:
15522e2caf59SThomas Veerman 	/*
15532e2caf59SThomas Veerman 	 * No need for the writing half
15542e2caf59SThomas Veerman 	 */
15552e2caf59SThomas Veerman 	(void)close(fds[1]);
15562e2caf59SThomas Veerman 
15570a6a1f1dSLionel Sambuc 	savederr = 0;
15582e2caf59SThomas Veerman 	Buf_Init(&buf, 0);
15592e2caf59SThomas Veerman 
15602e2caf59SThomas Veerman 	do {
15612e2caf59SThomas Veerman 	    char   result[BUFSIZ];
15622e2caf59SThomas Veerman 	    cc = read(fds[0], result, sizeof(result));
15632e2caf59SThomas Veerman 	    if (cc > 0)
15642e2caf59SThomas Veerman 		Buf_AddBytes(&buf, cc, result);
15652e2caf59SThomas Veerman 	}
15662e2caf59SThomas Veerman 	while (cc > 0 || (cc == -1 && errno == EINTR));
15670a6a1f1dSLionel Sambuc 	if (cc == -1)
15680a6a1f1dSLionel Sambuc 	    savederr = errno;
15692e2caf59SThomas Veerman 
15702e2caf59SThomas Veerman 	/*
15712e2caf59SThomas Veerman 	 * Close the input side of the pipe.
15722e2caf59SThomas Veerman 	 */
15732e2caf59SThomas Veerman 	(void)close(fds[0]);
15742e2caf59SThomas Veerman 
15752e2caf59SThomas Veerman 	/*
15762e2caf59SThomas Veerman 	 * Wait for the process to exit.
15772e2caf59SThomas Veerman 	 */
15782e2caf59SThomas Veerman 	while(((pid = waitpid(cpid, &status, 0)) != cpid) && (pid >= 0)) {
15792e2caf59SThomas Veerman 	    JobReapChild(pid, status, FALSE);
15802e2caf59SThomas Veerman 	    continue;
15812e2caf59SThomas Veerman 	}
15822e2caf59SThomas Veerman 	cc = Buf_Size(&buf);
15832e2caf59SThomas Veerman 	res = Buf_Destroy(&buf, FALSE);
15842e2caf59SThomas Veerman 
15850a6a1f1dSLionel Sambuc 	if (savederr != 0)
15862e2caf59SThomas Veerman 	    *errnum = "Couldn't read shell's output for \"%s\"";
15872e2caf59SThomas Veerman 
15882e2caf59SThomas Veerman 	if (WIFSIGNALED(status))
15892e2caf59SThomas Veerman 	    *errnum = "\"%s\" exited on a signal";
15902e2caf59SThomas Veerman 	else if (WEXITSTATUS(status) != 0)
15912e2caf59SThomas Veerman 	    *errnum = "\"%s\" returned non-zero status";
15922e2caf59SThomas Veerman 
15932e2caf59SThomas Veerman 	/*
15942e2caf59SThomas Veerman 	 * Null-terminate the result, convert newlines to spaces and
15952e2caf59SThomas Veerman 	 * install it in the variable.
15962e2caf59SThomas Veerman 	 */
15972e2caf59SThomas Veerman 	res[cc] = '\0';
15982e2caf59SThomas Veerman 	cp = &res[cc];
15992e2caf59SThomas Veerman 
16002e2caf59SThomas Veerman 	if (cc > 0 && *--cp == '\n') {
16012e2caf59SThomas Veerman 	    /*
16022e2caf59SThomas Veerman 	     * A final newline is just stripped
16032e2caf59SThomas Veerman 	     */
16042e2caf59SThomas Veerman 	    *cp-- = '\0';
16052e2caf59SThomas Veerman 	}
16062e2caf59SThomas Veerman 	while (cp >= res) {
16072e2caf59SThomas Veerman 	    if (*cp == '\n') {
16082e2caf59SThomas Veerman 		*cp = ' ';
16092e2caf59SThomas Veerman 	    }
16102e2caf59SThomas Veerman 	    cp--;
16112e2caf59SThomas Veerman 	}
16122e2caf59SThomas Veerman 	break;
16132e2caf59SThomas Veerman     }
16142e2caf59SThomas Veerman     return res;
16152e2caf59SThomas Veerman bad:
16162e2caf59SThomas Veerman     res = bmake_malloc(1);
16172e2caf59SThomas Veerman     *res = '\0';
16182e2caf59SThomas Veerman     return res;
16192e2caf59SThomas Veerman }
16202e2caf59SThomas Veerman 
16212e2caf59SThomas Veerman /*-
16222e2caf59SThomas Veerman  * Error --
16232e2caf59SThomas Veerman  *	Print an error message given its format.
16242e2caf59SThomas Veerman  *
16252e2caf59SThomas Veerman  * Results:
16262e2caf59SThomas Veerman  *	None.
16272e2caf59SThomas Veerman  *
16282e2caf59SThomas Veerman  * Side Effects:
16292e2caf59SThomas Veerman  *	The message is printed.
16302e2caf59SThomas Veerman  */
16312e2caf59SThomas Veerman /* VARARGS */
16322e2caf59SThomas Veerman void
Error(const char * fmt,...)16332e2caf59SThomas Veerman Error(const char *fmt, ...)
16342e2caf59SThomas Veerman {
16352e2caf59SThomas Veerman 	va_list ap;
16362e2caf59SThomas Veerman 	FILE *err_file;
16372e2caf59SThomas Veerman 
16382e2caf59SThomas Veerman 	err_file = debug_file;
16392e2caf59SThomas Veerman 	if (err_file == stdout)
16402e2caf59SThomas Veerman 		err_file = stderr;
16412e2caf59SThomas Veerman 	(void)fflush(stdout);
16422e2caf59SThomas Veerman 	for (;;) {
16432e2caf59SThomas Veerman 		va_start(ap, fmt);
16442e2caf59SThomas Veerman 		fprintf(err_file, "%s: ", progname);
16452e2caf59SThomas Veerman 		(void)vfprintf(err_file, fmt, ap);
16462e2caf59SThomas Veerman 		va_end(ap);
16472e2caf59SThomas Veerman 		(void)fprintf(err_file, "\n");
16482e2caf59SThomas Veerman 		(void)fflush(err_file);
16492e2caf59SThomas Veerman 		if (err_file == stderr)
16502e2caf59SThomas Veerman 			break;
16512e2caf59SThomas Veerman 		err_file = stderr;
16522e2caf59SThomas Veerman 	}
16532e2caf59SThomas Veerman }
16542e2caf59SThomas Veerman 
16552e2caf59SThomas Veerman /*-
16562e2caf59SThomas Veerman  * Fatal --
16572e2caf59SThomas Veerman  *	Produce a Fatal error message. If jobs are running, waits for them
16582e2caf59SThomas Veerman  *	to finish.
16592e2caf59SThomas Veerman  *
16602e2caf59SThomas Veerman  * Results:
16612e2caf59SThomas Veerman  *	None
16622e2caf59SThomas Veerman  *
16632e2caf59SThomas Veerman  * Side Effects:
16642e2caf59SThomas Veerman  *	The program exits
16652e2caf59SThomas Veerman  */
16662e2caf59SThomas Veerman /* VARARGS */
16672e2caf59SThomas Veerman void
Fatal(const char * fmt,...)16682e2caf59SThomas Veerman Fatal(const char *fmt, ...)
16692e2caf59SThomas Veerman {
16702e2caf59SThomas Veerman 	va_list ap;
16712e2caf59SThomas Veerman 
16722e2caf59SThomas Veerman 	va_start(ap, fmt);
16732e2caf59SThomas Veerman 	if (jobsRunning)
16742e2caf59SThomas Veerman 		Job_Wait();
16752e2caf59SThomas Veerman 
16762e2caf59SThomas Veerman 	(void)fflush(stdout);
16772e2caf59SThomas Veerman 	(void)vfprintf(stderr, fmt, ap);
16782e2caf59SThomas Veerman 	va_end(ap);
16792e2caf59SThomas Veerman 	(void)fprintf(stderr, "\n");
16802e2caf59SThomas Veerman 	(void)fflush(stderr);
16812e2caf59SThomas Veerman 
16822e2caf59SThomas Veerman 	PrintOnError(NULL, NULL);
16832e2caf59SThomas Veerman 
16842e2caf59SThomas Veerman 	if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
16852e2caf59SThomas Veerman 		Targ_PrintGraph(2);
16862e2caf59SThomas Veerman 	Trace_Log(MAKEERROR, 0);
16872e2caf59SThomas Veerman 	exit(2);		/* Not 1 so -q can distinguish error */
16882e2caf59SThomas Veerman }
16892e2caf59SThomas Veerman 
16902e2caf59SThomas Veerman /*
16912e2caf59SThomas Veerman  * Punt --
16922e2caf59SThomas Veerman  *	Major exception once jobs are being created. Kills all jobs, prints
16932e2caf59SThomas Veerman  *	a message and exits.
16942e2caf59SThomas Veerman  *
16952e2caf59SThomas Veerman  * Results:
16962e2caf59SThomas Veerman  *	None
16972e2caf59SThomas Veerman  *
16982e2caf59SThomas Veerman  * Side Effects:
16992e2caf59SThomas Veerman  *	All children are killed indiscriminately and the program Lib_Exits
17002e2caf59SThomas Veerman  */
17012e2caf59SThomas Veerman /* VARARGS */
17022e2caf59SThomas Veerman void
Punt(const char * fmt,...)17032e2caf59SThomas Veerman Punt(const char *fmt, ...)
17042e2caf59SThomas Veerman {
17052e2caf59SThomas Veerman 	va_list ap;
17062e2caf59SThomas Veerman 
17072e2caf59SThomas Veerman 	va_start(ap, fmt);
17082e2caf59SThomas Veerman 	(void)fflush(stdout);
17092e2caf59SThomas Veerman 	(void)fprintf(stderr, "%s: ", progname);
17102e2caf59SThomas Veerman 	(void)vfprintf(stderr, fmt, ap);
17112e2caf59SThomas Veerman 	va_end(ap);
17122e2caf59SThomas Veerman 	(void)fprintf(stderr, "\n");
17132e2caf59SThomas Veerman 	(void)fflush(stderr);
17142e2caf59SThomas Veerman 
17152e2caf59SThomas Veerman 	PrintOnError(NULL, NULL);
17162e2caf59SThomas Veerman 
17172e2caf59SThomas Veerman 	DieHorribly();
17182e2caf59SThomas Veerman }
17192e2caf59SThomas Veerman 
17202e2caf59SThomas Veerman /*-
17212e2caf59SThomas Veerman  * DieHorribly --
17222e2caf59SThomas Veerman  *	Exit without giving a message.
17232e2caf59SThomas Veerman  *
17242e2caf59SThomas Veerman  * Results:
17252e2caf59SThomas Veerman  *	None
17262e2caf59SThomas Veerman  *
17272e2caf59SThomas Veerman  * Side Effects:
17282e2caf59SThomas Veerman  *	A big one...
17292e2caf59SThomas Veerman  */
17302e2caf59SThomas Veerman void
DieHorribly(void)17312e2caf59SThomas Veerman DieHorribly(void)
17322e2caf59SThomas Veerman {
17332e2caf59SThomas Veerman 	if (jobsRunning)
17342e2caf59SThomas Veerman 		Job_AbortAll();
17352e2caf59SThomas Veerman 	if (DEBUG(GRAPH2))
17362e2caf59SThomas Veerman 		Targ_PrintGraph(2);
17372e2caf59SThomas Veerman 	Trace_Log(MAKEERROR, 0);
17382e2caf59SThomas Veerman 	exit(2);		/* Not 1, so -q can distinguish error */
17392e2caf59SThomas Veerman }
17402e2caf59SThomas Veerman 
17412e2caf59SThomas Veerman /*
17422e2caf59SThomas Veerman  * Finish --
17432e2caf59SThomas Veerman  *	Called when aborting due to errors in child shell to signal
17442e2caf59SThomas Veerman  *	abnormal exit.
17452e2caf59SThomas Veerman  *
17462e2caf59SThomas Veerman  * Results:
17472e2caf59SThomas Veerman  *	None
17482e2caf59SThomas Veerman  *
17492e2caf59SThomas Veerman  * Side Effects:
17502e2caf59SThomas Veerman  *	The program exits
17512e2caf59SThomas Veerman  */
17522e2caf59SThomas Veerman void
Finish(int errors)17532e2caf59SThomas Veerman Finish(int errors)
17542e2caf59SThomas Veerman 	           	/* number of errors encountered in Make_Make */
17552e2caf59SThomas Veerman {
17562e2caf59SThomas Veerman 	Fatal("%d error%s", errors, errors == 1 ? "" : "s");
17572e2caf59SThomas Veerman }
17582e2caf59SThomas Veerman 
17592e2caf59SThomas Veerman /*
176084d9c625SLionel Sambuc  * eunlink --
17612e2caf59SThomas Veerman  *	Remove a file carefully, avoiding directories.
17622e2caf59SThomas Veerman  */
17632e2caf59SThomas Veerman int
eunlink(const char * file)17642e2caf59SThomas Veerman eunlink(const char *file)
17652e2caf59SThomas Veerman {
17662e2caf59SThomas Veerman 	struct stat st;
17672e2caf59SThomas Veerman 
17682e2caf59SThomas Veerman 	if (lstat(file, &st) == -1)
17692e2caf59SThomas Veerman 		return -1;
17702e2caf59SThomas Veerman 
17712e2caf59SThomas Veerman 	if (S_ISDIR(st.st_mode)) {
17722e2caf59SThomas Veerman 		errno = EISDIR;
17732e2caf59SThomas Veerman 		return -1;
17742e2caf59SThomas Veerman 	}
17752e2caf59SThomas Veerman 	return unlink(file);
17762e2caf59SThomas Veerman }
17772e2caf59SThomas Veerman 
17782e2caf59SThomas Veerman /*
17792e2caf59SThomas Veerman  * execError --
17802e2caf59SThomas Veerman  *	Print why exec failed, avoiding stdio.
17812e2caf59SThomas Veerman  */
17822e2caf59SThomas Veerman void
execError(const char * af,const char * av)17832e2caf59SThomas Veerman execError(const char *af, const char *av)
17842e2caf59SThomas Veerman {
17852e2caf59SThomas Veerman #ifdef USE_IOVEC
17862e2caf59SThomas Veerman 	int i = 0;
17872e2caf59SThomas Veerman 	struct iovec iov[8];
17882e2caf59SThomas Veerman #define IOADD(s) \
17892e2caf59SThomas Veerman 	(void)(iov[i].iov_base = UNCONST(s), \
17902e2caf59SThomas Veerman 	    iov[i].iov_len = strlen(iov[i].iov_base), \
17912e2caf59SThomas Veerman 	    i++)
17922e2caf59SThomas Veerman #else
17932e2caf59SThomas Veerman #define	IOADD(void)write(2, s, strlen(s))
17942e2caf59SThomas Veerman #endif
17952e2caf59SThomas Veerman 
17962e2caf59SThomas Veerman 	IOADD(progname);
17972e2caf59SThomas Veerman 	IOADD(": ");
17982e2caf59SThomas Veerman 	IOADD(af);
17992e2caf59SThomas Veerman 	IOADD("(");
18002e2caf59SThomas Veerman 	IOADD(av);
18012e2caf59SThomas Veerman 	IOADD(") failed (");
18022e2caf59SThomas Veerman 	IOADD(strerror(errno));
18032e2caf59SThomas Veerman 	IOADD(")\n");
18042e2caf59SThomas Veerman 
18052e2caf59SThomas Veerman #ifdef USE_IOVEC
180684d9c625SLionel Sambuc 	while (writev(2, iov, 8) == -1 && errno == EAGAIN)
180784d9c625SLionel Sambuc 	    continue;
18082e2caf59SThomas Veerman #endif
18092e2caf59SThomas Veerman }
18102e2caf59SThomas Veerman 
18112e2caf59SThomas Veerman /*
18122e2caf59SThomas Veerman  * usage --
18132e2caf59SThomas Veerman  *	exit with usage message
18142e2caf59SThomas Veerman  */
18152e2caf59SThomas Veerman static void
usage(void)18162e2caf59SThomas Veerman usage(void)
18172e2caf59SThomas Veerman {
181884d9c625SLionel Sambuc 	char *p;
181984d9c625SLionel Sambuc 	if ((p = strchr(progname, '[')) != NULL)
182084d9c625SLionel Sambuc 	    *p = '\0';
182184d9c625SLionel Sambuc 
18222e2caf59SThomas Veerman 	(void)fprintf(stderr,
182384d9c625SLionel Sambuc "usage: %s [-BeikNnqrstWwX] \n\
18242e2caf59SThomas Veerman             [-C directory] [-D variable] [-d flags] [-f makefile]\n\
18252e2caf59SThomas Veerman             [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n\
18262e2caf59SThomas Veerman             [-V variable] [variable=value] [target ...]\n", progname);
18272e2caf59SThomas Veerman 	exit(2);
18282e2caf59SThomas Veerman }
18292e2caf59SThomas Veerman 
18302e2caf59SThomas Veerman 
18312e2caf59SThomas Veerman int
PrintAddr(void * a,void * b)18322e2caf59SThomas Veerman PrintAddr(void *a, void *b)
18332e2caf59SThomas Veerman {
18342e2caf59SThomas Veerman     printf("%lx ", (unsigned long) a);
18352e2caf59SThomas Veerman     return b ? 0 : 0;
18362e2caf59SThomas Veerman }
18372e2caf59SThomas Veerman 
18382e2caf59SThomas Veerman 
18392e2caf59SThomas Veerman 
18402e2caf59SThomas Veerman void
PrintOnError(GNode * gn,const char * s)18412e2caf59SThomas Veerman PrintOnError(GNode *gn, const char *s)
18422e2caf59SThomas Veerman {
18432e2caf59SThomas Veerman     static GNode *en = NULL;
18442e2caf59SThomas Veerman     char tmp[64];
18452e2caf59SThomas Veerman     char *cp;
18462e2caf59SThomas Veerman 
18472e2caf59SThomas Veerman     if (s)
18482e2caf59SThomas Veerman 	printf("%s", s);
18492e2caf59SThomas Veerman 
18502e2caf59SThomas Veerman     printf("\n%s: stopped in %s\n", progname, curdir);
18512e2caf59SThomas Veerman 
18522e2caf59SThomas Veerman     if (en)
18532e2caf59SThomas Veerman 	return;				/* we've been here! */
18542e2caf59SThomas Veerman     if (gn) {
18552e2caf59SThomas Veerman 	/*
18562e2caf59SThomas Veerman 	 * We can print this even if there is no .ERROR target.
18572e2caf59SThomas Veerman 	 */
18582e2caf59SThomas Veerman 	Var_Set(".ERROR_TARGET", gn->name, VAR_GLOBAL, 0);
18592e2caf59SThomas Veerman     }
18602e2caf59SThomas Veerman     strncpy(tmp, "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
18612e2caf59SThomas Veerman 	    sizeof(tmp) - 1);
18622e2caf59SThomas Veerman     cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
18632e2caf59SThomas Veerman     if (cp) {
18642e2caf59SThomas Veerman 	if (*cp)
18652e2caf59SThomas Veerman 	    printf("%s", cp);
18662e2caf59SThomas Veerman 	free(cp);
18672e2caf59SThomas Veerman     }
18682e2caf59SThomas Veerman     /*
18692e2caf59SThomas Veerman      * Finally, see if there is a .ERROR target, and run it if so.
18702e2caf59SThomas Veerman      */
18712e2caf59SThomas Veerman     en = Targ_FindNode(".ERROR", TARG_NOCREATE);
18722e2caf59SThomas Veerman     if (en) {
18732e2caf59SThomas Veerman 	en->type |= OP_SPECIAL;
18742e2caf59SThomas Veerman 	Compat_Make(en, en);
18752e2caf59SThomas Veerman     }
18762e2caf59SThomas Veerman }
18772e2caf59SThomas Veerman 
18782e2caf59SThomas Veerman void
Main_ExportMAKEFLAGS(Boolean first)18792e2caf59SThomas Veerman Main_ExportMAKEFLAGS(Boolean first)
18802e2caf59SThomas Veerman {
18812e2caf59SThomas Veerman     static int once = 1;
18822e2caf59SThomas Veerman     char tmp[64];
18832e2caf59SThomas Veerman     char *s;
18842e2caf59SThomas Veerman 
18852e2caf59SThomas Veerman     if (once != first)
18862e2caf59SThomas Veerman 	return;
18872e2caf59SThomas Veerman     once = 0;
18882e2caf59SThomas Veerman 
18892e2caf59SThomas Veerman     strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}",
18902e2caf59SThomas Veerman 	    sizeof(tmp));
18912e2caf59SThomas Veerman     s = Var_Subst(NULL, tmp, VAR_CMD, 0);
18922e2caf59SThomas Veerman     if (s && *s) {
18932e2caf59SThomas Veerman #ifdef POSIX
18942e2caf59SThomas Veerman 	setenv("MAKEFLAGS", s, 1);
18952e2caf59SThomas Veerman #else
18962e2caf59SThomas Veerman 	setenv("MAKE", s, 1);
18972e2caf59SThomas Veerman #endif
18982e2caf59SThomas Veerman     }
18992e2caf59SThomas Veerman }
19002e2caf59SThomas Veerman 
19012e2caf59SThomas Veerman char *
getTmpdir(void)19022e2caf59SThomas Veerman getTmpdir(void)
19032e2caf59SThomas Veerman {
19042e2caf59SThomas Veerman     static char *tmpdir = NULL;
19052e2caf59SThomas Veerman 
19062e2caf59SThomas Veerman     if (!tmpdir) {
19072e2caf59SThomas Veerman 	struct stat st;
19082e2caf59SThomas Veerman 
19092e2caf59SThomas Veerman 	/*
19102e2caf59SThomas Veerman 	 * Honor $TMPDIR but only if it is valid.
19112e2caf59SThomas Veerman 	 * Ensure it ends with /.
19122e2caf59SThomas Veerman 	 */
19132e2caf59SThomas Veerman 	tmpdir = Var_Subst(NULL, "${TMPDIR:tA:U" _PATH_TMP "}/", VAR_GLOBAL, 0);
19142e2caf59SThomas Veerman 	if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
19152e2caf59SThomas Veerman 	    free(tmpdir);
19162e2caf59SThomas Veerman 	    tmpdir = bmake_strdup(_PATH_TMP);
19172e2caf59SThomas Veerman 	}
19182e2caf59SThomas Veerman     }
19192e2caf59SThomas Veerman     return tmpdir;
19202e2caf59SThomas Veerman }
19212e2caf59SThomas Veerman 
19222e2caf59SThomas Veerman /*
19232e2caf59SThomas Veerman  * Create and open a temp file using "pattern".
19242e2caf59SThomas Veerman  * If "fnamep" is provided set it to a copy of the filename created.
19252e2caf59SThomas Veerman  * Otherwise unlink the file once open.
19262e2caf59SThomas Veerman  */
19272e2caf59SThomas Veerman int
mkTempFile(const char * pattern,char ** fnamep)19282e2caf59SThomas Veerman mkTempFile(const char *pattern, char **fnamep)
19292e2caf59SThomas Veerman {
19302e2caf59SThomas Veerman     static char *tmpdir = NULL;
19312e2caf59SThomas Veerman     char tfile[MAXPATHLEN];
19322e2caf59SThomas Veerman     int fd;
19332e2caf59SThomas Veerman 
19342e2caf59SThomas Veerman     if (!pattern)
19352e2caf59SThomas Veerman 	pattern = TMPPAT;
19362e2caf59SThomas Veerman     if (!tmpdir)
19372e2caf59SThomas Veerman 	tmpdir = getTmpdir();
19382e2caf59SThomas Veerman     if (pattern[0] == '/') {
19392e2caf59SThomas Veerman 	snprintf(tfile, sizeof(tfile), "%s", pattern);
19402e2caf59SThomas Veerman     } else {
19412e2caf59SThomas Veerman 	snprintf(tfile, sizeof(tfile), "%s%s", tmpdir, pattern);
19422e2caf59SThomas Veerman     }
19432e2caf59SThomas Veerman     if ((fd = mkstemp(tfile)) < 0)
19442e2caf59SThomas Veerman 	Punt("Could not create temporary file %s: %s", tfile, strerror(errno));
19452e2caf59SThomas Veerman     if (fnamep) {
19462e2caf59SThomas Veerman 	*fnamep = bmake_strdup(tfile);
19472e2caf59SThomas Veerman     } else {
19482e2caf59SThomas Veerman 	unlink(tfile);			/* we just want the descriptor */
19492e2caf59SThomas Veerman     }
19502e2caf59SThomas Veerman     return fd;
19512e2caf59SThomas Veerman }
19522bc7c627SLionel Sambuc 
19532bc7c627SLionel Sambuc 
19542bc7c627SLionel Sambuc /*
19552bc7c627SLionel Sambuc  * Return a Boolean based on setting of a knob.
19562bc7c627SLionel Sambuc  *
19572bc7c627SLionel Sambuc  * If the knob is not set, the supplied default is the return value.
19582bc7c627SLionel Sambuc  * If set, anything that looks or smells like "No", "False", "Off", "0" etc,
19592bc7c627SLionel Sambuc  * is FALSE, otherwise TRUE.
19602bc7c627SLionel Sambuc  */
19612bc7c627SLionel Sambuc Boolean
getBoolean(const char * name,Boolean bf)19622bc7c627SLionel Sambuc getBoolean(const char *name, Boolean bf)
19632bc7c627SLionel Sambuc {
19642bc7c627SLionel Sambuc     char tmp[64];
19652bc7c627SLionel Sambuc     char *cp;
19662bc7c627SLionel Sambuc 
19672bc7c627SLionel Sambuc     if (snprintf(tmp, sizeof(tmp), "${%s:tl}", name) < (int)(sizeof(tmp))) {
19682bc7c627SLionel Sambuc 	cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
19692bc7c627SLionel Sambuc 
19702bc7c627SLionel Sambuc 	if (cp) {
19712bc7c627SLionel Sambuc 	    switch(*cp) {
19722bc7c627SLionel Sambuc 	    case '\0':			/* not set - the default wins */
19732bc7c627SLionel Sambuc 		break;
19742bc7c627SLionel Sambuc 	    case '0':
19752bc7c627SLionel Sambuc 	    case 'f':
19762bc7c627SLionel Sambuc 	    case 'n':
19772bc7c627SLionel Sambuc 		bf = FALSE;
19782bc7c627SLionel Sambuc 		break;
19792bc7c627SLionel Sambuc 	    case 'o':
19802bc7c627SLionel Sambuc 		switch (cp[1]) {
19812bc7c627SLionel Sambuc 		case 'f':
19822bc7c627SLionel Sambuc 		    bf = FALSE;
19832bc7c627SLionel Sambuc 		    break;
19842bc7c627SLionel Sambuc 		default:
19852bc7c627SLionel Sambuc 		    bf = TRUE;
19862bc7c627SLionel Sambuc 		    break;
19872bc7c627SLionel Sambuc 		}
19882bc7c627SLionel Sambuc 		break;
19892bc7c627SLionel Sambuc 	    default:
19902bc7c627SLionel Sambuc 		bf = TRUE;
19912bc7c627SLionel Sambuc 		break;
19922bc7c627SLionel Sambuc 	    }
19932bc7c627SLionel Sambuc 	    free(cp);
19942bc7c627SLionel Sambuc 	}
19952bc7c627SLionel Sambuc     }
19962bc7c627SLionel Sambuc     return (bf);
19972bc7c627SLionel Sambuc }
1998