1 /* $NetBSD: lint.c,v 1.15 2014/10/29 17:14:50 christos 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.15 2014/10/29 17:14:50 christos Exp $"); 35 36 #include <assert.h> 37 #include <stdlib.h> 38 39 #include "defs.h" 40 41 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 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 value = dl2->dl_lintvalue; 85 else 86 value = dl->dl_value; 87 assert(dl2 == dl); 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 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 115 void 116 emit_options(void) 117 { 118 119 (void)dlhash_enumerate(defflagtab, do_emit_option, &opt_types[0]); 120 printf("\n"); 121 (void)dlhash_enumerate(defparamtab, do_emit_option, &opt_types[1]); 122 printf("\n"); 123 (void)nvhash_enumerate(deffstab, do_emit_fs, &opt_types[2]); 124 printf("\n"); 125 } 126 127 static void 128 do_emit_instances(struct devbase *d, struct attr *at) 129 { 130 struct nvlist *nv1; 131 struct loclist *ll; 132 struct attrlist *al; 133 struct attr *a; 134 struct deva *da; 135 136 /* 137 * d_isdef is used to check whether a deva has been seen or not, 138 * for there are devices that can be their own ancestor (e.g. 139 * uhub, pci). 140 */ 141 142 if (at != NULL) { 143 for (da = d->d_ahead; da != NULL; da = da->d_bsame) 144 if (onlist(da->d_atlist, at)) 145 break; 146 if (da == NULL) 147 panic("do_emit_instances: no deva found for %s at %s", 148 d->d_name, at->a_name); 149 150 if (da->d_isdef > 1) 151 return; 152 da->d_isdef = 2; 153 } 154 155 if (at == NULL && !d->d_ispseudo && d->d_ihead == NULL) 156 printf("%s0\tat\troot\n", d->d_name); 157 else if (at != NULL && !d->d_ispseudo && da->d_ihead == NULL) { 158 printf("%s0\tat\t%s?", d->d_name, at->a_name); 159 160 for (ll = at->a_locs; ll != NULL; ll = ll->ll_next) { 161 if (ll->ll_num == 0) 162 printf(" %s %c", ll->ll_name, 163 ll->ll_string ? '?' : '0'); 164 } 165 166 printf("\n"); 167 } 168 169 /* 170 * Children attachments are found the same way as in the orphan 171 * detection code in main.c. 172 */ 173 for (al = d->d_attrs; al != NULL; al = al->al_next) { 174 a = al->al_this; 175 for (nv1 = a->a_devs; nv1 != NULL; nv1 = nv1->nv_next) 176 do_emit_instances(nv1->nv_ptr, a); 177 } 178 } 179 180 /* ARGSUSED */ 181 static int 182 emit_root_instance(const char *name, void *value, void *v) 183 { 184 185 do_emit_instances((struct devbase *)value, NULL); 186 187 return 1; 188 } 189 190 /* ARGSUSED */ 191 static int 192 emit_pseudo_instance(const char *name, void *value, void *v) 193 { 194 struct devbase *d = value; 195 196 if (d->d_ispseudo && d->d_ihead == NULL) 197 printf("pseudo-device\t%s\n", d->d_name); 198 return 0; 199 } 200 201 void 202 emit_instances(void) 203 { 204 205 (void)ht_enumerate(devroottab, emit_root_instance, NULL); 206 printf("\n"); 207 (void)ht_enumerate(devbasetab, emit_pseudo_instance, NULL); 208 } 209