Home | History | Annotate | Line # | Download | only in librefuse
refuse_opt.c revision 1.5
      1  1.5      agc /* 	$NetBSD: refuse_opt.c,v 1.5 2007/04/16 09:55:51 agc 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  * 3. The name of the company nor the name of the author may be used to
     16  1.1  xtraeme  *    endorse or promote products derived from this software without
     17  1.1  xtraeme  *    specific prior written permission.
     18  1.1  xtraeme  *
     19  1.1  xtraeme  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  xtraeme  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  xtraeme  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  xtraeme  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  xtraeme  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  xtraeme  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  xtraeme  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  xtraeme  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  xtraeme  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  xtraeme  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  xtraeme  */
     30  1.1  xtraeme 
     31  1.1  xtraeme /*
     32  1.1  xtraeme  * TODO:
     33  1.1  xtraeme  * 	* -oblah,foo... works, but the options are not enabled.
     34  1.1  xtraeme  * 	* -ofoo=%s (accepts a string) or -ofoo=%u (int) is not
     35  1.1  xtraeme  * 	  supported for now.
     36  1.1  xtraeme  * 	* void *data: how is it used? I think it's used to enable
     37  1.1  xtraeme  * 	  options or pass values for the matching options.
     38  1.1  xtraeme  */
     39  1.1  xtraeme 
     40  1.1  xtraeme #include "defs.h"
     41  1.1  xtraeme #include "fuse.h"
     42  1.1  xtraeme #include "fuse_opt.h"
     43  1.1  xtraeme 
     44  1.1  xtraeme #ifdef FUSE_OPT_DEBUG
     45  1.2      agc #define DPRINTF(x)	do { printf x; } while ( /* CONSTCOND */ 0)
     46  1.1  xtraeme #else
     47  1.1  xtraeme #define DPRINTF(x)
     48  1.1  xtraeme #endif
     49  1.1  xtraeme 
     50  1.1  xtraeme enum {
     51  1.1  xtraeme 	KEY_HELP,
     52  1.1  xtraeme 	KEY_VERBOSE,
     53  1.1  xtraeme 	KEY_VERSION
     54  1.1  xtraeme };
     55  1.1  xtraeme 
     56  1.1  xtraeme struct fuse_opt_option {
     57  1.1  xtraeme 	const struct fuse_opt *fop;
     58  1.1  xtraeme 	char *option;
     59  1.1  xtraeme 	int key;
     60  1.1  xtraeme 	void *data;
     61  1.1  xtraeme };
     62  1.1  xtraeme 
     63  1.1  xtraeme static int fuse_opt_popt(struct fuse_opt_option *, const struct fuse_opt *);
     64  1.1  xtraeme 
     65  1.1  xtraeme /*
     66  1.1  xtraeme  * Public API.
     67  1.1  xtraeme  *
     68  1.1  xtraeme  * The following functions always return 0:
     69  1.1  xtraeme  *
     70  1.1  xtraeme  * int	fuse_opt_add_arg(struct fuse_args *, const char *);
     71  1.1  xtraeme  * int	fuse_opt_add_opt(char **, const char *);
     72  1.1  xtraeme  * void	fuse_opt_free_args(struct fuse_args *);
     73  1.1  xtraeme  * int	fuse_opt_insert_arg(struct fuse_args *, const char *);
     74  1.1  xtraeme  *
     75  1.1  xtraeme  * We implement the next ones:
     76  1.1  xtraeme  *
     77  1.1  xtraeme  * int	fuse_opt_match(const struct fuse_opt *, const char *);
     78  1.1  xtraeme  * int	fuse_opt_parse(struct fuse_args *, void *,
     79  1.1  xtraeme  * 		       const struct fuse_opt *, fuse_opt_proc_t);
     80  1.1  xtraeme  *
     81  1.1  xtraeme  */
     82  1.1  xtraeme 
     83  1.5      agc /* function to perform a deep copy of the args structure */
     84  1.5      agc static struct fuse_args *
     85  1.5      agc deep_copy_args(int argc, char **argv)
     86  1.5      agc {
     87  1.5      agc 	struct fuse_args	*ap;
     88  1.5      agc 
     89  1.5      agc 	NEW(struct fuse_args, ap, "deep_copy_args", return NULL);
     90  1.5      agc 	/* deep copy args structure into channel args */
     91  1.5      agc 	ap->allocated = ((argc / 10) + 1) * 10;
     92  1.5      agc 	NEWARRAY(char *, ap->argv, ap->allocated, "fuse_mount", return NULL);
     93  1.5      agc 	for (ap->argc = 0 ; ap->argc < argc ; ap->argc++) {
     94  1.5      agc 		ap->argv[ap->argc] = strdup(argv[ap->argc]);
     95  1.5      agc 	}
     96  1.5      agc 	return ap;
     97  1.5      agc }
     98  1.5      agc 
     99  1.4      agc /* ARGSUSED */
    100  1.1  xtraeme int
    101  1.1  xtraeme fuse_opt_add_arg(struct fuse_args *args, const char *arg)
    102  1.1  xtraeme {
    103  1.5      agc 	struct fuse_args	*ap;
    104  1.5      agc 
    105  1.5      agc 	if (args->allocated == 0) {
    106  1.5      agc 		ap = deep_copy_args(args->argc, args->argv);
    107  1.5      agc 		args->argv = ap->argv;
    108  1.5      agc 		args->argc = ap->argc;
    109  1.5      agc 		args->allocated = ap->allocated;
    110  1.5      agc 		(void) free(ap);
    111  1.5      agc 	} else if (args->allocated == args->argc) {
    112  1.5      agc 		args->allocated += 10;
    113  1.5      agc 		RENEW(char *, args->argv, args->allocated, "fuse_opt_add_arg", return 1);
    114  1.5      agc 	}
    115  1.1  xtraeme 	DPRINTF(("%s: arguments passed: [arg:%s]\n", __func__, arg));
    116  1.5      agc 	args->argv[args->argc++] = strdup(arg);
    117  1.5      agc         return 0;
    118  1.1  xtraeme }
    119  1.1  xtraeme 
    120  1.4      agc /* ARGSUSED */
    121  1.1  xtraeme void
    122  1.1  xtraeme fuse_opt_free_args(struct fuse_args *args)
    123  1.1  xtraeme {
    124  1.5      agc 	int	i;
    125  1.5      agc 
    126  1.5      agc 	for (i = 0 ; i < args->argc ; i++) {
    127  1.5      agc 		FREE(args->argv[i]);
    128  1.5      agc 	}
    129  1.5      agc 	FREE(args->argv);
    130  1.5      agc 	args->allocated = args->argc = 0;
    131  1.1  xtraeme }
    132  1.1  xtraeme 
    133  1.4      agc /* ARGSUSED */
    134  1.1  xtraeme int
    135  1.1  xtraeme fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
    136  1.1  xtraeme {
    137  1.5      agc 	int	i;
    138  1.5      agc 
    139  1.4      agc 	DPRINTF(("%s: arguments passed: [pos=%d] [arg=%s]\n",
    140  1.4      agc 	    __func__, pos, arg));
    141  1.5      agc 	ALLOC(char *, args->argv, args->allocated, args->argc, 10, 10, "fuse_opt_insert_org", return 1);
    142  1.5      agc 	for (i = args->argc++ ; i > pos ; --i) {
    143  1.5      agc 		args->argv[i] = args->argv[i - 1];
    144  1.5      agc 	}
    145  1.5      agc 	args->argv[pos] = strdup(arg);
    146  1.5      agc 	return 0;
    147  1.1  xtraeme }
    148  1.1  xtraeme 
    149  1.1  xtraeme /* ARGSUSED */
    150  1.1  xtraeme int fuse_opt_add_opt(char **opts, const char *opt)
    151  1.1  xtraeme {
    152  1.1  xtraeme 	DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
    153  1.1  xtraeme 	    __func__, *opts, opt));
    154  1.5      agc 	return 0;
    155  1.1  xtraeme }
    156  1.1  xtraeme 
    157  1.1  xtraeme /*
    158  1.1  xtraeme  * Returns 0 if opt was matched with any option from opts,
    159  1.1  xtraeme  * otherwise returns 1.
    160  1.1  xtraeme  */
    161  1.1  xtraeme int
    162  1.1  xtraeme fuse_opt_match(const struct fuse_opt *opts, const char *opt)
    163  1.1  xtraeme {
    164  1.1  xtraeme 	while (opts++) {
    165  1.1  xtraeme 		if (strcmp(opt, opts->templ) == 0)
    166  1.5      agc 			return 0;
    167  1.1  xtraeme 	}
    168  1.1  xtraeme 
    169  1.5      agc 	return 1;
    170  1.1  xtraeme }
    171  1.1  xtraeme 
    172  1.1  xtraeme /*
    173  1.1  xtraeme  * Returns 0 if foo->option was matched with any option from opts,
    174  1.1  xtraeme  * and sets the following on match:
    175  1.1  xtraeme  *
    176  1.1  xtraeme  * 	* foo->key is set to the foo->fop->value if offset == -1.
    177  1.1  xtraeme  * 	* foo->fop points to the matched struct opts.
    178  1.1  xtraeme  *
    179  1.1  xtraeme  * otherwise returns 1.
    180  1.1  xtraeme  */
    181  1.1  xtraeme static int
    182  1.1  xtraeme fuse_opt_popt(struct fuse_opt_option *foo, const struct fuse_opt *opts)
    183  1.1  xtraeme {
    184  1.1  xtraeme 	int i, found = 0;
    185  1.1  xtraeme 	char *match;
    186  1.1  xtraeme 
    187  1.1  xtraeme 	if (!foo->option) {
    188  1.1  xtraeme 		(void)fprintf(stderr, "fuse: missing argument after -o\n");
    189  1.5      agc 		return 1;
    190  1.1  xtraeme 	}
    191  1.1  xtraeme 	/*
    192  1.1  xtraeme 	 * iterate over argv and opts to see
    193  1.1  xtraeme 	 * if there's a match with any template.
    194  1.1  xtraeme 	 */
    195  1.1  xtraeme 	for (match = strtok(foo->option, ",");
    196  1.1  xtraeme 	     match; match = strtok(NULL, ",")) {
    197  1.1  xtraeme 
    198  1.1  xtraeme 		DPRINTF(("%s: specified option='%s'\n", __func__, match));
    199  1.1  xtraeme 		found = 0;
    200  1.1  xtraeme 
    201  1.1  xtraeme 		for (i = 0; opts && opts->templ; opts++, i++) {
    202  1.1  xtraeme 
    203  1.1  xtraeme 			DPRINTF(("%s: opts->templ='%s' opts->offset=%d "
    204  1.1  xtraeme 			    "opts->value=%d\n", __func__, opts->templ,
    205  1.1  xtraeme 			    opts->offset, opts->value));
    206  1.1  xtraeme 
    207  1.1  xtraeme 			/* option is ok */
    208  1.1  xtraeme 			if (strcmp(match, opts->templ) == 0) {
    209  1.1  xtraeme 				DPRINTF(("%s: option matched='%s'\n",
    210  1.1  xtraeme 				    __func__, match));
    211  1.1  xtraeme 				found++;
    212  1.1  xtraeme 				/*
    213  1.1  xtraeme 				 * our fop pointer now points
    214  1.1  xtraeme 				 * to the matched struct opts.
    215  1.1  xtraeme 				 */
    216  1.1  xtraeme 				foo->fop = opts;
    217  1.1  xtraeme 				/*
    218  1.1  xtraeme 				 * assign default key value, necessary for
    219  1.1  xtraeme 				 * KEY_HELP, KEY_VERSION and KEY_VERBOSE.
    220  1.1  xtraeme 				 */
    221  1.1  xtraeme 				if (foo->fop->offset == -1)
    222  1.1  xtraeme 					foo->key = foo->fop->value;
    223  1.1  xtraeme 				/* reset counter */
    224  1.1  xtraeme 				opts -= i;
    225  1.1  xtraeme 				break;
    226  1.1  xtraeme 			}
    227  1.1  xtraeme 		}
    228  1.1  xtraeme 		/* invalid option */
    229  1.1  xtraeme 		if (!found) {
    230  1.1  xtraeme 			(void)fprintf(stderr, "fuse: '%s' is not a "
    231  1.1  xtraeme 			    "valid option\n", match);
    232  1.5      agc 			return 1;
    233  1.1  xtraeme 		}
    234  1.1  xtraeme 	}
    235  1.1  xtraeme 
    236  1.5      agc 	return 0;
    237  1.1  xtraeme }
    238  1.1  xtraeme 
    239  1.2      agc /* ARGSUSED1 */
    240  1.1  xtraeme int
    241  1.1  xtraeme fuse_opt_parse(struct fuse_args *args, void *data,
    242  1.1  xtraeme         const struct fuse_opt *opts, fuse_opt_proc_t proc)
    243  1.1  xtraeme {
    244  1.1  xtraeme 	struct fuse_opt_option foo;
    245  1.1  xtraeme 	char *buf;
    246  1.5      agc 	int i, rv = 0;
    247  1.1  xtraeme 
    248  1.4      agc 	if (!args || !args->argv || !args->argc || !proc)
    249  1.1  xtraeme 		return 0;
    250  1.1  xtraeme 
    251  1.1  xtraeme 	if (args->argc == 1)
    252  1.1  xtraeme 		return proc(foo.data, *args->argv, FUSE_OPT_KEY_OPT, args);
    253  1.1  xtraeme 
    254  1.1  xtraeme 	/* the real loop to process the arguments */
    255  1.1  xtraeme 	for (i = 1; i < args->argc; i++) {
    256  1.1  xtraeme 
    257  1.1  xtraeme 		/* assign current argv string */
    258  1.1  xtraeme 		foo.option = buf = args->argv[i];
    259  1.1  xtraeme 
    260  1.1  xtraeme 		/* argvn != -foo... */
    261  1.1  xtraeme 		if (buf[0] != '-') {
    262  1.1  xtraeme 
    263  1.1  xtraeme 			foo.key = FUSE_OPT_KEY_NONOPT;
    264  1.2      agc 			rv = proc(foo.data, foo.option, foo.key, args);
    265  1.2      agc 			if (rv != 0)
    266  1.1  xtraeme 				break;
    267  1.1  xtraeme 
    268  1.1  xtraeme 		/* -o was specified... */
    269  1.1  xtraeme 		} else if (buf[0] == '-' && buf[1] == 'o') {
    270  1.1  xtraeme 
    271  1.1  xtraeme 			/* -oblah,foo... */
    272  1.1  xtraeme 			if (buf[2]) {
    273  1.1  xtraeme 				/* skip -o */
    274  1.1  xtraeme 				foo.option = args->argv[i] + 2;
    275  1.1  xtraeme 			/* -o blah,foo... */
    276  1.1  xtraeme 			} else {
    277  1.1  xtraeme 				/*
    278  1.1  xtraeme 			 	 * skip current argv and pass to the
    279  1.1  xtraeme 			 	 * next one to parse the options.
    280  1.1  xtraeme 				 */
    281  1.1  xtraeme 				++i;
    282  1.1  xtraeme 				foo.option = args->argv[i];
    283  1.1  xtraeme 			}
    284  1.1  xtraeme 
    285  1.2      agc 			rv = fuse_opt_popt(&foo, opts);
    286  1.2      agc 			if (rv != 0)
    287  1.1  xtraeme 				break;
    288  1.1  xtraeme 
    289  1.1  xtraeme 		/* help/version/verbose argument */
    290  1.1  xtraeme 		} else if (buf[0] == '-' && buf[1] != 'o') {
    291  1.1  xtraeme 			/*
    292  1.1  xtraeme 			 * check if the argument matches
    293  1.1  xtraeme 			 * with any template in opts.
    294  1.1  xtraeme 			 */
    295  1.2      agc 			rv = fuse_opt_popt(&foo, opts);
    296  1.2      agc 			if (rv != 0) {
    297  1.1  xtraeme 				break;
    298  1.1  xtraeme 			} else {
    299  1.1  xtraeme 				DPRINTF(("%s: foo.fop->templ='%s' "
    300  1.1  xtraeme 			    	    "foo.fop->offset: %d "
    301  1.1  xtraeme 			    	    "foo.fop->value: %d\n",
    302  1.1  xtraeme 			    	    __func__, foo.fop->templ,
    303  1.1  xtraeme 			    	    foo.fop->offset, foo.fop->value));
    304  1.1  xtraeme 
    305  1.1  xtraeme 				/* argument needs to be discarded */
    306  1.1  xtraeme 				if (foo.key == FUSE_OPT_KEY_DISCARD) {
    307  1.5      agc 					rv = 1;
    308  1.1  xtraeme 					break;
    309  1.1  xtraeme 				}
    310  1.1  xtraeme 
    311  1.1  xtraeme 				/* process help/version argument */
    312  1.1  xtraeme 				if (foo.key != KEY_VERBOSE &&
    313  1.1  xtraeme 				    foo.key != FUSE_OPT_KEY_KEEP) {
    314  1.1  xtraeme 					rv = proc(foo.data, foo.option,
    315  1.1  xtraeme 				    		  foo.key, args);
    316  1.1  xtraeme 					break;
    317  1.1  xtraeme 				} else {
    318  1.1  xtraeme 					/* process verbose argument */
    319  1.2      agc 					rv = proc(foo.data, foo.option,
    320  1.2      agc 						       foo.key, args);
    321  1.2      agc 					if (rv != 0)
    322  1.1  xtraeme 						break;
    323  1.1  xtraeme 				}
    324  1.1  xtraeme 			}
    325  1.1  xtraeme 		/* unknown option, how could that happen? */
    326  1.1  xtraeme 		} else {
    327  1.1  xtraeme 			DPRINTF(("%s: unknown option\n", __func__));
    328  1.5      agc 			rv = 1;
    329  1.1  xtraeme 			break;
    330  1.1  xtraeme 		}
    331  1.1  xtraeme 	}
    332  1.1  xtraeme 
    333  1.1  xtraeme 	return rv;
    334  1.1  xtraeme }
    335