2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This file is borrowed from sshfs-fuse. It provides an implementation
6 of the FUSE command line parsing functions which only appear in
7 later version of FUSE but are used in afuse.
9 This file can be distributed under the terms of the GNU LGPL.
10 See the file COPYING.LIB
20 struct fuse_opt_context {
22 const struct fuse_opt *opt;
27 struct fuse_args outargs;
32 void fuse_opt_free_args(struct fuse_args *args)
34 if (args && args->argv && args->allocated) {
36 for (i = 0; i < args->argc; i++)
44 static int alloc_failed(void)
46 fprintf(stderr, "fuse: memory allocation failed\n");
50 int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
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();
64 args->argv[args->argc++] = newarg;
65 args->argv[args->argc] = NULL;
69 static int next_arg(struct fuse_opt_context *ctx, const char *opt)
71 if (ctx->argctr + 1 >= ctx->argc) {
72 fprintf(stderr, "fuse: missing argument after `%s'\n", opt);
79 static int add_arg(struct fuse_opt_context *ctx, const char *arg)
81 return fuse_opt_add_arg(&ctx->outargs, arg);
84 int fuse_opt_add_opt(char **opts, const char *opt)
88 newopts = strdup(opt);
90 unsigned oldlen = strlen(*opts);
91 newopts = realloc(*opts, oldlen + 1 + strlen(opt) + 1);
93 newopts[oldlen] = ',';
94 strcpy(newopts + oldlen + 1, opt);
98 return alloc_failed();
104 static int add_opt(struct fuse_opt_context *ctx, const char *opt)
106 return fuse_opt_add_opt(&ctx->opts, opt);
109 static int insert_arg(struct fuse_opt_context *ctx, int pos, const char *arg)
111 assert(pos <= ctx->outargs.argc);
112 if (add_arg(ctx, arg) == -1)
115 if (pos != ctx->outargs.argc - 1) {
116 char *newarg = ctx->outargs.argv[ctx->outargs.argc - 1];
117 memmove(&ctx->outargs.argv[pos + 1], &ctx->outargs.argv[pos],
118 sizeof(char *) * (ctx->outargs.argc - pos - 1));
119 ctx->outargs.argv[pos] = newarg;
124 static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
128 int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
129 if (res == -1 || !res)
133 return add_opt(ctx, arg);
135 return add_arg(ctx, arg);
138 static int match_template(const char *t, const char *arg, unsigned *sepp)
140 int arglen = strlen(arg);
141 const char *sep = strchr(t, '=');
142 sep = sep ? sep : strchr(t, ' ');
143 if (sep && (!sep[1] || sep[1] == '%')) {
147 if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
152 if (strcmp(t, arg) == 0) {
159 static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
160 const char *arg, unsigned *sepp)
162 for (; opt && opt->template; opt++)
163 if (match_template(opt->template, arg, sepp))
168 int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
171 return find_opt(opts, opt, &dummy) ? 1 : 0;
174 static int process_opt_param(void *var, const char *format, const char *param,
177 assert(format[0] == '%');
178 if (format[1] == 's') {
179 char *copy = strdup(param);
181 return alloc_failed();
183 *(char **) var = copy;
185 if (sscanf(param, format, var) != 1) {
186 fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg);
193 static int process_opt(struct fuse_opt_context *ctx,
194 const struct fuse_opt *opt, unsigned sep,
195 const char *arg, int iso)
197 if (opt->offset == -1U) {
198 if (call_proc(ctx, arg, opt->value, iso) == -1)
201 void *var = ctx->data + opt->offset;
202 if (sep && opt->template[sep + 1]) {
203 const char *param = arg + sep;
204 if (opt->template[sep] == '=')
206 if (process_opt_param(var, opt->template + sep + 1,
210 *(int *)var = opt->value;
215 static int process_opt_sep_arg(struct fuse_opt_context *ctx,
216 const struct fuse_opt *opt, unsigned sep,
217 const char *arg, int iso)
223 if (next_arg(ctx, arg) == -1)
226 param = ctx->argv[ctx->argctr];
227 newarg = malloc(sep + strlen(param) + 1);
229 return alloc_failed();
231 memcpy(newarg, arg, sep);
232 strcpy(newarg + sep, param);
233 res = process_opt(ctx, opt, sep, newarg, iso);
239 static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
242 const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
244 for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
246 if (sep && opt->template[sep] == ' ' && !arg[sep])
247 res = process_opt_sep_arg(ctx, opt, sep, arg, iso);
249 res = process_opt(ctx, opt, sep, arg, iso);
255 return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
258 static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
264 sep = strchr(opts, ',');
267 res = process_gopt(ctx, opts, 1);
276 static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
280 const char *sep = strchr(opts, ',');
282 return process_gopt(ctx, opts, 1);
286 fprintf(stderr, "fuse: memory allocation failed\n");
289 res = process_real_option_group(ctx, copy);
294 static int process_one(struct fuse_opt_context *ctx, const char *arg)
296 if (ctx->nonopt || arg[0] != '-')
297 return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
298 else if (arg[1] == 'o') {
300 return process_option_group(ctx, arg + 2);
302 if (next_arg(ctx, arg) == -1)
305 return process_option_group(ctx, ctx->argv[ctx->argctr]);
307 } else if (arg[1] == '-' && !arg[2]) {
308 if (add_arg(ctx, arg) == -1)
310 ctx->nonopt = ctx->outargs.argc;
313 return process_gopt(ctx, arg, 0);
316 static int opt_parse(struct fuse_opt_context *ctx)
319 if (add_arg(ctx, ctx->argv[0]) == -1)
323 for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
324 if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
328 if (insert_arg(ctx, 1, "-o") == -1 ||
329 insert_arg(ctx, 2, ctx->opts) == -1)
332 if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc)
333 ctx->outargs.argv[--ctx->outargs.argc] = NULL;
338 int fuse_opt_parse(struct fuse_args *args, void *data,
339 const struct fuse_opt opts[], fuse_opt_proc_t proc)
342 struct fuse_opt_context ctx = {
348 if (!args || !args->argv || !args->argc)
351 ctx.argc = args->argc;
352 ctx.argv = args->argv;
354 res = opt_parse(&ctx);
356 struct fuse_args tmp = *args;
361 fuse_opt_free_args(&ctx.outargs);