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