diff options
author | Zachary T Welch <zw@superlucidity.net> | 2009-11-28 12:29:42 -0800 |
---|---|---|
committer | Zachary T Welch <zw@superlucidity.net> | 2009-11-28 13:00:39 -0800 |
commit | 3b5751a4d4aa4eb980aee6931b7d100a6d37224a (patch) | |
tree | 574673de1fa2904beccdc66bcbc04d83a5e29033 /src | |
parent | 89fa493a3bc34d22eeca06fa4e78523ac3b766a8 (diff) | |
download | openocd_libswd-3b5751a4d4aa4eb980aee6931b7d100a6d37224a.tar.gz openocd_libswd-3b5751a4d4aa4eb980aee6931b7d100a6d37224a.tar.bz2 openocd_libswd-3b5751a4d4aa4eb980aee6931b7d100a6d37224a.tar.xz openocd_libswd-3b5751a4d4aa4eb980aee6931b7d100a6d37224a.zip |
add 'command mode' introspective handler
Allows scripts to behave different depending on the current mode.
Also allows introspection of the mode required for commands.
Diffstat (limited to 'src')
-rw-r--r-- | src/helper/command.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/helper/command.c b/src/helper/command.c index 2b299568..61a791dc 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -950,6 +950,36 @@ static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv) return script_command_run(interp, count, start, c, found); } +static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv) +{ + struct command_context *cmd_ctx = current_command_context(); + enum command_mode mode; + if (argc > 1) + { + 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; + } + mode = c->mode; + } + else + mode = cmd_ctx->mode; + + const char *mode_str; + switch (mode) { + case COMMAND_ANY: mode_str = "any"; break; + case COMMAND_CONFIG: mode_str = "config"; break; + case COMMAND_EXEC: mode_str = "exec"; break; + default: mode_str = "unknown"; break; + } + Jim_SetResultString(interp, mode_str, -1); + return JIM_OK; +} + static int jim_command_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { if (1 == argc) @@ -1098,6 +1128,15 @@ COMMAND_HANDLER(handle_sleep_command) static const struct command_registration command_subcommand_handlers[] = { { + .name = "mode", + .mode = COMMAND_ANY, + .jim_handler = &jim_command_mode, + .usage = "[<name> ...]", + .help = "Returns the command modes allowed by a command:" + "'any', 'config', or 'exec'. If no command is" + "specified, returns the current command mode.", + }, + { .name = "type", .mode = COMMAND_ANY, .jim_handler = &jim_command_type, |