Home | History | Annotate | Line # | Download | only in config
sem.c revision 1.76
      1 /*	$NetBSD: sem.c,v 1.76 2016/09/09 21:09:11 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratories.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	from: @(#)sem.c	8.1 (Berkeley) 6/6/93
     41  */
     42 
     43 #if HAVE_NBTOOL_CONFIG_H
     44 #include "nbtool_config.h"
     45 #endif
     46 
     47 #include <sys/cdefs.h>
     48 __RCSID("$NetBSD: sem.c,v 1.76 2016/09/09 21:09:11 christos Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <ctype.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <util.h>
     56 #include "defs.h"
     57 #include "sem.h"
     58 
     59 /*
     60  * config semantics.
     61  */
     62 
     63 #define	NAMESIZE	100	/* local name buffers */
     64 
     65 const char *s_ifnet;		/* magic attribute */
     66 const char *s_qmark;
     67 const char *s_none;
     68 
     69 static struct hashtab *cfhashtab;	/* for config lookup */
     70 struct hashtab *devitab;		/* etc */
     71 struct attr allattr;
     72 size_t nattrs;
     73 
     74 static struct attr errattr;
     75 static struct devbase errdev;
     76 static struct deva errdeva;
     77 
     78 static int has_errobj(struct attrlist *, struct attr *);
     79 static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
     80 static int resolve(struct nvlist **, const char *, const char *,
     81 		   struct nvlist *, int);
     82 static struct pspec *getpspec(struct attr *, struct devbase *, int);
     83 static struct devi *newdevi(const char *, int, struct devbase *d);
     84 static struct devi *getdevi(const char *);
     85 static void remove_devi(struct devi *);
     86 static const char *concat(const char *, int);
     87 static char *extend(char *, const char *);
     88 static int split(const char *, size_t, char *, size_t, int *);
     89 static void selectbase(struct devbase *, struct deva *);
     90 static const char **fixloc(const char *, struct attr *, struct loclist *);
     91 static const char *makedevstr(devmajor_t, devminor_t);
     92 static const char *major2name(devmajor_t);
     93 static devmajor_t dev2major(struct devbase *);
     94 
     95 extern const char *yyfile;
     96 extern int vflag;
     97 
     98 #define V_ATTRIBUTE	0
     99 #define V_DEVICE	1
    100 struct vtype {
    101 	int type;
    102 	struct attr *attr;
    103 	void *value;
    104 };
    105 
    106 static struct nvlist *
    107 makedevstack(struct devbase *d)
    108 {
    109 	struct devi *firsti, *i;
    110 	struct nvlist *stack = NULL;
    111 
    112 	for (firsti = d->d_ihead; firsti != NULL; firsti = firsti->i_bsame)
    113 		for (i = firsti; i != NULL; i = i->i_alias)
    114 			stack = newnv(NULL, NULL, i, 0, stack);
    115 	return stack;
    116 }
    117 
    118 static void
    119 devcleanup(struct nvlist *stack)
    120 {
    121 	struct nvlist *nv;
    122 	for (nv = stack; nv != NULL; nv = nv->nv_next)
    123 		remove_devi(nv->nv_ptr);
    124 	nvfreel(stack);
    125 }
    126 
    127 static void *
    128 addvalue(int type, struct attr *a, void *value)
    129 {
    130 	struct vtype *vt = emalloc(sizeof(*vt));
    131 	vt->type = type;
    132 	vt->attr = a;
    133 	vt->value = value;
    134 	return vt;
    135 }
    136 
    137 void
    138 initsem(void)
    139 {
    140 
    141 	attrtab = ht_new();
    142 	attrdeptab = ht_new();
    143 
    144 	allattr.a_name = "netbsd";
    145 	TAILQ_INIT(&allattr.a_files);
    146 	(void)ht_insert(attrtab, allattr.a_name, &allattr);
    147 	selectattr(&allattr);
    148 
    149 	errattr.a_name = "<internal>";
    150 
    151 	TAILQ_INIT(&allbases);
    152 
    153 	TAILQ_INIT(&alldevas);
    154 
    155 	TAILQ_INIT(&allpspecs);
    156 
    157 	cfhashtab = ht_new();
    158 	TAILQ_INIT(&allcf);
    159 
    160 	TAILQ_INIT(&alldevi);
    161 	errdev.d_name = "<internal>";
    162 
    163 	TAILQ_INIT(&allpseudo);
    164 
    165 	TAILQ_INIT(&alldevms);
    166 
    167 	s_ifnet = intern("ifnet");
    168 	s_qmark = intern("?");
    169 	s_none = intern("none");
    170 }
    171 
    172 /* Name of include file just ended (set in scan.l) */
    173 extern const char *lastfile;
    174 
    175 static struct attr *
    176 finddep(struct attr *a, const char *name)
    177 {
    178 	struct attrlist *al;
    179 
    180 	for (al = a->a_deps; al != NULL; al = al->al_next) {
    181 		struct attr *this = al->al_this;
    182 		if (strcmp(this->a_name, name) == 0)
    183 			return this;
    184 	}
    185 	return NULL;
    186 }
    187 
    188 static void
    189 mergedeps(const char *dname, const char *name)
    190 {
    191 	struct attr *a, *newa;
    192 
    193 	CFGDBG(4, "merging attr `%s' to devbase `%s'", name, dname);
    194 	a = refattr(dname);
    195 	if (finddep(a, name) == NULL) {
    196 		newa = refattr(name);
    197 		a->a_deps = attrlist_cons(a->a_deps, newa);
    198 		CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name,
    199 		    a->a_name);
    200 	}
    201 }
    202 
    203 static void
    204 fixdev(struct devbase *dev)
    205 {
    206 	struct attrlist *al;
    207 	struct attr *devattr, *a;
    208 
    209 	devattr = refattr(dev->d_name);
    210 	if (devattr->a_devclass)
    211 		panic("%s: dev %s is devclass!", __func__, devattr->a_name);
    212 
    213 	CFGDBG(4, "fixing devbase `%s'", dev->d_name);
    214 	for (al = dev->d_attrs; al != NULL; al = al->al_next) {
    215 		a = al->al_this;
    216 		CFGDBG(4, "fixing devbase `%s' attr `%s'", dev->d_name, a->a_name);
    217 		if (a->a_iattr) {
    218 			a->a_refs = addtoattr(a->a_refs, dev);
    219 			CFGDBG(3, "device `%s' has iattr `%s'", dev->d_name,
    220 			    a->a_name);
    221 		} else if (a->a_devclass != NULL) {
    222 			if (dev->d_classattr != NULL && dev->d_classattr != a) {
    223 				cfgwarn("device `%s' has multiple classes "
    224 				    "(`%s' and `%s')",
    225 				    dev->d_name, dev->d_classattr->a_name,
    226 				    a->a_name);
    227 			}
    228 			if (dev->d_classattr == NULL) {
    229 				dev->d_classattr = a;
    230 				CFGDBG(3, "device `%s' is devclass `%s'", dev->d_name,
    231 				    a->a_name);
    232 			}
    233 		} else {
    234 			if (strcmp(dev->d_name, a->a_name) != 0) {
    235 				mergedeps(dev->d_name, a->a_name);
    236 			}
    237 		}
    238 	}
    239 }
    240 
    241 void
    242 enddefs(void)
    243 {
    244 	struct devbase *dev;
    245 
    246 	yyfile = "enddefs";
    247 
    248 	TAILQ_FOREACH(dev, &allbases, d_next) {
    249 		if (!dev->d_isdef) {
    250 			(void)fprintf(stderr,
    251 			    "%s: device `%s' used but not defined\n",
    252 			    lastfile, dev->d_name);
    253 			errors++;
    254 			continue;
    255 		}
    256 		fixdev(dev);
    257 	}
    258 	if (errors) {
    259 		(void)fprintf(stderr, "*** Stop.\n");
    260 		exit(1);
    261 	}
    262 }
    263 
    264 void
    265 setdefmaxusers(int min, int def, int max)
    266 {
    267 
    268 	if (min < 1 || min > def || def > max)
    269 		cfgerror("maxusers must have 1 <= min (%d) <= default (%d) "
    270 		    "<= max (%d)", min, def, max);
    271 	else {
    272 		minmaxusers = min;
    273 		defmaxusers = def;
    274 		maxmaxusers = max;
    275 	}
    276 }
    277 
    278 void
    279 setmaxusers(int n)
    280 {
    281 
    282 	if (maxusers == n) {
    283 		cfgerror("duplicate maxusers parameter");
    284 		return;
    285 	}
    286 	if (vflag && maxusers != 0)
    287 		cfgwarn("warning: maxusers already defined");
    288 	maxusers = n;
    289 	if (n < minmaxusers) {
    290 		cfgerror("warning: minimum of %d maxusers assumed",
    291 		    minmaxusers);
    292 		errors--;	/* take it away */
    293 		maxusers = minmaxusers;
    294 	} else if (n > maxmaxusers) {
    295 		cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers);
    296 		errors--;
    297 	}
    298 }
    299 
    300 void
    301 setident(const char *i)
    302 {
    303 
    304 	if (i)
    305 		ident = intern(i);
    306 	else
    307 		ident = NULL;
    308 }
    309 
    310 /*
    311  * Define an attribute, optionally with an interface (a locator list)
    312  * and a set of attribute-dependencies.
    313  *
    314  * Attribute dependencies MAY NOT be interface attributes.
    315  *
    316  * Since an empty locator list is logically different from "no interface",
    317  * all locator lists include a dummy head node, which we discard here.
    318  */
    319 int
    320 defattr0(const char *name, struct loclist *locs, struct attrlist *deps,
    321     int devclass)
    322 {
    323 
    324 	if (locs != NULL)
    325 		return defiattr(name, locs, deps, devclass);
    326 	else if (devclass)
    327 		return defdevclass(name, locs, deps, devclass);
    328 	else
    329 		return defattr(name, locs, deps, devclass);
    330 }
    331 
    332 int
    333 defattr(const char *name, struct loclist *locs, struct attrlist *deps,
    334     int devclass)
    335 {
    336 	struct attr *a, *dep;
    337 	struct attrlist *al;
    338 
    339 	/*
    340 	 * If this attribute depends on any others, make sure none of
    341 	 * the dependencies are interface attributes.
    342 	 */
    343 	for (al = deps; al != NULL; al = al->al_next) {
    344 		dep = al->al_this;
    345 		if (dep->a_iattr) {
    346 			cfgerror("`%s' dependency `%s' is an interface "
    347 			    "attribute", name, dep->a_name);
    348 			return (1);
    349 		}
    350 		(void)ht_insert2(attrdeptab, name, dep->a_name,
    351 		    addvalue(V_ATTRIBUTE, a, dep));
    352 		CFGDBG(2, "attr `%s' depends on attr `%s'", name, dep->a_name);
    353 	}
    354 
    355 	if (getrefattr(name, &a)) {
    356 		cfgerror("attribute `%s' already defined", name);
    357 		loclist_destroy(locs);
    358 		return (1);
    359 	}
    360 	if (a == NULL)
    361 		a = mkattr(name);
    362 
    363 	a->a_deps = deps;
    364 	expandattr(a, NULL);
    365 	CFGDBG(3, "attr `%s' defined", a->a_name);
    366 
    367 	return (0);
    368 }
    369 
    370 struct attr *
    371 mkattr(const char *name)
    372 {
    373 	struct attr *a;
    374 
    375 	a = ecalloc(1, sizeof *a);
    376 	if (ht_insert(attrtab, name, a)) {
    377 		free(a);
    378 		return NULL;
    379 	}
    380 	a->a_name = name;
    381 	TAILQ_INIT(&a->a_files);
    382 	CFGDBG(3, "attr `%s' allocated", name);
    383 
    384 	return a;
    385 }
    386 
    387 /* "interface attribute" initialization */
    388 int
    389 defiattr(const char *name, struct loclist *locs, struct attrlist *deps,
    390     int devclass)
    391 {
    392 	struct attr *a;
    393 	int len;
    394 	struct loclist *ll;
    395 
    396 	if (devclass)
    397 		panic("defattr(%s): locators and devclass", name);
    398 
    399 	if (defattr(name, locs, deps, devclass) != 0)
    400 		return (1);
    401 
    402 	a = getattr(name);
    403 	a->a_iattr = 1;
    404 	/* unwrap */
    405 	a->a_locs = locs->ll_next;
    406 	locs->ll_next = NULL;
    407 	loclist_destroy(locs);
    408 	len = 0;
    409 	for (ll = a->a_locs; ll != NULL; ll = ll->ll_next)
    410 		len++;
    411 	a->a_loclen = len;
    412 	if (deps)
    413 		CFGDBG(2, "attr `%s' iface with deps", a->a_name);
    414 	return (0);
    415 }
    416 
    417 /* "device class" initialization */
    418 int
    419 defdevclass(const char *name, struct loclist *locs, struct attrlist *deps,
    420     int devclass)
    421 {
    422 	struct attr *a;
    423 	char classenum[256], *cp;
    424 	int errored = 0;
    425 
    426 	if (deps)
    427 		panic("defattr(%s): dependencies and devclass", name);
    428 
    429 	if (defattr(name, locs, deps, devclass) != 0)
    430 		return (1);
    431 
    432 	a = getattr(name);
    433 	(void)snprintf(classenum, sizeof(classenum), "DV_%s", name);
    434 	for (cp = classenum + 3; *cp; cp++) {
    435 		if (!errored &&
    436 		    (!isalnum((unsigned char)*cp) ||
    437 		      (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) {
    438 			cfgerror("device class names must be "
    439 			    "lower-case alphanumeric characters");
    440 			errored = 1;
    441 		}
    442 		*cp = (char)toupper((unsigned char)*cp);
    443 	}
    444 	a->a_devclass = intern(classenum);
    445 
    446 	return (0);
    447 }
    448 
    449 /*
    450  * Return true if the given `error object' is embedded in the given
    451  * pointer list.
    452  */
    453 static int
    454 has_errobj(struct attrlist *al, struct attr *obj)
    455 {
    456 
    457 	for (; al != NULL; al = al->al_next)
    458 		if (al->al_this == obj)
    459 			return (1);
    460 	return (0);
    461 }
    462 
    463 /*
    464  * Return true if the given attribute is embedded in the given
    465  * pointer list.
    466  */
    467 int
    468 has_attr(struct attrlist *al, const char *attr)
    469 {
    470 	struct attr *a;
    471 
    472 	if ((a = getattr(attr)) == NULL)
    473 		return (0);
    474 
    475 	for (; al != NULL; al = al->al_next)
    476 		if (al->al_this == a)
    477 			return (1);
    478 	return (0);
    479 }
    480 
    481 /*
    482  * Add a device base to a list in an attribute (actually, to any list).
    483  * Note that this does not check for duplicates, and does reverse the
    484  * list order, but no one cares anyway.
    485  */
    486 static struct nvlist *
    487 addtoattr(struct nvlist *l, struct devbase *dev)
    488 {
    489 	struct nvlist *n;
    490 
    491 	n = newnv(NULL, NULL, dev, 0, l);
    492 	return (n);
    493 }
    494 
    495 /*
    496  * Define a device.  This may (or may not) also define an interface
    497  * attribute and/or refer to existing attributes.
    498  */
    499 void
    500 defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs,
    501        int ispseudo)
    502 {
    503 	struct loclist *ll;
    504 	struct attrlist *al;
    505 
    506 	if (dev == &errdev)
    507 		goto bad;
    508 	if (dev->d_isdef) {
    509 		cfgerror("redefinition of `%s'", dev->d_name);
    510 		goto bad;
    511 	}
    512 
    513 	dev->d_isdef = 1;
    514 	if (has_errobj(attrs, &errattr))
    515 		goto bad;
    516 
    517 	/*
    518 	 * Handle implicit attribute definition from locator list.  Do
    519 	 * this before scanning the `at' list so that we can have, e.g.:
    520 	 *	device foo at other, foo { slot = -1 }
    521 	 * (where you can plug in a foo-bus extender to a foo-bus).
    522 	 */
    523 	if (loclist != NULL) {
    524 		ll = loclist;
    525 		loclist = NULL;	/* defattr disposes of them for us */
    526 		if (defiattr(dev->d_name, ll, NULL, 0))
    527 			goto bad;
    528 		attrs = attrlist_cons(attrs, getattr(dev->d_name));
    529 		/* This used to be stored but was never used */
    530 		/* attrs->al_name = dev->d_name; */
    531 	}
    532 
    533 	/*
    534 	 * Pseudo-devices can have children.  Consider them as
    535 	 * attaching at root.
    536 	 */
    537 	if (ispseudo) {
    538 		for (al = attrs; al != NULL; al = al->al_next)
    539 			if (al->al_this->a_iattr)
    540 				break;
    541 		if (al != NULL) {
    542 			if (ispseudo < 2) {
    543 				if (version >= 20080610)
    544 					cfgerror("interface attribute on "
    545 					 "non-device pseudo `%s'", dev->d_name);
    546 				else {
    547 					ispseudo = 2;
    548 				}
    549 			}
    550 			ht_insert(devroottab, dev->d_name, dev);
    551 		}
    552 	}
    553 
    554 	/* Committed!  Set up fields. */
    555 	dev->d_ispseudo = ispseudo;
    556 	dev->d_attrs = attrs;
    557 	dev->d_classattr = NULL;		/* for now */
    558 	CFGDBG(3, "dev `%s' defined", dev->d_name);
    559 
    560 	/*
    561 	 * Implicit attribute definition for device.
    562 	 */
    563 	refattr(dev->d_name);
    564 
    565 	/*
    566 	 * For each interface attribute this device refers to, add this
    567 	 * device to its reference list.  This makes, e.g., finding all
    568 	 * "scsi"s easier.
    569 	 *
    570 	 * While looking through the attributes, set up the device
    571 	 * class if any are devclass attributes (and error out if the
    572 	 * device has two classes).
    573 	 */
    574 	for (al = attrs; al != NULL; al = al->al_next) {
    575 		/*
    576 		 * Implicit attribute definition for device dependencies.
    577 		 */
    578 		refattr(al->al_this->a_name);
    579 		(void)ht_insert2(attrdeptab, dev->d_name, al->al_this->a_name,
    580 			addvalue(V_DEVICE, al->al_this, dev));
    581 		CFGDBG(2, "device `%s' depends on attr `%s'", dev->d_name,
    582 		    al->al_this->a_name);
    583 	}
    584 	return;
    585  bad:
    586 	loclist_destroy(loclist);
    587 	attrlist_destroyall(attrs);
    588 }
    589 
    590 /*
    591  * Look up a devbase.  Also makes sure it is a reasonable name,
    592  * i.e., does not end in a digit or contain special characters.
    593  */
    594 struct devbase *
    595 getdevbase(const char *name)
    596 {
    597 	const u_char *p;
    598 	struct devbase *dev;
    599 
    600 	p = (const u_char *)name;
    601 	if (!isalpha(*p))
    602 		goto badname;
    603 	while (*++p) {
    604 		if (!isalnum(*p) && *p != '_')
    605 			goto badname;
    606 	}
    607 	if (isdigit(*--p)) {
    608  badname:
    609 		cfgerror("bad device base name `%s'", name);
    610 		return (&errdev);
    611 	}
    612 	dev = ht_lookup(devbasetab, name);
    613 	if (dev == NULL) {
    614 		dev = ecalloc(1, sizeof *dev);
    615 		dev->d_name = name;
    616 		dev->d_isdef = 0;
    617 		dev->d_major = NODEVMAJOR;
    618 		dev->d_attrs = NULL;
    619 		dev->d_ihead = NULL;
    620 		dev->d_ipp = &dev->d_ihead;
    621 		dev->d_ahead = NULL;
    622 		dev->d_app = &dev->d_ahead;
    623 		dev->d_umax = 0;
    624 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
    625 		if (ht_insert(devbasetab, name, dev))
    626 			panic("getdevbase(%s)", name);
    627 		CFGDBG(3, "devbase defined `%s'", dev->d_name);
    628 	}
    629 	return (dev);
    630 }
    631 
    632 /*
    633  * Define some of a device's allowable parent attachments.
    634  * There may be a list of (plain) attributes.
    635  */
    636 void
    637 defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
    638 	     struct attrlist *attrs)
    639 {
    640 	struct nvlist *nv;
    641 	struct attrlist *al;
    642 	struct attr *a;
    643 	struct deva *da;
    644 
    645 	if (dev == &errdev)
    646 		goto bad;
    647 	if (deva == NULL)
    648 		deva = getdevattach(dev->d_name);
    649 	if (deva == &errdeva)
    650 		goto bad;
    651 	if (!dev->d_isdef) {
    652 		cfgerror("attaching undefined device `%s'", dev->d_name);
    653 		goto bad;
    654 	}
    655 	if (deva->d_isdef) {
    656 		cfgerror("redefinition of `%s'", deva->d_name);
    657 		goto bad;
    658 	}
    659 	if (dev->d_ispseudo) {
    660 		cfgerror("pseudo-devices can't attach");
    661 		goto bad;
    662 	}
    663 
    664 	deva->d_isdef = 1;
    665 	if (has_errobj(attrs, &errattr))
    666 		goto bad;
    667 	for (al = attrs; al != NULL; al = al->al_next) {
    668 		a = al->al_this;
    669 		if (a == &errattr)
    670 			continue;		/* already complained */
    671 		if (a->a_iattr || a->a_devclass != NULL)
    672 			cfgerror("`%s' is not a plain attribute", a->a_name);
    673 	}
    674 
    675 	/* Committed!  Set up fields. */
    676 	deva->d_attrs = attrs;
    677 	deva->d_atlist = atlist;
    678 	deva->d_devbase = dev;
    679 	CFGDBG(3, "deva `%s' defined", deva->d_name);
    680 
    681 	/*
    682 	 * Implicit attribute definition for device attachment.
    683 	 */
    684 	refattr(deva->d_name);
    685 
    686 	/*
    687 	 * Turn the `at' list into interface attributes (map each
    688 	 * nv_name to an attribute, or to NULL for root), and add
    689 	 * this device to those attributes, so that children can
    690 	 * be listed at this particular device if they are supported
    691 	 * by that attribute.
    692 	 */
    693 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
    694 		if (nv->nv_name == NULL)
    695 			nv->nv_ptr = a = NULL;	/* at root */
    696 		else
    697 			nv->nv_ptr = a = getattr(nv->nv_name);
    698 		if (a == &errattr)
    699 			continue;		/* already complained */
    700 
    701 		/*
    702 		 * Make sure that an attachment spec doesn't
    703 		 * already say how to attach to this attribute.
    704 		 */
    705 		for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
    706 			if (onlist(da->d_atlist, a))
    707 				cfgerror("attach at `%s' already done by `%s'",
    708 				     a ? a->a_name : "root", da->d_name);
    709 
    710 		if (a == NULL) {
    711 			ht_insert(devroottab, dev->d_name, dev);
    712 			continue;		/* at root; don't add */
    713 		}
    714 		if (!a->a_iattr)
    715 			cfgerror("%s cannot be at plain attribute `%s'",
    716 			    dev->d_name, a->a_name);
    717 		else
    718 			a->a_devs = addtoattr(a->a_devs, dev);
    719 	}
    720 
    721 	/* attach to parent */
    722 	*dev->d_app = deva;
    723 	dev->d_app = &deva->d_bsame;
    724 	return;
    725  bad:
    726 	nvfreel(atlist);
    727 	attrlist_destroyall(attrs);
    728 }
    729 
    730 /*
    731  * Look up a device attachment.  Also makes sure it is a reasonable
    732  * name, i.e., does not contain digits or special characters.
    733  */
    734 struct deva *
    735 getdevattach(const char *name)
    736 {
    737 	const u_char *p;
    738 	struct deva *deva;
    739 
    740 	p = (const u_char *)name;
    741 	if (!isalpha(*p))
    742 		goto badname;
    743 	while (*++p) {
    744 		if (!isalnum(*p) && *p != '_')
    745 			goto badname;
    746 	}
    747 	if (isdigit(*--p)) {
    748  badname:
    749 		cfgerror("bad device attachment name `%s'", name);
    750 		return (&errdeva);
    751 	}
    752 	deva = ht_lookup(devatab, name);
    753 	if (deva == NULL) {
    754 		deva = ecalloc(1, sizeof *deva);
    755 		deva->d_name = name;
    756 		deva->d_bsame = NULL;
    757 		deva->d_isdef = 0;
    758 		deva->d_devbase = NULL;
    759 		deva->d_atlist = NULL;
    760 		deva->d_attrs = NULL;
    761 		deva->d_ihead = NULL;
    762 		deva->d_ipp = &deva->d_ihead;
    763 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
    764 		if (ht_insert(devatab, name, deva))
    765 			panic("getdeva(%s)", name);
    766 	}
    767 	return (deva);
    768 }
    769 
    770 /*
    771  * Look up an attribute.
    772  */
    773 struct attr *
    774 getattr(const char *name)
    775 {
    776 	struct attr *a;
    777 
    778 	if ((a = ht_lookup(attrtab, name)) == NULL) {
    779 		cfgerror("undefined attribute `%s'", name);
    780 		a = &errattr;
    781 	}
    782 	return (a);
    783 }
    784 
    785 /*
    786  * Implicit attribute definition.
    787  */
    788 struct attr *
    789 refattr(const char *name)
    790 {
    791 	struct attr *a;
    792 
    793 	if ((a = ht_lookup(attrtab, name)) == NULL)
    794 		a = mkattr(name);
    795 	return a;
    796 }
    797 
    798 int
    799 getrefattr(const char *name, struct attr **ra)
    800 {
    801 	struct attr *a;
    802 
    803 	a = ht_lookup(attrtab, name);
    804 	if (a == NULL) {
    805 		*ra = NULL;
    806 		return (0);
    807 	}
    808 	/*
    809 	 * Check if the existing attr is only referenced, not really defined.
    810 	 */
    811 	if (a->a_deps == NULL &&
    812 	    a->a_iattr == 0 &&
    813 	    a->a_devclass == 0) {
    814 		*ra = a;
    815 		return (0);
    816 	}
    817 	return (1);
    818 }
    819 
    820 /*
    821  * Recursively expand an attribute and its dependencies, checking for
    822  * cycles, and invoking a callback for each attribute found.
    823  */
    824 void
    825 expandattr(struct attr *a, void (*callback)(struct attr *))
    826 {
    827 	struct attrlist *al;
    828 	struct attr *dep;
    829 
    830 	if (a->a_expanding) {
    831 		cfgerror("circular dependency on attribute `%s'", a->a_name);
    832 		return;
    833 	}
    834 
    835 	a->a_expanding = 1;
    836 
    837 	/* First expand all of this attribute's dependencies. */
    838 	for (al = a->a_deps; al != NULL; al = al->al_next) {
    839 		dep = al->al_this;
    840 		expandattr(dep, callback);
    841 	}
    842 
    843 	/* ...and now invoke the callback for ourself. */
    844 	if (callback != NULL)
    845 		(*callback)(a);
    846 
    847 	a->a_expanding = 0;
    848 }
    849 
    850 /*
    851  * Set the major device number for a device, so that it can be used
    852  * as a root/dumps "on" device in a configuration.
    853  */
    854 void
    855 setmajor(struct devbase *d, devmajor_t n)
    856 {
    857 
    858 	if (d != &errdev && d->d_major != NODEVMAJOR)
    859 		cfgerror("device `%s' is already major %d",
    860 		    d->d_name, d->d_major);
    861 	else
    862 		d->d_major = n;
    863 }
    864 
    865 const char *
    866 major2name(devmajor_t maj)
    867 {
    868 	struct devbase *dev;
    869 	struct devm *dm;
    870 
    871 	if (!do_devsw) {
    872 		TAILQ_FOREACH(dev, &allbases, d_next) {
    873 			if (dev->d_major == maj)
    874 				return (dev->d_name);
    875 		}
    876 	} else {
    877 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
    878 			if (dm->dm_bmajor == maj)
    879 				return (dm->dm_name);
    880 		}
    881 	}
    882 	return (NULL);
    883 }
    884 
    885 devmajor_t
    886 dev2major(struct devbase *dev)
    887 {
    888 	struct devm *dm;
    889 
    890 	if (!do_devsw)
    891 		return (dev->d_major);
    892 
    893 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
    894 		if (strcmp(dm->dm_name, dev->d_name) == 0)
    895 			return (dm->dm_bmajor);
    896 	}
    897 	return (NODEVMAJOR);
    898 }
    899 
    900 /*
    901  * Make a string description of the device at maj/min.
    902  */
    903 static const char *
    904 makedevstr(devmajor_t maj, devminor_t min)
    905 {
    906 	const char *devicename;
    907 	char buf[32];
    908 
    909 	devicename = major2name(maj);
    910 	if (devicename == NULL)
    911 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
    912 	else
    913 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
    914 		    min / maxpartitions, (min % maxpartitions) + 'a');
    915 
    916 	return (intern(buf));
    917 }
    918 
    919 /*
    920  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
    921  * Handle the case where the device number is given but there is no
    922  * corresponding name, and map NULL to the default.
    923  */
    924 static int
    925 resolve(struct nvlist **nvp, const char *name, const char *what,
    926 	struct nvlist *dflt, int part)
    927 {
    928 	struct nvlist *nv;
    929 	struct devbase *dev;
    930 	const char *cp;
    931 	devmajor_t maj;
    932 	devminor_t min;
    933 	size_t i, l;
    934 	int unit;
    935 	char buf[NAMESIZE];
    936 
    937 	if ((part -= 'a') >= maxpartitions || part < 0)
    938 		panic("resolve");
    939 	if ((nv = *nvp) == NULL) {
    940 		dev_t	d = NODEV;
    941 		/*
    942 		 * Apply default.  Easiest to do this by number.
    943 		 * Make sure to retain NODEVness, if this is dflt's disposition.
    944 		 */
    945 		if ((dev_t)dflt->nv_num != NODEV) {
    946 			maj = major(dflt->nv_num);
    947 			min = ((minor(dflt->nv_num) / maxpartitions) *
    948 			    maxpartitions) + part;
    949 			d = makedev(maj, min);
    950 			cp = makedevstr(maj, min);
    951 		} else
    952 			cp = NULL;
    953 		*nvp = nv = newnv(NULL, cp, NULL, (long long)d, NULL);
    954 	}
    955 	if ((dev_t)nv->nv_num != NODEV) {
    956 		/*
    957 		 * By the numbers.  Find the appropriate major number
    958 		 * to make a name.
    959 		 */
    960 		maj = major(nv->nv_num);
    961 		min = minor(nv->nv_num);
    962 		nv->nv_str = makedevstr(maj, min);
    963 		return (0);
    964 	}
    965 
    966 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
    967 		/*
    968 		 * Wildcarded or unspecified; leave it as NODEV.
    969 		 */
    970 		return (0);
    971 
    972 	if (nv->nv_ptr != NULL && strcmp(nv->nv_ptr, "spec") == 0)
    973 		/*
    974 		 * spec string, interpreted by kernel
    975 		 */
    976 		return (0);
    977 
    978 	/*
    979 	 * The normal case: things like "ra2b".  Check for partition
    980 	 * suffix, remove it if there, and split into name ("ra") and
    981 	 * unit (2).
    982 	 */
    983 	l = i = strlen(nv->nv_str);
    984 	cp = &nv->nv_str[l];
    985 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
    986 	    isdigit((unsigned char)cp[-1])) {
    987 		l--;
    988 		part = *cp - 'a';
    989 	}
    990 	cp = nv->nv_str;
    991 	if (split(cp, l, buf, sizeof buf, &unit)) {
    992 		cfgerror("%s: invalid %s device name `%s'", name, what, cp);
    993 		return (1);
    994 	}
    995 	dev = ht_lookup(devbasetab, intern(buf));
    996 	if (dev == NULL) {
    997 		cfgerror("%s: device `%s' does not exist", name, buf);
    998 		return (1);
    999 	}
   1000 
   1001 	/*
   1002 	 * Check for the magic network interface attribute, and
   1003 	 * don't bother making a device number.
   1004 	 */
   1005 	if (has_attr(dev->d_attrs, s_ifnet)) {
   1006 		nv->nv_num = (long long)NODEV;
   1007 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
   1008 	} else {
   1009 		maj = dev2major(dev);
   1010 		if (maj == NODEVMAJOR) {
   1011 			cfgerror("%s: can't make %s device from `%s'",
   1012 			    name, what, nv->nv_str);
   1013 			return (1);
   1014 		}
   1015 		nv->nv_num = (long long)makedev(maj, unit * maxpartitions + part);
   1016 	}
   1017 
   1018 	nv->nv_name = dev->d_name;
   1019 	return (0);
   1020 }
   1021 
   1022 /*
   1023  * Add a completed configuration to the list.
   1024  */
   1025 void
   1026 addconf(struct config *cf0)
   1027 {
   1028 	struct config *cf;
   1029 	const char *name;
   1030 
   1031 	name = cf0->cf_name;
   1032 	cf = ecalloc(1, sizeof *cf);
   1033 	if (ht_insert(cfhashtab, name, cf)) {
   1034 		cfgerror("configuration `%s' already defined", name);
   1035 		free(cf);
   1036 		goto bad;
   1037 	}
   1038 	*cf = *cf0;
   1039 
   1040 	/*
   1041 	 * Resolve the root device.
   1042 	 */
   1043 	if (cf->cf_root == NULL) {
   1044 		cfgerror("%s: no root device specified", name);
   1045 		goto bad;
   1046 	}
   1047 	if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
   1048 		struct nvlist *nv;
   1049 		nv = cf->cf_root;
   1050 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
   1051 			goto bad;
   1052 	}
   1053 
   1054 	/*
   1055 	 * Resolve the dump device.
   1056 	 */
   1057 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
   1058 		/*
   1059 		 * Wildcarded dump device is equivalent to unspecified.
   1060 		 */
   1061 		cf->cf_dump = NULL;
   1062 	} else if (cf->cf_dump->nv_str == s_none) {
   1063 		/*
   1064 		 * Operator has requested that no dump device should be
   1065 		 * configured; do nothing.
   1066 		 */
   1067 	} else {
   1068 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
   1069 			goto bad;
   1070 	}
   1071 
   1072 	/* Wildcarded fstype is `unspecified'. */
   1073 	if (cf->cf_fstype == s_qmark)
   1074 		cf->cf_fstype = NULL;
   1075 
   1076 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
   1077 	return;
   1078  bad:
   1079 	nvfreel(cf0->cf_root);
   1080 	nvfreel(cf0->cf_dump);
   1081 }
   1082 
   1083 void
   1084 setconf(struct nvlist **npp, const char *what, struct nvlist *v)
   1085 {
   1086 
   1087 	if (*npp != NULL) {
   1088 		cfgerror("duplicate %s specification", what);
   1089 		nvfreel(v);
   1090 	} else
   1091 		*npp = v;
   1092 }
   1093 
   1094 void
   1095 delconf(const char *name, int nowarn)
   1096 {
   1097 	struct config *cf;
   1098 
   1099 	CFGDBG(5, "deselecting config `%s'", name);
   1100 	if (ht_lookup(cfhashtab, name) == NULL) {
   1101 		if (!nowarn)
   1102 			cfgerror("configuration `%s' undefined", name);
   1103 		return;
   1104 	}
   1105 	(void)ht_remove(cfhashtab, name);
   1106 
   1107 	TAILQ_FOREACH(cf, &allcf, cf_next)
   1108 		if (!strcmp(cf->cf_name, name))
   1109 			break;
   1110 	if (cf == NULL)
   1111 		panic("lost configuration `%s'", name);
   1112 
   1113 	TAILQ_REMOVE(&allcf, cf, cf_next);
   1114 }
   1115 
   1116 void
   1117 setfstype(const char **fstp, const char *v)
   1118 {
   1119 
   1120 	if (*fstp != NULL) {
   1121 		cfgerror("multiple fstype specifications");
   1122 		return;
   1123 	}
   1124 
   1125 	if (v != s_qmark && OPT_FSOPT(v)) {
   1126 		cfgerror("\"%s\" is not a configured file system", v);
   1127 		return;
   1128 	}
   1129 
   1130 	*fstp = v;
   1131 }
   1132 
   1133 static struct devi *
   1134 newdevi(const char *name, int unit, struct devbase *d)
   1135 {
   1136 	struct devi *i;
   1137 
   1138 	i = ecalloc(1, sizeof *i);
   1139 	i->i_name = name;
   1140 	i->i_unit = unit;
   1141 	i->i_base = d;
   1142 	i->i_bsame = NULL;
   1143 	i->i_asame = NULL;
   1144 	i->i_alias = NULL;
   1145 	i->i_at = NULL;
   1146 	i->i_pspec = NULL;
   1147 	i->i_atdeva = NULL;
   1148 	i->i_locs = NULL;
   1149 	i->i_cfflags = 0;
   1150 	i->i_lineno = currentline();
   1151 	i->i_srcfile = yyfile;
   1152 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
   1153 	i->i_level = devilevel;
   1154 	i->i_pseudoroot = 0;
   1155 	if (unit >= d->d_umax)
   1156 		d->d_umax = unit + 1;
   1157 	return (i);
   1158 }
   1159 
   1160 /*
   1161  * Add the named device as attaching to the named attribute (or perhaps
   1162  * another device instead) plus unit number.
   1163  */
   1164 void
   1165 adddev(const char *name, const char *at, struct loclist *loclist, int flags)
   1166 {
   1167 	struct devi *i;		/* the new instance */
   1168 	struct pspec *p;	/* and its pspec */
   1169 	struct attr *attr;	/* attribute that allows attach */
   1170 	struct devbase *ib;	/* i->i_base */
   1171 	struct devbase *ab;	/* not NULL => at another dev */
   1172 	struct attrlist *al;
   1173 	struct deva *iba;	/* devbase attachment used */
   1174 	const char *cp;
   1175 	int atunit;
   1176 	char atbuf[NAMESIZE];
   1177 	int hit;
   1178 
   1179 	ab = NULL;
   1180 	iba = NULL;
   1181 	if (at == NULL) {
   1182 		/* "at root" */
   1183 		p = NULL;
   1184 		if ((i = getdevi(name)) == NULL)
   1185 			goto bad;
   1186 		/*
   1187 		 * Must warn about i_unit > 0 later, after taking care of
   1188 		 * the STAR cases (we could do non-star's here but why
   1189 		 * bother?).  Make sure this device can be at root.
   1190 		 */
   1191 		ib = i->i_base;
   1192 		hit = 0;
   1193 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
   1194 			if (onlist(iba->d_atlist, NULL)) {
   1195 				hit = 1;
   1196 				break;
   1197 			}
   1198 		if (!hit) {
   1199 			cfgerror("`%s' cannot attach to the root", ib->d_name);
   1200 			i->i_active = DEVI_BROKEN;
   1201 			goto bad;
   1202 		}
   1203 		attr = &errattr;	/* a convenient "empty" attr */
   1204 	} else {
   1205 		if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
   1206 			cfgerror("invalid attachment name `%s'", at);
   1207 			/* (void)getdevi(name); -- ??? */
   1208 			goto bad;
   1209 		}
   1210 		if ((i = getdevi(name)) == NULL)
   1211 			goto bad;
   1212 		ib = i->i_base;
   1213 
   1214 		/*
   1215 		 * Devices can attach to two types of things: Attributes,
   1216 		 * and other devices (which have the appropriate attributes
   1217 		 * to allow attachment).
   1218 		 *
   1219 		 * (1) If we're attached to an attribute, then we don't need
   1220 		 *     look at the parent base device to see what attributes
   1221 		 *     it has, and make sure that we can attach to them.
   1222 		 *
   1223 		 * (2) If we're attached to a real device (i.e. named in
   1224 		 *     the config file), we want to remember that so that
   1225 		 *     at cross-check time, if the device we're attached to
   1226 		 *     is missing but other devices which also provide the
   1227 		 *     attribute are present, we don't get a false "OK."
   1228 		 *
   1229 		 * (3) If the thing we're attached to is an attribute
   1230 		 *     but is actually named in the config file, we still
   1231 		 *     have to remember its devbase.
   1232 		 */
   1233 		cp = intern(atbuf);
   1234 
   1235 		/* Figure out parent's devbase, to satisfy case (3). */
   1236 		ab = ht_lookup(devbasetab, cp);
   1237 
   1238 		/* Find out if it's an attribute. */
   1239 		attr = ht_lookup(attrtab, cp);
   1240 
   1241 		/* Make sure we're _really_ attached to the attr.  Case (1). */
   1242 		if (attr != NULL && onlist(attr->a_devs, ib))
   1243 			goto findattachment;
   1244 
   1245 		/*
   1246 		 * Else a real device, and not just an attribute.  Case (2).
   1247 		 *
   1248 		 * Have to work a bit harder to see whether we have
   1249 		 * something like "tg0 at esp0" (where esp is merely
   1250 		 * not an attribute) or "tg0 at nonesuch0" (where
   1251 		 * nonesuch is not even a device).
   1252 		 */
   1253 		if (ab == NULL) {
   1254 			cfgerror("%s at %s: `%s' unknown",
   1255 			    name, at, atbuf);
   1256 			i->i_active = DEVI_BROKEN;
   1257 			goto bad;
   1258 		}
   1259 
   1260 		/*
   1261 		 * See if the named parent carries an attribute
   1262 		 * that allows it to supervise device ib.
   1263 		 */
   1264 		for (al = ab->d_attrs; al != NULL; al = al->al_next) {
   1265 			attr = al->al_this;
   1266 			if (onlist(attr->a_devs, ib))
   1267 				goto findattachment;
   1268 		}
   1269 		cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
   1270 		i->i_active = DEVI_BROKEN;
   1271 		goto bad;
   1272 
   1273  findattachment:
   1274 		/*
   1275 		 * Find the parent spec.  If a matching one has not yet been
   1276 		 * created, create one.
   1277 		 */
   1278 		p = getpspec(attr, ab, atunit);
   1279 		p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
   1280 
   1281 		/* find out which attachment it uses */
   1282 		hit = 0;
   1283 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
   1284 			if (onlist(iba->d_atlist, attr)) {
   1285 				hit = 1;
   1286 				break;
   1287 			}
   1288 		if (!hit)
   1289 			panic("adddev: can't figure out attachment");
   1290 	}
   1291 	if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
   1292 		i->i_active = DEVI_BROKEN;
   1293 		goto bad;
   1294 	}
   1295 	i->i_at = at;
   1296 	i->i_pspec = p;
   1297 	i->i_atdeva = iba;
   1298 	i->i_cfflags = flags;
   1299 	CFGDBG(3, "devi `%s' added", i->i_name);
   1300 
   1301 	*iba->d_ipp = i;
   1302 	iba->d_ipp = &i->i_asame;
   1303 
   1304 	/* all done, fall into ... */
   1305  bad:
   1306 	loclist_destroy(loclist);
   1307 	return;
   1308 }
   1309 
   1310 void
   1311 deldevi(const char *name, const char *at, int nowarn)
   1312 {
   1313 	struct devi *firsti, *i;
   1314 	struct devbase *d;
   1315 	int unit;
   1316 	char base[NAMESIZE];
   1317 
   1318 	CFGDBG(5, "deselecting devi `%s'", name);
   1319 	if (split(name, strlen(name), base, sizeof base, &unit)) {
   1320 		if (!nowarn) {
   1321 			cfgerror("invalid device name `%s'", name);
   1322 			return;
   1323 		}
   1324 	}
   1325 	d = ht_lookup(devbasetab, intern(base));
   1326 	if (d == NULL) {
   1327 		if (!nowarn)
   1328 			cfgerror("%s: unknown device `%s'", name, base);
   1329 		return;
   1330 	}
   1331 	if (d->d_ispseudo) {
   1332 		cfgerror("%s: %s is a pseudo-device", name, base);
   1333 		return;
   1334 	}
   1335 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
   1336 		cfgerror("`%s' not defined", name);
   1337 		return;
   1338 	}
   1339 	if (at == NULL && firsti->i_at == NULL) {
   1340 		/* 'at root' */
   1341 		remove_devi(firsti);
   1342 		return;
   1343 	} else if (at != NULL)
   1344 		for (i = firsti; i != NULL; i = i->i_alias)
   1345 			if (i->i_active != DEVI_BROKEN &&
   1346 			    strcmp(at, i->i_at) == 0) {
   1347 				remove_devi(i);
   1348 				return;
   1349 			}
   1350 	cfgerror("`%s' at `%s' not found", name, at ? at : "root");
   1351 }
   1352 
   1353 static void
   1354 remove_devi(struct devi *i)
   1355 {
   1356 	struct devbase *d = i->i_base;
   1357 	struct devi *f, *j, **ppi;
   1358 	struct deva *iba;
   1359 
   1360 	CFGDBG(5, "removing devi `%s'", i->i_name);
   1361 	f = ht_lookup(devitab, i->i_name);
   1362 	if (f == NULL)
   1363 		panic("remove_devi(): instance %s disappeared from devitab",
   1364 		    i->i_name);
   1365 
   1366 	if (i->i_active == DEVI_BROKEN) {
   1367 		cfgerror("not removing broken instance `%s'", i->i_name);
   1368 		return;
   1369 	}
   1370 
   1371 	/*
   1372 	 * We have the device instance, i.
   1373 	 * We have to:
   1374 	 *   - delete the alias
   1375 	 *
   1376 	 *      If the devi was an alias of an already listed devi, all is
   1377 	 *      good we don't have to do more.
   1378 	 *      If it was the first alias, we have to replace i's entry in
   1379 	 *      d's list by its first alias.
   1380 	 *      If it was the only entry, we must remove i's entry from d's
   1381 	 *      list.
   1382 	 */
   1383 	if (i != f) {
   1384 		for (j = f; j->i_alias != i; j = j->i_alias)
   1385 			continue;
   1386 		j->i_alias = i->i_alias;
   1387 	} else {
   1388 		if (i->i_alias == NULL) {
   1389 			/* No alias, must unlink the entry from devitab */
   1390 			ht_remove(devitab, i->i_name);
   1391 			j = i->i_bsame;
   1392 		} else {
   1393 			/* Or have the first alias replace i in d's list */
   1394 			i->i_alias->i_bsame = i->i_bsame;
   1395 			j = i->i_alias;
   1396 			if (i == f)
   1397 				ht_replace(devitab, i->i_name, i->i_alias);
   1398 		}
   1399 
   1400 		/*
   1401 		 *   - remove/replace the instance from the devbase's list
   1402 		 *
   1403 		 * A double-linked list would make this much easier.  Oh, well,
   1404 		 * what is done is done.
   1405 		 */
   1406 		for (ppi = &d->d_ihead;
   1407 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
   1408 		    ppi = &(*ppi)->i_bsame)
   1409 			continue;
   1410 		if (*ppi == NULL)
   1411 			panic("deldev: dev (%s) doesn't list the devi"
   1412 			    " (%s at %s)", d->d_name, i->i_name, i->i_at);
   1413 		f = *ppi;
   1414 		if (f == i)
   1415 			/* That implies d->d_ihead == i */
   1416 			*ppi = j;
   1417 		else
   1418 			(*ppi)->i_bsame = j;
   1419 		if (d->d_ipp == &i->i_bsame) {
   1420 			if (i->i_alias == NULL) {
   1421 				if (f == i)
   1422 					d->d_ipp = &d->d_ihead;
   1423 				else
   1424 					d->d_ipp = &f->i_bsame;
   1425 			} else
   1426 				d->d_ipp = &i->i_alias->i_bsame;
   1427 		}
   1428 	}
   1429 	/*
   1430 	 *   - delete the attachment instance
   1431 	 */
   1432 	iba = i->i_atdeva;
   1433 	for (ppi = &iba->d_ihead;
   1434 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
   1435 	    ppi = &(*ppi)->i_asame)
   1436 		continue;
   1437 	if (*ppi == NULL)
   1438 		panic("deldev: deva (%s) doesn't list the devi (%s)",
   1439 		    iba->d_name, i->i_name);
   1440 	f = *ppi;
   1441 	if (f == i)
   1442 		/* That implies iba->d_ihead == i */
   1443 		*ppi = i->i_asame;
   1444 	else
   1445 		(*ppi)->i_asame = i->i_asame;
   1446 	if (iba->d_ipp == &i->i_asame) {
   1447 		if (f == i)
   1448 			iba->d_ipp = &iba->d_ihead;
   1449 		else
   1450 			iba->d_ipp = &f->i_asame;
   1451 	}
   1452 	/*
   1453 	 *   - delete the pspec
   1454 	 */
   1455 	if (i->i_pspec) {
   1456 		struct pspec *p = i->i_pspec;
   1457 		struct nvlist *nv, *onv;
   1458 
   1459 		/* Double-linked nvlist anyone? */
   1460 		for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
   1461 			if (nv->nv_next && nv->nv_next->nv_ptr == i) {
   1462 				onv = nv->nv_next;
   1463 				nv->nv_next = onv->nv_next;
   1464 				nvfree(onv);
   1465 				break;
   1466 			}
   1467 			if (nv->nv_ptr == i) {
   1468 				/* nv is p->p_devs in that case */
   1469 				p->p_devs = nv->nv_next;
   1470 				nvfree(nv);
   1471 				break;
   1472 			}
   1473 		}
   1474 		if (p->p_devs == NULL)
   1475 			TAILQ_REMOVE(&allpspecs, p, p_list);
   1476 	}
   1477 	/*
   1478 	 *   - delete the alldevi entry
   1479 	 */
   1480 	TAILQ_REMOVE(&alldevi, i, i_next);
   1481 	ndevi--;
   1482 	/*
   1483 	 * Put it in deaddevitab
   1484 	 *
   1485 	 * Each time a devi is removed, devilevel is increased so that later on
   1486 	 * it is possible to tell if an instance was added before or after the
   1487 	 * removal of its parent.
   1488 	 *
   1489 	 * For active instances, i_level contains the number of devi removed so
   1490 	 * far, and for dead devis, it contains its index.
   1491 	 */
   1492 	i->i_level = devilevel++;
   1493 	i->i_alias = NULL;
   1494 	f = ht_lookup(deaddevitab, i->i_name);
   1495 	if (f == NULL) {
   1496 		if (ht_insert(deaddevitab, i->i_name, i))
   1497 			panic("remove_devi(%s) - can't add to deaddevitab",
   1498 			    i->i_name);
   1499 	} else {
   1500 		for (j = f; j->i_alias != NULL; j = j->i_alias)
   1501 			continue;
   1502 		j->i_alias = i;
   1503 	}
   1504 	/*
   1505 	 *   - reconstruct d->d_umax
   1506 	 */
   1507 	d->d_umax = 0;
   1508 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
   1509 		if (i->i_unit >= d->d_umax)
   1510 			d->d_umax = i->i_unit + 1;
   1511 }
   1512 
   1513 void
   1514 deldeva(const char *at, int nowarn)
   1515 {
   1516 	int unit;
   1517 	const char *cp;
   1518 	struct devbase *d, *ad;
   1519 	struct devi *i, *j;
   1520 	struct attr *a;
   1521 	struct pspec *p;
   1522 	struct nvlist *nv, *stack = NULL;
   1523 
   1524 	if (at == NULL) {
   1525 		TAILQ_FOREACH(i, &alldevi, i_next)
   1526 			if (i->i_at == NULL)
   1527 				stack = newnv(NULL, NULL, i, 0, stack);
   1528 	} else {
   1529 		size_t l;
   1530 
   1531 		CFGDBG(5, "deselecting deva `%s'", at);
   1532 		if (at[0] == '\0')
   1533 			goto out;
   1534 
   1535 		l = strlen(at) - 1;
   1536 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
   1537 			char base[NAMESIZE];
   1538 
   1539 			if (split(at, l+1, base, sizeof base, &unit)) {
   1540 out:
   1541 				cfgerror("invalid attachment name `%s'", at);
   1542 				return;
   1543 			}
   1544 			cp = intern(base);
   1545 		} else {
   1546 			cp = intern(at);
   1547 			unit = STAR;
   1548 		}
   1549 
   1550 		ad = ht_lookup(devbasetab, cp);
   1551 		a = ht_lookup(attrtab, cp);
   1552 		if (a == NULL) {
   1553 			cfgerror("unknown attachment attribute or device `%s'",
   1554 			    cp);
   1555 			return;
   1556 		}
   1557 		if (!a->a_iattr) {
   1558 			cfgerror("plain attribute `%s' cannot have children",
   1559 			    a->a_name);
   1560 			return;
   1561 		}
   1562 
   1563 		/*
   1564 		 * remove_devi() makes changes to the devbase's list and the
   1565 		 * alias list, * so the actual deletion of the instances must
   1566 		 * be delayed.
   1567 		 */
   1568 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
   1569 			d = nv->nv_ptr;
   1570 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
   1571 				for (j = i; j != NULL; j = j->i_alias) {
   1572 					/* Ignore devices at root */
   1573 					if (j->i_at == NULL)
   1574 						continue;
   1575 					p = j->i_pspec;
   1576 					/*
   1577 					 * There are three cases:
   1578 					 *
   1579 					 * 1.  unit is not STAR.  Consider 'at'
   1580 					 *     to be explicit, even if it
   1581 					 *     references an interface
   1582 					 *     attribute.
   1583 					 *
   1584 					 * 2.  unit is STAR and 'at' references
   1585 					 *     a real device.  Look for pspec
   1586 					 *     that have a matching p_atdev
   1587 					 *     field.
   1588 					 *
   1589 					 * 3.  unit is STAR and 'at' references
   1590 					 *     an interface attribute.  Look
   1591 					 *     for pspec that have a matching
   1592 					 *     p_iattr field.
   1593 					 */
   1594 					if ((unit != STAR &&        /* Case */
   1595 					     !strcmp(j->i_at, at)) ||  /* 1 */
   1596 					    (unit == STAR &&
   1597 					     ((ad != NULL &&        /* Case */
   1598 					       p->p_atdev == ad) ||    /* 2 */
   1599 					      (ad == NULL &&        /* Case */
   1600 					       p->p_iattr == a))))     /* 3 */
   1601 						stack = newnv(NULL, NULL, j, 0,
   1602 						    stack);
   1603 				}
   1604 		}
   1605 	}
   1606 
   1607 	devcleanup(stack);
   1608 }
   1609 
   1610 void
   1611 deldev(const char *name, int nowarn)
   1612 {
   1613 	size_t l;
   1614 	struct devi *firsti, *i;
   1615 	struct nvlist *stack = NULL;
   1616 
   1617 	CFGDBG(5, "deselecting dev `%s'", name);
   1618 	if (name[0] == '\0')
   1619 		goto out;
   1620 
   1621 	l = strlen(name) - 1;
   1622 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
   1623 		/* `no mydev0' or `no mydev*' */
   1624 		firsti = ht_lookup(devitab, name);
   1625 		if (firsti == NULL) {
   1626 out:
   1627 			if (!nowarn)
   1628 				cfgerror("unknown instance %s", name);
   1629 			return;
   1630 		}
   1631 		for (i = firsti; i != NULL; i = i->i_alias)
   1632 			stack = newnv(NULL, NULL, i, 0, stack);
   1633 	} else {
   1634 		struct devbase *d = ht_lookup(devbasetab, name);
   1635 
   1636 		if (d == NULL) {
   1637 			cfgerror("unknown device %s", name);
   1638 			return;
   1639 		}
   1640 		if (d->d_ispseudo) {
   1641 			cfgerror("%s is a pseudo-device; "
   1642 			    "use \"no pseudo-device %s\" instead", name,
   1643 			    name);
   1644 			return;
   1645 		}
   1646 		stack = makedevstack(d);
   1647 	}
   1648 
   1649 	devcleanup(stack);
   1650 }
   1651 
   1652 /*
   1653  * Insert given device "name" into devroottab.  In case "name"
   1654  * designates a pure interface attribute, create a fake device
   1655  * instance for the attribute and insert that into the roottab
   1656  * (this scheme avoids mucking around with the orphanage analysis).
   1657  */
   1658 void
   1659 addpseudoroot(const char *name)
   1660 {
   1661 	char buf[NAMESIZE];
   1662 	int unit;
   1663 	struct attr *attr;
   1664 	struct devi *i;
   1665 	struct deva *iba;
   1666 	struct devbase *ib;
   1667 
   1668 	if (split(name, strlen(name), buf, sizeof(buf), &unit)) {
   1669 		cfgerror("invalid pseudo-root name `%s'", name);
   1670 		return;
   1671 	}
   1672 
   1673 	/*
   1674 	 * Prefer device because devices with locators define an
   1675 	 * implicit interface attribute.  However, if a device is
   1676 	 * not available, try to attach to the interface attribute.
   1677 	 * This makes sure adddev() doesn't get confused when we
   1678 	 * are really attaching to a device (alternatively we maybe
   1679 	 * could specify a non-NULL atlist to defdevattach() below).
   1680 	 */
   1681 	ib = ht_lookup(devbasetab, intern(buf));
   1682 	if (ib == NULL) {
   1683 		struct devbase *fakedev;
   1684 		char fakename[NAMESIZE];
   1685 
   1686 		attr = ht_lookup(attrtab, intern(buf));
   1687 		if (!(attr && attr->a_iattr)) {
   1688 			cfgerror("pseudo-root `%s' not available", name);
   1689 			return;
   1690 		}
   1691 
   1692 		/*
   1693 		 * here we cheat a bit: create a fake devbase with the
   1694 		 * interface attribute and instantiate it.  quick, cheap,
   1695 		 * dirty & bad for you, much like the stuff in the fridge.
   1696 		 * and, it works, since the pseudoroot device is not included
   1697 		 * in ioconf, just used by config to make sure we start from
   1698 		 * the right place.
   1699 		 */
   1700 		snprintf(fakename, sizeof(fakename), "%s_devattrs", buf);
   1701 		fakedev = getdevbase(intern(fakename));
   1702 		fakedev->d_isdef = 1;
   1703 		fakedev->d_ispseudo = 0;
   1704 		fakedev->d_attrs = attrlist_cons(NULL, attr);
   1705 		defdevattach(NULL, fakedev, NULL, NULL);
   1706 
   1707 		if (unit == STAR)
   1708 			snprintf(buf, sizeof(buf), "%s*", fakename);
   1709 		else
   1710 			snprintf(buf, sizeof(buf), "%s%d", fakename, unit);
   1711 		name = buf;
   1712 	}
   1713 
   1714 	/* ok, everything should be set up, so instantiate a fake device */
   1715 	i = getdevi(name);
   1716 	if (i == NULL)
   1717 		panic("device `%s' expected to be present", name);
   1718 	ib = i->i_base;
   1719 	iba = ib->d_ahead;
   1720 
   1721 	i->i_atdeva = iba;
   1722 	i->i_cfflags = 0;
   1723 	i->i_locs = fixloc(name, &errattr, NULL);
   1724 	i->i_pseudoroot = 1;
   1725 	i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */
   1726 
   1727 	*iba->d_ipp = i;
   1728 	iba->d_ipp = &i->i_asame;
   1729 
   1730 	ht_insert(devroottab, ib->d_name, ib);
   1731 }
   1732 
   1733 static void
   1734 deldevbase(struct devbase *d)
   1735 {
   1736 	struct devi *i;
   1737 	const char *name = d->d_name;
   1738 
   1739 	if (!d->d_ispseudo) {
   1740 		devcleanup(makedevstack(d));
   1741 		return;
   1742 	}
   1743 
   1744 	if ((i = ht_lookup(devitab, name)) == NULL)
   1745 		return;
   1746 
   1747 	d->d_umax = 0;		/* clear neads-count entries */
   1748 	d->d_ihead = NULL;	/* make sure it won't be considered active */
   1749 	TAILQ_REMOVE(&allpseudo, i, i_next);
   1750 	if (ht_remove(devitab, name))
   1751 		panic("%s(%s) - can't remove from devitab", __func__, name);
   1752 	if (ht_insert(deaddevitab, name, i))
   1753 		panic("%s(%s) - can't add to deaddevitab", __func__, name);
   1754 }
   1755 
   1756 void
   1757 addpseudo(const char *name, int number)
   1758 {
   1759 	struct devbase *d;
   1760 	struct devi *i;
   1761 
   1762 	d = ht_lookup(devbasetab, name);
   1763 	if (d == NULL) {
   1764 		cfgerror("undefined pseudo-device %s", name);
   1765 		return;
   1766 	}
   1767 	if (!d->d_ispseudo) {
   1768 		cfgerror("%s is a real device, not a pseudo-device", name);
   1769 		return;
   1770 	}
   1771 	if (ht_lookup(devitab, name) != NULL) {
   1772 		cfgerror("`%s' already defined", name);
   1773 		return;
   1774 	}
   1775 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
   1776 	if (ht_insert(devitab, name, i))
   1777 		panic("addpseudo(%s)", name);
   1778 	/* Useful to retrieve the instance from the devbase */
   1779 	d->d_ihead = i;
   1780 	i->i_active = DEVI_ACTIVE;
   1781 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
   1782 }
   1783 
   1784 void
   1785 delpseudo(const char *name, int nowarn)
   1786 {
   1787 	struct devbase *d;
   1788 
   1789 	CFGDBG(5, "deselecting pseudo `%s'", name);
   1790 	d = ht_lookup(devbasetab, name);
   1791 	if (d == NULL) {
   1792 		if (!nowarn)
   1793 			cfgerror("undefined pseudo-device %s", name);
   1794 		return;
   1795 	}
   1796 	if (!d->d_ispseudo) {
   1797 		cfgerror("%s is a real device, not a pseudo-device", name);
   1798 		return;
   1799 	}
   1800 	deldevbase(d);
   1801 }
   1802 
   1803 void
   1804 adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
   1805 	struct condexpr *cond, struct nvlist *nv_nodes)
   1806 {
   1807 	struct devm *dm;
   1808 
   1809 	if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
   1810 		cfgerror("character major %d is invalid", cmajor);
   1811 		condexpr_destroy(cond);
   1812 		nvfreel(nv_nodes);
   1813 		return;
   1814 	}
   1815 
   1816 	if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
   1817 		cfgerror("block major %d is invalid", bmajor);
   1818 		condexpr_destroy(cond);
   1819 		nvfreel(nv_nodes);
   1820 		return;
   1821 	}
   1822 	if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
   1823 		cfgerror("both character/block majors are not specified");
   1824 		condexpr_destroy(cond);
   1825 		nvfreel(nv_nodes);
   1826 		return;
   1827 	}
   1828 
   1829 	dm = ecalloc(1, sizeof(*dm));
   1830 	dm->dm_srcfile = yyfile;
   1831 	dm->dm_srcline = currentline();
   1832 	dm->dm_name = name;
   1833 	dm->dm_cmajor = cmajor;
   1834 	dm->dm_bmajor = bmajor;
   1835 	dm->dm_opts = cond;
   1836 	dm->dm_devnodes = nv_nodes;
   1837 
   1838 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
   1839 
   1840 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
   1841 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
   1842 }
   1843 
   1844 int
   1845 fixdevis(void)
   1846 {
   1847 	struct devi *i;
   1848 	int error = 0;
   1849 
   1850 	TAILQ_FOREACH(i, &alldevi, i_next) {
   1851 		CFGDBG(3, "fixing devis `%s'", i->i_name);
   1852 		if (i->i_active == DEVI_ACTIVE)
   1853 			selectbase(i->i_base, i->i_atdeva);
   1854 		else if (i->i_active == DEVI_ORPHAN) {
   1855 			/*
   1856 			 * At this point, we can't have instances for which
   1857 			 * i_at or i_pspec are NULL.
   1858 			 */
   1859 			++error;
   1860 			cfgxerror(i->i_srcfile, i->i_lineno,
   1861 			    "`%s at %s' is orphaned (%s `%s' found)",
   1862 			    i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ?
   1863 			    "nothing matching" : "no", i->i_at);
   1864 		} else if (vflag && i->i_active == DEVI_IGNORED)
   1865 			cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
   1866 			    "explicitly orphaned instance `%s at %s'",
   1867 			    i->i_name, i->i_at);
   1868 	}
   1869 
   1870 	if (error)
   1871 		return error;
   1872 
   1873 	TAILQ_FOREACH(i, &allpseudo, i_next)
   1874 		if (i->i_active == DEVI_ACTIVE)
   1875 			selectbase(i->i_base, NULL);
   1876 	return 0;
   1877 }
   1878 
   1879 /*
   1880  * Look up a parent spec, creating a new one if it does not exist.
   1881  */
   1882 static struct pspec *
   1883 getpspec(struct attr *attr, struct devbase *ab, int atunit)
   1884 {
   1885 	struct pspec *p;
   1886 
   1887 	TAILQ_FOREACH(p, &allpspecs, p_list) {
   1888 		if (p->p_iattr == attr &&
   1889 		    p->p_atdev == ab &&
   1890 		    p->p_atunit == atunit)
   1891 			return (p);
   1892 	}
   1893 
   1894 	p = ecalloc(1, sizeof(*p));
   1895 
   1896 	p->p_iattr = attr;
   1897 	p->p_atdev = ab;
   1898 	p->p_atunit = atunit;
   1899 	p->p_inst = npspecs++;
   1900 	p->p_active = 0;
   1901 
   1902 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
   1903 
   1904 	return (p);
   1905 }
   1906 
   1907 /*
   1908  * Define a new instance of a specific device.
   1909  */
   1910 static struct devi *
   1911 getdevi(const char *name)
   1912 {
   1913 	struct devi *i, *firsti;
   1914 	struct devbase *d;
   1915 	int unit;
   1916 	char base[NAMESIZE];
   1917 
   1918 	if (split(name, strlen(name), base, sizeof base, &unit)) {
   1919 		cfgerror("invalid device name `%s'", name);
   1920 		return (NULL);
   1921 	}
   1922 	d = ht_lookup(devbasetab, intern(base));
   1923 	if (d == NULL) {
   1924 		cfgerror("%s: unknown device `%s'", name, base);
   1925 		return (NULL);
   1926 	}
   1927 	if (d->d_ispseudo) {
   1928 		cfgerror("%s: %s is a pseudo-device", name, base);
   1929 		return (NULL);
   1930 	}
   1931 	firsti = ht_lookup(devitab, name);
   1932 	i = newdevi(name, unit, d);
   1933 	if (firsti == NULL) {
   1934 		if (ht_insert(devitab, name, i))
   1935 			panic("getdevi(%s)", name);
   1936 		*d->d_ipp = i;
   1937 		d->d_ipp = &i->i_bsame;
   1938 	} else {
   1939 		while (firsti->i_alias)
   1940 			firsti = firsti->i_alias;
   1941 		firsti->i_alias = i;
   1942 	}
   1943 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
   1944 	ndevi++;
   1945 	return (i);
   1946 }
   1947 
   1948 static const char *
   1949 concat(const char *name, int c)
   1950 {
   1951 	size_t len;
   1952 	char buf[NAMESIZE];
   1953 
   1954 	len = strlen(name);
   1955 	if (len + 2 > sizeof(buf)) {
   1956 		cfgerror("device name `%s%c' too long", name, c);
   1957 		len = sizeof(buf) - 2;
   1958 	}
   1959 	memmove(buf, name, len);
   1960 	buf[len] = (char)c;
   1961 	buf[len + 1] = '\0';
   1962 	return (intern(buf));
   1963 }
   1964 
   1965 const char *
   1966 starref(const char *name)
   1967 {
   1968 
   1969 	return (concat(name, '*'));
   1970 }
   1971 
   1972 const char *
   1973 wildref(const char *name)
   1974 {
   1975 
   1976 	return (concat(name, '?'));
   1977 }
   1978 
   1979 /*
   1980  * Split a name like "foo0" into base name (foo) and unit number (0).
   1981  * Return 0 on success.  To make this useful for names like "foo0a",
   1982  * the length of the "foo0" part is one of the arguments.
   1983  */
   1984 static int
   1985 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
   1986 {
   1987 	const char *cp;
   1988 	int c;
   1989 	size_t l;
   1990 
   1991 	l = nlen;
   1992 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
   1993 		return (1);
   1994 	c = (u_char)name[--l];
   1995 	if (!isdigit(c)) {
   1996 		if (c == '*')
   1997 			*aunit = STAR;
   1998 		else if (c == '?')
   1999 			*aunit = WILD;
   2000 		else
   2001 			return (1);
   2002 	} else {
   2003 		cp = &name[l];
   2004 		while (isdigit((unsigned char)cp[-1]))
   2005 			l--, cp--;
   2006 		*aunit = atoi(cp);
   2007 	}
   2008 	memmove(base, name, l);
   2009 	base[l] = 0;
   2010 	return (0);
   2011 }
   2012 
   2013 void
   2014 addattr(const char *name)
   2015 {
   2016 	struct attr *a;
   2017 
   2018 	a = refattr(name);
   2019 	selectattr(a);
   2020 }
   2021 
   2022 void
   2023 delattr(const char *name, int nowarn)
   2024 {
   2025 	struct attr *a;
   2026 
   2027 	a = refattr(name);
   2028 	deselectattr(a);
   2029 }
   2030 
   2031 void
   2032 selectattr(struct attr *a)
   2033 {
   2034 	struct attrlist *al;
   2035 	struct attr *dep;
   2036 
   2037 	CFGDBG(5, "selecting attr `%s'", a->a_name);
   2038 	for (al = a->a_deps; al != NULL; al = al->al_next) {
   2039 		dep = al->al_this;
   2040 		selectattr(dep);
   2041 	}
   2042 	if (ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)) == 0)
   2043 		nattrs++;
   2044 	CFGDBG(3, "attr selected `%s'", a->a_name);
   2045 }
   2046 
   2047 static int
   2048 deselectattrcb2(const char *name1, const char *name2, void *v, void *arg)
   2049 {
   2050 	struct attr *a = arg;
   2051 	const char *name = a->a_name;
   2052 	struct vtype *vt = v;
   2053 
   2054 	if (strcmp(name, name2) == 0) {
   2055 		delattr(name1, 0);
   2056 		return 0;
   2057 	}
   2058 
   2059 	if (!vt->attr->a_deselected)
   2060 		return 0;
   2061 
   2062 	switch (vt->type) {
   2063 	case V_ATTRIBUTE:
   2064 #ifdef notyet
   2065 		// XXX: Loops
   2066 		deselectattr(vt->value);
   2067 #endif
   2068 		break;
   2069 	case V_DEVICE:
   2070 		CFGDBG(5, "removing device `%s' with attr `%s' because attr `%s'"
   2071 		    " is deselected", name1, name2, name);
   2072 		deldevbase(vt->value);
   2073 		break;
   2074 	default:
   2075 		abort();
   2076 	}
   2077 	return 0;
   2078 }
   2079 
   2080 void
   2081 deselectattr(struct attr *a)
   2082 {
   2083 	CFGDBG(5, "deselecting attr `%s'", a->a_name);
   2084 	a->a_deselected = 1;
   2085 	ht_enumerate2(attrdeptab, deselectattrcb2, a);
   2086 	if (ht_remove(selecttab, a->a_name) == 0)
   2087 		nattrs--;
   2088 	CFGDBG(3, "attr deselected `%s'", a->a_name);
   2089 }
   2090 
   2091 static int
   2092 dumpattrdepcb2(const char *name1, const char *name2, void *v, void *arg)
   2093 {
   2094 
   2095 	CFGDBG(3, "attr `%s' depends on attr `%s'", name1, name2);
   2096 	return 0;
   2097 }
   2098 
   2099 void
   2100 dependattrs(void)
   2101 {
   2102 
   2103 	ht_enumerate2(attrdeptab, dumpattrdepcb2, NULL);
   2104 }
   2105 
   2106 /*
   2107  * We have an instance of the base foo, so select it and all its
   2108  * attributes for "optional foo".
   2109  */
   2110 static void
   2111 selectbase(struct devbase *d, struct deva *da)
   2112 {
   2113 	struct attr *a;
   2114 	struct attrlist *al;
   2115 
   2116 	(void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
   2117 	CFGDBG(3, "devbase selected `%s'", d->d_name);
   2118 	CFGDBG(5, "selecting dependencies of devbase `%s'", d->d_name);
   2119 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
   2120 		a = al->al_this;
   2121 		expandattr(a, selectattr);
   2122 	}
   2123 
   2124 	struct attr *devattr;
   2125 	devattr = refattr(d->d_name);
   2126 	expandattr(devattr, selectattr);
   2127 
   2128 	if (da != NULL) {
   2129 		(void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
   2130 		CFGDBG(3, "devattr selected `%s'", da->d_name);
   2131 		for (al = da->d_attrs; al != NULL; al = al->al_next) {
   2132 			a = al->al_this;
   2133 			expandattr(a, selectattr);
   2134 		}
   2135 	}
   2136 
   2137 	fixdev(d);
   2138 }
   2139 
   2140 /*
   2141  * Is the given pointer on the given list of pointers?
   2142  */
   2143 int
   2144 onlist(struct nvlist *nv, void *ptr)
   2145 {
   2146 	for (; nv != NULL; nv = nv->nv_next)
   2147 		if (nv->nv_ptr == ptr)
   2148 			return (1);
   2149 	return (0);
   2150 }
   2151 
   2152 static char *
   2153 extend(char *p, const char *name)
   2154 {
   2155 	size_t l;
   2156 
   2157 	l = strlen(name);
   2158 	memmove(p, name, l);
   2159 	p += l;
   2160 	*p++ = ',';
   2161 	*p++ = ' ';
   2162 	return (p);
   2163 }
   2164 
   2165 /*
   2166  * Check that we got all required locators, and default any that are
   2167  * given as "?" and have defaults.  Return 0 on success.
   2168  */
   2169 static const char **
   2170 fixloc(const char *name, struct attr *attr, struct loclist *got)
   2171 {
   2172 	struct loclist *m, *n;
   2173 	int ord;
   2174 	const char **lp;
   2175 	int nmissing, nextra, nnodefault;
   2176 	char *mp, *ep, *ndp;
   2177 	char missing[1000], extra[1000], nodefault[1000];
   2178 	static const char *nullvec[1];
   2179 
   2180 	/*
   2181 	 * Look for all required locators, and number the given ones
   2182 	 * according to the required order.  While we are numbering,
   2183 	 * set default values for defaulted locators.
   2184 	 */
   2185 	if (attr->a_loclen == 0)	/* e.g., "at root" */
   2186 		lp = nullvec;
   2187 	else
   2188 		lp = emalloc((size_t)(attr->a_loclen + 1) * sizeof(const char *));
   2189 	for (n = got; n != NULL; n = n->ll_next)
   2190 		n->ll_num = -1;
   2191 	nmissing = 0;
   2192 	mp = missing;
   2193 	/* yes, this is O(mn), but m and n should be small */
   2194 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) {
   2195 		for (n = got; n != NULL; n = n->ll_next) {
   2196 			if (n->ll_name == m->ll_name) {
   2197 				n->ll_num = ord;
   2198 				break;
   2199 			}
   2200 		}
   2201 		if (n == NULL && m->ll_num == 0) {
   2202 			nmissing++;
   2203 			mp = extend(mp, m->ll_name);
   2204 		}
   2205 		lp[ord] = m->ll_string;
   2206 	}
   2207 	if (ord != attr->a_loclen)
   2208 		panic("fixloc");
   2209 	lp[ord] = NULL;
   2210 	nextra = 0;
   2211 	ep = extra;
   2212 	nnodefault = 0;
   2213 	ndp = nodefault;
   2214 	for (n = got; n != NULL; n = n->ll_next) {
   2215 		if (n->ll_num >= 0) {
   2216 			if (n->ll_string != NULL)
   2217 				lp[n->ll_num] = n->ll_string;
   2218 			else if (lp[n->ll_num] == NULL) {
   2219 				nnodefault++;
   2220 				ndp = extend(ndp, n->ll_name);
   2221 			}
   2222 		} else {
   2223 			nextra++;
   2224 			ep = extend(ep, n->ll_name);
   2225 		}
   2226 	}
   2227 	if (nextra) {
   2228 		ep[-2] = 0;	/* kill ", " */
   2229 		cfgerror("%s: extraneous locator%s: %s",
   2230 		    name, nextra > 1 ? "s" : "", extra);
   2231 	}
   2232 	if (nmissing) {
   2233 		mp[-2] = 0;
   2234 		cfgerror("%s: must specify %s", name, missing);
   2235 	}
   2236 	if (nnodefault) {
   2237 		ndp[-2] = 0;
   2238 		cfgerror("%s: cannot wildcard %s", name, nodefault);
   2239 	}
   2240 	if (nmissing || nnodefault) {
   2241 		free(lp);
   2242 		lp = NULL;
   2243 	}
   2244 	return (lp);
   2245 }
   2246 
   2247 void
   2248 setversion(int newver)
   2249 {
   2250 	if (newver > CONFIG_VERSION)
   2251 		cfgerror("your sources require a newer version of config(1) "
   2252 		    "-- please rebuild it.");
   2253 	else if (newver < CONFIG_MINVERSION)
   2254 		cfgerror("your sources are out of date -- please update.");
   2255 	else
   2256 		version = newver;
   2257 }
   2258