+ if (!cmd)
+ return NULL;
+
+ char *optenv = getenv_str(PSTR("cmd"));
+ uint8_t opt_debug = optenv && strstr_P(optenv, PSTR("debug")) != NULL;
+
+ cmd_tbl_t *cmdtp_ret = NULL;
+ uint_fast8_t n_found = 0;
+ uint_fast8_t len = strlen(cmd);
+
+ 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;
+ }
+ }