]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/command.c
cli.c: The real bugfix for Comment only lines.
[z180-stamp.git] / avr / command.c
index c9137eabc83e3f4cf23636f88124d49087afc928..085dfdc8f2e7a5ce2dd693a1f7c868b5a1b4c57f 100644 (file)
 #include "env.h"
 #include "debug.h"
 
+#define DEBUG_CMD      1       /* set to 1 to debug */
+
+#define debug_cmd(fmt, args...)                                                              \
+       debug_cond(DEBUG_CMD, fmt, ##args)
+
 
 jmp_buf cmd_jbuf;
 
@@ -64,7 +69,7 @@ int cmd_tbl_item_count(cmd_tbl_t *p)
        int count = 0;
 
        while (p->name != NULL) {
-               if (p->subcmd && p->flags & CTBL_SUBCMDAUTO) {
+               if (p->subcmd) {
                        cmd_tbl_t *sub = p->subcmd;
                        while (sub->name != NULL) {
                                if (sub->flags & CTBL_SUBCMDAUTO)
@@ -72,7 +77,9 @@ int cmd_tbl_item_count(cmd_tbl_t *p)
                                sub++;
                        }
                }
-               p++; count++;
+               if ((p->flags & CTBL_SUBCMDAUTO) == 0)
+                       count++;
+               p++;
        }
        return count;
 }
@@ -91,71 +98,46 @@ cmd_tbl_t *get_cmd_tbl_base(cmd_tbl_t  *cmdtp)
 /*
  * find command table entry for a command
  */
-typedef struct {
-               size_t len;
-               uint_fast8_t level;
-               bool opt_debug;
-       } find_cmd_para_t;
 
-cmd_tbl_t *_find_cmd (const char *cmd, cmd_tbl_t *table, find_cmd_para_t *para)
+cmd_tbl_t *find_cmd (const char *cmd, cmd_tbl_t *table)
 {
-       cmd_tbl_t *cmdtp_ret = NULL;
-       uint_fast8_t n_found = 0, sub_found = 0;
-
-       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_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 && sub->flags & CTBL_SUBCMDAUTO) {
-                               cmdtp_ret = sub;
-                               ++n_found;
-                       }
-               }
-       }
-       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);
+       uint8_t opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
 
-       if (cmdlevel)
-               *cmdlevel = para.level;
-       return cmdtp;
-}
+       cmd_tbl_t *cmdtp_ret = NULL;
+       uint_fast8_t n_found = 0;
+       uint_fast8_t len = strlen(cmd);
 
-cmd_tbl_t *find_cmd_sub(const char *cmd, cmd_tbl_t *table)
-{
-       cmd_tbl_t *entry = NULL;
+       for (cmd_tbl_t *cmdtp = table; cmdtp->name != NULL; cmdtp++) {
+               if (cmdtp->subcmd) {
+                       for (cmd_tbl_t *sub = cmdtp->subcmd; sub->name != NULL; sub++) {
+                               if (sub->flags & CTBL_SUBCMDAUTO &&
+                                                       strncmp_P(cmd, sub->name, len) == 0 &&
+                                                       (opt_debug || !(sub->flags & CTBL_DBG))) {
+                                       if (len == strlen_P(sub->name))
+                                               return sub;                             /* full match */
+                                       cmdtp_ret = sub;                        /* abbreviated command ? */
+                                       ++n_found;
+                               }
+                       }
+               }
+               if ((cmdtp->flags & CTBL_SUBCMDAUTO) == 0 &&
+                                       strncmp_P(cmd, cmdtp->name, len) == 0 &&
+                                       (opt_debug || !(cmdtp->flags & CTBL_DBG))) {
+                       if (len == strlen_P(cmdtp->name))
+                               return cmdtp;                                   /* full match */
+                       cmdtp_ret = cmdtp;                                      /* abbreviated command ? */
+                       ++n_found;
+               }
+       }
 
-       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, NULL);
+       if (n_found == 1)
+               return cmdtp_ret;                       /* exactly one match */
 
-       return entry;
+       return NULL;    /* not found or ambiguous command */
 }
 
 /*
@@ -179,12 +161,11 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char
                cmd_tbl_t *tp = tbl_start;
                int i = 0;
                while (tp->name != NULL) {
-                       uint_fast8_t len;
-                       if (tp->subcmd && tp->flags & CTBL_SUBCMDAUTO) {
+                       if (tp->subcmd) {
                                cmd_tbl_t *sub = tp->subcmd;
                                while (sub->name != NULL) {
                                        if (sub->flags & CTBL_SUBCMDAUTO) {
-                                               len = strlen_P(sub->name);
+                                               uint_fast8_t len = strlen_P(sub->name);
                                                if (len > maxlen_cmd)
                                                        maxlen_cmd = len;
                                                cmd_list[i++] = sub;
@@ -192,10 +173,13 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char
                                        sub++;
                                }
                        }
-                       len = strlen_P(tp->name);
-                       if (len > maxlen_cmd)
-                               maxlen_cmd = len;
-                       cmd_list[i++] = tp++;
+                       if ((tp->flags & CTBL_SUBCMDAUTO) == 0) {
+                               uint_fast8_t len = strlen_P(tp->name);
+                               if (len > maxlen_cmd)
+                                       maxlen_cmd = len;
+                                       cmd_list[i++] = tp;
+                       }
+                       tp++;
                }
                /* Sort command list */
                qsort(cmd_list, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP);
@@ -228,8 +212,7 @@ command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag UNUSED, int argc, char
         * command help (long version)
         */
        for (uint8_t i = 1; i < argc; ++i) {
-               if ((cmdtp = find_cmd(argv[i], tbl_start, NULL)) != NULL &&
-                                       (opt_debug || !(cmdtp->flags & CTBL_DBG))) {
+               if ((cmdtp = find_cmd(argv[i], tbl_start)) != NULL) {
                        cmd_usage(cmdtp);
                } else {
                        printf_P(PSTR("Unknown command '%s' - try 'help'"
@@ -560,16 +543,15 @@ 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, &cmdlevel);
+       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, &cmdlevel);
+                       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;
@@ -579,12 +561,6 @@ command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
                        ++argv;
                }
        }
-#if 0
-        else {
-               /* Search subcommands */
-               cmdtp = find_cmd_sub(argv[0], cmd_tbl, &cmdlevel);
-       }
-#endif
 
        if (cmdtp == NULL) {
                printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]);