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