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