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 #ifdef HAVE_SYS_SYSCTL_H
38 #include <sys/sysctl.h>
41 static int help_flag, version_flag;
43 static const struct option long_options[] = {
44 {"help", no_argument, &help_flag, 1},
45 {"version", no_argument, &version_flag, 1},
46 {"debug", optional_argument, 0, 'd'},
47 {"file", required_argument, 0, 'f'},
48 {"search", required_argument, 0, 's'},
49 {"log_output", required_argument, 0, 'l'},
50 {"command", required_argument, 0, 'c'},
51 {"pipe", no_argument, 0, 'p'},
55 int configuration_output_handler(struct command_context *context, const char *line)
57 LOG_USER_N("%s", line);
62 /* Return the canonical path to the directory the openocd executable is in.
63 * The path should be absolute, use / as path separator and have all symlinks
64 * resolved. The returned string is malloc'd. */
65 static char *find_exe_path(void)
70 #if IS_WIN32 && !IS_CYGWIN
71 exepath = malloc(MAX_PATH);
74 GetModuleFileName(NULL, exepath, MAX_PATH);
76 /* Convert path separators to UNIX style, should work on Windows also. */
77 for (char *p = exepath; *p; p++) {
83 exepath = malloc(PROC_PIDPATHINFO_MAXSIZE);
86 if (proc_pidpath(getpid(), exepath, PROC_PIDPATHINFO_MAXSIZE) <= 0) {
91 #elif defined(CTL_KERN) && defined(KERN_PROC) && defined(KERN_PROC_PATHNAME) /* *BSD */
95 char *path = malloc(PATH_MAX);
98 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
99 size_t size = PATH_MAX;
101 if (sysctl(mib, (u_int)ARRAY_SIZE(mib), path, &size, NULL, 0) != 0)
105 exepath = realpath(path, NULL);
111 #elif defined(HAVE_REALPATH) /* Assume POSIX.1-2008 */
112 /* Try Unices in order of likelihood. */
113 exepath = realpath("/proc/self/exe", NULL); /* Linux/Cygwin */
115 exepath = realpath("/proc/self/path/a.out", NULL); /* Solaris */
117 exepath = realpath("/proc/curproc/file", NULL); /* FreeBSD (Should be covered above) */
121 if (exepath != NULL) {
122 /* Strip executable file name, leaving path */
123 *strrchr(exepath, '/') = '\0';
125 LOG_WARNING("Could not determine executable path, using configured BINDIR.");
126 LOG_DEBUG("BINDIR = %s", BINDIR);
128 exepath = realpath(BINDIR, NULL);
130 exepath = strdup(BINDIR);
137 static char *find_relative_path(const char *from, const char *to)
141 /* Skip common /-separated parts of from and to */
143 for (size_t n = 0; from[n] == to[n]; n++) {
144 if (from[n] == '\0') {
154 /* Count number of /-separated non-empty parts of from */
156 while (from[0] != '\0') {
159 char *next = strchr(from, '/');
165 /* Prepend that number of ../ in front of to */
166 char *relpath = malloc(i * 3 + strlen(to) + 1);
168 for (size_t n = 0; n < i; n++)
169 strcat(relpath, "../");
175 static void add_default_dirs(void)
178 char *exepath = find_exe_path();
179 char *bin2data = find_relative_path(BINDIR, PKGDATADIR);
181 LOG_DEBUG("bindir=%s", BINDIR);
182 LOG_DEBUG("pkgdatadir=%s", PKGDATADIR);
183 LOG_DEBUG("exepath=%s", exepath);
184 LOG_DEBUG("bin2data=%s", bin2data);
187 * The directory containing OpenOCD-supplied scripts should be
188 * listed last in the built-in search order, so the user can
189 * override these scripts with site-specific customizations.
191 const char *home = getenv("HOME");
194 path = alloc_printf("%s/.openocd", home);
196 add_script_search_dir(path);
201 path = getenv("OPENOCD_SCRIPTS");
204 add_script_search_dir(path);
207 const char *appdata = getenv("APPDATA");
210 path = alloc_printf("%s/OpenOCD", appdata);
212 add_script_search_dir(path);
218 path = alloc_printf("%s/%s/%s", exepath, bin2data, "site");
220 add_script_search_dir(path);
224 path = alloc_printf("%s/%s/%s", exepath, bin2data, "scripts");
226 add_script_search_dir(path);
234 int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
239 /* getopt_long stores the option index here. */
240 int option_index = 0;
242 c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
244 /* Detect the end of the options. */
251 case 'h': /* --help | -h */
254 case 'v': /* --version | -v */
257 case 'f': /* --file | -f */
259 char *command = alloc_printf("script {%s}", optarg);
260 add_config_command(command);
264 case 's': /* --search | -s */
265 add_script_search_dir(optarg);
267 case 'd': /* --debug | -d */
269 char *command = alloc_printf("debug_level %s", optarg ? optarg : "3");
270 command_run_line(cmd_ctx, command);
274 case 'l': /* --log_output | -l */
276 char *command = alloc_printf("log_output %s", optarg);
277 command_run_line(cmd_ctx, command);
281 case 'c': /* --command | -c */
283 add_config_command(optarg);
286 /* to replicate the old syntax this needs to be synchronous
287 * otherwise the gdb stdin will overflow with the warning message */
288 command_run_line(cmd_ctx, "gdb_port pipe; log_output openocd.log");
289 LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; "
290 "log_output openocd.log\"' instead.");
296 LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
297 LOG_OUTPUT("--help | -h\tdisplay this help\n");
298 LOG_OUTPUT("--version | -v\tdisplay OpenOCD version\n");
299 LOG_OUTPUT("--file | -f\tuse configuration file <name>\n");
300 LOG_OUTPUT("--search | -s\tdir to search for config files and scripts\n");
301 LOG_OUTPUT("--debug | -d\tset debug level <0-3>\n");
302 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
303 LOG_OUTPUT("--command | -c\trun <command>\n");
308 /* Nothing to do, version gets printed automatically. */
309 /* It is not an error to request the VERSION number. */
313 /* paths specified on the command line take precedence over these