]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
kmod_config: parse install and remove commands
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 7 Dec 2011 13:31:28 +0000 (11:31 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 7 Dec 2011 13:31:28 +0000 (11:31 -0200)
libkmod/libkmod-config.c
libkmod/libkmod-private.h

index 93e7049d66e648b512450500529ab5a5edb1a455..429f58af611584c3eec6050f80269f01b64ad17b 100644 (file)
@@ -48,6 +48,11 @@ struct kmod_options {
        char modname[];
 };
 
+struct kmod_command {
+       char *command;
+       char modname[];
+};
+
 const char *kmod_alias_get_name(const struct kmod_list *l) {
        const struct kmod_alias *alias = l->data;
        return alias->name;
@@ -58,6 +63,52 @@ const char *kmod_alias_get_modname(const struct kmod_list *l) {
        return alias->modname;
 }
 
+static int kmod_config_add_command(struct kmod_config *config,
+                                               const char *modname,
+                                               const char *command,
+                                               const char *command_name,
+                                               struct kmod_list **list)
+{
+       struct kmod_command *cmd;
+       struct kmod_list *l;
+       size_t modnamelen = strlen(modname) + 1;
+       size_t commandlen = strlen(command) + 1;
+
+       DBG(config->ctx, "modname'%s' cmd='%s %s'\n", modname, command_name,
+                                                               command);
+
+       cmd = malloc(sizeof(*cmd) + modnamelen + commandlen);
+       if (cmd == NULL)
+               goto oom_error_init;
+
+       cmd->command = sizeof(*cmd) + modnamelen + (char *)cmd;
+       memcpy(cmd->modname, modname, modnamelen);
+       memcpy(cmd->command, command, commandlen);
+
+       l = kmod_list_append(*list, cmd);
+       if (l == NULL)
+               goto oom_error;
+
+       *list = l;
+       return 0;
+
+oom_error:
+       free(cmd);
+oom_error_init:
+       ERR(config->ctx, "out-of-memory\n");
+       return -ENOMEM;
+}
+
+static void kmod_config_free_command(struct kmod_config *config,
+                                       struct kmod_list *l,
+                                       struct kmod_list **list)
+{
+       struct kmod_command *cmd = l->data;
+
+       free(cmd);
+       *list = kmod_list_remove(l);
+}
+
 static int kmod_config_add_options(struct kmod_config *config,
                                const char *modname, const char *options)
 {
@@ -230,9 +281,27 @@ static int kmod_config_parse(struct kmod_config *config, int fd,
                        kmod_config_add_options(config,
                                                underscores(ctx, modname),
                                                strtok_r(NULL, "\0", &saveptr));
+               } else if streq(cmd, "install") {
+                       char *modname = strtok_r(NULL, "\t ", &saveptr);
+
+                       if (modname == NULL)
+                               goto syntax_error;
+
+                       kmod_config_add_command(config,
+                                       underscores(ctx, modname),
+                                       strtok_r(NULL, "\0", &saveptr),
+                                       cmd, &config->install_commands);
+               } else if streq(cmd, "remove") {
+                       char *modname = strtok_r(NULL, "\t ", &saveptr);
+
+                       if (modname == NULL)
+                               goto syntax_error;
+
+                       kmod_config_add_command(config,
+                                       underscores(ctx, modname),
+                                       strtok_r(NULL, "\0", &saveptr),
+                                       cmd, &config->remove_commands);
                } else if (streq(cmd, "include")
-                               || streq(cmd, "install")
-                               || streq(cmd, "remove")
                                || streq(cmd, "softdep")
                                || streq(cmd, "config")) {
                        INFO(ctx, "%s: command %s not implemented yet\n",
@@ -263,6 +332,16 @@ void kmod_config_free(struct kmod_config *config)
        while (config->options)
                kmod_config_free_options(config, config->options);
 
+       while (config->install_commands) {
+               kmod_config_free_command(config, config->install_commands,
+                                               &config->install_commands);
+       }
+
+       while (config->remove_commands) {
+               kmod_config_free_command(config, config->remove_commands,
+                                               &config->remove_commands);
+       }
+
        free(config);
 }
 
index e3925ed023e430648571e8a239f41dbc6240fb38..e0cdd5589fb8174197cf7cd7a9c8545a7287748f 100644 (file)
@@ -85,6 +85,8 @@ struct kmod_config {
        struct kmod_list *aliases;
        struct kmod_list *blacklists;
        struct kmod_list *options;
+       struct kmod_list *remove_commands;
+       struct kmod_list *install_commands;
 };
 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)));