+static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 2)
+ return JIM_ERR;
+ int retcode;
+ const char *str = Jim_GetString(argv[1], NULL);
+
+ /* capture log output and return it */
+ Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
+ /* a garbage collect can happen, so we need a reference count to this object */
+ Jim_IncrRefCount(tclOutput);
+
+ log_add_callback(tcl_output, tclOutput);
+
+ retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__);
+
+ log_remove_callback(tcl_output, tclOutput);
+
+ /* We dump output into this local variable */
+ Jim_SetResult(interp, tclOutput);
+ Jim_DecrRefCount(interp, tclOutput);
+
+ return retcode;
+}
+
+static COMMAND_HELPER(command_help_find, struct command *head,
+ struct command **out)
+{
+ if (0 == CMD_ARGC)
+ return ERROR_INVALID_ARGUMENTS;
+ *out = command_find(head, CMD_ARGV[0]);
+ if (NULL == *out)
+ return ERROR_INVALID_ARGUMENTS;
+ if (--CMD_ARGC == 0)
+ return ERROR_OK;
+ CMD_ARGV++;
+ return CALL_COMMAND_HANDLER(command_help_find, (*out)->children, out);
+}
+
+static COMMAND_HELPER(command_help_show, struct command *c, unsigned n);
+
+static COMMAND_HELPER(command_help_show_list, struct command *head, unsigned n)
+{
+ for (struct command *c = head; NULL != c; c = c->next)
+ CALL_COMMAND_HANDLER(command_help_show, c, n);
+ return ERROR_OK;
+}
+static COMMAND_HELPER(command_help_show, struct command *c, unsigned n)
+{
+ command_run_linef(CMD_CTX, "cmd_help {%s} {%s} %d", command_name(c, ' '),
+ c->help ? : "no help available", n);
+
+ if (++n >= 2)
+ return ERROR_OK;
+
+ return CALL_COMMAND_HANDLER(command_help_show_list, c->children, n);
+}
+COMMAND_HANDLER(handle_help_command)
+{
+ struct command *c = CMD_CTX->commands;
+
+ if (0 == CMD_ARGC)
+ return CALL_COMMAND_HANDLER(command_help_show_list, c, 0);
+
+ int retval = CALL_COMMAND_HANDLER(command_help_find, c, &c);
+ if (ERROR_OK != retval)
+ return retval;
+
+ return CALL_COMMAND_HANDLER(command_help_show, c, 0);
+}
+
+
+int help_add_command(struct command_context *cmd_ctx, struct command *parent,
+ const char *cmd_name, const char *help_text)
+{
+ struct command **head = command_list_for_parent(cmd_ctx, parent);
+ struct command *nc = command_find(*head, cmd_name);
+ if (NULL == nc)
+ {
+ // add a new command with help text
+ nc = register_command(cmd_ctx, parent, cmd_name,
+ NULL, COMMAND_ANY, help_text);
+ if (NULL == nc)
+ {
+ LOG_ERROR("failed to add '%s' help text", cmd_name);
+ return ERROR_FAIL;
+ }
+ LOG_DEBUG("added '%s' help text", cmd_name);
+ }
+ else
+ {
+ bool replaced = false;
+ if (nc->help)
+ {
+ free((void *)nc->help);
+ replaced = true;
+ }
+ nc->help = strdup(help_text);
+
+ if (replaced)
+ LOG_INFO("replaced existing '%s' help", cmd_name);
+ else
+ LOG_DEBUG("added '%s' help text", cmd_name);
+ }
+ return ERROR_OK;
+}
+
+COMMAND_HANDLER(handle_help_add_command)
+{
+ if (CMD_ARGC < 2)
+ {
+ LOG_ERROR("%s: insufficient arguments", CMD_NAME);
+ return ERROR_INVALID_ARGUMENTS;
+ }
+
+ // save help text and remove it from argument list
+ const char *help_text = CMD_ARGV[--CMD_ARGC];
+ // likewise for the leaf command name
+ const char *cmd_name = CMD_ARGV[--CMD_ARGC];
+
+ struct command *c = NULL;
+ if (CMD_ARGC > 0)
+ {
+ c = CMD_CTX->commands;
+ int retval = CALL_COMMAND_HANDLER(command_help_find, c, &c);
+ if (ERROR_OK != retval)
+ return retval;
+ }
+ return help_add_command(CMD_CTX, c, cmd_name, help_text);
+}
+
+/* sleep command sleeps for <n> miliseconds
+ * this is useful in target startup scripts
+ */
+COMMAND_HANDLER(handle_sleep_command)
+{
+ bool busy = false;
+ if (CMD_ARGC == 2)
+ {
+ if (strcmp(CMD_ARGV[1], "busy") == 0)
+ busy = true;
+ else
+ return ERROR_COMMAND_SYNTAX_ERROR;
+ }
+ else if (CMD_ARGC < 1 || CMD_ARGC > 2)
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ unsigned long duration = 0;
+ int retval = parse_ulong(CMD_ARGV[0], &duration);
+ if (ERROR_OK != retval)
+ return retval;
+
+ if (!busy)
+ {
+ long long then = timeval_ms();
+ while (timeval_ms() - then < (long long)duration)
+ {
+ target_call_timer_callbacks_now();
+ usleep(1000);
+ }
+ }
+ else
+ busy_sleep(duration);
+
+ return ERROR_OK;
+}
+
+struct command_context* command_init(const char *startup_tcl)