summaryrefslogtreecommitdiff
path: root/lib/libfuse/fuse_opt.c
blob: bbb2ad38142c154b92834f424e3ad4d5137c31a1 (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* $OpenBSD: fuse_opt.c,v 1.16 2016/05/24 20:55:32 okan Exp $ */
/*
 * Copyright (c) 2013 Sylvestre Gallon <ccna.syl@gmail.com>
 * Copyright (c) 2013 Stefan Sperling <stsp@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

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

#include "debug.h"
#include "fuse_opt.h"
#include "fuse_private.h"

static void
free_argv(char **argv, int argc)
{
	int i;

	for (i = 0; i < argc; i++)
		free(argv[i]);
	free(argv);
}

static int
alloc_argv(struct fuse_args *args)
{
	char **argv;
	int i;

	assert(!args->allocated);

	argv = calloc(args->argc, sizeof(*argv));
	if (argv == NULL)
		return (-1);

	if (args->argv) {
		for (i = 0; i < args->argc; i++) {
			argv[i] = strdup(args->argv[i]);
			if (argv[i] == NULL) {
				free_argv(argv, i + 1);
				return (-1);
			}
		}
	}

	args->allocated = 1;
	args->argv = argv;

	return (0);
}

static int
match_opt(const char *templ, const char *opt)
{
	const char *o, *t;
	char *arg;

	arg = strpbrk(templ, " =");

	/* verify template */
	t = templ;
	if (*t == '-') {
		t++;
		if (*t == '-')
			t++;
		if (*t == 'o' || *t == '\0')
			return (0);
	}

	/* skip leading -, -o, and -- in option name */
	o = opt;
	if (*o == '-') {
		o++;
		if (*o == 'o' || *o == '-')
			o++;
	}

	/* empty option name is invalid */
	if (*o == '\0')
		return (0);

	/* match option name */
	while (*t && *o) {
		if (*t++ != *o++)
			return (0);
		if (arg && t == arg) {
			if (*o != ' ' && *o != '=')
				return (0);
			o++; /* o now points at argument */
			if (*o == '\0')
				return (0);
			break;
		}
	}

	/* match argument */
	if (arg) {
		if (t != arg)
			return (0);
		t++;
		/* does template have an argument? */
		if (*t != '%' && *t != '\0')
			return (0);
		if (*t == '%' && t[1] == '\0')
			return (0);
		/* yes it does, consume argument in opt */
		while (*o && *o != ' ')
			o++;
	} else if (*t != '\0')
		return (0);

	/* we should have consumed entire opt string */
	if (*o != '\0')
		return (0);

	return (1);
}

static int
add_opt(char **opts, const char *opt)
{
	char *new_opts;

	if (*opts == NULL) {
		*opts = strdup(opt);
		if (*opts == NULL)
			return (-1);
		return (0);
	}

	if (asprintf(&new_opts, "%s,%s", *opts, opt) == -1)
		return (-1);

	free(*opts);
	*opts = new_opts;
	return (0);
}

int
fuse_opt_add_opt(char **opts, const char *opt)
{
	int ret;

	if (opt == NULL || opt[0] == '\0')
		return (-1);

	ret = add_opt(opts, opt);
	return (ret);
}

int
fuse_opt_add_opt_escaped(char **opts, const char *opt)
{
	size_t size = 0, escaped = 0;
	const char *s = opt;
	char *escaped_opt, *p;
	int ret;

	if (opt == NULL || opt[0] == '\0')
		return (-1);

	while (*s) {
		/* malloc(size + escaped) overflow check */
		if (size >= (SIZE_MAX / 2))
			return (-1);

		if (*s == ',' || *s == '\\')
			escaped++;
		s++;
		size++;
	}

	if (escaped > 0) {
		escaped_opt = malloc(size + escaped);
		if (escaped_opt == NULL)
			return (-1);
		s = opt;
		p = escaped_opt;
		while (*s) {
			switch (*s) {
			case ',':
			case '\\':
				*p++ = '\\';
				/* FALLTHROUGH */
			default:
				*p++ = *s++;
			}
		}
		*p = '\0';
	} else {
		escaped_opt = strdup(opt);
		if (escaped_opt == NULL)
			return (-1);
	}

	ret = add_opt(opts, escaped_opt);
	free(escaped_opt);
	return (ret);
}

int
fuse_opt_add_arg(struct fuse_args *args, const char *name)
{
	return (fuse_opt_insert_arg(args, args->argc, name));
}

