add jim_handler to command_registration
authorZachary T Welch <zw@superlucidity.net>
Mon, 23 Nov 2009 23:03:04 +0000 (15:03 -0800)
committerZachary T Welch <zw@superlucidity.net>
Wed, 25 Nov 2009 05:37:37 +0000 (21:37 -0800)
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/flash/flash.c
src/helper/command.c
src/helper/command.h
src/helper/ioutil.c
src/jtag/tcl.c
src/target/target.c

index 4584c5d..bbdbaa8 100644 (file)
@@ -1368,8 +1368,6 @@ static const struct command_registration flash_exec_command_handlers[] = {
 
 int flash_init_drivers(struct command_context *cmd_ctx)
 {
-       register_jim(cmd_ctx, "ocd_flash_banks",
-                       jim_flash_banks, "return information about the flash banks");
        if (!flash_banks)
                return ERROR_OK;
 
@@ -1377,7 +1375,6 @@ int flash_init_drivers(struct command_context *cmd_ctx)
        return register_commands(cmd_ctx, parent, flash_exec_command_handlers);
 }
 
-
 static const struct command_registration flash_config_command_handlers[] = {
        {
                .name = "bank",
@@ -1389,6 +1386,12 @@ static const struct command_registration flash_config_command_handlers[] = {
                .help = "Define a new bank with the given name, "
                        "using the specified NOR flash driver.",
        },
+       {
+               .name = "banks",
+               .mode = COMMAND_ANY,
+               .jim_handler = &jim_flash_banks,
+               .help = "return information about the flash banks",
+       },
        COMMAND_REGISTRATION_DONE
 };
 static const struct command_registration flash_command_handlers[] = {
index 8d710c9..3cb36ea 100644 (file)
@@ -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) \
        { \
index 2edeca9..84bdb71 100644 (file)
@@ -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);
 
index 58521ee..e13f590 100644 (file)
@@ -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 = "<file>",
+       },
+       {
+               .name = "peek",
+               .mode = COMMAND_ANY,
+               .jim_handler = &zylinjtag_Jim_Command_peek,
+               .help = "peek at a memory address",
+               .usage = "<addr>",
+       },
+       {
+               .name = "poke",
+               .mode = COMMAND_ANY,
+               .jim_handler = &zylinjtag_Jim_Command_poke,
+               .help = "poke at a memory address",
+               .usage = "<addr> <value>",
+       },
+       {
+               .name = "ls",
+               .mode = COMMAND_ANY,
+               .jim_handler = &zylinjtag_Jim_Command_ls,
+               .help = "show a listing of files",
+               .usage = "<dir>",
+       },
+       {
+               .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);
 }
-
-
index 6b42021..bc29078 100644 (file)
@@ -1529,26 +1529,37 @@ static const struct command_registration jtag_command_handlers[] = {
                .help = "choose short(default) or long tms_sequence",
                .usage = "<short | long>",
        },
+       // jim commands
+       {
+               .name = "jtag",
+               .mode = COMMAND_ANY,
+               .jim_handler = &jim_jtag_command,
+               .help = "perform jtag tap actions",
+       },
+       {
+               .name = "drscan",
+               .mode = COMMAND_EXEC,
+               .jim_handler = &Jim_Command_drscan,
+               .help = "execute DR scan <device> "
+                       "<num_bits> <value> <num_bits1> <value2> ...",
+       },
+       {
+               .name = "flush_count",
+               .mode = COMMAND_EXEC,
+               .jim_handler = &Jim_Command_flush_count,
+               .help = "returns number of times the JTAG queue has been flushed",
+       },
+       {
+               .name = "pathmove",
+               .mode = COMMAND_EXEC,
+               .jim_handler = &Jim_Command_pathmove,
+               .usage = "<state1>,<state2>,<state3>... ",
+               .help = "move JTAG to state1 then to state2, state3, etc.",
+       },
        COMMAND_REGISTRATION_DONE
 };
 
 int jtag_register_commands(struct command_context *cmd_ctx)
 {
-       register_jim(cmd_ctx, "jtag", jim_jtag_command,
-                       "perform jtag tap actions");
-
-       register_jim(cmd_ctx, "drscan", Jim_Command_drscan,
-                       "execute DR scan <device> "
-                       "<num_bits> <value> <num_bits1> <value2> ...");
-
-       register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count,
-                       "returns number of times the JTAG queue has been flushed");
-
-       register_jim(cmd_ctx, "pathmove", Jim_Command_pathmove,
-                       "<state1>,<state2>,<state3>... "
-                       "- move JTAG to state1 then to state2, state3, etc.");
-
        return register_commands(cmd_ctx, NULL, jtag_command_handlers);
 }
