xref: /netbsd/usr.bin/config/sem.c (revision 437f8925)
1*437f8925Scube /*	$NetBSD: sem.c,v 1.14 2005/10/12 01:17:43 cube Exp $	*/
25ecc953bSthorpej 
35ecc953bSthorpej /*
45ecc953bSthorpej  * Copyright (c) 1992, 1993
55ecc953bSthorpej  *	The Regents of the University of California.  All rights reserved.
65ecc953bSthorpej  *
75ecc953bSthorpej  * This software was developed by the Computer Systems Engineering group
85ecc953bSthorpej  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
95ecc953bSthorpej  * contributed to Berkeley.
105ecc953bSthorpej  *
115ecc953bSthorpej  * All advertising materials mentioning features or use of this software
125ecc953bSthorpej  * must display the following acknowledgement:
135ecc953bSthorpej  *	This product includes software developed by the University of
145ecc953bSthorpej  *	California, Lawrence Berkeley Laboratories.
155ecc953bSthorpej  *
165ecc953bSthorpej  * Redistribution and use in source and binary forms, with or without
175ecc953bSthorpej  * modification, are permitted provided that the following conditions
185ecc953bSthorpej  * are met:
195ecc953bSthorpej  * 1. Redistributions of source code must retain the above copyright
205ecc953bSthorpej  *    notice, this list of conditions and the following disclaimer.
215ecc953bSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
225ecc953bSthorpej  *    notice, this list of conditions and the following disclaimer in the
235ecc953bSthorpej  *    documentation and/or other materials provided with the distribution.
245ecc953bSthorpej  * 3. Neither the name of the University nor the names of its contributors
255ecc953bSthorpej  *    may be used to endorse or promote products derived from this software
265ecc953bSthorpej  *    without specific prior written permission.
275ecc953bSthorpej  *
285ecc953bSthorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
295ecc953bSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
305ecc953bSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
315ecc953bSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
325ecc953bSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
335ecc953bSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
345ecc953bSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
355ecc953bSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
365ecc953bSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
375ecc953bSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
385ecc953bSthorpej  * SUCH DAMAGE.
395ecc953bSthorpej  *
405ecc953bSthorpej  *	from: @(#)sem.c	8.1 (Berkeley) 6/6/93
415ecc953bSthorpej  */
425ecc953bSthorpej 
435ecc953bSthorpej #if HAVE_NBTOOL_CONFIG_H
445ecc953bSthorpej #include "nbtool_config.h"
455ecc953bSthorpej #endif
465ecc953bSthorpej 
475ecc953bSthorpej #include <sys/param.h>
485ecc953bSthorpej #include <ctype.h>
495ecc953bSthorpej #include <stdio.h>
505ecc953bSthorpej #include <stdlib.h>
515ecc953bSthorpej #include <string.h>
525ecc953bSthorpej #include "defs.h"
535ecc953bSthorpej #include "sem.h"
545ecc953bSthorpej 
555ecc953bSthorpej /*
565ecc953bSthorpej  * config semantics.
575ecc953bSthorpej  */
585ecc953bSthorpej 
595ecc953bSthorpej #define	NAMESIZE	100	/* local name buffers */
605ecc953bSthorpej 
615ecc953bSthorpej const char *s_ifnet;		/* magic attribute */
625ecc953bSthorpej const char *s_qmark;
635ecc953bSthorpej const char *s_none;
645ecc953bSthorpej 
655ecc953bSthorpej static struct hashtab *cfhashtab;	/* for config lookup */
665ecc953bSthorpej struct hashtab *devitab;		/* etc */
675ecc953bSthorpej 
685ecc953bSthorpej static struct attr errattr;
695ecc953bSthorpej static struct devbase errdev;
705ecc953bSthorpej static struct deva errdeva;
715ecc953bSthorpej 
725ecc953bSthorpej static int has_errobj(struct nvlist *, void *);
735ecc953bSthorpej static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
745ecc953bSthorpej static int resolve(struct nvlist **, const char *, const char *,
755ecc953bSthorpej 		   struct nvlist *, int);
765ecc953bSthorpej static struct pspec *getpspec(struct attr *, struct devbase *, int);
775ecc953bSthorpej static struct devi *newdevi(const char *, int, struct devbase *d);
785ecc953bSthorpej static struct devi *getdevi(const char *);
79c130d400Scube static void remove_devi(struct devi *);
805ecc953bSthorpej static const char *concat(const char *, int);
815ecc953bSthorpej static char *extend(char *, const char *);
825ecc953bSthorpej static int split(const char *, size_t, char *, size_t, int *);
835ecc953bSthorpej static void selectbase(struct devbase *, struct deva *);
845ecc953bSthorpej static int onlist(struct nvlist *, void *);
855ecc953bSthorpej static const char **fixloc(const char *, struct attr *, struct nvlist *);
865ecc953bSthorpej static const char *makedevstr(int, int);
875ecc953bSthorpej static const char *major2name(int);
885ecc953bSthorpej static int dev2major(struct devbase *);
895ecc953bSthorpej 
905ecc953bSthorpej extern const char *yyfile;
917aa6070dScube extern int vflag;
925ecc953bSthorpej 
935ecc953bSthorpej void
945ecc953bSthorpej initsem(void)
955ecc953bSthorpej {
965ecc953bSthorpej 
975ecc953bSthorpej 	attrtab = ht_new();
985ecc953bSthorpej 	errattr.a_name = "<internal>";
995ecc953bSthorpej 
1005ecc953bSthorpej 	TAILQ_INIT(&allbases);
1015ecc953bSthorpej 
1025ecc953bSthorpej 	TAILQ_INIT(&alldevas);
1035ecc953bSthorpej 
1045ecc953bSthorpej 	TAILQ_INIT(&allpspecs);
1055ecc953bSthorpej 
1065ecc953bSthorpej 	cfhashtab = ht_new();
1075ecc953bSthorpej 	TAILQ_INIT(&allcf);
1085ecc953bSthorpej 
1095ecc953bSthorpej 	TAILQ_INIT(&alldevi);
1105ecc953bSthorpej 	errdev.d_name = "<internal>";
1115ecc953bSthorpej 
1125ecc953bSthorpej 	TAILQ_INIT(&allpseudo);
1135ecc953bSthorpej 
1145ecc953bSthorpej 	TAILQ_INIT(&alldevms);
1155ecc953bSthorpej 
1165ecc953bSthorpej 	s_ifnet = intern("ifnet");
1175ecc953bSthorpej 	s_qmark = intern("?");
1185ecc953bSthorpej 	s_none = intern("none");
1195ecc953bSthorpej }
1205ecc953bSthorpej 
1215ecc953bSthorpej /* Name of include file just ended (set in scan.l) */
1225ecc953bSthorpej extern const char *lastfile;
1235ecc953bSthorpej 
1245ecc953bSthorpej void
1255ecc953bSthorpej enddefs(void)
1265ecc953bSthorpej {
1275ecc953bSthorpej 	struct devbase *dev;
1285ecc953bSthorpej 
1295ecc953bSthorpej 	TAILQ_FOREACH(dev, &allbases, d_next) {
1305ecc953bSthorpej 		if (!dev->d_isdef) {
1315ecc953bSthorpej 			(void)fprintf(stderr,
1325ecc953bSthorpej 			    "%s: device `%s' used but not defined\n",
1335ecc953bSthorpej 			    lastfile, dev->d_name);
1345ecc953bSthorpej 			errors++;
1355ecc953bSthorpej 			continue;
1365ecc953bSthorpej 		}
1375ecc953bSthorpej 	}
1385ecc953bSthorpej 	if (errors) {
1395ecc953bSthorpej 		(void)fprintf(stderr, "*** Stop.\n");
1405ecc953bSthorpej 		exit(1);
1415ecc953bSthorpej 	}
1425ecc953bSthorpej }
1435ecc953bSthorpej 
1445ecc953bSthorpej void
1455ecc953bSthorpej setdefmaxusers(int min, int def, int max)
1465ecc953bSthorpej {
1475ecc953bSthorpej 
1485ecc953bSthorpej 	if (min < 1 || min > def || def > max)
1495ecc953bSthorpej 		error("maxusers must have 1 <= min (%d) <= default (%d) <= max (%d)", min, def, max);
1505ecc953bSthorpej 	else {
1515ecc953bSthorpej 		minmaxusers = min;
1525ecc953bSthorpej 		defmaxusers = def;
1535ecc953bSthorpej 		maxmaxusers = max;
1545ecc953bSthorpej 	}
1555ecc953bSthorpej }
1565ecc953bSthorpej 
1575ecc953bSthorpej void
1585ecc953bSthorpej setmaxusers(int n)
1595ecc953bSthorpej {
1605ecc953bSthorpej 
161895e5687Scube 	if (maxusers == n) {
1625ecc953bSthorpej 		error("duplicate maxusers parameter");
1635ecc953bSthorpej 		return;
1645ecc953bSthorpej 	}
165895e5687Scube 	if (vflag && maxusers != 0)
166895e5687Scube 		warn("maxusers already defined");
1675ecc953bSthorpej 	maxusers = n;
1685ecc953bSthorpej 	if (n < minmaxusers) {
1695ecc953bSthorpej 		error("warning: minimum of %d maxusers assumed", minmaxusers);
1705ecc953bSthorpej 		errors--;	/* take it away */
1715ecc953bSthorpej 		maxusers = minmaxusers;
1725ecc953bSthorpej 	} else if (n > maxmaxusers) {
1735ecc953bSthorpej 		error("warning: maxusers (%d) > %d", n, maxmaxusers);
1745ecc953bSthorpej 		errors--;
1755ecc953bSthorpej 	}
1765ecc953bSthorpej }
1775ecc953bSthorpej 
1785ecc953bSthorpej void
1795ecc953bSthorpej setident(const char *i)
1805ecc953bSthorpej {
1815ecc953bSthorpej 
1825ecc953bSthorpej 	ident = intern(i);
1835ecc953bSthorpej }
1845ecc953bSthorpej 
1855ecc953bSthorpej /*
1865ecc953bSthorpej  * Define an attribute, optionally with an interface (a locator list)
1875ecc953bSthorpej  * and a set of attribute-dependencies.
1885ecc953bSthorpej  *
1895ecc953bSthorpej  * Attribute dependencies MAY NOT be interface attributes.
1905ecc953bSthorpej  *
1915ecc953bSthorpej  * Since an empty locator list is logically different from "no interface",
1925ecc953bSthorpej  * all locator lists include a dummy head node, which we discard here.
1935ecc953bSthorpej  */
1945ecc953bSthorpej int
1955ecc953bSthorpej defattr(const char *name, struct nvlist *locs, struct nvlist *deps,
1965ecc953bSthorpej     int devclass)
1975ecc953bSthorpej {
1985ecc953bSthorpej 	struct attr *a, *dep;
1995ecc953bSthorpej 	struct nvlist *nv;
2005ecc953bSthorpej 	int len;
2015ecc953bSthorpej 
2025ecc953bSthorpej 	if (locs != NULL && devclass)
2035ecc953bSthorpej 		panic("defattr(%s): locators and devclass", name);
2045ecc953bSthorpej 
2055ecc953bSthorpej 	if (deps != NULL && devclass)
2065ecc953bSthorpej 		panic("defattr(%s): dependencies and devclass", name);
2075ecc953bSthorpej 
2085ecc953bSthorpej 	/*
2095ecc953bSthorpej 	 * If this attribute depends on any others, make sure none of
2105ecc953bSthorpej 	 * the dependencies are interface attributes.
2115ecc953bSthorpej 	 */
2125ecc953bSthorpej 	for (nv = deps; nv != NULL; nv = nv->nv_next) {
2135ecc953bSthorpej 		dep = nv->nv_ptr;
2145ecc953bSthorpej 		if (dep->a_iattr) {
2155ecc953bSthorpej 			error("`%s' dependency `%s' is an interface attribute",
2165ecc953bSthorpej 			    name, dep->a_name);
2175ecc953bSthorpej 			return (1);
2185ecc953bSthorpej 		}
2195ecc953bSthorpej 	}
2205ecc953bSthorpej 
2215ecc953bSthorpej 	a = ecalloc(1, sizeof *a);
2225ecc953bSthorpej 	if (ht_insert(attrtab, name, a)) {
2235ecc953bSthorpej 		free(a);
2245ecc953bSthorpej 		error("attribute `%s' already defined", name);
2255ecc953bSthorpej 		nvfreel(locs);
2265ecc953bSthorpej 		return (1);
2275ecc953bSthorpej 	}
2285ecc953bSthorpej 
2295ecc953bSthorpej 	a->a_name = name;
2305ecc953bSthorpej 	if (locs != NULL) {
2315ecc953bSthorpej 		a->a_iattr = 1;
2325ecc953bSthorpej 		a->a_locs = locs->nv_next;
2335ecc953bSthorpej 		nvfree(locs);
2345ecc953bSthorpej 	} else {
2355ecc953bSthorpej 		a->a_iattr = 0;
2365ecc953bSthorpej 		a->a_locs = NULL;
2375ecc953bSthorpej 	}
2385ecc953bSthorpej 	if (devclass) {
2395ecc953bSthorpej 		size_t l = strlen(name) + 4;
2405ecc953bSthorpej 		char *classenum = alloca(l), *cp;
2415ecc953bSthorpej 		int errored = 0;
2425ecc953bSthorpej 
2435ecc953bSthorpej 		strlcpy(classenum, "DV_", l);
2445ecc953bSthorpej 		strlcat(classenum, name, l);
2455ecc953bSthorpej 		for (cp = classenum + 3; *cp; cp++) {
2465ecc953bSthorpej 			if (!errored &&
2475ecc953bSthorpej 			    (!isalnum((unsigned char)*cp) ||
2485ecc953bSthorpej 			      (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) {
2495ecc953bSthorpej 				error("device class names must be lower-case alphanumeric characters");
2505ecc953bSthorpej 				errored = 1;
2515ecc953bSthorpej 			}
2525ecc953bSthorpej 			*cp = toupper((unsigned char)*cp);
2535ecc953bSthorpej 		}
2545ecc953bSthorpej 		a->a_devclass = intern(classenum);
2555ecc953bSthorpej 	} else
2565ecc953bSthorpej 		a->a_devclass = NULL;
2575ecc953bSthorpej 	len = 0;
2585ecc953bSthorpej 	for (nv = a->a_locs; nv != NULL; nv = nv->nv_next)
2595ecc953bSthorpej 		len++;
2605ecc953bSthorpej 	a->a_loclen = len;
2615ecc953bSthorpej 	a->a_devs = NULL;
2625ecc953bSthorpej 	a->a_refs = NULL;
2635ecc953bSthorpej 	a->a_deps = deps;
2645ecc953bSthorpej 	a->a_expanding = 0;
2655ecc953bSthorpej 
2665ecc953bSthorpej 	/* Expand the attribute to check for cycles in the graph. */
2675ecc953bSthorpej 	expandattr(a, NULL);
2685ecc953bSthorpej 
2695ecc953bSthorpej 	return (0);
2705ecc953bSthorpej }
2715ecc953bSthorpej 
2725ecc953bSthorpej /*
2735ecc953bSthorpej  * Return true if the given `error object' is embedded in the given
2745ecc953bSthorpej  * pointer list.
2755ecc953bSthorpej  */
2765ecc953bSthorpej static int
2775ecc953bSthorpej has_errobj(struct nvlist *nv, void *obj)
2785ecc953bSthorpej {
2795ecc953bSthorpej 
2805ecc953bSthorpej 	for (; nv != NULL; nv = nv->nv_next)
2815ecc953bSthorpej 		if (nv->nv_ptr == obj)
2825ecc953bSthorpej 			return (1);
2835ecc953bSthorpej 	return (0);
2845ecc953bSthorpej }
2855ecc953bSthorpej 
2865ecc953bSthorpej /*
2875ecc953bSthorpej  * Return true if the given attribute is embedded in the given
2885ecc953bSthorpej  * pointer list.
2895ecc953bSthorpej  */
2905ecc953bSthorpej int
2915ecc953bSthorpej has_attr(struct nvlist *nv, const char *attr)
2925ecc953bSthorpej {
2935ecc953bSthorpej 	struct attr *a;
2945ecc953bSthorpej 
2955ecc953bSthorpej 	if ((a = getattr(attr)) == NULL)
2965ecc953bSthorpej 		return (0);
2975ecc953bSthorpej 
2985ecc953bSthorpej 	for (; nv != NULL; nv = nv->nv_next)
2995ecc953bSthorpej 		if (nv->nv_ptr == a)
3005ecc953bSthorpej 			return (1);
3015ecc953bSthorpej 	return (0);
3025ecc953bSthorpej }
3035ecc953bSthorpej 
3045ecc953bSthorpej /*
3055ecc953bSthorpej  * Add a device base to a list in an attribute (actually, to any list).
3065ecc953bSthorpej  * Note that this does not check for duplicates, and does reverse the
3075ecc953bSthorpej  * list order, but no one cares anyway.
3085ecc953bSthorpej  */
3095ecc953bSthorpej static struct nvlist *
3105ecc953bSthorpej addtoattr(struct nvlist *l, struct devbase *dev)
3115ecc953bSthorpej {
3125ecc953bSthorpej 	struct nvlist *n;
3135ecc953bSthorpej 
3145ecc953bSthorpej 	n = newnv(NULL, NULL, dev, 0, l);
3155ecc953bSthorpej 	return (n);
3165ecc953bSthorpej }
3175ecc953bSthorpej 
3185ecc953bSthorpej /*
3195ecc953bSthorpej  * Define a device.  This may (or may not) also define an interface
3205ecc953bSthorpej  * attribute and/or refer to existing attributes.
3215ecc953bSthorpej  */
3225ecc953bSthorpej void
3235ecc953bSthorpej defdev(struct devbase *dev, struct nvlist *loclist, struct nvlist *attrs,
3245ecc953bSthorpej        int ispseudo)
3255ecc953bSthorpej {
3265ecc953bSthorpej 	struct nvlist *nv;
3275ecc953bSthorpej 	struct attr *a;
3285ecc953bSthorpej 
3295ecc953bSthorpej 	if (dev == &errdev)
3305ecc953bSthorpej 		goto bad;
3315ecc953bSthorpej 	if (dev->d_isdef) {
3325ecc953bSthorpej 		error("redefinition of `%s'", dev->d_name);
3335ecc953bSthorpej 		goto bad;
3345ecc953bSthorpej 	}
3355ecc953bSthorpej 
3365ecc953bSthorpej 	dev->d_isdef = 1;
3375ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
3385ecc953bSthorpej 		goto bad;
3395ecc953bSthorpej 
3405ecc953bSthorpej 	/*
3415ecc953bSthorpej 	 * Handle implicit attribute definition from locator list.  Do
3425ecc953bSthorpej 	 * this before scanning the `at' list so that we can have, e.g.:
3435ecc953bSthorpej 	 *	device foo at other, foo { slot = -1 }
3445ecc953bSthorpej 	 * (where you can plug in a foo-bus extender to a foo-bus).
3455ecc953bSthorpej 	 */
3465ecc953bSthorpej 	if (loclist != NULL) {
3475ecc953bSthorpej 		nv = loclist;
3485ecc953bSthorpej 		loclist = NULL;	/* defattr disposes of them for us */
3495ecc953bSthorpej 		if (defattr(dev->d_name, nv, NULL, 0))
3505ecc953bSthorpej 			goto bad;
3515ecc953bSthorpej 		attrs = newnv(dev->d_name, NULL, getattr(dev->d_name), 0,
3525ecc953bSthorpej 		    attrs);
353c130d400Scube 
354c130d400Scube 		/*
355c130d400Scube 		 * Pseudo-devices can have children.  Consider them as
356c130d400Scube 		 * attaching at root.
357c130d400Scube 		 */
358c130d400Scube 		if (ispseudo)
359c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
3605ecc953bSthorpej 	}
3615ecc953bSthorpej 
3625ecc953bSthorpej 	/* Committed!  Set up fields. */
3635ecc953bSthorpej 	dev->d_ispseudo = ispseudo;
3645ecc953bSthorpej 	dev->d_attrs = attrs;
3655ecc953bSthorpej 	dev->d_classattr = NULL;		/* for now */
3665ecc953bSthorpej 
3675ecc953bSthorpej 	/*
3685ecc953bSthorpej 	 * For each interface attribute this device refers to, add this
3695ecc953bSthorpej 	 * device to its reference list.  This makes, e.g., finding all
3705ecc953bSthorpej 	 * "scsi"s easier.
3715ecc953bSthorpej 	 *
3725ecc953bSthorpej 	 * While looking through the attributes, set up the device
3735ecc953bSthorpej 	 * class if any are devclass attributes (and error out if the
3745ecc953bSthorpej 	 * device has two classes).
3755ecc953bSthorpej 	 */
3765ecc953bSthorpej 	for (nv = attrs; nv != NULL; nv = nv->nv_next) {
3775ecc953bSthorpej 		a = nv->nv_ptr;
3785ecc953bSthorpej 		if (a->a_iattr)
3795ecc953bSthorpej 			a->a_refs = addtoattr(a->a_refs, dev);
3805ecc953bSthorpej 		if (a->a_devclass != NULL) {
3815ecc953bSthorpej 			if (dev->d_classattr != NULL) {
3825ecc953bSthorpej 				error("device `%s' has multiple classes (`%s' and `%s')",
3835ecc953bSthorpej 				    dev->d_name, dev->d_classattr->a_name,
3845ecc953bSthorpej 				    a->a_name);
3855ecc953bSthorpej 			}
3865ecc953bSthorpej 			dev->d_classattr = a;
3875ecc953bSthorpej 		}
3885ecc953bSthorpej 	}
3895ecc953bSthorpej 	return;
3905ecc953bSthorpej  bad:
3915ecc953bSthorpej 	nvfreel(loclist);
3925ecc953bSthorpej 	nvfreel(attrs);
3935ecc953bSthorpej }
3945ecc953bSthorpej 
3955ecc953bSthorpej /*
3965ecc953bSthorpej  * Look up a devbase.  Also makes sure it is a reasonable name,
3975ecc953bSthorpej  * i.e., does not end in a digit or contain special characters.
3985ecc953bSthorpej  */
3995ecc953bSthorpej struct devbase *
4005ecc953bSthorpej getdevbase(const char *name)
4015ecc953bSthorpej {
4025ecc953bSthorpej 	u_char *p;
4035ecc953bSthorpej 	struct devbase *dev;
4045ecc953bSthorpej 
4055ecc953bSthorpej 	p = (u_char *)name;
4065ecc953bSthorpej 	if (!isalpha(*p))
4075ecc953bSthorpej 		goto badname;
4085ecc953bSthorpej 	while (*++p) {
4095ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
4105ecc953bSthorpej 			goto badname;
4115ecc953bSthorpej 	}
4125ecc953bSthorpej 	if (isdigit(*--p)) {
4135ecc953bSthorpej  badname:
4145ecc953bSthorpej 		error("bad device base name `%s'", name);
4155ecc953bSthorpej 		return (&errdev);
4165ecc953bSthorpej 	}
4175ecc953bSthorpej 	dev = ht_lookup(devbasetab, name);
4185ecc953bSthorpej 	if (dev == NULL) {
4195ecc953bSthorpej 		dev = ecalloc(1, sizeof *dev);
4205ecc953bSthorpej 		dev->d_name = name;
4215ecc953bSthorpej 		dev->d_isdef = 0;
4225ecc953bSthorpej 		dev->d_major = NODEV;
4235ecc953bSthorpej 		dev->d_attrs = NULL;
4245ecc953bSthorpej 		dev->d_ihead = NULL;
4255ecc953bSthorpej 		dev->d_ipp = &dev->d_ihead;
4265ecc953bSthorpej 		dev->d_ahead = NULL;
4275ecc953bSthorpej 		dev->d_app = &dev->d_ahead;
4285ecc953bSthorpej 		dev->d_umax = 0;
4295ecc953bSthorpej 		TAILQ_INSERT_TAIL(&allbases, dev, d_next);
4305ecc953bSthorpej 		if (ht_insert(devbasetab, name, dev))
4315ecc953bSthorpej 			panic("getdevbase(%s)", name);
4325ecc953bSthorpej 	}
4335ecc953bSthorpej 	return (dev);
4345ecc953bSthorpej }
4355ecc953bSthorpej 
4365ecc953bSthorpej /*
4375ecc953bSthorpej  * Define some of a device's allowable parent attachments.
4385ecc953bSthorpej  * There may be a list of (plain) attributes.
4395ecc953bSthorpej  */
4405ecc953bSthorpej void
4415ecc953bSthorpej defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
4425ecc953bSthorpej 	     struct nvlist *attrs)
4435ecc953bSthorpej {
4445ecc953bSthorpej 	struct nvlist *nv;
4455ecc953bSthorpej 	struct attr *a;
4465ecc953bSthorpej 	struct deva *da;
4475ecc953bSthorpej 
4485ecc953bSthorpej 	if (dev == &errdev)
4495ecc953bSthorpej 		goto bad;
4505ecc953bSthorpej 	if (deva == NULL)
4515ecc953bSthorpej 		deva = getdevattach(dev->d_name);
4525ecc953bSthorpej 	if (deva == &errdeva)
4535ecc953bSthorpej 		goto bad;
4545ecc953bSthorpej 	if (!dev->d_isdef) {
4555ecc953bSthorpej 		error("attaching undefined device `%s'", dev->d_name);
4565ecc953bSthorpej 		goto bad;
4575ecc953bSthorpej 	}
4585ecc953bSthorpej 	if (deva->d_isdef) {
4595ecc953bSthorpej 		error("redefinition of `%s'", deva->d_name);
4605ecc953bSthorpej 		goto bad;
4615ecc953bSthorpej 	}
4625ecc953bSthorpej 	if (dev->d_ispseudo) {
4635ecc953bSthorpej 		error("pseudo-devices can't attach");
4645ecc953bSthorpej 		goto bad;
4655ecc953bSthorpej 	}
4665ecc953bSthorpej 
4675ecc953bSthorpej 	deva->d_isdef = 1;
4685ecc953bSthorpej 	if (has_errobj(attrs, &errattr))
4695ecc953bSthorpej 		goto bad;
4705ecc953bSthorpej 	for (nv = attrs; nv != NULL; nv = nv->nv_next) {
4715ecc953bSthorpej 		a = nv->nv_ptr;
4725ecc953bSthorpej 		if (a == &errattr)
4735ecc953bSthorpej 			continue;		/* already complained */
4745ecc953bSthorpej 		if (a->a_iattr || a->a_devclass != NULL)
4755ecc953bSthorpej 			error("`%s' is not a plain attribute", a->a_name);
4765ecc953bSthorpej 	}
4775ecc953bSthorpej 
4785ecc953bSthorpej 	/* Committed!  Set up fields. */
4795ecc953bSthorpej 	deva->d_attrs = attrs;
4805ecc953bSthorpej 	deva->d_atlist = atlist;
4815ecc953bSthorpej 	deva->d_devbase = dev;
4825ecc953bSthorpej 
4835ecc953bSthorpej 	/*
4845ecc953bSthorpej 	 * Turn the `at' list into interface attributes (map each
4855ecc953bSthorpej 	 * nv_name to an attribute, or to NULL for root), and add
4865ecc953bSthorpej 	 * this device to those attributes, so that children can
4875ecc953bSthorpej 	 * be listed at this particular device if they are supported
4885ecc953bSthorpej 	 * by that attribute.
4895ecc953bSthorpej 	 */
4905ecc953bSthorpej 	for (nv = atlist; nv != NULL; nv = nv->nv_next) {
4915ecc953bSthorpej 		if (nv->nv_name == NULL)
4925ecc953bSthorpej 			nv->nv_ptr = a = NULL;	/* at root */
4935ecc953bSthorpej 		else
4945ecc953bSthorpej 			nv->nv_ptr = a = getattr(nv->nv_name);
4955ecc953bSthorpej 		if (a == &errattr)
4965ecc953bSthorpej 			continue;		/* already complained */
4975ecc953bSthorpej 
4985ecc953bSthorpej 		/*
4995ecc953bSthorpej 		 * Make sure that an attachment spec doesn't
5005ecc953bSthorpej 		 * already say how to attach to this attribute.
5015ecc953bSthorpej 		 */
5025ecc953bSthorpej 		for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
5035ecc953bSthorpej 			if (onlist(da->d_atlist, a))
5045ecc953bSthorpej 				error("attach at `%s' already done by `%s'",
5055ecc953bSthorpej 				     a ? a->a_name : "root", da->d_name);
5065ecc953bSthorpej 
507c130d400Scube 		if (a == NULL) {
508c130d400Scube 			ht_insert(devroottab, dev->d_name, dev);
5095ecc953bSthorpej 			continue;		/* at root; don't add */
510c130d400Scube 		}
5115ecc953bSthorpej 		if (!a->a_iattr)
5125ecc953bSthorpej 			error("%s cannot be at plain attribute `%s'",
5135ecc953bSthorpej 			    dev->d_name, a->a_name);
5145ecc953bSthorpej 		else
5155ecc953bSthorpej 			a->a_devs = addtoattr(a->a_devs, dev);
5165ecc953bSthorpej 	}
5175ecc953bSthorpej 
5185ecc953bSthorpej 	/* attach to parent */
5195ecc953bSthorpej 	*dev->d_app = deva;
5205ecc953bSthorpej 	dev->d_app = &deva->d_bsame;
5215ecc953bSthorpej 	return;
5225ecc953bSthorpej  bad:
5235ecc953bSthorpej 	nvfreel(atlist);
5245ecc953bSthorpej 	nvfreel(attrs);
5255ecc953bSthorpej }
5265ecc953bSthorpej 
5275ecc953bSthorpej /*
5285ecc953bSthorpej  * Look up a device attachment.  Also makes sure it is a reasonable
5295ecc953bSthorpej  * name, i.e., does not contain digits or special characters.
5305ecc953bSthorpej  */
5315ecc953bSthorpej struct deva *
5325ecc953bSthorpej getdevattach(const char *name)
5335ecc953bSthorpej {
5345ecc953bSthorpej 	u_char *p;
5355ecc953bSthorpej 	struct deva *deva;
5365ecc953bSthorpej 
5375ecc953bSthorpej 	p = (u_char *)name;
5385ecc953bSthorpej 	if (!isalpha(*p))
5395ecc953bSthorpej 		goto badname;
5405ecc953bSthorpej 	while (*++p) {
5415ecc953bSthorpej 		if (!isalnum(*p) && *p != '_')
5425ecc953bSthorpej 			goto badname;
5435ecc953bSthorpej 	}
5445ecc953bSthorpej 	if (isdigit(*--p)) {
5455ecc953bSthorpej  badname:
5465ecc953bSthorpej 		error("bad device attachment name `%s'", name);
5475ecc953bSthorpej 		return (&errdeva);
5485ecc953bSthorpej 	}
5495ecc953bSthorpej 	deva = ht_lookup(devatab, name);
5505ecc953bSthorpej 	if (deva == NULL) {
5515ecc953bSthorpej 		deva = ecalloc(1, sizeof *deva);
5525ecc953bSthorpej 		deva->d_name = name;
5535ecc953bSthorpej 		deva->d_bsame = NULL;
5545ecc953bSthorpej 		deva->d_isdef = 0;
5555ecc953bSthorpej 		deva->d_devbase = NULL;
5565ecc953bSthorpej 		deva->d_atlist = NULL;
5575ecc953bSthorpej 		deva->d_attrs = NULL;
5585ecc953bSthorpej 		deva->d_ihead = NULL;
5595ecc953bSthorpej 		deva->d_ipp = &deva->d_ihead;
5605ecc953bSthorpej 		TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
5615ecc953bSthorpej 		if (ht_insert(devatab, name, deva))
5625ecc953bSthorpej 			panic("getdeva(%s)", name);
5635ecc953bSthorpej 	}
5645ecc953bSthorpej 	return (deva);
5655ecc953bSthorpej }
5665ecc953bSthorpej 
5675ecc953bSthorpej /*
5685ecc953bSthorpej  * Look up an attribute.
5695ecc953bSthorpej  */
5705ecc953bSthorpej struct attr *
5715ecc953bSthorpej getattr(const char *name)
5725ecc953bSthorpej {
5735ecc953bSthorpej 	struct attr *a;
5745ecc953bSthorpej 
5755ecc953bSthorpej 	if ((a = ht_lookup(attrtab, name)) == NULL) {
5765ecc953bSthorpej 		error("undefined attribute `%s'", name);
5775ecc953bSthorpej 		a = &errattr;
5785ecc953bSthorpej 	}
5795ecc953bSthorpej 	return (a);
5805ecc953bSthorpej }
5815ecc953bSthorpej 
5825ecc953bSthorpej /*
5835ecc953bSthorpej  * Recursively expand an attribute and its dependencies, checking for
5845ecc953bSthorpej  * cycles, and invoking a callback for each attribute found.
5855ecc953bSthorpej  */
5865ecc953bSthorpej void
5875ecc953bSthorpej expandattr(struct attr *a, void (*callback)(struct attr *))
5885ecc953bSthorpej {
5895ecc953bSthorpej 	struct nvlist *nv;
5905ecc953bSthorpej 	struct attr *dep;
5915ecc953bSthorpej 
5925ecc953bSthorpej 	if (a->a_expanding) {
5935ecc953bSthorpej 		error("circular dependency on attribute `%s'", a->a_name);
5945ecc953bSthorpej 		return;
5955ecc953bSthorpej 	}
5965ecc953bSthorpej 
5975ecc953bSthorpej 	a->a_expanding = 1;
5985ecc953bSthorpej 
5995ecc953bSthorpej 	/* First expand all of this attribute's dependencies. */
6005ecc953bSthorpej 	for (nv = a->a_deps; nv != NULL; nv = nv->nv_next) {
6015ecc953bSthorpej 		dep = nv->nv_ptr;
6025ecc953bSthorpej 		expandattr(dep, callback);
6035ecc953bSthorpej 	}
6045ecc953bSthorpej 
6055ecc953bSthorpej 	/* ...and now invoke the callback for ourself. */
6065ecc953bSthorpej 	if (callback != NULL)
6075ecc953bSthorpej 		(*callback)(a);
6085ecc953bSthorpej 
6095ecc953bSthorpej 	a->a_expanding = 0;
6105ecc953bSthorpej }
6115ecc953bSthorpej 
6125ecc953bSthorpej /*
6135ecc953bSthorpej  * Set the major device number for a device, so that it can be used
6145ecc953bSthorpej  * as a root/dumps "on" device in a configuration.
6155ecc953bSthorpej  */
6165ecc953bSthorpej void
6175ecc953bSthorpej setmajor(struct devbase *d, int n)
6185ecc953bSthorpej {
6195ecc953bSthorpej 
6205ecc953bSthorpej 	if (d != &errdev && d->d_major != NODEV)
6215ecc953bSthorpej 		error("device `%s' is already major %d",
6225ecc953bSthorpej 		    d->d_name, d->d_major);
6235ecc953bSthorpej 	else
6245ecc953bSthorpej 		d->d_major = n;
6255ecc953bSthorpej }
6265ecc953bSthorpej 
6275ecc953bSthorpej const char *
6285ecc953bSthorpej major2name(int maj)
6295ecc953bSthorpej {
6305ecc953bSthorpej 	struct devbase *dev;
6315ecc953bSthorpej 	struct devm *dm;
6325ecc953bSthorpej 
6335ecc953bSthorpej 	if (!do_devsw) {
6345ecc953bSthorpej 		TAILQ_FOREACH(dev, &allbases, d_next) {
6355ecc953bSthorpej 			if (dev->d_major == maj)
6365ecc953bSthorpej 				return (dev->d_name);
6375ecc953bSthorpej 		}
6385ecc953bSthorpej 	} else {
6395ecc953bSthorpej 		TAILQ_FOREACH(dm, &alldevms, dm_next) {
6405ecc953bSthorpej 			if (dm->dm_bmajor == maj)
6415ecc953bSthorpej 				return (dm->dm_name);
6425ecc953bSthorpej 		}
6435ecc953bSthorpej 	}
6445ecc953bSthorpej 	return (NULL);
6455ecc953bSthorpej }
6465ecc953bSthorpej 
6475ecc953bSthorpej int
6485ecc953bSthorpej dev2major(struct devbase *dev)
6495ecc953bSthorpej {
6505ecc953bSthorpej 	struct devm *dm;
6515ecc953bSthorpej 
6525ecc953bSthorpej 	if (!do_devsw)
6535ecc953bSthorpej 		return (dev->d_major);
6545ecc953bSthorpej 
6555ecc953bSthorpej 	TAILQ_FOREACH(dm, &alldevms, dm_next) {
6565ecc953bSthorpej 		if (strcmp(dm->dm_name, dev->d_name) == 0)
6575ecc953bSthorpej 			return (dm->dm_bmajor);
6585ecc953bSthorpej 	}
6595ecc953bSthorpej 	return (NODEV);
6605ecc953bSthorpej }
6615ecc953bSthorpej 
6625ecc953bSthorpej /*
6635ecc953bSthorpej  * Make a string description of the device at maj/min.
6645ecc953bSthorpej  */
6655ecc953bSthorpej static const char *
6665ecc953bSthorpej makedevstr(int maj, int min)
6675ecc953bSthorpej {
6685ecc953bSthorpej 	const char *devname;
6695ecc953bSthorpej 	char buf[32];
6705ecc953bSthorpej 
6715ecc953bSthorpej 	devname = major2name(maj);
6725ecc953bSthorpej 	if (devname == NULL)
6735ecc953bSthorpej 		(void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
6745ecc953bSthorpej 	else
6755ecc953bSthorpej 		(void)snprintf(buf, sizeof(buf), "%s%d%c", devname,
6765ecc953bSthorpej 		    min / maxpartitions, (min % maxpartitions) + 'a');
6775ecc953bSthorpej 
6785ecc953bSthorpej 	return (intern(buf));
6795ecc953bSthorpej }
6805ecc953bSthorpej 
6815ecc953bSthorpej /*
6825ecc953bSthorpej  * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
6835ecc953bSthorpej  * Handle the case where the device number is given but there is no
6845ecc953bSthorpej  * corresponding name, and map NULL to the default.
6855ecc953bSthorpej  */
6865ecc953bSthorpej static int
6875ecc953bSthorpej resolve(struct nvlist **nvp, const char *name, const char *what,
6885ecc953bSthorpej 	struct nvlist *dflt, int part)
6895ecc953bSthorpej {
6905ecc953bSthorpej 	struct nvlist *nv;
6915ecc953bSthorpej 	struct devbase *dev;
6925ecc953bSthorpej 	const char *cp;
6935ecc953bSthorpej 	int maj, min, i, l;
6945ecc953bSthorpej 	int unit;
6955ecc953bSthorpej 	char buf[NAMESIZE];
6965ecc953bSthorpej 
6975ecc953bSthorpej 	if ((u_int)(part -= 'a') >= maxpartitions)
6985ecc953bSthorpej 		panic("resolve");
6995ecc953bSthorpej 	if ((nv = *nvp) == NULL) {
7005ecc953bSthorpej 		dev_t	d = NODEV;
7015ecc953bSthorpej 		/*
7025ecc953bSthorpej 		 * Apply default.  Easiest to do this by number.
7035ecc953bSthorpej 		 * Make sure to retain NODEVness, if this is dflt's disposition.
7045ecc953bSthorpej 		 */
7055ecc953bSthorpej 		if (dflt->nv_int != NODEV) {
7065ecc953bSthorpej 			maj = major(dflt->nv_int);
7075ecc953bSthorpej 			min = ((minor(dflt->nv_int) / maxpartitions) *
7085ecc953bSthorpej 			    maxpartitions) + part;
7095ecc953bSthorpej 			d = makedev(maj, min);
7105ecc953bSthorpej 			cp = makedevstr(maj, min);
7115ecc953bSthorpej 		} else
7125ecc953bSthorpej 			cp = NULL;
7135ecc953bSthorpej 		*nvp = nv = newnv(NULL, cp, NULL, d, NULL);
7145ecc953bSthorpej 	}
7155ecc953bSthorpej 	if (nv->nv_int != NODEV) {
7165ecc953bSthorpej 		/*
7175ecc953bSthorpej 		 * By the numbers.  Find the appropriate major number
7185ecc953bSthorpej 		 * to make a name.
7195ecc953bSthorpej 		 */
7205ecc953bSthorpej 		maj = major(nv->nv_int);
7215ecc953bSthorpej 		min = minor(nv->nv_int);
7225ecc953bSthorpej 		nv->nv_str = makedevstr(maj, min);
7235ecc953bSthorpej 		return (0);
7245ecc953bSthorpej 	}
7255ecc953bSthorpej 
7265ecc953bSthorpej 	if (nv->nv_str == NULL || nv->nv_str == s_qmark)
7275ecc953bSthorpej 		/*
7285ecc953bSthorpej 		 * Wildcarded or unspecified; leave it as NODEV.
7295ecc953bSthorpej 		 */
7305ecc953bSthorpej 		return (0);
7315ecc953bSthorpej 
7325ecc953bSthorpej 	/*
7335ecc953bSthorpej 	 * The normal case: things like "ra2b".  Check for partition
7345ecc953bSthorpej 	 * suffix, remove it if there, and split into name ("ra") and
7355ecc953bSthorpej 	 * unit (2).
7365ecc953bSthorpej 	 */
7375ecc953bSthorpej 	l = i = strlen(nv->nv_str);
7385ecc953bSthorpej 	cp = &nv->nv_str[l];
7395ecc953bSthorpej 	if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
7405ecc953bSthorpej 	    isdigit((unsigned char)cp[-1])) {
7415ecc953bSthorpej 		l--;
7425ecc953bSthorpej 		part = *cp - 'a';
7435ecc953bSthorpej 	}
7445ecc953bSthorpej 	cp = nv->nv_str;
7455ecc953bSthorpej 	if (split(cp, l, buf, sizeof buf, &unit)) {
7465ecc953bSthorpej 		error("%s: invalid %s device name `%s'", name, what, cp);
7475ecc953bSthorpej 		return (1);
7485ecc953bSthorpej 	}
7495ecc953bSthorpej 	dev = ht_lookup(devbasetab, intern(buf));
7505ecc953bSthorpej 	if (dev == NULL) {
7515ecc953bSthorpej 		error("%s: device `%s' does not exist", name, buf);
7525ecc953bSthorpej 		return (1);
7535ecc953bSthorpej 	}
7545ecc953bSthorpej 
7555ecc953bSthorpej 	/*
7565ecc953bSthorpej 	 * Check for the magic network interface attribute, and
7575ecc953bSthorpej 	 * don't bother making a device number.
7585ecc953bSthorpej 	 */
7595ecc953bSthorpej 	if (has_attr(dev->d_attrs, s_ifnet)) {
7605ecc953bSthorpej 		nv->nv_int = NODEV;
7615ecc953bSthorpej 		nv->nv_ifunit = unit;	/* XXX XXX XXX */
7625ecc953bSthorpej 	} else {
7635ecc953bSthorpej 		maj = dev2major(dev);
7645ecc953bSthorpej 		if (maj == NODEV) {
7655ecc953bSthorpej 			error("%s: can't make %s device from `%s'",
7665ecc953bSthorpej 			    name, what, nv->nv_str);
7675ecc953bSthorpej 			return (1);
7685ecc953bSthorpej 		}
7695ecc953bSthorpej 		nv->nv_int = makedev(maj, unit * maxpartitions + part);
7705ecc953bSthorpej 	}
7715ecc953bSthorpej 
7725ecc953bSthorpej 	nv->nv_name = dev->d_name;
7735ecc953bSthorpej 	return (0);
7745ecc953bSthorpej }
7755ecc953bSthorpej 
7765ecc953bSthorpej /*
7775ecc953bSthorpej  * Add a completed configuration to the list.
7785ecc953bSthorpej  */
7795ecc953bSthorpej void
7805ecc953bSthorpej addconf(struct config *cf0)
7815ecc953bSthorpej {
7825ecc953bSthorpej 	struct config *cf;
7835ecc953bSthorpej 	struct nvlist *nv;
7845ecc953bSthorpej 	const char *name;
7855ecc953bSthorpej 
7865ecc953bSthorpej 	name = cf0->cf_name;
7875ecc953bSthorpej 	cf = ecalloc(1, sizeof *cf);
7885ecc953bSthorpej 	if (ht_insert(cfhashtab, name, cf)) {
7895ecc953bSthorpej 		error("configuration `%s' already defined", name);
7905ecc953bSthorpej 		free(cf);
7915ecc953bSthorpej 		goto bad;
7925ecc953bSthorpej 	}
7935ecc953bSthorpej 	*cf = *cf0;
7945ecc953bSthorpej 
7955ecc953bSthorpej 	/*
7965ecc953bSthorpej 	 * Resolve the root device.
7975ecc953bSthorpej 	 */
7985ecc953bSthorpej 	if (cf->cf_root->nv_str != s_qmark) {
7995ecc953bSthorpej 		nv = cf->cf_root;
8005ecc953bSthorpej 		if (nv == NULL) {
8015ecc953bSthorpej 			error("%s: no root device specified", name);
8025ecc953bSthorpej 			goto bad;
8035ecc953bSthorpej 		}
8045ecc953bSthorpej 		if (resolve(&cf->cf_root, name, "root", nv, 'a'))
8055ecc953bSthorpej 			goto bad;
8065ecc953bSthorpej 	}
8075ecc953bSthorpej 
8085ecc953bSthorpej 	/*
8095ecc953bSthorpej 	 * Resolve the dump device.
8105ecc953bSthorpej 	 */
8115ecc953bSthorpej 	if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
8125ecc953bSthorpej 		/*
8135ecc953bSthorpej 		 * Wildcarded dump device is equivalent to unspecified.
8145ecc953bSthorpej 		 */
8155ecc953bSthorpej 		cf->cf_dump = NULL;
8165ecc953bSthorpej 	} else if (cf->cf_dump->nv_str == s_none) {
8175ecc953bSthorpej 		/*
8185ecc953bSthorpej 		 * Operator has requested that no dump device should be
8195ecc953bSthorpej 		 * configured; do nothing.
8205ecc953bSthorpej 		 */
8215ecc953bSthorpej 	} else {
8225ecc953bSthorpej 		if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
8235ecc953bSthorpej 			goto bad;
8245ecc953bSthorpej 	}
8255ecc953bSthorpej 
8265ecc953bSthorpej 	/* Wildcarded fstype is `unspecified'. */
8275ecc953bSthorpej 	if (cf->cf_fstype == s_qmark)
8285ecc953bSthorpej 		cf->cf_fstype = NULL;
8295ecc953bSthorpej 
8305ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
8315ecc953bSthorpej 	return;
8325ecc953bSthorpej  bad:
8335ecc953bSthorpej 	nvfreel(cf0->cf_root);
8345ecc953bSthorpej 	nvfreel(cf0->cf_dump);
8355ecc953bSthorpej }
8365ecc953bSthorpej 
8375ecc953bSthorpej void
8385ecc953bSthorpej setconf(struct nvlist **npp, const char *what, struct nvlist *v)
8395ecc953bSthorpej {
8405ecc953bSthorpej 
8415ecc953bSthorpej 	if (*npp != NULL) {
8425ecc953bSthorpej 		error("duplicate %s specification", what);
8435ecc953bSthorpej 		nvfreel(v);
8445ecc953bSthorpej 	} else
8455ecc953bSthorpej 		*npp = v;
8465ecc953bSthorpej }
8475ecc953bSthorpej 
8485ecc953bSthorpej void
8495ecc953bSthorpej setfstype(const char **fstp, const char *v)
8505ecc953bSthorpej {
8515ecc953bSthorpej 
8525ecc953bSthorpej 	if (*fstp != NULL) {
8535ecc953bSthorpej 		error("multiple fstype specifications");
8545ecc953bSthorpej 		return;
8555ecc953bSthorpej 	}
8565ecc953bSthorpej 
8575ecc953bSthorpej 	if (v != s_qmark && OPT_FSOPT(v)) {
8585ecc953bSthorpej 		error("\"%s\" is not a configured file system", v);
8595ecc953bSthorpej 		return;
8605ecc953bSthorpej 	}
8615ecc953bSthorpej 
8625ecc953bSthorpej 	*fstp = v;
8635ecc953bSthorpej }
8645ecc953bSthorpej 
8655ecc953bSthorpej static struct devi *
8665ecc953bSthorpej newdevi(const char *name, int unit, struct devbase *d)
8675ecc953bSthorpej {
8685ecc953bSthorpej 	struct devi *i;
8695ecc953bSthorpej 
8705ecc953bSthorpej 	i = ecalloc(1, sizeof *i);
8715ecc953bSthorpej 	i->i_name = name;
8725ecc953bSthorpej 	i->i_unit = unit;
8735ecc953bSthorpej 	i->i_base = d;
8745ecc953bSthorpej 	i->i_bsame = NULL;
8755ecc953bSthorpej 	i->i_asame = NULL;
8765ecc953bSthorpej 	i->i_alias = NULL;
8775ecc953bSthorpej 	i->i_at = NULL;
8785ecc953bSthorpej 	i->i_pspec = NULL;
8795ecc953bSthorpej 	i->i_atdeva = NULL;
8805ecc953bSthorpej 	i->i_locs = NULL;
8815ecc953bSthorpej 	i->i_cfflags = 0;
8825ecc953bSthorpej 	i->i_lineno = currentline();
8830dbd1c0eScube 	i->i_srcfile = yyfile;
8847aa6070dScube 	i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
885a31ff6b4Scube 	i->i_level = devilevel;
8865ecc953bSthorpej 	if (unit >= d->d_umax)
8875ecc953bSthorpej 		d->d_umax = unit + 1;
8885ecc953bSthorpej 	return (i);
8895ecc953bSthorpej }
8905ecc953bSthorpej 
8915ecc953bSthorpej /*
8925ecc953bSthorpej  * Add the named device as attaching to the named attribute (or perhaps
8935ecc953bSthorpej  * another device instead) plus unit number.
8945ecc953bSthorpej  */
8955ecc953bSthorpej void
8965ecc953bSthorpej adddev(const char *name, const char *at, struct nvlist *loclist, int flags)
8975ecc953bSthorpej {
8985ecc953bSthorpej 	struct devi *i;		/* the new instance */
8995ecc953bSthorpej 	struct pspec *p;	/* and its pspec */
9005ecc953bSthorpej 	struct attr *attr;	/* attribute that allows attach */
9015ecc953bSthorpej 	struct devbase *ib;	/* i->i_base */
9025ecc953bSthorpej 	struct devbase *ab;	/* not NULL => at another dev */
9035ecc953bSthorpej 	struct nvlist *nv;
9045ecc953bSthorpej 	struct deva *iba;	/* devbase attachment used */
9055ecc953bSthorpej 	const char *cp;
9065ecc953bSthorpej 	int atunit;
9075ecc953bSthorpej 	char atbuf[NAMESIZE];
9085ecc953bSthorpej 	int hit;
9095ecc953bSthorpej 
9105ecc953bSthorpej 	ab = NULL;
9115ecc953bSthorpej 	iba = NULL;
9125ecc953bSthorpej 	if (at == NULL) {
9135ecc953bSthorpej 		/* "at root" */
9145ecc953bSthorpej 		p = NULL;
9155ecc953bSthorpej 		if ((i = getdevi(name)) == NULL)
9165ecc953bSthorpej 			goto bad;
9175ecc953bSthorpej 		/*
9185ecc953bSthorpej 		 * Must warn about i_unit > 0 later, after taking care of
9195ecc953bSthorpej 		 * the STAR cases (we could do non-star's here but why
9205ecc953bSthorpej 		 * bother?).  Make sure this device can be at root.
9215ecc953bSthorpej 		 */
9225ecc953bSthorpej 		ib = i->i_base;
9235ecc953bSthorpej 		hit = 0;
9245ecc953bSthorpej 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
9255ecc953bSthorpej 			if (onlist(iba->d_atlist, NULL)) {
9265ecc953bSthorpej 				hit = 1;
9275ecc953bSthorpej 				break;
9285ecc953bSthorpej 			}
9295ecc953bSthorpej 		if (!hit) {
9305ecc953bSthorpej 			error("`%s' cannot attach to the root", ib->d_name);
9315ec393e9Scube 			i->i_active = DEVI_BROKEN;
9325ecc953bSthorpej 			goto bad;
9335ecc953bSthorpej 		}
9345ecc953bSthorpej 		attr = &errattr;	/* a convenient "empty" attr */
9355ecc953bSthorpej 	} else {
9365ecc953bSthorpej 		if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
9375ecc953bSthorpej 			error("invalid attachment name `%s'", at);
9385ecc953bSthorpej 			/* (void)getdevi(name); -- ??? */
9395ecc953bSthorpej 			goto bad;
9405ecc953bSthorpej 		}
9415ecc953bSthorpej 		if ((i = getdevi(name)) == NULL)
9425ecc953bSthorpej 			goto bad;
9435ecc953bSthorpej 		ib = i->i_base;
9445ecc953bSthorpej 
9455ecc953bSthorpej 		/*
9465ecc953bSthorpej 		 * Devices can attach to two types of things: Attributes,
9475ecc953bSthorpej 		 * and other devices (which have the appropriate attributes
9485ecc953bSthorpej 		 * to allow attachment).
9495ecc953bSthorpej 		 *
9505ecc953bSthorpej 		 * (1) If we're attached to an attribute, then we don't need
9515ecc953bSthorpej 		 *     look at the parent base device to see what attributes
9525ecc953bSthorpej 		 *     it has, and make sure that we can attach to them.
9535ecc953bSthorpej 		 *
9545ecc953bSthorpej 		 * (2) If we're attached to a real device (i.e. named in
9555ecc953bSthorpej 		 *     the config file), we want to remember that so that
9565ecc953bSthorpej 		 *     at cross-check time, if the device we're attached to
9575ecc953bSthorpej 		 *     is missing but other devices which also provide the
9585ecc953bSthorpej 		 *     attribute are present, we don't get a false "OK."
9595ecc953bSthorpej 		 *
9605ecc953bSthorpej 		 * (3) If the thing we're attached to is an attribute
9615ecc953bSthorpej 		 *     but is actually named in the config file, we still
9625ecc953bSthorpej 		 *     have to remember its devbase.
9635ecc953bSthorpej 		 */
9645ecc953bSthorpej 		cp = intern(atbuf);
9655ecc953bSthorpej 
9665ecc953bSthorpej 		/* Figure out parent's devbase, to satisfy case (3). */
9675ecc953bSthorpej 		ab = ht_lookup(devbasetab, cp);
9685ecc953bSthorpej 
9695ecc953bSthorpej 		/* Find out if it's an attribute. */
9705ecc953bSthorpej 		attr = ht_lookup(attrtab, cp);
9715ecc953bSthorpej 
9725ecc953bSthorpej 		/* Make sure we're _really_ attached to the attr.  Case (1). */
9735ecc953bSthorpej 		if (attr != NULL && onlist(attr->a_devs, ib))
9745ecc953bSthorpej 			goto findattachment;
9755ecc953bSthorpej 
9765ecc953bSthorpej 		/*
9775ecc953bSthorpej 		 * Else a real device, and not just an attribute.  Case (2).
9785ecc953bSthorpej 		 *
9795ecc953bSthorpej 		 * Have to work a bit harder to see whether we have
9805ecc953bSthorpej 		 * something like "tg0 at esp0" (where esp is merely
9815ecc953bSthorpej 		 * not an attribute) or "tg0 at nonesuch0" (where
9825ecc953bSthorpej 		 * nonesuch is not even a device).
9835ecc953bSthorpej 		 */
9845ecc953bSthorpej 		if (ab == NULL) {
9855ecc953bSthorpej 			error("%s at %s: `%s' unknown",
9865ecc953bSthorpej 			    name, at, atbuf);
9875ec393e9Scube 			i->i_active = DEVI_BROKEN;
9885ecc953bSthorpej 			goto bad;
9895ecc953bSthorpej 		}
9905ecc953bSthorpej 
9915ecc953bSthorpej 		/*
9925ecc953bSthorpej 		 * See if the named parent carries an attribute
9935ecc953bSthorpej 		 * that allows it to supervise device ib.
9945ecc953bSthorpej 		 */
9955ecc953bSthorpej 		for (nv = ab->d_attrs; nv != NULL; nv = nv->nv_next) {
9965ecc953bSthorpej 			attr = nv->nv_ptr;
9975ecc953bSthorpej 			if (onlist(attr->a_devs, ib))
9985ecc953bSthorpej 				goto findattachment;
9995ecc953bSthorpej 		}
10005ecc953bSthorpej 		error("`%s' cannot attach to `%s'", ib->d_name, atbuf);
10015ec393e9Scube 		i->i_active = DEVI_BROKEN;
10025ecc953bSthorpej 		goto bad;
10035ecc953bSthorpej 
10045ecc953bSthorpej  findattachment:
10055ecc953bSthorpej 		/*
10065ecc953bSthorpej 		 * Find the parent spec.  If a matching one has not yet been
10075ecc953bSthorpej 		 * created, create one.
10085ecc953bSthorpej 		 */
10095ecc953bSthorpej 		p = getpspec(attr, ab, atunit);
10105ecc953bSthorpej 		p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
10115ecc953bSthorpej 
10125ecc953bSthorpej 		/* find out which attachment it uses */
10135ecc953bSthorpej 		hit = 0;
10145ecc953bSthorpej 		for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
10155ecc953bSthorpej 			if (onlist(iba->d_atlist, attr)) {
10165ecc953bSthorpej 				hit = 1;
10175ecc953bSthorpej 				break;
10185ecc953bSthorpej 			}
10195ecc953bSthorpej 		if (!hit)
10205ecc953bSthorpej 			panic("adddev: can't figure out attachment");
10215ecc953bSthorpej 	}
10225ec393e9Scube 	if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
10235ec393e9Scube 		i->i_active = DEVI_BROKEN;
10245ecc953bSthorpej 		goto bad;
10255ec393e9Scube 	}
10265ecc953bSthorpej 	i->i_at = at;
10275ecc953bSthorpej 	i->i_pspec = p;
10285ecc953bSthorpej 	i->i_atdeva = iba;
10295ecc953bSthorpej 	i->i_cfflags = flags;
10305ecc953bSthorpej 
10315ecc953bSthorpej 	*iba->d_ipp = i;
10325ecc953bSthorpej 	iba->d_ipp = &i->i_asame;
10335ecc953bSthorpej 
10345ecc953bSthorpej 	/* all done, fall into ... */
10355ecc953bSthorpej  bad:
10365ecc953bSthorpej 	nvfreel(loclist);
10375ecc953bSthorpej 	return;
10385ecc953bSthorpej }
10395ecc953bSthorpej 
10405ecc953bSthorpej void
10417b7c582aScube deldevi(const char *name, const char *at)
10425ecc953bSthorpej {
10437b7c582aScube 	struct devi *firsti, *i;
10445ecc953bSthorpej 	struct devbase *d;
10455ecc953bSthorpej 	int unit;
10465ecc953bSthorpej 	char base[NAMESIZE];
10475ecc953bSthorpej 
10485ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
10495ecc953bSthorpej 		error("invalid device name `%s'", name);
10505ecc953bSthorpej 		return;
10515ecc953bSthorpej 	}
10525ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
10535ecc953bSthorpej 	if (d == NULL) {
10545ecc953bSthorpej 		error("%s: unknown device `%s'", name, base);
10555ecc953bSthorpej 		return;
10565ecc953bSthorpej 	}
10575ecc953bSthorpej 	if (d->d_ispseudo) {
10585ecc953bSthorpej 		error("%s: %s is a pseudo-device", name, base);
10595ecc953bSthorpej 		return;
10605ecc953bSthorpej 	}
10615ecc953bSthorpej 	if ((firsti = ht_lookup(devitab, name)) == NULL) {
10625ecc953bSthorpej 		error("`%s' not defined", name);
10635ecc953bSthorpej 		return;
10645ecc953bSthorpej 	}
10657b7c582aScube 	if (at == NULL && firsti->i_at == NULL) {
1066e499d8b5Scube 		/* 'at root' */
10677b7c582aScube 		remove_devi(firsti);
10687b7c582aScube 		return;
10697b7c582aScube 	} else if (at != NULL)
10707b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
10715ecc953bSthorpej 			if (strcmp(at, i->i_at) == 0) {
10727b7c582aScube 				remove_devi(i);
10735ecc953bSthorpej 				return;
10745ecc953bSthorpej 			}
10757b7c582aScube 	error("`%s' at `%s' not found", name, at ? at : "root");
1076e499d8b5Scube }
10775ecc953bSthorpej 
10787b7c582aScube static void
10797b7c582aScube remove_devi(struct devi *i)
10807b7c582aScube {
10817b7c582aScube 	struct devbase *d = i->i_base;
10827b7c582aScube 	struct devi *f, *j, **ppi;
10837b7c582aScube 	struct deva *iba;
10847b7c582aScube 
10857b7c582aScube 	f = ht_lookup(devitab, i->i_name);
10867b7c582aScube 
1087e499d8b5Scube 	/*
1088e499d8b5Scube 	 * We have the device instance, i.
1089e499d8b5Scube 	 * We have to:
1090e499d8b5Scube 	 *   - delete the alias
1091e499d8b5Scube 	 *
1092e499d8b5Scube 	 *      If the devi was an alias of an already listed devi, all is
1093e499d8b5Scube 	 *      good we don't have to do more.
1094e499d8b5Scube 	 *      If it was the first alias, we have to replace i's entry in
1095e499d8b5Scube 	 *      d's list by its first alias.
1096e499d8b5Scube 	 *      If it was the only entry, we must remove i's entry from d's
1097e499d8b5Scube 	 *      list.
1098e499d8b5Scube 	 */
10997b7c582aScube 	if (i != f) {
11007b7c582aScube 		for (j = f; j->i_alias != i; j = j->i_alias);
11017b7c582aScube 		j->i_alias = i->i_alias;
11023b405775Scube 	} else {
11033b405775Scube 		if (i->i_alias == NULL) {
1104e499d8b5Scube 			/* No alias, must unlink the entry from devitab */
11057b7c582aScube 			ht_remove(devitab, i->i_name);
11067b7c582aScube 			j = i->i_bsame;
11073b405775Scube 		} else {
1108e499d8b5Scube 			/* Or have the first alias replace i in d's list */
1109e499d8b5Scube 			i->i_alias->i_bsame = i->i_bsame;
11107b7c582aScube 			j = i->i_alias;
11117b7c582aScube 			if (i == f)
11127b7c582aScube 				ht_replace(devitab, i->i_name, i->i_alias);
11133b405775Scube 		}
11143b405775Scube 
1115e499d8b5Scube 		/*
1116e499d8b5Scube 		 *   - remove/replace the instance from the devbase's list
1117e499d8b5Scube 		 *
1118e499d8b5Scube 		 * A double-linked list would make this much easier.  Oh, well,
1119e499d8b5Scube 		 * what is done is done.
1120e499d8b5Scube 		 */
1121e499d8b5Scube 		for (ppi = &d->d_ihead;
1122e499d8b5Scube 		    *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1123e499d8b5Scube 		    ppi = &(*ppi)->i_bsame);
1124e499d8b5Scube 		if (*ppi == NULL)
1125c3414672Scube 			panic("deldev: dev (%s) doesn't list the devi"
1126c3414672Scube 			    " (%s at %s)", d->d_name, i->i_name, i->i_at);
11277b7c582aScube 		f = *ppi;
11287b7c582aScube 		if (f == i)
1129c3414672Scube 			/* That implies d->d_ihead == i */
11307b7c582aScube 			*ppi = j;
1131e499d8b5Scube 		else
11327b7c582aScube 			(*ppi)->i_bsame = j;
1133e499d8b5Scube 		if (d->d_ipp == &i->i_bsame) {
11343b405775Scube 			if (i->i_alias == NULL) {
11357b7c582aScube 				if (f == i)
1136e499d8b5Scube 					d->d_ipp = &d->d_ihead;
1137e499d8b5Scube 				else
11387b7c582aScube 					d->d_ipp = &f->i_bsame;
11393b405775Scube 			} else
11403b405775Scube 				d->d_ipp = &i->i_alias->i_bsame;
1141e499d8b5Scube 		}
1142e499d8b5Scube 	}
1143e499d8b5Scube 	/*
1144e499d8b5Scube 	 *   - delete the attachment instance
1145e499d8b5Scube 	 */
1146e499d8b5Scube 	iba = i->i_atdeva;
1147e499d8b5Scube 	for (ppi = &iba->d_ihead;
1148e499d8b5Scube 	    *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1149e499d8b5Scube 	    ppi = &(*ppi)->i_asame);
1150e499d8b5Scube 	if (*ppi == NULL)
1151e499d8b5Scube 		panic("deldev: deva (%s) doesn't list the devi (%s)",
1152e499d8b5Scube 		    iba->d_name, i->i_name);
11537b7c582aScube 	f = *ppi;
11547b7c582aScube 	if (f == i)
1155c3414672Scube 		/* That implies iba->d_ihead == i */
1156e499d8b5Scube 		*ppi = i->i_asame;
1157e499d8b5Scube 	else
1158e499d8b5Scube 		(*ppi)->i_asame = i->i_asame;
1159e499d8b5Scube 	if (iba->d_ipp == &i->i_asame) {
11607b7c582aScube 		if (f == i)
1161e499d8b5Scube 			iba->d_ipp = &iba->d_ihead;
1162e499d8b5Scube 		else
11637b7c582aScube 			iba->d_ipp = &f->i_asame;
1164e499d8b5Scube 	}
1165e499d8b5Scube 	/*
1166e499d8b5Scube 	 *   - delete the pspec
1167e499d8b5Scube 	 */
1168e499d8b5Scube 	if (i->i_pspec) {
1169e499d8b5Scube 		struct pspec *p = i->i_pspec;
1170e499d8b5Scube 		struct nvlist *nv, *onv;
1171e499d8b5Scube 
1172e499d8b5Scube 		/* Double-linked nvlist anyone? */
1173e499d8b5Scube 		for (nv = p->p_devs; nv->nv_ptr != NULL; nv = nv->nv_next) {
1174e499d8b5Scube 			if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1175e499d8b5Scube 				onv = nv->nv_next;
1176e499d8b5Scube 				nv->nv_next = onv->nv_next;
1177e499d8b5Scube 				nvfree(onv);
1178e499d8b5Scube 				break;
1179878c29f5Scube 			} if (nv->nv_ptr == i) {
1180e499d8b5Scube 				/* nv is p->p_devs in that case */
1181e499d8b5Scube 				p->p_devs = nv->nv_next;
1182e499d8b5Scube 				nvfree(nv);
1183e499d8b5Scube 				break;
1184e499d8b5Scube 			}
1185e499d8b5Scube 		}
1186e499d8b5Scube 		if (p->p_devs == NULL)
1187e499d8b5Scube 			TAILQ_REMOVE(&allpspecs, p, p_list);
1188e499d8b5Scube 	}
1189e499d8b5Scube 	/*
1190e499d8b5Scube 	 *   - delete the alldevi entry
1191e499d8b5Scube 	 */
1192e499d8b5Scube 	TAILQ_REMOVE(&alldevi, i, i_next);
1193e499d8b5Scube 	ndevi--;
11947aa6070dScube 	/*
11957aa6070dScube 	 * Put it in deaddevitab
1196a31ff6b4Scube 	 *
1197a31ff6b4Scube 	 * Each time a devi is removed, devilevel is increased so that later on
1198a31ff6b4Scube 	 * it is possible to tell if an instance was added before or after the
1199a31ff6b4Scube 	 * removal of its parent.
1200a31ff6b4Scube 	 *
1201a31ff6b4Scube 	 * For active instances, i_level contains the number of devi removed so
1202a31ff6b4Scube 	 * far, and for dead devis, it contains its index.
12037aa6070dScube 	 */
1204a31ff6b4Scube 	i->i_level = devilevel++;
12057aa6070dScube 	i->i_alias = NULL;
12067aa6070dScube 	f = ht_lookup(deaddevitab, i->i_name);
12077aa6070dScube 	if (f == NULL) {
12087aa6070dScube 		if (ht_insert(deaddevitab, i->i_name, i))
12097aa6070dScube 			panic("remove_devi(%s) - can't add to deaddevitab",
12107aa6070dScube 			    i->i_name);
12117aa6070dScube 	} else {
12127aa6070dScube 		for (j = f; j->i_alias != NULL; j = j->i_alias);
12137aa6070dScube 		j->i_alias = i;
12147aa6070dScube 	}
1215e499d8b5Scube 	/*
1216e499d8b5Scube 	 *   - reconstuct d->d_umax
1217e499d8b5Scube 	 */
1218e499d8b5Scube 	d->d_umax = 0;
1219e499d8b5Scube 	for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1220e499d8b5Scube 		if (i->i_unit >= d->d_umax)
1221e499d8b5Scube 			d->d_umax = i->i_unit + 1;
12225ecc953bSthorpej }
12235ecc953bSthorpej 
12245ecc953bSthorpej void
12257b7c582aScube deldeva(const char *at)
12267b7c582aScube {
12277b7c582aScube 	int unit;
12287b7c582aScube 	const char *cp;
12297b7c582aScube 	struct devbase *d, *ad;
12307b7c582aScube 	struct devi *i, *j;
12317b7c582aScube 	struct attr *a;
12327b7c582aScube 	struct pspec *p;
12337b7c582aScube 	struct nvlist *nv, *stack = NULL;
12347b7c582aScube 
12357b7c582aScube 	if (at == NULL) {
12367b7c582aScube 		TAILQ_FOREACH(i, &alldevi, i_next)
12377b7c582aScube 			if (i->i_at == NULL)
12387b7c582aScube 				stack = newnv(NULL, NULL, i, 0, stack);
12397b7c582aScube 	} else {
12407b7c582aScube 		int l;
12417b7c582aScube 
12427b7c582aScube 		l = strlen(at) - 1;
12437b7c582aScube 		if (at[l] == '?' || isdigit((unsigned char)at[l])) {
12447b7c582aScube 			char base[NAMESIZE];
12457b7c582aScube 
12467b7c582aScube 			if (split(at, l+1, base, sizeof base, &unit)) {
12477b7c582aScube 				error("invalid attachment name `%s'", at);
12487b7c582aScube 				return;
12497b7c582aScube 			}
12507b7c582aScube 			cp = intern(base);
12517b7c582aScube 		} else {
12527b7c582aScube 			cp = intern(at);
12537b7c582aScube 			unit = STAR;
12547b7c582aScube 		}
12557b7c582aScube 
12567b7c582aScube 		ad = ht_lookup(devbasetab, cp);
12577b7c582aScube 		a = ht_lookup(attrtab, cp);
12587b7c582aScube 		if (a == NULL) {
12597b7c582aScube 			error("unknown attachment attribute or device `%s'",
12607b7c582aScube 			    cp);
12617b7c582aScube 			return;
12627b7c582aScube 		}
12637b7c582aScube 		if (!a->a_iattr) {
12647b7c582aScube 			error("plain attribute `%s' cannot have children",
12657b7c582aScube 			    a->a_name);
12667b7c582aScube 			return;
12677b7c582aScube 		}
12687b7c582aScube 
12697b7c582aScube 		/*
12707b7c582aScube 		 * remove_devi() makes changes to the devbase's list and the
12717b7c582aScube 		 * alias list, * so the actual deletion of the instances must
12727b7c582aScube 		 * be delayed.
12737b7c582aScube 		 */
12747b7c582aScube 		for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
12757b7c582aScube 			d = nv->nv_ptr;
12767b7c582aScube 			for (i = d->d_ihead; i != NULL; i = i->i_bsame)
12777b7c582aScube 				for (j = i; j != NULL; j = j->i_alias) {
12787b7c582aScube 					/* Ignore devices at root */
12797b7c582aScube 					if (j->i_at == NULL)
12807b7c582aScube 						continue;
12817b7c582aScube 					p = j->i_pspec;
12827b7c582aScube 					/*
12837b7c582aScube 					 * There are three cases:
12847b7c582aScube 					 *
12857b7c582aScube 					 * 1.  unit is not STAR.  Consider 'at'
12867b7c582aScube 					 *     to be explicit, even if it
12877b7c582aScube 					 *     references an interface
12887b7c582aScube 					 *     attribute.
12897b7c582aScube 					 *
12907b7c582aScube 					 * 2.  unit is STAR and 'at' references
12917b7c582aScube 					 *     a real device.  Look for pspec
12927b7c582aScube 					 *     that have a matching p_atdev
12937b7c582aScube 					 *     field.
12947b7c582aScube 					 *
12957b7c582aScube 					 * 3.  unit is STAR and 'at' references
12967b7c582aScube 					 *     an interface attribute.  Look
12977b7c582aScube 					 *     for pspec that have a matching
12987b7c582aScube 					 *     p_iattr field.
12997b7c582aScube 					 */
13007b7c582aScube 					if ((unit != STAR &&        /* Case */
13017b7c582aScube 					     !strcmp(j->i_at, at)) ||  /* 1 */
13027b7c582aScube 					    (unit == STAR &&
13037b7c582aScube 					     ((ad != NULL &&        /* Case */
13047b7c582aScube 					       p->p_atdev == ad) ||    /* 2 */
13057b7c582aScube 					      (ad == NULL &&        /* Case */
13067b7c582aScube 					       p->p_iattr == a))))     /* 3 */
13077b7c582aScube 						stack = newnv(NULL, NULL, j, 0,
13087b7c582aScube 						    stack);
13097b7c582aScube 				}
13107b7c582aScube 		}
13117b7c582aScube 	}
13127b7c582aScube 
13137b7c582aScube 	for (nv = stack; nv != NULL; nv = nv->nv_next)
13147b7c582aScube 		remove_devi(nv->nv_ptr);
13157b7c582aScube }
13167b7c582aScube 
13177b7c582aScube void
13187b7c582aScube deldev(const char *name)
13197b7c582aScube {
13207b7c582aScube 	int l;
13217b7c582aScube 	struct devi *firsti, *i;
13227b7c582aScube 	struct nvlist *nv, *stack = NULL;
13237b7c582aScube 
13247b7c582aScube 	l = strlen(name) - 1;
13257b7c582aScube 	if (name[l] == '*' || isdigit((unsigned char)name[l])) {
13267b7c582aScube 		/* `no mydev0' or `no mydev*' */
13277b7c582aScube 		firsti = ht_lookup(devitab, name);
13287b7c582aScube 		if (firsti == NULL) {
13297b7c582aScube 			error("unknown instance %s", name);
13307b7c582aScube 			return;
13317b7c582aScube 		}
13327b7c582aScube 		for (i = firsti; i != NULL; i = i->i_alias)
13337b7c582aScube 			stack = newnv(NULL, NULL, i, 0, stack);
13347b7c582aScube 	} else {
13357b7c582aScube 		struct devbase *d = ht_lookup(devbasetab, name);
13367b7c582aScube 
13377b7c582aScube 		if (d == NULL) {
13387b7c582aScube 			error("unknown device %s", name);
13397b7c582aScube 			return;
13407b7c582aScube 		}
13417b7c582aScube 
13427b7c582aScube 		for (firsti = d->d_ihead; firsti != NULL;
13437b7c582aScube 		    firsti = firsti->i_bsame)
13447b7c582aScube 			for (i = firsti; i != NULL; i = i->i_alias)
13457b7c582aScube 				stack = newnv(NULL, NULL, i, 0, stack);
13467b7c582aScube 	}
13477b7c582aScube 
13487b7c582aScube 	for (nv = stack; nv != NULL; nv = nv->nv_next)
13497b7c582aScube 		remove_devi(nv->nv_ptr);
13507b7c582aScube }
13517b7c582aScube 
13527b7c582aScube void
13535ecc953bSthorpej addpseudo(const char *name, int number)
13545ecc953bSthorpej {
13555ecc953bSthorpej 	struct devbase *d;
13565ecc953bSthorpej 	struct devi *i;
13575ecc953bSthorpej 
13585ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
13595ecc953bSthorpej 	if (d == NULL) {
13605ecc953bSthorpej 		error("undefined pseudo-device %s", name);
13615ecc953bSthorpej 		return;
13625ecc953bSthorpej 	}
13635ecc953bSthorpej 	if (!d->d_ispseudo) {
13645ecc953bSthorpej 		error("%s is a real device, not a pseudo-device", name);
13655ecc953bSthorpej 		return;
13665ecc953bSthorpej 	}
13675ecc953bSthorpej 	if (ht_lookup(devitab, name) != NULL) {
13685ecc953bSthorpej 		error("`%s' already defined", name);
13695ecc953bSthorpej 		return;
13705ecc953bSthorpej 	}
13715ecc953bSthorpej 	i = newdevi(name, number - 1, d);	/* foo 16 => "foo0..foo15" */
13725ecc953bSthorpej 	if (ht_insert(devitab, name, i))
13735ecc953bSthorpej 		panic("addpseudo(%s)", name);
13747b7c582aScube 	/* Useful to retrieve the instance from the devbase */
13757b7c582aScube 	d->d_ihead = i;
13767aa6070dScube 	i->i_active = DEVI_ACTIVE;
13775ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
13785ecc953bSthorpej }
13795ecc953bSthorpej 
13805ecc953bSthorpej void
13815ecc953bSthorpej delpseudo(const char *name)
13825ecc953bSthorpej {
13835ecc953bSthorpej 	struct devbase *d;
13845ecc953bSthorpej 	struct devi *i;
13855ecc953bSthorpej 
13865ecc953bSthorpej 	d = ht_lookup(devbasetab, name);
13875ecc953bSthorpej 	if (d == NULL) {
13885ecc953bSthorpej 		error("undefined pseudo-device %s", name);
13895ecc953bSthorpej 		return;
13905ecc953bSthorpej 	}
13915ecc953bSthorpej 	if (!d->d_ispseudo) {
13925ecc953bSthorpej 		error("%s is a real device, not a pseudo-device", name);
13935ecc953bSthorpej 		return;
13945ecc953bSthorpej 	}
13955ecc953bSthorpej 	if ((i = ht_lookup(devitab, name)) == NULL) {
13965ecc953bSthorpej 		error("`%s' not defined", name);
13975ecc953bSthorpej 		return;
13985ecc953bSthorpej 	}
13995ecc953bSthorpej 	d->d_umax = 0;		/* clear neads-count entries */
14007aa6070dScube 	d->d_ihead = NULL;	/* make sure it won't be considered active */
14015ecc953bSthorpej 	TAILQ_REMOVE(&allpseudo, i, i_next);
14025ecc953bSthorpej 	if (ht_remove(devitab, name))
14035ecc953bSthorpej 		panic("delpseudo(%s) - can't remove from devitab", name);
14047aa6070dScube 	if (ht_insert(deaddevitab, name, i))
14057aa6070dScube 		panic("delpseudo(%s) - can't add to deaddevitab", name);
14065ecc953bSthorpej }
14075ecc953bSthorpej 
14085ecc953bSthorpej void
14095ecc953bSthorpej adddevm(const char *name, int cmajor, int bmajor, struct nvlist *options)
14105ecc953bSthorpej {
14115ecc953bSthorpej 	struct devm *dm;
14125ecc953bSthorpej 
14135ecc953bSthorpej 	if (cmajor < -1 || cmajor >= 4096) {
14145ecc953bSthorpej 		error("character major %d is invalid", cmajor);
14155ecc953bSthorpej 		nvfreel(options);
14165ecc953bSthorpej 		return;
14175ecc953bSthorpej 	}
14185ecc953bSthorpej 
14195ecc953bSthorpej 	if (bmajor < -1 || bmajor >= 4096) {
14205ecc953bSthorpej 		error("block major %d is invalid", bmajor);
14215ecc953bSthorpej 		nvfreel(options);
14225ecc953bSthorpej 		return;
14235ecc953bSthorpej 	}
14245ecc953bSthorpej 	if (cmajor == -1 && bmajor == -1) {
14255ecc953bSthorpej 		error("both character/block majors are not specified");
14265ecc953bSthorpej 		nvfreel(options);
14275ecc953bSthorpej 		return;
14285ecc953bSthorpej 	}
14295ecc953bSthorpej 
14305ecc953bSthorpej 	dm = ecalloc(1, sizeof(*dm));
14315ecc953bSthorpej 	dm->dm_srcfile = yyfile;
14325ecc953bSthorpej 	dm->dm_srcline = currentline();
14335ecc953bSthorpej 	dm->dm_name = name;
14345ecc953bSthorpej 	dm->dm_cmajor = cmajor;
14355ecc953bSthorpej 	dm->dm_bmajor = bmajor;
14365ecc953bSthorpej 	dm->dm_opts = options;
14375ecc953bSthorpej 
14385ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
14395ecc953bSthorpej 
14405ecc953bSthorpej 	maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
14415ecc953bSthorpej 	maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
14425ecc953bSthorpej }
14435ecc953bSthorpej 
14447aa6070dScube int
14455ecc953bSthorpej fixdevis(void)
14465ecc953bSthorpej {
14475ecc953bSthorpej 	struct devi *i;
14487aa6070dScube 	int error = 0;
14495ecc953bSthorpej 
14505ecc953bSthorpej 	TAILQ_FOREACH(i, &alldevi, i_next)
14517aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
14525ecc953bSthorpej 			selectbase(i->i_base, i->i_atdeva);
14537aa6070dScube 		else if (i->i_active == DEVI_ORPHAN) {
1454c130d400Scube 			/*
1455c130d400Scube 			 * At this point, we can't have instances for which
1456c130d400Scube 			 * i_at or i_pspec are NULL.
1457c130d400Scube 			 */
14587aa6070dScube 			++error;
14590dbd1c0eScube 			xerror(i->i_srcfile, i->i_lineno,
14600dbd1c0eScube 			    "`%s at %s' is orphaned (%s `%s' found)",
1461c130d400Scube 			    i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ?
1462c130d400Scube 			    "nothing matching" : "no", i->i_at);
14637aa6070dScube 		} else if (vflag && i->i_active == DEVI_IGNORED)
14640dbd1c0eScube 			xwarn(i->i_srcfile, i->i_lineno, "ignoring explicitely"
14659ea755ebScube 			    " orphaned instance `%s at %s'", i->i_name,
14660dbd1c0eScube 			    i->i_at);
14677aa6070dScube 
14687aa6070dScube 	if (error)
14697aa6070dScube 		return error;
14705ecc953bSthorpej 
14715ecc953bSthorpej 	TAILQ_FOREACH(i, &allpseudo, i_next)
14727aa6070dScube 		if (i->i_active == DEVI_ACTIVE)
14735ecc953bSthorpej 			selectbase(i->i_base, NULL);
14747aa6070dScube 	return 0;
14755ecc953bSthorpej }
14765ecc953bSthorpej 
14775ecc953bSthorpej /*
14785ecc953bSthorpej  * Look up a parent spec, creating a new one if it does not exist.
14795ecc953bSthorpej  */
14805ecc953bSthorpej static struct pspec *
14815ecc953bSthorpej getpspec(struct attr *attr, struct devbase *ab, int atunit)
14825ecc953bSthorpej {
14835ecc953bSthorpej 	struct pspec *p;
14845ecc953bSthorpej 
14855ecc953bSthorpej 	TAILQ_FOREACH(p, &allpspecs, p_list) {
14865ecc953bSthorpej 		if (p->p_iattr == attr &&
14875ecc953bSthorpej 		    p->p_atdev == ab &&
14885ecc953bSthorpej 		    p->p_atunit == atunit)
14895ecc953bSthorpej 			return (p);
14905ecc953bSthorpej 	}
14915ecc953bSthorpej 
14925ecc953bSthorpej 	p = ecalloc(1, sizeof(*p));
14935ecc953bSthorpej 
14945ecc953bSthorpej 	p->p_iattr = attr;
14955ecc953bSthorpej 	p->p_atdev = ab;
14965ecc953bSthorpej 	p->p_atunit = atunit;
14975ecc953bSthorpej 	p->p_inst = npspecs++;
1498c130d400Scube 	p->p_active = 0;
14995ecc953bSthorpej 
15005ecc953bSthorpej 	TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
15015ecc953bSthorpej 
15025ecc953bSthorpej 	return (p);
15035ecc953bSthorpej }
15045ecc953bSthorpej 
15055ecc953bSthorpej /*
15065ecc953bSthorpej  * Define a new instance of a specific device.
15075ecc953bSthorpej  */
15085ecc953bSthorpej static struct devi *
15095ecc953bSthorpej getdevi(const char *name)
15105ecc953bSthorpej {
15115ecc953bSthorpej 	struct devi *i, *firsti;
15125ecc953bSthorpej 	struct devbase *d;
15135ecc953bSthorpej 	int unit;
15145ecc953bSthorpej 	char base[NAMESIZE];
15155ecc953bSthorpej 
15165ecc953bSthorpej 	if (split(name, strlen(name), base, sizeof base, &unit)) {
15175ecc953bSthorpej 		error("invalid device name `%s'", name);
15185ecc953bSthorpej 		return (NULL);
15195ecc953bSthorpej 	}
15205ecc953bSthorpej 	d = ht_lookup(devbasetab, intern(base));
15215ecc953bSthorpej 	if (d == NULL) {
15225ecc953bSthorpej 		error("%s: unknown device `%s'", name, base);
15235ecc953bSthorpej 		return (NULL);
15245ecc953bSthorpej 	}
15255ecc953bSthorpej 	if (d->d_ispseudo) {
15265ecc953bSthorpej 		error("%s: %s is a pseudo-device", name, base);
15275ecc953bSthorpej 		return (NULL);
15285ecc953bSthorpej 	}
15295ecc953bSthorpej 	firsti = ht_lookup(devitab, name);
15305ecc953bSthorpej 	i = newdevi(name, unit, d);
15315ecc953bSthorpej 	if (firsti == NULL) {
15325ecc953bSthorpej 		if (ht_insert(devitab, name, i))
15335ecc953bSthorpej 			panic("getdevi(%s)", name);
15345ecc953bSthorpej 		*d->d_ipp = i;
15355ecc953bSthorpej 		d->d_ipp = &i->i_bsame;
15365ecc953bSthorpej 	} else {
15375ecc953bSthorpej 		while (firsti->i_alias)
15385ecc953bSthorpej 			firsti = firsti->i_alias;
15395ecc953bSthorpej 		firsti->i_alias = i;
15405ecc953bSthorpej 	}
15415ecc953bSthorpej 	TAILQ_INSERT_TAIL(&alldevi, i, i_next);
15425ecc953bSthorpej 	ndevi++;
15435ecc953bSthorpej 	return (i);
15445ecc953bSthorpej }
15455ecc953bSthorpej 
15465ecc953bSthorpej static const char *
15475ecc953bSthorpej concat(const char *name, int c)
15485ecc953bSthorpej {
15495ecc953bSthorpej 	int len;
15505ecc953bSthorpej 	char buf[NAMESIZE];
15515ecc953bSthorpej 
15525ecc953bSthorpej 	len = strlen(name);
15535ecc953bSthorpej 	if (len + 2 > sizeof(buf)) {
15545ecc953bSthorpej 		error("device name `%s%c' too long", name, c);
15555ecc953bSthorpej 		len = sizeof(buf) - 2;
15565ecc953bSthorpej 	}
15575ecc953bSthorpej 	memmove(buf, name, len);
15585ecc953bSthorpej 	buf[len] = c;
15595ecc953bSthorpej 	buf[len + 1] = 0;
15605ecc953bSthorpej 	return (intern(buf));
15615ecc953bSthorpej }
15625ecc953bSthorpej 
15635ecc953bSthorpej const char *
15645ecc953bSthorpej starref(const char *name)
15655ecc953bSthorpej {
15665ecc953bSthorpej 
15675ecc953bSthorpej 	return (concat(name, '*'));
15685ecc953bSthorpej }
15695ecc953bSthorpej 
15705ecc953bSthorpej const char *
15715ecc953bSthorpej wildref(const char *name)
15725ecc953bSthorpej {
15735ecc953bSthorpej 
15745ecc953bSthorpej 	return (concat(name, '?'));
15755ecc953bSthorpej }
15765ecc953bSthorpej 
15775ecc953bSthorpej /*
15785ecc953bSthorpej  * Split a name like "foo0" into base name (foo) and unit number (0).
15795ecc953bSthorpej  * Return 0 on success.  To make this useful for names like "foo0a",
15805ecc953bSthorpej  * the length of the "foo0" part is one of the arguments.
15815ecc953bSthorpej  */
15825ecc953bSthorpej static int
15835ecc953bSthorpej split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
15845ecc953bSthorpej {
15855ecc953bSthorpej 	const char *cp;
15865ecc953bSthorpej 	int c, l;
15875ecc953bSthorpej 
15885ecc953bSthorpej 	l = nlen;
15895ecc953bSthorpej 	if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
15905ecc953bSthorpej 		return (1);
15915ecc953bSthorpej 	c = (u_char)name[--l];
15925ecc953bSthorpej 	if (!isdigit(c)) {
15935ecc953bSthorpej 		if (c == '*')
15945ecc953bSthorpej 			*aunit = STAR;
15955ecc953bSthorpej 		else if (c == '?')
15965ecc953bSthorpej 			*aunit = WILD;
15975ecc953bSthorpej 		else
15985ecc953bSthorpej 			return (1);
15995ecc953bSthorpej 	} else {
16005ecc953bSthorpej 		cp = &name[l];
16015ecc953bSthorpej 		while (isdigit((unsigned char)cp[-1]))
16025ecc953bSthorpej 			l--, cp--;
16035ecc953bSthorpej 		*aunit = atoi(cp);
16045ecc953bSthorpej 	}
16055ecc953bSthorpej 	memmove(base, name, l);
16065ecc953bSthorpej 	base[l] = 0;
16075ecc953bSthorpej 	return (0);
16085ecc953bSthorpej }
16095ecc953bSthorpej 
16105ecc953bSthorpej void
16115ecc953bSthorpej selectattr(struct attr *a)
16125ecc953bSthorpej {
16135ecc953bSthorpej 
16145ecc953bSthorpej 	(void)ht_insert(selecttab, a->a_name, (char *)a->a_name);
16155ecc953bSthorpej }
16165ecc953bSthorpej 
16175ecc953bSthorpej /*
16185ecc953bSthorpej  * We have an instance of the base foo, so select it and all its
16195ecc953bSthorpej  * attributes for "optional foo".
16205ecc953bSthorpej  */
16215ecc953bSthorpej static void
16225ecc953bSthorpej selectbase(struct devbase *d, struct deva *da)
16235ecc953bSthorpej {
16245ecc953bSthorpej 	struct attr *a;
16255ecc953bSthorpej 	struct nvlist *nv;
16265ecc953bSthorpej 
16275ecc953bSthorpej 	(void)ht_insert(selecttab, d->d_name, (char *)d->d_name);
16285ecc953bSthorpej 	for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
16295ecc953bSthorpej 		a = nv->nv_ptr;
16305ecc953bSthorpej 		expandattr(a, selectattr);
16315ecc953bSthorpej 	}
16325ecc953bSthorpej 	if (da != NULL) {
16335ecc953bSthorpej 		(void)ht_insert(selecttab, da->d_name, (char *)da->d_name);
16345ecc953bSthorpej 		for (nv = da->d_attrs; nv != NULL; nv = nv->nv_next) {
16355ecc953bSthorpej 			a = nv->nv_ptr;
16365ecc953bSthorpej 			expandattr(a, selectattr);
16375ecc953bSthorpej 		}
16385ecc953bSthorpej 	}
16395ecc953bSthorpej }
16405ecc953bSthorpej 
16415ecc953bSthorpej /*
16425ecc953bSthorpej  * Is the given pointer on the given list of pointers?
16435ecc953bSthorpej  */
16445ecc953bSthorpej static int
16455ecc953bSthorpej onlist(struct nvlist *nv, void *ptr)
16465ecc953bSthorpej {
16475ecc953bSthorpej 	for (; nv != NULL; nv = nv->nv_next)
16485ecc953bSthorpej 		if (nv->nv_ptr == ptr)
16495ecc953bSthorpej 			return (1);
16505ecc953bSthorpej 	return (0);
16515ecc953bSthorpej }
16525ecc953bSthorpej 
16535ecc953bSthorpej static char *
16545ecc953bSthorpej extend(char *p, const char *name)
16555ecc953bSthorpej {
16565ecc953bSthorpej 	int l;
16575ecc953bSthorpej 
16585ecc953bSthorpej 	l = strlen(name);
16595ecc953bSthorpej 	memmove(p, name, l);
16605ecc953bSthorpej 	p += l;
16615ecc953bSthorpej 	*p++ = ',';
16625ecc953bSthorpej 	*p++ = ' ';
16635ecc953bSthorpej 	return (p);
16645ecc953bSthorpej }
16655ecc953bSthorpej 
16665ecc953bSthorpej /*
16675ecc953bSthorpej  * Check that we got all required locators, and default any that are
16685ecc953bSthorpej  * given as "?" and have defaults.  Return 0 on success.
16695ecc953bSthorpej  */
16705ecc953bSthorpej static const char **
16715ecc953bSthorpej fixloc(const char *name, struct attr *attr, struct nvlist *got)
16725ecc953bSthorpej {
16735ecc953bSthorpej 	struct nvlist *m, *n;
16745ecc953bSthorpej 	int ord;
16755ecc953bSthorpej 	const char **lp;
16765ecc953bSthorpej 	int nmissing, nextra, nnodefault;
16775ecc953bSthorpej 	char *mp, *ep, *ndp;
16785ecc953bSthorpej 	char missing[1000], extra[1000], nodefault[1000];
16795ecc953bSthorpej 	static const char *nullvec[1];
16805ecc953bSthorpej 
16815ecc953bSthorpej 	/*
16825ecc953bSthorpej 	 * Look for all required locators, and number the given ones
16835ecc953bSthorpej 	 * according to the required order.  While we are numbering,
16845ecc953bSthorpej 	 * set default values for defaulted locators.
16855ecc953bSthorpej 	 */
16865ecc953bSthorpej 	if (attr->a_loclen == 0)	/* e.g., "at root" */
16875ecc953bSthorpej 		lp = nullvec;
16885ecc953bSthorpej 	else
16895ecc953bSthorpej 		lp = emalloc((attr->a_loclen + 1) * sizeof(const char *));
16905ecc953bSthorpej 	for (n = got; n != NULL; n = n->nv_next)
16915ecc953bSthorpej 		n->nv_int = -1;
16925ecc953bSthorpej 	nmissing = 0;
16935ecc953bSthorpej 	mp = missing;
16945ecc953bSthorpej 	/* yes, this is O(mn), but m and n should be small */
16955ecc953bSthorpej 	for (ord = 0, m = attr->a_locs; m != NULL; m = m->nv_next, ord++) {
16965ecc953bSthorpej 		for (n = got; n != NULL; n = n->nv_next) {
16975ecc953bSthorpej 			if (n->nv_name == m->nv_name) {
16985ecc953bSthorpej 				n->nv_int = ord;
16995ecc953bSthorpej 				break;
17005ecc953bSthorpej 			}
17015ecc953bSthorpej 		}
17025ecc953bSthorpej 		if (n == NULL && m->nv_int == 0) {
17035ecc953bSthorpej 			nmissing++;
17045ecc953bSthorpej 			mp = extend(mp, m->nv_name);
17055ecc953bSthorpej 		}
17065ecc953bSthorpej 		lp[ord] = m->nv_str;
17075ecc953bSthorpej 	}
17085ecc953bSthorpej 	if (ord != attr->a_loclen)
17095ecc953bSthorpej 		panic("fixloc");
17105ecc953bSthorpej 	lp[ord] = NULL;
17115ecc953bSthorpej 	nextra = 0;
17125ecc953bSthorpej 	ep = extra;
17135ecc953bSthorpej 	nnodefault = 0;
17145ecc953bSthorpej 	ndp = nodefault;
17155ecc953bSthorpej 	for (n = got; n != NULL; n = n->nv_next) {
17165ecc953bSthorpej 		if (n->nv_int >= 0) {
17175ecc953bSthorpej 			if (n->nv_str != NULL)
17185ecc953bSthorpej 				lp[n->nv_int] = n->nv_str;
17195ecc953bSthorpej 			else if (lp[n->nv_int] == NULL) {
17205ecc953bSthorpej 				nnodefault++;
17215ecc953bSthorpej 				ndp = extend(ndp, n->nv_name);
17225ecc953bSthorpej 			}
17235ecc953bSthorpej 		} else {
17245ecc953bSthorpej 			nextra++;
17255ecc953bSthorpej 			ep = extend(ep, n->nv_name);
17265ecc953bSthorpej 		}
17275ecc953bSthorpej 	}
17285ecc953bSthorpej 	if (nextra) {
17295ecc953bSthorpej 		ep[-2] = 0;	/* kill ", " */
17305ecc953bSthorpej 		error("%s: extraneous locator%s: %s",
17315ecc953bSthorpej 		    name, nextra > 1 ? "s" : "", extra);
17325ecc953bSthorpej 	}
17335ecc953bSthorpej 	if (nmissing) {
17345ecc953bSthorpej 		mp[-2] = 0;
17355ecc953bSthorpej 		error("%s: must specify %s", name, missing);
17365ecc953bSthorpej 	}
17375ecc953bSthorpej 	if (nnodefault) {
17385ecc953bSthorpej 		ndp[-2] = 0;
17395ecc953bSthorpej 		error("%s: cannot wildcard %s", name, nodefault);
17405ecc953bSthorpej 	}
17415ecc953bSthorpej 	if (nmissing || nnodefault) {
17425ecc953bSthorpej 		free(lp);
17435ecc953bSthorpej 		lp = NULL;
17445ecc953bSthorpej 	}
17455ecc953bSthorpej 	return (lp);
17465ecc953bSthorpej }
1747*437f8925Scube 
1748*437f8925Scube void
1749*437f8925Scube setversion(int newver)
1750*437f8925Scube {
1751*437f8925Scube 	if (newver > CONFIG_VERSION)
1752*437f8925Scube 		error("your sources require a newer version of config(1) "
1753*437f8925Scube 		    "-- please rebuild it.");
1754*437f8925Scube 	else if (newver < CONFIG_MINVERSION)
1755*437f8925Scube 		error("your sources are out of date -- please update.");
1756*437f8925Scube 	else
1757*437f8925Scube 		version = newver;
1758*437f8925Scube }
1759