~lattis/muon

ref: 1c1ab113103e1a2ebcb7edf3a47f826428f8e124 muon/src/args.c -rw-r--r-- 7.3 KiB
1c1ab113Stone Tickle extract string.split() 8 months ago
                                                                                
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "posix.h"

#include <assert.h>
#include <string.h>

#include "args.h"
#include "buf_size.h"
#include "compilers.h"
#include "functions/environment.h"
#include "lang/interpreter.h"
#include "lang/workspace.h"
#include "log.h"
#include "platform/path.h"

void
push_args(struct workspace *wk, uint32_t arr, const struct args *args)
{
	uint32_t i;
	for (i = 0; i < args->len; ++i) {
		obj_array_push(wk, arr, make_str(wk, args->args[i]));
	}
}

void
push_args_null_terminated(struct workspace *wk, uint32_t arr, char *const *argv)
{
	char *const *arg;
	for (arg = argv; *arg; ++arg) {
		obj_array_push(wk, arr, make_str(wk, *arg));
	}
}

void
push_argv_single(const char **argv, uint32_t *len, uint32_t max, const char *arg)
{
	assert(*len < max && "too many arguments");
	argv[*len] = arg;
	++(*len);
}

void
push_argv(const char **argv, uint32_t *len, uint32_t max, const struct args *args)
{
	uint32_t i;
	for (i = 0; i < args->len; ++i) {
		push_argv_single(argv, len, max, args->args[i]);
	}
}

static bool
escape_str(char *buf, uint32_t len, const char *str, char esc_char, const char *need_escaping)
{
	const char *s = str;
	uint32_t bufi = 0;
	bool esc;

	for (; *s; ++s) {
		esc = strchr(need_escaping, *s) != NULL;

		if (bufi + 1 + (esc ? 1 : 0) >= len - 1) {
			LOG_E("truncation during escape");
			return false;
		}

		if (esc) {
			buf[bufi] = esc_char;
			++bufi;
		}

		buf[bufi] = *s;
		++bufi;
	}

	assert(bufi < len);
	buf[bufi] = 0;
	return true;
}

static bool
shell_escape(char *buf, uint32_t len, const char *str)
{
	return escape_str(buf, len, str, '\\', "\"'$ \\><&#");
}

bool
ninja_escape(char *buf, uint32_t len, const char *str)
{
	return escape_str(buf, len, str, '$', " :$");
}

typedef bool ((*escape_func)(char *buf, uint32_t len, const char *str));

struct join_args_iter_ctx {
	uint32_t i, len;
	uint32_t *obj;
	escape_func escape;
};

static enum iteration_result
join_args_iter(struct workspace *wk, void *_ctx, uint32_t val)
{
	struct join_args_iter_ctx *ctx = _ctx;

	assert(get_obj(wk, val)->type == obj_string);

	const char *s = get_cstr(wk, val);
	char buf[BUF_SIZE_4k];

	if (ctx->escape) {
		if (!ctx->escape(buf, BUF_SIZE_4k, s)) {
			return ir_err;
		}

		s = buf;
	}

	wk_str_app(wk, &get_obj(wk, *ctx->obj)->dat.str, s);

	if (ctx->i < ctx->len - 1) {
		wk_str_app(wk, &get_obj(wk, *ctx->obj)->dat.str, " ");
	}

	++ctx->i;

	return ir_cont;
}

static uint32_t
join_args(struct workspace *wk, uint32_t arr, escape_func escape)
{
	uint32_t obj;
	make_obj(wk, &obj, obj_string)->dat.str = wk_str_push(wk, "");

	struct join_args_iter_ctx ctx = {
		.obj = &obj,
		.len = get_obj(wk, arr)->dat.arr.len,
		.escape = escape
	};

	if (!obj_array_foreach(wk, arr, &ctx, join_args_iter)) {
		assert(false);
		return 0;
	}

	return obj;
}

uint32_t
join_args_plain(struct workspace *wk, uint32_t arr)
{
	return join_args(wk, arr, NULL);
}

uint32_t
join_args_shell(struct workspace *wk, uint32_t arr)
{
	return join_args(wk, arr, shell_escape);
}

uint32_t
join_args_ninja(struct workspace *wk, uint32_t arr)
{
	return join_args(wk, arr, ninja_escape);
}

struct join_args_argv_iter_ctx {
	const char **argv;
	uint32_t len;
	uint32_t i;
};

static enum iteration_result
join_args_argv_iter(struct workspace *wk, void *_ctx, uint32_t v)
{
	struct join_args_argv_iter_ctx *ctx = _ctx;

	if (ctx->i >= ctx->len - 1) {
		return ir_err;
	}

	ctx->argv[ctx->i] = get_cstr(wk, v);
	++ctx->i;
	return ir_cont;
}

bool
join_args_argv(struct workspace *wk, const char **argv, uint32_t len, uint32_t arr)
{
	struct join_args_argv_iter_ctx ctx = {
		.argv = argv,
		.len = len,
	};

	if (!obj_array_foreach(wk, arr, &ctx, join_args_argv_iter)) {
		return false;
	}

	assert(ctx.i < ctx.len);
	ctx.argv[ctx.i] = NULL;

	return true;
}

