xref: /netbsd/usr.bin/config/sem.c (revision 334baaa8)
1*334baaa8Srillig /*	$NetBSD: sem.c,v 1.86 2021/08/25 23:07:34 rillig 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*334baaa8Srillig __RCSID("$NetBSD: sem.c,v 1.86 2021/08/25 23:07:34 rillig 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);
824b9e5cb1Schristos static struct pspec *getpspec(struct attr *, struct devbase *, int, int);
835ecc953bSthorpej static struct devi *newdevi(const char *, int, struct devbase *d);
845ecc953bSthorpej static struct devi *getdevi(const char *);
85c130d400Scube static void remove_devi(struct devi *);
865ecc953bSthorpej static const char *concat(const char *, int);
875ecc953bSthorpej static char *extend(char *, const char *);
885ecc953bSthorpej static int split(const char *, size_t, char *, size_t, int *);
895ecc953bSthorpej static void selectbase(struct devbase *, struct deva *);
9016a8771bSdholland static const char **fixloc(const char *, struct attr *, struct loclist *);
91d767912bSdrochner static const char *makedevstr(devmajor_t, devminor_t);
92d767912bSdrochner static const char *major2name(devmajor_t);
93d767912bSdrochner static devmajor_t dev2major(struct devbase *);
945ecc953bSthorpej 
955ecc953bSthorpej extern const char *yyfile;
967aa6070dScube extern int vflag;
975ecc953bSthorpej 
9811a68adeSchristos #define V_ATTRIBUTE	0
9911a68adeSchristos #define V_DEVICE	1
10011a68adeSchristos struct vtype {
10111a68adeSchristos 	int type;
10211a68adeSchristos 	struct attr *attr;
10311a68adeSchristos 	void *value;
10411a68adeSchristos };
10511a68adeSchristos 
10611a68adeSchristos static struct nvlist *
makedevstack(struct devbase * d)10711a68adeSchristos makedevstack(struct devbase *d)
10811a68adeSchristos {
10911a68adeSchristos 	struct devi *firsti, *i;
11011a68adeSchristos 	struct nvlist *stack = NULL;
11111a68adeSchristos 
11211a68adeSchristos 	for (firsti = d->d_ihead; firsti != NULL; firsti = firsti->i_bsame)
11311a68adeSchristos 		for (i = firsti; i != NULL; i = i->i_alias)
11411a68adeSchristos 			stack = newnv(NULL, NULL, i, 0, stack);
11511a68adeSchristos 	return stack;
11611a68adeSchristos }
11711a68adeSchristos 
11811a68adeSchristos static void
devcleanup(struct nvlist * stack)11911a68adeSchristos devcleanup(struct nvlist *stack)
12011a68adeSchristos {
12111a68adeSchristos 	struct nvlist *nv;
12211a68adeSchristos 	for (nv = stack; nv != NULL; nv = nv->nv_next)
12311a68adeSchristos 		remove_devi(nv->nv_ptr);
12411a68adeSchristos 	nvfreel(stack);
12511a68adeSchristos }
12611a68adeSchristos 
12711a68adeSchristos static void *
addvalue(int type,struct attr * a,void * value)12811a68adeSchristos addvalue(int type, struct attr *a, void *value)
12911a68adeSchristos {
13011a68adeSchristos 	struct vtype *vt = emalloc(sizeof(*vt));
13111a68adeSchristos 	vt->type = type;
13211a68adeSchristos 	vt->attr = a;
13311a68adeSchristos 	vt->value = value;
13411a68adeSchristos 	return vt;
13511a68adeSchristos }
13611a68adeSchristos 
1375ecc953bSthorpej void
initsem(void)1385ecc953bSthorpej initsem(void)
1395ecc953bSthorpej {
1405ecc953bSthorpej 
1415ecc953bSthorpej 	attrtab = ht_new();
1426d1bba4aSuebayasi 	attrdeptab = ht_new();
1437c7d91e6Suebayasi 
1447c7d91e6Suebayasi 	allattr.a_name = "netbsd";
1457c7d91e6Suebayasi 	TAILQ_INIT(&allattr.a_files);
1467c7d91e6Suebayasi 	(void)ht_insert(attrtab, allattr.a_name, &allattr);
147cd06fea3Suebayasi 	selectattr(&allattr);
1487c7d91e6Suebayasi 
1495ecc953bSthorpej 	errattr.a_name = "<internal>";
1505ecc953bSthorpej 
1515ecc953bSthorpej 	TAILQ_INIT(&allbases);
1525ecc953bSthorpej 
1535ecc953bSthorpej 	TAILQ_INIT(&alldevas);
1545ecc953bSthorpej 
1555ecc953bSthorpej 	TAILQ_INIT(&allpspecs);
1565ecc953bSthorpej 
1575ecc953bSthorpej 	cfhashtab = ht_new();
1585ecc953bSthorpej 	TAILQ_INIT(&allcf);
1595ecc953bSthorpej 
1605ecc953bSthorpej 	TAILQ_INIT(&alldevi);
1615ecc953bSthorpej 	errdev.d_name = "<internal>";
1625ecc953bSthorpej 
1635ecc953bSthorpej 	TAILQ_INIT(&allpseudo);
1645ecc953bSthorpej 
1655ecc953bSthorpej 	TAILQ_INIT(&alldevms);
1665ecc953bSthorpej 
1675ecc953bSthorpej 	s_ifnet = intern("ifnet");
1685ecc953bSthorpej 	s_qmark = intern("?");
1695ecc953bSthorpej 	s_none = intern("none");
1705ecc953bSthorpej }
1715ecc953bSthorpej 
1725ecc953bSthorpej /* Name of include file just ended (set in scan.l) */
1735ecc953bSthorpej extern const char *lastfile;
1745ecc953bSthorpej 
175a87832feSuebayasi static struct attr *
finddep(struct attr * a,const char * name)176da706167Suebayasi finddep(struct attr *a, const char *name)
177da706167Suebayasi {
178da706167Suebayasi 	struct attrlist *al;
179da706167Suebayasi 
180da706167Suebayasi 	for (al = a->a_deps; al != NULL; al = al->al_next) {
181da706167Suebayasi 		struct attr *this = al->al_this;
182da706167Suebayasi 		if (strcmp(this->a_name, name) == 0)
183da706167Suebayasi 			return this;
184da706167Suebayasi 	}
185da706167Suebayasi 	return NULL;
186da706167Suebayasi }
187da706167Suebayasi 
188da706167Suebayasi static void
mergedeps(const char * dname,const char * name)189c80681c9Suebayasi mergedeps(const char *dname, const char *name)
190da706167Suebayasi {
191da706167Suebayasi 	struct attr *a, *newa;
192da706167Suebayasi 
193c80681c9Suebayasi 	CFGDBG(4, "merging attr `%s' to devbase `%s'", name, dname);
194c80681c9Suebayasi 	a = refattr(dname);
195da706167Suebayasi 	if (finddep(a, name) == NULL) {
196da706167Suebayasi 		newa = refattr(name);
197da706167Suebayasi 		a->a_deps = attrlist_cons(a->a_deps, newa);
198da706167Suebayasi 		CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name,
199da706167Suebayasi 		    a->a_name);
200da706167Suebayasi 	}
201da706167Suebayasi }
202da706167Suebayasi 
203da706167Suebayasi static void
fixdev(struct devbase * dev)204da706167Suebayasi fixdev(struct devbase *dev)
205da706167Suebayasi {
206da706167Suebayasi 	struct attrlist *al;
207da706167Suebayasi 	struct attr *devattr, *a;
208da706167Suebayasi 
209da706167Suebayasi 	devattr = refattr(dev->d_name);
210da706167Suebayasi 	if (devattr->a_devclass)
211a87832feSuebayasi 		panic("%s: dev %s is devclass!", __func__, devattr->a_name);
212da706167Suebayasi 
213c4bac1d0Suebayasi 	CFGDBG(4, "fixing devbase `%s'", dev->d_name);
214da706167Suebayasi 	for (al = dev->d_attrs; al != NULL; al = al->al_next) {
215da706167Suebayasi 		a = al->al_this;
216d25ad65eSchristos 		CFGDBG(4, "fixing devbase `%s' attr `%s'", dev->d_name,
217d25ad65eSchristos 		    a->a_name);
218da706167Suebayasi 		if (a->a_iattr) {
219da706167Suebayasi 			a->a_refs = addtoattr(a->a_refs, dev);
220c4bac1d0Suebayasi 			CFGDBG(3, "device `%s' has iattr `%s'", dev->d_name,
221c4bac1d0Suebayasi 			    a->a_name);
222da706167Suebayasi 		} else if (a->a_devclass != NULL) {
223c5a6c986Suebayasi 			if (dev->d_classattr != NULL && dev->d_classattr != a) {
224da706167Suebayasi 				cfgwarn("device `%s' has multiple classes "
225da706167Suebayasi 				    "(`%s' and `%s')",
226da706167Suebayasi 				    dev->d_name, dev->d_classattr->a_name,
227da706167Suebayasi 				    a->a_name);
228da706167Suebayasi 			}
229c5a6c986Suebayasi 			if (dev->d_classattr == NULL) {
230da706167Suebayasi 				dev->d_classattr = a;
231d25ad65eSchristos 				CFGDBG(3, "device `%s' is devclass `%s'",
232d25ad65eSchristos 				    dev->d_name, a->a_name);
233c5a6c986Suebayasi 			}
234da706167Suebayasi 		} else {
235da706167Suebayasi 			if (strcmp(dev->d_name, a->a_name) != 0) {
236bc707469Suebayasi 				mergedeps(dev->d_name, a->a_name);
237da706167Suebayasi 			}
238da706167Suebayasi 		}
239da706167Suebayasi 	}
240da706167Suebayasi }
241da706167Suebayasi 
2425ecc953bSthorpej void
enddefs(void)2435ecc953bSthorpej enddefs(void)
2445ecc953bSthorpej {
2455ecc953bSthorpej 	struct devbase *dev;
2465ecc953bSthorpej 
247c4bac1d0Suebayasi 	yyfile = "enddefs";
248c4bac1d0Suebayasi 
2495ecc953bSthorpej 	TAILQ_FOREACH(dev, &allbases, d_next) {
2505ecc953bSthorpej 		if (!dev->d_isdef) {
2515ecc953bSthorpej 			(void)fprintf(stderr,
2525ecc953bSthorpej 			    "%s: device `%s' used but not defined\n",
2535ecc953bSthorpej 			    lastfile, dev->d_name);
2545ecc953bSthorpej 			errors++;
2555ecc953bSthorpej 			continue;
2565ecc953bSthorpej 		}
257bc707469Suebayasi 		fixdev(dev);
2585ecc953bSthorpej 	}
2595ecc953bSthorpej 	if (errors) {
2605ecc953bSthorpej 		(void)fprintf(stderr, "*** Stop.\n");
2615ecc953bSthorpej 		exit(1);
2625ecc953bSthorpej 	}
2635ecc953bSthorpej }
2645ecc953bSthorpej 
2655ecc953bSthorpej void
setdefmaxusers(int min,int def,int max)2665ecc953bSthorpej setdefmaxusers(int min, int def, int max)
2675ecc953bSthorpej {
2685ecc953bSthorpej 
2695ecc953bSthorpej 	if (min < 1 || min > def || def > max)
270c7295a4cSchristos 		cfgerror("maxusers must have 1 <= min (%d) <= default (%d) "
271c7295a4cSchristos 		    "<= max (%d)", min, def, max);
2725ecc953bSthorpej 	else {
2735ecc953bSthorpej 		minmaxusers = min;
2745ecc953bSthorpej 		defmaxusers = def;
2755ecc953bSthorpej 		maxmaxusers = max;
2765ecc953bSthorpej 	}
2775ecc953bSthorpej }
2785ecc953bSthorpej 
279f079f926Schristos static const char *maxusers_srcfile;
280f079f926Schristos static u_short maxusers_srcline;
281f079f926Schristos 
2825ecc953bSthorpej void
setmaxusers(int n)2835ecc953bSthorpej setmaxusers(int n)
2845ecc953bSthorpej {
2855ecc953bSthorpej 
286895e5687Scube 	if (maxusers == n) {
287f079f926Schristos 		cfgerror("duplicate maxusers parameter at %s:%hu",
288f079f926Schristos 		    maxusers_srcfile, maxusers_srcline);
2895ecc953bSthorpej 		return;
2905ecc953bSthorpej 	}
291895e5687Scube 	if (vflag && maxusers != 0)
292f079f926Schristos 		cfgwarn("warning: maxusers already defined at %s:%hu",
293f079f926Schristos 		    maxusers_srcfile, maxusers_srcline);
2945ecc953bSthorpej 	maxusers = n;
295f079f926Schristos 	maxusers_srcfile = yyfile;
296f079f926Schristos 	maxusers_srcline = currentline();
2975ecc953bSthorpej 	if (n < minmaxusers) {
298c7295a4cSchristos 		cfgerror("warning: minimum of %d maxusers assumed",
299c7295a4cSchristos 		    minmaxusers);
3005ecc953bSthorpej 		errors--;	/* take it away */
3015ecc953bSthorpej 		maxusers = minmaxusers;
3025ecc953bSthorpej 	} else if (n > maxmaxusers) {
303c7295a4cSchristos 		cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers);
3045ecc953bSthorpej 		errors--;
3055ecc953bSthorpej 	}
3065ecc953bSthorpej }
3075ecc953bSthorpej 
3085ecc953bSthorpej void
setident(const char * i)3095ecc953bSthorpej setident(const char *i)
3105ecc953bSthorpej {
3115ecc953bSthorpej 
3124a7be1f9Smrg 	if (i)
3135ecc953bSthorpej 		ident = intern(i);
3144a7be1f9Smrg 	else
3154a7be1f9Smrg 		ident = NULL;
3165ecc953bSthorpej }
3175ecc953bSthorpej 
3185ecc953bSthorpej /*
3195ecc953bSthorpej  * Define an attribute, optionally with an interface (a locator list)
3205ecc953bSthorpej  * and a set of attribute-dependencies.
3215ecc953bSthorpej  *
3225ecc953bSthorpej  * Attribute dependencies MAY NOT be interface attributes.
3235ecc953bSthorpej  *
3245ecc953bSthorpej  * Since an empty locator list is logically different from "no interface",
3255ecc953bSthorpej  * all locator lists include a dummy head node, which we discard here.
3265ecc953bSthorpej  */
3275ecc953bSthorpej int
defattr0(const char * name,struct loclist * locs,struct attrlist * deps,int devclass)328bb4307a6Suebayasi defattr0(const char *name, struct loclist *locs, struct attrlist *deps,
329bb4307a6Suebayasi     int devclass)
330bb4307a6Suebayasi {
331bb4307a6Suebayasi 
332bb4307a6Suebayasi 	if (locs != NULL)
333bb4307a6Suebayasi 		return defiattr(name, locs, deps, devclass);
334bb4307a6Suebayasi 	else if (devclass)
335bb4307a6Suebayasi 		return defdevclass(name, locs, deps, devclass);
336bb4307a6Suebayasi 	else
337bb4307a6Suebayasi 		return defattr(name, locs, deps, devclass);
338bb4307a6Suebayasi }
339bb4307a6Suebayasi 
340bb4307a6Suebayasi int
defattr(const char * name,struct loclist * locs,struct attrlist * deps,int devclass)34116a8771bSdholland defattr(const char *name, struct loclist *locs, struct attrlist *deps,
3425ecc953bSthorpej     int devclass)
3435ecc953bSthorpej {
3445ecc953bSthorpej 	struct attr *a, *dep;
3454caf067bSdholland 	struct attrlist *al;
3465ecc953bSthorpej 
3478955e3e2Schristos 	if (getrefattr(name, &a)) {
348f079f926Schristos 		cfgerror("attribute `%s' already defined at %s:%hu", name,
349f079f926Schristos 		    a->a_where.w_srcfile, a->a_where.w_srcline);
3508955e3e2Schristos 		loclist_destroy(locs);
3518955e3e2Schristos 		return (1);
3528955e3e2Schristos 	}
3538955e3e2Schristos 	if (a == NULL)
3548955e3e2Schristos 		a = mkattr(name);
3558955e3e2Schristos 
3565ecc953bSthorpej 	/*
3575ecc953bSthorpej 	 * If this attribute depends on any others, make sure none of
3585ecc953bSthorpej 	 * the dependencies are interface attributes.
3595ecc953bSthorpej 	 */
3604caf067bSdholland 	for (al = deps; al != NULL; al = al->al_next) {
3614caf067bSdholland 		dep = al->al_this;
3625ecc953bSthorpej 		if (dep->a_iattr) {
363c7295a4cSchristos 			cfgerror("`%s' dependency `%s' is an interface "
364c7295a4cSchristos 			    "attribute", name, dep->a_name);
3655ecc953bSthorpej 			return (1);
3665ecc953bSthorpej 		}
36711a68adeSchristos 		(void)ht_insert2(attrdeptab, name, dep->a_name,
36811a68adeSchristos 		    addvalue(V_ATTRIBUTE, a, dep));
369bc707469Suebayasi 		CFGDBG(2, "attr `%s' depends on attr `%s'", name, dep->a_name);
3705ecc953bSthorpej 	}
3715ecc953bSthorpej 
3725ecc953bSthorpej 
373a949e288Suebayasi 	a->a_deps = deps;
374bb4307a6Suebayasi 	expandattr(a, NULL);
3752f2f3382Suebayasi 	CFGDBG(3, "attr `%s' defined", a->a_name);
376bb4307a6Suebayasi 
377bb4307a6Suebayasi 	return (0);
378bb4307a6Suebayasi }
379a949e288Suebayasi 
3802f2f3382Suebayasi struct attr *
mkattr(const char * name)3812f2f3382Suebayasi mkattr(const char *name)
3822f2f3382Suebayasi {
3832f2f3382Suebayasi 	struct attr *a;
3842f2f3382Suebayasi 
3852f2f3382Suebayasi 	a = ecalloc(1, sizeof *a);
3862f2f3382Suebayasi 	if (ht_insert(attrtab, name, a)) {
3872f2f3382Suebayasi 		free(a);
3882f2f3382Suebayasi 		return NULL;
3892f2f3382Suebayasi 	}
3902f2f3382Suebayasi 	a->a_name = name;
391f079f926Schristos 	a->a_where.w_srcfile = yyfile;
392f079f926Schristos 	a->a_where.w_srcline = currentline();
3932f2f3382Suebayasi 	TAILQ_INIT(&a->a_files);
3942f2f3382Suebayasi 	CFGDBG(3, "attr `%s' allocated", name);
3952f2f3382Suebayasi 
3962f2f3382Suebayasi 	return a;
3972f2f3382Suebayasi }
3982f2f3382Suebayasi 
399a949e288Suebayasi /* "interface attribute" initialization */
400bb4307a6Suebayasi int
defiattr(const char * name,struct loclist * locs,struct attrlist * deps,int devclass)401bb4307a6Suebayasi defiattr(const char *name, struct loclist *locs, struct attrlist *deps,
402bb4307a6Suebayasi     int devclass)
403bb4307a6Suebayasi {
404bb4307a6Suebayasi 	struct attr *a;
405bb4307a6Suebayasi 	int len;
406bb4307a6Suebayasi 	struct loclist *ll;
407bb4307a6Suebayasi 
408bb4307a6Suebayasi 	if (devclass)
409d25ad65eSchristos 		panic("%s: %s has both locators and devclass", __func__, name);
410bb4307a6Suebayasi 
411bb4307a6Suebayasi 	if (defattr(name, locs, deps, devclass) != 0)
412bb4307a6Suebayasi 		return (1);
413bb4307a6Suebayasi 
414bb4307a6Suebayasi 	a = getattr(name);
4155ecc953bSthorpej 	a->a_iattr = 1;
41616a8771bSdholland 	/* unwrap */
41716a8771bSdholland 	a->a_locs = locs->ll_next;
41816a8771bSdholland 	locs->ll_next = NULL;
41916a8771bSdholland 	loclist_destroy(locs);
420a949e288Suebayasi 	len = 0;
421a949e288Suebayasi 	for (ll = a->a_locs; ll != NULL; ll = ll->ll_next)
422a949e288Suebayasi 		len++;
423a949e288Suebayasi 	a->a_loclen = len;
424bb4307a6Suebayasi 	if (deps)
425bb4307a6Suebayasi 		CFGDBG(2, "attr `%s' iface with deps", a->a_name);
426bb4307a6Suebayasi 	return (0);
4275ecc953bSthorpej }
428a949e288Suebayasi 
429a949e288Suebayasi /* "device class" initialization */
430bb4307a6Suebayasi int
defdevclass(const char * name,struct loclist * locs,struct attrlist * deps,int devclass)431bb4307a6Suebayasi defdevclass(const char *name, struct loclist *locs, struct attrlist *deps,
432bb4307a6Suebayasi     int devclass)
433bb4307a6Suebayasi {
434bb4307a6Suebayasi 	struct attr *a;
435bbfbbde1Schristos 	char classenum[256], *cp;
4365ecc953bSthorpej 	int errored = 0;
4375ecc953bSthorpej 
438bb4307a6Suebayasi 	if (deps)
439d25ad65eSchristos 		panic("%s: %s has both dependencies and devclass", __func__,
440d25ad65eSchristos 		    name);
441bb4307a6Suebayasi 
442bb4307a6Suebayasi 	if (defattr(name, locs, deps, devclass) != 0)
443bb4307a6Suebayasi 		return (1);
444bb4307a6Suebayasi 
445bb4307a6Suebayasi 	a = getattr(name);
446bbfbbde1Schristos 	(void)snprintf(classenum, sizeof(classenum), "DV_%s", name);
4475ecc953bSthorpej 	for (cp = classenum + 3; *cp; cp++) {
448d25ad65eSchristos 		if (!errored && (!isalnum((unsigned char)*cp) ||
449d25ad65eSchristos 		      (isalpha((unsigned char)*cp)
450d25ad65eSchristos 		      && !islower((unsigned char)*cp)))) {
451c7295a4cSchristos 			cfgerror("device class names must be "
452c7295a4cSchristos 			    "lower-case alphanumeric characters");
4535ecc953bSthorpej 			errored = 1;
4545ecc953bSthorpej 		}
45564a07b54Sjoerg 		*cp = (char)toupper((unsigned char)*cp);
4565ecc953bSthorpej 	}
4575ecc953bSthorpej 	a->a_devclass = intern(classenum);
4585ecc953bSthorpej 
4595ecc953bSthorpej 	return (0);
4605ecc953bSthorpej }
4615ecc953bSthorpej 
4625ecc953bSthorpej /*
4635ecc953bSthorpej  * Return true if the given `error object' is embedded in the given
4645ecc953bSthorpej  * pointer list.
4655ecc953bSthorpej  */
4665ecc953bSthorpej static int
has_errobj(struct attrlist * al,struct attr * obj)4674caf067bSdholland has_errobj(struct attrlist *al, struct attr *obj)
4685ecc953bSthorpej {
4695ecc953bSthorpej 
4704caf067bSdholland 	for (; al != NULL; al = al->al_next)
4714caf067bSdholland 		if (al->al_this == obj)
4725ecc953bSthorpej 			return (1);
4735ecc953bSthorpej 	return (0);
4745ecc953bSthorpej }
4755ecc953bSthorpej 
4765ecc953bSthorpej /*
4775ecc953bSthorpej  * Return true if the given attribute is embedded in the given
4785ecc953bSthorpej  * pointer list.
4795ecc953bSthorpej  */
4805ecc953bSthorpej int
has_attr(struct attrlist * al,const char * attr)4814caf067bSdholland has_attr(struct attrlist *al, const char *attr)
4825ecc953bSthorpej {
4835ecc953bSthorpej 	struct attr *a;
4845ecc953bSthorpej 
4855ecc953bSthorpej 	if ((a = getattr(attr)) == NULL)
4865ecc953bSthorpej 		return (0);
4875ecc953bSthorpej 
4884caf067bSdholland 	for (; al != NULL; al = al->al_next)
4894caf067bSdholland 		if (al->al_this == a)
4905ecc953bSthorpej 			return (1);
4915ecc953bSthorpej 	return (0);
4925ecc953bSthorpej }
4935ecc953bSthorpej 
4945ecc953bSthorpej /*
4955ecc953bSthorpej  * Add a device base to a list in an attribute (actually, to any list).
4965ecc953bSthorpej  * Note that this does not check for duplicates, and does reverse the
4975ecc953bSthorpej  * list order, but no one cares anyway.
4985ecc953bSthorpej  */
4995ecc953bSthorpej static struct nvlist *
addtoattr(struct nvlist * l,struct devbase * dev)5005ecc953bSthorpej addtoattr(struct nvlist *l, struct devbase *dev)
5015ecc953bSthorpej {
5025ecc953bSthorpej 	struct nvlist *n;
5035ecc953bSthorpej 
5045ecc953bSthorpej 	n = newnv(NULL, NULL, dev, 0, l);
5055ecc953bSthorpej 	return (n);
5065ecc953bSthorpej }
5075ecc953bSthorpej 
5085ecc953bSthorpej /*
5095ecc953bSthorpej  * Define a device.  This may (or may not) also define an interface
5105ecc953bSthorpej  * attribute and/or refer to existing attributes.
5115ecc953bSthorpej  */
5125ecc953bSthorpej void
defdev(struct devbase * dev,struct loclist * loclist,struct attrlist * attrs,int ispseudo)51316a8771bSdholland defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs,
5145ecc953bSthorpej        int ispseudo)
5155ecc953bSthorpej {
51616a8771bSdholland 	struct loclist *ll;
5174caf067bSdholland 	struct attrlist *al;
5185ecc953bSthorpej 
5195ecc953bSthorpej 	if (dev == &errdev)
5205ecc953bSthorpej 		goto bad;
5215ecc953bSthorpej 	if (dev->d_isdef) {
52209274d9eSchristos 		cfgerror("redefinition of `%s' (previously defined at %s:%d)",
523f079f926Schristos 		    dev->d_name, dev->d_where.w_srcfile, dev->d_where.w_srcline);
5245ecc953bSthorpej 		goto bad;
5255ecc953bSthorpej 	}
5265ecc953bSthorpej 
5275ecc953bSthorpej 	dev->d_isdef = 1;
5285ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
5295ecc953bSthorpej 		goto bad;
5305ecc953bSthorpej 
5315ecc953bSthorpej 	/*
5325ecc953bSthorpej 	 * Handle implicit attribute definition from locator list.  Do
5335ecc953bSthorpej 	 * this before scanning the `at' list so that we can have, e.g.:
5345ecc953bSthorpej 	 *	device foo at other, foo { slot = -1 }
5355ecc953bSthorpej 	 * (where you can plug in a foo-bus extender to a foo-bus).
5365ecc953bSthorpej 	 */
5375ecc953bSthorpej 	if (loclist != NULL) {
53816a8771bSdholland 		ll = loclist;
5395ecc953bSthorpej 		loclist = NULL;	/* defattr disposes of them for us */
540bb4307a6Suebayasi 		if (defiattr(dev->d_name, ll, NULL, 0))
5415ecc953bSthorpej 			goto bad;
5424caf067bSdholland 		attrs = attrlist_cons(attrs, getattr(dev->d_name));
5434caf067bSdholland 		/* This used to be stored but was never used */
5444caf067bSdholland 		/* attrs->al_name = dev->d_name; */
545c454f920Scube 	}
546c454f920Scube 
547c130d400Scube 	/*
548c130d400Scube 	 * Pseudo-devices can have children.  Consider them as
549c130d400Scube 	 * attaching at root.
550c130d400Scube 	 */
551c454f920Scube 	if (ispseudo) {
5524caf067bSdholland 		for (al = attrs; al != NULL; al = al->al_next)
5534caf067bSdholland 			if (al->al_this->a_iattr)
554c454f920Scube 				break;
5554caf067bSdholland 		if (al != NULL) {
556b66156c7Sdrochner 			if (ispseudo < 2) {
557b66156c7Sdrochner 				if (version >= 20080610)
558b66156c7Sdrochner 					cfgerror("interface attribute on "
559b66156c7Sdrochner 					 "non-device pseudo `%s'", dev->d_name);
560b66156c7Sdrochner 				else {
561b66156c7Sdrochner 					ispseudo = 2;
562b66156c7Sdrochner 				}
563b66156c7Sdrochner 			}
564c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
5655ecc953bSthorpej 		}
566b66156c7Sdrochner 	}
5675ecc953bSthorpej 
5685ecc953bSthorpej 	/* Committed!  Set up fields. */
5695ecc953bSthorpej 	dev->d_ispseudo = ispseudo;
5705ecc953bSthorpej 	dev->d_attrs = attrs;
5715ecc953bSthorpej 	dev->d_classattr = NULL;		/* for now */
572da706167Suebayasi 	CFGDBG(3, "dev `%s' defined", dev->d_name);
5735ecc953bSthorpej 
5745ecc953bSthorpej 	/*
575a2dd9676Suebayasi 	 * Implicit attribute definition for device.
576a2dd9676Suebayasi 	 */
57701b2a130Smartin 	refattr(dev->d_name);
578a2dd9676Suebayasi 
579a2dd9676Suebayasi 	/*
5805ecc953bSthorpej 	 * For each interface attribute this device refers to, add this
5815ecc953bSthorpej 	 * device to its reference list.  This makes, e.g., finding all
5825ecc953bSthorpej 	 * "scsi"s easier.
5835ecc953bSthorpej 	 *
5845ecc953bSthorpej 	 * While looking through the attributes, set up the device
5855ecc953bSthorpej 	 * class if any are devclass attributes (and error out if the
5865ecc953bSthorpej 	 * device has two classes).
5875ecc953bSthorpej 	 */
5884caf067bSdholland 	for (al = attrs; al != NULL; al = al->al_next) {
589da706167Suebayasi 		/*
590da706167Suebayasi 		 * Implicit attribute definition for device dependencies.
591da706167Suebayasi 		 */
592bc707469Suebayasi 		refattr(al->al_this->a_name);
59311a68adeSchristos 		(void)ht_insert2(attrdeptab, dev->d_name, al->al_this->a_name,
59411a68adeSchristos 			addvalue(V_DEVICE, al->al_this, dev));
595bc707469Suebayasi 		CFGDBG(2, "device `%s' depends on attr `%s'", dev->d_name,
596bc707469Suebayasi 		    al->al_this->a_name);
5975ecc953bSthorpej 	}
5985ecc953bSthorpej 	return;
5995ecc953bSthorpej  bad:
60016a8771bSdholland 	loclist_destroy(loclist);
6014caf067bSdholland 	attrlist_destroyall(attrs);
6025ecc953bSthorpej }
6035ecc953bSthorpej 
6045ecc953bSthorpej /*
6055ecc953bSthorpej  * Look up a devbase.  Also makes sure it is a reasonable name,
6065ecc953bSthorpej  * i.e., does not end in a digit or contain special characters.
6075ecc953bSthorpej  */
6085ecc953bSthorpej struct devbase *
getdevbase(const char * name)6095ecc953bSthorpej getdevbase(const char *name)
6105ecc953bSthorpej {
611c7295a4cSchristos 	const u_char *p;
6125ecc953bSthorpej 	struct devbase *dev;
6135ecc953bSthorpej 
614c7295a4cSchristos 	p = (const u_char *)name;
6155ecc953bSthorpej 	if (!isalpha(*p))
6165ecc953bSthorpej 		goto badname;
6175ecc953bSthorpej 	while (*++p) {
6185ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
6195ecc953bSthorpej 			goto badname;
6205ecc953bSthorpej 	}
6215ecc953bSthorpej 	if (isdigit(*--p)) {
6225ecc953bSthorpej  badname:
623c7295a4cSchristos 		cfgerror("bad device base name `%s'", name);
6245ecc953bSthorpej 		return (&errdev);
6255ecc953bSthorpej 	}
6265ecc953bSthorpej 	dev = ht_lookup(devbasetab, name);
6275ecc953bSthorpej 	if (dev == NULL) {
6285ecc953bSthorpej 		dev = ecalloc(1, sizeof *dev);
6295ecc953bSthorpej 		dev->d_name = name;
6305ecc953bSthorpej 		dev->d_isdef = 0;
631d767912bSdrochner 		dev->d_major = NODEVMAJOR;
6325ecc953bSthorpej 		dev->d_attrs = NULL;
6335ecc953bSthorpej 		dev->d_ihead = NULL;
6345ecc953bSthorpej 		dev->d_ipp = &dev->d_ihead;
6355ecc953bSthorpej 		dev->d_ahead = NULL;
6365ecc953bSthorpej 		dev->d_app = &dev->d_ahead;
6375ecc953bSthorpej 		dev->d_umax = 0;
638f079f926Schristos 		dev->d_where.w_srcfile = yyfile;
639f079f926Schristos 		dev->d_where.w_srcline = currentline();
6405ecc953bSthorpej 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
6415ecc953bSthorpej 		if (ht_insert(devbasetab, name, dev))
642d25ad65eSchristos 			panic("%s: Can't insert %s", __func__, name);
643da706167Suebayasi 		CFGDBG(3, "devbase defined `%s'", dev->d_name);
6445ecc953bSthorpej 	}
6455ecc953bSthorpej 	return (dev);
6465ecc953bSthorpej }
6475ecc953bSthorpej 
6485ecc953bSthorpej /*
6495ecc953bSthorpej  * Define some of a device's allowable parent attachments.
6505ecc953bSthorpej  * There may be a list of (plain) attributes.
6515ecc953bSthorpej  */
6525ecc953bSthorpej void
defdevattach(struct deva * deva,struct devbase * dev,struct nvlist * atlist,struct attrlist * attrs)6535ecc953bSthorpej defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
6544caf067bSdholland 	     struct attrlist *attrs)
6555ecc953bSthorpej {
6565ecc953bSthorpej 	struct nvlist *nv;
6574caf067bSdholland 	struct attrlist *al;
6585ecc953bSthorpej 	struct attr *a;
6595ecc953bSthorpej 
6605ecc953bSthorpej 	if (dev == &errdev)
6615ecc953bSthorpej 		goto bad;
6625ecc953bSthorpej 	if (deva == NULL)
6635ecc953bSthorpej 		deva = getdevattach(dev->d_name);
6645ecc953bSthorpej 	if (deva == &errdeva)
6655ecc953bSthorpej 		goto bad;
6665ecc953bSthorpej 	if (!dev->d_isdef) {
667c7295a4cSchristos 		cfgerror("attaching undefined device `%s'", dev->d_name);
6685ecc953bSthorpej 		goto bad;
6695ecc953bSthorpej 	}
6705ecc953bSthorpej 	if (deva->d_isdef) {
67109274d9eSchristos 		cfgerror("redefinition of `%s' (previously defined at %s:%d)",
672f079f926Schristos 		    deva->d_name, deva->d_where.w_srcfile, deva->d_where.w_srcline);
6735ecc953bSthorpej 		goto bad;
6745ecc953bSthorpej 	}
6755ecc953bSthorpej 	if (dev->d_ispseudo) {
676c7295a4cSchristos 		cfgerror("pseudo-devices can't attach");
6775ecc953bSthorpej 		goto bad;
6785ecc953bSthorpej 	}
6795ecc953bSthorpej 
6805ecc953bSthorpej 	deva->d_isdef = 1;
6815ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
6825ecc953bSthorpej 		goto bad;
6834caf067bSdholland 	for (al = attrs; al != NULL; al = al->al_next) {
6844caf067bSdholland 		a = al->al_this;
6855ecc953bSthorpej 		if (a == &errattr)
6865ecc953bSthorpej 			continue;		/* already complained */
6875ecc953bSthorpej 		if (a->a_iattr || a->a_devclass != NULL)
688c7295a4cSchristos 			cfgerror("`%s' is not a plain attribute", a->a_name);
6895ecc953bSthorpej 	}
6905ecc953bSthorpej 
6915ecc953bSthorpej 	/* Committed!  Set up fields. */
6925ecc953bSthorpej 	deva->d_attrs = attrs;
6935ecc953bSthorpej 	deva->d_atlist = atlist;
6945ecc953bSthorpej 	deva->d_devbase = dev;
6952f2f3382Suebayasi 	CFGDBG(3, "deva `%s' defined", deva->d_name);
6962f2f3382Suebayasi 
6972f2f3382Suebayasi 	/*
698a5a68b71Suebayasi 	 * Implicit attribute definition for device attachment.
6992f2f3382Suebayasi 	 */
7002f2f3382Suebayasi 	refattr(deva->d_name);
7015ecc953bSthorpej 
7025ecc953bSthorpej 	/*
7035ecc953bSthorpej 	 * Turn the `at' list into interface attributes (map each
7045ecc953bSthorpej 	 * nv_name to an attribute, or to NULL for root), and add
7055ecc953bSthorpej 	 * this device to those attributes, so that children can
7065ecc953bSthorpej 	 * be listed at this particular device if they are supported
7075ecc953bSthorpej 	 * by that attribute.
7085ecc953bSthorpej 	 */
7095ecc953bSthorpej 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
7105ecc953bSthorpej 		if (nv->nv_name == NULL)
7115ecc953bSthorpej 			nv->nv_ptr = a = NULL;	/* at root */
7125ecc953bSthorpej 		else
7135ecc953bSthorpej 			nv->nv_ptr = a = getattr(nv->nv_name);
7145ecc953bSthorpej 		if (a == &errattr)
7155ecc953bSthorpej 			continue;		/* already complained */
7165ecc953bSthorpej 
71753be83e0Schristos #if 0
7185ecc953bSthorpej 		/*
7195ecc953bSthorpej 		 * Make sure that an attachment spec doesn't
7205ecc953bSthorpej 		 * already say how to attach to this attribute.
7215ecc953bSthorpej 		 */
72253be83e0Schristos 		for (struct deva *da = dev->d_ahead; da; da = da->d_bsame)
7235ecc953bSthorpej 			if (onlist(da->d_atlist, a))
724c7295a4cSchristos 				cfgerror("attach at `%s' already done by `%s'",
7255ecc953bSthorpej 				     a ? a->a_name : "root", da->d_name);
72653be83e0Schristos #endif
7275ecc953bSthorpej 
728c130d400Scube 		if (a == NULL) {
729c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
7305ecc953bSthorpej 			continue;		/* at root; don't add */
731c130d400Scube 		}
7325ecc953bSthorpej 		if (!a->a_iattr)
733c7295a4cSchristos 			cfgerror("%s cannot be at plain attribute `%s'",
7345ecc953bSthorpej 			    dev->d_name, a->a_name);
7355ecc953bSthorpej 		else
7365ecc953bSthorpej 			a->a_devs = addtoattr(a->a_devs, dev);
7375ecc953bSthorpej 	}
7385ecc953bSthorpej 
7395ecc953bSthorpej 	/* attach to parent */
7405ecc953bSthorpej 	*dev->d_app = deva;
7415ecc953bSthorpej 	dev->d_app = &deva->d_bsame;
7425ecc953bSthorpej 	return;
7435ecc953bSthorpej  bad:
7445ecc953bSthorpej 	nvfreel(atlist);
7454caf067bSdholland 	attrlist_destroyall(attrs);
7465ecc953bSthorpej }
7475ecc953bSthorpej 
7485ecc953bSthorpej /*
7495ecc953bSthorpej  * Look up a device attachment.  Also makes sure it is a reasonable
7505ecc953bSthorpej  * name, i.e., does not contain digits or special characters.
7515ecc953bSthorpej  */
7525ecc953bSthorpej struct deva *
getdevattach(const char * name)7535ecc953bSthorpej getdevattach(const char *name)
7545ecc953bSthorpej {
755c7295a4cSchristos 	const u_char *p;
7565ecc953bSthorpej 	struct deva *deva;
7575ecc953bSthorpej 
758c7295a4cSchristos 	p = (const u_char *)name;
7595ecc953bSthorpej 	if (!isalpha(*p))
7605ecc953bSthorpej 		goto badname;
7615ecc953bSthorpej 	while (*++p) {
7625ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
7635ecc953bSthorpej 			goto badname;
7645ecc953bSthorpej 	}
7655ecc953bSthorpej 	if (isdigit(*--p)) {
7665ecc953bSthorpej  badname:
767c7295a4cSchristos 		cfgerror("bad device attachment name `%s'", name);
7685ecc953bSthorpej 		return (&errdeva);
7695ecc953bSthorpej 	}
7705ecc953bSthorpej 	deva = ht_lookup(devatab, name);
7715ecc953bSthorpej 	if (deva == NULL) {
7725ecc953bSthorpej 		deva = ecalloc(1, sizeof *deva);
7735ecc953bSthorpej 		deva->d_name = name;
7745ecc953bSthorpej 		deva->d_bsame = NULL;
7755ecc953bSthorpej 		deva->d_isdef = 0;
7765ecc953bSthorpej 		deva->d_devbase = NULL;
7775ecc953bSthorpej 		deva->d_atlist = NULL;
7785ecc953bSthorpej 		deva->d_attrs = NULL;
7795ecc953bSthorpej 		deva->d_ihead = NULL;
7805ecc953bSthorpej 		deva->d_ipp = &deva->d_ihead;
781f079f926Schristos 		deva->d_where.w_srcfile = yyfile;
782f079f926Schristos 		deva->d_where.w_srcline = currentline();
7835ecc953bSthorpej 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
7845ecc953bSthorpej 		if (ht_insert(devatab, name, deva))
785d25ad65eSchristos 			panic("%s: Can't insert %s", __func__, name);
7865ecc953bSthorpej 	}
7875ecc953bSthorpej 	return (deva);
7885ecc953bSthorpej }
7895ecc953bSthorpej 
7905ecc953bSthorpej /*
7915ecc953bSthorpej  * Look up an attribute.
7925ecc953bSthorpej  */
7935ecc953bSthorpej struct attr *
getattr(const char * name)7945ecc953bSthorpej getattr(const char *name)
7955ecc953bSthorpej {
7965ecc953bSthorpej 	struct attr *a;
7975ecc953bSthorpej 
7985ecc953bSthorpej 	if ((a = ht_lookup(attrtab, name)) == NULL) {
799c7295a4cSchristos 		cfgerror("undefined attribute `%s'", name);
8005ecc953bSthorpej 		a = &errattr;
8015ecc953bSthorpej 	}
8025ecc953bSthorpej 	return (a);
8035ecc953bSthorpej }
8045ecc953bSthorpej 
8055ecc953bSthorpej /*
8062f2f3382Suebayasi  * Implicit attribute definition.
8072f2f3382Suebayasi  */
80851deed3eSuebayasi struct attr *
refattr(const char * name)8092f2f3382Suebayasi refattr(const char *name)
8102f2f3382Suebayasi {
81151deed3eSuebayasi 	struct attr *a;
8122f2f3382Suebayasi 
81351deed3eSuebayasi 	if ((a = ht_lookup(attrtab, name)) == NULL)
81451deed3eSuebayasi 		a = mkattr(name);
81551deed3eSuebayasi 	return a;
8162f2f3382Suebayasi }
8172f2f3382Suebayasi 
818bb52c144Suebayasi int
getrefattr(const char * name,struct attr ** ra)819bb52c144Suebayasi getrefattr(const char *name, struct attr **ra)
820bb52c144Suebayasi {
821bb52c144Suebayasi 	struct attr *a;
822bb52c144Suebayasi 
823bb52c144Suebayasi 	a = ht_lookup(attrtab, name);
824bb52c144Suebayasi 	if (a == NULL) {
825bb52c144Suebayasi 		*ra = NULL;
826bb52c144Suebayasi 		return (0);
827bb52c144Suebayasi 	}
828bb52c144Suebayasi 	/*
829bb52c144Suebayasi 	 * Check if the existing attr is only referenced, not really defined.
830bb52c144Suebayasi 	 */
8317d058632Smrg 	*ra = a;
832bb52c144Suebayasi 	if (a->a_deps == NULL &&
833bb52c144Suebayasi 	    a->a_iattr == 0 &&
834bb52c144Suebayasi 	    a->a_devclass == 0) {
835bb52c144Suebayasi 		return (0);
836bb52c144Suebayasi 	}
837bb52c144Suebayasi 	return (1);
838bb52c144Suebayasi }
839bb52c144Suebayasi 
8402f2f3382Suebayasi /*
8415ecc953bSthorpej  * Recursively expand an attribute and its dependencies, checking for
8425ecc953bSthorpej  * cycles, and invoking a callback for each attribute found.
8435ecc953bSthorpej  */
8445ecc953bSthorpej void
expandattr(struct attr * a,void (* callback)(struct attr *))8455ecc953bSthorpej expandattr(struct attr *a, void (*callback)(struct attr *))
8465ecc953bSthorpej {
8474caf067bSdholland 	struct attrlist *al;
8485ecc953bSthorpej 	struct attr *dep;
8495ecc953bSthorpej 
8505ecc953bSthorpej 	if (a->a_expanding) {
851c7295a4cSchristos 		cfgerror("circular dependency on attribute `%s'", a->a_name);
8525ecc953bSthorpej 		return;
8535ecc953bSthorpej 	}
8545ecc953bSthorpej 
8555ecc953bSthorpej 	a->a_expanding = 1;
8565ecc953bSthorpej 
8575ecc953bSthorpej 	/* First expand all of this attribute's dependencies. */
8584caf067bSdholland 	for (al = a->a_deps; al != NULL; al = al->al_next) {
8594caf067bSdholland 		dep = al->al_this;
8605ecc953bSthorpej 		expandattr(dep, callback);
8615ecc953bSthorpej 	}
8625ecc953bSthorpej 
8635ecc953bSthorpej 	/* ...and now invoke the callback for ourself. */
8645ecc953bSthorpej 	if (callback != NULL)
8655ecc953bSthorpej 		(*callback)(a);
8665ecc953bSthorpej 
8675ecc953bSthorpej 	a->a_expanding = 0;
8685ecc953bSthorpej }
8695ecc953bSthorpej 
8705ecc953bSthorpej /*
8715ecc953bSthorpej  * Set the major device number for a device, so that it can be used
8725ecc953bSthorpej  * as a root/dumps "on" device in a configuration.
8735ecc953bSthorpej  */
8745ecc953bSthorpej void
setmajor(struct devbase * d,devmajor_t n)875d767912bSdrochner setmajor(struct devbase *d, devmajor_t n)
8765ecc953bSthorpej {
8775ecc953bSthorpej 
878d767912bSdrochner 	if (d != &errdev && d->d_major != NODEVMAJOR)
879d767912bSdrochner 		cfgerror("device `%s' is already major %d",
880d767912bSdrochner 		    d->d_name, d->d_major);
8815ecc953bSthorpej 	else
8825ecc953bSthorpej 		d->d_major = n;
8835ecc953bSthorpej }
8845ecc953bSthorpej 
8855ecc953bSthorpej const char *
major2name(devmajor_t maj)886d767912bSdrochner major2name(devmajor_t maj)
8875ecc953bSthorpej {
8885ecc953bSthorpej 	struct devbase *dev;
8895ecc953bSthorpej 	struct devm *dm;
8905ecc953bSthorpej 
8915ecc953bSthorpej 	if (!do_devsw) {
8925ecc953bSthorpej 		TAILQ_FOREACH(dev, &allbases, d_next) {
8935ecc953bSthorpej 			if (dev->d_major == maj)
8945ecc953bSthorpej 				return (dev->d_name);
8955ecc953bSthorpej 		}
8965ecc953bSthorpej 	} else {
8975ecc953bSthorpej 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
8985ecc953bSthorpej 			if (dm->dm_bmajor == maj)
8995ecc953bSthorpej 				return (dm->dm_name);
9005ecc953bSthorpej 		}
9015ecc953bSthorpej 	}
9025ecc953bSthorpej 	return (NULL);
9035ecc953bSthorpej }
9045ecc953bSthorpej 
905d767912bSdrochner devmajor_t
dev2major(struct devbase * dev)9065ecc953bSthorpej dev2major(struct devbase *dev)
9075ecc953bSthorpej {
9085ecc953bSthorpej 	struct devm *dm;
9095ecc953bSthorpej 
9105ecc953bSthorpej 	if (!do_devsw)
9115ecc953bSthorpej 		return (dev->d_major);
9125ecc953bSthorpej 
9135ecc953bSthorpej 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
9145ecc953bSthorpej 		if (strcmp(dm->dm_name, dev->d_name) == 0)
9155ecc953bSthorpej 			return (dm->dm_bmajor);
9165ecc953bSthorpej 	}
917d767912bSdrochner 	return (NODEVMAJOR);
9185ecc953bSthorpej }
9195ecc953bSthorpej 
9205ecc953bSthorpej /*
9215ecc953bSthorpej  * Make a string description of the device at maj/min.
9225ecc953bSthorpej  */
9235ecc953bSthorpej static const char *
makedevstr(devmajor_t maj,devminor_t min)924d767912bSdrochner makedevstr(devmajor_t maj, devminor_t min)
9255ecc953bSthorpej {
926c7295a4cSchristos 	const char *devicename;
9275ecc953bSthorpej 	char buf[32];
9285ecc953bSthorpej 
929c7295a4cSchristos 	devicename = major2name(maj);
930c7295a4cSchristos 	if (devicename == NULL)
931d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
9325ecc953bSthorpej 	else
933d767912bSdrochner 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
934d767912bSdrochner 		    min / maxpartitions, (min % maxpartitions) + 'a');
9355ecc953bSthorpej 
9365ecc953bSthorpej 	return (intern(buf));
9375ecc953bSthorpej }
9385ecc953bSthorpej 
9395ecc953bSthorpej /*
9405ecc953bSthorpej  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
9415ecc953bSthorpej  * Handle the case where the device number is given but there is no
9425ecc953bSthorpej  * corresponding name, and map NULL to the default.
9435ecc953bSthorpej  */
9445ecc953bSthorpej static int
resolve(struct nvlist ** nvp,const char * name,const char * what,struct nvlist * dflt,int part)9455ecc953bSthorpej resolve(struct nvlist **nvp, const char *name, const char *what,
9465ecc953bSthorpej 	struct nvlist *dflt, int part)
9475ecc953bSthorpej {
9485ecc953bSthorpej 	struct nvlist *nv;
9495ecc953bSthorpej 	struct devbase *dev;
9505ecc953bSthorpej 	const char *cp;
951d767912bSdrochner 	devmajor_t maj;
952d767912bSdrochner 	devminor_t min;
953*334baaa8Srillig 	size_t l;
9545ecc953bSthorpej 	int unit;
9555ecc953bSthorpej 	char buf[NAMESIZE];
9565ecc953bSthorpej 
9575cc303e1Slukem 	if ((part -= 'a') >= maxpartitions || part < 0)
958d25ad65eSchristos 		panic("%s: Bad partition %c", __func__, part);
9595ecc953bSthorpej 	if ((nv = *nvp) == NULL) {
9605ecc953bSthorpej 		dev_t	d = NODEV;
9615ecc953bSthorpej 		/*
9625ecc953bSthorpej 		 * Apply default.  Easiest to do this by number.
9635ecc953bSthorpej 		 * Make sure to retain NODEVness, if this is dflt's disposition.
9645ecc953bSthorpej 		 */
9655cc303e1Slukem 		if ((dev_t)dflt->nv_num != NODEV) {
9660001b928Schristos 			maj = major(dflt->nv_num);
9670001b928Schristos 			min = ((minor(dflt->nv_num) / maxpartitions) *
9685ecc953bSthorpej 			    maxpartitions) + part;
9695ecc953bSthorpej 			d = makedev(maj, min);
9705ecc953bSthorpej 			cp = makedevstr(maj, min);
9715ecc953bSthorpej 		} else
9725ecc953bSthorpej 			cp = NULL;
9739a7f4bbaSchristos 		*nvp = nv = newnv(NULL, cp, NULL, (long long)d, NULL);
9745ecc953bSthorpej 	}
9755cc303e1Slukem 	if ((dev_t)nv->nv_num != NODEV) {
9765ecc953bSthorpej 		/*
9775ecc953bSthorpej 		 * By the numbers.  Find the appropriate major number
9785ecc953bSthorpej 		 * to make a name.
9795ecc953bSthorpej 		 */
9800001b928Schristos 		maj = major(nv->nv_num);
9810001b928Schristos 		min = minor(nv->nv_num);
9825ecc953bSthorpej 		nv->nv_str = makedevstr(maj, min);
9835ecc953bSthorpej 		return (0);
9845ecc953bSthorpej 	}
9855ecc953bSthorpej 
9865ecc953bSthorpej 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
9875ecc953bSthorpej 		/*
9885ecc953bSthorpej 		 * Wildcarded or unspecified; leave it as NODEV.
9895ecc953bSthorpej 		 */
9905ecc953bSthorpej 		return (0);
9915ecc953bSthorpej 
99279cd2972Smlelstv 	if (nv->nv_ptr != NULL && strcmp(nv->nv_ptr, "spec") == 0)
99379cd2972Smlelstv 		/*
99479cd2972Smlelstv 		 * spec string, interpreted by kernel
99579cd2972Smlelstv 		 */
99679cd2972Smlelstv 		return (0);
99779cd2972Smlelstv 
9985ecc953bSthorpej 	/*
9995ecc953bSthorpej 	 * The normal case: things like "ra2b".  Check for partition
10005ecc953bSthorpej 	 * suffix, remove it if there, and split into name ("ra") and
10015ecc953bSthorpej 	 * unit (2).
10025ecc953bSthorpej 	 */
1003*334baaa8Srillig 	l = strlen(nv->nv_str);
10045ecc953bSthorpej 	cp = &nv->nv_str[l];
10055ecc953bSthorpej 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
10065ecc953bSthorpej 	    isdigit((unsigned char)cp[-1])) {
10075ecc953bSthorpej 		l--;
10085ecc953bSthorpej 		part = *cp - 'a';
10095ecc953bSthorpej 	}
10105ecc953bSthorpej 	cp = nv->nv_str;
10115ecc953bSthorpej 	if (split(cp, l, buf, sizeof buf, &unit)) {
1012c7295a4cSchristos 		cfgerror("%s: invalid %s device name `%s'", name, what, cp);
10135ecc953bSthorpej 		return (1);
10145ecc953bSthorpej 	}
10155ecc953bSthorpej 	dev = ht_lookup(devbasetab, intern(buf));
10165ecc953bSthorpej 	if (dev == NULL) {
1017c7295a4cSchristos 		cfgerror("%s: device `%s' does not exist", name, buf);
10185ecc953bSthorpej 		return (1);
10195ecc953bSthorpej 	}
10205ecc953bSthorpej 
10215ecc953bSthorpej 	/*
10225ecc953bSthorpej 	 * Check for the magic network interface attribute, and
10235ecc953bSthorpej 	 * don't bother making a device number.
10245ecc953bSthorpej 	 */
10255ecc953bSthorpej 	if (has_attr(dev->d_attrs, s_ifnet)) {
10269a7f4bbaSchristos 		nv->nv_num = (long long)NODEV;
10275ecc953bSthorpej 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
10285ecc953bSthorpej 	} else {
10295ecc953bSthorpej 		maj = dev2major(dev);
10305cc303e1Slukem 		if (maj == NODEVMAJOR) {
1031c7295a4cSchristos 			cfgerror("%s: can't make %s device from `%s'",
10325ecc953bSthorpej 			    name, what, nv->nv_str);
10335ecc953bSthorpej 			return (1);
10345ecc953bSthorpej 		}
10355beb9d5aSjoerg 		nv->nv_num = (long long)makedev(maj, unit * maxpartitions + part);
10365ecc953bSthorpej 	}
10375ecc953bSthorpej 
10385ecc953bSthorpej 	nv->nv_name = dev->d_name;
10395ecc953bSthorpej 	return (0);
10405ecc953bSthorpej }
10415ecc953bSthorpej 
10425ecc953bSthorpej /*
10435ecc953bSthorpej  * Add a completed configuration to the list.
10445ecc953bSthorpej  */
10455ecc953bSthorpej void
addconf(struct config * cf0)10465ecc953bSthorpej addconf(struct config *cf0)
10475ecc953bSthorpej {
10485ecc953bSthorpej 	struct config *cf;
10495ecc953bSthorpej 	const char *name;
10505ecc953bSthorpej 
10515ecc953bSthorpej 	name = cf0->cf_name;
1052f079f926Schristos 	if ((cf = ht_lookup(cfhashtab, name)) != NULL) {
1053f079f926Schristos 		cfgerror("configuration `%s' already defined %s:%hu", name,
1054f079f926Schristos 			cf->cf_where.w_srcfile, cf->cf_where.w_srcline);
10555ecc953bSthorpej 		goto bad;
10565ecc953bSthorpej 	}
1057f079f926Schristos 	cf = ecalloc(1, sizeof *cf);
1058f079f926Schristos 	if (ht_insert(cfhashtab, name, cf)) {
1059f079f926Schristos 		free(cf);
1060f079f926Schristos 	}
10615ecc953bSthorpej 	*cf = *cf0;
1062f079f926Schristos 	cf->cf_where.w_srcfile = yyfile;
1063f079f926Schristos 	cf->cf_where.w_srcline = currentline();
10645ecc953bSthorpej 
10655ecc953bSthorpej 	/*
10665ecc953bSthorpej 	 * Resolve the root device.
10675ecc953bSthorpej 	 */
1068cd429362Serh 	if (cf->cf_root == NULL) {
1069c7295a4cSchristos 		cfgerror("%s: no root device specified", name);
10705ecc953bSthorpej 		goto bad;
10715ecc953bSthorpej 	}
1072cd429362Serh 	if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
1073cd429362Serh 		struct nvlist *nv;
1074cd429362Serh 		nv = cf->cf_root;
10755ecc953bSthorpej 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
10765ecc953bSthorpej 			goto bad;
10775ecc953bSthorpej 	}
10785ecc953bSthorpej 
10795ecc953bSthorpej 	/*
10805ecc953bSthorpej 	 * Resolve the dump device.
10815ecc953bSthorpej 	 */
10825ecc953bSthorpej 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
10835ecc953bSthorpej 		/*
10845ecc953bSthorpej 		 * Wildcarded dump device is equivalent to unspecified.
10855ecc953bSthorpej 		 */
10865ecc953bSthorpej 		cf->cf_dump = NULL;
10875ecc953bSthorpej 	} else if (cf->cf_dump->nv_str == s_none) {
10885ecc953bSthorpej 		/*
10895ecc953bSthorpej 		 * Operator has requested that no dump device should be
10905ecc953bSthorpej 		 * configured; do nothing.
10915ecc953bSthorpej 		 */
10925ecc953bSthorpej 	} else {
10935ecc953bSthorpej 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
10945ecc953bSthorpej 			goto bad;
10955ecc953bSthorpej 	}
10965ecc953bSthorpej 
10975ecc953bSthorpej 	/* Wildcarded fstype is `unspecified'. */
10985ecc953bSthorpej 	if (cf->cf_fstype == s_qmark)
10995ecc953bSthorpej 		cf->cf_fstype = NULL;
11005ecc953bSthorpej 
11015ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
11025ecc953bSthorpej 	return;
11035ecc953bSthorpej  bad:
11045ecc953bSthorpej 	nvfreel(cf0->cf_root);
11055ecc953bSthorpej 	nvfreel(cf0->cf_dump);
11065ecc953bSthorpej }
11075ecc953bSthorpej 
11085ecc953bSthorpej void
setconf(struct nvlist ** npp,const char * what,struct nvlist * v)11095ecc953bSthorpej setconf(struct nvlist **npp, const char *what, struct nvlist *v)
11105ecc953bSthorpej {
11115ecc953bSthorpej 
11125ecc953bSthorpej 	if (*npp != NULL) {
1113c7295a4cSchristos 		cfgerror("duplicate %s specification", what);
11145ecc953bSthorpej 		nvfreel(v);
11155ecc953bSthorpej 	} else
11165ecc953bSthorpej 		*npp = v;
11175ecc953bSthorpej }
11185ecc953bSthorpej 
11195ecc953bSthorpej void
delconf(const char * name,int nowarn)11203dbd1573Schristos delconf(const char *name, int nowarn)
112142b52b8aScube {
112242b52b8aScube 	struct config *cf;
112342b52b8aScube 
112475eb07f2Suebayasi 	CFGDBG(5, "deselecting config `%s'", name);
112542b52b8aScube 	if (ht_lookup(cfhashtab, name) == NULL) {
11263dbd1573Schristos 		if (!nowarn)
1127c7295a4cSchristos 			cfgerror("configuration `%s' undefined", name);
112842b52b8aScube 		return;
112942b52b8aScube 	}
113042b52b8aScube 	(void)ht_remove(cfhashtab, name);
113142b52b8aScube 
113242b52b8aScube 	TAILQ_FOREACH(cf, &allcf, cf_next)
113342b52b8aScube 		if (!strcmp(cf->cf_name, name))
113442b52b8aScube 			break;
113542b52b8aScube 	if (cf == NULL)
1136d25ad65eSchristos 		panic("%s: lost configuration for %s", __func__, name);
113742b52b8aScube 
113842b52b8aScube 	TAILQ_REMOVE(&allcf, cf, cf_next);
113942b52b8aScube }
114042b52b8aScube 
114142b52b8aScube void
setfstype(const char ** fstp,const char * v)11425ecc953bSthorpej setfstype(const char **fstp, const char *v)
11435ecc953bSthorpej {
11445ecc953bSthorpej 
11455ecc953bSthorpej 	if (*fstp != NULL) {
1146c7295a4cSchristos 		cfgerror("multiple fstype specifications");
11475ecc953bSthorpej 		return;
11485ecc953bSthorpej 	}
11495ecc953bSthorpej 
11505ecc953bSthorpej 	if (v != s_qmark && OPT_FSOPT(v)) {
1151c7295a4cSchristos 		cfgerror("\"%s\" is not a configured file system", v);
11525ecc953bSthorpej 		return;
11535ecc953bSthorpej 	}
11545ecc953bSthorpej 
11555ecc953bSthorpej 	*fstp = v;
11565ecc953bSthorpej }
11575ecc953bSthorpej 
11585ecc953bSthorpej static struct devi *
newdevi(const char * name,int unit,struct devbase * d)11595ecc953bSthorpej newdevi(const char *name, int unit, struct devbase *d)
11605ecc953bSthorpej {
11615ecc953bSthorpej 	struct devi *i;
11625ecc953bSthorpej 
11635ecc953bSthorpej 	i = ecalloc(1, sizeof *i);
11645ecc953bSthorpej 	i->i_name = name;
11655ecc953bSthorpej 	i->i_unit = unit;
11665ecc953bSthorpej 	i->i_base = d;
11675ecc953bSthorpej 	i->i_bsame = NULL;
11685ecc953bSthorpej 	i->i_asame = NULL;
11695ecc953bSthorpej 	i->i_alias = NULL;
11705ecc953bSthorpej 	i->i_at = NULL;
11715ecc953bSthorpej 	i->i_pspec = NULL;
11725ecc953bSthorpej 	i->i_atdeva = NULL;
11735ecc953bSthorpej 	i->i_locs = NULL;
11745ecc953bSthorpej 	i->i_cfflags = 0;
1175f079f926Schristos 	i->i_where.w_srcline = currentline();
1176f079f926Schristos 	i->i_where.w_srcfile = yyfile;
11777aa6070dScube 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
1178a31ff6b4Scube 	i->i_level = devilevel;
117990ac64deSpooka 	i->i_pseudoroot = 0;
1180f079f926Schristos 	i->i_where.w_srcfile = yyfile;
1181f079f926Schristos 	i->i_where.w_srcline = currentline();
11825ecc953bSthorpej 	if (unit >= d->d_umax)
11835ecc953bSthorpej 		d->d_umax = unit + 1;
11845ecc953bSthorpej 	return (i);
11855ecc953bSthorpej }
11865ecc953bSthorpej 
118753be83e0Schristos static struct attr *
finddevattr(const char * name,const char * at,struct devbase * ib,struct devbase ** ab,int * atunit)118853be83e0Schristos finddevattr(const char *name, const char *at, struct devbase *ib,
118953be83e0Schristos     struct devbase **ab, int *atunit)
11905ecc953bSthorpej {
11915ecc953bSthorpej 	const char *cp;
11925ecc953bSthorpej 	char atbuf[NAMESIZE];
119353be83e0Schristos 	struct attrlist *al;
119453be83e0Schristos 	struct attr *attr;
11955ecc953bSthorpej 
11965ecc953bSthorpej 	if (at == NULL) {
119753be83e0Schristos 		*ab = NULL;
119853be83e0Schristos 		*atunit = -1;
119953be83e0Schristos 		return &errattr;	/* a convenient "empty" attr */
12005ecc953bSthorpej 	}
120153be83e0Schristos 	if (split(at, strlen(at), atbuf, sizeof atbuf, atunit)) {
1202c7295a4cSchristos 		cfgerror("invalid attachment name `%s'", at);
12035ecc953bSthorpej 		/* (void)getdevi(name); -- ??? */
120453be83e0Schristos 		return NULL;
12055ecc953bSthorpej 	}
12065ecc953bSthorpej 
12075ecc953bSthorpej 	/*
12085ecc953bSthorpej 	 * Devices can attach to two types of things: Attributes,
12095ecc953bSthorpej 	 * and other devices (which have the appropriate attributes
12105ecc953bSthorpej 	 * to allow attachment).
12115ecc953bSthorpej 	 *
12125ecc953bSthorpej 	 * (1) If we're attached to an attribute, then we don't need
12135ecc953bSthorpej 	 *     look at the parent base device to see what attributes
12145ecc953bSthorpej 	 *     it has, and make sure that we can attach to them.
12155ecc953bSthorpej 	 *
12165ecc953bSthorpej 	 * (2) If we're attached to a real device (i.e. named in
12175ecc953bSthorpej 	 *     the config file), we want to remember that so that
12185ecc953bSthorpej 	 *     at cross-check time, if the device we're attached to
12195ecc953bSthorpej 	 *     is missing but other devices which also provide the
12205ecc953bSthorpej 	 *     attribute are present, we don't get a false "OK."
12215ecc953bSthorpej 	 *
12225ecc953bSthorpej 	 * (3) If the thing we're attached to is an attribute
12235ecc953bSthorpej 	 *     but is actually named in the config file, we still
12245ecc953bSthorpej 	 *     have to remember its devbase.
12255ecc953bSthorpej 	 */
12265ecc953bSthorpej 	cp = intern(atbuf);
12275ecc953bSthorpej 
12285ecc953bSthorpej 	/* Figure out parent's devbase, to satisfy case (3). */
122953be83e0Schristos 	*ab = ht_lookup(devbasetab, cp);
12305ecc953bSthorpej 
12315ecc953bSthorpej 	/* Find out if it's an attribute. */
12325ecc953bSthorpej 	attr = ht_lookup(attrtab, cp);
12335ecc953bSthorpej 
12345ecc953bSthorpej 	/* Make sure we're _really_ attached to the attr.  Case (1). */
12355ecc953bSthorpej 	if (attr != NULL && onlist(attr->a_devs, ib))
123653be83e0Schristos 		return attr;
12375ecc953bSthorpej 
12385ecc953bSthorpej 	/*
12395ecc953bSthorpej 	 * Else a real device, and not just an attribute.  Case (2).
12405ecc953bSthorpej 	 *
12415ecc953bSthorpej 	 * Have to work a bit harder to see whether we have
12425ecc953bSthorpej 	 * something like "tg0 at esp0" (where esp is merely
12435ecc953bSthorpej 	 * not an attribute) or "tg0 at nonesuch0" (where
12445ecc953bSthorpej 	 * nonesuch is not even a device).
12455ecc953bSthorpej 	 */
124653be83e0Schristos 	if (*ab == NULL) {
124753be83e0Schristos 		cfgerror("%s at %s: `%s' unknown", name, at, atbuf);
124853be83e0Schristos 		return NULL;
12495ecc953bSthorpej 	}
12505ecc953bSthorpej 
12515ecc953bSthorpej 	/*
12525ecc953bSthorpej 	 * See if the named parent carries an attribute
12535ecc953bSthorpej 	 * that allows it to supervise device ib.
12545ecc953bSthorpej 	 */
125553be83e0Schristos 	for (al = (*ab)->d_attrs; al != NULL; al = al->al_next) {
12564caf067bSdholland 		attr = al->al_this;
12575ecc953bSthorpej 		if (onlist(attr->a_devs, ib))
125853be83e0Schristos 			return attr;
12595ecc953bSthorpej 	}
1260c7295a4cSchristos 	cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
126153be83e0Schristos 	return NULL;
126253be83e0Schristos }
126353be83e0Schristos 
126453be83e0Schristos /*
126553be83e0Schristos  * Add the named device as attaching to the named attribute (or perhaps
126653be83e0Schristos  * another device instead) plus unit number.
126753be83e0Schristos  */
126853be83e0Schristos void
adddev(const char * name,const char * at,struct loclist * loclist,int flags)126953be83e0Schristos adddev(const char *name, const char *at, struct loclist *loclist, int flags)
127053be83e0Schristos {
127153be83e0Schristos 	struct devi *i;		/* the new instance */
127253be83e0Schristos 	struct pspec *p;	/* and its pspec */
127353be83e0Schristos 	struct attr *attr;	/* attribute that allows attach */
127453be83e0Schristos 	struct devbase *ib;	/* i->i_base */
127553be83e0Schristos 	struct devbase *ab;	/* not NULL => at another dev */
127653be83e0Schristos 	struct deva *iba;	/* devbase attachment used */
127753be83e0Schristos 	struct deva *lastiba;
1278d25ad65eSchristos 	int atunit, first;
127953be83e0Schristos 
128053be83e0Schristos 	lastiba = NULL;
128153be83e0Schristos 	if ((i = getdevi(name)) == NULL)
128253be83e0Schristos 		goto bad;
128353be83e0Schristos 	ib = i->i_base;
128453be83e0Schristos 	attr = finddevattr(name, at, ib, &ab, &atunit);
128553be83e0Schristos 	if (attr == NULL) {
12865ec393e9Scube 		i->i_active = DEVI_BROKEN;
12875ecc953bSthorpej 		goto bad;
128853be83e0Schristos 	}
12895ecc953bSthorpej 
129053be83e0Schristos 	for (lastiba = ib->d_ahead; lastiba; lastiba = iba->d_bsame) {
129153be83e0Schristos 		for (iba = lastiba; iba != NULL; iba = iba->d_bsame)
129253be83e0Schristos 			if (onlist(iba->d_atlist,
129353be83e0Schristos 			    attr == &errattr ? NULL : attr))
12945ecc953bSthorpej 				break;
129553be83e0Schristos 
1296d25ad65eSchristos 		first = lastiba == ib->d_ahead;
129753be83e0Schristos 		if (iba == NULL) {
1298d25ad65eSchristos 			if (!first)
129953be83e0Schristos 				goto bad;
130053be83e0Schristos 			if (attr != &errattr) {
1301d25ad65eSchristos 				panic("%s: can't figure out attachment",
1302d25ad65eSchristos 				    __func__);
130353be83e0Schristos 			} else {
130453be83e0Schristos 				cfgerror("`%s' cannot attach to the root",
130553be83e0Schristos 				    ib->d_name);
130653be83e0Schristos 				i->i_active = DEVI_BROKEN;
13075ecc953bSthorpej 			}
130853be83e0Schristos 		}
130953be83e0Schristos 		// get a new one if it is not the first time
1310d25ad65eSchristos 		if (!first && (i = getdevi(name)) == NULL)
131153be83e0Schristos 			goto bad;
131253be83e0Schristos 
131353be83e0Schristos 		if (attr != &errattr) {
131453be83e0Schristos 			/*
131553be83e0Schristos 			 * Find the parent spec.  If a matching one has not
131653be83e0Schristos 			 * yet been created, create one.
131753be83e0Schristos 			 *
131853be83e0Schristos 			 * XXX: This creates multiple pspecs that look the
131953be83e0Schristos 			 * same in the config file and could be merged.
132053be83e0Schristos 			 */
13214b9e5cb1Schristos 			p = getpspec(attr, ab, atunit, first);
132253be83e0Schristos 			p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
1323d25ad65eSchristos 		} else
1324d25ad65eSchristos 			p = NULL;
132553be83e0Schristos 
13265ec393e9Scube 		if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
13275ec393e9Scube 			i->i_active = DEVI_BROKEN;
13285ecc953bSthorpej 			goto bad;
13295ec393e9Scube 		}
13305ecc953bSthorpej 		i->i_at = at;
13315ecc953bSthorpej 		i->i_pspec = p;
13325ecc953bSthorpej 		i->i_atdeva = iba;
13335ecc953bSthorpej 		i->i_cfflags = flags;
133453be83e0Schristos 		CFGDBG(3, "devi `%s' at '%s' added", i->i_name, iba->d_name);
13355ecc953bSthorpej 
13365ecc953bSthorpej 		*iba->d_ipp = i;
13375ecc953bSthorpej 		iba->d_ipp = &i->i_asame;
133853be83e0Schristos 	}
13395ecc953bSthorpej 
13405ecc953bSthorpej 	/* all done, fall into ... */
13415ecc953bSthorpej  bad:
134216a8771bSdholland 	loclist_destroy(loclist);
13435ecc953bSthorpej 	return;
13445ecc953bSthorpej }
13455ecc953bSthorpej 
13465ecc953bSthorpej void
deldevi(const char * name,const char * at,int nowarn)13473dbd1573Schristos deldevi(const char *name, const char *at, int nowarn)
13485ecc953bSthorpej {
13497b7c582aScube 	struct devi *firsti, *i;
13505ecc953bSthorpej 	struct devbase *d;
13515ecc953bSthorpej 	int unit;
13525ecc953bSthorpej 	char base[NAMESIZE];
13535ecc953bSthorpej 
135475eb07f2Suebayasi 	CFGDBG(5, "deselecting devi `%s'", name);
13555ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
13563dbd1573Schristos 		if (!nowarn) {
1357c7295a4cSchristos 			cfgerror("invalid device name `%s'", name);
13585ecc953bSthorpej 			return;
13595ecc953bSthorpej 		}
13603dbd1573Schristos 	}
13615ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
13625ecc953bSthorpej 	if (d == NULL) {
13633dbd1573Schristos 		if (!nowarn)
1364c7295a4cSchristos 			cfgerror("%s: unknown device `%s'", name, base);
13655ecc953bSthorpej 		return;
13665ecc953bSthorpej 	}
13675ecc953bSthorpej 	if (d->d_ispseudo) {
1368c7295a4cSchristos 		cfgerror("%s: %s is a pseudo-device", name, base);
13695ecc953bSthorpej 		return;
13705ecc953bSthorpej 	}
13715ecc953bSthorpej 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
1372c7295a4cSchristos 		cfgerror("`%s' not defined", name);
13735ecc953bSthorpej 		return;
13745ecc953bSthorpej 	}
13757b7c582aScube 	if (at == NULL && firsti->i_at == NULL) {
1376e499d8b5Scube 		/* 'at root' */
13777b7c582aScube 		remove_devi(firsti);
13787b7c582aScube 		return;
13797b7c582aScube 	} else if (at != NULL)
13807b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
1381bfc250f2Scube 			if (i->i_active != DEVI_BROKEN &&
1382bfc250f2Scube 			    strcmp(at, i->i_at) == 0) {
13837b7c582aScube 				remove_devi(i);
13845ecc953bSthorpej 				return;
13855ecc953bSthorpej 			}
1386c7295a4cSchristos 	cfgerror("`%s' at `%s' not found", name, at ? at : "root");
1387e499d8b5Scube }
13885ecc953bSthorpej 
13897b7c582aScube static void
remove_pspec(struct devi * i)1390511fbc70Schristos remove_pspec(struct devi *i)
1391511fbc70Schristos {
1392511fbc70Schristos 	struct pspec *p = i->i_pspec;
1393511fbc70Schristos 	struct nvlist *nv, *onv;
1394511fbc70Schristos 
1395511fbc70Schristos 	if (p == NULL)
1396511fbc70Schristos 		return;
1397511fbc70Schristos 
1398511fbc70Schristos 	/* Double-linked nvlist anyone? */
1399511fbc70Schristos 	for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
1400511fbc70Schristos 		if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1401511fbc70Schristos 			onv = nv->nv_next;
1402511fbc70Schristos 			nv->nv_next = onv->nv_next;
1403511fbc70Schristos 			nvfree(onv);
1404511fbc70Schristos 			break;
1405511fbc70Schristos 		}
1406511fbc70Schristos 		if (nv->nv_ptr == i) {
1407511fbc70Schristos 			/* nv is p->p_devs in that case */
1408511fbc70Schristos 			p->p_devs = nv->nv_next;
1409511fbc70Schristos 			nvfree(nv);
1410511fbc70Schristos 			break;
1411511fbc70Schristos 		}
1412511fbc70Schristos 	}
1413511fbc70Schristos 	if (p->p_devs == NULL)
1414511fbc70Schristos 		TAILQ_REMOVE(&allpspecs, p, p_list);
1415511fbc70Schristos }
1416511fbc70Schristos 
1417511fbc70Schristos static void
remove_devi(struct devi * i)14187b7c582aScube remove_devi(struct devi *i)
14197b7c582aScube {
14207b7c582aScube 	struct devbase *d = i->i_base;
14217b7c582aScube 	struct devi *f, *j, **ppi;
14227b7c582aScube 	struct deva *iba;
14237b7c582aScube 
14248d928a75Suebayasi 	CFGDBG(5, "removing devi `%s'", i->i_name);
14257b7c582aScube 	f = ht_lookup(devitab, i->i_name);
14266a6299ebScube 	if (f == NULL)
1427d25ad65eSchristos 		panic("%s: instance %s disappeared from devitab", __func__,
14286a6299ebScube 		    i->i_name);
14297b7c582aScube 
1430bfc250f2Scube 	if (i->i_active == DEVI_BROKEN) {
1431bfc250f2Scube 		cfgerror("not removing broken instance `%s'", i->i_name);
1432bfc250f2Scube 		return;
1433bfc250f2Scube 	}
1434bfc250f2Scube 
1435e499d8b5Scube 	/*
1436e499d8b5Scube 	 * We have the device instance, i.
1437e499d8b5Scube 	 * We have to:
1438e499d8b5Scube 	 *   - delete the alias
1439e499d8b5Scube 	 *
1440e499d8b5Scube 	 *      If the devi was an alias of an already listed devi, all is
1441e499d8b5Scube 	 *      good we don't have to do more.
1442e499d8b5Scube 	 *      If it was the first alias, we have to replace i's entry in
1443e499d8b5Scube 	 *      d's list by its first alias.
1444e499d8b5Scube 	 *      If it was the only entry, we must remove i's entry from d's
1445e499d8b5Scube 	 *      list.
1446e499d8b5Scube 	 */
14477b7c582aScube 	if (i != f) {
1448db7dbdacSuebayasi 		for (j = f; j->i_alias != i; j = j->i_alias)
1449db7dbdacSuebayasi 			continue;
14507b7c582aScube 		j->i_alias = i->i_alias;
14513b405775Scube 	} else {
14523b405775Scube 		if (i->i_alias == NULL) {
1453e499d8b5Scube 			/* No alias, must unlink the entry from devitab */
14547b7c582aScube 			ht_remove(devitab, i->i_name);
14557b7c582aScube 			j = i->i_bsame;
14563b405775Scube 		} else {
1457e499d8b5Scube 			/* Or have the first alias replace i in d's list */
1458e499d8b5Scube 			i->i_alias->i_bsame = i->i_bsame;
14597b7c582aScube 			j = i->i_alias;
14607b7c582aScube 			if (i == f)
14617b7c582aScube 				ht_replace(devitab, i->i_name, i->i_alias);
14623b405775Scube 		}
14633b405775Scube 
1464e499d8b5Scube 		/*
1465e499d8b5Scube 		 *   - remove/replace the instance from the devbase's list
1466e499d8b5Scube 		 *
1467e499d8b5Scube 		 * A double-linked list would make this much easier.  Oh, well,
1468e499d8b5Scube 		 * what is done is done.
1469e499d8b5Scube 		 */
1470e499d8b5Scube 		for (ppi = &d->d_ihead;
1471e499d8b5Scube 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1472db7dbdacSuebayasi 		    ppi = &(*ppi)->i_bsame)
1473db7dbdacSuebayasi 			continue;
1474e499d8b5Scube 		if (*ppi == NULL)
1475d25ad65eSchristos 			panic("%s: dev (%s) doesn't list the devi (%s at %s)",
1476d25ad65eSchristos 			    __func__, d->d_name, i->i_name, i->i_at);
14777b7c582aScube 		f = *ppi;
14787b7c582aScube 		if (f == i)
1479c3414672Scube 			/* That implies d->d_ihead == i */
14807b7c582aScube 			*ppi = j;
1481e499d8b5Scube 		else
14827b7c582aScube 			(*ppi)->i_bsame = j;
1483e499d8b5Scube 		if (d->d_ipp == &i->i_bsame) {
14843b405775Scube 			if (i->i_alias == NULL) {
14857b7c582aScube 				if (f == i)
1486e499d8b5Scube 					d->d_ipp = &d->d_ihead;
1487e499d8b5Scube 				else
14887b7c582aScube 					d->d_ipp = &f->i_bsame;
14893b405775Scube 			} else
14903b405775Scube 				d->d_ipp = &i->i_alias->i_bsame;
1491e499d8b5Scube 		}
1492e499d8b5Scube 	}
1493e499d8b5Scube 	/*
1494e499d8b5Scube 	 *   - delete the attachment instance
1495e499d8b5Scube 	 */
1496e499d8b5Scube 	iba = i->i_atdeva;
1497e499d8b5Scube 	for (ppi = &iba->d_ihead;
1498e499d8b5Scube 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1499db7dbdacSuebayasi 	    ppi = &(*ppi)->i_asame)
1500db7dbdacSuebayasi 		continue;
1501e499d8b5Scube 	if (*ppi == NULL)
1502d25ad65eSchristos 		panic("%s: deva (%s) doesn't list the devi (%s)", __func__,
1503e499d8b5Scube 		    iba->d_name, i->i_name);
15047b7c582aScube 	f = *ppi;
15057b7c582aScube 	if (f == i)
1506c3414672Scube 		/* That implies iba->d_ihead == i */
1507e499d8b5Scube 		*ppi = i->i_asame;
1508e499d8b5Scube 	else
1509e499d8b5Scube 		(*ppi)->i_asame = i->i_asame;
1510e499d8b5Scube 	if (iba->d_ipp == &i->i_asame) {
15117b7c582aScube 		if (f == i)
1512e499d8b5Scube 			iba->d_ipp = &iba->d_ihead;
1513e499d8b5Scube 		else
15147b7c582aScube 			iba->d_ipp = &f->i_asame;
1515e499d8b5Scube 	}
1516e499d8b5Scube 	/*
1517e499d8b5Scube 	 *   - delete the pspec
1518e499d8b5Scube 	 */
1519511fbc70Schristos 	remove_pspec(i);
1520e499d8b5Scube 
1521e499d8b5Scube 	/*
1522e499d8b5Scube 	 *   - delete the alldevi entry
1523e499d8b5Scube 	 */
1524e499d8b5Scube 	TAILQ_REMOVE(&alldevi, i, i_next);
1525e499d8b5Scube 	ndevi--;
15267aa6070dScube 	/*
15277aa6070dScube 	 * Put it in deaddevitab
1528a31ff6b4Scube 	 *
1529a31ff6b4Scube 	 * Each time a devi is removed, devilevel is increased so that later on
1530a31ff6b4Scube 	 * it is possible to tell if an instance was added before or after the
1531a31ff6b4Scube 	 * removal of its parent.
1532a31ff6b4Scube 	 *
1533a31ff6b4Scube 	 * For active instances, i_level contains the number of devi removed so
1534a31ff6b4Scube 	 * far, and for dead devis, it contains its index.
15357aa6070dScube 	 */
1536a31ff6b4Scube 	i->i_level = devilevel++;
15377aa6070dScube 	i->i_alias = NULL;
15387aa6070dScube 	f = ht_lookup(deaddevitab, i->i_name);
15397aa6070dScube 	if (f == NULL) {
15407aa6070dScube 		if (ht_insert(deaddevitab, i->i_name, i))
1541d25ad65eSchristos 			panic("%s: can't add %s to deaddevitab", __func__,
15427aa6070dScube 			    i->i_name);
15437aa6070dScube 	} else {
1544db7dbdacSuebayasi 		for (j = f; j->i_alias != NULL; j = j->i_alias)
1545db7dbdacSuebayasi 			continue;
15467aa6070dScube 		j->i_alias = i;
15477aa6070dScube 	}
1548e499d8b5Scube 	/*
15495d1e8b27Swiz 	 *   - reconstruct d->d_umax
1550e499d8b5Scube 	 */
1551e499d8b5Scube 	d->d_umax = 0;
1552e499d8b5Scube 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1553e499d8b5Scube 		if (i->i_unit >= d->d_umax)
1554e499d8b5Scube 			d->d_umax = i->i_unit + 1;
15555ecc953bSthorpej }
15565ecc953bSthorpej 
15575ecc953bSthorpej void
deldeva(const char * at,int nowarn)15583dbd1573Schristos deldeva(const char *at, int nowarn)
15597b7c582aScube {
15607b7c582aScube 	int unit;
15617b7c582aScube 	const char *cp;
15627b7c582aScube 	struct devbase *d, *ad;
15637b7c582aScube 	struct devi *i, *j;
15647b7c582aScube 	struct attr *a;
15657b7c582aScube 	struct pspec *p;
15667b7c582aScube 	struct nvlist *nv, *stack = NULL;
15677b7c582aScube 
15687b7c582aScube 	if (at == NULL) {
15697b7c582aScube 		TAILQ_FOREACH(i, &alldevi, i_next)
15707b7c582aScube 			if (i->i_at == NULL)
15717b7c582aScube 				stack = newnv(NULL, NULL, i, 0, stack);
15727b7c582aScube 	} else {
15739a7f4bbaSchristos 		size_t l;
15747b7c582aScube 
157575eb07f2Suebayasi 		CFGDBG(5, "deselecting deva `%s'", at);
15769a7f4bbaSchristos 		if (at[0] == '\0')
15779a7f4bbaSchristos 			goto out;
15789a7f4bbaSchristos 
15797b7c582aScube 		l = strlen(at) - 1;
15807b7c582aScube 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
15817b7c582aScube 			char base[NAMESIZE];
15827b7c582aScube 
15837b7c582aScube 			if (split(at, l+1, base, sizeof base, &unit)) {
15849a7f4bbaSchristos out:
1585c7295a4cSchristos 				cfgerror("invalid attachment name `%s'", at);
15867b7c582aScube 				return;
15877b7c582aScube 			}
15887b7c582aScube 			cp = intern(base);
15897b7c582aScube 		} else {
15907b7c582aScube 			cp = intern(at);
15917b7c582aScube 			unit = STAR;
15927b7c582aScube 		}
15937b7c582aScube 
15947b7c582aScube 		ad = ht_lookup(devbasetab, cp);
15957b7c582aScube 		a = ht_lookup(attrtab, cp);
15967b7c582aScube 		if (a == NULL) {
1597c7295a4cSchristos 			cfgerror("unknown attachment attribute or device `%s'",
15987b7c582aScube 			    cp);
15997b7c582aScube 			return;
16007b7c582aScube 		}
16017b7c582aScube 		if (!a->a_iattr) {
1602c7295a4cSchristos 			cfgerror("plain attribute `%s' cannot have children",
16037b7c582aScube 			    a->a_name);
16047b7c582aScube 			return;
16057b7c582aScube 		}
16067b7c582aScube 
16077b7c582aScube 		/*
16087b7c582aScube 		 * remove_devi() makes changes to the devbase's list and the
16097b7c582aScube 		 * alias list, * so the actual deletion of the instances must
16107b7c582aScube 		 * be delayed.
16117b7c582aScube 		 */
16127b7c582aScube 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
16137b7c582aScube 			d = nv->nv_ptr;
16147b7c582aScube 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
16157b7c582aScube 				for (j = i; j != NULL; j = j->i_alias) {
16167b7c582aScube 					/* Ignore devices at root */
16177b7c582aScube 					if (j->i_at == NULL)
16187b7c582aScube 						continue;
16197b7c582aScube 					p = j->i_pspec;
16207b7c582aScube 					/*
16217b7c582aScube 					 * There are three cases:
16227b7c582aScube 					 *
16237b7c582aScube 					 * 1.  unit is not STAR.  Consider 'at'
16247b7c582aScube 					 *     to be explicit, even if it
16257b7c582aScube 					 *     references an interface
16267b7c582aScube 					 *     attribute.
16277b7c582aScube 					 *
16287b7c582aScube 					 * 2.  unit is STAR and 'at' references
16297b7c582aScube 					 *     a real device.  Look for pspec
16307b7c582aScube 					 *     that have a matching p_atdev
16317b7c582aScube 					 *     field.
16327b7c582aScube 					 *
16337b7c582aScube 					 * 3.  unit is STAR and 'at' references
16347b7c582aScube 					 *     an interface attribute.  Look
16357b7c582aScube 					 *     for pspec that have a matching
16367b7c582aScube 					 *     p_iattr field.
16377b7c582aScube 					 */
16387b7c582aScube 					if ((unit != STAR &&        /* Case */
16397b7c582aScube 					     !strcmp(j->i_at, at)) ||  /* 1 */
16407b7c582aScube 					    (unit == STAR &&
16417b7c582aScube 					     ((ad != NULL &&        /* Case */
16427b7c582aScube 					       p->p_atdev == ad) ||    /* 2 */
16437b7c582aScube 					      (ad == NULL &&        /* Case */
16447b7c582aScube 					       p->p_iattr == a))))     /* 3 */
16457b7c582aScube 						stack = newnv(NULL, NULL, j, 0,
16467b7c582aScube 						    stack);
16477b7c582aScube 				}
16487b7c582aScube 		}
16497b7c582aScube 	}
16507b7c582aScube 
165111a68adeSchristos 	devcleanup(stack);
16527b7c582aScube }
16537b7c582aScube 
16547b7c582aScube void
deldev(const char * name,int nowarn)16553dbd1573Schristos deldev(const char *name, int nowarn)
16567b7c582aScube {
16579a7f4bbaSchristos 	size_t l;
16587b7c582aScube 	struct devi *firsti, *i;
165911a68adeSchristos 	struct nvlist *stack = NULL;
16607b7c582aScube 
166175eb07f2Suebayasi 	CFGDBG(5, "deselecting dev `%s'", name);
16629a7f4bbaSchristos 	if (name[0] == '\0')
16639a7f4bbaSchristos 		goto out;
16649a7f4bbaSchristos 
16657b7c582aScube 	l = strlen(name) - 1;
16667b7c582aScube 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
16677b7c582aScube 		/* `no mydev0' or `no mydev*' */
16687b7c582aScube 		firsti = ht_lookup(devitab, name);
16697b7c582aScube 		if (firsti == NULL) {
16709a7f4bbaSchristos out:
16713dbd1573Schristos 			if (!nowarn)
1672c7295a4cSchristos 				cfgerror("unknown instance %s", name);
16737b7c582aScube 			return;
16747b7c582aScube 		}
16757b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
16767b7c582aScube 			stack = newnv(NULL, NULL, i, 0, stack);
16777b7c582aScube 	} else {
16787b7c582aScube 		struct devbase *d = ht_lookup(devbasetab, name);
16797b7c582aScube 
16807b7c582aScube 		if (d == NULL) {
1681c7295a4cSchristos 			cfgerror("unknown device %s", name);
16827b7c582aScube 			return;
16837b7c582aScube 		}
168471af9028Scube 		if (d->d_ispseudo) {
1685c7295a4cSchristos 			cfgerror("%s is a pseudo-device; "
168671af9028Scube 			    "use \"no pseudo-device %s\" instead", name,
168771af9028Scube 			    name);
168871af9028Scube 			return;
168971af9028Scube 		}
169011a68adeSchristos 		stack = makedevstack(d);
16917b7c582aScube 	}
16927b7c582aScube 
169311a68adeSchristos 	devcleanup(stack);
16947b7c582aScube }
16957b7c582aScube 
169600ed8a38Spooka /*
169700ed8a38Spooka  * Insert given device "name" into devroottab.  In case "name"
169800ed8a38Spooka  * designates a pure interface attribute, create a fake device
169900ed8a38Spooka  * instance for the attribute and insert that into the roottab
170000ed8a38Spooka  * (this scheme avoids mucking around with the orphanage analysis).
170100ed8a38Spooka  */
17027b7c582aScube void
addpseudoroot(const char * name)170390ac64deSpooka addpseudoroot(const char *name)
170490ac64deSpooka {
1705194e1c80Spooka 	char buf[NAMESIZE];
1706194e1c80Spooka 	int unit;
1707194e1c80Spooka 	struct attr *attr;
170890ac64deSpooka 	struct devi *i;
170990ac64deSpooka 	struct deva *iba;
171090ac64deSpooka 	struct devbase *ib;
171190ac64deSpooka 
1712194e1c80Spooka 	if (split(name, strlen(name), buf, sizeof(buf), &unit)) {
1713194e1c80Spooka 		cfgerror("invalid pseudo-root name `%s'", name);
1714194e1c80Spooka 		return;
1715194e1c80Spooka 	}
1716194e1c80Spooka 
1717194e1c80Spooka 	/*
171800ed8a38Spooka 	 * Prefer device because devices with locators define an
171900ed8a38Spooka 	 * implicit interface attribute.  However, if a device is
172000ed8a38Spooka 	 * not available, try to attach to the interface attribute.
172100ed8a38Spooka 	 * This makes sure adddev() doesn't get confused when we
172200ed8a38Spooka 	 * are really attaching to a device (alternatively we maybe
172300ed8a38Spooka 	 * could specify a non-NULL atlist to defdevattach() below).
1724194e1c80Spooka 	 */
172500ed8a38Spooka 	ib = ht_lookup(devbasetab, intern(buf));
172600ed8a38Spooka 	if (ib == NULL) {
1727194e1c80Spooka 		struct devbase *fakedev;
1728194e1c80Spooka 		char fakename[NAMESIZE];
1729194e1c80Spooka 
173000ed8a38Spooka 		attr = ht_lookup(attrtab, intern(buf));
173100ed8a38Spooka 		if (!(attr && attr->a_iattr)) {
173200ed8a38Spooka 			cfgerror("pseudo-root `%s' not available", name);
173300ed8a38Spooka 			return;
173400ed8a38Spooka 		}
173500ed8a38Spooka 
1736194e1c80Spooka 		/*
1737194e1c80Spooka 		 * here we cheat a bit: create a fake devbase with the
1738194e1c80Spooka 		 * interface attribute and instantiate it.  quick, cheap,
1739194e1c80Spooka 		 * dirty & bad for you, much like the stuff in the fridge.
1740194e1c80Spooka 		 * and, it works, since the pseudoroot device is not included
1741194e1c80Spooka 		 * in ioconf, just used by config to make sure we start from
1742194e1c80Spooka 		 * the right place.
1743194e1c80Spooka 		 */
174400ed8a38Spooka 		snprintf(fakename, sizeof(fakename), "%s_devattrs", buf);
1745194e1c80Spooka 		fakedev = getdevbase(intern(fakename));
1746194e1c80Spooka 		fakedev->d_isdef = 1;
1747194e1c80Spooka 		fakedev->d_ispseudo = 0;
17484caf067bSdholland 		fakedev->d_attrs = attrlist_cons(NULL, attr);
1749194e1c80Spooka 		defdevattach(NULL, fakedev, NULL, NULL);
1750194e1c80Spooka 
1751194e1c80Spooka 		if (unit == STAR)
1752194e1c80Spooka 			snprintf(buf, sizeof(buf), "%s*", fakename);
1753194e1c80Spooka 		else
1754194e1c80Spooka 			snprintf(buf, sizeof(buf), "%s%d", fakename, unit);
1755194e1c80Spooka 		name = buf;
1756194e1c80Spooka 	}
1757194e1c80Spooka 
1758194e1c80Spooka 	/* ok, everything should be set up, so instantiate a fake device */
175990ac64deSpooka 	i = getdevi(name);
176090ac64deSpooka 	if (i == NULL)
1761d25ad65eSchristos 		panic("%s: device `%s' expected to be present", __func__,
1762d25ad65eSchristos 		    name);
176390ac64deSpooka 	ib = i->i_base;
1764194e1c80Spooka 	iba = ib->d_ahead;
176590ac64deSpooka 
176690ac64deSpooka 	i->i_atdeva = iba;
176790ac64deSpooka 	i->i_cfflags = 0;
176890ac64deSpooka 	i->i_locs = fixloc(name, &errattr, NULL);
176990ac64deSpooka 	i->i_pseudoroot = 1;
177090ac64deSpooka 	i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */
177190ac64deSpooka 
177290ac64deSpooka 	*iba->d_ipp = i;
177390ac64deSpooka 	iba->d_ipp = &i->i_asame;
177490ac64deSpooka 
177590ac64deSpooka 	ht_insert(devroottab, ib->d_name, ib);
177690ac64deSpooka }
177790ac64deSpooka 
177811a68adeSchristos static void
deldevbase(struct devbase * d)177911a68adeSchristos deldevbase(struct devbase *d)
178011a68adeSchristos {
178111a68adeSchristos 	struct devi *i;
178211a68adeSchristos 	const char *name = d->d_name;
178311a68adeSchristos 
178411a68adeSchristos 	if (!d->d_ispseudo) {
178511a68adeSchristos 		devcleanup(makedevstack(d));
178611a68adeSchristos 		return;
178711a68adeSchristos 	}
178811a68adeSchristos 
178911a68adeSchristos 	if ((i = ht_lookup(devitab, name)) == NULL)
179011a68adeSchristos 		return;
179111a68adeSchristos 
179211a68adeSchristos 	d->d_umax = 0;		/* clear neads-count entries */
179311a68adeSchristos 	d->d_ihead = NULL;	/* make sure it won't be considered active */
179411a68adeSchristos 	TAILQ_REMOVE(&allpseudo, i, i_next);
179511a68adeSchristos 	if (ht_remove(devitab, name))
1796d25ad65eSchristos 		panic("%s: Can't remove %s from devitab", __func__, name);
179711a68adeSchristos 	if (ht_insert(deaddevitab, name, i))
1798d25ad65eSchristos 		panic("%s: Can't add %s to deaddevitab", __func__, name);
179911a68adeSchristos }
180011a68adeSchristos 
180190ac64deSpooka void
addpseudo(const char * name,int number)18025ecc953bSthorpej addpseudo(const char *name, int number)
18035ecc953bSthorpej {
18045ecc953bSthorpej 	struct devbase *d;
18055ecc953bSthorpej 	struct devi *i;
18065ecc953bSthorpej 
18075ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
18085ecc953bSthorpej 	if (d == NULL) {
1809c7295a4cSchristos 		cfgerror("undefined pseudo-device %s", name);
18105ecc953bSthorpej 		return;
18115ecc953bSthorpej 	}
18125ecc953bSthorpej 	if (!d->d_ispseudo) {
1813c7295a4cSchristos 		cfgerror("%s is a real device, not a pseudo-device", name);
18145ecc953bSthorpej 		return;
18155ecc953bSthorpej 	}
1816f079f926Schristos 	if ((i = ht_lookup(devitab, name)) != NULL) {
1817f079f926Schristos 		cfgerror("`%s' already defined at %s:%hu", name,
1818f079f926Schristos 		    i->i_where.w_srcfile, i->i_where.w_srcline);
18195ecc953bSthorpej 		return;
18205ecc953bSthorpej 	}
18215ecc953bSthorpej 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
18225ecc953bSthorpej 	if (ht_insert(devitab, name, i))
1823d25ad65eSchristos 		panic("%s: %s", __func__, name);
18247b7c582aScube 	/* Useful to retrieve the instance from the devbase */
18257b7c582aScube 	d->d_ihead = i;
18267aa6070dScube 	i->i_active = DEVI_ACTIVE;
18275ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
18285ecc953bSthorpej }
18295ecc953bSthorpej 
18305ecc953bSthorpej void
delpseudo(const char * name,int nowarn)18313dbd1573Schristos delpseudo(const char *name, int nowarn)
18325ecc953bSthorpej {
18335ecc953bSthorpej 	struct devbase *d;
18345ecc953bSthorpej 
183575eb07f2Suebayasi 	CFGDBG(5, "deselecting pseudo `%s'", name);
18365ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
18375ecc953bSthorpej 	if (d == NULL) {
18383dbd1573Schristos 		if (!nowarn)
1839c7295a4cSchristos 			cfgerror("undefined pseudo-device %s", name);
18405ecc953bSthorpej 		return;
18415ecc953bSthorpej 	}
18425ecc953bSthorpej 	if (!d->d_ispseudo) {
1843c7295a4cSchristos 		cfgerror("%s is a real device, not a pseudo-device", name);
18445ecc953bSthorpej 		return;
18455ecc953bSthorpej 	}
184611a68adeSchristos 	deldevbase(d);
18475ecc953bSthorpej }
18485ecc953bSthorpej 
18495ecc953bSthorpej void
adddevm(const char * name,devmajor_t cmajor,devmajor_t bmajor,struct condexpr * cond,struct nvlist * nv_nodes)1850d767912bSdrochner adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
18519483bda7Sdholland 	struct condexpr *cond, struct nvlist *nv_nodes)
18525ecc953bSthorpej {
18535ecc953bSthorpej 	struct devm *dm;
18545ecc953bSthorpej 
1855d767912bSdrochner 	if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
1856c7295a4cSchristos 		cfgerror("character major %d is invalid", cmajor);
18579483bda7Sdholland 		condexpr_destroy(cond);
18583da3ab25Spooka 		nvfreel(nv_nodes);
18595ecc953bSthorpej 		return;
18605ecc953bSthorpej 	}
18615ecc953bSthorpej 
1862d767912bSdrochner 	if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
1863c7295a4cSchristos 		cfgerror("block major %d is invalid", bmajor);
18649483bda7Sdholland 		condexpr_destroy(cond);
18653da3ab25Spooka 		nvfreel(nv_nodes);
18665ecc953bSthorpej 		return;
18675ecc953bSthorpej 	}
1868d767912bSdrochner 	if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
1869c7295a4cSchristos 		cfgerror("both character/block majors are not specified");
18709483bda7Sdholland 		condexpr_destroy(cond);
18713da3ab25Spooka 		nvfreel(nv_nodes);
18725ecc953bSthorpej 		return;
18735ecc953bSthorpej 	}
18745ecc953bSthorpej 
18755ecc953bSthorpej 	dm = ecalloc(1, sizeof(*dm));
1876f079f926Schristos 	dm->dm_where.w_srcfile = yyfile;
1877f079f926Schristos 	dm->dm_where.w_srcline = currentline();
18785ecc953bSthorpej 	dm->dm_name = name;
18795ecc953bSthorpej 	dm->dm_cmajor = cmajor;
18805ecc953bSthorpej 	dm->dm_bmajor = bmajor;
18819483bda7Sdholland 	dm->dm_opts = cond;
18823da3ab25Spooka 	dm->dm_devnodes = nv_nodes;
18835ecc953bSthorpej 
18845ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
18855ecc953bSthorpej 
18865ecc953bSthorpej 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
18875ecc953bSthorpej 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
18885ecc953bSthorpej }
18895ecc953bSthorpej 
18907aa6070dScube int
fixdevis(void)18915ecc953bSthorpej fixdevis(void)
18925ecc953bSthorpej {
1893d25ad65eSchristos 	const char *msg;
18945ecc953bSthorpej 	struct devi *i;
1895511fbc70Schristos 	struct pspec *p;
18967aa6070dScube 	int error = 0;
18975ecc953bSthorpej 
1898bc707469Suebayasi 	TAILQ_FOREACH(i, &alldevi, i_next) {
1899bc707469Suebayasi 		CFGDBG(3, "fixing devis `%s'", i->i_name);
19007aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
19015ecc953bSthorpej 			selectbase(i->i_base, i->i_atdeva);
19027aa6070dScube 		else if (i->i_active == DEVI_ORPHAN) {
1903c130d400Scube 			/*
1904c130d400Scube 			 * At this point, we can't have instances for which
1905c130d400Scube 			 * i_at or i_pspec are NULL.
1906c130d400Scube 			 */
19077aa6070dScube 			++error;
1908511fbc70Schristos 			p = i->i_pspec;
1909d25ad65eSchristos 			msg = p == NULL ? "no parent" :
1910d25ad65eSchristos 			    (p->p_atunit == WILD ? "nothing matching" : "no");
1911f079f926Schristos 			cfgxerror(i->i_where.w_srcfile, i->i_where.w_srcline,
19120dbd1c0eScube 			    "`%s at %s' is orphaned (%s `%s' found)",
1913d25ad65eSchristos 			    i->i_name, i->i_at, msg, i->i_at);
19147aa6070dScube 		} else if (vflag && i->i_active == DEVI_IGNORED)
1915f079f926Schristos 			cfgxwarn(i->i_where.w_srcfile, i->i_where.w_srcline, "ignoring "
1916c7295a4cSchristos 			    "explicitly orphaned instance `%s at %s'",
1917c7295a4cSchristos 			    i->i_name, i->i_at);
1918bc707469Suebayasi 	}
19197aa6070dScube 
19207aa6070dScube 	if (error)
19217aa6070dScube 		return error;
19225ecc953bSthorpej 
19235ecc953bSthorpej 	TAILQ_FOREACH(i, &allpseudo, i_next)
19247aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
19255ecc953bSthorpej 			selectbase(i->i_base, NULL);
19267aa6070dScube 	return 0;
19275ecc953bSthorpej }
19285ecc953bSthorpej 
19295ecc953bSthorpej /*
19305ecc953bSthorpej  * Look up a parent spec, creating a new one if it does not exist.
19315ecc953bSthorpej  */
19325ecc953bSthorpej static struct pspec *
getpspec(struct attr * attr,struct devbase * ab,int atunit,int first)19334b9e5cb1Schristos getpspec(struct attr *attr, struct devbase *ab, int atunit, int first)
19345ecc953bSthorpej {
19355ecc953bSthorpej 	struct pspec *p;
193653be83e0Schristos 	int inst = npspecs;
19374b9e5cb1Schristos 	int ref = 1;
19385ecc953bSthorpej 
19395ecc953bSthorpej 	TAILQ_FOREACH(p, &allpspecs, p_list) {
194053be83e0Schristos 		if (p->p_iattr == attr && p->p_atdev == ab &&
194153be83e0Schristos 		    p->p_atunit == atunit) {
19424b9e5cb1Schristos 			p->p_ref++;
19434b9e5cb1Schristos 			if (first)
19444b9e5cb1Schristos 				return p;
19454b9e5cb1Schristos 			else {
194653be83e0Schristos 				inst = p->p_inst;
19474b9e5cb1Schristos 				ref = p->p_ref;
19484b9e5cb1Schristos 			}
19494b9e5cb1Schristos 
19505ecc953bSthorpej 		}
195153be83e0Schristos 	}
19525ecc953bSthorpej 
19535ecc953bSthorpej 	p = ecalloc(1, sizeof(*p));
19545ecc953bSthorpej 
19555ecc953bSthorpej 	p->p_iattr = attr;
19565ecc953bSthorpej 	p->p_atdev = ab;
19575ecc953bSthorpej 	p->p_atunit = atunit;
195853be83e0Schristos 	p->p_inst = inst;
195953be83e0Schristos 	if (inst == npspecs)
196053be83e0Schristos 		npspecs++;
1961c130d400Scube 	p->p_active = 0;
19624b9e5cb1Schristos 	p->p_ref = ref;
19635ecc953bSthorpej 
19645ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
19655ecc953bSthorpej 
19665ecc953bSthorpej 	return (p);
19675ecc953bSthorpej }
19685ecc953bSthorpej 
19695ecc953bSthorpej /*
19705ecc953bSthorpej  * Define a new instance of a specific device.
19715ecc953bSthorpej  */
19725ecc953bSthorpej static struct devi *
getdevi(const char * name)19735ecc953bSthorpej getdevi(const char *name)
19745ecc953bSthorpej {
19755ecc953bSthorpej 	struct devi *i, *firsti;
19765ecc953bSthorpej 	struct devbase *d;
19775ecc953bSthorpej 	int unit;
19785ecc953bSthorpej 	char base[NAMESIZE];
19795ecc953bSthorpej 
19805ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1981c7295a4cSchristos 		cfgerror("invalid device name `%s'", name);
19825ecc953bSthorpej 		return (NULL);
19835ecc953bSthorpej 	}
19845ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
19855ecc953bSthorpej 	if (d == NULL) {
1986c7295a4cSchristos 		cfgerror("%s: unknown device `%s'", name, base);
19875ecc953bSthorpej 		return (NULL);
19885ecc953bSthorpej 	}
19895ecc953bSthorpej 	if (d->d_ispseudo) {
1990c7295a4cSchristos 		cfgerror("%s: %s is a pseudo-device", name, base);
19915ecc953bSthorpej 		return (NULL);
19925ecc953bSthorpej 	}
19935ecc953bSthorpej 	firsti = ht_lookup(devitab, name);
19945ecc953bSthorpej 	i = newdevi(name, unit, d);
19955ecc953bSthorpej 	if (firsti == NULL) {
19965ecc953bSthorpej 		if (ht_insert(devitab, name, i))
1997d25ad65eSchristos 			panic("%s: %s", __func__, name);
19985ecc953bSthorpej 		*d->d_ipp = i;
19995ecc953bSthorpej 		d->d_ipp = &i->i_bsame;
20005ecc953bSthorpej 	} else {
20015ecc953bSthorpej 		while (firsti->i_alias)
20025ecc953bSthorpej 			firsti = firsti->i_alias;
20035ecc953bSthorpej 		firsti->i_alias = i;
20045ecc953bSthorpej 	}
20055ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
20065ecc953bSthorpej 	ndevi++;
20075ecc953bSthorpej 	return (i);
20085ecc953bSthorpej }
20095ecc953bSthorpej 
20105ecc953bSthorpej static const char *
concat(const char * name,int c)20115ecc953bSthorpej concat(const char *name, int c)
20125ecc953bSthorpej {
2013c7295a4cSchristos 	size_t len;
20145ecc953bSthorpej 	char buf[NAMESIZE];
20155ecc953bSthorpej 
20165ecc953bSthorpej 	len = strlen(name);
20175ecc953bSthorpej 	if (len + 2 > sizeof(buf)) {
2018c7295a4cSchristos 		cfgerror("device name `%s%c' too long", name, c);
20195ecc953bSthorpej 		len = sizeof(buf) - 2;
20205ecc953bSthorpej 	}
20215ecc953bSthorpej 	memmove(buf, name, len);
20229a7f4bbaSchristos 	buf[len] = (char)c;
20239a7f4bbaSchristos 	buf[len + 1] = '\0';
20245ecc953bSthorpej 	return (intern(buf));
20255ecc953bSthorpej }
20265ecc953bSthorpej 
20275ecc953bSthorpej const char *
starref(const char * name)20285ecc953bSthorpej starref(const char *name)
20295ecc953bSthorpej {
20305ecc953bSthorpej 
20315ecc953bSthorpej 	return (concat(name, '*'));
20325ecc953bSthorpej }
20335ecc953bSthorpej 
20345ecc953bSthorpej const char *
wildref(const char * name)20355ecc953bSthorpej wildref(const char *name)
20365ecc953bSthorpej {
20375ecc953bSthorpej 
20385ecc953bSthorpej 	return (concat(name, '?'));
20395ecc953bSthorpej }
20405ecc953bSthorpej 
20415ecc953bSthorpej /*
20425ecc953bSthorpej  * Split a name like "foo0" into base name (foo) and unit number (0).
20435ecc953bSthorpej  * Return 0 on success.  To make this useful for names like "foo0a",
20445ecc953bSthorpej  * the length of the "foo0" part is one of the arguments.
20455ecc953bSthorpej  */
20465ecc953bSthorpej static int
split(const char * name,size_t nlen,char * base,size_t bsize,int * aunit)20475ecc953bSthorpej split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
20485ecc953bSthorpej {
20495ecc953bSthorpej 	const char *cp;
2050c7295a4cSchristos 	int c;
2051c7295a4cSchristos 	size_t l;
20525ecc953bSthorpej 
20535ecc953bSthorpej 	l = nlen;
20545ecc953bSthorpej 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
20555ecc953bSthorpej 		return (1);
20565ecc953bSthorpej 	c = (u_char)name[--l];
20575ecc953bSthorpej 	if (!isdigit(c)) {
20585ecc953bSthorpej 		if (c == '*')
20595ecc953bSthorpej 			*aunit = STAR;
20605ecc953bSthorpej 		else if (c == '?')
20615ecc953bSthorpej 			*aunit = WILD;
20625ecc953bSthorpej 		else
20635ecc953bSthorpej 			return (1);
20645ecc953bSthorpej 	} else {
20655ecc953bSthorpej 		cp = &name[l];
20665ecc953bSthorpej 		while (isdigit((unsigned char)cp[-1]))
20675ecc953bSthorpej 			l--, cp--;
20685ecc953bSthorpej 		*aunit = atoi(cp);
20695ecc953bSthorpej 	}
20705ecc953bSthorpej 	memmove(base, name, l);
20715ecc953bSthorpej 	base[l] = 0;
20725ecc953bSthorpej 	return (0);
20735ecc953bSthorpej }
20745ecc953bSthorpej 
20755ecc953bSthorpej void
addattr(const char * name)20768d928a75Suebayasi addattr(const char *name)
20778d928a75Suebayasi {
20788d928a75Suebayasi 	struct attr *a;
20798d928a75Suebayasi 
20808d928a75Suebayasi 	a = refattr(name);
20818d928a75Suebayasi 	selectattr(a);
20828d928a75Suebayasi }
20838d928a75Suebayasi 
20848d928a75Suebayasi void
delattr(const char * name,int nowarn)20853dbd1573Schristos delattr(const char *name, int nowarn)
20868d928a75Suebayasi {
20878d928a75Suebayasi 	struct attr *a;
20888d928a75Suebayasi 
20898d928a75Suebayasi 	a = refattr(name);
20908d928a75Suebayasi 	deselectattr(a);
20918d928a75Suebayasi }
20928d928a75Suebayasi 
20938d928a75Suebayasi void
selectattr(struct attr * a)20945ecc953bSthorpej selectattr(struct attr *a)
20955ecc953bSthorpej {
20963a03bee6Suebayasi 	struct attrlist *al;
20973a03bee6Suebayasi 	struct attr *dep;
20985ecc953bSthorpej 
20998d928a75Suebayasi 	CFGDBG(5, "selecting attr `%s'", a->a_name);
21003a03bee6Suebayasi 	for (al = a->a_deps; al != NULL; al = al->al_next) {
21013a03bee6Suebayasi 		dep = al->al_this;
21023a03bee6Suebayasi 		selectattr(dep);
21033a03bee6Suebayasi 	}
2104d6687201Suebayasi 	if (ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)) == 0)
2105d6687201Suebayasi 		nattrs++;
2106b599bf99Suebayasi 	CFGDBG(3, "attr selected `%s'", a->a_name);
21075ecc953bSthorpej }
21085ecc953bSthorpej 
21096d1bba4aSuebayasi static int
deselectattrcb2(const char * name1,const char * name2,void * v,void * arg)21108d928a75Suebayasi deselectattrcb2(const char *name1, const char *name2, void *v, void *arg)
21118d928a75Suebayasi {
211211a68adeSchristos 	struct attr *a = arg;
211311a68adeSchristos 	const char *name = a->a_name;
211411a68adeSchristos 	struct vtype *vt = v;
21158d928a75Suebayasi 
211611a68adeSchristos 	if (strcmp(name, name2) == 0) {
21173dbd1573Schristos 		delattr(name1, 0);
21188d928a75Suebayasi 		return 0;
21198d928a75Suebayasi 	}
21208d928a75Suebayasi 
212111a68adeSchristos 	if (!vt->attr->a_deselected)
212211a68adeSchristos 		return 0;
212311a68adeSchristos 
212411a68adeSchristos 	switch (vt->type) {
212511a68adeSchristos 	case V_ATTRIBUTE:
212611a68adeSchristos #ifdef notyet
212711a68adeSchristos 		// XXX: Loops
212811a68adeSchristos 		deselectattr(vt->value);
212911a68adeSchristos #endif
213011a68adeSchristos 		break;
213111a68adeSchristos 	case V_DEVICE:
213211a68adeSchristos 		CFGDBG(5, "removing device `%s' with attr `%s' because attr `%s'"
213311a68adeSchristos 		    " is deselected", name1, name2, name);
213411a68adeSchristos 		deldevbase(vt->value);
213511a68adeSchristos 		break;
213611a68adeSchristos 	default:
213711a68adeSchristos 		abort();
213811a68adeSchristos 	}
213911a68adeSchristos 	return 0;
214011a68adeSchristos }
214111a68adeSchristos 
21428d928a75Suebayasi void
deselectattr(struct attr * a)21438d928a75Suebayasi deselectattr(struct attr *a)
21448d928a75Suebayasi {
21458d928a75Suebayasi 	CFGDBG(5, "deselecting attr `%s'", a->a_name);
214611a68adeSchristos 	a->a_deselected = 1;
214711a68adeSchristos 	ht_enumerate2(attrdeptab, deselectattrcb2, a);
2148d6687201Suebayasi 	if (ht_remove(selecttab, a->a_name) == 0)
2149d6687201Suebayasi 		nattrs--;
21508d928a75Suebayasi 	CFGDBG(3, "attr deselected `%s'", a->a_name);
21518d928a75Suebayasi }
21528d928a75Suebayasi 
21538d928a75Suebayasi static int
dumpattrdepcb2(const char * name1,const char * name2,void * v,void * arg)21546d1bba4aSuebayasi dumpattrdepcb2(const char *name1, const char *name2, void *v, void *arg)
21556d1bba4aSuebayasi {
21566d1bba4aSuebayasi 
21576d1bba4aSuebayasi 	CFGDBG(3, "attr `%s' depends on attr `%s'", name1, name2);
21586d1bba4aSuebayasi 	return 0;
21596d1bba4aSuebayasi }
21606d1bba4aSuebayasi 
21616d1bba4aSuebayasi void
dependattrs(void)21626d1bba4aSuebayasi dependattrs(void)
21636d1bba4aSuebayasi {
21646d1bba4aSuebayasi 
21656d1bba4aSuebayasi 	ht_enumerate2(attrdeptab, dumpattrdepcb2, NULL);
21666d1bba4aSuebayasi }
21676d1bba4aSuebayasi 
21685ecc953bSthorpej /*
21695ecc953bSthorpej  * We have an instance of the base foo, so select it and all its
21705ecc953bSthorpej  * attributes for "optional foo".
21715ecc953bSthorpej  */
21725ecc953bSthorpej static void
selectbase(struct devbase * d,struct deva * da)21735ecc953bSthorpej selectbase(struct devbase *d, struct deva *da)
21745ecc953bSthorpej {
21755ecc953bSthorpej 	struct attr *a;
21764caf067bSdholland 	struct attrlist *al;
21775ecc953bSthorpej 
2178c7295a4cSchristos 	(void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
2179b599bf99Suebayasi 	CFGDBG(3, "devbase selected `%s'", d->d_name);
21806d1bba4aSuebayasi 	CFGDBG(5, "selecting dependencies of devbase `%s'", d->d_name);
21814caf067bSdholland 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
21824caf067bSdholland 		a = al->al_this;
21835ecc953bSthorpej 		expandattr(a, selectattr);
21845ecc953bSthorpej 	}
2185da706167Suebayasi 
2186da706167Suebayasi 	struct attr *devattr;
2187da706167Suebayasi 	devattr = refattr(d->d_name);
2188da706167Suebayasi 	expandattr(devattr, selectattr);
2189da706167Suebayasi 
21905ecc953bSthorpej 	if (da != NULL) {
2191c7295a4cSchristos 		(void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
2192b599bf99Suebayasi 		CFGDBG(3, "devattr selected `%s'", da->d_name);
21934caf067bSdholland 		for (al = da->d_attrs; al != NULL; al = al->al_next) {
21944caf067bSdholland 			a = al->al_this;
21955ecc953bSthorpej 			expandattr(a, selectattr);
21965ecc953bSthorpej 		}
21975ecc953bSthorpej 	}
2198c4bac1d0Suebayasi 
2199c4bac1d0Suebayasi 	fixdev(d);
22005ecc953bSthorpej }
22015ecc953bSthorpej 
22025ecc953bSthorpej /*
22035ecc953bSthorpej  * Is the given pointer on the given list of pointers?
22045ecc953bSthorpej  */
220559c94545Scube int
onlist(struct nvlist * nv,void * ptr)22065ecc953bSthorpej onlist(struct nvlist *nv, void *ptr)
22075ecc953bSthorpej {
22085ecc953bSthorpej 	for (; nv != NULL; nv = nv->nv_next)
22095ecc953bSthorpej 		if (nv->nv_ptr == ptr)
22105ecc953bSthorpej 			return (1);
22115ecc953bSthorpej 	return (0);
22125ecc953bSthorpej }
22135ecc953bSthorpej 
22145ecc953bSthorpej static char *
extend(char * p,const char * name)22155ecc953bSthorpej extend(char *p, const char *name)
22165ecc953bSthorpej {
2217c7295a4cSchristos 	size_t l;
22185ecc953bSthorpej 
22195ecc953bSthorpej 	l = strlen(name);
22205ecc953bSthorpej 	memmove(p, name, l);
22215ecc953bSthorpej 	p += l;
22225ecc953bSthorpej 	*p++ = ',';
22235ecc953bSthorpej 	*p++ = ' ';
22245ecc953bSthorpej 	return (p);
22255ecc953bSthorpej }
22265ecc953bSthorpej 
22275ecc953bSthorpej /*
22285ecc953bSthorpej  * Check that we got all required locators, and default any that are
22295ecc953bSthorpej  * given as "?" and have defaults.  Return 0 on success.
22305ecc953bSthorpej  */
22315ecc953bSthorpej static const char **
fixloc(const char * name,struct attr * attr,struct loclist * got)223216a8771bSdholland fixloc(const char *name, struct attr *attr, struct loclist *got)
22335ecc953bSthorpej {
223416a8771bSdholland 	struct loclist *m, *n;
22355ecc953bSthorpej 	int ord;
22365ecc953bSthorpej 	const char **lp;
22375ecc953bSthorpej 	int nmissing, nextra, nnodefault;
22385ecc953bSthorpej 	char *mp, *ep, *ndp;
22395ecc953bSthorpej 	char missing[1000], extra[1000], nodefault[1000];
22405ecc953bSthorpej 	static const char *nullvec[1];
22415ecc953bSthorpej 
22425ecc953bSthorpej 	/*
22435ecc953bSthorpej 	 * Look for all required locators, and number the given ones
22445ecc953bSthorpej 	 * according to the required order.  While we are numbering,
22455ecc953bSthorpej 	 * set default values for defaulted locators.
22465ecc953bSthorpej 	 */
22475ecc953bSthorpej 	if (attr->a_loclen == 0)	/* e.g., "at root" */
22485ecc953bSthorpej 		lp = nullvec;
22495ecc953bSthorpej 	else
22509a7f4bbaSchristos 		lp = emalloc((size_t)(attr->a_loclen + 1) * sizeof(const char *));
225116a8771bSdholland 	for (n = got; n != NULL; n = n->ll_next)
225216a8771bSdholland 		n->ll_num = -1;
22535ecc953bSthorpej 	nmissing = 0;
22545ecc953bSthorpej 	mp = missing;
22555ecc953bSthorpej 	/* yes, this is O(mn), but m and n should be small */
225616a8771bSdholland 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) {
225716a8771bSdholland 		for (n = got; n != NULL; n = n->ll_next) {
225816a8771bSdholland 			if (n->ll_name == m->ll_name) {
225916a8771bSdholland 				n->ll_num = ord;
22605ecc953bSthorpej 				break;
22615ecc953bSthorpej 			}
22625ecc953bSthorpej 		}
226316a8771bSdholland 		if (n == NULL && m->ll_num == 0) {
22645ecc953bSthorpej 			nmissing++;
226516a8771bSdholland 			mp = extend(mp, m->ll_name);
22665ecc953bSthorpej 		}
226716a8771bSdholland 		lp[ord] = m->ll_string;
22685ecc953bSthorpej 	}
22695ecc953bSthorpej 	if (ord != attr->a_loclen)
2270d25ad65eSchristos 		panic("%s: bad length", __func__);
22715ecc953bSthorpej 	lp[ord] = NULL;
22725ecc953bSthorpej 	nextra = 0;
22735ecc953bSthorpej 	ep = extra;
22745ecc953bSthorpej 	nnodefault = 0;
22755ecc953bSthorpej 	ndp = nodefault;
227616a8771bSdholland 	for (n = got; n != NULL; n = n->ll_next) {
227716a8771bSdholland 		if (n->ll_num >= 0) {
227816a8771bSdholland 			if (n->ll_string != NULL)
227916a8771bSdholland 				lp[n->ll_num] = n->ll_string;
228016a8771bSdholland 			else if (lp[n->ll_num] == NULL) {
22815ecc953bSthorpej 				nnodefault++;
228216a8771bSdholland 				ndp = extend(ndp, n->ll_name);
22835ecc953bSthorpej 			}
22845ecc953bSthorpej 		} else {
22855ecc953bSthorpej 			nextra++;
228616a8771bSdholland 			ep = extend(ep, n->ll_name);
22875ecc953bSthorpej 		}
22885ecc953bSthorpej 	}
22895ecc953bSthorpej 	if (nextra) {
22905ecc953bSthorpej 		ep[-2] = 0;	/* kill ", " */
2291c7295a4cSchristos 		cfgerror("%s: extraneous locator%s: %s",
22925ecc953bSthorpej 		    name, nextra > 1 ? "s" : "", extra);
22935ecc953bSthorpej 	}
22945ecc953bSthorpej 	if (nmissing) {
22955ecc953bSthorpej 		mp[-2] = 0;
2296c7295a4cSchristos 		cfgerror("%s: must specify %s", name, missing);
22975ecc953bSthorpej 	}
22985ecc953bSthorpej 	if (nnodefault) {
22995ecc953bSthorpej 		ndp[-2] = 0;
2300c7295a4cSchristos 		cfgerror("%s: cannot wildcard %s", name, nodefault);
23015ecc953bSthorpej 	}
23025ecc953bSthorpej 	if (nmissing || nnodefault) {
23035ecc953bSthorpej 		free(lp);
23045ecc953bSthorpej 		lp = NULL;
23055ecc953bSthorpej 	}
23065ecc953bSthorpej 	return (lp);
23075ecc953bSthorpej }
2308437f8925Scube 
2309437f8925Scube void
setversion(int newver)2310437f8925Scube setversion(int newver)
2311437f8925Scube {
2312437f8925Scube 	if (newver > CONFIG_VERSION)
2313c7295a4cSchristos 		cfgerror("your sources require a newer version of config(1) "
2314437f8925Scube 		    "-- please rebuild it.");
2315437f8925Scube 	else if (newver < CONFIG_MINVERSION)
2316c7295a4cSchristos 		cfgerror("your sources are out of date -- please update.");
2317437f8925Scube 	else
2318437f8925Scube 		version = newver;
2319437f8925Scube }
2320