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