+ if (argc != 2)
+ return JIM_ERR;
+
+ Jim_Obj *tclOutput = command_log_capture_start(interp);
+
+ const char *str = Jim_GetString(argv[1], NULL);
+ int retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__);
+
+ command_log_capture_finish(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,
+ bool show_help);
+
+static COMMAND_HELPER(command_help_show_list, struct command *head, unsigned n,
+ bool show_help)
+{
+ for (struct command *c = head; NULL != c; c = c->next)
+ CALL_COMMAND_HANDLER(command_help_show, c, n, show_help);
+ return ERROR_OK;
+}
+
+#define HELP_LINE_WIDTH(_n) (int)(76 - (2 * _n))
+
+static void command_help_show_indent(unsigned n)
+{
+ for (unsigned i = 0; i < n; i++)
+ LOG_USER_N(" ");
+}
+static void command_help_show_wrap(const char *str, unsigned n, unsigned n2)
+{
+ const char *cp = str, *last = str;
+ while (*cp)
+ {
+ const char *next = last;
+ do {
+ cp = next;
+ do {
+ next++;
+ } while (*next != ' ' && *next != '\t' && *next != '\0');
+ } while ((next - last < HELP_LINE_WIDTH(n)) && *next != '\0');
+ if (next - last < HELP_LINE_WIDTH(n))
+ cp = next;
+ command_help_show_indent(n);
+ LOG_USER_N("%.*s", (int)(cp - last), last);
+ LOG_USER_N("\n");
+ last = cp + 1;
+ n = n2;
+ }
+}
+static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
+ bool show_help)
+{
+ command_help_show_indent(n);
+ LOG_USER_N("%s", command_name(c, ' '));
+ if (c->usage) {
+ LOG_USER_N(" ");
+ command_help_show_wrap(c->usage, 0, n + 5);
+ }
+ else
+ LOG_USER_N("\n");
+ if (show_help && c->help)
+ command_help_show_wrap(c->help, n + 3, n + 3);
+ if (++n >= 2)
+ return ERROR_OK;
+
+ return CALL_COMMAND_HANDLER(command_help_show_list,
+ c->children, n, show_help);
+}
+COMMAND_HANDLER(handle_help_command)
+{
+ bool full = strcmp(CMD_NAME, "help") == 0;
+
+ struct command *c = CMD_CTX->commands;
+
+ if (0 == CMD_ARGC)
+ return CALL_COMMAND_HANDLER(command_help_show_list, c, 0, full);
+
+ 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, full);
+}
+
+static int command_unknown_find(unsigned argc, Jim_Obj *const *argv,
+ struct command *head, struct command **out)
+{
+ if (0 == argc)
+ return argc;
+ struct command *c = command_find(head, Jim_GetString(argv[0], NULL));
+ if (NULL == c)
+ return argc;
+ *out = c;
+ return command_unknown_find(--argc, ++argv, (*out)->children, out);
+}
+
+static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *cmd_name = Jim_GetString(argv[0], NULL);
+ script_debug(interp, cmd_name, argc - 1, argv + 1);
+
+ struct command_context *cmd_ctx = current_command_context();
+ struct command *c = cmd_ctx->commands;
+ int remaining = command_unknown_find(argc - 1, argv + 1, c, &c);
+ // if nothing could be consumed, then it's really an unknown command
+ if (remaining == argc - 1)
+ {
+ const char *cmd = Jim_GetString(argv[1], NULL);
+ LOG_ERROR("Unknown command:\n %s", cmd);
+ return JIM_OK;
+ }
+
+ bool found = true;
+ Jim_Obj *const *start;
+ unsigned count;
+ if (c->handler || c->jim_handler)
+ {
+ // include the command name in the list
+ count = remaining + 1;
+ start = argv + (argc - remaining - 1);
+ }
+ else
+ {
+ c = command_find(cmd_ctx->commands, "help");
+ if (NULL == c)
+ {
+ LOG_ERROR("unknown command, but help is missing too");
+ return JIM_ERR;
+ }
+ count = argc - remaining;
+ start = argv;
+ found = false;
+ }
+ // pass the command through to the intended handler
+ if (c->jim_handler)
+ {
+ interp->cmdPrivData = c->jim_handler_data;
+ return (*c->jim_handler)(interp, count, start);
+ }
+
+ return script_command_run(interp, count, start, c, found);
+}
+
+int help_add_command(struct command_context *cmd_ctx, struct command *parent,
+ const char *cmd_name, const char *help_text, const char *usage)
+{
+ 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;