-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_foreach.c
More file actions
45 lines (40 loc) · 1 KB
/
node_foreach.c
File metadata and controls
45 lines (40 loc) · 1 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
#ifndef _AMALGAMATE_
#include "script.h"
#include "log.h"
#include "alloc.h"
#include "utils.h"
#endif
static void _foreach_setup(struct node *n) {
if (!n->child) {
node_fatal(AK_ERROR_SCRIPT_SYNTAX, n, "Foreach must have a variable name as first parameter");
}
struct node_foreach *fe = xcalloc(1, sizeof(*fe));
n->impl = fe;
fe->name = xstrdup(node_value(n->child));
struct xstr *xstr = xstr_create_empty();
const char *v = node_value(n->child->next);
if (v && *v != '\0') {
xstr_cat(xstr, v);
}
if (!is_vlist(xstr_ptr(xstr))) {
xstr_unshift(xstr, "\1", 1);
}
fe->items = xstr_destroy_keep_ptr(xstr);
}
static void _foreach_dispose(struct node *n) {
struct node_foreach *fe = n->impl;
if (fe) {
free(fe->items);
free(fe->name);
free(fe);
}
}
static void _foreach_init(struct node *n) {
}
int node_foreach_setup(struct node *n) {
n->flags |= NODE_FLG_NO_CWD;
n->init = _foreach_init;
n->setup = _foreach_setup;
n->dispose = _foreach_dispose;
return 0;
}