2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPL.
6 See the file COPYING.LIB
16 struct fuse_opt_context {
18 const struct fuse_opt *opt;
23 struct fuse_args outargs;
28 void fuse_opt_free_args(struct fuse_args *args)
30 if (args && args->argv && args->allocated) {
32 for (i = 0; i < args->argc; i++)
40 static int alloc_failed(void)
42 fprintf(stderr, "fuse: memory allocation failed\n");
46 int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
51 assert(!args->argv || args->allocated);
53 newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
54 newarg = newargv ? strdup(arg) : NULL;
55 if (!newargv || !newarg)
56 return alloc_failed();
60 args->argv[args->argc++] = newarg;
61 args->argv[args->argc] = NULL;
65 int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
67 assert(pos <= args->argc);
68 if (fuse_opt_add_arg(args, arg) == -1)
71 if (pos != args->argc - 1) {
72 char *newarg = args->argv[args->argc - 1];
73 memmove(&args->argv[pos + 1], &args->argv[pos],
74 sizeof(char *) * (args->argc - pos - 1));
75 args->argv[pos] = newarg;
80 static int next_arg(struct fuse_opt_context *ctx, const char *opt)
82 if (ctx->argctr + 1 >= ctx->argc) {
83 fprintf(stderr, "fuse: missing argument after `%s'\n", opt);
90 static int add_arg(struct fuse_opt_context *ctx, const char *arg)
92 return fuse_opt_add_arg(&ctx->outargs, arg);
95 int fuse_opt_add_opt(char **opts, const char *opt)
99 newopts = strdup(opt);
101 unsigned oldlen = strlen(*opts);
102 newopts = realloc(*opts, oldlen + 1 + strlen(opt) + 1);
104 newopts[oldlen] = ',';
105 strcpy(newopts + oldlen + 1, opt);
109 return alloc_failed();
115 static int add_opt(struct fuse_opt_context *ctx, const char *opt)
117 return fuse_opt_add_opt(&ctx->opts, opt);
120 static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
123 if (key == FUSE_OPT_KEY_DISCARD)
126 if (key != FUSE_OPT_KEY_KEEP && ctx->proc) {
127 int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
128 if (res == -1 || !res)
132 return add_opt(ctx, arg);
134 return add_arg(ctx, arg);
137 static int match_template(const char *t, const char *arg, unsigned *sepp)
139 int arglen = strlen(arg);
140 const char *sep = strchr(t, '=');
141 sep = sep ? sep : strchr(t, ' ');
142 if (sep && (!sep[1] || sep[1] == '%')) {
146 if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
151 if (strcmp(t, arg) == 0) {
158 static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
159 const char *arg, unsigned *sepp)
161 for (; opt && opt->templ; opt++)
162 if (match_template(opt->templ, arg, sepp))
167 int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
170 return find_opt(opts, opt, &dummy) ? 1 : 0;
173 static int process_opt_param(void *var, const char *format, const char *param,
176 assert(format[0] == '%');
177 if (format[1] == 's') {
178 char *copy = strdup(param);
180 return alloc_failed();
182 *(char **) var = copy;
184 if (sscanf(param, format, var) != 1) {
185 fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg);
192 static int process_opt(struct fuse_opt_context *ctx,
193 const struct fuse_opt *opt, unsigned sep,
194 const char *arg, int iso)
196 if (opt->offset == -1U) {
197 if (call_proc(ctx, arg, opt->value, iso) == -1)
200 void *var = ctx->data + opt->offset;
201 if (sep && opt->templ[sep + 1]) {
202 const char *param = arg + sep;
203 if (opt->templ[sep] == '=')
205 if (process_opt_param(var, opt->templ + sep + 1,
209 *(int *)var = opt->value;
214 static int process_opt_sep_arg(struct fuse_opt_context *ctx,
215 const struct fuse_opt *opt, unsigned sep,
216 const char *arg, int iso)
222 if (next_arg(ctx, arg) == -1)
225 param = ctx->argv[ctx->argctr];
226 newarg = malloc(sep + strlen(param) + 1);
228 return alloc_failed();
230 memcpy(newarg, arg, sep);
231 strcpy(newarg + sep, param);
232 res = process_opt(ctx, opt, sep, newarg, iso);
238 static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
241 const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
243 for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
245 if (sep && opt->templ[sep] == ' ' && !arg[sep])
246 res = process_opt_sep_arg(ctx, opt, sep, arg, iso);
248 res = process_opt(ctx, opt, sep, arg, iso);
254 return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
257 static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
263 sep = strchr(opts, ',');
266 res = process_gopt(ctx, opts, 1);
275 static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
279 const char *sep = strchr(opts, ',');
281 return process_gopt(ctx, opts, 1);
285 fprintf(stderr, "fuse: memory allocation failed\n");
288 res = process_real_option_group(ctx, copy);
293 static int process_one(struct fuse_opt_context *ctx, const char *arg)
295 if (ctx->nonopt || arg[0] != '-')
296 return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
297 else if (arg[1] == 'o') {
299 return process_option_group(ctx, arg + 2);
301 if (next_arg(ctx, arg) == -1)
304 return process_option_group(ctx, ctx->argv[ctx->argctr]);
306 } else if (arg[1] == '-' && !arg[2]) {
307 if (add_arg(ctx, arg) == -1)
309 ctx->nonopt = ctx->outargs.argc;
312 return process_gopt(ctx, arg, 0);
315 static int opt_parse(struct fuse_opt_context *ctx)
318 if (add_arg(ctx, ctx->argv[0]) == -1)
322 for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
323 if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
327 if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
328 fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
331 if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc) {
332 free(ctx->outargs.argv[ctx->outargs.argc - 1]);
333 ctx->outargs.argv[--ctx->outargs.argc] = NULL;
339 int fuse_opt_parse(struct fuse_args *args, void *data,
340 const struct fuse_opt opts[], fuse_opt_proc_t proc)
343 struct fuse_opt_context ctx = {
349 if (!args || !args->argv || !args->argc)
352 ctx.argc = args->argc;
353 ctx.argv = args->argv;
355 res = opt_parse(&ctx);
357 struct fuse_args tmp = *args;
362 fuse_opt_free_args(&ctx.outargs);