From: <ge...@op...> - 2025-08-10 14:48:33
|
This is an automated email from Gerrit. "Antonio Borneo <bor...@gm...>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9076 -- gerrit commit 4c4af22066e97617fab6e53648bee4fc9a81c9f2 Author: Antonio Borneo <bor...@gm...> Date: Sat May 17 18:28:47 2025 +0200 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 <bor...@gm...> diff --git a/src/helper/command.c b/src/helper/command.c index b70081a4dd..a3ceed0e2e 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -358,14 +358,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); } @@ -381,16 +379,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); } @@ -441,11 +435,9 @@ static int 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 help for command */ @@ -456,20 +448,23 @@ static int 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); + return command_retval_set(interp, retval); } diff --git a/src/helper/command.h b/src/helper/command.h index 8bd2430e0b..8ce45473f2 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; }; /** -- |