diff options
author | Leo C | 2018-04-08 16:45:56 +0200 |
---|---|---|
committer | Leo C | 2018-07-21 13:27:51 +0200 |
commit | 9d6c43faec80158445b1131640356cc0c90b45e3 (patch) | |
tree | b673009cddefd0dab1cc94034d693e9110450fa9 /avr | |
parent | 7eecbdacec66226ebc3959411883aeaebcf9791d (diff) | |
download | z180-stamp-9d6c43faec80158445b1131640356cc0c90b45e3.zip |
Find subcommands without prefix
Diffstat (limited to 'avr')
-rw-r--r-- | avr/command.c | 61 | ||||
-rw-r--r-- | avr/command_tbl.c | 4 |
2 files changed, 42 insertions, 23 deletions
diff --git a/avr/command.c b/avr/command.c index b9c5e56..aa784cb 100644 --- a/avr/command.c +++ b/avr/command.c @@ -59,6 +59,8 @@ int cmpstring_PP(const void *p1, const void *p2) (*(const FLASH cmd_tbl_t **) p2)->name); } +/****************************************************************************/ + int cmd_tbl_item_count(cmd_tbl_t *p) { int count = 0; @@ -69,7 +71,18 @@ int cmd_tbl_item_count(cmd_tbl_t *p) return count; } -/*************************************************************************** + +cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t *cmdtp) +{ + cmd_tbl_t *p = cmdtp; + + while (p->name != NULL) + ++p; + + return p->subcmd; +} + +/* * find command table entry for a command */ cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table) @@ -106,14 +119,15 @@ cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table) return NULL; /* not found or ambiguous command */ } -cmd_tbl_t *get_cmd_tbl_start(cmd_tbl_t *cmdtp) +cmd_tbl_t *find_cmd_sub(const char *cmd, cmd_tbl_t *table) { - cmd_tbl_t *p = cmdtp; + cmd_tbl_t *entry = NULL; - while (p->name != NULL) - ++p; + for (cmd_tbl_t *tp = get_cmd_tbl_base(table); tp->name && entry == NULL; tp++) + if (tp->subcmd && tp->flags & CTBL_SUBCMDAUTO) + entry = find_cmd(cmd, tp->subcmd); - return p->subcmd; + return entry; } /* @@ -125,7 +139,7 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * cons { (void) flag; - cmd_tbl_t *tbl_start = get_cmd_tbl_start(cmdtp); + cmd_tbl_t *tbl_start = get_cmd_tbl_base(cmdtp); char *optenv = getenv_str(PSTR("cmd")); bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL; @@ -507,25 +521,30 @@ command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[], /* Look up command in command table */ cmdtp = find_cmd(argv[0], cmd_tbl); + if (cmdtp != NULL) { + /* Check if this command has subcommands */ + if (cmdtp->subcmd && argc > 1) { + + /* Look up subcommand in subcommand table */ + cmd_tbl_t *cmdtpsub = find_cmd(argv[1], cmdtp->subcmd); + if (cmdtpsub == NULL) { + printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv[0], argv[1], argv[0]); + return CMD_RET_FAILURE; + } + cmdtp = cmdtpsub; + --argc; + ++argv; + } + } else { + /* Search subcommands */ + cmdtp = find_cmd_sub(argv[0], cmd_tbl); + } + if (cmdtp == NULL) { printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]); return CMD_RET_FAILURE; } - /* Check for subcommand */ - if (cmdtp->subcmd && argc > 1) { - - /* Look up subcommand in subcommand table */ - cmd_tbl_t *cmdtpsub = find_cmd(argv[1], cmdtp->subcmd); - if (cmdtpsub == NULL) { - printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv[0], argv[1], argv[0]); - return CMD_RET_FAILURE; - } - cmdtp = cmdtpsub; - --argc; - ++argv; - } - /* found - check max args */ if (argc > cmdtp->maxargs) rc = CMD_RET_USAGE; diff --git a/avr/command_tbl.c b/avr/command_tbl.c index ce06081..07c9684 100644 --- a/avr/command_tbl.c +++ b/avr/command_tbl.c @@ -319,7 +319,7 @@ CMD_TBL_ITEM( #endif /* CONFIG_MX_CYCLIC */ CMD_TBL_ITEM_TOP( - sd, CONFIG_SYS_MAXARGS, 1, do_sd, + sd, CONFIG_SYS_MAXARGS, CTBL_SUBCMDAUTO, do_sd, "SD/MMC card handling commands", "<subcommand> args ...\n" "sd help\n" @@ -327,7 +327,7 @@ CMD_TBL_ITEM_TOP( cmd_tbl_sd ), CMD_TBL_ITEM_TOP( - fat, CONFIG_SYS_MAXARGS, 1, do_fat, + fat, CONFIG_SYS_MAXARGS, CTBL_SUBCMD|CTBL_SUBCMDAUTO, do_fat, "fat filesystem commands", "<subcommand> args ...\n" "fat help\n" |