-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpfp-parser.l
More file actions
231 lines (188 loc) · 3.71 KB
/
pfp-parser.l
File metadata and controls
231 lines (188 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* PCI Finger-Print Parser
*
* Copyright (c) 2016-2020 Alexei A. Smekalkine <ikle@ikle.ru>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
%{
#include <assert.h>
#include <stdlib.h>
#include "pfp-parser.h"
#define YY_DECL struct pfp_rule *pfplex (yyscan_t yyscanner)
static void fatal_error (const char *msg, yyscan_t yyscanner);
#define YY_FATAL_ERROR(msg) fatal_error(msg, yyscanner)
%}
%option reentrant prefix="pfp"
%option yylineno never-interactive
%option nodefault noyywrap
%option noinput
%x COMMENT
%x PATH
%x SLOT SLOT_DEV SLOT_FN
%x CLASS CLASS_IF
%x ID
%x RULE
space [ \t]+
xdigit [0-9a-f]
eq {space}={space}
any .|\n
%%
struct pfp_rule *head = NULL, **tail = &head, *rule = NULL;
struct pfp_sbdf *slot = NULL;
int *id = NULL;
char *p;
BEGIN (INITIAL);
<COMMENT>{
({space}\([^()]+\))?\n {
BEGIN (RULE);
}
{any} {
YY_FATAL_ERROR ("extra characters at end of line");
}
}
<PATH>{
{xdigit}{1,4}(\/[01]?{xdigit}\.[0-7])* {
rule->path = strdup (yytext);
BEGIN (COMMENT);
}
}
<SLOT>{
{xdigit}{1,4}:{xdigit}{1,2}: {
assert (slot != NULL);
slot->segment = strtol (yytext, &p, 16);
slot->bus = strtol (p + 1, NULL, 16);
BEGIN (SLOT_DEV);
}
{xdigit}{1,2}: {
assert (slot != NULL);
slot->segment = 0;
slot->bus = strtol (yytext, NULL, 16);
BEGIN (SLOT_DEV);
}
{any} {
unput (yytext[0]);
assert (slot != NULL);
slot->segment = 0;
slot->bus = 0;
BEGIN (SLOT_DEV);
}
}
<SLOT_DEV>{
[01]?{xdigit}\. {
assert (slot != NULL);
slot->device = strtol (yytext, NULL, 16);
BEGIN (SLOT_FN);
}
{any} {
YY_FATAL_ERROR ("PCI device number expected");
}
}
<SLOT_FN>{
[0-7] {
assert (slot != NULL);
slot->function = strtol (yytext, NULL, 8);
BEGIN (COMMENT);
}
{any} {
YY_FATAL_ERROR ("PCI device function expected");
}
}
<CLASS>{
{xdigit}{4} {
rule->class = strtol (yytext, NULL, 16);
BEGIN (CLASS_IF);
}
{any} {
YY_FATAL_ERROR ("PCI class expected");
}
}
<CLASS_IF>{
\.{xdigit}{1,2} {
rule->interface = strtol (yytext + 1, NULL, 16);
BEGIN (COMMENT);
}
{any} {
unput (yytext[0]);
rule->interface = -1;
BEGIN (COMMENT);
}
}
<ID>{
{xdigit}{4} {
assert (id != NULL);
*id = strtol (yytext, NULL, 16);
BEGIN (COMMENT);
}
{any} {
YY_FATAL_ERROR ("PCI identifier expected");
}
}
<RULE>{
#.+\n /* line comment */
path{eq} BEGIN (PATH);
parent{eq} slot = &rule->parent; BEGIN (SLOT);
slot{eq} slot = &rule->slot; BEGIN (SLOT);
class{eq} BEGIN (CLASS);
vendor{eq} id = &rule->vendor; BEGIN (ID);
device{eq} id = &rule->device; BEGIN (ID);
svendor{eq} id = &rule->svendor; BEGIN (ID);
sdevice{eq} id = &rule->sdevice; BEGIN (ID);
<<EOF>> return head;
\n BEGIN (INITIAL);
{any} YY_FATAL_ERROR ("unrecognized rule line");
}
<INITIAL>{
#.+\n /* line comment */
\n /* empty line */
{any} {
if ((rule = pfp_rule_alloc ()) == NULL) {
pfp_rule_free (head);
return NULL;
}
*tail = rule;
tail = &rule->next;
unput (yytext[0]);
BEGIN (RULE);
}
}
%%
static void fatal_error (const char *msg, yyscan_t yyscanner)
{
struct yyguts_t *yyg = yyscanner;
fprintf (stderr, "E:%d: %s\n", yylineno, msg);
exit (1);
}
struct pfp_parser *pfp_parser_alloc (FILE *from)
{
yyscan_t s;
if (yylex_init (&s) != 0)
return NULL;
yyset_in (from, s);
return s;
}
void pfp_parser_free (struct pfp_parser *o)
{
if (o == NULL)
return;
yylex_destroy (o);
}
struct pfp_rule *pfp_parser_run (struct pfp_parser *o)
{
return yylex (o);
}
void pfp_parser_reset (struct pfp_parser *o, FILE *from)
{
yyrestart (from, o);
}
/* all in one */
struct pfp_rule *pfp_parse (FILE *from)
{
struct pfp_parser *p;
struct pfp_rule *r;
if ((p = pfp_parser_alloc (from)) == NULL)
return NULL;
r = pfp_parser_run (p);
pfp_parser_free (p);
return r;
}