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