-
-
index e999e68..4a9095a 100644 (file)
@@ -777,12 +777,14 @@ int target_init(struct command_context *cmd_ctx)
                        target->type->mcr = default_mcr;
                } else
                {
-                       /* FIX! multiple targets will generally register global commands
-                        * multiple times. Only register this one if *one* of the
-                        * targets need the command. Hmm... make it a command on the
-                        * Jim Tcl target object?
-                        */
-                       register_jim(cmd_ctx, "mcr", jim_mcrmrc, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
+                       const struct command_registration mcr_cmd = {
+                               .name = "mcr",
+                               .mode = COMMAND_EXEC,
+                               .jim_handler = &jim_mcrmrc,
+                               .help = "write coprocessor",
+                               .usage = "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
+                       };
+                       register_command(cmd_ctx, NULL, &mcr_cmd);
                }
 
                if (target->type->mrc == NULL)
@@ -790,7 +792,13 @@ int target_init(struct command_context *cmd_ctx)
                        target->type->mrc = default_mrc;
                } else
                {
-                       register_jim(cmd_ctx, "mrc", jim_mcrmrc, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
+                       const struct command_registration mrc_cmd = {
+                               .name = "mrc",
+                               .jim_handler = &jim_mcrmrc,
+                               .help = "read coprocessor",
+                               .usage = "<cpnum> <op1> <op2> <CRn> <CRm>",
+                       };
+                       register_command(cmd_ctx, NULL, &mrc_cmd);
                }
 
 
@@ -4377,14 +4385,14 @@ static int target_create(Jim_GetOptInfo *goi)
        }
 
        /* now - create the new target name command */
-       e = Jim_CreateCommand(goi->interp,
-                                                  /* name */
-                                                  cp,
-                                                  tcl_target_func, /* C function */
-                                                  target, /* private data */
-                                                  NULL); /* no del proc */
-
-       return e;
+       const struct command_registration target_command = {
+               .name = cp,
+               .jim_handler = &tcl_target_func,
+               .jim_handler_data = target,
+               .help = "target command group",
+       };
+       struct command *c = register_command(cmd_ctx, NULL, &target_command);
+       return (NULL != c) ? ERROR_OK : ERROR_FAIL;
 }
 
 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
@@ -4773,12 +4781,17 @@ static const struct command_registration target_command_handlers[] = {
                        "or list targets (no parameters)",
                .usage = "[<new_current_target>]",
        },
+       {
+               .name = "target",
+               .mode = COMMAND_CONFIG,
+               .jim_handler = &jim_target,
+               .help = "configure target",
+       },
        COMMAND_REGISTRATION_DONE
 };
 
 int target_register_commands(struct command_context *cmd_ctx)
 {
-       register_jim(cmd_ctx, "target", jim_target, "configure target");
        return register_commands(cmd_ctx, NULL, target_command_handlers);
 }
 
@@ -4967,6 +4980,22 @@ static const struct command_registration target_exec_command_handlers[] = {
                .mode = COMMAND_EXEC,
                .usage = "<file> [offset] [type]",
        },
+       {
+               .name = "ocd_mem2array",
+               .mode = COMMAND_EXEC,
+               .jim_handler = &jim_mem2array,
+               .help = "read memory and return as a TCL array "
+                       "for script processing",
+               .usage = "<arrayname> <width=32|16|8> <address> <count>",
+       },
+       {
+               .name = "ocd_array2mem",
+               .mode = COMMAND_EXEC,
+               .jim_handler = &jim_array2mem,
+               .help = "convert a TCL array to memory locations "
+                       "and write the values",
+               .usage = "<arrayname> <width=32|16|8> <address> <count>",
+       },
        COMMAND_REGISTRATION_DONE
 };
 int target_register_user_commands(struct command_context *cmd_ctx)
@@ -4978,13 +5007,6 @@ int target_register_user_commands(struct command_context *cmd_ctx)
        if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
                return retval;
 
-       register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array,
-                       "read memory and return as a TCL array for script processing "
-                       "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
-
-       register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem,
-                       "convert a TCL array to memory locations and write the values "
-                       "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
 
        return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
 }