+static char *find_relative_path(const char *from, const char *to)
+{
+ size_t i;
+
+ /* Skip common /-separated parts of from and to */
+ i = 0;
+ for (size_t n = 0; from[n] == to[n]; n++) {
+ if (from[n] == '\0') {
+ i = n;
+ break;
+ }
+ if (from[n] == '/')
+ i = n + 1;
+ }
+ from += i;
+ to += i;
+
+ /* Count number of /-separated non-empty parts of from */
+ i = 0;
+ while (from[0] != '\0') {
+ if (from[0] != '/')
+ i++;
+ char *next = strchr(from, '/');
+ if (!next)
+ break;
+ from = next + 1;
+ }
+
+ /* Prepend that number of ../ in front of to */
+ char *relpath = malloc(i * 3 + strlen(to) + 1);
+ relpath[0] = '\0';
+ for (size_t n = 0; n < i; n++)
+ strcat(relpath, "../");
+ strcat(relpath, to);
+
+ return relpath;
+}
+
+static void add_user_dirs(void)
+{
+ char *path;
+
+#if IS_WIN32
+ const char *appdata = getenv("APPDATA");
+
+ if (appdata) {
+ path = alloc_printf("%s/OpenOCD", appdata);
+ if (path) {
+ /* Convert path separators to UNIX style, should work on Windows also. */
+ for (char *p = path; *p; p++) {
+ if (*p == '\\')
+ *p = '/';
+ }
+ add_script_search_dir(path);
+ free(path);
+ }
+ }
+ /* WIN32 may also have HOME defined, particularly under Cygwin, so add those paths below too */
+#endif
+
+ const char *home = getenv("HOME");
+#if IS_DARWIN
+ if (home) {
+ path = alloc_printf("%s/Library/Preferences/org.openocd", home);
+ if (path) {
+ add_script_search_dir(path);
+ free(path);
+ }
+ }
+#endif
+ const char *xdg_config = getenv("XDG_CONFIG_HOME");
+
+ if (xdg_config) {
+ path = alloc_printf("%s/openocd", xdg_config);
+ if (path) {
+ add_script_search_dir(path);
+ free(path);
+ }
+ } else if (home) {
+ path = alloc_printf("%s/.config/openocd", home);
+ if (path) {
+ add_script_search_dir(path);
+ free(path);
+ }
+ }
+
+ if (home) {
+ path = alloc_printf("%s/.openocd", home);
+ if (path) {
+ add_script_search_dir(path);
+ free(path);
+ }
+ }
+}
+
+static void add_default_dirs(void)
+{
+ char *path;
+ char *exepath = find_exe_path();
+ char *bin2data = find_relative_path(BINDIR, PKGDATADIR);
+
+ LOG_DEBUG("bindir=%s", BINDIR);
+ LOG_DEBUG("pkgdatadir=%s", PKGDATADIR);
+ LOG_DEBUG("exepath=%s", exepath);
+ LOG_DEBUG("bin2data=%s", bin2data);
+
+ /*
+ * The directory containing OpenOCD-supplied scripts should be
+ * listed last in the built-in search order, so the user can
+ * override these scripts with site-specific customizations.
+ */
+ path = getenv("OPENOCD_SCRIPTS");
+ if (path)
+ add_script_search_dir(path);
+
+ add_user_dirs();
+
+ path = alloc_printf("%s/%s/%s", exepath, bin2data, "site");
+ if (path) {
+ add_script_search_dir(path);
+ free(path);
+ }
+
+ path = alloc_printf("%s/%s/%s", exepath, bin2data, "scripts");
+ if (path) {
+ add_script_search_dir(path);
+ free(path);
+ }
+
+ free(exepath);
+ free(bin2data);
+}
+
+int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])