1 // SPDX-License-Identifier: BSD-2-Clause-Views
3 /* Jim - A small embeddable Tcl interpreter
5 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
6 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
7 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
8 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
9 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
10 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
11 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
12 * Copyright 2008 Steve Bennett <steveb@workware.net.au>
13 * Copyright 2009 Nico Coesel <ncoesel@dealogic.nl>
14 * Copyright 2009 Zachary T Welch zw@superlucidity.net
15 * Copyright 2009 David Brownell
16 * Copyright (c) 2005-2011 Jim Tcl Project. All rights reserved.
26 int jim_get_nvp(Jim_Interp
*interp
,
27 Jim_Obj
*objptr
, const struct jim_nvp
*nvp_table
, const struct jim_nvp
**result
)
32 e
= jim_nvp_name2value_obj(interp
, nvp_table
, objptr
, &n
);
39 *result
= (struct jim_nvp
*)n
;
45 struct jim_nvp
*jim_nvp_name2value_simple(const struct jim_nvp
*p
, const char *name
)
48 if (strcmp(name
, p
->name
) == 0)
52 return (struct jim_nvp
*)p
;
55 struct jim_nvp
*jim_nvp_name2value_nocase_simple(const struct jim_nvp
*p
, const char *name
)
58 if (strcasecmp(name
, p
->name
) == 0)
62 return (struct jim_nvp
*)p
;
65 int jim_nvp_name2value_obj(Jim_Interp
*interp
, const struct jim_nvp
*p
, Jim_Obj
*o
, struct jim_nvp
**result
)
67 return jim_nvp_name2value(interp
, p
, Jim_String(o
), result
);
70 int jim_nvp_name2value(Jim_Interp
*interp
, const struct jim_nvp
*_p
, const char *name
, struct jim_nvp
**result
)
72 const struct jim_nvp
*p
;
74 p
= jim_nvp_name2value_simple(_p
, name
);
78 *result
= (struct jim_nvp
*)p
;
87 int jim_nvp_name2value_obj_nocase(Jim_Interp
*interp
,
88 const struct jim_nvp
*p
,
90 struct jim_nvp
**puthere
)
92 return jim_nvp_name2value_nocase(interp
, p
, Jim_String(o
), puthere
);
95 int jim_nvp_name2value_nocase(Jim_Interp
*interp
, const struct jim_nvp
*_p
, const char *name
,
96 struct jim_nvp
**puthere
)
98 const struct jim_nvp
*p
;
100 p
= jim_nvp_name2value_nocase_simple(_p
, name
);
103 *puthere
= (struct jim_nvp
*)p
;
111 int jim_nvp_value2name_obj(Jim_Interp
*interp
, const struct jim_nvp
*p
, Jim_Obj
*o
, struct jim_nvp
**result
)
116 e
= Jim_GetWide(interp
, o
, &w
);
120 return jim_nvp_value2name(interp
, p
, w
, result
);
123 struct jim_nvp
*jim_nvp_value2name_simple(const struct jim_nvp
*p
, int value
)
126 if (value
== p
->value
)
130 return (struct jim_nvp
*)p
;
133 int jim_nvp_value2name(Jim_Interp
*interp
, const struct jim_nvp
*_p
, int value
, struct jim_nvp
**result
)
135 const struct jim_nvp
*p
;
137 p
= jim_nvp_value2name_simple(_p
, value
);
140 *result
= (struct jim_nvp
*)p
;
148 int jim_getopt_setup(struct jim_getopt_info
*p
, Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
150 memset(p
, 0, sizeof(*p
));
158 void jim_getopt_debug(struct jim_getopt_info
*p
)
162 fprintf(stderr
, "---args---\n");
163 for (x
= 0; x
< p
->argc
; x
++)
164 fprintf(stderr
, "%2d) %s\n", x
, Jim_String(p
->argv
[x
]));
165 fprintf(stderr
, "-------\n");
168 int jim_getopt_obj(struct jim_getopt_info
*goi
, Jim_Obj
**puthere
)
172 o
= NULL
; /* failure */
187 int jim_getopt_string(struct jim_getopt_info
*goi
, const char **puthere
, int *len
)
193 r
= jim_getopt_obj(goi
, &o
);
195 cp
= Jim_GetString(o
, len
);
203 int jim_getopt_double(struct jim_getopt_info
*goi
, double *puthere
)
212 r
= jim_getopt_obj(goi
, &o
);
214 r
= Jim_GetDouble(goi
->interp
, o
, puthere
);
216 Jim_SetResultFormatted(goi
->interp
, "not a number: %#s", o
);
221 int jim_getopt_wide(struct jim_getopt_info
*goi
, jim_wide
*puthere
)
230 r
= jim_getopt_obj(goi
, &o
);
232 r
= Jim_GetWide(goi
->interp
, o
, puthere
);
236 int jim_getopt_nvp(struct jim_getopt_info
*goi
, const struct jim_nvp
*nvp
, struct jim_nvp
**puthere
)
238 struct jim_nvp
*_safe
;
245 e
= jim_getopt_obj(goi
, &o
);
247 e
= jim_nvp_name2value_obj(goi
->interp
, nvp
, o
, puthere
);
252 void jim_getopt_nvp_unknown(struct jim_getopt_info
*goi
, const struct jim_nvp
*nvptable
, int hadprefix
)
255 jim_set_result_nvp_unknown(goi
->interp
, goi
->argv
[-2], goi
->argv
[-1], nvptable
);
257 jim_set_result_nvp_unknown(goi
->interp
, NULL
, goi
->argv
[-1], nvptable
);
260 int jim_getopt_enum(struct jim_getopt_info
*goi
, const char *const *lookup
, int *puthere
)
268 e
= jim_getopt_obj(goi
, &o
);
270 e
= Jim_GetEnum(goi
->interp
, o
, lookup
, puthere
, "option", JIM_ERRMSG
);
274 void jim_set_result_nvp_unknown(Jim_Interp
*interp
,
275 Jim_Obj
*param_name
, Jim_Obj
*param_value
, const struct jim_nvp
*nvp
)
278 Jim_SetResultFormatted(interp
,
279 "%#s: Unknown: %#s, try one of: ",
283 Jim_SetResultFormatted(interp
, "Unknown param: %#s, try one of: ", param_value
);
288 if ((nvp
+ 1)->name
) {
295 Jim_AppendStrings(interp
, Jim_GetResult(interp
), a
, b
, NULL
);
300 const char *jim_debug_argv_string(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
302 static Jim_Obj
*debug_string_obj
;
306 if (debug_string_obj
)
307 Jim_FreeObj(interp
, debug_string_obj
);
309 debug_string_obj
= Jim_NewEmptyStringObj(interp
);
310 for (x
= 0; x
< argc
; x
++)
311 Jim_AppendStrings(interp
, debug_string_obj
, Jim_String(argv
[x
]), " ", NULL
);
313 return Jim_String(debug_string_obj
);