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