-
Notifications
You must be signed in to change notification settings - Fork 1
/
agnostic.c
374 lines (334 loc) · 10.8 KB
/
agnostic.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "agnostic.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static const char* error_messages[] = {
[OK] = "OK",
[UNABLE_TO_OPEN_FILE] = "Unable to open file",
[FILE_NOT_FOUND] = "File not found",
[PROJECT_GOES_AFTER_COMPONENT] = "Project section must go before any component sections",
[INVALID_PROJECT_FILE] = "Invalid project file",
[DEPENDENCY_LOOP] = "Dependency loop detected",
[COMPONENT_NOT_FOUND] = "Component not found"
};
const char* ag_error_msg(int code) {
if (0 > code || code >= ARRAY_SIZE(error_messages)) {
return NULL;
}
return error_messages[code];
}
static void ag_free_component(void* data) {
if (!data) {
return;
}
struct ag_component* c = (struct ag_component*)data;
free(c->name);
free(c->alias);
free(c->description);
free(c->git);
free(c->hg);
free(c->build);
free(c->integrate);
free(c->clean);
free(c->test);
list_free(c->build_after, &free);
free(c);
}
void ag_free(struct ag_project* p) {
if (!p) {
return;
}
free(p->name);
free(p->description);
free(p->bugs);
free(p->dir);
free(p->file);
list_free(p->components, &ag_free_component);
list_free(p->docs, &free);
free(p);
}
static int file_exist(const char* fname) {
return access(fname, F_OK) != -1;
}
static char * normalize_path(const char * src, size_t src_len) {
// initial version of this function is written by the user 'arnaud576875' from StackOverflow:
// http://stackoverflow.com/questions/4774116/c-realpath-without-resolving-symlinks
char * res;
size_t res_len;
const char * ptr = src;
const char * end = &src[src_len];
const char * next;
if (src_len == 0 || src[0] != '/') {
// relative path
char* pwd = NULL;
size_t pwd_len;
if (!(pwd = getcwd(NULL, 0))) {
return NULL;
}
pwd_len = strlen(pwd);
res = xmalloc(pwd_len + 1 + src_len + 1);
memcpy(res, pwd, pwd_len);
res_len = pwd_len;
free(pwd);
} else {
res = xmalloc((src_len > 0 ? src_len : 1) + 1);
res_len = 0;
}
for (ptr = src; ptr < end; ptr=next+1) {
size_t len;
next = memchr(ptr, '/', end-ptr);
if (next == NULL) {
next = end;
}
len = next-ptr;
switch(len) {
case 2:
if (ptr[0] == '.' && ptr[1] == '.') {
const char * slash = strrchr(res, '/');
if (slash != NULL) {
res_len = slash - res;
}
continue;
}
break;
case 1:
if (ptr[0] == '.') {
continue;
}
break;
case 0:
continue;
}
res[res_len++] = '/';
memcpy(&res[res_len], ptr, len);
res_len += len;
}
if (res_len == 0) {
res[res_len++] = '/';
}
res[res_len] = '\0';
return res;
}
char* ag_find_project_file() {
const char* files[] = { "../agnostic.yaml", "agnostic.yaml" };
const char* relative = NULL;
for (int i = 0; i < ARRAY_SIZE(files); ++i) {
if (file_exist(files[i])) {
relative = files[i];
break;
}
}
if (!relative) {
return NULL;
}
return normalize_path(relative, strlen(relative)); // realpath() doesn't work here, since we DON'T want to resolve symlinks
}
struct ag_component* ag_find_current_component(struct ag_project* project) {
assert(project);
char* buf = getcwd(NULL, 0);
if (!buf) {
return NULL;
}
if (!strcmp(project->dir, buf)) {
// we're in the project directory -> no current component.
free(buf);
return NULL;
}
char* name = strrchr(buf, '/');
name = name ? (name + 1) : buf;
struct ag_component* ret = ag_find_component(project, name);
free(buf);
return ret;
}
struct ag_component* ag_find_component(struct ag_project* project, const char* name_or_alias) {
assert(project);
assert(name_or_alias);
struct list* l = project->components;
struct ag_component* ret = NULL;
while (l && !ret) {
struct ag_component* c = (struct ag_component*)l->data;
if ((c->name && !strcmp(c->name, name_or_alias)) ||
(c->alias && !strcmp(c->alias, name_or_alias))) {
ret = c;
}
l = l->next;
}
return ret;
}
char* ag_component_dir(struct ag_project* project, struct ag_component* component) {
assert(project);
assert(project->dir);
assert(component);
assert(component->name);
char* ret = NULL;
asprintf(&ret, "%s/%s", project->dir, component->name);
return ret;
}
static int is_component_up_in_branch_guarded(struct ag_project* project, struct ag_component* leaf, const char* name, int count) {
if (count > project->component_count + 2) {
// actually, it's a dependency loop
return 0;
}
if (!leaf) {
return 0;
}
if (!strcmp(leaf->name, name) || (leaf->alias && !strcmp(leaf->alias, name))) {
return 1;
}
for (struct list* slist = leaf->build_after; slist; slist = slist->next) {
if (is_component_up_in_branch_guarded(project, ag_find_component(project, (char*)slist->data), name, count + 1)) {
return 1;
}
}
return 0;
}
static int is_component_up_in_branch(struct ag_project* project, struct ag_component* leaf, const char* name) {
return is_component_up_in_branch_guarded(project, leaf, name, 0);
}
static struct list* fill_build_up_list(struct list* old_root, struct ag_project* project, struct ag_component* component,
const char* up_to_component, int count, int* ret_code) {
// TODO: heavy code here and in is_component_up_in_branch() function. Need to rewrite in a more efficient way
if (0 > count || count > project->component_count * project->component_count) {
list_free(old_root, NULL);
*ret_code = DEPENDENCY_LOOP;
return NULL;
}
if (!old_root) {
return NULL;
}
struct list* new_root = old_root;
for (struct list* slist = component->build_after; slist; slist = slist->next) {
const char* s = (char*)slist->data;
int found = 0;
for (struct list* l = project->components; l && !found; l = l->next) {
struct ag_component* c = (struct ag_component*)l->data;
if (!strcmp(c->name, s)) {
if (!up_to_component || is_component_up_in_branch(project, c, up_to_component)) {
new_root = list_create(c, new_root);
new_root = fill_build_up_list(new_root, project, c, up_to_component, count + 1, ret_code);
if (!new_root) {
return NULL;
}
}
found = 1;
}
}
if (!found) {
list_free(new_root, NULL);
*ret_code = COMPONENT_NOT_FOUND;
return NULL;
}
}
return new_root;
}
static void remove_duplicates(struct list* list) {
for (struct list* i = list; i; i = i->next) {
for (struct list* j = i->next, *prev_j = i; j; prev_j = j, j = j->next) {
struct ag_component* ic = (struct ag_component*)i->data;
struct ag_component* jc = (struct ag_component*)j->data;
if (!strcmp(ic->name, jc->name)) {
prev_j->next = j->next;
free(j);
j = prev_j;
}
}
}
}
struct list* ag_build_up_list(struct ag_project* project, struct ag_component* component, const char* up_to_component, int* ret_code) {
assert(project);
assert(component);
int rc = OK;
struct list* ret = fill_build_up_list(list_create(component, NULL), project, component, up_to_component, 0, &rc);
if (ret) {
remove_duplicates(ret);
}
if (ret_code) {
*ret_code = rc;
}
return ret;
}
static struct list* fill_build_down_list(struct ag_component* root, struct ag_project* project, struct ag_component* down_cmp, int* ret_code) {
if (!root) {
return NULL;
}
struct list* ret = NULL;
struct list* l = NULL;
int ret_count = 0;
struct list* queue_head = list_create(root, NULL);
struct list* queue_tail = queue_head;
while (queue_head) {
struct ag_component* c = (struct ag_component*)queue_head->data;
list_add(&ret, &l, c);
++ret_count;
if (0 > ret_count || ret_count > project->component_count * project->component_count) {
list_free(queue_head, NULL);
list_free(ret, NULL);
*ret_code = DEPENDENCY_LOOP;
return NULL;
}
for (struct list* pl = project->components; pl; pl = pl->next) {
struct ag_component* pc = (struct ag_component*)pl->data;
for (struct list* sl = pc->build_after; sl; sl = sl->next) {
if (!strcmp((char*)sl->data, c->name)) {
if (!down_cmp || is_component_up_in_branch(project, down_cmp, pc->name)) {
list_add(&queue_head, &queue_tail, pc);
}
break;
}
}
}
list_pop(&queue_head);
}
return ret;
}
struct list* ag_build_down_list(struct ag_project* project, struct ag_component* component, const char* down_to_component, int* ret_code) {
assert(project);
assert(component);
struct ag_component* down_cmp = NULL;
if (down_to_component) {
down_cmp = ag_find_component(project, down_to_component);
if (!down_cmp) {
if (ret_code) {
*ret_code = COMPONENT_NOT_FOUND;
}
return NULL;
}
}
int rc = OK;
struct list* ret = fill_build_down_list(component, project, down_cmp, &rc);
if (ret) {
remove_duplicates(ret);
}
if (ret_code) {
*ret_code = rc;
}
return ret;
}
struct list* ag_build_all_list(struct ag_project* project) {
assert(project);
struct list* ret = NULL;
struct list* tail = NULL;
for (struct list* i = project->components; i; i = i->next) {
int found = 0;
struct ag_component* ic = (struct ag_component*)i->data;
for (struct list* j = ret, *prev_j = NULL; j; prev_j = j, j = j->next) {
struct ag_component* jc = (struct ag_component*)j->data;
if (is_component_up_in_branch(project, jc, ic->name)) {
struct list* n = list_create(ic, j);
if (prev_j) {
prev_j->next = n;
} else {
ret = n;
}
found = 1;
break;
}
}
if (!found) {
list_add(&ret, &tail, ic);
}
}
return ret;
}