static int
parse_opt(const struct fuse_opt *o, const char *val, void *data,
    fuse_opt_proc_t f, struct fuse_args *arg)
{
	int found, ret, keyval;
	size_t idx;

	ret = 0;
	found = 0;
	keyval = 0;

	/* check if it is a key=value entry */
	idx = strcspn(val, "=");
	if (idx != strlen(val)) {
		idx++;
		keyval = 1;
	}

	for(; o->templ; o++) {
		if ((keyval && strncmp(val, o->templ, idx) == 0) ||
		    (!keyval && strcmp(val, o->templ) == 0)) {
			if (o->val == FUSE_OPT_KEY_DISCARD)
				return (1);

			if (FUSE_OPT_IS_OPT_KEY(o)) {
				if (keyval)
					ret = f(data, &val[idx], o->val, arg);
				else
					ret = f(data, val, o->val, arg);
			}

			if (o->off != ULONG_MAX && data && o->val >= 0) {
				ret = f(data, val, o->val, arg);
				int *addr = (int *)(data + o->off);
				*addr = o->val;
				ret = 0;
			}

			if (ret == -1)
				return (ret);
			if (ret == 1)
				fuse_opt_add_arg(arg, val);
			found++;
			break;
		}
	}

	if (!found) {
		ret = f(data, val, FUSE_OPT_KEY_OPT, arg);
		if (ret == 1)
			fuse_opt_add_arg(arg, val);
		return (ret);
	}

	return (ret);
}

/*
 * this code is not very sexy but we are forced to follow
 * the fuse api.
 *
 * when f() returns 1 we need to keep the arg
 * when f() returns 0 we need to discard the arg
 */
int
fuse_opt_parse(struct fuse_args *args, void *data,
    const struct fuse_opt *opt, fuse_opt_proc_t f)
{
	struct fuse_args outargs;
	const char *arg;
	int ret = 0;
	int i;

	if (!f || !args || !args->argc || !args->argv)
		return (0);

	bzero(&outargs, sizeof(outargs));
	fuse_opt_add_arg(&outargs, args->argv[0]);

	for (i = 1; i < args->argc; i++) {
		arg = args->argv[i];

		/* not - and not -- */
		if (arg[0] != '-') {
			ret = f(data, arg, FUSE_OPT_KEY_NONOPT, &outargs);

			if (ret == 1)
				fuse_opt_add_arg(&outargs, arg);
			if (ret == -1)
				goto err;
		} else if (arg[1] == 'o') {
			if (arg[2])
				arg += 2;	/* -ofoo,bar */
			else
				arg = args->argv[++i];

			ret = parse_opt(opt, arg, data, f, &outargs);

			if (ret == -1)
				goto err;
		} else {
			ret = parse_opt(opt, arg, data, f, &outargs);

			if (ret == -1)
				goto err;
		}
	}
	ret = 0;

err:
	/* Update args */
	fuse_opt_free_args(args);
	args->allocated = outargs.allocated;
	args->argc = outargs.argc;
	args->argv = outargs.argv;

	return (ret);
}

int
fuse_opt_insert_arg(struct fuse_args *args, int p, const char *name)
{
	char **av;
	char *this_arg, *next_arg;
	int i;

	if (name == NULL)
		return (-1);

	if (!args->allocated && alloc_argv(args))
		return (-1);

	if (p < 0 || p > args->argc)
		return (-1);

	av = reallocarray(args->argv, args->argc + 2, sizeof(*av));
	if (av == NULL)
		return (-1);

	this_arg = strdup(name);
	if (this_arg == NULL) {
		free(av);
		return (-1);
	}

	args->argc++;
	args->argv = av;
	args->argv[args->argc] = NULL;
	for (i = p; i < args->argc; i++) {
		next_arg = args->argv[i];
		args->argv[i] = this_arg;
		this_arg = next_arg;
	}
	return (0);
}

void
fuse_opt_free_args(struct fuse_args *args)
{
	if (!args->allocated)
		return;

	free_argv(args->argv, args->argc);
	args->argv = 0;
	args->argc = 0;
	args->allocated = 0;
}

int
fuse_opt_match(const struct fuse_opt *opts, const char *opt)
{
	const struct fuse_opt *this_opt = opts;

	if (opt == NULL || opt[0] == '\0')
		return (0);

	while (this_opt->templ) {
		if (match_opt(this_opt->templ, opt))
			return (1);
		this_opt++;
	}

	return (0);
}