From 37dd5a685a67f9069ac0c1d98d47077a67fb897a Mon Sep 17 00:00:00 2001
From: Zachary T Welch <zw@superlucidity.net>
Date: Fri, 27 Nov 2009 18:59:14 -0800
Subject: 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).
---
 src/helper/command.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

(limited to 'src')

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
 };
 
-- 
cgit v1.2.3