xref: /netbsd-src/usr.bin/config/lint.c (revision 6d322f2f4598f0d8a138f10ea648ec4fabe41f8b)
1 /*	$NetBSD: lint.c,v 1.14 2012/03/20 20:34:57 matt 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 <assert.h>
34 #include <stdlib.h>
35 
36 #include "defs.h"
37 
38 void
39 emit_params(void)
40 {
41 
42 	printf("version\t%d\n", CONFIG_VERSION);
43 	printf("ident\t\"LINT_%s\"\n", conffile);
44 	printf("maxusers\t%d\n", defmaxusers);
45 	printf("config netbsdlint root on ?\n");
46 	printf("\n");
47 }
48 
49 enum opt_types {
50 	OT_FLAG,
51 	OT_PARAM,
52 	OT_FS
53 };
54 
55 static struct opt_type {
56 	enum opt_types ot_type;
57 	const char *ot_name;
58 	struct hashtab **ot_ht;
59 } opt_types[] = {
60 	{ OT_FLAG, "options", &opttab },
61 	{ OT_PARAM, "options", &opttab },
62 	{ OT_FS, "file-system", &fsopttab },
63 };
64 
65 static int
66 do_emit_option(const char *name, struct defoptlist *dl, void *v)
67 {
68 	const struct opt_type *ot = v;
69 	const char *value;
70 
71 	if (dl->dl_obsolete)
72 		return 0;
73 
74 	if (ht_lookup(*(ot->ot_ht), name))
75 		return 0;
76 
77 	printf("%s\t%s", ot->ot_name, dl->dl_name);
78 	if (ot->ot_type == OT_PARAM) {
79 		struct defoptlist *dl2 = dlhash_lookup(defoptlint, dl->dl_name);
80 		if (dl2 != NULL)
81 			value = dl2->dl_lintvalue;
82 		else
83 			value = dl->dl_value;
84 		assert(dl2 == dl);
85 		printf("=\"%s\"", value ? value : "1");
86 	}
87 	printf("\n");
88 
89 	return 1;
90 }
91 
92 /*
93  * Same as do_emit_option but for filesystem definitions, which now
94  * have a different data type. XXX these should probably be unified
95  * again.
96  */
97 static int
98 do_emit_fs(const char *name, struct nvlist *nv, void *v)
99 {
100 	const struct opt_type *ot = v;
101 
102 	if (ht_lookup(*(ot->ot_ht), name))
103 		return 0;
104 
105 	assert(ot->ot_type != OT_PARAM);
106 	printf("%s\t%s\n", ot->ot_name, nv->nv_name);
107 
108 	return 1;
109 }
110 
111 
112 void
113 emit_options(void)
114 {
115 
116 	(void)dlhash_enumerate(defflagtab, do_emit_option, &opt_types[0]);
117 	printf("\n");
118 	(void)dlhash_enumerate(defparamtab, do_emit_option, &opt_types[1]);
119 	printf("\n");
120 	(void)nvhash_enumerate(deffstab, do_emit_fs, &opt_types[2]);
121 	printf("\n");
122 }
123 
124 static void
125 do_emit_instances(struct devbase *d, struct attr *at)
126 {
127 	struct nvlist *nv1;
128 	struct loclist *ll;
129 	struct attrlist *al;
130 	struct attr *a;
131 	struct deva *da;
132 
133 	/*
134 	 * d_isdef is used to check whether a deva has been seen or not,
135 	 * for there are devices that can be their own ancestor (e.g.
136 	 * uhub, pci).
137 	 */
138 
139 	if (at != NULL) {
140 		for (da = d->d_ahead; da != NULL; da = da->d_bsame)
141 			if (onlist(da->d_atlist, at))
142 				break;
143 		if (da == NULL)
144 			panic("do_emit_instances: no deva found for %s at %s",
145 			    d->d_name, at->a_name);
146 
147 		if (da->d_isdef > 1)
148 			return;
149 		da->d_isdef = 2;
150 	}
151 
152 	if (at == NULL && !d->d_ispseudo && d->d_ihead == NULL)
153 		printf("%s0\tat\troot\n", d->d_name);
154 	else if (at != NULL && !d->d_ispseudo && da->d_ihead == NULL) {
155 		printf("%s0\tat\t%s?", d->d_name, at->a_name);
156 
157 		for (ll = at->a_locs; ll != NULL; ll = ll->ll_next) {
158 			if (ll->ll_num == 0)
159 				printf(" %s %c", ll->ll_name,
160 				    ll->ll_string ? '?' : '0');
161 		}
162 
163 		printf("\n");
164 	}
165 
166 	/*
167 	 * Children attachments are found the same way as in the orphan
168 	 * detection code in main.c.
169 	 */
170 	for (al = d->d_attrs; al != NULL; al = al->al_next) {
171 		a = al->al_this;
172 		for (nv1 = a->a_devs; nv1 != NULL; nv1 = nv1->nv_next)
173 			do_emit_instances(nv1->nv_ptr, a);
174 	}
175 }
176 
177 /* ARGSUSED */
178 static int
179 emit_root_instance(const char *name, void *value, void *v)
180 {
181 
182 	do_emit_instances((struct devbase *)value, NULL);
183 
184 	return 1;
185 }
186 
187 /* ARGSUSED */
188 static int
189 emit_pseudo_instance(const char *name, void *value, void *v)
190 {
191 	struct devbase *d = value;
192 
193 	if (d->d_ispseudo && d->d_ihead == NULL)
194 		printf("pseudo-device\t%s\n", d->d_name);
195 	return 0;
196 }
197 
198 void
199 emit_instances(void)
200 {
201 
202 	(void)ht_enumerate(devroottab, emit_root_instance, NULL);
203 	printf("\n");
204 	(void)ht_enumerate(devbasetab, emit_pseudo_instance, NULL);
205 }
206