xref: /netbsd-src/usr.bin/config/sem.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: sem.c,v 1.31 2008/12/28 01:23:46 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratories.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	from: @(#)sem.c	8.1 (Berkeley) 6/6/93
41  */
42 
43 #if HAVE_NBTOOL_CONFIG_H
44 #include "nbtool_config.h"
45 #endif
46 
47 #include <sys/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(dev_t, dev_t);
87 static const char *major2name(int);
88 static dev_t 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 %lld",
639 		    d->d_name, (long long)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 dev_t
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(dev_t maj, dev_t 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), "<%lld/%lld>",
691 		    (long long)maj, (long long)min);
692 	else
693 		(void)snprintf(buf, sizeof(buf), "%s%lld%c", devicename,
694 		    (long long)min / maxpartitions,
695 		    (char)(min % maxpartitions) + 'a');
696 
697 	return (intern(buf));
698 }
699 
700 /*
701  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
702  * Handle the case where the device number is given but there is no
703  * corresponding name, and map NULL to the default.
704  */
705 static int
706 resolve(struct nvlist **nvp, const char *name, const char *what,
707 	struct nvlist *dflt, int part)
708 {
709 	struct nvlist *nv;
710 	struct devbase *dev;
711 	const char *cp;
712 	dev_t maj, min;
713 	int i, l;
714 	int unit;
715 	char buf[NAMESIZE];
716 
717 	if ((u_int)(part -= 'a') >= maxpartitions)
718 		panic("resolve");
719 	if ((nv = *nvp) == NULL) {
720 		dev_t	d = NODEV;
721 		/*
722 		 * Apply default.  Easiest to do this by number.
723 		 * Make sure to retain NODEVness, if this is dflt's disposition.
724 		 */
725 		if (dflt->nv_num != NODEV) {
726 			maj = major(dflt->nv_num);
727 			min = ((minor(dflt->nv_num) / maxpartitions) *
728 			    maxpartitions) + part;
729 			d = makedev(maj, min);
730 			cp = makedevstr(maj, min);
731 		} else
732 			cp = NULL;
733 		*nvp = nv = newnv(NULL, cp, NULL, d, NULL);
734 	}
735 	if (nv->nv_num != NODEV) {
736 		/*
737 		 * By the numbers.  Find the appropriate major number
738 		 * to make a name.
739 		 */
740 		maj = major(nv->nv_num);
741 		min = minor(nv->nv_num);
742 		nv->nv_str = makedevstr(maj, min);
743 		return (0);
744 	}
745 
746 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
747 		/*
748 		 * Wildcarded or unspecified; leave it as NODEV.
749 		 */
750 		return (0);
751 
752 	/*
753 	 * The normal case: things like "ra2b".  Check for partition
754 	 * suffix, remove it if there, and split into name ("ra") and
755 	 * unit (2).
756 	 */
757 	l = i = strlen(nv->nv_str);
758 	cp = &nv->nv_str[l];
759 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
760 	    isdigit((unsigned char)cp[-1])) {
761 		l--;
762 		part = *cp - 'a';
763 	}
764 	cp = nv->nv_str;
765 	if (split(cp, l, buf, sizeof buf, &unit)) {
766 		cfgerror("%s: invalid %s device name `%s'", name, what, cp);
767 		return (1);
768 	}
769 	dev = ht_lookup(devbasetab, intern(buf));
770 	if (dev == NULL) {
771 		cfgerror("%s: device `%s' does not exist", name, buf);
772 		return (1);
773 	}
774 
775 	/*
776 	 * Check for the magic network interface attribute, and
777 	 * don't bother making a device number.
778 	 */
779 	if (has_attr(dev->d_attrs, s_ifnet)) {
780 		nv->nv_num = NODEV;
781 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
782 	} else {
783 		maj = dev2major(dev);
784 		if (maj == NODEV) {
785 			cfgerror("%s: can't make %s device from `%s'",
786 			    name, what, nv->nv_str);
787 			return (1);
788 		}
789 		nv->nv_num = makedev(maj, unit * maxpartitions + part);
790 	}
791 
792 	nv->nv_name = dev->d_name;
793 	return (0);
794 }
795 
796 /*
797  * Add a completed configuration to the list.
798  */
799 void
800 addconf(struct config *cf0)
801 {
802 	struct config *cf;
803 	const char *name;
804 
805 	name = cf0->cf_name;
806 	cf = ecalloc(1, sizeof *cf);
807 	if (ht_insert(cfhashtab, name, cf)) {
808 		cfgerror("configuration `%s' already defined", name);
809 		free(cf);
810 		goto bad;
811 	}
812 	*cf = *cf0;
813 
814 	/*
815 	 * Resolve the root device.
816 	 */
817 	if (cf->cf_root == NULL) {
818 		cfgerror("%s: no root device specified", name);
819 		goto bad;
820 	}
821 	if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
822 		struct nvlist *nv;
823 		nv = cf->cf_root;
824 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
825 			goto bad;
826 	}
827 
828 	/*
829 	 * Resolve the dump device.
830 	 */
831 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
832 		/*
833 		 * Wildcarded dump device is equivalent to unspecified.
834 		 */
835 		cf->cf_dump = NULL;
836 	} else if (cf->cf_dump->nv_str == s_none) {
837 		/*
838 		 * Operator has requested that no dump device should be
839 		 * configured; do nothing.
840 		 */
841 	} else {
842 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
843 			goto bad;
844 	}
845 
846 	/* Wildcarded fstype is `unspecified'. */
847 	if (cf->cf_fstype == s_qmark)
848 		cf->cf_fstype = NULL;
849 
850 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
851 	return;
852  bad:
853 	nvfreel(cf0->cf_root);
854 	nvfreel(cf0->cf_dump);
855 }
856 
857 void
858 setconf(struct nvlist **npp, const char *what, struct nvlist *v)
859 {
860 
861 	if (*npp != NULL) {
862 		cfgerror("duplicate %s specification", what);
863 		nvfreel(v);
864 	} else
865 		*npp = v;
866 }
867 
868 void
869 delconf(const char *name)
870 {
871 	struct config *cf;
872 
873 	if (ht_lookup(cfhashtab, name) == NULL) {
874 		cfgerror("configuration `%s' undefined", name);
875 		return;
876 	}
877 	(void)ht_remove(cfhashtab, name);
878 
879 	TAILQ_FOREACH(cf, &allcf, cf_next)
880 		if (!strcmp(cf->cf_name, name))
881 			break;
882 	if (cf == NULL)
883 		panic("lost configuration `%s'", name);
884 
885 	TAILQ_REMOVE(&allcf, cf, cf_next);
886 }
887 
888 void
889 setfstype(const char **fstp, const char *v)
890 {
891 
892 	if (*fstp != NULL) {
893 		cfgerror("multiple fstype specifications");
894 		return;
895 	}
896 
897 	if (v != s_qmark && OPT_FSOPT(v)) {
898 		cfgerror("\"%s\" is not a configured file system", v);
899 		return;
900 	}
901 
902 	*fstp = v;
903 }
904 
905 static struct devi *
906 newdevi(const char *name, int unit, struct devbase *d)
907 {
908 	struct devi *i;
909 
910 	i = ecalloc(1, sizeof *i);
911 	i->i_name = name;
912 	i->i_unit = unit;
913 	i->i_base = d;
914 	i->i_bsame = NULL;
915 	i->i_asame = NULL;
916 	i->i_alias = NULL;
917 	i->i_at = NULL;
918 	i->i_pspec = NULL;
919 	i->i_atdeva = NULL;
920 	i->i_locs = NULL;
921 	i->i_cfflags = 0;
922 	i->i_lineno = currentline();
923 	i->i_srcfile = yyfile;
924 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
925 	i->i_level = devilevel;
926 	if (unit >= d->d_umax)
927 		d->d_umax = unit + 1;
928 	return (i);
929 }
930 
931 /*
932  * Add the named device as attaching to the named attribute (or perhaps
933  * another device instead) plus unit number.
934  */
935 void
936 adddev(const char *name, const char *at, struct nvlist *loclist, int flags)
937 {
938 	struct devi *i;		/* the new instance */
939 	struct pspec *p;	/* and its pspec */
940 	struct attr *attr;	/* attribute that allows attach */
941 	struct devbase *ib;	/* i->i_base */
942 	struct devbase *ab;	/* not NULL => at another dev */
943 	struct nvlist *nv;
944 	struct deva *iba;	/* devbase attachment used */
945 	const char *cp;
946 	int atunit;
947 	char atbuf[NAMESIZE];
948 	int hit;
949 
950 	ab = NULL;
951 	iba = NULL;
952 	if (at == NULL) {
953 		/* "at root" */
954 		p = NULL;
955 		if ((i = getdevi(name)) == NULL)
956 			goto bad;
957 		/*
958 		 * Must warn about i_unit > 0 later, after taking care of
959 		 * the STAR cases (we could do non-star's here but why
960 		 * bother?).  Make sure this device can be at root.
961 		 */
962 		ib = i->i_base;
963 		hit = 0;
964 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
965 			if (onlist(iba->d_atlist, NULL)) {
966 				hit = 1;
967 				break;
968 			}
969 		if (!hit) {
970 			cfgerror("`%s' cannot attach to the root", ib->d_name);
971 			i->i_active = DEVI_BROKEN;
972 			goto bad;
973 		}
974 		attr = &errattr;	/* a convenient "empty" attr */
975 	} else {
976 		if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
977 			cfgerror("invalid attachment name `%s'", at);
978 			/* (void)getdevi(name); -- ??? */
979 			goto bad;
980 		}
981 		if ((i = getdevi(name)) == NULL)
982 			goto bad;
983 		ib = i->i_base;
984 
985 		/*
986 		 * Devices can attach to two types of things: Attributes,
987 		 * and other devices (which have the appropriate attributes
988 		 * to allow attachment).
989 		 *
990 		 * (1) If we're attached to an attribute, then we don't need
991 		 *     look at the parent base device to see what attributes
992 		 *     it has, and make sure that we can attach to them.
993 		 *
994 		 * (2) If we're attached to a real device (i.e. named in
995 		 *     the config file), we want to remember that so that
996 		 *     at cross-check time, if the device we're attached to
997 		 *     is missing but other devices which also provide the
998 		 *     attribute are present, we don't get a false "OK."
999 		 *
1000 		 * (3) If the thing we're attached to is an attribute
1001 		 *     but is actually named in the config file, we still
1002 		 *     have to remember its devbase.
1003 		 */
1004 		cp = intern(atbuf);
1005 
1006 		/* Figure out parent's devbase, to satisfy case (3). */
1007 		ab = ht_lookup(devbasetab, cp);
1008 
1009 		/* Find out if it's an attribute. */
1010 		attr = ht_lookup(attrtab, cp);
1011 
1012 		/* Make sure we're _really_ attached to the attr.  Case (1). */
1013 		if (attr != NULL && onlist(attr->a_devs, ib))
1014 			goto findattachment;
1015 
1016 		/*
1017 		 * Else a real device, and not just an attribute.  Case (2).
1018 		 *
1019 		 * Have to work a bit harder to see whether we have
1020 		 * something like "tg0 at esp0" (where esp is merely
1021 		 * not an attribute) or "tg0 at nonesuch0" (where
1022 		 * nonesuch is not even a device).
1023 		 */
1024 		if (ab == NULL) {
1025 			cfgerror("%s at %s: `%s' unknown",
1026 			    name, at, atbuf);
1027 			i->i_active = DEVI_BROKEN;
1028 			goto bad;
1029 		}
1030 
1031 		/*
1032 		 * See if the named parent carries an attribute
1033 		 * that allows it to supervise device ib.
1034 		 */
1035 		for (nv = ab->d_attrs; nv != NULL; nv = nv->nv_next) {
1036 			attr = nv->nv_ptr;
1037 			if (onlist(attr->a_devs, ib))
1038 				goto findattachment;
1039 		}
1040 		cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
1041 		i->i_active = DEVI_BROKEN;
1042 		goto bad;
1043 
1044  findattachment:
1045 		/*
1046 		 * Find the parent spec.  If a matching one has not yet been
1047 		 * created, create one.
1048 		 */
1049 		p = getpspec(attr, ab, atunit);
1050 		p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
1051 
1052 		/* find out which attachment it uses */
1053 		hit = 0;
1054 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
1055 			if (onlist(iba->d_atlist, attr)) {
1056 				hit = 1;
1057 				break;
1058 			}
1059 		if (!hit)
1060 			panic("adddev: can't figure out attachment");
1061 	}
1062 	if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
1063 		i->i_active = DEVI_BROKEN;
1064 		goto bad;
1065 	}
1066 	i->i_at = at;
1067 	i->i_pspec = p;
1068 	i->i_atdeva = iba;
1069 	i->i_cfflags = flags;
1070 
1071 	*iba->d_ipp = i;
1072 	iba->d_ipp = &i->i_asame;
1073 
1074 	/* all done, fall into ... */
1075  bad:
1076 	nvfreel(loclist);
1077 	return;
1078 }
1079 
1080 void
1081 deldevi(const char *name, const char *at)
1082 {
1083 	struct devi *firsti, *i;
1084 	struct devbase *d;
1085 	int unit;
1086 	char base[NAMESIZE];
1087 
1088 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1089 		cfgerror("invalid device name `%s'", name);
1090 		return;
1091 	}
1092 	d = ht_lookup(devbasetab, intern(base));
1093 	if (d == NULL) {
1094 		cfgerror("%s: unknown device `%s'", name, base);
1095 		return;
1096 	}
1097 	if (d->d_ispseudo) {
1098 		cfgerror("%s: %s is a pseudo-device", name, base);
1099 		return;
1100 	}
1101 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
1102 		cfgerror("`%s' not defined", name);
1103 		return;
1104 	}
1105 	if (at == NULL && firsti->i_at == NULL) {
1106 		/* 'at root' */
1107 		remove_devi(firsti);
1108 		return;
1109 	} else if (at != NULL)
1110 		for (i = firsti; i != NULL; i = i->i_alias)
1111 			if (i->i_active != DEVI_BROKEN &&
1112 			    strcmp(at, i->i_at) == 0) {
1113 				remove_devi(i);
1114 				return;
1115 			}
1116 	cfgerror("`%s' at `%s' not found", name, at ? at : "root");
1117 }
1118 
1119 static void
1120 remove_devi(struct devi *i)
1121 {
1122 	struct devbase *d = i->i_base;
1123 	struct devi *f, *j, **ppi;
1124 	struct deva *iba;
1125 
1126 	f = ht_lookup(devitab, i->i_name);
1127 	if (f == NULL)
1128 		panic("remove_devi(): instance %s disappeared from devitab",
1129 		    i->i_name);
1130 
1131 	if (i->i_active == DEVI_BROKEN) {
1132 		cfgerror("not removing broken instance `%s'", i->i_name);
1133 		return;
1134 	}
1135 
1136 	/*
1137 	 * We have the device instance, i.
1138 	 * We have to:
1139 	 *   - delete the alias
1140 	 *
1141 	 *      If the devi was an alias of an already listed devi, all is
1142 	 *      good we don't have to do more.
1143 	 *      If it was the first alias, we have to replace i's entry in
1144 	 *      d's list by its first alias.
1145 	 *      If it was the only entry, we must remove i's entry from d's
1146 	 *      list.
1147 	 */
1148 	if (i != f) {
1149 		for (j = f; j->i_alias != i; j = j->i_alias);
1150 		j->i_alias = i->i_alias;
1151 	} else {
1152 		if (i->i_alias == NULL) {
1153 			/* No alias, must unlink the entry from devitab */
1154 			ht_remove(devitab, i->i_name);
1155 			j = i->i_bsame;
1156 		} else {
1157 			/* Or have the first alias replace i in d's list */
1158 			i->i_alias->i_bsame = i->i_bsame;
1159 			j = i->i_alias;
1160 			if (i == f)
1161 				ht_replace(devitab, i->i_name, i->i_alias);
1162 		}
1163 
1164 		/*
1165 		 *   - remove/replace the instance from the devbase's list
1166 		 *
1167 		 * A double-linked list would make this much easier.  Oh, well,
1168 		 * what is done is done.
1169 		 */
1170 		for (ppi = &d->d_ihead;
1171 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1172 		    ppi = &(*ppi)->i_bsame);
1173 		if (*ppi == NULL)
1174 			panic("deldev: dev (%s) doesn't list the devi"
1175 			    " (%s at %s)", d->d_name, i->i_name, i->i_at);
1176 		f = *ppi;
1177 		if (f == i)
1178 			/* That implies d->d_ihead == i */
1179 			*ppi = j;
1180 		else
1181 			(*ppi)->i_bsame = j;
1182 		if (d->d_ipp == &i->i_bsame) {
1183 			if (i->i_alias == NULL) {
1184 				if (f == i)
1185 					d->d_ipp = &d->d_ihead;
1186 				else
1187 					d->d_ipp = &f->i_bsame;
1188 			} else
1189 				d->d_ipp = &i->i_alias->i_bsame;
1190 		}
1191 	}
1192 	/*
1193 	 *   - delete the attachment instance
1194 	 */
1195 	iba = i->i_atdeva;
1196 	for (ppi = &iba->d_ihead;
1197 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1198 	    ppi = &(*ppi)->i_asame);
1199 	if (*ppi == NULL)
1200 		panic("deldev: deva (%s) doesn't list the devi (%s)",
1201 		    iba->d_name, i->i_name);
1202 	f = *ppi;
1203 	if (f == i)
1204 		/* That implies iba->d_ihead == i */
1205 		*ppi = i->i_asame;
1206 	else
1207 		(*ppi)->i_asame = i->i_asame;
1208 	if (iba->d_ipp == &i->i_asame) {
1209 		if (f == i)
1210 			iba->d_ipp = &iba->d_ihead;
1211 		else
1212 			iba->d_ipp = &f->i_asame;
1213 	}
1214 	/*
1215 	 *   - delete the pspec
1216 	 */
1217 	if (i->i_pspec) {
1218 		struct pspec *p = i->i_pspec;
1219 		struct nvlist *nv, *onv;
1220 
1221 		/* Double-linked nvlist anyone? */
1222 		for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
1223 			if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1224 				onv = nv->nv_next;
1225 				nv->nv_next = onv->nv_next;
1226 				nvfree(onv);
1227 				break;
1228 			}
1229 			if (nv->nv_ptr == i) {
1230 				/* nv is p->p_devs in that case */
1231 				p->p_devs = nv->nv_next;
1232 				nvfree(nv);
1233 				break;
1234 			}
1235 		}
1236 		if (p->p_devs == NULL)
1237 			TAILQ_REMOVE(&allpspecs, p, p_list);
1238 	}
1239 	/*
1240 	 *   - delete the alldevi entry
1241 	 */
1242 	TAILQ_REMOVE(&alldevi, i, i_next);
1243 	ndevi--;
1244 	/*
1245 	 * Put it in deaddevitab
1246 	 *
1247 	 * Each time a devi is removed, devilevel is increased so that later on
1248 	 * it is possible to tell if an instance was added before or after the
1249 	 * removal of its parent.
1250 	 *
1251 	 * For active instances, i_level contains the number of devi removed so
1252 	 * far, and for dead devis, it contains its index.
1253 	 */
1254 	i->i_level = devilevel++;
1255 	i->i_alias = NULL;
1256 	f = ht_lookup(deaddevitab, i->i_name);
1257 	if (f == NULL) {
1258 		if (ht_insert(deaddevitab, i->i_name, i))
1259 			panic("remove_devi(%s) - can't add to deaddevitab",
1260 			    i->i_name);
1261 	} else {
1262 		for (j = f; j->i_alias != NULL; j = j->i_alias);
1263 		j->i_alias = i;
1264 	}
1265 	/*
1266 	 *   - reconstruct d->d_umax
1267 	 */
1268 	d->d_umax = 0;
1269 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1270 		if (i->i_unit >= d->d_umax)
1271 			d->d_umax = i->i_unit + 1;
1272 }
1273 
1274 void
1275 deldeva(const char *at)
1276 {
1277 	int unit;
1278 	const char *cp;
1279 	struct devbase *d, *ad;
1280 	struct devi *i, *j;
1281 	struct attr *a;
1282 	struct pspec *p;
1283 	struct nvlist *nv, *stack = NULL;
1284 
1285 	if (at == NULL) {
1286 		TAILQ_FOREACH(i, &alldevi, i_next)
1287 			if (i->i_at == NULL)
1288 				stack = newnv(NULL, NULL, i, 0, stack);
1289 	} else {
1290 		int l;
1291 
1292 		l = strlen(at) - 1;
1293 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
1294 			char base[NAMESIZE];
1295 
1296 			if (split(at, l+1, base, sizeof base, &unit)) {
1297 				cfgerror("invalid attachment name `%s'", at);
1298 				return;
1299 			}
1300 			cp = intern(base);
1301 		} else {
1302 			cp = intern(at);
1303 			unit = STAR;
1304 		}
1305 
1306 		ad = ht_lookup(devbasetab, cp);
1307 		a = ht_lookup(attrtab, cp);
1308 		if (a == NULL) {
1309 			cfgerror("unknown attachment attribute or device `%s'",
1310 			    cp);
1311 			return;
1312 		}
1313 		if (!a->a_iattr) {
1314 			cfgerror("plain attribute `%s' cannot have children",
1315 			    a->a_name);
1316 			return;
1317 		}
1318 
1319 		/*
1320 		 * remove_devi() makes changes to the devbase's list and the
1321 		 * alias list, * so the actual deletion of the instances must
1322 		 * be delayed.
1323 		 */
1324 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
1325 			d = nv->nv_ptr;
1326 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1327 				for (j = i; j != NULL; j = j->i_alias) {
1328 					/* Ignore devices at root */
1329 					if (j->i_at == NULL)
1330 						continue;
1331 					p = j->i_pspec;
1332 					/*
1333 					 * There are three cases:
1334 					 *
1335 					 * 1.  unit is not STAR.  Consider 'at'
1336 					 *     to be explicit, even if it
1337 					 *     references an interface
1338 					 *     attribute.
1339 					 *
1340 					 * 2.  unit is STAR and 'at' references
1341 					 *     a real device.  Look for pspec
1342 					 *     that have a matching p_atdev
1343 					 *     field.
1344 					 *
1345 					 * 3.  unit is STAR and 'at' references
1346 					 *     an interface attribute.  Look
1347 					 *     for pspec that have a matching
1348 					 *     p_iattr field.
1349 					 */
1350 					if ((unit != STAR &&        /* Case */
1351 					     !strcmp(j->i_at, at)) ||  /* 1 */
1352 					    (unit == STAR &&
1353 					     ((ad != NULL &&        /* Case */
1354 					       p->p_atdev == ad) ||    /* 2 */
1355 					      (ad == NULL &&        /* Case */
1356 					       p->p_iattr == a))))     /* 3 */
1357 						stack = newnv(NULL, NULL, j, 0,
1358 						    stack);
1359 				}
1360 		}
1361 	}
1362 
1363 	for (nv = stack; nv != NULL; nv = nv->nv_next)
1364 		remove_devi(nv->nv_ptr);
1365 	nvfreel(stack);
1366 }
1367 
1368 void
1369 deldev(const char *name)
1370 {
1371 	int l;
1372 	struct devi *firsti, *i;
1373 	struct nvlist *nv, *stack = NULL;
1374 
1375 	l = strlen(name) - 1;
1376 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
1377 		/* `no mydev0' or `no mydev*' */
1378 		firsti = ht_lookup(devitab, name);
1379 		if (firsti == NULL) {
1380 			cfgerror("unknown instance %s", name);
1381 			return;
1382 		}
1383 		for (i = firsti; i != NULL; i = i->i_alias)
1384 			stack = newnv(NULL, NULL, i, 0, stack);
1385 	} else {
1386 		struct devbase *d = ht_lookup(devbasetab, name);
1387 
1388 		if (d == NULL) {
1389 			cfgerror("unknown device %s", name);
1390 			return;
1391 		}
1392 		if (d->d_ispseudo) {
1393 			cfgerror("%s is a pseudo-device; "
1394 			    "use \"no pseudo-device %s\" instead", name,
1395 			    name);
1396 			return;
1397 		}
1398 
1399 		for (firsti = d->d_ihead; firsti != NULL;
1400 		    firsti = firsti->i_bsame)
1401 			for (i = firsti; i != NULL; i = i->i_alias)
1402 				stack = newnv(NULL, NULL, i, 0, stack);
1403 	}
1404 
1405 	for (nv = stack; nv != NULL; nv = nv->nv_next)
1406 		remove_devi(nv->nv_ptr);
1407 	nvfreel(stack);
1408 }
1409 
1410 void
1411 addpseudo(const char *name, int number)
1412 {
1413 	struct devbase *d;
1414 	struct devi *i;
1415 
1416 	d = ht_lookup(devbasetab, name);
1417 	if (d == NULL) {
1418 		cfgerror("undefined pseudo-device %s", name);
1419 		return;
1420 	}
1421 	if (!d->d_ispseudo) {
1422 		cfgerror("%s is a real device, not a pseudo-device", name);
1423 		return;
1424 	}
1425 	if (ht_lookup(devitab, name) != NULL) {
1426 		cfgerror("`%s' already defined", name);
1427 		return;
1428 	}
1429 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
1430 	if (ht_insert(devitab, name, i))
1431 		panic("addpseudo(%s)", name);
1432 	/* Useful to retrieve the instance from the devbase */
1433 	d->d_ihead = i;
1434 	i->i_active = DEVI_ACTIVE;
1435 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
1436 }
1437 
1438 void
1439 delpseudo(const char *name)
1440 {
1441 	struct devbase *d;
1442 	struct devi *i;
1443 
1444 	d = ht_lookup(devbasetab, name);
1445 	if (d == NULL) {
1446 		cfgerror("undefined pseudo-device %s", name);
1447 		return;
1448 	}
1449 	if (!d->d_ispseudo) {
1450 		cfgerror("%s is a real device, not a pseudo-device", name);
1451 		return;
1452 	}
1453 	if ((i = ht_lookup(devitab, name)) == NULL) {
1454 		cfgerror("`%s' not defined", name);
1455 		return;
1456 	}
1457 	d->d_umax = 0;		/* clear neads-count entries */
1458 	d->d_ihead = NULL;	/* make sure it won't be considered active */
1459 	TAILQ_REMOVE(&allpseudo, i, i_next);
1460 	if (ht_remove(devitab, name))
1461 		panic("delpseudo(%s) - can't remove from devitab", name);
1462 	if (ht_insert(deaddevitab, name, i))
1463 		panic("delpseudo(%s) - can't add to deaddevitab", name);
1464 }
1465 
1466 void
1467 adddevm(const char *name, int cmajor, int bmajor, struct nvlist *options)
1468 {
1469 	struct devm *dm;
1470 
1471 	if (cmajor < -1 || cmajor >= 4096) {
1472 		cfgerror("character major %d is invalid", cmajor);
1473 		nvfreel(options);
1474 		return;
1475 	}
1476 
1477 	if (bmajor < -1 || bmajor >= 4096) {
1478 		cfgerror("block major %d is invalid", bmajor);
1479 		nvfreel(options);
1480 		return;
1481 	}
1482 	if (cmajor == -1 && bmajor == -1) {
1483 		cfgerror("both character/block majors are not specified");
1484 		nvfreel(options);
1485 		return;
1486 	}
1487 
1488 	dm = ecalloc(1, sizeof(*dm));
1489 	dm->dm_srcfile = yyfile;
1490 	dm->dm_srcline = currentline();
1491 	dm->dm_name = name;
1492 	dm->dm_cmajor = cmajor;
1493 	dm->dm_bmajor = bmajor;
1494 	dm->dm_opts = options;
1495 
1496 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
1497 
1498 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
1499 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
1500 }
1501 
1502 int
1503 fixdevis(void)
1504 {
1505 	struct devi *i;
1506 	int error = 0;
1507 
1508 	TAILQ_FOREACH(i, &alldevi, i_next)
1509 		if (i->i_active == DEVI_ACTIVE)
1510 			selectbase(i->i_base, i->i_atdeva);
1511 		else if (i->i_active == DEVI_ORPHAN) {
1512 			/*
1513 			 * At this point, we can't have instances for which
1514 			 * i_at or i_pspec are NULL.
1515 			 */
1516 			++error;
1517 			cfgxerror(i->i_srcfile, i->i_lineno,
1518 			    "`%s at %s' is orphaned (%s `%s' found)",
1519 			    i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ?
1520 			    "nothing matching" : "no", i->i_at);
1521 		} else if (vflag && i->i_active == DEVI_IGNORED)
1522 			cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
1523 			    "explicitly orphaned instance `%s at %s'",
1524 			    i->i_name, i->i_at);
1525 
1526 	if (error)
1527 		return error;
1528 
1529 	TAILQ_FOREACH(i, &allpseudo, i_next)
1530 		if (i->i_active == DEVI_ACTIVE)
1531 			selectbase(i->i_base, NULL);
1532 	return 0;
1533 }
1534 
1535 /*
1536  * Look up a parent spec, creating a new one if it does not exist.
1537  */
1538 static struct pspec *
1539 getpspec(struct attr *attr, struct devbase *ab, int atunit)
1540 {
1541 	struct pspec *p;
1542 
1543 	TAILQ_FOREACH(p, &allpspecs, p_list) {
1544 		if (p->p_iattr == attr &&
1545 		    p->p_atdev == ab &&
1546 		    p->p_atunit == atunit)
1547 			return (p);
1548 	}
1549 
1550 	p = ecalloc(1, sizeof(*p));
1551 
1552 	p->p_iattr = attr;
1553 	p->p_atdev = ab;
1554 	p->p_atunit = atunit;
1555 	p->p_inst = npspecs++;
1556 	p->p_active = 0;
1557 
1558 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
1559 
1560 	return (p);
1561 }
1562 
1563 /*
1564  * Define a new instance of a specific device.
1565  */
1566 static struct devi *
1567 getdevi(const char *name)
1568 {
1569 	struct devi *i, *firsti;
1570 	struct devbase *d;
1571 	int unit;
1572 	char base[NAMESIZE];
1573 
1574 	if (split(name, strlen(name), base, sizeof base, &unit)) {
1575 		cfgerror("invalid device name `%s'", name);
1576 		return (NULL);
1577 	}
1578 	d = ht_lookup(devbasetab, intern(base));
1579 	if (d == NULL) {
1580 		cfgerror("%s: unknown device `%s'", name, base);
1581 		return (NULL);
1582 	}
1583 	if (d->d_ispseudo) {
1584 		cfgerror("%s: %s is a pseudo-device", name, base);
1585 		return (NULL);
1586 	}
1587 	firsti = ht_lookup(devitab, name);
1588 	i = newdevi(name, unit, d);
1589 	if (firsti == NULL) {
1590 		if (ht_insert(devitab, name, i))
1591 			panic("getdevi(%s)", name);
1592 		*d->d_ipp = i;
1593 		d->d_ipp = &i->i_bsame;
1594 	} else {
1595 		while (firsti->i_alias)
1596 			firsti = firsti->i_alias;
1597 		firsti->i_alias = i;
1598 	}
1599 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
1600 	ndevi++;
1601 	return (i);
1602 }
1603 
1604 static const char *
1605 concat(const char *name, int c)
1606 {
1607 	size_t len;
1608 	char buf[NAMESIZE];
1609 
1610 	len = strlen(name);
1611 	if (len + 2 > sizeof(buf)) {
1612 		cfgerror("device name `%s%c' too long", name, c);
1613 		len = sizeof(buf) - 2;
1614 	}
1615 	memmove(buf, name, len);
1616 	buf[len] = c;
1617 	buf[len + 1] = 0;
1618 	return (intern(buf));
1619 }
1620 
1621 const char *
1622 starref(const char *name)
1623 {
1624 
1625 	return (concat(name, '*'));
1626 }
1627 
1628 const char *
1629 wildref(const char *name)
1630 {
1631 
1632 	return (concat(name, '?'));
1633 }
1634 
1635 /*
1636  * Split a name like "foo0" into base name (foo) and unit number (0).
1637  * Return 0 on success.  To make this useful for names like "foo0a",
1638  * the length of the "foo0" part is one of the arguments.
1639  */
1640 static int
1641 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
1642 {
1643 	const char *cp;
1644 	int c;
1645 	size_t l;
1646 
1647 	l = nlen;
1648 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
1649 		return (1);
1650 	c = (u_char)name[--l];
1651 	if (!isdigit(c)) {
1652 		if (c == '*')
1653 			*aunit = STAR;
1654 		else if (c == '?')
1655 			*aunit = WILD;
1656 		else
1657 			return (1);
1658 	} else {
1659 		cp = &name[l];
1660 		while (isdigit((unsigned char)cp[-1]))
1661 			l--, cp--;
1662 		*aunit = atoi(cp);
1663 	}
1664 	memmove(base, name, l);
1665 	base[l] = 0;
1666 	return (0);
1667 }
1668 
1669 void
1670 selectattr(struct attr *a)
1671 {
1672 
1673 	(void)ht_insert(selecttab, a->a_name, __UNCONST(a->a_name));
1674 }
1675 
1676 /*
1677  * We have an instance of the base foo, so select it and all its
1678  * attributes for "optional foo".
1679  */
1680 static void
1681 selectbase(struct devbase *d, struct deva *da)
1682 {
1683 	struct attr *a;
1684 	struct nvlist *nv;
1685 
1686 	(void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
1687 	for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
1688 		a = nv->nv_ptr;
1689 		expandattr(a, selectattr);
1690 	}
1691 	if (da != NULL) {
1692 		(void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
1693 		for (nv = da->d_attrs; nv != NULL; nv = nv->nv_next) {
1694 			a = nv->nv_ptr;
1695 			expandattr(a, selectattr);
1696 		}
1697 	}
1698 }
1699 
1700 /*
1701  * Is the given pointer on the given list of pointers?
1702  */
1703 int
1704 onlist(struct nvlist *nv, void *ptr)
1705 {
1706 	for (; nv != NULL; nv = nv->nv_next)
1707 		if (nv->nv_ptr == ptr)
1708 			return (1);
1709 	return (0);
1710 }
1711 
1712 static char *
1713 extend(char *p, const char *name)
1714 {
1715 	size_t l;
1716 
1717 	l = strlen(name);
1718 	memmove(p, name, l);
1719 	p += l;
1720 	*p++ = ',';
1721 	*p++ = ' ';
1722 	return (p);
1723 }
1724 
1725 /*
1726  * Check that we got all required locators, and default any that are
1727  * given as "?" and have defaults.  Return 0 on success.
1728  */
1729 static const char **
1730 fixloc(const char *name, struct attr *attr, struct nvlist *got)
1731 {
1732 	struct nvlist *m, *n;
1733 	int ord;
1734 	const char **lp;
1735 	int nmissing, nextra, nnodefault;
1736 	char *mp, *ep, *ndp;
1737 	char missing[1000], extra[1000], nodefault[1000];
1738 	static const char *nullvec[1];
1739 
1740 	/*
1741 	 * Look for all required locators, and number the given ones
1742 	 * according to the required order.  While we are numbering,
1743 	 * set default values for defaulted locators.
1744 	 */
1745 	if (attr->a_loclen == 0)	/* e.g., "at root" */
1746 		lp = nullvec;
1747 	else
1748 		lp = emalloc((attr->a_loclen + 1) * sizeof(const char *));
1749 	for (n = got; n != NULL; n = n->nv_next)
1750 		n->nv_num = -1;
1751 	nmissing = 0;
1752 	mp = missing;
1753 	/* yes, this is O(mn), but m and n should be small */
1754 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->nv_next, ord++) {
1755 		for (n = got; n != NULL; n = n->nv_next) {
1756 			if (n->nv_name == m->nv_name) {
1757 				n->nv_num = ord;
1758 				break;
1759 			}
1760 		}
1761 		if (n == NULL && m->nv_num == 0) {
1762 			nmissing++;
1763 			mp = extend(mp, m->nv_name);
1764 		}
1765 		lp[ord] = m->nv_str;
1766 	}
1767 	if (ord != attr->a_loclen)
1768 		panic("fixloc");
1769 	lp[ord] = NULL;
1770 	nextra = 0;
1771 	ep = extra;
1772 	nnodefault = 0;
1773 	ndp = nodefault;
1774 	for (n = got; n != NULL; n = n->nv_next) {
1775 		if (n->nv_num >= 0) {
1776 			if (n->nv_str != NULL)
1777 				lp[n->nv_num] = n->nv_str;
1778 			else if (lp[n->nv_num] == NULL) {
1779 				nnodefault++;
1780 				ndp = extend(ndp, n->nv_name);
1781 			}
1782 		} else {
1783 			nextra++;
1784 			ep = extend(ep, n->nv_name);
1785 		}
1786 	}
1787 	if (nextra) {
1788 		ep[-2] = 0;	/* kill ", " */
1789 		cfgerror("%s: extraneous locator%s: %s",
1790 		    name, nextra > 1 ? "s" : "", extra);
1791 	}
1792 	if (nmissing) {
1793 		mp[-2] = 0;
1794 		cfgerror("%s: must specify %s", name, missing);
1795 	}
1796 	if (nnodefault) {
1797 		ndp[-2] = 0;
1798 		cfgerror("%s: cannot wildcard %s", name, nodefault);
1799 	}
1800 	if (nmissing || nnodefault) {
1801 		free(lp);
1802 		lp = NULL;
1803 	}
1804 	return (lp);
1805 }
1806 
1807 void
1808 setversion(int newver)
1809 {
1810 	if (newver > CONFIG_VERSION)
1811 		cfgerror("your sources require a newer version of config(1) "
1812 		    "-- please rebuild it.");
1813 	else if (newver < CONFIG_MINVERSION)
1814 		cfgerror("your sources are out of date -- please update.");
1815 	else
1816 		version = newver;
1817 }
1818