xref: /netbsd-src/usr.bin/config/lint.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: lint.c,v 1.8 2009/08/30 21:07:41 cube 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 <stdlib.h>
34 
35 #include "defs.h"
36 
37 void
38 emit_params()
39 {
40 
41 	printf("version\t%d\n", CONFIG_VERSION);
42 	printf("ident\t\"LINT_%s\"\n", conffile);
43 	printf("maxusers\t%d\n", defmaxusers);
44 	printf("config netbsdlint root on ?\n");
45 	printf("\n");
46 }
47 
48 enum opt_types {
49 	OT_FLAG,
50 	OT_PARAM,
51 	OT_FS
52 };
53 
54 static struct opt_type {
55 	enum opt_types ot_type;
56 	const char *ot_name;
57 	struct hashtab **ot_ht;
58 } opt_types[] = {
59 	{ OT_FLAG, "options", &opttab },
60 	{ OT_PARAM, "options", &opttab },
61 	{ OT_FS, "file-system", &fsopttab },
62 };
63 
64 static int
65 do_emit_option(const char *name, void *value, void *v)
66 {
67 	struct nvlist *nv = value;
68 	const struct opt_type *ot = v;
69 
70 	if (nv->nv_flags & NV_OBSOLETE)
71 		return 0;
72 
73 	if (ht_lookup(*(ot->ot_ht), name))
74 		return 0;
75 
76 	printf("%s\t%s", ot->ot_name, nv->nv_name);
77 	if (ot->ot_type == OT_PARAM) {
78 		struct nvlist *nv2  = ht_lookup(defoptlint, nv->nv_name);
79 		if (nv2 == NULL)
80 			nv2 = nv;
81 		printf("=\"%s\"", nv2->nv_str ? nv2->nv_str : "1");
82 	}
83 	printf("\n");
84 
85 	return 1;
86 }
87 
88 
89 void
90 emit_options()
91 {
92 
93 	(void)ht_enumerate(defflagtab, do_emit_option, &opt_types[0]);
94 	printf("\n");
95 	(void)ht_enumerate(defparamtab, do_emit_option, &opt_types[1]);
96 	printf("\n");
97 	(void)ht_enumerate(deffstab, do_emit_option, &opt_types[2]);
98 	printf("\n");
99 }
100 
101 static void
102 do_emit_instances(struct devbase *d, struct attr *at)
103 {
104 	struct nvlist *nv, *nv1;
105 	struct attr *a;
106 	struct deva *da;
107 
108 	/*
109 	 * d_isdef is used to check whether a deva has been seen or not,
110 	 * for there are devices that can be their own ancestor (e.g.
111 	 * uhub, pci).
112 	 */
113 
114 	if (at != NULL) {
115 		for (da = d->d_ahead; da != NULL; da = da->d_bsame)
116 			if (onlist(da->d_atlist, at))
117 				break;
118 		if (da == NULL)
119 			panic("do_emit_instances: no deva found for %s at %s",
120 			    d->d_name, at->a_name);
121 
122 		if (da->d_isdef > 1)
123 			return;
124 		da->d_isdef = 2;
125 	}
126 
127 	if (at == NULL && !d->d_ispseudo && d->d_ihead == NULL)
128 		printf("%s0\tat\troot\n", d->d_name);
129 	else if (at != NULL && !d->d_ispseudo && da->d_ihead == NULL) {
130 		printf("%s0\tat\t%s?", d->d_name, at->a_name);
131 
132 		for (nv = at->a_locs; nv != NULL; nv = nv->nv_next) {
133 			if (nv->nv_num == 0)
134 				printf(" %s %c", nv->nv_name,
135 				    nv->nv_str ? '?' : '0');
136 		}
137 
138 		printf("\n");
139 	}
140 
141 	/*
142 	 * Children attachments are found the same way as in the orphan
143 	 * detection code in main.c.
144 	 */
145 	for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
146 		a = nv->nv_ptr;
147 		for (nv1 = a->a_devs; nv1 != NULL; nv1 = nv1->nv_next)
148 			do_emit_instances(nv1->nv_ptr, a);
149 	}
150 }
151 
152 /* ARGSUSED */
153 static int
154 emit_root_instance(const char *name, void *value, void *v)
155 {
156 
157 	do_emit_instances((struct devbase *)value, NULL);
158 
159 	return 1;
160 }
161 
162 /* ARGSUSED */
163 static int
164 emit_pseudo_instance(const char *name, void *value, void *v)
165 {
166 	struct devbase *d = value;
167 
168 	if (d->d_ispseudo && d->d_ihead == NULL)
169 		printf("pseudo-device\t%s\n", d->d_name);
170 	return 0;
171 }
172 
173 void
174 emit_instances()
175 {
176 
177 	(void)ht_enumerate(devroottab, emit_root_instance, NULL);
178 	printf("\n");
179 	(void)ht_enumerate(devbasetab, emit_pseudo_instance, NULL);
180 }
181