-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_if.c
More file actions
227 lines (207 loc) · 5.36 KB
/
node_if.c
File metadata and controls
227 lines (207 loc) · 5.36 KB
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
#ifndef _AMALGAMATE_
#include "script.h"
#include "log.h"
#include "env.h"
#include "utils.h"
#include <string.h>
#endif
static bool _if_defined_eval(struct node *mn) {
struct node *n = mn->parent;
for (struct node *nn = mn->child; nn; nn = nn->next) {
const char *val = node_value(mn->child);
if (val && *val != '\0' && node_env_get(n, val)) {
return true;
}
}
return false;
}
static bool _if_matched_eval(struct node *mn) {
const char *val1 = node_value(mn->child);
const char *val2 = mn->child ? node_value(mn->child->next) : 0;
if (val1 && val2) {
return strcmp(val1, val2) == 0;
} else if (val1 == 0 && val2 == 0) {
return true;
} else {
return false;
}
}
static bool _if_prefix_eval(struct node *mn) {
const char *val1 = node_value(mn->child);
const char *val2 = mn->child ? node_value(mn->child->next) : 0;
if (val1 && val2) {
return utils_startswith(val1, val2);
} else if (val1 == 0 && val2 == 0) {
return true;
} else {
return false;
}
}
static bool _if_contains_eval(struct node *mn) {
const char *val1 = node_value(mn->child);
const char *val2 = mn->child ? node_value(mn->child->next) : 0;
if (val1 && val2) {
return strstr(val1, val2);
} else if (val1 == 0 && val2 == 0) {
return true;
} else {
return false;
}
}
static bool _if_cond_eval(struct node *n, struct node *mn);
static bool _if_OR_eval(struct node *n, struct node *mn) {
for (struct node *nn = mn->child; nn; nn = nn->next) {
if (_if_cond_eval(n, nn)) {
return true;
}
}
return false;
}
static bool _if_AND_eval(struct node *n, struct node *mn) {
for (struct node *nn = mn->child; nn; nn = nn->child) {
if (!_if_cond_eval(n, nn)) {
return false;
}
}
return mn->child != 0;
}
static bool _if_cond_eval(struct node *n, struct node *mn) {
if (node_is_value(mn)) {
return true;
}
const char *op = mn->value;
if (!op) {
node_fatal(AK_ERROR_SCRIPT_SYNTAX, n, "Matching condition is not set");
}
bool eq = false;
bool neg = false;
if (mn->flags & NODE_FLG_NEGATE) {
neg = true;
}
if (mn->type == NODE_TYPE_BAG) {
if (*op == '!') {
++op;
}
if (strcmp(op, "eq") == 0) {
eq = _if_matched_eval(mn);
} else if (strcmp(op, "defined") == 0) {
eq = _if_defined_eval(mn);
} else if (strcmp(op, "or") == 0) {
eq = _if_OR_eval(n, mn);
} else if (strcmp(op, "and") == 0) {
eq = _if_AND_eval(n, mn);
} else if (strcmp(op, "prefix") == 0) {
eq = _if_prefix_eval(mn);
} else if (strcmp(op, "contains") == 0) {
eq = _if_contains_eval(mn);
} else {
node_fatal(AK_ERROR_SCRIPT_SYNTAX, n, "Unknown matching condition: %s", op);
}
} else if (node_is_can_be_value(mn)) {
op = node_value(mn);
if (is_vlist(op)) {
struct vlist_iter iter;
vlist_iter_init(op, &iter);
if (vlist_iter_next(&iter) && !(iter.len == 1 && iter.item[0] == '0')) {
eq = iter.len > 0 || vlist_iter_next(&iter);
}
} else {
eq = (op && *op != '\0' && !(op[0] == '0' && op[1] == '\0'));
}
} else {
node_fatal(AK_ERROR_SCRIPT_SYNTAX, n, "Unknown matching condition: %s", op);
}
if (neg) {
eq = !eq;
}
if (g_env.verbose) {
node_info(n, "Evaluated to: %s", (eq ? "true" : "false"));
}
return eq;
}
static inline bool _if_node_is_else(struct node *n) {
return n && n->value && strcmp(n->value, "else") == 0;
}
static void _if_pull_else(struct node *n) {
struct node *prev = node_find_prev_sibling(n);
struct node *next = n->next;
struct node *nn = next;
bool elz = false;
if (_if_node_is_else(nn)) {
elz = true;
next = nn->next;
}
n->child = 0;
if (elz && nn->child) {
nn = nn->child;
n->next = nn; // Keep upper iterations (if any) be consistent.
n->child = 0;
if (prev) {
prev->next = nn;
} else {
n->parent->child = nn;
}
for ( ; nn; nn = nn->next) {
nn->parent = n->parent;
if (nn->next == 0) {
nn->next = next;
break;
}
}
} else if (prev) {
n->next = next;
prev->next = next;
} else {
n->next = next;
n->parent->child = next;
}
}
static void _if_pull_if(struct node *n) {
struct node *mn = n->child;
struct node *prev = node_find_prev_sibling(n);
struct node *next = n->next;
struct node *nn = mn->next;
if (_if_node_is_else(next)) {
next = next->next; // Skip else block
}
n->child = 0;
if (nn) {
n->next = nn; // Keep upper iterations (if any) be consistent.
if (prev) {
prev->next = nn;
} else {
n->parent->child = nn;
}
for ( ; nn; nn = nn->next) {
nn->parent = n->parent;
if (nn->next == 0) {
nn->next = next;
break;
}
}
} else if (prev) {
n->next = next;
prev->next = next;
} else {
n->next = next;
n->parent->child = next;
}
}
static void _if_init(struct node *n) {
struct node *cn = n->child; // Conditional node
if (!cn) {
node_fatal(AK_ERROR_SCRIPT_SYNTAX, n, "'if {...}' must have a condition clause");
}
node_init(cn); // Explicitly init conditional node since in script init worflow 'if' initiated first.
bool matched = _if_cond_eval(n, cn);
if (matched) {
_if_pull_if(n);
} else {
_if_pull_else(n);
}
n->init = 0; // Protect me from second call in any way
}
int node_if_setup(struct node *n) {
n->init = _if_init;
return 0;
}