main.c revision 1.269 1 /* $NetBSD: main.c,v 1.269 2017/06/17 19:59:28 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.269 2017/06/17 19:59:28 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.269 2017/06/17 19:59:28 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", FALSE);
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 = Var_Value(var, VAR_GLOBAL, &p1);
884 if (!noExpand && value && *value == '$') {
885 value = Var_Subst(NULL, value,
886 VAR_GLOBAL, VARF_WANTRES);
887 free(p1);
888 p1 = value;
889 }
890 }
891 printf("%s\n", value ? value : "");
892 free(p1);
893 }
894 }
895
896 static Boolean
897 runTargets(void)
898 {
899 Lst targs; /* target nodes to create -- passed to Make_Init */
900 Boolean outOfDate; /* FALSE if all targets up to date */
901
902 /*
903 * Have now read the entire graph and need to make a list of
904 * targets to create. If none was given on the command line,
905 * we consult the parsing module to find the main target(s)
906 * to create.
907 */
908 if (Lst_IsEmpty(create))
909 targs = Parse_MainName();
910 else
911 targs = Targ_FindList(create, TARG_CREATE);
912
913 if (!compatMake) {
914 /*
915 * Initialize job module before traversing the graph
916 * now that any .BEGIN and .END targets have been read.
917 * This is done only if the -q flag wasn't given
918 * (to prevent the .BEGIN from being executed should
919 * it exist).
920 */
921 if (!queryFlag) {
922 Job_Init();
923 jobsRunning = TRUE;
924 }
925
926 /* Traverse the graph, checking on all the targets */
927 outOfDate = Make_Run(targs);
928 } else {
929 /*
930 * Compat_Init will take care of creating all the
931 * targets as well as initializing the module.
932 */
933 Compat_Run(targs);
934 outOfDate = FALSE;
935 }
936 Lst_Destroy(targs, NULL);
937 return outOfDate;
938 }
939
940 /*-
941 * main --
942 * The main function, for obvious reasons. Initializes variables
943 * and a few modules, then parses the arguments give it in the
944 * environment and on the command line. Reads the system makefile
945 * followed by either Makefile, makefile or the file given by the
946 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
947 * flags it has received by then uses either the Make or the Compat
948 * module to create the initial list of targets.
949 *
950 * Results:
951 * If -q was given, exits -1 if anything was out-of-date. Else it exits
952 * 0.
953 *
954 * Side Effects:
955 * The program exits when done. Targets are created. etc. etc. etc.
956 */
957 int
958 main(int argc, char **argv)
959 {
960 Boolean outOfDate; /* FALSE if all targets up to date */
961 struct stat sb, sa;
962 char *p1, *path;
963 char mdpath[MAXPATHLEN];
964 const char *machine = getenv("MACHINE");
965 const char *machine_arch = getenv("MACHINE_ARCH");
966 char *syspath = getenv("MAKESYSPATH");
967 Lst sysMkPath; /* Path of sys.mk */
968 char *cp = NULL, *start;
969 /* avoid faults on read-only strings */
970 static char defsyspath[] = _PATH_DEFSYSPATH;
971 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */
972 struct timeval rightnow; /* to initialize random seed */
973 struct utsname utsname;
974
975 /* default to writing debug to stderr */
976 debug_file = stderr;
977
978 #ifdef SIGINFO
979 (void)bmake_signal(SIGINFO, siginfo);
980 #endif
981 /*
982 * Set the seed to produce a different random sequence
983 * on each program execution.
984 */
985 gettimeofday(&rightnow, NULL);
986 srandom(rightnow.tv_sec + rightnow.tv_usec);
987
988 if ((progname = strrchr(argv[0], '/')) != NULL)
989 progname++;
990 else
991 progname = argv[0];
992 #if defined(MAKE_NATIVE) || (defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE))
993 /*
994 * get rid of resource limit on file descriptors
995 */
996 {
997 struct rlimit rl;
998 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
999 rl.rlim_cur != rl.rlim_max) {
1000 rl.rlim_cur = rl.rlim_max;
1001 (void)setrlimit(RLIMIT_NOFILE, &rl);
1002 }
1003 }
1004 #endif
1005
1006 if (uname(&utsname) == -1) {
1007 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
1008 strerror(errno));
1009 exit(2);
1010 }
1011
1012 /*
1013 * Get the name of this type of MACHINE from utsname
1014 * so we can share an executable for similar machines.
1015 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
1016 *
1017 * Note that both MACHINE and MACHINE_ARCH are decided at
1018 * run-time.
1019 */
1020 if (!machine) {
1021 #ifdef MAKE_NATIVE
1022 machine = utsname.machine;
1023 #else
1024 #ifdef MAKE_MACHINE
1025 machine = MAKE_MACHINE;
1026 #else
1027 machine = "unknown";
1028 #endif
1029 #endif
1030 }
1031
1032 if (!machine_arch) {
1033 #ifdef MAKE_NATIVE
1034 static char machine_arch_buf[sizeof(utsname.machine)];
1035 const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
1036 size_t len = sizeof(machine_arch_buf);
1037
1038 if (sysctl(mib, __arraycount(mib), machine_arch_buf,
1039 &len, NULL, 0) < 0) {
1040 (void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
1041 strerror(errno));
1042 exit(2);
1043 }
1044
1045 machine_arch = machine_arch_buf;
1046 #else
1047 #ifndef MACHINE_ARCH
1048 #ifdef MAKE_MACHINE_ARCH
1049 machine_arch = MAKE_MACHINE_ARCH;
1050 #else
1051 machine_arch = "unknown";
1052 #endif
1053 #else
1054 machine_arch = MACHINE_ARCH;
1055 #endif
1056 #endif
1057 }
1058
1059 myPid = getpid(); /* remember this for vFork() */
1060
1061 /*
1062 * Just in case MAKEOBJDIR wants us to do something tricky.
1063 */
1064 Var_Init(); /* Initialize the lists of variables for
1065 * parsing arguments */
1066 Var_Set(".MAKE.OS", utsname.sysname, VAR_GLOBAL, 0);
1067 Var_Set("MACHINE", machine, VAR_GLOBAL, 0);
1068 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL, 0);
1069 #ifdef MAKE_VERSION
1070 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL, 0);
1071 #endif
1072 Var_Set(".newline", "\n", VAR_GLOBAL, 0); /* handy for :@ loops */
1073 /*
1074 * This is the traditional preference for makefiles.
1075 */
1076 #ifndef MAKEFILE_PREFERENCE_LIST
1077 # define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
1078 #endif
1079 Var_Set(MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST,
1080 VAR_GLOBAL, 0);
1081 Var_Set(MAKE_DEPENDFILE, ".depend", VAR_GLOBAL, 0);
1082
1083 create = Lst_Init(FALSE);
1084 makefiles = Lst_Init(FALSE);
1085 printVars = FALSE;
1086 debugVflag = FALSE;
1087 variables = Lst_Init(FALSE);
1088 beSilent = FALSE; /* Print commands as executed */
1089 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
1090 noExecute = FALSE; /* Execute all commands */
1091 noRecursiveExecute = FALSE; /* Execute all .MAKE targets */
1092 keepgoing = FALSE; /* Stop on error */
1093 allPrecious = FALSE; /* Remove targets when interrupted */
1094 deleteOnError = FALSE; /* Historical default behavior */
1095 queryFlag = FALSE; /* This is not just a check-run */
1096 noBuiltins = FALSE; /* Read the built-in rules */
1097 touchFlag = FALSE; /* Actually update targets */
1098 debug = 0; /* No debug verbosity, please. */
1099 jobsRunning = FALSE;
1100
1101 maxJobs = DEFMAXLOCAL; /* Set default local max concurrency */
1102 maxJobTokens = maxJobs;
1103 compatMake = FALSE; /* No compat mode */
1104 ignorePWD = FALSE;
1105
1106 /*
1107 * Initialize the parsing, directory and variable modules to prepare
1108 * for the reading of inclusion paths and variable settings on the
1109 * command line
1110 */
1111
1112 /*
1113 * Initialize various variables.
1114 * MAKE also gets this name, for compatibility
1115 * .MAKEFLAGS gets set to the empty string just in case.
1116 * MFLAGS also gets initialized empty, for compatibility.
1117 */
1118 Parse_Init();
1119 if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) {
1120 /*
1121 * Leave alone if it is an absolute path, or if it does
1122 * not contain a '/' in which case we need to find it in
1123 * the path, like execvp(3) and the shells do.
1124 */
1125 p1 = argv[0];
1126 } else {
1127 /*
1128 * A relative path, canonicalize it.
1129 */
1130 p1 = cached_realpath(argv[0], mdpath);
1131 if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) {
1132 p1 = argv[0]; /* realpath failed */
1133 }
1134 }
1135 Var_Set("MAKE", p1, VAR_GLOBAL, 0);
1136 Var_Set(".MAKE", p1, VAR_GLOBAL, 0);
1137 Var_Set(MAKEFLAGS, "", VAR_GLOBAL, 0);
1138 Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL, 0);
1139 Var_Set("MFLAGS", "", VAR_GLOBAL, 0);
1140 Var_Set(".ALLTARGETS", "", VAR_GLOBAL, 0);
1141 /* some makefiles need to know this */
1142 Var_Set(MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV, VAR_CMD, 0);
1143
1144 /*
1145 * Set some other useful macros
1146 */
1147 {
1148 char tmp[64], *ep;
1149
1150 makelevel = ((ep = getenv(MAKE_LEVEL_ENV)) && *ep) ? atoi(ep) : 0;
1151 if (makelevel < 0)
1152 makelevel = 0;
1153 snprintf(tmp, sizeof(tmp), "%d", makelevel);
1154 Var_Set(MAKE_LEVEL, tmp, VAR_GLOBAL, 0);
1155 snprintf(tmp, sizeof(tmp), "%u", myPid);
1156 Var_Set(".MAKE.PID", tmp, VAR_GLOBAL, 0);
1157 snprintf(tmp, sizeof(tmp), "%u", getppid());
1158 Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL, 0);
1159 }
1160 if (makelevel > 0) {
1161 char pn[1024];
1162 snprintf(pn, sizeof(pn), "%s[%d]", progname, makelevel);
1163 progname = bmake_strdup(pn);
1164 }
1165
1166 #ifdef USE_META
1167 meta_init();
1168 #endif
1169 Dir_Init(NULL); /* Dir_* safe to call from MainParseArgs */
1170
1171 /*
1172 * First snag any flags out of the MAKE environment variable.
1173 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
1174 * in a different format).
1175 */
1176 #ifdef POSIX
1177 p1 = explode(getenv("MAKEFLAGS"));
1178 Main_ParseArgLine(p1);
1179 free(p1);
1180 #else
1181 Main_ParseArgLine(getenv("MAKE"));
1182 #endif
1183
1184 /*
1185 * Find where we are (now).
1186 * We take care of PWD for the automounter below...
1187 */
1188 if (getcwd(curdir, MAXPATHLEN) == NULL) {
1189 (void)fprintf(stderr, "%s: getcwd: %s.\n",
1190 progname, strerror(errno));
1191 exit(2);
1192 }
1193
1194 MainParseArgs(argc, argv);
1195
1196 if (enterFlag)
1197 printf("%s: Entering directory `%s'\n", progname, curdir);
1198
1199 /*
1200 * Verify that cwd is sane.
1201 */
1202 if (stat(curdir, &sa) == -1) {
1203 (void)fprintf(stderr, "%s: %s: %s.\n",
1204 progname, curdir, strerror(errno));
1205 exit(2);
1206 }
1207
1208 /*
1209 * All this code is so that we know where we are when we start up
1210 * on a different machine with pmake.
1211 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
1212 * since the value of curdir can vary depending on how we got
1213 * here. Ie sitting at a shell prompt (shell that provides $PWD)
1214 * or via subdir.mk in which case its likely a shell which does
1215 * not provide it.
1216 * So, to stop it breaking this case only, we ignore PWD if
1217 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform.
1218 */
1219 #ifndef NO_PWD_OVERRIDE
1220 if (!ignorePWD) {
1221 char *pwd, *ptmp1 = NULL, *ptmp2 = NULL;
1222
1223 if ((pwd = getenv("PWD")) != NULL &&
1224 Var_Value("MAKEOBJDIRPREFIX", VAR_CMD, &ptmp1) == NULL) {
1225 const char *makeobjdir = Var_Value("MAKEOBJDIR",
1226 VAR_CMD, &ptmp2);
1227
1228 if (makeobjdir == NULL || !strchr(makeobjdir, '$')) {
1229 if (stat(pwd, &sb) == 0 &&
1230 sa.st_ino == sb.st_ino &&
1231 sa.st_dev == sb.st_dev)
1232 (void)strncpy(curdir, pwd, MAXPATHLEN);
1233 }
1234 }
1235 free(ptmp1);
1236 free(ptmp2);
1237 }
1238 #endif
1239 Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0);
1240
1241 /*
1242 * Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that,
1243 * MAKEOBJDIR is set in the environment, try only that value
1244 * and fall back to .CURDIR if it does not exist.
1245 *
1246 * Otherwise, try _PATH_OBJDIR.MACHINE-MACHINE_ARCH, _PATH_OBJDIR.MACHINE,
1247 * and * finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none
1248 * of these paths exist, just use .CURDIR.
1249 */
1250 Dir_Init(curdir);
1251 (void)Main_SetObjdir("%s", curdir);
1252
1253 if (!Main_SetVarObjdir("MAKEOBJDIRPREFIX", curdir) &&
1254 !Main_SetVarObjdir("MAKEOBJDIR", "") &&
1255 !Main_SetObjdir("%s.%s-%s", _PATH_OBJDIR, machine, machine_arch) &&
1256 !Main_SetObjdir("%s.%s", _PATH_OBJDIR, machine) &&
1257 !Main_SetObjdir("%s", _PATH_OBJDIR))
1258 (void)Main_SetObjdir("%s%s", _PATH_OBJDIRPREFIX, curdir);
1259
1260 /*
1261 * Initialize archive, target and suffix modules in preparation for
1262 * parsing the makefile(s)
1263 */
1264 Arch_Init();
1265 Targ_Init();
1266 Suff_Init();
1267 Trace_Init(tracefile);
1268
1269 DEFAULT = NULL;
1270 (void)time(&now);
1271
1272 Trace_Log(MAKESTART, NULL);
1273
1274 /*
1275 * Set up the .TARGETS variable to contain the list of targets to be
1276 * created. If none specified, make the variable empty -- the parser
1277 * will fill the thing in with the default or .MAIN target.
1278 */
1279 if (!Lst_IsEmpty(create)) {
1280 LstNode ln;
1281
1282 for (ln = Lst_First(create); ln != NULL;
1283 ln = Lst_Succ(ln)) {
1284 char *name = (char *)Lst_Datum(ln);
1285
1286 Var_Append(".TARGETS", name, VAR_GLOBAL);
1287 }
1288 } else
1289 Var_Set(".TARGETS", "", VAR_GLOBAL, 0);
1290
1291
1292 /*
1293 * If no user-supplied system path was given (through the -m option)
1294 * add the directories from the DEFSYSPATH (more than one may be given
1295 * as dir1:...:dirn) to the system include path.
1296 */
1297 if (syspath == NULL || *syspath == '\0')
1298 syspath = defsyspath;
1299 else
1300 syspath = bmake_strdup(syspath);
1301
1302 for (start = syspath; *start != '\0'; start = cp) {
1303 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1304 continue;
1305 if (*cp == ':') {
1306 *cp++ = '\0';
1307 }
1308 /* look for magic parent directory search string */
1309 if (strncmp(".../", start, 4) != 0) {
1310 (void)Dir_AddDir(defIncPath, start);
1311 } else {
1312 if (Dir_FindHereOrAbove(curdir, start+4,
1313 found_path, sizeof(found_path))) {
1314 (void)Dir_AddDir(defIncPath, found_path);
1315 }
1316 }
1317 }
1318 if (syspath != defsyspath)
1319 free(syspath);
1320
1321 /*
1322 * Read in the built-in rules first, followed by the specified
1323 * makefile, if it was (makefile != NULL), or the default
1324 * makefile and Makefile, in that order, if it wasn't.
1325 */
1326 if (!noBuiltins) {
1327 LstNode ln;
1328
1329 sysMkPath = Lst_Init(FALSE);
1330 Dir_Expand(_PATH_DEFSYSMK,
1331 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath,
1332 sysMkPath);
1333 if (Lst_IsEmpty(sysMkPath))
1334 Fatal("%s: no system rules (%s).", progname,
1335 _PATH_DEFSYSMK);
1336 ln = Lst_Find(sysMkPath, NULL, ReadMakefile);
1337 if (ln == NULL)
1338 Fatal("%s: cannot open %s.", progname,
1339 (char *)Lst_Datum(ln));
1340 }
1341
1342 if (!Lst_IsEmpty(makefiles)) {
1343 LstNode ln;
1344
1345 ln = Lst_Find(makefiles, NULL, ReadAllMakefiles);
1346 if (ln != NULL)
1347 Fatal("%s: cannot open %s.", progname,
1348 (char *)Lst_Datum(ln));
1349 } else {
1350 p1 = Var_Subst(NULL, "${" MAKEFILE_PREFERENCE "}",
1351 VAR_CMD, VARF_WANTRES);
1352 if (p1) {
1353 (void)str2Lst_Append(makefiles, p1, NULL);
1354 (void)Lst_Find(makefiles, NULL, ReadMakefile);
1355 free(p1);
1356 }
1357 }
1358
1359 /* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
1360 if (!noBuiltins || !printVars) {
1361 makeDependfile = Var_Subst(NULL, "${.MAKE.DEPENDFILE:T}",
1362 VAR_CMD, VARF_WANTRES);
1363 doing_depend = TRUE;
1364 (void)ReadMakefile(makeDependfile, NULL);
1365 doing_depend = FALSE;
1366 }
1367
1368 if (enterFlagObj)
1369 printf("%s: Entering directory `%s'\n", progname, objdir);
1370
1371 MakeMode(NULL);
1372
1373 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
1374 free(p1);
1375
1376 if (!forceJobs && !compatMake &&
1377 Var_Exists(".MAKE.JOBS", VAR_GLOBAL)) {
1378 char *value;
1379 int n;
1380
1381 value = Var_Subst(NULL, "${.MAKE.JOBS}", VAR_GLOBAL, VARF_WANTRES);
1382 n = strtol(value, NULL, 0);
1383 if (n < 1) {
1384 (void)fprintf(stderr, "%s: illegal value for .MAKE.JOBS -- must be positive integer!\n",
1385 progname);
1386 exit(1);
1387 }
1388 if (n != maxJobs) {
1389 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
1390 Var_Append(MAKEFLAGS, value, VAR_GLOBAL);
1391 }
1392 maxJobs = n;
1393 maxJobTokens = maxJobs;
1394 forceJobs = TRUE;
1395 free(value);
1396 }
1397
1398 /*
1399 * Be compatible if user did not specify -j and did not explicitly
1400 * turned compatibility on
1401 */
1402 if (!compatMake && !forceJobs) {
1403 compatMake = TRUE;
1404 }
1405
1406 if (!compatMake)
1407 Job_ServerStart(maxJobTokens, jp_0, jp_1);
1408 if (DEBUG(JOB))
1409 fprintf(debug_file, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
1410 jp_0, jp_1, maxJobs, maxJobTokens, compatMake);
1411
1412 if (!printVars)
1413 Main_ExportMAKEFLAGS(TRUE); /* initial export */
1414
1415
1416 /*
1417 * For compatibility, look at the directories in the VPATH variable
1418 * and add them to the search path, if the variable is defined. The
1419 * variable's value is in the same format as the PATH envariable, i.e.
1420 * <directory>:<directory>:<directory>...
1421 */
1422 if (Var_Exists("VPATH", VAR_CMD)) {
1423 char *vpath, savec;
1424 /*
1425 * GCC stores string constants in read-only memory, but
1426 * Var_Subst will want to write this thing, so store it
1427 * in an array
1428 */
1429 static char VPATH[] = "${VPATH}";
1430
1431 vpath = Var_Subst(NULL, VPATH, VAR_CMD, VARF_WANTRES);
1432 path = vpath;
1433 do {
1434 /* skip to end of directory */
1435 for (cp = path; *cp != ':' && *cp != '\0'; cp++)
1436 continue;
1437 /* Save terminator character so know when to stop */
1438 savec = *cp;
1439 *cp = '\0';
1440 /* Add directory to search path */
1441 (void)Dir_AddDir(dirSearchPath, path);
1442 *cp = savec;
1443 path = cp + 1;
1444 } while (savec == ':');
1445 free(vpath);
1446 }
1447
1448 /*
1449 * Now that all search paths have been read for suffixes et al, it's
1450 * time to add the default search path to their lists...
1451 */
1452 Suff_DoPaths();
1453
1454 /*
1455 * Propagate attributes through :: dependency lists.
1456 */
1457 Targ_Propagate();
1458
1459 /* print the initial graph, if the user requested it */
1460 if (DEBUG(GRAPH1))
1461 Targ_PrintGraph(1);
1462
1463 /* print the values of any variables requested by the user */
1464 if (printVars) {
1465 doPrintVars();
1466 outOfDate = FALSE;
1467 } else {
1468 outOfDate = runTargets();
1469 }
1470
1471 #ifdef CLEANUP
1472 Lst_Destroy(variables, NULL);
1473 Lst_Destroy(makefiles, NULL);
1474 Lst_Destroy(create, (FreeProc *)free);
1475 #endif
1476
1477 /* print the graph now it's been processed if the user requested it */
1478 if (DEBUG(GRAPH2))
1479 Targ_PrintGraph(2);
1480
1481 Trace_Log(MAKEEND, 0);
1482
1483 if (enterFlagObj)
1484 printf("%s: Leaving directory `%s'\n", progname, objdir);
1485 if (enterFlag)
1486 printf("%s: Leaving directory `%s'\n", progname, curdir);
1487
1488 #ifdef USE_META
1489 meta_finish();
1490 #endif
1491 Suff_End();
1492 Targ_End();
1493 Arch_End();
1494 Var_End();
1495 Parse_End();
1496 Dir_End();
1497 Job_End();
1498 Trace_End();
1499
1500 return outOfDate ? 1 : 0;
1501 }
1502
1503 /*-
1504 * ReadMakefile --
1505 * Open and parse the given makefile.
1506 *
1507 * Results:
1508 * 0 if ok. -1 if couldn't open file.
1509 *
1510 * Side Effects:
1511 * lots
1512 */
1513 static int
1514 ReadMakefile(const void *p, const void *q MAKE_ATTR_UNUSED)
1515 {
1516 const char *fname = p; /* makefile to read */
1517 int fd;
1518 size_t len = MAXPATHLEN;
1519 char *name, *path = bmake_malloc(len);
1520
1521 if (!strcmp(fname, "-")) {
1522 Parse_File(NULL /*stdin*/, -1);
1523 Var_Set("MAKEFILE", "", VAR_INTERNAL, 0);
1524 } else {
1525 /* if we've chdir'd, rebuild the path name */
1526 if (strcmp(curdir, objdir) && *fname != '/') {
1527 size_t plen = strlen(curdir) + strlen(fname) + 2;
1528 if (len < plen)
1529 path = bmake_realloc(path, len = 2 * plen);
1530
1531 (void)snprintf(path, len, "%s/%s", curdir, fname);
1532 fd = open(path, O_RDONLY);
1533 if (fd != -1) {
1534 fname = path;
1535 goto found;
1536 }
1537
1538 /* If curdir failed, try objdir (ala .depend) */
1539 plen = strlen(objdir) + strlen(fname) + 2;
1540 if (len < plen)
1541 path = bmake_realloc(path, len = 2 * plen);
1542 (void)snprintf(path, len, "%s/%s", objdir, fname);
1543 fd = open(path, O_RDONLY);
1544 if (fd != -1) {
1545 fname = path;
1546 goto found;
1547 }
1548 } else {
1549 fd = open(fname, O_RDONLY);
1550 if (fd != -1)
1551 goto found;
1552 }
1553 /* look in -I and system include directories. */
1554 name = Dir_FindFile(fname, parseIncPath);
1555 if (!name)
1556 name = Dir_FindFile(fname,
1557 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
1558 if (!name || (fd = open(name, O_RDONLY)) == -1) {
1559 free(name);
1560 free(path);
1561 return(-1);
1562 }
1563 fname = name;
1564 /*
1565 * set the MAKEFILE variable desired by System V fans -- the
1566 * placement of the setting here means it gets set to the last
1567 * makefile specified, as it is set by SysV make.
1568 */
1569 found:
1570 if (!doing_depend)
1571 Var_Set("MAKEFILE", fname, VAR_INTERNAL, 0);
1572 Parse_File(fname, fd);
1573 }
1574 free(path);
1575 return(0);
1576 }
1577
1578
1579
1580 /*-
1581 * Cmd_Exec --
1582 * Execute the command in cmd, and return the output of that command
1583 * in a string.
1584 *
1585 * Results:
1586 * A string containing the output of the command, or the empty string
1587 * If errnum is not NULL, it contains the reason for the command failure
1588 *
1589 * Side Effects:
1590 * The string must be freed by the caller.
1591 */
1592 char *
1593 Cmd_Exec(const char *cmd, const char **errnum)
1594 {
1595 const char *args[4]; /* Args for invoking the shell */
1596 int fds[2]; /* Pipe streams */
1597 int cpid; /* Child PID */
1598 int pid; /* PID from wait() */
1599 char *res; /* result */
1600 int status; /* command exit status */
1601 Buffer buf; /* buffer to store the result */
1602 char *cp;
1603 int cc; /* bytes read, or -1 */
1604 int savederr; /* saved errno */
1605
1606
1607 *errnum = NULL;
1608
1609 if (!shellName)
1610 Shell_Init();
1611 /*
1612 * Set up arguments for shell
1613 */
1614 args[0] = shellName;
1615 args[1] = "-c";
1616 args[2] = cmd;
1617 args[3] = NULL;
1618
1619 /*
1620 * Open a pipe for fetching its output
1621 */
1622 if (pipe(fds) == -1) {
1623 *errnum = "Couldn't create pipe for \"%s\"";
1624 goto bad;
1625 }
1626
1627 /*
1628 * Fork
1629 */
1630 switch (cpid = vFork()) {
1631 case 0:
1632 /*
1633 * Close input side of pipe
1634 */
1635 (void)close(fds[0]);
1636
1637 /*
1638 * Duplicate the output stream to the shell's output, then
1639 * shut the extra thing down. Note we don't fetch the error
1640 * stream...why not? Why?
1641 */
1642 (void)dup2(fds[1], 1);
1643 (void)close(fds[1]);
1644
1645 Var_ExportVars();
1646
1647 (void)execv(shellPath, UNCONST(args));
1648 _exit(1);
1649 /*NOTREACHED*/
1650
1651 case -1:
1652 *errnum = "Couldn't exec \"%s\"";
1653 goto bad;
1654
1655 default:
1656 /*
1657 * No need for the writing half
1658 */
1659 (void)close(fds[1]);
1660
1661 savederr = 0;
1662 Buf_Init(&buf, 0);
1663
1664 do {
1665 char result[BUFSIZ];
1666 cc = read(fds[0], result, sizeof(result));
1667 if (cc > 0)
1668 Buf_AddBytes(&buf, cc, result);
1669 }
1670 while (cc > 0 || (cc == -1 && errno == EINTR));
1671 if (cc == -1)
1672 savederr = errno;
1673
1674 /*
1675 * Close the input side of the pipe.
1676 */
1677 (void)close(fds[0]);
1678
1679 /*
1680 * Wait for the process to exit.
1681 */
1682 while(((pid = waitpid(cpid, &status, 0)) != cpid) && (pid >= 0)) {
1683 JobReapChild(pid, status, FALSE);
1684 continue;
1685 }
1686 cc = Buf_Size(&buf);
1687 res = Buf_Destroy(&buf, FALSE);
1688
1689 if (savederr != 0)
1690 *errnum = "Couldn't read shell's output for \"%s\"";
1691
1692 if (WIFSIGNALED(status))
1693 *errnum = "\"%s\" exited on a signal";
1694 else if (WEXITSTATUS(status) != 0)
1695 *errnum = "\"%s\" returned non-zero status";
1696
1697 /*
1698 * Null-terminate the result, convert newlines to spaces and
1699 * install it in the variable.
1700 */
1701 res[cc] = '\0';
1702 cp = &res[cc];
1703
1704 if (cc > 0 && *--cp == '\n') {
1705 /*
1706 * A final newline is just stripped
1707 */
1708 *cp-- = '\0';
1709 }
1710 while (cp >= res) {
1711 if (*cp == '\n') {
1712 *cp = ' ';
1713 }
1714 cp--;
1715 }
1716 break;
1717 }
1718 return res;
1719 bad:
1720 res = bmake_malloc(1);
1721 *res = '\0';
1722 return res;
1723 }
1724
1725 /*-
1726 * Error --
1727 * Print an error message given its format.
1728 *
1729 * Results:
1730 * None.
1731 *
1732 * Side Effects:
1733 * The message is printed.
1734 */
1735 /* VARARGS */
1736 void
1737 Error(const char *fmt, ...)
1738 {
1739 va_list ap;
1740 FILE *err_file;
1741
1742 err_file = debug_file;
1743 if (err_file == stdout)
1744 err_file = stderr;
1745 (void)fflush(stdout);
1746 for (;;) {
1747 va_start(ap, fmt);
1748 fprintf(err_file, "%s: ", progname);
1749 (void)vfprintf(err_file, fmt, ap);
1750 va_end(ap);
1751 (void)fprintf(err_file, "\n");
1752 (void)fflush(err_file);
1753 if (err_file == stderr)
1754 break;
1755 err_file = stderr;
1756 }
1757 }
1758
1759 /*-
1760 * Fatal --
1761 * Produce a Fatal error message. If jobs are running, waits for them
1762 * to finish.
1763 *
1764 * Results:
1765 * None
1766 *
1767 * Side Effects:
1768 * The program exits
1769 */
1770 /* VARARGS */
1771 void
1772 Fatal(const char *fmt, ...)
1773 {
1774 va_list ap;
1775
1776 va_start(ap, fmt);
1777 if (jobsRunning)
1778 Job_Wait();
1779
1780 (void)fflush(stdout);
1781 (void)vfprintf(stderr, fmt, ap);
1782 va_end(ap);
1783 (void)fprintf(stderr, "\n");
1784 (void)fflush(stderr);
1785
1786 PrintOnError(NULL, NULL);
1787
1788 if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
1789 Targ_PrintGraph(2);
1790 Trace_Log(MAKEERROR, 0);
1791 exit(2); /* Not 1 so -q can distinguish error */
1792 }
1793
1794 /*
1795 * Punt --
1796 * Major exception once jobs are being created. Kills all jobs, prints
1797 * a message and exits.
1798 *
1799 * Results:
1800 * None
1801 *
1802 * Side Effects:
1803 * All children are killed indiscriminately and the program Lib_Exits
1804 */
1805 /* VARARGS */
1806 void
1807 Punt(const char *fmt, ...)
1808 {
1809 va_list ap;
1810
1811 va_start(ap, fmt);
1812 (void)fflush(stdout);
1813 (void)fprintf(stderr, "%s: ", progname);
1814 (void)vfprintf(stderr, fmt, ap);
1815 va_end(ap);
1816 (void)fprintf(stderr, "\n");
1817 (void)fflush(stderr);
1818
1819 PrintOnError(NULL, NULL);
1820
1821 DieHorribly();
1822 }
1823
1824 /*-
1825 * DieHorribly --
1826 * Exit without giving a message.
1827 *
1828 * Results:
1829 * None
1830 *
1831 * Side Effects:
1832 * A big one...
1833 */
1834 void
1835 DieHorribly(void)
1836 {
1837 if (jobsRunning)
1838 Job_AbortAll();
1839 if (DEBUG(GRAPH2))
1840 Targ_PrintGraph(2);
1841 Trace_Log(MAKEERROR, 0);
1842 exit(2); /* Not 1, so -q can distinguish error */
1843 }
1844
1845 /*
1846 * Finish --
1847 * Called when aborting due to errors in child shell to signal
1848 * abnormal exit.
1849 *
1850 * Results:
1851 * None
1852 *
1853 * Side Effects:
1854 * The program exits
1855 */
1856 void
1857 Finish(int errors)
1858 /* number of errors encountered in Make_Make */
1859 {
1860 Fatal("%d error%s", errors, errors == 1 ? "" : "s");
1861 }
1862
1863 /*
1864 * eunlink --
1865 * Remove a file carefully, avoiding directories.
1866 */
1867 int
1868 eunlink(const char *file)
1869 {
1870 struct stat st;
1871
1872 if (lstat(file, &st) == -1)
1873 return -1;
1874
1875 if (S_ISDIR(st.st_mode)) {
1876 errno = EISDIR;
1877 return -1;
1878 }
1879 return unlink(file);
1880 }
1881
1882 /*
1883 * execError --
1884 * Print why exec failed, avoiding stdio.
1885 */
1886 void
1887 execError(const char *af, const char *av)
1888 {
1889 #ifdef USE_IOVEC
1890 int i = 0;
1891 struct iovec iov[8];
1892 #define IOADD(s) \
1893 (void)(iov[i].iov_base = UNCONST(s), \
1894 iov[i].iov_len = strlen(iov[i].iov_base), \
1895 i++)
1896 #else
1897 #define IOADD(void)write(2, s, strlen(s))
1898 #endif
1899
1900 IOADD(progname);
1901 IOADD(": ");
1902 IOADD(af);
1903 IOADD("(");
1904 IOADD(av);
1905 IOADD(") failed (");
1906 IOADD(strerror(errno));
1907 IOADD(")\n");
1908
1909 #ifdef USE_IOVEC
1910 while (writev(2, iov, 8) == -1 && errno == EAGAIN)
1911 continue;
1912 #endif
1913 }
1914
1915 /*
1916 * usage --
1917 * exit with usage message
1918 */
1919 static void
1920 usage(void)
1921 {
1922 char *p;
1923 if ((p = strchr(progname, '[')) != NULL)
1924 *p = '\0';
1925
1926 (void)fprintf(stderr,
1927 "usage: %s [-BeikNnqrstWwX] \n\
1928 [-C directory] [-D variable] [-d flags] [-f makefile]\n\
1929 [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n\
1930 [-V variable] [variable=value] [target ...]\n", progname);
1931 exit(2);
1932 }
1933
1934 /*
1935 * realpath(3) can get expensive, cache results...
1936 */
1937 static GNode *cached_realpaths = NULL;
1938
1939 static GNode *
1940 get_cached_realpaths(void)
1941 {
1942
1943 if (!cached_realpaths) {
1944 cached_realpaths = Targ_NewGN("Realpath");
1945 #ifndef DEBUG_REALPATH_CACHE
1946 cached_realpaths->flags = INTERNAL;
1947 #endif
1948 }
1949
1950 return cached_realpaths;
1951 }
1952
1953 /* purge any relative paths */
1954 static void
1955 purge_cached_realpaths(void)
1956 {
1957 GNode *cache = get_cached_realpaths();
1958 Hash_Entry *he, *nhe;
1959 Hash_Search hs;
1960
1961 he = Hash_EnumFirst(&cache->context, &hs);
1962 while (he) {
1963 nhe = Hash_EnumNext(&hs);
1964 if (he->name[0] != '/') {
1965 if (DEBUG(DIR))
1966 fprintf(stderr, "cached_realpath: purging %s\n", he->name);
1967 Hash_DeleteEntry(&cache->context, he);
1968 }
1969 he = nhe;
1970 }
1971 }
1972
1973 char *
1974 cached_realpath(const char *pathname, char *resolved)
1975 {
1976 GNode *cache;
1977 char *rp, *cp;
1978
1979 if (!pathname || !pathname[0])
1980 return NULL;
1981
1982 cache = get_cached_realpaths();
1983
1984 if ((rp = Var_Value(pathname, cache, &cp)) != NULL) {
1985 /* a hit */
1986 strncpy(resolved, rp, MAXPATHLEN);
1987 resolved[MAXPATHLEN - 1] = '\0';
1988 } else if ((rp = realpath(pathname, resolved)) != NULL) {
1989 Var_Set(pathname, rp, cache, 0);
1990 } /* else should we negative-cache? */
1991
1992 free(cp);
1993 return rp ? resolved : NULL;
1994 }
1995
1996 int
1997 PrintAddr(void *a, void *b)
1998 {
1999 printf("%lx ", (unsigned long) a);
2000 return b ? 0 : 0;
2001 }
2002
2003
2004 static int
2005 addErrorCMD(void *cmdp, void *gnp)
2006 {
2007 if (cmdp == NULL)
2008 return 1; /* stop */
2009 Var_Append(".ERROR_CMD", cmdp, VAR_GLOBAL);
2010 return 0;
2011 }
2012
2013 void
2014 PrintOnError(GNode *gn, const char *s)
2015 {
2016 static GNode *en = NULL;
2017 char tmp[64];
2018 char *cp;
2019
2020 if (s)
2021 printf("%s", s);
2022
2023 printf("\n%s: stopped in %s\n", progname, curdir);
2024
2025 if (en)
2026 return; /* we've been here! */
2027 if (gn) {
2028 /*
2029 * We can print this even if there is no .ERROR target.
2030 */
2031 Var_Set(".ERROR_TARGET", gn->name, VAR_GLOBAL, 0);
2032 Var_Delete(".ERROR_CMD", VAR_GLOBAL);
2033 Lst_ForEach(gn->commands, addErrorCMD, gn);
2034 }
2035 strncpy(tmp, "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
2036 sizeof(tmp) - 1);
2037 cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
2038 if (cp) {
2039 if (*cp)
2040 printf("%s", cp);
2041 free(cp);
2042 }
2043 fflush(stdout);
2044
2045 /*
2046 * Finally, see if there is a .ERROR target, and run it if so.
2047 */
2048 en = Targ_FindNode(".ERROR", TARG_NOCREATE);
2049 if (en) {
2050 en->type |= OP_SPECIAL;
2051 Compat_Make(en, en);
2052 }
2053 }
2054
2055 void
2056 Main_ExportMAKEFLAGS(Boolean first)
2057 {
2058 static int once = 1;
2059 char tmp[64];
2060 char *s;
2061
2062 if (once != first)
2063 return;
2064 once = 0;
2065
2066 strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}",
2067 sizeof(tmp));
2068 s = Var_Subst(NULL, tmp, VAR_CMD, VARF_WANTRES);
2069 if (s && *s) {
2070 #ifdef POSIX
2071 setenv("MAKEFLAGS", s, 1);
2072 #else
2073 setenv("MAKE", s, 1);
2074 #endif
2075 }
2076 }
2077
2078 char *
2079 getTmpdir(void)
2080 {
2081 static char *tmpdir = NULL;
2082
2083 if (!tmpdir) {
2084 struct stat st;
2085
2086 /*
2087 * Honor $TMPDIR but only if it is valid.
2088 * Ensure it ends with /.
2089 */
2090 tmpdir = Var_Subst(NULL, "${TMPDIR:tA:U" _PATH_TMP "}/", VAR_GLOBAL,
2091 VARF_WANTRES);
2092 if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
2093 free(tmpdir);
2094 tmpdir = bmake_strdup(_PATH_TMP);
2095 }
2096 }
2097 return tmpdir;
2098 }
2099
2100 /*
2101 * Create and open a temp file using "pattern".
2102 * If "fnamep" is provided set it to a copy of the filename created.
2103 * Otherwise unlink the file once open.
2104 */
2105 int
2106 mkTempFile(const char *pattern, char **fnamep)
2107 {
2108 static char *tmpdir = NULL;
2109 char tfile[MAXPATHLEN];
2110 int fd;
2111
2112 if (!pattern)
2113 pattern = TMPPAT;
2114 if (!tmpdir)
2115 tmpdir = getTmpdir();
2116 if (pattern[0] == '/') {
2117 snprintf(tfile, sizeof(tfile), "%s", pattern);
2118 } else {
2119 snprintf(tfile, sizeof(tfile), "%s%s", tmpdir, pattern);
2120 }
2121 if ((fd = mkstemp(tfile)) < 0)
2122 Punt("Could not create temporary file %s: %s", tfile, strerror(errno));
2123 if (fnamep) {
2124 *fnamep = bmake_strdup(tfile);
2125 } else {
2126 unlink(tfile); /* we just want the descriptor */
2127 }
2128 return fd;
2129 }
2130
2131 /*
2132 * Convert a string representation of a boolean.
2133 * Anything that looks like "No", "False", "Off", "0" etc,
2134 * is FALSE, otherwise TRUE.
2135 */
2136 Boolean
2137 s2Boolean(const char *s, Boolean bf)
2138 {
2139 if (s) {
2140 switch(*s) {
2141 case '\0': /* not set - the default wins */
2142 break;
2143 case '0':
2144 case 'F':
2145 case 'f':
2146 case 'N':
2147 case 'n':
2148 bf = FALSE;
2149 break;
2150 case 'O':
2151 case 'o':
2152 switch (s[1]) {
2153 case 'F':
2154 case 'f':
2155 bf = FALSE;
2156 break;
2157 default:
2158 bf = TRUE;
2159 break;
2160 }
2161 break;
2162 default:
2163 bf = TRUE;
2164 break;
2165 }
2166 }
2167 return (bf);
2168 }
2169
2170 /*
2171 * Return a Boolean based on setting of a knob.
2172 *
2173 * If the knob is not set, the supplied default is the return value.
2174 * If set, anything that looks or smells like "No", "False", "Off", "0" etc,
2175 * is FALSE, otherwise TRUE.
2176 */
2177 Boolean
2178 getBoolean(const char *name, Boolean bf)
2179 {
2180 char tmp[64];
2181 char *cp;
2182
2183 if (snprintf(tmp, sizeof(tmp), "${%s:U:tl}", name) < (int)(sizeof(tmp))) {
2184 cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
2185
2186 if (cp) {
2187 bf = s2Boolean(cp, bf);
2188 free(cp);
2189 }
2190 }
2191 return (bf);
2192 }
2193