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.

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