add 'command mode' introspective handler
authorZachary T Welch <zw@superlucidity.net>
Sat, 28 Nov 2009 20:29:42 +0000 (12:29 -0800)
committerZachary T Welch <zw@superlucidity.net>
Sat, 28 Nov 2009 21:00:39 +0000 (13:00 -0800)
Allows scripts to behave different depending on the current mode.
Also allows introspection of the mode required for commands.

src/helper/command.c

index 2b299568e33849074e2e043c831042011980db02..61a791dc676a8df084b9d2d41cadf0f457878735 100644 (file)
@@ -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)
@@ -1097,6 +1127,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,

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)