+ if (CMD_ARGC != 0)
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ int retval;
+ static int initialized;
+ if (initialized)
+ return ERROR_OK;
+
+ initialized = 1;
+
+ retval = command_run_line(CMD_CTX, "target init");
+ if (ERROR_OK != retval)
+ return ERROR_FAIL;
+
+ retval = adapter_init(CMD_CTX);
+ if (retval != ERROR_OK) {
+ /* we must be able to set up the debug adapter */
+ return retval;
+ }
+
+ LOG_DEBUG("Debug Adapter init complete");
+
+ /* "transport init" verifies the expected devices are present;
+ * for JTAG, it checks the list of configured TAPs against
+ * what's discoverable, possibly with help from the platform's
+ * JTAG event handlers. (which require COMMAND_EXEC)
+ */
+ command_context_mode(CMD_CTX, COMMAND_EXEC);
+
+ retval = command_run_line(CMD_CTX, "transport init");
+ if (ERROR_OK != retval)
+ return ERROR_FAIL;
+
+ retval = command_run_line(CMD_CTX, "dap init");
+ if (ERROR_OK != retval)
+ return ERROR_FAIL;
+
+ LOG_DEBUG("Examining targets...");
+ if (target_examine() != ERROR_OK)
+ LOG_DEBUG("target examination failed");
+
+ command_context_mode(CMD_CTX, COMMAND_CONFIG);
+
+ if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
+ return ERROR_FAIL;
+
+ if (command_run_line(CMD_CTX, "mflash init") != ERROR_OK)
+ return ERROR_FAIL;
+
+ if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
+ return ERROR_FAIL;
+
+ if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
+ return ERROR_FAIL;
+ command_context_mode(CMD_CTX, COMMAND_EXEC);
+
+ /* initialize telnet subsystem */
+ gdb_target_add_all(all_targets);
+
+ target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
+
+ return ERROR_OK;
+}
+
+COMMAND_HANDLER(handle_add_script_search_dir_command)
+{
+ if (CMD_ARGC != 1)
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ add_script_search_dir(CMD_ARGV[0]);
+
+ return ERROR_OK;
+}
+
+static const struct command_registration openocd_command_handlers[] = {
+ {
+ .name = "version",
+ .jim_handler = jim_version_command,
+ .mode = COMMAND_ANY,
+ .help = "show program version",
+ },
+ {
+ .name = "noinit",
+ .handler = &handle_noinit_command,
+ .mode = COMMAND_CONFIG,
+ .help = "Prevent 'init' from being called at startup.",
+ .usage = ""
+ },
+ {
+ .name = "init",
+ .handler = &handle_init_command,
+ .mode = COMMAND_ANY,
+ .help = "Initializes configured targets and servers. "
+ "Changes command mode from CONFIG to EXEC. "
+ "Unless 'noinit' is called, this command is "
+ "called automatically at the end of startup.",
+ .usage = ""
+ },
+ {
+ .name = "add_script_search_dir",
+ .handler = &handle_add_script_search_dir_command,
+ .mode = COMMAND_ANY,
+ .help = "dir to search for config files and scripts",
+ .usage = "<directory>"
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
+static int openocd_register_commands(struct command_context *cmd_ctx)
+{
+ return register_commands(cmd_ctx, NULL, openocd_command_handlers);
+}
+
+struct command_context *global_cmd_ctx;
+
+/* NB! this fn can be invoked outside this file for non PC hosted builds
+ * NB! do not change to 'static'!!!!
+ */
+struct command_context *setup_command_handler(Jim_Interp *interp)
+{
+ log_init();
+ LOG_DEBUG("log_init: complete");
+
+ struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);