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