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