1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
26 #include "configuration.h"
37 /* sys/sysctl.h is deprecated on Linux from glibc 2.30 */
39 #ifdef HAVE_SYS_SYSCTL_H
40 #include <sys/sysctl.h>
43 #if IS_WIN32 && !IS_CYGWIN
47 static int help_flag, version_flag;
49 static const struct option long_options[] = {
50 {"help", no_argument, &help_flag, 1},
51 {"version", no_argument, &version_flag, 1},
52 {"debug", optional_argument, 0, 'd'},
53 {"file", required_argument, 0, 'f'},
54 {"search", required_argument, 0, 's'},
55 {"log_output", required_argument, 0, 'l'},
56 {"command", required_argument, 0, 'c'},
60 int configuration_output_handler(struct command_context *context, const char *line)
62 LOG_USER_N("%s", line);
67 /* Return the canonical path to the directory the openocd executable is in.
68 * The path should be absolute, use / as path separator and have all symlinks
69 * resolved. The returned string is malloc'd. */
70 static char *find_exe_path(void)
75 #if IS_WIN32 && !IS_CYGWIN
76 exepath = malloc(MAX_PATH);
79 GetModuleFileName(NULL, exepath, MAX_PATH);
81 /* Convert path separators to UNIX style, should work on Windows also. */
82 for (char *p = exepath; *p; p++) {
88 exepath = malloc(PROC_PIDPATHINFO_MAXSIZE);
91 if (proc_pidpath(getpid(), exepath, PROC_PIDPATHINFO_MAXSIZE) <= 0) {
96 #elif defined(CTL_KERN) && defined(KERN_PROC) && defined(KERN_PROC_PATHNAME) /* *BSD */
100 char *path = malloc(PATH_MAX);
103 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
104 size_t size = PATH_MAX;
106 if (sysctl(mib, (u_int)ARRAY_SIZE(mib), path, &size, NULL, 0) != 0)
110 exepath = realpath(path, NULL);
116 #elif defined(HAVE_REALPATH) /* Assume POSIX.1-2008 */
117 /* Try Unices in order of likelihood. */
118 exepath = realpath("/proc/self/exe", NULL); /* Linux/Cygwin */
120 exepath = realpath("/proc/self/path/a.out", NULL); /* Solaris */
122 exepath = realpath("/proc/curproc/file", NULL); /* FreeBSD (Should be covered above) */
126 if (exepath != NULL) {
127 /* Strip executable file name, leaving path */
128 *strrchr(exepath, '/') = '\0';
130 LOG_WARNING("Could not determine executable path, using configured BINDIR.");
131 LOG_DEBUG("BINDIR = %s", BINDIR);
133 exepath = realpath(BINDIR, NULL);
135 exepath = strdup(BINDIR);
142 static char *find_relative_path(const char *from, const char *to)
146 /* Skip common /-separated parts of from and to */
148 for (size_t n = 0; from[n] == to[n]; n++) {
149 if (from[n] == '\0') {
159 /* Count number of /-separated non-empty parts of from */
161 while (from[0] != '\0') {
164 char *next = strchr(from, '/');
170 /* Prepend that number of ../ in front of to */
171 char *relpath = malloc(i * 3 + strlen(to) + 1);
173 for (size_t n = 0; n < i; n++)
174 strcat(relpath, "../");
180 static void add_user_dirs(void)
185 const char *appdata = getenv("APPDATA");
188 path = alloc_printf("%s/OpenOCD", appdata);
190 /* Convert path separators to UNIX style, should work on Windows also. */
191 for (char *p = path; *p; p++) {
195 add_script_search_dir(path);
199 /* WIN32 may also have HOME defined, particularly under Cygwin, so add those paths below too */
202 const char *home = getenv("HOME");
205 path = alloc_printf("%s/Library/Preferences/org.openocd", home);
207 add_script_search_dir(path);
212 const char *xdg_config = getenv("XDG_CONFIG_HOME");
215 path = alloc_printf("%s/openocd", xdg_config);
217 add_script_search_dir(path);
221 path = alloc_printf("%s/.config/openocd", home);
223 add_script_search_dir(path);
229 path = alloc_printf("%s/.openocd", home);
231 add_script_search_dir(path);
237 static void add_default_dirs(void)
240 char *exepath = find_exe_path();
241 char *bin2data = find_relative_path(BINDIR, PKGDATADIR);
243 LOG_DEBUG("bindir=%s", BINDIR);
244 LOG_DEBUG("pkgdatadir=%s", PKGDATADIR);
245 LOG_DEBUG("exepath=%s", exepath);
246 LOG_DEBUG("bin2data=%s", bin2data);
249 * The directory containing OpenOCD-supplied scripts should be
250 * listed last in the built-in search order, so the user can
251 * override these scripts with site-specific customizations.
253 path = getenv("OPENOCD_SCRIPTS");
255 add_script_search_dir(path);
259 path = alloc_printf("%s/%s/%s", exepath, bin2data, "site");
261 add_script_search_dir(path);
265 path = alloc_printf("%s/%s/%s", exepath, bin2data, "scripts");
267 add_script_search_dir(path);
275 int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
280 /* getopt_long stores the option index here. */
281 int option_index = 0;
283 c = getopt_long(argc, argv, "hvd::l:f:s:c:", long_options, &option_index);
285 /* Detect the end of the options. */
292 case 'h': /* --help | -h */
295 case 'v': /* --version | -v */
298 case 'f': /* --file | -f */
300 char *command = alloc_printf("script {%s}", optarg);
301 add_config_command(command);
305 case 's': /* --search | -s */
306 add_script_search_dir(optarg);
308 case 'd': /* --debug | -d */
310 int retval = command_run_linef(cmd_ctx, "debug_level %s", optarg ? optarg : "3");
311 if (retval != ERROR_OK)
315 case 'l': /* --log_output | -l */
317 command_run_linef(cmd_ctx, "log_output %s", optarg);
319 case 'c': /* --command | -c */
321 add_config_command(optarg);
324 /* getopt will emit an error message, all we have to do is bail. */
330 /* Catch extra arguments on the command line. */
331 LOG_OUTPUT("Unexpected command line argument: %s\n", argv[optind]);
336 LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
337 LOG_OUTPUT("--help | -h\tdisplay this help\n");
338 LOG_OUTPUT("--version | -v\tdisplay OpenOCD version\n");
339 LOG_OUTPUT("--file | -f\tuse configuration file <name>\n");
340 LOG_OUTPUT("--search | -s\tdir to search for config files and scripts\n");
341 LOG_OUTPUT("--debug | -d\tset debug level to 3\n");
342 LOG_OUTPUT(" | -d<n>\tset debug level to <level>\n");
343 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
344 LOG_OUTPUT("--command | -c\trun <command>\n");
349 /* Nothing to do, version gets printed automatically. */
350 /* It is not an error to request the VERSION number. */
354 /* paths specified on the command line take precedence over these