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