Remove FSF address from GPL notices
[fw/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, see <http://www.gnu.org/licenses/>. *
20  ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "configuration.h"
27 #include "log.h"
28 #include "command.h"
29
30 #include <getopt.h>
31
32 static int help_flag, version_flag;
33
34 static const struct option long_options[] = {
35         {"help",                no_argument,                    &help_flag,             1},
36         {"version",             no_argument,                    &version_flag,  1},
37         {"debug",               optional_argument,              0,                              'd'},
38         {"file",                required_argument,              0,                              'f'},
39         {"search",              required_argument,              0,                              's'},
40         {"log_output",  required_argument,              0,                              'l'},
41         {"command",             required_argument,              0,                              'c'},
42         {"pipe",                no_argument,                    0,                              'p'},
43         {0, 0, 0, 0}
44 };
45
46 int configuration_output_handler(struct command_context *context, const char *line)
47 {
48         LOG_USER_N("%s", line);
49
50         return ERROR_OK;
51 }
52
53 #ifdef _WIN32
54 static char *find_suffix(const char *text, const char *suffix)
55 {
56         size_t text_len = strlen(text);
57         size_t suffix_len = strlen(suffix);
58
59         if (suffix_len == 0)
60                 return (char *)text + text_len;
61
62         if (suffix_len > text_len || strncmp(text + text_len - suffix_len, suffix, suffix_len) != 0)
63                 return NULL; /* Not a suffix of text */
64
65         return (char *)text + text_len - suffix_len;
66 }
67 #endif
68
69 static void add_default_dirs(void)
70 {
71         const char *run_prefix;
72         char *path;
73
74 #ifdef _WIN32
75         char strExePath[MAX_PATH];
76         GetModuleFileName(NULL, strExePath, MAX_PATH);
77
78         /* Strip executable file name, leaving path */
79         *strrchr(strExePath, '\\') = '\0';
80
81         /* Convert path separators to UNIX style, should work on Windows also. */
82         for (char *p = strExePath; *p; p++) {
83                 if (*p == '\\')
84                         *p = '/';
85         }
86
87         char *end_of_prefix = find_suffix(strExePath, BINDIR);
88         if (end_of_prefix != NULL)
89                 *end_of_prefix = '\0';
90
91         run_prefix = strExePath;
92 #else
93         run_prefix = "";
94 #endif
95
96         LOG_DEBUG("bindir=%s", BINDIR);
97         LOG_DEBUG("pkgdatadir=%s", PKGDATADIR);
98         LOG_DEBUG("run_prefix=%s", run_prefix);
99
100         /*
101          * The directory containing OpenOCD-supplied scripts should be
102          * listed last in the built-in search order, so the user can
103          * override these scripts with site-specific customizations.
104          */
105         const char *home = getenv("HOME");
106
107         if (home) {
108                 path = alloc_printf("%s/.openocd", home);
109                 if (path) {
110                         add_script_search_dir(path);
111                         free(path);
112                 }
113         }
114
115         path = getenv("OPENOCD_SCRIPTS");
116
117         if (path)
118                 add_script_search_dir(path);
119
120 #ifdef _WIN32
121         const char *appdata = getenv("APPDATA");
122
123         if (appdata) {
124                 path = alloc_printf("%s/OpenOCD", appdata);
125                 if (path) {
126                         add_script_search_dir(path);
127                         free(path);
128                 }
129         }
130 #endif
131
132         path = alloc_printf("%s%s%s", run_prefix, PKGDATADIR, "/site");
133         if (path) {
134                 add_script_search_dir(path);
135                 free(path);
136         }
137
138         path = alloc_printf("%s%s%s", run_prefix, PKGDATADIR, "/scripts");
139         if (path) {
140                 add_script_search_dir(path);
141                 free(path);
142         }
143 }
144
145 int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
146 {
147         int c;
148
149         while (1) {
150                 /* getopt_long stores the option index here. */
151                 int option_index = 0;
152
153                 c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
154
155                 /* Detect the end of the options. */
156                 if (c == -1)
157                         break;
158
159                 switch (c) {
160                         case 0:
161                                 break;
162                         case 'h':               /* --help | -h */
163                                 help_flag = 1;
164                                 break;
165                         case 'v':               /* --version | -v */
166                                 version_flag = 1;
167                                 break;
168                         case 'f':               /* --file | -f */
169                         {
170                                 char *command = alloc_printf("script {%s}", optarg);
171                                 add_config_command(command);
172                                 free(command);
173                                 break;
174                         }
175                         case 's':               /* --search | -s */
176                                 add_script_search_dir(optarg);
177                                 break;
178                         case 'd':               /* --debug | -d */
179                         {
180                                 char *command = alloc_printf("debug_level %s", optarg ? optarg : "3");
181                                 command_run_line(cmd_ctx, command);
182                                 free(command);
183                                 break;
184                         }
185                         case 'l':               /* --log_output | -l */
186                                 if (optarg) {
187                                         char *command = alloc_printf("log_output %s", optarg);
188                                         command_run_line(cmd_ctx, command);
189                                         free(command);
190                                 }
191                                 break;
192                         case 'c':               /* --command | -c */
193                                 if (optarg)
194                                     add_config_command(optarg);
195                                 break;
196                         case 'p':
197                                 /* to replicate the old syntax this needs to be synchronous
198                                  * otherwise the gdb stdin will overflow with the warning message */
199                                 command_run_line(cmd_ctx, "gdb_port pipe; log_output openocd.log");
200                                 LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; "
201                                                 "log_output openocd.log\"' instead.");
202                                 break;
203                 }
204         }
205
206         if (help_flag) {
207                 LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
208                 LOG_OUTPUT("--help       | -h\tdisplay this help\n");
209                 LOG_OUTPUT("--version    | -v\tdisplay OpenOCD version\n");
210                 LOG_OUTPUT("--file       | -f\tuse configuration file <name>\n");
211                 LOG_OUTPUT("--search     | -s\tdir to search for config files and scripts\n");
212                 LOG_OUTPUT("--debug      | -d\tset debug level <0-3>\n");
213                 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
214                 LOG_OUTPUT("--command    | -c\trun <command>\n");
215                 exit(-1);
216         }
217
218         if (version_flag) {
219                 /* Nothing to do, version gets printed automatically. */
220                 /* It is not an error to request the VERSION number. */
221                 exit(0);
222         }
223
224         /* paths specified on the command line take precedence over these
225          * built-in paths
226          */
227         add_default_dirs();
228
229         return ERROR_OK;
230 }