summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZachary T Welch <zw@superlucidity.net>2009-11-27 18:59:14 -0800
committerZachary T Welch <zw@superlucidity.net>2009-11-28 12:58:35 -0800
commit37dd5a685a67f9069ac0c1d98d47077a67fb897a (patch)
treef34e8b56304aa8c61f9c9de99c1128f1701241fc
parent5f0223423d6c9a41bf0a9ac45772d1b01469bf0a (diff)
downloadopenocd_libswd-37dd5a685a67f9069ac0c1d98d47077a67fb897a.tar.gz
openocd_libswd-37dd5a685a67f9069ac0c1d98d47077a67fb897a.tar.bz2
openocd_libswd-37dd5a685a67f9069ac0c1d98d47077a67fb897a.tar.xz
openocd_libswd-37dd5a685a67f9069ac0c1d98d47077a67fb897a.zip
add 'command type' introspective handler
Adds the 'command' group handler, with the 'type' command producing a string that tells whether the given command is 'native' (for Jim-based command handlers), 'simple' (for simple built-in commands), 'group' for command group placeholders, and 'unknown' if not found in the command registration tables (e.g. core built-ins functions).
-rw-r--r--src/helper/command.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/helper/command.c b/src/helper/command.c
index 65421f58..23175baf 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -948,6 +948,31 @@ static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
return script_command_run(interp, count, start, c, found);
}
+static int jim_command_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (1 == argc)
+ return JIM_ERR;
+
+ struct command_context *cmd_ctx = current_command_context();
+ struct command *c = cmd_ctx->commands;
+ int remaining = command_unknown_find(argc - 1, argv + 1, c, &c, true);
+ // if nothing could be consumed, then it's an unknown command
+ if (remaining == argc - 1)
+ {
+ Jim_SetResultString(interp, "unknown", -1);
+ return JIM_OK;
+ }
+
+ if (c->jim_handler)
+ Jim_SetResultString(interp, "native", -1);
+ else if (c->handler)
+ Jim_SetResultString(interp, "simple", -1);
+ else
+ Jim_SetResultString(interp, "group", -1);
+
+ return JIM_OK;
+}
+
int help_add_command(struct command_context *cmd_ctx, struct command *parent,
const char *cmd_name, const char *help_text, const char *usage)
{
@@ -1069,6 +1094,18 @@ COMMAND_HANDLER(handle_sleep_command)
return ERROR_OK;
}
+static const struct command_registration command_subcommand_handlers[] = {
+ {
+ .name = "type",
+ .mode = COMMAND_ANY,
+ .jim_handler = &jim_command_type,
+ .usage = "<name> ...",
+ .help = "Returns the type of built-in command:"
+ "'native', 'simple', 'group', or 'unknown'",
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
static const struct command_registration command_builtin_handlers[] = {
{
.name = "add_help_text",
@@ -1106,6 +1143,12 @@ static const struct command_registration command_builtin_handlers[] = {
.help = "show basic command usage",
.usage = "[<command> ...]",
},
+ {
+ .name = "command",
+ .mode= COMMAND_ANY,
+ .help = "core command group (introspection)",
+ .chain = command_subcommand_handlers,
+ },
COMMAND_REGISTRATION_DONE
};