]> git.sur5r.net Git - openocd/blob - src/helper/options.c
e6b0f64415884845b70110817e1912dbd3b0dbd2
[openocd] / src / helper / options.c
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  ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "configuration.h"
29 /* @todo the inclusion of server.h here is a layering violation */
30 #include <server/server.h>
31
32 #include <getopt.h>
33
34 static int help_flag, version_flag;
35
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}
46 };
47
48 int configuration_output_handler(struct command_context *context, const char *line)
49 {
50         LOG_USER_N("%s", line);
51
52         return ERROR_OK;
53 }
54
55 static void add_default_dirs(void)
56 {
57 #ifdef _WIN32
58         char strExePath[MAX_PATH];
59         char *path;
60         GetModuleFileName(NULL, strExePath, MAX_PATH);
61
62         /* Strip executable file name, leaving path */
63         *strrchr(strExePath, '\\') = '\0';
64
65         /* Convert path separators to UNIX style, should work on Windows also. */
66         for (char *p = strExePath; *p; p++) {
67                 if (*p == '\\')
68                         *p = '/';
69         }
70
71         /* Add the parent of the directory where openocd.exe resides to the
72          * config script search path.
73          *
74          * bin/openocd.exe
75          * interface/dummy.cfg
76          * target/at91eb40a.cfg
77          */
78         path = alloc_printf("%s%s", strExePath, "/..");
79         if (path) {
80                 add_script_search_dir(path);
81                 free(path);
82         }
83         /* Add support for the directory layout resulting from a 'make install'.
84          *
85          * bin/openocd.exe
86          * share/openocd/scripts/interface/dummy.cfg
87          * share/openocd/scripts/target/at91eb40a.cfg
88          */
89         path = alloc_printf("%s%s", strExePath, "/../share/" PACKAGE "/scripts");
90         if (path) {
91                 add_script_search_dir(path);
92                 free(path);
93         }
94         /* Add single "scripts" folder to search path for Windows OpenOCD builds that don't use cygwin
95          *
96          * bin/openocd.exe
97          * scripts/interface/dummy.cfg
98          * scripts/target/at91eb40a.cfg
99          */
100         path = alloc_printf("%s%s", strExePath, "/../scripts");
101         if (path) {
102                 add_script_search_dir(path);
103                 free(path);
104         }
105 #else
106         /*
107          * The directory containing OpenOCD-supplied scripts should be
108          * listed last in the built-in search order, so the user can
109          * override these scripts with site-specific customizations.
110          */
111
112         const char *home = getenv("HOME");
113
114         if (home) {
115                 char *path;
116
117                 path = alloc_printf("%s/.openocd", home);
118
119                 if (path) {
120                         add_script_search_dir(path);
121                         free(path);
122                 }
123         }
124
125         add_script_search_dir(PKGDATADIR "/site");
126         add_script_search_dir(PKGDATADIR "/scripts");
127 #endif
128 }
129
130 int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
131 {
132         int c;
133         char command_buffer[128];
134
135         while (1) {
136                 /* getopt_long stores the option index here. */
137                 int option_index = 0;
138
139                 c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
140
141                 /* Detect the end of the options. */
142                 if (c == -1)
143                         break;
144
145                 switch (c) {
146                         case 0:
147                                 break;
148                         case 'h':               /* --help | -h */
149                                 help_flag = 1;
150                                 break;
151                         case 'v':               /* --version | -v */
152                                 version_flag = 1;
153                                 break;
154                         case 'f':               /* --file | -f */
155                         {
156                                 snprintf(command_buffer, 128, "script {%s}", optarg);
157                                 add_config_command(command_buffer);
158                                 break;
159                         }
160                         case 's':               /* --search | -s */
161                                 add_script_search_dir(optarg);
162                                 break;
163                         case 'd':               /* --debug | -d */
164                                 if (optarg)
165                                         snprintf(command_buffer, 128, "debug_level %s", optarg);
166                                 else
167                                         snprintf(command_buffer, 128, "debug_level 3");
168                                 command_run_line(cmd_ctx, command_buffer);
169                                 break;
170                         case 'l':               /* --log_output | -l */
171                                 if (optarg) {
172                                         snprintf(command_buffer, 128, "log_output %s", optarg);
173                                         command_run_line(cmd_ctx, command_buffer);
174                                 }
175                                 break;
176                         case 'c':               /* --command | -c */
177                                 if (optarg)
178                                     add_config_command(optarg);
179                                 break;
180                         case 'p':
181                                 /* to replicate the old syntax this needs to be synchronous
182                                  * otherwise the gdb stdin will overflow with the warning message */
183                                 command_run_line(cmd_ctx, "gdb_port pipe; log_output openocd.log");
184                                 LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; "
185                                                 "log_output openocd.log\"' instead.");
186                                 break;
187                 }
188         }
189
190         if (help_flag) {
191                 LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
192                 LOG_OUTPUT("--help       | -h\tdisplay this help\n");
193                 LOG_OUTPUT("--version    | -v\tdisplay OpenOCD version\n");
194                 LOG_OUTPUT("--file       | -f\tuse configuration file <name>\n");
195                 LOG_OUTPUT("--search     | -s\tdir to search for config files and scripts\n");
196                 LOG_OUTPUT("--debug      | -d\tset debug level <0-3>\n");
197                 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
198                 LOG_OUTPUT("--command    | -c\trun <command>\n");
199                 exit(-1);
200         }
201
202         if (version_flag) {
203                 /* Nothing to do, version gets printed automatically. */
204                 /* It is not an error to request the VERSION number. */
205                 exit(0);
206         }
207
208         /* paths specified on the command line take precedence over these
209          * built-in paths
210          */
211         add_default_dirs();
212
213         return ERROR_OK;
214 }