xref: /netbsd/usr.bin/config/sem.c (revision d25ad65e)
1*d25ad65eSchristos /*	$NetBSD: sem.c,v 1.81 2017/11/24 18:45:59 christos 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: @(#)sem.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*d25ad65eSchristos __RCSID("$NetBSD: sem.c,v 1.81 2017/11/24 18:45:59 christos Exp $");
499a7f4bbaSchristos 
505ecc953bSthorpej #include <sys/param.h>
515ecc953bSthorpej #include <ctype.h>
525ecc953bSthorpej #include <stdio.h>
535ecc953bSthorpej #include <stdlib.h>
545ecc953bSthorpej #include <string.h>
55d0fb8901Schristos #include <util.h>
565ecc953bSthorpej #include "defs.h"
575ecc953bSthorpej #include "sem.h"
585ecc953bSthorpej 
595ecc953bSthorpej /*
605ecc953bSthorpej  * config semantics.
615ecc953bSthorpej  */
625ecc953bSthorpej 
635ecc953bSthorpej #define	NAMESIZE	100	/* local name buffers */
645ecc953bSthorpej 
655ecc953bSthorpej const char *s_ifnet;		/* magic attribute */
665ecc953bSthorpej const char *s_qmark;
675ecc953bSthorpej const char *s_none;
685ecc953bSthorpej 
695ecc953bSthorpej static struct hashtab *cfhashtab;	/* for config lookup */
705ecc953bSthorpej struct hashtab *devitab;		/* etc */
717c7d91e6Suebayasi struct attr allattr;
72374a3e4fSchristos size_t nattrs;
735ecc953bSthorpej 
745ecc953bSthorpej static struct attr errattr;
755ecc953bSthorpej static struct devbase errdev;
765ecc953bSthorpej static struct deva errdeva;
775ecc953bSthorpej 
784caf067bSdholland static int has_errobj(struct attrlist *, struct attr *);
795ecc953bSthorpej static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
805ecc953bSthorpej static int resolve(struct nvlist **, const char *, const char *,
815ecc953bSthorpej 		   struct nvlist *, int);
8253be83e0Schristos static struct pspec *getpspec(struct attr *, struct devbase *, int,
8353be83e0Schristos     struct deva *);
845ecc953bSthorpej static struct devi *newdevi(const char *, int, struct devbase *d);
855ecc953bSthorpej static struct devi *getdevi(const char *);
86c130d400Scube static void remove_devi(struct devi *);
875ecc953bSthorpej static const char *concat(const char *, int);
885ecc953bSthorpej static char *extend(char *, const char *);
895ecc953bSthorpej static int split(const char *, size_t, char *, size_t, int *);
905ecc953bSthorpej static void selectbase(struct devbase *, struct deva *);
9116a8771bSdholland static const char **fixloc(const char *, struct attr *, struct loclist *);
92d767912bSdrochner static const char *makedevstr(devmajor_t, devminor_t);
93d767912bSdrochner static const char *major2name(devmajor_t);
94d767912bSdrochner static devmajor_t dev2major(struct devbase *);
955ecc953bSthorpej 
965ecc953bSthorpej extern const char *yyfile;
977aa6070dScube extern int vflag;
985ecc953bSthorpej 
9911a68adeSchristos #define V_ATTRIBUTE	0
10011a68adeSchristos #define V_DEVICE	1
10111a68adeSchristos struct vtype {
10211a68adeSchristos 	int type;
10311a68adeSchristos 	struct attr *attr;
10411a68adeSchristos 	void *value;
10511a68adeSchristos };
10611a68adeSchristos 
10711a68adeSchristos static struct nvlist *
10811a68adeSchristos makedevstack(struct devbase *d)
10911a68adeSchristos {
11011a68adeSchristos 	struct devi *firsti, *i;
11111a68adeSchristos 	struct nvlist *stack = NULL;
11211a68adeSchristos 
11311a68adeSchristos 	for (firsti = d->d_ihead; firsti != NULL; firsti = firsti->i_bsame)
11411a68adeSchristos 		for (i = firsti; i != NULL; i = i->i_alias)
11511a68adeSchristos 			stack = newnv(NULL, NULL, i, 0, stack);
11611a68adeSchristos 	return stack;
11711a68adeSchristos }
11811a68adeSchristos 
11911a68adeSchristos static void
12011a68adeSchristos devcleanup(struct nvlist *stack)
12111a68adeSchristos {
12211a68adeSchristos 	struct nvlist *nv;
12311a68adeSchristos 	for (nv = stack; nv != NULL; nv = nv->nv_next)
12411a68adeSchristos 		remove_devi(nv->nv_ptr);
12511a68adeSchristos 	nvfreel(stack);
12611a68adeSchristos }
12711a68adeSchristos 
12811a68adeSchristos static void *
12911a68adeSchristos addvalue(int type, struct attr *a, void *value)
13011a68adeSchristos {
13111a68adeSchristos 	struct vtype *vt = emalloc(sizeof(*vt));
13211a68adeSchristos 	vt->type = type;
13311a68adeSchristos 	vt->attr = a;
13411a68adeSchristos 	vt->value = value;
13511a68adeSchristos 	return vt;
13611a68adeSchristos }
13711a68adeSchristos 
1385ecc953bSthorpej void
1395ecc953bSthorpej initsem(void)
1405ecc953bSthorpej {
1415ecc953bSthorpej 
1425ecc953bSthorpej 	attrtab = ht_new();
1436d1bba4aSuebayasi 	attrdeptab = ht_new();
1447c7d91e6Suebayasi 
1457c7d91e6Suebayasi 	allattr.a_name = "netbsd";
1467c7d91e6Suebayasi 	TAILQ_INIT(&allattr.a_files);
1477c7d91e6Suebayasi 	(void)ht_insert(attrtab, allattr.a_name, &allattr);
148cd06fea3Suebayasi 	selectattr(&allattr);
1497c7d91e6Suebayasi 
1505ecc953bSthorpej 	errattr.a_name = "<internal>";
1515ecc953bSthorpej 
1525ecc953bSthorpej 	TAILQ_INIT(&allbases);
1535ecc953bSthorpej 
1545ecc953bSthorpej 	TAILQ_INIT(&alldevas);
1555ecc953bSthorpej 
1565ecc953bSthorpej 	TAILQ_INIT(&allpspecs);
1575ecc953bSthorpej 
1585ecc953bSthorpej 	cfhashtab = ht_new();
1595ecc953bSthorpej 	TAILQ_INIT(&allcf);
1605ecc953bSthorpej 
1615ecc953bSthorpej 	TAILQ_INIT(&alldevi);
1625ecc953bSthorpej 	errdev.d_name = "<internal>";
1635ecc953bSthorpej 
1645ecc953bSthorpej 	TAILQ_INIT(&allpseudo);
1655ecc953bSthorpej 
1665ecc953bSthorpej 	TAILQ_INIT(&alldevms);
1675ecc953bSthorpej 
1685ecc953bSthorpej 	s_ifnet = intern("ifnet");
1695ecc953bSthorpej 	s_qmark = intern("?");
1705ecc953bSthorpej 	s_none = intern("none");
1715ecc953bSthorpej }
1725ecc953bSthorpej 
1735ecc953bSthorpej /* Name of include file just ended (set in scan.l) */
1745ecc953bSthorpej extern const char *lastfile;
1755ecc953bSthorpej 
176a87832feSuebayasi static struct attr *
177da706167Suebayasi finddep(struct attr *a, const char *name)
178da706167Suebayasi {
179da706167Suebayasi 	struct attrlist *al;
180da706167Suebayasi 
181da706167Suebayasi 	for (al = a->a_deps; al != NULL; al = al->al_next) {
182da706167Suebayasi 		struct attr *this = al->al_this;
183da706167Suebayasi 		if (strcmp(this->a_name, name) == 0)
184da706167Suebayasi 			return this;
185da706167Suebayasi 	}
186da706167Suebayasi 	return NULL;
187da706167Suebayasi }
188da706167Suebayasi 
189da706167Suebayasi static void
190c80681c9Suebayasi mergedeps(const char *dname, const char *name)
191da706167Suebayasi {
192da706167Suebayasi 	struct attr *a, *newa;
193da706167Suebayasi 
194c80681c9Suebayasi 	CFGDBG(4, "merging attr `%s' to devbase `%s'", name, dname);
195c80681c9Suebayasi 	a = refattr(dname);
196da706167Suebayasi 	if (finddep(a, name) == NULL) {
197da706167Suebayasi 		newa = refattr(name);
198da706167Suebayasi 		a->a_deps = attrlist_cons(a->a_deps, newa);
199da706167Suebayasi 		CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name,
200da706167Suebayasi 		    a->a_name);
201da706167Suebayasi 	}
202da706167Suebayasi }
203da706167Suebayasi 
204da706167Suebayasi static void
205da706167Suebayasi fixdev(struct devbase *dev)
206da706167Suebayasi {
207da706167Suebayasi 	struct attrlist *al;
208da706167Suebayasi 	struct attr *devattr, *a;
209da706167Suebayasi 
210da706167Suebayasi 	devattr = refattr(dev->d_name);
211da706167Suebayasi 	if (devattr->a_devclass)
212a87832feSuebayasi 		panic("%s: dev %s is devclass!", __func__, devattr->a_name);
213da706167Suebayasi 
214c4bac1d0Suebayasi 	CFGDBG(4, "fixing devbase `%s'", dev->d_name);
215da706167Suebayasi 	for (al = dev->d_attrs; al != NULL; al = al->al_next) {
216da706167Suebayasi 		a = al->al_this;
217*d25ad65eSchristos 		CFGDBG(4, "fixing devbase `%s' attr `%s'", dev->d_name,
218*d25ad65eSchristos 		    a->a_name);
219da706167Suebayasi 		if (a->a_iattr) {
220da706167Suebayasi 			a->a_refs = addtoattr(a->a_refs, dev);
221c4bac1d0Suebayasi 			CFGDBG(3, "device `%s' has iattr `%s'", dev->d_name,
222c4bac1d0Suebayasi 			    a->a_name);
223da706167Suebayasi 		} else if (a->a_devclass != NULL) {
224c5a6c986Suebayasi 			if (dev->d_classattr != NULL && dev->d_classattr != a) {
225da706167Suebayasi 				cfgwarn("device `%s' has multiple classes "
226da706167Suebayasi 				    "(`%s' and `%s')",
227da706167Suebayasi 				    dev->d_name, dev->d_classattr->a_name,
228da706167Suebayasi 				    a->a_name);
229da706167Suebayasi 			}
230c5a6c986Suebayasi 			if (dev->d_classattr == NULL) {
231da706167Suebayasi 				dev->d_classattr = a;
232*d25ad65eSchristos 				CFGDBG(3, "device `%s' is devclass `%s'",
233*d25ad65eSchristos 				    dev->d_name, a->a_name);
234c5a6c986Suebayasi 			}
235da706167Suebayasi 		} else {
236da706167Suebayasi 			if (strcmp(dev->d_name, a->a_name) != 0) {
237bc707469Suebayasi 				mergedeps(dev->d_name, a->a_name);
238da706167Suebayasi 			}
239da706167Suebayasi 		}
240da706167Suebayasi 	}
241da706167Suebayasi }
242da706167Suebayasi 
2435ecc953bSthorpej void
2445ecc953bSthorpej enddefs(void)
2455ecc953bSthorpej {
2465ecc953bSthorpej 	struct devbase *dev;
2475ecc953bSthorpej 
248c4bac1d0Suebayasi 	yyfile = "enddefs";
249c4bac1d0Suebayasi 
2505ecc953bSthorpej 	TAILQ_FOREACH(dev, &allbases, d_next) {
2515ecc953bSthorpej 		if (!dev->d_isdef) {
2525ecc953bSthorpej 			(void)fprintf(stderr,
2535ecc953bSthorpej 			    "%s: device `%s' used but not defined\n",
2545ecc953bSthorpej 			    lastfile, dev->d_name);
2555ecc953bSthorpej 			errors++;
2565ecc953bSthorpej 			continue;
2575ecc953bSthorpej 		}
258bc707469Suebayasi 		fixdev(dev);
2595ecc953bSthorpej 	}
2605ecc953bSthorpej 	if (errors) {
2615ecc953bSthorpej 		(void)fprintf(stderr, "*** Stop.\n");
2625ecc953bSthorpej 		exit(1);
2635ecc953bSthorpej 	}
2645ecc953bSthorpej }
2655ecc953bSthorpej 
2665ecc953bSthorpej void
2675ecc953bSthorpej setdefmaxusers(int min, int def, int max)
2685ecc953bSthorpej {
2695ecc953bSthorpej 
2705ecc953bSthorpej 	if (min < 1 || min > def || def > max)
271c7295a4cSchristos 		cfgerror("maxusers must have 1 <= min (%d) <= default (%d) "
272c7295a4cSchristos 		    "<= max (%d)", min, def, max);
2735ecc953bSthorpej 	else {
2745ecc953bSthorpej 		minmaxusers = min;
2755ecc953bSthorpej 		defmaxusers = def;
2765ecc953bSthorpej 		maxmaxusers = max;
2775ecc953bSthorpej 	}
2785ecc953bSthorpej }
2795ecc953bSthorpej 
2805ecc953bSthorpej void
2815ecc953bSthorpej setmaxusers(int n)
2825ecc953bSthorpej {
2835ecc953bSthorpej 
284895e5687Scube 	if (maxusers == n) {
285c7295a4cSchristos 		cfgerror("duplicate maxusers parameter");
2865ecc953bSthorpej 		return;
2875ecc953bSthorpej 	}
288895e5687Scube 	if (vflag && maxusers != 0)
289c7295a4cSchristos 		cfgwarn("warning: maxusers already defined");
2905ecc953bSthorpej 	maxusers = n;
2915ecc953bSthorpej 	if (n < minmaxusers) {
292c7295a4cSchristos 		cfgerror("warning: minimum of %d maxusers assumed",
293c7295a4cSchristos 		    minmaxusers);
2945ecc953bSthorpej 		errors--;	/* take it away */
2955ecc953bSthorpej 		maxusers = minmaxusers;
2965ecc953bSthorpej 	} else if (n > maxmaxusers) {
297c7295a4cSchristos 		cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers);
2985ecc953bSthorpej 		errors--;
2995ecc953bSthorpej 	}
3005ecc953bSthorpej }
3015ecc953bSthorpej 
3025ecc953bSthorpej void
3035ecc953bSthorpej setident(const char *i)
3045ecc953bSthorpej {
3055ecc953bSthorpej 
3064a7be1f9Smrg 	if (i)
3075ecc953bSthorpej 		ident = intern(i);
3084a7be1f9Smrg 	else
3094a7be1f9Smrg 		ident = NULL;
3105ecc953bSthorpej }
3115ecc953bSthorpej 
3125ecc953bSthorpej /*
3135ecc953bSthorpej  * Define an attribute, optionally with an interface (a locator list)
3145ecc953bSthorpej  * and a set of attribute-dependencies.
3155ecc953bSthorpej  *
3165ecc953bSthorpej  * Attribute dependencies MAY NOT be interface attributes.
3175ecc953bSthorpej  *
3185ecc953bSthorpej  * Since an empty locator list is logically different from "no interface",
3195ecc953bSthorpej  * all locator lists include a dummy head node, which we discard here.
3205ecc953bSthorpej  */
3215ecc953bSthorpej int
322bb4307a6Suebayasi defattr0(const char *name, struct loclist *locs, struct attrlist *deps,
323bb4307a6Suebayasi     int devclass)
324bb4307a6Suebayasi {
325bb4307a6Suebayasi 
326bb4307a6Suebayasi 	if (locs != NULL)
327bb4307a6Suebayasi 		return defiattr(name, locs, deps, devclass);
328bb4307a6Suebayasi 	else if (devclass)
329bb4307a6Suebayasi 		return defdevclass(name, locs, deps, devclass);
330bb4307a6Suebayasi 	else
331bb4307a6Suebayasi 		return defattr(name, locs, deps, devclass);
332bb4307a6Suebayasi }
333bb4307a6Suebayasi 
334bb4307a6Suebayasi int
33516a8771bSdholland defattr(const char *name, struct loclist *locs, struct attrlist *deps,
3365ecc953bSthorpej     int devclass)
3375ecc953bSthorpej {
3385ecc953bSthorpej 	struct attr *a, *dep;
3394caf067bSdholland 	struct attrlist *al;
3405ecc953bSthorpej 
3418955e3e2Schristos 	if (getrefattr(name, &a)) {
3428955e3e2Schristos 		cfgerror("attribute `%s' already defined", name);
3438955e3e2Schristos 		loclist_destroy(locs);
3448955e3e2Schristos 		return (1);
3458955e3e2Schristos 	}
3468955e3e2Schristos 	if (a == NULL)
3478955e3e2Schristos 		a = mkattr(name);
3488955e3e2Schristos 
3495ecc953bSthorpej 	/*
3505ecc953bSthorpej 	 * If this attribute depends on any others, make sure none of
3515ecc953bSthorpej 	 * the dependencies are interface attributes.
3525ecc953bSthorpej 	 */
3534caf067bSdholland 	for (al = deps; al != NULL; al = al->al_next) {
3544caf067bSdholland 		dep = al->al_this;
3555ecc953bSthorpej 		if (dep->a_iattr) {
356c7295a4cSchristos 			cfgerror("`%s' dependency `%s' is an interface "
357c7295a4cSchristos 			    "attribute", name, dep->a_name);
3585ecc953bSthorpej 			return (1);
3595ecc953bSthorpej 		}
36011a68adeSchristos 		(void)ht_insert2(attrdeptab, name, dep->a_name,
36111a68adeSchristos 		    addvalue(V_ATTRIBUTE, a, dep));
362bc707469Suebayasi 		CFGDBG(2, "attr `%s' depends on attr `%s'", name, dep->a_name);
3635ecc953bSthorpej 	}
3645ecc953bSthorpej 
3655ecc953bSthorpej 
366a949e288Suebayasi 	a->a_deps = deps;
367bb4307a6Suebayasi 	expandattr(a, NULL);
3682f2f3382Suebayasi 	CFGDBG(3, "attr `%s' defined", a->a_name);
369bb4307a6Suebayasi 
370bb4307a6Suebayasi 	return (0);
371bb4307a6Suebayasi }
372a949e288Suebayasi 
3732f2f3382Suebayasi struct attr *
3742f2f3382Suebayasi mkattr(const char *name)
3752f2f3382Suebayasi {
3762f2f3382Suebayasi 	struct attr *a;
3772f2f3382Suebayasi 
3782f2f3382Suebayasi 	a = ecalloc(1, sizeof *a);
3792f2f3382Suebayasi 	if (ht_insert(attrtab, name, a)) {
3802f2f3382Suebayasi 		free(a);
3812f2f3382Suebayasi 		return NULL;
3822f2f3382Suebayasi 	}
3832f2f3382Suebayasi 	a->a_name = name;
3842f2f3382Suebayasi 	TAILQ_INIT(&a->a_files);
3852f2f3382Suebayasi 	CFGDBG(3, "attr `%s' allocated", name);
3862f2f3382Suebayasi 
3872f2f3382Suebayasi 	return a;
3882f2f3382Suebayasi }
3892f2f3382Suebayasi 
390a949e288Suebayasi /* "interface attribute" initialization */
391bb4307a6Suebayasi int
392bb4307a6Suebayasi defiattr(const char *name, struct loclist *locs, struct attrlist *deps,
393bb4307a6Suebayasi     int devclass)
394bb4307a6Suebayasi {
395bb4307a6Suebayasi 	struct attr *a;
396bb4307a6Suebayasi 	int len;
397bb4307a6Suebayasi 	struct loclist *ll;
398bb4307a6Suebayasi 
399bb4307a6Suebayasi 	if (devclass)
400*d25ad65eSchristos 		panic("%s: %s has both locators and devclass", __func__, name);
401bb4307a6Suebayasi 
402bb4307a6Suebayasi 	if (defattr(name, locs, deps, devclass) != 0)
403bb4307a6Suebayasi 		return (1);
404bb4307a6Suebayasi 
405bb4307a6Suebayasi 	a = getattr(name);
4065ecc953bSthorpej 	a->a_iattr = 1;
40716a8771bSdholland 	/* unwrap */
40816a8771bSdholland 	a->a_locs = locs->ll_next;
40916a8771bSdholland 	locs->ll_next = NULL;
41016a8771bSdholland 	loclist_destroy(locs);
411a949e288Suebayasi 	len = 0;
412a949e288Suebayasi 	for (ll = a->a_locs; ll != NULL; ll = ll->ll_next)
413a949e288Suebayasi 		len++;
414a949e288Suebayasi 	a->a_loclen = len;
415bb4307a6Suebayasi 	if (deps)
416bb4307a6Suebayasi 		CFGDBG(2, "attr `%s' iface with deps", a->a_name);
417bb4307a6Suebayasi 	return (0);
4185ecc953bSthorpej }
419a949e288Suebayasi 
420a949e288Suebayasi /* "device class" initialization */
421bb4307a6Suebayasi int
422bb4307a6Suebayasi defdevclass(const char *name, struct loclist *locs, struct attrlist *deps,
423bb4307a6Suebayasi     int devclass)
424bb4307a6Suebayasi {
425bb4307a6Suebayasi 	struct attr *a;
426bbfbbde1Schristos 	char classenum[256], *cp;
4275ecc953bSthorpej 	int errored = 0;
4285ecc953bSthorpej 
429bb4307a6Suebayasi 	if (deps)
430*d25ad65eSchristos 		panic("%s: %s has both dependencies and devclass", __func__,
431*d25ad65eSchristos 		    name);
432bb4307a6Suebayasi 
433bb4307a6Suebayasi 	if (defattr(name, locs, deps, devclass) != 0)
434bb4307a6Suebayasi 		return (1);
435bb4307a6Suebayasi 
436bb4307a6Suebayasi 	a = getattr(name);
437bbfbbde1Schristos 	(void)snprintf(classenum, sizeof(classenum), "DV_%s", name);
4385ecc953bSthorpej 	for (cp = classenum + 3; *cp; cp++) {
439*d25ad65eSchristos 		if (!errored && (!isalnum((unsigned char)*cp) ||
440*d25ad65eSchristos 		      (isalpha((unsigned char)*cp)
441*d25ad65eSchristos 		      && !islower((unsigned char)*cp)))) {
442c7295a4cSchristos 			cfgerror("device class names must be "
443c7295a4cSchristos 			    "lower-case alphanumeric characters");
4445ecc953bSthorpej 			errored = 1;
4455ecc953bSthorpej 		}
44664a07b54Sjoerg 		*cp = (char)toupper((unsigned char)*cp);
4475ecc953bSthorpej 	}
4485ecc953bSthorpej 	a->a_devclass = intern(classenum);
4495ecc953bSthorpej 
4505ecc953bSthorpej 	return (0);
4515ecc953bSthorpej }
4525ecc953bSthorpej 
4535ecc953bSthorpej /*
4545ecc953bSthorpej  * Return true if the given `error object' is embedded in the given
4555ecc953bSthorpej  * pointer list.
4565ecc953bSthorpej  */
4575ecc953bSthorpej static int
4584caf067bSdholland has_errobj(struct attrlist *al, struct attr *obj)
4595ecc953bSthorpej {
4605ecc953bSthorpej 
4614caf067bSdholland 	for (; al != NULL; al = al->al_next)
4624caf067bSdholland 		if (al->al_this == obj)
4635ecc953bSthorpej 			return (1);
4645ecc953bSthorpej 	return (0);
4655ecc953bSthorpej }
4665ecc953bSthorpej 
4675ecc953bSthorpej /*
4685ecc953bSthorpej  * Return true if the given attribute is embedded in the given
4695ecc953bSthorpej  * pointer list.
4705ecc953bSthorpej  */
4715ecc953bSthorpej int
4724caf067bSdholland has_attr(struct attrlist *al, const char *attr)
4735ecc953bSthorpej {
4745ecc953bSthorpej 	struct attr *a;
4755ecc953bSthorpej 
4765ecc953bSthorpej 	if ((a = getattr(attr)) == NULL)
4775ecc953bSthorpej 		return (0);
4785ecc953bSthorpej 
4794caf067bSdholland 	for (; al != NULL; al = al->al_next)
4804caf067bSdholland 		if (al->al_this == a)
4815ecc953bSthorpej 			return (1);
4825ecc953bSthorpej 	return (0);
4835ecc953bSthorpej }
4845ecc953bSthorpej 
4855ecc953bSthorpej /*
4865ecc953bSthorpej  * Add a device base to a list in an attribute (actually, to any list).
4875ecc953bSthorpej  * Note that this does not check for duplicates, and does reverse the
4885ecc953bSthorpej  * list order, but no one cares anyway.
4895ecc953bSthorpej  */
4905ecc953bSthorpej static struct nvlist *
4915ecc953bSthorpej addtoattr(struct nvlist *l, struct devbase *dev)
4925ecc953bSthorpej {
4935ecc953bSthorpej 	struct nvlist *n;
4945ecc953bSthorpej 
4955ecc953bSthorpej 	n = newnv(NULL, NULL, dev, 0, l);
4965ecc953bSthorpej 	return (n);
4975ecc953bSthorpej }
4985ecc953bSthorpej 
4995ecc953bSthorpej /*
5005ecc953bSthorpej  * Define a device.  This may (or may not) also define an interface
5015ecc953bSthorpej  * attribute and/or refer to existing attributes.
5025ecc953bSthorpej  */
5035ecc953bSthorpej void
50416a8771bSdholland defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs,
5055ecc953bSthorpej        int ispseudo)
5065ecc953bSthorpej {
50716a8771bSdholland 	struct loclist *ll;
5084caf067bSdholland 	struct attrlist *al;
5095ecc953bSthorpej 
5105ecc953bSthorpej 	if (dev == &errdev)
5115ecc953bSthorpej 		goto bad;
5125ecc953bSthorpej 	if (dev->d_isdef) {
513c7295a4cSchristos 		cfgerror("redefinition of `%s'", dev->d_name);
5145ecc953bSthorpej 		goto bad;
5155ecc953bSthorpej 	}
5165ecc953bSthorpej 
5175ecc953bSthorpej 	dev->d_isdef = 1;
5185ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
5195ecc953bSthorpej 		goto bad;
5205ecc953bSthorpej 
5215ecc953bSthorpej 	/*
5225ecc953bSthorpej 	 * Handle implicit attribute definition from locator list.  Do
5235ecc953bSthorpej 	 * this before scanning the `at' list so that we can have, e.g.:
5245ecc953bSthorpej 	 *	device foo at other, foo { slot = -1 }
5255ecc953bSthorpej 	 * (where you can plug in a foo-bus extender to a foo-bus).
5265ecc953bSthorpej 	 */
5275ecc953bSthorpej 	if (loclist != NULL) {
52816a8771bSdholland 		ll = loclist;
5295ecc953bSthorpej 		loclist = NULL;	/* defattr disposes of them for us */
530bb4307a6Suebayasi 		if (defiattr(dev->d_name, ll, NULL, 0))
5315ecc953bSthorpej 			goto bad;
5324caf067bSdholland 		attrs = attrlist_cons(attrs, getattr(dev->d_name));
5334caf067bSdholland 		/* This used to be stored but was never used */
5344caf067bSdholland 		/* attrs->al_name = dev->d_name; */
535c454f920Scube 	}
536c454f920Scube 
537c130d400Scube 	/*
538c130d400Scube 	 * Pseudo-devices can have children.  Consider them as
539c130d400Scube 	 * attaching at root.
540c130d400Scube 	 */
541c454f920Scube 	if (ispseudo) {
5424caf067bSdholland 		for (al = attrs; al != NULL; al = al->al_next)
5434caf067bSdholland 			if (al->al_this->a_iattr)
544c454f920Scube 				break;
5454caf067bSdholland 		if (al != NULL) {
546b66156c7Sdrochner 			if (ispseudo < 2) {
547b66156c7Sdrochner 				if (version >= 20080610)
548b66156c7Sdrochner 					cfgerror("interface attribute on "
549b66156c7Sdrochner 					 "non-device pseudo `%s'", dev->d_name);
550b66156c7Sdrochner 				else {
551b66156c7Sdrochner 					ispseudo = 2;
552b66156c7Sdrochner 				}
553b66156c7Sdrochner 			}
554c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
5555ecc953bSthorpej 		}
556b66156c7Sdrochner 	}
5575ecc953bSthorpej 
5585ecc953bSthorpej 	/* Committed!  Set up fields. */
5595ecc953bSthorpej 	dev->d_ispseudo = ispseudo;
5605ecc953bSthorpej 	dev->d_attrs = attrs;
5615ecc953bSthorpej 	dev->d_classattr = NULL;		/* for now */
562da706167Suebayasi 	CFGDBG(3, "dev `%s' defined", dev->d_name);
5635ecc953bSthorpej 
5645ecc953bSthorpej 	/*
565a2dd9676Suebayasi 	 * Implicit attribute definition for device.
566a2dd9676Suebayasi 	 */
56701b2a130Smartin 	refattr(dev->d_name);
568a2dd9676Suebayasi 
569a2dd9676Suebayasi 	/*
5705ecc953bSthorpej 	 * For each interface attribute this device refers to, add this
5715ecc953bSthorpej 	 * device to its reference list.  This makes, e.g., finding all
5725ecc953bSthorpej 	 * "scsi"s easier.
5735ecc953bSthorpej 	 *
5745ecc953bSthorpej 	 * While looking through the attributes, set up the device
5755ecc953bSthorpej 	 * class if any are devclass attributes (and error out if the
5765ecc953bSthorpej 	 * device has two classes).
5775ecc953bSthorpej 	 */
5784caf067bSdholland 	for (al = attrs; al != NULL; al = al->al_next) {
579da706167Suebayasi 		/*
580da706167Suebayasi 		 * Implicit attribute definition for device dependencies.
581da706167Suebayasi 		 */
582bc707469Suebayasi 		refattr(al->al_this->a_name);
58311a68adeSchristos 		(void)ht_insert2(attrdeptab, dev->d_name, al->al_this->a_name,
58411a68adeSchristos 			addvalue(V_DEVICE, al->al_this, dev));
585bc707469Suebayasi 		CFGDBG(2, "device `%s' depends on attr `%s'", dev->d_name,
586bc707469Suebayasi 		    al->al_this->a_name);
5875ecc953bSthorpej 	}
5885ecc953bSthorpej 	return;
5895ecc953bSthorpej  bad:
59016a8771bSdholland 	loclist_destroy(loclist);
5914caf067bSdholland 	attrlist_destroyall(attrs);
5925ecc953bSthorpej }
5935ecc953bSthorpej 
5945ecc953bSthorpej /*
5955ecc953bSthorpej  * Look up a devbase.  Also makes sure it is a reasonable name,
5965ecc953bSthorpej  * i.e., does not end in a digit or contain special characters.
5975ecc953bSthorpej  */
5985ecc953bSthorpej struct devbase *
5995ecc953bSthorpej getdevbase(const char *name)
6005ecc953bSthorpej {
601c7295a4cSchristos 	const u_char *p;
6025ecc953bSthorpej 	struct devbase *dev;
6035ecc953bSthorpej 
604c7295a4cSchristos 	p = (const u_char *)name;
6055ecc953bSthorpej 	if (!isalpha(*p))
6065ecc953bSthorpej 		goto badname;
6075ecc953bSthorpej 	while (*++p) {
6085ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
6095ecc953bSthorpej 			goto badname;
6105ecc953bSthorpej 	}
6115ecc953bSthorpej 	if (isdigit(*--p)) {
6125ecc953bSthorpej  badname:
613c7295a4cSchristos 		cfgerror("bad device base name `%s'", name);
6145ecc953bSthorpej 		return (&errdev);
6155ecc953bSthorpej 	}
6165ecc953bSthorpej 	dev = ht_lookup(devbasetab, name);
6175ecc953bSthorpej 	if (dev == NULL) {
6185ecc953bSthorpej 		dev = ecalloc(1, sizeof *dev);
6195ecc953bSthorpej 		dev->d_name = name;
6205ecc953bSthorpej 		dev->d_isdef = 0;
621d767912bSdrochner 		dev->d_major = NODEVMAJOR;
6225ecc953bSthorpej 		dev->d_attrs = NULL;
6235ecc953bSthorpej 		dev->d_ihead = NULL;
6245ecc953bSthorpej 		dev->d_ipp = &dev->d_ihead;
6255ecc953bSthorpej 		dev->d_ahead = NULL;
6265ecc953bSthorpej 		dev->d_app = &dev->d_ahead;
6275ecc953bSthorpej 		dev->d_umax = 0;
6285ecc953bSthorpej 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
6295ecc953bSthorpej 		if (ht_insert(devbasetab, name, dev))
630*d25ad65eSchristos 			panic("%s: Can't insert %s", __func__, name);
631da706167Suebayasi 		CFGDBG(3, "devbase defined `%s'", dev->d_name);
6325ecc953bSthorpej 	}
6335ecc953bSthorpej 	return (dev);
6345ecc953bSthorpej }
6355ecc953bSthorpej 
6365ecc953bSthorpej /*
6375ecc953bSthorpej  * Define some of a device's allowable parent attachments.
6385ecc953bSthorpej  * There may be a list of (plain) attributes.
6395ecc953bSthorpej  */
6405ecc953bSthorpej void
6415ecc953bSthorpej defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
6424caf067bSdholland 	     struct attrlist *attrs)
6435ecc953bSthorpej {
6445ecc953bSthorpej 	struct nvlist *nv;
6454caf067bSdholland 	struct attrlist *al;
6465ecc953bSthorpej 	struct attr *a;
6475ecc953bSthorpej 
6485ecc953bSthorpej 	if (dev == &errdev)
6495ecc953bSthorpej 		goto bad;
6505ecc953bSthorpej 	if (deva == NULL)
6515ecc953bSthorpej 		deva = getdevattach(dev->d_name);
6525ecc953bSthorpej 	if (deva == &errdeva)
6535ecc953bSthorpej 		goto bad;
6545ecc953bSthorpej 	if (!dev->d_isdef) {
655c7295a4cSchristos 		cfgerror("attaching undefined device `%s'", dev->d_name);
6565ecc953bSthorpej 		goto bad;
6575ecc953bSthorpej 	}
6585ecc953bSthorpej 	if (deva->d_isdef) {
659c7295a4cSchristos 		cfgerror("redefinition of `%s'", deva->d_name);
6605ecc953bSthorpej 		goto bad;
6615ecc953bSthorpej 	}
6625ecc953bSthorpej 	if (dev->d_ispseudo) {
663c7295a4cSchristos 		cfgerror("pseudo-devices can't attach");
6645ecc953bSthorpej 		goto bad;
6655ecc953bSthorpej 	}
6665ecc953bSthorpej 
6675ecc953bSthorpej 	deva->d_isdef = 1;
6685ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
6695ecc953bSthorpej 		goto bad;
6704caf067bSdholland 	for (al = attrs; al != NULL; al = al->al_next) {
6714caf067bSdholland 		a = al->al_this;
6725ecc953bSthorpej 		if (a == &errattr)
6735ecc953bSthorpej 			continue;		/* already complained */
6745ecc953bSthorpej 		if (a->a_iattr || a->a_devclass != NULL)
675c7295a4cSchristos 			cfgerror("`%s' is not a plain attribute", a->a_name);
6765ecc953bSthorpej 	}
6775ecc953bSthorpej 
6785ecc953bSthorpej 	/* Committed!  Set up fields. */
6795ecc953bSthorpej 	deva->d_attrs = attrs;
6805ecc953bSthorpej 	deva->d_atlist = atlist;
6815ecc953bSthorpej 	deva->d_devbase = dev;
6822f2f3382Suebayasi 	CFGDBG(3, "deva `%s' defined", deva->d_name);
6832f2f3382Suebayasi 
6842f2f3382Suebayasi 	/*
685a5a68b71Suebayasi 	 * Implicit attribute definition for device attachment.
6862f2f3382Suebayasi 	 */
6872f2f3382Suebayasi 	refattr(deva->d_name);
6885ecc953bSthorpej 
6895ecc953bSthorpej 	/*
6905ecc953bSthorpej 	 * Turn the `at' list into interface attributes (map each
6915ecc953bSthorpej 	 * nv_name to an attribute, or to NULL for root), and add
6925ecc953bSthorpej 	 * this device to those attributes, so that children can
6935ecc953bSthorpej 	 * be listed at this particular device if they are supported
6945ecc953bSthorpej 	 * by that attribute.
6955ecc953bSthorpej 	 */
6965ecc953bSthorpej 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
6975ecc953bSthorpej 		if (nv->nv_name == NULL)
6985ecc953bSthorpej 			nv->nv_ptr = a = NULL;	/* at root */
6995ecc953bSthorpej 		else
7005ecc953bSthorpej 			nv->nv_ptr = a = getattr(nv->nv_name);
7015ecc953bSthorpej 		if (a == &errattr)
7025ecc953bSthorpej 			continue;		/* already complained */
7035ecc953bSthorpej 
70453be83e0Schristos #if 0
7055ecc953bSthorpej 		/*
7065ecc953bSthorpej 		 * Make sure that an attachment spec doesn't
7075ecc953bSthorpej 		 * already say how to attach to this attribute.
7085ecc953bSthorpej 		 */
70953be83e0Schristos 		for (struct deva *da = dev->d_ahead; da; da = da->d_bsame)
7105ecc953bSthorpej 			if (onlist(da->d_atlist, a))
711c7295a4cSchristos 				cfgerror("attach at `%s' already done by `%s'",
7125ecc953bSthorpej 				     a ? a->a_name : "root", da->d_name);
71353be83e0Schristos #endif
7145ecc953bSthorpej 
715c130d400Scube 		if (a == NULL) {
716c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
7175ecc953bSthorpej 			continue;		/* at root; don't add */
718c130d400Scube 		}
7195ecc953bSthorpej 		if (!a->a_iattr)
720c7295a4cSchristos 			cfgerror("%s cannot be at plain attribute `%s'",
7215ecc953bSthorpej 			    dev->d_name, a->a_name);
7225ecc953bSthorpej 		else
7235ecc953bSthorpej 			a->a_devs = addtoattr(a->a_devs, dev);
7245ecc953bSthorpej 	}
7255ecc953bSthorpej 
7265ecc953bSthorpej 	/* attach to parent */
7275ecc953bSthorpej 	*dev->d_app = deva;
7285ecc953bSthorpej 	dev->d_app = &deva->d_bsame;
7295ecc953bSthorpej 	return;
7305ecc953bSthorpej  bad:
7315ecc953bSthorpej 	nvfreel(atlist);
7324caf067bSdholland 	attrlist_destroyall(attrs);
7335ecc953bSthorpej }
7345ecc953bSthorpej 
7355ecc953bSthorpej /*
7365ecc953bSthorpej  * Look up a device attachment.  Also makes sure it is a reasonable
7375ecc953bSthorpej  * name, i.e., does not contain digits or special characters.
7385ecc953bSthorpej  */
7395ecc953bSthorpej struct deva *
7405ecc953bSthorpej getdevattach(const char *name)
7415ecc953bSthorpej {
742c7295a4cSchristos 	const u_char *p;
7435ecc953bSthorpej 	struct deva *deva;
7445ecc953bSthorpej 
745c7295a4cSchristos 	p = (const u_char *)name;
7465ecc953bSthorpej 	if (!isalpha(*p))
7475ecc953bSthorpej 		goto badname;
7485ecc953bSthorpej 	while (*++p) {
7495ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
7505ecc953bSthorpej 			goto badname;
7515ecc953bSthorpej 	}
7525ecc953bSthorpej 	if (isdigit(*--p)) {
7535ecc953bSthorpej  badname:
754c7295a4cSchristos 		cfgerror("bad device attachment name `%s'", name);
7555ecc953bSthorpej 		return (&errdeva);
7565ecc953bSthorpej 	}
7575ecc953bSthorpej 	deva = ht_lookup(devatab, name);
7585ecc953bSthorpej 	if (deva == NULL) {
7595ecc953bSthorpej 		deva = ecalloc(1, sizeof *deva);
7605ecc953bSthorpej 		deva->d_name = name;
7615ecc953bSthorpej 		deva->d_bsame = NULL;
7625ecc953bSthorpej 		deva->d_isdef = 0;
7635ecc953bSthorpej 		deva->d_devbase = NULL;
7645ecc953bSthorpej 		deva->d_atlist = NULL;
7655ecc953bSthorpej 		deva->d_attrs = NULL;
7665ecc953bSthorpej 		deva->d_ihead = NULL;
7675ecc953bSthorpej 		deva->d_ipp = &deva->d_ihead;
7685ecc953bSthorpej 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
7695ecc953bSthorpej 		if (ht_insert(devatab, name, deva))
770*d25ad65eSchristos 			panic("%s: Can't insert %s", __func__, name);
7715ecc953bSthorpej 	}
7725ecc953bSthorpej 	return (deva);
7735ecc953bSthorpej }
7745ecc953bSthorpej 
7755ecc953bSthorpej /*
7765ecc953bSthorpej  * Look up an attribute.
7775ecc953bSthorpej  */
7785ecc953bSthorpej struct attr *
7795ecc953bSthorpej getattr(const char *name)
7805ecc953bSthorpej {
7815ecc953bSthorpej 	struct attr *a;
7825ecc953bSthorpej 
7835ecc953bSthorpej 	if ((a = ht_lookup(attrtab, name)) == NULL) {
784c7295a4cSchristos 		cfgerror("undefined attribute `%s'", name);
7855ecc953bSthorpej 		a = &errattr;
7865ecc953bSthorpej 	}
7875ecc953bSthorpej 	return (a);
7885ecc953bSthorpej }
7895ecc953bSthorpej 
7905ecc953bSthorpej /*
7912f2f3382Suebayasi  * Implicit attribute definition.
7922f2f3382Suebayasi  */
79351deed3eSuebayasi struct attr *
7942f2f3382Suebayasi refattr(const char *name)
7952f2f3382Suebayasi {
79651deed3eSuebayasi 	struct attr *a;
7972f2f3382Suebayasi 
79851deed3eSuebayasi 	if ((a = ht_lookup(attrtab, name)) == NULL)
79951deed3eSuebayasi 		a = mkattr(name);
80051deed3eSuebayasi 	return a;
8012f2f3382Suebayasi }
8022f2f3382Suebayasi 
803bb52c144Suebayasi int
804bb52c144Suebayasi getrefattr(const char *name, struct attr **ra)
805bb52c144Suebayasi {
806bb52c144Suebayasi 	struct attr *a;
807bb52c144Suebayasi 
808bb52c144Suebayasi 	a = ht_lookup(attrtab, name);
809bb52c144Suebayasi 	if (a == NULL) {
810bb52c144Suebayasi 		*ra = NULL;
811bb52c144Suebayasi 		return (0);
812bb52c144Suebayasi 	}
813bb52c144Suebayasi 	/*
814bb52c144Suebayasi 	 * Check if the existing attr is only referenced, not really defined.
815bb52c144Suebayasi 	 */
816bb52c144Suebayasi 	if (a->a_deps == NULL &&
817bb52c144Suebayasi 	    a->a_iattr == 0 &&
818bb52c144Suebayasi 	    a->a_devclass == 0) {
819bb52c144Suebayasi 		*ra = a;
820bb52c144Suebayasi 		return (0);
821bb52c144Suebayasi 	}
822bb52c144Suebayasi 	return (1);
823bb52c144Suebayasi }
824bb52c144Suebayasi 
8252f2f3382Suebayasi /*
8265ecc953bSthorpej  * Recursively expand an attribute and its dependencies, checking for
8275ecc953bSthorpej  * cycles, and invoking a callback for each attribute found.
8285ecc953bSthorpej  */
8295ecc953bSthorpej void
8305ecc953bSthorpej expandattr(struct attr *a, void (*callback)(struct attr *))
8315ecc953bSthorpej {
8324caf067bSdholland 	struct attrlist *al;
8335ecc953bSthorpej 	struct attr *dep;
8345ecc953bSthorpej 
8355ecc953bSthorpej 	if (a->a_expanding) {
836c7295a4cSchristos 		cfgerror("circular dependency on attribute `%s'", a->a_name);
8375ecc953bSthorpej 		return;
8385ecc953bSthorpej 	}
8395ecc953bSthorpej 
8405ecc953bSthorpej 	a->a_expanding = 1;
8415ecc953bSthorpej 
8425ecc953bSthorpej 	/* First expand all of this attribute's dependencies. */
8434caf067bSdholland 	for (al = a->a_deps; al != NULL; al = al->al_next) {
8444caf067bSdholland 		dep = al->al_this;
8455ecc953bSthorpej 		expandattr(dep, callback);
8465ecc953bSthorpej 	}
8475ecc953bSthorpej 
8485ecc953bSthorpej 	/* ...and now invoke the callback for ourself. */
8495ecc953bSthorpej 	if (callback != NULL)
8505ecc953bSthorpej 		(*callback)(a);
8515ecc953bSthorpej 
8525ecc953bSthorpej 	a->a_expanding = 0;
8535ecc953bSthorpej }
8545ecc953bSthorpej 
8555ecc953bSthorpej /*
8565ecc953bSthorpej  * Set the major device number for a device, so that it can be used
8575ecc953bSthorpej  * as a root/dumps "on" device in a configuration.
8585ecc953bSthorpej  */
8595ecc953bSthorpej void
860d767912bSdrochner setmajor(struct devbase *d, devmajor_t n)
8615ecc953bSthorpej {
8625ecc953bSthorpej 
863d767912bSdrochner 	if (d != &errdev && d->d_major != NODEVMAJOR)
864d767912bSdrochner 		cfgerror("device `%s' is already major %d",
865d767912bSdrochner 		    d->d_name, d->d_major);
8665ecc953bSthorpej 	else
8675ecc953bSthorpej 		d->d_major = n;
8685ecc953bSthorpej }
8695ecc953bSthorpej 
8705ecc953bSthorpej const char *
871d767912bSdrochner major2name(devmajor_t maj)
8725ecc953bSthorpej {
8735ecc953bSthorpej 	struct devbase *dev;
8745ecc953bSthorpej 	struct devm *dm;
8755ecc953bSthorpej 
8765ecc953bSthorpej 	if (!do_devsw) {
8775ecc953bSthorpej 		TAILQ_FOREACH(dev, &allbases, d_next) {
8785ecc953bSthorpej 			if (dev->d_major == maj)
8795ecc953bSthorpej 				return (dev->d_name);
8805ecc953bSthorpej 		}
8815ecc953bSthorpej 	} else {
8825ecc953bSthorpej 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
8835ecc953bSthorpej 			if (dm->dm_bmajor == maj)
8845ecc953bSthorpej 				return (dm->dm_name);
8855ecc953bSthorpej 		}
8865ecc953bSthorpej 	}
8875ecc953bSthorpej 	return (NULL);
8885ecc953bSthorpej }
8895ecc953bSthorpej 
890d767912bSdrochner devmajor_t
8915ecc953bSthorpej dev2major(struct devbase *dev)
8925ecc953bSthorpej {
8935ecc953bSthorpej 	struct devm *dm;
8945ecc953bSthorpej 
8955ecc953bSthorpej 	if (!do_devsw)
8965ecc953bSthorpej 		return (dev->d_major);
8975ecc953bSthorpej 
8985ecc953bSthorpej 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
8995ecc953bSthorpej 		if (strcmp(dm->dm_name, dev->d_name) == 0)
9005ecc953bSthorpej 			return (dm->dm_bmajor);
9015ecc953bSthorpej 	}
902d767912bSdrochner 	return (NODEVMAJOR);
9035ecc953bSthorpej }
9045ecc953bSthorpej 
9055ecc953bSthorpej /*
9065ecc953bSthorpej  * Make a string description of the device at maj/min.
9075ecc953bSthorpej  */
9085ecc953bSthorpej static const char *
909d767912bSdrochner makedevstr(devmajor_t maj, devminor_t min)
9105ecc953bSthorpej {
911c7295a4cSchristos 	const char *devicename;
9125ecc953bSthorpej 	char buf[32];
9135ecc953bSthorpej 
914c7295a4cSchristos 	devicename = major2name(maj);
915c7295a4cSchristos 	if (devicename == NULL)
916d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
9175ecc953bSthorpej 	else
918d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
919d767912bSdrochner 		    min / maxpartitions, (min % maxpartitions) + 'a');
9205ecc953bSthorpej 
9215ecc953bSthorpej 	return (intern(buf));
9225ecc953bSthorpej }
9235ecc953bSthorpej 
9245ecc953bSthorpej /*
9255ecc953bSthorpej  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
9265ecc953bSthorpej  * Handle the case where the device number is given but there is no
9275ecc953bSthorpej  * corresponding name, and map NULL to the default.
9285ecc953bSthorpej  */
9295ecc953bSthorpej static int
9305ecc953bSthorpej resolve(struct nvlist **nvp, const char *name, const char *what,
9315ecc953bSthorpej 	struct nvlist *dflt, int part)
9325ecc953bSthorpej {
9335ecc953bSthorpej 	struct nvlist *nv;
9345ecc953bSthorpej 	struct devbase *dev;
9355ecc953bSthorpej 	const char *cp;
936d767912bSdrochner 	devmajor_t maj;
937d767912bSdrochner 	devminor_t min;
9389a7f4bbaSchristos 	size_t i, l;
9395ecc953bSthorpej 	int unit;
9405ecc953bSthorpej 	char buf[NAMESIZE];
9415ecc953bSthorpej 
9425cc303e1Slukem 	if ((part -= 'a') >= maxpartitions || part < 0)
943*d25ad65eSchristos 		panic("%s: Bad partition %c", __func__, part);
9445ecc953bSthorpej 	if ((nv = *nvp) == NULL) {
9455ecc953bSthorpej 		dev_t	d = NODEV;
9465ecc953bSthorpej 		/*
9475ecc953bSthorpej 		 * Apply default.  Easiest to do this by number.
9485ecc953bSthorpej 		 * Make sure to retain NODEVness, if this is dflt's disposition.
9495ecc953bSthorpej 		 */
9505cc303e1Slukem 		if ((dev_t)dflt->nv_num != NODEV) {
9510001b928Schristos 			maj = major(dflt->nv_num);
9520001b928Schristos 			min = ((minor(dflt->nv_num) / maxpartitions) *
9535ecc953bSthorpej 			    maxpartitions) + part;
9545ecc953bSthorpej 			d = makedev(maj, min);
9555ecc953bSthorpej 			cp = makedevstr(maj, min);
9565ecc953bSthorpej 		} else
9575ecc953bSthorpej 			cp = NULL;
9589a7f4bbaSchristos 		*nvp = nv = newnv(NULL, cp, NULL, (long long)d, NULL);
9595ecc953bSthorpej 	}
9605cc303e1Slukem 	if ((dev_t)nv->nv_num != NODEV) {
9615ecc953bSthorpej 		/*
9625ecc953bSthorpej 		 * By the numbers.  Find the appropriate major number
9635ecc953bSthorpej 		 * to make a name.
9645ecc953bSthorpej 		 */
9650001b928Schristos 		maj = major(nv->nv_num);
9660001b928Schristos 		min = minor(nv->nv_num);
9675ecc953bSthorpej 		nv->nv_str = makedevstr(maj, min);
9685ecc953bSthorpej 		return (0);
9695ecc953bSthorpej 	}
9705ecc953bSthorpej 
9715ecc953bSthorpej 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
9725ecc953bSthorpej 		/*
9735ecc953bSthorpej 		 * Wildcarded or unspecified; leave it as NODEV.
9745ecc953bSthorpej 		 */
9755ecc953bSthorpej 		return (0);
9765ecc953bSthorpej 
97779cd2972Smlelstv 	if (nv->nv_ptr != NULL && strcmp(nv->nv_ptr, "spec") == 0)
97879cd2972Smlelstv 		/*
97979cd2972Smlelstv 		 * spec string, interpreted by kernel
98079cd2972Smlelstv 		 */
98179cd2972Smlelstv 		return (0);
98279cd2972Smlelstv 
9835ecc953bSthorpej 	/*
9845ecc953bSthorpej 	 * The normal case: things like "ra2b".  Check for partition
9855ecc953bSthorpej 	 * suffix, remove it if there, and split into name ("ra") and
9865ecc953bSthorpej 	 * unit (2).
9875ecc953bSthorpej 	 */
9885ecc953bSthorpej 	l = i = strlen(nv->nv_str);
9895ecc953bSthorpej 	cp = &nv->nv_str[l];
9905ecc953bSthorpej 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
9915ecc953bSthorpej 	    isdigit((unsigned char)cp[-1])) {
9925ecc953bSthorpej 		l--;
9935ecc953bSthorpej 		part = *cp - 'a';
9945ecc953bSthorpej 	}
9955ecc953bSthorpej 	cp = nv->nv_str;
9965ecc953bSthorpej 	if (split(cp, l, buf, sizeof buf, &unit)) {
997c7295a4cSchristos 		cfgerror("%s: invalid %s device name `%s'", name, what, cp);
9985ecc953bSthorpej 		return (1);
9995ecc953bSthorpej 	}
10005ecc953bSthorpej 	dev = ht_lookup(devbasetab, intern(buf));
10015ecc953bSthorpej 	if (dev == NULL) {
1002c7295a4cSchristos 		cfgerror("%s: device `%s' does not exist", name, buf);
10035ecc953bSthorpej 		return (1);
10045ecc953bSthorpej 	}
10055ecc953bSthorpej 
10065ecc953bSthorpej 	/*
10075ecc953bSthorpej 	 * Check for the magic network interface attribute, and
10085ecc953bSthorpej 	 * don't bother making a device number.
10095ecc953bSthorpej 	 */
10105ecc953bSthorpej 	if (has_attr(dev->d_attrs, s_ifnet)) {
10119a7f4bbaSchristos 		nv->nv_num = (long long)NODEV;
10125ecc953bSthorpej 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
10135ecc953bSthorpej 	} else {
10145ecc953bSthorpej 		maj = dev2major(dev);
10155cc303e1Slukem 		if (maj == NODEVMAJOR) {
1016c7295a4cSchristos 			cfgerror("%s: can't make %s device from `%s'",
10175ecc953bSthorpej 			    name, what, nv->nv_str);
10185ecc953bSthorpej 			return (1);
10195ecc953bSthorpej 		}
10205beb9d5aSjoerg 		nv->nv_num = (long long)makedev(maj, unit * maxpartitions + part);
10215ecc953bSthorpej 	}
10225ecc953bSthorpej 
10235ecc953bSthorpej 	nv->nv_name = dev->d_name;
10245ecc953bSthorpej 	return (0);
10255ecc953bSthorpej }
10265ecc953bSthorpej 
10275ecc953bSthorpej /*
10285ecc953bSthorpej  * Add a completed configuration to the list.
10295ecc953bSthorpej  */
10305ecc953bSthorpej void
10315ecc953bSthorpej addconf(struct config *cf0)
10325ecc953bSthorpej {
10335ecc953bSthorpej 	struct config *cf;
10345ecc953bSthorpej 	const char *name;
10355ecc953bSthorpej 
10365ecc953bSthorpej 	name = cf0->cf_name;
10375ecc953bSthorpej 	cf = ecalloc(1, sizeof *cf);
10385ecc953bSthorpej 	if (ht_insert(cfhashtab, name, cf)) {
1039c7295a4cSchristos 		cfgerror("configuration `%s' already defined", name);
10405ecc953bSthorpej 		free(cf);
10415ecc953bSthorpej 		goto bad;
10425ecc953bSthorpej 	}
10435ecc953bSthorpej 	*cf = *cf0;
10445ecc953bSthorpej 
10455ecc953bSthorpej 	/*
10465ecc953bSthorpej 	 * Resolve the root device.
10475ecc953bSthorpej 	 */
1048cd429362Serh 	if (cf->cf_root == NULL) {
1049c7295a4cSchristos 		cfgerror("%s: no root device specified", name);
10505ecc953bSthorpej 		goto bad;
10515ecc953bSthorpej 	}
1052cd429362Serh 	if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
1053cd429362Serh 		struct nvlist *nv;
1054cd429362Serh 		nv = cf->cf_root;
10555ecc953bSthorpej 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
10565ecc953bSthorpej 			goto bad;
10575ecc953bSthorpej 	}
10585ecc953bSthorpej 
10595ecc953bSthorpej 	/*
10605ecc953bSthorpej 	 * Resolve the dump device.
10615ecc953bSthorpej 	 */
10625ecc953bSthorpej 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
10635ecc953bSthorpej 		/*
10645ecc953bSthorpej 		 * Wildcarded dump device is equivalent to unspecified.
10655ecc953bSthorpej 		 */
10665ecc953bSthorpej 		cf->cf_dump = NULL;
10675ecc953bSthorpej 	} else if (cf->cf_dump->nv_str == s_none) {
10685ecc953bSthorpej 		/*
10695ecc953bSthorpej 		 * Operator has requested that no dump device should be
10705ecc953bSthorpej 		 * configured; do nothing.
10715ecc953bSthorpej 		 */
10725ecc953bSthorpej 	} else {
10735ecc953bSthorpej 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
10745ecc953bSthorpej 			goto bad;
10755ecc953bSthorpej 	}
10765ecc953bSthorpej 
10775ecc953bSthorpej 	/* Wildcarded fstype is `unspecified'. */
10785ecc953bSthorpej 	if (cf->cf_fstype == s_qmark)
10795ecc953bSthorpej 		cf->cf_fstype = NULL;
10805ecc953bSthorpej 
10815ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
10825ecc953bSthorpej 	return;
10835ecc953bSthorpej  bad:
10845ecc953bSthorpej 	nvfreel(cf0->cf_root);
10855ecc953bSthorpej 	nvfreel(cf0->cf_dump);
10865ecc953bSthorpej }
10875ecc953bSthorpej 
10885ecc953bSthorpej void
10895ecc953bSthorpej setconf(struct nvlist **npp, const char *what, struct nvlist *v)
10905ecc953bSthorpej {
10915ecc953bSthorpej 
10925ecc953bSthorpej 	if (*npp != NULL) {
1093c7295a4cSchristos 		cfgerror("duplicate %s specification", what);
10945ecc953bSthorpej 		nvfreel(v);
10955ecc953bSthorpej 	} else
10965ecc953bSthorpej 		*npp = v;
10975ecc953bSthorpej }
10985ecc953bSthorpej 
10995ecc953bSthorpej void
11003dbd1573Schristos delconf(const char *name, int nowarn)
110142b52b8aScube {
110242b52b8aScube 	struct config *cf;
110342b52b8aScube 
110475eb07f2Suebayasi 	CFGDBG(5, "deselecting config `%s'", name);
110542b52b8aScube 	if (ht_lookup(cfhashtab, name) == NULL) {
11063dbd1573Schristos 		if (!nowarn)
1107c7295a4cSchristos 			cfgerror("configuration `%s' undefined", name);
110842b52b8aScube 		return;
110942b52b8aScube 	}
111042b52b8aScube 	(void)ht_remove(cfhashtab, name);
111142b52b8aScube 
111242b52b8aScube 	TAILQ_FOREACH(cf, &allcf, cf_next)
111342b52b8aScube 		if (!strcmp(cf->cf_name, name))
111442b52b8aScube 			break;
111542b52b8aScube 	if (cf == NULL)
1116*d25ad65eSchristos 		panic("%s: lost configuration for %s", __func__, name);
111742b52b8aScube 
111842b52b8aScube 	TAILQ_REMOVE(&allcf, cf, cf_next);
111942b52b8aScube }
112042b52b8aScube 
112142b52b8aScube void
11225ecc953bSthorpej setfstype(const char **fstp, const char *v)
11235ecc953bSthorpej {
11245ecc953bSthorpej 
11255ecc953bSthorpej 	if (*fstp != NULL) {
1126c7295a4cSchristos 		cfgerror("multiple fstype specifications");
11275ecc953bSthorpej 		return;
11285ecc953bSthorpej 	}
11295ecc953bSthorpej 
11305ecc953bSthorpej 	if (v != s_qmark && OPT_FSOPT(v)) {
1131c7295a4cSchristos 		cfgerror("\"%s\" is not a configured file system", v);
11325ecc953bSthorpej 		return;
11335ecc953bSthorpej 	}
11345ecc953bSthorpej 
11355ecc953bSthorpej 	*fstp = v;
11365ecc953bSthorpej }
11375ecc953bSthorpej 
11385ecc953bSthorpej static struct devi *
11395ecc953bSthorpej newdevi(const char *name, int unit, struct devbase *d)
11405ecc953bSthorpej {
11415ecc953bSthorpej 	struct devi *i;
11425ecc953bSthorpej 
11435ecc953bSthorpej 	i = ecalloc(1, sizeof *i);
11445ecc953bSthorpej 	i->i_name = name;
11455ecc953bSthorpej 	i->i_unit = unit;
11465ecc953bSthorpej 	i->i_base = d;
11475ecc953bSthorpej 	i->i_bsame = NULL;
11485ecc953bSthorpej 	i->i_asame = NULL;
11495ecc953bSthorpej 	i->i_alias = NULL;
11505ecc953bSthorpej 	i->i_at = NULL;
11515ecc953bSthorpej 	i->i_pspec = NULL;
11525ecc953bSthorpej 	i->i_atdeva = NULL;
11535ecc953bSthorpej 	i->i_locs = NULL;
11545ecc953bSthorpej 	i->i_cfflags = 0;
11555ecc953bSthorpej 	i->i_lineno = currentline();
11560dbd1c0eScube 	i->i_srcfile = yyfile;
11577aa6070dScube 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
1158a31ff6b4Scube 	i->i_level = devilevel;
115990ac64deSpooka 	i->i_pseudoroot = 0;
11605ecc953bSthorpej 	if (unit >= d->d_umax)
11615ecc953bSthorpej 		d->d_umax = unit + 1;
11625ecc953bSthorpej 	return (i);
11635ecc953bSthorpej }
11645ecc953bSthorpej 
116553be83e0Schristos static struct attr *
116653be83e0Schristos finddevattr(const char *name, const char *at, struct devbase *ib,
116753be83e0Schristos     struct devbase **ab, int *atunit)
11685ecc953bSthorpej {
11695ecc953bSthorpej 	const char *cp;
11705ecc953bSthorpej 	char atbuf[NAMESIZE];
117153be83e0Schristos 	struct attrlist *al;
117253be83e0Schristos 	struct attr *attr;
11735ecc953bSthorpej 
11745ecc953bSthorpej 	if (at == NULL) {
117553be83e0Schristos 		*ab = NULL;
117653be83e0Schristos 		*atunit = -1;
117753be83e0Schristos 		return &errattr;	/* a convenient "empty" attr */
11785ecc953bSthorpej 	}
117953be83e0Schristos 	if (split(at, strlen(at), atbuf, sizeof atbuf, atunit)) {
1180c7295a4cSchristos 		cfgerror("invalid attachment name `%s'", at);
11815ecc953bSthorpej 		/* (void)getdevi(name); -- ??? */
118253be83e0Schristos 		return NULL;
11835ecc953bSthorpej 	}
11845ecc953bSthorpej 
11855ecc953bSthorpej 	/*
11865ecc953bSthorpej 	 * Devices can attach to two types of things: Attributes,
11875ecc953bSthorpej 	 * and other devices (which have the appropriate attributes
11885ecc953bSthorpej 	 * to allow attachment).
11895ecc953bSthorpej 	 *
11905ecc953bSthorpej 	 * (1) If we're attached to an attribute, then we don't need
11915ecc953bSthorpej 	 *     look at the parent base device to see what attributes
11925ecc953bSthorpej 	 *     it has, and make sure that we can attach to them.
11935ecc953bSthorpej 	 *
11945ecc953bSthorpej 	 * (2) If we're attached to a real device (i.e. named in
11955ecc953bSthorpej 	 *     the config file), we want to remember that so that
11965ecc953bSthorpej 	 *     at cross-check time, if the device we're attached to
11975ecc953bSthorpej 	 *     is missing but other devices which also provide the
11985ecc953bSthorpej 	 *     attribute are present, we don't get a false "OK."
11995ecc953bSthorpej 	 *
12005ecc953bSthorpej 	 * (3) If the thing we're attached to is an attribute
12015ecc953bSthorpej 	 *     but is actually named in the config file, we still
12025ecc953bSthorpej 	 *     have to remember its devbase.
12035ecc953bSthorpej 	 */
12045ecc953bSthorpej 	cp = intern(atbuf);
12055ecc953bSthorpej 
12065ecc953bSthorpej 	/* Figure out parent's devbase, to satisfy case (3). */
120753be83e0Schristos 	*ab = ht_lookup(devbasetab, cp);
12085ecc953bSthorpej 
12095ecc953bSthorpej 	/* Find out if it's an attribute. */
12105ecc953bSthorpej 	attr = ht_lookup(attrtab, cp);
12115ecc953bSthorpej 
12125ecc953bSthorpej 	/* Make sure we're _really_ attached to the attr.  Case (1). */
12135ecc953bSthorpej 	if (attr != NULL && onlist(attr->a_devs, ib))
121453be83e0Schristos 		return attr;
12155ecc953bSthorpej 
12165ecc953bSthorpej 	/*
12175ecc953bSthorpej 	 * Else a real device, and not just an attribute.  Case (2).
12185ecc953bSthorpej 	 *
12195ecc953bSthorpej 	 * Have to work a bit harder to see whether we have
12205ecc953bSthorpej 	 * something like "tg0 at esp0" (where esp is merely
12215ecc953bSthorpej 	 * not an attribute) or "tg0 at nonesuch0" (where
12225ecc953bSthorpej 	 * nonesuch is not even a device).
12235ecc953bSthorpej 	 */
122453be83e0Schristos 	if (*ab == NULL) {
122553be83e0Schristos 		cfgerror("%s at %s: `%s' unknown", name, at, atbuf);
122653be83e0Schristos 		return NULL;
12275ecc953bSthorpej 	}
12285ecc953bSthorpej 
12295ecc953bSthorpej 	/*
12305ecc953bSthorpej 	 * See if the named parent carries an attribute
12315ecc953bSthorpej 	 * that allows it to supervise device ib.
12325ecc953bSthorpej 	 */
123353be83e0Schristos 	for (al = (*ab)->d_attrs; al != NULL; al = al->al_next) {
12344caf067bSdholland 		attr = al->al_this;
12355ecc953bSthorpej 		if (onlist(attr->a_devs, ib))
123653be83e0Schristos 			return attr;
12375ecc953bSthorpej 	}
1238c7295a4cSchristos 	cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
123953be83e0Schristos 	return NULL;
124053be83e0Schristos }
124153be83e0Schristos 
124253be83e0Schristos /*
124353be83e0Schristos  * Add the named device as attaching to the named attribute (or perhaps
124453be83e0Schristos  * another device instead) plus unit number.
124553be83e0Schristos  */
124653be83e0Schristos void
124753be83e0Schristos adddev(const char *name, const char *at, struct loclist *loclist, int flags)
124853be83e0Schristos {
124953be83e0Schristos 	struct devi *i;		/* the new instance */
125053be83e0Schristos 	struct pspec *p;	/* and its pspec */
125153be83e0Schristos 	struct attr *attr;	/* attribute that allows attach */
125253be83e0Schristos 	struct devbase *ib;	/* i->i_base */
125353be83e0Schristos 	struct devbase *ab;	/* not NULL => at another dev */
125453be83e0Schristos 	struct deva *iba;	/* devbase attachment used */
125553be83e0Schristos 	struct deva *lastiba;
1256*d25ad65eSchristos 	int atunit, first;
125753be83e0Schristos 
125853be83e0Schristos 	lastiba = NULL;
125953be83e0Schristos 	if ((i = getdevi(name)) == NULL)
126053be83e0Schristos 		goto bad;
126153be83e0Schristos 	ib = i->i_base;
126253be83e0Schristos 	attr = finddevattr(name, at, ib, &ab, &atunit);
126353be83e0Schristos 	if (attr == NULL) {
12645ec393e9Scube 		i->i_active = DEVI_BROKEN;
12655ecc953bSthorpej 		goto bad;
126653be83e0Schristos 	}
12675ecc953bSthorpej 
126853be83e0Schristos 	for (lastiba = ib->d_ahead; lastiba; lastiba = iba->d_bsame) {
126953be83e0Schristos 		for (iba = lastiba; iba != NULL; iba = iba->d_bsame)
127053be83e0Schristos 			if (onlist(iba->d_atlist,
127153be83e0Schristos 			    attr == &errattr ? NULL : attr))
12725ecc953bSthorpej 				break;
127353be83e0Schristos 
1274*d25ad65eSchristos 		first = lastiba == ib->d_ahead;
127553be83e0Schristos 		if (iba == NULL) {
1276*d25ad65eSchristos 			if (!first)
127753be83e0Schristos 				goto bad;
127853be83e0Schristos 			if (attr != &errattr) {
1279*d25ad65eSchristos 				panic("%s: can't figure out attachment",
1280*d25ad65eSchristos 				    __func__);
128153be83e0Schristos 			} else {
128253be83e0Schristos 				cfgerror("`%s' cannot attach to the root",
128353be83e0Schristos 				    ib->d_name);
128453be83e0Schristos 				i->i_active = DEVI_BROKEN;
12855ecc953bSthorpej 			}
128653be83e0Schristos 		}
128753be83e0Schristos 		// get a new one if it is not the first time
1288*d25ad65eSchristos 		if (!first && (i = getdevi(name)) == NULL)
128953be83e0Schristos 			goto bad;
129053be83e0Schristos 
129153be83e0Schristos 		if (attr != &errattr) {
129253be83e0Schristos 			/*
129353be83e0Schristos 			 * Find the parent spec.  If a matching one has not
129453be83e0Schristos 			 * yet been created, create one.
129553be83e0Schristos 			 *
129653be83e0Schristos 			 * XXX: This creates multiple pspecs that look the
129753be83e0Schristos 			 * same in the config file and could be merged.
129853be83e0Schristos 			 */
129953be83e0Schristos 			p = getpspec(attr, ab, atunit, iba);
130053be83e0Schristos 			p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
1301*d25ad65eSchristos 		} else
1302*d25ad65eSchristos 			p = NULL;
130353be83e0Schristos 
13045ec393e9Scube 		if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
13055ec393e9Scube 			i->i_active = DEVI_BROKEN;
13065ecc953bSthorpej 			goto bad;
13075ec393e9Scube 		}
13085ecc953bSthorpej 		i->i_at = at;
13095ecc953bSthorpej 		i->i_pspec = p;
13105ecc953bSthorpej 		i->i_atdeva = iba;
13115ecc953bSthorpej 		i->i_cfflags = flags;
131253be83e0Schristos 		CFGDBG(3, "devi `%s' at '%s' added", i->i_name, iba->d_name);
13135ecc953bSthorpej 
13145ecc953bSthorpej 		*iba->d_ipp = i;
13155ecc953bSthorpej 		iba->d_ipp = &i->i_asame;
131653be83e0Schristos 	}
13175ecc953bSthorpej 
13185ecc953bSthorpej 	/* all done, fall into ... */
13195ecc953bSthorpej  bad:
132016a8771bSdholland 	loclist_destroy(loclist);
13215ecc953bSthorpej 	return;
13225ecc953bSthorpej }
13235ecc953bSthorpej 
13245ecc953bSthorpej void
13253dbd1573Schristos deldevi(const char *name, const char *at, int nowarn)
13265ecc953bSthorpej {
13277b7c582aScube 	struct devi *firsti, *i;
13285ecc953bSthorpej 	struct devbase *d;
13295ecc953bSthorpej 	int unit;
13305ecc953bSthorpej 	char base[NAMESIZE];
13315ecc953bSthorpej 
133275eb07f2Suebayasi 	CFGDBG(5, "deselecting devi `%s'", name);
13335ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
13343dbd1573Schristos 		if (!nowarn) {
1335c7295a4cSchristos 			cfgerror("invalid device name `%s'", name);
13365ecc953bSthorpej 			return;
13375ecc953bSthorpej 		}
13383dbd1573Schristos 	}
13395ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
13405ecc953bSthorpej 	if (d == NULL) {
13413dbd1573Schristos 		if (!nowarn)
1342c7295a4cSchristos 			cfgerror("%s: unknown device `%s'", name, base);
13435ecc953bSthorpej 		return;
13445ecc953bSthorpej 	}
13455ecc953bSthorpej 	if (d->d_ispseudo) {
1346c7295a4cSchristos 		cfgerror("%s: %s is a pseudo-device", name, base);
13475ecc953bSthorpej 		return;
13485ecc953bSthorpej 	}
13495ecc953bSthorpej 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
1350c7295a4cSchristos 		cfgerror("`%s' not defined", name);
13515ecc953bSthorpej 		return;
13525ecc953bSthorpej 	}
13537b7c582aScube 	if (at == NULL && firsti->i_at == NULL) {
1354e499d8b5Scube 		/* 'at root' */
13557b7c582aScube 		remove_devi(firsti);
13567b7c582aScube 		return;
13577b7c582aScube 	} else if (at != NULL)
13587b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
1359bfc250f2Scube 			if (i->i_active != DEVI_BROKEN &&
1360bfc250f2Scube 			    strcmp(at, i->i_at) == 0) {
13617b7c582aScube 				remove_devi(i);
13625ecc953bSthorpej 				return;
13635ecc953bSthorpej 			}
1364c7295a4cSchristos 	cfgerror("`%s' at `%s' not found", name, at ? at : "root");
1365e499d8b5Scube }
13665ecc953bSthorpej 
13677b7c582aScube static void
1368511fbc70Schristos remove_pspec(struct devi *i)
1369511fbc70Schristos {
1370511fbc70Schristos 	struct pspec *p = i->i_pspec;
1371511fbc70Schristos 	struct nvlist *nv, *onv;
1372511fbc70Schristos 
1373511fbc70Schristos 	if (p == NULL)
1374511fbc70Schristos 		return;
1375511fbc70Schristos 
1376511fbc70Schristos 	/* Double-linked nvlist anyone? */
1377511fbc70Schristos 	for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
1378511fbc70Schristos 		if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1379511fbc70Schristos 			onv = nv->nv_next;
1380511fbc70Schristos 			nv->nv_next = onv->nv_next;
1381511fbc70Schristos 			nvfree(onv);
1382511fbc70Schristos 			break;
1383511fbc70Schristos 		}
1384511fbc70Schristos 		if (nv->nv_ptr == i) {
1385511fbc70Schristos 			/* nv is p->p_devs in that case */
1386511fbc70Schristos 			p->p_devs = nv->nv_next;
1387511fbc70Schristos 			nvfree(nv);
1388511fbc70Schristos 			break;
1389511fbc70Schristos 		}
1390511fbc70Schristos 	}
1391511fbc70Schristos 	if (p->p_devs == NULL)
1392511fbc70Schristos 		TAILQ_REMOVE(&allpspecs, p, p_list);
1393511fbc70Schristos }
1394511fbc70Schristos 
1395511fbc70Schristos static void
13967b7c582aScube remove_devi(struct devi *i)
13977b7c582aScube {
13987b7c582aScube 	struct devbase *d = i->i_base;
13997b7c582aScube 	struct devi *f, *j, **ppi;
14007b7c582aScube 	struct deva *iba;
14017b7c582aScube 
14028d928a75Suebayasi 	CFGDBG(5, "removing devi `%s'", i->i_name);
14037b7c582aScube 	f = ht_lookup(devitab, i->i_name);
14046a6299ebScube 	if (f == NULL)
1405*d25ad65eSchristos 		panic("%s: instance %s disappeared from devitab", __func__,
14066a6299ebScube 		    i->i_name);
14077b7c582aScube 
1408bfc250f2Scube 	if (i->i_active == DEVI_BROKEN) {
1409bfc250f2Scube 		cfgerror("not removing broken instance `%s'", i->i_name);
1410bfc250f2Scube 		return;
1411bfc250f2Scube 	}
1412bfc250f2Scube 
1413e499d8b5Scube 	/*
1414e499d8b5Scube 	 * We have the device instance, i.
1415e499d8b5Scube 	 * We have to:
1416e499d8b5Scube 	 *   - delete the alias
1417e499d8b5Scube 	 *
1418e499d8b5Scube 	 *      If the devi was an alias of an already listed devi, all is
1419e499d8b5Scube 	 *      good we don't have to do more.
1420e499d8b5Scube 	 *      If it was the first alias, we have to replace i's entry in
1421e499d8b5Scube 	 *      d's list by its first alias.
1422e499d8b5Scube 	 *      If it was the only entry, we must remove i's entry from d's
1423e499d8b5Scube 	 *      list.
1424e499d8b5Scube 	 */
14257b7c582aScube 	if (i != f) {
1426db7dbdacSuebayasi 		for (j = f; j->i_alias != i; j = j->i_alias)
1427db7dbdacSuebayasi 			continue;
14287b7c582aScube 		j->i_alias = i->i_alias;
14293b405775Scube 	} else {
14303b405775Scube 		if (i->i_alias == NULL) {
1431e499d8b5Scube 			/* No alias, must unlink the entry from devitab */
14327b7c582aScube 			ht_remove(devitab, i->i_name);
14337b7c582aScube 			j = i->i_bsame;
14343b405775Scube 		} else {
1435e499d8b5Scube 			/* Or have the first alias replace i in d's list */
1436e499d8b5Scube 			i->i_alias->i_bsame = i->i_bsame;
14377b7c582aScube 			j = i->i_alias;
14387b7c582aScube 			if (i == f)
14397b7c582aScube 				ht_replace(devitab, i->i_name, i->i_alias);
14403b405775Scube 		}
14413b405775Scube 
1442e499d8b5Scube 		/*
1443e499d8b5Scube 		 *   - remove/replace the instance from the devbase's list
1444e499d8b5Scube 		 *
1445e499d8b5Scube 		 * A double-linked list would make this much easier.  Oh, well,
1446e499d8b5Scube 		 * what is done is done.
1447e499d8b5Scube 		 */
1448e499d8b5Scube 		for (ppi = &d->d_ihead;
1449e499d8b5Scube 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1450db7dbdacSuebayasi 		    ppi = &(*ppi)->i_bsame)
1451db7dbdacSuebayasi 			continue;
1452e499d8b5Scube 		if (*ppi == NULL)
1453*d25ad65eSchristos 			panic("%s: dev (%s) doesn't list the devi (%s at %s)",
1454*d25ad65eSchristos 			    __func__, d->d_name, i->i_name, i->i_at);
14557b7c582aScube 		f = *ppi;
14567b7c582aScube 		if (f == i)
1457c3414672Scube 			/* That implies d->d_ihead == i */
14587b7c582aScube 			*ppi = j;
1459e499d8b5Scube 		else
14607b7c582aScube 			(*ppi)->i_bsame = j;
1461e499d8b5Scube 		if (d->d_ipp == &i->i_bsame) {
14623b405775Scube 			if (i->i_alias == NULL) {
14637b7c582aScube 				if (f == i)
1464e499d8b5Scube 					d->d_ipp = &d->d_ihead;
1465e499d8b5Scube 				else
14667b7c582aScube 					d->d_ipp = &f->i_bsame;
14673b405775Scube 			} else
14683b405775Scube 				d->d_ipp = &i->i_alias->i_bsame;
1469e499d8b5Scube 		}
1470e499d8b5Scube 	}
1471e499d8b5Scube 	/*
1472e499d8b5Scube 	 *   - delete the attachment instance
1473e499d8b5Scube 	 */
1474e499d8b5Scube 	iba = i->i_atdeva;
1475e499d8b5Scube 	for (ppi = &iba->d_ihead;
1476e499d8b5Scube 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1477db7dbdacSuebayasi 	    ppi = &(*ppi)->i_asame)
1478db7dbdacSuebayasi 		continue;
1479e499d8b5Scube 	if (*ppi == NULL)
1480*d25ad65eSchristos 		panic("%s: deva (%s) doesn't list the devi (%s)", __func__,
1481e499d8b5Scube 		    iba->d_name, i->i_name);
14827b7c582aScube 	f = *ppi;
14837b7c582aScube 	if (f == i)
1484c3414672Scube 		/* That implies iba->d_ihead == i */
1485e499d8b5Scube 		*ppi = i->i_asame;
1486e499d8b5Scube 	else
1487e499d8b5Scube 		(*ppi)->i_asame = i->i_asame;
1488e499d8b5Scube 	if (iba->d_ipp == &i->i_asame) {
14897b7c582aScube 		if (f == i)
1490e499d8b5Scube 			iba->d_ipp = &iba->d_ihead;
1491e499d8b5Scube 		else
14927b7c582aScube 			iba->d_ipp = &f->i_asame;
1493e499d8b5Scube 	}
1494e499d8b5Scube 	/*
1495e499d8b5Scube 	 *   - delete the pspec
1496e499d8b5Scube 	 */
1497511fbc70Schristos 	remove_pspec(i);
1498e499d8b5Scube 
1499e499d8b5Scube 	/*
1500e499d8b5Scube 	 *   - delete the alldevi entry
1501e499d8b5Scube 	 */
1502e499d8b5Scube 	TAILQ_REMOVE(&alldevi, i, i_next);
1503e499d8b5Scube 	ndevi--;
15047aa6070dScube 	/*
15057aa6070dScube 	 * Put it in deaddevitab
1506a31ff6b4Scube 	 *
1507a31ff6b4Scube 	 * Each time a devi is removed, devilevel is increased so that later on
1508a31ff6b4Scube 	 * it is possible to tell if an instance was added before or after the
1509a31ff6b4Scube 	 * removal of its parent.
1510a31ff6b4Scube 	 *
1511a31ff6b4Scube 	 * For active instances, i_level contains the number of devi removed so
1512a31ff6b4Scube 	 * far, and for dead devis, it contains its index.
15137aa6070dScube 	 */
1514a31ff6b4Scube 	i->i_level = devilevel++;
15157aa6070dScube 	i->i_alias = NULL;
15167aa6070dScube 	f = ht_lookup(deaddevitab, i->i_name);
15177aa6070dScube 	if (f == NULL) {
15187aa6070dScube 		if (ht_insert(deaddevitab, i->i_name, i))
1519*d25ad65eSchristos 			panic("%s: can't add %s to deaddevitab", __func__,
15207aa6070dScube 			    i->i_name);
15217aa6070dScube 	} else {
1522db7dbdacSuebayasi 		for (j = f; j->i_alias != NULL; j = j->i_alias)
1523db7dbdacSuebayasi 			continue;
15247aa6070dScube 		j->i_alias = i;
15257aa6070dScube 	}
1526e499d8b5Scube 	/*
15275d1e8b27Swiz 	 *   - reconstruct d->d_umax
1528e499d8b5Scube 	 */
1529e499d8b5Scube 	d->d_umax = 0;
1530e499d8b5Scube 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1531e499d8b5Scube 		if (i->i_unit >= d->d_umax)
1532e499d8b5Scube 			d->d_umax = i->i_unit + 1;
15335ecc953bSthorpej }
15345ecc953bSthorpej 
15355ecc953bSthorpej void
15363dbd1573Schristos deldeva(const char *at, int nowarn)
15377b7c582aScube {
15387b7c582aScube 	int unit;
15397b7c582aScube 	const char *cp;
15407b7c582aScube 	struct devbase *d, *ad;
15417b7c582aScube 	struct devi *i, *j;
15427b7c582aScube 	struct attr *a;
15437b7c582aScube 	struct pspec *p;
15447b7c582aScube 	struct nvlist *nv, *stack = NULL;
15457b7c582aScube 
15467b7c582aScube 	if (at == NULL) {
15477b7c582aScube 		TAILQ_FOREACH(i, &alldevi, i_next)
15487b7c582aScube 			if (i->i_at == NULL)
15497b7c582aScube 				stack = newnv(NULL, NULL, i, 0, stack);
15507b7c582aScube 	} else {
15519a7f4bbaSchristos 		size_t l;
15527b7c582aScube 
155375eb07f2Suebayasi 		CFGDBG(5, "deselecting deva `%s'", at);
15549a7f4bbaSchristos 		if (at[0] == '\0')
15559a7f4bbaSchristos 			goto out;
15569a7f4bbaSchristos 
15577b7c582aScube 		l = strlen(at) - 1;
15587b7c582aScube 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
15597b7c582aScube 			char base[NAMESIZE];
15607b7c582aScube 
15617b7c582aScube 			if (split(at, l+1, base, sizeof base, &unit)) {
15629a7f4bbaSchristos out:
1563c7295a4cSchristos 				cfgerror("invalid attachment name `%s'", at);
15647b7c582aScube 				return;
15657b7c582aScube 			}
15667b7c582aScube 			cp = intern(base);
15677b7c582aScube 		} else {
15687b7c582aScube 			cp = intern(at);
15697b7c582aScube 			unit = STAR;
15707b7c582aScube 		}
15717b7c582aScube 
15727b7c582aScube 		ad = ht_lookup(devbasetab, cp);
15737b7c582aScube 		a = ht_lookup(attrtab, cp);
15747b7c582aScube 		if (a == NULL) {
1575c7295a4cSchristos 			cfgerror("unknown attachment attribute or device `%s'",
15767b7c582aScube 			    cp);
15777b7c582aScube 			return;
15787b7c582aScube 		}
15797b7c582aScube 		if (!a->a_iattr) {
1580c7295a4cSchristos 			cfgerror("plain attribute `%s' cannot have children",
15817b7c582aScube 			    a->a_name);
15827b7c582aScube 			return;
15837b7c582aScube 		}
15847b7c582aScube 
15857b7c582aScube 		/*
15867b7c582aScube 		 * remove_devi() makes changes to the devbase's list and the
15877b7c582aScube 		 * alias list, * so the actual deletion of the instances must
15887b7c582aScube 		 * be delayed.
15897b7c582aScube 		 */
15907b7c582aScube 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
15917b7c582aScube 			d = nv->nv_ptr;
15927b7c582aScube 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
15937b7c582aScube 				for (j = i; j != NULL; j = j->i_alias) {
15947b7c582aScube 					/* Ignore devices at root */
15957b7c582aScube 					if (j->i_at == NULL)
15967b7c582aScube 						continue;
15977b7c582aScube 					p = j->i_pspec;
15987b7c582aScube 					/*
15997b7c582aScube 					 * There are three cases:
16007b7c582aScube 					 *
16017b7c582aScube 					 * 1.  unit is not STAR.  Consider 'at'
16027b7c582aScube 					 *     to be explicit, even if it
16037b7c582aScube 					 *     references an interface
16047b7c582aScube 					 *     attribute.
16057b7c582aScube 					 *
16067b7c582aScube 					 * 2.  unit is STAR and 'at' references
16077b7c582aScube 					 *     a real device.  Look for pspec
16087b7c582aScube 					 *     that have a matching p_atdev
16097b7c582aScube 					 *     field.
16107b7c582aScube 					 *
16117b7c582aScube 					 * 3.  unit is STAR and 'at' references
16127b7c582aScube 					 *     an interface attribute.  Look
16137b7c582aScube 					 *     for pspec that have a matching
16147b7c582aScube 					 *     p_iattr field.
16157b7c582aScube 					 */
16167b7c582aScube 					if ((unit != STAR &&        /* Case */
16177b7c582aScube 					     !strcmp(j->i_at, at)) ||  /* 1 */
16187b7c582aScube 					    (unit == STAR &&
16197b7c582aScube 					     ((ad != NULL &&        /* Case */
16207b7c582aScube 					       p->p_atdev == ad) ||    /* 2 */
16217b7c582aScube 					      (ad == NULL &&        /* Case */
16227b7c582aScube 					       p->p_iattr == a))))     /* 3 */
16237b7c582aScube 						stack = newnv(NULL, NULL, j, 0,
16247b7c582aScube 						    stack);
16257b7c582aScube 				}
16267b7c582aScube 		}
16277b7c582aScube 	}
16287b7c582aScube 
162911a68adeSchristos 	devcleanup(stack);
16307b7c582aScube }
16317b7c582aScube 
16327b7c582aScube void
16333dbd1573Schristos deldev(const char *name, int nowarn)
16347b7c582aScube {
16359a7f4bbaSchristos 	size_t l;
16367b7c582aScube 	struct devi *firsti, *i;
163711a68adeSchristos 	struct nvlist *stack = NULL;
16387b7c582aScube 
163975eb07f2Suebayasi 	CFGDBG(5, "deselecting dev `%s'", name);
16409a7f4bbaSchristos 	if (name[0] == '\0')
16419a7f4bbaSchristos 		goto out;
16429a7f4bbaSchristos 
16437b7c582aScube 	l = strlen(name) - 1;
16447b7c582aScube 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
16457b7c582aScube 		/* `no mydev0' or `no mydev*' */
16467b7c582aScube 		firsti = ht_lookup(devitab, name);
16477b7c582aScube 		if (firsti == NULL) {
16489a7f4bbaSchristos out:
16493dbd1573Schristos 			if (!nowarn)
1650c7295a4cSchristos 				cfgerror("unknown instance %s", name);
16517b7c582aScube 			return;
16527b7c582aScube 		}
16537b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
16547b7c582aScube 			stack = newnv(NULL, NULL, i, 0, stack);
16557b7c582aScube 	} else {
16567b7c582aScube 		struct devbase *d = ht_lookup(devbasetab, name);
16577b7c582aScube 
16587b7c582aScube 		if (d == NULL) {
1659c7295a4cSchristos 			cfgerror("unknown device %s", name);
16607b7c582aScube 			return;
16617b7c582aScube 		}
166271af9028Scube 		if (d->d_ispseudo) {
1663c7295a4cSchristos 			cfgerror("%s is a pseudo-device; "
166471af9028Scube 			    "use \"no pseudo-device %s\" instead", name,
166571af9028Scube 			    name);
166671af9028Scube 			return;
166771af9028Scube 		}
166811a68adeSchristos 		stack = makedevstack(d);
16697b7c582aScube 	}
16707b7c582aScube 
167111a68adeSchristos 	devcleanup(stack);
16727b7c582aScube }
16737b7c582aScube 
167400ed8a38Spooka /*
167500ed8a38Spooka  * Insert given device "name" into devroottab.  In case "name"
167600ed8a38Spooka  * designates a pure interface attribute, create a fake device
167700ed8a38Spooka  * instance for the attribute and insert that into the roottab
167800ed8a38Spooka  * (this scheme avoids mucking around with the orphanage analysis).
167900ed8a38Spooka  */
16807b7c582aScube void
168190ac64deSpooka addpseudoroot(const char *name)
168290ac64deSpooka {
1683194e1c80Spooka 	char buf[NAMESIZE];
1684194e1c80Spooka 	int unit;
1685194e1c80Spooka 	struct attr *attr;
168690ac64deSpooka 	struct devi *i;
168790ac64deSpooka 	struct deva *iba;
168890ac64deSpooka 	struct devbase *ib;
168990ac64deSpooka 
1690194e1c80Spooka 	if (split(name, strlen(name), buf, sizeof(buf), &unit)) {
1691194e1c80Spooka 		cfgerror("invalid pseudo-root name `%s'", name);
1692194e1c80Spooka 		return;
1693194e1c80Spooka 	}
1694194e1c80Spooka 
1695194e1c80Spooka 	/*
169600ed8a38Spooka 	 * Prefer device because devices with locators define an
169700ed8a38Spooka 	 * implicit interface attribute.  However, if a device is
169800ed8a38Spooka 	 * not available, try to attach to the interface attribute.
169900ed8a38Spooka 	 * This makes sure adddev() doesn't get confused when we
170000ed8a38Spooka 	 * are really attaching to a device (alternatively we maybe
170100ed8a38Spooka 	 * could specify a non-NULL atlist to defdevattach() below).
1702194e1c80Spooka 	 */
170300ed8a38Spooka 	ib = ht_lookup(devbasetab, intern(buf));
170400ed8a38Spooka 	if (ib == NULL) {
1705194e1c80Spooka 		struct devbase *fakedev;
1706194e1c80Spooka 		char fakename[NAMESIZE];
1707194e1c80Spooka 
170800ed8a38Spooka 		attr = ht_lookup(attrtab, intern(buf));
170900ed8a38Spooka 		if (!(attr && attr->a_iattr)) {
171000ed8a38Spooka 			cfgerror("pseudo-root `%s' not available", name);
171100ed8a38Spooka 			return;
171200ed8a38Spooka 		}
171300ed8a38Spooka 
1714194e1c80Spooka 		/*
1715194e1c80Spooka 		 * here we cheat a bit: create a fake devbase with the
1716194e1c80Spooka 		 * interface attribute and instantiate it.  quick, cheap,
1717194e1c80Spooka 		 * dirty & bad for you, much like the stuff in the fridge.
1718194e1c80Spooka 		 * and, it works, since the pseudoroot device is not included
1719194e1c80Spooka 		 * in ioconf, just used by config to make sure we start from
1720194e1c80Spooka 		 * the right place.
1721194e1c80Spooka 		 */
172200ed8a38Spooka 		snprintf(fakename, sizeof(fakename), "%s_devattrs", buf);
1723194e1c80Spooka 		fakedev = getdevbase(intern(fakename));
1724194e1c80Spooka 		fakedev->d_isdef = 1;
1725194e1c80Spooka 		fakedev->d_ispseudo = 0;
17264caf067bSdholland 		fakedev->d_attrs = attrlist_cons(NULL, attr);
1727194e1c80Spooka 		defdevattach(NULL, fakedev, NULL, NULL);
1728194e1c80Spooka 
1729194e1c80Spooka 		if (unit == STAR)
1730194e1c80Spooka 			snprintf(buf, sizeof(buf), "%s*", fakename);
1731194e1c80Spooka 		else
1732194e1c80Spooka 			snprintf(buf, sizeof(buf), "%s%d", fakename, unit);
1733194e1c80Spooka 		name = buf;
1734194e1c80Spooka 	}
1735194e1c80Spooka 
1736194e1c80Spooka 	/* ok, everything should be set up, so instantiate a fake device */
173790ac64deSpooka 	i = getdevi(name);
173890ac64deSpooka 	if (i == NULL)
1739*d25ad65eSchristos 		panic("%s: device `%s' expected to be present", __func__,
1740*d25ad65eSchristos 		    name);
174190ac64deSpooka 	ib = i->i_base;
1742194e1c80Spooka 	iba = ib->d_ahead;
174390ac64deSpooka 
174490ac64deSpooka 	i->i_atdeva = iba;
174590ac64deSpooka 	i->i_cfflags = 0;
174690ac64deSpooka 	i->i_locs = fixloc(name, &errattr, NULL);
174790ac64deSpooka 	i->i_pseudoroot = 1;
174890ac64deSpooka 	i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */
174990ac64deSpooka 
175090ac64deSpooka 	*iba->d_ipp = i;
175190ac64deSpooka 	iba->d_ipp = &i->i_asame;
175290ac64deSpooka 
175390ac64deSpooka 	ht_insert(devroottab, ib->d_name, ib);
175490ac64deSpooka }
175590ac64deSpooka 
175611a68adeSchristos static void
175711a68adeSchristos deldevbase(struct devbase *d)
175811a68adeSchristos {
175911a68adeSchristos 	struct devi *i;
176011a68adeSchristos 	const char *name = d->d_name;
176111a68adeSchristos 
176211a68adeSchristos 	if (!d->d_ispseudo) {
176311a68adeSchristos 		devcleanup(makedevstack(d));
176411a68adeSchristos 		return;
176511a68adeSchristos 	}
176611a68adeSchristos 
176711a68adeSchristos 	if ((i = ht_lookup(devitab, name)) == NULL)
176811a68adeSchristos 		return;
176911a68adeSchristos 
177011a68adeSchristos 	d->d_umax = 0;		/* clear neads-count entries */
177111a68adeSchristos 	d->d_ihead = NULL;	/* make sure it won't be considered active */
177211a68adeSchristos 	TAILQ_REMOVE(&allpseudo, i, i_next);
177311a68adeSchristos 	if (ht_remove(devitab, name))
1774*d25ad65eSchristos 		panic("%s: Can't remove %s from devitab", __func__, name);
177511a68adeSchristos 	if (ht_insert(deaddevitab, name, i))
1776*d25ad65eSchristos 		panic("%s: Can't add %s to deaddevitab", __func__, name);
177711a68adeSchristos }
177811a68adeSchristos 
177990ac64deSpooka void
17805ecc953bSthorpej addpseudo(const char *name, int number)
17815ecc953bSthorpej {
17825ecc953bSthorpej 	struct devbase *d;
17835ecc953bSthorpej 	struct devi *i;
17845ecc953bSthorpej 
17855ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
17865ecc953bSthorpej 	if (d == NULL) {
1787c7295a4cSchristos 		cfgerror("undefined pseudo-device %s", name);
17885ecc953bSthorpej 		return;
17895ecc953bSthorpej 	}
17905ecc953bSthorpej 	if (!d->d_ispseudo) {
1791c7295a4cSchristos 		cfgerror("%s is a real device, not a pseudo-device", name);
17925ecc953bSthorpej 		return;
17935ecc953bSthorpej 	}
17945ecc953bSthorpej 	if (ht_lookup(devitab, name) != NULL) {
1795c7295a4cSchristos 		cfgerror("`%s' already defined", name);
17965ecc953bSthorpej 		return;
17975ecc953bSthorpej 	}
17985ecc953bSthorpej 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
17995ecc953bSthorpej 	if (ht_insert(devitab, name, i))
1800*d25ad65eSchristos 		panic("%s: %s", __func__, name);
18017b7c582aScube 	/* Useful to retrieve the instance from the devbase */
18027b7c582aScube 	d->d_ihead = i;
18037aa6070dScube 	i->i_active = DEVI_ACTIVE;
18045ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
18055ecc953bSthorpej }
18065ecc953bSthorpej 
18075ecc953bSthorpej void
18083dbd1573Schristos delpseudo(const char *name, int nowarn)
18095ecc953bSthorpej {
18105ecc953bSthorpej 	struct devbase *d;
18115ecc953bSthorpej 
181275eb07f2Suebayasi 	CFGDBG(5, "deselecting pseudo `%s'", name);
18135ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
18145ecc953bSthorpej 	if (d == NULL) {
18153dbd1573Schristos 		if (!nowarn)
1816c7295a4cSchristos 			cfgerror("undefined pseudo-device %s", name);
18175ecc953bSthorpej 		return;
18185ecc953bSthorpej 	}
18195ecc953bSthorpej 	if (!d->d_ispseudo) {
1820c7295a4cSchristos 		cfgerror("%s is a real device, not a pseudo-device", name);
18215ecc953bSthorpej 		return;
18225ecc953bSthorpej 	}
182311a68adeSchristos 	deldevbase(d);
18245ecc953bSthorpej }
18255ecc953bSthorpej 
18265ecc953bSthorpej void
1827d767912bSdrochner adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
18289483bda7Sdholland 	struct condexpr *cond, struct nvlist *nv_nodes)
18295ecc953bSthorpej {
18305ecc953bSthorpej 	struct devm *dm;
18315ecc953bSthorpej 
1832d767912bSdrochner 	if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
1833c7295a4cSchristos 		cfgerror("character major %d is invalid", cmajor);
18349483bda7Sdholland 		condexpr_destroy(cond);
18353da3ab25Spooka 		nvfreel(nv_nodes);
18365ecc953bSthorpej 		return;
18375ecc953bSthorpej 	}
18385ecc953bSthorpej 
1839d767912bSdrochner 	if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
1840c7295a4cSchristos 		cfgerror("block major %d is invalid", bmajor);
18419483bda7Sdholland 		condexpr_destroy(cond);
18423da3ab25Spooka 		nvfreel(nv_nodes);
18435ecc953bSthorpej 		return;
18445ecc953bSthorpej 	}
1845d767912bSdrochner 	if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
1846c7295a4cSchristos 		cfgerror("both character/block majors are not specified");
18479483bda7Sdholland 		condexpr_destroy(cond);
18483da3ab25Spooka 		nvfreel(nv_nodes);
18495ecc953bSthorpej 		return;
18505ecc953bSthorpej 	}
18515ecc953bSthorpej 
18525ecc953bSthorpej 	dm = ecalloc(1, sizeof(*dm));
18535ecc953bSthorpej 	dm->dm_srcfile = yyfile;
18545ecc953bSthorpej 	dm->dm_srcline = currentline();
18555ecc953bSthorpej 	dm->dm_name = name;
18565ecc953bSthorpej 	dm->dm_cmajor = cmajor;
18575ecc953bSthorpej 	dm->dm_bmajor = bmajor;
18589483bda7Sdholland 	dm->dm_opts = cond;
18593da3ab25Spooka 	dm->dm_devnodes = nv_nodes;
18605ecc953bSthorpej 
18615ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
18625ecc953bSthorpej 
18635ecc953bSthorpej 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
18645ecc953bSthorpej 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
18655ecc953bSthorpej }
18665ecc953bSthorpej 
18677aa6070dScube int
18685ecc953bSthorpej fixdevis(void)
18695ecc953bSthorpej {
1870*d25ad65eSchristos 	const char *msg;
18715ecc953bSthorpej 	struct devi *i;
1872511fbc70Schristos 	struct pspec *p;
18737aa6070dScube 	int error = 0;
18745ecc953bSthorpej 
1875bc707469Suebayasi 	TAILQ_FOREACH(i, &alldevi, i_next) {
1876bc707469Suebayasi 		CFGDBG(3, "fixing devis `%s'", i->i_name);
18777aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
18785ecc953bSthorpej 			selectbase(i->i_base, i->i_atdeva);
18797aa6070dScube 		else if (i->i_active == DEVI_ORPHAN) {
1880c130d400Scube 			/*
1881c130d400Scube 			 * At this point, we can't have instances for which
1882c130d400Scube 			 * i_at or i_pspec are NULL.
1883c130d400Scube 			 */
18847aa6070dScube 			++error;
1885511fbc70Schristos 			p = i->i_pspec;
1886*d25ad65eSchristos 			msg = p == NULL ? "no parent" :
1887*d25ad65eSchristos 			    (p->p_atunit == WILD ? "nothing matching" : "no");
1888c7295a4cSchristos 			cfgxerror(i->i_srcfile, i->i_lineno,
18890dbd1c0eScube 			    "`%s at %s' is orphaned (%s `%s' found)",
1890*d25ad65eSchristos 			    i->i_name, i->i_at, msg, i->i_at);
18917aa6070dScube 		} else if (vflag && i->i_active == DEVI_IGNORED)
1892c7295a4cSchristos 			cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
1893c7295a4cSchristos 			    "explicitly orphaned instance `%s at %s'",
1894c7295a4cSchristos 			    i->i_name, i->i_at);
1895bc707469Suebayasi 	}
18967aa6070dScube 
18977aa6070dScube 	if (error)
18987aa6070dScube 		return error;
18995ecc953bSthorpej 
19005ecc953bSthorpej 	TAILQ_FOREACH(i, &allpseudo, i_next)
19017aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
19025ecc953bSthorpej 			selectbase(i->i_base, NULL);
19037aa6070dScube 	return 0;
19045ecc953bSthorpej }
19055ecc953bSthorpej 
19065ecc953bSthorpej /*
19075ecc953bSthorpej  * Look up a parent spec, creating a new one if it does not exist.
19085ecc953bSthorpej  */
19095ecc953bSthorpej static struct pspec *
191053be83e0Schristos getpspec(struct attr *attr, struct devbase *ab, int atunit, struct deva *da)
19115ecc953bSthorpej {
19125ecc953bSthorpej 	struct pspec *p;
191353be83e0Schristos 	int inst = npspecs;
19145ecc953bSthorpej 
19155ecc953bSthorpej 	TAILQ_FOREACH(p, &allpspecs, p_list) {
191653be83e0Schristos 		if (p->p_iattr == attr && p->p_atdev == ab &&
191753be83e0Schristos 		    p->p_atunit == atunit) {
191853be83e0Schristos 			if (p->p_deva == da)
19195ecc953bSthorpej 				return (p);
192053be83e0Schristos 		   	inst = p->p_inst;
19215ecc953bSthorpej 		}
192253be83e0Schristos 	}
19235ecc953bSthorpej 
19245ecc953bSthorpej 	p = ecalloc(1, sizeof(*p));
19255ecc953bSthorpej 
19265ecc953bSthorpej 	p->p_iattr = attr;
19275ecc953bSthorpej 	p->p_atdev = ab;
19285ecc953bSthorpej 	p->p_atunit = atunit;
192953be83e0Schristos 	p->p_inst = inst;
193053be83e0Schristos 	if (inst == npspecs)
193153be83e0Schristos 		npspecs++;
193253be83e0Schristos 	p->p_deva = da;
1933c130d400Scube 	p->p_active = 0;
19345ecc953bSthorpej 
19355ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
19365ecc953bSthorpej 
19375ecc953bSthorpej 	return (p);
19385ecc953bSthorpej }
19395ecc953bSthorpej 
19405ecc953bSthorpej /*
19415ecc953bSthorpej  * Define a new instance of a specific device.
19425ecc953bSthorpej  */
19435ecc953bSthorpej static struct devi *
19445ecc953bSthorpej getdevi(const char *name)
19455ecc953bSthorpej {
19465ecc953bSthorpej 	struct devi *i, *firsti;
19475ecc953bSthorpej 	struct devbase *d;
19485ecc953bSthorpej 	int unit;
19495ecc953bSthorpej 	char base[NAMESIZE];
19505ecc953bSthorpej 
19515ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1952c7295a4cSchristos 		cfgerror("invalid device name `%s'", name);
19535ecc953bSthorpej 		return (NULL);
19545ecc953bSthorpej 	}
19555ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
19565ecc953bSthorpej 	if (d == NULL) {
1957c7295a4cSchristos 		cfgerror("%s: unknown device `%s'", name, base);
19585ecc953bSthorpej 		return (NULL);
19595ecc953bSthorpej 	}
19605ecc953bSthorpej 	if (d->d_ispseudo) {
1961c7295a4cSchristos 		cfgerror("%s: %s is a pseudo-device", name, base);
19625ecc953bSthorpej 		return (NULL);
19635ecc953bSthorpej 	}
19645ecc953bSthorpej 	firsti = ht_lookup(devitab, name);
19655ecc953bSthorpej 	i = newdevi(name, unit, d);
19665ecc953bSthorpej 	if (firsti == NULL) {
19675ecc953bSthorpej 		if (ht_insert(devitab, name, i))
1968*d25ad65eSchristos 			panic("%s: %s", __func__, name);
19695ecc953bSthorpej 		*d->d_ipp = i;
19705ecc953bSthorpej 		d->d_ipp = &i->i_bsame;
19715ecc953bSthorpej 	} else {
19725ecc953bSthorpej 		while (firsti->i_alias)
19735ecc953bSthorpej 			firsti = firsti->i_alias;
19745ecc953bSthorpej 		firsti->i_alias = i;
19755ecc953bSthorpej 	}
19765ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
19775ecc953bSthorpej 	ndevi++;
19785ecc953bSthorpej 	return (i);
19795ecc953bSthorpej }
19805ecc953bSthorpej 
19815ecc953bSthorpej static const char *
19825ecc953bSthorpej concat(const char *name, int c)
19835ecc953bSthorpej {
1984c7295a4cSchristos 	size_t len;
19855ecc953bSthorpej 	char buf[NAMESIZE];
19865ecc953bSthorpej 
19875ecc953bSthorpej 	len = strlen(name);
19885ecc953bSthorpej 	if (len + 2 > sizeof(buf)) {
1989c7295a4cSchristos 		cfgerror("device name `%s%c' too long", name, c);
19905ecc953bSthorpej 		len = sizeof(buf) - 2;
19915ecc953bSthorpej 	}
19925ecc953bSthorpej 	memmove(buf, name, len);
19939a7f4bbaSchristos 	buf[len] = (char)c;
19949a7f4bbaSchristos 	buf[len + 1] = '\0';
19955ecc953bSthorpej 	return (intern(buf));
19965ecc953bSthorpej }
19975ecc953bSthorpej 
19985ecc953bSthorpej const char *
19995ecc953bSthorpej starref(const char *name)
20005ecc953bSthorpej {
20015ecc953bSthorpej 
20025ecc953bSthorpej 	return (concat(name, '*'));
20035ecc953bSthorpej }
20045ecc953bSthorpej 
20055ecc953bSthorpej const char *
20065ecc953bSthorpej wildref(const char *name)
20075ecc953bSthorpej {
20085ecc953bSthorpej 
20095ecc953bSthorpej 	return (concat(name, '?'));
20105ecc953bSthorpej }
20115ecc953bSthorpej 
20125ecc953bSthorpej /*
20135ecc953bSthorpej  * Split a name like "foo0" into base name (foo) and unit number (0).
20145ecc953bSthorpej  * Return 0 on success.  To make this useful for names like "foo0a",
20155ecc953bSthorpej  * the length of the "foo0" part is one of the arguments.
20165ecc953bSthorpej  */
20175ecc953bSthorpej static int
20185ecc953bSthorpej split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
20195ecc953bSthorpej {
20205ecc953bSthorpej 	const char *cp;
2021c7295a4cSchristos 	int c;
2022c7295a4cSchristos 	size_t l;
20235ecc953bSthorpej 
20245ecc953bSthorpej 	l = nlen;
20255ecc953bSthorpej 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
20265ecc953bSthorpej 		return (1);
20275ecc953bSthorpej 	c = (u_char)name[--l];
20285ecc953bSthorpej 	if (!isdigit(c)) {
20295ecc953bSthorpej 		if (c == '*')
20305ecc953bSthorpej 			*aunit = STAR;
20315ecc953bSthorpej 		else if (c == '?')
20325ecc953bSthorpej 			*aunit = WILD;
20335ecc953bSthorpej 		else
20345ecc953bSthorpej 			return (1);
20355ecc953bSthorpej 	} else {
20365ecc953bSthorpej 		cp = &name[l];
20375ecc953bSthorpej 		while (isdigit((unsigned char)cp[-1]))
20385ecc953bSthorpej 			l--, cp--;
20395ecc953bSthorpej 		*aunit = atoi(cp);
20405ecc953bSthorpej 	}
20415ecc953bSthorpej 	memmove(base, name, l);
20425ecc953bSthorpej 	base[l] = 0;
20435ecc953bSthorpej 	return (0);
20445ecc953bSthorpej }
20455ecc953bSthorpej 
20465ecc953bSthorpej void
20478d928a75Suebayasi addattr(const char *name)
20488d928a75Suebayasi {
20498d928a75Suebayasi 	struct attr *a;
20508d928a75Suebayasi 
20518d928a75Suebayasi 	a = refattr(name);
20528d928a75Suebayasi 	selectattr(a);
20538d928a75Suebayasi }
20548d928a75Suebayasi 
20558d928a75Suebayasi void
20563dbd1573Schristos delattr(const char *name, int nowarn)
20578d928a75Suebayasi {
20588d928a75Suebayasi 	struct attr *a;
20598d928a75Suebayasi 
20608d928a75Suebayasi 	a = refattr(name);
20618d928a75Suebayasi 	deselectattr(a);
20628d928a75Suebayasi }
20638d928a75Suebayasi 
20648d928a75Suebayasi void
20655ecc953bSthorpej selectattr(struct attr *a)
20665ecc953bSthorpej {
20673a03bee6Suebayasi 	struct attrlist *al;
20683a03bee6Suebayasi 	struct attr *dep;
20695ecc953bSthorpej 
20708d928a75Suebayasi 	CFGDBG(5, "selecting attr `%s'", a->a_name);
20713a03bee6Suebayasi 	for (al = a->a_deps; al != NULL; al = al->al_next) {
20723a03bee6Suebayasi 		dep = al->al_this;
20733a03bee6Suebayasi 		selectattr(dep);
20743a03bee6Suebayasi 	}
2075d6687201Suebayasi 	if (ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)) == 0)
2076d6687201Suebayasi 		nattrs++;
2077b599bf99Suebayasi 	CFGDBG(3, "attr selected `%s'", a->a_name);
20785ecc953bSthorpej }
20795ecc953bSthorpej 
20806d1bba4aSuebayasi static int
20818d928a75Suebayasi deselectattrcb2(const char *name1, const char *name2, void *v, void *arg)
20828d928a75Suebayasi {
208311a68adeSchristos 	struct attr *a = arg;
208411a68adeSchristos 	const char *name = a->a_name;
208511a68adeSchristos 	struct vtype *vt = v;
20868d928a75Suebayasi 
208711a68adeSchristos 	if (strcmp(name, name2) == 0) {
20883dbd1573Schristos 		delattr(name1, 0);
20898d928a75Suebayasi 		return 0;
20908d928a75Suebayasi 	}
20918d928a75Suebayasi 
209211a68adeSchristos 	if (!vt->attr->a_deselected)
209311a68adeSchristos 		return 0;
209411a68adeSchristos 
209511a68adeSchristos 	switch (vt->type) {
209611a68adeSchristos 	case V_ATTRIBUTE:
209711a68adeSchristos #ifdef notyet
209811a68adeSchristos 		// XXX: Loops
209911a68adeSchristos 		deselectattr(vt->value);
210011a68adeSchristos #endif
210111a68adeSchristos 		break;
210211a68adeSchristos 	case V_DEVICE:
210311a68adeSchristos 		CFGDBG(5, "removing device `%s' with attr `%s' because attr `%s'"
210411a68adeSchristos 		    " is deselected", name1, name2, name);
210511a68adeSchristos 		deldevbase(vt->value);
210611a68adeSchristos 		break;
210711a68adeSchristos 	default:
210811a68adeSchristos 		abort();
210911a68adeSchristos 	}
211011a68adeSchristos 	return 0;
211111a68adeSchristos }
211211a68adeSchristos 
21138d928a75Suebayasi void
21148d928a75Suebayasi deselectattr(struct attr *a)
21158d928a75Suebayasi {
21168d928a75Suebayasi 	CFGDBG(5, "deselecting attr `%s'", a->a_name);
211711a68adeSchristos 	a->a_deselected = 1;
211811a68adeSchristos 	ht_enumerate2(attrdeptab, deselectattrcb2, a);
2119d6687201Suebayasi 	if (ht_remove(selecttab, a->a_name) == 0)
2120d6687201Suebayasi 		nattrs--;
21218d928a75Suebayasi 	CFGDBG(3, "attr deselected `%s'", a->a_name);
21228d928a75Suebayasi }
21238d928a75Suebayasi 
21248d928a75Suebayasi static int
21256d1bba4aSuebayasi dumpattrdepcb2(const char *name1, const char *name2, void *v, void *arg)
21266d1bba4aSuebayasi {
21276d1bba4aSuebayasi 
21286d1bba4aSuebayasi 	CFGDBG(3, "attr `%s' depends on attr `%s'", name1, name2);
21296d1bba4aSuebayasi 	return 0;
21306d1bba4aSuebayasi }
21316d1bba4aSuebayasi 
21326d1bba4aSuebayasi void
21336d1bba4aSuebayasi dependattrs(void)
21346d1bba4aSuebayasi {
21356d1bba4aSuebayasi 
21366d1bba4aSuebayasi 	ht_enumerate2(attrdeptab, dumpattrdepcb2, NULL);
21376d1bba4aSuebayasi }
21386d1bba4aSuebayasi 
21395ecc953bSthorpej /*
21405ecc953bSthorpej  * We have an instance of the base foo, so select it and all its
21415ecc953bSthorpej  * attributes for "optional foo".
21425ecc953bSthorpej  */
21435ecc953bSthorpej static void
21445ecc953bSthorpej selectbase(struct devbase *d, struct deva *da)
21455ecc953bSthorpej {
21465ecc953bSthorpej 	struct attr *a;
21474caf067bSdholland 	struct attrlist *al;
21485ecc953bSthorpej 
2149c7295a4cSchristos 	(void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
2150b599bf99Suebayasi 	CFGDBG(3, "devbase selected `%s'", d->d_name);
21516d1bba4aSuebayasi 	CFGDBG(5, "selecting dependencies of devbase `%s'", d->d_name);
21524caf067bSdholland 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
21534caf067bSdholland 		a = al->al_this;
21545ecc953bSthorpej 		expandattr(a, selectattr);
21555ecc953bSthorpej 	}
2156da706167Suebayasi 
2157da706167Suebayasi 	struct attr *devattr;
2158da706167Suebayasi 	devattr = refattr(d->d_name);
2159da706167Suebayasi 	expandattr(devattr, selectattr);
2160da706167Suebayasi 
21615ecc953bSthorpej 	if (da != NULL) {
2162c7295a4cSchristos 		(void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
2163b599bf99Suebayasi 		CFGDBG(3, "devattr selected `%s'", da->d_name);
21644caf067bSdholland 		for (al = da->d_attrs; al != NULL; al = al->al_next) {
21654caf067bSdholland 			a = al->al_this;
21665ecc953bSthorpej 			expandattr(a, selectattr);
21675ecc953bSthorpej 		}
21685ecc953bSthorpej 	}
2169c4bac1d0Suebayasi 
2170c4bac1d0Suebayasi 	fixdev(d);
21715ecc953bSthorpej }
21725ecc953bSthorpej 
21735ecc953bSthorpej /*
21745ecc953bSthorpej  * Is the given pointer on the given list of pointers?
21755ecc953bSthorpej  */
217659c94545Scube int
21775ecc953bSthorpej onlist(struct nvlist *nv, void *ptr)
21785ecc953bSthorpej {
21795ecc953bSthorpej 	for (; nv != NULL; nv = nv->nv_next)
21805ecc953bSthorpej 		if (nv->nv_ptr == ptr)
21815ecc953bSthorpej 			return (1);
21825ecc953bSthorpej 	return (0);
21835ecc953bSthorpej }
21845ecc953bSthorpej 
21855ecc953bSthorpej static char *
21865ecc953bSthorpej extend(char *p, const char *name)
21875ecc953bSthorpej {
2188c7295a4cSchristos 	size_t l;
21895ecc953bSthorpej 
21905ecc953bSthorpej 	l = strlen(name);
21915ecc953bSthorpej 	memmove(p, name, l);
21925ecc953bSthorpej 	p += l;
21935ecc953bSthorpej 	*p++ = ',';
21945ecc953bSthorpej 	*p++ = ' ';
21955ecc953bSthorpej 	return (p);
21965ecc953bSthorpej }
21975ecc953bSthorpej 
21985ecc953bSthorpej /*
21995ecc953bSthorpej  * Check that we got all required locators, and default any that are
22005ecc953bSthorpej  * given as "?" and have defaults.  Return 0 on success.
22015ecc953bSthorpej  */
22025ecc953bSthorpej static const char **
220316a8771bSdholland fixloc(const char *name, struct attr *attr, struct loclist *got)
22045ecc953bSthorpej {
220516a8771bSdholland 	struct loclist *m, *n;
22065ecc953bSthorpej 	int ord;
22075ecc953bSthorpej 	const char **lp;
22085ecc953bSthorpej 	int nmissing, nextra, nnodefault;
22095ecc953bSthorpej 	char *mp, *ep, *ndp;
22105ecc953bSthorpej 	char missing[1000], extra[1000], nodefault[1000];
22115ecc953bSthorpej 	static const char *nullvec[1];
22125ecc953bSthorpej 
22135ecc953bSthorpej 	/*
22145ecc953bSthorpej 	 * Look for all required locators, and number the given ones
22155ecc953bSthorpej 	 * according to the required order.  While we are numbering,
22165ecc953bSthorpej 	 * set default values for defaulted locators.
22175ecc953bSthorpej 	 */
22185ecc953bSthorpej 	if (attr->a_loclen == 0)	/* e.g., "at root" */
22195ecc953bSthorpej 		lp = nullvec;
22205ecc953bSthorpej 	else
22219a7f4bbaSchristos 		lp = emalloc((size_t)(attr->a_loclen + 1) * sizeof(const char *));
222216a8771bSdholland 	for (n = got; n != NULL; n = n->ll_next)
222316a8771bSdholland 		n->ll_num = -1;
22245ecc953bSthorpej 	nmissing = 0;
22255ecc953bSthorpej 	mp = missing;
22265ecc953bSthorpej 	/* yes, this is O(mn), but m and n should be small */
222716a8771bSdholland 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) {
222816a8771bSdholland 		for (n = got; n != NULL; n = n->ll_next) {
222916a8771bSdholland 			if (n->ll_name == m->ll_name) {
223016a8771bSdholland 				n->ll_num = ord;
22315ecc953bSthorpej 				break;
22325ecc953bSthorpej 			}
22335ecc953bSthorpej 		}
223416a8771bSdholland 		if (n == NULL && m->ll_num == 0) {
22355ecc953bSthorpej 			nmissing++;
223616a8771bSdholland 			mp = extend(mp, m->ll_name);
22375ecc953bSthorpej 		}
223816a8771bSdholland 		lp[ord] = m->ll_string;
22395ecc953bSthorpej 	}
22405ecc953bSthorpej 	if (ord != attr->a_loclen)
2241*d25ad65eSchristos 		panic("%s: bad length", __func__);
22425ecc953bSthorpej 	lp[ord] = NULL;
22435ecc953bSthorpej 	nextra = 0;
22445ecc953bSthorpej 	ep = extra;
22455ecc953bSthorpej 	nnodefault = 0;
22465ecc953bSthorpej 	ndp = nodefault;
224716a8771bSdholland 	for (n = got; n != NULL; n = n->ll_next) {
224816a8771bSdholland 		if (n->ll_num >= 0) {
224916a8771bSdholland 			if (n->ll_string != NULL)
225016a8771bSdholland 				lp[n->ll_num] = n->ll_string;
225116a8771bSdholland 			else if (lp[n->ll_num] == NULL) {
22525ecc953bSthorpej 				nnodefault++;
225316a8771bSdholland 				ndp = extend(ndp, n->ll_name);
22545ecc953bSthorpej 			}
22555ecc953bSthorpej 		} else {
22565ecc953bSthorpej 			nextra++;
225716a8771bSdholland 			ep = extend(ep, n->ll_name);
22585ecc953bSthorpej 		}
22595ecc953bSthorpej 	}
22605ecc953bSthorpej 	if (nextra) {
22615ecc953bSthorpej 		ep[-2] = 0;	/* kill ", " */
2262c7295a4cSchristos 		cfgerror("%s: extraneous locator%s: %s",
22635ecc953bSthorpej 		    name, nextra > 1 ? "s" : "", extra);
22645ecc953bSthorpej 	}
22655ecc953bSthorpej 	if (nmissing) {
22665ecc953bSthorpej 		mp[-2] = 0;
2267c7295a4cSchristos 		cfgerror("%s: must specify %s", name, missing);
22685ecc953bSthorpej 	}
22695ecc953bSthorpej 	if (nnodefault) {
22705ecc953bSthorpej 		ndp[-2] = 0;
2271c7295a4cSchristos 		cfgerror("%s: cannot wildcard %s", name, nodefault);
22725ecc953bSthorpej 	}
22735ecc953bSthorpej 	if (nmissing || nnodefault) {
22745ecc953bSthorpej 		free(lp);
22755ecc953bSthorpej 		lp = NULL;
22765ecc953bSthorpej 	}
22775ecc953bSthorpej 	return (lp);
22785ecc953bSthorpej }
2279437f8925Scube 
2280437f8925Scube void
2281437f8925Scube setversion(int newver)
2282437f8925Scube {
2283437f8925Scube 	if (newver > CONFIG_VERSION)
2284c7295a4cSchristos 		cfgerror("your sources require a newer version of config(1) "
2285437f8925Scube 		    "-- please rebuild it.");
2286437f8925Scube 	else if (newver < CONFIG_MINVERSION)
2287c7295a4cSchristos 		cfgerror("your sources are out of date -- please update.");
2288437f8925Scube 	else
2289437f8925Scube 		version = newver;
2290437f8925Scube }
2291