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