xref: /openbsd/sys/dev/pci/devlist2h.awk (revision f2dfb0a4)
1#! /usr/bin/awk -f
2#	$OpenBSD: devlist2h.awk,v 1.3 1997/11/07 08:07:26 niklas Exp $
3#	$NetBSD: devlist2h.awk,v 1.2 1996/01/22 21:08:09 cgd Exp $
4#
5# Copyright (c) 1995, 1996 Christopher G. Demetriou
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16# 3. All advertising materials mentioning features or use of this software
17#    must display the following acknowledgement:
18#      This product includes software developed by Christopher G. Demetriou.
19# 4. The name of the author may not be used to endorse or promote products
20#    derived from this software without specific prior written permission
21#
22# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32#
33BEGIN {
34	nproducts = nvendors = 0
35	dfile="pcidevs_data.h"
36	hfile="pcidevs.h"
37}
38NR == 1 {
39	VERSION = $0
40	gsub("\\$", "", VERSION)
41
42	printf("/*\n") > dfile
43	printf(" * THIS FILE AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
44	    > dfile
45	printf(" *\n") > dfile
46	printf(" * generated from:\n") > dfile
47	printf(" *\t%s\n", VERSION) > dfile
48	printf(" */\n") > dfile
49
50	printf("/*\n") > hfile
51	printf(" * THIS FILE AUTOMATICALLY GENERATED.  DO NOT EDIT.\n") \
52	    > hfile
53	printf(" *\n") > hfile
54	printf(" * generated from:\n") > hfile
55	printf(" *\t%s\n", VERSION) > hfile
56	printf(" */\n") > hfile
57
58	next
59}
60$1 == "vendor" {
61	nvendors++
62
63	vendorindex[$2] = nvendors;		# record index for this name, for later.
64	vendors[nvendors, 1] = $2;		# name
65	vendors[nvendors, 2] = $3;		# id
66	printf("#define\tPCI_VENDOR_%s\t%s\t", vendors[nvendors, 1],
67	    vendors[nvendors, 2]) > hfile
68
69	i = 3; f = 4;
70
71	# comments
72	ocomment = oparen = 0
73	if (f <= NF) {
74		printf("\t/* ") > hfile
75		ocomment = 1;
76	}
77	while (f <= NF) {
78		if ($f == "#") {
79			printf("(") > hfile
80			oparen = 1
81			f++
82			continue
83		}
84		if (oparen) {
85			printf("%s", $f) > hfile
86			if (f < NF)
87				printf(" ") > hfile
88			f++
89			continue
90		}
91		vendors[nvendors, i] = $f
92		printf("%s", vendors[nvendors, i]) > hfile
93		if (f < NF)
94			printf(" ") > hfile
95		i++; f++;
96	}
97	if (oparen)
98		printf(")") > hfile
99	if (ocomment)
100		printf(" */") > hfile
101	printf("\n") > hfile
102
103	next
104}
105$1 == "product" {
106	nproducts++
107
108	products[nproducts, 1] = $2;		# vendor name
109	products[nproducts, 2] = $3;		# product id
110	products[nproducts, 3] = $4;		# id
111	printf("#define\tPCI_PRODUCT_%s_%s\t%s\t", products[nproducts, 1],
112	    products[nproducts, 2], products[nproducts, 3]) > hfile
113
114	i=4; f = 5;
115
116	# comments
117	ocomment = oparen = 0
118	if (f <= NF) {
119		printf("\t/* ") > hfile
120		ocomment = 1;
121	}
122	while (f <= NF) {
123		if ($f == "#") {
124			printf("(") > hfile
125			oparen = 1
126			f++
127			continue
128		}
129		if (oparen) {
130			printf("%s", $f) > hfile
131			if (f < NF)
132				printf(" ") > hfile
133			f++
134			continue
135		}
136		products[nproducts, i] = $f
137		printf("%s", products[nproducts, i]) > hfile
138		if (f < NF)
139			printf(" ") > hfile
140		i++; f++;
141	}
142	if (oparen)
143		printf(")") > hfile
144	if (ocomment)
145		printf(" */") > hfile
146	printf("\n") > hfile
147
148	next
149}
150{
151	if ($0 == "")
152		blanklines++
153	print $0 > hfile
154	if (blanklines < 2)
155		print $0 > dfile
156}
157END {
158	# print out the match tables
159
160	printf("\n") > dfile
161
162	printf("struct pci_knowndev pci_knowndevs[] = {\n") > dfile
163	for (i = 1; i <= nproducts; i++) {
164		printf("\t{\n") > dfile
165		printf("\t    PCI_VENDOR_%s, PCI_PRODUCT_%s_%s,\n",
166		    products[i, 1], products[i, 1], products[i, 2]) \
167		    > dfile
168		printf("\t    ") > dfile
169		printf("0") > dfile
170		printf(",\n") > dfile
171
172		vendi = vendorindex[products[i, 1]];
173		printf("\t    \"") > dfile
174		j = 3;
175		needspace = 0;
176		while (vendors[vendi, j] != "") {
177			if (needspace)
178				printf(" ") > dfile
179			printf("%s", vendors[vendi, j]) > dfile
180			needspace = 1
181			j++
182		}
183		printf("\",\n") > dfile
184
185		printf("\t    \"") > dfile
186		j = 4;
187		needspace = 0;
188		while (products[i, j] != "") {
189			if (needspace)
190				printf(" ") > dfile
191			printf("%s", products[i, j]) > dfile
192			needspace = 1
193			j++
194		}
195		printf("\",\n") > dfile
196		printf("\t},\n") > dfile
197	}
198	for (i = 1; i <= nvendors; i++) {
199		printf("\t{\n") > dfile
200		printf("\t    PCI_VENDOR_%s, 0,\n", vendors[i, 1]) \
201		    > dfile
202		printf("\t    PCI_KNOWNDEV_NOPROD,\n") \
203		    > dfile
204		printf("\t    \"") > dfile
205		j = 3;
206		needspace = 0;
207		while (vendors[i, j] != "") {
208			if (needspace)
209				printf(" ") > dfile
210			printf("%s", vendors[i, j]) > dfile
211			needspace = 1
212			j++
213		}
214		printf("\",\n") > dfile
215		printf("\t    NULL,\n") > dfile
216		printf("\t},\n") > dfile
217	}
218	printf("\t{ 0, 0, 0, NULL, NULL, }\n") > dfile
219	printf("};\n") > dfile
220}
221