include mode information in help text.
[fw/openocd] / src / helper / command.c
index 61a791dc676a8df084b9d2d41cadf0f457878735..4b7d8cb9b4d86f282806a0ab9341eab0f19c7c3a 100644 (file)
@@ -439,6 +439,14 @@ int unregister_command(struct command_context *context,
        return ERROR_OK;
 }
 
+void command_set_handler_data(struct command *c, void *p)
+{
+       if (NULL != c->handler || NULL != c->jim_handler)
+               c->jim_handler_data = p;
+       for (struct command *cc = c->children; NULL != cc; cc = cc->next)
+               command_set_handler_data(cc, p);
+}
+
 void command_output_text(struct command_context *context, const char *data)
 {
        if (context && context->output_handler && data) {
@@ -517,13 +525,18 @@ char *command_name(struct command *c, char delim)
        return __command_name(c, delim, 0);
 }
 
+static bool command_can_run(struct command_context *cmd_ctx, struct command *c)
+{
+       return c->mode == COMMAND_ANY || c->mode == cmd_ctx->mode;
+}
+
 static int run_command(struct command_context *context,
                struct command *c, const char *words[], unsigned num_words)
 {
-       if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode)))
+       if (!command_can_run(context, c))
        {
                /* Config commands can not run after the config stage */
-               LOG_ERROR("Command '%s' only runs during configuration stage", c->name);
+               LOG_ERROR("The '%s' command must be used before 'init'.", c->name);
                return ERROR_FAIL;
        }
 
@@ -849,16 +862,40 @@ static void command_help_show_wrap(const char *str, unsigned n, unsigned n2)
 static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
                bool show_help)
 {
+       char *cmd_name = command_name(c, ' ');
+       if (NULL == cmd_name)
+               return -ENOMEM;
+
        command_help_show_indent(n);
-       LOG_USER_N("%s", command_name(c, ' '));
+       LOG_USER_N("%s", cmd_name);
+       free(cmd_name);
+
        if (c->usage) {
                LOG_USER_N(" ");
                command_help_show_wrap(c->usage, 0, n + 5);
        }
        else
                LOG_USER_N("\n");
-       if (show_help && c->help)
-               command_help_show_wrap(c->help, n + 3, n + 3);
+
+       if (show_help)
+       {
+               const char *stage_msg;
+               switch (c->mode) {
+               case COMMAND_CONFIG: stage_msg = "CONFIG"; break;
+               case COMMAND_EXEC: stage_msg = "EXEC"; break;
+               case COMMAND_ANY: stage_msg = "CONFIG or EXEC"; break;
+               default: stage_msg = "***UNKNOWN***"; break;
+               }
+               char *msg = alloc_printf("%s%sValid Modes: %s",
+                       c->help ? : "", c->help ? "  " : "", stage_msg);
+               if (NULL != msg)
+               {
+                       command_help_show_wrap(msg, n + 3, n + 3);
+                       free(msg);
+               } else
+                       return -ENOMEM;
+       }
+
        if (++n >= 2)
                return ERROR_OK;