Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.bin / config / sem.c
blob4551f071e689548df2351e7b0350b881588539bf
1 /* $NetBSD: sem.c,v 1.32 2009/01/20 18:20:48 drochner Exp $ */
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.
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.
40 * from: @(#)sem.c 8.1 (Berkeley) 6/6/93
43 #if HAVE_NBTOOL_CONFIG_H
44 #include "nbtool_config.h"
45 #endif
47 #include <sys/param.h>
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <util.h>
53 #include "defs.h"
54 #include "sem.h"
57 * config semantics.
60 #define NAMESIZE 100 /* local name buffers */
62 const char *s_ifnet; /* magic attribute */
63 const char *s_qmark;
64 const char *s_none;
66 static struct hashtab *cfhashtab; /* for config lookup */
67 struct hashtab *devitab; /* etc */
69 static struct attr errattr;
70 static struct devbase errdev;
71 static struct deva errdeva;
73 static int has_errobj(struct nvlist *, void *);
74 static struct nvlist *addtoattr(struct nvlist *, struct devbase *);
75 static int resolve(struct nvlist **, const char *, const char *,
76 struct nvlist *, int);
77 static struct pspec *getpspec(struct attr *, struct devbase *, int);
78 static struct devi *newdevi(const char *, int, struct devbase *d);
79 static struct devi *getdevi(const char *);
80 static void remove_devi(struct devi *);
81 static const char *concat(const char *, int);
82 static char *extend(char *, const char *);
83 static int split(const char *, size_t, char *, size_t, int *);
84 static void selectbase(struct devbase *, struct deva *);
85 static const char **fixloc(const char *, struct attr *, struct nvlist *);
86 static const char *makedevstr(devmajor_t, devminor_t);
87 static const char *major2name(devmajor_t);
88 static devmajor_t dev2major(struct devbase *);
90 extern const char *yyfile;
91 extern int vflag;
93 void
94 initsem(void)
97 attrtab = ht_new();
98 errattr.a_name = "<internal>";
100 TAILQ_INIT(&allbases);
102 TAILQ_INIT(&alldevas);
104 TAILQ_INIT(&allpspecs);
106 cfhashtab = ht_new();
107 TAILQ_INIT(&allcf);
109 TAILQ_INIT(&alldevi);
110 errdev.d_name = "<internal>";
112 TAILQ_INIT(&allpseudo);
114 TAILQ_INIT(&alldevms);
116 s_ifnet = intern("ifnet");
117 s_qmark = intern("?");
118 s_none = intern("none");
121 /* Name of include file just ended (set in scan.l) */
122 extern const char *lastfile;
124 void
125 enddefs(void)
127 struct devbase *dev;
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;
138 if (errors) {
139 (void)fprintf(stderr, "*** Stop.\n");
140 exit(1);
144 void
145 setdefmaxusers(int min, int def, int max)
148 if (min < 1 || min > def || def > max)
149 cfgerror("maxusers must have 1 <= min (%d) <= default (%d) "
150 "<= max (%d)", min, def, max);
151 else {
152 minmaxusers = min;
153 defmaxusers = def;
154 maxmaxusers = max;
158 void
159 setmaxusers(int n)
162 if (maxusers == n) {
163 cfgerror("duplicate maxusers parameter");
164 return;
166 if (vflag && maxusers != 0)
167 cfgwarn("warning: maxusers already defined");
168 maxusers = n;
169 if (n < minmaxusers) {
170 cfgerror("warning: minimum of %d maxusers assumed",
171 minmaxusers);
172 errors--; /* take it away */
173 maxusers = minmaxusers;
174 } else if (n > maxmaxusers) {
175 cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers);
176 errors--;
180 void
181 setident(const char *i)
184 ident = intern(i);
188 * Define an attribute, optionally with an interface (a locator list)
189 * and a set of attribute-dependencies.
191 * Attribute dependencies MAY NOT be interface attributes.
193 * Since an empty locator list is logically different from "no interface",
194 * all locator lists include a dummy head node, which we discard here.
197 defattr(const char *name, struct nvlist *locs, struct nvlist *deps,
198 int devclass)
200 struct attr *a, *dep;
201 struct nvlist *nv;
202 int len;
204 if (locs != NULL && devclass)
205 panic("defattr(%s): locators and devclass", name);
207 if (deps != NULL && devclass)
208 panic("defattr(%s): dependencies and devclass", name);
211 * If this attribute depends on any others, make sure none of
212 * the dependencies are interface attributes.
214 for (nv = deps; nv != NULL; nv = nv->nv_next) {
215 dep = nv->nv_ptr;
216 if (dep->a_iattr) {
217 cfgerror("`%s' dependency `%s' is an interface "
218 "attribute", name, dep->a_name);
219 return (1);
223 a = ecalloc(1, sizeof *a);
224 if (ht_insert(attrtab, name, a)) {
225 free(a);
226 cfgerror("attribute `%s' already defined", name);
227 nvfreel(locs);
228 return (1);
231 a->a_name = name;
232 if (locs != NULL) {
233 a->a_iattr = 1;
234 a->a_locs = locs->nv_next;
235 nvfree(locs);
236 } else {
237 a->a_iattr = 0;
238 a->a_locs = NULL;
240 if (devclass) {
241 char classenum[256], *cp;
242 int errored = 0;
244 (void)snprintf(classenum, sizeof(classenum), "DV_%s", name);
245 for (cp = classenum + 3; *cp; cp++) {
246 if (!errored &&
247 (!isalnum((unsigned char)*cp) ||
248 (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) {
249 cfgerror("device class names must be "
250 "lower-case alphanumeric characters");
251 errored = 1;
253 *cp = toupper((unsigned char)*cp);
255 a->a_devclass = intern(classenum);
256 } else
257 a->a_devclass = NULL;
258 len = 0;
259 for (nv = a->a_locs; nv != NULL; nv = nv->nv_next)
260 len++;
261 a->a_loclen = len;
262 a->a_devs = NULL;
263 a->a_refs = NULL;
264 a->a_deps = deps;
265 a->a_expanding = 0;
267 /* Expand the attribute to check for cycles in the graph. */
268 expandattr(a, NULL);
270 return (0);
274 * Return true if the given `error object' is embedded in the given
275 * pointer list.
277 static int
278 has_errobj(struct nvlist *nv, void *obj)
281 for (; nv != NULL; nv = nv->nv_next)
282 if (nv->nv_ptr == obj)
283 return (1);
284 return (0);
288 * Return true if the given attribute is embedded in the given
289 * pointer list.
292 has_attr(struct nvlist *nv, const char *attr)
294 struct attr *a;
296 if ((a = getattr(attr)) == NULL)
297 return (0);
299 for (; nv != NULL; nv = nv->nv_next)
300 if (nv->nv_ptr == a)
301 return (1);
302 return (0);
306 * Add a device base to a list in an attribute (actually, to any list).
307 * Note that this does not check for duplicates, and does reverse the
308 * list order, but no one cares anyway.
310 static struct nvlist *
311 addtoattr(struct nvlist *l, struct devbase *dev)
313 struct nvlist *n;
315 n = newnv(NULL, NULL, dev, 0, l);
316 return (n);
320 * Define a device. This may (or may not) also define an interface
321 * attribute and/or refer to existing attributes.
323 void
324 defdev(struct devbase *dev, struct nvlist *loclist, struct nvlist *attrs,
325 int ispseudo)
327 struct nvlist *nv;
328 struct attr *a;
330 if (dev == &errdev)
331 goto bad;
332 if (dev->d_isdef) {
333 cfgerror("redefinition of `%s'", dev->d_name);
334 goto bad;
337 dev->d_isdef = 1;
338 if (has_errobj(attrs, &errattr))
339 goto bad;
342 * Handle implicit attribute definition from locator list. Do
343 * this before scanning the `at' list so that we can have, e.g.:
344 * device foo at other, foo { slot = -1 }
345 * (where you can plug in a foo-bus extender to a foo-bus).
347 if (loclist != NULL) {
348 nv = loclist;
349 loclist = NULL; /* defattr disposes of them for us */
350 if (defattr(dev->d_name, nv, NULL, 0))
351 goto bad;
352 attrs = newnv(dev->d_name, NULL, getattr(dev->d_name), 0,
353 attrs);
358 * Pseudo-devices can have children. Consider them as
359 * attaching at root.
361 if (ispseudo) {
362 for (nv = attrs; nv != NULL; nv = nv->nv_next)
363 if (((struct attr *)(nv->nv_ptr))->a_iattr)
364 break;
365 if (nv != NULL) {
366 if (ispseudo < 2) {
367 if (version >= 20080610)
368 cfgerror("interface attribute on "
369 "non-device pseudo `%s'", dev->d_name);
370 else {
371 ispseudo = 2;
374 ht_insert(devroottab, dev->d_name, dev);
378 /* Committed! Set up fields. */
379 dev->d_ispseudo = ispseudo;
380 dev->d_attrs = attrs;
381 dev->d_classattr = NULL; /* for now */
384 * For each interface attribute this device refers to, add this
385 * device to its reference list. This makes, e.g., finding all
386 * "scsi"s easier.
388 * While looking through the attributes, set up the device
389 * class if any are devclass attributes (and error out if the
390 * device has two classes).
392 for (nv = attrs; nv != NULL; nv = nv->nv_next) {
393 a = nv->nv_ptr;
394 if (a->a_iattr)
395 a->a_refs = addtoattr(a->a_refs, dev);
396 if (a->a_devclass != NULL) {
397 if (dev->d_classattr != NULL) {
398 cfgerror("device `%s' has multiple classes "
399 "(`%s' and `%s')",
400 dev->d_name, dev->d_classattr->a_name,
401 a->a_name);
403 dev->d_classattr = a;
406 return;
407 bad:
408 nvfreel(loclist);
409 nvfreel(attrs);
413 * Look up a devbase. Also makes sure it is a reasonable name,
414 * i.e., does not end in a digit or contain special characters.
416 struct devbase *
417 getdevbase(const char *name)
419 const u_char *p;
420 struct devbase *dev;
422 p = (const u_char *)name;
423 if (!isalpha(*p))
424 goto badname;
425 while (*++p) {
426 if (!isalnum(*p) && *p != '_')
427 goto badname;
429 if (isdigit(*--p)) {
430 badname:
431 cfgerror("bad device base name `%s'", name);
432 return (&errdev);
434 dev = ht_lookup(devbasetab, name);
435 if (dev == NULL) {
436 dev = ecalloc(1, sizeof *dev);
437 dev->d_name = name;
438 dev->d_isdef = 0;
439 dev->d_major = NODEVMAJOR;
440 dev->d_attrs = NULL;
441 dev->d_ihead = NULL;
442 dev->d_ipp = &dev->d_ihead;
443 dev->d_ahead = NULL;
444 dev->d_app = &dev->d_ahead;
445 dev->d_umax = 0;
446 TAILQ_INSERT_TAIL(&allbases, dev, d_next);
447 if (ht_insert(devbasetab, name, dev))
448 panic("getdevbase(%s)", name);
450 return (dev);
454 * Define some of a device's allowable parent attachments.
455 * There may be a list of (plain) attributes.
457 void
458 defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist,
459 struct nvlist *attrs)
461 struct nvlist *nv;
462 struct attr *a;
463 struct deva *da;
465 if (dev == &errdev)
466 goto bad;
467 if (deva == NULL)
468 deva = getdevattach(dev->d_name);
469 if (deva == &errdeva)
470 goto bad;
471 if (!dev->d_isdef) {
472 cfgerror("attaching undefined device `%s'", dev->d_name);
473 goto bad;
475 if (deva->d_isdef) {
476 cfgerror("redefinition of `%s'", deva->d_name);
477 goto bad;
479 if (dev->d_ispseudo) {
480 cfgerror("pseudo-devices can't attach");
481 goto bad;
484 deva->d_isdef = 1;
485 if (has_errobj(attrs, &errattr))
486 goto bad;
487 for (nv = attrs; nv != NULL; nv = nv->nv_next) {
488 a = nv->nv_ptr;
489 if (a == &errattr)
490 continue; /* already complained */
491 if (a->a_iattr || a->a_devclass != NULL)
492 cfgerror("`%s' is not a plain attribute", a->a_name);
495 /* Committed! Set up fields. */
496 deva->d_attrs = attrs;
497 deva->d_atlist = atlist;
498 deva->d_devbase = dev;
501 * Turn the `at' list into interface attributes (map each
502 * nv_name to an attribute, or to NULL for root), and add
503 * this device to those attributes, so that children can
504 * be listed at this particular device if they are supported
505 * by that attribute.
507 for (nv = atlist; nv != NULL; nv = nv->nv_next) {
508 if (nv->nv_name == NULL)
509 nv->nv_ptr = a = NULL; /* at root */
510 else
511 nv->nv_ptr = a = getattr(nv->nv_name);
512 if (a == &errattr)
513 continue; /* already complained */
516 * Make sure that an attachment spec doesn't
517 * already say how to attach to this attribute.
519 for (da = dev->d_ahead; da != NULL; da = da->d_bsame)
520 if (onlist(da->d_atlist, a))
521 cfgerror("attach at `%s' already done by `%s'",
522 a ? a->a_name : "root", da->d_name);
524 if (a == NULL) {
525 ht_insert(devroottab, dev->d_name, dev);
526 continue; /* at root; don't add */
528 if (!a->a_iattr)
529 cfgerror("%s cannot be at plain attribute `%s'",
530 dev->d_name, a->a_name);
531 else
532 a->a_devs = addtoattr(a->a_devs, dev);
535 /* attach to parent */
536 *dev->d_app = deva;
537 dev->d_app = &deva->d_bsame;
538 return;
539 bad:
540 nvfreel(atlist);
541 nvfreel(attrs);
545 * Look up a device attachment. Also makes sure it is a reasonable
546 * name, i.e., does not contain digits or special characters.
548 struct deva *
549 getdevattach(const char *name)
551 const u_char *p;
552 struct deva *deva;
554 p = (const u_char *)name;
555 if (!isalpha(*p))
556 goto badname;
557 while (*++p) {
558 if (!isalnum(*p) && *p != '_')
559 goto badname;
561 if (isdigit(*--p)) {
562 badname:
563 cfgerror("bad device attachment name `%s'", name);
564 return (&errdeva);
566 deva = ht_lookup(devatab, name);
567 if (deva == NULL) {
568 deva = ecalloc(1, sizeof *deva);
569 deva->d_name = name;
570 deva->d_bsame = NULL;
571 deva->d_isdef = 0;
572 deva->d_devbase = NULL;
573 deva->d_atlist = NULL;
574 deva->d_attrs = NULL;
575 deva->d_ihead = NULL;
576 deva->d_ipp = &deva->d_ihead;
577 TAILQ_INSERT_TAIL(&alldevas, deva, d_next);
578 if (ht_insert(devatab, name, deva))
579 panic("getdeva(%s)", name);
581 return (deva);
585 * Look up an attribute.
587 struct attr *
588 getattr(const char *name)
590 struct attr *a;
592 if ((a = ht_lookup(attrtab, name)) == NULL) {
593 cfgerror("undefined attribute `%s'", name);
594 a = &errattr;
596 return (a);
600 * Recursively expand an attribute and its dependencies, checking for
601 * cycles, and invoking a callback for each attribute found.
603 void
604 expandattr(struct attr *a, void (*callback)(struct attr *))
606 struct nvlist *nv;
607 struct attr *dep;
609 if (a->a_expanding) {
610 cfgerror("circular dependency on attribute `%s'", a->a_name);
611 return;
614 a->a_expanding = 1;
616 /* First expand all of this attribute's dependencies. */
617 for (nv = a->a_deps; nv != NULL; nv = nv->nv_next) {
618 dep = nv->nv_ptr;
619 expandattr(dep, callback);
622 /* ...and now invoke the callback for ourself. */
623 if (callback != NULL)
624 (*callback)(a);
626 a->a_expanding = 0;
630 * Set the major device number for a device, so that it can be used
631 * as a root/dumps "on" device in a configuration.
633 void
634 setmajor(struct devbase *d, devmajor_t n)
637 if (d != &errdev && d->d_major != NODEVMAJOR)
638 cfgerror("device `%s' is already major %d",
639 d->d_name, d->d_major);
640 else
641 d->d_major = n;
644 const char *
645 major2name(devmajor_t maj)
647 struct devbase *dev;
648 struct devm *dm;
650 if (!do_devsw) {
651 TAILQ_FOREACH(dev, &allbases, d_next) {
652 if (dev->d_major == maj)
653 return (dev->d_name);
655 } else {
656 TAILQ_FOREACH(dm, &alldevms, dm_next) {
657 if (dm->dm_bmajor == maj)
658 return (dm->dm_name);
661 return (NULL);
664 devmajor_t
665 dev2major(struct devbase *dev)
667 struct devm *dm;
669 if (!do_devsw)
670 return (dev->d_major);
672 TAILQ_FOREACH(dm, &alldevms, dm_next) {
673 if (strcmp(dm->dm_name, dev->d_name) == 0)
674 return (dm->dm_bmajor);
676 return (NODEVMAJOR);
680 * Make a string description of the device at maj/min.
682 static const char *
683 makedevstr(devmajor_t maj, devminor_t min)
685 const char *devicename;
686 char buf[32];
688 devicename = major2name(maj);
689 if (devicename == NULL)
690 (void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min);
691 else
692 (void)snprintf(buf, sizeof(buf), "%s%d%c", devicename,
693 min / maxpartitions, (min % maxpartitions) + 'a');
695 return (intern(buf));
699 * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a').
700 * Handle the case where the device number is given but there is no
701 * corresponding name, and map NULL to the default.
703 static int
704 resolve(struct nvlist **nvp, const char *name, const char *what,
705 struct nvlist *dflt, int part)
707 struct nvlist *nv;
708 struct devbase *dev;
709 const char *cp;
710 devmajor_t maj;
711 devminor_t min;
712 int i, l;
713 int unit;
714 char buf[NAMESIZE];
716 if ((part -= 'a') >= maxpartitions || part < 0)
717 panic("resolve");
718 if ((nv = *nvp) == NULL) {
719 dev_t d = NODEV;
721 * Apply default. Easiest to do this by number.
722 * Make sure to retain NODEVness, if this is dflt's disposition.
724 if ((dev_t)dflt->nv_num != NODEV) {
725 maj = major(dflt->nv_num);
726 min = ((minor(dflt->nv_num) / maxpartitions) *
727 maxpartitions) + part;
728 d = makedev(maj, min);
729 cp = makedevstr(maj, min);
730 } else
731 cp = NULL;
732 *nvp = nv = newnv(NULL, cp, NULL, d, NULL);
734 if ((dev_t)nv->nv_num != NODEV) {
736 * By the numbers. Find the appropriate major number
737 * to make a name.
739 maj = major(nv->nv_num);
740 min = minor(nv->nv_num);
741 nv->nv_str = makedevstr(maj, min);
742 return (0);
745 if (nv->nv_str == NULL || nv->nv_str == s_qmark)
747 * Wildcarded or unspecified; leave it as NODEV.
749 return (0);
752 * The normal case: things like "ra2b". Check for partition
753 * suffix, remove it if there, and split into name ("ra") and
754 * unit (2).
756 l = i = strlen(nv->nv_str);
757 cp = &nv->nv_str[l];
758 if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions &&
759 isdigit((unsigned char)cp[-1])) {
760 l--;
761 part = *cp - 'a';
763 cp = nv->nv_str;
764 if (split(cp, l, buf, sizeof buf, &unit)) {
765 cfgerror("%s: invalid %s device name `%s'", name, what, cp);
766 return (1);
768 dev = ht_lookup(devbasetab, intern(buf));
769 if (dev == NULL) {
770 cfgerror("%s: device `%s' does not exist", name, buf);
771 return (1);
775 * Check for the magic network interface attribute, and
776 * don't bother making a device number.
778 if (has_attr(dev->d_attrs, s_ifnet)) {
779 nv->nv_num = NODEV;
780 nv->nv_ifunit = unit; /* XXX XXX XXX */
781 } else {
782 maj = dev2major(dev);
783 if (maj == NODEVMAJOR) {
784 cfgerror("%s: can't make %s device from `%s'",
785 name, what, nv->nv_str);
786 return (1);
788 nv->nv_num = makedev(maj, unit * maxpartitions + part);
791 nv->nv_name = dev->d_name;
792 return (0);
796 * Add a completed configuration to the list.
798 void
799 addconf(struct config *cf0)
801 struct config *cf;
802 const char *name;
804 name = cf0->cf_name;
805 cf = ecalloc(1, sizeof *cf);
806 if (ht_insert(cfhashtab, name, cf)) {
807 cfgerror("configuration `%s' already defined", name);
808 free(cf);
809 goto bad;
811 *cf = *cf0;
814 * Resolve the root device.
816 if (cf->cf_root == NULL) {
817 cfgerror("%s: no root device specified", name);
818 goto bad;
820 if (cf->cf_root && cf->cf_root->nv_str != s_qmark) {
821 struct nvlist *nv;
822 nv = cf->cf_root;
823 if (resolve(&cf->cf_root, name, "root", nv, 'a'))
824 goto bad;
828 * Resolve the dump device.
830 if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) {
832 * Wildcarded dump device is equivalent to unspecified.
834 cf->cf_dump = NULL;
835 } else if (cf->cf_dump->nv_str == s_none) {
837 * Operator has requested that no dump device should be
838 * configured; do nothing.
840 } else {
841 if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b'))
842 goto bad;
845 /* Wildcarded fstype is `unspecified'. */
846 if (cf->cf_fstype == s_qmark)
847 cf->cf_fstype = NULL;
849 TAILQ_INSERT_TAIL(&allcf, cf, cf_next);
850 return;
851 bad:
852 nvfreel(cf0->cf_root);
853 nvfreel(cf0->cf_dump);
856 void
857 setconf(struct nvlist **npp, const char *what, struct nvlist *v)
860 if (*npp != NULL) {
861 cfgerror("duplicate %s specification", what);
862 nvfreel(v);
863 } else
864 *npp = v;
867 void
868 delconf(const char *name)
870 struct config *cf;
872 if (ht_lookup(cfhashtab, name) == NULL) {
873 cfgerror("configuration `%s' undefined", name);
874 return;
876 (void)ht_remove(cfhashtab, name);
878 TAILQ_FOREACH(cf, &allcf, cf_next)
879 if (!strcmp(cf->cf_name, name))
880 break;
881 if (cf == NULL)
882 panic("lost configuration `%s'", name);
884 TAILQ_REMOVE(&allcf, cf, cf_next);
887 void
888 setfstype(const char **fstp, const char *v)
891 if (*fstp != NULL) {
892 cfgerror("multiple fstype specifications");
893 return;
896 if (v != s_qmark && OPT_FSOPT(v)) {
897 cfgerror("\"%s\" is not a configured file system", v);
898 return;
901 *fstp = v;
904 static struct devi *
905 newdevi(const char *name, int unit, struct devbase *d)
907 struct devi *i;
909 i = ecalloc(1, sizeof *i);
910 i->i_name = name;
911 i->i_unit = unit;
912 i->i_base = d;
913 i->i_bsame = NULL;
914 i->i_asame = NULL;
915 i->i_alias = NULL;
916 i->i_at = NULL;
917 i->i_pspec = NULL;
918 i->i_atdeva = NULL;
919 i->i_locs = NULL;
920 i->i_cfflags = 0;
921 i->i_lineno = currentline();
922 i->i_srcfile = yyfile;
923 i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */
924 i->i_level = devilevel;
925 if (unit >= d->d_umax)
926 d->d_umax = unit + 1;
927 return (i);
931 * Add the named device as attaching to the named attribute (or perhaps
932 * another device instead) plus unit number.
934 void
935 adddev(const char *name, const char *at, struct nvlist *loclist, int flags)
937 struct devi *i; /* the new instance */
938 struct pspec *p; /* and its pspec */
939 struct attr *attr; /* attribute that allows attach */
940 struct devbase *ib; /* i->i_base */
941 struct devbase *ab; /* not NULL => at another dev */
942 struct nvlist *nv;
943 struct deva *iba; /* devbase attachment used */
944 const char *cp;
945 int atunit;
946 char atbuf[NAMESIZE];
947 int hit;
949 ab = NULL;
950 iba = NULL;
951 if (at == NULL) {
952 /* "at root" */
953 p = NULL;
954 if ((i = getdevi(name)) == NULL)
955 goto bad;
957 * Must warn about i_unit > 0 later, after taking care of
958 * the STAR cases (we could do non-star's here but why
959 * bother?). Make sure this device can be at root.
961 ib = i->i_base;
962 hit = 0;
963 for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
964 if (onlist(iba->d_atlist, NULL)) {
965 hit = 1;
966 break;
968 if (!hit) {
969 cfgerror("`%s' cannot attach to the root", ib->d_name);
970 i->i_active = DEVI_BROKEN;
971 goto bad;
973 attr = &errattr; /* a convenient "empty" attr */
974 } else {
975 if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) {
976 cfgerror("invalid attachment name `%s'", at);
977 /* (void)getdevi(name); -- ??? */
978 goto bad;
980 if ((i = getdevi(name)) == NULL)
981 goto bad;
982 ib = i->i_base;
985 * Devices can attach to two types of things: Attributes,
986 * and other devices (which have the appropriate attributes
987 * to allow attachment).
989 * (1) If we're attached to an attribute, then we don't need
990 * look at the parent base device to see what attributes
991 * it has, and make sure that we can attach to them.
993 * (2) If we're attached to a real device (i.e. named in
994 * the config file), we want to remember that so that
995 * at cross-check time, if the device we're attached to
996 * is missing but other devices which also provide the
997 * attribute are present, we don't get a false "OK."
999 * (3) If the thing we're attached to is an attribute
1000 * but is actually named in the config file, we still
1001 * have to remember its devbase.
1003 cp = intern(atbuf);
1005 /* Figure out parent's devbase, to satisfy case (3). */
1006 ab = ht_lookup(devbasetab, cp);
1008 /* Find out if it's an attribute. */
1009 attr = ht_lookup(attrtab, cp);
1011 /* Make sure we're _really_ attached to the attr. Case (1). */
1012 if (attr != NULL && onlist(attr->a_devs, ib))
1013 goto findattachment;
1016 * Else a real device, and not just an attribute. Case (2).
1018 * Have to work a bit harder to see whether we have
1019 * something like "tg0 at esp0" (where esp is merely
1020 * not an attribute) or "tg0 at nonesuch0" (where
1021 * nonesuch is not even a device).
1023 if (ab == NULL) {
1024 cfgerror("%s at %s: `%s' unknown",
1025 name, at, atbuf);
1026 i->i_active = DEVI_BROKEN;
1027 goto bad;
1031 * See if the named parent carries an attribute
1032 * that allows it to supervise device ib.
1034 for (nv = ab->d_attrs; nv != NULL; nv = nv->nv_next) {
1035 attr = nv->nv_ptr;
1036 if (onlist(attr->a_devs, ib))
1037 goto findattachment;
1039 cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf);
1040 i->i_active = DEVI_BROKEN;
1041 goto bad;
1043 findattachment:
1045 * Find the parent spec. If a matching one has not yet been
1046 * created, create one.
1048 p = getpspec(attr, ab, atunit);
1049 p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs);
1051 /* find out which attachment it uses */
1052 hit = 0;
1053 for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame)
1054 if (onlist(iba->d_atlist, attr)) {
1055 hit = 1;
1056 break;
1058 if (!hit)
1059 panic("adddev: can't figure out attachment");
1061 if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) {
1062 i->i_active = DEVI_BROKEN;
1063 goto bad;
1065 i->i_at = at;
1066 i->i_pspec = p;
1067 i->i_atdeva = iba;
1068 i->i_cfflags = flags;
1070 *iba->d_ipp = i;
1071 iba->d_ipp = &i->i_asame;
1073 /* all done, fall into ... */
1074 bad:
1075 nvfreel(loclist);
1076 return;
1079 void
1080 deldevi(const char *name, const char *at)
1082 struct devi *firsti, *i;
1083 struct devbase *d;
1084 int unit;
1085 char base[NAMESIZE];
1087 if (split(name, strlen(name), base, sizeof base, &unit)) {
1088 cfgerror("invalid device name `%s'", name);
1089 return;
1091 d = ht_lookup(devbasetab, intern(base));
1092 if (d == NULL) {
1093 cfgerror("%s: unknown device `%s'", name, base);
1094 return;
1096 if (d->d_ispseudo) {
1097 cfgerror("%s: %s is a pseudo-device", name, base);
1098 return;
1100 if ((firsti = ht_lookup(devitab, name)) == NULL) {
1101 cfgerror("`%s' not defined", name);
1102 return;
1104 if (at == NULL && firsti->i_at == NULL) {
1105 /* 'at root' */
1106 remove_devi(firsti);
1107 return;
1108 } else if (at != NULL)
1109 for (i = firsti; i != NULL; i = i->i_alias)
1110 if (i->i_active != DEVI_BROKEN &&
1111 strcmp(at, i->i_at) == 0) {
1112 remove_devi(i);
1113 return;
1115 cfgerror("`%s' at `%s' not found", name, at ? at : "root");
1118 static void
1119 remove_devi(struct devi *i)
1121 struct devbase *d = i->i_base;
1122 struct devi *f, *j, **ppi;
1123 struct deva *iba;
1125 f = ht_lookup(devitab, i->i_name);
1126 if (f == NULL)
1127 panic("remove_devi(): instance %s disappeared from devitab",
1128 i->i_name);
1130 if (i->i_active == DEVI_BROKEN) {
1131 cfgerror("not removing broken instance `%s'", i->i_name);
1132 return;
1136 * We have the device instance, i.
1137 * We have to:
1138 * - delete the alias
1140 * If the devi was an alias of an already listed devi, all is
1141 * good we don't have to do more.
1142 * If it was the first alias, we have to replace i's entry in
1143 * d's list by its first alias.
1144 * If it was the only entry, we must remove i's entry from d's
1145 * list.
1147 if (i != f) {
1148 for (j = f; j->i_alias != i; j = j->i_alias);
1149 j->i_alias = i->i_alias;
1150 } else {
1151 if (i->i_alias == NULL) {
1152 /* No alias, must unlink the entry from devitab */
1153 ht_remove(devitab, i->i_name);
1154 j = i->i_bsame;
1155 } else {
1156 /* Or have the first alias replace i in d's list */
1157 i->i_alias->i_bsame = i->i_bsame;
1158 j = i->i_alias;
1159 if (i == f)
1160 ht_replace(devitab, i->i_name, i->i_alias);
1164 * - remove/replace the instance from the devbase's list
1166 * A double-linked list would make this much easier. Oh, well,
1167 * what is done is done.
1169 for (ppi = &d->d_ihead;
1170 *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i;
1171 ppi = &(*ppi)->i_bsame);
1172 if (*ppi == NULL)
1173 panic("deldev: dev (%s) doesn't list the devi"
1174 " (%s at %s)", d->d_name, i->i_name, i->i_at);
1175 f = *ppi;
1176 if (f == i)
1177 /* That implies d->d_ihead == i */
1178 *ppi = j;
1179 else
1180 (*ppi)->i_bsame = j;
1181 if (d->d_ipp == &i->i_bsame) {
1182 if (i->i_alias == NULL) {
1183 if (f == i)
1184 d->d_ipp = &d->d_ihead;
1185 else
1186 d->d_ipp = &f->i_bsame;
1187 } else
1188 d->d_ipp = &i->i_alias->i_bsame;
1192 * - delete the attachment instance
1194 iba = i->i_atdeva;
1195 for (ppi = &iba->d_ihead;
1196 *ppi != NULL && *ppi != i && (*ppi)->i_asame != i;
1197 ppi = &(*ppi)->i_asame);
1198 if (*ppi == NULL)
1199 panic("deldev: deva (%s) doesn't list the devi (%s)",
1200 iba->d_name, i->i_name);
1201 f = *ppi;
1202 if (f == i)
1203 /* That implies iba->d_ihead == i */
1204 *ppi = i->i_asame;
1205 else
1206 (*ppi)->i_asame = i->i_asame;
1207 if (iba->d_ipp == &i->i_asame) {
1208 if (f == i)
1209 iba->d_ipp = &iba->d_ihead;
1210 else
1211 iba->d_ipp = &f->i_asame;
1214 * - delete the pspec
1216 if (i->i_pspec) {
1217 struct pspec *p = i->i_pspec;
1218 struct nvlist *nv, *onv;
1220 /* Double-linked nvlist anyone? */
1221 for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) {
1222 if (nv->nv_next && nv->nv_next->nv_ptr == i) {
1223 onv = nv->nv_next;
1224 nv->nv_next = onv->nv_next;
1225 nvfree(onv);
1226 break;
1228 if (nv->nv_ptr == i) {
1229 /* nv is p->p_devs in that case */
1230 p->p_devs = nv->nv_next;
1231 nvfree(nv);
1232 break;
1235 if (p->p_devs == NULL)
1236 TAILQ_REMOVE(&allpspecs, p, p_list);
1239 * - delete the alldevi entry
1241 TAILQ_REMOVE(&alldevi, i, i_next);
1242 ndevi--;
1244 * Put it in deaddevitab
1246 * Each time a devi is removed, devilevel is increased so that later on
1247 * it is possible to tell if an instance was added before or after the
1248 * removal of its parent.
1250 * For active instances, i_level contains the number of devi removed so
1251 * far, and for dead devis, it contains its index.
1253 i->i_level = devilevel++;
1254 i->i_alias = NULL;
1255 f = ht_lookup(deaddevitab, i->i_name);
1256 if (f == NULL) {
1257 if (ht_insert(deaddevitab, i->i_name, i))
1258 panic("remove_devi(%s) - can't add to deaddevitab",
1259 i->i_name);
1260 } else {
1261 for (j = f; j->i_alias != NULL; j = j->i_alias);
1262 j->i_alias = i;
1265 * - reconstruct d->d_umax
1267 d->d_umax = 0;
1268 for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1269 if (i->i_unit >= d->d_umax)
1270 d->d_umax = i->i_unit + 1;
1273 void
1274 deldeva(const char *at)
1276 int unit;
1277 const char *cp;
1278 struct devbase *d, *ad;
1279 struct devi *i, *j;
1280 struct attr *a;
1281 struct pspec *p;
1282 struct nvlist *nv, *stack = NULL;
1284 if (at == NULL) {
1285 TAILQ_FOREACH(i, &alldevi, i_next)
1286 if (i->i_at == NULL)
1287 stack = newnv(NULL, NULL, i, 0, stack);
1288 } else {
1289 int l;
1291 l = strlen(at) - 1;
1292 if (at[l] == '?' || isdigit((unsigned char)at[l])) {
1293 char base[NAMESIZE];
1295 if (split(at, l+1, base, sizeof base, &unit)) {
1296 cfgerror("invalid attachment name `%s'", at);
1297 return;
1299 cp = intern(base);
1300 } else {
1301 cp = intern(at);
1302 unit = STAR;
1305 ad = ht_lookup(devbasetab, cp);
1306 a = ht_lookup(attrtab, cp);
1307 if (a == NULL) {
1308 cfgerror("unknown attachment attribute or device `%s'",
1309 cp);
1310 return;
1312 if (!a->a_iattr) {
1313 cfgerror("plain attribute `%s' cannot have children",
1314 a->a_name);
1315 return;
1319 * remove_devi() makes changes to the devbase's list and the
1320 * alias list, * so the actual deletion of the instances must
1321 * be delayed.
1323 for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) {
1324 d = nv->nv_ptr;
1325 for (i = d->d_ihead; i != NULL; i = i->i_bsame)
1326 for (j = i; j != NULL; j = j->i_alias) {
1327 /* Ignore devices at root */
1328 if (j->i_at == NULL)
1329 continue;
1330 p = j->i_pspec;
1332 * There are three cases:
1334 * 1. unit is not STAR. Consider 'at'
1335 * to be explicit, even if it
1336 * references an interface
1337 * attribute.
1339 * 2. unit is STAR and 'at' references
1340 * a real device. Look for pspec
1341 * that have a matching p_atdev
1342 * field.
1344 * 3. unit is STAR and 'at' references
1345 * an interface attribute. Look
1346 * for pspec that have a matching
1347 * p_iattr field.
1349 if ((unit != STAR && /* Case */
1350 !strcmp(j->i_at, at)) || /* 1 */
1351 (unit == STAR &&
1352 ((ad != NULL && /* Case */
1353 p->p_atdev == ad) || /* 2 */
1354 (ad == NULL && /* Case */
1355 p->p_iattr == a)))) /* 3 */
1356 stack = newnv(NULL, NULL, j, 0,
1357 stack);
1362 for (nv = stack; nv != NULL; nv = nv->nv_next)
1363 remove_devi(nv->nv_ptr);
1364 nvfreel(stack);
1367 void
1368 deldev(const char *name)
1370 int l;
1371 struct devi *firsti, *i;
1372 struct nvlist *nv, *stack = NULL;
1374 l = strlen(name) - 1;
1375 if (name[l] == '*' || isdigit((unsigned char)name[l])) {
1376 /* `no mydev0' or `no mydev*' */
1377 firsti = ht_lookup(devitab, name);
1378 if (firsti == NULL) {
1379 cfgerror("unknown instance %s", name);
1380 return;
1382 for (i = firsti; i != NULL; i = i->i_alias)
1383 stack = newnv(NULL, NULL, i, 0, stack);
1384 } else {
1385 struct devbase *d = ht_lookup(devbasetab, name);
1387 if (d == NULL) {
1388 cfgerror("unknown device %s", name);
1389 return;
1391 if (d->d_ispseudo) {
1392 cfgerror("%s is a pseudo-device; "
1393 "use \"no pseudo-device %s\" instead", name,
1394 name);
1395 return;
1398 for (firsti = d->d_ihead; firsti != NULL;
1399 firsti = firsti->i_bsame)
1400 for (i = firsti; i != NULL; i = i->i_alias)
1401 stack = newnv(NULL, NULL, i, 0, stack);
1404 for (nv = stack; nv != NULL; nv = nv->nv_next)
1405 remove_devi(nv->nv_ptr);
1406 nvfreel(stack);
1409 void
1410 addpseudo(const char *name, int number)
1412 struct devbase *d;
1413 struct devi *i;
1415 d = ht_lookup(devbasetab, name);
1416 if (d == NULL) {
1417 cfgerror("undefined pseudo-device %s", name);
1418 return;
1420 if (!d->d_ispseudo) {
1421 cfgerror("%s is a real device, not a pseudo-device", name);
1422 return;
1424 if (ht_lookup(devitab, name) != NULL) {
1425 cfgerror("`%s' already defined", name);
1426 return;
1428 i = newdevi(name, number - 1, d); /* foo 16 => "foo0..foo15" */
1429 if (ht_insert(devitab, name, i))
1430 panic("addpseudo(%s)", name);
1431 /* Useful to retrieve the instance from the devbase */
1432 d->d_ihead = i;
1433 i->i_active = DEVI_ACTIVE;
1434 TAILQ_INSERT_TAIL(&allpseudo, i, i_next);
1437 void
1438 delpseudo(const char *name)
1440 struct devbase *d;
1441 struct devi *i;
1443 d = ht_lookup(devbasetab, name);
1444 if (d == NULL) {
1445 cfgerror("undefined pseudo-device %s", name);
1446 return;
1448 if (!d->d_ispseudo) {
1449 cfgerror("%s is a real device, not a pseudo-device", name);
1450 return;
1452 if ((i = ht_lookup(devitab, name)) == NULL) {
1453 cfgerror("`%s' not defined", name);
1454 return;
1456 d->d_umax = 0; /* clear neads-count entries */
1457 d->d_ihead = NULL; /* make sure it won't be considered active */
1458 TAILQ_REMOVE(&allpseudo, i, i_next);
1459 if (ht_remove(devitab, name))
1460 panic("delpseudo(%s) - can't remove from devitab", name);
1461 if (ht_insert(deaddevitab, name, i))
1462 panic("delpseudo(%s) - can't add to deaddevitab", name);
1465 void
1466 adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor,
1467 struct nvlist *nv)
1469 struct devm *dm;
1471 if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) {
1472 cfgerror("character major %d is invalid", cmajor);
1473 nvfreel(nv);
1474 return;
1477 if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) {
1478 cfgerror("block major %d is invalid", bmajor);
1479 nvfreel(nv);
1480 return;
1482 if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) {
1483 cfgerror("both character/block majors are not specified");
1484 nvfreel(nv);
1485 return;
1488 dm = ecalloc(1, sizeof(*dm));
1489 dm->dm_srcfile = yyfile;
1490 dm->dm_srcline = currentline();
1491 dm->dm_name = name;
1492 dm->dm_cmajor = cmajor;
1493 dm->dm_bmajor = bmajor;
1494 dm->dm_opts = nv;
1496 TAILQ_INSERT_TAIL(&alldevms, dm, dm_next);
1498 maxcdevm = MAX(maxcdevm, dm->dm_cmajor);
1499 maxbdevm = MAX(maxbdevm, dm->dm_bmajor);
1503 fixdevis(void)
1505 struct devi *i;
1506 int error = 0;
1508 TAILQ_FOREACH(i, &alldevi, i_next)
1509 if (i->i_active == DEVI_ACTIVE)
1510 selectbase(i->i_base, i->i_atdeva);
1511 else if (i->i_active == DEVI_ORPHAN) {
1513 * At this point, we can't have instances for which
1514 * i_at or i_pspec are NULL.
1516 ++error;
1517 cfgxerror(i->i_srcfile, i->i_lineno,
1518 "`%s at %s' is orphaned (%s `%s' found)",
1519 i->i_name, i->i_at, i->i_pspec->p_atunit == WILD ?
1520 "nothing matching" : "no", i->i_at);
1521 } else if (vflag && i->i_active == DEVI_IGNORED)
1522 cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring "
1523 "explicitly orphaned instance `%s at %s'",
1524 i->i_name, i->i_at);
1526 if (error)
1527 return error;
1529 TAILQ_FOREACH(i, &allpseudo, i_next)
1530 if (i->i_active == DEVI_ACTIVE)
1531 selectbase(i->i_base, NULL);
1532 return 0;
1536 * Look up a parent spec, creating a new one if it does not exist.
1538 static struct pspec *
1539 getpspec(struct attr *attr, struct devbase *ab, int atunit)
1541 struct pspec *p;
1543 TAILQ_FOREACH(p, &allpspecs, p_list) {
1544 if (p->p_iattr == attr &&
1545 p->p_atdev == ab &&
1546 p->p_atunit == atunit)
1547 return (p);
1550 p = ecalloc(1, sizeof(*p));
1552 p->p_iattr = attr;
1553 p->p_atdev = ab;
1554 p->p_atunit = atunit;
1555 p->p_inst = npspecs++;
1556 p->p_active = 0;
1558 TAILQ_INSERT_TAIL(&allpspecs, p, p_list);
1560 return (p);
1564 * Define a new instance of a specific device.
1566 static struct devi *
1567 getdevi(const char *name)
1569 struct devi *i, *firsti;
1570 struct devbase *d;
1571 int unit;
1572 char base[NAMESIZE];
1574 if (split(name, strlen(name), base, sizeof base, &unit)) {
1575 cfgerror("invalid device name `%s'", name);
1576 return (NULL);
1578 d = ht_lookup(devbasetab, intern(base));
1579 if (d == NULL) {
1580 cfgerror("%s: unknown device `%s'", name, base);
1581 return (NULL);
1583 if (d->d_ispseudo) {
1584 cfgerror("%s: %s is a pseudo-device", name, base);
1585 return (NULL);
1587 firsti = ht_lookup(devitab, name);
1588 i = newdevi(name, unit, d);
1589 if (firsti == NULL) {
1590 if (ht_insert(devitab, name, i))
1591 panic("getdevi(%s)", name);
1592 *d->d_ipp = i;
1593 d->d_ipp = &i->i_bsame;
1594 } else {
1595 while (firsti->i_alias)
1596 firsti = firsti->i_alias;
1597 firsti->i_alias = i;
1599 TAILQ_INSERT_TAIL(&alldevi, i, i_next);
1600 ndevi++;
1601 return (i);
1604 static const char *
1605 concat(const char *name, int c)
1607 size_t len;
1608 char buf[NAMESIZE];
1610 len = strlen(name);
1611 if (len + 2 > sizeof(buf)) {
1612 cfgerror("device name `%s%c' too long", name, c);
1613 len = sizeof(buf) - 2;
1615 memmove(buf, name, len);
1616 buf[len] = c;
1617 buf[len + 1] = 0;
1618 return (intern(buf));
1621 const char *
1622 starref(const char *name)
1625 return (concat(name, '*'));
1628 const char *
1629 wildref(const char *name)
1632 return (concat(name, '?'));
1636 * Split a name like "foo0" into base name (foo) and unit number (0).
1637 * Return 0 on success. To make this useful for names like "foo0a",
1638 * the length of the "foo0" part is one of the arguments.
1640 static int
1641 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit)
1643 const char *cp;
1644 int c;
1645 size_t l;
1647 l = nlen;
1648 if (l < 2 || l >= bsize || isdigit((unsigned char)*name))
1649 return (1);
1650 c = (u_char)name[--l];
1651 if (!isdigit(c)) {
1652 if (c == '*')
1653 *aunit = STAR;
1654 else if (c == '?')
1655 *aunit = WILD;
1656 else
1657 return (1);
1658 } else {
1659 cp = &name[l];
1660 while (isdigit((unsigned char)cp[-1]))
1661 l--, cp--;
1662 *aunit = atoi(cp);
1664 memmove(base, name, l);
1665 base[l] = 0;
1666 return (0);
1669 void
1670 selectattr(struct attr *a)
1673 (void)ht_insert(selecttab, a->a_name, __UNCONST(a->a_name));
1677 * We have an instance of the base foo, so select it and all its
1678 * attributes for "optional foo".
1680 static void
1681 selectbase(struct devbase *d, struct deva *da)
1683 struct attr *a;
1684 struct nvlist *nv;
1686 (void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name));
1687 for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
1688 a = nv->nv_ptr;
1689 expandattr(a, selectattr);
1691 if (da != NULL) {
1692 (void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name));
1693 for (nv = da->d_attrs; nv != NULL; nv = nv->nv_next) {
1694 a = nv->nv_ptr;
1695 expandattr(a, selectattr);
1701 * Is the given pointer on the given list of pointers?
1704 onlist(struct nvlist *nv, void *ptr)
1706 for (; nv != NULL; nv = nv->nv_next)
1707 if (nv->nv_ptr == ptr)
1708 return (1);
1709 return (0);
1712 static char *
1713 extend(char *p, const char *name)
1715 size_t l;
1717 l = strlen(name);
1718 memmove(p, name, l);
1719 p += l;
1720 *p++ = ',';
1721 *p++ = ' ';
1722 return (p);
1726 * Check that we got all required locators, and default any that are
1727 * given as "?" and have defaults. Return 0 on success.
1729 static const char **
1730 fixloc(const char *name, struct attr *attr, struct nvlist *got)
1732 struct nvlist *m, *n;
1733 int ord;
1734 const char **lp;
1735 int nmissing, nextra, nnodefault;
1736 char *mp, *ep, *ndp;
1737 char missing[1000], extra[1000], nodefault[1000];
1738 static const char *nullvec[1];
1741 * Look for all required locators, and number the given ones
1742 * according to the required order. While we are numbering,
1743 * set default values for defaulted locators.
1745 if (attr->a_loclen == 0) /* e.g., "at root" */
1746 lp = nullvec;
1747 else
1748 lp = emalloc((attr->a_loclen + 1) * sizeof(const char *));
1749 for (n = got; n != NULL; n = n->nv_next)
1750 n->nv_num = -1;
1751 nmissing = 0;
1752 mp = missing;
1753 /* yes, this is O(mn), but m and n should be small */
1754 for (ord = 0, m = attr->a_locs; m != NULL; m = m->nv_next, ord++) {
1755 for (n = got; n != NULL; n = n->nv_next) {
1756 if (n->nv_name == m->nv_name) {
1757 n->nv_num = ord;
1758 break;
1761 if (n == NULL && m->nv_num == 0) {
1762 nmissing++;
1763 mp = extend(mp, m->nv_name);
1765 lp[ord] = m->nv_str;
1767 if (ord != attr->a_loclen)
1768 panic("fixloc");
1769 lp[ord] = NULL;
1770 nextra = 0;
1771 ep = extra;
1772 nnodefault = 0;
1773 ndp = nodefault;
1774 for (n = got; n != NULL; n = n->nv_next) {
1775 if (n->nv_num >= 0) {
1776 if (n->nv_str != NULL)
1777 lp[n->nv_num] = n->nv_str;
1778 else if (lp[n->nv_num] == NULL) {
1779 nnodefault++;
1780 ndp = extend(ndp, n->nv_name);
1782 } else {
1783 nextra++;
1784 ep = extend(ep, n->nv_name);
1787 if (nextra) {
1788 ep[-2] = 0; /* kill ", " */
1789 cfgerror("%s: extraneous locator%s: %s",
1790 name, nextra > 1 ? "s" : "", extra);
1792 if (nmissing) {
1793 mp[-2] = 0;
1794 cfgerror("%s: must specify %s", name, missing);
1796 if (nnodefault) {
1797 ndp[-2] = 0;
1798 cfgerror("%s: cannot wildcard %s", name, nodefault);
1800 if (nmissing || nnodefault) {
1801 free(lp);
1802 lp = NULL;
1804 return (lp);
1807 void
1808 setversion(int newver)
1810 if (newver > CONFIG_VERSION)
1811 cfgerror("your sources require a newer version of config(1) "
1812 "-- please rebuild it.");
1813 else if (newver < CONFIG_MINVERSION)
1814 cfgerror("your sources are out of date -- please update.");
1815 else
1816 version = newver;