acpi_quirks2h.awk revision 131311
1#!/usr/bin/awk -f
2#
3# $FreeBSD: head/sys/tools/acpi_quirks2h.awk 131311 2004-06-30 04:40:20Z 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_q_rule %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",
95	    TABLE, CREATOR) > OUTPUT;
96}
97
98#
99# OEM REVISION field
100#
101$1 == "oem_rev:" {
102	TABLE = $2;
103	SIGN = $3;
104	VALUE = $4;
105
106	# Parse operand
107	OPERAND = trans_sign(SIGN);
108
109	printf("\t{ ACPI_TABLE_%s, OEM_REV, {.op = %s}, {.rev = %s} },\n",
110	    TABLE, OPERAND, VALUE) > OUTPUT;
111}
112
113#
114# CREATOR REVISION field
115#
116$1 == "creator_rev:" {
117	TABLE = $2;
118	SIGN = $3;
119	VALUE = $4;
120
121	# Parse operand
122	OPERAND = trans_sign(SIGN);
123
124	printf("\t{ ACPI_TABLE_%s, CREATOR_REV, {.op = %s}, {.rev = %s} },\n",
125	    TABLE, OPERAND, VALUE) > OUTPUT;
126}
127
128#
129# QUIRKS field: This is the last line of every entry
130#
131$1 == "quirks:" {
132	printf("\t{ ACPI_TABLE_END }\n};\n\n") > OUTPUT;
133
134	QUIRKS = $0;
135	sub(/^quirks:[ ]*/ , "", QUIRKS);
136
137	QUIRK_COUNT++;
138	QUIRK_LIST[QUIRK_COUNT] = QUIRKS;
139	QUIRK_NAME[QUIRK_COUNT] = ENTRY_NAME;
140}
141
142#
143# All information is gathered, now create acpi_quirks_table
144#
145END {
146	# Header
147	printf("const struct acpi_q_entry acpi_quirks_table[] = {\n") \
148	    > OUTPUT;
149
150	# Array of all quirks
151	for (i = 1; i <= QUIRK_COUNT; i++) {
152		printf("\t{ %s, %s },\n", QUIRK_NAME[i], QUIRK_LIST[i]) \
153		    > OUTPUT;
154	}
155
156	# Footer
157	printf("\t{ NULL, 0 }\n") > OUTPUT;
158	printf("};\n") > OUTPUT;
159
160	exit(0);
161}
162
163#
164# Translate math SIGN into verbal OPERAND
165#
166function trans_sign(TMP_SIGN)
167{
168	if (TMP_SIGN == "=")
169		TMP_OPERAND = "OP_EQL";
170	else if (TMP_SIGN == "!=")
171		TMP_OPERAND = "OP_NEQ";
172	else if (TMP_SIGN == "<=")
173		TMP_OPERAND = "OP_LEQ";
174	else if (TMP_SIGN == ">=")
175		TMP_OPERAND = "OP_GEQ";
176	else if (TMP_SIGN == ">")
177		TMP_OPERAND = "OP_GTR";
178	else if (TMP_SIGN == "<")
179		TMP_OPERAND = "OP_LES";
180	else {
181		printf("error: unknown sign: " TMP_SIGN "\n");
182		exit(1);
183	}
184
185	return (TMP_OPERAND);
186}
187