summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZachary T Welch <zw@superlucidity.net>2009-11-21 13:33:32 -0800
committerZachary T Welch <zw@superlucidity.net>2009-11-24 21:37:30 -0800
commit607634f967cf25630860794847dea770eb17a8f4 (patch)
tree1e59aaa18de99dbecb2bfb6cd6a617998176961f /src
parent25a7ac2c756c78603c9c99d06f55717440409a23 (diff)
downloadopenocd+libswd-607634f967cf25630860794847dea770eb17a8f4.tar.gz
openocd+libswd-607634f967cf25630860794847dea770eb17a8f4.tar.bz2
openocd+libswd-607634f967cf25630860794847dea770eb17a8f4.tar.xz
openocd+libswd-607634f967cf25630860794847dea770eb17a8f4.zip
more command registration refactoring
Split out the handler registration into its own function, and add a few obviously missing NULL pointer error checking.
Diffstat (limited to 'src')
-rw-r--r--src/helper/command.c54
1 files changed, 38 insertions, 16 deletions
diff --git a/src/helper/command.c b/src/helper/command.c
index 28952fda..9cc996c2 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -266,6 +266,38 @@ static void command_free(struct command *c)
free(c);
}
+static int register_command_handler(struct command *c)
+{
+ int retval = -ENOMEM;
+ const char *full_name = command_name(c, '_');
+ if (NULL == full_name)
+ return retval;
+
+ const char *ocd_name = alloc_printf("ocd_%s", full_name);
+ if (NULL == full_name)
+ goto free_full_name;
+
+ Jim_CreateCommand(interp, ocd_name, script_command, c, NULL);
+ free((void *)ocd_name);
+
+ /* 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}}",
+ full_name, full_name);
+ if (NULL == full_name)
+ goto free_full_name;
+
+ Jim_Eval_Named(interp, override_name, __THIS__FILE__, __LINE__);
+ free((void *)override_name);
+
+ retval = ERROR_OK;
+
+free_full_name:
+ free((void *)full_name);
+ return retval;
+}
+
struct command* register_command(struct command_context *context,
struct command *parent, const struct command_registration *cr)
{
@@ -287,22 +319,12 @@ struct command* register_command(struct command_context *context,
if (NULL == c || NULL == c->handler)
return c;
- const char *full_name = command_name(c, '_');
-
- const char *ocd_name = alloc_printf("ocd_%s", full_name);
- Jim_CreateCommand(interp, ocd_name, script_command, c, NULL);
- free((void *)ocd_name);
-
- /* 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}}",
- full_name, full_name);
- Jim_Eval_Named(interp, override_name, __THIS__FILE__, __LINE__);
- free((void *)override_name);
-
- free((void *)full_name);
-
+ int retval = register_command_handler(c);
+ if (ERROR_OK != retval)
+ {
+ unregister_command(context, parent, name);
+ c = NULL;
+ }
return c;
}