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