1 /* Jim - A small embeddable Tcl interpreter
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
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
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.
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.
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.
51 int jim_get_nvp(Jim_Interp *interp,
52 Jim_Obj *objptr, const struct jim_nvp *nvp_table, const struct jim_nvp **result)
57 e = jim_nvp_name2value_obj(interp, nvp_table, objptr, &n);
64 *result = (struct jim_nvp *)n;
70 struct jim_nvp *jim_nvp_name2value_simple(const struct jim_nvp *p, const char *name)
73 if (strcmp(name, p->name) == 0)
77 return (struct jim_nvp *)p;
80 struct jim_nvp *jim_nvp_name2value_nocase_simple(const struct jim_nvp *p, const char *name)
83 if (strcasecmp(name, p->name) == 0)
87 return (struct jim_nvp *)p;
90 int jim_nvp_name2value_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
92 return jim_nvp_name2value(interp, p, Jim_String(o), result);
95 int jim_nvp_name2value(Jim_Interp *interp, const struct jim_nvp *_p, const char *name, struct jim_nvp **result)
97 const struct jim_nvp *p;
99 p = jim_nvp_name2value_simple(_p, name);
103 *result = (struct jim_nvp *)p;
112 int jim_nvp_name2value_obj_nocase(Jim_Interp *interp,
113 const struct jim_nvp *p,
115 struct jim_nvp **puthere)
117 return jim_nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
120 int jim_nvp_name2value_nocase(Jim_Interp *interp, const struct jim_nvp *_p, const char *name,
121 struct jim_nvp **puthere)
123 const struct jim_nvp *p;
125 p = jim_nvp_name2value_nocase_simple(_p, name);
128 *puthere = (struct jim_nvp *)p;
136 int jim_nvp_value2name_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
141 e = Jim_GetWide(interp, o, &w);
145 return jim_nvp_value2name(interp, p, w, result);
148 struct jim_nvp *jim_nvp_value2name_simple(const struct jim_nvp *p, int value)
151 if (value == p->value)
155 return (struct jim_nvp *)p;
158 int jim_nvp_value2name(Jim_Interp *interp, const struct jim_nvp *_p, int value, struct jim_nvp **result)
160 const struct jim_nvp *p;
162 p = jim_nvp_value2name_simple(_p, value);
165 *result = (struct jim_nvp *)p;
173 int jim_getopt_setup(struct jim_getopt_info *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
175 memset(p, 0, sizeof(*p));
183 void jim_getopt_debug(struct jim_getopt_info *p)
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");
193 int jim_getopt_obj(struct jim_getopt_info *goi, Jim_Obj **puthere)
197 o = NULL; /* failure */
212 int jim_getopt_string(struct jim_getopt_info *goi, const char **puthere, int *len)
218 r = jim_getopt_obj(goi, &o);
220 cp = Jim_GetString(o, len);
228 int jim_getopt_double(struct jim_getopt_info *goi, double *puthere)
237 r = jim_getopt_obj(goi, &o);
239 r = Jim_GetDouble(goi->interp, o, puthere);
241 Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
246 int jim_getopt_wide(struct jim_getopt_info *goi, jim_wide *puthere)
255 r = jim_getopt_obj(goi, &o);
257 r = Jim_GetWide(goi->interp, o, puthere);
261 int jim_getopt_nvp(struct jim_getopt_info *goi, const struct jim_nvp *nvp, struct jim_nvp **puthere)
263 struct jim_nvp *_safe;
270 e = jim_getopt_obj(goi, &o);
272 e = jim_nvp_name2value_obj(goi->interp, nvp, o, puthere);
277 void jim_getopt_nvp_unknown(struct jim_getopt_info *goi, const struct jim_nvp *nvptable, int hadprefix)
280 jim_set_result_nvp_unknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
282 jim_set_result_nvp_unknown(goi->interp, NULL, goi->argv[-1], nvptable);
285 int jim_getopt_enum(struct jim_getopt_info *goi, const char *const *lookup, int *puthere)
293 e = jim_getopt_obj(goi, &o);
295 e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
299 void jim_set_result_nvp_unknown(Jim_Interp *interp,
300 Jim_Obj *param_name, Jim_Obj *param_value, const struct jim_nvp *nvp)
303 Jim_SetResultFormatted(interp,
304 "%#s: Unknown: %#s, try one of: ",
308 Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
313 if ((nvp + 1)->name) {
320 Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
325 const char *jim_debug_argv_string(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
327 static Jim_Obj *debug_string_obj;
331 if (debug_string_obj)
332 Jim_FreeObj(interp, debug_string_obj);
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);
338 return Jim_String(debug_string_obj);