-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_call.c
More file actions
165 lines (148 loc) · 3.86 KB
/
Copy pathnode_call.c
File metadata and controls
165 lines (148 loc) · 3.86 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
#ifndef _AMALGAMATE_
#include "script.h"
#include "utils.h"
#include "alloc.h"
#include "ulist.h"
#endif
#define MACRO_MAX_ARGS_NUM 64
struct _call {
const char *key; ///< Macro name
struct node *n; ///< Call Parent node
struct node *mn; ///< Macro node
struct node *prev; ///< Call Prev node
struct node *cloned; ///< Macro cloned node
struct ulist nodes; ///< Nodes stack. struct node*
struct node *args[MACRO_MAX_ARGS_NUM];
int nn_idx;
int arg_idx;
};
struct node* call_macro_node(struct node *n) {
akassert(n->type == NODE_TYPE_CALL);
struct _call *c = n->impl;
akassert(c && c->mn);
return c->mn;
}
struct node* call_first_node(struct node *n) {
akassert(n->type == NODE_TYPE_CALL);
struct _call *c = n->impl;
akassert(c);
if (c->nn_idx > -1) {
struct node *nn = *(struct node**) ulist_get(&n->ctx->nodes, c->nn_idx);
return nn;
} else {
return 0;
}
}
static int _call_macro_visit(struct node *n, int lvl, void *d) {
struct _call *call = d;
int ret = 0;
if (call->mn == n /*skip macro itself */ || call->mn->child == n /* skip macro name */) {
return 0;
}
if (lvl < 0) {
ulist_pop(&call->nodes);
return 0;
}
// Macro arg
if (n->value[0] == '&' && n->value[1] == '\0') {
int idx;
int rc = 0;
if (n->child) {
idx = utils_strtol(n->child->value, 10, &rc);
if (rc || idx < 1 || idx >= MACRO_MAX_ARGS_NUM) {
node_fatal(rc, n, "Invalid macro arg index: %d", idx);
return 0;
}
ret = INT_MAX;
idx--;
} else {
call->arg_idx++;
idx = call->arg_idx;
}
call->arg_idx = idx;
n = call->args[idx];
if (!n) {
node_fatal(rc, call->n, "Call argument: %d for macro: %s is not set", (idx + 1), call->key);
return 0;
}
}
struct node *parent = *(struct node**) ulist_peek(&call->nodes);
if (call->nn_idx == -1) {
call->nn_idx = n->ctx->nodes.num;
}
struct node *nn = node_clone_and_register(n);
nn->parent = parent;
// Add node to the parent
if (!parent->child) {
parent->child = nn;
} else {
struct node *c = parent->child;
while (c && c->next) {
c = c->next;
}
c->next = nn;
}
ulist_push(&call->nodes, &nn);
return ret;
}
static void _call_remove(struct node *n) {
struct node *prev = node_find_prev_sibling(n);
if (prev) {
prev->next = n->next;
} else {
n->parent->child = n->next;
}
}
static void _call_init(struct node *n) {
struct unit *unit = unit_peek();
const char *key = node_value(n->child);
if (!key || key[0] == '\0') {
node_warn(n, "No name specified for 'call' directive");
return;
}
struct node *mn = unit_env_get_node(unit, key, 0);
if (!mn) {
node_fatal(0, n, "Unknown script macro: %s", key);
return;
}
struct _call *call = xcalloc(1, sizeof(*call));
call->nn_idx = -1;
call->arg_idx = -1;
call->key = key;
call->mn = mn;
call->n = n;
call->prev = node_find_prev_sibling(n);
ulist_init(&call->nodes, 16, sizeof(struct node*));
n->impl = call;
int idx = 0;
struct node *next = 0;
for (struct node *pn = n->child->next; pn; pn = next) {
next = pn->next;
if (idx >= MACRO_MAX_ARGS_NUM) {
node_fatal(0, n, "Exceeded the maximum number of macro args: " Q_STR(MACRO_MAX_ARGS_NUM));
return;
}
pn->next = 0;
call->args[idx++] = pn;
}
ulist_push(&call->nodes, &n->parent);
_call_remove(n);
node_visit(mn, 1, call, _call_macro_visit);
for (int i = call->nn_idx; i < n->ctx->nodes.num; ++i) {
struct node *nn = *(struct node**) ulist_get(&n->ctx->nodes, i);
node_bind(nn);
}
}
static void _call_dispose(struct node *n) {
struct _call *call = n->impl;
if (call) {
ulist_destroy_keep(&call->nodes);
free(call);
}
}
int node_call_setup(struct node *n) {
n->flags |= NODE_FLG_NO_CWD;
n->init = _call_init;
n->dispose = _call_dispose;
return 0;
}