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