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.
47 int jim_get_nvp(Jim_Interp
*interp
,
48 Jim_Obj
*objptr
, const struct jim_nvp
*nvp_table
, const struct jim_nvp
**result
)
53 e
= jim_nvp_name2value_obj(interp
, nvp_table
, objptr
, &n
);
60 *result
= (struct jim_nvp
*)n
;
66 struct jim_nvp
*jim_nvp_name2value_simple(const struct jim_nvp
*p
, const char *name
)
69 if (strcmp(name
, p
->name
) == 0)
73 return (struct jim_nvp
*)p
;
76 struct jim_nvp
*jim_nvp_name2value_nocase_simple(const struct jim_nvp
*p
, const char *name
)
79 if (strcasecmp(name
, p
->name
) == 0)
83 return (struct jim_nvp
*)p
;
86 int jim_nvp_name2value_obj(Jim_Interp
*interp
, const struct jim_nvp
*p
, Jim_Obj
*o
, struct jim_nvp
**result
)
88 return jim_nvp_name2value(interp
, p
, Jim_String(o
), result
);
91 int jim_nvp_name2value(Jim_Interp
*interp
, const struct jim_nvp
*_p
, const char *name
, struct jim_nvp
**result
)
93 const struct jim_nvp
*p
;
95 p
= jim_nvp_name2value_simple(_p
, name
);
99 *result
= (struct jim_nvp
*)p
;
108 int jim_nvp_name2value_obj_nocase(Jim_Interp
*interp
,
109 const struct jim_nvp
*p
,
111 struct jim_nvp
**puthere
)
113 return jim_nvp_name2value_nocase(interp
, p
, Jim_String(o
), puthere
);
116 int jim_nvp_name2value_nocase(Jim_Interp
*interp
, const struct jim_nvp
*_p
, const char *name
,
117 struct jim_nvp
**puthere
)
119 const struct jim_nvp
*p
;
121 p
= jim_nvp_name2value_nocase_simple(_p
, name
);
124 *puthere
= (struct jim_nvp
*)p
;
132 int jim_nvp_value2name_obj(Jim_Interp
*interp
, const struct jim_nvp
*p
, Jim_Obj
*o
, struct jim_nvp
**result
)
137 e
= Jim_GetWide(interp
, o
, &w
);
141 return jim_nvp_value2name(interp
, p
, w
, result
);
144 struct jim_nvp
*jim_nvp_value2name_simple(const struct jim_nvp
*p
, int value
)
147 if (value
== p
->value
)
151 return (struct jim_nvp
*)p
;
154 int jim_nvp_value2name(Jim_Interp
*interp
, const struct jim_nvp
*_p
, int value
, struct jim_nvp
**result
)
156 const struct jim_nvp
*p
;
158 p
= jim_nvp_value2name_simple(_p
, value
);
161 *result
= (struct jim_nvp
*)p
;
169 int jim_getopt_setup(struct jim_getopt_info
*p
, Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
171 memset(p
, 0, sizeof(*p
));
179 void jim_getopt_debug(struct jim_getopt_info
*p
)
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");
189 int jim_getopt_obj(struct jim_getopt_info
*goi
, Jim_Obj
**puthere
)
193 o
= NULL
; /* failure */
208 int jim_getopt_string(struct jim_getopt_info
*goi
, const char **puthere
, int *len
)
214 r
= jim_getopt_obj(goi
, &o
);
216 cp
= Jim_GetString(o
, len
);
224 int jim_getopt_double(struct jim_getopt_info
*goi
, double *puthere
)
233 r
= jim_getopt_obj(goi
, &o
);
235 r
= Jim_GetDouble(goi
->interp
, o
, puthere
);
237 Jim_SetResultFormatted(goi
->interp
, "not a number: %#s", o
);
242 int jim_getopt_wide(struct jim_getopt_info
*goi
, jim_wide
*puthere
)
251 r
= jim_getopt_obj(goi
, &o
);
253 r
= Jim_GetWide(goi
->interp
, o
, puthere
);
257 int jim_getopt_nvp(struct jim_getopt_info
*goi
, const struct jim_nvp
*nvp
, struct jim_nvp
**puthere
)
259 struct jim_nvp
*_safe
;
266 e
= jim_getopt_obj(goi
, &o
);
268 e
= jim_nvp_name2value_obj(goi
->interp
, nvp
, o
, puthere
);
273 void jim_getopt_nvp_unknown(struct jim_getopt_info
*goi
, const struct jim_nvp
*nvptable
, int hadprefix
)
276 jim_set_result_nvp_unknown(goi
->interp
, goi
->argv
[-2], goi
->argv
[-1], nvptable
);
278 jim_set_result_nvp_unknown(goi
->interp
, NULL
, goi
->argv
[-1], nvptable
);
281 int jim_getopt_enum(struct jim_getopt_info
*goi
, const char *const *lookup
, int *puthere
)
289 e
= jim_getopt_obj(goi
, &o
);
291 e
= Jim_GetEnum(goi
->interp
, o
, lookup
, puthere
, "option", JIM_ERRMSG
);
295 void jim_set_result_nvp_unknown(Jim_Interp
*interp
,
296 Jim_Obj
*param_name
, Jim_Obj
*param_value
, const struct jim_nvp
*nvp
)
299 Jim_SetResultFormatted(interp
,
300 "%#s: Unknown: %#s, try one of: ",
304 Jim_SetResultFormatted(interp
, "Unknown param: %#s, try one of: ", param_value
);
309 if ((nvp
+ 1)->name
) {
316 Jim_AppendStrings(interp
, Jim_GetResult(interp
), a
, b
, NULL
);
321 const char *jim_debug_argv_string(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
323 static Jim_Obj
*debug_string_obj
;
327 if (debug_string_obj
)
328 Jim_FreeObj(interp
, debug_string_obj
);
330 debug_string_obj
= Jim_NewEmptyStringObj(interp
);
331 for (x
= 0; x
< argc
; x
++)
332 Jim_AppendStrings(interp
, debug_string_obj
, Jim_String(argv
[x
]), " ", NULL
);
334 return Jim_String(debug_string_obj
);