flash: reduce code duplication in stm32 flash probe
[openocd.git] / src / helper / jim-nvp.c
1 /* Jim - A small embeddable Tcl interpreter
2 *
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
5 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
6 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
7 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
8 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
9 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
10 * Copyright 2008 Steve Bennett <steveb@workware.net.au>
11 * Copyright 2009 Nico Coesel <ncoesel@dealogic.nl>
12 * Copyright 2009 Zachary T Welch zw@superlucidity.net
13 * Copyright 2009 David Brownell
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * The views and conclusions contained in the software and documentation
40 * are those of the authors and should not be interpreted as representing
41 * official policies, either expressed or implied, of the Jim Tcl Project.
42 */
43
44 #include <string.h>
45 #include <jim-nvp.h>
46
47 int Jim_GetNvp(Jim_Interp *interp,
48 Jim_Obj *objPtr, const Jim_Nvp *nvp_table, const Jim_Nvp **result)
49 {
50 Jim_Nvp *n;
51 int e;
52
53 e = Jim_Nvp_name2value_obj(interp, nvp_table, objPtr, &n);
54 if (e == JIM_ERR)
55 return e;
56
57 /* Success? found? */
58 if (n->name) {
59 /* remove const */
60 *result = (Jim_Nvp *) n;
61 return JIM_OK;
62 } else
63 return JIM_ERR;
64 }
65
66 Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp *p, const char *name)
67 {
68 while (p->name) {
69 if (0 == strcmp(name, p->name))
70 break;
71 p++;
72 }
73 return (Jim_Nvp *) (p);
74 }
75
76 Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp *p, const char *name)
77 {
78 while (p->name) {
79 if (0 == strcasecmp(name, p->name))
80 break;
81 p++;
82 }
83 return (Jim_Nvp *) (p);
84 }
85
86 int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp *p, Jim_Obj *o, Jim_Nvp **result)
87 {
88 return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
89 }
90
91 int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp *_p, const char *name, Jim_Nvp **result)
92 {
93 const Jim_Nvp *p;
94
95 p = Jim_Nvp_name2value_simple(_p, name);
96
97 /* result */
98 if (result)
99 *result = (Jim_Nvp *) (p);
100
101 /* found? */
102 if (p->name)
103 return JIM_OK;
104 else
105 return JIM_ERR;
106 }
107
108 int Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp,
109 const Jim_Nvp *p,
110 Jim_Obj *o,
111 Jim_Nvp **puthere)
112 {
113 return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
114 }
115
116 int Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp *_p, const char *name,
117 Jim_Nvp **puthere)
118 {
119 const Jim_Nvp *p;
120
121 p = Jim_Nvp_name2value_nocase_simple(_p, name);
122
123 if (puthere)
124 *puthere = (Jim_Nvp *) (p);
125 /* found */
126 if (p->name)
127 return JIM_OK;
128 else
129 return JIM_ERR;
130 }
131
132 int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp *p, Jim_Obj *o, Jim_Nvp **result)
133 {
134 int e;
135 jim_wide w;
136
137 e = Jim_GetWide(interp, o, &w);
138 if (e != JIM_OK)
139 return e;
140
141 return Jim_Nvp_value2name(interp, p, w, result);
142 }
143
144 Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp *p, int value)
145 {
146 while (p->name) {
147 if (value == p->value)
148 break;
149 p++;
150 }
151 return (Jim_Nvp *) (p);
152 }
153
154 int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp *_p, int value, Jim_Nvp **result)
155 {
156 const Jim_Nvp *p;
157
158 p = Jim_Nvp_value2name_simple(_p, value);
159
160 if (result)
161 *result = (Jim_Nvp *) (p);
162
163 if (p->name)
164 return JIM_OK;
165 else
166 return JIM_ERR;
167 }
168
169 int Jim_GetOpt_Setup(Jim_GetOptInfo *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
170 {
171 memset(p, 0, sizeof(*p));
172 p->interp = interp;
173 p->argc = argc;
174 p->argv = argv;
175
176 return JIM_OK;
177 }
178
179 void Jim_GetOpt_Debug(Jim_GetOptInfo *p)
180 {
181 int x;
182
183 fprintf(stderr, "---args---\n");
184 for (x = 0; x < p->argc; x++)
185 fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
186 fprintf(stderr, "-------\n");
187 }
188
189 int Jim_GetOpt_Obj(Jim_GetOptInfo *goi, Jim_Obj **puthere)
190 {
191 Jim_Obj *o;
192
193 o = NULL; /* failure */
194 if (goi->argc) {
195 /* success */
196 o = goi->argv[0];
197 goi->argc -= 1;
198 goi->argv += 1;
199 }
200 if (puthere)
201 *puthere = o;
202 if (o != NULL)
203 return JIM_OK;
204 else
205 return JIM_ERR;
206 }
207
208 int Jim_GetOpt_String(Jim_GetOptInfo *goi, char **puthere, int *len)
209 {
210 int r;
211 Jim_Obj *o;
212 const char *cp;
213
214 r = Jim_GetOpt_Obj(goi, &o);
215 if (r == JIM_OK) {
216 cp = Jim_GetString(o, len);
217 if (puthere) {
218 /* remove const */
219 *puthere = (char *)(cp);
220 }
221 }
222 return r;
223 }
224
225 int Jim_GetOpt_Double(Jim_GetOptInfo *goi, double *puthere)
226 {
227 int r;
228 Jim_Obj *o;
229 double _safe;
230
231 if (puthere == NULL)
232 puthere = &_safe;
233
234 r = Jim_GetOpt_Obj(goi, &o);
235 if (r == JIM_OK) {
236 r = Jim_GetDouble(goi->interp, o, puthere);
237 if (r != JIM_OK)
238 Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
239 }
240 return r;
241 }
242
243 int Jim_GetOpt_Wide(Jim_GetOptInfo *goi, jim_wide *puthere)
244 {
245 int r;
246 Jim_Obj *o;
247 jim_wide _safe;
248
249 if (puthere == NULL)
250 puthere = &_safe;
251
252 r = Jim_GetOpt_Obj(goi, &o);
253 if (r == JIM_OK)
254 r = Jim_GetWide(goi->interp, o, puthere);
255 return r;
256 }
257
258 int Jim_GetOpt_Nvp(Jim_GetOptInfo *goi, const Jim_Nvp *nvp, Jim_Nvp **puthere)
259 {
260 Jim_Nvp *_safe;
261 Jim_Obj *o;
262 int e;
263
264 if (puthere == NULL)
265 puthere = &_safe;
266
267 e = Jim_GetOpt_Obj(goi, &o);
268 if (e == JIM_OK)
269 e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);
270
271 return e;
272 }
273
274 void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo *goi, const Jim_Nvp *nvptable, int hadprefix)
275 {
276 if (hadprefix)
277 Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
278 else
279 Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
280 }
281
282 int Jim_GetOpt_Enum(Jim_GetOptInfo *goi, const char *const *lookup, int *puthere)
283 {
284 int _safe;
285 Jim_Obj *o;
286 int e;
287
288 if (puthere == NULL)
289 puthere = &_safe;
290 e = Jim_GetOpt_Obj(goi, &o);
291 if (e == JIM_OK)
292 e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
293 return e;
294 }
295
296 void Jim_SetResult_NvpUnknown(Jim_Interp *interp,
297 Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp *nvp)
298 {
299 if (param_name)
300 Jim_SetResultFormatted(interp,
301 "%#s: Unknown: %#s, try one of: ",
302 param_name,
303 param_value);
304 else
305 Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
306 while (nvp->name) {
307 const char *a;
308 const char *b;
309
310 if ((nvp + 1)->name) {
311 a = nvp->name;
312 b = ", ";
313 } else {
314 a = "or ";
315 b = nvp->name;
316 }
317 Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
318 nvp++;
319 }
320 }
321
322 const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
323 {
324 static Jim_Obj *debug_string_obj;
325
326 int x;
327
328 if (debug_string_obj)
329 Jim_FreeObj(interp, debug_string_obj);
330
331 debug_string_obj = Jim_NewEmptyStringObj(interp);
332 for (x = 0; x < argc; x++)
333 Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
334
335 return Jim_String(debug_string_obj);
336 }
337
338 int Jim_nvpInit(Jim_Interp *interp)
339 {
340 /* This is really a helper library, not an extension, but this is the easy way */
341 return JIM_OK;
342 }

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)