Home | History | Annotate | Line # | Download | only in dev
devlist2h.awk revision 1.2
      1  1.1  christos #! /usr/bin/awk -f
      2  1.2  pgoyette #	$NetBSD: devlist2h.awk,v 1.2 2016/10/26 01:03:23 pgoyette 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.1  christos NR == 1 {
     33  1.1  christos 	nproducts = nvendors = blanklines = 0
     34  1.2  pgoyette 	vendormaxlen = productmaxlen = 0
     35  1.1  christos 	nchars = 1
     36  1.1  christos 	dfile= FILENAME "_data.h"
     37  1.1  christos 	hfile= FILENAME ".h"
     38  1.1  christos 	prefix = FILENAME
     39  1.1  christos 	gsub("devs", "", prefix)
     40  1.1  christos 	PREFIX = toupper(prefix)
     41  1.1  christos 	VERSION = $0
     42  1.1  christos 	gsub("\\$", "", VERSION)
     43  1.1  christos 	gsub(/ $/, "", VERSION)
     44  1.1  christos 
     45  1.1  christos 	printf("/*\t$NetBSD" "$\t*/\n\n") > dfile
     46  1.1  christos 	printf("/*\n") > dfile
     47  1.1  christos 	printf(" * THIS FILE AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
     48  1.1  christos 	    > dfile
     49  1.1  christos 	printf(" *\n") > dfile
     50  1.1  christos 	printf(" * generated from:\n") > dfile
     51  1.1  christos 	printf(" *\t%s\n", VERSION) > dfile
     52  1.1  christos 	printf(" */\n") > dfile
     53  1.1  christos 
     54  1.1  christos 	printf("/*\t$NetBSD" "$\t*/\n\n") > hfile
     55  1.1  christos 	printf("/*\n") > hfile
     56  1.1  christos 	printf(" * THIS FILE AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
     57  1.1  christos 	    > hfile
     58  1.1  christos 	printf(" *\n") > hfile
     59  1.1  christos 	printf(" * generated from:\n") > hfile
     60  1.1  christos 	printf(" *\t%s\n", VERSION) > hfile
     61  1.1  christos 	printf(" */\n") > hfile
     62  1.1  christos 
     63  1.1  christos 	next
     64  1.1  christos }
     65  1.1  christos NF > 0 && $1 == "vendor" {
     66  1.1  christos 	nvendors++
     67  1.1  christos 
     68  1.1  christos 	vendorindex[$2] = nvendors;		# record index for this name, for later.
     69  1.1  christos 	vendors[nvendors, 1] = $2;		# name
     70  1.1  christos 	vendors[nvendors, 2] = $3;		# id
     71  1.1  christos 	printf("#define\t%s_VENDOR_%s\t%s", PREFIX, vendors[nvendors, 1],
     72  1.1  christos 	    vendors[nvendors, 2]) > hfile
     73  1.1  christos 
     74  1.1  christos 	i = 3; f = 4;
     75  1.1  christos 
     76  1.1  christos 	# comments
     77  1.1  christos 	ocomment = oparen = 0
     78  1.1  christos 	if (f <= NF) {
     79  1.1  christos 		printf("\t\t/* ") > hfile
     80  1.1  christos 		ocomment = 1;
     81  1.1  christos 	}
     82  1.1  christos 	while (f <= NF) {
     83  1.1  christos 		if ($f == "#") {
     84  1.1  christos 			printf("(") > hfile
     85  1.1  christos 			oparen = 1
     86  1.1  christos 			f++
     87  1.1  christos 			continue
     88  1.1  christos 		}
     89  1.1  christos 		if (oparen) {
     90  1.1  christos 			printf("%s", $f) > hfile
     91  1.1  christos 			if (f < NF)
     92  1.1  christos 				printf(" ") > hfile
     93  1.1  christos 			f++
     94  1.1  christos 			continue
     95  1.1  christos 		}
     96  1.1  christos 		vendors[nvendors, i] = $f
     97  1.1  christos 		if (words[$f, 1] == 0) {
     98  1.1  christos 			l = length($f);
     99  1.1  christos 			parts = split($f, junk, "\\");
    100  1.1  christos 			l = l - (parts - 1);
    101  1.1  christos 			nwords++;
    102  1.1  christos 			words[$f, 1] = nwords;
    103  1.1  christos 			words[$f, 2] = l;
    104  1.1  christos 			wordlist[nwords, 1] = $f;
    105  1.1  christos 			wordlist[nwords, 3] = nchars;
    106  1.1  christos 			nchars = nchars + l + 1;
    107  1.1  christos 		}
    108  1.1  christos 		wordlist[words[$f, 1], 2]++;
    109  1.1  christos 		vendors[nvendors, i] = words[$f, 1];
    110  1.1  christos 		printf("%s", $f) > hfile
    111  1.1  christos 		if (f < NF)
    112  1.1  christos 			printf(" ") > hfile
    113  1.1  christos 		i++; f++;
    114  1.1  christos 	}
    115  1.1  christos 	if (oparen)
    116  1.1  christos 		printf(")") > hfile
    117  1.1  christos 	if (ocomment)
    118  1.1  christos 		printf(" */") > hfile
    119  1.1  christos 	printf("\n") > hfile
    120  1.1  christos 
    121  1.2  pgoyette 	if (length($2) > vendormaxlen) {
    122  1.2  pgoyette 		vendormaxlen = length($2)
    123  1.2  pgoyette 	}
    124  1.2  pgoyette 
    125  1.1  christos 	next
    126  1.1  christos }
    127  1.1  christos NF > 0 && $1 == "product" {
    128  1.1  christos 	nproducts++
    129  1.1  christos 
    130  1.1  christos 	products[nproducts, 1] = $2;		# vendor name
    131  1.1  christos 	products[nproducts, 2] = $3;		# product id
    132  1.1  christos 	products[nproducts, 3] = $4;		# id
    133  1.1  christos 	printf("#define\t%s_PRODUCT_%s_%s\t%s", PREFIX, products[nproducts, 1],
    134  1.1  christos 	    products[nproducts, 2], products[nproducts, 3]) > hfile
    135  1.1  christos 
    136  1.1  christos 	i=4; f = 5;
    137  1.1  christos 
    138  1.1  christos 	# comments
    139  1.2  pgoyette 	productlen = ocomment = oparen = 0
    140  1.1  christos 	if (f <= NF) {
    141  1.1  christos 		printf("\t\t/* ") > hfile
    142  1.1  christos 		ocomment = 1;
    143  1.1  christos 	}
    144  1.1  christos 	while (f <= NF) {
    145  1.1  christos 		if ($f == "#") {
    146  1.1  christos 			printf("(") > hfile
    147  1.1  christos 			oparen = 1
    148  1.1  christos 			f++
    149  1.1  christos 			continue
    150  1.1  christos 		}
    151  1.1  christos 		if (oparen) {
    152  1.1  christos 			printf("%s", $f) > hfile
    153  1.1  christos 			if (f < NF)
    154  1.1  christos 				printf(" ") > hfile
    155  1.1  christos 			f++
    156  1.1  christos 			continue
    157  1.1  christos 		}
    158  1.1  christos 		if (words[$f, 1] == 0) {
    159  1.1  christos 			l = length($f);
    160  1.1  christos 			parts = split($f, junk, "\\");
    161  1.1  christos 			l = l - (parts - 1);
    162  1.1  christos 			nwords++;
    163  1.1  christos 			words[$f, 2] = l;
    164  1.1  christos 			words[$f, 1] = nwords;
    165  1.1  christos 			wordlist[nwords, 1] = $f;
    166  1.1  christos 			wordlist[nwords, 3] = nchars;
    167  1.1  christos 			nchars = nchars + l + 1;
    168  1.1  christos 		}
    169  1.2  pgoyette 		productlen += words[$f, 2] + 1;
    170  1.1  christos 		wordlist[words[$f, 1], 2]++;
    171  1.1  christos 		products[nproducts, i] = words[$f, 1];
    172  1.1  christos 		printf("%s", $f) > hfile
    173  1.1  christos 		if (f < NF)
    174  1.1  christos 			printf(" ") > hfile
    175  1.1  christos 		i++; f++;
    176  1.1  christos 	}
    177  1.1  christos 	if (oparen)
    178  1.1  christos 		printf(")") > hfile
    179  1.1  christos 	if (ocomment)
    180  1.1  christos 		printf(" */") > hfile
    181  1.1  christos 	printf("\n") > hfile
    182  1.1  christos 
    183  1.2  pgoyette 	if (productlen > productmaxlen) {
    184  1.2  pgoyette 		productmaxlen = productlen;
    185  1.2  pgoyette 	}
    186  1.2  pgoyette 
    187  1.1  christos 	next
    188  1.1  christos }
    189  1.1  christos {
    190  1.1  christos 	if ($0 == "")
    191  1.1  christos 		blanklines++
    192  1.1  christos 	print $0 > hfile
    193  1.1  christos 	if (blanklines < 2)
    194  1.1  christos 		print $0 > dfile
    195  1.1  christos }
    196  1.1  christos END {
    197  1.1  christos 	# print out the match tables
    198  1.1  christos 
    199  1.1  christos 	printf("\n") > dfile
    200  1.1  christos 
    201  1.1  christos 	printf("static const uint16_t %s_vendors[] = {\n", prefix) > dfile
    202  1.1  christos 	for (i = 1; i <= nvendors; i++) {
    203  1.1  christos 		printf("\t    %s_VENDOR_%s", PREFIX, vendors[i, 1]) \
    204  1.1  christos 		    > dfile
    205  1.1  christos 
    206  1.1  christos 		j = 3;
    207  1.1  christos 		while ((i, j) in vendors) {
    208  1.1  christos 			printf(", %d",
    209  1.1  christos 			    wordlist[vendors[i, j], 3]) > dfile
    210  1.1  christos #			printf(", %d /* %s */",
    211  1.1  christos #			    wordlist[vendors[i, j], 3],
    212  1.1  christos #			    wordlist[vendors[i, j], 1]) > dfile
    213  1.1  christos 			j++
    214  1.1  christos 		}
    215  1.1  christos 		printf(", 0,\n", sep) > dfile
    216  1.1  christos 	}
    217  1.1  christos 	printf("};\n") > dfile
    218  1.1  christos 
    219  1.1  christos 	printf("\n") > dfile
    220  1.1  christos 
    221  1.1  christos 	printf("static const uint16_t %s_products[] = {\n", prefix) > dfile
    222  1.1  christos 	for (i = 1; i <= nproducts; i++) {
    223  1.1  christos 		printf("\t    %s_VENDOR_%s, %s_PRODUCT_%s_%s, \n",
    224  1.1  christos 		    PREFIX, products[i, 1], PREFIX, products[i, 1],
    225  1.1  christos 		    products[i, 2]) > dfile
    226  1.1  christos 
    227  1.1  christos 		printf("\t    ") > dfile
    228  1.1  christos 		j = 4
    229  1.1  christos 		sep = ""
    230  1.1  christos 		while ((i, j) in products) {
    231  1.1  christos 			printf("%s%d", sep,
    232  1.1  christos 			    wordlist[products[i, j], 3]) > dfile
    233  1.1  christos #			printf("%s%d /* %s */", sep,
    234  1.1  christos #			    wordlist[products[i, j], 3],
    235  1.1  christos #			    wordlist[products[i, j], 1]) > dfile
    236  1.1  christos 			sep = ", "
    237  1.1  christos 			j++
    238  1.1  christos 		}
    239  1.1  christos 		printf("%s0,\n", sep) > dfile
    240  1.1  christos 	}
    241  1.1  christos 	printf("};\n") > dfile
    242  1.1  christos 
    243  1.1  christos 	printf("static const char %s_words[] = { \".\" \n", prefix) > dfile
    244  1.1  christos 	for (i = 1; i <= nwords; i++) {
    245  1.1  christos 		printf("\t    \"%s\\0\" /* %d refs @ %d */\n",
    246  1.1  christos 		    wordlist[i, 1], wordlist[i, 2], wordlist[i, 3]) > dfile
    247  1.1  christos 	}
    248  1.1  christos 	printf("};\n") > dfile
    249  1.1  christos 	printf("const int %s_nwords = %d;\n", prefix, nwords) > dfile
    250  1.1  christos 
    251  1.1  christos 	printf("\n") > dfile
    252  1.1  christos 
    253  1.2  pgoyette 	printf("Maximum vendor string length:  %d\n", vendormaxlen + 1)
    254  1.2  pgoyette 	printf("Maximum product string length: %d\n", productmaxlen + 1)
    255  1.2  pgoyette 	printf("\nEnsure that device-specific values are sufficiently large");
    256  1.2  pgoyette 	printf("\ncheck Makefile.%s for details).\n", FILENAME);
    257  1.2  pgoyette 
    258  1.1  christos 	close(dfile)
    259  1.1  christos 	close(hfile)
    260  1.1  christos }
    261