jim: add missing jim license
[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
58 /* Success? found? */
59 if (n->name) {
60 /* remove const */
61 *result = (Jim_Nvp *) n;
62 return JIM_OK;
63 }
64 else {
65 return JIM_ERR;
66 }
67 }
68
69 Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp * p, const char *name)
70 {
71 while (p->name) {
72 if (0 == strcmp(name, p->name)) {
73 break;
74 }
75 p++;
76 }
77 return ((Jim_Nvp *) (p));
78 }
79
80 Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp * p, const char *name)
81 {
82 while (p->name) {
83 if (0 == strcasecmp(name, p->name)) {
84 break;
85 }
86 p++;
87 }
88 return ((Jim_Nvp *) (p));
89 }
90
91 int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
92 {
93 return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
94 }
95
96
97 int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp * _p, const char *name, Jim_Nvp ** result)
98 {
99 const Jim_Nvp *p;
100
101 p = Jim_Nvp_name2value_simple(_p, name);
102
103 /* result */
104 if (result) {
105 *result = (Jim_Nvp *) (p);
106 }
107
108 /* found? */
109 if (p->name) {
110 return JIM_OK;
111 }
112 else {
113 return JIM_ERR;
114 }
115 }
116
117 int
118 Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** puthere)
119 {
120 return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
121 }
122
123 int
124 Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp * _p, const char *name,
125 Jim_Nvp ** puthere)
126 {
127 const Jim_Nvp *p;
128
129 p = Jim_Nvp_name2value_nocase_simple(_p, name);
130
131 if (puthere) {
132 *puthere = (Jim_Nvp *) (p);
133 }
134 /* found */
135 if (p->name) {
136 return JIM_OK;
137 }
138 else {
139 return JIM_ERR;
140 }
141 }
142
143
144 int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
145 {
146 int e;;
147 jim_wide w;
148
149 e = Jim_GetWide(interp, o, &w);
150 if (e != JIM_OK) {
151 return e;
152 }
153
154 return Jim_Nvp_value2name(interp, p, w, result);
155 }
156
157 Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp * p, int value)
158 {
159 while (p->name) {
160 if (value == p->value) {
161 break;
162 }
163 p++;
164 }
165 return ((Jim_Nvp *) (p));
166 }
167
168
169 int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp * _p, int value, Jim_Nvp ** result)
170 {
171 const Jim_Nvp *p;
172
173 p = Jim_Nvp_value2name_simple(_p, value);
174
175 if (result) {
176 *result = (Jim_Nvp *) (p);
177 }
178
179 if (p->name) {
180 return JIM_OK;
181 }
182 else {
183 return JIM_ERR;
184 }
185 }
186
187
188 int Jim_GetOpt_Setup(Jim_GetOptInfo * p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
189 {
190 memset(p, 0, sizeof(*p));
191 p->interp = interp;
192 p->argc = argc;
193 p->argv = argv;
194
195 return JIM_OK;
196 }
197
198 void Jim_GetOpt_Debug(Jim_GetOptInfo * p)
199 {
200 int x;
201
202 fprintf(stderr, "---args---\n");
203 for (x = 0; x < p->argc; x++) {
204 fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
205 }
206 fprintf(stderr, "-------\n");
207 }
208
209
210 int Jim_GetOpt_Obj(Jim_GetOptInfo * goi, Jim_Obj **puthere)
211 {
212 Jim_Obj *o;
213
214 o = NULL; // failure
215 if (goi->argc) {
216 // success
217 o = goi->argv[0];
218 goi->argc -= 1;
219 goi->argv += 1;
220 }
221 if (puthere) {
222 *puthere = o;
223 }
224 if (o != NULL) {
225 return JIM_OK;
226 }
227 else {
228 return JIM_ERR;
229 }
230 }
231
232 int Jim_GetOpt_String(Jim_GetOptInfo * goi, char **puthere, int *len)
233 {
234 int r;
235 Jim_Obj *o;
236 const char *cp;
237
238
239 r = Jim_GetOpt_Obj(goi, &o);
240 if (r == JIM_OK) {
241 cp = Jim_GetString(o, len);
242 if (puthere) {
243 /* remove const */
244 *puthere = (char *)(cp);
245 }
246 }
247 return r;
248 }
249
250 int Jim_GetOpt_Double(Jim_GetOptInfo * goi, double *puthere)
251 {
252 int r;
253 Jim_Obj *o;
254 double _safe;
255
256 if (puthere == NULL) {
257 puthere = &_safe;
258 }
259
260 r = Jim_GetOpt_Obj(goi, &o);
261 if (r == JIM_OK) {
262 r = Jim_GetDouble(goi->interp, o, puthere);
263 if (r != JIM_OK) {
264 Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
265 }
266 }
267 return r;
268 }
269
270 int Jim_GetOpt_Wide(Jim_GetOptInfo * goi, jim_wide * puthere)
271 {
272 int r;
273 Jim_Obj *o;
274 jim_wide _safe;
275
276 if (puthere == NULL) {
277 puthere = &_safe;
278 }
279
280 r = Jim_GetOpt_Obj(goi, &o);
281 if (r == JIM_OK) {
282 r = Jim_GetWide(goi->interp, o, puthere);
283 }
284 return r;
285 }
286
287 int Jim_GetOpt_Nvp(Jim_GetOptInfo * goi, const Jim_Nvp * nvp, Jim_Nvp ** puthere)
288 {
289 Jim_Nvp *_safe;
290 Jim_Obj *o;
291 int e;
292
293 if (puthere == NULL) {
294 puthere = &_safe;
295 }
296
297 e = Jim_GetOpt_Obj(goi, &o);
298 if (e == JIM_OK) {
299 e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);
300 }
301
302 return e;
303 }
304
305 void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo * goi, const Jim_Nvp * nvptable, int hadprefix)
306 {
307 if (hadprefix) {
308 Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
309 }
310 else {
311 Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
312 }
313 }
314
315
316 int Jim_GetOpt_Enum(Jim_GetOptInfo * goi, const char *const *lookup, int *puthere)
317 {
318 int _safe;
319 Jim_Obj *o;
320 int e;
321
322 if (puthere == NULL) {
323 puthere = &_safe;
324 }
325 e = Jim_GetOpt_Obj(goi, &o);
326 if (e == JIM_OK) {
327 e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
328 }
329 return e;
330 }
331
332 void
333 Jim_SetResult_NvpUnknown(Jim_Interp *interp,
334 Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp * nvp)
335 {
336 if (param_name) {
337 Jim_SetResultFormatted(interp, "%#s: Unknown: %#s, try one of: ", param_name, param_value);
338 }
339 else {
340 Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
341 }
342 while (nvp->name) {
343 const char *a;
344 const char *b;
345
346 if ((nvp + 1)->name) {
347 a = nvp->name;
348 b = ", ";
349 }
350 else {
351 a = "or ";
352 b = nvp->name;
353 }
354 Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
355 nvp++;
356 }
357 }
358
359 const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
360 {
361 static Jim_Obj *debug_string_obj;
362
363 int x;
364
365 if (debug_string_obj) {
366 Jim_FreeObj(interp, debug_string_obj);
367 }
368
369 debug_string_obj = Jim_NewEmptyStringObj(interp);
370 for (x = 0; x < argc; x++) {
371 Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
372 }
373
374 return Jim_String(debug_string_obj);
375 }
376
377 int Jim_nvpInit(Jim_Interp *interp)
378 {
379 /* This is really a helper library, not an extension, but this is the easy way */
380 return JIM_OK;
381 }