xref: /netbsd/usr.bin/config/main.c (revision dc96015c)
1*dc96015cSjoerg /*	$NetBSD: main.c,v 1.100 2020/04/03 19:53:41 joerg Exp $	*/
25ecc953bSthorpej 
35ecc953bSthorpej /*
45ecc953bSthorpej  * Copyright (c) 1992, 1993
55ecc953bSthorpej  *	The Regents of the University of California.  All rights reserved.
65ecc953bSthorpej  *
75ecc953bSthorpej  * This software was developed by the Computer Systems Engineering group
85ecc953bSthorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
95ecc953bSthorpej  * contributed to Berkeley.
105ecc953bSthorpej  *
115ecc953bSthorpej  * All advertising materials mentioning features or use of this software
125ecc953bSthorpej  * must display the following acknowledgement:
135ecc953bSthorpej  *	This product includes software developed by the University of
145ecc953bSthorpej  *	California, Lawrence Berkeley Laboratories.
155ecc953bSthorpej  *
165ecc953bSthorpej  * Redistribution and use in source and binary forms, with or without
175ecc953bSthorpej  * modification, are permitted provided that the following conditions
185ecc953bSthorpej  * are met:
195ecc953bSthorpej  * 1. Redistributions of source code must retain the above copyright
205ecc953bSthorpej  *    notice, this list of conditions and the following disclaimer.
215ecc953bSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
225ecc953bSthorpej  *    notice, this list of conditions and the following disclaimer in the
235ecc953bSthorpej  *    documentation and/or other materials provided with the distribution.
245ecc953bSthorpej  * 3. Neither the name of the University nor the names of its contributors
255ecc953bSthorpej  *    may be used to endorse or promote products derived from this software
265ecc953bSthorpej  *    without specific prior written permission.
275ecc953bSthorpej  *
285ecc953bSthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
295ecc953bSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
305ecc953bSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
315ecc953bSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
325ecc953bSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
335ecc953bSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
345ecc953bSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
355ecc953bSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
365ecc953bSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
375ecc953bSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
385ecc953bSthorpej  * SUCH DAMAGE.
395ecc953bSthorpej  *
405ecc953bSthorpej  *	from: @(#)main.c	8.1 (Berkeley) 6/6/93
415ecc953bSthorpej  */
425ecc953bSthorpej 
435ecc953bSthorpej #if HAVE_NBTOOL_CONFIG_H
445ecc953bSthorpej #include "nbtool_config.h"
455ecc953bSthorpej #endif
465ecc953bSthorpej 
479a7f4bbaSchristos #include <sys/cdefs.h>
48*dc96015cSjoerg __RCSID("$NetBSD: main.c,v 1.100 2020/04/03 19:53:41 joerg Exp $");
499a7f4bbaSchristos 
505ecc953bSthorpej #ifndef MAKE_BOOTSTRAP
515ecc953bSthorpej #include <sys/cdefs.h>
525ecc953bSthorpej #define	COPYRIGHT(x)	__COPYRIGHT(x)
535ecc953bSthorpej #else
545ecc953bSthorpej #define	COPYRIGHT(x)	static const char copyright[] = x
555ecc953bSthorpej #endif
565ecc953bSthorpej 
575ecc953bSthorpej #ifndef lint
5898e5374cSlukem COPYRIGHT("@(#) Copyright (c) 1992, 1993\
5998e5374cSlukem  The Regents of the University of California.  All rights reserved.");
605ecc953bSthorpej #endif /* not lint */
615ecc953bSthorpej 
625ecc953bSthorpej #include <sys/types.h>
635ecc953bSthorpej #include <sys/stat.h>
645ecc953bSthorpej #include <sys/param.h>
655ecc953bSthorpej #include <sys/mman.h>
6699d2b097Schristos #if !HAVE_NBTOOL_CONFIG_H
679de19619Schristos #include <sys/sysctl.h>
689de19619Schristos #endif
695ecc953bSthorpej #include <paths.h>
705ecc953bSthorpej #include <ctype.h>
71342d3579Sdsl #include <err.h>
725ecc953bSthorpej #include <errno.h>
735ecc953bSthorpej #include <fcntl.h>
7436c1b19bSdholland #include <limits.h>
755ecc953bSthorpej #include <stdio.h>
765ecc953bSthorpej #include <stdlib.h>
775ecc953bSthorpej #include <string.h>
785ecc953bSthorpej #include <unistd.h>
79d0fb8901Schristos #include <vis.h>
80d0fb8901Schristos #include <util.h>
81d0fb8901Schristos 
825ecc953bSthorpej #include "defs.h"
835ecc953bSthorpej #include "sem.h"
845ecc953bSthorpej 
855ecc953bSthorpej #ifndef LINE_MAX
865ecc953bSthorpej #define LINE_MAX 1024
875ecc953bSthorpej #endif
885ecc953bSthorpej 
89*dc96015cSjoerg struct devbasetq allbases;
90*dc96015cSjoerg struct devatq alldevas;
91*dc96015cSjoerg struct conftq allcf;
92*dc96015cSjoerg struct devitq alldevi, allpseudo;
93*dc96015cSjoerg struct devmtq alldevms;
94*dc96015cSjoerg struct pspectq allpspecs;
95*dc96015cSjoerg 
96*dc96015cSjoerg struct devi **packed;
97*dc96015cSjoerg size_t npacked;
98*dc96015cSjoerg 
99*dc96015cSjoerg struct locators locators;
100*dc96015cSjoerg 
101*dc96015cSjoerg int lkmmode;
102*dc96015cSjoerg const char *conffile;		/* source file, e.g., "GENERIC.sparc" */
103*dc96015cSjoerg const char *machine;		/* machine type, e.g., "sparc" or "sun3" */
104*dc96015cSjoerg const char *machinearch;	/* machine arch, e.g., "sparc" or "m68k" */
105*dc96015cSjoerg struct	nvlist *machinesubarches;
106*dc96015cSjoerg 				/* machine subarches, e.g., "sun68k" or "hpc" */
107*dc96015cSjoerg const char *ioconfname;		/* ioconf name, mutually exclusive to machine */
108*dc96015cSjoerg const char *srcdir;		/* path to source directory (rel. to build) */
109*dc96015cSjoerg const char *builddir;		/* path to build directory */
110*dc96015cSjoerg const char *defbuilddir;	/* default build directory */
111*dc96015cSjoerg const char *ident;		/* kernel "ident"ification string */
112*dc96015cSjoerg int	errors;			/* counts calls to error() */
113*dc96015cSjoerg int	minmaxusers;		/* minimum "maxusers" parameter */
114*dc96015cSjoerg int	defmaxusers;		/* default "maxusers" parameter */
115*dc96015cSjoerg int	maxmaxusers;		/* default "maxusers" parameter */
116*dc96015cSjoerg int	maxusers;		/* configuration's "maxusers" parameter */
117*dc96015cSjoerg int	maxpartitions;		/* configuration's "maxpartitions" parameter */
118*dc96015cSjoerg int	version;		/* version of the configuration file */
119*dc96015cSjoerg struct	nvlist *options;	/* options */
120*dc96015cSjoerg struct	nvlist *fsoptions;	/* filesystems */
121*dc96015cSjoerg struct	nvlist *mkoptions;	/* makeoptions */
122*dc96015cSjoerg struct	nvlist *appmkoptions;	/* appending mkoptions */
123*dc96015cSjoerg struct	nvlist *condmkoptions;	/* conditional makeoption table */
124*dc96015cSjoerg struct	hashtab *devbasetab;	/* devbase lookup */
125*dc96015cSjoerg struct	hashtab *devroottab;	/* attach at root lookup */
126*dc96015cSjoerg struct	hashtab *devatab;	/* devbase attachment lookup */
127*dc96015cSjoerg struct	hashtab *deaddevitab;	/* removed instances lookup */
128*dc96015cSjoerg struct	hashtab *selecttab;	/* selects things that are "optional foo" */
129*dc96015cSjoerg struct	hashtab *needcnttab;	/* retains names marked "needs-count" */
130*dc96015cSjoerg struct	hashtab *opttab;	/* table of configured options */
131*dc96015cSjoerg struct	hashtab *fsopttab;	/* table of configured file systems */
132*dc96015cSjoerg struct	dlhash *defopttab;	/* options that have been "defopt"'d */
133*dc96015cSjoerg struct	dlhash *defflagtab;	/* options that have been "defflag"'d */
134*dc96015cSjoerg struct	dlhash *defparamtab;	/* options that have been "defparam"'d */
135*dc96015cSjoerg struct	dlhash *defoptlint;	/* lint values for options */
136*dc96015cSjoerg struct	nvhash *deffstab;	/* defined file systems */
137*dc96015cSjoerg struct	dlhash *optfiletab;	/* "defopt"'d option .h files */
138*dc96015cSjoerg struct	hashtab *attrtab;	/* attributes (locators, etc.) */
139*dc96015cSjoerg struct	hashtab *attrdeptab;	/* attribute dependencies */
140*dc96015cSjoerg struct	hashtab *bdevmtab;	/* block devm lookup */
141*dc96015cSjoerg struct	hashtab *cdevmtab;	/* character devm lookup */
142*dc96015cSjoerg 
143*dc96015cSjoerg int	ndevi;				/* number of devi's (before packing) */
144*dc96015cSjoerg int	npspecs;			/* number of parent specs */
145*dc96015cSjoerg devmajor_t maxbdevm;			/* max number of block major */
146*dc96015cSjoerg devmajor_t maxcdevm;			/* max number of character major */
147*dc96015cSjoerg int	do_devsw;			/* 0 if pre-devsw config */
148*dc96015cSjoerg int	oktopackage;			/* 0 before setmachine() */
149*dc96015cSjoerg int	devilevel;			/* used for devi->i_level */
150*dc96015cSjoerg 
151*dc96015cSjoerg struct filelist		allfiles;	/* list of all kernel source files */
152*dc96015cSjoerg struct filelist		allcfiles;	/* list of all .c files */
153*dc96015cSjoerg struct filelist		allsfiles;	/* list of all .S files */
154*dc96015cSjoerg struct filelist		allofiles;	/* list of all .o files */
155*dc96015cSjoerg 
156*dc96015cSjoerg struct prefixlist	prefixes,	/* prefix stack */
157*dc96015cSjoerg 			allprefixes;	/* all prefixes used (after popped) */
158*dc96015cSjoerg struct prefixlist	buildprefixes,	/* build prefix stack */
159*dc96015cSjoerg 			allbuildprefixes;/* all build prefixes used (after popped) */
160*dc96015cSjoerg 
1615ecc953bSthorpej int	vflag;				/* verbose output */
1625ecc953bSthorpej int	Pflag;				/* pack locators */
16359c94545Scube int	Lflag;				/* lint config generation */
164cf8e144cSuebayasi int	Mflag;				/* modular build */
16501598ce7Suebayasi int	Sflag;				/* suffix rules & subdirectory */
166d2737476Smartin int	handling_cmdlineopts;		/* currently processing -D/-U options */
1675ecc953bSthorpej 
1685ecc953bSthorpej int	yyparse(void);
1695ecc953bSthorpej 
170551b3bdaSchristos #if !defined(MAKE_BOOTSTRAP) && defined(YYDEBUG)
1715ecc953bSthorpej extern int yydebug;
1725ecc953bSthorpej #endif
1735a918911Suebayasi int	dflag;
1745ecc953bSthorpej 
175d74eb995Sdholland static struct dlhash *obsopttab;
1765ecc953bSthorpej static struct hashtab *mkopttab;
1775ecc953bSthorpej static struct nvlist **nextopt;
1785ecc953bSthorpej static struct nvlist **nextmkopt;
1795ecc953bSthorpej static struct nvlist **nextappmkopt;
180a16a6365Scube static struct nvlist **nextcndmkopt;
1815ecc953bSthorpej static struct nvlist **nextfsopt;
182d2737476Smartin static struct nvlist *cmdlinedefs, *cmdlineundefs;
1835ecc953bSthorpej 
1848b0f9554Sperry static	void	usage(void) __dead;
1855ecc953bSthorpej static	void	dependopts(void);
186d74eb995Sdholland static	void	dependopts_one(const char *);
187d74eb995Sdholland static	void	do_depends(struct nvlist *);
1885ecc953bSthorpej static	void	do_depend(struct nvlist *);
1895ecc953bSthorpej static	void	stop(void);
190c7c59663Spgoyette static	int	do_option(struct hashtab *, struct nvlist **,
191c7c59663Spgoyette 		    struct nvlist ***, const char *, const char *,
192c7c59663Spgoyette 		    const char *, struct hashtab *);
1935ecc953bSthorpej static	int	undo_option(struct hashtab *, struct nvlist **,
1943dbd1573Schristos 		    struct nvlist ***, const char *, const char *, int);
1955ecc953bSthorpej static	int	crosscheck(void);
1965ecc953bSthorpej static	int	badstar(void);
197d0a0f799Suebayasi static	int	mkallsubdirs(void);
1985ecc953bSthorpej static	int	mksymlinks(void);
1995ecc953bSthorpej static	int	mkident(void);
2007aa6070dScube static	int	devbase_has_dead_instances(const char *, void *, void *);
201a31ff6b4Scube static	int	devbase_has_any_instance(struct devbase *, int, int, int);
2027aa6070dScube static	int	check_dead_devi(const char *, void *, void *);
203d2737476Smartin static	void	add_makeopt(const char *);
204d2737476Smartin static	void	remove_makeopt(const char *);
205d2737476Smartin static	void	handle_cmdline_makeoptions(void);
206c130d400Scube static	void	kill_orphans(void);
2077aa6070dScube static	void	do_kill_orphans(struct devbase *, struct attr *,
2087aa6070dScube     struct devbase *, int);
209c130d400Scube static	int	kill_orphans_cb(const char *, void *, void *);
2105ecc953bSthorpej static	int	cfcrosscheck(struct config *, const char *, struct nvlist *);
211d74eb995Sdholland static void	defopt(struct dlhash *ht, const char *fname,
212d74eb995Sdholland 	     struct defoptlist *opts, struct nvlist *deps, int obs);
213d74eb995Sdholland static struct defoptlist *find_declared_option_option(const char *name);
214d74eb995Sdholland static struct nvlist *find_declared_fs_option(const char *name);
2155ecc953bSthorpej 
2165ecc953bSthorpej #define LOGCONFIG_LARGE "INCLUDE_CONFIG_FILE"
2175ecc953bSthorpej #define LOGCONFIG_SMALL "INCLUDE_JUST_CONFIG"
2185ecc953bSthorpej 
2195ecc953bSthorpej static	void	logconfig_start(void);
2205ecc953bSthorpej static	void	logconfig_end(void);
2215ecc953bSthorpej static	FILE	*cfg;
2225ecc953bSthorpej static	time_t	cfgtime;
2235ecc953bSthorpej 
2245ecc953bSthorpej static	int	is_elf(const char *);
2255ecc953bSthorpej static	int	extract_config(const char *, const char *, int);
2265ecc953bSthorpej 
2275ecc953bSthorpej int badfilename(const char *fname);
2285ecc953bSthorpej 
2295ecc953bSthorpej const char *progname;
23044e453f5Suebayasi extern const char *yyfile;
2315ecc953bSthorpej 
2325ecc953bSthorpej int
main(int argc,char ** argv)2335ecc953bSthorpej main(int argc, char **argv)
2345ecc953bSthorpej {
23536c1b19bSdholland 	char *p, cname[PATH_MAX];
2365ecc953bSthorpej 	const char *last_component;
2375ecc953bSthorpej 	int pflag, xflag, ch, removeit;
2385ecc953bSthorpej 
2395ecc953bSthorpej 	setprogname(argv[0]);
2405ecc953bSthorpej 
2415ecc953bSthorpej 	pflag = 0;
2425ecc953bSthorpej 	xflag = 0;
24301598ce7Suebayasi 	while ((ch = getopt(argc, argv, "D:LMPSU:dgpvb:s:x")) != -1) {
2445ecc953bSthorpej 		switch (ch) {
2455ecc953bSthorpej 
246d2737476Smartin 		case 'd':
247551b3bdaSchristos #if !defined(MAKE_BOOTSTRAP) && defined(YYDEBUG)
2485ecc953bSthorpej 			yydebug = 1;
2495a918911Suebayasi #endif
250d3809db7Suebayasi 			dflag++;
2515ecc953bSthorpej 			break;
2525ecc953bSthorpej 
2535f8cf5aeSuebayasi 		case 'M':
254cf8e144cSuebayasi 			Mflag = 1;
2555f8cf5aeSuebayasi 			break;
2565f8cf5aeSuebayasi 
25759c94545Scube 		case 'L':
25859c94545Scube 			Lflag = 1;
25959c94545Scube 			break;
26059c94545Scube 
2615ecc953bSthorpej 		case 'P':
2625ecc953bSthorpej 			Pflag = 1;
2635ecc953bSthorpej 			break;
2645ecc953bSthorpej 
2655ecc953bSthorpej 		case 'g':
2665ecc953bSthorpej 			/*
2675ecc953bSthorpej 			 * In addition to DEBUG, you probably wanted to
2685ecc953bSthorpej 			 * set "options KGDB" and maybe others.  We could
2695ecc953bSthorpej 			 * do that for you, but you really should just
2705ecc953bSthorpej 			 * put them in the config file.
2715ecc953bSthorpej 			 */
272d2737476Smartin 			warnx("-g is obsolete (use -D DEBUG=\"-g\")");
2735ecc953bSthorpej 			usage();
274c7295a4cSchristos 			/*NOTREACHED*/
2755ecc953bSthorpej 
2765ecc953bSthorpej 		case 'p':
2775ecc953bSthorpej 			/*
2785ecc953bSthorpej 			 * Essentially the same as makeoptions PROF="-pg",
2795ecc953bSthorpej 			 * but also changes the path from ../../compile/FOO
2805ecc953bSthorpej 			 * to ../../compile/FOO.PROF; i.e., compile a
2815ecc953bSthorpej 			 * profiling kernel based on a typical "regular"
2825ecc953bSthorpej 			 * kernel.
2835ecc953bSthorpej 			 *
2845ecc953bSthorpej 			 * Note that if you always want profiling, you
2855ecc953bSthorpej 			 * can (and should) use a "makeoptions" line.
2865ecc953bSthorpej 			 */
2875ecc953bSthorpej 			pflag = 1;
2885ecc953bSthorpej 			break;
2895ecc953bSthorpej 
2905ecc953bSthorpej 		case 'v':
2915ecc953bSthorpej 			vflag = 1;
2925ecc953bSthorpej 			break;
2935ecc953bSthorpej 
2945ecc953bSthorpej 		case 'b':
2955ecc953bSthorpej 			builddir = optarg;
2965ecc953bSthorpej 			break;
2975ecc953bSthorpej 
2985ecc953bSthorpej 		case 's':
2995ecc953bSthorpej 			srcdir = optarg;
3005ecc953bSthorpej 			break;
3015ecc953bSthorpej 
30201598ce7Suebayasi 		case 'S':
30301598ce7Suebayasi 			Sflag = 1;
30401598ce7Suebayasi 			break;
30501598ce7Suebayasi 
3065ecc953bSthorpej 		case 'x':
3075ecc953bSthorpej 			xflag = 1;
3085ecc953bSthorpej 			break;
3095ecc953bSthorpej 
310d2737476Smartin 		case 'D':
311d2737476Smartin 			add_makeopt(optarg);
312d2737476Smartin 			break;
313d2737476Smartin 
314d2737476Smartin 		case 'U':
315d2737476Smartin 			remove_makeopt(optarg);
316d2737476Smartin 			break;
317d2737476Smartin 
3185ecc953bSthorpej 		case '?':
3195ecc953bSthorpej 		default:
3205ecc953bSthorpej 			usage();
3215ecc953bSthorpej 		}
3225ecc953bSthorpej 	}
3235ecc953bSthorpej 
3247547791dSpooka 	if (xflag && optind != 2) {
325e239742fSpooka 		errx(EXIT_FAILURE, "-x must be used alone");
326e239742fSpooka 	}
327e239742fSpooka 
3285ecc953bSthorpej 	argc -= optind;
3295ecc953bSthorpej 	argv += optind;
3305ecc953bSthorpej 	if (argc > 1) {
3315ecc953bSthorpej 		usage();
3325ecc953bSthorpej 	}
3335ecc953bSthorpej 
334c7295a4cSchristos 	if (Lflag && (builddir != NULL || Pflag || pflag))
335c7295a4cSchristos 		errx(EXIT_FAILURE, "-L can only be used with -s and -v");
3365ecc953bSthorpej 
3375ecc953bSthorpej 	if (xflag) {
3389de19619Schristos 		if (argc == 0) {
33999d2b097Schristos #if !HAVE_NBTOOL_CONFIG_H
3409de19619Schristos 			char path_unix[MAXPATHLEN];
3419de19619Schristos 			size_t len = sizeof(path_unix) - 1;
3429de19619Schristos 			path_unix[0] = '/';
3439de19619Schristos 
3449de19619Schristos 			conffile = sysctlbyname("machdep.booted_kernel",
3459de19619Schristos 			    &path_unix[1], &len, NULL, 0) == -1 ? _PATH_UNIX :
3469de19619Schristos 			    path_unix;
3475ecc953bSthorpej #else
348c7295a4cSchristos 			errx(EXIT_FAILURE, "no kernel supplied");
3495ecc953bSthorpej #endif
3509de19619Schristos 		} else
3519de19619Schristos 			conffile = argv[0];
352c7295a4cSchristos 		if (!is_elf(conffile))
353c7295a4cSchristos 			errx(EXIT_FAILURE, "%s: not a binary kernel",
3545ecc953bSthorpej 			    conffile);
355c7295a4cSchristos 		if (!extract_config(conffile, "stdout", STDOUT_FILENO))
356c7295a4cSchristos 			errx(EXIT_FAILURE, "%s does not contain embedded "
357c7295a4cSchristos 			    "configuration data", conffile);
3585ecc953bSthorpej 		exit(0);
3595ecc953bSthorpej 	}
3605ecc953bSthorpej 
3615ecc953bSthorpej 	conffile = (argc == 1) ? argv[0] : "CONFIG";
3625ecc953bSthorpej 	if (firstfile(conffile)) {
363c7295a4cSchristos 		err(EXIT_FAILURE, "Cannot read `%s'", conffile);
3645ecc953bSthorpej 		exit(2);
3655ecc953bSthorpej 	}
3665ecc953bSthorpej 
3675ecc953bSthorpej 	/*
3685ecc953bSthorpej 	 * Init variables.
3695ecc953bSthorpej 	 */
3705ecc953bSthorpej 	minmaxusers = 1;
3715ecc953bSthorpej 	maxmaxusers = 10000;
3725ecc953bSthorpej 	initintern();
3735ecc953bSthorpej 	ident = NULL;
3745ecc953bSthorpej 	devbasetab = ht_new();
375c130d400Scube 	devroottab = ht_new();
3765ecc953bSthorpej 	devatab = ht_new();
3775ecc953bSthorpej 	devitab = ht_new();
3787aa6070dScube 	deaddevitab = ht_new();
3795ecc953bSthorpej 	selecttab = ht_new();
3805ecc953bSthorpej 	needcnttab = ht_new();
3815ecc953bSthorpej 	opttab = ht_new();
3825ecc953bSthorpej 	mkopttab = ht_new();
3835ecc953bSthorpej 	fsopttab = ht_new();
38402245173Sdholland 	deffstab = nvhash_create();
385d74eb995Sdholland 	defopttab = dlhash_create();
386d74eb995Sdholland 	defparamtab = dlhash_create();
387d74eb995Sdholland 	defoptlint = dlhash_create();
388d74eb995Sdholland 	defflagtab = dlhash_create();
389d74eb995Sdholland 	optfiletab = dlhash_create();
390d74eb995Sdholland 	obsopttab = dlhash_create();
3915ecc953bSthorpej 	bdevmtab = ht_new();
3925ecc953bSthorpej 	maxbdevm = 0;
3935ecc953bSthorpej 	cdevmtab = ht_new();
3945ecc953bSthorpej 	maxcdevm = 0;
3955ecc953bSthorpej 	nextopt = &options;
3965ecc953bSthorpej 	nextmkopt = &mkoptions;
3975ecc953bSthorpej 	nextappmkopt = &appmkoptions;
3981894a7d2Scube 	nextcndmkopt = &condmkoptions;
3995ecc953bSthorpej 	nextfsopt = &fsoptions;
400cd06fea3Suebayasi 	initfiles();
401cd06fea3Suebayasi 	initsem();
4025ecc953bSthorpej 
4035ecc953bSthorpej 	/*
4045ecc953bSthorpej 	 * Handle profiling (must do this before we try to create any
4055ecc953bSthorpej 	 * files).
4065ecc953bSthorpej 	 */
4075ecc953bSthorpej 	last_component = strrchr(conffile, '/');
4085ecc953bSthorpej 	last_component = (last_component) ? last_component + 1 : conffile;
4095ecc953bSthorpej 	if (pflag) {
4105ecc953bSthorpej 		p = emalloc(strlen(last_component) + 17);
4115ecc953bSthorpej 		(void)sprintf(p, "../compile/%s.PROF", last_component);
4125ecc953bSthorpej 		(void)addmkoption(intern("PROF"), "-pg");
4135ecc953bSthorpej 		(void)addoption(intern("GPROF"), NULL);
4145ecc953bSthorpej 	} else {
4155ecc953bSthorpej 		p = emalloc(strlen(last_component) + 13);
4165ecc953bSthorpej 		(void)sprintf(p, "../compile/%s", last_component);
4175ecc953bSthorpej 	}
4185ecc953bSthorpej 	defbuilddir = (argc == 0) ? "." : p;
4195ecc953bSthorpej 
42059c94545Scube 	if (Lflag) {
42159c94545Scube 		char resolvedname[MAXPATHLEN];
42259c94545Scube 
42359c94545Scube 		if (realpath(conffile, resolvedname) == NULL)
42459c94545Scube 			err(EXIT_FAILURE, "realpath(%s)", conffile);
42559c94545Scube 
42659c94545Scube 		if (yyparse())
42759c94545Scube 			stop();
42859c94545Scube 
429aa3d0249Scube 		printf("include \"%s\"\n", resolvedname);
43059c94545Scube 
43159c94545Scube 		emit_params();
43259c94545Scube 		emit_options();
43359c94545Scube 		emit_instances();
43459c94545Scube 
43559c94545Scube 		exit(EXIT_SUCCESS);
43659c94545Scube 	}
43759c94545Scube 
4385ecc953bSthorpej 	removeit = 0;
4395ecc953bSthorpej 	if (is_elf(conffile)) {
4405ecc953bSthorpej 		const char *tmpdir;
4415ecc953bSthorpej 		int cfd;
4425ecc953bSthorpej 
443c7295a4cSchristos 		if (builddir == NULL)
444c7295a4cSchristos 			errx(EXIT_FAILURE, "Build directory must be specified "
445c7295a4cSchristos 			    "with binary kernels");
4465ecc953bSthorpej 
4475ecc953bSthorpej 		/* Open temporary configuration file */
4485ecc953bSthorpej 		tmpdir = getenv("TMPDIR");
4495ecc953bSthorpej 		if (tmpdir == NULL)
45036c1b19bSdholland 			tmpdir = _PATH_TMP;
4515ecc953bSthorpej 		snprintf(cname, sizeof(cname), "%s/config.tmp.XXXXXX", tmpdir);
4525ecc953bSthorpej 		cfd = mkstemp(cname);
453c7295a4cSchristos 		if (cfd == -1)
454c7295a4cSchristos 			err(EXIT_FAILURE, "Cannot create `%s'", cname);
4555ecc953bSthorpej 
4565ecc953bSthorpej 		printf("Using configuration data embedded in kernel...\n");
45736c1b19bSdholland 		if (!extract_config(conffile, cname, cfd)) {
45836c1b19bSdholland 			unlink(cname);
459c7295a4cSchristos 			errx(EXIT_FAILURE, "%s does not contain embedded "
460c7295a4cSchristos 			    "configuration data", conffile);
46136c1b19bSdholland 		}
4625ecc953bSthorpej 
4635ecc953bSthorpej 		removeit = 1;
4645ecc953bSthorpej 		close(cfd);
4655ecc953bSthorpej 		firstfile(cname);
4665ecc953bSthorpej 	}
4675ecc953bSthorpej 
4685ecc953bSthorpej 	 /*
469e239742fSpooka 	  * Log config file.  We don't know until yyparse() if we're
470e239742fSpooka 	  * going to need config_file.h (i.e. if we're doing ioconf-only
471e239742fSpooka 	  * or not).  Just start creating the file, and when we know
472e239742fSpooka 	  * later, we'll just keep or discard our work here.
4735ecc953bSthorpej 	  */
4745ecc953bSthorpej 	logconfig_start();
475e239742fSpooka 
476e239742fSpooka 	/*
477e239742fSpooka 	 * Parse config file (including machine definitions).
478e239742fSpooka 	 */
4795ecc953bSthorpej 	if (yyparse())
4805ecc953bSthorpej 		stop();
481e239742fSpooka 
482e239742fSpooka 	if (ioconfname && cfg)
483e239742fSpooka 		fclose(cfg);
484e239742fSpooka 	else
4855ecc953bSthorpej 		logconfig_end();
4865ecc953bSthorpej 
4875ecc953bSthorpej 	if (removeit)
4885ecc953bSthorpej 		unlink(cname);
4895ecc953bSthorpej 
4905ecc953bSthorpej 	/*
491d2737476Smartin 	 * Handle command line overrides
492d2737476Smartin 	 */
49344e453f5Suebayasi 	yyfile = "handle_cmdline_makeoptions";
494d2737476Smartin 	handle_cmdline_makeoptions();
495d2737476Smartin 
496d2737476Smartin 	/*
497c130d400Scube 	 * Detect and properly ignore orphaned devices
498c130d400Scube 	 */
49944e453f5Suebayasi 	yyfile = "kill_orphans";
500c130d400Scube 	kill_orphans();
501c130d400Scube 
502c130d400Scube 	/*
5035ecc953bSthorpej 	 * Select devices and pseudo devices and their attributes
5045ecc953bSthorpej 	 */
50544e453f5Suebayasi 	yyfile = "fixdevis";
5067aa6070dScube 	if (fixdevis())
5077aa6070dScube 		stop();
5085ecc953bSthorpej 
5095ecc953bSthorpej 	/*
510e8a8b10dSuebayasi 	 * Copy maxusers to param.
511e8a8b10dSuebayasi 	 */
512e8a8b10dSuebayasi 	yyfile = "fixmaxusers";
513e8a8b10dSuebayasi 	fixmaxusers();
514e8a8b10dSuebayasi 
515e8a8b10dSuebayasi 	/*
5160b1d177bSuebayasi 	 * Copy makeoptions to params
5170b1d177bSuebayasi 	 */
5180b1d177bSuebayasi 	yyfile = "fixmkoption";
5190b1d177bSuebayasi 	fixmkoption();
5200b1d177bSuebayasi 
5210b1d177bSuebayasi 	/*
522e239742fSpooka 	 * If working on an ioconf-only config, process here and exit
523e239742fSpooka 	 */
524e239742fSpooka 	if (ioconfname) {
52544e453f5Suebayasi 		yyfile = "pack";
526e239742fSpooka 		pack();
52744e453f5Suebayasi 		yyfile = "mkioconf";
528e239742fSpooka 		mkioconf();
52944e453f5Suebayasi 		yyfile = "emitlocs";
530e239742fSpooka 		emitlocs();
53144e453f5Suebayasi 		yyfile = "emitioconfh";
532d0e5d937Spooka 		emitioconfh();
533e239742fSpooka 		return 0;
534e239742fSpooka 	}
535e239742fSpooka 
5366d1bba4aSuebayasi 	yyfile = "dependattrs";
5376d1bba4aSuebayasi 	dependattrs();
5386d1bba4aSuebayasi 
539e239742fSpooka 	/*
5405ecc953bSthorpej 	 * Deal with option dependencies.
5415ecc953bSthorpej 	 */
54244e453f5Suebayasi 	yyfile = "dependopts";
5435ecc953bSthorpej 	dependopts();
5445ecc953bSthorpej 
5455ecc953bSthorpej 	/*
5465ecc953bSthorpej 	 * Fix (as in `set firmly in place') files.
5475ecc953bSthorpej 	 */
54844e453f5Suebayasi 	yyfile = "fixfiles";
5495ecc953bSthorpej 	if (fixfiles())
5505ecc953bSthorpej 		stop();
5515ecc953bSthorpej 
5525ecc953bSthorpej 	/*
5535ecc953bSthorpej 	 * Fix device-majors.
5545ecc953bSthorpej 	 */
55544e453f5Suebayasi 	yyfile = "fixdevsw";
5565ecc953bSthorpej 	if (fixdevsw())
5575ecc953bSthorpej 		stop();
5585ecc953bSthorpej 
5595ecc953bSthorpej 	/*
5605ecc953bSthorpej 	 * Perform cross-checking.
5615ecc953bSthorpej 	 */
5625ecc953bSthorpej 	if (maxusers == 0) {
5635ecc953bSthorpej 		if (defmaxusers) {
5645ecc953bSthorpej 			(void)printf("maxusers not specified; %d assumed\n",
5655ecc953bSthorpej 			    defmaxusers);
5665ecc953bSthorpej 			maxusers = defmaxusers;
5675ecc953bSthorpej 		} else {
568c7295a4cSchristos 			warnx("need \"maxusers\" line");
5695ecc953bSthorpej 			errors++;
5705ecc953bSthorpej 		}
5715ecc953bSthorpej 	}
5725ecc953bSthorpej 	if (crosscheck() || errors)
5735ecc953bSthorpej 		stop();
5745ecc953bSthorpej 
5755ecc953bSthorpej 	/*
5765ecc953bSthorpej 	 * Squeeze things down and finish cross-checks (STAR checks must
5775ecc953bSthorpej 	 * run after packing).
5785ecc953bSthorpej 	 */
57944e453f5Suebayasi 	yyfile = "pack";
5805ecc953bSthorpej 	pack();
58144e453f5Suebayasi 	yyfile = "badstar";
5825ecc953bSthorpej 	if (badstar())
5835ecc953bSthorpej 		stop();
5845ecc953bSthorpej 
58544e453f5Suebayasi 	yyfile = NULL;
5865ecc953bSthorpej 	/*
5875ecc953bSthorpej 	 * Ready to go.  Build all the various files.
5885ecc953bSthorpej 	 */
589d0a0f799Suebayasi 	if ((Sflag && mkallsubdirs()) || mksymlinks() || mkmakefile() || mkheaders() || mkswap() ||
5900367057bScube 	    mkioconf() || (do_devsw ? mkdevsw() : 0) || mkident() || errors)
5915ecc953bSthorpej 		stop();
5925ecc953bSthorpej 	(void)printf("Build directory is %s\n", builddir);
5935ecc953bSthorpej 	(void)printf("Don't forget to run \"make depend\"\n");
5942e4b6d37Suebayasi 
595c7295a4cSchristos 	return 0;
5965ecc953bSthorpej }
5975ecc953bSthorpej 
5985ecc953bSthorpej static void
usage(void)5995ecc953bSthorpej usage(void)
6005ecc953bSthorpej {
6018566a894Swiz 	(void)fprintf(stderr, "Usage: %s [-Ppv] [-b builddir] [-D var=value] "
6028566a894Swiz 	    "[-s srcdir] [-U var] "
603c7295a4cSchristos 	    "[config-file]\n\t%s -x [kernel-file]\n"
604c7295a4cSchristos 	    "\t%s -L [-v] [-s srcdir] [config-file]\n",
605c7295a4cSchristos 	    getprogname(), getprogname(), getprogname());
6065ecc953bSthorpej 	exit(1);
6075ecc953bSthorpej }
6085ecc953bSthorpej 
6095ecc953bSthorpej /*
6105ecc953bSthorpej  * Set any options that are implied by other options.
6115ecc953bSthorpej  */
6125ecc953bSthorpej static void
dependopts(void)6135ecc953bSthorpej dependopts(void)
6145ecc953bSthorpej {
615d74eb995Sdholland 	struct nvlist *nv;
6165ecc953bSthorpej 
6175ecc953bSthorpej 	for (nv = options; nv != NULL; nv = nv->nv_next) {
618d74eb995Sdholland 		dependopts_one(nv->nv_name);
6195ecc953bSthorpej 	}
6204a82747aSmatt 
6214a82747aSmatt 	for (nv = fsoptions; nv != NULL; nv = nv->nv_next) {
622d74eb995Sdholland 		dependopts_one(nv->nv_name);
623d74eb995Sdholland 	}
624d74eb995Sdholland }
625d74eb995Sdholland 
626d74eb995Sdholland static void
dependopts_one(const char * name)627d74eb995Sdholland dependopts_one(const char *name)
628d74eb995Sdholland {
629d74eb995Sdholland 	struct defoptlist *dl;
630d74eb995Sdholland 	struct nvlist *fs;
631d74eb995Sdholland 
632d74eb995Sdholland 	dl = find_declared_option_option(name);
633d74eb995Sdholland 	if (dl != NULL) {
634d74eb995Sdholland 		do_depends(dl->dl_depends);
635d74eb995Sdholland 	}
636d74eb995Sdholland 	fs = find_declared_fs_option(name);
637d74eb995Sdholland 	if (fs != NULL) {
638d74eb995Sdholland 		do_depends(fs->nv_ptr);
639d74eb995Sdholland 	}
640bd5b1cb6Suebayasi 
641bd5b1cb6Suebayasi 	CFGDBG(3, "depend `%s' searched", name);
642d74eb995Sdholland }
643d74eb995Sdholland 
644d74eb995Sdholland static void
do_depends(struct nvlist * nv)645d74eb995Sdholland do_depends(struct nvlist *nv)
646d74eb995Sdholland {
647d74eb995Sdholland 	struct nvlist *opt;
648d74eb995Sdholland 
649d74eb995Sdholland 	for (opt = nv; opt != NULL; opt = opt->nv_next) {
6504a82747aSmatt 		do_depend(opt);
6514a82747aSmatt 	}
6524a82747aSmatt }
6535ecc953bSthorpej 
6545ecc953bSthorpej static void
do_depend(struct nvlist * nv)6555ecc953bSthorpej do_depend(struct nvlist *nv)
6565ecc953bSthorpej {
6575ecc953bSthorpej 	struct attr *a;
6585ecc953bSthorpej 
6595ecc953bSthorpej 	if (nv != NULL && (nv->nv_flags & NV_DEPENDED) == 0) {
6605ecc953bSthorpej 		nv->nv_flags |= NV_DEPENDED;
6615ecc953bSthorpej 		/*
6625ecc953bSthorpej 		 * If the dependency is an attribute, then just add
6635ecc953bSthorpej 		 * it to the selecttab.
6645ecc953bSthorpej 		 */
665bd5b1cb6Suebayasi 		CFGDBG(3, "depend attr `%s'", nv->nv_name);
6665ecc953bSthorpej 		if ((a = ht_lookup(attrtab, nv->nv_name)) != NULL) {
6675ecc953bSthorpej 			if (a->a_iattr)
6685ecc953bSthorpej 				panic("do_depend(%s): dep `%s' is an iattr",
6695ecc953bSthorpej 				    nv->nv_name, a->a_name);
6705ecc953bSthorpej 			expandattr(a, selectattr);
6715ecc953bSthorpej 		} else {
6725ecc953bSthorpej 			if (ht_lookup(opttab, nv->nv_name) == NULL)
6735ecc953bSthorpej 				addoption(nv->nv_name, NULL);
674d74eb995Sdholland 			dependopts_one(nv->nv_name);
6755ecc953bSthorpej 		}
6765ecc953bSthorpej 	}
6775ecc953bSthorpej }
6785ecc953bSthorpej 
679c7295a4cSchristos static int
recreate(const char * p,const char * q)680c7295a4cSchristos recreate(const char *p, const char *q)
681c7295a4cSchristos {
682c7295a4cSchristos 	int ret;
683c7295a4cSchristos 
684c7295a4cSchristos 	if ((ret = unlink(q)) == -1 && errno != ENOENT)
68550093f14Schristos 		warn("unlink(%s)", q);
686c7295a4cSchristos 	if ((ret = symlink(p, q)) == -1)
687c7295a4cSchristos 		warn("symlink(%s -> %s)", q, p);
688c7295a4cSchristos 	return ret;
689c7295a4cSchristos }
690c7295a4cSchristos 
69159b283efSuebayasi static void
mksubdir(char * buf)69259b283efSuebayasi mksubdir(char *buf)
69359b283efSuebayasi {
69459b283efSuebayasi 	char *p;
69559b283efSuebayasi 	struct stat st;
69659b283efSuebayasi 
69759b283efSuebayasi 	p = strrchr(buf, '/');
69859b283efSuebayasi 	if (p != NULL && *p == '/') {
69959b283efSuebayasi 		*p = '\0';
70059b283efSuebayasi 		mksubdir(buf);
70159b283efSuebayasi 		*p = '/';
70259b283efSuebayasi 	}
70359b283efSuebayasi 	if (stat(buf, &st) == 0) {
70459b283efSuebayasi 		if (!S_ISDIR(st.st_mode))
70559b283efSuebayasi 			errx(EXIT_FAILURE, "not directory %s", buf);
70659b283efSuebayasi 	} else
70759b283efSuebayasi 		if (mkdir(buf, 0777) == -1)
70859b283efSuebayasi 			errx(EXIT_FAILURE, "cannot create %s", buf);
70959b283efSuebayasi }
71059b283efSuebayasi 
71159b283efSuebayasi static int
mksubdirs(struct filelist * fl)712d0a0f799Suebayasi mksubdirs(struct filelist *fl)
71359b283efSuebayasi {
71459b283efSuebayasi 	struct files *fi;
715ba1d542aSuebayasi 	const char *prologue, *prefix, *sep;
71659b283efSuebayasi 	char buf[MAXPATHLEN];
71759b283efSuebayasi 
718d0a0f799Suebayasi 	TAILQ_FOREACH(fi, fl, fi_next) {
71959b283efSuebayasi 		if ((fi->fi_flags & FI_SEL) == 0)
72059b283efSuebayasi 			continue;
72159b283efSuebayasi 		prefix = sep = "";
72259b283efSuebayasi 		if (fi->fi_buildprefix != NULL) {
72359b283efSuebayasi 			prefix = fi->fi_buildprefix;
72459b283efSuebayasi 			sep = "/";
72559b283efSuebayasi 		} else {
72659b283efSuebayasi 			if (fi->fi_prefix != NULL) {
72759b283efSuebayasi 				prefix = fi->fi_prefix;
72859b283efSuebayasi 				sep = "/";
72959b283efSuebayasi 			}
73059b283efSuebayasi 		}
73159b283efSuebayasi 		snprintf(buf, sizeof(buf), "%s%s%s", prefix, sep, fi->fi_dir);
73259b283efSuebayasi 		if (buf[0] == '\0')
73359b283efSuebayasi 			continue;
73459b283efSuebayasi 		mksubdir(buf);
735d0a0f799Suebayasi 		if (fi->fi_prefix != NULL && fi->fi_buildprefix != NULL) {
736d0a0f799Suebayasi 			char org[MAXPATHLEN];
737d0a0f799Suebayasi 
738d0a0f799Suebayasi 			if (fi->fi_prefix[0] == '/') {
739d0a0f799Suebayasi 				prologue = "";
740d0a0f799Suebayasi 				sep = "";
741d0a0f799Suebayasi 			} else {
742d0a0f799Suebayasi 				prologue = srcdir;
743d0a0f799Suebayasi 				sep = "/";
744d0a0f799Suebayasi 			}
745d0a0f799Suebayasi 			snprintf(buf, sizeof(buf), "%s%s%s",
746d0a0f799Suebayasi 			    fi->fi_buildprefix, "/", fi->fi_path);
747d0a0f799Suebayasi 			snprintf(org, sizeof(org), "%s%s%s%s%s",
748d0a0f799Suebayasi 			    prologue, sep, fi->fi_prefix, "/", fi->fi_path);
749d0a0f799Suebayasi 			recreate(org, buf);
750d0a0f799Suebayasi 			fi->fi_prefix = fi->fi_buildprefix;
751d0a0f799Suebayasi 			fi->fi_buildprefix = NULL;
752d0a0f799Suebayasi 		}
75359b283efSuebayasi 	}
75459b283efSuebayasi 
75559b283efSuebayasi 	return 0;
75659b283efSuebayasi }
75759b283efSuebayasi 
758d0a0f799Suebayasi static int
mkallsubdirs(void)759d0a0f799Suebayasi mkallsubdirs(void)
760d0a0f799Suebayasi {
761d0a0f799Suebayasi 
762d0a0f799Suebayasi 	mksubdirs(&allfiles);
763d0a0f799Suebayasi 	mksubdirs(&allofiles);
764ba1d542aSuebayasi 	return 0;
765d0a0f799Suebayasi }
766d0a0f799Suebayasi 
7675ecc953bSthorpej /*
7685ecc953bSthorpej  * Make a symlink for "machine" so that "#include <machine/foo.h>" works,
7695ecc953bSthorpej  * and for the machine's CPU architecture, so that works as well.
7705ecc953bSthorpej  */
7715ecc953bSthorpej static int
mksymlinks(void)7725ecc953bSthorpej mksymlinks(void)
7735ecc953bSthorpej {
7745ecc953bSthorpej 	int ret;
7755ecc953bSthorpej 	char *p, buf[MAXPATHLEN];
7765ecc953bSthorpej 	const char *q;
7775ecc953bSthorpej 	struct nvlist *nv;
7785ecc953bSthorpej 
779579fbacfSuebayasi 	p = buf;
780579fbacfSuebayasi 
781579fbacfSuebayasi 	snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, machine);
782c7295a4cSchristos 	ret = recreate(p, "machine");
783cfbe3a4fSmatt 	ret = recreate(p, machine);
7845ecc953bSthorpej 
7855ecc953bSthorpej 	if (machinearch != NULL) {
786579fbacfSuebayasi 		snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, machinearch);
7875ecc953bSthorpej 		q = machinearch;
7885ecc953bSthorpej 	} else {
789579fbacfSuebayasi 		snprintf(buf, sizeof(buf), "machine");
7905ecc953bSthorpej 		q = machine;
7915ecc953bSthorpej 	}
792c7295a4cSchristos 
793c7295a4cSchristos 	ret = recreate(p, q);
7945ecc953bSthorpej 
7955ecc953bSthorpej 	for (nv = machinesubarches; nv != NULL; nv = nv->nv_next) {
7965ecc953bSthorpej 		q = nv->nv_name;
797579fbacfSuebayasi 		snprintf(buf, sizeof(buf), "%s/arch/%s/include", srcdir, q);
798c7295a4cSchristos 		ret = recreate(p, q);
7995ecc953bSthorpej 	}
8005ecc953bSthorpej 
8015ecc953bSthorpej 	return (ret);
8025ecc953bSthorpej }
8035ecc953bSthorpej 
8045ecc953bSthorpej static __dead void
stop(void)8055ecc953bSthorpej stop(void)
8065ecc953bSthorpej {
8075ecc953bSthorpej 	(void)fprintf(stderr, "*** Stop.\n");
8085ecc953bSthorpej 	exit(1);
8095ecc953bSthorpej }
8105ecc953bSthorpej 
8114a82747aSmatt static void
check_dependencies(const char * thing,struct nvlist * deps)812d74eb995Sdholland check_dependencies(const char *thing, struct nvlist *deps)
8134a82747aSmatt {
8144a82747aSmatt 	struct nvlist *dep;
8154a82747aSmatt 	struct attr *a;
8164a82747aSmatt 
8174a82747aSmatt 	for (dep = deps; dep != NULL; dep = dep->nv_next) {
8184a82747aSmatt 		/*
8194a82747aSmatt 		 * If the dependency is an attribute, it must not
8204a82747aSmatt 		 * be an interface attribute.  Otherwise, it must
8214a82747aSmatt 		 * be a previously declared option.
8224a82747aSmatt 		 */
8234a82747aSmatt 		if ((a = ht_lookup(attrtab, dep->nv_name)) != NULL) {
8244a82747aSmatt 			if (a->a_iattr)
825c7295a4cSchristos 				cfgerror("option `%s' dependency `%s' "
8264a82747aSmatt 				    "is an interface attribute",
827d74eb995Sdholland 				    thing, a->a_name);
8284a82747aSmatt 		} else if (OPT_OBSOLETE(dep->nv_name)) {
829c7295a4cSchristos 			cfgerror("option `%s' dependency `%s' "
830d74eb995Sdholland 			    "is obsolete", thing, dep->nv_name);
831f079f926Schristos 		} else if (!find_declared_option(dep->nv_name)) {
832c7295a4cSchristos 			cfgerror("option `%s' dependency `%s' "
8334a82747aSmatt 			    "is an unknown option",
834d74eb995Sdholland 			    thing, dep->nv_name);
8354a82747aSmatt 		}
8364a82747aSmatt 	}
8374a82747aSmatt }
8384a82747aSmatt 
839d74eb995Sdholland static void
add_fs_dependencies(struct nvlist * nv,struct nvlist * deps)840d74eb995Sdholland add_fs_dependencies(struct nvlist *nv, struct nvlist *deps)
841d74eb995Sdholland {
842d74eb995Sdholland 	/* Use nv_ptr to link any other options that are implied. */
843d74eb995Sdholland 	nv->nv_ptr = deps;
844d74eb995Sdholland 	check_dependencies(nv->nv_name, deps);
845d74eb995Sdholland }
846d74eb995Sdholland 
847d74eb995Sdholland static void
add_opt_dependencies(struct defoptlist * dl,struct nvlist * deps)848d74eb995Sdholland add_opt_dependencies(struct defoptlist *dl, struct nvlist *deps)
849d74eb995Sdholland {
850d74eb995Sdholland 	dl->dl_depends = deps;
851d74eb995Sdholland 	check_dependencies(dl->dl_name, deps);
852d74eb995Sdholland }
853d74eb995Sdholland 
8545ecc953bSthorpej /*
855e6e6744fSpooka  * Define one or more file systems.
8565ecc953bSthorpej  */
8575ecc953bSthorpej void
deffilesystem(struct nvlist * fses,struct nvlist * deps)858c9607065Spooka deffilesystem(struct nvlist *fses, struct nvlist *deps)
8595ecc953bSthorpej {
8605ecc953bSthorpej 	struct nvlist *nv;
861f079f926Schristos 	struct where *w;
8625ecc953bSthorpej 
8635ecc953bSthorpej 	/*
8645ecc953bSthorpej 	 * Mark these options as ones to skip when creating the Makefile.
8655ecc953bSthorpej 	 */
8665ecc953bSthorpej 	for (nv = fses; nv != NULL; nv = nv->nv_next) {
867f079f926Schristos 		if ((w = DEFINED_OPTION(nv->nv_name)) != NULL) {
868f079f926Schristos 			cfgerror("file system or option `%s' already defined"
869f079f926Schristos 			    " at %s:%hu", nv->nv_name, w->w_srcfile,
870f079f926Schristos 			    w->w_srcline);
8715ecc953bSthorpej 			return;
8725ecc953bSthorpej 		}
8735ecc953bSthorpej 
8745ecc953bSthorpej 		/*
8755ecc953bSthorpej 		 * Also mark it as a valid file system, which may be
8765ecc953bSthorpej 		 * used in "file-system" directives in the config
8775ecc953bSthorpej 		 * file.
8785ecc953bSthorpej 		 */
87902245173Sdholland 		if (nvhash_insert(deffstab, nv->nv_name, nv))
8805ecc953bSthorpej 			panic("file system `%s' already in table?!",
8815ecc953bSthorpej 			    nv->nv_name);
8825ecc953bSthorpej 
883d74eb995Sdholland 		add_fs_dependencies(nv, deps);
884a5a68b71Suebayasi 
885a5a68b71Suebayasi 		/*
886a5a68b71Suebayasi 		 * Implicit attribute definition for filesystem.
887a5a68b71Suebayasi 		 */
888a5a68b71Suebayasi 		const char *n;
889a5a68b71Suebayasi 		n = strtolower(nv->nv_name);
890a5a68b71Suebayasi 		refattr(n);
8915ecc953bSthorpej 	}
8925ecc953bSthorpej }
8935ecc953bSthorpej 
8945ecc953bSthorpej /*
8955ecc953bSthorpej  * Sanity check a file name.
8965ecc953bSthorpej  */
8975ecc953bSthorpej int
badfilename(const char * fname)8985ecc953bSthorpej badfilename(const char *fname)
8995ecc953bSthorpej {
9005ecc953bSthorpej 	const char *n;
9015ecc953bSthorpej 
9025ecc953bSthorpej 	/*
9035ecc953bSthorpej 	 * We're putting multiple options into one file.  Sanity
9045ecc953bSthorpej 	 * check the file name.
9055ecc953bSthorpej 	 */
9065ecc953bSthorpej 	if (strchr(fname, '/') != NULL) {
907c7295a4cSchristos 		cfgerror("option file name contains a `/'");
9085ecc953bSthorpej 		return 1;
9095ecc953bSthorpej 	}
9105ecc953bSthorpej 	if ((n = strrchr(fname, '.')) == NULL || strcmp(n, ".h") != 0) {
911c7295a4cSchristos 		cfgerror("option file name does not end in `.h'");
9125ecc953bSthorpej 		return 1;
9135ecc953bSthorpej 	}
9145ecc953bSthorpej 	return 0;
9155ecc953bSthorpej }
9165ecc953bSthorpej 
9175ecc953bSthorpej 
9185ecc953bSthorpej /*
9195ecc953bSthorpej  * Search for a defined option (defopt, filesystem, etc), and if found,
9205ecc953bSthorpej  * return the option's struct nvlist.
921d74eb995Sdholland  *
922d74eb995Sdholland  * This used to be one function (find_declared_option) before options
923d74eb995Sdholland  * and filesystems became different types.
9245ecc953bSthorpej  */
925d74eb995Sdholland static struct defoptlist *
find_declared_option_option(const char * name)926d74eb995Sdholland find_declared_option_option(const char *name)
9275ecc953bSthorpej {
928d74eb995Sdholland 	struct defoptlist *option;
9295ecc953bSthorpej 
930d74eb995Sdholland 	if ((option = dlhash_lookup(defopttab, name)) != NULL ||
931d74eb995Sdholland 	    (option = dlhash_lookup(defparamtab, name)) != NULL ||
932d74eb995Sdholland 	    (option = dlhash_lookup(defflagtab, name)) != NULL) {
9335ecc953bSthorpej 		return (option);
9345ecc953bSthorpej 	}
9355ecc953bSthorpej 
9365ecc953bSthorpej 	return (NULL);
9375ecc953bSthorpej }
9385ecc953bSthorpej 
939d74eb995Sdholland static struct nvlist *
find_declared_fs_option(const char * name)940d74eb995Sdholland find_declared_fs_option(const char *name)
941d74eb995Sdholland {
942d74eb995Sdholland 	struct nvlist *fs;
943d74eb995Sdholland 
944d74eb995Sdholland 	if ((fs = nvhash_lookup(deffstab, name)) != NULL) {
945d74eb995Sdholland 		return fs;
946d74eb995Sdholland 	}
947d74eb995Sdholland 
948d74eb995Sdholland 	return (NULL);
949d74eb995Sdholland }
950d74eb995Sdholland 
951d74eb995Sdholland /*
952d74eb995Sdholland  * Like find_declared_option but doesn't return what it finds, so it
953d74eb995Sdholland  * can search both the various kinds of options and also filesystems.
954d74eb995Sdholland  */
955f079f926Schristos struct where *
find_declared_option(const char * name)956f079f926Schristos find_declared_option(const char *name)
957d74eb995Sdholland {
958d74eb995Sdholland 	struct defoptlist *option = NULL;
959d74eb995Sdholland 	struct nvlist *fs;
960d74eb995Sdholland 
961d74eb995Sdholland 	if ((option = dlhash_lookup(defopttab, name)) != NULL ||
962d74eb995Sdholland 	    (option = dlhash_lookup(defparamtab, name)) != NULL ||
963d74eb995Sdholland 	    (option = dlhash_lookup(defflagtab, name)) != NULL) {
964f079f926Schristos 		return &option->dl_where;
965d74eb995Sdholland 	}
966d74eb995Sdholland 	if ((fs = nvhash_lookup(deffstab, name)) != NULL) {
967f079f926Schristos 		return &fs->nv_where;
968d74eb995Sdholland 	}
969d74eb995Sdholland 
970f079f926Schristos 	return NULL;
971d74eb995Sdholland }
9725ecc953bSthorpej 
9735ecc953bSthorpej /*
9745ecc953bSthorpej  * Define one or more standard options.  If an option file name is specified,
9755ecc953bSthorpej  * place all options in one file with the specified name.  Otherwise, create
9765ecc953bSthorpej  * an option file for each option.
9775ecc953bSthorpej  * record the option information in the specified table.
9785ecc953bSthorpej  */
9795ecc953bSthorpej void
defopt(struct dlhash * ht,const char * fname,struct defoptlist * opts,struct nvlist * deps,int obs)980d74eb995Sdholland defopt(struct dlhash *ht, const char *fname, struct defoptlist *opts,
981ac24e161Scube        struct nvlist *deps, int obs)
9825ecc953bSthorpej {
983d74eb995Sdholland 	struct defoptlist *dl, *nextdl, *olddl;
9845ecc953bSthorpej 	const char *name;
985f079f926Schristos 	struct where *w;
9865ecc953bSthorpej 	char buf[500];
9875ecc953bSthorpej 
9885ecc953bSthorpej 	if (fname != NULL && badfilename(fname)) {
9895ecc953bSthorpej 		return;
9905ecc953bSthorpej 	}
9915ecc953bSthorpej 
9925ecc953bSthorpej 	/*
9935ecc953bSthorpej 	 * Mark these options as ones to skip when creating the Makefile.
9945ecc953bSthorpej 	 */
995d74eb995Sdholland 	for (dl = opts; dl != NULL; dl = nextdl) {
996d74eb995Sdholland 		nextdl = dl->dl_next;
9975ecc953bSthorpej 
998d74eb995Sdholland 		if (dl->dl_lintvalue != NULL) {
99990426267Scube 			/*
100090426267Scube 			 * If an entry already exists, then we are about to
100190426267Scube 			 * complain, so no worry.
100290426267Scube 			 */
1003d74eb995Sdholland 			(void) dlhash_insert(defoptlint, dl->dl_name,
1004d74eb995Sdholland 			    dl);
100590426267Scube 		}
100690426267Scube 
10075ecc953bSthorpej 		/* An option name can be declared at most once. */
1008f079f926Schristos 		if ((w = DEFINED_OPTION(dl->dl_name)) != NULL) {
1009f079f926Schristos 			cfgerror("file system or option `%s' already defined"
1010f079f926Schristos 			    " at %s:%hu", dl->dl_name, w->w_srcfile,
1011f079f926Schristos 			    w->w_srcline);
10125ecc953bSthorpej 			return;
10135ecc953bSthorpej 		}
10145ecc953bSthorpej 
1015d74eb995Sdholland 		if (dlhash_insert(ht, dl->dl_name, dl)) {
1016f079f926Schristos 			cfgerror("file system or option `%s' already defined"
1017f079f926Schristos 			    " at %s:%hu", dl->dl_name, dl->dl_where.w_srcfile,
1018f079f926Schristos 			    dl->dl_where.w_srcline);
10195ecc953bSthorpej 			return;
10205ecc953bSthorpej 		}
10215ecc953bSthorpej 
10225ecc953bSthorpej 		if (fname == NULL) {
10235ecc953bSthorpej 			/*
10245ecc953bSthorpej 			 * Each option will be going into its own file.
10255ecc953bSthorpej 			 * Convert the option name to lower case.  This
10265ecc953bSthorpej 			 * lower case name will be used as the option
10275ecc953bSthorpej 			 * file name.
10285ecc953bSthorpej 			 */
10295ecc953bSthorpej 			(void) snprintf(buf, sizeof(buf), "opt_%s.h",
1030d74eb995Sdholland 			    strtolower(dl->dl_name));
10315ecc953bSthorpej 			name = intern(buf);
10325ecc953bSthorpej 		} else {
10335ecc953bSthorpej 			name = fname;
10345ecc953bSthorpej 		}
10355ecc953bSthorpej 
1036d74eb995Sdholland 		add_opt_dependencies(dl, deps);
10375ecc953bSthorpej 
10385ecc953bSthorpej 		/*
10395ecc953bSthorpej 		 * Remove this option from the parameter list before adding
10405ecc953bSthorpej 		 * it to the list associated with this option file.
10415ecc953bSthorpej 		 */
1042d74eb995Sdholland 		dl->dl_next = NULL;
10435ecc953bSthorpej 
10445ecc953bSthorpej 		/*
1045ac24e161Scube 		 * Flag as obsolete, if requested.
1046ac24e161Scube 		 */
1047ac24e161Scube 		if (obs) {
1048d74eb995Sdholland 			dl->dl_obsolete = 1;
1049d74eb995Sdholland 			(void)dlhash_insert(obsopttab, dl->dl_name, dl);
1050ac24e161Scube 		}
1051ac24e161Scube 
1052ac24e161Scube 		/*
10535ecc953bSthorpej 		 * Add this option file if we haven't seen it yet.
10545ecc953bSthorpej 		 * Otherwise, append to the list of options already
10555ecc953bSthorpej 		 * associated with this file.
10565ecc953bSthorpej 		 */
1057d74eb995Sdholland 		if ((olddl = dlhash_lookup(optfiletab, name)) == NULL) {
1058d74eb995Sdholland 			(void)dlhash_insert(optfiletab, name, dl);
10595ecc953bSthorpej 		} else {
1060d74eb995Sdholland 			while (olddl->dl_next != NULL)
1061d74eb995Sdholland 				olddl = olddl->dl_next;
1062d74eb995Sdholland 			olddl->dl_next = dl;
10635ecc953bSthorpej 		}
10645ecc953bSthorpej 	}
10655ecc953bSthorpej }
10665ecc953bSthorpej 
10675ecc953bSthorpej /*
10685ecc953bSthorpej  * Define one or more standard options.  If an option file name is specified,
10695ecc953bSthorpej  * place all options in one file with the specified name.  Otherwise, create
10705ecc953bSthorpej  * an option file for each option.
10715ecc953bSthorpej  */
10725ecc953bSthorpej void
defoption(const char * fname,struct defoptlist * opts,struct nvlist * deps)1073d74eb995Sdholland defoption(const char *fname, struct defoptlist *opts, struct nvlist *deps)
10745ecc953bSthorpej {
10755ecc953bSthorpej 
1076c7295a4cSchristos 	cfgwarn("The use of `defopt' is deprecated");
1077ac24e161Scube 	defopt(defopttab, fname, opts, deps, 0);
10785ecc953bSthorpej }
10795ecc953bSthorpej 
10805ecc953bSthorpej 
10815ecc953bSthorpej /*
10825ecc953bSthorpej  * Define an option for which a value is required.
10835ecc953bSthorpej  */
10845ecc953bSthorpej void
defparam(const char * fname,struct defoptlist * opts,struct nvlist * deps,int obs)1085d74eb995Sdholland defparam(const char *fname, struct defoptlist *opts, struct nvlist *deps, int obs)
10865ecc953bSthorpej {
10875ecc953bSthorpej 
1088ac24e161Scube 	defopt(defparamtab, fname, opts, deps, obs);
10895ecc953bSthorpej }
10905ecc953bSthorpej 
10915ecc953bSthorpej /*
109272303951Scube  * Define an option which must not have a value, and which
10935ecc953bSthorpej  * emits a "needs-flag" style output.
10945ecc953bSthorpej  */
10955ecc953bSthorpej void
defflag(const char * fname,struct defoptlist * opts,struct nvlist * deps,int obs)1096d74eb995Sdholland defflag(const char *fname, struct defoptlist *opts, struct nvlist *deps, int obs)
10975ecc953bSthorpej {
10985ecc953bSthorpej 
1099ac24e161Scube 	defopt(defflagtab, fname, opts, deps, obs);
11005ecc953bSthorpej }
11015ecc953bSthorpej 
11025ecc953bSthorpej 
11035ecc953bSthorpej /*
11045ecc953bSthorpej  * Add an option from "options FOO".  Note that this selects things that
11055ecc953bSthorpej  * are "optional foo".
11065ecc953bSthorpej  */
11075ecc953bSthorpej void
addoption(const char * name,const char * value)11085ecc953bSthorpej addoption(const char *name, const char *value)
11095ecc953bSthorpej {
11105ecc953bSthorpej 	const char *n;
1111c7295a4cSchristos 	int is_fs, is_param, is_flag, is_undecl, is_obs;
11125ecc953bSthorpej 
11135ecc953bSthorpej 	/*
11145ecc953bSthorpej 	 * Figure out how this option was declared (if at all.)
11155ecc953bSthorpej 	 * XXX should use "params" and "flags" in config.
11165ecc953bSthorpej 	 * XXX crying out for a type field in a unified hashtab.
11175ecc953bSthorpej 	 */
11185ecc953bSthorpej 	is_fs = OPT_FSOPT(name);
11195ecc953bSthorpej 	is_param = OPT_DEFPARAM(name);
11205ecc953bSthorpej 	is_flag =  OPT_DEFFLAG(name);
1121ac24e161Scube 	is_obs = OPT_OBSOLETE(name);
11225ecc953bSthorpej 	is_undecl = !DEFINED_OPTION(name);
11235ecc953bSthorpej 
1124ac24e161Scube 	/* Warn and pretend the user had not selected the option  */
1125ac24e161Scube 	if (is_obs) {
1126c7295a4cSchristos 		cfgwarn("obsolete option `%s' will be ignored", name);
1127ac24e161Scube 		return;
1128ac24e161Scube 	}
1129ac24e161Scube 
11305ecc953bSthorpej 	/* Make sure this is not a defined file system. */
11315ecc953bSthorpej 	if (is_fs) {
1132c7295a4cSchristos 		cfgerror("`%s' is a defined file system", name);
11335ecc953bSthorpej 		return;
11345ecc953bSthorpej 	}
11355ecc953bSthorpej 	/* A defparam must have a value */
11365ecc953bSthorpej 	if (is_param && value == NULL) {
1137c7295a4cSchristos 		cfgerror("option `%s' must have a value", name);
11385ecc953bSthorpej 		return;
11395ecc953bSthorpej 	}
11405ecc953bSthorpej 	/* A defflag must not have a value */
11415ecc953bSthorpej 	if (is_flag && value != NULL) {
1142c7295a4cSchristos 		cfgerror("option `%s' must not have a value", name);
11435ecc953bSthorpej 		return;
11445ecc953bSthorpej 	}
11455ecc953bSthorpej 
11465ecc953bSthorpej 	if (is_undecl && vflag) {
1147c7295a4cSchristos 		cfgwarn("undeclared option `%s' added to IDENT", name);
11485ecc953bSthorpej 	}
11495ecc953bSthorpej 
1150c7c59663Spgoyette 	if (do_option(opttab, &options, &nextopt, name, value, "options",
1151c7c59663Spgoyette 	    selecttab))
11525ecc953bSthorpej 		return;
11535ecc953bSthorpej 
11545ecc953bSthorpej 	/* make lowercase, then add to select table */
11555ecc953bSthorpej 	n = strtolower(name);
1156c7295a4cSchristos 	(void)ht_insert(selecttab, n, (void *)__UNCONST(n));
1157b599bf99Suebayasi 	CFGDBG(3, "option selected `%s'", n);
11585ecc953bSthorpej }
11595ecc953bSthorpej 
11605ecc953bSthorpej void
deloption(const char * name,int nowarn)11613dbd1573Schristos deloption(const char *name, int nowarn)
11625ecc953bSthorpej {
11635ecc953bSthorpej 
116475eb07f2Suebayasi 	CFGDBG(4, "deselecting opt `%s'", name);
11653dbd1573Schristos 	if (undo_option(opttab, &options, &nextopt, name, "options", nowarn))
11665ecc953bSthorpej 		return;
11673dbd1573Schristos 	if (undo_option(selecttab, NULL, NULL, strtolower(name), "options", nowarn))
11685ecc953bSthorpej 		return;
11695ecc953bSthorpej }
11705ecc953bSthorpej 
11715ecc953bSthorpej /*
11725ecc953bSthorpej  * Add a file system option.  This routine simply inserts the name into
11735ecc953bSthorpej  * a list of valid file systems, which is used to validate the root
11745ecc953bSthorpej  * file system type.  The name is then treated like a standard option.
11755ecc953bSthorpej  */
11765ecc953bSthorpej void
addfsoption(const char * name)11775ecc953bSthorpej addfsoption(const char *name)
11785ecc953bSthorpej {
11795ecc953bSthorpej 	const char *n;
11805ecc953bSthorpej 
11815ecc953bSthorpej 	/* Make sure this is a defined file system. */
11825ecc953bSthorpej 	if (!OPT_FSOPT(name)) {
1183c7295a4cSchristos 		cfgerror("`%s' is not a defined file system", name);
11845ecc953bSthorpej 		return;
11855ecc953bSthorpej 	}
11865ecc953bSthorpej 
11875ecc953bSthorpej 	/*
11885ecc953bSthorpej 	 * Convert to lower case.  This will be used in the select
1189baec5c01Scube 	 * table, to verify root file systems.
11905ecc953bSthorpej 	 */
11915ecc953bSthorpej 	n = strtolower(name);
11925ecc953bSthorpej 
1193c7c59663Spgoyette 	if (do_option(fsopttab, &fsoptions, &nextfsopt, name, n, "file-system",
1194c7c59663Spgoyette 	    selecttab))
11955ecc953bSthorpej 		return;
11965ecc953bSthorpej 
11975ecc953bSthorpej 	/* Add to select table. */
1198c7295a4cSchristos 	(void)ht_insert(selecttab, n, __UNCONST(n));
1199b599bf99Suebayasi 	CFGDBG(3, "fs selected `%s'", name);
1200e845f2ccSuebayasi 
1201e845f2ccSuebayasi 	/*
1202e845f2ccSuebayasi 	 * Select attribute if one exists.
1203e845f2ccSuebayasi 	 */
1204e845f2ccSuebayasi 	struct attr *a;
1205e845f2ccSuebayasi 	if ((a = ht_lookup(attrtab, n)) != NULL)
1206e845f2ccSuebayasi 		selectattr(a);
12075ecc953bSthorpej }
12085ecc953bSthorpej 
12095ecc953bSthorpej void
delfsoption(const char * name,int nowarn)12103dbd1573Schristos delfsoption(const char *name, int nowarn)
12115ecc953bSthorpej {
12125ecc953bSthorpej 	const char *n;
12135ecc953bSthorpej 
121475eb07f2Suebayasi 	CFGDBG(4, "deselecting fs `%s'", name);
12155ecc953bSthorpej 	n = strtolower(name);
12163dbd1573Schristos 	if (undo_option(fsopttab, &fsoptions, &nextfsopt, name, "file-system", nowarn))
12175ecc953bSthorpej 		return;
12183dbd1573Schristos 	if (undo_option(selecttab, NULL, NULL, n, "file-system", nowarn))
12195ecc953bSthorpej 		return;
12205ecc953bSthorpej }
12215ecc953bSthorpej 
12225ecc953bSthorpej /*
12235ecc953bSthorpej  * Add a "make" option.
12245ecc953bSthorpej  */
12255ecc953bSthorpej void
addmkoption(const char * name,const char * value)12265ecc953bSthorpej addmkoption(const char *name, const char *value)
12275ecc953bSthorpej {
12285ecc953bSthorpej 
1229c7c59663Spgoyette 	(void)do_option(mkopttab, &mkoptions, &nextmkopt, name, value,
1230c7c59663Spgoyette 		        "makeoptions", NULL);
12315ecc953bSthorpej }
12325ecc953bSthorpej 
12335ecc953bSthorpej void
delmkoption(const char * name,int nowarn)12343dbd1573Schristos delmkoption(const char *name, int nowarn)
12355ecc953bSthorpej {
12365ecc953bSthorpej 
123775eb07f2Suebayasi 	CFGDBG(4, "deselecting mkopt `%s'", name);
12385ecc953bSthorpej 	(void)undo_option(mkopttab, &mkoptions, &nextmkopt, name,
12393dbd1573Schristos 	    "makeoptions", nowarn);
12405ecc953bSthorpej }
12415ecc953bSthorpej 
12425ecc953bSthorpej /*
12435ecc953bSthorpej  * Add an appending "make" option.
12445ecc953bSthorpej  */
12455ecc953bSthorpej void
appendmkoption(const char * name,const char * value)12465ecc953bSthorpej appendmkoption(const char *name, const char *value)
12475ecc953bSthorpej {
12485ecc953bSthorpej 	struct nvlist *nv;
12495ecc953bSthorpej 
12505ecc953bSthorpej 	nv = newnv(name, value, NULL, 0, NULL);
12515ecc953bSthorpej 	*nextappmkopt = nv;
12525ecc953bSthorpej 	nextappmkopt = &nv->nv_next;
12535ecc953bSthorpej }
12545ecc953bSthorpej 
12555ecc953bSthorpej /*
12565ecc953bSthorpej  * Add a conditional appending "make" option.
12575ecc953bSthorpej  */
12585ecc953bSthorpej void
appendcondmkoption(struct condexpr * cond,const char * name,const char * value)12599483bda7Sdholland appendcondmkoption(struct condexpr *cond, const char *name, const char *value)
12605ecc953bSthorpej {
1261a16a6365Scube 	struct nvlist *nv;
12625ecc953bSthorpej 
12639483bda7Sdholland 	nv = newnv(name, value, cond, 0, NULL);
1264a16a6365Scube 	*nextcndmkopt = nv;
1265a16a6365Scube 	nextcndmkopt = &nv->nv_next;
12665ecc953bSthorpej }
12675ecc953bSthorpej 
12685ecc953bSthorpej /*
1269e8a8b10dSuebayasi  * Copy maxusers to param "MAXUSERS".
1270e8a8b10dSuebayasi  */
1271e8a8b10dSuebayasi void
fixmaxusers(void)1272e8a8b10dSuebayasi fixmaxusers(void)
1273e8a8b10dSuebayasi {
1274e8a8b10dSuebayasi 	char str[32];
1275e8a8b10dSuebayasi 
1276e8a8b10dSuebayasi 	snprintf(str, sizeof(str), "%d", maxusers);
1277e8a8b10dSuebayasi 	addoption(intern("MAXUSERS"), intern(str));
1278e8a8b10dSuebayasi }
1279e8a8b10dSuebayasi 
1280e8a8b10dSuebayasi /*
12810b1d177bSuebayasi  * Copy makeoptions to params with "makeoptions_" prefix.
12820b1d177bSuebayasi  */
12830b1d177bSuebayasi void
fixmkoption(void)12840b1d177bSuebayasi fixmkoption(void)
12850b1d177bSuebayasi {
12860b1d177bSuebayasi 	struct nvlist *nv;
12870b1d177bSuebayasi 	char buf[100];
12880b1d177bSuebayasi 	const char *name;
12890b1d177bSuebayasi 
12900b1d177bSuebayasi 	for (nv = mkoptions; nv != NULL; nv = nv->nv_next) {
12910b1d177bSuebayasi 		snprintf(buf, sizeof(buf), "makeoptions_%s", nv->nv_name);
12920b1d177bSuebayasi 		name = intern(buf);
12930b1d177bSuebayasi 		if (!DEFINED_OPTION(name) || !OPT_DEFPARAM(name))
12940b1d177bSuebayasi 			continue;
12950b1d177bSuebayasi 		addoption(name, intern(nv->nv_str));
12960b1d177bSuebayasi 	}
12970b1d177bSuebayasi }
12980b1d177bSuebayasi 
12990b1d177bSuebayasi /*
13005ecc953bSthorpej  * Add a name=value pair to an option list.  The value may be NULL.
13015ecc953bSthorpej  */
13025ecc953bSthorpej static int
do_option(struct hashtab * ht,struct nvlist ** npp,struct nvlist *** next,const char * name,const char * value,const char * type,struct hashtab * stab)1303c7c59663Spgoyette do_option(struct hashtab *ht, struct nvlist **npp, struct nvlist ***next,
1304c7c59663Spgoyette 	  const char *name, const char *value, const char *type,
1305c7c59663Spgoyette 	  struct hashtab *stab)
13065ecc953bSthorpej {
1307c7c59663Spgoyette 	struct nvlist *nv, *onv;
13085ecc953bSthorpej 
13095ecc953bSthorpej 	/* assume it will work */
13105ecc953bSthorpej 	nv = newnv(name, value, NULL, 0, NULL);
1311c7c59663Spgoyette 	if (ht_insert(ht, name, nv) != 0) {
13125ecc953bSthorpej 
1313c7c59663Spgoyette 		/* oops, already got that option - remove it first */
1314c7c59663Spgoyette 		if ((onv = ht_lookup(ht, name)) == NULL)
1315c7c59663Spgoyette 			panic("do_option 1");
1316c7c59663Spgoyette 		if (onv->nv_str != NULL && !OPT_FSOPT(name))
1317c7c59663Spgoyette 			cfgwarn("already have %s `%s=%s'", type, name,
1318c7c59663Spgoyette 			    onv->nv_str);
13195ecc953bSthorpej 		else
1320c7c59663Spgoyette 			cfgwarn("already have %s `%s'", type, name);
1321c7c59663Spgoyette 
13223dbd1573Schristos 		if (undo_option(ht, npp, next, name, type, 0))
1323c7c59663Spgoyette 			panic("do_option 2");
1324c7c59663Spgoyette 		if (stab != NULL &&
13253dbd1573Schristos 		    undo_option(stab, NULL, NULL, strtolower(name), type, 0))
1326c7c59663Spgoyette 			panic("do_option 3");
1327c7c59663Spgoyette 
1328c7c59663Spgoyette 		/* now try adding it again */
1329c7c59663Spgoyette 		if (ht_insert(ht, name, nv) != 0)
1330c7c59663Spgoyette 			panic("do_option 4");
1331c7c59663Spgoyette 
1332c7c59663Spgoyette 		CFGDBG(2, "opt `%s' replaced", name);
1333c7c59663Spgoyette 	}
1334c7c59663Spgoyette 	**next = nv;
1335c7c59663Spgoyette 	*next = &nv->nv_next;
1336c7c59663Spgoyette 
1337c7c59663Spgoyette 	return (0);
13385ecc953bSthorpej }
13395ecc953bSthorpej 
13405ecc953bSthorpej /*
13415ecc953bSthorpej  * Remove a name from a hash table,
13425ecc953bSthorpej  * and optionally, a name=value pair from an option list.
13435ecc953bSthorpej  */
13445ecc953bSthorpej static int
undo_option(struct hashtab * ht,struct nvlist ** npp,struct nvlist *** next,const char * name,const char * type,int nowarn)13455ecc953bSthorpej undo_option(struct hashtab *ht, struct nvlist **npp,
13463dbd1573Schristos     struct nvlist ***next, const char *name, const char *type, int nowarn)
13475ecc953bSthorpej {
13485ecc953bSthorpej 	struct nvlist *nv;
13495ecc953bSthorpej 
13505ecc953bSthorpej 	if (ht_remove(ht, name)) {
1351d2737476Smartin 		/*
1352d2737476Smartin 		 * -U command line option removals are always silent
1353d2737476Smartin 		 */
13543dbd1573Schristos 		if (!handling_cmdlineopts && !nowarn)
135534e6631aSbad 			cfgwarn("%s `%s' is not defined", type, name);
13565ecc953bSthorpej 		return (1);
13575ecc953bSthorpej 	}
135875eb07f2Suebayasi 	if (npp == NULL) {
135975eb07f2Suebayasi 		CFGDBG(2, "opt `%s' deselected", name);
13605ecc953bSthorpej 		return (0);
136175eb07f2Suebayasi 	}
13625ecc953bSthorpej 
13635ecc953bSthorpej 	for ( ; *npp != NULL; npp = &(*npp)->nv_next) {
13645ecc953bSthorpej 		if ((*npp)->nv_name != name)
13655ecc953bSthorpej 			continue;
13665ecc953bSthorpej 		if (next != NULL && *next == &(*npp)->nv_next)
13675ecc953bSthorpej 			*next = npp;
13685ecc953bSthorpej 		nv = (*npp)->nv_next;
136975eb07f2Suebayasi 		CFGDBG(2, "opt `%s' deselected", (*npp)->nv_name);
13705ecc953bSthorpej 		nvfree(*npp);
13715ecc953bSthorpej 		*npp = nv;
13725ecc953bSthorpej 		return (0);
13735ecc953bSthorpej 	}
13745ecc953bSthorpej 	panic("%s `%s' is not defined in nvlist", type, name);
13755ecc953bSthorpej 	return (1);
13765ecc953bSthorpej }
13775ecc953bSthorpej 
13785ecc953bSthorpej /*
13795ecc953bSthorpej  * Return true if there is at least one instance of the given unit
13805ecc953bSthorpej  * on the given device attachment (or any units, if unit == WILD).
13815ecc953bSthorpej  */
13825ecc953bSthorpej int
deva_has_instances(struct deva * deva,int unit)13835ecc953bSthorpej deva_has_instances(struct deva *deva, int unit)
13845ecc953bSthorpej {
13855ecc953bSthorpej 	struct devi *i;
13865ecc953bSthorpej 
138790ac64deSpooka 	/*
138890ac64deSpooka 	 * EHAMMERTOOBIG: we shouldn't check i_pseudoroot here.
138990ac64deSpooka 	 * What we want by this check is them to appear non-present
139090ac64deSpooka 	 * except for purposes of other devices being able to attach
139190ac64deSpooka 	 * to them.
139290ac64deSpooka 	 */
13935ecc953bSthorpej 	for (i = deva->d_ihead; i != NULL; i = i->i_asame)
139490ac64deSpooka 		if (i->i_active == DEVI_ACTIVE && i->i_pseudoroot == 0 &&
1395c130d400Scube 		    (unit == WILD || unit == i->i_unit || i->i_unit == STAR))
13965ecc953bSthorpej 			return (1);
13975ecc953bSthorpej 	return (0);
13985ecc953bSthorpej }
13995ecc953bSthorpej 
14005ecc953bSthorpej /*
14015ecc953bSthorpej  * Return true if there is at least one instance of the given unit
14025ecc953bSthorpej  * on the given base (or any units, if unit == WILD).
14035ecc953bSthorpej  */
14045ecc953bSthorpej int
devbase_has_instances(struct devbase * dev,int unit)14055ecc953bSthorpej devbase_has_instances(struct devbase *dev, int unit)
14065ecc953bSthorpej {
14075ecc953bSthorpej 	struct deva *da;
14085ecc953bSthorpej 
14095ecc953bSthorpej 	/*
14105ecc953bSthorpej 	 * Pseudo-devices are a little special.  We consider them
14115ecc953bSthorpej 	 * to have instances only if they are both:
14125ecc953bSthorpej 	 *
14135ecc953bSthorpej 	 *	1. Included in this kernel configuration.
14145ecc953bSthorpej 	 *
1415b66156c7Sdrochner 	 *	2. Be declared "defpseudodev".
14165ecc953bSthorpej 	 */
14175ecc953bSthorpej 	if (dev->d_ispseudo) {
1418b66156c7Sdrochner 		return ((ht_lookup(devitab, dev->d_name) != NULL)
1419b66156c7Sdrochner 			&& (dev->d_ispseudo > 1));
14205ecc953bSthorpej 	}
14215ecc953bSthorpej 
14225ecc953bSthorpej 	for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
14235ecc953bSthorpej 		if (deva_has_instances(da, unit))
14245ecc953bSthorpej 			return (1);
14255ecc953bSthorpej 	return (0);
14265ecc953bSthorpej }
14275ecc953bSthorpej 
14285ecc953bSthorpej static int
cfcrosscheck(struct config * cf,const char * what,struct nvlist * nv)14295ecc953bSthorpej cfcrosscheck(struct config *cf, const char *what, struct nvlist *nv)
14305ecc953bSthorpej {
14315ecc953bSthorpej 	struct devbase *dev;
14325ecc953bSthorpej 	struct devi *pd;
14335ecc953bSthorpej 	int errs, devunit;
14345ecc953bSthorpej 
14355ecc953bSthorpej 	if (maxpartitions <= 0)
14365ecc953bSthorpej 		panic("cfcrosscheck");
14375ecc953bSthorpej 
14385ecc953bSthorpej 	for (errs = 0; nv != NULL; nv = nv->nv_next) {
14395ecc953bSthorpej 		if (nv->nv_name == NULL)
14405ecc953bSthorpej 			continue;
14415ecc953bSthorpej 		dev = ht_lookup(devbasetab, nv->nv_name);
14425ecc953bSthorpej 		if (dev == NULL)
14435ecc953bSthorpej 			panic("cfcrosscheck(%s)", nv->nv_name);
14445ecc953bSthorpej 		if (has_attr(dev->d_attrs, s_ifnet))
14455ecc953bSthorpej 			devunit = nv->nv_ifunit;	/* XXX XXX XXX */
14465ecc953bSthorpej 		else
14470001b928Schristos 			devunit = (int)(minor(nv->nv_num) / maxpartitions);
14485ecc953bSthorpej 		if (devbase_has_instances(dev, devunit))
14495ecc953bSthorpej 			continue;
14505ecc953bSthorpej 		if (devbase_has_instances(dev, STAR) &&
14515ecc953bSthorpej 		    devunit >= dev->d_umax)
14525ecc953bSthorpej 			continue;
14535ecc953bSthorpej 		TAILQ_FOREACH(pd, &allpseudo, i_next) {
14545ecc953bSthorpej 			if (pd->i_base == dev && devunit < dev->d_umax &&
14555ecc953bSthorpej 			    devunit >= 0)
14565ecc953bSthorpej 				goto loop;
14575ecc953bSthorpej 		}
14585ecc953bSthorpej 		(void)fprintf(stderr,
1459f079f926Schristos 		    "%s:%hu: %s says %s on %s, but there's no %s\n",
1460f079f926Schristos 		    conffile, cf->cf_where.w_srcline,
14615ecc953bSthorpej 		    cf->cf_name, what, nv->nv_str, nv->nv_str);
14625ecc953bSthorpej 		errs++;
14635ecc953bSthorpej  loop:
14645ecc953bSthorpej 		;
14655ecc953bSthorpej 	}
14665ecc953bSthorpej 	return (errs);
14675ecc953bSthorpej }
14685ecc953bSthorpej 
14695ecc953bSthorpej /*
14705ecc953bSthorpej  * Cross-check the configuration: make sure that each target device
14715ecc953bSthorpej  * or attribute (`at foo[0*?]') names at least one real device.  Also
14725ecc953bSthorpej  * see that the root and dump devices for all configurations are there.
14735ecc953bSthorpej  */
14745ecc953bSthorpej int
crosscheck(void)14755ecc953bSthorpej crosscheck(void)
14765ecc953bSthorpej {
14775ecc953bSthorpej 	struct config *cf;
14785ecc953bSthorpej 	int errs;
14795ecc953bSthorpej 
14805ecc953bSthorpej 	errs = 0;
14815ecc953bSthorpej 	if (TAILQ_EMPTY(&allcf)) {
1482c7295a4cSchristos 		warnx("%s has no configurations!", conffile);
14835ecc953bSthorpej 		errs++;
14845ecc953bSthorpej 	}
14855ecc953bSthorpej 	TAILQ_FOREACH(cf, &allcf, cf_next) {
14865ecc953bSthorpej 		if (cf->cf_root != NULL) {	/* i.e., not root on ? */
14875ecc953bSthorpej 			errs += cfcrosscheck(cf, "root", cf->cf_root);
14885ecc953bSthorpej 			errs += cfcrosscheck(cf, "dumps", cf->cf_dump);
14895ecc953bSthorpej 		}
14905ecc953bSthorpej 	}
14915ecc953bSthorpej 	return (errs);
14925ecc953bSthorpej }
14935ecc953bSthorpej 
14945ecc953bSthorpej /*
14955ecc953bSthorpej  * Check to see if there is a *'d unit with a needs-count file.
14965ecc953bSthorpej  */
14975ecc953bSthorpej int
badstar(void)14985ecc953bSthorpej badstar(void)
14995ecc953bSthorpej {
15005ecc953bSthorpej 	struct devbase *d;
15015ecc953bSthorpej 	struct deva *da;
15025ecc953bSthorpej 	struct devi *i;
15035ecc953bSthorpej 	int errs, n;
15045ecc953bSthorpej 
15055ecc953bSthorpej 	errs = 0;
15065ecc953bSthorpej 	TAILQ_FOREACH(d, &allbases, d_next) {
15075ecc953bSthorpej 		for (da = d->d_ahead; da != NULL; da = da->d_bsame)
15085ecc953bSthorpej 			for (i = da->d_ihead; i != NULL; i = i->i_asame) {
15095ecc953bSthorpej 				if (i->i_unit == STAR)
15105ecc953bSthorpej 					goto aybabtu;
15115ecc953bSthorpej 			}
15125ecc953bSthorpej 		continue;
15135ecc953bSthorpej  aybabtu:
15145ecc953bSthorpej 		if (ht_lookup(needcnttab, d->d_name)) {
1515c7295a4cSchristos 			warnx("%s's cannot be *'d until its driver is fixed",
15165ecc953bSthorpej 			    d->d_name);
15175ecc953bSthorpej 			errs++;
15185ecc953bSthorpej 			continue;
15195ecc953bSthorpej 		}
15205ecc953bSthorpej 		for (n = 0; i != NULL; i = i->i_alias)
15215ecc953bSthorpej 			if (!i->i_collapsed)
15225ecc953bSthorpej 				n++;
15235ecc953bSthorpej 		if (n < 1)
15245ecc953bSthorpej 			panic("badstar() n<1");
15255ecc953bSthorpej 	}
15265ecc953bSthorpej 	return (errs);
15275ecc953bSthorpej }
15285ecc953bSthorpej 
15295ecc953bSthorpej /*
15305ecc953bSthorpej  * Verify/create builddir if necessary, change to it, and verify srcdir.
15315ecc953bSthorpej  * This will be called when we see the first include.
15325ecc953bSthorpej  */
15335ecc953bSthorpej void
setupdirs(void)15345ecc953bSthorpej setupdirs(void)
15355ecc953bSthorpej {
15365ecc953bSthorpej 	struct stat st;
15375ecc953bSthorpej 
15385ecc953bSthorpej 	/* srcdir must be specified if builddir is not specified or if
15395ecc953bSthorpej 	 * no configuration filename was specified. */
15405ecc953bSthorpej 	if ((builddir || strcmp(defbuilddir, ".") == 0) && !srcdir) {
1541c7295a4cSchristos 		cfgerror("source directory must be specified");
15425ecc953bSthorpej 		exit(1);
15435ecc953bSthorpej 	}
15445ecc953bSthorpej 
154559c94545Scube 	if (Lflag) {
154659c94545Scube 		if (srcdir == NULL)
154759c94545Scube 			srcdir = "../../..";
154859c94545Scube 		return;
154959c94545Scube 	}
155059c94545Scube 
15515ecc953bSthorpej 	if (srcdir == NULL)
15525ecc953bSthorpej 		srcdir = "../../../..";
15535ecc953bSthorpej 	if (builddir == NULL)
15545ecc953bSthorpej 		builddir = defbuilddir;
15555ecc953bSthorpej 
1556c7295a4cSchristos 	if (stat(builddir, &st) == -1) {
1557c7295a4cSchristos 		if (mkdir(builddir, 0777) == -1)
1558c7295a4cSchristos 			errx(EXIT_FAILURE, "cannot create %s", builddir);
1559c7295a4cSchristos 	} else if (!S_ISDIR(st.st_mode))
1560c7295a4cSchristos 		errx(EXIT_FAILURE, "%s is not a directory", builddir);
1561c7295a4cSchristos 	if (chdir(builddir) == -1)
1562c7295a4cSchristos 		err(EXIT_FAILURE, "cannot change to %s", builddir);
1563c7295a4cSchristos 	if (stat(srcdir, &st) == -1)
1564c7295a4cSchristos 		err(EXIT_FAILURE, "cannot stat %s", srcdir);
1565c7295a4cSchristos 	if (!S_ISDIR(st.st_mode))
1566c7295a4cSchristos 		errx(EXIT_FAILURE, "%s is not a directory", srcdir);
15675ecc953bSthorpej }
15685ecc953bSthorpej 
15695ecc953bSthorpej /*
15705ecc953bSthorpej  * Write identifier from "ident" directive into file, for
15715ecc953bSthorpej  * newvers.sh to pick it up.
15725ecc953bSthorpej  */
15735ecc953bSthorpej int
mkident(void)15745ecc953bSthorpej mkident(void)
15755ecc953bSthorpej {
15765ecc953bSthorpej 	FILE *fp;
157711820066Scube 	int error = 0;
15785ecc953bSthorpej 
15795ecc953bSthorpej 	(void)unlink("ident");
15805ecc953bSthorpej 
15815ecc953bSthorpej 	if (ident == NULL)
15825ecc953bSthorpej 		return (0);
15835ecc953bSthorpej 
15845ecc953bSthorpej 	if ((fp = fopen("ident", "w")) == NULL) {
1585c7295a4cSchristos 		warn("cannot write ident");
15865ecc953bSthorpej 		return (1);
15875ecc953bSthorpej 	}
15885ecc953bSthorpej 	if (vflag)
15895ecc953bSthorpej 		(void)printf("using ident '%s'\n", ident);
1590342d3579Sdsl 	fprintf(fp, "%s\n", ident);
1591342d3579Sdsl 	fflush(fp);
1592342d3579Sdsl 	if (ferror(fp))
159311820066Scube 		error = 1;
15945ecc953bSthorpej 	(void)fclose(fp);
15955ecc953bSthorpej 
159611820066Scube 	return error;
15975ecc953bSthorpej }
15985ecc953bSthorpej 
15995ecc953bSthorpej void
logconfig_start(void)16005ecc953bSthorpej logconfig_start(void)
16015ecc953bSthorpej {
16025ecc953bSthorpej 	extern FILE *yyin;
16035ecc953bSthorpej 	char line[1024];
16045ecc953bSthorpej 	const char *tmpdir;
16055ecc953bSthorpej 	struct stat st;
16065ecc953bSthorpej 	int fd;
16075ecc953bSthorpej 
16085ecc953bSthorpej 	if (yyin == NULL || fstat(fileno(yyin), &st) == -1)
16095ecc953bSthorpej 		return;
16105ecc953bSthorpej 	cfgtime = st.st_mtime;
16115ecc953bSthorpej 
16125ecc953bSthorpej 	tmpdir = getenv("TMPDIR");
16135ecc953bSthorpej 	if (tmpdir == NULL)
16143b398d5cSdholland 		tmpdir = _PATH_TMP;
1615c7295a4cSchristos 	(void)snprintf(line, sizeof(line), "%s/config.tmp.XXXXXX", tmpdir);
16165ecc953bSthorpej 	if ((fd = mkstemp(line)) == -1 ||
16175ecc953bSthorpej 	    (cfg = fdopen(fd, "r+")) == NULL) {
16185ecc953bSthorpej 		if (fd != -1) {
1619c7295a4cSchristos 			(void)unlink(line);
1620c7295a4cSchristos 			(void)close(fd);
16215ecc953bSthorpej 		}
16225ecc953bSthorpej 		cfg = NULL;
16235ecc953bSthorpej 		return;
16245ecc953bSthorpej 	}
1625c7295a4cSchristos 	(void)unlink(line);
16265ecc953bSthorpej 
16274e4935bbSuwe 	(void)fprintf(cfg, "#include <sys/cdefs.h>\n\n");
16285ecc953bSthorpej 	(void)fprintf(cfg, "#include \"opt_config.h\"\n");
16295ecc953bSthorpej 	(void)fprintf(cfg, "\n");
16305ecc953bSthorpej 	(void)fprintf(cfg, "/*\n");
16315ecc953bSthorpej 	(void)fprintf(cfg, " * Add either (or both) of\n");
16325ecc953bSthorpej 	(void)fprintf(cfg, " *\n");
16335ecc953bSthorpej 	(void)fprintf(cfg, " *\toptions %s\n", LOGCONFIG_LARGE);
16345ecc953bSthorpej 	(void)fprintf(cfg, " *\toptions %s\n", LOGCONFIG_SMALL);
16355ecc953bSthorpej 	(void)fprintf(cfg, " *\n");
16365ecc953bSthorpej 	(void)fprintf(cfg,
16375ecc953bSthorpej 	    " * to your kernel config file to embed it in the resulting\n");
16385ecc953bSthorpej 	(void)fprintf(cfg,
16395ecc953bSthorpej 	    " * kernel.  The latter option does not include files that are\n");
16405ecc953bSthorpej 	(void)fprintf(cfg,
16415ecc953bSthorpej 	    " * included (recursively) by your config file.  The embedded\n");
16425ecc953bSthorpej 	(void)fprintf(cfg,
16435ecc953bSthorpej 	    " * data be extracted by using the command:\n");
16445ecc953bSthorpej 	(void)fprintf(cfg, " *\n");
16455ecc953bSthorpej 	(void)fprintf(cfg,
16465ecc953bSthorpej 	    " *\tstrings netbsd | sed -n 's/^_CFG_//p' | unvis\n");
16475ecc953bSthorpej 	(void)fprintf(cfg, " */\n");
16485ecc953bSthorpej 	(void)fprintf(cfg, "\n");
16495ecc953bSthorpej 	(void)fprintf(cfg, "#ifdef CONFIG_FILE\n");
16505ecc953bSthorpej 	(void)fprintf(cfg, "#if defined(%s) || defined(%s)\n\n",
16515ecc953bSthorpej 	    LOGCONFIG_LARGE, LOGCONFIG_SMALL);
16524e4935bbSuwe 	(void)fprintf(cfg, "static const char config[] __used =\n\n");
16535ecc953bSthorpej 
16545ecc953bSthorpej 	(void)fprintf(cfg, "#ifdef %s\n\n", LOGCONFIG_LARGE);
16555ecc953bSthorpej 	(void)fprintf(cfg, "\"_CFG_### START CONFIG FILE \\\"%s\\\"\\n\"\n\n",
16565ecc953bSthorpej 	    conffile);
16575ecc953bSthorpej 	(void)fprintf(cfg, "#endif /* %s */\n\n", LOGCONFIG_LARGE);
16585ecc953bSthorpej 
16595ecc953bSthorpej 	logconfig_include(yyin, NULL);
16605ecc953bSthorpej 
16615ecc953bSthorpej 	(void)fprintf(cfg, "#ifdef %s\n\n", LOGCONFIG_LARGE);
16625ecc953bSthorpej 	(void)fprintf(cfg, "\"_CFG_### END CONFIG FILE \\\"%s\\\"\\n\"\n",
16635ecc953bSthorpej 	    conffile);
16645ecc953bSthorpej 
16655ecc953bSthorpej 	rewind(yyin);
16665ecc953bSthorpej }
16675ecc953bSthorpej 
16685ecc953bSthorpej void
logconfig_include(FILE * cf,const char * filename)16695ecc953bSthorpej logconfig_include(FILE *cf, const char *filename)
16705ecc953bSthorpej {
16715ecc953bSthorpej 	char line[1024], in[2048], *out;
16725ecc953bSthorpej 	struct stat st;
16735ecc953bSthorpej 	int missingeol;
16745ecc953bSthorpej 
16755ecc953bSthorpej 	if (!cfg)
16765ecc953bSthorpej 		return;
16775ecc953bSthorpej 
16785ecc953bSthorpej 	missingeol = 0;
16795ecc953bSthorpej 	if (fstat(fileno(cf), &st) == -1)
16805ecc953bSthorpej 		return;
16815ecc953bSthorpej 	if (cfgtime < st.st_mtime)
16825ecc953bSthorpej 		cfgtime = st.st_mtime;
16835ecc953bSthorpej 
16845ecc953bSthorpej 	if (filename)
16855ecc953bSthorpej 		(void)fprintf(cfg,
16865ecc953bSthorpej 		    "\"_CFG_### (included from \\\"%s\\\")\\n\"\n",
16875ecc953bSthorpej 		    filename);
16885ecc953bSthorpej 	while (fgets(line, sizeof(line), cf) != NULL) {
16895ecc953bSthorpej 		missingeol = 1;
16905ecc953bSthorpej 		(void)fprintf(cfg, "\"_CFG_");
16915ecc953bSthorpej 		if (filename)
16925ecc953bSthorpej 			(void)fprintf(cfg, "###> ");
16935ecc953bSthorpej 		strvis(in, line, VIS_TAB);
16945ecc953bSthorpej 		for (out = in; *out; out++)
16955ecc953bSthorpej 			switch (*out) {
16965ecc953bSthorpej 			case '\n':
16975ecc953bSthorpej 				(void)fprintf(cfg, "\\n\"\n");
16985ecc953bSthorpej 				missingeol = 0;
16995ecc953bSthorpej 				break;
17005ecc953bSthorpej 			case '"': case '\\':
17015ecc953bSthorpej 				(void)fputc('\\', cfg);
17025ecc953bSthorpej 				/* FALLTHROUGH */
17035ecc953bSthorpej 			default:
17045ecc953bSthorpej 				(void)fputc(*out, cfg);
17055ecc953bSthorpej 				break;
17065ecc953bSthorpej 			}
17075ecc953bSthorpej 	}
17085ecc953bSthorpej 	if (missingeol) {
17095ecc953bSthorpej 		(void)fprintf(cfg, "\\n\"\n");
1710c7295a4cSchristos 		warnx("%s: newline missing at EOF",
17115ecc953bSthorpej 		    filename != NULL ? filename : conffile);
17125ecc953bSthorpej 	}
17135ecc953bSthorpej 	if (filename)
17145ecc953bSthorpej 		(void)fprintf(cfg, "\"_CFG_### (end include \\\"%s\\\")\\n\"\n",
17155ecc953bSthorpej 		    filename);
17165ecc953bSthorpej 
17175ecc953bSthorpej 	rewind(cf);
17185ecc953bSthorpej }
17195ecc953bSthorpej 
17205ecc953bSthorpej void
logconfig_end(void)17215ecc953bSthorpej logconfig_end(void)
17225ecc953bSthorpej {
17235ecc953bSthorpej 	char line[1024];
17245ecc953bSthorpej 	FILE *fp;
17255ecc953bSthorpej 	struct stat st;
17265ecc953bSthorpej 
17275ecc953bSthorpej 	if (!cfg)
17285ecc953bSthorpej 		return;
17295ecc953bSthorpej 
17305ecc953bSthorpej 	(void)fprintf(cfg, "#endif /* %s */\n", LOGCONFIG_LARGE);
17315ecc953bSthorpej 	(void)fprintf(cfg, ";\n");
17325ecc953bSthorpej 	(void)fprintf(cfg, "#endif /* %s || %s */\n",
17335ecc953bSthorpej 	    LOGCONFIG_LARGE, LOGCONFIG_SMALL);
17345ecc953bSthorpej 	(void)fprintf(cfg, "#endif /* CONFIG_FILE */\n");
1735342d3579Sdsl 	fflush(cfg);
1736342d3579Sdsl 	if (ferror(cfg))
1737342d3579Sdsl 		err(EXIT_FAILURE, "write to temporary file for config.h failed");
17385ecc953bSthorpej 	rewind(cfg);
17395ecc953bSthorpej 
17405ecc953bSthorpej 	if (stat("config_file.h", &st) != -1) {
17415ecc953bSthorpej 		if (cfgtime < st.st_mtime) {
17425ecc953bSthorpej 			fclose(cfg);
17435ecc953bSthorpej 			return;
17445ecc953bSthorpej 		}
17455ecc953bSthorpej 	}
17465ecc953bSthorpej 
17475ecc953bSthorpej 	fp = fopen("config_file.h", "w");
1748342d3579Sdsl 	if (!fp)
1749342d3579Sdsl 		err(EXIT_FAILURE, "cannot open \"config.h\"");
17505ecc953bSthorpej 
17515ecc953bSthorpej 	while (fgets(line, sizeof(line), cfg) != NULL)
17525ecc953bSthorpej 		fputs(line, fp);
1753342d3579Sdsl 	fflush(fp);
1754342d3579Sdsl 	if (ferror(fp))
1755342d3579Sdsl 		err(EXIT_FAILURE, "write to \"config.h\" failed");
17565ecc953bSthorpej 	fclose(fp);
17575ecc953bSthorpej 	fclose(cfg);
17585ecc953bSthorpej }
17595ecc953bSthorpej 
1760a16a6365Scube const char *
strtolower(const char * name)17615ecc953bSthorpej strtolower(const char *name)
17625ecc953bSthorpej {
17635ecc953bSthorpej 	const char *n;
17645ecc953bSthorpej 	char *p, low[500];
17659a7f4bbaSchristos 	char c;
17665ecc953bSthorpej 
17675ecc953bSthorpej 	for (n = name, p = low; (c = *n) != '\0'; n++)
176858467814Schristos 		*p++ = (char)(isupper((u_char)c) ? tolower((u_char)c) : c);
176958467814Schristos 	*p = '\0';
17705ecc953bSthorpej 	return (intern(low));
17715ecc953bSthorpej }
17725ecc953bSthorpej 
17735ecc953bSthorpej static int
is_elf(const char * file)17745ecc953bSthorpej is_elf(const char *file)
17755ecc953bSthorpej {
17765ecc953bSthorpej 	int kernel;
17775ecc953bSthorpej 	char hdr[4];
17785ecc953bSthorpej 
17795ecc953bSthorpej 	kernel = open(file, O_RDONLY);
1780c7295a4cSchristos 	if (kernel == -1)
1781c7295a4cSchristos 		err(EXIT_FAILURE, "cannot open %s", file);
1782c7295a4cSchristos 	if (read(kernel, hdr, 4) != 4)
1783c7295a4cSchristos 		err(EXIT_FAILURE, "Cannot read from %s", file);
1784c7295a4cSchristos 	(void)close(kernel);
17855ecc953bSthorpej 
17865ecc953bSthorpej 	return memcmp("\177ELF", hdr, 4) == 0 ? 1 : 0;
17875ecc953bSthorpej }
17885ecc953bSthorpej 
17895ecc953bSthorpej static int
extract_config(const char * kname,const char * cname,int cfd)17905ecc953bSthorpej extract_config(const char *kname, const char *cname, int cfd)
17915ecc953bSthorpej {
17925ecc953bSthorpej 	char *ptr;
1793d361ef06Sshm 	void *base;
17949a7f4bbaSchristos 	int found, kfd;
17955ecc953bSthorpej 	struct stat st;
17969a7f4bbaSchristos 	off_t i;
17975ecc953bSthorpej 
17985ecc953bSthorpej 	found = 0;
17995ecc953bSthorpej 
18005ecc953bSthorpej 	/* mmap(2) binary kernel */
18015ecc953bSthorpej 	kfd = open(conffile, O_RDONLY);
1802c7295a4cSchristos 	if (kfd == -1)
1803c7295a4cSchristos 		err(EXIT_FAILURE, "cannot open %s", kname);
1804c7295a4cSchristos 	if (fstat(kfd, &st) == -1)
1805c7295a4cSchristos 		err(EXIT_FAILURE, "cannot stat %s", kname);
1806d361ef06Sshm 	base = mmap(0, (size_t)st.st_size, PROT_READ, MAP_FILE | MAP_SHARED,
18075ecc953bSthorpej 	    kfd, 0);
1808d361ef06Sshm 	if (base == MAP_FAILED)
1809c7295a4cSchristos 		err(EXIT_FAILURE, "cannot mmap %s", kname);
1810d361ef06Sshm 	ptr = base;
18115ecc953bSthorpej 
18125ecc953bSthorpej 	/* Scan mmap(2)'ed region, extracting kernel configuration */
18135ecc953bSthorpej 	for (i = 0; i < st.st_size; i++) {
18145ecc953bSthorpej 		if ((*ptr == '_') && (st.st_size - i > 5) && memcmp(ptr,
18155ecc953bSthorpej 		    "_CFG_", 5) == 0) {
18165ecc953bSthorpej 			/* Line found */
18175ecc953bSthorpej 			char *oldptr, line[LINE_MAX + 1], uline[LINE_MAX + 1];
18185ecc953bSthorpej 			int j;
18195ecc953bSthorpej 
18205ecc953bSthorpej 			found = 1;
18215ecc953bSthorpej 
18225ecc953bSthorpej 			oldptr = (ptr += 5);
1823c7295a4cSchristos 			while (*ptr != '\n' && *ptr != '\0')
1824c7295a4cSchristos 				ptr++;
1825c7295a4cSchristos 			if (ptr - oldptr > LINE_MAX)
1826c7295a4cSchristos 				errx(EXIT_FAILURE, "line too long");
18275ecc953bSthorpej 			i += ptr - oldptr + 5;
1828c7295a4cSchristos 			(void)memcpy(line, oldptr, (size_t)(ptr - oldptr));
18295ecc953bSthorpej 			line[ptr - oldptr] = '\0';
18305ecc953bSthorpej 			j = strunvis(uline, line);
1831c7295a4cSchristos 			if (j == -1)
1832c7295a4cSchristos 				errx(EXIT_FAILURE, "unvis: invalid "
1833c7295a4cSchristos 				    "encoded sequence");
18345ecc953bSthorpej 			uline[j] = '\n';
1835c7295a4cSchristos 			if (write(cfd, uline, (size_t)j + 1) == -1)
1836c7295a4cSchristos 				err(EXIT_FAILURE, "cannot write to %s", cname);
1837c7295a4cSchristos 		} else
1838c7295a4cSchristos 			ptr++;
18395ecc953bSthorpej 	}
18405ecc953bSthorpej 
1841c7295a4cSchristos 	(void)close(kfd);
1842d361ef06Sshm 	(void)munmap(base, (size_t)st.st_size);
18435ecc953bSthorpej 
18445ecc953bSthorpej 	return found;
18455ecc953bSthorpej }
1846c130d400Scube 
18477aa6070dScube struct dhdi_params {
18487aa6070dScube 	struct devbase *d;
18497aa6070dScube 	int unit;
1850a31ff6b4Scube 	int level;
18517aa6070dScube };
18527aa6070dScube 
18537aa6070dScube static int
devbase_has_dead_instances(const char * key,void * value,void * aux)18547aa6070dScube devbase_has_dead_instances(const char *key, void *value, void *aux)
18557aa6070dScube {
1856a31ff6b4Scube 	struct devi *i;
18577aa6070dScube 	struct dhdi_params *dhdi = aux;
18587aa6070dScube 
1859a31ff6b4Scube 	for (i = value; i != NULL; i = i->i_alias)
18607aa6070dScube 		if (i->i_base == dhdi->d &&
18617aa6070dScube 		    (dhdi->unit == WILD || dhdi->unit == i->i_unit ||
1862a31ff6b4Scube 		     i->i_unit == STAR) &&
1863a31ff6b4Scube 		    i->i_level >= dhdi->level)
18647aa6070dScube 			return 1;
18657aa6070dScube 	return 0;
18667aa6070dScube }
18677aa6070dScube 
18687aa6070dScube /*
18697aa6070dScube  * This is almost the same as devbase_has_instances, except it
18707aa6070dScube  * may have special considerations regarding ignored instances.
18717aa6070dScube  */
18727aa6070dScube 
18737aa6070dScube static int
devbase_has_any_instance(struct devbase * dev,int unit,int state,int level)1874a31ff6b4Scube devbase_has_any_instance(struct devbase *dev, int unit, int state, int level)
18757aa6070dScube {
18767aa6070dScube 	struct deva *da;
18777aa6070dScube 	struct devi *i;
18787aa6070dScube 
18797aa6070dScube 	if (dev->d_ispseudo) {
18807aa6070dScube 		if (dev->d_ihead != NULL)
18817aa6070dScube 			return 1;
1882a31ff6b4Scube 		else if (state != DEVI_IGNORED)
18837aa6070dScube 			return 0;
1884a31ff6b4Scube 		if ((i = ht_lookup(deaddevitab, dev->d_name)) == NULL)
1885a31ff6b4Scube 			return 0;
1886a31ff6b4Scube 		return (i->i_level >= level);
18877aa6070dScube 	}
18887aa6070dScube 
18897aa6070dScube 	for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
18907aa6070dScube 		for (i = da->d_ihead; i != NULL; i = i->i_asame)
18917aa6070dScube 			if ((i->i_active == DEVI_ACTIVE ||
18927aa6070dScube 			     i->i_active == state) &&
18937aa6070dScube 			    (unit == WILD || unit == i->i_unit ||
18947aa6070dScube 			     i->i_unit == STAR))
18957aa6070dScube 				return 1;
18967aa6070dScube 
18977aa6070dScube 	if (state == DEVI_IGNORED) {
1898a31ff6b4Scube 		struct dhdi_params dhdi = { dev, unit, level };
18997aa6070dScube 		/* also check dead devices */
19007aa6070dScube 		return ht_enumerate(deaddevitab, devbase_has_dead_instances,
19017aa6070dScube 		    &dhdi);
19027aa6070dScube 	}
19037aa6070dScube 
19047aa6070dScube 	return 0;
19057aa6070dScube }
19067aa6070dScube 
19077aa6070dScube /*
19087aa6070dScube  * check_dead_devi(), used with ht_enumerate, checks if any of the removed
19097aa6070dScube  * device instances would have been a valid instance considering the devbase,
19107aa6070dScube  * the parent device and the interface attribute.
19117aa6070dScube  *
19127aa6070dScube  * In other words, for a non-active device, it checks if children would be
19137aa6070dScube  * actual orphans or the result of a negative statement in the config file.
19147aa6070dScube  */
19157aa6070dScube 
19167aa6070dScube struct cdd_params {
19177aa6070dScube 	struct devbase *d;
19187aa6070dScube 	struct attr *at;
19197aa6070dScube 	struct devbase *parent;
19207aa6070dScube };
19217aa6070dScube 
19227aa6070dScube static int
check_dead_devi(const char * key,void * value,void * aux)19237aa6070dScube check_dead_devi(const char *key, void *value, void *aux)
19247aa6070dScube {
19257aa6070dScube 	struct cdd_params *cdd = aux;
19267aa6070dScube 	struct devi *i = value;
1927a31ff6b4Scube 	struct pspec *p;
19287aa6070dScube 
19297aa6070dScube 	if (i->i_base != cdd->d)
19307aa6070dScube 		return 0;
19317aa6070dScube 
1932a31ff6b4Scube 	for (; i != NULL; i = i->i_alias) {
1933a31ff6b4Scube 		p = i->i_pspec;
19347aa6070dScube 		if ((p == NULL && cdd->at == NULL) ||
19357aa6070dScube 		    (p != NULL && p->p_iattr == cdd->at &&
19367aa6070dScube 		     (p->p_atdev == NULL || p->p_atdev == cdd->parent))) {
19377aa6070dScube 			if (p != NULL &&
19387aa6070dScube 			    !devbase_has_any_instance(cdd->parent, p->p_atunit,
1939a31ff6b4Scube 			    DEVI_IGNORED, i->i_level))
19407aa6070dScube 				return 0;
19417aa6070dScube 			else
19427aa6070dScube 				return 1;
19437aa6070dScube 		}
1944a31ff6b4Scube 	}
19457aa6070dScube 	return 0;
19467aa6070dScube }
19477aa6070dScube 
194813282de8Schristos static struct devbase root;
194913282de8Schristos 
195084e20876Schristos static int
addlevelparent(struct devbase * d,struct devbase * parent)1951973bbc98Schristos addlevelparent(struct devbase *d, struct devbase *parent)
1952973bbc98Schristos {
1953973bbc98Schristos 	struct devbase *p;
1954973bbc98Schristos 
195513282de8Schristos 	if (d == parent) {
19560f287115Schristos 		if (d->d_level > 1)
195713282de8Schristos 			return 0;
195813282de8Schristos 		return 1;
195913282de8Schristos 	}
196013282de8Schristos 
196113282de8Schristos 	if (d->d_levelparent) {
19620f287115Schristos 		if (d->d_level > 1)
196313282de8Schristos 			return 0;
196413282de8Schristos 		return 1;
196513282de8Schristos 	}
1966973bbc98Schristos 
1967973bbc98Schristos 	for (p = parent; p != NULL; p = p->d_levelparent)
19680f287115Schristos 		if (d == p && d->d_level > 1)
196913282de8Schristos 			return 0;
197013282de8Schristos 	d->d_levelparent = p ? p : &root;
19710f287115Schristos 	d->d_level++;
197213282de8Schristos 	return 1;
1973973bbc98Schristos }
1974973bbc98Schristos 
1975973bbc98Schristos static void
do_kill_orphans(struct devbase * d,struct attr * at,struct devbase * parent,int state)19767aa6070dScube do_kill_orphans(struct devbase *d, struct attr *at, struct devbase *parent,
19777aa6070dScube     int state)
1978c130d400Scube {
19794caf067bSdholland 	struct nvlist *nv1;
19804caf067bSdholland 	struct attrlist *al;
1981c130d400Scube 	struct attr *a;
1982c130d400Scube 	struct devi *i, *j = NULL;
1983c130d400Scube 	struct pspec *p;
19847aa6070dScube 	int active = 0;
1985c130d400Scube 
198613282de8Schristos 	if (!addlevelparent(d, parent))
198713282de8Schristos 		return;
198884e20876Schristos 
1989c130d400Scube 	/*
1990c130d400Scube 	 * A pseudo-device will always attach at root, and if it has an
1991c130d400Scube 	 * instance (it cannot have more than one), it is enough to consider
1992c130d400Scube 	 * it active, as there is no real attachment.
19937aa6070dScube 	 *
19947aa6070dScube 	 * A pseudo device can never be marked DEVI_IGNORED.
1995c130d400Scube 	 */
1996c130d400Scube 	if (d->d_ispseudo) {
19977aa6070dScube 		if (d->d_ihead != NULL)
19987aa6070dScube 			d->d_ihead->i_active = active = DEVI_ACTIVE;
19997aa6070dScube 		else {
20007aa6070dScube 			if (ht_lookup(deaddevitab, d->d_name) != NULL)
20017aa6070dScube 				active = DEVI_IGNORED;
20027aa6070dScube 			else
2003c130d400Scube 				return;
20047aa6070dScube 		}
2005c130d400Scube 	} else {
20062fb411a1Scube 		int seen = 0;
20070f287115Schristos 		int changed = 0;
20082fb411a1Scube 
2009c130d400Scube 		for (i = d->d_ihead; i != NULL; i = i->i_bsame) {
2010c130d400Scube 			for (j = i; j != NULL; j = j->i_alias) {
2011c130d400Scube 				p = j->i_pspec;
2012c130d400Scube 				if ((p == NULL && at == NULL) ||
2013c130d400Scube 				    (p != NULL && p->p_iattr == at &&
2014c130d400Scube 				    (p->p_atdev == NULL ||
2015c130d400Scube 				    p->p_atdev == parent))) {
2016c130d400Scube 					if (p != NULL &&
20177aa6070dScube 					    !devbase_has_any_instance(parent,
2018a31ff6b4Scube 					      p->p_atunit, state, j->i_level))
2019c130d400Scube 						continue;
2020c130d400Scube 					/*
2021c130d400Scube 					 * There are Fry-like devices which can
2022c130d400Scube 					 * be their own grand-parent (or even
2023c130d400Scube 					 * parent, like uhub).  We don't want
2024c130d400Scube 					 * to loop, so if we've already reached
2025c130d400Scube 					 * an instance for one reason or
2026c130d400Scube 					 * another, stop there.
2027c130d400Scube 					 */
20287aa6070dScube 					if (j->i_active == DEVI_ACTIVE ||
20292fb411a1Scube 					    j->i_active == state) {
2030c130d400Scube 						/*
2031c130d400Scube 						 * Device has already been
20322fb411a1Scube 						 * seen.  However it might
20332fb411a1Scube 						 * have siblings who still
20342fb411a1Scube 						 * have to be activated or
20352fb411a1Scube 						 * orphaned.
2036c130d400Scube 						 */
20372fb411a1Scube 						seen = 1;
20382fb411a1Scube 						continue;
20392fb411a1Scube 					}
20400f287115Schristos 					changed |= j->i_active != state;
20417aa6070dScube 					j->i_active = active = state;
20424b9e5cb1Schristos 					if (p != NULL) {
20434b9e5cb1Schristos 						if (state == DEVI_ACTIVE ||
20444b9e5cb1Schristos 						    --p->p_ref == 0)
20457aa6070dScube 							p->p_active = state;
20464b9e5cb1Schristos 					}
2047136fefb8Schristos 					if (state == DEVI_IGNORED) {
2048136fefb8Schristos 						CFGDBG(5,
2049136fefb8Schristos 						    "`%s' at '%s' ignored",
2050136fefb8Schristos 						    d->d_name, parent ?
2051136fefb8Schristos 						    parent->d_name : "(root)");
2052136fefb8Schristos 					}
2053c130d400Scube 				}
2054c130d400Scube 			}
2055c130d400Scube 		}
2056b7505c15Scube 		/*
2057b7505c15Scube 		 * If we've been there but have made no change, stop.
2058b7505c15Scube 		 */
205984e20876Schristos 		if (seen && active != DEVI_ACTIVE)
206013282de8Schristos 			goto out;
206184e20876Schristos 		if (active != DEVI_ACTIVE) {
20627aa6070dScube 			struct cdd_params cdd = { d, at, parent };
20637aa6070dScube 			/* Look for a matching dead devi */
206442256315Scube 			if (ht_enumerate(deaddevitab, check_dead_devi, &cdd) &&
2065136fefb8Schristos 			    d != parent) {
20667aa6070dScube 				/*
20677aa6070dScube 				 * That device had its instances removed.
20687aa6070dScube 				 * Continue the loop marking descendants
20697aa6070dScube 				 * with DEVI_IGNORED instead of DEVI_ACTIVE.
207042256315Scube 				 *
207142256315Scube 				 * There is one special case for devices that
207242256315Scube 				 * are their own parent:  if that instance is
207342256315Scube 				 * removed (e.g., no uhub* at uhub?), we don't
207442256315Scube 				 * have to continue looping.
20757aa6070dScube 				 */
20767aa6070dScube 				active = DEVI_IGNORED;
2077136fefb8Schristos 				CFGDBG(5, "`%s' at '%s' ignored", d->d_name,
2078136fefb8Schristos 				    parent ? parent->d_name : "(root)");
2079136fefb8Schristos 
20800f287115Schristos 			} else if (!changed)
20810f287115Schristos 				goto out;
2082c130d400Scube 		}
20837aa6070dScube 	}
2084c130d400Scube 
20854caf067bSdholland 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
20864caf067bSdholland 		a = al->al_this;
208784e20876Schristos 		for (nv1 = a->a_devs; nv1 != NULL; nv1 = nv1->nv_next) {
20887aa6070dScube 			do_kill_orphans(nv1->nv_ptr, a, d, active);
2089c130d400Scube 		}
2090c130d400Scube 	}
209113282de8Schristos out:
209284e20876Schristos 	d->d_levelparent = NULL;
209313282de8Schristos 	d->d_level--;
209484e20876Schristos }
2095c130d400Scube 
2096c130d400Scube static int
2097c7295a4cSchristos /*ARGSUSED*/
kill_orphans_cb(const char * key,void * value,void * aux)2098c130d400Scube kill_orphans_cb(const char *key, void *value, void *aux)
2099c130d400Scube {
21007aa6070dScube 	do_kill_orphans((struct devbase *)value, NULL, NULL, DEVI_ACTIVE);
2101c130d400Scube 	return 0;
2102c130d400Scube }
2103c130d400Scube 
2104c130d400Scube static void
kill_orphans(void)2105c7295a4cSchristos kill_orphans(void)
2106c130d400Scube {
2107c130d400Scube 	ht_enumerate(devroottab, kill_orphans_cb, NULL);
2108c130d400Scube }
2109d2737476Smartin 
2110d2737476Smartin static void
add_makeopt(const char * opt)2111d2737476Smartin add_makeopt(const char *opt)
2112d2737476Smartin {
2113d2737476Smartin 	struct nvlist *p;
2114d2737476Smartin 	char *buf = estrdup(opt);
2115d2737476Smartin 	char *eq = strchr(buf, '=');
2116d2737476Smartin 
2117d2737476Smartin 	if (!eq)
2118d2737476Smartin 		errx(EXIT_FAILURE, "-D %s is not in var=value format", opt);
2119d2737476Smartin 
2120d2737476Smartin 	*eq = 0;
2121d2737476Smartin 	p = newnv(estrdup(buf), estrdup(eq+1), NULL, 0, NULL);
2122d2737476Smartin 	free(buf);
2123d2737476Smartin 	p->nv_next = cmdlinedefs;
2124d2737476Smartin 	cmdlinedefs = p;
2125d2737476Smartin }
2126d2737476Smartin 
2127d2737476Smartin static void
remove_makeopt(const char * opt)2128d2737476Smartin remove_makeopt(const char *opt)
2129d2737476Smartin {
2130d2737476Smartin 	struct nvlist *p;
2131d2737476Smartin 
2132d2737476Smartin 	p = newnv(estrdup(opt), NULL, NULL, 0, NULL);
2133d2737476Smartin 	p->nv_next = cmdlineundefs;
2134d2737476Smartin 	cmdlineundefs = p;
2135d2737476Smartin }
2136d2737476Smartin 
2137d2737476Smartin static void
handle_cmdline_makeoptions(void)2138d2737476Smartin handle_cmdline_makeoptions(void)
2139d2737476Smartin {
2140d2737476Smartin 	struct nvlist *p, *n;
2141d2737476Smartin 
2142d2737476Smartin 	handling_cmdlineopts = 1;
2143d2737476Smartin 	for (p = cmdlineundefs; p; p = n) {
2144d2737476Smartin 		n = p->nv_next;
21453dbd1573Schristos 		delmkoption(intern(p->nv_name), 0);
2146d2737476Smartin 		free(__UNCONST(p->nv_name));
2147d2737476Smartin 		nvfree(p);
2148d2737476Smartin 	}
2149d2737476Smartin 	for (p = cmdlinedefs; p; p = n) {
2150d2737476Smartin 		const char *name = intern(p->nv_name);
2151d2737476Smartin 
2152d2737476Smartin 		n = p->nv_next;
21533dbd1573Schristos 		delmkoption(name, 0);
2154d2737476Smartin 		addmkoption(name, intern(p->nv_str));
2155d2737476Smartin 		free(__UNCONST(p->nv_name));
2156d2737476Smartin 		free(__UNCONST(p->nv_str));
2157d2737476Smartin 
2158d2737476Smartin 		nvfree(p);
2159d2737476Smartin 	}
2160d2737476Smartin 	handling_cmdlineopts = 0;
2161d2737476Smartin }
2162