+ if (CMD_ARGC < 2) {
+ LOG_ERROR("%s: insufficient arguments", CMD_NAME);
+ return ERROR_COMMAND_SYNTAX_ERROR;
+ }
+
+ /* 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_COMMAND_SYNTAX_ERROR;
+ }
+ /* 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> milliseconds
+ * 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_subcommand_handlers[] = {
+ {
+ .name = "mode",
+ .mode = COMMAND_ANY,
+ .jim_handler = jim_command_mode,
+ .usage = "[command_name ...]",
+ .help = "Returns the command modes allowed by a command:"
+ "'any', 'config', or 'exec'. If no command is"
+ "specified, returns the current command mode. "
+ "Returns 'unknown' if an unknown command is given. "
+ "Command can be multiple tokens.",
+ },
+ {
+ .name = "type",
+ .mode = COMMAND_ANY,
+ .jim_handler = jim_command_type,
+ .usage = "command_name [...]",
+ .help = "Returns the type of built-in command:"
+ "'native', 'simple', 'group', or 'unknown'. "
+ "Command can be multiple tokens.",
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
+static const struct command_registration command_builtin_handlers[] = {
+ {
+ .name = "echo",
+ .handler = jim_echo,
+ .mode = COMMAND_ANY,
+ .help = "Logs a message at \"user\" priority. "
+ "Output message to stdout. "
+ "Option \"-n\" suppresses trailing newline",
+ .usage = "[-n] string",
+ },
+ {
+ .name = "add_help_text",
+ .handler = handle_help_add_command,
+ .mode = COMMAND_ANY,
+ .help = "Add new command help text; "
+ "Command can be multiple tokens.",
+ .usage = "command_name helptext_string",
+ },
+ {
+ .name = "add_usage_text",
+ .handler = handle_help_add_command,
+ .mode = COMMAND_ANY,
+ .help = "Add new command usage text; "
+ "command can be multiple tokens.",
+ .usage = "command_name usage_string",
+ },
+ {
+ .name = "sleep",
+ .handler = handle_sleep_command,
+ .mode = COMMAND_ANY,
+ .help = "Sleep for specified number of milliseconds. "
+ "\"busy\" will busy wait instead (avoid this).",
+ .usage = "milliseconds ['busy']",
+ },
+ {
+ .name = "help",
+ .handler = handle_help_command,
+ .mode = COMMAND_ANY,
+ .help = "Show full command help; "
+ "command can be multiple tokens.",
+ .usage = "[command_name]",
+ },
+ {
+ .name = "usage",
+ .handler = handle_help_command,
+ .mode = COMMAND_ANY,
+ .help = "Show basic command usage; "
+ "command can be multiple tokens.",
+ .usage = "[command_name]",
+ },
+ {
+ .name = "command",
+ .mode = COMMAND_ANY,
+ .help = "core command group (introspection)",
+ .chain = command_subcommand_handlers,
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
+struct command_context *command_init(const char *startup_tcl, Jim_Interp *interp)
+{
+ struct command_context *context = malloc(sizeof(struct command_context));
+ const char *HostOs;