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