Reset args->argc in fuse_opt_free_args()
[fuse.git] / lib / fuse_opt.c
blob31cffaf59f31fcdee6b378e76ca9c663384c0081
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 LGPL.
6 See the file COPYING.LIB
7 */
9 #include "fuse_opt.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <assert.h>
16 struct fuse_opt_context {
17 void *data;
18 const struct fuse_opt *opt;
19 fuse_opt_proc_t proc;
20 int argctr;
21 int argc;
22 char **argv;
23 struct fuse_args outargs;
24 char *opts;
25 int nonopt;
28 void fuse_opt_free_args(struct fuse_args *args)
30 if (args && args->argv && args->allocated) {
31 int i;
32 for (i = 0; i < args->argc; i++)
33 free(args->argv[i]);
34 free(args->argv);
35 args->argc = 0;
36 args->argv = NULL;
37 args->allocated = 0;
41 static int alloc_failed(void)
43 fprintf(stderr, "fuse: memory allocation failed\n");
44 return -1;
47 int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
49 char **newargv;
50 char *newarg;
52 assert(!args->argv || args->allocated);
54 newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
55 newarg = newargv ? strdup(arg) : NULL;
56 if (!newargv || !newarg)
57 return alloc_failed();
59 args->argv = newargv;
60 args->allocated = 1;
61 args->argv[args->argc++] = newarg;
62 args->argv[args->argc] = NULL;
63 return 0;
66 int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
68 assert(pos <= args->argc);
69 if (fuse_opt_add_arg(args, arg) == -1)
70 return -1;
72 if (pos != args->argc - 1) {
73 char *newarg = args->argv[args->argc - 1];
74 memmove(&args->argv[pos + 1], &args->argv[pos],
75 sizeof(char *) * (args->argc - pos - 1));
76 args->argv[pos] = newarg;
78 return 0;
81 static int next_arg(struct fuse_opt_context *ctx, const char *opt)
83 if (ctx->argctr + 1 >= ctx->argc) {
84 fprintf(stderr, "fuse: missing argument after `%s'\n", opt);
85 return -1;
87 ctx->argctr++;
88 return 0;
91 static int add_arg(struct fuse_opt_context *ctx, const char *arg)
93 return fuse_opt_add_arg(&ctx->outargs, arg);
96 int fuse_opt_add_opt(char **opts, const char *opt)
98 char *newopts;
99 if (!*opts)
100 newopts = strdup(opt);
101 else {
102 unsigned oldlen = strlen(*opts);
103 newopts = realloc(*opts, oldlen + 1 + strlen(opt) + 1);
104 if (newopts) {
105 newopts[oldlen] = ',';
106 strcpy(newopts + oldlen + 1, opt);
109 if (!newopts)
110 return alloc_failed();
112 *opts = newopts;
113 return 0;
116 static int add_opt(struct fuse_opt_context *ctx, const char *opt)
118 return fuse_opt_add_opt(&ctx->opts, opt);
121 static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
122 int iso)
124 if (key == FUSE_OPT_KEY_DISCARD)
125 return 0;
127 if (key != FUSE_OPT_KEY_KEEP && ctx->proc) {
128 int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
129 if (res == -1 || !res)
130 return res;
132 if (iso)
133 return add_opt(ctx, arg);
134 else
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] == '%')) {
144 int tlen = sep - t;
145 if (sep[0] == '=')
146 tlen ++;
147 if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
148 *sepp = sep - t;
149 return 1;
152 if (strcmp(t, arg) == 0) {
153 *sepp = 0;
154 return 1;
156 return 0;
159 static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
160 const char *arg, unsigned *sepp)
162 for (; opt && opt->templ; opt++)
163 if (match_template(opt->templ, arg, sepp))
164 return opt;
165 return NULL;
168 int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
170 unsigned dummy;
171 return find_opt(opts, opt, &dummy) ? 1 : 0;
174 static int process_opt_param(void *var, const char *format, const char *param,
175 const char *arg)
177 assert(format[0] == '%');
178 if (format[1] == 's') {
179 char *copy = strdup(param);
180 if (!copy)
181 return alloc_failed();
183 *(char **) var = copy;
184 } else {
185 if (sscanf(param, format, var) != 1) {
186 fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg);
187 return -1;
190 return 0;
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)
199 return -1;
200 } else {
201 void *var = ctx->data + opt->offset;
202 if (sep && opt->templ[sep + 1]) {
203 const char *param = arg + sep;
204 if (opt->templ[sep] == '=')
205 param ++;
206 if (process_opt_param(var, opt->templ + sep + 1,
207 param, arg) == -1)
208 return -1;
209 } else
210 *(int *)var = opt->value;
212 return 0;
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)
219 int res;
220 char *newarg;
221 char *param;
223 if (next_arg(ctx, arg) == -1)
224 return -1;
226 param = ctx->argv[ctx->argctr];
227 newarg = malloc(sep + strlen(param) + 1);
228 if (!newarg)
229 return alloc_failed();
231 memcpy(newarg, arg, sep);
232 strcpy(newarg + sep, param);
233 res = process_opt(ctx, opt, sep, newarg, iso);
234 free(newarg);
236 return res;
239 static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
241 unsigned sep;
242 const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
243 if (opt) {
244 for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
245 int res;
246 if (sep && opt->templ[sep] == ' ' && !arg[sep])
247 res = process_opt_sep_arg(ctx, opt, sep, arg, iso);
248 else
249 res = process_opt(ctx, opt, sep, arg, iso);
250 if (res == -1)
251 return -1;
253 return 0;
254 } else
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)
260 char *sep;
262 do {
263 int res;
264 sep = strchr(opts, ',');
265 if (sep)
266 *sep = '\0';
267 res = process_gopt(ctx, opts, 1);
268 if (res == -1)
269 return -1;
270 opts = sep + 1;
271 } while (sep);
273 return 0;
276 static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
278 int res;
279 char *copy;
280 const char *sep = strchr(opts, ',');
281 if (!sep)
282 return process_gopt(ctx, opts, 1);
284 copy = strdup(opts);
285 if (!copy) {
286 fprintf(stderr, "fuse: memory allocation failed\n");
287 return -1;
289 res = process_real_option_group(ctx, copy);
290 free(copy);
291 return res;
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') {
299 if (arg[2])
300 return process_option_group(ctx, arg + 2);
301 else {
302 if (next_arg(ctx, arg) == -1)
303 return -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)
309 return -1;
310 ctx->nonopt = ctx->outargs.argc;
311 return 0;
312 } else
313 return process_gopt(ctx, arg, 0);
316 static int opt_parse(struct fuse_opt_context *ctx)
318 if (ctx->argc) {
319 if (add_arg(ctx, ctx->argv[0]) == -1)
320 return -1;
323 for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
324 if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
325 return -1;
327 if (ctx->opts) {
328 if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
329 fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
330 return -1;
332 if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc) {
333 free(ctx->outargs.argv[ctx->outargs.argc - 1]);
334 ctx->outargs.argv[--ctx->outargs.argc] = NULL;
337 return 0;
340 int fuse_opt_parse(struct fuse_args *args, void *data,
341 const struct fuse_opt opts[], fuse_opt_proc_t proc)
343 int res;
344 struct fuse_opt_context ctx = {
345 .data = data,
346 .opt = opts,
347 .proc = proc,
350 if (!args || !args->argv || !args->argc)
351 return 0;
353 ctx.argc = args->argc;
354 ctx.argv = args->argv;
356 res = opt_parse(&ctx);
357 if (res != -1) {
358 struct fuse_args tmp = *args;
359 *args = ctx.outargs;
360 ctx.outargs = tmp;
362 free(ctx.opts);
363 fuse_opt_free_args(&ctx.outargs);
364 return res;