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.

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