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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)