From d6f5943fdf316933b72cb05cf79fab4e71139cf1 Mon Sep 17 00:00:00 2001 From: Pawel Szewczyk Date: Tue, 14 Jul 2015 12:16:18 +0200 Subject: [PATCH] function: Add explicit show command Using default behavior of 'gt func' command as only way of showing functions is solution far from ideal. E.g. in following example second command is ambiguous: $ gt create rm $ gt func rm Even if gadget names like 'rm' will not be commonly used, we cannot forbid them. To prevent situations like that, this commit introduce explicit 'func show' command for showing functions. Previous default behavior for 'gt func' command is preserved. Change-Id: Ifa5d5fbb3e78174cfb69231da7272ad8fa72c568 Signed-off-by: Pawel Szewczyk --- source/function/include/function.h | 8 ++--- source/function/src/function.c | 48 +++++++++++++++++++------- source/function/src/function_gadgetd.c | 2 +- source/function/src/function_libusbg.c | 2 +- source/function/src/function_not_implemented.c | 18 +++++----- 5 files changed, 51 insertions(+), 27 deletions(-) diff --git a/source/function/include/function.h b/source/function/include/function.h index aced5fc..5d6abef 100644 --- a/source/function/include/function.h +++ b/source/function/include/function.h @@ -48,7 +48,7 @@ struct gt_function_backend { /** * Show functions */ - int (*func)(void *); + int (*show)(void *); /** * Load function from file */ @@ -110,10 +110,10 @@ struct gt_func_set_data { int opts; }; -struct gt_func_func_data { +struct gt_func_show_data { const char *gadget; - const char *type; - const char *name; + int type; + const char *instance; int opts; }; diff --git a/source/function/src/function.c b/source/function/src/function.c index 84964fd..a754135 100644 --- a/source/function/src/function.c +++ b/source/function/src/function.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "function.h" #include "common.h" @@ -291,14 +292,13 @@ out: executable_command_set(exec, gt_func_get_help, data, NULL); } - -static void gt_func_func_destructor(void *data) +static void gt_func_show_destructor(void *data) { - struct gt_func_func_data *dt; + struct gt_func_show_data *dt; if (data == NULL) return; - dt = (struct gt_func_func_data *)data; + dt = (struct gt_func_show_data *)data; free(dt); } @@ -309,6 +309,7 @@ static int gt_func_func_help(void *data) program_name); printf("Command:\n" + " show\n" " create\n" " rm\n" " get\n" @@ -321,10 +322,23 @@ static int gt_func_func_help(void *data) return 0; } -static void gt_parse_func_func(const Command *cmd, int argc, +static int gt_func_show_help(void *data) +{ + printf("usage: %s func show [ ]\n" + "Show functions. If no function was specified, show all functions\n\n", + program_name); + + printf("Options:\n" + " -v, --verbose\tShow also attributes\n" + " -h, --help\tPrint this help\n"); + + return 0; +} + +static void gt_parse_func_show(const Command *cmd, int argc, char **argv, ExecutableCommand *exec, void * data) { - struct gt_func_func_data *dt = NULL; + struct gt_func_show_data *dt = NULL; int ind; int avaible_opts = GT_VERBOSE | GT_HELP; @@ -339,21 +353,28 @@ static void gt_parse_func_func(const Command *cmd, int argc, if (ind < 0 || dt->opts & GT_HELP) goto out; + dt->gadget = argv[ind++]; if (argc > ind) { if (argc - ind != 2) goto out; - dt->type = argv[ind++]; - dt->name = argv[ind++]; + dt->type = usbg_lookup_function_type(argv[ind]); + if (dt->type < 0) { + fprintf(stderr, "Unknown function type: %s", argv[ind]); + goto out; + } + + ind++; + dt->instance = argv[ind++]; } - executable_command_set(exec, GET_EXECUTABLE(func), (void *)dt, - gt_func_func_destructor); + executable_command_set(exec, GET_EXECUTABLE(show), (void *)dt, + gt_func_show_destructor); return; out: - gt_func_func_destructor(dt); - executable_command_set(exec, gt_func_func_help, data, NULL); + gt_func_show_destructor(dt); + executable_command_set(exec, cmd->printHelp, data, NULL); } static int gt_func_load_help(void *data) @@ -738,7 +759,8 @@ const Command *gt_func_get_children(const Command *cmd) {"save", NEXT, gt_parse_func_save, NULL, gt_func_save_help}, {"template", NEXT, command_parse, gt_func_template_get_children, gt_func_template_help}, - {NULL, AGAIN, gt_parse_func_func, NULL, gt_func_func_help}, + {"show", NEXT, gt_parse_func_show, NULL, gt_func_show_help}, + {NULL, AGAIN, gt_parse_func_show, NULL, gt_func_func_help}, {NULL, AGAIN, NULL, NULL, NULL} }; return commands; diff --git a/source/function/src/function_gadgetd.c b/source/function/src/function_gadgetd.c index d46c1f8..765afa4 100644 --- a/source/function/src/function_gadgetd.c +++ b/source/function/src/function_gadgetd.c @@ -125,7 +125,7 @@ struct gt_function_backend gt_function_backend_gadgetd = { .list_types = list_types_func, .get = NULL, .set = NULL, - .func = NULL, + .show = NULL, .load = NULL, .save = NULL, .template_default = NULL, diff --git a/source/function/src/function_libusbg.c b/source/function/src/function_libusbg.c index 71fd9a2..f408771 100644 --- a/source/function/src/function_libusbg.c +++ b/source/function/src/function_libusbg.c @@ -84,7 +84,7 @@ struct gt_function_backend gt_function_backend_libusbg = { .list_types = list_types_func, .get = NULL, .set = NULL, - .func = NULL, + .show = NULL, .load = NULL, .save = NULL, .template_default = NULL, diff --git a/source/function/src/function_not_implemented.c b/source/function/src/function_not_implemented.c index 7ba93f4..2db1bcd 100644 --- a/source/function/src/function_not_implemented.c +++ b/source/function/src/function_not_implemented.c @@ -90,17 +90,19 @@ static int set_func(void *data) return 0; } -static int func_func(void *data) +static int show_func(void *data) { - struct gt_func_func_data *dt; + struct gt_func_show_data *dt; - dt = (struct gt_func_func_data *)data; + dt = (struct gt_func_show_data *)data; printf("Func func called successfully. Not implemented.\n"); printf("gadget=%s", dt->gadget); - if (dt->type) - printf(", type=%s", dt->type); - if (dt->name) - printf(", name=%s", dt->name); + + if (dt->instance) { + printf(", type=%s", usbg_get_function_type_str(dt->type)); + printf(", instance=%s", dt->instance); + } + printf(", verbose=%d\n", !!(dt->opts & GT_VERBOSE)); return 0; @@ -217,7 +219,7 @@ struct gt_function_backend gt_function_backend_not_implemented = { .list_types = list_types_func, .get = get_func, .set = set_func, - .func = func_func, + .show = show_func, .load = load_func, .save = save_func, .template_default = template_func, -- 2.7.4