Michael Bruck:
[openocd.git] / src / helper / interpreter.c
1 /***************************************************************************
2 * Copyright (C) 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 "interpreter.h"
25 #include "configuration.h"
26
27 #include "binarybuffer.h"
28 #include <stdlib.h>
29 #include <string.h>
30
31 var_t *variables = NULL;
32
33 int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
34 int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
35 int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
36
37 int interpreter_register_commands(struct command_context_s *cmd_ctx)
38 {
39 register_command(cmd_ctx, NULL, "var", handle_var_command,
40 COMMAND_ANY, "allocate, display or delete variable <name> [num_fields|'del'] [size1] ...");
41 register_command(cmd_ctx, NULL, "field", handle_field_command,
42 COMMAND_ANY, "display/modify variable field <var> <field> [value|'flip']");
43 register_command(cmd_ctx, NULL, "script", handle_script_command,
44 COMMAND_ANY, "execute commands from <file>");
45
46 return ERROR_OK;
47 }
48
49 var_t* get_var_by_num(int num)
50 {
51 int count = 0;
52 var_t *var = variables;
53
54 if (var)
55 {
56 if (num == count)
57 return var;
58 while (var->next)
59 {
60 var = var->next;
61 count++;
62 if (num == count)
63 return var;
64 }
65 }
66 return NULL;
67 }
68
69 var_t* get_var_by_name(char *name)
70 {
71 var_t *var = variables;
72
73 if (var)
74 {
75 if (strcmp(var->name, name) == 0)
76 return var;
77 while (var->next)
78 {
79 var = var->next;
80 if (strcmp(var->name, name) == 0)
81 return var;
82 }
83 }
84 return NULL;
85 }
86
87 var_t* get_var_by_namenum(char *namenum)
88 {
89 if ((namenum[0] >= '0') && (namenum[0] <= '9'))
90 return get_var_by_num(strtol(namenum, NULL, 0));
91 else
92 return get_var_by_name(namenum);
93
94 }
95
96 int field_le_to_host(u8 *buffer, void *priv, struct scan_field_s *dummy)
97 {
98 var_field_t *field = priv;
99 field->value = buf_get_u32(buffer, 0, field->num_bits);
100
101 return ERROR_OK;
102 }
103
104 int handle_var_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
105 {
106 var_t **last_var_p = &variables;
107 int i;
108
109 if (argc >= 2)
110 {
111 while (*last_var_p)
112 {
113 if (strcmp((*last_var_p)->name, args[0]) == 0)
114 {
115 if (strcmp(args[1], "del") == 0)
116 {
117 var_t *next = (*last_var_p)->next;
118 free ((*last_var_p)->fields);
119 free (*last_var_p);
120 *last_var_p = next;
121 command_print(cmd_ctx, "variable %s deleted", args[0]);
122 }
123 else
124 command_print(cmd_ctx, "variable of that name already exists");
125 return ERROR_OK;
126 }
127 last_var_p = &((*last_var_p)->next);
128 }
129
130 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
131 {
132 command_print(cmd_ctx, "invalid name specified (first character may not be a number)");
133 return ERROR_OK;
134 }
135
136 *last_var_p = malloc(sizeof(var_t));
137 (*last_var_p)->name = strdup(args[0]);
138 (*last_var_p)->num_fields = argc - 1;
139 (*last_var_p)->next = NULL;
140
141 (*last_var_p)->fields = malloc(sizeof(var_field_t) * (*last_var_p)->num_fields);
142 for (i = 0; i < (*last_var_p)->num_fields; i++)
143 {
144 (*last_var_p)->fields[i].num_bits = strtol(args[1+i], NULL, 0);
145 (*last_var_p)->fields[i].value = 0x0;
146 }
147 return ERROR_OK;
148 }
149
150 if (argc == 1)
151 {
152 var_t *var = get_var_by_namenum(args[0]);
153 if (var)
154 {
155 int i;
156 command_print(cmd_ctx, "%s (%i fields):", var->name, var->num_fields);
157 for (i = 0; i < (var->num_fields); i++)
158 {
159 command_print(cmd_ctx, "0x%x (/%i)", var->fields[i].value, var->fields[i].num_bits);
160 }
161 }
162 else
163 {
164 command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
165 }
166 }
167
168 if (argc == 0)
169 {
170 var_t *var = variables;
171 int count = 0;
172 while (var)
173 {
174 command_print(cmd_ctx, "%i: %s (%i fields)", count, var->name, var->num_fields);
175 var = var->next;
176 count++;
177 }
178 }
179
180 return ERROR_OK;
181 }
182
183 int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
184 {
185
186 if (argc < 2)
187 return ERROR_COMMAND_SYNTAX_ERROR;
188
189 if (argc >= 2)
190 {
191 var_t *var = get_var_by_namenum(args[0]);
192 int field_num = strtol(args[1], NULL, 0);
193 if (!var)
194 {
195 command_print(cmd_ctx, "variable %s doesn't exist", args[0]);
196 return ERROR_OK;
197 }
198 if (field_num >= var->num_fields)
199 command_print(cmd_ctx, "variable field %i is out of bounds (max. %i)", field_num, var->num_fields - 1);
200 if ((var) && (field_num < var->num_fields))
201 {
202 if (argc > 2)
203 {
204 if (strcmp(args[2], "flip") == 0)
205 var->fields[field_num].value = flip_u32(var->fields[field_num].value, var->fields[field_num].num_bits);
206 else
207 var->fields[field_num].value = strtoul(args[2], NULL, 0);
208 }
209
210 command_print(cmd_ctx, "%s(%i): 0x%x (/%i)", var->name, field_num, var->fields[field_num].value, var->fields[field_num].num_bits);
211 }
212 }
213
214 return ERROR_OK;
215 }
216
217 int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
218 {
219 FILE *script_file;
220
221 if (argc != 1)
222 return ERROR_COMMAND_SYNTAX_ERROR;
223
224 script_file = open_file_from_path(cmd_ctx, args[0], "r");
225
226 if (!script_file)
227 {
228 command_print(cmd_ctx, "couldn't open script file %s", args[0]);
229 return ERROR_OK;
230 }
231
232 command_run_file(cmd_ctx, script_file, cmd_ctx->mode);
233
234 fclose(script_file);
235
236 return ERROR_OK;
237 }

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)