diff options
| author | Jianting (Meco) Man <[email protected]> | 2021-10-11 15:58:20 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-10-11 15:58:20 -0500 |
| commit | 1426e57db023b2fd40887c278d4c089678987d4e (patch) | |
| tree | 58c7e21f1f4dc298c9ad231fa2c86814e1ed4b76 /components/utilities/var_export/var_export_cmd.c | |
| parent | 2ae8a05963b83c5a0deba6bd9b95f81b93d33092 (diff) | |
| parent | 277bb736d7d4a7e6c32df107e550c2918bf95726 (diff) | |
Merge branch 'RT-Thread:master' into hal
Diffstat (limited to 'components/utilities/var_export/var_export_cmd.c')
| -rw-r--r-- | components/utilities/var_export/var_export_cmd.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/components/utilities/var_export/var_export_cmd.c b/components/utilities/var_export/var_export_cmd.c new file mode 100644 index 0000000000..fba3cc92bb --- /dev/null +++ b/components/utilities/var_export/var_export_cmd.c @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-06-05 WillianChan first version + */ + +#include <var_export.h> + +static int ve_cmd_help(int argc, char **argv); +static int ve_find_module(int argc, char **argv); +static int ve_find_value(int argc, char **argv); + + +struct ve_cmd_des +{ + const char *cmd; + int (*fun)(int argc, char **argv); +}; + +/* dcm cmd table */ +static const struct ve_cmd_des cmd_tab[] = +{ + {"module", ve_find_module}, + {"value", ve_find_value}, +}; + +static int ve_cmd_help(int argc, char **argv) +{ + rt_kprintf("Usage:\n"); + rt_kprintf("ve_find module <module> - Find by module name\n"); + rt_kprintf("ve_find value <module> <identifier> - Find accurately\n"); + + return RT_EOK; +} + +rt_inline void ve_object_split(int len) +{ + while (len--) rt_kprintf("-"); +} + +static int ve_find_module(int argc, char **argv) +{ + ve_iterator_t iter; + const ve_exporter_t *exporter; + ve_module_t module; + int maxlen = (RT_NAME_MAX * 2); + const char *item_title = "ve_module"; + + rt_kprintf("%-*.s identifier value\n", maxlen, item_title); ve_object_split(maxlen); + rt_kprintf(" ---------------- -----\n"); + + if (!ve_module_init(&module, argv[2])) + { + ve_iter_init(&module, &iter); + } + else + { + return -RT_ERROR; + } + + while (1) + { + exporter = ve_iter_next(&iter); + if (exporter == RT_NULL) + { + return -RT_ERROR; + } + else + { + rt_kprintf("%-*.s %-*.s %d\n", + maxlen, exporter->module, + maxlen, exporter->identifier, + exporter->value); + } + } +} + +static int ve_find_value(int argc, char **argv) +{ + ve_iterator_t iter; + const ve_exporter_t *exporter; + ve_module_t module; + int maxlen = (RT_NAME_MAX * 2); + const char *item_title = "ve_module"; + + rt_kprintf("%-*.s identifier value\n", maxlen, item_title); ve_object_split(maxlen); + rt_kprintf(" ---------------- -----\n"); + + if (!ve_module_init(&module, argv[2])) + { + ve_iter_init(&module, &iter); + } + else + { + return -RT_ERROR; + } + + while (1) + { + exporter = ve_iter_next(&iter); + if (exporter == RT_NULL) + { + return -RT_ERROR; + } + else + { + if (!rt_strcmp(exporter->identifier, argv[3])) + { + rt_kprintf("%-*.s %-*.s %d\n", + maxlen, exporter->module, + maxlen, exporter->identifier, + exporter->value); + + return RT_EOK; + } + } + } +} + +static int ve_find(int argc, char **argv) +{ + int i, resule = RT_EOK; + const struct ve_cmd_des *run_cmd = RT_NULL; + + if (argc == 1) + { + ve_cmd_help(argc, argv); + return RT_EOK; + } + + /* find command function */ + for (i = 0; i < sizeof(cmd_tab) / sizeof(cmd_tab[0]); i++) + { + if (rt_strcmp(cmd_tab[i].cmd, argv[1]) == 0) + { + run_cmd = &cmd_tab[i]; + break; + } + } + + /* not find command function, print help information */ + if (run_cmd == RT_NULL) + { + rt_kprintf("There is no command option named %s.\n", argv[1]); + ve_cmd_help(argc, argv); + return RT_EOK; + } + + /* run command function */ + if (run_cmd->fun != RT_NULL) + { + resule = run_cmd->fun(argc, argv); + } + + if (resule) + { + ve_cmd_help(argc, argv); + } + + return RT_EOK; +} +MSH_CMD_EXPORT(ve_find, find the specified export variable); |
