helper/command: Handle Tcl return values consistently
[openocd.git] / src / helper / command.c
index be131bb3b93840d3b414e11fbd11960da39fd992..11c9e5f5265bb804fbd30fa8cdf798d58cfcc2a3 100644 (file)
@@ -510,13 +510,13 @@ void command_print_sameline(struct command_invocation *cmd, const char *format,
        va_start(ap, format);
 
        string = alloc_vprintf(format, ap);
-       if (string != NULL) {
+       if (string != NULL && 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.
                 */
-               LOG_USER_N("%s", string);
+               Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1);
                /* We already printed it above
                 * command_output_text(context, string); */
                free(string);
@@ -533,7 +533,7 @@ void command_print(struct command_invocation *cmd, const char *format, ...)
        va_start(ap, format);
 
        string = alloc_vprintf(format, ap);
-       if (string != NULL) {
+       if (string != NULL && 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
@@ -541,7 +541,7 @@ void command_print(struct command_invocation *cmd, const char *format, ...)
                 *
                 * The latter bit isn't precisely neat, but will do for now.
                 */
-               LOG_USER_N("%s", string);
+               Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1);
                /* We already printed it above
                 * command_output_text(context, string); */
                free(string);
@@ -628,6 +628,9 @@ static int run_command(struct command_context *context,
        if (c->jim_handler_data)
                context->current_target_override = c->jim_handler_data;
 
+       cmd.output = Jim_NewEmptyStringObj(context->interp);
+       Jim_IncrRefCount(cmd.output);
+
        int retval = c->handler(&cmd);
 
        if (c->jim_handler_data)
@@ -650,7 +653,11 @@ static int run_command(struct command_context *context,
                LOG_DEBUG("Command '%s' failed with error code %d",
                                        full_name ? full_name : c->name, retval);
                free(full_name);
+       } else {
+               /* Use the command output as the Tcl result */
+               Jim_SetResult(context->interp, cmd.output);
        }
+       Jim_DecrRefCount(context->interp, cmd.output);
 
        return retval;
 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)