- for (; cmd < commands + command_cnt; cmd++)
- if (!memcasecmp (cmd->name, text, strlen (text))
- && (!(cmd->flags & F_TESTING) || get_testing_mode ())
- && (!(cmd->flags & F_ENHANCED) || get_syntax () == ENHANCED))
- return xstrdup (cmd++->name);
+ for (; *cmd < commands + command_cnt; (*cmd)++)
+ if (!memcasecmp ((*cmd)->name, prefix, strlen (prefix))
+ && (!((*cmd)->flags & F_TESTING) || get_testing_mode ())
+ && (!((*cmd)->flags & F_ENHANCED) || get_syntax () == ENHANCED)
+ && ((*cmd)->function != NULL)
+ && in_correct_state (*cmd, completion_state))
+ return (*cmd)++->name;