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 <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/9076
Reviewed-by: Evgeniy Naydanov <eugnay@gmail.com>
Tested-by: jenkins
This commit is contained in:
Antonio Borneo
2025-05-17 18:28:47 +02:00
parent e39ae004b0
commit 6420e83aff
2 changed files with 28 additions and 34 deletions

View File

@@ -354,14 +354,12 @@ void command_print_sameline(struct command_invocation *cmd, const char *format,
string = alloc_vprintf(format, ap); string = alloc_vprintf(format, ap);
if (string && cmd) { if (string && cmd) {
/* we want this collected in the log + we also want to pick it up as a tcl return char *output = cmd->output ? cmd->output : "";
* value. output = alloc_printf("%s%s", output, string);
* if (output) {
* The latter bit isn't precisely neat, but will do for now. free(cmd->output);
*/ cmd->output = output;
Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1); }
/* We already printed it above
* command_output_text(context, string); */
free(string); free(string);
} }
@@ -377,16 +375,12 @@ void command_print(struct command_invocation *cmd, const char *format, ...)
string = alloc_vprintf(format, ap); string = alloc_vprintf(format, ap);
if (string && cmd) { if (string && cmd) {
strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char *output = cmd->output ? cmd->output : "";
*char longer */ output = alloc_printf("%s%s\n", output, string);
/* we want this collected in the log + we also want to pick it up as a tcl return if (output) {
* value. free(cmd->output);
* cmd->output = output;
* 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); */
free(string); free(string);
} }
@@ -437,11 +431,9 @@ static int jim_exec_command(Jim_Interp *interp, struct command_context *context,
.argc = argc - 1, .argc = argc - 1,
.argv = words + 1, .argv = words + 1,
.jimtcl_argv = argv + 1, .jimtcl_argv = argv + 1,
.output = NULL,
}; };
cmd.output = Jim_NewEmptyStringObj(context->interp);
Jim_IncrRefCount(cmd.output);
int retval = c->handler(&cmd); int retval = c->handler(&cmd);
if (retval == ERROR_COMMAND_SYNTAX_ERROR) { if (retval == ERROR_COMMAND_SYNTAX_ERROR) {
// Print command syntax // Print command syntax
@@ -452,19 +444,21 @@ static int jim_exec_command(Jim_Interp *interp, struct command_context *context,
if (retval != ERROR_OK) if (retval != ERROR_OK)
LOG_DEBUG("Command '%s' failed with error code %d", LOG_DEBUG("Command '%s' failed with error code %d",
words[0], retval); words[0], retval);
if (cmd.output) {
/* /*
* Use the command output as the Tcl result. * Use the command output as the Tcl result.
* Drop last '\n' to allow command output concatenation * Drop last '\n' to allow command output concatenation
* while keep using command_print() everywhere. * while keep using command_print() everywhere.
*/ */
const char *output_txt = Jim_String(cmd.output); int len = strlen(cmd.output);
int len = strlen(output_txt); if (len && cmd.output[len - 1] == '\n')
if (len && output_txt[len - 1] == '\n')
--len; --len;
Jim_SetResultString(context->interp, output_txt, len); Jim_SetResultString(context->interp, cmd.output, len);
} else {
Jim_SetEmptyResult(context->interp);
} }
Jim_DecrRefCount(context->interp, cmd.output); }
free(cmd.output);
free(words); free(words);
if (retval == ERROR_OK) if (retval == ERROR_OK)

View File

@@ -80,7 +80,7 @@ struct command_invocation {
unsigned int argc; unsigned int argc;
const char **argv; const char **argv;
Jim_Obj * const *jimtcl_argv; Jim_Obj * const *jimtcl_argv;
Jim_Obj *output; char *output;
}; };
/** /**