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);
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)

View File

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