-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain_wasm.c
More file actions
284 lines (240 loc) · 7.02 KB
/
Copy pathmain_wasm.c
File metadata and controls
284 lines (240 loc) · 7.02 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "src/u-config.c"
typedef long long i64;
enum {
WASI_FD_READ = 1 << 1,
WASI_FD_READDIR = 1 << 14,
WASI_O_DIRECTORY = 1 << 1,
};
#define WASI(s) __attribute((import_module("wasi_unstable"), import_name(s)))
WASI("args_get") i32 args_get(u8 **, u8 *);
WASI("args_sizes_get") i32 args_sizes_get(i32 *, iz *);
WASI("environ_get") i32 environ_get(u8 **, u8 *);
WASI("environ_sizes_get") i32 environ_sizes_get(i32 *, iz *);
WASI("fd_close") i32 fd_close(i32);
WASI("fd_prestat_dir_name") i32 fd_prestat_dir_name(i32, u8 *, iz);
WASI("fd_prestat_get") i32 fd_prestat_get(i32, iz *);
WASI("fd_read") i32 fd_read(i32, s8 *, iz, iz *);
WASI("fd_readdir") i32 fd_readdir(i32, u8 *, iz, i64, iz *);
WASI("fd_write") i32 fd_write(i32, s8 *, iz, iz *);
WASI("path_open") i32 path_open(i32,i32,u8*,iz,i32,i64,i64,i32,i32*);
WASI("proc_exit") void proc_exit(i32);
typedef struct dirfd dirfd;
struct dirfd {
dirfd *next;
s8 path;
i32 fd;
};
struct os {
dirfd *dirs;
};
static b32 endswith_(s8 s, s8 suffix)
{
return s.len>=suffix.len && s8equals(taketail(s, suffix.len), suffix);
}
static dirfd *find_preopens(arena *a)
{
dirfd *head = 0;
dirfd **tail = &head;
for (i32 fd = 3;; fd++) {
iz stat[2];
if (fd_prestat_get(fd, stat)) {
return head;
}
s8 path = {0};
path.len = stat[1]+1;
path.s = new(a, u8, path.len);
if (fd_prestat_dir_name(fd, path.s, stat[1])) {
return head;
}
path.s[stat[1]] = '/';
// Force exactly one trailing slash
while (endswith_(path, S("//"))) {
path = cuttail(path, 1);
}
*tail = new(a, dirfd, 1);
(*tail)->path = path;
(*tail)->fd = fd;
tail = &(*tail)->next;
}
}
typedef struct {
s8 relpath;
i32 fd;
b32 ok;
} relpath;
static relpath find_dirfd(os *ctx, s8 path)
{
relpath r = {0};
for (dirfd *d = ctx->dirs; d; d = d->next) {
// Match without trailing slash
s8 dir = d->path;
while (endswith_(dir, S("/"))) {
dir = cuttail(dir, 1);
}
// TODO: parse path and match components
if (startswith(path, dir)) {
r.relpath = cuthead(path, dir.len);
// Remove leading slashes
while (startswith(r.relpath, S("/"))) {
r.relpath = cuthead(r.relpath, 1);
}
// Empty path really means current directory
if (!r.relpath.len) {
r.relpath = S(".");
}
r.fd = d->fd;
r.ok = 1;
return r;
}
}
return r;
}
static filemap os_mapfile(os *ctx, arena *perm, s8 path)
{
filemap r = {0};
path = cuttail(path, 1); // remove null terminator
relpath rel = find_dirfd(ctx, path);
if (!rel.ok) {
r.status = filemap_NOTFOUND;
return r;
}
path = rel.relpath;
i32 fd = -1;
i32 err = path_open(
rel.fd, 0, path.s, path.len, 0, WASI_FD_READ, 0, 0, &fd
);
if (err) {
r.status = filemap_NOTFOUND;
return r;
}
iz cap = perm->end - perm->beg;
r.data.s = (u8 *)perm->beg;
r.data.len = cap;
err = fd_read(fd, &r.data, 1, &r.data.len);
fd_close(fd);
if (err || r.data.len==cap) {
r.status = filemap_READERR;
return r;
}
perm->beg += r.data.len;
r.status = filemap_OK;
return r;
}
static s8node *os_listing(os *ctx, arena *a, s8 path)
{
s8list r = {0};
path = cuttail(path, 1); // remove null terminator
relpath rel = find_dirfd(ctx, path);
if (!rel.ok) {
return 0;
}
path = rel.relpath;
i32 fd = -1;
i32 err = path_open(
rel.fd, 0,
path.s, path.len,
WASI_O_DIRECTORY, WASI_FD_READDIR, 0, 0, &fd
);
if (err) {
return 0;
}
i64 cookie = 0;
i32 cap = 1<<14;
u8 *buf = new(a, u8, cap);
for (;;) {
iz len = 0;
err = fd_readdir(fd, buf, cap, cookie, &len);
if (err || !len) {
break;
}
for (iz off = 0; off < len;) {
struct {
i64 cookie;
i64 inode;
i32 len;
u8 type;
} entry;
// NOTE: fd_readdir() returns unaligned data (!!!)
__builtin_memcpy(&entry, buf+off, sizeof(entry));
off += sizeof(entry);
s8 name = {buf+off, entry.len};
off += entry.len;
if (off > len) {
break; // truncated
}
cookie = entry.cookie;
if (endswith_(name, S(".pc"))) {
s8 copy = news8(a, name.len);
s8copy(copy, name);
append(&r, copy, a);
}
}
}
fd_close(fd);
return r.head;
}
static void os_write(os *ctx, i32 fd, s8 data)
{
if (fd_write(fd, &data, 1, &data.len)) {
os_fail(ctx);
}
}
static void os_fail(os *ctx)
{
(void)ctx;
proc_exit(1);
__builtin_unreachable();
}
void _start(void)
{
os ctx = {0};
static byte heap[1<<22];
arena perm = {0};
perm.beg = heap;
perm.end = heap + sizeof(heap);
perm.ctx = &ctx;
ctx.dirs = find_preopens(&perm);
i32 argc = 0;
iz buflen = 0;
args_sizes_get(&argc, &buflen);
u8 **argv = new(&perm, u8 *, argc);
u8 *buf = new(&perm, u8, buflen);
args_get(argv, buf);
i32 envc = 0;
iz envlen = 0;
environ_sizes_get(&envc, &envlen);
u8 **envp = new(&perm, u8 *, envc);
u8 *env = new(&perm, u8, envlen);
environ_get(envp, env);
config conf = {0};
conf.perm = perm;
conf.args = argv + !!argc;
conf.nargs = argc - !!argc;
conf.pc_path = S("/usr/lib/pkgconfig:/usr/share/pkgconfig");
conf.fixedpath = conf.pc_path;
conf.pc_sysincpath = conf.sys_incpath = S("/usr/include");
conf.pc_syslibpath = conf.sys_libpath = S("/usr/lib");
conf.delim = ':';
conf.haslisting = 1;
for (i32 i = 0; i < envc; i++) {
cut c = s8cut(s8fromcstr(envp[i]), '=');
s8 name = c.head;
s8 value = c.tail;
if (s8equals(name, S("PKG_CONFIG_PATH"))) {
conf.envpath = value;
} else if (s8equals(name, S("PKG_CONFIG_LIBDIR"))) {
conf.fixedpath = value;
} else if (s8equals(name, S("PKG_CONFIG_TOP_BUILD_DIR"))) {
conf.top_builddir = value;
} else if (s8equals(name, S("PKG_CONFIG_SYSTEM_INCLUDE_PATH"))) {
conf.sys_incpath = value;
} else if (s8equals(name, S("PKG_CONFIG_SYSTEM_LIBRARY_PATH"))) {
conf.sys_libpath = value;
} else if (s8equals(name, S("PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"))) {
conf.print_sysinc = value;
} else if (s8equals(name, S("PKG_CONFIG_ALLOW_SYSTEM_LIBS"))) {
conf.print_syslib = value;
}
}
uconfig(&conf);
}