UserGuide: Fixed link to ST flashlink.
[openocd.git] / src / helper / options.c
blob98cd634bc2cd3c4613c3dbfddcdede1b68451dc0
1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
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. *
12 * *
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. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include "configuration.h"
29 /* @todo the inclusion of server.h here is a layering violation */
30 #include <server/server.h>
32 #include <getopt.h>
34 static int help_flag, version_flag;
36 static const struct option long_options[] = {
37 {"help", no_argument, &help_flag, 1},
38 {"version", no_argument, &version_flag, 1},
39 {"debug", optional_argument, 0, 'd'},
40 {"file", required_argument, 0, 'f'},
41 {"search", required_argument, 0, 's'},
42 {"log_output", required_argument, 0, 'l'},
43 {"command", required_argument, 0, 'c'},
44 {"pipe", no_argument, 0, 'p'},
45 {0, 0, 0, 0}
48 int configuration_output_handler(struct command_context *context, const char *line)
50 LOG_USER_N("%s", line);
52 return ERROR_OK;
55 static void add_default_dirs(void)
57 #ifdef _WIN32
58 /* Add the parent of the directory where openocd.exe resides to the
59 * config script search path.
60 * Directory layout:
61 * bin\openocd.exe
62 * lib\openocd
65 char strExePath[MAX_PATH];
66 GetModuleFileName(NULL, strExePath, MAX_PATH);
67 /* Either this code will *always* work or it will SEGFAULT giving
68 * excellent information on the culprit.
70 *strrchr(strExePath, '\\') = 0;
71 strcat(strExePath, "\\..");
72 add_script_search_dir(strExePath);
75 * Add support for the default (as of 20091118) layout when
76 * using autotools and cygwin/MinGW to build native binary.
77 * Path separator is converted to UNIX style so that MinGW is
78 * pleased.
80 * bin/openocd.exe
81 * share/openocd/scripts/interface/dummy.cfg
82 * share/openocd/scripts/target/at91eb40a.cfg
85 char strExePath[MAX_PATH];
86 char *p;
87 GetModuleFileName(NULL, strExePath, MAX_PATH);
88 *strrchr(strExePath, '\\') = 0;
89 strcat(strExePath, "/../share/"PACKAGE "/scripts");
90 for (p = strExePath; *p; p++) {
91 if (*p == '\\')
92 *p = '/';
94 add_script_search_dir(strExePath);
96 #else
98 * The directory containing OpenOCD-supplied scripts should be
99 * listed last in the built-in search order, so the user can
100 * override these scripts with site-specific customizations.
103 const char *home = getenv("HOME");
105 if (home) {
106 char *path;
108 path = alloc_printf("%s/.openocd", home);
110 if (path) {
111 add_script_search_dir(path);
112 free(path);
116 add_script_search_dir(PKGDATADIR "/site");
117 add_script_search_dir(PKGDATADIR "/scripts");
118 #endif
121 int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
123 int c;
124 char command_buffer[128];
126 while (1) {
127 /* getopt_long stores the option index here. */
128 int option_index = 0;
130 c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
132 /* Detect the end of the options. */
133 if (c == -1)
134 break;
136 switch (c) {
137 case 0:
138 break;
139 case 'h': /* --help | -h */
140 help_flag = 1;
141 break;
142 case 'v': /* --version | -v */
143 version_flag = 1;
144 break;
145 case 'f': /* --file | -f */
147 snprintf(command_buffer, 128, "script {%s}", optarg);
148 add_config_command(command_buffer);
149 break;
151 case 's': /* --search | -s */
152 add_script_search_dir(optarg);
153 break;
154 case 'd': /* --debug | -d */
155 if (optarg)
156 snprintf(command_buffer, 128, "debug_level %s", optarg);
157 else
158 snprintf(command_buffer, 128, "debug_level 3");
159 command_run_line(cmd_ctx, command_buffer);
160 break;
161 case 'l': /* --log_output | -l */
162 if (optarg) {
163 snprintf(command_buffer, 128, "log_output %s", optarg);
164 command_run_line(cmd_ctx, command_buffer);
166 break;
167 case 'c': /* --command | -c */
168 if (optarg)
169 add_config_command(optarg);
170 break;
171 case 'p':
172 /* to replicate the old syntax this needs to be synchronous
173 * otherwise the gdb stdin will overflow with the warning message */
174 command_run_line(cmd_ctx, "gdb_port pipe; log_output openocd.log");
175 LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; "
176 "log_output openocd.log\"' instead.");
177 break;
181 if (help_flag) {
182 LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
183 LOG_OUTPUT("--help | -h\tdisplay this help\n");
184 LOG_OUTPUT("--version | -v\tdisplay OpenOCD version\n");
185 LOG_OUTPUT("--file | -f\tuse configuration file <name>\n");
186 LOG_OUTPUT("--search | -s\tdir to search for config files and scripts\n");
187 LOG_OUTPUT("--debug | -d\tset debug level <0-3>\n");
188 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
189 LOG_OUTPUT("--command | -c\trun <command>\n");
190 exit(-1);
193 if (version_flag) {
194 /* Nothing to do, version gets printed automatically. */
195 /* It is not an error to request the VERSION number. */
196 exit(0);
199 /* paths specified on the command line take precedence over these
200 * built-in paths
202 add_default_dirs();
204 return ERROR_OK;