-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeps.c
368 lines (317 loc) · 7.47 KB
/
deps.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
/* cdep -- simple dependency resolver
cdep is released under the New BSD license (see LICENSE.txt). Go to the project
home page for more info:
https://github.com/bitmonger/cdep
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "deps.h"
#define NAME_LEN 1024
#define CMD_LEN 1024
#define PATH_LEN 1024
int __find_target_by_name(const char *name);
const char* __find_name_by_target(int target);
const char* __find_path_by_target(int target);
struct deps_list* __allocate_new_entry(struct deps_list *here);
struct deps_list* __insert_list_entry(struct deps_list *here, struct deps_list *entry);
struct deps_list* __remove_list_entry(struct deps_list *ent);
struct deps_list* __find_target(struct deps_list *start, int target);
void __remove_target(int target);
void __remove_marked();
void __mark_target(int target);
void __store_target_commands(int group, int target);
struct deps_list* __list_tail(struct deps_list* list);
void __print_list();
int __list_length();
int __test_target_has_no_deps(int target);
struct deps_list {
int target;
int dep;
int marked;
char cmd[CMD_LEN];
char name[NAME_LEN];
char path[PATH_LEN];
struct deps_list *next;
};
int gl_nextId = 1;
struct deps_list dl;
struct deps_list cl;
int cdep_init()
{
memset(&dl, 0, sizeof(struct deps_list));
memset(&cl, 0, sizeof(struct deps_list));
return 0;
}
int cdep_add_target(const char* name)
{
struct deps_list *c;
if (__find_target_by_name(name) > 0)
return -1;
c = __allocate_new_entry(__list_tail(&dl));
c->target = gl_nextId++;
c->dep = 0;
strncpy(c->name, name, NAME_LEN);
return c->target;
}
int cdep_add_dependency(const char* name, const char *dep_name)
{
int target, dep;
struct deps_list *c;
target = __find_target_by_name(name);
if (target == 0)
target = cdep_add_target(name);
dep = __find_target_by_name(dep_name);
if (dep == 0)
dep = cdep_add_target(dep_name);
c = __allocate_new_entry(__list_tail(&dl));
c->target = target;
c->dep = dep;
return 0;
}
int cdep_add_command(const char *target_name, const char *command)
{
int target;
struct deps_list *c;
target = __find_target_by_name(target_name);
if (target == 0)
target = cdep_add_target(target_name);
c = __allocate_new_entry(__list_tail(&dl));
c->target = target;
strncpy(c->cmd, command, CMD_LEN);
return 0;
}
int cdep_add_path(const char *target_name, const char *path)
{
int target;
struct deps_list *c;
target = __find_target_by_name(target_name);
if (target == 0)
target = cdep_add_target(target_name);
c = __allocate_new_entry(__list_tail(&dl));
c->target = target;
strncpy(c->path, path, PATH_LEN);
return 0;
}
int cdep_execute()
{
int i;
int len;
int group = 1;
struct deps_list *c;
while ((len = __list_length()))
{
for (i = 1; i < gl_nextId; i++)
{
c = __find_target(&dl, i);
if (c == NULL)
continue;
if (c->marked == 0 && __test_target_has_no_deps(i))
{
__store_target_commands(group, i);
__mark_target(i);
}
}
group++;
__remove_marked();
if (len == __list_length())
{
printf("Error: Dependencies not solvable\n");
return -1;
}
}
return 0;
}
int cdep_print_build()
{
struct deps_list *c = cl.next;
while (c != NULL)
{
printf("%d \"%s\" \"%s\" \"%s\"\n", c->dep, c->name, c->path, c->cmd);
c = c->next;
}
return 0;
}
int cdep_cleanup()
{
struct deps_list *c = dl.next, *cn;
while (c)
{
cn = c->next;
free(c);
c = cn;
}
c = cl.next;
while (c)
{
cn = c->next;
free(c);
c = cn;
}
return 0;
}
int __find_target_by_name(const char *name)
{
struct deps_list *c = &dl;
while (c != NULL)
{
if (strcmp(c->name, name) == 0)
return c->target;
c = c->next;
}
return 0;
}
const char* __find_name_by_target(int target)
{
struct deps_list *c = &dl;
while (c != NULL)
{
if (c->target == target)
if (strlen(c->name) > 0)
return c->name;
c = c->next;
}
return 0;
}
const char* __find_path_by_target(int target)
{
struct deps_list *c = &dl;
while (c != NULL)
{
if (c->target == target)
if (strlen(c->path) > 0)
return c->path;
c = c->next;
}
return 0;
}
struct deps_list* __allocate_new_entry(struct deps_list *here)
{
struct deps_list *c = here->next;
here->next = malloc(sizeof(struct deps_list));
memset(here->next, 0, sizeof(struct deps_list));
here->next->next = c;
return here->next;
}
struct deps_list* __insert_list_entry(struct deps_list *here, struct deps_list *entry)
{
struct deps_list *c = here->next;
here->next = entry;
here->next->next = c;
return entry;
}
struct deps_list* __remove_list_entry(struct deps_list *ent)
{
struct deps_list *before = &dl;
while (before->next && before->next != ent)
{
before = before->next;
}
before->next = ent->next;
free(ent);
return before;
}
struct deps_list* __list_tail(struct deps_list *list)
{
while (list->next != NULL)
{
list = list->next;
}
return list;
}
int __list_length()
{
int i = 0;
struct deps_list *c = &dl;
while (c->next)
{
i++;
c = c->next;
}
return i;
}
int __test_target_has_no_deps(int target)
{
struct deps_list *c = &dl;
while (c)
{
if (c->target == target && c->dep != 0)
return 0;
c = c->next;
}
return 1;
}
struct deps_list* __find_target(struct deps_list *start, int target)
{
struct deps_list *c = start;
while (c && c->target != target)
{
c = c->next;
}
return c;
}
void __remove_target(int target)
{
struct deps_list *c = &dl;
while (c->next != NULL)
{
c = c->next;
if (c->dep == target)
c->dep = 0;
if (c->target == target)
{
c = __remove_list_entry(c);
}
}
}
void __remove_marked()
{
struct deps_list *c = &dl;
int i;
for (i=1; i < gl_nextId; i++)
{
if ((c = __find_target(&dl, i)))
{
if (!c->marked)
continue;
__remove_target(c->target);
}
}
}
void __mark_target(int target)
{
struct deps_list *c = &dl;
while (c->next != NULL)
{
c = c->next;
if (c->target == target)
c->marked = 1;
}
}
void __store_target_commands(int group, int target)
{
struct deps_list *c = &dl;
struct deps_list *n = &dl;
while ((c = __find_target(c, target)))
{
if (strlen(c->cmd) > 0)
{
n = __allocate_new_entry(__list_tail(&cl));
strncpy(n->cmd, c->cmd, CMD_LEN);
strncpy(n->name, __find_name_by_target(c->target), NAME_LEN);
strncpy(n->path, __find_path_by_target(c->target), PATH_LEN);
n->dep = group;
n->target = target;
}
c = c->next;
}
}
void __print_list(struct deps_list *list)
{
struct deps_list *c = list;
while (c->next)
{
c = c->next;
printf("[%s]\ntarget = %d\ndep = %d\ncmd = %s\n\n", __find_name_by_target(c->target), c->target, c->dep, c->cmd);
}
}