From 17a9dea53a71e9d7e241262725f3dd707b620d37 Mon Sep 17 00:00:00 2001 From: Zachary T Welch Date: Mon, 23 Nov 2009 15:03:04 -0800 Subject: add jim_handler to command_registration Adding jim_handler field to command_registration allows removing the register_jim helper. All command registrations now go through the register_command{,s}() functions. --- src/helper/command.c | 42 ++++++++++++++++++------------------ src/helper/command.h | 7 +++--- src/helper/ioutil.c | 60 ++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 68 insertions(+), 41 deletions(-) (limited to 'src/helper') diff --git a/src/helper/command.c b/src/helper/command.c index 8d710c99..3cb36ea2 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -252,6 +252,8 @@ static struct command *command_new(struct command_context *cmd_ctx, c->usage = strdup(cr->usage); c->parent = parent; c->handler = cr->handler; + c->jim_handler = cr->jim_handler; + c->jim_handler_data = cr->jim_handler_data; c->mode = cr->mode; command_add_child(command_list_for_parent(cmd_ctx, parent), c); @@ -327,16 +329,22 @@ struct command* register_command(struct command_context *context, } c = command_new(context, parent, cr); - /* if allocation failed or it is a placeholder (no handler), we're done */ - if (NULL == c || NULL == c->handler) - return c; + if (NULL == c) + return NULL; - int retval = register_command_handler(c); - if (ERROR_OK != retval) + if (NULL != c->handler) { - unregister_command(context, parent, name); - c = NULL; + int retval = register_command_handler(c); + if (ERROR_OK != retval) + { + unregister_command(context, parent, name); + return NULL; + } } + + if (NULL != cr->jim_handler && NULL == parent) + Jim_CreateCommand(interp, cr->name, cr->jim_handler, cr->jim_handler_data, NULL); + return c; } @@ -882,7 +890,7 @@ static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv) bool found = true; Jim_Obj *const *start; unsigned count; - if (c->handler) + if (c->handler || c->jim_handler) { // include the command name in the list count = remaining + 1; @@ -900,6 +908,12 @@ static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv) start = argv; found = false; } + // pass the command through to the intended handler + if (c->jim_handler) + { + interp->cmdPrivData = c->jim_handler_data; + return (*c->jim_handler)(interp, count, start); + } unsigned nwords; const char **words = script_command_args_alloc(count, start, &nwords); @@ -1149,18 +1163,6 @@ void process_jim_events(void) #endif } -void register_jim(struct command_context *cmd_ctx, const char *name, - Jim_CmdProc cmd, const char *help) -{ - Jim_CreateCommand(interp, name, cmd, NULL, NULL); - - Jim_Obj *cmd_list = Jim_NewListObj(interp, NULL, 0); - Jim_ListAppendElement(interp, cmd_list, - Jim_NewStringObj(interp, name, -1)); - - help_add_command(cmd_ctx, NULL, name, help, NULL); -} - #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \ int parse##name(const char *str, type *ul) \ { \ diff --git a/src/helper/command.h b/src/helper/command.h index 2edeca91..84bdb71e 100644 --- a/src/helper/command.h +++ b/src/helper/command.h @@ -164,6 +164,8 @@ struct command struct command *parent; struct command *children; command_handler_t handler; + Jim_CmdProc jim_handler; + void *jim_handler_data; enum command_mode mode; struct command *next; }; @@ -198,6 +200,8 @@ char *command_name(struct command *c, char delim); struct command_registration { const char *name; command_handler_t handler; + Jim_CmdProc jim_handler; + void *jim_handler_data; enum command_mode mode; const char *help; /// a string listing the options and arguments, required or optional @@ -319,9 +323,6 @@ void process_jim_events(void); extern Jim_Interp *interp; -void register_jim(struct command_context *context, const char *name, - Jim_CmdProc cmd, const char *help); - int parse_ulong(const char *str, unsigned long *ul); int parse_ullong(const char *str, unsigned long long *ul); diff --git a/src/helper/ioutil.c b/src/helper/ioutil.c index 58521eef..e13f5902 100644 --- a/src/helper/ioutil.c +++ b/src/helper/ioutil.c @@ -685,27 +685,51 @@ static const struct command_registration ioutil_command_handlers[] = { .mode = COMMAND_ANY, .help = "display available ram memory", }, + // jim handlers + { + .name = "rm", + .mode = COMMAND_ANY, + .jim_handler = &zylinjtag_Jim_Command_rm, + .help = "remove a file", + .usage = "", + }, + { + .name = "peek", + .mode = COMMAND_ANY, + .jim_handler = &zylinjtag_Jim_Command_peek, + .help = "peek at a memory address", + .usage = "", + }, + { + .name = "poke", + .mode = COMMAND_ANY, + .jim_handler = &zylinjtag_Jim_Command_poke, + .help = "poke at a memory address", + .usage = " ", + }, + { + .name = "ls", + .mode = COMMAND_ANY, + .jim_handler = &zylinjtag_Jim_Command_ls, + .help = "show a listing of files", + .usage = "", + }, + { + .name = "mac", + .mode = COMMAND_ANY, + .jim_handler = &zylinjtag_Jim_Command_mac, + .help = "show MAC address", + }, + { + .name = "ip", + .jim_handler = &zylinjtag_Jim_Command_ip, + .mode = COMMAND_ANY, + .help = "show IP address", + }, COMMAND_REGISTRATION_DONE }; - int ioutil_init(struct command_context *cmd_ctx) { - register_commands(cmd_ctx, NULL, ioutil_command_handlers); - - Jim_CreateCommand(interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL); - - Jim_CreateCommand(interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL); - Jim_CreateCommand(interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL); - Jim_CreateCommand(interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL); - - Jim_CreateCommand(interp, "mac", zylinjtag_Jim_Command_mac, - NULL, NULL); - - Jim_CreateCommand(interp, "ip", zylinjtag_Jim_Command_ip, - NULL, NULL); - - return ERROR_OK; + return register_commands(cmd_ctx, NULL, ioutil_command_handlers); } - - -- cgit v1.2.3