+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);
+
+ /* register subsystem commands */
+ typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
+ static const command_registrant_t command_registrants[] = {
+ &openocd_register_commands,
+ &server_register_commands,
+ &gdb_register_commands,
+ &log_register_commands,
+ &transport_register_commands,
+ &interface_register_commands,
+ &target_register_commands,
+ &flash_register_commands,
+ &nand_register_commands,
+ &pld_register_commands,
+ &mflash_register_commands,
+ NULL
+ };
+ for (unsigned i = 0; NULL != command_registrants[i]; i++) {
+ int retval = (*command_registrants[i])(cmd_ctx);
+ if (ERROR_OK != retval) {
+ command_done(cmd_ctx);
+ return NULL;
+ }
+ }
+ LOG_DEBUG("command registration: complete");
+
+ LOG_OUTPUT(OPENOCD_VERSION "\n"
+ "Licensed under GNU GPL v2\n");
+
+ global_cmd_ctx = cmd_ctx;
+
+ return cmd_ctx;
+}
+
+/** OpenOCD runtime meat that can become single-thread in future. It parse
+ * commandline, reads configuration, sets up the target and starts server loop.
+ * Commandline arguments are passed into this function from openocd_main().
+ */
+static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
+{
+ int ret;
+
+ if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
+ return ERROR_FAIL;
+
+ if (server_preinit() != ERROR_OK)
+ return ERROR_FAIL;
+
+ ret = parse_config_file(cmd_ctx);
+ if (ret == ERROR_COMMAND_CLOSE_CONNECTION)
+ return ERROR_OK;
+ else if (ret != ERROR_OK)
+ return ERROR_FAIL;
+
+ ret = server_init(cmd_ctx);
+ if (ERROR_OK != ret)
+ return ERROR_FAIL;
+
+ if (init_at_startup) {
+ ret = command_run_line(cmd_ctx, "init");
+ if (ERROR_OK != ret)
+ return ERROR_FAIL;
+ }
+
+ ret = server_loop(cmd_ctx);
+
+ int last_signal = server_quit();
+ if (last_signal != ERROR_OK)
+ return last_signal;
+
+ if (ret != ERROR_OK)
+ return ERROR_FAIL;
+ return ERROR_OK;
+}
+
+/* normally this is the main() function entry, but if OpenOCD is linked
+ * into application, then this fn will not be invoked, but rather that
+ * application will have it's own implementation of main(). */
+int openocd_main(int argc, char *argv[])
+{
+ int ret;
+
+ /* initialize commandline interface */
+ struct command_context *cmd_ctx;
+
+ cmd_ctx = setup_command_handler(NULL);
+
+ if (util_init(cmd_ctx) != ERROR_OK)