X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/7eecbdacec66226ebc3959411883aeaebcf9791d..b9aef06edc26e3d7560a7ab2d83fe033349ef874:/avr/command.c 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;