-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse_job_description.c
149 lines (130 loc) · 3.81 KB
/
parse_job_description.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsmn.h"
#include "parse_job_description.h"
int parse_job_description(const char *js, char **build_id, int *ttl, char **distrib_type, char ***env) {
int res;
res = count_json_tokens(js, strlen(js));;
if (res <= 0) {
return -1;
}
jsmn_parser json_parser;
jsmntok_t *json;
json = xmalloc(res * sizeof(jsmntok_t));
jsmn_init(&json_parser);
res = jsmn_parse(&json_parser, js, strlen(js), json, res);
int i;
for(i = 0; i<res; i++) {
if(!strncmp(&js[json[i].start], "worker_args", strlen("worker_args")) && json[i+2].type == JSMN_OBJECT) {
break;
}
}
if(i == res) {
return -2;
}
i += 3;
int j;
for(j = 0; j<res; j++) {
if(!strncmp(&js[json[j].start], "cmd_params", strlen("cmd_params")) && json[j+1].type == JSMN_OBJECT) {
break;
}
}
if(j == res) {
return -2;
}
int cmd_params_len = json[j+1].size, cur_env = 0;
//cmd_params + 6 necessary parameters + NULL
*env = xmalloc((cmd_params_len + 7) * sizeof(char *));
while(i < res) {
int param_len = json[i].end - json[i].start;
const char *start = &js[json[i].start];
if(COMPARE(start, "id", param_len)) {
*build_id = xstrndup(&js[json[i+1].start], json[i+1].end-json[i+1].start);
i += 2;
}
else if(COMPARE(start, "distrib_type", param_len)) {
*distrib_type = xstrndup(&js[json[i+1].start], json[i+1].end-json[i+1].start);
i += 2;
}
else if(COMPARE(start, "time_living", param_len)) {
char *t = xstrndup(&js[json[i+1].start], json[i+1].end-json[i+1].start);
*ttl = atoi(t);
free(t);
i+=2;
}
else if(COMPARE(start, "cmd_params", param_len)) {
//skip jsmn_object straight to the first parameter
i += 2;
for(j = 0; j < cmd_params_len; j++, i+=2, cur_env++) {
char *key = xstrndup(&js[json[i].start], json[i].end - json[i].start);
if(json[i+1].type == JSMN_PRIMITIVE && js[json[i+1].start] == 'n') {
(*env)[j] = make_env(key, "");
}
else {
char *value = xstrndup(&js[json[i+1].start], json[i+1].end - json[i+1].start);
(*env)[j] = make_env(key, value);
free(value);
}
free(key);
}
}
else if(COMPARE(start, "include_repos", param_len)) {
int repos_len = json[i+1].size;
char *repo_names = xmalloc(json[i+1].end-json[i+1].start), *repo_urls = xmalloc(json[i+1].end-json[i+1].start);
char *prn = repo_names, *pru = repo_urls;
i += 2;
int f = 0;
for(j = 0; j < 2 * repos_len; j++, i++) {
char *t = xstrndup(&js[json[i].start], json[i].end - json[i].start);
if(!f) {
prn += sprintf(prn, "%s ", t);
f = 1;
}
else {
pru += sprintf(pru, "%s ", t);
f = 0;
}
free(t);
}
(*env)[cur_env] = make_env("REPO_NAMES", repo_names);
(*env)[cur_env+1] = make_env("REPO_URL", repo_urls);
free(repo_urls);
free(repo_names);
cur_env += 2;
}
else if(COMPARE(start, "uname", param_len)) {
char *t = xstrndup(&js[json[i+1].start], json[i+1].end - json[i+1].start);
(*env)[cur_env++] = make_env("UNAME", t);
free(t);
i += 2;
}
else if(COMPARE(start, "email", param_len)) {
char *t = xstrndup(&js[json[i+1].start], json[i+1].end - json[i+1].start);
(*env)[cur_env++] = make_env("EMAIL", t);
free(t);
i += 2;
}
else if(COMPARE(start, "platform", param_len)) {
int platform_len = json[i+1].size;
i += 2;
for(j = 0; j<platform_len; i+=2, j++) {
if(COMPARE(&js[json[i].start], "arch", json[i].end-json[i].start)) {
char *t = xstrndup(&js[json[i+1].start], json[i+1].end-json[i+1].start);
(*env)[cur_env++] = make_env("PLATFORM_ARCH", t);
free(t);
}
}
}
else {
i++;
}
}
free(json);
(*env)[cur_env++] = make_env("HOME", builder_config.work_dir);
(*env)[cur_env++] = NULL;
return cur_env;
}
inline static char *make_env(const char *key, const char *value) {
return alloc_sprintf("%s=%s", key, value);
}