second commit :)
[rrq/fuse_xattrs.git] / compat / fuse_opt.c
1 /*
2     FUSE: Filesystem in Userspace
3     Copyright (C) 2001-2006  Miklos Szeredi <miklos@szeredi.hu>
4
5     This program can be distributed under the terms of the GNU LGPL.
6     See the file COPYING.LIB
7 */
8
9 #include "fuse_opt.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <assert.h>
15
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;
26 };
27
28 void fuse_opt_free_args(struct fuse_args *args)
29 {
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->argv = NULL;
36         args->allocated = 0;
37     }
38 }
39
40 static int alloc_failed(void)
41 {
42     fprintf(stderr, "fuse: memory allocation failed\n");
43     return -1;
44 }
45
46 int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
47 {
48     char **newargv;
49     char *newarg;
50
51     assert(!args->argv || args->allocated);
52
53     newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
54     newarg = newargv ? strdup(arg) : NULL;
55     if (!newargv || !newarg)
56         return alloc_failed();
57
58     args->argv = newargv;
59     args->allocated = 1;
60     args->argv[args->argc++] = newarg;
61     args->argv[args->argc] = NULL;
62     return 0;
63 }
64
65 int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
66 {
67     assert(pos <= args->argc);
68     if (fuse_opt_add_arg(args, arg) == -1)
69         return -1;
70
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;
76     }
77     return 0;
78 }
79
80 static int next_arg(struct fuse_opt_context *ctx, const char *opt)
81 {
82     if (ctx->argctr + 1 >= ctx->argc) {
83         fprintf(stderr, "fuse: missing argument after `%s'\n", opt);
84         return -1;
85     }
86     ctx->argctr++;
87     return 0;
88 }
89
90 static int add_arg(struct fuse_opt_context *ctx, const char *arg)
91 {
92     return fuse_opt_add_arg(&ctx->outargs, arg);
93 }
94
95 int fuse_opt_add_opt(char **opts, const char *opt)
96 {
97     char *newopts;
98     if (!*opts)
99         newopts = strdup(opt);
100     else {
101         unsigned oldlen = strlen(*opts);
102         newopts = realloc(*opts, oldlen + 1 + strlen(opt) + 1);
103         if (newopts) {
104             newopts[oldlen] = ',';
105             strcpy(newopts + oldlen + 1, opt);
106         }
107     }
108     if (!newopts)
109         return alloc_failed();
110
111     *opts = newopts;
112     return 0;
113 }
114
115 static int add_opt(struct fuse_opt_context *ctx, const char *opt)
116 {
117     return fuse_opt_add_opt(&ctx->opts, opt);
118 }
119
120 static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
121                      int iso)
122 {
123     if (key == FUSE_OPT_KEY_DISCARD)
124         return 0;
125
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)
129             return res;
130     }
131     if (iso)
132         return add_opt(ctx, arg);
133     else
134         return add_arg(ctx, arg);
135 }
136
137 static int match_template(const char *t, const char *arg, unsigned *sepp)
138 {
139     int arglen = strlen(arg);
140     const char *sep = strchr(t, '=');
141     sep = sep ? sep : strchr(t, ' ');
142     if (sep && (!sep[1] || sep[1] == '%')) {
143         int tlen = sep - t;
144         if (sep[0] == '=')
145             tlen ++;
146         if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
147             *sepp = sep - t;
148             return 1;
149         }
150     }
151     if (strcmp(t, arg) == 0) {
152         *sepp = 0;
153         return 1;
154     }
155     return 0;
156 }
157
158 static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
159                                        const char *arg, unsigned *sepp)
160 {
161     for (; opt && opt->templ; opt++)
162         if (match_template(opt->templ, arg, sepp))
163             return opt;
164     return NULL;
165 }
166
167 int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
168 {
169     unsigned dummy;
170     return find_opt(opts, opt, &dummy) ? 1 : 0;
171 }
172
173 static int process_opt_param(void *var, const char *format, const char *param,
174                              const char *arg)
175 {
176     assert(format[0] == '%');
177     if (format[1] == 's') {
178         char *copy = strdup(param);
179         if (!copy)
180             return alloc_failed();
181
182         *(char **) var = copy;
183     } else {
184         if (sscanf(param, format, var) != 1) {
185             fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg);
186             return -1;
187         }
188     }
189     return 0;
190 }
191
192 static int process_opt(struct fuse_opt_context *ctx,
193                        const struct fuse_opt *opt, unsigned sep,
194                        const char *arg, int iso)
195 {
196     if (opt->offset == -1U) {
197         if (call_proc(ctx, arg, opt->value, iso) == -1)
198             return -1;
199     } else {
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] == '=')
204                 param ++;
205             if (process_opt_param(var, opt->templ + sep + 1,
206                                   param, arg) == -1)
207                 return -1;
208         } else
209             *(int *)var = opt->value;
210     }
211     return 0;
212 }
213
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)
217 {
218     int res;
219     char *newarg;
220     char *param;
221
222     if (next_arg(ctx, arg) == -1)
223         return -1;
224
225     param = ctx->argv[ctx->argctr];
226     newarg = malloc(sep + strlen(param) + 1);
227     if (!newarg)
228         return alloc_failed();
229
230     memcpy(newarg, arg, sep);
231     strcpy(newarg + sep, param);
232     res = process_opt(ctx, opt, sep, newarg, iso);
233     free(newarg);
234
235     return res;
236 }
237
238 static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
239 {
240     unsigned sep;
241     const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
242     if (opt) {
243         for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
244             int res;
245             if (sep && opt->templ[sep] == ' ' && !arg[sep])
246                 res = process_opt_sep_arg(ctx, opt, sep, arg, iso);
247             else
248                 res = process_opt(ctx, opt, sep, arg, iso);
249             if (res == -1)
250                 return -1;
251         }
252         return 0;
253     } else
254         return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
255 }
256
257 static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
258 {
259     char *sep;
260
261     do {
262         int res;
263         sep = strchr(opts, ',');
264         if (sep)
265             *sep = '\0';
266         res = process_gopt(ctx, opts, 1);
267         if (res == -1)
268             return -1;
269         opts = sep + 1;
270     } while (sep);
271
272     return 0;
273 }
274
275 static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
276 {
277     int res;
278     char *copy;
279     const char *sep = strchr(opts, ',');
280     if (!sep)
281         return process_gopt(ctx, opts, 1);
282
283     copy = strdup(opts);
284     if (!copy) {
285         fprintf(stderr, "fuse: memory allocation failed\n");
286         return -1;
287     }
288     res = process_real_option_group(ctx, copy);
289     free(copy);
290     return res;
291 }
292
293 static int process_one(struct fuse_opt_context *ctx, const char *arg)
294 {
295     if (ctx->nonopt || arg[0] != '-')
296         return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
297     else if (arg[1] == 'o') {
298         if (arg[2])
299             return process_option_group(ctx, arg + 2);
300         else {
301             if (next_arg(ctx, arg) == -1)
302                 return -1;
303
304             return process_option_group(ctx, ctx->argv[ctx->argctr]);
305         }
306     } else if (arg[1] == '-' && !arg[2]) {
307         if (add_arg(ctx, arg) == -1)
308             return -1;
309         ctx->nonopt = ctx->outargs.argc;
310         return 0;
311     } else
312         return process_gopt(ctx, arg, 0);
313 }
314
315 static int opt_parse(struct fuse_opt_context *ctx)
316 {
317     if (ctx->argc) {
318         if (add_arg(ctx, ctx->argv[0]) == -1)
319             return -1;
320     }
321
322     for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
323         if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
324             return -1;
325
326     if (ctx->opts) {
327         if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
328             fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
329             return -1;
330     }
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;
334     }
335
336     return 0;
337 }
338
339 int fuse_opt_parse(struct fuse_args *args, void *data,
340                    const struct fuse_opt opts[], fuse_opt_proc_t proc)
341 {
342     int res;
343     struct fuse_opt_context ctx = {
344         .data = data,
345         .opt = opts,
346         .proc = proc,
347     };
348
349     if (!args || !args->argv || !args->argc)
350         return 0;
351
352     ctx.argc = args->argc;
353     ctx.argv = args->argv;
354
355     res = opt_parse(&ctx);
356     if (res != -1) {
357         struct fuse_args tmp = *args;
358         *args = ctx.outargs;
359         ctx.outargs = tmp;
360     }
361     free(ctx.opts);
362     fuse_opt_free_args(&ctx.outargs);
363     return res;
364 }