tcl: replace FSF boilerplate with SPDX tag
[fw/openocd] / src / helper / options.c
index 6622ece6ca2c076854301517e7aef959ab500f52..199672789b865e16185dfd2eee1da02df6a936b4 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <limits.h>
 #include <stdlib.h>
+#include <string.h>
 #if IS_DARWIN
 #include <libproc.h>
 #endif
@@ -54,7 +55,6 @@ static const struct option long_options[] = {
        {"search",              required_argument,              0,                              's'},
        {"log_output",  required_argument,              0,                              'l'},
        {"command",             required_argument,              0,                              'c'},
-       {"pipe",                no_argument,                    0,                              'p'},
        {0, 0, 0, 0}
 };
 
@@ -75,7 +75,7 @@ static char *find_exe_path(void)
        do {
 #if IS_WIN32 && !IS_CYGWIN
                exepath = malloc(MAX_PATH);
-               if (exepath == NULL)
+               if (!exepath)
                        break;
                GetModuleFileName(NULL, exepath, MAX_PATH);
 
@@ -87,7 +87,7 @@ static char *find_exe_path(void)
 
 #elif IS_DARWIN
                exepath = malloc(PROC_PIDPATHINFO_MAXSIZE);
-               if (exepath == NULL)
+               if (!exepath)
                        break;
                if (proc_pidpath(getpid(), exepath, PROC_PIDPATHINFO_MAXSIZE) <= 0) {
                        free(exepath);
@@ -99,7 +99,7 @@ static char *find_exe_path(void)
 #define PATH_MAX 1024
 #endif
                char *path = malloc(PATH_MAX);
-               if (path == NULL)
+               if (!path)
                        break;
                int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
                size_t size = PATH_MAX;
@@ -117,14 +117,14 @@ static char *find_exe_path(void)
 #elif defined(HAVE_REALPATH) /* Assume POSIX.1-2008 */
                /* Try Unices in order of likelihood. */
                exepath = realpath("/proc/self/exe", NULL); /* Linux/Cygwin */
-               if (exepath == NULL)
+               if (!exepath)
                        exepath = realpath("/proc/self/path/a.out", NULL); /* Solaris */
-               if (exepath == NULL)
+               if (!exepath)
                        exepath = realpath("/proc/curproc/file", NULL); /* FreeBSD (Should be covered above) */
 #endif
        } while (0);
 
-       if (exepath != NULL) {
+       if (exepath) {
                /* Strip executable file name, leaving path */
                *strrchr(exepath, '/') = '\0';
        } else {
@@ -163,7 +163,7 @@ static char *find_relative_path(const char *from, const char *to)
                if (from[0] != '/')
                        i++;
                char *next = strchr(from, '/');
-               if (next == NULL)
+               if (!next)
                        break;
                from = next + 1;
        }
@@ -178,6 +178,63 @@ static char *find_relative_path(const char *from, const char *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;
@@ -194,32 +251,11 @@ static void add_default_dirs(void)
         * listed last in the built-in search order, so the user can
         * override these scripts with site-specific customizations.
         */
-       const char *home = getenv("HOME");
-
-       if (home) {
-               path = alloc_printf("%s/.openocd", home);
-               if (path) {
-                       add_script_search_dir(path);
-                       free(path);
-               }
-       }
-
        path = getenv("OPENOCD_SCRIPTS");
-
        if (path)
                add_script_search_dir(path);
 
-#ifdef _WIN32
-       const char *appdata = getenv("APPDATA");
-
-       if (appdata) {
-               path = alloc_printf("%s/OpenOCD", appdata);
-               if (path) {
-                       add_script_search_dir(path);
-                       free(path);
-               }
-       }
-#endif
+       add_user_dirs();
 
        path = alloc_printf("%s/%s/%s", exepath, bin2data, "site");
        if (path) {
@@ -245,7 +281,7 @@ int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
                /* getopt_long stores the option index here. */
                int option_index = 0;
 
-               c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
+               c = getopt_long(argc, argv, "hvd::l:f:s:c:", long_options, &option_index);
 
                /* Detect the end of the options. */
                if (c == -1)
@@ -285,13 +321,6 @@ int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
                                if (optarg)
                                    add_config_command(optarg);
                                break;
-                       case 'p':
-                               /* to replicate the old syntax this needs to be synchronous
-                                * otherwise the gdb stdin will overflow with the warning message */
-                               command_run_line(cmd_ctx, "gdb_port pipe; log_output openocd.log");
-                               LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; "
-                                               "log_output openocd.log\"' instead.");
-                               break;
                        default:  /* '?' */
                                /* getopt will emit an error message, all we have to do is bail. */
                                return ERROR_FAIL;