refuse_opt.c revision 1.15.26.1 1 1.15.26.1 pgoyette /* $NetBSD: refuse_opt.c,v 1.15.26.1 2017/01/07 08:56:04 pgoyette 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 *
16 1.1 xtraeme * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 xtraeme * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 xtraeme * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 xtraeme * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 xtraeme * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 xtraeme * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 xtraeme * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 xtraeme * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 xtraeme * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 xtraeme * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 xtraeme */
27 1.1 xtraeme
28 1.13 pooka #include <sys/types.h>
29 1.13 pooka
30 1.10 christos #include <err.h>
31 1.13 pooka #include <fuse.h>
32 1.13 pooka #include <fuse_opt.h>
33 1.15.26.1 pgoyette #include <stdbool.h>
34 1.13 pooka #include <stdio.h>
35 1.13 pooka #include <stdlib.h>
36 1.13 pooka #include <string.h>
37 1.1 xtraeme
38 1.1 xtraeme #ifdef FUSE_OPT_DEBUG
39 1.2 agc #define DPRINTF(x) do { printf x; } while ( /* CONSTCOND */ 0)
40 1.1 xtraeme #else
41 1.1 xtraeme #define DPRINTF(x)
42 1.1 xtraeme #endif
43 1.1 xtraeme
44 1.15.26.1 pgoyette /*
45 1.1 xtraeme * Public API.
46 1.1 xtraeme */
47 1.1 xtraeme
48 1.4 agc /* ARGSUSED */
49 1.1 xtraeme int
50 1.1 xtraeme fuse_opt_add_arg(struct fuse_args *args, const char *arg)
51 1.1 xtraeme {
52 1.5 agc struct fuse_args *ap;
53 1.5 agc
54 1.5 agc if (args->allocated == 0) {
55 1.11 christos ap = fuse_opt_deep_copy_args(args->argc, args->argv);
56 1.5 agc args->argv = ap->argv;
57 1.5 agc args->argc = ap->argc;
58 1.5 agc args->allocated = ap->allocated;
59 1.5 agc (void) free(ap);
60 1.5 agc } else if (args->allocated == args->argc) {
61 1.10 christos void *a;
62 1.10 christos int na = args->allocated + 10;
63 1.10 christos
64 1.10 christos if ((a = realloc(args->argv, na * sizeof(*args->argv))) == NULL)
65 1.10 christos return -1;
66 1.10 christos
67 1.10 christos args->argv = a;
68 1.10 christos args->allocated = na;
69 1.5 agc }
70 1.1 xtraeme DPRINTF(("%s: arguments passed: [arg:%s]\n", __func__, arg));
71 1.10 christos if ((args->argv[args->argc++] = strdup(arg)) == NULL)
72 1.10 christos err(1, "fuse_opt_add_arg");
73 1.11 christos args->argv[args->argc] = NULL;
74 1.5 agc return 0;
75 1.1 xtraeme }
76 1.1 xtraeme
77 1.11 christos struct fuse_args *
78 1.11 christos fuse_opt_deep_copy_args(int argc, char **argv)
79 1.11 christos {
80 1.11 christos struct fuse_args *ap;
81 1.14 lukem int i;
82 1.11 christos
83 1.11 christos if ((ap = malloc(sizeof(*ap))) == NULL)
84 1.11 christos err(1, "_fuse_deep_copy_args");
85 1.11 christos /* deep copy args structure into channel args */
86 1.11 christos ap->allocated = ((argc / 10) + 1) * 10;
87 1.11 christos
88 1.11 christos if ((ap->argv = calloc((size_t)ap->allocated,
89 1.11 christos sizeof(*ap->argv))) == NULL)
90 1.11 christos err(1, "_fuse_deep_copy_args");
91 1.11 christos
92 1.11 christos for (i = 0; i < argc; i++) {
93 1.11 christos if ((ap->argv[i] = strdup(argv[i])) == NULL)
94 1.11 christos err(1, "_fuse_deep_copy_args");
95 1.11 christos }
96 1.11 christos ap->argv[ap->argc = i] = NULL;
97 1.11 christos return ap;
98 1.11 christos }
99 1.11 christos
100 1.1 xtraeme void
101 1.11 christos fuse_opt_free_args(struct fuse_args *ap)
102 1.1 xtraeme {
103 1.15.26.1 pgoyette if (ap) {
104 1.15.26.1 pgoyette if (ap->allocated) {
105 1.15.26.1 pgoyette int i;
106 1.15.26.1 pgoyette for (i = 0; i < ap->argc; i++) {
107 1.15.26.1 pgoyette free(ap->argv[i]);
108 1.15.26.1 pgoyette }
109 1.15.26.1 pgoyette free(ap->argv);
110 1.15.26.1 pgoyette }
111 1.15.26.1 pgoyette ap->argv = NULL;
112 1.15.26.1 pgoyette ap->allocated = ap->argc = 0;
113 1.11 christos }
114 1.1 xtraeme }
115 1.1 xtraeme
116 1.4 agc /* ARGSUSED */
117 1.1 xtraeme int
118 1.1 xtraeme fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
119 1.1 xtraeme {
120 1.5 agc int i;
121 1.10 christos int na;
122 1.10 christos void *a;
123 1.5 agc
124 1.4 agc DPRINTF(("%s: arguments passed: [pos=%d] [arg=%s]\n",
125 1.4 agc __func__, pos, arg));
126 1.10 christos if (args->argv == NULL) {
127 1.10 christos na = 10;
128 1.10 christos a = malloc(na * sizeof(*args->argv));
129 1.10 christos } else {
130 1.10 christos na = args->allocated + 10;
131 1.10 christos a = realloc(args->argv, na * sizeof(*args->argv));
132 1.10 christos }
133 1.10 christos if (a == NULL) {
134 1.10 christos warn("fuse_opt_insert_arg");
135 1.10 christos return -1;
136 1.10 christos }
137 1.10 christos args->argv = a;
138 1.10 christos args->allocated = na;
139 1.10 christos
140 1.10 christos for (i = args->argc++; i > pos; --i) {
141 1.5 agc args->argv[i] = args->argv[i - 1];
142 1.5 agc }
143 1.10 christos if ((args->argv[pos] = strdup(arg)) == NULL)
144 1.10 christos err(1, "fuse_opt_insert_arg");
145 1.11 christos args->argv[args->argc] = NULL;
146 1.5 agc return 0;
147 1.1 xtraeme }
148 1.1 xtraeme
149 1.15.26.1 pgoyette static int add_opt(char **opts, const char *opt, bool escape)
150 1.15.26.1 pgoyette {
151 1.15.26.1 pgoyette const size_t orig_len = *opts == NULL ? 0 : strlen(*opts);
152 1.15.26.1 pgoyette char* buf = realloc(*opts, orig_len + 1 + strlen(opt) * 2 + 1);
153 1.15.26.1 pgoyette
154 1.15.26.1 pgoyette if (buf == NULL) {
155 1.15.26.1 pgoyette return -1;
156 1.15.26.1 pgoyette }
157 1.15.26.1 pgoyette *opts = buf;
158 1.15.26.1 pgoyette
159 1.15.26.1 pgoyette if (orig_len > 0) {
160 1.15.26.1 pgoyette buf += orig_len;
161 1.15.26.1 pgoyette *buf++ = ',';
162 1.15.26.1 pgoyette }
163 1.15.26.1 pgoyette
164 1.15.26.1 pgoyette for (; *opt; opt++) {
165 1.15.26.1 pgoyette if (escape && (*opt == ',' || *opt == '\\')) {
166 1.15.26.1 pgoyette *buf++ = '\\';
167 1.15.26.1 pgoyette }
168 1.15.26.1 pgoyette *buf++ = *opt;
169 1.15.26.1 pgoyette }
170 1.15.26.1 pgoyette *buf = '\0';
171 1.15.26.1 pgoyette
172 1.15.26.1 pgoyette return 0;
173 1.15.26.1 pgoyette }
174 1.15.26.1 pgoyette
175 1.1 xtraeme int fuse_opt_add_opt(char **opts, const char *opt)
176 1.1 xtraeme {
177 1.1 xtraeme DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
178 1.1 xtraeme __func__, *opts, opt));
179 1.15.26.1 pgoyette return add_opt(opts, opt, false);
180 1.15.26.1 pgoyette }
181 1.15.26.1 pgoyette
182 1.15.26.1 pgoyette int fuse_opt_add_opt_escaped(char **opts, const char *opt)
183 1.15.26.1 pgoyette {
184 1.15.26.1 pgoyette DPRINTF(("%s: arguments passed: [opts=%s] [opt=%s]\n",
185 1.15.26.1 pgoyette __func__, *opts, opt));
186 1.15.26.1 pgoyette return add_opt(opts, opt, true);
187 1.15.26.1 pgoyette }
188 1.15.26.1 pgoyette
189 1.15.26.1 pgoyette static bool match_templ(const char *templ, const char *opt, int *sep_idx)
190 1.15.26.1 pgoyette {
191 1.15.26.1 pgoyette const char *sep = strpbrk(templ, "= ");
192 1.15.26.1 pgoyette
193 1.15.26.1 pgoyette if (sep != NULL && (sep[1] == '\0' || sep[1] == '%')) {
194 1.15.26.1 pgoyette const size_t cmp_len =
195 1.15.26.1 pgoyette sep[0] == '=' ? sep - templ + 1 : sep - templ;
196 1.15.26.1 pgoyette
197 1.15.26.1 pgoyette if (strlen(opt) >= cmp_len && strncmp(templ, opt, cmp_len) == 0) {
198 1.15.26.1 pgoyette if (sep_idx != NULL)
199 1.15.26.1 pgoyette *sep_idx = sep - templ;
200 1.15.26.1 pgoyette return true;
201 1.15.26.1 pgoyette }
202 1.15.26.1 pgoyette else {
203 1.15.26.1 pgoyette return false;
204 1.15.26.1 pgoyette }
205 1.15.26.1 pgoyette }
206 1.15.26.1 pgoyette else {
207 1.15.26.1 pgoyette if (strcmp(templ, opt) == 0) {
208 1.15.26.1 pgoyette if (sep_idx != NULL)
209 1.15.26.1 pgoyette *sep_idx = -1;
210 1.15.26.1 pgoyette return true;
211 1.15.26.1 pgoyette }
212 1.15.26.1 pgoyette else {
213 1.15.26.1 pgoyette return false;
214 1.15.26.1 pgoyette }
215 1.15.26.1 pgoyette }
216 1.15.26.1 pgoyette }
217 1.15.26.1 pgoyette
218 1.15.26.1 pgoyette static const struct fuse_opt *
219 1.15.26.1 pgoyette find_opt(const struct fuse_opt *opts, const char *opt, int *sep_idx)
220 1.15.26.1 pgoyette {
221 1.15.26.1 pgoyette for (; opts != NULL && opts->templ != NULL; opts++) {
222 1.15.26.1 pgoyette if (match_templ(opts->templ, opt, sep_idx))
223 1.15.26.1 pgoyette return opts;
224 1.15.26.1 pgoyette }
225 1.15.26.1 pgoyette return NULL;
226 1.1 xtraeme }
227 1.1 xtraeme
228 1.1 xtraeme /*
229 1.15.26.1 pgoyette * Returns 1 if opt was matched with any option from opts,
230 1.15.26.1 pgoyette * otherwise returns 0.
231 1.1 xtraeme */
232 1.1 xtraeme int
233 1.1 xtraeme fuse_opt_match(const struct fuse_opt *opts, const char *opt)
234 1.1 xtraeme {
235 1.15.26.1 pgoyette return find_opt(opts, opt, NULL) != NULL ? 1 : 0;
236 1.15.26.1 pgoyette }
237 1.15.26.1 pgoyette
238 1.15.26.1 pgoyette static int call_proc(fuse_opt_proc_t proc, void* data,
239 1.15.26.1 pgoyette const char* arg, int key, struct fuse_args *outargs, bool is_opt)
240 1.15.26.1 pgoyette {
241 1.15.26.1 pgoyette if (key == FUSE_OPT_KEY_DISCARD)
242 1.15.26.1 pgoyette return 0;
243 1.15.26.1 pgoyette
244 1.15.26.1 pgoyette if (key != FUSE_OPT_KEY_KEEP && proc != NULL) {
245 1.15.26.1 pgoyette const int rv = proc(data, arg, key, outargs);
246 1.15.26.1 pgoyette
247 1.15.26.1 pgoyette if (rv == -1 || /* error */
248 1.15.26.1 pgoyette rv == 0 /* discard */)
249 1.15.26.1 pgoyette return rv;
250 1.15.26.1 pgoyette }
251 1.15.26.1 pgoyette
252 1.15.26.1 pgoyette if (is_opt) {
253 1.15.26.1 pgoyette /* Do we already have "-o" at the beginning of outargs? */
254 1.15.26.1 pgoyette if (outargs->argc >= 3 && strcmp(outargs->argv[1], "-o") == 0) {
255 1.15.26.1 pgoyette /* Append the option to the comma-separated list. */
256 1.15.26.1 pgoyette if (fuse_opt_add_opt_escaped(&outargs->argv[2], arg) == -1)
257 1.15.26.1 pgoyette return -1;
258 1.15.26.1 pgoyette }
259 1.15.26.1 pgoyette else {
260 1.15.26.1 pgoyette /* Insert -o arg at the beginning. */
261 1.15.26.1 pgoyette if (fuse_opt_insert_arg(outargs, 1, "-o") == -1)
262 1.15.26.1 pgoyette return -1;
263 1.15.26.1 pgoyette if (fuse_opt_insert_arg(outargs, 2, arg) == -1)
264 1.15.26.1 pgoyette return -1;
265 1.15.26.1 pgoyette }
266 1.15.26.1 pgoyette }
267 1.15.26.1 pgoyette else {
268 1.15.26.1 pgoyette if (fuse_opt_add_arg(outargs, arg) == -1)
269 1.15.26.1 pgoyette return -1;
270 1.1 xtraeme }
271 1.1 xtraeme
272 1.15.26.1 pgoyette return 0;
273 1.1 xtraeme }
274 1.1 xtraeme
275 1.15.26.1 pgoyette /* Skip the current argv if possible. */
276 1.15.26.1 pgoyette static int next_arg(const struct fuse_args *args, int *i)
277 1.15.26.1 pgoyette {
278 1.15.26.1 pgoyette if (*i + 1 >= args->argc) {
279 1.15.26.1 pgoyette (void)fprintf(stderr, "fuse: missing argument"
280 1.15.26.1 pgoyette " after '%s'\n", args->argv[*i]);
281 1.15.26.1 pgoyette return -1;
282 1.15.26.1 pgoyette }
283 1.15.26.1 pgoyette else {
284 1.15.26.1 pgoyette *i += 1;
285 1.15.26.1 pgoyette return 0;
286 1.15.26.1 pgoyette }
287 1.15.26.1 pgoyette }
288 1.15.26.1 pgoyette
289 1.15.26.1 pgoyette /* Parse a single argument with a matched template. */
290 1.1 xtraeme static int
291 1.15.26.1 pgoyette parse_matched_arg(const char* arg, struct fuse_args *outargs,
292 1.15.26.1 pgoyette const struct fuse_opt* opt, int sep_idx, void* data,
293 1.15.26.1 pgoyette fuse_opt_proc_t proc, bool is_opt)
294 1.1 xtraeme {
295 1.15.26.1 pgoyette if (opt->offset == -1) {
296 1.15.26.1 pgoyette /* The option description does not want any variables to be
297 1.15.26.1 pgoyette * updated.*/
298 1.15.26.1 pgoyette if (call_proc(proc, data, arg, opt->value, outargs, is_opt) == -1)
299 1.15.26.1 pgoyette return -1;
300 1.15.26.1 pgoyette }
301 1.15.26.1 pgoyette else {
302 1.15.26.1 pgoyette void *var = (char*)data + opt->offset;
303 1.15.26.1 pgoyette
304 1.15.26.1 pgoyette if (sep_idx > 0 && opt->templ[sep_idx + 1] == '%') {
305 1.15.26.1 pgoyette /* "foo=%y" or "-x %y" */
306 1.15.26.1 pgoyette const char* param =
307 1.15.26.1 pgoyette opt->templ[sep_idx] == '=' ? &arg[sep_idx + 1] : &arg[sep_idx];
308 1.15.26.1 pgoyette
309 1.15.26.1 pgoyette if (opt->templ[sep_idx + 2] == 's') {
310 1.15.26.1 pgoyette char* dup = strdup(param);
311 1.15.26.1 pgoyette if (dup == NULL)
312 1.15.26.1 pgoyette return -1;
313 1.15.26.1 pgoyette
314 1.15.26.1 pgoyette *(char **)var = dup;
315 1.15.26.1 pgoyette }
316 1.15.26.1 pgoyette else {
317 1.15.26.1 pgoyette /* The format string is not a literal. We all know
318 1.15.26.1 pgoyette * this is a bad idea but it's exactly what fuse_opt
319 1.15.26.1 pgoyette * wants to do... */
320 1.15.26.1 pgoyette #pragma GCC diagnostic push
321 1.15.26.1 pgoyette #pragma GCC diagnostic ignored "-Wformat-nonliteral"
322 1.15.26.1 pgoyette if (sscanf(param, &opt->templ[sep_idx + 1], var) == -1) {
323 1.15.26.1 pgoyette #pragma GCC diagnostic pop
324 1.15.26.1 pgoyette (void)fprintf(stderr, "fuse: '%s' is not a "
325 1.15.26.1 pgoyette "valid parameter for option '%.*s'\n",
326 1.15.26.1 pgoyette param, sep_idx, opt->templ);
327 1.15.26.1 pgoyette return -1;
328 1.15.26.1 pgoyette }
329 1.1 xtraeme }
330 1.1 xtraeme }
331 1.15.26.1 pgoyette else {
332 1.15.26.1 pgoyette /* No parameter format is given. */
333 1.15.26.1 pgoyette *(int *)var = opt->value;
334 1.1 xtraeme }
335 1.1 xtraeme }
336 1.5 agc return 0;
337 1.1 xtraeme }
338 1.1 xtraeme
339 1.15.26.1 pgoyette /* Parse a single argument with matching templates. */
340 1.15.26.1 pgoyette static int
341 1.15.26.1 pgoyette parse_arg(struct fuse_args* args, int *argi, const char* arg,
342 1.15.26.1 pgoyette struct fuse_args *outargs, void *data,
343 1.15.26.1 pgoyette const struct fuse_opt *opts, fuse_opt_proc_t proc, bool is_opt)
344 1.1 xtraeme {
345 1.15.26.1 pgoyette int sep_idx;
346 1.15.26.1 pgoyette const struct fuse_opt *opt = find_opt(opts, arg, &sep_idx);
347 1.15.26.1 pgoyette
348 1.15.26.1 pgoyette if (opt) {
349 1.15.26.1 pgoyette /* An argument can match to multiple templates. Process them
350 1.15.26.1 pgoyette * all. */
351 1.15.26.1 pgoyette for (; opt != NULL && opt->templ != NULL;
352 1.15.26.1 pgoyette opt = find_opt(++opt, arg, &sep_idx)) {
353 1.15.26.1 pgoyette
354 1.15.26.1 pgoyette if (sep_idx > 0 && opt->templ[sep_idx] == ' ' &&
355 1.15.26.1 pgoyette arg[sep_idx] == '\0') {
356 1.15.26.1 pgoyette /* The template "-x %y" requests a separate
357 1.15.26.1 pgoyette * parameter "%y". Try to find one. */
358 1.15.26.1 pgoyette char *new_arg;
359 1.15.26.1 pgoyette int rv;
360 1.15.26.1 pgoyette
361 1.15.26.1 pgoyette if (next_arg(args, argi) == -1)
362 1.15.26.1 pgoyette return -1;
363 1.15.26.1 pgoyette
364 1.15.26.1 pgoyette /* ...but processor callbacks expect a concatenated
365 1.15.26.1 pgoyette * argument "-xfoo". */
366 1.15.26.1 pgoyette if ((new_arg = malloc(sep_idx +
367 1.15.26.1 pgoyette strlen(args->argv[*argi]) + 1)) == NULL)
368 1.15.26.1 pgoyette return -1;
369 1.15.26.1 pgoyette
370 1.15.26.1 pgoyette strncpy(new_arg, arg, sep_idx); /* -x */
371 1.15.26.1 pgoyette strcpy(new_arg + sep_idx, args->argv[*argi]); /* foo */
372 1.15.26.1 pgoyette rv = parse_matched_arg(new_arg, outargs, opt, sep_idx,
373 1.15.26.1 pgoyette data, proc, is_opt);
374 1.15.26.1 pgoyette free(new_arg);
375 1.1 xtraeme
376 1.15.26.1 pgoyette if (rv == -1)
377 1.15.26.1 pgoyette return -1;
378 1.15.26.1 pgoyette }
379 1.15.26.1 pgoyette else {
380 1.15.26.1 pgoyette int rv;
381 1.15.26.1 pgoyette rv = parse_matched_arg(arg, outargs, opt, sep_idx,
382 1.15.26.1 pgoyette data, proc, is_opt);
383 1.15.26.1 pgoyette if (rv == -1)
384 1.15.26.1 pgoyette return -1;
385 1.15.26.1 pgoyette }
386 1.15.26.1 pgoyette }
387 1.1 xtraeme return 0;
388 1.15.26.1 pgoyette }
389 1.15.26.1 pgoyette else {
390 1.15.26.1 pgoyette /* No templates matched to it so just invoke the callback. */
391 1.15.26.1 pgoyette return call_proc(proc, data, arg, FUSE_OPT_KEY_OPT, outargs, is_opt);
392 1.15.26.1 pgoyette }
393 1.15.26.1 pgoyette }
394 1.1 xtraeme
395 1.15.26.1 pgoyette /* Parse a comma-separated list of options which possibly has escaped
396 1.15.26.1 pgoyette * characters. */
397 1.15.26.1 pgoyette static int
398 1.15.26.1 pgoyette parse_opts(struct fuse_args *args, int *argi, const char* arg,
399 1.15.26.1 pgoyette struct fuse_args *outargs, void *data,
400 1.15.26.1 pgoyette const struct fuse_opt *opts, fuse_opt_proc_t proc)
401 1.15.26.1 pgoyette {
402 1.15.26.1 pgoyette char *opt;
403 1.15.26.1 pgoyette size_t i, opt_len = 0;
404 1.1 xtraeme
405 1.15.26.1 pgoyette /* An unescaped option can never be longer than the original
406 1.15.26.1 pgoyette * list. */
407 1.15.26.1 pgoyette if ((opt = malloc(strlen(arg) + 1)) == NULL)
408 1.15.26.1 pgoyette return -1;
409 1.1 xtraeme
410 1.15.26.1 pgoyette for (i = 0; i < strlen(arg); i++) {
411 1.15.26.1 pgoyette if (arg[i] == ',') {
412 1.15.26.1 pgoyette opt[opt_len] = '\0';
413 1.15.26.1 pgoyette if (parse_arg(args, argi, opt, outargs,
414 1.15.26.1 pgoyette data, opts, proc, true) == -1) {
415 1.15.26.1 pgoyette free(opt);
416 1.15.26.1 pgoyette return -1;
417 1.15.26.1 pgoyette }
418 1.15.26.1 pgoyette /* Start a new option. */
419 1.15.26.1 pgoyette opt_len = 0;
420 1.15.26.1 pgoyette }
421 1.15.26.1 pgoyette else if (arg[i] == '\\' && arg[i+1] != '\0') {
422 1.15.26.1 pgoyette /* Unescape it. */
423 1.15.26.1 pgoyette opt[opt_len++] = arg[i+1];
424 1.15.26.1 pgoyette i++;
425 1.15.26.1 pgoyette }
426 1.15.26.1 pgoyette else {
427 1.15.26.1 pgoyette opt[opt_len++] = arg[i];
428 1.15.26.1 pgoyette }
429 1.15.26.1 pgoyette }
430 1.1 xtraeme
431 1.15.26.1 pgoyette /* Parse the last option here. */
432 1.15.26.1 pgoyette opt[opt_len] = '\0';
433 1.15.26.1 pgoyette if (parse_arg(args, argi, opt, outargs, data, opts, proc, true) == -1) {
434 1.15.26.1 pgoyette free(opt);
435 1.15.26.1 pgoyette return -1;
436 1.15.26.1 pgoyette }
437 1.1 xtraeme
438 1.15.26.1 pgoyette free(opt);
439 1.15.26.1 pgoyette return 0;
440 1.15.26.1 pgoyette }
441 1.1 xtraeme
442 1.15.26.1 pgoyette static int
443 1.15.26.1 pgoyette parse_all(struct fuse_args *args, struct fuse_args *outargs, void *data,
444 1.15.26.1 pgoyette const struct fuse_opt *opts, fuse_opt_proc_t proc)
445 1.15.26.1 pgoyette {
446 1.15.26.1 pgoyette bool nonopt = false; /* Have we seen the "--" marker? */
447 1.15.26.1 pgoyette int i;
448 1.15.26.1 pgoyette
449 1.15.26.1 pgoyette /* The first argument, the program name, is implicitly
450 1.15.26.1 pgoyette * FUSE_OPT_KEY_KEEP. */
451 1.15.26.1 pgoyette if (args->argc > 0) {
452 1.15.26.1 pgoyette if (fuse_opt_add_arg(outargs, args->argv[0]) == -1)
453 1.15.26.1 pgoyette return -1;
454 1.15.26.1 pgoyette }
455 1.1 xtraeme
456 1.15.26.1 pgoyette /* the real loop to process the arguments */
457 1.15.26.1 pgoyette for (i = 1; i < args->argc; i++) {
458 1.15.26.1 pgoyette const char *arg = args->argv[i];
459 1.15.26.1 pgoyette
460 1.15.26.1 pgoyette /* argvn != -foo... */
461 1.15.26.1 pgoyette if (nonopt || arg[0] != '-') {
462 1.15.26.1 pgoyette if (call_proc(proc, data, arg, FUSE_OPT_KEY_NONOPT,
463 1.15.26.1 pgoyette outargs, false) == -1)
464 1.15.26.1 pgoyette return -1;
465 1.15.26.1 pgoyette }
466 1.15.26.1 pgoyette /* -o or -ofoo */
467 1.15.26.1 pgoyette else if (arg[1] == 'o') {
468 1.1 xtraeme /* -oblah,foo... */
469 1.15.26.1 pgoyette if (arg[2] != '\0') {
470 1.1 xtraeme /* skip -o */
471 1.15.26.1 pgoyette if (parse_opts(args, &i, arg + 2, outargs,
472 1.15.26.1 pgoyette data, opts, proc) == -1)
473 1.15.26.1 pgoyette return -1;
474 1.15.26.1 pgoyette }
475 1.1 xtraeme /* -o blah,foo... */
476 1.15.26.1 pgoyette else {
477 1.15.26.1 pgoyette if (next_arg(args, &i) == -1)
478 1.15.26.1 pgoyette return -1;
479 1.15.26.1 pgoyette if (parse_opts(args, &i, args->argv[i], outargs,
480 1.15.26.1 pgoyette data, opts, proc) == -1)
481 1.15.26.1 pgoyette return -1;
482 1.1 xtraeme }
483 1.15.26.1 pgoyette }
484 1.15.26.1 pgoyette /* -- */
485 1.15.26.1 pgoyette else if (arg[1] == '-' && arg[2] == '\0') {
486 1.15.26.1 pgoyette if (fuse_opt_add_arg(outargs, arg) == -1)
487 1.15.26.1 pgoyette return -1;
488 1.15.26.1 pgoyette nonopt = true;
489 1.15.26.1 pgoyette }
490 1.15.26.1 pgoyette /* -foo */
491 1.15.26.1 pgoyette else {
492 1.15.26.1 pgoyette if (parse_arg(args, &i, arg, outargs,
493 1.15.26.1 pgoyette data, opts, proc, false) == -1)
494 1.15.26.1 pgoyette return -1;
495 1.15.26.1 pgoyette }
496 1.15.26.1 pgoyette }
497 1.1 xtraeme
498 1.15.26.1 pgoyette /* The "--" marker at the last of outargs should be removed */
499 1.15.26.1 pgoyette if (nonopt && strcmp(outargs->argv[outargs->argc - 1], "--") == 0) {
500 1.15.26.1 pgoyette free(outargs->argv[outargs->argc - 1]);
501 1.15.26.1 pgoyette outargs->argv[--outargs->argc] = NULL;
502 1.15.26.1 pgoyette }
503 1.1 xtraeme
504 1.15.26.1 pgoyette return 0;
505 1.15.26.1 pgoyette }
506 1.15.26.1 pgoyette
507 1.15.26.1 pgoyette int
508 1.15.26.1 pgoyette fuse_opt_parse(struct fuse_args *args, void *data,
509 1.15.26.1 pgoyette const struct fuse_opt *opts, fuse_opt_proc_t proc)
510 1.15.26.1 pgoyette {
511 1.15.26.1 pgoyette struct fuse_args outargs = FUSE_ARGS_INIT(0, NULL);
512 1.15.26.1 pgoyette int rv;
513 1.15.26.1 pgoyette
514 1.15.26.1 pgoyette if (!args || !args->argv || !args->argc)
515 1.15.26.1 pgoyette return 0;
516 1.15.26.1 pgoyette
517 1.15.26.1 pgoyette rv = parse_all(args, &outargs, data, opts, proc);
518 1.15.26.1 pgoyette if (rv != -1) {
519 1.15.26.1 pgoyette /* Succeeded. Swap the outargs and args. */
520 1.15.26.1 pgoyette struct fuse_args tmp = *args;
521 1.15.26.1 pgoyette *args = outargs;
522 1.15.26.1 pgoyette outargs = tmp;
523 1.1 xtraeme }
524 1.1 xtraeme
525 1.15.26.1 pgoyette fuse_opt_free_args(&outargs);
526 1.1 xtraeme return rv;
527 1.1 xtraeme }
528