File:  [DragonFly] / src / usr.bin / make / main.c
Revision 1.4: download - view: text, annotated - select for diffs
Mon Jan 26 18:16:18 2004 UTC (10 years, 6 months ago) by drhodus
Branches: MAIN
CVS tags: HEAD
*	DragonFly<-FreeBSD Name clean ups

    1: /*
    2:  * Copyright (c) 1988, 1989, 1990, 1993
    3:  *	The Regents of the University of California.  All rights reserved.
    4:  * Copyright (c) 1989 by Berkeley Softworks
    5:  * 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. All advertising materials mentioning features or use of this software
   19:  *    must display the following acknowledgement:
   20:  *	This product includes software developed by the University of
   21:  *	California, Berkeley and its contributors.
   22:  * 4. Neither the name of the University nor the names of its contributors
   23:  *    may be used to endorse or promote products derived from this software
   24:  *    without specific prior written permission.
   25:  *
   26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   36:  * SUCH DAMAGE.
   37:  *
   38:  * @(#) Copyright (c) 1988, 1989, 1990, 1993 The Regents of the University of California.  All rights reserved.
   39:  * @(#)main.c	8.3 (Berkeley) 3/19/94
   40:  * $FreeBSD: src/usr.bin/make/main.c,v 1.35.2.9 2003/04/15 14:37:35 ru Exp $
   41:  * $DragonFly: src/usr.bin/make/main.c,v 1.4 2004/01/26 18:16:18 drhodus Exp $
   42:  */
   43: 
   44: /*-
   45:  * main.c --
   46:  *	The main file for this entire program. Exit routines etc
   47:  *	reside here.
   48:  *
   49:  * Utility functions defined in this file:
   50:  *	Main_ParseArgLine	Takes a line of arguments, breaks them and
   51:  *				treats them as if they were given when first
   52:  *				invoked. Used by the parse module to implement
   53:  *				the .MFLAGS target.
   54:  *
   55:  *	Error			Print a tagged error message. The global
   56:  *				MAKE variable must have been defined. This
   57:  *				takes a format string and two optional
   58:  *				arguments for it.
   59:  *
   60:  *	Fatal			Print an error message and exit. Also takes
   61:  *				a format string and two arguments.
   62:  *
   63:  *	Punt			Aborts all jobs and exits with a message. Also
   64:  *				takes a format string and two arguments.
   65:  *
   66:  *	Finish			Finish things up by printing the number of
   67:  *				errors which occured, as passed to it, and
   68:  *				exiting.
   69:  */
   70: 
   71: #include <sys/types.h>
   72: #include <sys/time.h>
   73: #include <sys/param.h>
   74: #include <sys/resource.h>
   75: #include <sys/signal.h>
   76: #include <sys/stat.h>
   77: #if defined(__i386__)
   78: #include <sys/sysctl.h>
   79: #endif
   80: #ifndef MACHINE
   81: #include <sys/utsname.h>
   82: #endif
   83: #include <sys/wait.h>
   84: #include <err.h>
   85: #include <stdlib.h>
   86: #include <errno.h>
   87: #include <fcntl.h>
   88: #include <stdio.h>
   89: #include <sysexits.h>
   90: #ifdef __STDC__
   91: #include <stdarg.h>
   92: #else
   93: #include <varargs.h>
   94: #endif
   95: #include "make.h"
   96: #include "hash.h"
   97: #include "dir.h"
   98: #include "job.h"
   99: #include "pathnames.h"
  100: 
  101: #ifndef	DEFMAXLOCAL
  102: #define	DEFMAXLOCAL DEFMAXJOBS
  103: #endif	/* DEFMAXLOCAL */
  104: 
  105: #define	MAKEFLAGS	".MAKEFLAGS"
  106: 
  107: Lst			create;		/* Targets to be made */
  108: time_t			now;		/* Time at start of make */
  109: GNode			*DEFAULT;	/* .DEFAULT node */
  110: Boolean			allPrecious;	/* .PRECIOUS given on line by itself */
  111: 
  112: static Boolean		noBuiltins;	/* -r flag */
  113: static Lst		makefiles;	/* ordered list of makefiles to read */
  114: static Boolean		printVars;	/* print value of one or more vars */
  115: static Boolean		expandVars;	/* fully expand printed variables */
  116: static Lst		variables;	/* list of variables to print */
  117: int			maxJobs;	/* -j argument */
  118: static Boolean          forceJobs;      /* -j argument given */
  119: static int		maxLocal;	/* -L argument */
  120: Boolean			compatMake;	/* -B argument */
  121: Boolean			debug;		/* -d flag */
  122: Boolean			noExecute;	/* -n flag */
  123: Boolean			keepgoing;	/* -k flag */
  124: Boolean			queryFlag;	/* -q flag */
  125: Boolean			touchFlag;	/* -t flag */
  126: Boolean			usePipes;	/* !-P flag */
  127: Boolean			ignoreErrors;	/* -i flag */
  128: Boolean			beSilent;	/* -s flag */
  129: Boolean			beVerbose;	/* -v flag */
  130: Boolean			oldVars;	/* variable substitution style */
  131: Boolean			checkEnvFirst;	/* -e flag */
  132: Lst			envFirstVars;	/* (-E) vars to override from env */
  133: static Boolean		jobsRunning;	/* TRUE if the jobs might be running */
  134: 
  135: static void		MainParseArgs(int, char **);
  136: char *			chdir_verify_path(char *, char *);
  137: static int		ReadMakefile(ClientData, ClientData);
  138: static void		usage(void);
  139: 
  140: static char *curdir;			/* startup directory */
  141: static char *objdir;			/* where we chdir'ed to */
  142: 
  143: /*-
  144:  * MainParseArgs --
  145:  *	Parse a given argument vector. Called from main() and from
  146:  *	Main_ParseArgLine() when the .MAKEFLAGS target is used.
  147:  *
  148:  *	XXX: Deal with command line overriding .MAKEFLAGS in makefile
  149:  *
  150:  * Results:
  151:  *	None
  152:  *
  153:  * Side Effects:
  154:  *	Various global and local flags will be set depending on the flags
  155:  *	given
  156:  */
  157: static void
  158: MainParseArgs(argc, argv)
  159: 	int argc;
  160: 	char **argv;
  161: {
  162: 	extern int optind;
  163: 	extern char *optarg;
  164: 	char *p;
  165: 	int c;
  166: 
  167: 	optind = 1;	/* since we're called more than once */
  168: #ifdef REMOTE
  169: # define OPTFLAGS "BC:D:E:I:L:PSV:Xd:ef:ij:km:nqrstv"
  170: #else
  171: # define OPTFLAGS "BC:D:E:I:PSV:Xd:ef:ij:km:nqrstv"
  172: #endif
  173: rearg:	while((c = getopt(argc, argv, OPTFLAGS)) != -1) {
  174: 		switch(c) {
  175: 		case 'C':
  176: 			if (chdir(optarg) == -1)
  177: 				err(1, "chdir %s", optarg);
  178: 			break;
  179: 		case 'D':
  180: 			Var_Set(optarg, "1", VAR_GLOBAL);
  181: 			Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
  182: 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
  183: 			break;
  184: 		case 'I':
  185: 			Parse_AddIncludeDir(optarg);
  186: 			Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
  187: 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
  188: 			break;
  189: 		case 'V':
  190: 			printVars = TRUE;
  191: 			(void)Lst_AtEnd(variables, (ClientData)optarg);
  192: 			Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
  193: 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
  194: 			break;
  195: 		case 'X':
  196: 			expandVars = FALSE;
  197: 			break;
  198: 		case 'B':
  199: 			compatMake = TRUE;
  200: 			Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
  201: 			break;
  202: #ifdef REMOTE
  203: 		case 'L': {
  204: 			char *endptr;
  205: 
  206: 			maxLocal = strtol(optarg, &endptr, 10);
  207: 			if (maxLocal < 0 || *endptr != '\0') {
  208: 				warnx("illegal number, -L argument -- %s",
  209: 				    optarg);
  210: 				usage();
  211: 			}
  212: 			Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL);
  213: 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
  214: 			break;
  215: 		}
  216: #endif
  217: 		case 'P':
  218: 			usePipes = FALSE;
  219: 			Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL);
  220: 			break;
  221: 		case 'S':
  222: 			keepgoing = FALSE;
  223: 			Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
  224: 			break;
  225: 		case 'd': {
  226: 			char *modules = optarg;
  227: 
  228: 			for (; *modules; ++modules)
  229: 				switch (*modules) {
  230: 				case 'A':
  231: 					debug = ~0;
  232: 					break;
  233: 				case 'a':
  234: 					debug |= DEBUG_ARCH;
  235: 					break;
  236: 				case 'c':
  237: 					debug |= DEBUG_COND;
  238: 					break;
  239: 				case 'd':
  240: 					debug |= DEBUG_DIR;
  241: 					break;
  242: 				case 'f':
  243: 					debug |= DEBUG_FOR;
  244: 					break;
  245: 				case 'g':
  246: 					if (modules[1] == '1') {
  247: 						debug |= DEBUG_GRAPH1;
  248: 						++modules;
  249: 					}
  250: 					else if (modules[1] == '2') {
  251: 						debug |= DEBUG_GRAPH2;
  252: 						++modules;
  253: 					}
  254: 					break;
  255: 				case 'j':
  256: 					debug |= DEBUG_JOB;
  257: 					break;
  258: 				case 'l':
  259: 					debug |= DEBUG_LOUD;
  260: 					break;
  261: 				case 'm':
  262: 					debug |= DEBUG_MAKE;
  263: 					break;
  264: 				case 's':
  265: 					debug |= DEBUG_SUFF;
  266: 					break;
  267: 				case 't':
  268: 					debug |= DEBUG_TARG;
  269: 					break;
  270: 				case 'v':
  271: 					debug |= DEBUG_VAR;
  272: 					break;
  273: 				default:
  274: 					warnx("illegal argument to d option -- %c", *modules);
  275: 					usage();
  276: 				}
  277: 			Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
  278: 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
  279: 			break;
  280: 		}
  281: 		case 'E':
  282: 			p = malloc(strlen(optarg) + 1);
  283: 			if (!p)
  284: 				Punt("make: cannot allocate memory.");
  285: 			(void)strcpy(p, optarg);
  286: 			(void)Lst_AtEnd(envFirstVars, (ClientData)p);
  287: 			Var_Append(MAKEFLAGS, "-E", VAR_GLOBAL);
  288: 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
  289: 			break;
  290: 		case 'e':
  291: 			checkEnvFirst = TRUE;
  292: 			Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
  293: 			break;
  294: 		case 'f':
  295: 			(void)Lst_AtEnd(makefiles, (ClientData)optarg);
  296: 			break;
  297: 		case 'i':
  298: 			ignoreErrors = TRUE;
  299: 			Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
  300: 			break;
  301: 		case 'j': {
  302: 			char *endptr;
  303: 
  304: 			forceJobs = TRUE;
  305: 			maxJobs = strtol(optarg, &endptr, 10);
  306: 			if (maxJobs <= 0 || *endptr != '\0') {
  307: 				warnx("illegal number, -j argument -- %s",
  308: 				    optarg);
  309: 				usage();
  310: 			}
  311: #ifndef REMOTE
  312: 			maxLocal = maxJobs;
  313: #endif
  314: 			Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
  315: 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
  316: 			break;
  317: 		}
  318: 		case 'k':
  319: 			keepgoing = TRUE;
  320: 			Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
  321: 			break;
  322: 		case 'm':
  323: 			Dir_AddDir(sysIncPath, optarg);
  324: 			Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
  325: 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
  326: 			break;
  327: 		case 'n':
  328: 			noExecute = TRUE;
  329: 			Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
  330: 			break;
  331: 		case 'q':
  332: 			queryFlag = TRUE;
  333: 			/* Kind of nonsensical, wot? */
  334: 			Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
  335: 			break;
  336: 		case 'r':
  337: 			noBuiltins = TRUE;
  338: 			Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
  339: 			break;
  340: 		case 's':
  341: 			beSilent = TRUE;
  342: 			Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
  343: 			break;
  344: 		case 't':
  345: 			touchFlag = TRUE;
  346: 			Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
  347: 			break;
  348: 		case 'v':
  349: 			beVerbose = TRUE;
  350: 			Var_Append(MAKEFLAGS, "-v", VAR_GLOBAL);
  351: 			break;
  352: 		default:
  353: 		case '?':
  354: 			usage();
  355: 		}
  356: 	}
  357: 
  358: 	oldVars = TRUE;
  359: 
  360: 	/*
  361: 	 * See if the rest of the arguments are variable assignments and
  362: 	 * perform them if so. Else take them to be targets and stuff them
  363: 	 * on the end of the "create" list.
  364: 	 */
  365: 	for (argv += optind, argc -= optind; *argv; ++argv, --argc)
  366: 		if (Parse_IsVar(*argv))
  367: 			Parse_DoVar(*argv, VAR_CMD);
  368: 		else {
  369: 			if (!**argv)
  370: 				Punt("illegal (null) argument.");
  371: 			if (**argv == '-') {
  372: 				if ((*argv)[1])
  373: 					optind = 0;     /* -flag... */
  374: 				else
  375: 					optind = 1;     /* - */
  376: 				goto rearg;
  377: 			}
  378: 			(void)Lst_AtEnd(create, (ClientData)estrdup(*argv));
  379: 		}
  380: }
  381: 
  382: /*-
  383:  * Main_ParseArgLine --
  384:  *  	Used by the parse module when a .MFLAGS or .MAKEFLAGS target
  385:  *	is encountered and by main() when reading the .MAKEFLAGS envariable.
  386:  *	Takes a line of arguments and breaks it into its
  387:  * 	component words and passes those words and the number of them to the
  388:  *	MainParseArgs function.
  389:  *	The line should have all its leading whitespace removed.
  390:  *
  391:  * Results:
  392:  *	None
  393:  *
  394:  * Side Effects:
  395:  *	Only those that come from the various arguments.
  396:  */
  397: void
  398: Main_ParseArgLine(line)
  399: 	char *line;			/* Line to fracture */
  400: {
  401: 	char **argv;			/* Manufactured argument vector */
  402: 	int argc;			/* Number of arguments in argv */
  403: 
  404: 	if (line == NULL)
  405: 		return;
  406: 	for (; *line == ' '; ++line)
  407: 		continue;
  408: 	if (!*line)
  409: 		return;
  410: 
  411: 	argv = brk_string(line, &argc, TRUE);
  412: 	MainParseArgs(argc, argv);
  413: }
  414: 
  415: char *
  416: chdir_verify_path(path, obpath)
  417: 	char *path;
  418: 	char *obpath;
  419: {
  420: 	struct stat sb;
  421: 
  422: 	if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
  423: 		if (chdir(path)) {
  424: 			warn("warning: %s", path);
  425: 			return 0;
  426: 		}
  427: 		else {
  428: 			if (path[0] != '/') {
  429: 				(void) snprintf(obpath, MAXPATHLEN, "%s/%s",
  430: 						curdir, path);
  431: 				return obpath;
  432: 			}
  433: 			else
  434: 				return path;
  435: 		}
  436: 	}
  437: 
  438: 	return 0;
  439: }
  440: 
  441: 
  442: /*-
  443:  * main --
  444:  *	The main function, for obvious reasons. Initializes variables
  445:  *	and a few modules, then parses the arguments give it in the
  446:  *	environment and on the command line. Reads the system makefile
  447:  *	followed by either Makefile, makefile or the file given by the
  448:  *	-f argument. Sets the .MAKEFLAGS PMake variable based on all the
  449:  *	flags it has received by then uses either the Make or the Compat
  450:  *	module to create the initial list of targets.
  451:  *
  452:  * Results:
  453:  *	If -q was given, exits -1 if anything was out-of-date. Else it exits
  454:  *	0.
  455:  *
  456:  * Side Effects:
  457:  *	The program exits when done. Targets are created. etc. etc. etc.
  458:  */
  459: int
  460: main(argc, argv)
  461: 	int argc;
  462: 	char **argv;
  463: {
  464: 	Lst targs;	/* target nodes to create -- passed to Make_Init */
  465: 	Boolean outOfDate = TRUE; 	/* FALSE if all targets up to date */
  466: 	struct stat sa;
  467: 	char *p, *p1, *path, *pathp;
  468: #ifdef WANT_ENV_PWD
  469: 	struct stat sb;
  470: 	char *pwd;
  471: #endif
  472: 	char mdpath[MAXPATHLEN + 1];
  473: 	char obpath[MAXPATHLEN + 1];
  474: 	char cdpath[MAXPATHLEN + 1];
  475:     	char *machine = getenv("MACHINE");
  476: 	char *machine_arch = getenv("MACHINE_ARCH");
  477: 	char *machine_cpu = getenv("MACHINE_CPU");
  478: 	Lst sysMkPath;			/* Path of sys.mk */
  479: 	char *cp = NULL, *start;
  480: 					/* avoid faults on read-only strings */
  481: 	static char syspath[] = _PATH_DEFSYSPATH;
  482: 
  483: #ifdef RLIMIT_NOFILE
  484: 	/*
  485: 	 * get rid of resource limit on file descriptors
  486: 	 */
  487: 	{
  488: 		struct rlimit rl;
  489: 		if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
  490: 		    rl.rlim_cur != rl.rlim_max) {
  491: 			rl.rlim_cur = rl.rlim_max;
  492: 			(void) setrlimit(RLIMIT_NOFILE, &rl);
  493: 		}
  494: 	}
  495: #endif
  496: 	/*
  497: 	 * Find where we are and take care of PWD for the automounter...
  498: 	 * All this code is so that we know where we are when we start up
  499: 	 * on a different machine with pmake.
  500: 	 */
  501: 	curdir = cdpath;
  502: 	if (getcwd(curdir, MAXPATHLEN) == NULL)
  503: 		err(2, NULL);
  504: 
  505: 	if (stat(curdir, &sa) == -1)
  506: 	    err(2, "%s", curdir);
  507: 
  508: #ifdef WANT_ENV_PWD
  509: 	if ((pwd = getenv("PWD")) != NULL) {
  510: 	    if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
  511: 		sa.st_dev == sb.st_dev)
  512: 		(void) strcpy(curdir, pwd);
  513: 	}
  514: #endif
  515: 
  516: #if defined(__i386__) && defined(__DragonFly_version)
  517: 	/*
  518: 	 * PC-98 kernel sets the `i386' string to the utsname.machine and
  519: 	 * it cannot be distinguished from IBM-PC by uname(3).  Therefore,
  520: 	 * we check machine.ispc98 and adjust the machine variable before
  521: 	 * using usname(3) below.
  522: 	 * NOTE: machdep.ispc98 was defined on 1998/8/31.
  523: 	 */
  524: 	if (!machine) {
  525: 		int	ispc98;
  526: 		size_t	len;
  527: 
  528: 		len = sizeof(ispc98);
  529: 		if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) {
  530: 			if (ispc98)
  531: 				machine = "pc98";
  532: 		}
  533: 	}
  534: #endif
  535: 
  536: 	/*
  537: 	 * Get the name of this type of MACHINE from utsname
  538: 	 * so we can share an executable for similar machines.
  539: 	 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
  540: 	 *
  541: 	 * Note that while MACHINE is decided at run-time,
  542: 	 * MACHINE_ARCH is always known at compile time.
  543: 	 */
  544: 	if (!machine) {
  545: #ifndef MACHINE
  546: 	    struct utsname utsname;
  547: 
  548: 	    if (uname(&utsname) == -1) {
  549: 		    perror("make: uname");
  550: 		    exit(2);
  551: 	    }
  552: 	    machine = utsname.machine;
  553: #else
  554: 	    machine = MACHINE;
  555: #endif
  556: 	}
  557: 
  558: 	if (!machine_arch) {
  559: #ifndef MACHINE_ARCH
  560: 		machine_arch = "unknown";
  561: #else
  562: 		machine_arch = MACHINE_ARCH;
  563: #endif
  564: 	}
  565: 
  566: 	/*
  567: 	 * Set machine_cpu to the minumum supported CPU revision based
  568: 	 * on the target architecture, if not already set.
  569: 	 */
  570: 	if (!machine_cpu) {
  571: 		if (!strcmp(machine_arch, "i386"))
  572: 			machine_cpu = "i386";
  573: 		else if (!strcmp(machine_arch, "alpha"))
  574: 			machine_cpu = "ev4";
  575: 		else
  576: 			machine_cpu = "unknown";
  577: 	}
  578: 	
  579: 	/*
  580: 	 * The object directory location is determined using the
  581: 	 * following order of preference:
  582: 	 *
  583: 	 *	1. MAKEOBJDIRPREFIX`cwd`
  584: 	 *	2. MAKEOBJDIR
  585: 	 *	3. _PATH_OBJDIR.${MACHINE}
  586: 	 *	4. _PATH_OBJDIR
  587: 	 *	5. _PATH_OBJDIRPREFIX`cwd`
  588: 	 *
  589: 	 * If one of the first two fails, use the current directory.
  590: 	 * If the remaining three all fail, use the current directory.
  591: 	 *
  592: 	 * Once things are initted,
  593: 	 * have to add the original directory to the search path,
  594: 	 * and modify the paths for the Makefiles apropriately.  The
  595: 	 * current directory is also placed as a variable for make scripts.
  596: 	 */
  597: 	if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
  598: 		if (!(path = getenv("MAKEOBJDIR"))) {
  599: 			path = _PATH_OBJDIR;
  600: 			pathp = _PATH_OBJDIRPREFIX;
  601: 			(void) snprintf(mdpath, MAXPATHLEN, "%s.%s",
  602: 					path, machine);
  603: 			if (!(objdir = chdir_verify_path(mdpath, obpath)))
  604: 				if (!(objdir=chdir_verify_path(path, obpath))) {
  605: 					(void) snprintf(mdpath, MAXPATHLEN,
  606: 							"%s%s", pathp, curdir);
  607: 					if (!(objdir=chdir_verify_path(mdpath,
  608: 								       obpath)))
  609: 						objdir = curdir;
  610: 				}
  611: 		}
  612: 		else if (!(objdir = chdir_verify_path(path, obpath)))
  613: 			objdir = curdir;
  614: 	}
  615: 	else {
  616: 		(void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
  617: 		if (!(objdir = chdir_verify_path(mdpath, obpath)))
  618: 			objdir = curdir;
  619: 	}
  620: 
  621: #ifdef WANT_ENV_PWD
  622: 	setenv("PWD", objdir, 1);
  623: #endif
  624: 
  625: 	create = Lst_Init(FALSE);
  626: 	makefiles = Lst_Init(FALSE);
  627: 	envFirstVars = Lst_Init(FALSE);
  628: 	printVars = FALSE;
  629: 	expandVars = TRUE;
  630: 	variables = Lst_Init(FALSE);
  631: 	beSilent = FALSE;		/* Print commands as executed */
  632: 	ignoreErrors = FALSE;		/* Pay attention to non-zero returns */
  633: 	noExecute = FALSE;		/* Execute all commands */
  634: 	keepgoing = FALSE;		/* Stop on error */
  635: 	allPrecious = FALSE;		/* Remove targets when interrupted */
  636: 	queryFlag = FALSE;		/* This is not just a check-run */
  637: 	noBuiltins = FALSE;		/* Read the built-in rules */
  638: 	touchFlag = FALSE;		/* Actually update targets */
  639: 	usePipes = TRUE;		/* Catch child output in pipes */
  640: 	debug = 0;			/* No debug verbosity, please. */
  641: 	jobsRunning = FALSE;
  642: 
  643: 	maxLocal = DEFMAXLOCAL;		/* Set default local max concurrency */
  644: #ifdef REMOTE
  645: 	maxJobs = DEFMAXJOBS;		/* Set default max concurrency */
  646: #else
  647: 	maxJobs = maxLocal;
  648: #endif
  649: 	forceJobs = FALSE;              /* No -j flag */
  650: 	compatMake = FALSE;		/* No compat mode */
  651: 
  652: 
  653: 	/*
  654: 	 * Initialize the parsing, directory and variable modules to prepare
  655: 	 * for the reading of inclusion paths and variable settings on the
  656: 	 * command line
  657: 	 */
  658: 	Dir_Init();		/* Initialize directory structures so -I flags
  659: 				 * can be processed correctly */
  660: 	Parse_Init();		/* Need to initialize the paths of #include
  661: 				 * directories */
  662: 	Var_Init();		/* As well as the lists of variables for
  663: 				 * parsing arguments */
  664:         str_init();
  665: 	if (objdir != curdir)
  666: 		Dir_AddDir(dirSearchPath, curdir);
  667: 	Var_Set(".CURDIR", curdir, VAR_GLOBAL);
  668: 	Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
  669: 
  670: 	/*
  671: 	 * Initialize various variables.
  672: 	 *	MAKE also gets this name, for compatibility
  673: 	 *	.MAKEFLAGS gets set to the empty string just in case.
  674: 	 *	MFLAGS also gets initialized empty, for compatibility.
  675: 	 */
  676: 	Var_Set("MAKE", argv[0], VAR_GLOBAL);
  677: 	Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
  678: 	Var_Set("MFLAGS", "", VAR_GLOBAL);
  679: 	Var_Set("MACHINE", machine, VAR_GLOBAL);
  680: 	Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
  681: 	Var_Set("MACHINE_CPU", machine_cpu, VAR_GLOBAL);
  682: 
  683: 	/*
  684: 	 * First snag any flags out of the MAKE environment variable.
  685: 	 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
  686: 	 * in a different format).
  687: 	 */
  688: #ifdef POSIX
  689: 	Main_ParseArgLine(getenv("MAKEFLAGS"));
  690: #else
  691: 	Main_ParseArgLine(getenv("MAKE"));
  692: #endif
  693: 
  694: 	MainParseArgs(argc, argv);
  695: 
  696: 	/*
  697: 	 * Be compatible if user did not specify -j and did not explicitly
  698: 	 * turned compatibility on
  699: 	 */
  700: 	if (!compatMake && !forceJobs)
  701: 		compatMake = TRUE;
  702: 
  703: 	/*
  704: 	 * Initialize archive, target and suffix modules in preparation for
  705: 	 * parsing the makefile(s)
  706: 	 */
  707: 	Arch_Init();
  708: 	Targ_Init();
  709: 	Suff_Init();
  710: 
  711: 	DEFAULT = NILGNODE;
  712: 	(void)time(&now);
  713: 
  714: 	/*
  715: 	 * Set up the .TARGETS variable to contain the list of targets to be
  716: 	 * created. If none specified, make the variable empty -- the parser
  717: 	 * will fill the thing in with the default or .MAIN target.
  718: 	 */
  719: 	if (!Lst_IsEmpty(create)) {
  720: 		LstNode ln;
  721: 
  722: 		for (ln = Lst_First(create); ln != NILLNODE;
  723: 		    ln = Lst_Succ(ln)) {
  724: 			char *name = (char *)Lst_Datum(ln);
  725: 
  726: 			Var_Append(".TARGETS", name, VAR_GLOBAL);
  727: 		}
  728: 	} else
  729: 		Var_Set(".TARGETS", "", VAR_GLOBAL);
  730: 
  731: 
  732: 	/*
  733: 	 * If no user-supplied system path was given (through the -m option)
  734: 	 * add the directories from the DEFSYSPATH (more than one may be given
  735: 	 * as dir1:...:dirn) to the system include path.
  736: 	 */
  737: 	if (Lst_IsEmpty(sysIncPath)) {
  738: 		for (start = syspath; *start != '\0'; start = cp) {
  739: 			for (cp = start; *cp != '\0' && *cp != ':'; cp++)
  740: 				continue;
  741: 			if (*cp == '\0') {
  742: 				Dir_AddDir(sysIncPath, start);
  743: 			} else {
  744: 				*cp++ = '\0';
  745: 				Dir_AddDir(sysIncPath, start);
  746: 			}
  747: 		}
  748: 	}
  749: 
  750: 	/*
  751: 	 * Read in the built-in rules first, followed by the specified
  752: 	 * makefile, if it was (makefile != (char *) NULL), or the default
  753: 	 * Makefile and makefile, in that order, if it wasn't.
  754: 	 */
  755: 	if (!noBuiltins) {
  756: 		LstNode ln;
  757: 
  758: 		sysMkPath = Lst_Init (FALSE);
  759: 		Dir_Expand (_PATH_DEFSYSMK, sysIncPath, sysMkPath);
  760: 		if (Lst_IsEmpty(sysMkPath))
  761: 			Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
  762: 		ln = Lst_Find(sysMkPath, (ClientData)NULL, ReadMakefile);
  763: 		if (ln != NILLNODE)
  764: 			Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
  765: 	}
  766: 
  767: 	if (!Lst_IsEmpty(makefiles)) {
  768: 		LstNode ln;
  769: 
  770: 		ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile);
  771: 		if (ln != NILLNODE)
  772: 			Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
  773: 	} else if (!ReadMakefile("makefile", NULL))
  774: 		(void)ReadMakefile("Makefile", NULL);
  775: 
  776: 	(void)ReadMakefile(".depend", NULL);
  777: 
  778: 	Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
  779: 	efree(p1);
  780: 
  781: 	/* Install all the flags into the MAKE envariable. */
  782: 	if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p)
  783: #ifdef POSIX
  784: 		setenv("MAKEFLAGS", p, 1);
  785: #else
  786: 		setenv("MAKE", p, 1);
  787: #endif
  788: 	efree(p1);
  789: 
  790: 	/*
  791: 	 * For compatibility, look at the directories in the VPATH variable
  792: 	 * and add them to the search path, if the variable is defined. The
  793: 	 * variable's value is in the same format as the PATH envariable, i.e.
  794: 	 * <directory>:<directory>:<directory>...
  795: 	 */
  796: 	if (Var_Exists("VPATH", VAR_CMD)) {
  797: 		char *vpath, *path, *cp, savec;
  798: 		/*
  799: 		 * GCC stores string constants in read-only memory, but
  800: 		 * Var_Subst will want to write this thing, so store it
  801: 		 * in an array
  802: 		 */
  803: 		static char VPATH[] = "${VPATH}";
  804: 
  805: 		vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
  806: 		path = vpath;
  807: 		do {
  808: 			/* skip to end of directory */
  809: 			for (cp = path; *cp != ':' && *cp != '\0'; cp++)
  810: 				continue;
  811: 			/* Save terminator character so know when to stop */
  812: 			savec = *cp;
  813: 			*cp = '\0';
  814: 			/* Add directory to search path */
  815: 			Dir_AddDir(dirSearchPath, path);
  816: 			*cp = savec;
  817: 			path = cp + 1;
  818: 		} while (savec == ':');
  819: 		(void)free((Address)vpath);
  820: 	}
  821: 
  822: 	/*
  823: 	 * Now that all search paths have been read for suffixes et al, it's
  824: 	 * time to add the default search path to their lists...
  825: 	 */
  826: 	Suff_DoPaths();
  827: 
  828: 	/* print the initial graph, if the user requested it */
  829: 	if (DEBUG(GRAPH1))
  830: 		Targ_PrintGraph(1);
  831: 
  832: 	/* print the values of any variables requested by the user */
  833: 	if (printVars) {
  834: 		LstNode ln;
  835: 
  836: 		for (ln = Lst_First(variables); ln != NILLNODE;
  837: 		    ln = Lst_Succ(ln)) {
  838: 			char *value;
  839: 			if (expandVars) {
  840: 				p1 = malloc(strlen((char *)Lst_Datum(ln)) + 1 + 3);
  841: 				if (!p1)
  842: 					Punt("make: cannot allocate memory.");
  843: 				/* This sprintf is safe, because of the malloc above */
  844: 				(void)sprintf(p1, "${%s}", (char *)Lst_Datum(ln));
  845: 				value = Var_Subst(NULL, p1, VAR_GLOBAL, FALSE);
  846: 			} else {
  847: 				value = Var_Value((char *)Lst_Datum(ln),
  848: 						  VAR_GLOBAL, &p1);
  849: 			}
  850: 			printf("%s\n", value ? value : "");
  851: 			if (p1)
  852: 				free(p1);
  853: 		}
  854: 	}
  855: 
  856: 	/*
  857: 	 * Have now read the entire graph and need to make a list of targets
  858: 	 * to create. If none was given on the command line, we consult the
  859: 	 * parsing module to find the main target(s) to create.
  860: 	 */
  861: 	if (Lst_IsEmpty(create))
  862: 		targs = Parse_MainName();
  863: 	else
  864: 		targs = Targ_FindList(create, TARG_CREATE);
  865: 
  866: 	if (!compatMake && !printVars) {
  867: 		/*
  868: 		 * Initialize job module before traversing the graph, now that
  869: 		 * any .BEGIN and .END targets have been read.  This is done
  870: 		 * only if the -q flag wasn't given (to prevent the .BEGIN from
  871: 		 * being executed should it exist).
  872: 		 */
  873: 		if (!queryFlag) {
  874: 			if (maxLocal == -1)
  875: 				maxLocal = maxJobs;
  876: 			Job_Init(maxJobs, maxLocal);
  877: 			jobsRunning = TRUE;
  878: 		}
  879: 
  880: 		/* Traverse the graph, checking on all the targets */
  881: 		outOfDate = Make_Run(targs);
  882: 	} else if (!printVars) {
  883: 		/*
  884: 		 * Compat_Init will take care of creating all the targets as
  885: 		 * well as initializing the module.
  886: 		 */
  887: 		Compat_Run(targs);
  888: 	}
  889: 
  890: 	Lst_Destroy(targs, NOFREE);
  891: 	Lst_Destroy(variables, NOFREE);
  892: 	Lst_Destroy(makefiles, NOFREE);
  893: 	Lst_Destroy(create, (void (*)(ClientData)) free);
  894: 
  895: 	/* print the graph now it's been processed if the user requested it */
  896: 	if (DEBUG(GRAPH2))
  897: 		Targ_PrintGraph(2);
  898: 
  899: 	Suff_End();
  900:         Targ_End();
  901: 	Arch_End();
  902: 	str_end();
  903: 	Var_End();
  904: 	Parse_End();
  905: 	Dir_End();
  906: 
  907: 	if (queryFlag && outOfDate)
  908: 		return(1);
  909: 	else
  910: 		return(0);
  911: }
  912: 
  913: /*-
  914:  * ReadMakefile  --
  915:  *	Open and parse the given makefile.
  916:  *
  917:  * Results:
  918:  *	TRUE if ok. FALSE if couldn't open file.
  919:  *
  920:  * Side Effects:
  921:  *	lots
  922:  */
  923: static Boolean
  924: ReadMakefile(p, q)
  925: 	ClientData p, q;
  926: {
  927: 	char *fname = p;		/* makefile to read */
  928: 	extern Lst parseIncPath;
  929: 	FILE *stream;
  930: 	char *name, path[MAXPATHLEN + 1];
  931: 	char *MAKEFILE;
  932: 	int setMAKEFILE;
  933: 
  934: 	if (!strcmp(fname, "-")) {
  935: 		Parse_File("(stdin)", stdin);
  936: 		Var_Set("MAKEFILE", "", VAR_GLOBAL);
  937: 	} else {
  938: 		setMAKEFILE = strcmp(fname, ".depend");
  939: 
  940: 		/* if we've chdir'd, rebuild the path name */
  941: 		if (curdir != objdir && *fname != '/') {
  942: 			(void)snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
  943: 			/*
  944: 			 * XXX The realpath stuff breaks relative includes
  945: 			 * XXX in some cases.   The problem likely is in
  946: 			 * XXX parse.c where it does special things in
  947: 			 * XXX ParseDoInclude if the file is relateive
  948: 			 * XXX or absolute and not a system file.  There
  949: 			 * XXX it assumes that if the current file that's
  950: 			 * XXX being included is absolute, that any files
  951: 			 * XXX that it includes shouldn't do the -I path
  952: 			 * XXX stuff, which is inconsistant with historical
  953: 			 * XXX behavior.  However, I can't pentrate the mists
  954: 			 * XXX further, so I'm putting this workaround in
  955: 			 * XXX here until such time as the underlying bug
  956: 			 * XXX can be fixed.
  957: 			 */
  958: #if THIS_BREAKS_THINGS
  959: 			if (realpath(path, path) != NULL &&
  960: 			    (stream = fopen(path, "r")) != NULL) {
  961: 				MAKEFILE = fname;
  962: 				fname = path;
  963: 				goto found;
  964: 			}
  965: 		} else if (realpath(fname, path) != NULL) {
  966: 			MAKEFILE = fname;
  967: 			fname = path;
  968: 			if ((stream = fopen(fname, "r")) != NULL)
  969: 				goto found;
  970: 		}
  971: #else
  972: 			if ((stream = fopen(path, "r")) != NULL) {
  973: 				MAKEFILE = fname;
  974: 				fname = path;
  975: 				goto found;
  976: 			}
  977: 		} else {
  978: 			MAKEFILE = fname;
  979: 			if ((stream = fopen(fname, "r")) != NULL)
  980: 				goto found;
  981: 		}
  982: #endif
  983: 		/* look in -I and system include directories. */
  984: 		name = Dir_FindFile(fname, parseIncPath);
  985: 		if (!name)
  986: 			name = Dir_FindFile(fname, sysIncPath);
  987: 		if (!name || !(stream = fopen(name, "r")))
  988: 			return(FALSE);
  989: 		MAKEFILE = fname = name;
  990: 		/*
  991: 		 * set the MAKEFILE variable desired by System V fans -- the
  992: 		 * placement of the setting here means it gets set to the last
  993: 		 * makefile specified, as it is set by SysV make.
  994: 		 */
  995: found:
  996: 		if (setMAKEFILE)
  997: 			Var_Set("MAKEFILE", MAKEFILE, VAR_GLOBAL);
  998: 		Parse_File(fname, stream);
  999: 		(void)fclose(stream);
 1000: 	}
 1001: 	return(TRUE);
 1002: }
 1003: 
 1004: /*-
 1005:  * Cmd_Exec --
 1006:  *	Execute the command in cmd, and return the output of that command
 1007:  *	in a string.
 1008:  *
 1009:  * Results:
 1010:  *	A string containing the output of the command, or the empty string
 1011:  *	If err is not NULL, it contains the reason for the command failure
 1012:  *
 1013:  * Side Effects:
 1014:  *	The string must be freed by the caller.
 1015:  */
 1016: char *
 1017: Cmd_Exec(cmd, err)
 1018:     char *cmd;
 1019:     char **err;
 1020: {
 1021:     char	*args[4];   	/* Args for invoking the shell */
 1022:     int 	fds[2];	    	/* Pipe streams */
 1023:     int 	cpid;	    	/* Child PID */
 1024:     int 	pid;	    	/* PID from wait() */
 1025:     char	*res;		/* result */
 1026:     int		status;		/* command exit status */
 1027:     Buffer	buf;		/* buffer to store the result */
 1028:     char	*cp;
 1029:     int		cc;
 1030: 
 1031: 
 1032:     *err = NULL;
 1033: 
 1034:     /*
 1035:      * Set up arguments for shell
 1036:      */
 1037:     args[0] = "sh";
 1038:     args[1] = "-c";
 1039:     args[2] = cmd;
 1040:     args[3] = NULL;
 1041: 
 1042:     /*
 1043:      * Open a pipe for fetching its output
 1044:      */
 1045:     if (pipe(fds) == -1) {
 1046: 	*err = "Couldn't create pipe for \"%s\"";
 1047: 	goto bad;
 1048:     }
 1049: 
 1050:     /*
 1051:      * Fork
 1052:      */
 1053:     switch (cpid = vfork()) {
 1054:     case 0:
 1055: 	/*
 1056: 	 * Close input side of pipe
 1057: 	 */
 1058: 	(void) close(fds[0]);
 1059: 
 1060: 	/*
 1061: 	 * Duplicate the output stream to the shell's output, then
 1062: 	 * shut the extra thing down. Note we don't fetch the error
 1063: 	 * stream...why not? Why?
 1064: 	 */
 1065: 	(void) dup2(fds[1], 1);
 1066: 	(void) close(fds[1]);
 1067: 
 1068: 	(void) execv("/bin/sh", args);
 1069: 	_exit(1);
 1070: 	/*NOTREACHED*/
 1071: 
 1072:     case -1:
 1073: 	*err = "Couldn't exec \"%s\"";
 1074: 	goto bad;
 1075: 
 1076:     default:
 1077: 	/*
 1078: 	 * No need for the writing half
 1079: 	 */
 1080: 	(void) close(fds[1]);
 1081: 
 1082: 	buf = Buf_Init (MAKE_BSIZE);
 1083: 
 1084: 	do {
 1085: 	    char   result[BUFSIZ];
 1086: 	    cc = read(fds[0], result, sizeof(result));
 1087: 	    if (cc > 0)
 1088: 		Buf_AddBytes(buf, cc, (Byte *) result);
 1089: 	}
 1090: 	while (cc > 0 || (cc == -1 && errno == EINTR));
 1091: 
 1092: 	/*
 1093: 	 * Close the input side of the pipe.
 1094: 	 */
 1095: 	(void) close(fds[0]);
 1096: 
 1097: 	/*
 1098: 	 * Wait for the process to exit.
 1099: 	 */
 1100: 	while(((pid = wait(&status)) != cpid) && (pid >= 0))
 1101: 	    continue;
 1102: 
 1103: 	if (cc == -1)
 1104: 	    *err = "Error reading shell's output for \"%s\"";
 1105: 
 1106: 	res = (char *)Buf_GetAll (buf, &cc);
 1107: 	Buf_Destroy (buf, FALSE);
 1108: 
 1109: 	if (status)
 1110: 	    *err = "\"%s\" returned non-zero status";
 1111: 
 1112: 	/*
 1113: 	 * Null-terminate the result, convert newlines to spaces and
 1114: 	 * install it in the variable.
 1115: 	 */
 1116: 	res[cc] = '\0';
 1117: 	cp = &res[cc] - 1;
 1118: 
 1119: 	if (*cp == '\n') {
 1120: 	    /*
 1121: 	     * A final newline is just stripped
 1122: 	     */
 1123: 	    *cp-- = '\0';
 1124: 	}
 1125: 	while (cp >= res) {
 1126: 	    if (*cp == '\n') {
 1127: 		*cp = ' ';
 1128: 	    }
 1129: 	    cp--;
 1130: 	}
 1131: 	break;
 1132:     }
 1133:     return res;
 1134: bad:
 1135:     res = emalloc(1);
 1136:     *res = '\0';
 1137:     return res;
 1138: }
 1139: 
 1140: /*-
 1141:  * Error --
 1142:  *	Print an error message given its format.
 1143:  *
 1144:  * Results:
 1145:  *	None.
 1146:  *
 1147:  * Side Effects:
 1148:  *	The message is printed.
 1149:  */
 1150: /* VARARGS */
 1151: void
 1152: #ifdef __STDC__
 1153: Error(char *fmt, ...)
 1154: #else
 1155: Error(va_alist)
 1156: 	va_dcl
 1157: #endif
 1158: {
 1159: 	va_list ap;
 1160: #ifdef __STDC__
 1161: 	va_start(ap, fmt);
 1162: #else
 1163: 	char *fmt;
 1164: 
 1165: 	va_start(ap);
 1166: 	fmt = va_arg(ap, char *);
 1167: #endif
 1168: 	(void)vfprintf(stderr, fmt, ap);
 1169: 	va_end(ap);
 1170: 	(void)fprintf(stderr, "\n");
 1171: 	(void)fflush(stderr);
 1172: }
 1173: 
 1174: /*-
 1175:  * Fatal --
 1176:  *	Produce a Fatal error message. If jobs are running, waits for them
 1177:  *	to finish.
 1178:  *
 1179:  * Results:
 1180:  *	None
 1181:  *
 1182:  * Side Effects:
 1183:  *	The program exits
 1184:  */
 1185: /* VARARGS */
 1186: void
 1187: #ifdef __STDC__
 1188: Fatal(char *fmt, ...)
 1189: #else
 1190: Fatal(va_alist)
 1191: 	va_dcl
 1192: #endif
 1193: {
 1194: 	va_list ap;
 1195: #ifdef __STDC__
 1196: 	va_start(ap, fmt);
 1197: #else
 1198: 	char *fmt;
 1199: 
 1200: 	va_start(ap);
 1201: 	fmt = va_arg(ap, char *);
 1202: #endif
 1203: 	if (jobsRunning)
 1204: 		Job_Wait();
 1205: 
 1206: 	(void)vfprintf(stderr, fmt, ap);
 1207: 	va_end(ap);
 1208: 	(void)fprintf(stderr, "\n");
 1209: 	(void)fflush(stderr);
 1210: 
 1211: 	if (DEBUG(GRAPH2))
 1212: 		Targ_PrintGraph(2);
 1213: 	exit(2);		/* Not 1 so -q can distinguish error */
 1214: }
 1215: 
 1216: /*
 1217:  * Punt --
 1218:  *	Major exception once jobs are being created. Kills all jobs, prints
 1219:  *	a message and exits.
 1220:  *
 1221:  * Results:
 1222:  *	None
 1223:  *
 1224:  * Side Effects:
 1225:  *	All children are killed indiscriminately and the program Lib_Exits
 1226:  */
 1227: /* VARARGS */
 1228: void
 1229: #ifdef __STDC__
 1230: Punt(char *fmt, ...)
 1231: #else
 1232: Punt(va_alist)
 1233: 	va_dcl
 1234: #endif
 1235: {
 1236: 	va_list ap;
 1237: #if __STDC__
 1238: 	va_start(ap, fmt);
 1239: #else
 1240: 	char *fmt;
 1241: 
 1242: 	va_start(ap);
 1243: 	fmt = va_arg(ap, char *);
 1244: #endif
 1245: 
 1246: 	(void)fprintf(stderr, "make: ");
 1247: 	(void)vfprintf(stderr, fmt, ap);
 1248: 	va_end(ap);
 1249: 	(void)fprintf(stderr, "\n");
 1250: 	(void)fflush(stderr);
 1251: 
 1252: 	DieHorribly();
 1253: }
 1254: 
 1255: /*-
 1256:  * DieHorribly --
 1257:  *	Exit without giving a message.
 1258:  *
 1259:  * Results:
 1260:  *	None
 1261:  *
 1262:  * Side Effects:
 1263:  *	A big one...
 1264:  */
 1265: void
 1266: DieHorribly()
 1267: {
 1268: 	if (jobsRunning)
 1269: 		Job_AbortAll();
 1270: 	if (DEBUG(GRAPH2))
 1271: 		Targ_PrintGraph(2);
 1272: 	exit(2);		/* Not 1, so -q can distinguish error */
 1273: }
 1274: 
 1275: /*
 1276:  * Finish --
 1277:  *	Called when aborting due to errors in child shell to signal
 1278:  *	abnormal exit.
 1279:  *
 1280:  * Results:
 1281:  *	None
 1282:  *
 1283:  * Side Effects:
 1284:  *	The program exits
 1285:  */
 1286: void
 1287: Finish(errors)
 1288: 	int errors;	/* number of errors encountered in Make_Make */
 1289: {
 1290: 	Fatal("%d error%s", errors, errors == 1 ? "" : "s");
 1291: }
 1292: 
 1293: /*
 1294:  * emalloc --
 1295:  *	malloc, but die on error.
 1296:  */
 1297: void *
 1298: emalloc(len)
 1299: 	size_t len;
 1300: {
 1301: 	void *p;
 1302: 
 1303: 	if ((p = malloc(len)) == NULL)
 1304: 		enomem();
 1305: 	return(p);
 1306: }
 1307: 
 1308: /*
 1309:  * estrdup --
 1310:  *	strdup, but die on error.
 1311:  */
 1312: char *
 1313: estrdup(str)
 1314: 	const char *str;
 1315: {
 1316: 	char *p;
 1317: 
 1318: 	if ((p = strdup(str)) == NULL)
 1319: 		enomem();
 1320: 	return(p);
 1321: }
 1322: 
 1323: /*
 1324:  * erealloc --
 1325:  *	realloc, but die on error.
 1326:  */
 1327: void *
 1328: erealloc(ptr, size)
 1329: 	void *ptr;
 1330: 	size_t size;
 1331: {
 1332: 	if ((ptr = realloc(ptr, size)) == NULL)
 1333: 		enomem();
 1334: 	return(ptr);
 1335: }
 1336: 
 1337: /*
 1338:  * enomem --
 1339:  *	die when out of memory.
 1340:  */
 1341: void
 1342: enomem()
 1343: {
 1344: 	err(2, NULL);
 1345: }
 1346: 
 1347: /*
 1348:  * enunlink --
 1349:  *	Remove a file carefully, avoiding directories.
 1350:  */
 1351: int
 1352: eunlink(file)
 1353: 	const char *file;
 1354: {
 1355: 	struct stat st;
 1356: 
 1357: 	if (lstat(file, &st) == -1)
 1358: 		return -1;
 1359: 
 1360: 	if (S_ISDIR(st.st_mode)) {
 1361: 		errno = EISDIR;
 1362: 		return -1;
 1363: 	}
 1364: 	return unlink(file);
 1365: }
 1366: 
 1367: /*
 1368:  * usage --
 1369:  *	exit with usage message
 1370:  */
 1371: static void
 1372: usage()
 1373: {
 1374: 	(void)fprintf(stderr, "%s\n%s\n%s\n",
 1375: "usage: make [-BPSXeiknqrstv] [-C directory] [-D variable] [-d flags]",
 1376: "            [-E variable] [-f makefile] [-I directory] [-j max_jobs]",
 1377: "            [-m directory] [-V variable] [variable=value] [target ...]");
 1378: 	exit(2);
 1379: }
 1380: 
 1381: 
 1382: int
 1383: PrintAddr(a, b)
 1384:     ClientData a;
 1385:     ClientData b;
 1386: {
 1387:     printf("%lx ", (unsigned long) a);
 1388:     return b ? 0 : 0;
 1389: }