-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_config.c
160 lines (157 loc) · 5.41 KB
/
parse_config.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
151
152
153
154
155
156
157
158
159
160
#include <stdio.h>
#include <stdlib.h>
#include "parse_config.h"
#include "tokenizer.h"
int is_in_range(range_node* node, int val) {
if (node == NULL) {
return 0;
}
if ((node->range[0] == val) || ((val > node->range[0]) && (val <= node->range[1]))) {
return 1;
}
else {
return is_in_range(node->next, val);
}
}
res* parse_config(char* path) {
//int status;
FILE* conf = fopen(path, "r");
// can read file
res* head = NULL;
res* tail = NULL;
if (conf) {
// do parsing
char* line = NULL;
size_t len = 0;
ssize_t read;
// iterate over input lines
char** columns = NULL;
char** elements = NULL;
char** range = NULL;
while ((read = getline(&line, &len, conf)) != -1) {
// columns[0] = port, columns[1] = uid, columns[2] = gid
columns = tokenize(line, ":");
int i,j;
range_node* port_head = NULL;
range_node* uid_head = NULL;
range_node* gid_head = NULL;
range_node* port_tail = NULL;
range_node* uid_tail = NULL;
range_node* gid_tail = NULL;
/*
port = malloc(sizeof(range_node));
uid = malloc(sizeof(range_node));
gid = malloc(sizeof(range_node));
*/
// iterate over colon-delineated segments
for (i = 0; columns[i]; i++) {
elements = tokenize(columns[i], ",");
/*
if (i == 0 && token_count(elements) != 1) {
printf("can't have multiple ports or ranges");
}
*/
// iterate over comma-delineated segments
for (j = 0; elements[j]; j++) {
range = tokenize(elements[j], "-");
range_node new_range;
if (token_count(range) > 2) {
printf("ranges must have 0 or 1 dashes");
}
else if (token_count(range) == 2) {
range_node n = {NULL, {atoi(range[0]), atoi(range[1])}};
new_range = n;
}
else {
range_node n = {NULL, {atoi(range[0]), 0}};
new_range = n;
}
if (i == 0) { // port
if (port_head == NULL) {
port_head = malloc(sizeof(range_node));
*port_head = new_range;
port_tail = port_head;
}
else {
port_tail->next = malloc(sizeof(range_node));
*(port_tail->next) = new_range;
port_tail = port_tail->next;
}
}
else if (i == 1) { // uid
if (uid_head == NULL) {
uid_head = malloc(sizeof(range_node));
*uid_head = new_range;
uid_tail = uid_head;
}
else {
uid_tail->next = malloc(sizeof(range_node));
*(uid_tail->next) = new_range;
uid_tail = uid_tail->next;
}
}
else { // gid
if (gid_head == NULL) {
gid_head = malloc(sizeof(range_node));
*gid_head = new_range;
gid_tail = gid_head;
}
else {
gid_tail->next = malloc(sizeof(range_node));
*(gid_tail->next) = new_range;
gid_tail = gid_tail->next;
}
}
}
}
//res new_res = {&port, &uid, &gid};
if (head == NULL) {
head = malloc(sizeof(res));
head->port_head = port_head;
head->uid_head = uid_head;
head->gid_head = gid_head;
head->next = NULL;
tail = head;
}
else {
tail->next = malloc(sizeof(res));
tail->next->port_head = port_head;
tail->next->uid_head = uid_head;
tail->next->gid_head = gid_head;
tail->next->next = NULL;
tail = tail->next;
}
}
}
// can't read file
else {
perror("fopen");
exit(EXIT_FAILURE);
}
return head;
}
/*
int main() {
res* r = parse_config("sprd.conf");
while (r != NULL) {
range_node* p = r->port_head;
range_node* u = r->uid_head;
range_node* g = r->gid_head;
printf("Unpacking reservation...\n");
while (p != NULL) {
printf("Port range: %d %d\n", p->range[0], p->range[1]);
p = p->next;
}
while (u != NULL) {
printf("uid range: %d %d\n", u->range[0], u->range[1]);
u = u->next;
}
while (g != NULL) {
printf("gid range: %d %d\n", g->range[0], g->range[1]);
g = g->next;
}
printf("End of reservation\n");
r = r->next;
}
return 0;
}*/