Home | History | Annotate | Line # | Download | only in librefuse
refuse_opt.c revision 1.18
      1  1.18       pho /* 	$NetBSD: refuse_opt.c,v 1.18 2016/11/16 16:11:42 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.13     pooka #include <fuse.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 		void *a;
     62  1.10  christos 		int na = args->allocated + 10;
     63  1.10  christos 
     64  1.10  christos 		if ((a = realloc(args->argv, na * sizeof(*args->argv))) == NULL)
     65  1.10  christos 			return -1;
     66  1.10  christos 
     67  1.10  christos 		args->argv = a;
     68  1.10  christos 		args->allocated = na;
     69   1.5       agc 	}
     70   1.1   xtraeme 	DPRINTF(("%s: arguments passed: [arg:%s]\n", __func__, arg));
     71  1.10  christos 	if ((args->argv[args->argc++] = strdup(arg)) == NULL)
     72  1.10  christos 		err(1, "fuse_opt_add_arg");
     73  1.11  christos 	args->argv[args->argc] = NULL;
     74   1.5       agc         return 0;
     75   1.1   xtraeme }
     76   1.1   xtraeme 
     77  1.11  christos struct fuse_args *
     78  1.11  christos fuse_opt_deep_copy_args(int argc, char **argv)
     79  1.11  christos {
     80  1.11  christos 	struct fuse_args	*ap;
     81  1.14     lukem 	int			 i;
     82  1.11  christos 
     83  1.11  christos 	if ((ap = malloc(sizeof(*ap))) == NULL)
     84  1.11  christos 		err(1, "_fuse_deep_copy_args");
     85  1.11  christos 	/* deep copy args structure into channel args */
     86  1.11  christos 	ap->allocated = ((argc / 10) + 1) * 10;
     87  1.11  christos 
     88  1.11  christos 	if ((ap->argv = calloc((size_t)ap->allocated,
     89  1.11  christos 	    sizeof(*ap->argv))) == NULL)
     90  1.11  christos 		err(1, "_fuse_deep_copy_args");
     91  1.11  christos 
     92  1.11  christos 	for (i = 0; i < argc; i++) {
     93  1.11  christos 		if ((ap->argv[i] = strdup(argv[i])) == NULL)
     94  1.11  christos 			err(1, "_fuse_deep_copy_args");
     95  1.11  christos 	}
     96  1.11  christos 	ap->argv[ap->argc = i] = NULL;
     97  1.11  christos 	return ap;
     98  1.11  christos }
     99  1.11  christos 
    100   1.1   xtraeme void
    101  1.11  christos fuse_opt_free_args(struct fuse_args *ap)
    102   1.1   xtraeme {
    103  1.18       pho 	if (ap) {
    104  1.18       pho 		if (ap->allocated) {
    105  1.18       pho 			int	i;
    106  1.18       pho 			for (i = 0; i < ap->argc; i++) {
    107  1.18       pho 				free(ap->argv[i]);
    108  1.18       pho 			}
    109  1.18       pho 			free(ap->argv);
    110  1.18       pho 		}
    111  1.18       pho 		ap->argv = NULL;
    112  1.18       pho 		ap->allocated = ap->argc = 0;
    113  1.11  christos 	}
    114   1.1   xtraeme }
    115   1.1   xtraeme 
    116   1.4       agc /* ARGSUSED */
    117   1.1   xtraeme int
    118   1.1   xtraeme fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
    119   1.1   xtraeme {
    120   1.5       agc 	int	i;
    121  1.10  christos 	int	na;
    122  1.10  christos 	void   *a;
    123   1.5       agc 
    124   1.4       agc 	DPRINTF(("%s: arguments passed: [pos=%d] [arg=%s]\n",
    125   1.4       agc 	    __func__, pos, arg));
    126  1.10  christos 	if (args->argv == NULL) {
    127  1.10  christos 		na = 10;
    128  1.10  christos 		a = malloc(na * sizeof(*args->argv));
    129  1.10  christos 	} else {
    130  1.10  christos 		na = args->allocated + 10;
    131  1.10  christos 		a = realloc(args->argv, na * sizeof(*args->argv));
    132  1.10  christos 	}
    133  1.10  christos 	if (a == NULL) {
    134  1.10  christos 		warn("fuse_opt_insert_arg");
    135  1.10  christos 		return -1;
    136  1.10  christos 	}
    137  1.10  christos 	args->argv = a;
    138  1.10  christos 	args->allocated = na;
    139  1.10  christos 
    140  1.10  christos 	for (i = args->argc++; i > pos; --i) {
    141   1.5       agc 		args->argv[i] = args->argv[i - 1];
    142   1.5       agc 	}
    143  1.10  christos 	if ((args->argv[pos] = strdup(arg)) == NULL)
    144  1.10  christos 		err(1, "fuse_opt_insert_arg");
    145  1.11  christos 	args->argv[args->argc] = NULL;
    146   1.5       agc 	return 0;
    147   1.1   xtraeme }
    148   1.1   xtraeme 
    149  1.16       pho static int add_opt(char **opts, const char *opt, bool escape)
    150  1.16       pho {
    151  1.16       pho 	const size_t orig_len = *opts == NULL ? 0 : strlen(*opts);
    152  1.16       pho 	char* buf = realloc(*opts, orig_len + 1 + strlen(opt) * 2 + 1);
    153  1.16       pho 
    154  1.16       pho 	if (buf == NULL) {
    155  1.16       pho 		return -1;
    156  1.16       pho 	}
    157  1.16       pho 	*opts = buf;
    158  1.16       pho 
    159  1.16       pho 	if (orig_len > 0) {
    160  1.16       pho 		buf += orig_len;
    161  1.16       pho 		*buf++ = ',';
    162  1.16       pho 	}
    163  1.16       pho 
    164  1.16       pho 	for (; *opt; opt++) {
    165  1.16       pho 		if (escape && (*opt == ',' || *opt == '\\')) {
    166  1.16       pho 			*buf++ = '\\';
    167  1.16       pho 		}
    168  1.16       pho 		*buf++ = *opt;
    169  1.16       pho 	}
    170  1.16       pho 	*buf = '\0';
    171  1.16       pho 
    172  1.16       pho 	return 0;
    173  1.16       pho }
    174  1.16       pho 
    175   1.1   xtraeme int fuse_opt_add_opt(char **opts, const char *opt)
    176   1.1   xtraeme {
    177   1.1   xtraeme 	DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
    178   1.1   xtraeme 	    __func__, *opts, opt));
    179  1.16       pho 	return add_opt(opts, opt, false);
    180  1.16       pho }
    181  1.16       pho 
    182  1.16       pho int fuse_opt_add_opt_escaped(char **opts, const char *opt)
    183  1.16       pho {
    184  1.16       pho 	DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
    185  1.16       pho 	    __func__, *opts, opt));
    186  1.16       pho 	return add_opt(opts, opt, true);
    187   1.1   xtraeme }
    188   1.1   xtraeme 
    189  1.18       pho static bool match_templ(const char *templ, const char *opt, int *sep_idx)
    190  1.17       pho {
    191  1.17       pho 	const char *sep = strpbrk(templ, "= ");
    192  1.17       pho 
    193  1.17       pho 	if (sep != NULL && (sep[1] == '\0' || sep[1] == '%')) {
    194  1.17       pho 		const size_t cmp_len =
    195  1.17       pho 			sep[0] == '=' ? sep - templ + 1 : sep - templ;
    196  1.17       pho 
    197  1.17       pho 		if (strlen(opt) >= cmp_len && strncmp(templ, opt, cmp_len) == 0) {
    198  1.17       pho 			if (sep_idx != NULL)
    199  1.17       pho 				*sep_idx = sep - templ;
    200  1.17       pho 			return true;
    201  1.17       pho 		}
    202  1.17       pho 		else {
    203  1.17       pho 			return false;
    204  1.17       pho 		}
    205  1.17       pho 	}
    206  1.17       pho 	else {
    207  1.17       pho 		if (strcmp(templ, opt) == 0) {
    208  1.17       pho 			if (sep_idx != NULL)
    209  1.18       pho 				*sep_idx = -1;
    210  1.17       pho 			return true;
    211  1.17       pho 		}
    212  1.17       pho 		else {
    213  1.17       pho 			return false;
    214  1.17       pho 		}
    215  1.17       pho 	}
    216  1.17       pho }
    217  1.17       pho 
    218  1.17       pho static const struct fuse_opt *
    219  1.18       pho find_opt(const struct fuse_opt *opts, const char *opt, int *sep_idx)
    220  1.17       pho {
    221  1.17       pho 	for (; opts != NULL && opts->templ != NULL; opts++) {
    222  1.17       pho 		if (match_templ(opts->templ, opt, sep_idx))
    223  1.17       pho 			return opts;
    224  1.17       pho 	}
    225  1.17       pho 	return NULL;
    226  1.17       pho }
    227  1.17       pho 
    228   1.1   xtraeme /*
    229  1.17       pho  * Returns 1 if opt was matched with any option from opts,
    230  1.17       pho  * otherwise returns 0.
    231   1.1   xtraeme  */
    232   1.1   xtraeme int
    233   1.1   xtraeme fuse_opt_match(const struct fuse_opt *opts, const char *opt)
    234   1.1   xtraeme {
    235  1.17       pho 	return find_opt(opts, opt, NULL) != NULL ? 1 : 0;
    236   1.1   xtraeme }
    237   1.1   xtraeme 
    238  1.18       pho static int call_proc(fuse_opt_proc_t proc, void* data,
    239  1.18       pho 		const char* arg, int key, struct fuse_args *outargs, bool is_opt)
    240   1.1   xtraeme {
    241  1.18       pho 	if (key == FUSE_OPT_KEY_DISCARD)
    242  1.18       pho 		return 0;
    243  1.18       pho 
    244  1.18       pho 	if (key != FUSE_OPT_KEY_KEEP && proc != NULL) {
    245  1.18       pho 		const int rv = proc(data, arg, key, outargs);
    246  1.18       pho 
    247  1.18       pho 		if (rv == -1 || /* error   */
    248  1.18       pho 			rv ==  0    /* discard */)
    249  1.18       pho 			return rv;
    250  1.18       pho 	}
    251  1.18       pho 
    252  1.18       pho 	if (is_opt) {
    253  1.18       pho 		/* Do we already have "-o" at the beginning of outargs? */
    254  1.18       pho 		if (outargs->argc >= 3 && strcmp(outargs->argv[1], "-o") == 0) {
    255  1.18       pho 			/* Append the option to the comma-separated list. */
    256  1.18       pho 			if (fuse_opt_add_opt_escaped(&outargs->argv[2], arg) == -1)
    257  1.18       pho 				return -1;
    258   1.1   xtraeme 		}
    259  1.18       pho 		else {
    260  1.18       pho 			/* Insert -o arg at the beginning. */
    261  1.18       pho 			if (fuse_opt_insert_arg(outargs, 1, "-o") == -1)
    262  1.18       pho 				return -1;
    263  1.18       pho 			if (fuse_opt_insert_arg(outargs, 2, arg) == -1)
    264  1.18       pho 				return -1;
    265   1.1   xtraeme 		}
    266   1.1   xtraeme 	}
    267  1.18       pho 	else {
    268  1.18       pho 		if (fuse_opt_add_arg(outargs, arg) == -1)
    269  1.18       pho 			return -1;
    270  1.18       pho 	}
    271   1.1   xtraeme 
    272   1.5       agc 	return 0;
    273   1.1   xtraeme }
    274   1.1   xtraeme 
    275  1.18       pho /* Skip the current argv if possible. */
    276  1.18       pho static int next_arg(const struct fuse_args *args, int *i)
    277   1.1   xtraeme {
    278  1.18       pho 	if (*i + 1 >= args->argc) {
    279  1.18       pho 		(void)fprintf(stderr, "fuse: missing argument"
    280  1.18       pho 				" after '%s'\n", args->argv[*i]);
    281  1.18       pho 		return -1;
    282  1.18       pho 	}
    283  1.18       pho 	else {
    284  1.18       pho 		*i += 1;
    285  1.18       pho 		return 0;
    286  1.18       pho 	}
    287  1.18       pho }
    288   1.1   xtraeme 
    289  1.18       pho /* Parse a single argument with a matched template. */
    290  1.18       pho static int
    291  1.18       pho parse_matched_arg(const char* arg, struct fuse_args *outargs,
    292  1.18       pho 		const struct fuse_opt* opt, int sep_idx, void* data,
    293  1.18       pho 		fuse_opt_proc_t proc, bool is_opt)
    294  1.18       pho {
    295  1.18       pho 	if (opt->offset == -1) {
    296  1.18       pho 		/* The option description does not want any variables to be
    297  1.18       pho 		 * updated.*/
    298  1.18       pho 		if (call_proc(proc, data, arg, opt->value, outargs, is_opt) == -1)
    299  1.18       pho 			return -1;
    300  1.18       pho 	}
    301  1.18       pho 	else {
    302  1.18       pho 		void *var = (char*)data + opt->offset;
    303  1.18       pho 
    304  1.18       pho 		if (sep_idx > 0 && opt->templ[sep_idx + 1] == '%') {
    305  1.18       pho 			/* "foo=%y" or "-x %y" */
    306  1.18       pho 			const char* param =
    307  1.18       pho 				opt->templ[sep_idx] == '=' ? &arg[sep_idx + 1] : &arg[sep_idx];
    308  1.18       pho 
    309  1.18       pho 			if (opt->templ[sep_idx + 2] == 's') {
    310  1.18       pho 				char* dup = strdup(param);
    311  1.18       pho 				if (dup == NULL)
    312  1.18       pho 					return -1;
    313  1.18       pho 
    314  1.18       pho 				*(char **)var = dup;
    315  1.18       pho 			}
    316  1.18       pho 			else {
    317  1.18       pho 				/* The format string is not a literal. We all know
    318  1.18       pho 				 * this is a bad idea but it's exactly what fuse_opt
    319  1.18       pho 				 * wants to do... */
    320  1.18       pho #pragma GCC diagnostic push
    321  1.18       pho #pragma GCC diagnostic ignored "-Wformat-nonliteral"
    322  1.18       pho 				if (sscanf(param, &opt->templ[sep_idx + 1], var) == -1) {
    323  1.18       pho #pragma GCC diagnostic pop
    324  1.18       pho 					(void)fprintf(stderr, "fuse: '%s' is not a "
    325  1.18       pho 								"valid parameter for option '%.*s'\n",
    326  1.18       pho 								param, sep_idx, opt->templ);
    327  1.18       pho 					return -1;
    328  1.18       pho 				}
    329  1.18       pho 			}
    330  1.18       pho 		}
    331  1.18       pho 		else {
    332  1.18       pho 			/* No parameter format is given. */
    333  1.18       pho 			*(int *)var = opt->value;
    334  1.18       pho 		}
    335  1.18       pho 	}
    336  1.18       pho 	return 0;
    337  1.18       pho }
    338  1.18       pho 
    339  1.18       pho /* Parse a single argument with matching templates. */
    340  1.18       pho static int
    341  1.18       pho parse_arg(struct fuse_args* args, int *argi, const char* arg,
    342  1.18       pho 		struct fuse_args *outargs, void *data,
    343  1.18       pho 		const struct fuse_opt *opts, fuse_opt_proc_t proc, bool is_opt)
    344  1.18       pho {
    345  1.18       pho 	int sep_idx;
    346  1.18       pho 	const struct fuse_opt *opt = find_opt(opts, arg, &sep_idx);
    347  1.18       pho 
    348  1.18       pho 	if (opt) {
    349  1.18       pho 		/* An argument can match to multiple templates. Process them
    350  1.18       pho 		 * all. */
    351  1.18       pho 		for (; opt != NULL && opt->templ != NULL;
    352  1.18       pho 			opt = find_opt(++opt, arg, &sep_idx)) {
    353  1.18       pho 
    354  1.18       pho 			if (sep_idx > 0 && opt->templ[sep_idx] == ' ' &&
    355  1.18       pho 				arg[sep_idx] == '\0') {
    356  1.18       pho 				/* The template "-x %y" requests a separate
    357  1.18       pho 				 * parameter "%y". Try to find one. */
    358  1.18       pho 				char *new_arg;
    359  1.18       pho 				int rv;
    360  1.18       pho 
    361  1.18       pho 				if (next_arg(args, argi) == -1)
    362  1.18       pho 					return -1;
    363  1.18       pho 
    364  1.18       pho 				/* ...but processor callbacks expect a concatenated
    365  1.18       pho 				 * argument "-xfoo". */
    366  1.18       pho 				if ((new_arg = malloc(sep_idx +
    367  1.18       pho 									strlen(args->argv[*argi]) + 1)) == NULL)
    368  1.18       pho 					return -1;
    369  1.18       pho 
    370  1.18       pho 				strncpy(new_arg, arg, sep_idx); /* -x */
    371  1.18       pho 				strcpy(new_arg + sep_idx, args->argv[*argi]); /* foo */
    372  1.18       pho 				rv = parse_matched_arg(new_arg, outargs, opt, sep_idx,
    373  1.18       pho 									data, proc, is_opt);
    374  1.18       pho 				free(new_arg);
    375  1.18       pho 
    376  1.18       pho 				if (rv == -1)
    377  1.18       pho 					return -1;
    378  1.18       pho 			}
    379  1.18       pho 			else {
    380  1.18       pho 				int rv;
    381  1.18       pho 				rv = parse_matched_arg(arg, outargs, opt, sep_idx,
    382  1.18       pho 									data, proc, is_opt);
    383  1.18       pho 				if (rv == -1)
    384  1.18       pho 					return -1;
    385  1.18       pho 			}
    386  1.18       pho 		}
    387   1.1   xtraeme 		return 0;
    388  1.18       pho 	}
    389  1.18       pho 	else {
    390  1.18       pho 		/* No templates matched to it so just invoke the callback. */
    391  1.18       pho 		return call_proc(proc, data, arg, FUSE_OPT_KEY_OPT, outargs, is_opt);
    392  1.18       pho 	}
    393  1.18       pho }
    394   1.1   xtraeme 
    395  1.18       pho /* Parse a comma-separated list of options which possibly has escaped
    396  1.18       pho  * characters. */
    397  1.18       pho static int
    398  1.18       pho parse_opts(struct fuse_args *args, int *argi, const char* arg,
    399  1.18       pho 		struct fuse_args *outargs, void *data,
    400  1.18       pho 		const struct fuse_opt *opts, fuse_opt_proc_t proc)
    401  1.18       pho {
    402  1.18       pho 	char *opt;
    403  1.18       pho 	size_t i, opt_len = 0;
    404  1.18       pho 
    405  1.18       pho 	/* An unescaped option can never be longer than the original
    406  1.18       pho 	 * list. */
    407  1.18       pho 	if ((opt = malloc(strlen(arg) + 1)) == NULL)
    408  1.18       pho 		return -1;
    409   1.1   xtraeme 
    410  1.18       pho 	for (i = 0; i < strlen(arg); i++) {
    411  1.18       pho 		if (arg[i] == ',') {
    412  1.18       pho 			opt[opt_len] = '\0';
    413  1.18       pho 			if (parse_arg(args, argi, opt, outargs,
    414  1.18       pho 						data, opts, proc, true) == -1) {
    415  1.18       pho 				free(opt);
    416  1.18       pho 				return -1;
    417  1.18       pho 			}
    418  1.18       pho 			/* Start a new option. */
    419  1.18       pho 			opt_len = 0;
    420  1.18       pho 		}
    421  1.18       pho 		else if (arg[i] == '\\' && arg[i+1] != '\0') {
    422  1.18       pho 			/* Unescape it. */
    423  1.18       pho 			opt[opt_len++] = arg[i+1];
    424  1.18       pho 			i++;
    425  1.18       pho 		}
    426  1.18       pho 		else {
    427  1.18       pho 			opt[opt_len++] = arg[i];
    428  1.18       pho 		}
    429  1.18       pho 	}
    430  1.18       pho 
    431  1.18       pho 	/* Parse the last option here. */
    432  1.18       pho 	opt[opt_len] = '\0';
    433  1.18       pho 	if (parse_arg(args, argi, opt, outargs, data, opts, proc, true) == -1) {
    434  1.18       pho 		free(opt);
    435  1.18       pho 		return -1;
    436  1.18       pho 	}
    437   1.1   xtraeme 
    438  1.18       pho 	free(opt);
    439  1.18       pho 	return 0;
    440  1.18       pho }
    441   1.1   xtraeme 
    442  1.18       pho static int
    443  1.18       pho parse_all(struct fuse_args *args, struct fuse_args *outargs, void *data,
    444  1.18       pho 		const struct fuse_opt *opts, fuse_opt_proc_t proc)
    445  1.18       pho {
    446  1.18       pho 	bool nonopt = false; /* Have we seen the "--" marker? */
    447  1.18       pho 	int i;
    448   1.1   xtraeme 
    449  1.18       pho 	/* The first argument, the program name, is implicitly
    450  1.18       pho 	 * FUSE_OPT_KEY_KEEP. */
    451  1.18       pho 	if (args->argc > 0) {
    452  1.18       pho 		if (fuse_opt_add_arg(outargs, args->argv[0]) == -1)
    453  1.18       pho 			return -1;
    454  1.18       pho 	}
    455   1.1   xtraeme 
    456  1.18       pho 	/* the real loop to process the arguments */
    457  1.18       pho 	for (i = 1; i < args->argc; i++) {
    458  1.18       pho 		const char *arg = args->argv[i];
    459   1.1   xtraeme 
    460  1.18       pho 		/* argvn != -foo... */
    461  1.18       pho 		if (nonopt || arg[0] != '-') {
    462  1.18       pho 			if (call_proc(proc, data, arg, FUSE_OPT_KEY_NONOPT,
    463  1.18       pho 						outargs, false) == -1)
    464  1.18       pho 				return -1;
    465  1.18       pho 		}
    466  1.18       pho 		/* -o or -ofoo */
    467  1.18       pho 		else if (arg[1] == 'o') {
    468   1.1   xtraeme 			/* -oblah,foo... */
    469  1.18       pho 			if (arg[2] != '\0') {
    470   1.1   xtraeme 				/* skip -o */
    471  1.18       pho 				if (parse_opts(args, &i, arg + 2, outargs,
    472  1.18       pho 							data, opts, proc) == -1)
    473  1.18       pho 					return -1;
    474  1.18       pho 			}
    475   1.1   xtraeme 			/* -o blah,foo... */
    476  1.18       pho 			else {
    477  1.18       pho 				if (next_arg(args, &i) == -1)
    478  1.18       pho 					return -1;
    479  1.18       pho 				if (parse_opts(args, &i, args->argv[i], outargs,
    480  1.18       pho 							data, opts, proc) == -1)
    481  1.18       pho 					return -1;
    482   1.1   xtraeme 			}
    483  1.18       pho 		}
    484  1.18       pho 		/* -- */
    485  1.18       pho 		else if (arg[1] == '-' && arg[2] == '\0') {
    486  1.18       pho 			if (fuse_opt_add_arg(outargs, arg) == -1)
    487  1.18       pho 				return -1;
    488  1.18       pho 			nonopt = true;
    489  1.18       pho 		}
    490  1.18       pho 		/* -foo */
    491  1.18       pho 		else {
    492  1.18       pho 			if (parse_arg(args, &i, arg, outargs,
    493  1.18       pho 						data, opts, proc, false) == -1)
    494  1.18       pho 				return -1;
    495  1.18       pho 		}
    496  1.18       pho 	}
    497  1.18       pho 
    498  1.18       pho 	/* The "--" marker at the last of outargs should be removed */
    499  1.18       pho 	if (nonopt && strcmp(outargs->argv[outargs->argc - 1], "--") == 0) {
    500  1.18       pho 		free(outargs->argv[outargs->argc - 1]);
    501  1.18       pho 		outargs->argv[--outargs->argc] = NULL;
    502  1.18       pho 	}
    503   1.1   xtraeme 
    504  1.18       pho 	return 0;
    505  1.18       pho }
    506  1.18       pho 
    507  1.18       pho int
    508  1.18       pho fuse_opt_parse(struct fuse_args *args, void *data,
    509  1.18       pho 		const struct fuse_opt *opts, fuse_opt_proc_t proc)
    510  1.18       pho {
    511  1.18       pho 	struct fuse_args outargs = FUSE_ARGS_INIT(0, NULL);
    512  1.18       pho 	int rv;
    513  1.18       pho 
    514  1.18       pho 	if (!args || !args->argv || !args->argc)
    515  1.18       pho 		return 0;
    516   1.1   xtraeme 
    517  1.18       pho 	rv = parse_all(args, &outargs, data, opts, proc);
    518  1.18       pho 	if (rv != -1) {
    519  1.18       pho 		/* Succeeded. Swap the outargs and args. */
    520  1.18       pho 		struct fuse_args tmp = *args;
    521  1.18       pho 		*args = outargs;
    522  1.18       pho 		outargs = tmp;
    523   1.1   xtraeme 	}
    524   1.1   xtraeme 
    525  1.18       pho 	fuse_opt_free_args(&outargs);
    526   1.1   xtraeme 	return rv;
    527   1.1   xtraeme }
    528