-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagnostic-loader.c
313 lines (247 loc) · 9.63 KB
/
agnostic-loader.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "agnostic.h"
#include <yaml.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static const char* yaml_token_names[] = {
/** An empty token. */
"YAML_NO_TOKEN",
/** A STREAM-START token. */
"YAML_STREAM_START_TOKEN",
/** A STREAM-END token. */
"YAML_STREAM_END_TOKEN",
/** A VERSION-DIRECTIVE token. */
"YAML_VERSION_DIRECTIVE_TOKEN",
/** A TAG-DIRECTIVE token. */
"YAML_TAG_DIRECTIVE_TOKEN",
/** A DOCUMENT-START token. */
"YAML_DOCUMENT_START_TOKEN",
/** A DOCUMENT-END token. */
"YAML_DOCUMENT_END_TOKEN",
/** A BLOCK-SEQUENCE-START token. */
"YAML_BLOCK_SEQUENCE_START_TOKEN",
/** A BLOCK-SEQUENCE-END token. */
"YAML_BLOCK_MAPPING_START_TOKEN",
/** A BLOCK-END token. */
"YAML_BLOCK_END_TOKEN",
/** A FLOW-SEQUENCE-START token. */
"YAML_FLOW_SEQUENCE_START_TOKEN",
/** A FLOW-SEQUENCE-END token. */
"YAML_FLOW_SEQUENCE_END_TOKEN",
/** A FLOW-MAPPING-START token. */
"YAML_FLOW_MAPPING_START_TOKEN",
/** A FLOW-MAPPING-END token. */
"YAML_FLOW_MAPPING_END_TOKEN",
/** A BLOCK-ENTRY token. */
"YAML_BLOCK_ENTRY_TOKEN",
/** A FLOW-ENTRY token. */
"YAML_FLOW_ENTRY_TOKEN",
/** A KEY token. */
"YAML_KEY_TOKEN",
/** A VALUE token. */
"YAML_VALUE_TOKEN",
/** An ALIAS token. */
"YAML_ALIAS_TOKEN",
/** An ANCHOR token. */
"YAML_ANCHOR_TOKEN",
/** A TAG token. */
"YAML_TAG_TOKEN",
/** A SCALAR token. */
"YAML_SCALAR_TOKEN"
};
enum structure_state {
s_unknown,
s_doc_root,
s_project,
s_project_docs,
s_component,
s_component_build_after,
__s_length
};
int ag_load_default(struct ag_project** project) {
char* cfg_file = ag_find_project_file();
if (!cfg_file) {
return FILE_NOT_FOUND;
}
int ret = ag_load(cfg_file, project);
free(cfg_file);
return ret;
}
struct ag_project* ag_load_default_or_die() {
struct ag_project* ret = NULL;
int x = ag_load_default(&ret);
if (x) {
die("Failed to load the project. %s.", ag_error_msg(x));
}
return ret;
}
static int stack_v(struct list* stack) {
return stack ? *((int*)stack->data) : -1;
}
int ag_load(const char* file_name, struct ag_project** project) {
assert(file_name);
FILE *fh = fopen(file_name, "r");
if (!fh) {
return UNABLE_TO_OPEN_FILE;
}
*project = (struct ag_project*)xcalloc(1, sizeof(struct ag_project));
if ('/' == file_name[0]) {
(*project)->file = xstrdup(file_name);
} else {
(*project)->file = realpath(file_name, NULL);
}
if (!(*project)->file) {
free(*project);
return FILE_NOT_FOUND;
}
(*project)->dir = parent_dir((*project)->file);
yaml_parser_t parser;
yaml_token_t token;
if (!yaml_parser_initialize(&parser)) {
die("Unable to initialize YAML parser");
}
yaml_parser_set_input_file(&parser, fh);
struct ag_component* component = NULL;
struct list* components_head = NULL;
struct list* components_tail = NULL;
struct list* build_after_head = NULL;
struct list* build_after_tail = NULL;
struct list* docs_head = NULL;
struct list* docs_tail = NULL;
struct list* stack = NULL;
int stack_vals[__s_length];
for (int i = 0; i < __s_length; ++i) {
stack_vals[i] = i;
}
char* key = NULL;
int is_key = 0;
int eof = 0;
int ret = OK;
while (!eof) {
yaml_parser_scan(&parser, &token);
debug_print("token %s\n", yaml_token_names[token.type]);
int sval = stack_v(stack);
switch(token.type) {
case YAML_STREAM_END_TOKEN:
eof = 1;
break;
case YAML_KEY_TOKEN:
is_key = 1;
break;
case YAML_VALUE_TOKEN:
is_key = 0;
break;
case YAML_DOCUMENT_START_TOKEN:
list_free(stack, NULL);
stack = list_create(stack_vals + s_doc_root, NULL);
break;
case YAML_DOCUMENT_END_TOKEN:
list_free(stack, NULL);
stack = NULL;
break;
case YAML_BLOCK_SEQUENCE_START_TOKEN:
case YAML_BLOCK_MAPPING_START_TOKEN:
case YAML_FLOW_SEQUENCE_START_TOKEN:
case YAML_FLOW_MAPPING_START_TOKEN:
debug_print("started block or flow with key '%s', stack state is %d\n", key, sval);
if (!key) {
break;
}
if (s_doc_root == sval && !strcmp(key, "project")) {
if ((*project)->components) {
eof = 1;
ret = PROJECT_GOES_AFTER_COMPONENT;
break;
}
stack = list_create(stack_vals + s_project, stack);
debug_print("%s\n", "push project");
} else if (s_project == sval && !strcmp(key, "docs")) {
stack = list_create(stack_vals + s_project_docs, stack);
debug_print("%s\n", "push project docs");
} else if (s_doc_root == sval && !strcmp(key, "component")) {
stack = list_create(stack_vals + s_component, stack);
component = (struct ag_component*)xcalloc(1, sizeof(struct ag_component));
build_after_head = NULL;
build_after_tail = NULL;
list_add(&components_head, &components_tail, component);
(*project)->component_count++;
debug_print("%s\n", "push component");
} else if (s_component == sval && !strcmp(key, "buildAfter")) {
stack = list_create(stack_vals + s_component_build_after, stack);
debug_print("%s\n", "push component build after");
} else {
stack = list_create(stack_vals + s_unknown, stack);
debug_print("%s\n", "push unknown");
}
break;
case YAML_BLOCK_END_TOKEN:
case YAML_FLOW_SEQUENCE_END_TOKEN:
case YAML_FLOW_MAPPING_END_TOKEN:
if (s_component == sval) {
if (component) {
component->build_after = build_after_head;
}
}
list_pop(&stack);
break;
case YAML_SCALAR_TOKEN:
if (is_key) {
free(key);
key = xstrdup((const char *)token.data.scalar.value);
} else {
if (s_project == sval) {
if (!strcmp(key, "name")) {
(*project)->name = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "description")) {
(*project)->description = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "bugs")) {
(*project)->bugs = xstrdup((const char*)token.data.scalar.value);
}
} else if (s_project_docs == sval) {
list_add(&docs_head, &docs_tail, xstrdup((const char*)token.data.scalar.value));
} else if (s_component == sval) {
if (!strcmp(key, "name")) {
component->name = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "alias")) {
component->alias = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "description")) {
component->description = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "git")) {
component->git = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "hg")) {
component->hg = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "build")) {
component->build = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "integrate")) {
component->integrate = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "clean")) {
component->clean = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "test")) {
component->test = xstrdup((const char*)token.data.scalar.value);
} else if (!strcmp(key, "disabled")) {
component->disabled = (0 == strcmp("true", (const char*)token.data.scalar.value));
}
} else if (s_component_build_after == sval) {
list_add(&build_after_head, &build_after_tail, xstrdup((const char*)token.data.scalar.value));
}
free(key);
key = NULL;
}
break;
default:
break;
}
yaml_token_delete(&token);
}
(*project)->docs = docs_head;
(*project)->components = components_head;
if (!(*project)->name || !((*project)->name)[0]) {
ag_free(*project);
return INVALID_PROJECT_FILE;
}
free(key);
list_free(stack, NULL);
yaml_parser_delete(&parser);
fclose(fh);
return ret;
}