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