1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2004, 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 ***************************************************************************/
15 #include "configuration.h"
27 /* sys/sysctl.h is deprecated on Linux from glibc 2.30 */
29 #ifdef HAVE_SYS_SYSCTL_H
30 #include <sys/sysctl.h>
33 #if IS_WIN32 && !IS_CYGWIN
37 static int help_flag
, version_flag
;
39 static const struct option long_options
[] = {
40 {"help", no_argument
, &help_flag
, 1},
41 {"version", no_argument
, &version_flag
, 1},
42 {"debug", optional_argument
, NULL
, 'd'},
43 {"file", required_argument
, NULL
, 'f'},
44 {"search", required_argument
, NULL
, 's'},
45 {"log_output", required_argument
, NULL
, 'l'},
46 {"command", required_argument
, NULL
, 'c'},
50 int configuration_output_handler(struct command_context
*context
, const char *line
)
52 LOG_USER_N("%s", line
);
57 /* Return the canonical path to the directory the openocd executable is in.
58 * The path should be absolute, use / as path separator and have all symlinks
59 * resolved. The returned string is malloc'd. */
60 static char *find_exe_path(void)
65 #if IS_WIN32 && !IS_CYGWIN
66 exepath
= malloc(MAX_PATH
);
69 GetModuleFileName(NULL
, exepath
, MAX_PATH
);
71 /* Convert path separators to UNIX style, should work on Windows also. */
72 for (char *p
= exepath
; *p
; p
++) {
78 exepath
= malloc(PROC_PIDPATHINFO_MAXSIZE
);
81 if (proc_pidpath(getpid(), exepath
, PROC_PIDPATHINFO_MAXSIZE
) <= 0) {
86 #elif defined(CTL_KERN) && defined(KERN_PROC) && defined(KERN_PROC_PATHNAME) /* *BSD */
90 char *path
= malloc(PATH_MAX
);
93 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
94 size_t size
= PATH_MAX
;
96 if (sysctl(mib
, (u_int
)ARRAY_SIZE(mib
), path
, &size
, NULL
, 0) != 0)
100 exepath
= realpath(path
, NULL
);
106 #elif defined(HAVE_REALPATH) /* Assume POSIX.1-2008 */
107 /* Try Unices in order of likelihood. */
108 exepath
= realpath("/proc/self/exe", NULL
); /* Linux/Cygwin */
110 exepath
= realpath("/proc/self/path/a.out", NULL
); /* Solaris */
112 exepath
= realpath("/proc/curproc/file", NULL
); /* FreeBSD (Should be covered above) */
117 /* Strip executable file name, leaving path */
118 *strrchr(exepath
, '/') = '\0';
120 LOG_WARNING("Could not determine executable path, using configured BINDIR.");
121 LOG_DEBUG("BINDIR = %s", BINDIR
);
123 exepath
= realpath(BINDIR
, NULL
);
125 exepath
= strdup(BINDIR
);
132 static char *find_relative_path(const char *from
, const char *to
)
136 /* Skip common /-separated parts of from and to */
138 for (size_t n
= 0; from
[n
] == to
[n
]; n
++) {
139 if (from
[n
] == '\0') {
149 /* Count number of /-separated non-empty parts of from */
151 while (from
[0] != '\0') {
154 char *next
= strchr(from
, '/');
160 /* Prepend that number of ../ in front of to */
161 char *relpath
= malloc(i
* 3 + strlen(to
) + 1);
163 for (size_t n
= 0; n
< i
; n
++)
164 strcat(relpath
, "../");
170 static void add_user_dirs(void)
175 const char *appdata
= getenv("APPDATA");
178 path
= alloc_printf("%s/OpenOCD", appdata
);
180 /* Convert path separators to UNIX style, should work on Windows also. */
181 for (char *p
= path
; *p
; p
++) {
185 add_script_search_dir(path
);
189 /* WIN32 may also have HOME defined, particularly under Cygwin, so add those paths below too */
192 const char *home
= getenv("HOME");
195 path
= alloc_printf("%s/Library/Preferences/org.openocd", home
);
197 add_script_search_dir(path
);
202 const char *xdg_config
= getenv("XDG_CONFIG_HOME");
205 path
= alloc_printf("%s/openocd", xdg_config
);
207 add_script_search_dir(path
);
211 path
= alloc_printf("%s/.config/openocd", home
);
213 add_script_search_dir(path
);
219 path
= alloc_printf("%s/.openocd", home
);
221 add_script_search_dir(path
);
227 static void add_default_dirs(void)
230 char *exepath
= find_exe_path();
231 char *bin2data
= find_relative_path(BINDIR
, PKGDATADIR
);
233 LOG_DEBUG("bindir=%s", BINDIR
);
234 LOG_DEBUG("pkgdatadir=%s", PKGDATADIR
);
235 LOG_DEBUG("exepath=%s", exepath
);
236 LOG_DEBUG("bin2data=%s", bin2data
);
239 * The directory containing OpenOCD-supplied scripts should be
240 * listed last in the built-in search order, so the user can
241 * override these scripts with site-specific customizations.
243 path
= getenv("OPENOCD_SCRIPTS");
245 add_script_search_dir(path
);
249 path
= alloc_printf("%s/%s/%s", exepath
, bin2data
, "site");
251 add_script_search_dir(path
);
255 path
= alloc_printf("%s/%s/%s", exepath
, bin2data
, "scripts");
257 add_script_search_dir(path
);
265 int parse_cmdline_args(struct command_context
*cmd_ctx
, int argc
, char *argv
[])
270 /* getopt_long stores the option index here. */
271 int option_index
= 0;
273 c
= getopt_long(argc
, argv
, "hvd::l:f:s:c:", long_options
, &option_index
);
275 /* Detect the end of the options. */
282 case 'h': /* --help | -h */
285 case 'v': /* --version | -v */
288 case 'f': /* --file | -f */
290 char *command
= alloc_printf("script {%s}", optarg
);
291 add_config_command(command
);
295 case 's': /* --search | -s */
296 add_script_search_dir(optarg
);
298 case 'd': /* --debug | -d */
300 int retval
= command_run_linef(cmd_ctx
, "debug_level %s", optarg
? optarg
: "3");
301 if (retval
!= ERROR_OK
)
305 case 'l': /* --log_output | -l */
307 command_run_linef(cmd_ctx
, "log_output %s", optarg
);
309 case 'c': /* --command | -c */
311 add_config_command(optarg
);
314 /* getopt will emit an error message, all we have to do is bail. */
320 /* Catch extra arguments on the command line. */
321 LOG_OUTPUT("Unexpected command line argument: %s\n", argv
[optind
]);
326 LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
327 LOG_OUTPUT("--help | -h\tdisplay this help\n");
328 LOG_OUTPUT("--version | -v\tdisplay OpenOCD version\n");
329 LOG_OUTPUT("--file | -f\tuse configuration file <name>\n");
330 LOG_OUTPUT("--search | -s\tdir to search for config files and scripts\n");
331 LOG_OUTPUT("--debug | -d\tset debug level to 3\n");
332 LOG_OUTPUT(" | -d<n>\tset debug level to <level>\n");
333 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
334 LOG_OUTPUT("--command | -c\trun <command>\n");
339 /* Nothing to do, version gets printed automatically. */
340 /* It is not an error to request the VERSION number. */
344 /* dump full command line */
345 for (int i
= 0; i
< argc
; i
++)
346 LOG_DEBUG("ARGV[%d] = \"%s\"", i
, argv
[i
]);
348 /* paths specified on the command line take precedence over these