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