You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

426 lines
8.9 KiB

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