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