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