Home | History | Annotate | Line # | Download | only in librefuse
refuse_opt.c revision 1.11.4.1
      1  1.11.4.1      matt /* 	$NetBSD: refuse_opt.c,v 1.11.4.1 2007/11/06 23:11:57 matt 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.1   xtraeme /*
     29       1.1   xtraeme  * TODO:
     30       1.1   xtraeme  * 	* -oblah,foo... works, but the options are not enabled.
     31       1.1   xtraeme  * 	* -ofoo=%s (accepts a string) or -ofoo=%u (int) is not
     32       1.1   xtraeme  * 	  supported for now.
     33       1.1   xtraeme  * 	* void *data: how is it used? I think it's used to enable
     34       1.1   xtraeme  * 	  options or pass values for the matching options.
     35       1.1   xtraeme  */
     36       1.1   xtraeme 
     37  1.11.4.1      matt #include <sys/types.h>
     38  1.11.4.1      matt 
     39      1.10  christos #include <err.h>
     40  1.11.4.1      matt #include <fuse.h>
     41  1.11.4.1      matt #include <fuse_opt.h>
     42  1.11.4.1      matt #include <stdio.h>
     43  1.11.4.1      matt #include <stdlib.h>
     44  1.11.4.1      matt #include <string.h>
     45       1.1   xtraeme 
     46       1.1   xtraeme #ifdef FUSE_OPT_DEBUG
     47       1.2       agc #define DPRINTF(x)	do { printf x; } while ( /* CONSTCOND */ 0)
     48       1.1   xtraeme #else
     49       1.1   xtraeme #define DPRINTF(x)
     50       1.1   xtraeme #endif
     51       1.1   xtraeme 
     52       1.1   xtraeme enum {
     53       1.1   xtraeme 	KEY_HELP,
     54       1.1   xtraeme 	KEY_VERBOSE,
     55       1.1   xtraeme 	KEY_VERSION
     56       1.1   xtraeme };
     57       1.1   xtraeme 
     58       1.1   xtraeme struct fuse_opt_option {
     59       1.1   xtraeme 	const struct fuse_opt *fop;
     60       1.1   xtraeme 	char *option;
     61       1.1   xtraeme 	int key;
     62       1.1   xtraeme 	void *data;
     63       1.1   xtraeme };
     64       1.1   xtraeme 
     65       1.1   xtraeme static int fuse_opt_popt(struct fuse_opt_option *, const struct fuse_opt *);
     66       1.1   xtraeme 
     67       1.1   xtraeme /*
     68       1.1   xtraeme  * Public API.
     69       1.1   xtraeme  *
     70       1.1   xtraeme  * The following functions always return 0:
     71       1.1   xtraeme  *
     72       1.7   xtraeme  * int	fuse_opt_add_opt(char **, const char *);
     73       1.7   xtraeme  *
     74       1.7   xtraeme  * We implement the next ones:
     75       1.7   xtraeme  *
     76       1.1   xtraeme  * int	fuse_opt_add_arg(struct fuse_args *, const char *);
     77       1.1   xtraeme  * void	fuse_opt_free_args(struct fuse_args *);
     78       1.1   xtraeme  * int	fuse_opt_insert_arg(struct fuse_args *, const char *);
     79       1.1   xtraeme  * int	fuse_opt_match(const struct fuse_opt *, const char *);
     80       1.1   xtraeme  * int	fuse_opt_parse(struct fuse_args *, void *,
     81       1.1   xtraeme  * 		       const struct fuse_opt *, fuse_opt_proc_t);
     82       1.1   xtraeme  *
     83       1.1   xtraeme  */
     84       1.1   xtraeme 
     85       1.4       agc /* ARGSUSED */
     86       1.1   xtraeme int
     87       1.1   xtraeme fuse_opt_add_arg(struct fuse_args *args, const char *arg)
     88       1.1   xtraeme {
     89       1.5       agc 	struct fuse_args	*ap;
     90       1.5       agc 
     91       1.5       agc 	if (args->allocated == 0) {
     92      1.11  christos 		ap = fuse_opt_deep_copy_args(args->argc, args->argv);
     93       1.5       agc 		args->argv = ap->argv;
     94       1.5       agc 		args->argc = ap->argc;
     95       1.5       agc 		args->allocated = ap->allocated;
     96       1.5       agc 		(void) free(ap);
     97       1.5       agc 	} else if (args->allocated == args->argc) {
     98      1.10  christos 		void *a;
     99      1.10  christos 		int na = args->allocated + 10;
    100      1.10  christos 
    101      1.10  christos 		if ((a = realloc(args->argv, na * sizeof(*args->argv))) == NULL)
    102      1.10  christos 			return -1;
    103      1.10  christos 
    104      1.10  christos 		args->argv = a;
    105      1.10  christos 		args->allocated = na;
    106       1.5       agc 	}
    107       1.1   xtraeme 	DPRINTF(("%s: arguments passed: [arg:%s]\n", __func__, arg));
    108      1.10  christos 	if ((args->argv[args->argc++] = strdup(arg)) == NULL)
    109      1.10  christos 		err(1, "fuse_opt_add_arg");
    110      1.11  christos 	args->argv[args->argc] = NULL;
    111       1.5       agc         return 0;
    112       1.1   xtraeme }
    113       1.1   xtraeme 
    114      1.11  christos struct fuse_args *
    115      1.11  christos fuse_opt_deep_copy_args(int argc, char **argv)
    116      1.11  christos {
    117      1.11  christos 	struct fuse_args	*ap;
    118      1.11  christos 	size_t			 i;
    119      1.11  christos 
    120      1.11  christos 	if ((ap = malloc(sizeof(*ap))) == NULL)
    121      1.11  christos 		err(1, "_fuse_deep_copy_args");
    122      1.11  christos 	/* deep copy args structure into channel args */
    123      1.11  christos 	ap->allocated = ((argc / 10) + 1) * 10;
    124      1.11  christos 
    125      1.11  christos 	if ((ap->argv = calloc((size_t)ap->allocated,
    126      1.11  christos 	    sizeof(*ap->argv))) == NULL)
    127      1.11  christos 		err(1, "_fuse_deep_copy_args");
    128      1.11  christos 
    129      1.11  christos 	for (i = 0; i < argc; i++) {
    130      1.11  christos 		if ((ap->argv[i] = strdup(argv[i])) == NULL)
    131      1.11  christos 			err(1, "_fuse_deep_copy_args");
    132      1.11  christos 	}
    133      1.11  christos 	ap->argv[ap->argc = i] = NULL;
    134      1.11  christos 	return ap;
    135      1.11  christos }
    136      1.11  christos 
    137       1.1   xtraeme void
    138      1.11  christos fuse_opt_free_args(struct fuse_args *ap)
    139       1.1   xtraeme {
    140      1.11  christos 	int	i;
    141      1.11  christos 
    142      1.11  christos 	for (i = 0; i < ap->argc; i++) {
    143      1.11  christos 		free(ap->argv[i]);
    144      1.11  christos 	}
    145      1.11  christos 	free(ap->argv);
    146      1.11  christos 	ap->argv = NULL;
    147      1.11  christos 	ap->allocated = ap->argc = 0;
    148       1.1   xtraeme }
    149       1.1   xtraeme 
    150       1.4       agc /* ARGSUSED */
    151       1.1   xtraeme int
    152       1.1   xtraeme fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
    153       1.1   xtraeme {
    154       1.5       agc 	int	i;
    155      1.10  christos 	int	na;
    156      1.10  christos 	void   *a;
    157       1.5       agc 
    158       1.4       agc 	DPRINTF(("%s: arguments passed: [pos=%d] [arg=%s]\n",
    159       1.4       agc 	    __func__, pos, arg));
    160      1.10  christos 	if (args->argv == NULL) {
    161      1.10  christos 		na = 10;
    162      1.10  christos 		a = malloc(na * sizeof(*args->argv));
    163      1.10  christos 	} else {
    164      1.10  christos 		na = args->allocated + 10;
    165      1.10  christos 		a = realloc(args->argv, na * sizeof(*args->argv));
    166      1.10  christos 	}
    167      1.10  christos 	if (a == NULL) {
    168      1.10  christos 		warn("fuse_opt_insert_arg");
    169      1.10  christos 		return -1;
    170      1.10  christos 	}
    171      1.10  christos 	args->argv = a;
    172      1.10  christos 	args->allocated = na;
    173      1.10  christos 
    174      1.10  christos 	for (i = args->argc++; i > pos; --i) {
    175       1.5       agc 		args->argv[i] = args->argv[i - 1];
    176       1.5       agc 	}
    177      1.10  christos 	if ((args->argv[pos] = strdup(arg)) == NULL)
    178      1.10  christos 		err(1, "fuse_opt_insert_arg");
    179      1.11  christos 	args->argv[args->argc] = NULL;
    180       1.5       agc 	return 0;
    181       1.1   xtraeme }
    182       1.1   xtraeme 
    183       1.1   xtraeme /* ARGSUSED */
    184       1.1   xtraeme int fuse_opt_add_opt(char **opts, const char *opt)
    185       1.1   xtraeme {
    186       1.1   xtraeme 	DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
    187       1.1   xtraeme 	    __func__, *opts, opt));
    188       1.5       agc 	return 0;
    189       1.1   xtraeme }
    190       1.1   xtraeme 
    191       1.1   xtraeme /*
    192       1.1   xtraeme  * Returns 0 if opt was matched with any option from opts,
    193       1.1   xtraeme  * otherwise returns 1.
    194       1.1   xtraeme  */
    195       1.1   xtraeme int
    196       1.1   xtraeme fuse_opt_match(const struct fuse_opt *opts, const char *opt)
    197       1.1   xtraeme {
    198       1.1   xtraeme 	while (opts++) {
    199       1.1   xtraeme 		if (strcmp(opt, opts->templ) == 0)
    200       1.5       agc 			return 0;
    201       1.1   xtraeme 	}
    202       1.1   xtraeme 
    203       1.5       agc 	return 1;
    204       1.1   xtraeme }
    205       1.1   xtraeme 
    206       1.1   xtraeme /*
    207       1.1   xtraeme  * Returns 0 if foo->option was matched with any option from opts,
    208       1.1   xtraeme  * and sets the following on match:
    209       1.1   xtraeme  *
    210       1.1   xtraeme  * 	* foo->key is set to the foo->fop->value if offset == -1.
    211       1.1   xtraeme  * 	* foo->fop points to the matched struct opts.
    212       1.1   xtraeme  *
    213       1.1   xtraeme  * otherwise returns 1.
    214       1.1   xtraeme  */
    215       1.1   xtraeme static int
    216       1.1   xtraeme fuse_opt_popt(struct fuse_opt_option *foo, const struct fuse_opt *opts)
    217       1.1   xtraeme {
    218       1.1   xtraeme 	int i, found = 0;
    219       1.1   xtraeme 	char *match;
    220       1.1   xtraeme 
    221       1.1   xtraeme 	if (!foo->option) {
    222       1.1   xtraeme 		(void)fprintf(stderr, "fuse: missing argument after -o\n");
    223       1.5       agc 		return 1;
    224       1.1   xtraeme 	}
    225       1.1   xtraeme 	/*
    226       1.1   xtraeme 	 * iterate over argv and opts to see
    227       1.1   xtraeme 	 * if there's a match with any template.
    228       1.1   xtraeme 	 */
    229       1.1   xtraeme 	for (match = strtok(foo->option, ",");
    230       1.1   xtraeme 	     match; match = strtok(NULL, ",")) {
    231       1.1   xtraeme 
    232       1.1   xtraeme 		DPRINTF(("%s: specified option='%s'\n", __func__, match));
    233       1.1   xtraeme 		found = 0;
    234       1.1   xtraeme 
    235       1.1   xtraeme 		for (i = 0; opts && opts->templ; opts++, i++) {
    236       1.1   xtraeme 
    237       1.1   xtraeme 			DPRINTF(("%s: opts->templ='%s' opts->offset=%d "
    238       1.1   xtraeme 			    "opts->value=%d\n", __func__, opts->templ,
    239       1.1   xtraeme 			    opts->offset, opts->value));
    240       1.1   xtraeme 
    241       1.1   xtraeme 			/* option is ok */
    242       1.1   xtraeme 			if (strcmp(match, opts->templ) == 0) {
    243       1.1   xtraeme 				DPRINTF(("%s: option matched='%s'\n",
    244       1.1   xtraeme 				    __func__, match));
    245       1.1   xtraeme 				found++;
    246       1.1   xtraeme 				/*
    247       1.1   xtraeme 				 * our fop pointer now points
    248       1.1   xtraeme 				 * to the matched struct opts.
    249       1.1   xtraeme 				 */
    250       1.1   xtraeme 				foo->fop = opts;
    251       1.1   xtraeme 				/*
    252       1.1   xtraeme 				 * assign default key value, necessary for
    253       1.1   xtraeme 				 * KEY_HELP, KEY_VERSION and KEY_VERBOSE.
    254       1.1   xtraeme 				 */
    255       1.1   xtraeme 				if (foo->fop->offset == -1)
    256       1.1   xtraeme 					foo->key = foo->fop->value;
    257       1.1   xtraeme 				/* reset counter */
    258       1.1   xtraeme 				opts -= i;
    259       1.1   xtraeme 				break;
    260       1.1   xtraeme 			}
    261       1.1   xtraeme 		}
    262       1.1   xtraeme 		/* invalid option */
    263       1.1   xtraeme 		if (!found) {
    264       1.1   xtraeme 			(void)fprintf(stderr, "fuse: '%s' is not a "
    265       1.1   xtraeme 			    "valid option\n", match);
    266       1.5       agc 			return 1;
    267       1.1   xtraeme 		}
    268       1.1   xtraeme 	}
    269       1.1   xtraeme 
    270       1.5       agc 	return 0;
    271       1.1   xtraeme }
    272       1.1   xtraeme 
    273       1.2       agc /* ARGSUSED1 */
    274       1.1   xtraeme int
    275       1.1   xtraeme fuse_opt_parse(struct fuse_args *args, void *data,
    276       1.1   xtraeme         const struct fuse_opt *opts, fuse_opt_proc_t proc)
    277       1.1   xtraeme {
    278       1.1   xtraeme 	struct fuse_opt_option foo;
    279       1.1   xtraeme 	char *buf;
    280       1.5       agc 	int i, rv = 0;
    281       1.1   xtraeme 
    282       1.4       agc 	if (!args || !args->argv || !args->argc || !proc)
    283       1.1   xtraeme 		return 0;
    284       1.1   xtraeme 
    285       1.1   xtraeme 	if (args->argc == 1)
    286       1.1   xtraeme 		return proc(foo.data, *args->argv, FUSE_OPT_KEY_OPT, args);
    287       1.1   xtraeme 
    288       1.1   xtraeme 	/* the real loop to process the arguments */
    289       1.1   xtraeme 	for (i = 1; i < args->argc; i++) {
    290       1.1   xtraeme 
    291       1.1   xtraeme 		/* assign current argv string */
    292       1.1   xtraeme 		foo.option = buf = args->argv[i];
    293       1.1   xtraeme 
    294       1.1   xtraeme 		/* argvn != -foo... */
    295       1.1   xtraeme 		if (buf[0] != '-') {
    296       1.1   xtraeme 
    297       1.1   xtraeme 			foo.key = FUSE_OPT_KEY_NONOPT;
    298       1.2       agc 			rv = proc(foo.data, foo.option, foo.key, args);
    299       1.2       agc 			if (rv != 0)
    300       1.1   xtraeme 				break;
    301       1.1   xtraeme 
    302       1.1   xtraeme 		/* -o was specified... */
    303       1.1   xtraeme 		} else if (buf[0] == '-' && buf[1] == 'o') {
    304       1.1   xtraeme 
    305       1.1   xtraeme 			/* -oblah,foo... */
    306       1.1   xtraeme 			if (buf[2]) {
    307       1.1   xtraeme 				/* skip -o */
    308       1.1   xtraeme 				foo.option = args->argv[i] + 2;
    309       1.1   xtraeme 			/* -o blah,foo... */
    310       1.1   xtraeme 			} else {
    311       1.1   xtraeme 				/*
    312       1.1   xtraeme 			 	 * skip current argv and pass to the
    313       1.1   xtraeme 			 	 * next one to parse the options.
    314       1.1   xtraeme 				 */
    315       1.1   xtraeme 				++i;
    316       1.1   xtraeme 				foo.option = args->argv[i];
    317       1.1   xtraeme 			}
    318       1.1   xtraeme 
    319       1.2       agc 			rv = fuse_opt_popt(&foo, opts);
    320       1.2       agc 			if (rv != 0)
    321       1.1   xtraeme 				break;
    322       1.1   xtraeme 
    323       1.1   xtraeme 		/* help/version/verbose argument */
    324       1.1   xtraeme 		} else if (buf[0] == '-' && buf[1] != 'o') {
    325       1.1   xtraeme 			/*
    326       1.1   xtraeme 			 * check if the argument matches
    327       1.1   xtraeme 			 * with any template in opts.
    328       1.1   xtraeme 			 */
    329       1.2       agc 			rv = fuse_opt_popt(&foo, opts);
    330       1.2       agc 			if (rv != 0) {
    331       1.1   xtraeme 				break;
    332       1.1   xtraeme 			} else {
    333       1.1   xtraeme 				DPRINTF(("%s: foo.fop->templ='%s' "
    334       1.1   xtraeme 			    	    "foo.fop->offset: %d "
    335       1.1   xtraeme 			    	    "foo.fop->value: %d\n",
    336       1.1   xtraeme 			    	    __func__, foo.fop->templ,
    337       1.1   xtraeme 			    	    foo.fop->offset, foo.fop->value));
    338       1.1   xtraeme 
    339       1.1   xtraeme 				/* argument needs to be discarded */
    340       1.1   xtraeme 				if (foo.key == FUSE_OPT_KEY_DISCARD) {
    341       1.5       agc 					rv = 1;
    342       1.1   xtraeme 					break;
    343       1.1   xtraeme 				}
    344       1.1   xtraeme 
    345       1.1   xtraeme 				/* process help/version argument */
    346       1.1   xtraeme 				if (foo.key != KEY_VERBOSE &&
    347       1.1   xtraeme 				    foo.key != FUSE_OPT_KEY_KEEP) {
    348       1.1   xtraeme 					rv = proc(foo.data, foo.option,
    349       1.1   xtraeme 				    		  foo.key, args);
    350       1.1   xtraeme 					break;
    351       1.1   xtraeme 				} else {
    352       1.1   xtraeme 					/* process verbose argument */
    353       1.2       agc 					rv = proc(foo.data, foo.option,
    354       1.2       agc 						       foo.key, args);
    355       1.2       agc 					if (rv != 0)
    356       1.1   xtraeme 						break;
    357       1.1   xtraeme 				}
    358       1.1   xtraeme 			}
    359       1.1   xtraeme 		/* unknown option, how could that happen? */
    360       1.1   xtraeme 		} else {
    361       1.1   xtraeme 			DPRINTF(("%s: unknown option\n", __func__));
    362       1.5       agc 			rv = 1;
    363       1.1   xtraeme 			break;
    364       1.1   xtraeme 		}
    365       1.1   xtraeme 	}
    366       1.1   xtraeme 
    367       1.1   xtraeme 	return rv;
    368       1.1   xtraeme }
    369