0409a83cdfb1a72feaa0f84b48ed244d656a0373
[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 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include "jim-nvp.h"
49 #include <string.h>
50
51 int jim_get_nvp(Jim_Interp *interp,
52 Jim_Obj *objptr, const struct jim_nvp *nvp_table, const struct jim_nvp **result)
53 {
54 struct jim_nvp *n;
55 int e;
56
57 e = jim_nvp_name2value_obj(interp, nvp_table, objptr, &n);
58 if (e == JIM_ERR)
59 return e;
60
61 /* Success? found? */
62 if (n->name) {
63 /* remove const */
64 *result = (struct jim_nvp *)n;
65 return JIM_OK;
66 } else
67 return JIM_ERR;
68 }
69
70 struct jim_nvp *jim_nvp_name2value_simple(const struct jim_nvp *p, const char *name)
71 {
72 while (p->name) {
73 if (strcmp(name, p->name) == 0)
74 break;
75 p++;
76 }
77 return (struct jim_nvp *)p;
78 }
79
80 struct jim_nvp *jim_nvp_name2value_nocase_simple(const struct jim_nvp *p, const char *name)
81 {
82 while (p->name) {
83 if (strcasecmp(name, p->name) == 0)
84 break;
85 p++;
86 }
87 return (struct jim_nvp *)p;
88 }
89
90 int jim_nvp_name2value_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
91 {
92 return jim_nvp_name2value(interp, p, Jim_String(o), result);
93 }
94
95 int jim_nvp_name2value(Jim_Interp *interp, const struct jim_nvp *_p, const char *name, struct jim_nvp **result)
96 {
97 const struct jim_nvp *p;
98
99 p = jim_nvp_name2value_simple(_p, name);
100
101 /* result */
102 if (result)
103 *result = (struct jim_nvp *)p;
104
105 /* found? */
106 if (p->name)
107 return JIM_OK;
108 else
109 return JIM_ERR;
110 }
111
112 int jim_nvp_name2value_obj_nocase(Jim_Interp *interp,
113 const struct jim_nvp *p,
114 Jim_Obj *o,
115 struct jim_nvp **puthere)
116 {
117 return jim_nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
118 }
119
120 int jim_nvp_name2value_nocase(Jim_Interp *interp, const struct jim_nvp *_p, const char *name,
121 struct jim_nvp **puthere)
122 {
123 const struct jim_nvp *p;
124
125 p = jim_nvp_name2value_nocase_simple(_p, name);
126
127 if (puthere)
128 *puthere = (struct jim_nvp *)p;
129 /* found */
130 if (p->name)
131 return JIM_OK;
132 else
133 return JIM_ERR;
134 }
135
136 int jim_nvp_value2name_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
137 {
138 int e;
139 jim_wide w;
140
141 e = Jim_GetWide(interp, o, &w);
142 if (e != JIM_OK)
143 return e;
144
145 return jim_nvp_value2name(interp, p, w, result);
146 }
147
148 struct jim_nvp *jim_nvp_value2name_simple(const struct jim_nvp *p, int value)
149 {
150 while (p->name) {
151 if (value == p->value)
152 break;
153 p++;
154 }
155 return (struct jim_nvp *)p;
156 }
157
158 int jim_nvp_value2name(Jim_Interp *interp, const struct jim_nvp *_p, int value, struct jim_nvp **result)
159 {
160 const struct jim_nvp *p;
161
162 p = jim_nvp_value2name_simple(_p, value);
163
164 if (result)
165 *result = (struct jim_nvp *)p;
166
167 if (p->name)
168 return JIM_OK;
169 else
170 return JIM_ERR;
171 }
172
173 int jim_getopt_setup(struct jim_getopt_info *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
174 {
175 memset(p, 0, sizeof(*p));
176 p->interp = interp;
177 p->argc = argc;
178 p->argv = argv;
179
180 return JIM_OK;
181 }
182
183 void jim_getopt_debug(struct jim_getopt_info *p)
184 {
185 int x;
186
187 fprintf(stderr, "---args---\n");
188 for (x = 0; x < p->argc; x++)
189 fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
190 fprintf(stderr, "-------\n");
191 }
192
193 int jim_getopt_obj(struct jim_getopt_info *goi, Jim_Obj **puthere)
194 {
195 Jim_Obj *o;
196
197 o = NULL; /* failure */
198 if (goi->argc) {
199 /* success */
200 o = goi->argv[0];
201 goi->argc -= 1;
202 goi->argv += 1;
203 }
204 if (puthere)
205 *puthere = o;
206 if (o)
207 return JIM_OK;
208 else
209 return JIM_ERR;
210 }
211
212 int jim_getopt_string(struct jim_getopt_info *goi, const char **puthere, int *len)
213 {
214 int r;
215 Jim_Obj *o;
216 const char *cp;
217
218 r = jim_getopt_obj(goi, &o);
219 if (r == JIM_OK) {
220 cp = Jim_GetString(o, len);
221 if (puthere) {
222 *puthere = cp;
223 }
224 }
225 return r;
226 }
227
228 int jim_getopt_double(struct jim_getopt_info *goi, double *puthere)
229 {
230 int r;
231 Jim_Obj *o;
232 double _safe;
233
234 if (!puthere)
235 puthere = &_safe;
236
237 r = jim_getopt_obj(goi, &o);
238 if (r == JIM_OK) {
239 r = Jim_GetDouble(goi->interp, o, puthere);
240 if (r != JIM_OK)
241 Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
242 }
243 return r;
244 }
245
246 int jim_getopt_wide(struct jim_getopt_info *goi, jim_wide *puthere)
247 {
248 int r;
249 Jim_Obj *o;
250 jim_wide _safe;
251
252 if (!puthere)
253 puthere = &_safe;
254
255 r = jim_getopt_obj(goi, &o);
256 if (r == JIM_OK)
257 r = Jim_GetWide(goi->interp, o, puthere);
258 return r;
259 }
260
261 int jim_getopt_nvp(struct jim_getopt_info *goi, const struct jim_nvp *nvp, struct jim_nvp **puthere)
262 {
263 struct jim_nvp *_safe;
264 Jim_Obj *o;
265 int e;
266
267 if (!puthere)
268 puthere = &_safe;
269
270 e = jim_getopt_obj(goi, &o);
271 if (e == JIM_OK)
272 e = jim_nvp_name2value_obj(goi->interp, nvp, o, puthere);
273
274 return e;
275 }
276
277 void jim_getopt_nvp_unknown(struct jim_getopt_info *goi, const struct jim_nvp *nvptable, int hadprefix)
278 {
279 if (hadprefix)
280 jim_set_result_nvp_unknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
281 else
282 jim_set_result_nvp_unknown(goi->interp, NULL, goi->argv[-1], nvptable);
283 }
284
285 int jim_getopt_enum(struct jim_getopt_info *goi, const char *const *lookup, int *puthere)
286 {
287 int _safe;
288 Jim_Obj *o;
289 int e;
290
291 if (!puthere)
292 puthere = &_safe;
293 e = jim_getopt_obj(goi, &o);
294 if (e == JIM_OK)
295 e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
296 return e;
297 }
298
299 void jim_set_result_nvp_unknown(Jim_Interp *interp,
300 Jim_Obj *param_name, Jim_Obj *param_value, const struct jim_nvp *nvp)
301 {
302 if (param_name)
303 Jim_SetResultFormatted(interp,
304 "%#s: Unknown: %#s, try one of: ",
305 param_name,
306 param_value);
307 else
308 Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
309 while (nvp->name) {
310 const char *a;
311 const char *b;
312
313 if ((nvp + 1)->name) {
314 a = nvp->name;
315 b = ", ";
316 } else {
317 a = "or ";
318 b = nvp->name;
319 }
320 Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
321 nvp++;
322 }
323 }
324
325 const char *jim_debug_argv_string(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
326 {
327 static Jim_Obj *debug_string_obj;
328
329 int x;
330
331 if (debug_string_obj)
332 Jim_FreeObj(interp, debug_string_obj);
333
334 debug_string_obj = Jim_NewEmptyStringObj(interp);
335 for (x = 0; x < argc; x++)
336 Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
337
338 return Jim_String(debug_string_obj);
339 }

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)