From 6420e83aff53eb9e3c755ada57128809a9d9c152 Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Sat, 17 May 2025 18:28:47 +0200 Subject: [PATCH] helper: command: rewrite command_print() dropping jimtcl strings Rewrite the function command_print() without using any specific API from jimtcl for string manipulation. Change-Id: I1adddd493b43e30ead26e96da09a4ee8c0a41307 Signed-off-by: Antonio Borneo Reviewed-on: https://review.openocd.org/c/openocd/+/9076 Reviewed-by: Evgeniy Naydanov Tested-by: jenkins --- src/helper/command.c | 60 ++++++++++++++++++++------------------------ src/helper/command.h | 2 +- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/helper/command.c b/src/helper/command.c index 6b5337f42..c3873be27 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -354,14 +354,12 @@ void command_print_sameline(struct command_invocation *cmd, const char *format, string = alloc_vprintf(format, ap); if (string && cmd) { - /* we want this collected in the log + we also want to pick it up as a tcl return - * value. - * - * The latter bit isn't precisely neat, but will do for now. - */ - Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1); - /* We already printed it above - * command_output_text(context, string); */ + char *output = cmd->output ? cmd->output : ""; + output = alloc_printf("%s%s", output, string); + if (output) { + free(cmd->output); + cmd->output = output; + } free(string); } @@ -377,16 +375,12 @@ void command_print(struct command_invocation *cmd, const char *format, ...) string = alloc_vprintf(format, ap); if (string && cmd) { - strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one - *char longer */ - /* we want this collected in the log + we also want to pick it up as a tcl return - * value. - * - * The latter bit isn't precisely neat, but will do for now. - */ - Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1); - /* We already printed it above - * command_output_text(context, string); */ + char *output = cmd->output ? cmd->output : ""; + output = alloc_printf("%s%s\n", output, string); + if (output) { + free(cmd->output); + cmd->output = output; + } free(string); } @@ -437,11 +431,9 @@ static int jim_exec_command(Jim_Interp *interp, struct command_context *context, .argc = argc - 1, .argv = words + 1, .jimtcl_argv = argv + 1, + .output = NULL, }; - cmd.output = Jim_NewEmptyStringObj(context->interp); - Jim_IncrRefCount(cmd.output); - int retval = c->handler(&cmd); if (retval == ERROR_COMMAND_SYNTAX_ERROR) { // Print command syntax @@ -452,19 +444,21 @@ static int jim_exec_command(Jim_Interp *interp, struct command_context *context, if (retval != ERROR_OK) LOG_DEBUG("Command '%s' failed with error code %d", words[0], retval); - /* - * Use the command output as the Tcl result. - * Drop last '\n' to allow command output concatenation - * while keep using command_print() everywhere. - */ - const char *output_txt = Jim_String(cmd.output); - int len = strlen(output_txt); - if (len && output_txt[len - 1] == '\n') - --len; - Jim_SetResultString(context->interp, output_txt, len); + if (cmd.output) { + /* + * Use the command output as the Tcl result. + * Drop last '\n' to allow command output concatenation + * while keep using command_print() everywhere. + */ + int len = strlen(cmd.output); + if (len && cmd.output[len - 1] == '\n') + --len; + Jim_SetResultString(context->interp, cmd.output, len); + } else { + Jim_SetEmptyResult(context->interp); + } } - Jim_DecrRefCount(context->interp, cmd.output); - + free(cmd.output); free(words); if (retval == ERROR_OK) diff --git a/src/helper/command.h b/src/helper/command.h index 8bd2430e0..8ce45473f 100644 --- a/src/helper/command.h +++ b/src/helper/command.h @@ -80,7 +80,7 @@ struct command_invocation { unsigned int argc; const char **argv; Jim_Obj * const *jimtcl_argv; - Jim_Obj *output; + char *output; }; /**