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