From 6b066cd17073d868b88414556d1ca3f0580449ec Mon Sep 17 00:00:00 2001 From: Zachary T Welch Date: Mon, 23 Nov 2009 12:16:27 -0800 Subject: [PATCH] allow scripts to update usage information The add_usage_text command uses the same C handler, which was updated to support its new polymorphic role. This patch updates the two script commands that needed this support: 'find' and 'script'. --- src/helper/command.c | 36 ++++++++++++++++++++++++++++++++---- src/helper/startup.tcl | 7 ++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/helper/command.c b/src/helper/command.c index 3cb36ea2..1263a931 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -951,8 +951,9 @@ int help_add_command(struct command_context *cmd_ctx, struct command *parent, return ERROR_FAIL; } LOG_DEBUG("added '%s' help text", cmd_name); + return ERROR_OK; } - else + if (help_text) { bool replaced = false; if (nc->help) @@ -961,12 +962,25 @@ int help_add_command(struct command_context *cmd_ctx, struct command *parent, replaced = true; } nc->help = strdup(help_text); - if (replaced) LOG_INFO("replaced existing '%s' help", cmd_name); else LOG_DEBUG("added '%s' help text", cmd_name); } + if (usage) + { + bool replaced = false; + if (nc->usage) + { + free((void *)nc->usage); + replaced = true; + } + nc->usage = strdup(usage); + if (replaced) + LOG_INFO("replaced existing '%s' usage", cmd_name); + else + LOG_DEBUG("added '%s' usage text", cmd_name); + } return ERROR_OK; } @@ -979,7 +993,14 @@ COMMAND_HANDLER(handle_help_add_command) } // save help text and remove it from argument list - const char *help_text = CMD_ARGV[--CMD_ARGC]; + const char *str = CMD_ARGV[--CMD_ARGC]; + const char *help = !strcmp(CMD_NAME, "add_help_text") ? str : NULL; + const char *usage = !strcmp(CMD_NAME, "add_usage_text") ? str : NULL; + if (!help && !usage) + { + LOG_ERROR("command name '%s' is unknown", CMD_NAME); + return ERROR_INVALID_ARGUMENTS; + } // likewise for the leaf command name const char *cmd_name = CMD_ARGV[--CMD_ARGC]; @@ -991,7 +1012,7 @@ COMMAND_HANDLER(handle_help_add_command) if (ERROR_OK != retval) return retval; } - return help_add_command(CMD_CTX, c, cmd_name, help_text, NULL); + return help_add_command(CMD_CTX, c, cmd_name, help, usage); } /* sleep command sleeps for miliseconds @@ -1038,6 +1059,13 @@ static const struct command_registration command_builtin_handlers[] = { .help = "add new command help text", .usage = " [...] ]", }, + { + .name = "add_usage_text", + .handler = &handle_help_add_command, + .mode = COMMAND_ANY, + .help = "add new command usage text", + .usage = " [...] ]", + }, { .name = "sleep", .handler = &handle_sleep_command, diff --git a/src/helper/startup.tcl b/src/helper/startup.tcl index ede8cdb9..5969cfec 100644 --- a/src/helper/startup.tcl +++ b/src/helper/startup.tcl @@ -59,14 +59,15 @@ proc find {filename} { # make sure error message matches original input string return -code error "Can't find $filename" } -add_help_text find " - print full path to file according to OpenOCD search rules" +add_usage_text find "" +add_help_text find "print full path to file according to OpenOCD search rules" # Run script proc script {filename} { source [find $filename] } - -add_help_text script " - filename of OpenOCD script (tcl) to run" +add_help_text script "filename of OpenOCD script (tcl) to run" +add_usage_text script "" ######### -- 2.39.5