1 1.1 christos #! /usr/bin/awk -f 2 1.6 hgutch # $NetBSD: devlist2h.awk,v 1.6 2025/07/29 18:52:15 hgutch Exp $ 3 1.1 christos # 4 1.1 christos # Copyright (c) 1995, 1996 Christopher G. Demetriou 5 1.1 christos # All rights reserved. 6 1.1 christos # 7 1.1 christos # Redistribution and use in source and binary forms, with or without 8 1.1 christos # modification, are permitted provided that the following conditions 9 1.1 christos # are met: 10 1.1 christos # 1. Redistributions of source code must retain the above copyright 11 1.1 christos # notice, this list of conditions and the following disclaimer. 12 1.1 christos # 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos # notice, this list of conditions and the following disclaimer in the 14 1.1 christos # documentation and/or other materials provided with the distribution. 15 1.1 christos # 3. All advertising materials mentioning features or use of this software 16 1.1 christos # must display the following acknowledgement: 17 1.1 christos # This product includes software developed by Christopher G. Demetriou. 18 1.1 christos # 4. The name of the author may not be used to endorse or promote products 19 1.1 christos # derived from this software without specific prior written permission 20 1.1 christos # 21 1.1 christos # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 1.1 christos # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 1.1 christos # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 1.1 christos # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 1.1 christos # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 1.1 christos # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 1.1 christos # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 1.1 christos # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 1.1 christos # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 1.1 christos # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 1.1 christos # 32 1.4 pgoyette 33 1.4 pgoyette function collectline(f) { 34 1.5 riastrad oparen = 0 35 1.4 pgoyette line = "" 36 1.4 pgoyette while (f <= NF) { 37 1.4 pgoyette if ($f == "#") { 38 1.4 pgoyette line = line "(" 39 1.4 pgoyette oparen = 1 40 1.4 pgoyette f++ 41 1.4 pgoyette continue 42 1.4 pgoyette } 43 1.5 riastrad if (oparen) { 44 1.4 pgoyette line = line $f 45 1.4 pgoyette if (f < NF) 46 1.4 pgoyette line = line " " 47 1.4 pgoyette f++ 48 1.4 pgoyette continue 49 1.4 pgoyette } 50 1.4 pgoyette line = line $f 51 1.4 pgoyette if (f < NF) 52 1.4 pgoyette line = line " " 53 1.4 pgoyette f++ 54 1.4 pgoyette } 55 1.4 pgoyette if (oparen) 56 1.4 pgoyette line = line ")" 57 1.4 pgoyette return line 58 1.4 pgoyette } 59 1.4 pgoyette 60 1.1 christos NR == 1 { 61 1.1 christos nproducts = nvendors = blanklines = 0 62 1.2 pgoyette vendormaxlen = productmaxlen = 0 63 1.1 christos nchars = 1 64 1.1 christos dfile= FILENAME "_data.h" 65 1.1 christos hfile= FILENAME ".h" 66 1.1 christos prefix = FILENAME 67 1.1 christos gsub("devs", "", prefix) 68 1.1 christos PREFIX = toupper(prefix) 69 1.1 christos VERSION = $0 70 1.1 christos gsub("\\$", "", VERSION) 71 1.1 christos gsub(/ $/, "", VERSION) 72 1.1 christos 73 1.4 pgoyette if ( PREFIX == "MII" ) { 74 1.4 pgoyette VENDOR="OUI" 75 1.4 pgoyette PRODUCT="MODEL" 76 1.4 pgoyette vendor="oui" 77 1.4 pgoyette product="model" 78 1.4 pgoyette fmt_vendor="oui %6.6x" 79 1.4 pgoyette fmt_product="model %4.4x" 80 1.4 pgoyette } else { 81 1.4 pgoyette VENDOR="VENDOR" 82 1.4 pgoyette PRODUCT="PRODUCT" 83 1.4 pgoyette vendor="vendor" 84 1.4 pgoyette product="product" 85 1.4 pgoyette fmt_vendor="vendor %4.4x" 86 1.4 pgoyette fmt_product="product %4.4x" 87 1.4 pgoyette } 88 1.4 pgoyette 89 1.1 christos printf("/*\t$NetBSD" "$\t*/\n\n") > dfile 90 1.1 christos printf("/*\n") > dfile 91 1.3 wiz printf(" * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.\n") \ 92 1.1 christos > dfile 93 1.1 christos printf(" *\n") > dfile 94 1.1 christos printf(" * generated from:\n") > dfile 95 1.1 christos printf(" *\t%s\n", VERSION) > dfile 96 1.1 christos printf(" */\n") > dfile 97 1.1 christos 98 1.1 christos printf("/*\t$NetBSD" "$\t*/\n\n") > hfile 99 1.1 christos printf("/*\n") > hfile 100 1.3 wiz printf(" * THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.\n") \ 101 1.1 christos > hfile 102 1.1 christos printf(" *\n") > hfile 103 1.1 christos printf(" * generated from:\n") > hfile 104 1.1 christos printf(" *\t%s\n", VERSION) > hfile 105 1.1 christos printf(" */\n") > hfile 106 1.1 christos 107 1.1 christos next 108 1.1 christos } 109 1.4 pgoyette NF > 0 && $1 == vendor { 110 1.1 christos nvendors++ 111 1.1 christos 112 1.1 christos vendorindex[$2] = nvendors; # record index for this name, for later. 113 1.1 christos vendors[nvendors, 1] = $2; # name 114 1.1 christos vendors[nvendors, 2] = $3; # id 115 1.4 pgoyette printf("#define\t%s_%s_%s\t%s", PREFIX, VENDOR, vendors[nvendors, 1], 116 1.1 christos vendors[nvendors, 2]) > hfile 117 1.1 christos 118 1.1 christos i = 3; f = 4; 119 1.1 christos 120 1.1 christos # comments 121 1.1 christos ocomment = oparen = 0 122 1.1 christos if (f <= NF) { 123 1.1 christos printf("\t\t/* ") > hfile 124 1.1 christos ocomment = 1; 125 1.1 christos } 126 1.1 christos while (f <= NF) { 127 1.1 christos if ($f == "#") { 128 1.1 christos printf("(") > hfile 129 1.1 christos oparen = 1 130 1.1 christos f++ 131 1.1 christos continue 132 1.1 christos } 133 1.1 christos if (oparen) { 134 1.1 christos printf("%s", $f) > hfile 135 1.1 christos if (f < NF) 136 1.1 christos printf(" ") > hfile 137 1.1 christos f++ 138 1.1 christos continue 139 1.1 christos } 140 1.1 christos vendors[nvendors, i] = $f 141 1.1 christos if (words[$f, 1] == 0) { 142 1.1 christos l = length($f); 143 1.1 christos parts = split($f, junk, "\\"); 144 1.1 christos l = l - (parts - 1); 145 1.1 christos nwords++; 146 1.1 christos words[$f, 1] = nwords; 147 1.1 christos words[$f, 2] = l; 148 1.1 christos wordlist[nwords, 1] = $f; 149 1.1 christos wordlist[nwords, 3] = nchars; 150 1.1 christos nchars = nchars + l + 1; 151 1.1 christos } 152 1.1 christos wordlist[words[$f, 1], 2]++; 153 1.1 christos vendors[nvendors, i] = words[$f, 1]; 154 1.1 christos printf("%s", $f) > hfile 155 1.1 christos if (f < NF) 156 1.1 christos printf(" ") > hfile 157 1.1 christos i++; f++; 158 1.1 christos } 159 1.1 christos if (oparen) 160 1.1 christos printf(")") > hfile 161 1.1 christos if (ocomment) 162 1.1 christos printf(" */") > hfile 163 1.1 christos printf("\n") > hfile 164 1.1 christos 165 1.2 pgoyette if (length($2) > vendormaxlen) { 166 1.2 pgoyette vendormaxlen = length($2) 167 1.2 pgoyette } 168 1.2 pgoyette 169 1.1 christos next 170 1.1 christos } 171 1.4 pgoyette NF > 0 && $1 == product { 172 1.1 christos nproducts++ 173 1.1 christos 174 1.1 christos products[nproducts, 1] = $2; # vendor name 175 1.1 christos products[nproducts, 2] = $3; # product id 176 1.1 christos products[nproducts, 3] = $4; # id 177 1.4 pgoyette printf("#define\t%s_%s_%s_%s\t%s", PREFIX, PRODUCT, 178 1.4 pgoyette products[nproducts, 1], products[nproducts, 2], 179 1.4 pgoyette products[nproducts, 3]) > hfile 180 1.1 christos 181 1.1 christos i=4; f = 5; 182 1.1 christos 183 1.1 christos # comments 184 1.2 pgoyette productlen = ocomment = oparen = 0 185 1.1 christos if (f <= NF) { 186 1.1 christos printf("\t\t/* ") > hfile 187 1.1 christos ocomment = 1; 188 1.1 christos } 189 1.1 christos while (f <= NF) { 190 1.1 christos if ($f == "#") { 191 1.1 christos printf("(") > hfile 192 1.1 christos oparen = 1 193 1.1 christos f++ 194 1.1 christos continue 195 1.1 christos } 196 1.1 christos if (oparen) { 197 1.1 christos printf("%s", $f) > hfile 198 1.1 christos if (f < NF) 199 1.1 christos printf(" ") > hfile 200 1.1 christos f++ 201 1.1 christos continue 202 1.1 christos } 203 1.1 christos if (words[$f, 1] == 0) { 204 1.1 christos l = length($f); 205 1.1 christos parts = split($f, junk, "\\"); 206 1.1 christos l = l - (parts - 1); 207 1.1 christos nwords++; 208 1.1 christos words[$f, 2] = l; 209 1.1 christos words[$f, 1] = nwords; 210 1.1 christos wordlist[nwords, 1] = $f; 211 1.1 christos wordlist[nwords, 3] = nchars; 212 1.1 christos nchars = nchars + l + 1; 213 1.1 christos } 214 1.2 pgoyette productlen += words[$f, 2] + 1; 215 1.1 christos wordlist[words[$f, 1], 2]++; 216 1.1 christos products[nproducts, i] = words[$f, 1]; 217 1.1 christos printf("%s", $f) > hfile 218 1.1 christos if (f < NF) 219 1.1 christos printf(" ") > hfile 220 1.1 christos i++; f++; 221 1.1 christos } 222 1.1 christos if (oparen) 223 1.1 christos printf(")") > hfile 224 1.1 christos if (ocomment) 225 1.1 christos printf(" */") > hfile 226 1.1 christos printf("\n") > hfile 227 1.1 christos 228 1.2 pgoyette if (productlen > productmaxlen) { 229 1.2 pgoyette productmaxlen = productlen; 230 1.2 pgoyette } 231 1.2 pgoyette 232 1.4 pgoyette if (PREFIX == "MII") { 233 1.4 pgoyette printf("#define\tMII_STR_%s_%s\t\"%s\"\n", 234 1.4 pgoyette products[nproducts, 1], products[nproducts, 2], 235 1.4 pgoyette collectline(5)) > hfile 236 1.4 pgoyette } 237 1.4 pgoyette 238 1.1 christos next 239 1.1 christos } 240 1.1 christos { 241 1.1 christos if ($0 == "") 242 1.1 christos blanklines++ 243 1.1 christos print $0 > hfile 244 1.1 christos if (blanklines < 2) 245 1.1 christos print $0 > dfile 246 1.1 christos } 247 1.1 christos END { 248 1.1 christos # print out the match tables 249 1.1 christos 250 1.1 christos printf("\n") > dfile 251 1.1 christos 252 1.4 pgoyette printf("static const uint32_t %s_vendors[] = {\n", prefix) > dfile 253 1.1 christos for (i = 1; i <= nvendors; i++) { 254 1.4 pgoyette printf("\t %s_%s_%s", PREFIX, VENDOR, vendors[i, 1]) \ 255 1.1 christos > dfile 256 1.1 christos 257 1.1 christos j = 3; 258 1.1 christos while ((i, j) in vendors) { 259 1.1 christos printf(", %d", 260 1.1 christos wordlist[vendors[i, j], 3]) > dfile 261 1.1 christos # printf(", %d /* %s */", 262 1.1 christos # wordlist[vendors[i, j], 3], 263 1.1 christos # wordlist[vendors[i, j], 1]) > dfile 264 1.1 christos j++ 265 1.1 christos } 266 1.1 christos printf(", 0,\n", sep) > dfile 267 1.1 christos } 268 1.1 christos printf("};\n") > dfile 269 1.1 christos 270 1.1 christos printf("\n") > dfile 271 1.1 christos 272 1.4 pgoyette printf("static const uint32_t %s_products[] = {\n", prefix) > dfile 273 1.1 christos for (i = 1; i <= nproducts; i++) { 274 1.4 pgoyette printf("\t %s_%s_%s, %s_%s_%s_%s, \n", 275 1.4 pgoyette PREFIX, VENDOR, products[i, 1], PREFIX, PRODUCT, 276 1.4 pgoyette products[i, 1], products[i, 2]) > dfile 277 1.1 christos 278 1.1 christos printf("\t ") > dfile 279 1.1 christos j = 4 280 1.1 christos sep = "" 281 1.1 christos while ((i, j) in products) { 282 1.1 christos printf("%s%d", sep, 283 1.1 christos wordlist[products[i, j], 3]) > dfile 284 1.1 christos # printf("%s%d /* %s */", sep, 285 1.1 christos # wordlist[products[i, j], 3], 286 1.1 christos # wordlist[products[i, j], 1]) > dfile 287 1.1 christos sep = ", " 288 1.1 christos j++ 289 1.1 christos } 290 1.1 christos printf("%s0,\n", sep) > dfile 291 1.1 christos } 292 1.1 christos printf("};\n") > dfile 293 1.1 christos 294 1.1 christos printf("static const char %s_words[] = { \".\" \n", prefix) > dfile 295 1.1 christos for (i = 1; i <= nwords; i++) { 296 1.1 christos printf("\t \"%s\\0\" /* %d refs @ %d */\n", 297 1.1 christos wordlist[i, 1], wordlist[i, 2], wordlist[i, 3]) > dfile 298 1.1 christos } 299 1.1 christos printf("};\n") > dfile 300 1.1 christos printf("const int %s_nwords = %d;\n", prefix, nwords) > dfile 301 1.1 christos 302 1.1 christos printf("\n") > dfile 303 1.1 christos 304 1.4 pgoyette printf("\n") > hfile 305 1.4 pgoyette printf("/* Define format strings for non-existent values */\n") > hfile 306 1.4 pgoyette printf("#define %s_id1_format\t\"%s\"\n", prefix, fmt_vendor) > hfile 307 1.4 pgoyette printf("#define %s_id2_format\t\"%s\"\n", prefix, fmt_product) > hfile 308 1.4 pgoyette 309 1.4 pgoyette printf("Maximum %s string length: %d\n", vendor, vendormaxlen + 1) 310 1.4 pgoyette printf("Maximum %s string length: %d\n", product, productmaxlen + 1) 311 1.2 pgoyette printf("\nEnsure that device-specific values are sufficiently large"); 312 1.6 hgutch printf("\n(check Makefile.%s for details).\n", FILENAME); 313 1.2 pgoyette 314 1.1 christos close(dfile) 315 1.1 christos close(hfile) 316 1.1 christos } 317