1131037Snjl#!/usr/bin/awk -f
2131037Snjl#
3131037Snjl# $FreeBSD$
4139825Simp
5139825Simp#-
6131037Snjl# Copyright (c) 2004 Mark Santcroos <marks@ripe.net>
7131037Snjl# All rights reserved.
8131037Snjl#
9131037Snjl# Redistribution and use in source and binary forms, with or without
10131037Snjl# modification, are permitted provided that the following conditions
11131037Snjl# are met:
12131037Snjl# 1. Redistributions of source code must retain the above copyright
13131037Snjl#    notice, this list of conditions and the following disclaimer.
14131037Snjl# 2. Redistributions in binary form must reproduce the above copyright
15131037Snjl#    notice, this list of conditions and the following disclaimer in the
16131037Snjl#    documentation and/or other materials provided with the distribution.
17131037Snjl#
18131037Snjl# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19131037Snjl# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20131037Snjl# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21131037Snjl# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22131037Snjl# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23131037Snjl# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24131037Snjl# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25131037Snjl# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26131037Snjl# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27131037Snjl# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28131037Snjl# SUCH DAMAGE.
29131037Snjl#
30131037Snjl
31131037SnjlBEGIN {
32131037Snjl	OUTPUT="acpi_quirks.h"
33131037Snjl}
34131037Snjl
35131037Snjl# Print header and id
36131037SnjlNR == 1 {
37131037Snjl	VERSION = $0;
38131037Snjl	gsub("\^# ", "", VERSION)
39131037Snjl	gsub("\\$", "", VERSION)
40131037Snjl
41131037Snjl	printf("/*\n") > OUTPUT;
42131037Snjl	printf(" * THIS FILE IS AUTOMAGICALLY GENERATED.  DO NOT EDIT.\n") \
43131037Snjl	    > OUTPUT;
44131037Snjl	printf(" *\n") > OUTPUT;
45131037Snjl	printf(" * Generated from:\n") > OUTPUT;
46131037Snjl	printf(" * %s\n", VERSION) > OUTPUT;
47131037Snjl	printf(" */\n\n") > OUTPUT;
48131037Snjl}
49131037Snjl
50131037Snjl# Ignore comments and empty lines
51131037Snjl/^#/, NF == 0 {
52131037Snjl}
53131037Snjl
54131037Snjl#
55131037Snjl# NAME field: this is the first line of every entry
56131037Snjl#
57131037Snjl$1 == "name:" {
58131037Snjl	ENTRY_NAME = $2;
59131311Snjl	printf("const struct acpi_q_rule %s[] = {\n", ENTRY_NAME) > OUTPUT;
60131037Snjl}
61131037Snjl
62131037Snjl#
63131037Snjl# OEM field
64131037Snjl#
65131037Snjl$1 == "oem:" {
66131037Snjl	LENGTH = length();
67131037Snjl
68131037Snjl	# Parse table type to match
69131037Snjl	TABLE = $2;
70131037Snjl
71131037Snjl	# Parse OEM ID
72131037Snjl	M = match ($0, /\"[^\"]*\"/);
73131037Snjl	OEM_ID = substr($0, M, RLENGTH);
74131037Snjl
75131037Snjl	# Parse OEM Table ID
76131037Snjl	ANCHOR = LENGTH - (M + RLENGTH - 1);
77131037Snjl	REMAINDER = substr($0, M + RLENGTH, ANCHOR);
78131037Snjl	M = match (REMAINDER, /\"[^\"]*\"/);
79131037Snjl	OEM_TABLE_ID = substr(REMAINDER, M, RLENGTH);
80131037Snjl
81167814Sjkim	printf("\t{ \"%s\", OEM, {%s}, {%s} },\n",
82131037Snjl	    TABLE, OEM_ID, OEM_TABLE_ID) > OUTPUT;
83131037Snjl}
84131037Snjl
85131037Snjl#
86131037Snjl# CREATOR field
87131037Snjl#
88131037Snjl$1 == "creator:" {
89131037Snjl	# Parse table type to match
90131037Snjl	TABLE = $2;
91131037Snjl
92131037Snjl	M = match ($0, /\"[^\"]*\"/);
93131037Snjl	CREATOR = substr($0, M, RLENGTH);
94131037Snjl
95167814Sjkim	printf("\t{ \"%s\", CREATOR, {%s} },\n",
96131311Snjl	    TABLE, CREATOR) > OUTPUT;
97131037Snjl}
98131037Snjl
99131037Snjl#
100131037Snjl# OEM REVISION field
101131037Snjl#
102131037Snjl$1 == "oem_rev:" {
103131311Snjl	TABLE = $2;
104131037Snjl	SIGN = $3;
105131037Snjl	VALUE = $4;
106131037Snjl
107131037Snjl	# Parse operand
108131037Snjl	OPERAND = trans_sign(SIGN);
109131037Snjl
110167814Sjkim	printf("\t{ \"%s\", OEM_REV, {.op = %s}, {.rev = %s} },\n",
111131311Snjl	    TABLE, OPERAND, VALUE) > OUTPUT;
112131037Snjl}
113131037Snjl
114131037Snjl#
115131037Snjl# CREATOR REVISION field
116131037Snjl#
117131037Snjl$1 == "creator_rev:" {
118131311Snjl	TABLE = $2;
119131037Snjl	SIGN = $3;
120131037Snjl	VALUE = $4;
121131037Snjl
122131037Snjl	# Parse operand
123131037Snjl	OPERAND = trans_sign(SIGN);
124131037Snjl
125167814Sjkim	printf("\t{ \"%s\", CREATOR_REV, {.op = %s}, {.rev = %s} },\n",
126131311Snjl	    TABLE, OPERAND, VALUE) > OUTPUT;
127131037Snjl}
128131037Snjl
129131037Snjl#
130131037Snjl# QUIRKS field: This is the last line of every entry
131131037Snjl#
132131037Snjl$1 == "quirks:" {
133167814Sjkim	printf("\t{ \"\" }\n};\n\n") > OUTPUT;
134131037Snjl
135131037Snjl	QUIRKS = $0;
136131037Snjl	sub(/^quirks:[ ]*/ , "", QUIRKS);
137131037Snjl
138131037Snjl	QUIRK_COUNT++;
139131037Snjl	QUIRK_LIST[QUIRK_COUNT] = QUIRKS;
140131037Snjl	QUIRK_NAME[QUIRK_COUNT] = ENTRY_NAME;
141131037Snjl}
142131037Snjl
143131037Snjl#
144131037Snjl# All information is gathered, now create acpi_quirks_table
145131037Snjl#
146131037SnjlEND {
147131037Snjl	# Header
148131311Snjl	printf("const struct acpi_q_entry acpi_quirks_table[] = {\n") \
149131037Snjl	    > OUTPUT;
150131037Snjl
151131037Snjl	# Array of all quirks
152131037Snjl	for (i = 1; i <= QUIRK_COUNT; i++) {
153131037Snjl		printf("\t{ %s, %s },\n", QUIRK_NAME[i], QUIRK_LIST[i]) \
154131037Snjl		    > OUTPUT;
155131037Snjl	}
156131037Snjl
157131037Snjl	# Footer
158131037Snjl	printf("\t{ NULL, 0 }\n") > OUTPUT;
159131037Snjl	printf("};\n") > OUTPUT;
160131037Snjl
161131037Snjl	exit(0);
162131037Snjl}
163131037Snjl
164131037Snjl#
165131037Snjl# Translate math SIGN into verbal OPERAND
166131037Snjl#
167131037Snjlfunction trans_sign(TMP_SIGN)
168131037Snjl{
169131037Snjl	if (TMP_SIGN == "=")
170131037Snjl		TMP_OPERAND = "OP_EQL";
171131037Snjl	else if (TMP_SIGN == "!=")
172131037Snjl		TMP_OPERAND = "OP_NEQ";
173131037Snjl	else if (TMP_SIGN == "<=")
174131037Snjl		TMP_OPERAND = "OP_LEQ";
175131037Snjl	else if (TMP_SIGN == ">=")
176131037Snjl		TMP_OPERAND = "OP_GEQ";
177131037Snjl	else if (TMP_SIGN == ">")
178131037Snjl		TMP_OPERAND = "OP_GTR";
179131037Snjl	else if (TMP_SIGN == "<")
180131037Snjl		TMP_OPERAND = "OP_LES";
181131037Snjl	else {
182131037Snjl		printf("error: unknown sign: " TMP_SIGN "\n");
183131037Snjl		exit(1);
184131037Snjl	}
185131037Snjl
186131037Snjl	return (TMP_OPERAND);
187131037Snjl}
188