xref: /netbsd-src/lib/librefuse/refuse_opt.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /* 	$NetBSD: refuse_opt.c,v 1.18 2016/11/16 16:11:42 pho Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 Juan Romero Pardines.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 
30 #include <err.h>
31 #include <fuse.h>
32 #include <fuse_opt.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #ifdef FUSE_OPT_DEBUG
39 #define DPRINTF(x)	do { printf x; } while ( /* CONSTCOND */ 0)
40 #else
41 #define DPRINTF(x)
42 #endif
43 
44 /*
45  * Public API.
46  */
47 
48 /* ARGSUSED */
49 int
50 fuse_opt_add_arg(struct fuse_args *args, const char *arg)
51 {
52 	struct fuse_args	*ap;
53 
54 	if (args->allocated == 0) {
55 		ap = fuse_opt_deep_copy_args(args->argc, args->argv);
56 		args->argv = ap->argv;
57 		args->argc = ap->argc;
58 		args->allocated = ap->allocated;
59 		(void) free(ap);
60 	} else if (args->allocated == args->argc) {
61 		void *a;
62 		int na = args->allocated + 10;
63 
64 		if ((a = realloc(args->argv, na * sizeof(*args->argv))) == NULL)
65 			return -1;
66 
67 		args->argv = a;
68 		args->allocated = na;
69 	}
70 	DPRINTF(("%s: arguments passed: [arg:%s]\n", __func__, arg));
71 	if ((args->argv[args->argc++] = strdup(arg)) == NULL)
72 		err(1, "fuse_opt_add_arg");
73 	args->argv[args->argc] = NULL;
74         return 0;
75 }
76 
77 struct fuse_args *
78 fuse_opt_deep_copy_args(int argc, char **argv)
79 {
80 	struct fuse_args	*ap;
81 	int			 i;
82 
83 	if ((ap = malloc(sizeof(*ap))) == NULL)
84 		err(1, "_fuse_deep_copy_args");
85 	/* deep copy args structure into channel args */
86 	ap->allocated = ((argc / 10) + 1) * 10;
87 
88 	if ((ap->argv = calloc((size_t)ap->allocated,
89 	    sizeof(*ap->argv))) == NULL)
90 		err(1, "_fuse_deep_copy_args");
91 
92 	for (i = 0; i < argc; i++) {
93 		if ((ap->argv[i] = strdup(argv[i])) == NULL)
94 			err(1, "_fuse_deep_copy_args");
95 	}
96 	ap->argv[ap->argc = i] = NULL;
97 	return ap;
98 }
99 
100 void
101 fuse_opt_free_args(struct fuse_args *ap)
102 {
103 	if (ap) {
104 		if (ap->allocated) {
105 			int	i;
106 			for (i = 0; i < ap->argc; i++) {
107 				free(ap->argv[i]);
108 			}
109 			free(ap->argv);
110 		}
111 		ap->argv = NULL;
112 		ap->allocated = ap->argc = 0;
113 	}
114 }
115 
116 /* ARGSUSED */
117 int
118 fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
119 {
120 	int	i;
121 	int	na;
122 	void   *a;
123 
124 	DPRINTF(("%s: arguments passed: [pos=%d] [arg=%s]\n",
125 	    __func__, pos, arg));
126 	if (args->argv == NULL) {
127 		na = 10;
128 		a = malloc(na * sizeof(*args->argv));
129 	} else {
130 		na = args->allocated + 10;
131 		a = realloc(args->argv, na * sizeof(*args->argv));
132 	}
133 	if (a == NULL) {
134 		warn("fuse_opt_insert_arg");
135 		return -1;
136 	}
137 	args->argv = a;
138 	args->allocated = na;
139 
140 	for (i = args->argc++; i > pos; --i) {
141 		args->argv[i] = args->argv[i - 1];
142 	}
143 	if ((args->argv[pos] = strdup(arg)) == NULL)
144 		err(1, "fuse_opt_insert_arg");
145 	args->argv[args->argc] = NULL;
146 	return 0;
147 }
148 
149 static int add_opt(char **opts, const char *opt, bool escape)
150 {
151 	const size_t orig_len = *opts == NULL ? 0 : strlen(*opts);
152 	char* buf = realloc(*opts, orig_len + 1 + strlen(opt) * 2 + 1);
153 
154 	if (buf == NULL) {
155 		return -1;
156 	}
157 	*opts = buf;
158 
159 	if (orig_len > 0) {
160 		buf += orig_len;
161 		*buf++ = ',';
162 	}
163 
164 	for (; *opt; opt++) {
165 		if (escape && (*opt == ',' || *opt == '\\')) {
166 			*buf++ = '\\';
167 		}
168 		*buf++ = *opt;
169 	}
170 	*buf = '\0';
171 
172 	return 0;
173 }
174 
175 int fuse_opt_add_opt(char **opts, const char *opt)
176 {
177 	DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
178 	    __func__, *opts, opt));
179 	return add_opt(opts, opt, false);
180 }
181 
182 int fuse_opt_add_opt_escaped(char **opts, const char *opt)
183 {
184 	DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
185 	    __func__, *opts, opt));
186 	return add_opt(opts, opt, true);
187 }
188 
189 static bool match_templ(const char *templ, const char *opt, int *sep_idx)
190 {
191 	const char *sep = strpbrk(templ, "= ");
192 
193 	if (sep != NULL && (sep[1] == '\0' || sep[1] == '%')) {
194 		const size_t cmp_len =
195 			sep[0] == '=' ? sep - templ + 1 : sep - templ;
196 
197 		if (strlen(opt) >= cmp_len && strncmp(templ, opt, cmp_len) == 0) {
198 			if (sep_idx != NULL)
199 				*sep_idx = sep - templ;
200 			return true;
201 		}
202 		else {
203 			return false;
204 		}
205 	}
206 	else {
207 		if (strcmp(templ, opt) == 0) {
208 			if (sep_idx != NULL)
209 				*sep_idx = -1;
210 			return true;
211 		}
212 		else {
213 			return false;
214 		}
215 	}
216 }
217 
218 static const struct fuse_opt *
219 find_opt(const struct fuse_opt *opts, const char *opt, int *sep_idx)
220 {
221 	for (; opts != NULL && opts->templ != NULL; opts++) {
222 		if (match_templ(opts->templ, opt, sep_idx))
223 			return opts;
224 	}
225 	return NULL;
226 }
227 
228 /*
229  * Returns 1 if opt was matched with any option from opts,
230  * otherwise returns 0.
231  */
232 int
233 fuse_opt_match(const struct fuse_opt *opts, const char *opt)
234 {
235 	return find_opt(opts, opt, NULL) != NULL ? 1 : 0;
236 }
237 
238 static int call_proc(fuse_opt_proc_t proc, void* data,
239 		const char* arg, int key, struct fuse_args *outargs, bool is_opt)
240 {
241 	if (key == FUSE_OPT_KEY_DISCARD)
242 		return 0;
243 
244 	if (key != FUSE_OPT_KEY_KEEP && proc != NULL) {
245 		const int rv = proc(data, arg, key, outargs);
246 
247 		if (rv == -1 || /* error   */
248 			rv ==  0    /* discard */)
249 			return rv;
250 	}
251 
252 	if (is_opt) {
253 		/* Do we already have "-o" at the beginning of outargs? */
254 		if (outargs->argc >= 3 && strcmp(outargs->argv[1], "-o") == 0) {
255 			/* Append the option to the comma-separated list. */
256 			if (fuse_opt_add_opt_escaped(&outargs->argv[2], arg) == -1)
257 				return -1;
258 		}
259 		else {
260 			/* Insert -o arg at the beginning. */
261 			if (fuse_opt_insert_arg(outargs, 1, "-o") == -1)
262 				return -1;
263 			if (fuse_opt_insert_arg(outargs, 2, arg) == -1)
264 				return -1;
265 		}
266 	}
267 	else {
268 		if (fuse_opt_add_arg(outargs, arg) == -1)
269 			return -1;
270 	}
271 
272 	return 0;
273 }
274 
275 /* Skip the current argv if possible. */
276 static int next_arg(const struct fuse_args *args, int *i)
277 {
278 	if (*i + 1 >= args->argc) {
279 		(void)fprintf(stderr, "fuse: missing argument"
280 				" after '%s'\n", args->argv[*i]);
281 		return -1;
282 	}
283 	else {
284 		*i += 1;
285 		return 0;
286 	}
287 }
288 
289 /* Parse a single argument with a matched template. */
290 static int
291 parse_matched_arg(const char* arg, struct fuse_args *outargs,
292 		const struct fuse_opt* opt, int sep_idx, void* data,
293 		fuse_opt_proc_t proc, bool is_opt)
294 {
295 	if (opt->offset == -1) {
296 		/* The option description does not want any variables to be
297 		 * updated.*/
298 		if (call_proc(proc, data, arg, opt->value, outargs, is_opt) == -1)
299 			return -1;
300 	}
301 	else {
302 		void *var = (char*)data + opt->offset;
303 
304 		if (sep_idx > 0 && opt->templ[sep_idx + 1] == '%') {
305 			/* "foo=%y" or "-x %y" */
306 			const char* param =
307 				opt->templ[sep_idx] == '=' ? &arg[sep_idx + 1] : &arg[sep_idx];
308 
309 			if (opt->templ[sep_idx + 2] == 's') {
310 				char* dup = strdup(param);
311 				if (dup == NULL)
312 					return -1;
313 
314 				*(char **)var = dup;
315 			}
316 			else {
317 				/* The format string is not a literal. We all know
318 				 * this is a bad idea but it's exactly what fuse_opt
319 				 * wants to do... */
320 #pragma GCC diagnostic push
321 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
322 				if (sscanf(param, &opt->templ[sep_idx + 1], var) == -1) {
323 #pragma GCC diagnostic pop
324 					(void)fprintf(stderr, "fuse: '%s' is not a "
325 								"valid parameter for option '%.*s'\n",
326 								param, sep_idx, opt->templ);
327 					return -1;
328 				}
329 			}
330 		}
331 		else {
332 			/* No parameter format is given. */
333 			*(int *)var = opt->value;
334 		}
335 	}
336 	return 0;
337 }
338 
339 /* Parse a single argument with matching templates. */
340 static int
341 parse_arg(struct fuse_args* args, int *argi, const char* arg,
342 		struct fuse_args *outargs, void *data,
343 		const struct fuse_opt *opts, fuse_opt_proc_t proc, bool is_opt)
344 {
345 	int sep_idx;
346 	const struct fuse_opt *opt = find_opt(opts, arg, &sep_idx);
347 
348 	if (opt) {
349 		/* An argument can match to multiple templates. Process them
350 		 * all. */
351 		for (; opt != NULL && opt->templ != NULL;
352 			opt = find_opt(++opt, arg, &sep_idx)) {
353 
354 			if (sep_idx > 0 && opt->templ[sep_idx] == ' ' &&
355 				arg[sep_idx] == '\0') {
356 				/* The template "-x %y" requests a separate
357 				 * parameter "%y". Try to find one. */
358 				char *new_arg;
359 				int rv;
360 
361 				if (next_arg(args, argi) == -1)
362 					return -1;
363 
364 				/* ...but processor callbacks expect a concatenated
365 				 * argument "-xfoo". */
366 				if ((new_arg = malloc(sep_idx +
367 									strlen(args->argv[*argi]) + 1)) == NULL)
368 					return -1;
369 
370 				strncpy(new_arg, arg, sep_idx); /* -x */
371 				strcpy(new_arg + sep_idx, args->argv[*argi]); /* foo */
372 				rv = parse_matched_arg(new_arg, outargs, opt, sep_idx,
373 									data, proc, is_opt);
374 				free(new_arg);
375 
376 				if (rv == -1)
377 					return -1;
378 			}
379 			else {
380 				int rv;
381 				rv = parse_matched_arg(arg, outargs, opt, sep_idx,
382 									data, proc, is_opt);
383 				if (rv == -1)
384 					return -1;
385 			}
386 		}
387 		return 0;
388 	}
389 	else {
390 		/* No templates matched to it so just invoke the callback. */
391 		return call_proc(proc, data, arg, FUSE_OPT_KEY_OPT, outargs, is_opt);
392 	}
393 }
394 
395 /* Parse a comma-separated list of options which possibly has escaped
396  * characters. */
397 static int
398 parse_opts(struct fuse_args *args, int *argi, const char* arg,
399 		struct fuse_args *outargs, void *data,
400 		const struct fuse_opt *opts, fuse_opt_proc_t proc)
401 {
402 	char *opt;
403 	size_t i, opt_len = 0;
404 
405 	/* An unescaped option can never be longer than the original
406 	 * list. */
407 	if ((opt = malloc(strlen(arg) + 1)) == NULL)
408 		return -1;
409 
410 	for (i = 0; i < strlen(arg); i++) {
411 		if (arg[i] == ',') {
412 			opt[opt_len] = '\0';
413 			if (parse_arg(args, argi, opt, outargs,
414 						data, opts, proc, true) == -1) {
415 				free(opt);
416 				return -1;
417 			}
418 			/* Start a new option. */
419 			opt_len = 0;
420 		}
421 		else if (arg[i] == '\\' && arg[i+1] != '\0') {
422 			/* Unescape it. */
423 			opt[opt_len++] = arg[i+1];
424 			i++;
425 		}
426 		else {
427 			opt[opt_len++] = arg[i];
428 		}
429 	}
430 
431 	/* Parse the last option here. */
432 	opt[opt_len] = '\0';
433 	if (parse_arg(args, argi, opt, outargs, data, opts, proc, true) == -1) {
434 		free(opt);
435 		return -1;
436 	}
437 
438 	free(opt);
439 	return 0;
440 }
441 
442 static int
443 parse_all(struct fuse_args *args, struct fuse_args *outargs, void *data,
444 		const struct fuse_opt *opts, fuse_opt_proc_t proc)
445 {
446 	bool nonopt = false; /* Have we seen the "--" marker? */
447 	int i;
448 
449 	/* The first argument, the program name, is implicitly
450 	 * FUSE_OPT_KEY_KEEP. */
451 	if (args->argc > 0) {
452 		if (fuse_opt_add_arg(outargs, args->argv[0]) == -1)
453 			return -1;
454 	}
455 
456 	/* the real loop to process the arguments */
457 	for (i = 1; i < args->argc; i++) {
458 		const char *arg = args->argv[i];
459 
460 		/* argvn != -foo... */
461 		if (nonopt || arg[0] != '-') {
462 			if (call_proc(proc, data, arg, FUSE_OPT_KEY_NONOPT,
463 						outargs, false) == -1)
464 				return -1;
465 		}
466 		/* -o or -ofoo */
467 		else if (arg[1] == 'o') {
468 			/* -oblah,foo... */
469 			if (arg[2] != '\0') {
470 				/* skip -o */
471 				if (parse_opts(args, &i, arg + 2, outargs,
472 							data, opts, proc) == -1)
473 					return -1;
474 			}
475 			/* -o blah,foo... */
476 			else {
477 				if (next_arg(args, &i) == -1)
478 					return -1;
479 				if (parse_opts(args, &i, args->argv[i], outargs,
480 							data, opts, proc) == -1)
481 					return -1;
482 			}
483 		}
484 		/* -- */
485 		else if (arg[1] == '-' && arg[2] == '\0') {
486 			if (fuse_opt_add_arg(outargs, arg) == -1)
487 				return -1;
488 			nonopt = true;
489 		}
490 		/* -foo */
491 		else {
492 			if (parse_arg(args, &i, arg, outargs,
493 						data, opts, proc, false) == -1)
494 				return -1;
495 		}
496 	}
497 
498 	/* The "--" marker at the last of outargs should be removed */
499 	if (nonopt && strcmp(outargs->argv[outargs->argc - 1], "--") == 0) {
500 		free(outargs->argv[outargs->argc - 1]);
501 		outargs->argv[--outargs->argc] = NULL;
502 	}
503 
504 	return 0;
505 }
506 
507 int
508 fuse_opt_parse(struct fuse_args *args, void *data,
509 		const struct fuse_opt *opts, fuse_opt_proc_t proc)
510 {
511 	struct fuse_args outargs = FUSE_ARGS_INIT(0, NULL);
512 	int rv;
513 
514 	if (!args || !args->argv || !args->argc)
515 		return 0;
516 
517 	rv = parse_all(args, &outargs, data, opts, proc);
518 	if (rv != -1) {
519 		/* Succeeded. Swap the outargs and args. */
520 		struct fuse_args tmp = *args;
521 		*args = outargs;
522 		outargs = tmp;
523 	}
524 
525 	fuse_opt_free_args(&outargs);
526 	return rv;
527 }
528