Home | History | Annotate | Line # | Download | only in librefuse
refuse_opt.c revision 1.7
      1  1.7  xtraeme /* 	$NetBSD: refuse_opt.c,v 1.7 2007/04/17 00:23:23 xtraeme 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.7  xtraeme  * int	fuse_opt_add_opt(char **, const char *);
     71  1.7  xtraeme  *
     72  1.7  xtraeme  * We implement the next ones:
     73  1.7  xtraeme  *
     74  1.1  xtraeme  * int	fuse_opt_add_arg(struct fuse_args *, const char *);
     75  1.1  xtraeme  * int	fuse_opt_add_opt(char **, const char *);
     76  1.1  xtraeme  * void	fuse_opt_free_args(struct fuse_args *);
     77  1.1  xtraeme  * int	fuse_opt_insert_arg(struct fuse_args *, const char *);
     78  1.1  xtraeme  * int	fuse_opt_match(const struct fuse_opt *, const char *);
     79  1.1  xtraeme  * int	fuse_opt_parse(struct fuse_args *, void *,
     80  1.1  xtraeme  * 		       const struct fuse_opt *, fuse_opt_proc_t);
     81  1.1  xtraeme  *
     82  1.1  xtraeme  */
     83  1.1  xtraeme 
     84  1.5      agc /* function to perform a deep copy of the args structure */
     85  1.5      agc static struct fuse_args *
     86  1.5      agc deep_copy_args(int argc, char **argv)
     87  1.5      agc {
     88  1.5      agc 	struct fuse_args	*ap;
     89  1.5      agc 
     90  1.5      agc 	NEW(struct fuse_args, ap, "deep_copy_args", return NULL);
     91  1.5      agc 	/* deep copy args structure into channel args */
     92  1.5      agc 	ap->allocated = ((argc / 10) + 1) * 10;
     93  1.5      agc 	NEWARRAY(char *, ap->argv, ap->allocated, "fuse_mount", return NULL);
     94  1.5      agc 	for (ap->argc = 0 ; ap->argc < argc ; ap->argc++) {
     95  1.5      agc 		ap->argv[ap->argc] = strdup(argv[ap->argc]);
     96  1.5      agc 	}
     97  1.5      agc 	return ap;
     98  1.5      agc }
     99  1.5      agc 
    100  1.4      agc /* ARGSUSED */
    101  1.1  xtraeme int
    102  1.1  xtraeme fuse_opt_add_arg(struct fuse_args *args, const char *arg)
    103  1.1  xtraeme {
    104  1.5      agc 	struct fuse_args	*ap;
    105  1.5      agc 
    106  1.5      agc 	if (args->allocated == 0) {
    107  1.5      agc 		ap = deep_copy_args(args->argc, args->argv);
    108  1.5      agc 		args->argv = ap->argv;
    109  1.5      agc 		args->argc = ap->argc;
    110  1.5      agc 		args->allocated = ap->allocated;
    111  1.5      agc 		(void) free(ap);
    112  1.5      agc 	} else if (args->allocated == args->argc) {
    113  1.5      agc 		args->allocated += 10;
    114  1.6  xtraeme 		RENEW(char *, args->argv, args->allocated,
    115  1.6  xtraeme 		    "fuse_opt_add_arg", return 1);
    116  1.5      agc 	}
    117  1.1  xtraeme 	DPRINTF(("%s: arguments passed: [arg:%s]\n", __func__, arg));
    118  1.5      agc 	args->argv[args->argc++] = strdup(arg);
    119  1.5      agc         return 0;
    120  1.1  xtraeme }
    121  1.1  xtraeme 
    122  1.4      agc /* ARGSUSED */
    123  1.1  xtraeme void
    124  1.1  xtraeme fuse_opt_free_args(struct fuse_args *args)
    125  1.1  xtraeme {
    126  1.5      agc 	int	i;
    127  1.5      agc 
    128  1.5      agc 	for (i = 0 ; i < args->argc ; i++) {
    129  1.5      agc 		FREE(args->argv[i]);
    130  1.5      agc 	}
    131  1.5      agc 	FREE(args->argv);
    132  1.5      agc 	args->allocated = args->argc = 0;
    133  1.1  xtraeme }
    134  1.1  xtraeme 
    135  1.4      agc /* ARGSUSED */
    136  1.1  xtraeme int
    137  1.1  xtraeme fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
    138  1.1  xtraeme {
    139  1.5      agc 	int	i;
    140  1.5      agc 
    141  1.4      agc 	DPRINTF(("%s: arguments passed: [pos=%d] [arg=%s]\n",
    142  1.4      agc 	    __func__, pos, arg));
    143  1.6  xtraeme 	ALLOC(char *, args->argv, args->allocated, args->argc, 10, 10,
    144  1.6  xtraeme 	    "fuse_opt_insert_org", return 1);
    145  1.5      agc 	for (i = args->argc++ ; i > pos ; --i) {
    146  1.5      agc 		args->argv[i] = args->argv[i - 1];
    147  1.5      agc 	}
    148  1.5      agc 	args->argv[pos] = strdup(arg);
    149  1.5      agc 	return 0;
    150  1.1  xtraeme }
    151  1.1  xtraeme 
    152  1.1  xtraeme /* ARGSUSED */
    153  1.1  xtraeme int fuse_opt_add_opt(char **opts, const char *opt)
    154  1.1  xtraeme {
    155  1.1  xtraeme 	DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
    156  1.1  xtraeme 	    __func__, *opts, opt));
    157  1.5      agc 	return 0;
    158  1.1  xtraeme }
    159  1.1  xtraeme 
    160  1.1  xtraeme /*
    161  1.1  xtraeme  * Returns 0 if opt was matched with any option from opts,
    162  1.1  xtraeme  * otherwise returns 1.
    163  1.1  xtraeme  */
    164  1.1  xtraeme int
    165  1.1  xtraeme fuse_opt_match(const struct fuse_opt *opts, const char *opt)
    166  1.1  xtraeme {
    167  1.1  xtraeme 	while (opts++) {
    168  1.1  xtraeme 		if (strcmp(opt, opts->templ) == 0)
    169  1.5      agc 			return 0;
    170  1.1  xtraeme 	}
    171  1.1  xtraeme 
    172  1.5      agc 	return 1;
    173  1.1  xtraeme }
    174  1.1  xtraeme 
    175  1.1  xtraeme /*
    176  1.1  xtraeme  * Returns 0 if foo->option was matched with any option from opts,
    177  1.1  xtraeme  * and sets the following on match:
    178  1.1  xtraeme  *
    179  1.1  xtraeme  * 	* foo->key is set to the foo->fop->value if offset == -1.
    180  1.1  xtraeme  * 	* foo->fop points to the matched struct opts.
    181  1.1  xtraeme  *
    182  1.1  xtraeme  * otherwise returns 1.
    183  1.1  xtraeme  */
    184  1.1  xtraeme static int
    185  1.1  xtraeme fuse_opt_popt(struct fuse_opt_option *foo, const struct fuse_opt *opts)
    186  1.1  xtraeme {
    187  1.1  xtraeme 	int i, found = 0;
    188  1.1  xtraeme 	char *match;
    189  1.1  xtraeme 
    190  1.1  xtraeme 	if (!foo->option) {
    191  1.1  xtraeme 		(void)fprintf(stderr, "fuse: missing argument after -o\n");
    192  1.5      agc 		return 1;
    193  1.1  xtraeme 	}
    194  1.1  xtraeme 	/*
    195  1.1  xtraeme 	 * iterate over argv and opts to see
    196  1.1  xtraeme 	 * if there's a match with any template.
    197  1.1  xtraeme 	 */
    198  1.1  xtraeme 	for (match = strtok(foo->option, ",");
    199  1.1  xtraeme 	     match; match = strtok(NULL, ",")) {
    200  1.1  xtraeme 
    201  1.1  xtraeme 		DPRINTF(("%s: specified option='%s'\n", __func__, match));
    202  1.1  xtraeme 		found = 0;
    203  1.1  xtraeme 
    204  1.1  xtraeme 		for (i = 0; opts && opts->templ; opts++, i++) {
    205  1.1  xtraeme 
    206  1.1  xtraeme 			DPRINTF(("%s: opts->templ='%s' opts->offset=%d "
    207  1.1  xtraeme 			    "opts->value=%d\n", __func__, opts->templ,
    208  1.1  xtraeme 			    opts->offset, opts->value));
    209  1.1  xtraeme 
    210  1.1  xtraeme 			/* option is ok */
    211  1.1  xtraeme 			if (strcmp(match, opts->templ) == 0) {
    212  1.1  xtraeme 				DPRINTF(("%s: option matched='%s'\n",
    213  1.1  xtraeme 				    __func__, match));
    214  1.1  xtraeme 				found++;
    215  1.1  xtraeme 				/*
    216  1.1  xtraeme 				 * our fop pointer now points
    217  1.1  xtraeme 				 * to the matched struct opts.
    218  1.1  xtraeme 				 */
    219  1.1  xtraeme 				foo->fop = opts;
    220  1.1  xtraeme 				/*
    221  1.1  xtraeme 				 * assign default key value, necessary for
    222  1.1  xtraeme 				 * KEY_HELP, KEY_VERSION and KEY_VERBOSE.
    223  1.1  xtraeme 				 */
    224  1.1  xtraeme 				if (foo->fop->offset == -1)
    225  1.1  xtraeme 					foo->key = foo->fop->value;
    226  1.1  xtraeme 				/* reset counter */
    227  1.1  xtraeme 				opts -= i;
    228  1.1  xtraeme 				break;
    229  1.1  xtraeme 			}
    230  1.1  xtraeme 		}
    231  1.1  xtraeme 		/* invalid option */
    232  1.1  xtraeme 		if (!found) {
    233  1.1  xtraeme 			(void)fprintf(stderr, "fuse: '%s' is not a "
    234  1.1  xtraeme 			    "valid option\n", match);
    235  1.5      agc 			return 1;
    236  1.1  xtraeme 		}
    237  1.1  xtraeme 	}
    238  1.1  xtraeme 
    239  1.5      agc 	return 0;
    240  1.1  xtraeme }
    241  1.1  xtraeme 
    242  1.2      agc /* ARGSUSED1 */
    243  1.1  xtraeme int
    244  1.1  xtraeme fuse_opt_parse(struct fuse_args *args, void *data,
    245  1.1  xtraeme         const struct fuse_opt *opts, fuse_opt_proc_t proc)
    246  1.1  xtraeme {
    247  1.1  xtraeme 	struct fuse_opt_option foo;
    248  1.1  xtraeme 	char *buf;
    249  1.5      agc 	int i, rv = 0;
    250  1.1  xtraeme 
    251  1.4      agc 	if (!args || !args->argv || !args->argc || !proc)
    252  1.1  xtraeme 		return 0;
    253  1.1  xtraeme 
    254  1.1  xtraeme 	if (args->argc == 1)
    255  1.1  xtraeme 		return proc(foo.data, *args->argv, FUSE_OPT_KEY_OPT, args);
    256  1.1  xtraeme 
    257  1.1  xtraeme 	/* the real loop to process the arguments */
    258  1.1  xtraeme 	for (i = 1; i < args->argc; i++) {
    259  1.1  xtraeme 
    260  1.1  xtraeme 		/* assign current argv string */
    261  1.1  xtraeme 		foo.option = buf = args->argv[i];
    262  1.1  xtraeme 
    263  1.1  xtraeme 		/* argvn != -foo... */
    264  1.1  xtraeme 		if (buf[0] != '-') {
    265  1.1  xtraeme 
    266  1.1  xtraeme 			foo.key = FUSE_OPT_KEY_NONOPT;
    267  1.2      agc 			rv = proc(foo.data, foo.option, foo.key, args);
    268  1.2      agc 			if (rv != 0)
    269  1.1  xtraeme 				break;
    270  1.1  xtraeme 
    271  1.1  xtraeme 		/* -o was specified... */
    272  1.1  xtraeme 		} else if (buf[0] == '-' && buf[1] == 'o') {
    273  1.1  xtraeme 
    274  1.1  xtraeme 			/* -oblah,foo... */
    275  1.1  xtraeme 			if (buf[2]) {
    276  1.1  xtraeme 				/* skip -o */
    277  1.1  xtraeme 				foo.option = args->argv[i] + 2;
    278  1.1  xtraeme 			/* -o blah,foo... */
    279  1.1  xtraeme 			} else {
    280  1.1  xtraeme 				/*
    281  1.1  xtraeme 			 	 * skip current argv and pass to the
    282  1.1  xtraeme 			 	 * next one to parse the options.
    283  1.1  xtraeme 				 */
    284  1.1  xtraeme 				++i;
    285  1.1  xtraeme 				foo.option = args->argv[i];
    286  1.1  xtraeme 			}
    287  1.1  xtraeme 
    288  1.2      agc 			rv = fuse_opt_popt(&foo, opts);
    289  1.2      agc 			if (rv != 0)
    290  1.1  xtraeme 				break;
    291  1.1  xtraeme 
    292  1.1  xtraeme 		/* help/version/verbose argument */
    293  1.1  xtraeme 		} else if (buf[0] == '-' && buf[1] != 'o') {
    294  1.1  xtraeme 			/*
    295  1.1  xtraeme 			 * check if the argument matches
    296  1.1  xtraeme 			 * with any template in opts.
    297  1.1  xtraeme 			 */
    298  1.2      agc 			rv = fuse_opt_popt(&foo, opts);
    299  1.2      agc 			if (rv != 0) {
    300  1.1  xtraeme 				break;
    301  1.1  xtraeme 			} else {
    302  1.1  xtraeme 				DPRINTF(("%s: foo.fop->templ='%s' "
    303  1.1  xtraeme 			    	    "foo.fop->offset: %d "
    304  1.1  xtraeme 			    	    "foo.fop->value: %d\n",
    305  1.1  xtraeme 			    	    __func__, foo.fop->templ,
    306  1.1  xtraeme 			    	    foo.fop->offset, foo.fop->value));
    307  1.1  xtraeme 
    308  1.1  xtraeme 				/* argument needs to be discarded */
    309  1.1  xtraeme 				if (foo.key == FUSE_OPT_KEY_DISCARD) {
    310  1.5      agc 					rv = 1;
    311  1.1  xtraeme 					break;
    312  1.1  xtraeme 				}
    313  1.1  xtraeme 
    314  1.1  xtraeme 				/* process help/version argument */
    315  1.1  xtraeme 				if (foo.key != KEY_VERBOSE &&
    316  1.1  xtraeme 				    foo.key != FUSE_OPT_KEY_KEEP) {
    317  1.1  xtraeme 					rv = proc(foo.data, foo.option,
    318  1.1  xtraeme 				    		  foo.key, args);
    319  1.1  xtraeme 					break;
    320  1.1  xtraeme 				} else {
    321  1.1  xtraeme 					/* process verbose argument */
    322  1.2      agc 					rv = proc(foo.data, foo.option,
    323  1.2      agc 						       foo.key, args);
    324  1.2      agc 					if (rv != 0)
    325  1.1  xtraeme 						break;
    326  1.1  xtraeme 				}
    327  1.1  xtraeme 			}
    328  1.1  xtraeme 		/* unknown option, how could that happen? */
    329  1.1  xtraeme 		} else {
    330  1.1  xtraeme 			DPRINTF(("%s: unknown option\n", __func__));
    331  1.5      agc 			rv = 1;
    332  1.1  xtraeme 			break;
    333  1.1  xtraeme 		}
    334  1.1  xtraeme 	}
    335  1.1  xtraeme 
    336  1.1  xtraeme 	return rv;
    337  1.1  xtraeme }
    338