-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_dir.c
More file actions
87 lines (78 loc) · 1.89 KB
/
node_dir.c
File metadata and controls
87 lines (78 loc) · 1.89 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
#ifndef _AMALGAMATE_
#include "script.h"
#include "xstr.h"
#include "env.h"
#include "paths.h"
#include "utils.h"
#include <unistd.h>
#endif
static void _node_dir_normalize_add(const char *dir, struct xstr *xstr, const char *v, char buf[PATH_MAX]) {
if (xstr_size(xstr)) {
if (!is_vlist(xstr_ptr(xstr))) {
xstr_unshift(xstr, "\1", 1);
}
xstr_cat2(xstr, "\1", 1);
}
char *path = path_normalize_cwd(v, dir, buf);
xstr_cat(xstr, path);
}
static const char* _dir_value(struct node *n) {
struct node_foreach *fe = node_find_parent_foreach(n);
if (fe) {
free(n->impl);
n->impl = 0;
}
if (n->impl) {
return n->impl;
}
char buf[PATH_MAX];
struct unit *root = unit_root();
struct unit *unit = unit_peek();
struct xstr *xstr = xstr_create_empty();
const char *dir = 0;
if (n->value[0] == 'S') {
if (n->value[1] == 'S') {
dir = unit->dir;
} else {
dir = root->dir;
}
} else {
if (n->value[1] == 'C') {
dir = unit->cache_dir;
} else {
dir = root->cache_dir;
}
}
for (struct node *nn = n->child; nn; nn = nn->next) {
const char *v = node_value(nn);
if (is_vlist(v)) {
struct vlist_iter iter;
vlist_iter_init(v, &iter);
while (vlist_iter_next(&iter)) {
if (iter.len) {
char vbuf[iter.len + 1];
utils_strnncpy(vbuf, iter.item, iter.len, iter.len + 1);
_node_dir_normalize_add(dir, xstr, vbuf, buf);
}
}
} else {
_node_dir_normalize_add(dir, xstr, v, buf);
}
}
if (xstr_size(xstr) == 0) {
_node_dir_normalize_add(dir, xstr, ".", buf);
}
n->impl = xstr_destroy_keep_ptr(xstr);
return n->impl;
}
static void _dir_dispose(struct node *n) {
if (n->impl) {
free(n->impl);
}
}
int node_dir_setup(struct node *n) {
n->flags |= NODE_FLG_NO_CWD;
n->value_get = _dir_value;
n->dispose = _dir_dispose;
return 0;
}