static enum iteration_result
arr_to_args_iter(struct workspace *wk, void *_ctx, uint32_t src)
{
	uint32_t *res = _ctx;
	uint32_t str, str_obj;

	struct obj *o = get_obj(wk, src);

	switch (o->type) {
	case obj_string:
		str = o->dat.str;
		break;
	case obj_file:
		str = o->dat.file;
		break;
	case obj_build_target: {
		char tmp[PATH_MAX];

		if (!path_join(tmp, PATH_MAX, get_cstr(wk, o->dat.tgt.build_dir),
			get_cstr(wk, o->dat.tgt.build_name))) {
			return ir_err;
		}

		str = wk_str_push(wk, tmp);
		break;
	}
	case obj_custom_target: {
		obj output_arr = get_obj(wk, src)->dat.custom_target.output;
		if (!obj_array_foreach(wk, output_arr, res, arr_to_args_iter)) {
			return ir_err;
		}
		return ir_cont;
	}
	case obj_external_program:
		str = o->dat.external_program.full_path;
		break;
	default:
		LOG_E("cannot convert '%s' to argument", obj_type_to_s(o->type));
		return ir_err;
	}

	make_obj(wk, &str_obj, obj_string)->dat.str = str;
	obj_array_push(wk, *res, str_obj);
	return ir_cont;
}

bool
arr_to_args(struct workspace *wk, uint32_t arr, uint32_t *res)
{
	make_obj(wk, res, obj_array);

	return obj_array_foreach_flat(wk, arr, res, arr_to_args_iter);
}

struct env_to_envp_ctx {
	char *envp[MAX_ENV + 1], envd[MAX_ENV][BUF_SIZE_4k];
	uint32_t i;
};

static bool
push_envp_str(struct env_to_envp_ctx *ctx, const char *s)
{
	if (ctx->i >= MAX_ENV) {
		LOG_E("too many environment elements (max: %d)", MAX_ENV);
		return false;
	}

	if (!strchr(s, '=')) {
		LOG_E("env elements must be of the format key=value: '%s'", s);
		return false;
	}

	if (strlen(s) + 1 >= BUF_SIZE_4k) {
		LOG_E("env element too long: '%s'", s);
		return false;
	}

	strncpy(ctx->envd[ctx->i], s, BUF_SIZE_4k);
	ctx->envp[ctx->i] = ctx->envd[ctx->i];
	++ctx->i;
	return true;
}

static bool
push_envp_key_val(struct env_to_envp_ctx *ctx, const char *k, const char *v)
{
	if (strlen(k) + strlen(v) + 1 >= BUF_SIZE_4k) {
		LOG_E("env element too long: '%s=%s'", k, v);
		return false;
	}

	char buf[BUF_SIZE_4k];
	sprintf(buf, "%s=%s", k, v);
	return push_envp_str(ctx, buf);
}

static enum iteration_result
env_to_envp_arr_iter(struct workspace *wk, void *_ctx, obj val)
{
	struct env_to_envp_ctx *ctx = _ctx;

	if (!push_envp_str(ctx, get_cstr(wk, val))) {
		return ir_err;
	}

	return ir_cont;
}

static enum iteration_result
env_to_envp_dict_iter(struct workspace *wk, void *_ctx, obj key, obj val)
{
	struct env_to_envp_ctx *ctx = _ctx;

	if (!push_envp_key_val(ctx, get_cstr(wk, key), get_cstr(wk, val))) {
		return ir_err;
	}

	return ir_cont;
}

bool
env_to_envp(struct workspace *wk, uint32_t err_node, char *const *ret[], obj val, enum env_to_envp_flags flags)
{
	static struct env_to_envp_ctx ctx = { 0 };
	memset(&ctx, 0, sizeof(struct env_to_envp_ctx));

	*ret = ctx.envp;

	push_envp_key_val(&ctx, "MESON_BUILD_ROOT", wk->build_root);
	push_envp_key_val(&ctx, "MESON_SOURCE_ROOT", wk->source_root);

	if (flags & env_to_envp_flag_dist_root) {
		assert(false && "TODO");
	}

	if (flags & env_to_envp_flag_subdir) {
		push_envp_key_val(&ctx, "MESON_SUBDIR", get_cstr(wk, current_project(wk)->cwd));
	}

	if (!val) {
		return true;
	}

	struct obj *v = get_obj(wk, val);
	switch (v->type) {
	case obj_string:
		return push_envp_str(&ctx, get_cstr(wk, v->dat.str));
	case obj_array:
		return obj_array_foreach(wk, val, &ctx, env_to_envp_arr_iter);
	case obj_dict:
		if (!typecheck_environment_dict(wk, err_node, val)) {
			return false;
		}
		return obj_dict_foreach(wk, val, &ctx, env_to_envp_dict_iter);
	case obj_environment:
		return obj_dict_foreach(wk, v->dat.environment.env, &ctx, env_to_envp_dict_iter);
	default:
		interp_error(wk, err_node, "unable to coerce type '%s' into environment", obj_type_to_s(v->type));
		return false;
	}
}