From: Leo C Date: Sun, 8 Apr 2018 14:45:56 +0000 (+0200) Subject: Find subcommands without prefix X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/commitdiff_plain/1c62c84445d153698ed4b74752c2e800374ba0db?hp=228b1c5f79749940e11247bd97626e21df7033bd Find subcommands without prefix --- 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 b65db54..c3eb239 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", " 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", " args ...\n" "fat help\n" diff --git a/include/command.h b/include/command.h index e44512e..fb7f9b8 100644 --- a/include/command.h +++ b/include/command.h @@ -135,6 +135,7 @@ extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, */ #define CTBL_REPEAT 0x01 /* command is repeatable */ #define CTBL_SUBCMD 0x02 /* command has subcommands */ +#define CTBL_SUBCMDAUTO 0x04 /* execute subcommands whithout prefix*/ #ifdef CONFIG_AUTO_COMPLETE # define _CMD_COMPLETE(x) x,