acpi_quirks2h.awk revision 131037
1#!/usr/bin/awk -f
2#
3# $FreeBSD: head/sys/tools/acpi_quirks2h.awk 131037 2004-06-24 06:28:05Z njl $
4#
5# Copyright (c) 2004 Mark Santcroos <marks@ripe.net>
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#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30BEGIN {
31	OUTPUT="acpi_quirks.h"
32}
33
34# Print header and id
35NR == 1 {
36	VERSION = $0;
37	gsub("\^# ", "", VERSION)
38	gsub("\\$", "", VERSION)
39
40	printf("/*\n") > OUTPUT;
41	printf(" * THIS FILE IS AUTOMAGICALLY GENERATED.  DO NOT EDIT.\n") \
42	    > OUTPUT;
43	printf(" *\n") > OUTPUT;
44	printf(" * Generated from:\n") > OUTPUT;
45	printf(" * %s\n", VERSION) > OUTPUT;
46	printf(" */\n\n") > OUTPUT;
47}
48
49# Ignore comments and empty lines
50/^#/, NF == 0 {
51}
52
53#
54# NAME field: this is the first line of every entry
55#
56$1 == "name:" {
57	ENTRY_NAME = $2;
58	printf("const struct acpi_table %s[] = {\n", ENTRY_NAME) > OUTPUT;
59}
60
61#
62# OEM field
63#
64$1 == "oem:" {
65	LENGTH = length();
66
67	# Parse table type to match
68	TABLE = $2;
69
70	# Parse OEM ID
71	M = match ($0, /\"[^\"]*\"/);
72	OEM_ID = substr($0, M, RLENGTH);
73
74	# Parse OEM Table ID
75	ANCHOR = LENGTH - (M + RLENGTH - 1);
76	REMAINDER = substr($0, M + RLENGTH, ANCHOR);
77	M = match (REMAINDER, /\"[^\"]*\"/);
78	OEM_TABLE_ID = substr(REMAINDER, M, RLENGTH);
79
80	printf("\t{ ACPI_TABLE_%s, OEM, %s, %s },\n",
81	    TABLE, OEM_ID, OEM_TABLE_ID) > OUTPUT;
82}
83
84#
85# CREATOR field
86#
87$1 == "creator:" {
88	# Parse table type to match
89	TABLE = $2;
90
91	M = match ($0, /\"[^\"]*\"/);
92	CREATOR = substr($0, M, RLENGTH);
93
94	printf("\t{ ACPI_TABLE_%s, CREATOR, %s },\n", TABLE, CREATOR) > OUTPUT;
95}
96
97#
98# OEM REVISION field
99#
100$1 == "oem_rev:" {
101	TYPE = $2;
102	SIGN = $3;
103	VALUE = $4;
104
105	# Parse operand
106	OPERAND = trans_sign(SIGN);
107
108	# Parse OEM revision ID
109	REV = "OEM_REV";
110
111	printf("\t{ ACPI_TABLE_%s, %s, %s, %s },\n",
112	    TYPE, REV, OPERAND, VALUE) > OUTPUT;
113}
114
115#
116# CREATOR REVISION field
117#
118$1 == "creator_rev:" {
119	TYPE = $2;
120	SIGN = $3;
121	VALUE = $4;
122
123	# Parse operand
124	OPERAND = trans_sign(SIGN);
125
126	# Parse creator revision ID
127	REV = "CREATOR_REV";
128
129	printf("\t{ ACPI_TABLE_%s, %s, %s, %s },\n",
130	    TYPE, REV, OPERAND, VALUE) > OUTPUT;
131}
132
133#
134# QUIRKS field: This is the last line of every entry
135#
136$1 == "quirks:" {
137	printf("\t{ ACPI_TABLE_END }\n};\n\n") > OUTPUT;
138
139	QUIRKS = $0;
140	sub(/^quirks:[ ]*/ , "", QUIRKS);
141
142	QUIRK_COUNT++;
143	QUIRK_LIST[QUIRK_COUNT] = QUIRKS;
144	QUIRK_NAME[QUIRK_COUNT] = ENTRY_NAME;
145}
146
147#
148# All information is gathered, now create acpi_quirks_table
149#
150END {
151	# Header
152	printf("const struct acpi_blacklist acpi_quirks_table[] = {\n") \
153	    > OUTPUT;
154
155	# Array of all quirks
156	for (i = 1; i <= QUIRK_COUNT; i++) {
157		printf("\t{ %s, %s },\n", QUIRK_NAME[i], QUIRK_LIST[i]) \
158		    > OUTPUT;
159	}
160
161	# Footer
162	printf("\t{ NULL, 0 }\n") > OUTPUT;
163	printf("};\n") > OUTPUT;
164
165	exit(0);
166}
167
168#
169# Translate math SIGN into verbal OPERAND
170#
171function trans_sign(TMP_SIGN)
172{
173	if (TMP_SIGN == "=")
174		TMP_OPERAND = "OP_EQL";
175	else if (TMP_SIGN == "!=")
176		TMP_OPERAND = "OP_NEQ";
177	else if (TMP_SIGN == "<=")
178		TMP_OPERAND = "OP_LEQ";
179	else if (TMP_SIGN == ">=")
180		TMP_OPERAND = "OP_GEQ";
181	else if (TMP_SIGN == ">")
182		TMP_OPERAND = "OP_GTR";
183	else if (TMP_SIGN == "<")
184		TMP_OPERAND = "OP_LES";
185	else {
186		printf("error: unknown sign: " TMP_SIGN "\n");
187		exit(1);
188	}
189
190	return (TMP_OPERAND);
191}
192