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.
This commit is contained in:
@@ -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) \
|
||||
{ \
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user