1 /*        $NetBSD: lint.c,v 1.17 2025/01/07 14:21:11 joe Exp $        */
2 
3 /*
4  *  Copyright (c) 2007 The NetBSD Foundation.
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #if HAVE_NBTOOL_CONFIG_H
30 #include "nbtool_config.h"
31 #endif
32 
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: lint.c,v 1.17 2025/01/07 14:21:11 joe Exp $");
35 
36 #include <assert.h>
37 #include <stdlib.h>
38 
39 #include "defs.h"
40 
41 void
emit_params(void)42 emit_params(void)
43 {
44 
45           printf("version\t%d\n", CONFIG_VERSION);
46           printf("ident\t\"LINT_%s\"\n", conffile);
47           printf("maxusers\t%d\n", defmaxusers);
48           printf("config netbsdlint root on ?\n");
49           printf("\n");
50 }
51 
52 enum opt_types {
53           OT_FLAG,
54           OT_PARAM,
55           OT_FS
56 };
57 
58 static struct opt_type {
59           enum opt_types ot_type;
60           const char *ot_name;
61           struct hashtab **ot_ht;
62 } opt_types[] = {
63           { OT_FLAG, "options", &opttab },
64           { OT_PARAM, "options", &opttab },
65           { OT_FS, "file-system", &fsopttab },
66 };
67 
68 static int
do_emit_option(const char * name,struct defoptlist * dl,void * v)69 do_emit_option(const char *name, struct defoptlist *dl, void *v)
70 {
71           const struct opt_type *ot = v;
72           const char *value;
73 
74           if (dl->dl_obsolete)
75                     return 0;
76 
77           if (ht_lookup(*(ot->ot_ht), name))
78                     return 0;
79 
80           printf("%s\t%s", ot->ot_name, dl->dl_name);
81           if (ot->ot_type == OT_PARAM) {
82                     struct defoptlist *dl2 = dlhash_lookup(defoptlint, dl->dl_name);
83                     if (dl2 != NULL) {
84                               assert(dl2 == dl);
85                               value = dl2->dl_lintvalue;
86                     } else
87                               value = dl->dl_value;
88                     printf("=\"%s\"", value ? value : "1");
89           }
90           printf("\n");
91 
92           return 1;
93 }
94 
95 /*
96  * Same as do_emit_option but for filesystem definitions, which now
97  * have a different data type. XXX these should probably be unified
98  * again.
99  */
100 static int
do_emit_fs(const char * name,struct nvlist * nv,void * v)101 do_emit_fs(const char *name, struct nvlist *nv, void *v)
102 {
103           const struct opt_type *ot = v;
104 
105           if (ht_lookup(*(ot->ot_ht), name))
106                     return 0;
107 
108           assert(ot->ot_type != OT_PARAM);
109           printf("%s\t%s\n", ot->ot_name, nv->nv_name);
110 
111           return 1;
112 }
113 
114 void
emit_options(void)115 emit_options(void)
116 {
117 
118           (void)dlhash_enumerate(defflagtab, do_emit_option, &opt_types[0]);
119           printf("\n");
120           (void)dlhash_enumerate(defparamtab, do_emit_option, &opt_types[1]);
121           printf("\n");
122           (void)nvhash_enumerate(deffstab, do_emit_fs, &opt_types[2]);
123           printf("\n");
124 }
125 
126 static void
do_emit_instances(struct devbase * d,struct attr * at)127 do_emit_instances(struct devbase *d, struct attr *at)
128 {
129           struct nvlist *nv1;
130           struct loclist *ll;
131           struct attrlist *al;
132           struct attr *a;
133           struct deva *da;
134 
135           /*
136            * d_isdef is used to check whether a deva has been seen or not,
137            * for there are devices that can be their own ancestor (e.g.
138            * uhub, pci).
139            */
140 
141           if (at != NULL) {
142                     for (da = d->d_ahead; da != NULL; da = da->d_bsame)
143                               if (onlist(da->d_atlist, at))
144                                         break;
145                     if (da == NULL)
146                               panic("do_emit_instances: no deva found for %s at %s",
147                                   d->d_name, at->a_name);
148 
149                     if (da->d_isdef > 1)
150                               return;
151                     da->d_isdef = 2;
152           }
153 
154           if (at == NULL && !d->d_ispseudo && d->d_ihead == NULL)
155                     printf("%s0\tat\troot\n", d->d_name);
156           else if (at != NULL && !d->d_ispseudo && da->d_ihead == NULL) {
157                     printf("%s0\tat\t%s?", d->d_name, at->a_name);
158 
159                     for (ll = at->a_locs; ll != NULL; ll = ll->ll_next) {
160                               if (ll->ll_num == 0)
161                                         printf(" %s %c", ll->ll_name,
162                                             ll->ll_string ? '?' : '0');
163                     }
164 
165                     printf("\n");
166           }
167 
168           /*
169            * Children attachments are found the same way as in the orphan
170            * detection code in main.c.
171            */
172           for (al = d->d_attrs; al != NULL; al = al->al_next) {
173                     a = al->al_this;
174                     for (nv1 = a->a_devs; nv1 != NULL; nv1 = nv1->nv_next)
175                               do_emit_instances(nv1->nv_ptr, a);
176           }
177 }
178 
179 /* ARGSUSED */
180 static int
emit_root_instance(const char * name,void * value,void * v)181 emit_root_instance(const char *name, void *value, void *v)
182 {
183 
184           do_emit_instances((struct devbase *)value, NULL);
185 
186           return 1;
187 }
188 
189 /* ARGSUSED */
190 static int
emit_pseudo_instance(const char * name,void * value,void * v)191 emit_pseudo_instance(const char *name, void *value, void *v)
192 {
193           struct devbase *d = value;
194 
195           if (d->d_ispseudo && d->d_ihead == NULL)
196                     printf("pseudo-device\t%s\n", d->d_name);
197           return 0;
198 }
199 
200 void
emit_instances(void)201 emit_instances(void)
202 {
203 
204           (void)ht_enumerate(devroottab, emit_root_instance, NULL);
205           printf("\n");
206           (void)ht_enumerate(devbasetab, emit_pseudo_instance, NULL);
207 }
208