~lattis/muon

ref: f9a8cbad39b665a9496cd3adb4756e85ee5ec97c muon/src/args.c -rw-r--r-- 5.3 KiB
f9a8cbadStone Tickle print stdout/stderr of failing test 4 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
#include "posix.h"

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

#include "args.h"
#include "buf_size.h"
#include "compilers.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_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 = wk_objstr(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 {
	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] = wk_objstr(wk, v);
	++ctx->i;
	return ir_cont;
}

bool
join_args_argv(struct workspace *wk, 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, wk_str(wk, o->dat.tgt.build_dir),
			wk_str(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;
	}
	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);
}

static bool
push_envp(char envp[MAX_ENV][BUF_SIZE_4k], uint32_t *i, const char *k, const char *v)
{
	if (*i >= MAX_ENV) {
		return false;
	}

	if (strlen(k) + strlen(v) + 1 >= BUF_SIZE_4k) {
		return false;
	}

	sprintf(envp[*i], "%s=%s", k, v);
	++(*i);
	return true;
}

bool
build_envp(struct workspace *wk, char *const *ret[], enum build_envp_flags flags)
{
	static char *envp[MAX_ENV + 1],
		    envd[MAX_ENV][BUF_SIZE_4k];
	uint32_t i = 0, j;

	push_envp(envd, &i, "MESON_BUILD_ROOT", wk->build_root);
	push_envp(envd, &i, "MESON_SOURCE_ROOT", wk->source_root);

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

	if (flags & build_envp_flag_subdir) {
		push_envp(envd, &i, "MESON_SUBDIR", wk_str(wk, current_project(wk)->cwd));
	}

	for (j = 0; j < i; ++j) {
		envp[j] = envd[j];
	}
	assert(j < MAX_ARGS);
	envp[j] = NULL;

	*ret = envp;
	return true;
}