summaryrefslogtreecommitdiff
path: root/avr/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'avr/command.c')
-rw-r--r--avr/command.c92
1 files changed, 58 insertions, 34 deletions
diff --git a/avr/command.c b/avr/command.c
index aa784cb..e88e2f3 100644
--- a/avr/command.c
+++ b/avr/command.c
@@ -85,47 +85,69 @@ cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t *cmdtp)
/*
* find command table entry for a command
*/
-cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table)
-{
- cmd_tbl_t *cmdtp;
- cmd_tbl_t *cmdtp_temp = table; /*Init value */
- int table_len = cmd_tbl_item_count(table);
- size_t len;
- uint_fast8_t n_found = 0;
-
- char *optenv = getenv_str(PSTR("cmd"));
- bool opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
-
- if (!cmd)
- return NULL;
+typedef struct {
+ size_t len;
+ uint_fast8_t level;
+ bool opt_debug;
+ } find_cmd_para_t;
- len = strlen(cmd);
+cmd_tbl_t *_find_cmd (const char *cmd, cmd_tbl_t *table, find_cmd_para_t *para)
+{
+ cmd_tbl_t *cmdtp_ret = NULL;
+ uint_fast8_t n_found = 0, sub_found = 0;
- for (cmdtp = table;
- cmdtp != table + table_len;
- cmdtp++) {
- if (strncmp_P(cmd, cmdtp->name, len) == 0 &&
- (opt_debug || cmdtp->name[0] != '!')) {
- if (len == strlen_P(cmdtp->name))
+ for (cmd_tbl_t *cmdtp = table; cmdtp->name != NULL; cmdtp++) {
+ if (strncmp_P(cmd, cmdtp->name, para->len) == 0 &&
+ (para->opt_debug || !(cmdtp->flags & CTBL_DBG))) {
+ if (para->len == strlen_P(cmdtp->name))
return cmdtp; /* full match */
- cmdtp_temp = cmdtp; /* abbreviated command ? */
+ cmdtp_ret = cmdtp; /* abbreviated command ? */
n_found++;
+ } else if (cmdtp->subcmd && cmdtp->flags & CTBL_SUBCMDAUTO) {
+ cmd_tbl_t *sub = _find_cmd(cmd, cmdtp->subcmd, para);
+ if (sub) {
+ cmdtp_ret = sub;
+ ++n_found;
+ }
}
}
- if (n_found == 1) /* exactly one match */
- return cmdtp_temp;
+ if (n_found == 1) { /* exactly one match */
+ if (sub_found)
+ para->level++;
+ return cmdtp_ret;
+ }
return NULL; /* not found or ambiguous command */
}
+
+cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table, uint_fast8_t *cmdlevel)
+{
+ find_cmd_para_t para;
+
+ if (!cmd)
+ return NULL;
+
+ char *optenv = getenv_str(PSTR("cmd"));
+ para.level = 0;
+ para.opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+ para.len = strlen(cmd);
+
+ cmd_tbl_t *cmdtp = _find_cmd(cmd, table, &para);
+
+ if (cmdlevel)
+ *cmdlevel = para.level;
+ return cmdtp;
+}
+
cmd_tbl_t *find_cmd_sub(const char *cmd, cmd_tbl_t *table)
{
cmd_tbl_t *entry = NULL;
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);
+ entry = find_cmd(cmd, tp->subcmd, NULL);
return entry;
}
@@ -135,10 +157,8 @@ cmd_tbl_t *find_cmd_sub(const char *cmd, cmd_tbl_t *table)
* for long help messages
*/
-command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char * const argv[])
{
- (void) flag;
-
cmd_tbl_t *tbl_start = get_cmd_tbl_base(cmdtp);
char *optenv = getenv_str(PSTR("cmd"));
@@ -163,7 +183,7 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * cons
/* print short help (usage) */
for (int i = 0; i < cmd_items; i++) {
- if (opt_debug || cmd_array[i]->name[0] != '!') {
+ if (opt_debug || !(cmd_array[i]->flags & CTBL_DBG)) {
const FLASH char *usage = cmd_array[i]->usage;
/* allow user abort */
@@ -186,7 +206,7 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * cons
* command help (long version)
*/
for (uint8_t i = 1; i < argc; ++i) {
- if ((cmdtp = find_cmd(argv[i], tbl_start)) != NULL &&
+ if ((cmdtp = find_cmd(argv[i], tbl_start, NULL)) != NULL &&
(opt_debug || cmdtp->name[0] != '!')) {
cmd_usage(cmdtp);
} else {
@@ -518,15 +538,16 @@ command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
{
command_ret_t rc = CMD_RET_SUCCESS;
cmd_tbl_t *cmdtp;
+ uint_fast8_t cmdlevel;
/* Look up command in command table */
- cmdtp = find_cmd(argv[0], cmd_tbl);
+ cmdtp = find_cmd(argv[0], cmd_tbl, &cmdlevel);
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);
+ cmd_tbl_t *cmdtpsub = find_cmd(argv[1], cmdtp->subcmd, &cmdlevel);
if (cmdtpsub == NULL) {
printf_P(PSTR("Unknown '%s' subcommand '%s' - try '%s help'\n"), argv[0], argv[1], argv[0]);
return CMD_RET_FAILURE;
@@ -535,10 +556,13 @@ command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
--argc;
++argv;
}
- } else {
+ }
+#if 0
+ else {
/* Search subcommands */
- cmdtp = find_cmd_sub(argv[0], cmd_tbl);
+ cmdtp = find_cmd_sub(argv[0], cmd_tbl, &cmdlevel);
}
+#endif
if (cmdtp == NULL) {
printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]);
@@ -567,7 +591,7 @@ command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
/* If OK so far, then do the command */
if (!rc) {
rc = cmd_call(cmdtp, flag, argc, argv);
- *repeatable &= (cmdtp->flags & CTBL_REPEAT) != 0;
+ *repeatable &= (cmdtp->flags & CTBL_RPT) != 0;
}
if (rc == CMD_RET_USAGE)
rc = cmd_usage(cmdtp);