aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Miklos Szeredi <miklos@szeredi.hu>2005-12-16 11:12:16 +0000
committerGravatar Miklos Szeredi <miklos@szeredi.hu>2005-12-16 11:12:16 +0000
commit73f4139eb94ed98d66ec072f3ceb2a7d7f7bad59 (patch)
treeafdf837b0371f04253dd813859266a7df5b1c105
parent154ffe2c27e84a29eafedc5327f663a313f2721a (diff)
fix
-rw-r--r--ChangeLog6
-rw-r--r--include/fuse_opt.h43
-rw-r--r--lib/fuse.c7
-rw-r--r--lib/fuse_lowlevel.c7
-rw-r--r--lib/fuse_opt.c73
-rw-r--r--lib/helper.c7
6 files changed, 72 insertions, 71 deletions
diff --git a/ChangeLog b/ChangeLog
index 2da637b..e613354 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2005-12-16 Miklos Szeredi <miklos@szeredi.hu>
+
+ * Clean up the option parsing interface slightly, by creating an
+ "argument list" structure, that contains the argument vector and
+ count
+
2005-12-15 Miklos Szeredi <miklos@szeredi.hu>
* fusermount: check if /mnt/mtab is a symlink and don't modify it
diff --git a/include/fuse_opt.h b/include/fuse_opt.h
index 8281882..f3a06dd 100644
--- a/include/fuse_opt.h
+++ b/include/fuse_opt.h
@@ -94,6 +94,17 @@ struct fuse_opt {
};
/**
+ * Argument list
+ */
+struct fuse_args {
+ /** Argument count */
+ int argc;
+
+ /* Argument vector. NULL terminated */
+ char **argv;
+};
+
+/**
* Last option. An array of 'struct fuse_opt' must end with a NULL
* template value
*/
@@ -136,18 +147,16 @@ struct fuse_opt {
* @param data is the user data passed to the fuse_opt_parse() function
* @param arg is the whole argument or option
* @param key determines why the processing function was called
- * @param argcout pointer to output argument count
- * @param argvout pointer to output argument vector
+ * @param outargs the current output argument list
* @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept
*/
-typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key,
- int *argcout, char **argvout[]);
+typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key,
+ struct fuse_args *outargs);
/**
* Option parsing function
*
- * If 'argv' is NULL, the values pointed by argcout and argvout will
- * be used as input
+ * If 'argv' is NULL, the contents of 'outargs' will be used as input
*
* A NULL 'opts' is equivalent to an 'opts' array containing a single
* end marker
@@ -155,22 +164,19 @@ typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key,
* A NULL 'proc' is equivalent to a processing function always
* returning '1'
*
- * If argvout is NULL, then any output arguments are discarded
- *
- * If argcout is NULL, then the output argument count is not stored
+ * If outargs is NULL, then any output arguments are discarded
*
* @param argc is the input argument count
* @param argv is the input argument vector
* @param data is the user data
* @param opts is the option description array
* @param proc is the processing function
- * @param argcout is pointer to output argument count
- * @param argvout is pointer to output argument vector
+ * @param outargs is the output argument list
* @return -1 on error, 0 on success
*/
int fuse_opt_parse(int argc, char *argv[], void *data,
const struct fuse_opt opts[], fuse_opt_proc_t proc,
- int *argcout, char **argvout[]);
+ struct fuse_args *outargs);
/**
* Add an option to a comma separated option list
@@ -184,19 +190,20 @@ int fuse_opt_add_opt(char **opts, const char *opt);
/**
* Add an argument to a NULL terminated argument vector
*
- * @param argcp is a pointer to argument count
- * @param argvp is a pointer to argument vector
+ * @param args is the structure containing the current argument list
* @param arg is the new argument to add
* @return -1 on allocation error, 0 on success
*/
-int fuse_opt_add_arg(int *argcp, char **argvp[], const char *arg);
+int fuse_opt_add_arg(struct fuse_args *args, const char *arg);
/**
- * Free argument vector
+ * Free the contents of argument list
+ *
+ * The structure itself is not freed
*
- * @param args is the argument vector
+ * @param args is the structure containing the argument list
*/
-void fuse_opt_free_args(char *args[]);
+void fuse_opt_free_args(struct fuse_args *args);
/**
diff --git a/lib/fuse.c b/lib/fuse.c
index 3b64bc4..35dc227 100644
--- a/lib/fuse.c
+++ b/lib/fuse.c
@@ -1808,12 +1808,11 @@ void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
}
static int fuse_lib_opt_proc(void *data, const char *arg, int key,
- int *argcp, char **argvp[])
+ struct fuse_args *outargs)
{
struct fuse_config *conf = data;
(void) key;
- (void) argcp;
- (void) argvp;
+ (void) outargs;
return fuse_opt_add_opt(&conf->llopts, arg);
}
@@ -1871,7 +1870,7 @@ struct fuse *fuse_new_common(int fd, const char *opts,
if (opts) {
const char *argv[] = { "", "-o", opts, NULL };
if (fuse_opt_parse(3, (char **) argv, &f->conf,
- fuse_lib_opts, fuse_lib_opt_proc, NULL, NULL) == -1)
+ fuse_lib_opts, fuse_lib_opt_proc, NULL) == -1)
goto out_free;
}
diff --git a/lib/fuse_lowlevel.c b/lib/fuse_lowlevel.c
index 3e45a59..6913d63 100644
--- a/lib/fuse_lowlevel.c
+++ b/lib/fuse_lowlevel.c
@@ -900,12 +900,11 @@ static struct fuse_opt fuse_ll_opts[] = {
};
static int fuse_ll_opt_proc(void *data, const char *arg, int key,
- int *argcp, char **argvp[])
+ struct fuse_args *outargs)
{
(void) data;
(void) key;
- (void) argcp;
- (void) argvp;
+ (void) outargs;
fprintf(stderr, "fuse: unknown option `%s'\n", arg);
return -1;
}
@@ -950,7 +949,7 @@ struct fuse_session *fuse_lowlevel_new(const char *opts,
if (opts) {
const char *argv[] = { "", "-o", opts, NULL };
if (fuse_opt_parse(3, (char **) argv, f, fuse_ll_opts,
- fuse_ll_opt_proc, NULL, NULL) == -1)
+ fuse_ll_opt_proc, NULL) == -1)
goto out_free;
}
diff --git a/lib/fuse_opt.c b/lib/fuse_opt.c
index a0ed4ac..d1b3259 100644
--- a/lib/fuse_opt.c
+++ b/lib/fuse_opt.c
@@ -20,19 +20,19 @@ struct fuse_opt_context {
int argctr;
int argc;
char **argv;
- int argcout;
- char **argvout;
+ struct fuse_args outargs;
char *opts;
int nonopt;
};
-void fuse_opt_free_args(char *args[])
+void fuse_opt_free_args(struct fuse_args *args)
{
- if (args) {
+ if (args && args->argv) {
int i;
- for (i = 0; args[i]; i++)
- free(args[i]);
- free(args);
+ for (i = 0; i < args->argc; i++)
+ free(args->argv[i]);
+ free(args->argv);
+ args->argv = NULL;
}
}
@@ -42,16 +42,16 @@ static int alloc_failed(void)
return -1;
}
-int fuse_opt_add_arg(int *argcp, char **argvp[], const char *arg)
+int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
{
- char **newargv = realloc(*argvp, (*argcp + 2) * sizeof(char *));
+ char **newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
char *newarg = newargv ? strdup(arg) : NULL;
if (!newargv || !newarg)
return alloc_failed();
- newargv[(*argcp)++] = newarg;
- newargv[*argcp] = NULL;
- *argvp = newargv;
+ args->argv = newargv;
+ args->argv[args->argc++] = newarg;
+ args->argv[args->argc] = NULL;
return 0;
}
@@ -67,7 +67,7 @@ static int next_arg(struct fuse_opt_context *ctx, const char *opt)
static int add_arg(struct fuse_opt_context *ctx, const char *arg)
{
- return fuse_opt_add_arg(&ctx->argcout, &ctx->argvout, arg);
+ return fuse_opt_add_arg(&ctx->outargs, arg);
}
int fuse_opt_add_opt(char **opts, const char *opt)
@@ -97,15 +97,15 @@ static int add_opt(struct fuse_opt_context *ctx, const char *opt)
static int insert_arg(struct fuse_opt_context *ctx, int pos, const char *arg)
{
- assert(pos <= ctx->argcout);
+ assert(pos <= ctx->outargs.argc);
if (add_arg(ctx, arg) == -1)
return -1;
- if (pos != ctx->argcout - 1) {
- char *newarg = ctx->argvout[ctx->argcout - 1];
- memmove(&ctx->argvout[pos + 1], &ctx->argvout[pos],
- sizeof(char *) * (ctx->argcout - pos - 1));
- ctx->argvout[pos] = newarg;
+ if (pos != ctx->outargs.argc - 1) {
+ char *newarg = ctx->outargs.argv[ctx->outargs.argc - 1];
+ memmove(&ctx->outargs.argv[pos + 1], &ctx->outargs.argv[pos],
+ sizeof(char *) * (ctx->outargs.argc - pos - 1));
+ ctx->outargs.argv[pos] = newarg;
}
return 0;
}
@@ -114,7 +114,7 @@ static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
int iso)
{
if (ctx->proc) {
- int res = ctx->proc(ctx->data, arg, key, &ctx->argcout, &ctx->argvout);
+ int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
if (res == -1 || !res)
return res;
}
@@ -296,7 +296,7 @@ static int process_one(struct fuse_opt_context *ctx, const char *arg)
} else if (arg[1] == '-' && !arg[2]) {
if (add_arg(ctx, arg) == -1)
return -1;
- ctx->nonopt = ctx->argcout;
+ ctx->nonopt = ctx->outargs.argc;
return 0;
} else
return process_gopt(ctx, arg, 0);
@@ -318,42 +318,33 @@ static int opt_parse(struct fuse_opt_context *ctx)
insert_arg(ctx, 2, ctx->opts) == -1)
return -1;
}
- if (ctx->nonopt && ctx->nonopt == ctx->argcout)
- ctx->argvout[--ctx->argcout] = NULL;
+ if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc)
+ ctx->outargs.argv[--ctx->outargs.argc] = NULL;
return 0;
}
int fuse_opt_parse(int argc, char *argv[], void *data,
const struct fuse_opt opts[], fuse_opt_proc_t proc,
- int *argcout, char **argvout[])
+ struct fuse_args *outargs)
{
int res;
struct fuse_opt_context ctx = {
- .argc = argv ? argc : *argcout,
- .argv = argv ? argv : *argvout,
+ .argc = argv ? argc : outargs->argc,
+ .argv = argv ? argv : outargs->argv,
.data = data,
.opt = opts,
.proc = proc,
- .argcout = 0,
- .argvout = NULL,
- .opts = NULL,
- .nonopt = 0,
};
res = opt_parse(&ctx);
if (!argv)
- fuse_opt_free_args(ctx.argv);
+ fuse_opt_free_args(outargs);
free(ctx.opts);
- if (res == -1)
- fuse_opt_free_args(ctx.argvout);
- else {
- if (argcout)
- *argcout = ctx.argcout;
- if (argvout)
- *argvout = ctx.argvout;
- else
- fuse_opt_free_args(ctx.argvout);
- }
+ if (res != -1 && outargs)
+ *outargs = ctx.outargs;
+ else
+ fuse_opt_free_args(&ctx.outargs);
+
return res;
}
diff --git a/lib/helper.c b/lib/helper.c
index d61aaec..1e46476 100644
--- a/lib/helper.c
+++ b/lib/helper.c
@@ -157,12 +157,11 @@ static const struct fuse_opt fuse_helper_opts[] = {
};
static int fuse_helper_opt_proc(void *data, const char *arg, int key,
- int *argcp, char **argvp[])
+ struct fuse_args *outargs)
{
struct helper_opts *hopts = data;
- (void) argcp;
- (void) argvp;
+ (void) outargs;
switch (key) {
case KEY_HELP:
@@ -207,7 +206,7 @@ static int fuse_parse_cmdline(int argc, const char *argv[],
hopts->progname = argv[0];
res = fuse_opt_parse(argc, (char **) argv, hopts, fuse_helper_opts,
- fuse_helper_opt_proc, NULL, NULL);
+ fuse_helper_opt_proc, NULL);
if (res == -1)
return -1;