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).

Signed-off-by: Zachary T Welch <z...@superlucidity.net>
---
 src/helper/command.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/src/helper/command.c b/src/helper/command.c
index 65421f5..b34847b 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);
 }
 
+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
 };
 
-- 
1.6.4.4


>From aafcd6e134f413bb610f26a342d9cbf13d923a14 Mon Sep 17 00:00:00 2001
From: Zachary T Welch <z...@superlucidity.net>
Date: Fri, 27 Nov 2009 19:14:30 -0800
Subject: [PATCH 2/2] improve command handler wrapper script

Adds 'ocd_bouncer' in startup.tcl that is called as a helper for
all command handlers, shrinking the embedded C wrapper to a mere stub.

Jim handlers are called directly, simple handlers get called with the
wrapper to capture and discard their output on error, and placeholders
call help directly (though the unknown handler still does this too).
It attempts to improve the quality of the error messages as well.

Signed-off-by: Zachary T Welch <z...@superlucidity.net>
---
 src/helper/command.c   |    5 ++---
 src/helper/startup.tcl |   24 ++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/helper/command.c b/src/helper/command.c
index b34847b..64db95d 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -314,9 +314,8 @@ static int register_command_handler(struct command *c)
        }
 
        /* we now need to add an overrideable proc */
-       const char *override_name = alloc_printf("proc %s {args} {"
-                       "if {[catch {eval ocd_%s $args}] == 0} "
-                       "{return \"\"} else {return -code error}}",
+       const char *override_name = alloc_printf(
+                       "proc %s {args} {eval ocd_bouncer %s $args}",
                        full_name, full_name);
        if (NULL == override_name)
                goto free_full_name;
diff --git a/src/helper/startup.tcl b/src/helper/startup.tcl
index f11d5b6..cb5fb02 100644
--- a/src/helper/startup.tcl
+++ b/src/helper/startup.tcl
@@ -10,6 +10,30 @@ proc exit {} {
        ocd_throw exit
 }
 
+# All commands are registered with an 'ocd_' prefix, while the "real"
+# command is a wrapper that calls this function.  Its primary purpose is
+# to discard 'handler' command output,
+proc ocd_bouncer {name args} {
+       set cmd [format "ocd_%s" $name]
+       set type [eval command type $cmd $args]
+       if {$type == "native"} {
+               return [eval $cmd $args]
+       } else {if {$type == "simple"} {
+               if {[catch {eval $cmd $args}] == 0} {
+                       return ""
+               } else {
+                       set errmsg "Command handler execution failed"
+               }
+       } else {if {$type == "group"} {
+               catch {eval help $name $args}
+               set errmsg [format "%s: command requires more arguments" \
+                       [concat $name " " $args]]
+       } else {
+               set errmsg [format "Unknown command type: %s" $type]
+       }}}
+       return -code error $errmsg
+}
+
 # Try flipping / and \ to find file if the filename does not
 # match the precise spelling
 proc find {filename} {
-- 
1.6.4.4

_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to