+ 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
+ struct command_registration cr = {
+ .name = cmd_name,
+ .mode = COMMAND_ANY,
+ .help = help_text,
+ .usage = usage,
+ };
+ nc = register_command(cmd_ctx, parent, &cr);
+ if (NULL == nc)
+ {
+ LOG_ERROR("failed to add '%s' help text", cmd_name);
+ return ERROR_FAIL;
+ }
+ LOG_DEBUG("added '%s' help text", cmd_name);
+ return ERROR_OK;
+ }
+ if (help_text)
+ {
+ 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);
+ }
+ if (usage)
+ {
+ bool replaced = false;
+ if (nc->usage)
+ {
+ free((void *)nc->usage);
+ replaced = true;
+ }
+ nc->usage = strdup(usage);
+ if (replaced)
+ LOG_INFO("replaced existing '%s' usage", cmd_name);
+ else
+ LOG_DEBUG("added '%s' usage 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 *str = CMD_ARGV[--CMD_ARGC];
+ const char *help = !strcmp(CMD_NAME, "add_help_text") ? str : NULL;
+ const char *usage = !strcmp(CMD_NAME, "add_usage_text") ? str : NULL;
+ if (!help && !usage)
+ {
+ LOG_ERROR("command name '%s' is unknown", CMD_NAME);
+ return ERROR_INVALID_ARGUMENTS;
+ }
+ // 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, usage);
+}
+
+/* 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;
+}
+
+static const struct command_registration command_builtin_handlers[] = {
+ {
+ .name = "add_help_text",
+ .handler = &handle_help_add_command,
+ .mode = COMMAND_ANY,
+ .help = "add new command help text",
+ .usage = "<command> [...] <help_text>]",
+ },
+ {
+ .name = "add_usage_text",
+ .handler = &handle_help_add_command,
+ .mode = COMMAND_ANY,
+ .help = "add new command usage text",
+ .usage = "<command> [...] <usage_text>]",
+ },
+ {
+ .name = "sleep",
+ .handler = &handle_sleep_command,
+ .mode = COMMAND_ANY,
+ .help = "sleep for n milliseconds. "
+ "\"busy\" will busy wait",
+ .usage = "<n> [busy]",
+ },
+ {
+ .name = "help",
+ .handler = &handle_help_command,
+ .mode = COMMAND_ANY,
+ .help = "show full command help",
+ .usage = "[<command> ...]",
+ },
+ {
+ .name = "usage",
+ .handler = &handle_help_command,
+ .mode = COMMAND_ANY,
+ .help = "show basic command usage",
+ .usage = "[<command> ...]",
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
+struct command_context* command_init(const char *startup_tcl)
+{
+ struct command_context* context = malloc(sizeof(struct command_context));
+ const char *HostOs;