]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
kmod_config: parse module options
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 7 Dec 2011 05:18:57 +0000 (03:18 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 7 Dec 2011 12:59:44 +0000 (10:59 -0200)
libkmod/libkmod-config.c
libkmod/libkmod-private.h

index 5b85fb2f84c7917c478edd43fc59d338e63e2a60..93e7049d66e648b512450500529ab5a5edb1a455 100644 (file)
@@ -43,6 +43,11 @@ struct kmod_alias {
        char modname[];
 };
 
+struct kmod_options {
+       char *options;
+       char modname[];
+};
+
 const char *kmod_alias_get_name(const struct kmod_list *l) {
        const struct kmod_alias *alias = l->data;
        return alias->name;
@@ -53,6 +58,49 @@ const char *kmod_alias_get_modname(const struct kmod_list *l) {
        return alias->modname;
 }
 
+static int kmod_config_add_options(struct kmod_config *config,
+                               const char *modname, const char *options)
+{
+       struct kmod_options *opt;
+       struct kmod_list *list;
+       size_t modnamelen = strlen(modname) + 1;
+       size_t optionslen = strlen(options) + 1;
+
+       DBG(config->ctx, "modname'%s' options='%s'\n", modname, options);
+
+       opt = malloc(sizeof(*opt) + modnamelen + optionslen);
+       if (opt == NULL)
+               goto oom_error_init;
+
+       opt->options = sizeof(*opt) + modnamelen + (char *)opt;
+
+       memcpy(opt->modname, modname, modnamelen);
+       memcpy(opt->options, options, optionslen);
+       strchr_replace(opt->options, '\t', ' ');
+
+       list = kmod_list_append(config->options, opt);
+       if (list == NULL)
+               goto oom_error;
+
+       config->options = list;
+       return 0;
+
+oom_error:
+       free(opt);
+oom_error_init:
+       ERR(config->ctx, "out-of-memory\n");
+       return -ENOMEM;
+}
+
+static void kmod_config_free_options(struct kmod_config *config, struct kmod_list *l)
+{
+       struct kmod_options *opt = l->data;
+
+       free(opt);
+
+       config->options = kmod_list_remove(l);
+}
+
 static int kmod_config_add_alias(struct kmod_config *config,
                                const char *name, const char *modname)
 {
@@ -173,7 +221,16 @@ static int kmod_config_parse(struct kmod_config *config, int fd,
 
                        kmod_config_add_blacklist(config,
                                                underscores(ctx, modname));
-               } else if (streq(cmd, "include") || streq(cmd, "options")
+               } else if (streq(cmd, "options")) {
+                       char *modname = strtok_r(NULL, "\t ", &saveptr);
+
+                       if (modname == NULL)
+                               goto syntax_error;
+
+                       kmod_config_add_options(config,
+                                               underscores(ctx, modname),
+                                               strtok_r(NULL, "\0", &saveptr));
+               } else if (streq(cmd, "include")
                                || streq(cmd, "install")
                                || streq(cmd, "remove")
                                || streq(cmd, "softdep")
@@ -203,6 +260,9 @@ void kmod_config_free(struct kmod_config *config)
        while (config->blacklists)
                kmod_config_free_blacklist(config, config->blacklists);
 
+       while (config->options)
+               kmod_config_free_options(config, config->options);
+
        free(config);
 }
 
index df7e82a82ebaec34dec6952296ecc7d916469f44..e3925ed023e430648571e8a239f41dbc6240fb38 100644 (file)
@@ -84,6 +84,7 @@ struct kmod_config {
        struct kmod_ctx *ctx;
        struct kmod_list *aliases;
        struct kmod_list *blacklists;
+       struct kmod_list *options;
 };
 int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **config) __attribute__((nonnull(1)));
 void kmod_config_free(struct kmod_config *config) __attribute__((nonnull(1)));