jim-nvp is moving from jimtcl to openocd
[openocd.git] / src / helper / jim-nvp.c
1 #include <string.h>
2 #include <jim-nvp.h>
3
4 int Jim_GetNvp(Jim_Interp *interp,
5 Jim_Obj *objPtr, const Jim_Nvp * nvp_table, const Jim_Nvp ** result)
6 {
7 Jim_Nvp *n;
8 int e;
9
10 e = Jim_Nvp_name2value_obj(interp, nvp_table, objPtr, &n);
11 if (e == JIM_ERR) {
12 return e;
13 }
14
15 /* Success? found? */
16 if (n->name) {
17 /* remove const */
18 *result = (Jim_Nvp *) n;
19 return JIM_OK;
20 }
21 else {
22 return JIM_ERR;
23 }
24 }
25
26 Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp * p, const char *name)
27 {
28 while (p->name) {
29 if (0 == strcmp(name, p->name)) {
30 break;
31 }
32 p++;
33 }
34 return ((Jim_Nvp *) (p));
35 }
36
37 Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp * p, const char *name)
38 {
39 while (p->name) {
40 if (0 == strcasecmp(name, p->name)) {
41 break;
42 }
43 p++;
44 }
45 return ((Jim_Nvp *) (p));
46 }
47
48 int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
49 {
50 return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
51 }
52
53
54 int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp * _p, const char *name, Jim_Nvp ** result)
55 {
56 const Jim_Nvp *p;
57
58 p = Jim_Nvp_name2value_simple(_p, name);
59
60 /* result */
61 if (result) {
62 *result = (Jim_Nvp *) (p);
63 }
64
65 /* found? */
66 if (p->name) {
67 return JIM_OK;
68 }
69 else {
70 return JIM_ERR;
71 }
72 }
73
74 int
75 Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** puthere)
76 {
77 return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
78 }
79
80 int
81 Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp * _p, const char *name,
82 Jim_Nvp ** puthere)
83 {
84 const Jim_Nvp *p;
85
86 p = Jim_Nvp_name2value_nocase_simple(_p, name);
87
88 if (puthere) {
89 *puthere = (Jim_Nvp *) (p);
90 }
91 /* found */
92 if (p->name) {
93 return JIM_OK;
94 }
95 else {
96 return JIM_ERR;
97 }
98 }
99
100
101 int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
102 {
103 int e;;
104 jim_wide w;
105
106 e = Jim_GetWide(interp, o, &w);
107 if (e != JIM_OK) {
108 return e;
109 }
110
111 return Jim_Nvp_value2name(interp, p, w, result);
112 }
113
114 Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp * p, int value)
115 {
116 while (p->name) {
117 if (value == p->value) {
118 break;
119 }
120 p++;
121 }
122 return ((Jim_Nvp *) (p));
123 }
124
125
126 int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp * _p, int value, Jim_Nvp ** result)
127 {
128 const Jim_Nvp *p;
129
130 p = Jim_Nvp_value2name_simple(_p, value);
131
132 if (result) {
133 *result = (Jim_Nvp *) (p);
134 }
135
136 if (p->name) {
137 return JIM_OK;
138 }
139 else {
140 return JIM_ERR;
141 }
142 }
143
144
145 int Jim_GetOpt_Setup(Jim_GetOptInfo * p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
146 {
147 memset(p, 0, sizeof(*p));
148 p->interp = interp;
149 p->argc = argc;
150 p->argv = argv;
151
152 return JIM_OK;
153 }
154
155 void Jim_GetOpt_Debug(Jim_GetOptInfo * p)
156 {
157 int x;
158
159 fprintf(stderr, "---args---\n");
160 for (x = 0; x < p->argc; x++) {
161 fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
162 }
163 fprintf(stderr, "-------\n");
164 }
165
166
167 int Jim_GetOpt_Obj(Jim_GetOptInfo * goi, Jim_Obj **puthere)
168 {
169 Jim_Obj *o;
170
171 o = NULL; // failure
172 if (goi->argc) {
173 // success
174 o = goi->argv[0];
175 goi->argc -= 1;
176 goi->argv += 1;
177 }
178 if (puthere) {
179 *puthere = o;
180 }
181 if (o != NULL) {
182 return JIM_OK;
183 }
184 else {
185 return JIM_ERR;
186 }
187 }
188
189 int Jim_GetOpt_String(Jim_GetOptInfo * goi, char **puthere, int *len)
190 {
191 int r;
192 Jim_Obj *o;
193 const char *cp;
194
195
196 r = Jim_GetOpt_Obj(goi, &o);
197 if (r == JIM_OK) {
198 cp = Jim_GetString(o, len);
199 if (puthere) {
200 /* remove const */
201 *puthere = (char *)(cp);
202 }
203 }
204 return r;
205 }
206
207 int Jim_GetOpt_Double(Jim_GetOptInfo * goi, double *puthere)
208 {
209 int r;
210 Jim_Obj *o;
211 double _safe;
212
213 if (puthere == NULL) {
214 puthere = &_safe;
215 }
216
217 r = Jim_GetOpt_Obj(goi, &o);
218 if (r == JIM_OK) {
219 r = Jim_GetDouble(goi->interp, o, puthere);
220 if (r != JIM_OK) {
221 Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
222 }
223 }
224 return r;
225 }
226
227 int Jim_GetOpt_Wide(Jim_GetOptInfo * goi, jim_wide * puthere)
228 {
229 int r;
230 Jim_Obj *o;
231 jim_wide _safe;
232
233 if (puthere == NULL) {
234 puthere = &_safe;
235 }
236
237 r = Jim_GetOpt_Obj(goi, &o);
238 if (r == JIM_OK) {
239 r = Jim_GetWide(goi->interp, o, puthere);
240 }
241 return r;
242 }
243
244 int Jim_GetOpt_Nvp(Jim_GetOptInfo * goi, const Jim_Nvp * nvp, Jim_Nvp ** puthere)
245 {
246 Jim_Nvp *_safe;
247 Jim_Obj *o;
248 int e;
249
250 if (puthere == NULL) {
251 puthere = &_safe;
252 }
253
254 e = Jim_GetOpt_Obj(goi, &o);
255 if (e == JIM_OK) {
256 e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);
257 }
258
259 return e;
260 }
261
262 void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo * goi, const Jim_Nvp * nvptable, int hadprefix)
263 {
264 if (hadprefix) {
265 Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
266 }
267 else {
268 Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
269 }
270 }
271
272
273 int Jim_GetOpt_Enum(Jim_GetOptInfo * goi, const char *const *lookup, int *puthere)
274 {
275 int _safe;
276 Jim_Obj *o;
277 int e;
278
279 if (puthere == NULL) {
280 puthere = &_safe;
281 }
282 e = Jim_GetOpt_Obj(goi, &o);
283 if (e == JIM_OK) {
284 e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
285 }
286 return e;
287 }
288
289 void
290 Jim_SetResult_NvpUnknown(Jim_Interp *interp,
291 Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp * nvp)
292 {
293 if (param_name) {
294 Jim_SetResultFormatted(interp, "%#s: Unknown: %#s, try one of: ", param_name, param_value);
295 }
296 else {
297 Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
298 }
299 while (nvp->name) {
300 const char *a;
301 const char *b;
302
303 if ((nvp + 1)->name) {
304 a = nvp->name;
305 b = ", ";
306 }
307 else {
308 a = "or ";
309 b = nvp->name;
310 }
311 Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
312 nvp++;
313 }
314 }
315
316 const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
317 {
318 static Jim_Obj *debug_string_obj;
319
320 int x;
321
322 if (debug_string_obj) {
323 Jim_FreeObj(interp, debug_string_obj);
324 }
325
326 debug_string_obj = Jim_NewEmptyStringObj(interp);
327 for (x = 0; x < argc; x++) {
328 Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
329 }
330
331 return Jim_String(debug_string_obj);
332 }
333
334 int Jim_nvpInit(Jim_Interp *interp)
335 {
336 /* This is really a helper library, not an extension, but this is the easy way */
337 return JIM_OK;
338 }