From: Lucas De Marchi Date: Wed, 7 Dec 2011 13:31:28 +0000 (-0200) Subject: kmod_config: parse install and remove commands X-Git-Tag: v1~77 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a5cce6d6ef6e3b521f903f52066133ddf2ccd70b;p=thirdparty%2Fkmod.git kmod_config: parse install and remove commands --- diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c index 93e7049d..429f58af 100644 --- a/libkmod/libkmod-config.c +++ b/libkmod/libkmod-config.c @@ -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); } diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index e3925ed0..e0cdd558 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -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)));