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
static int next_arg(struct fuse_opt_context *ctx, const char *opt)
67
if (ctx->argctr + 1 >= ctx->argc) {
68
fprintf(stderr, "fuse: missing argument after `%s'\n", opt);
75
static int add_arg(struct fuse_opt_context *ctx, const char *arg)
77
return fuse_opt_add_arg(&ctx->outargs, arg);
80
int fuse_opt_add_opt(char **opts, const char *opt)
84
newopts = strdup(opt);
86
unsigned oldlen = strlen(*opts);
87
newopts = realloc(*opts, oldlen + 1 + strlen(opt) + 1);
89
newopts[oldlen] = ',';
90
strcpy(newopts + oldlen + 1, opt);
94
return alloc_failed();
100
static int add_opt(struct fuse_opt_context *ctx, const char *opt)
102
return fuse_opt_add_opt(&ctx->opts, opt);
105
static int insert_arg(struct fuse_opt_context *ctx, int pos, const char *arg)
107
assert(pos <= ctx->outargs.argc);
108
if (add_arg(ctx, arg) == -1)
111
if (pos != ctx->outargs.argc - 1) {
112
char *newarg = ctx->outargs.argv[ctx->outargs.argc - 1];
113
memmove(&ctx->outargs.argv[pos + 1], &ctx->outargs.argv[pos],
114
sizeof(char *) * (ctx->outargs.argc - pos - 1));
115
ctx->outargs.argv[pos] = newarg;
120
static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
124
int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
125
if (res == -1 || !res)
129
return add_opt(ctx, arg);
131
return add_arg(ctx, arg);
134
static int match_template(const char *t, const char *arg, unsigned *sepp)
136
int arglen = strlen(arg);
137
const char *sep = strchr(t, '=');
138
sep = sep ? sep : strchr(t, ' ');
139
if (sep && (!sep[1] || sep[1] == '%')) {
143
if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
148
if (strcmp(t, arg) == 0) {
155
static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
156
const char *arg, unsigned *sepp)
158
for (; opt && opt->templ; opt++)
159
if (match_template(opt->templ, arg, sepp))
164
int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
167
return find_opt(opts, opt, &dummy) ? 1 : 0;
170
static int process_opt_param(void *var, const char *format, const char *param,
173
assert(format[0] == '%');
174
if (format[1] == 's') {
175
char *copy = strdup(param);
177
return alloc_failed();
179
*(char **) var = copy;
181
if (sscanf(param, format, var) != 1) {
182
fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg);
189
static int process_opt(struct fuse_opt_context *ctx,
190
const struct fuse_opt *opt, unsigned sep,
191
const char *arg, int iso)
193
if (opt->offset == -1U) {
194
if (call_proc(ctx, arg, opt->value, iso) == -1)
197
void *var = ctx->data + opt->offset;
198
if (sep && opt->templ[sep + 1]) {
199
const char *param = arg + sep;
200
if (opt->templ[sep] == '=')
202
if (process_opt_param(var, opt->templ + sep + 1,
206
*(int *)var = opt->value;
211
static int process_opt_sep_arg(struct fuse_opt_context *ctx,
212
const struct fuse_opt *opt, unsigned sep,
213
const char *arg, int iso)
219
if (next_arg(ctx, arg) == -1)
222
param = ctx->argv[ctx->argctr];
223
newarg = malloc(sep + strlen(param) + 1);
225
return alloc_failed();
227
memcpy(newarg, arg, sep);
228
strcpy(newarg + sep, param);
229
res = process_opt(ctx, opt, sep, newarg, iso);
235
static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
238
const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
240
for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
242
if (sep && opt->templ[sep] == ' ' && !arg[sep])
243
res = process_opt_sep_arg(ctx, opt, sep, arg, iso);
245
res = process_opt(ctx, opt, sep, arg, iso);
251
return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
254
static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
260
sep = strchr(opts, ',');
263
res = process_gopt(ctx, opts, 1);
272
static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
276
const char *sep = strchr(opts, ',');
278
return process_gopt(ctx, opts, 1);
282
fprintf(stderr, "fuse: memory allocation failed\n");
285
res = process_real_option_group(ctx, copy);
290
static int process_one(struct fuse_opt_context *ctx, const char *arg)
292
if (ctx->nonopt || arg[0] != '-')
293
return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
294
else if (arg[1] == 'o') {
296
return process_option_group(ctx, arg + 2);
298
if (next_arg(ctx, arg) == -1)
301
return process_option_group(ctx, ctx->argv[ctx->argctr]);
303
} else if (arg[1] == '-' && !arg[2]) {
304
if (add_arg(ctx, arg) == -1)
306
ctx->nonopt = ctx->outargs.argc;
309
return process_gopt(ctx, arg, 0);
312
static int opt_parse(struct fuse_opt_context *ctx)
315
if (add_arg(ctx, ctx->argv[0]) == -1)
319
for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
320
if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
324
if (insert_arg(ctx, 1, "-o") == -1 ||
325
insert_arg(ctx, 2, ctx->opts) == -1)
328
if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc) {
329
free(ctx->outargs.argv[ctx->outargs.argc - 1]);
330
ctx->outargs.argv[--ctx->outargs.argc] = NULL;
336
int fuse_opt_parse(struct fuse_args *args, void *data,
337
const struct fuse_opt opts[], fuse_opt_proc_t proc)
340
struct fuse_opt_context ctx = {
346
if (!args || !args->argv || !args->argc)
349
ctx.argc = args->argc;
350
ctx.argv = args->argv;
352
res = opt_parse(&ctx);
354
struct fuse_args tmp = *args;
359
fuse_opt_free_args(&ctx.outargs);