-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.c
152 lines (130 loc) · 2.94 KB
/
parse.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
enum subsystem_t {
null = 0,
tty = 1,
usb = 2,
};
enum action_t {
adding = 1,
removing = 2,
};
struct config {
enum subsystem_t subsystem;
enum action_t action;
char idvendor[512];
char idproduct[512];
} configs[512];
#define CONFIG_FILE "./rules"
int parse_token(int line, const char *token, int len)
{
static int counter = 0;
printf("----: %d / %d : %s\n", line, len, token);
return 0;
}
int main(int argc, const char *argv[])
{
int config = open(CONFIG_FILE, O_RDONLY);
if (config == -1) {
printf("Error opening config file %s: %s[%d]\n", CONFIG_FILE, strerror(errno), errno);
return 1;
}
char config_file[4096];
memset(config_file, '\0', 4096);
int r = read(config, config_file, 4096);
if (r == -1) {
printf("Error reading from file %s: %s[%d]\n", CONFIG_FILE, strerror(errno), errno);
return 1;
}
int i = 0;
int j = 0;
int k = 1;
char config_line[1024];
memset(config_line, '\0', 1024);
char tokens[1024];
char token[1024];
memset(tokens, '\0', 1024);
memset(token, '\0', 1024);
int line = 1;
int len_line = 0;
int n = 0;
int t = 0;
int delim = 0;
int c = 0;
int s = 0;
while (config_file[i] != '\0') {
memset(config_line, '\0', 1024);
line = 1;
while (line) {
if (config_file[i] == '\n') {
line = 0;
break;
} else {
config_line[j] = config_file[i];
}
i++;
j++;
}
printf("LINE: n==%d, len==%d: %s\n", k, j, config_line);
len_line = j;
printf("====>>>>\n");
for (n = 0; n < len_line; n++) {
if ((config_line[n] == ' ') && delim) {
delim = 0;
continue;
}
if (config_line[n] == ',') {
delim = 1;
}
tokens[t] = config_line[n];
t++;
}
printf("\tTOKEN LINE:%s\n", tokens);
n = 0; c = 0;
while (n <= t) {
if ((tokens[n] == ',') || (n == t)) {
printf("\t\tTOKEN VALUE:%s[%d]\n", token, c);
// parse_token(k, token, c);
if (memcmp("SUBSYSTEM==\"tty\"", token, 16) == 0) {
printf("MATCH: subsystem TTY\n");
configs[s].subsystem = tty;
} else if (memcmp("ACTION=\"add\"", token, 12) == 0) {
printf("MATCH: action add\n");
configs[s].action = adding;
} else if (memcmp("ACTION=\"remove\"", token, 15) == 0) {
printf("MATCH: action remove\n");
configs[s].action = removing;
} else if (memcmp("ATTRS{idVendor}==", token, 17) == 0) {
printf("MATCH: ATTR(S) idvendor\n");
if ((17 + 1 <= c) && (token[17] == '"') && (token[c-1] == '"')) {
printf("VENDOR==%s\n", token+17);
} else {
printf("MALFORMED KEY=\"VALUE\"!!!\n");
}
} else {
printf("invalid token: %s!\n", token);
}
memset(token, '\0', 1024);
c = 0;
if (tokens[n] == ',') {
n++;
continue;
}
}
token[c++] = tokens[n++];
}
s++;
printf("\n<<<<====\n");
i++;
j = 0;
k++;
t = 0;
memset(tokens, '\0', 1024);
}
close(config);
return 0;
}