From 226dbdf388f13706f57e766a80d19a99ca180051 Mon Sep 17 00:00:00 2001 From: Pawel Szewczyk Date: Wed, 20 Aug 2014 15:48:41 +0200 Subject: [PATCH] gt: Add setting command parsing Change-Id: Ib1e5a484934b0e08977305d7824aa46399d3da01 Signed-off-by: Pawel Szewczyk --- source/settings/src/settings.c | 240 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 235 insertions(+), 5 deletions(-) diff --git a/source/settings/src/settings.c b/source/settings/src/settings.c index 9f7d200..fffe4c6 100644 --- a/source/settings/src/settings.c +++ b/source/settings/src/settings.c @@ -15,8 +15,32 @@ */ #include +#include +#include #include "settings.h" +#include "common.h" +#include "parser.h" + +static int gt_check_settings_var(const char *name) +{ + static const char *vars[] = { + "default-udc", + "config-fs-path", + "lookup-path", + "default-template-path", + "default-gadget", + NULL + }; + int i = 0; + + for (i = 0; vars[i]; i++) { + if (strcmp(name, vars[i]) == 0) + return 0; + } + + return -1; +} int gt_settings_help(void *data) { @@ -24,14 +48,220 @@ int gt_settings_help(void *data) return -1; } +static int gt_settings_get_func(void *data) +{ + const char **dt; + + dt = (const char **)data; + + printf("Settings get called successfully. Not implemented yet.\n"); + while (*dt) { + printf("%s, ", *dt); + dt++; + } + + putchar('\n'); + return 0; +} + +static int gt_settings_get_help(void *data) +{ + printf("Settings get help function\n"); + return -1; +} + +void gt_settings_parse_get(const Command *cmd, int argc, char **argv, + ExecutableCommand *exec, void *data) +{ + const char **dt = NULL; + int i; + + dt = calloc(argc + 1, sizeof(*dt)); + if (dt == NULL) + goto out; + + for (i = 0; i < argc; i++){ + if (gt_check_settings_var(argv[i]) < 0) { + printf("Unrecognized variable name\n"); + goto out; + } + dt[i] = argv[i]; + } + + executable_command_set(exec, gt_settings_get_func, (void *)dt, free); + return; +out: + free(dt); + executable_command_set(exec, cmd->printHelp, data, NULL); +} + +static int gt_settings_set_func(void *data) +{ + struct gt_setting *ptr; + + ptr = (struct gt_setting *)data; + printf("Settings set called successfully. Not implemented yet.\n"); + while (ptr->variable) { + printf("%s = %s, ", ptr->variable, ptr->value); + ptr++; + } + putchar('\n'); + return 0; +} + +static int gt_settings_set_help(void *data) +{ + printf("Settings set help.\n"); + return -1; +} + +void gt_settings_parse_set(const Command *cmd, int argc, char **argv, + ExecutableCommand *exec, void *data) +{ + struct gt_setting *dt = NULL; + struct gt_setting *ptr = NULL; + int tmp; + + if (argc == 0) + goto out; + + tmp = gt_parse_setting_list(&dt, argc, argv); + if (tmp < 0) + goto out; + + ptr = dt; + while (ptr->variable) { + if (gt_check_settings_var(ptr->variable) < 0) { + printf("Unrecognized variable name: %s\n", + ptr->variable); + goto out; + } + ptr++; + + executable_command_set(exec, gt_settings_set_func, + (void *)dt, gt_setting_list_cleanup); + } + return; +out: + gt_setting_list_cleanup((void *)dt); + executable_command_set(exec, cmd->printHelp, data, NULL); +} + +static int gt_settings_append_func(void *data) +{ + struct gt_setting *dt; + + dt = (struct gt_setting *)data; + printf("Settings append called successfully. Not implemented,\n"); + printf("var = %s, val = %s\n", dt->variable, dt->value); + return 0; +} + +static int gt_settings_append_help(void *data) +{ + printf("Settings append help.\n"); + return -1; +} + +void gt_settings_parse_append(const Command *cmd, int argc, char **argv, + ExecutableCommand *exec, void *data) +{ + struct gt_setting *dt; + + dt = zalloc(sizeof(*dt)); + if (dt == NULL) + goto out; + + switch (argc) { + case 0: + goto out; + case 1: + printf("Expected value after '%s'\n", argv[0]); + goto out; + case 2: + dt->variable = argv[0]; + if (gt_check_settings_var(dt->variable) < 0) { + printf("Unrecognized variable name\n"); + goto out; + } + dt->value = argv[1]; + executable_command_set(exec, gt_settings_append_func, + (void *)dt, free); + break; + default: + printf("Too many arguments\n"); + goto out; + } + return; +out: + free(dt); + executable_command_set(exec, cmd->printHelp, data, NULL); +} + +static int gt_settings_detach_func(void *data) +{ + struct gt_setting *dt; + + dt = (struct gt_setting *)data; + printf("Settings detach called successfully. Not implemented.\n"); + printf("var = %s, val = %s\n", dt->variable, dt->value); + return 0; +} + +static int gt_settings_detach_help(void *data) +{ + printf("Settings detach help.\n"); + return -1; +} + +void gt_settings_parse_detach(const Command *cmd, int argc, char **argv, + ExecutableCommand *exec, void *data) +{ + struct gt_setting *dt; + + dt = zalloc(sizeof(*dt)); + if (dt == NULL) + goto out; + + switch (argc) { + case 0: + goto out; + case 1: + printf("Expected value after '%s'\n", argv[0]); + goto out; + case 2: + dt->variable = argv[0]; + if (gt_check_settings_var(dt->variable) < 0) { + printf("Unrecognized variable name\n"); + goto out; + } + dt->value = argv[1]; + executable_command_set(exec, gt_settings_detach_func, + (void *)dt, free); + break; + default: + printf("Too many arguments\n"); + goto out; + } + return; +out: + free(dt); + executable_command_set(exec, cmd->printHelp, data, NULL); +} + const Command *gt_settings_get_children(const Command *cmd) { static Command commands[] = { - // {"get", NEXT, settings_parse_get, get_gt_root_children, global_help_func}, - // {"set", NEXT, settings_parse_set, get_gt_root_children, global_help_func}, - // {"append", NEXT, settings_parse_append, get_gt_root_children, global_help_func}, - // {"detach", NEXT, settings_parse_detach, get_gt_root_children, global_help_func}, - {NULL, AGAIN, NULL, NULL, NULL} + {"get", NEXT, gt_settings_parse_get, NULL, + gt_settings_get_help}, + {"set", NEXT, gt_settings_parse_set, NULL, + gt_settings_set_help}, + {"append", NEXT, gt_settings_parse_append, NULL, + gt_settings_append_help}, + {"detach", NEXT, gt_settings_parse_detach, NULL, + gt_settings_detach_help}, + CMD_LIST_END }; + return commands; } -- 2.7.4