fusermount: clean up do_mount() function
[fuse.git] / lib / fuse_opt.c
blobb15e7db11905b4f315fd99f0cac7231cb9368345
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
7 */
9 #include "fuse_opt.h"
10 #include "fuse_misc.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <assert.h>
17 struct fuse_opt_context {
18 void *data;
19 const struct fuse_opt *opt;
20 fuse_opt_proc_t proc;
21 int argctr;
22 int argc;
23 char **argv;
24 struct fuse_args outargs;
25 char *opts;
26 int nonopt;
29 void fuse_opt_free_args(struct fuse_args *args)
31 if (args) {
32 if (args->argv && args->allocated) {
33 int i;
34 for (i = 0; i < args->argc; i++)
35 free(args->argv[i]);
36 free(args->argv);
38 args->argc = 0;
39 args->argv = NULL;
40 args->allocated = 0;
44 static int alloc_failed(void)
46 fprintf(stderr, "fuse: memory allocation failed\n");
47 return -1;
50 int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
52 char **newargv;
53 char *newarg;
55 assert(!args->argv || args->allocated);
57 newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
58 newarg = newargv ? strdup(arg) : NULL;
59 if (!newargv || !newarg)
60 return alloc_failed();
62 args->argv = newargv;
63 args->allocated = 1;
64 args->argv[args->argc++] = newarg;
65 args->argv[args->argc] = NULL;
66 return 0;
69 static int fuse_opt_insert_arg_common(struct fuse_args *args, int pos,
70 const char *arg)
72 assert(pos <= args->argc);
73 if (fuse_opt_add_arg(args, arg) == -1)
74 return -1;
76 if (pos != args->argc - 1) {
77 char *newarg = args->argv[args->argc - 1];
78 memmove(&args->argv[pos + 1], &args->argv[pos],
79 sizeof(char *) * (args->argc - pos - 1));
80 args->argv[pos] = newarg;
82 return 0;
85 int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
87 return fuse_opt_insert_arg_common(args, pos, arg);
90 int fuse_opt_insert_arg_compat(struct fuse_args *args, int pos,
91 const char *arg);
92 int fuse_opt_insert_arg_compat(struct fuse_args *args, int pos, const char *arg)
94 return fuse_opt_insert_arg_common(args, pos, arg);
97 static int next_arg(struct fuse_opt_context *ctx, const char *opt)
99 if (ctx->argctr + 1 >= ctx->argc) {
100 fprintf(stderr, "fuse: missing argument after `%s'\n", opt);
101 return -1;
103 ctx->argctr++;
104 return 0;
107 static int add_arg(struct fuse_opt_context *ctx, const char *arg)
109 return fuse_opt_add_arg(&ctx->outargs, arg);
112 static int add_opt_common(char **opts, const char *opt, int esc)
114 unsigned oldlen = *opts ? strlen(*opts) : 0;
115 char *d = realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1);
117 if (!d)
118 return alloc_failed();
120 *opts = d;
121 if (oldlen) {
122 d += oldlen;
123 *d++ = ',';
126 for (; *opt; opt++) {
127 if (esc && (*opt == ',' || *opt == '\\'))
128 *d++ = '\\';
129 *d++ = *opt;
131 *d = '\0';
133 return 0;
136 int fuse_opt_add_opt(char **opts, const char *opt)
138 return add_opt_common(opts, opt, 0);
141 int fuse_opt_add_opt_escaped(char **opts, const char *opt)
143 return add_opt_common(opts, opt, 1);
146 static int add_opt(struct fuse_opt_context *ctx, const char *opt)
148 return add_opt_common(&ctx->opts, opt, 1);
151 static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
152 int iso)
154 if (key == FUSE_OPT_KEY_DISCARD)
155 return 0;
157 if (key != FUSE_OPT_KEY_KEEP && ctx->proc) {
158 int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
159 if (res == -1 || !res)
160 return res;
162 if (iso)
163 return add_opt(ctx, arg);
164 else
165 return add_arg(ctx, arg);
168 static int match_template(const char *t, const char *arg, unsigned *sepp)
170 int arglen = strlen(arg);
171 const char *sep = strchr(t, '=');
172 sep = sep ? sep : strchr(t, ' ');
173 if (sep && (!sep[1] || sep[1] == '%')) {
174 int tlen = sep - t;
175 if (sep[0] == '=')
176 tlen ++;
177 if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
178 *sepp = sep - t;
179 return 1;
182 if (strcmp(t, arg) == 0) {
183 *sepp = 0;
184 return 1;
186 return 0;
189 static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
190 const char *arg, unsigned *sepp)
192 for (; opt && opt->templ; opt++)
193 if (match_template(opt->templ, arg, sepp))
194 return opt;
195 return NULL;
198 int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
200 unsigned dummy;
201 return find_opt(opts, opt, &dummy) ? 1 : 0;
204 static int process_opt_param(void *var, const char *format, const char *param,
205 const char *arg)
207 assert(format[0] == '%');
208 if (format[1] == 's') {
209 char *copy = strdup(param);
210 if (!copy)
211 return alloc_failed();
213 *(char **) var = copy;
214 } else {
215 if (sscanf(param, format, var) != 1) {
216 fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg);
217 return -1;
220 return 0;
223 static int process_opt(struct fuse_opt_context *ctx,
224 const struct fuse_opt *opt, unsigned sep,
225 const char *arg, int iso)
227 if (opt->offset == -1U) {
228 if (call_proc(ctx, arg, opt->value, iso) == -1)
229 return -1;
230 } else {
231 void *var = ctx->data + opt->offset;
232 if (sep && opt->templ[sep + 1]) {
233 const char *param = arg + sep;
234 if (opt->templ[sep] == '=')
235 param ++;
236 if (process_opt_param(var, opt->templ + sep + 1,
237 param, arg) == -1)
238 return -1;
239 } else
240 *(int *)var = opt->value;
242 return 0;
245 static int process_opt_sep_arg(struct fuse_opt_context *ctx,
246 const struct fuse_opt *opt, unsigned sep,
247 const char *arg, int iso)
249 int res;
250 char *newarg;
251 char *param;
253 if (next_arg(ctx, arg) == -1)
254 return -1;
256 param = ctx->argv[ctx->argctr];
257 newarg = malloc(sep + strlen(param) + 1);
258 if (!newarg)
259 return alloc_failed();
261 memcpy(newarg, arg, sep);
262 strcpy(newarg + sep, param);
263 res = process_opt(ctx, opt, sep, newarg, iso);
264 free(newarg);
266 return res;
269 static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
271 unsigned sep;
272 const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
273 if (opt) {
274 for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
275 int res;
276 if (sep && opt->templ[sep] == ' ' && !arg[sep])
277 res = process_opt_sep_arg(ctx, opt, sep, arg,
278 iso);
279 else
280 res = process_opt(ctx, opt, sep, arg, iso);
281 if (res == -1)
282 return -1;
284 return 0;
285 } else
286 return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
289 static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
291 char *s = opts;
292 char *d = s;
293 int end = 0;
295 while (!end) {
296 if (*s == '\0')
297 end = 1;
298 if (*s == ',' || end) {
299 int res;
301 *d = '\0';
302 res = process_gopt(ctx, opts, 1);
303 if (res == -1)
304 return -1;
305 d = opts;
306 } else {
307 if (s[0] == '\\' && s[1] != '\0')
308 s++;
309 *d++ = *s;
311 s++;
314 return 0;
317 static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
319 int res;
320 char *copy = strdup(opts);
322 if (!copy) {
323 fprintf(stderr, "fuse: memory allocation failed\n");
324 return -1;
326 res = process_real_option_group(ctx, copy);
327 free(copy);
328 return res;
331 static int process_one(struct fuse_opt_context *ctx, const char *arg)
333 if (ctx->nonopt || arg[0] != '-')
334 return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
335 else if (arg[1] == 'o') {
336 if (arg[2])
337 return process_option_group(ctx, arg + 2);
338 else {
339 if (next_arg(ctx, arg) == -1)
340 return -1;
342 return process_option_group(ctx,
343 ctx->argv[ctx->argctr]);
345 } else if (arg[1] == '-' && !arg[2]) {
346 if (add_arg(ctx, arg) == -1)
347 return -1;
348 ctx->nonopt = ctx->outargs.argc;
349 return 0;
350 } else
351 return process_gopt(ctx, arg, 0);
354 static int opt_parse(struct fuse_opt_context *ctx)
356 if (ctx->argc) {
357 if (add_arg(ctx, ctx->argv[0]) == -1)
358 return -1;
361 for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
362 if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
363 return -1;
365 if (ctx->opts) {
366 if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
367 fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
368 return -1;
371 /* If option separator ("--") is the last argument, remove it */
372 if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc &&
373 strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") == 0) {
374 free(ctx->outargs.argv[ctx->outargs.argc - 1]);
375 ctx->outargs.argv[--ctx->outargs.argc] = NULL;
378 return 0;
381 int fuse_opt_parse(struct fuse_args *args, void *data,
382 const struct fuse_opt opts[], fuse_opt_proc_t proc)
384 int res;
385 struct fuse_opt_context ctx = {
386 .data = data,
387 .opt = opts,
388 .proc = proc,
391 if (!args || !args->argv || !args->argc)
392 return 0;
394 ctx.argc = args->argc;
395 ctx.argv = args->argv;
397 res = opt_parse(&ctx);
398 if (res != -1) {
399 struct fuse_args tmp = *args;
400 *args = ctx.outargs;
401 ctx.outargs = tmp;
403 free(ctx.opts);
404 fuse_opt_free_args(&ctx.outargs);
405 return res;
408 /* This symbol version was mistakenly added to the version script */
409 FUSE_SYMVER(".symver fuse_opt_insert_arg_compat,fuse_opt_insert_arg@FUSE_2.5");