xref: /f-stack/freebsd/tools/sdiodevs2h.awk (revision 22ce4aff)
1*22ce4affSfengbojiang#! /usr/bin/awk -f
2*22ce4affSfengbojiang#-
3*22ce4affSfengbojiang#	$NetBSD: usb/devlist2h.awk,v 1.9 2001/01/18 20:28:22 jdolecek Exp $
4*22ce4affSfengbojiang#  $FreeBSD$
5*22ce4affSfengbojiang#
6*22ce4affSfengbojiang# SPDX-License-Identifier: BSD-4-Clause
7*22ce4affSfengbojiang#
8*22ce4affSfengbojiang# Copyright (c) 1995, 1996 Christopher G. Demetriou
9*22ce4affSfengbojiang# All rights reserved.
10*22ce4affSfengbojiang#
11*22ce4affSfengbojiang# Redistribution and use in source and binary forms, with or without
12*22ce4affSfengbojiang# modification, are permitted provided that the following conditions
13*22ce4affSfengbojiang# are met:
14*22ce4affSfengbojiang# 1. Redistributions of source code must retain the above copyright
15*22ce4affSfengbojiang#    notice, this list of conditions and the following disclaimer.
16*22ce4affSfengbojiang# 2. Redistributions in binary form must reproduce the above copyright
17*22ce4affSfengbojiang#    notice, this list of conditions and the following disclaimer in the
18*22ce4affSfengbojiang#    documentation and/or other materials provided with the distribution.
19*22ce4affSfengbojiang# 3. All advertising materials mentioning features or use of this software
20*22ce4affSfengbojiang#    must display the following acknowledgement:
21*22ce4affSfengbojiang#      This product includes software developed by Christopher G. Demetriou.
22*22ce4affSfengbojiang# 4. The name of the author may not be used to endorse or promote products
23*22ce4affSfengbojiang#    derived from this software without specific prior written permission
24*22ce4affSfengbojiang#
25*22ce4affSfengbojiang# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26*22ce4affSfengbojiang# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27*22ce4affSfengbojiang# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28*22ce4affSfengbojiang# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29*22ce4affSfengbojiang# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30*22ce4affSfengbojiang# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31*22ce4affSfengbojiang# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32*22ce4affSfengbojiang# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33*22ce4affSfengbojiang# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34*22ce4affSfengbojiang# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*22ce4affSfengbojiang#
36*22ce4affSfengbojiang
37*22ce4affSfengbojiangfunction usage()
38*22ce4affSfengbojiang{
39*22ce4affSfengbojiang	print "usage: sdiodevs2h.awk <srcfile> [-d|-h]";
40*22ce4affSfengbojiang	exit 1;
41*22ce4affSfengbojiang}
42*22ce4affSfengbojiang
43*22ce4affSfengbojiangfunction header(file)
44*22ce4affSfengbojiang{
45*22ce4affSfengbojiang	printf("/*\n") > file
46*22ce4affSfengbojiang	printf(" * THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
47*22ce4affSfengbojiang	    > file
48*22ce4affSfengbojiang	printf(" *\n") > file
49*22ce4affSfengbojiang	printf(" * generated from:\n") > file
50*22ce4affSfengbojiang	printf(" *\t%s\n", VERSION) > file
51*22ce4affSfengbojiang	printf(" */\n") > file
52*22ce4affSfengbojiang}
53*22ce4affSfengbojiang
54*22ce4affSfengbojiangfunction vendor(hfile)
55*22ce4affSfengbojiang{
56*22ce4affSfengbojiang	nvendors++
57*22ce4affSfengbojiang
58*22ce4affSfengbojiang	vendorindex[$2] = nvendors;		# record index for this name, for later.
59*22ce4affSfengbojiang	vendors[nvendors, 1] = $2;		# name
60*22ce4affSfengbojiang	vendors[nvendors, 2] = $3;		# id
61*22ce4affSfengbojiang	if (hfile)
62*22ce4affSfengbojiang		printf("#define\tSDIO_VENDOR_%s\t%s\t", vendors[nvendors, 1],
63*22ce4affSfengbojiang		    vendors[nvendors, 2]) > hfile
64*22ce4affSfengbojiang	i = 3; f = 4;
65*22ce4affSfengbojiang
66*22ce4affSfengbojiang	# comments
67*22ce4affSfengbojiang	ocomment = oparen = 0
68*22ce4affSfengbojiang	if (f <= NF) {
69*22ce4affSfengbojiang		if (hfile)
70*22ce4affSfengbojiang			printf("\t/* ") > hfile
71*22ce4affSfengbojiang		ocomment = 1;
72*22ce4affSfengbojiang	}
73*22ce4affSfengbojiang	while (f <= NF) {
74*22ce4affSfengbojiang		if ($f == "#") {
75*22ce4affSfengbojiang			if (hfile)
76*22ce4affSfengbojiang				printf("(") > hfile
77*22ce4affSfengbojiang			oparen = 1
78*22ce4affSfengbojiang			f++
79*22ce4affSfengbojiang			continue
80*22ce4affSfengbojiang		}
81*22ce4affSfengbojiang		if (oparen) {
82*22ce4affSfengbojiang			if (hfile)
83*22ce4affSfengbojiang				printf("%s", $f) > hfile
84*22ce4affSfengbojiang			if (f < NF && hfile)
85*22ce4affSfengbojiang				printf(" ") > hfile
86*22ce4affSfengbojiang			f++
87*22ce4affSfengbojiang			continue
88*22ce4affSfengbojiang		}
89*22ce4affSfengbojiang		vendors[nvendors, i] = $f
90*22ce4affSfengbojiang		if (hfile)
91*22ce4affSfengbojiang			printf("%s", vendors[nvendors, i]) > hfile
92*22ce4affSfengbojiang		if (f < NF && hfile)
93*22ce4affSfengbojiang			printf(" ") > hfile
94*22ce4affSfengbojiang		i++; f++;
95*22ce4affSfengbojiang	}
96*22ce4affSfengbojiang	if (oparen && hfile)
97*22ce4affSfengbojiang		printf(")") > hfile
98*22ce4affSfengbojiang	if (ocomment && hfile)
99*22ce4affSfengbojiang		printf(" */") > hfile
100*22ce4affSfengbojiang	if (hfile)
101*22ce4affSfengbojiang		printf("\n") > hfile
102*22ce4affSfengbojiang}
103*22ce4affSfengbojiang
104*22ce4affSfengbojiangfunction product(hfile)
105*22ce4affSfengbojiang{
106*22ce4affSfengbojiang	nproducts++
107*22ce4affSfengbojiang
108*22ce4affSfengbojiang	products[nproducts, 1] = $2;		# vendor name
109*22ce4affSfengbojiang	products[nproducts, 2] = $3;		# product id
110*22ce4affSfengbojiang	products[nproducts, 3] = $4;		# id
111*22ce4affSfengbojiang	if (hfile)
112*22ce4affSfengbojiang		printf("#define\tSDIO_PRODUCT_%s_%s\t%s\t", \
113*22ce4affSfengbojiang		  products[nproducts, 1], products[nproducts, 2], \
114*22ce4affSfengbojiang		  products[nproducts, 3]) > hfile
115*22ce4affSfengbojiang
116*22ce4affSfengbojiang	i=4; f = 5;
117*22ce4affSfengbojiang
118*22ce4affSfengbojiang	# comments
119*22ce4affSfengbojiang	ocomment = oparen = 0
120*22ce4affSfengbojiang	if (f <= NF) {
121*22ce4affSfengbojiang		if (hfile)
122*22ce4affSfengbojiang			printf("\t/* ") > hfile
123*22ce4affSfengbojiang		ocomment = 1;
124*22ce4affSfengbojiang	}
125*22ce4affSfengbojiang	while (f <= NF) {
126*22ce4affSfengbojiang		if ($f == "#") {
127*22ce4affSfengbojiang			if (hfile)
128*22ce4affSfengbojiang				printf("(") > hfile
129*22ce4affSfengbojiang			oparen = 1
130*22ce4affSfengbojiang			f++
131*22ce4affSfengbojiang			continue
132*22ce4affSfengbojiang		}
133*22ce4affSfengbojiang		if (oparen) {
134*22ce4affSfengbojiang			if (hfile)
135*22ce4affSfengbojiang				printf("%s", $f) > hfile
136*22ce4affSfengbojiang			if (f < NF && hfile)
137*22ce4affSfengbojiang				printf(" ") > hfile
138*22ce4affSfengbojiang			f++
139*22ce4affSfengbojiang			continue
140*22ce4affSfengbojiang		}
141*22ce4affSfengbojiang		products[nproducts, i] = $f
142*22ce4affSfengbojiang		if (hfile)
143*22ce4affSfengbojiang			printf("%s", products[nproducts, i]) > hfile
144*22ce4affSfengbojiang		if (f < NF && hfile)
145*22ce4affSfengbojiang			printf(" ") > hfile
146*22ce4affSfengbojiang		i++; f++;
147*22ce4affSfengbojiang	}
148*22ce4affSfengbojiang	if (oparen && hfile)
149*22ce4affSfengbojiang		printf(")") > hfile
150*22ce4affSfengbojiang	if (ocomment && hfile)
151*22ce4affSfengbojiang		printf(" */") > hfile
152*22ce4affSfengbojiang	if (hfile)
153*22ce4affSfengbojiang		printf("\n") > hfile
154*22ce4affSfengbojiang}
155*22ce4affSfengbojiang
156*22ce4affSfengbojiangfunction dump_dfile(dfile)
157*22ce4affSfengbojiang{
158*22ce4affSfengbojiang	printf("\n") > dfile
159*22ce4affSfengbojiang	printf("const struct sdio_knowndev sdio_knowndevs[] = {\n") > dfile
160*22ce4affSfengbojiang	for (i = 1; i <= nproducts; i++) {
161*22ce4affSfengbojiang		printf("\t{\n") > dfile
162*22ce4affSfengbojiang		printf("\t    SDIO_VENDOR_%s, SDIO_PRODUCT_%s_%s,\n",
163*22ce4affSfengbojiang		    products[i, 1], products[i, 1], products[i, 2]) > dfile
164*22ce4affSfengbojiang		printf("\t    ") > dfile
165*22ce4affSfengbojiang		printf("0") > dfile
166*22ce4affSfengbojiang		printf(",\n") > dfile
167*22ce4affSfengbojiang
168*22ce4affSfengbojiang		vendi = vendorindex[products[i, 1]];
169*22ce4affSfengbojiang		printf("\t    \"") > dfile
170*22ce4affSfengbojiang		j = 3;
171*22ce4affSfengbojiang		needspace = 0;
172*22ce4affSfengbojiang		while (vendors[vendi, j] != "") {
173*22ce4affSfengbojiang			if (needspace)
174*22ce4affSfengbojiang				printf(" ") > dfile
175*22ce4affSfengbojiang			printf("%s", vendors[vendi, j]) > dfile
176*22ce4affSfengbojiang			needspace = 1
177*22ce4affSfengbojiang			j++
178*22ce4affSfengbojiang		}
179*22ce4affSfengbojiang		printf("\",\n") > dfile
180*22ce4affSfengbojiang
181*22ce4affSfengbojiang		printf("\t    \"") > dfile
182*22ce4affSfengbojiang		j = 4;
183*22ce4affSfengbojiang		needspace = 0;
184*22ce4affSfengbojiang		while (products[i, j] != "") {
185*22ce4affSfengbojiang			if (needspace)
186*22ce4affSfengbojiang				printf(" ") > dfile
187*22ce4affSfengbojiang			printf("%s", products[i, j]) > dfile
188*22ce4affSfengbojiang			needspace = 1
189*22ce4affSfengbojiang			j++
190*22ce4affSfengbojiang		}
191*22ce4affSfengbojiang		printf("\",\n") > dfile
192*22ce4affSfengbojiang		printf("\t},\n") > dfile
193*22ce4affSfengbojiang	}
194*22ce4affSfengbojiang	for (i = 1; i <= nvendors; i++) {
195*22ce4affSfengbojiang		printf("\t{\n") > dfile
196*22ce4affSfengbojiang		printf("\t    SDIO_VENDOR_%s, 0,\n", vendors[i, 1]) > dfile
197*22ce4affSfengbojiang		printf("\t    SDIO_KNOWNDEV_NOPROD,\n") > dfile
198*22ce4affSfengbojiang		printf("\t    \"") > dfile
199*22ce4affSfengbojiang		j = 3;
200*22ce4affSfengbojiang		needspace = 0;
201*22ce4affSfengbojiang		while (vendors[i, j] != "") {
202*22ce4affSfengbojiang			if (needspace)
203*22ce4affSfengbojiang				printf(" ") > dfile
204*22ce4affSfengbojiang			printf("%s", vendors[i, j]) > dfile
205*22ce4affSfengbojiang			needspace = 1
206*22ce4affSfengbojiang			j++
207*22ce4affSfengbojiang		}
208*22ce4affSfengbojiang		printf("\",\n") > dfile
209*22ce4affSfengbojiang		printf("\t    NULL,\n") > dfile
210*22ce4affSfengbojiang		printf("\t},\n") > dfile
211*22ce4affSfengbojiang	}
212*22ce4affSfengbojiang	printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
213*22ce4affSfengbojiang	printf("};\n") > dfile
214*22ce4affSfengbojiang}
215*22ce4affSfengbojiang
216*22ce4affSfengbojiangBEGIN {
217*22ce4affSfengbojiang
218*22ce4affSfengbojiangnproducts = nvendors = 0
219*22ce4affSfengbojiang# Process the command line
220*22ce4affSfengbojiangfor (i = 1; i < ARGC; i++) {
221*22ce4affSfengbojiang	arg = ARGV[i];
222*22ce4affSfengbojiang	if (arg !~ /^-[dh]+$/ && arg !~ /devs$/)
223*22ce4affSfengbojiang		usage();
224*22ce4affSfengbojiang	if (arg ~ /^-.*d/)
225*22ce4affSfengbojiang		dfile="sdiodevs_data.h"
226*22ce4affSfengbojiang	if (arg ~ /^-.*h/)
227*22ce4affSfengbojiang		hfile="sdiodevs.h"
228*22ce4affSfengbojiang	if (arg ~ /devs$/)
229*22ce4affSfengbojiang		srcfile = arg;
230*22ce4affSfengbojiang}
231*22ce4affSfengbojiangARGC = 1;
232*22ce4affSfengbojiangline=0;
233*22ce4affSfengbojiang
234*22ce4affSfengbojiangwhile ((getline < srcfile) > 0) {
235*22ce4affSfengbojiang	line++;
236*22ce4affSfengbojiang	if (line == 1) {
237*22ce4affSfengbojiang		VERSION = $0
238*22ce4affSfengbojiang		gsub("\\$", "", VERSION)
239*22ce4affSfengbojiang		if (dfile)
240*22ce4affSfengbojiang			header(dfile)
241*22ce4affSfengbojiang		if (hfile)
242*22ce4affSfengbojiang			header(hfile)
243*22ce4affSfengbojiang		continue;
244*22ce4affSfengbojiang	}
245*22ce4affSfengbojiang	if ($1 == "vendor") {
246*22ce4affSfengbojiang		vendor(hfile)
247*22ce4affSfengbojiang		continue
248*22ce4affSfengbojiang	}
249*22ce4affSfengbojiang	if ($1 == "product") {
250*22ce4affSfengbojiang		product(hfile)
251*22ce4affSfengbojiang		continue
252*22ce4affSfengbojiang	}
253*22ce4affSfengbojiang	if ($0 == "")
254*22ce4affSfengbojiang		blanklines++
255*22ce4affSfengbojiang	if (hfile)
256*22ce4affSfengbojiang		print $0 > hfile
257*22ce4affSfengbojiang	if (blanklines < 2 && dfile)
258*22ce4affSfengbojiang	    print $0 > dfile
259*22ce4affSfengbojiang}
260*22ce4affSfengbojiang
261*22ce4affSfengbojiang# print out the match tables
262*22ce4affSfengbojiang
263*22ce4affSfengbojiangif (dfile)
264*22ce4affSfengbojiang	dump_dfile(dfile)
265*22ce4affSfengbojiang}
266