blob: a2118cedc38c069facabb670dca3f2ba5ab24504 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
7*/
8
9#include "fuse_opt.h"
10#include "fuse_misc.h"
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <assert.h>
16
17struct fuse_opt_context {
18 void *data;
19 const struct fuse_opt *opt;
20 fuse_opt_proc_t proc;
21 int argctr;
22 int argc;
23 char **argv;
24 struct fuse_args outargs;
25 char *opts;
26 int nonopt;
27};
28
29void fuse_opt_free_args(struct fuse_args *args)
30{
31 if (args) {
32 if (args->argv && args->allocated) {
33 int i;
34 for (i = 0; i < args->argc; i++)
35 free(args->argv[i]);
36 free(args->argv);
37 }
38 args->argc = 0;
39 args->argv = NULL;
40 args->allocated = 0;
41 }
42}
43
44static int alloc_failed(void)
45{
46 fprintf(stderr, "fuse: memory allocation failed\n");
47 return -1;
48}
49
50int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
51{
52 char **newargv;
53 char *newarg;
54
55 assert(!args->argv || args->allocated);
56
Dees_Troye34c1332013-02-06 19:13:00 +000057 newarg = strdup(arg);
58 if (!newarg)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050059 return alloc_failed();
60
Dees_Troye34c1332013-02-06 19:13:00 +000061 newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
62 if (!newargv) {
63 free(newarg);
64 return alloc_failed();
65 }
66
bigbiff bigbiff9c754052013-01-09 09:09:08 -050067 args->argv = newargv;
68 args->allocated = 1;
69 args->argv[args->argc++] = newarg;
70 args->argv[args->argc] = NULL;
71 return 0;
72}
73
74static int fuse_opt_insert_arg_common(struct fuse_args *args, int pos,
75 const char *arg)
76{
77 assert(pos <= args->argc);
78 if (fuse_opt_add_arg(args, arg) == -1)
79 return -1;
80
81 if (pos != args->argc - 1) {
82 char *newarg = args->argv[args->argc - 1];
83 memmove(&args->argv[pos + 1], &args->argv[pos],
84 sizeof(char *) * (args->argc - pos - 1));
85 args->argv[pos] = newarg;
86 }
87 return 0;
88}
89
90int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
91{
92 return fuse_opt_insert_arg_common(args, pos, arg);
93}
94
95int fuse_opt_insert_arg_compat(struct fuse_args *args, int pos,
96 const char *arg);
97int fuse_opt_insert_arg_compat(struct fuse_args *args, int pos, const char *arg)
98{
99 return fuse_opt_insert_arg_common(args, pos, arg);
100}
101
102static int next_arg(struct fuse_opt_context *ctx, const char *opt)
103{
104 if (ctx->argctr + 1 >= ctx->argc) {
105 fprintf(stderr, "fuse: missing argument after `%s'\n", opt);
106 return -1;
107 }
108 ctx->argctr++;
109 return 0;
110}
111
112static int add_arg(struct fuse_opt_context *ctx, const char *arg)
113{
114 return fuse_opt_add_arg(&ctx->outargs, arg);
115}
116
117static int add_opt_common(char **opts, const char *opt, int esc)
118{
119 unsigned oldlen = *opts ? strlen(*opts) : 0;
120 char *d = realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1);
121
122 if (!d)
123 return alloc_failed();
124
125 *opts = d;
126 if (oldlen) {
127 d += oldlen;
128 *d++ = ',';
129 }
130
131 for (; *opt; opt++) {
132 if (esc && (*opt == ',' || *opt == '\\'))
133 *d++ = '\\';
134 *d++ = *opt;
135 }
136 *d = '\0';
137
138 return 0;
139}
140
141int fuse_opt_add_opt(char **opts, const char *opt)
142{
143 return add_opt_common(opts, opt, 0);
144}
145
146int fuse_opt_add_opt_escaped(char **opts, const char *opt)
147{
148 return add_opt_common(opts, opt, 1);
149}
150
151static int add_opt(struct fuse_opt_context *ctx, const char *opt)
152{
153 return add_opt_common(&ctx->opts, opt, 1);
154}
155
156static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
157 int iso)
158{
159 if (key == FUSE_OPT_KEY_DISCARD)
160 return 0;
161
162 if (key != FUSE_OPT_KEY_KEEP && ctx->proc) {
163 int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
164 if (res == -1 || !res)
165 return res;
166 }
167 if (iso)
168 return add_opt(ctx, arg);
169 else
170 return add_arg(ctx, arg);
171}
172
173static int match_template(const char *t, const char *arg, unsigned *sepp)
174{
175 int arglen = strlen(arg);
176 const char *sep = strchr(t, '=');
177 sep = sep ? sep : strchr(t, ' ');
178 if (sep && (!sep[1] || sep[1] == '%')) {
179 int tlen = sep - t;
180 if (sep[0] == '=')
181 tlen ++;
182 if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
183 *sepp = sep - t;
184 return 1;
185 }
186 }
187 if (strcmp(t, arg) == 0) {
188 *sepp = 0;
189 return 1;
190 }
191 return 0;
192}
193
194static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
195 const char *arg, unsigned *sepp)
196{
197 for (; opt && opt->templ; opt++)
198 if (match_template(opt->templ, arg, sepp))
199 return opt;
200 return NULL;
201}
202
203int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
204{
205 unsigned dummy;
206 return find_opt(opts, opt, &dummy) ? 1 : 0;
207}
208
209static int process_opt_param(void *var, const char *format, const char *param,
210 const char *arg)
211{
212 assert(format[0] == '%');
213 if (format[1] == 's') {
214 char *copy = strdup(param);
215 if (!copy)
216 return alloc_failed();
217
218 *(char **) var = copy;
219 } else {
220 if (sscanf(param, format, var) != 1) {
221 fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg);
222 return -1;
223 }
224 }
225 return 0;
226}
227
228static int process_opt(struct fuse_opt_context *ctx,
229 const struct fuse_opt *opt, unsigned sep,
230 const char *arg, int iso)
231{
232 if (opt->offset == -1U) {
233 if (call_proc(ctx, arg, opt->value, iso) == -1)
234 return -1;
235 } else {
236 void *var = ctx->data + opt->offset;
237 if (sep && opt->templ[sep + 1]) {
238 const char *param = arg + sep;
239 if (opt->templ[sep] == '=')
240 param ++;
241 if (process_opt_param(var, opt->templ + sep + 1,
242 param, arg) == -1)
243 return -1;
244 } else
245 *(int *)var = opt->value;
246 }
247 return 0;
248}
249
250static int process_opt_sep_arg(struct fuse_opt_context *ctx,
251 const struct fuse_opt *opt, unsigned sep,
252 const char *arg, int iso)
253{
254 int res;
255 char *newarg;
256 char *param;
257
258 if (next_arg(ctx, arg) == -1)
259 return -1;
260
261 param = ctx->argv[ctx->argctr];
262 newarg = malloc(sep + strlen(param) + 1);
263 if (!newarg)
264 return alloc_failed();
265
266 memcpy(newarg, arg, sep);
267 strcpy(newarg + sep, param);
268 res = process_opt(ctx, opt, sep, newarg, iso);
269 free(newarg);
270
271 return res;
272}
273
274static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
275{
276 unsigned sep;
277 const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
278 if (opt) {
279 for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
280 int res;
281 if (sep && opt->templ[sep] == ' ' && !arg[sep])
282 res = process_opt_sep_arg(ctx, opt, sep, arg,
283 iso);
284 else
285 res = process_opt(ctx, opt, sep, arg, iso);
286 if (res == -1)
287 return -1;
288 }
289 return 0;
290 } else
291 return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
292}
293
294static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
295{
296 char *s = opts;
297 char *d = s;
298 int end = 0;
299
300 while (!end) {
301 if (*s == '\0')
302 end = 1;
303 if (*s == ',' || end) {
304 int res;
305
306 *d = '\0';
307 res = process_gopt(ctx, opts, 1);
308 if (res == -1)
309 return -1;
310 d = opts;
311 } else {
Dees_Troye34c1332013-02-06 19:13:00 +0000312 if (s[0] == '\\' && s[1] != '\0') {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500313 s++;
Dees_Troye34c1332013-02-06 19:13:00 +0000314 if (s[0] >= '0' && s[0] <= '3' &&
315 s[1] >= '0' && s[1] <= '7' &&
316 s[2] >= '0' && s[2] <= '7') {
317 *d++ = (s[0] - '0') * 0100 +
318 (s[1] - '0') * 0010 +
319 (s[2] - '0');
320 s += 2;
321 } else {
322 *d++ = *s;
323 }
324 } else {
325 *d++ = *s;
326 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500327 }
328 s++;
329 }
330
331 return 0;
332}
333
334static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
335{
336 int res;
337 char *copy = strdup(opts);
338
339 if (!copy) {
340 fprintf(stderr, "fuse: memory allocation failed\n");
341 return -1;
342 }
343 res = process_real_option_group(ctx, copy);
344 free(copy);
345 return res;
346}
347
348static int process_one(struct fuse_opt_context *ctx, const char *arg)
349{
350 if (ctx->nonopt || arg[0] != '-')
351 return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
352 else if (arg[1] == 'o') {
353 if (arg[2])
354 return process_option_group(ctx, arg + 2);
355 else {
356 if (next_arg(ctx, arg) == -1)
357 return -1;
358
359 return process_option_group(ctx,
360 ctx->argv[ctx->argctr]);
361 }
362 } else if (arg[1] == '-' && !arg[2]) {
363 if (add_arg(ctx, arg) == -1)
364 return -1;
365 ctx->nonopt = ctx->outargs.argc;
366 return 0;
367 } else
368 return process_gopt(ctx, arg, 0);
369}
370
371static int opt_parse(struct fuse_opt_context *ctx)
372{
373 if (ctx->argc) {
374 if (add_arg(ctx, ctx->argv[0]) == -1)
375 return -1;
376 }
377
378 for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
379 if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
380 return -1;
381
382 if (ctx->opts) {
383 if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
384 fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
385 return -1;
386 }
387
388 /* If option separator ("--") is the last argument, remove it */
389 if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc &&
390 strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") == 0) {
391 free(ctx->outargs.argv[ctx->outargs.argc - 1]);
392 ctx->outargs.argv[--ctx->outargs.argc] = NULL;
393 }
394
395 return 0;
396}
397
398int fuse_opt_parse(struct fuse_args *args, void *data,
399 const struct fuse_opt opts[], fuse_opt_proc_t proc)
400{
401 int res;
402 struct fuse_opt_context ctx = {
403 .data = data,
404 .opt = opts,
405 .proc = proc,
406 };
407
408 if (!args || !args->argv || !args->argc)
409 return 0;
410
411 ctx.argc = args->argc;
412 ctx.argv = args->argv;
413
414 res = opt_parse(&ctx);
415 if (res != -1) {
416 struct fuse_args tmp = *args;
417 *args = ctx.outargs;
418 ctx.outargs = tmp;
419 }
420 free(ctx.opts);
421 fuse_opt_free_args(&ctx.outargs);
422 return res;
423}
424
425/* This symbol version was mistakenly added to the version script */
426FUSE_SYMVER(".symver fuse_opt_insert_arg_compat,fuse_opt_insert_arg@FUSE_2.5");