d9300e9dcc54ccd2415d3fe4aa111bfa0db7a17f
[openocd.git] / src / helper / configuration.c
1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "types.h"
25 #include "command.h"
26 #include "configuration.h"
27 #include "log.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <getopt.h>
32 #include <string.h>
33
34 static size_t num_config_files;
35 static char** config_file_names;
36
37 static size_t num_script_dirs;
38 static char** script_search_dirs;
39
40 static int help_flag;
41
42 static struct option long_options[] =
43 {
44 {"help", no_argument, &help_flag, 1},
45
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
52 {0, 0, 0, 0}
53 };
54
55 int configuration_output_handler(struct command_context_s *context, char* line)
56 {
57 INFO(line);
58
59 return ERROR_OK;
60 }
61
62 void add_script_search_dir (const char *dir)
63 {
64 num_script_dirs++;
65 script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));
66
67 script_search_dirs[num_script_dirs-1] = strdup(dir);
68 script_search_dirs[num_script_dirs] = NULL;
69 }
70
71 void add_config_file_name (const char *cfg)
72 {
73 num_config_files++;
74 config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));
75
76 config_file_names[num_config_files-1] = strdup(cfg);
77 config_file_names[num_config_files] = NULL;
78 }
79
80 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
81 {
82 int c;
83 char command_buffer[128];
84
85 while (1)
86 {
87 /* getopt_long stores the option index here. */
88 int option_index = 0;
89
90 c = getopt_long(argc, argv, "hd::l:f:s:c:", long_options, &option_index);
91
92 /* Detect the end of the options. */
93 if (c == -1)
94 break;
95
96 switch (c)
97 {
98 case 0:
99 break;
100 case 'h': /* --help | -h */
101 help_flag = 1;
102 break;
103 case 'f': /* --file | -f */
104 snprintf(command_buffer, 128, "script %s", optarg);
105 add_config_file_name(command_buffer);
106 break;
107 case 's': /* --search | -s */
108 add_script_search_dir(optarg);
109 break;
110 case 'd': /* --debug | -d */
111 if (optarg)
112 snprintf(command_buffer, 128, "debug_level %s", optarg);
113 else
114 snprintf(command_buffer, 128, "debug_level 3");
115 command_run_line(cmd_ctx, command_buffer);
116 break;
117 case 'l': /* --log_output | -l */
118 if (optarg)
119 {
120 snprintf(command_buffer, 128, "log_output %s", optarg);
121 command_run_line(cmd_ctx, command_buffer);
122 }
123 break;
124 case 'c': /* --command | -c */
125 if (optarg)
126 {
127 add_config_file_name(optarg);
128 }
129 break;
130
131 }
132 }
133
134 if (help_flag)
135 {
136 printf("Open On-Chip Debugger\n(c) 2005 by Dominic Rath\n\n");
137 printf("--help | -h\tdisplay this help\n");
138 printf("--file | -f\tuse configuration file <name>\n");
139 printf("--search | -s\tdir to search for config files and scripts.\n");
140 printf("--debug | -d\tset debug level <0-3>\n");
141 printf("--log_output | -l\tredirect log output to file <name>\n");
142 printf("--command | -c\trun <command>\n");
143 exit(-1);
144 }
145
146 /* Add dir for openocd supplied scripts last so that user can over
147 ride those scripts if desired. */
148 add_script_search_dir(PKGDATADIR);
149 add_script_search_dir(PKGLIBDIR);
150
151 return ERROR_OK;
152 }
153
154 FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)
155 {
156 FILE *fp = NULL;
157 char **search_dirs = script_search_dirs;
158 char *dir;
159 char full_path[1024];
160
161 /* Check absolute and relative to current working dir first.
162 * This keeps full_path reporting belowing working. */
163 snprintf(full_path, 1024, "%s", file);
164 fp = fopen(full_path, mode);
165
166 while (!fp)
167 {
168 dir = *search_dirs++;
169
170 if (!dir)
171 break;
172
173 snprintf(full_path, 1024, "%s/%s", dir, file);
174 fp = fopen(full_path, mode);
175 }
176
177 if (fp)
178 command_print(cmd_ctx, "opened %s", full_path);
179
180 return fp;
181 }
182
183 int parse_config_file(struct command_context_s *cmd_ctx)
184 {
185 char **cfg;
186 FILE *config_file;
187
188 if (!config_file_names)
189 add_config_file_name ("script openocd.cfg");
190
191 cfg = config_file_names;
192
193 while (*cfg)
194 {
195 command_run_line(cmd_ctx, *cfg);
196 cfg++;
197 }
198
199 return ERROR_OK;
200 }

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)