Home | History | Annotate | Line # | Download | only in config
sem.c revision 1.3
      1 /*	$NetBSD: sem.c,v 1.3 2005/07/25 06:22:09 cube 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 "defs.h"
     53 #include "sem.h"
     54 
     55 /*
     56  * config semantics.
     57  */
     58 
     59 #define	NAMESIZE	100	/* local name buffers */
     60 
     61 const char *s_ifnet;		/* magic attribute */
     62 const char *s_qmark;
     63 const char *s_none;
     64 
     65 static struct hashtab *cfhashtab;	/* for config lookup */
     66 struct hashtab *devitab;		/* etc */
     67 
     68 static struct attr errattr;
     69 static struct devbase errdev;
     70 static struct deva errdeva;
     71 
     72 static int has_errobj(struct nvlist *, void *);
     73 static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
     74 static int resolve(struct nvlist **, const char *, const char *,
     75 		   struct nvlist *, int);
     76 static struct pspec *getpspec(struct attr *, struct devbase *, int);
     77 static struct devi *newdevi(const char *, int, struct devbase *d);
     78 static struct devi *getdevi(const char *);
     79 static const char *concat(const char *, int);
     80 static char *extend(char *, const char *);
     81 static int split(const char *, size_t, char *, size_t, int *);
     82 static void selectbase(struct devbase *, struct deva *);
     83 static int onlist(struct nvlist *, void *);
     84 static const char **fixloc(const char *, struct attr *, struct nvlist *);
     85 static const char *makedevstr(int, int);
     86 static const char *major2name(int);
     87 static int dev2major(struct devbase *);
     88 
     89 extern const char *yyfile;
     90 
     91 void
     92 initsem(void)
     93 {
     94 
     95 	attrtab = ht_new();
     96 	errattr.a_name = "<internal>";
     97 
     98 	TAILQ_INIT(&allbases);
     99 
    100 	TAILQ_INIT(&alldevas);
    101 
    102 	TAILQ_INIT(&allpspecs);
    103 
    104 	cfhashtab = ht_new();
    105 	TAILQ_INIT(&allcf);
    106 
    107 	TAILQ_INIT(&alldevi);
    108 	errdev.d_name = "<internal>";
    109 
    110 	TAILQ_INIT(&allpseudo);
    111 
    112 	TAILQ_INIT(&alldevms);
    113 
    114 	s_ifnet = intern("ifnet");
    115 	s_qmark = intern("?");
    116 	s_none = intern("none");
    117 }
    118 
    119 /* Name of include file just ended (set in scan.l) */
    120 extern const char *lastfile;
    121 
    122 void
    123 enddefs(void)
    124 {
    125 	struct devbase *dev;
    126 
    127 	TAILQ_FOREACH(dev, &allbases, d_next) {
    128 		if (!dev->d_isdef) {
    129 			(void)fprintf(stderr,
    130 			    "%s: device `%s' used but not defined\n",
    131 			    lastfile, dev->d_name);
    132 			errors++;
    133 			continue;
    134 		}
    135 	}
    136 	if (errors) {
    137 		(void)fprintf(stderr, "*** Stop.\n");
    138 		exit(1);
    139 	}
    140 }
    141 
    142 void
    143 setdefmaxusers(int min, int def, int max)
    144 {
    145 
    146 	if (min < 1 || min > def || def > max)
    147 		error("maxusers must have 1 <= min (%d) <= default (%d) <= max (%d)", min, def, max);
    148 	else {
    149 		minmaxusers = min;
    150 		defmaxusers = def;
    151 		maxmaxusers = max;
    152 	}
    153 }
    154 
    155 void
    156 setmaxusers(int n)
    157 {
    158 
    159 	if (maxusers != 0) {
    160 		error("duplicate maxusers parameter");
    161 		return;
    162 	}
    163 	maxusers = n;
    164 	if (n < minmaxusers) {
    165 		error("warning: minimum of %d maxusers assumed", minmaxusers);
    166 		errors--;	/* take it away */
    167 		maxusers = minmaxusers;
    168 	} else if (n > maxmaxusers) {
    169 		error("warning: maxusers (%d) > %d", n, maxmaxusers);
    170 		errors--;
    171 	}
    172 }
    173 
    174 void
    175 setident(const char *i)
    176 {
    177 
    178 	ident = intern(i);
    179 }
    180 
    181 /*
    182  * Define an attribute, optionally with an interface (a locator list)
    183  * and a set of attribute-dependencies.
    184  *
    185  * Attribute dependencies MAY NOT be interface attributes.
    186  *
    187  * Since an empty locator list is logically different from "no interface",
    188  * all locator lists include a dummy head node, which we discard here.
    189  */
    190 int
    191 defattr(const char *name, struct nvlist *locs, struct nvlist *deps,
    192     int devclass)
    193 {
    194 	struct attr *a, *dep;
    195 	struct nvlist *nv;
    196 	int len;
    197 
    198 	if (locs != NULL && devclass)
    199 		panic("defattr(%s): locators and devclass", name);
    200 
    201 	if (deps != NULL && devclass)
    202 		panic("defattr(%s): dependencies and devclass", name);
    203 
    204 	/*
    205 	 * If this attribute depends on any others, make sure none of
    206 	 * the dependencies are interface attributes.
    207 	 */
    208 	for (nv = deps; nv != NULL; nv = nv->nv_next) {
    209 		dep = nv->nv_ptr;
    210 		if (dep->a_iattr) {
    211 			error("`%s' dependency `%s' is an interface attribute",
    212 			    name, dep->a_name);
    213 			return (1);
    214 		}
    215 	}
    216 
    217 	a = ecalloc(1, sizeof *a);
    218 	if (ht_insert(attrtab, name, a)) {
    219 		free(a);
    220 		error("attribute `%s' already defined", name);
    221 		nvfreel(locs);
    222 		return (1);
    223 	}
    224 
    225 	a->a_name = name;
    226 	if (locs != NULL) {
    227 		a->a_iattr = 1;
    228 		a->a_locs = locs->nv_next;
    229 		nvfree(locs);
    230 	} else {
    231 		a->a_iattr = 0;
    232 		a->a_locs = NULL;
    233 	}
    234 	if (devclass) {
    235 		size_t l = strlen(name) + 4;
    236 		char *classenum = alloca(l), *cp;
    237 		int errored = 0;
    238 
    239 		strlcpy(classenum, "DV_", l);
    240 		strlcat(classenum, name, l);
    241 		for (cp = classenum + 3; *cp; cp++) {
    242 			if (!errored &&
    243 			    (!isalnum((unsigned char)*cp) ||
    244 			      (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) {
    245 				error("device class names must be lower-case alphanumeric characters");
    246 				errored = 1;
    247 			}
    248 			*cp = toupper((unsigned char)*cp);
    249 		}
    250 		a->a_devclass = intern(classenum);
    251 	} else
    252 		a->a_devclass = NULL;
    253 	len = 0;
    254 	for (nv = a->a_locs; nv != NULL; nv = nv->nv_next)
    255 		len++;
    256 	a->a_loclen = len;
    257 	a->a_devs = NULL;
    258 	a->a_refs = NULL;
    259 	a->a_deps = deps;
    260 	a->a_expanding = 0;
    261 
    262 	/* Expand the attribute to check for cycles in the graph. */
    263 	expandattr(a, NULL);
    264 
    265 	return (0);
    266 }
    267 
    268 /*
    269  * Return true if the given `error object' is embedded in the given
    270  * pointer list.
    271  */
    272 static int
    273 has_errobj(struct nvlist *nv, void *obj)
    274 {
    275 
    276 	for (; nv != NULL; nv = nv->nv_next)
    277 		if (nv->nv_ptr == obj)
    278 			return (1);
    279 	return (0);
    280 }
    281 
    282 /*
    283  * Return true if the given attribute is embedded in the given
    284  * pointer list.
    285  */
    286 int
    287 has_attr(struct nvlist *nv, const char *attr)
    288 {
    289 	struct attr *a;
    290 
    291 	if ((a = getattr(attr)) == NULL)
    292 		return (0);
    293 
    294 	for (; nv != NULL; nv = nv->nv_next)
    295 		if (nv->nv_ptr == a)
    296 			return (1);
    297 	return (0);
    298 }
    299 
    300 /*
    301  * Add a device base to a list in an attribute (actually, to any list).
    302  * Note that this does not check for duplicates, and does reverse the
    303  * list order, but no one cares anyway.
    304  */
    305 static struct nvlist *
    306 addtoattr(struct nvlist *l, struct devbase *dev)
    307 {
    308 	struct nvlist *n;
    309 
    310 	n = newnv(NULL, NULL, dev, 0, l);
    311 	return (n);
    312 }
    313 
    314 /*
    315  * Define a device.  This may (or may not) also define an interface
    316  * attribute and/or refer to existing attributes.
    317  */
    318 void
    319 defdev(struct devbase *dev, struct nvlist *loclist, struct nvlist *attrs,
    320        int ispseudo)
    321 {
    322 	struct nvlist *nv;
    323 	struct attr *a;
    324 
    325 	if (dev == &errdev)
    326 		goto bad;
    327 	if (dev->d_isdef) {
    328 		error("redefinition of `%s'", dev->d_name);
    329 		goto bad;
    330 	}
    331 
    332 	dev->d_isdef = 1;
    333 	if (has_errobj(attrs, &errattr))
    334 		goto bad;
    335 
    336 	/*
    337 	 * Handle implicit attribute definition from locator list.  Do
    338 	 * this before scanning the `at' list so that we can have, e.g.:
    339 	 *	device foo at other, foo { slot = -1 }
    340 	 * (where you can plug in a foo-bus extender to a foo-bus).
    341 	 */
    342 	if (loclist != NULL) {
    343 		nv = loclist;
    344 		loclist = NULL;	/* defattr disposes of them for us */
    345 		if (defattr(dev->d_name, nv, NULL, 0))
    346 			goto bad;
    347 		attrs = newnv(dev->d_name, NULL, getattr(dev->d_name), 0,
    348 		    attrs);
    349 	}
    350 
    351 	/* Committed!  Set up fields. */
    352 	dev->d_ispseudo = ispseudo;
    353 	dev->d_attrs = attrs;
    354 	dev->d_classattr = NULL;		/* for now */
    355 
    356 	/*
    357 	 * For each interface attribute this device refers to, add this
    358 	 * device to its reference list.  This makes, e.g., finding all
    359 	 * "scsi"s easier.
    360 	 *
    361 	 * While looking through the attributes, set up the device
    362 	 * class if any are devclass attributes (and error out if the
    363 	 * device has two classes).
    364 	 */
    365 	for (nv = attrs; nv != NULL; nv = nv->nv_next) {
    366 		a = nv->nv_ptr;
    367 		if (a->a_iattr)
    368 			a->a_refs = addtoattr(a->a_refs, dev);
    369 		if (a->a_devclass != NULL) {
    370 			if (dev->d_classattr != NULL) {
    371 				error("device `%s' has multiple classes (`%s' and `%s')",
    372 				    dev->d_name, dev->d_classattr->a_name,
    373 				    a->a_name);
    374 			}
    375 			dev->d_classattr = a;
    376 		}
    377 	}
    378 	return;
    379  bad:
    380 	nvfreel(loclist);
    381 	nvfreel(attrs);
    382 }
    383 
    384 /*
    385  * Look up a devbase.  Also makes sure it is a reasonable name,
    386  * i.e., does not end in a digit or contain special characters.
    387  */
    388 struct devbase *
    389 getdevbase(const char *name)
    390 {
    391 	u_char *p;
    392 	struct devbase *dev;
    393 
    394 	p = (u_char *)name;
    395 	if (!isalpha(*p))
    396 		goto badname;
    397 	while (*++p) {
    398 		if (!isalnum(*p) && *p != '_')
    399 			goto badname;
    400 	}
    401 	if (isdigit(*--p)) {
    402  badname:
    403 		error("bad device base name `%s'", name);
    404 		return (&errdev);
    405 	}
    406 	dev = ht_lookup(devbasetab, name);
    407 	if (dev == NULL) {
    408 		dev = ecalloc(1, sizeof *dev);
    409 		dev->d_name = name;
    410 		dev->d_isdef = 0;
    411 		dev->d_major = NODEV;
    412 		dev->d_attrs = NULL;
    413 		dev->d_ihead = NULL;
    414 		dev->d_ipp = &dev->d_ihead;
    415 		dev->d_ahead = NULL;
    416 		dev->d_app = &dev->d_ahead;
    417 		dev->d_umax = 0;
    418 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
    419 		if (ht_insert(devbasetab, name, dev))
    420 			panic("getdevbase(%s)", name);
    421 	}
    422 	return (dev);
    423 }
    424 
    425 /*
    426  * Define some of a device's allowable parent attachments.
    427  * There may be a list of (plain) attributes.
    428  */
    429 void
    430 defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
    431 	     struct nvlist *attrs)
    432 {
    433 	struct nvlist *nv;
    434 	struct attr *a;
    435 	struct deva *da;
    436 
    437 	if (dev == &errdev)
    438 		goto bad;
    439 	if (deva == NULL)
    440 		deva = getdevattach(dev->d_name);
    441 	if (deva == &errdeva)
    442 		goto bad;
    443 	if (!dev->d_isdef) {
    444 		error("attaching undefined device `%s'", dev->d_name);
    445 		goto bad;
    446 	}
    447 	if (deva->d_isdef) {
    448 		error("redefinition of `%s'", deva->d_name);
    449 		goto bad;
    450 	}
    451 	if (dev->d_ispseudo) {
    452 		error("pseudo-devices can't attach");
    453 		goto bad;
    454 	}
    455 
    456 	deva->d_isdef = 1;
    457 	if (has_errobj(attrs, &errattr))
    458 		goto bad;
    459 	for (nv = attrs; nv != NULL; nv = nv->nv_next) {
    460 		a = nv->nv_ptr;
    461 		if (a == &errattr)
    462 			continue;		/* already complained */
    463 		if (a->a_iattr || a->a_devclass != NULL)
    464 			error("`%s' is not a plain attribute", a->a_name);
    465 	}
    466 
    467 	/* Committed!  Set up fields. */
    468 	deva->d_attrs = attrs;
    469 	deva->d_atlist = atlist;
    470 	deva->d_devbase = dev;
    471 
    472 	/*
    473 	 * Turn the `at' list into interface attributes (map each
    474 	 * nv_name to an attribute, or to NULL for root), and add
    475 	 * this device to those attributes, so that children can
    476 	 * be listed at this particular device if they are supported
    477 	 * by that attribute.
    478 	 */
    479 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
    480 		if (nv->nv_name == NULL)
    481 			nv->nv_ptr = a = NULL;	/* at root */
    482 		else
    483 			nv->nv_ptr = a = getattr(nv->nv_name);
    484 		if (a == &errattr)
    485 			continue;		/* already complained */
    486 
    487 		/*
    488 		 * Make sure that an attachment spec doesn't
    489 		 * already say how to attach to this attribute.
    490 		 */
    491 		for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
    492 			if (onlist(da->d_atlist, a))
    493 				error("attach at `%s' already done by `%s'",
    494 				     a ? a->a_name : "root", da->d_name);
    495 
    496 		if (a == NULL)
    497 			continue;		/* at root; don't add */
    498 		if (!a->a_iattr)
    499 			error("%s cannot be at plain attribute `%s'",
    500 			    dev->d_name, a->a_name);
    501 		else
    502 			a->a_devs = addtoattr(a->a_devs, dev);
    503 	}
    504 
    505 	/* attach to parent */
    506 	*dev->d_app = deva;
    507 	dev->d_app = &deva->d_bsame;
    508 	return;
    509  bad:
    510 	nvfreel(atlist);
    511 	nvfreel(attrs);
    512 }
    513 
    514 /*
    515  * Look up a device attachment.  Also makes sure it is a reasonable
    516  * name, i.e., does not contain digits or special characters.
    517  */
    518 struct deva *
    519 getdevattach(const char *name)
    520 {
    521 	u_char *p;
    522 	struct deva *deva;
    523 
    524 	p = (u_char *)name;
    525 	if (!isalpha(*p))
    526 		goto badname;
    527 	while (*++p) {
    528 		if (!isalnum(*p) && *p != '_')
    529 			goto badname;
    530 	}
    531 	if (isdigit(*--p)) {
    532  badname:
    533 		error("bad device attachment name `%s'", name);
    534 		return (&errdeva);
    535 	}
    536 	deva = ht_lookup(devatab, name);
    537 	if (deva == NULL) {
    538 		deva = ecalloc(1, sizeof *deva);
    539 		deva->d_name = name;
    540 		deva->d_bsame = NULL;
    541 		deva->d_isdef = 0;
    542 		deva->d_devbase = NULL;
    543 		deva->d_atlist = NULL;
    544 		deva->d_attrs = NULL;
    545 		deva->d_ihead = NULL;
    546 		deva->d_ipp = &deva->d_ihead;
    547 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
    548 		if (ht_insert(devatab, name, deva))
    549 			panic("getdeva(%s)", name);
    550 	}
    551 	return (deva);
    552 }
    553 
    554 /*
    555  * Look up an attribute.
    556  */
    557 struct attr *
    558 getattr(const char *name)
    559 {
    560 	struct attr *a;
    561 
    562 	if ((a = ht_lookup(attrtab, name)) == NULL) {
    563 		error("undefined attribute `%s'", name);
    564 		a = &errattr;
    565 	}
    566 	return (a);
    567 }
    568 
    569 /*
    570  * Recursively expand an attribute and its dependencies, checking for
    571  * cycles, and invoking a callback for each attribute found.
    572  */
    573 void
    574 expandattr(struct attr *a, void (*callback)(struct attr *))
    575 {
    576 	struct nvlist *nv;
    577 	struct attr *dep;
    578 
    579 	if (a->a_expanding) {
    580 		error("circular dependency on attribute `%s'", a->a_name);
    581 		return;
    582 	}
    583 
    584 	a->a_expanding = 1;
    585 
    586 	/* First expand all of this attribute's dependencies. */
    587 	for (nv = a->a_deps; nv != NULL; nv = nv->nv_next) {
    588 		dep = nv->nv_ptr;
    589 		expandattr(dep, callback);
    590 	}
    591 
    592 	/* ...and now invoke the callback for ourself. */
    593 	if (callback != NULL)
    594 		(*callback)(a);
    595 
    596 	a->a_expanding = 0;
    597 }
    598 
    599 /*
    600  * Set the major device number for a device, so that it can be used
    601  * as a root/dumps "on" device in a configuration.
    602  */
    603 void
    604 setmajor(struct devbase *d, int n)
    605 {
    606 
    607 	if (d != &errdev && d->d_major != NODEV)
    608 		error("device `%s' is already major %d",
    609 		    d->d_name, d->d_major);
    610 	else
    611 		d->d_major = n;
    612 }
    613 
    614 const char *
    615 major2name(int maj)
    616 {
    617 	struct devbase *dev;
    618 	struct devm *dm;
    619 
    620 	if (!do_devsw) {
    621 		TAILQ_FOREACH(dev, &allbases, d_next) {
    622 			if (dev->d_major == maj)
    623 				return (dev->d_name);
    624 		}
    625 	} else {
    626 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
    627 			if (dm->dm_bmajor == maj)
    628 				return (dm->dm_name);
    629 		}
    630 	}
    631 	return (NULL);
    632 }
    633 
    634 int
    635 dev2major(struct devbase *dev)
    636 {
    637 	struct devm *dm;
    638 
    639 	if (!do_devsw)
    640 		return (dev->d_major);
    641 
    642 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
    643 		if (strcmp(dm->dm_name, dev->d_name) == 0)
    644 			return (dm->dm_bmajor);
    645 	}
    646 	return (NODEV);
    647 }
    648 
    649 /*
    650  * Make a string description of the device at maj/min.
    651  */
    652 static const char *
    653 makedevstr(int maj, int min)
    654 {
    655 	const char *devname;
    656 	char buf[32];
    657 
    658 	devname = major2name(maj);
    659 	if (devname == NULL)
    660 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
    661 	else
    662 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devname,
    663 		    min / maxpartitions, (min % maxpartitions) + 'a');
    664 
    665 	return (intern(buf));
    666 }
    667 
    668 /*
    669  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
    670  * Handle the case where the device number is given but there is no
    671  * corresponding name, and map NULL to the default.
    672  */
    673 static int
    674 resolve(struct nvlist **nvp, const char *name, const char *what,
    675 	struct nvlist *dflt, int part)
    676 {
    677 	struct nvlist *nv;
    678 	struct devbase *dev;
    679 	const char *cp;
    680 	int maj, min, i, l;
    681 	int unit;
    682 	char buf[NAMESIZE];
    683 
    684 	if ((u_int)(part -= 'a') >= maxpartitions)
    685 		panic("resolve");
    686 	if ((nv = *nvp) == NULL) {
    687 		dev_t	d = NODEV;
    688 		/*
    689 		 * Apply default.  Easiest to do this by number.
    690 		 * Make sure to retain NODEVness, if this is dflt's disposition.
    691 		 */
    692 		if (dflt->nv_int != NODEV) {
    693 			maj = major(dflt->nv_int);
    694 			min = ((minor(dflt->nv_int) / maxpartitions) *
    695 			    maxpartitions) + part;
    696 			d = makedev(maj, min);
    697 			cp = makedevstr(maj, min);
    698 		} else
    699 			cp = NULL;
    700 		*nvp = nv = newnv(NULL, cp, NULL, d, NULL);
    701 	}
    702 	if (nv->nv_int != NODEV) {
    703 		/*
    704 		 * By the numbers.  Find the appropriate major number
    705 		 * to make a name.
    706 		 */
    707 		maj = major(nv->nv_int);
    708 		min = minor(nv->nv_int);
    709 		nv->nv_str = makedevstr(maj, min);
    710 		return (0);
    711 	}
    712 
    713 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
    714 		/*
    715 		 * Wildcarded or unspecified; leave it as NODEV.
    716 		 */
    717 		return (0);
    718 
    719 	/*
    720 	 * The normal case: things like "ra2b".  Check for partition
    721 	 * suffix, remove it if there, and split into name ("ra") and
    722 	 * unit (2).
    723 	 */
    724 	l = i = strlen(nv->nv_str);
    725 	cp = &nv->nv_str[l];
    726 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
    727 	    isdigit((unsigned char)cp[-1])) {
    728 		l--;
    729 		part = *cp - 'a';
    730 	}
    731 	cp = nv->nv_str;
    732 	if (split(cp, l, buf, sizeof buf, &unit)) {
    733 		error("%s: invalid %s device name `%s'", name, what, cp);
    734 		return (1);
    735 	}
    736 	dev = ht_lookup(devbasetab, intern(buf));
    737 	if (dev == NULL) {
    738 		error("%s: device `%s' does not exist", name, buf);
    739 		return (1);
    740 	}
    741 
    742 	/*
    743 	 * Check for the magic network interface attribute, and
    744 	 * don't bother making a device number.
    745 	 */
    746 	if (has_attr(dev->d_attrs, s_ifnet)) {
    747 		nv->nv_int = NODEV;
    748 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
    749 	} else {
    750 		maj = dev2major(dev);
    751 		if (maj == NODEV) {
    752 			error("%s: can't make %s device from `%s'",
    753 			    name, what, nv->nv_str);
    754 			return (1);
    755 		}
    756 		nv->nv_int = makedev(maj, unit * maxpartitions + part);
    757 	}
    758 
    759 	nv->nv_name = dev->d_name;
    760 	return (0);
    761 }
    762 
    763 /*
    764  * Add a completed configuration to the list.
    765  */
    766 void
    767 addconf(struct config *cf0)
    768 {
    769 	struct config *cf;
    770 	struct nvlist *nv;
    771 	const char *name;
    772 
    773 	name = cf0->cf_name;
    774 	cf = ecalloc(1, sizeof *cf);
    775 	if (ht_insert(cfhashtab, name, cf)) {
    776 		error("configuration `%s' already defined", name);
    777 		free(cf);
    778 		goto bad;
    779 	}
    780 	*cf = *cf0;
    781 
    782 	/*
    783 	 * Resolve the root device.
    784 	 */
    785 	if (cf->cf_root->nv_str != s_qmark) {
    786 		nv = cf->cf_root;
    787 		if (nv == NULL) {
    788 			error("%s: no root device specified", name);
    789 			goto bad;
    790 		}
    791 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
    792 			goto bad;
    793 	}
    794 
    795 	/*
    796 	 * Resolve the dump device.
    797 	 */
    798 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
    799 		/*
    800 		 * Wildcarded dump device is equivalent to unspecified.
    801 		 */
    802 		cf->cf_dump = NULL;
    803 	} else if (cf->cf_dump->nv_str == s_none) {
    804 		/*
    805 		 * Operator has requested that no dump device should be
    806 		 * configured; do nothing.
    807 		 */
    808 	} else {
    809 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
    810 			goto bad;
    811 	}
    812 
    813 	/* Wildcarded fstype is `unspecified'. */
    814 	if (cf->cf_fstype == s_qmark)
    815 		cf->cf_fstype = NULL;
    816 
    817 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
    818 	return;
    819  bad:
    820 	nvfreel(cf0->cf_root);
    821 	nvfreel(cf0->cf_dump);
    822 }
    823 
    824 void
    825 setconf(struct nvlist **npp, const char *what, struct nvlist *v)
    826 {
    827 
    828 	if (*npp != NULL) {
    829 		error("duplicate %s specification", what);
    830 		nvfreel(v);
    831 	} else
    832 		*npp = v;
    833 }
    834 
    835 void
    836 setfstype(const char **fstp, const char *v)
    837 {
    838 
    839 	if (*fstp != NULL) {
    840 		error("multiple fstype specifications");
    841 		return;
    842 	}
    843 
    844 	if (v != s_qmark && OPT_FSOPT(v)) {
    845 		error("\"%s\" is not a configured file system", v);
    846 		return;
    847 	}
    848 
    849 	*fstp = v;
    850 }
    851 
    852 static struct devi *
    853 newdevi(const char *name, int unit, struct devbase *d)
    854 {
    855 	struct devi *i;
    856 
    857 	i = ecalloc(1, sizeof *i);
    858 	i->i_name = name;
    859 	i->i_unit = unit;
    860 	i->i_base = d;
    861 	i->i_bsame = NULL;
    862 	i->i_asame = NULL;
    863 	i->i_alias = NULL;
    864 	i->i_at = NULL;
    865 	i->i_pspec = NULL;
    866 	i->i_atdeva = NULL;
    867 	i->i_locs = NULL;
    868 	i->i_cfflags = 0;
    869 	i->i_lineno = currentline();
    870 	if (unit >= d->d_umax)
    871 		d->d_umax = unit + 1;
    872 	return (i);
    873 }
    874 
    875 /*
    876  * Add the named device as attaching to the named attribute (or perhaps
    877  * another device instead) plus unit number.
    878  */
    879 void
    880 adddev(const char *name, const char *at, struct nvlist *loclist, int flags)
    881 {
    882 	struct devi *i;		/* the new instance */
    883 	struct pspec *p;	/* and its pspec */
    884 	struct attr *attr;	/* attribute that allows attach */
    885 	struct devbase *ib;	/* i->i_base */
    886 	struct devbase *ab;	/* not NULL => at another dev */
    887 	struct nvlist *nv;
    888 	struct deva *iba;	/* devbase attachment used */
    889 	const char *cp;
    890 	int atunit;
    891 	char atbuf[NAMESIZE];
    892 	int hit;
    893 
    894 	ab = NULL;
    895 	iba = NULL;
    896 	if (at == NULL) {
    897 		/* "at root" */
    898 		p = NULL;
    899 		if ((i = getdevi(name)) == NULL)
    900 			goto bad;
    901 		/*
    902 		 * Must warn about i_unit > 0 later, after taking care of
    903 		 * the STAR cases (we could do non-star's here but why
    904 		 * bother?).  Make sure this device can be at root.
    905 		 */
    906 		ib = i->i_base;
    907 		hit = 0;
    908 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
    909 			if (onlist(iba->d_atlist, NULL)) {
    910 				hit = 1;
    911 				break;
    912 			}
    913 		if (!hit) {
    914 			error("`%s' cannot attach to the root", ib->d_name);
    915 			goto bad;
    916 		}
    917 		attr = &errattr;	/* a convenient "empty" attr */
    918 	} else {
    919 		if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
    920 			error("invalid attachment name `%s'", at);
    921 			/* (void)getdevi(name); -- ??? */
    922 			goto bad;
    923 		}
    924 		if ((i = getdevi(name)) == NULL)
    925 			goto bad;
    926 		ib = i->i_base;
    927 
    928 		/*
    929 		 * Devices can attach to two types of things: Attributes,
    930 		 * and other devices (which have the appropriate attributes
    931 		 * to allow attachment).
    932 		 *
    933 		 * (1) If we're attached to an attribute, then we don't need
    934 		 *     look at the parent base device to see what attributes
    935 		 *     it has, and make sure that we can attach to them.
    936 		 *
    937 		 * (2) If we're attached to a real device (i.e. named in
    938 		 *     the config file), we want to remember that so that
    939 		 *     at cross-check time, if the device we're attached to
    940 		 *     is missing but other devices which also provide the
    941 		 *     attribute are present, we don't get a false "OK."
    942 		 *
    943 		 * (3) If the thing we're attached to is an attribute
    944 		 *     but is actually named in the config file, we still
    945 		 *     have to remember its devbase.
    946 		 */
    947 		cp = intern(atbuf);
    948 
    949 		/* Figure out parent's devbase, to satisfy case (3). */
    950 		ab = ht_lookup(devbasetab, cp);
    951 
    952 		/* Find out if it's an attribute. */
    953 		attr = ht_lookup(attrtab, cp);
    954 
    955 		/* Make sure we're _really_ attached to the attr.  Case (1). */
    956 		if (attr != NULL && onlist(attr->a_devs, ib))
    957 			goto findattachment;
    958 
    959 		/*
    960 		 * Else a real device, and not just an attribute.  Case (2).
    961 		 *
    962 		 * Have to work a bit harder to see whether we have
    963 		 * something like "tg0 at esp0" (where esp is merely
    964 		 * not an attribute) or "tg0 at nonesuch0" (where
    965 		 * nonesuch is not even a device).
    966 		 */
    967 		if (ab == NULL) {
    968 			error("%s at %s: `%s' unknown",
    969 			    name, at, atbuf);
    970 			goto bad;
    971 		}
    972 
    973 		/*
    974 		 * See if the named parent carries an attribute
    975 		 * that allows it to supervise device ib.
    976 		 */
    977 		for (nv = ab->d_attrs; nv != NULL; nv = nv->nv_next) {
    978 			attr = nv->nv_ptr;
    979 			if (onlist(attr->a_devs, ib))
    980 				goto findattachment;
    981 		}
    982 		error("`%s' cannot attach to `%s'", ib->d_name, atbuf);
    983 		goto bad;
    984 
    985  findattachment:
    986 		/*
    987 		 * Find the parent spec.  If a matching one has not yet been
    988 		 * created, create one.
    989 		 */
    990 		p = getpspec(attr, ab, atunit);
    991 		p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
    992 
    993 		/* find out which attachment it uses */
    994 		hit = 0;
    995 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
    996 			if (onlist(iba->d_atlist, attr)) {
    997 				hit = 1;
    998 				break;
    999 			}
   1000 		if (!hit)
   1001 			panic("adddev: can't figure out attachment");
   1002 	}
   1003 	if ((i->i_locs = fixloc(name, attr, loclist)) == NULL)
   1004 		goto bad;
   1005 	i->i_at = at;
   1006 	i->i_pspec = p;
   1007 	i->i_atdeva = iba;
   1008 	i->i_cfflags = flags;
   1009 
   1010 	*iba->d_ipp = i;
   1011 	iba->d_ipp = &i->i_asame;
   1012 
   1013 	/* all done, fall into ... */
   1014  bad:
   1015 	nvfreel(loclist);
   1016 	return;
   1017 }
   1018 
   1019 void
   1020 deldev(const char *name, const char *at)
   1021 {
   1022 	struct devi *firsti, *i, *match, *previ, **ppi;
   1023 	struct deva *iba;
   1024 	struct devbase *d;
   1025 	int unit;
   1026 	char base[NAMESIZE];
   1027 
   1028 	if (split(name, strlen(name), base, sizeof base, &unit)) {
   1029 		error("invalid device name `%s'", name);
   1030 		return;
   1031 	}
   1032 	d = ht_lookup(devbasetab, intern(base));
   1033 	if (d == NULL) {
   1034 		error("%s: unknown device `%s'", name, base);
   1035 		return;
   1036 	}
   1037 	if (d->d_ispseudo) {
   1038 		error("%s: %s is a pseudo-device", name, base);
   1039 		return;
   1040 	}
   1041 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
   1042 		error("`%s' not defined", name);
   1043 		return;
   1044 	}
   1045 	match = NULL;
   1046 	previ = NULL;
   1047 	if (at == NULL)
   1048 		/* 'at root' */
   1049 		match = firsti;
   1050 	else {
   1051 		if (strcmp(at, firsti->i_at) == 0) {
   1052 			match = firsti;
   1053 		} else {
   1054 			for (i = firsti; i != NULL; i = i->i_alias) {
   1055 				if (strcmp(at, i->i_at) == 0) {
   1056 					match = i;
   1057 					break;
   1058 				}
   1059 				previ = i;
   1060 			}
   1061 		}
   1062 		if (match == NULL) {
   1063 			error("`%s' at `%s' not found", name, at);
   1064 			return;
   1065 		}
   1066 	}
   1067 
   1068 	i = match;
   1069 	/*
   1070 	 * We have the device instance, i.
   1071 	 * We have to:
   1072 	 *   - delete the alias
   1073 	 *
   1074 	 *      If the devi was an alias of an already listed devi, all is
   1075 	 *      good we don't have to do more.
   1076 	 *      If it was the first alias, we have to replace i's entry in
   1077 	 *      d's list by its first alias.
   1078 	 *      If it was the only entry, we must remove i's entry from d's
   1079 	 *      list.
   1080 	 */
   1081 	if (previ != NULL)
   1082 		previ->i_alias = i->i_alias;
   1083 	else {
   1084 		if (i->i_alias == NULL)
   1085 			/* No alias, must unlink the entry from devitab */
   1086 			ht_remove(devitab, i->i_name);
   1087 		else
   1088 			/* Or have the first alias replace i in d's list */
   1089 			i->i_alias->i_bsame = i->i_bsame;
   1090 		/*
   1091 		 *   - remove/replace the instance from the devbase's list
   1092 		 *
   1093 		 * A double-linked list would make this much easier.  Oh, well,
   1094 		 * what is done is done.
   1095 		 */
   1096 		ppi = &d->d_ihead;
   1097 		for (ppi = &d->d_ihead;
   1098 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
   1099 		    ppi = &(*ppi)->i_bsame);
   1100 		if (*ppi == NULL)
   1101 			panic("deldev: dev (%s) doesn't list the devi (%s at %s)",
   1102 			    d->d_name, i->i_name, i->i_at);
   1103 		if (*ppi == i)
   1104 			*ppi = i->i_bsame;
   1105 		else
   1106 			(*ppi)->i_bsame = i->i_bsame;
   1107 		if (d->d_ipp == &i->i_bsame) {
   1108 			if (d->d_ihead == i)
   1109 				d->d_ipp = &d->d_ihead;
   1110 			else
   1111 				d->d_ipp = &(*ppi)->i_bsame;
   1112 		}
   1113 	}
   1114 	/*
   1115 	 *   - delete the attachment instance
   1116 	 */
   1117 	iba = i->i_atdeva;
   1118 	ppi = &iba->d_ihead;
   1119 	for (ppi = &iba->d_ihead;
   1120 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
   1121 	    ppi = &(*ppi)->i_asame);
   1122 	if (*ppi == NULL)
   1123 		panic("deldev: deva (%s) doesn't list the devi (%s)",
   1124 		    iba->d_name, i->i_name);
   1125 	if (*ppi == i)
   1126 		*ppi = i->i_asame;
   1127 	else
   1128 		(*ppi)->i_asame = i->i_asame;
   1129 	if (iba->d_ipp == &i->i_asame) {
   1130 		if (iba->d_ihead == i)
   1131 			iba->d_ipp = &iba->d_ihead;
   1132 		else
   1133 			iba->d_ipp = &(*ppi)->i_asame;
   1134 	}
   1135 	/*
   1136 	 *   - delete the pspec
   1137 	 */
   1138 	if (i->i_pspec) {
   1139 		struct pspec *p = i->i_pspec;
   1140 		struct nvlist *nv, *onv;
   1141 
   1142 		/* Double-linked nvlist anyone? */
   1143 		for (nv = p->p_devs; nv->nv_ptr != NULL; nv = nv->nv_next) {
   1144 			if (nv->nv_next && nv->nv_next->nv_ptr == i) {
   1145 				onv = nv->nv_next;
   1146 				nv->nv_next = onv->nv_next;
   1147 				nvfree(onv);
   1148 				break;
   1149 			} if (nv->nv_ptr == i) {
   1150 				/* nv is p->p_devs in that case */
   1151 				p->p_devs = nv->nv_next;
   1152 				nvfree(nv);
   1153 				break;
   1154 			}
   1155 		}
   1156 		if (p->p_devs == NULL)
   1157 			TAILQ_REMOVE(&allpspecs, p, p_list);
   1158 	}
   1159 	/*
   1160 	 *   - delete the alldevi entry
   1161 	 */
   1162 	TAILQ_REMOVE(&alldevi, i, i_next);
   1163 	ndevi--;
   1164 	/*
   1165 	 *   - reconstuct d->d_umax
   1166 	 */
   1167 	d->d_umax = 0;
   1168 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
   1169 		if (i->i_unit >= d->d_umax)
   1170 			d->d_umax = i->i_unit + 1;
   1171 }
   1172 
   1173 void
   1174 addpseudo(const char *name, int number)
   1175 {
   1176 	struct devbase *d;
   1177 	struct devi *i;
   1178 
   1179 	d = ht_lookup(devbasetab, name);
   1180 	if (d == NULL) {
   1181 		error("undefined pseudo-device %s", name);
   1182 		return;
   1183 	}
   1184 	if (!d->d_ispseudo) {
   1185 		error("%s is a real device, not a pseudo-device", name);
   1186 		return;
   1187 	}
   1188 	if (ht_lookup(devitab, name) != NULL) {
   1189 		error("`%s' already defined", name);
   1190 		return;
   1191 	}
   1192 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
   1193 	if (ht_insert(devitab, name, i))
   1194 		panic("addpseudo(%s)", name);
   1195 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
   1196 }
   1197 
   1198 void
   1199 delpseudo(const char *name)
   1200 {
   1201 	struct devbase *d;
   1202 	struct devi *i;
   1203 
   1204 	d = ht_lookup(devbasetab, name);
   1205 	if (d == NULL) {
   1206 		error("undefined pseudo-device %s", name);
   1207 		return;
   1208 	}
   1209 	if (!d->d_ispseudo) {
   1210 		error("%s is a real device, not a pseudo-device", name);
   1211 		return;
   1212 	}
   1213 	if ((i = ht_lookup(devitab, name)) == NULL) {
   1214 		error("`%s' not defined", name);
   1215 		return;
   1216 	}
   1217 	d->d_umax = 0;		/* clear neads-count entries */
   1218 	TAILQ_REMOVE(&allpseudo, i, i_next);
   1219 	if (ht_remove(devitab, name))
   1220 		panic("delpseudo(%s) - can't remove from devitab", name);
   1221 }
   1222 
   1223 void
   1224 adddevm(const char *name, int cmajor, int bmajor, struct nvlist *options)
   1225 {
   1226 	struct devm *dm;
   1227 
   1228 	if (cmajor < -1 || cmajor >= 4096) {
   1229 		error("character major %d is invalid", cmajor);
   1230 		nvfreel(options);
   1231 		return;
   1232 	}
   1233 
   1234 	if (bmajor < -1 || bmajor >= 4096) {
   1235 		error("block major %d is invalid", bmajor);
   1236 		nvfreel(options);
   1237 		return;
   1238 	}
   1239 	if (cmajor == -1 && bmajor == -1) {
   1240 		error("both character/block majors are not specified");
   1241 		nvfreel(options);
   1242 		return;
   1243 	}
   1244 
   1245 	dm = ecalloc(1, sizeof(*dm));
   1246 	dm->dm_srcfile = yyfile;
   1247 	dm->dm_srcline = currentline();
   1248 	dm->dm_name = name;
   1249 	dm->dm_cmajor = cmajor;
   1250 	dm->dm_bmajor = bmajor;
   1251 	dm->dm_opts = options;
   1252 
   1253 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
   1254 
   1255 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
   1256 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
   1257 }
   1258 
   1259 void
   1260 fixdevis(void)
   1261 {
   1262 	struct devi *i;
   1263 
   1264 	TAILQ_FOREACH(i, &alldevi, i_next)
   1265 		selectbase(i->i_base, i->i_atdeva);
   1266 
   1267 	TAILQ_FOREACH(i, &allpseudo, i_next)
   1268 		selectbase(i->i_base, NULL);
   1269 }
   1270 
   1271 /*
   1272  * Look up a parent spec, creating a new one if it does not exist.
   1273  */
   1274 static struct pspec *
   1275 getpspec(struct attr *attr, struct devbase *ab, int atunit)
   1276 {
   1277 	struct pspec *p;
   1278 
   1279 	TAILQ_FOREACH(p, &allpspecs, p_list) {
   1280 		if (p->p_iattr == attr &&
   1281 		    p->p_atdev == ab &&
   1282 		    p->p_atunit == atunit)
   1283 			return (p);
   1284 	}
   1285 
   1286 	p = ecalloc(1, sizeof(*p));
   1287 
   1288 	p->p_iattr = attr;
   1289 	p->p_atdev = ab;
   1290 	p->p_atunit = atunit;
   1291 	p->p_inst = npspecs++;
   1292 
   1293 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
   1294 
   1295 	return (p);
   1296 }
   1297 
   1298 /*
   1299  * Define a new instance of a specific device.
   1300  */
   1301 static struct devi *
   1302 getdevi(const char *name)
   1303 {
   1304 	struct devi *i, *firsti;
   1305 	struct devbase *d;
   1306 	int unit;
   1307 	char base[NAMESIZE];
   1308 
   1309 	if (split(name, strlen(name), base, sizeof base, &unit)) {
   1310 		error("invalid device name `%s'", name);
   1311 		return (NULL);
   1312 	}
   1313 	d = ht_lookup(devbasetab, intern(base));
   1314 	if (d == NULL) {
   1315 		error("%s: unknown device `%s'", name, base);
   1316 		return (NULL);
   1317 	}
   1318 	if (d->d_ispseudo) {
   1319 		error("%s: %s is a pseudo-device", name, base);
   1320 		return (NULL);
   1321 	}
   1322 	firsti = ht_lookup(devitab, name);
   1323 	i = newdevi(name, unit, d);
   1324 	if (firsti == NULL) {
   1325 		if (ht_insert(devitab, name, i))
   1326 			panic("getdevi(%s)", name);
   1327 		*d->d_ipp = i;
   1328 		d->d_ipp = &i->i_bsame;
   1329 	} else {
   1330 		while (firsti->i_alias)
   1331 			firsti = firsti->i_alias;
   1332 		firsti->i_alias = i;
   1333 	}
   1334 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
   1335 	ndevi++;
   1336 	return (i);
   1337 }
   1338 
   1339 static const char *
   1340 concat(const char *name, int c)
   1341 {
   1342 	int len;
   1343 	char buf[NAMESIZE];
   1344 
   1345 	len = strlen(name);
   1346 	if (len + 2 > sizeof(buf)) {
   1347 		error("device name `%s%c' too long", name, c);
   1348 		len = sizeof(buf) - 2;
   1349 	}
   1350 	memmove(buf, name, len);
   1351 	buf[len] = c;
   1352 	buf[len + 1] = 0;
   1353 	return (intern(buf));
   1354 }
   1355 
   1356 const char *
   1357 starref(const char *name)
   1358 {
   1359 
   1360 	return (concat(name, '*'));
   1361 }
   1362 
   1363 const char *
   1364 wildref(const char *name)
   1365 {
   1366 
   1367 	return (concat(name, '?'));
   1368 }
   1369 
   1370 /*
   1371  * Split a name like "foo0" into base name (foo) and unit number (0).
   1372  * Return 0 on success.  To make this useful for names like "foo0a",
   1373  * the length of the "foo0" part is one of the arguments.
   1374  */
   1375 static int
   1376 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
   1377 {
   1378 	const char *cp;
   1379 	int c, l;
   1380 
   1381 	l = nlen;
   1382 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
   1383 		return (1);
   1384 	c = (u_char)name[--l];
   1385 	if (!isdigit(c)) {
   1386 		if (c == '*')
   1387 			*aunit = STAR;
   1388 		else if (c == '?')
   1389 			*aunit = WILD;
   1390 		else
   1391 			return (1);
   1392 	} else {
   1393 		cp = &name[l];
   1394 		while (isdigit((unsigned char)cp[-1]))
   1395 			l--, cp--;
   1396 		*aunit = atoi(cp);
   1397 	}
   1398 	memmove(base, name, l);
   1399 	base[l] = 0;
   1400 	return (0);
   1401 }
   1402 
   1403 void
   1404 selectattr(struct attr *a)
   1405 {
   1406 
   1407 	(void)ht_insert(selecttab, a->a_name, (char *)a->a_name);
   1408 }
   1409 
   1410 /*
   1411  * We have an instance of the base foo, so select it and all its
   1412  * attributes for "optional foo".
   1413  */
   1414 static void
   1415 selectbase(struct devbase *d, struct deva *da)
   1416 {
   1417 	struct attr *a;
   1418 	struct nvlist *nv;
   1419 
   1420 	(void)ht_insert(selecttab, d->d_name, (char *)d->d_name);
   1421 	for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
   1422 		a = nv->nv_ptr;
   1423 		expandattr(a, selectattr);
   1424 	}
   1425 	if (da != NULL) {
   1426 		(void)ht_insert(selecttab, da->d_name, (char *)da->d_name);
   1427 		for (nv = da->d_attrs; nv != NULL; nv = nv->nv_next) {
   1428 			a = nv->nv_ptr;
   1429 			expandattr(a, selectattr);
   1430 		}
   1431 	}
   1432 }
   1433 
   1434 /*
   1435  * Is the given pointer on the given list of pointers?
   1436  */
   1437 static int
   1438 onlist(struct nvlist *nv, void *ptr)
   1439 {
   1440 	for (; nv != NULL; nv = nv->nv_next)
   1441 		if (nv->nv_ptr == ptr)
   1442 			return (1);
   1443 	return (0);
   1444 }
   1445 
   1446 static char *
   1447 extend(char *p, const char *name)
   1448 {
   1449 	int l;
   1450 
   1451 	l = strlen(name);
   1452 	memmove(p, name, l);
   1453 	p += l;
   1454 	*p++ = ',';
   1455 	*p++ = ' ';
   1456 	return (p);
   1457 }
   1458 
   1459 /*
   1460  * Check that we got all required locators, and default any that are
   1461  * given as "?" and have defaults.  Return 0 on success.
   1462  */
   1463 static const char **
   1464 fixloc(const char *name, struct attr *attr, struct nvlist *got)
   1465 {
   1466 	struct nvlist *m, *n;
   1467 	int ord;
   1468 	const char **lp;
   1469 	int nmissing, nextra, nnodefault;
   1470 	char *mp, *ep, *ndp;
   1471 	char missing[1000], extra[1000], nodefault[1000];
   1472 	static const char *nullvec[1];
   1473 
   1474 	/*
   1475 	 * Look for all required locators, and number the given ones
   1476 	 * according to the required order.  While we are numbering,
   1477 	 * set default values for defaulted locators.
   1478 	 */
   1479 	if (attr->a_loclen == 0)	/* e.g., "at root" */
   1480 		lp = nullvec;
   1481 	else
   1482 		lp = emalloc((attr->a_loclen + 1) * sizeof(const char *));
   1483 	for (n = got; n != NULL; n = n->nv_next)
   1484 		n->nv_int = -1;
   1485 	nmissing = 0;
   1486 	mp = missing;
   1487 	/* yes, this is O(mn), but m and n should be small */
   1488 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->nv_next, ord++) {
   1489 		for (n = got; n != NULL; n = n->nv_next) {
   1490 			if (n->nv_name == m->nv_name) {
   1491 				n->nv_int = ord;
   1492 				break;
   1493 			}
   1494 		}
   1495 		if (n == NULL && m->nv_int == 0) {
   1496 			nmissing++;
   1497 			mp = extend(mp, m->nv_name);
   1498 		}
   1499 		lp[ord] = m->nv_str;
   1500 	}
   1501 	if (ord != attr->a_loclen)
   1502 		panic("fixloc");
   1503 	lp[ord] = NULL;
   1504 	nextra = 0;
   1505 	ep = extra;
   1506 	nnodefault = 0;
   1507 	ndp = nodefault;
   1508 	for (n = got; n != NULL; n = n->nv_next) {
   1509 		if (n->nv_int >= 0) {
   1510 			if (n->nv_str != NULL)
   1511 				lp[n->nv_int] = n->nv_str;
   1512 			else if (lp[n->nv_int] == NULL) {
   1513 				nnodefault++;
   1514 				ndp = extend(ndp, n->nv_name);
   1515 			}
   1516 		} else {
   1517 			nextra++;
   1518 			ep = extend(ep, n->nv_name);
   1519 		}
   1520 	}
   1521 	if (nextra) {
   1522 		ep[-2] = 0;	/* kill ", " */
   1523 		error("%s: extraneous locator%s: %s",
   1524 		    name, nextra > 1 ? "s" : "", extra);
   1525 	}
   1526 	if (nmissing) {
   1527 		mp[-2] = 0;
   1528 		error("%s: must specify %s", name, missing);
   1529 	}
   1530 	if (nnodefault) {
   1531 		ndp[-2] = 0;
   1532 		error("%s: cannot wildcard %s", name, nodefault);
   1533 	}
   1534 	if (nmissing || nnodefault) {
   1535 		free(lp);
   1536 		lp = NULL;
   1537 	}
   1538 	return (lp);
   1539 }
   1540