fix
authorMiklos Szeredi <miklos@szeredi.hu>
Fri, 16 Dec 2005 11:12:16 +0000 (11:12 +0000)
committerMiklos Szeredi <miklos@szeredi.hu>
Fri, 16 Dec 2005 11:12:16 +0000 (11:12 +0000)
ChangeLog
include/fuse_opt.h
lib/fuse.c
lib/fuse_lowlevel.c
lib/fuse_opt.c
lib/helper.c

index 2da637b2a76478ebd4deae566da6bc1f5ee9005c..e613354b45580d336918afcd37fbdb699dd48836 100644 (file)
--- 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
index 82818827d303db84e76a48266d8da07a732d83cb..f3a06ddbdf5c26c3e350c9ec0879368c906c3a3e 100644 (file)
@@ -93,6 +93,17 @@ struct fuse_opt {
     int value;
 };
 
+/**
+ * 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);
 
 
 /**
index 3b64bc4e9c6517faec4afe53da327dba4dda18f5..35dc2275b2c3228514e735e8cd717db3e115529b 100644 (file)
@@ -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;
     }
 
index 3e45a59dea8428d7c546d7cf61fa64285d8020e1..6913d638fbac41b458add1668f9be00226cfeb72 100644 (file)
@@ -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;
     }
 
index a0ed4acfae2f8c53b803fbd732c0d3614d49d664..d1b3259881798750a029ec919af783adb97d94f0 100644 (file)
@@ -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;
 }
index d61aaec85dc88ad18370a1238cf9d2d50344768a..1e464764c1de4235092343f92b1d03c0c50be3b7 100644 (file)
@@ -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;