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.
19 #ifndef OPENOCD_HELPER_JIM_NVP_H
20 #define OPENOCD_HELPER_JIM_NVP_H
24 /** Name Value Pairs, aka: NVP
25 * - Given a string - return the associated int.
26 * - Given a number - return the associated string.
29 * Very useful when the number is not a simple index into an array of
30 * known string, or there may be multiple strings (aliases) that mean then same
33 * An NVP Table is terminated with ".name = NULL".
35 * During the 'name2value' operation, if no matching string is found
36 * the pointer to the terminal element (with p->name == NULL) is returned.
40 * const struct jim_nvp yn[] = {
48 * struct jim_nvp *result
49 * e = jim_nvp_name2value(interp, yn, "y", &result);
51 * e = jim_nvp_name2value(interp, yn, "n", &result);
53 * e = jim_nvp_name2value(interp, yn, "Blah", &result);
57 * During the number2name operation, the first matching value is returned.
64 int jim_get_nvp(Jim_Interp
*interp
,
66 const struct jim_nvp
*nvp_table
,
67 const struct jim_nvp
**result
);
69 /* Name Value Pairs Operations */
70 struct jim_nvp
*jim_nvp_name2value_simple(const struct jim_nvp
*nvp_table
, const char *name
);
71 struct jim_nvp
*jim_nvp_name2value_nocase_simple(const struct jim_nvp
*nvp_table
, const char *name
);
72 struct jim_nvp
*jim_nvp_value2name_simple(const struct jim_nvp
*nvp_table
, int v
);
74 int jim_nvp_name2value(Jim_Interp
*interp
,
75 const struct jim_nvp
*nvp_table
,
77 struct jim_nvp
**result
);
78 int jim_nvp_name2value_nocase(Jim_Interp
*interp
,
79 const struct jim_nvp
*nvp_table
,
81 struct jim_nvp
**result
);
82 int jim_nvp_value2name(Jim_Interp
*interp
, const struct jim_nvp
*nvp_table
, int value
, struct jim_nvp
**result
);
84 int jim_nvp_name2value_obj(Jim_Interp
*interp
,
85 const struct jim_nvp
*nvp_table
,
87 struct jim_nvp
**result
);
88 int jim_nvp_name2value_obj_nocase(Jim_Interp
*interp
,
89 const struct jim_nvp
*nvp_table
,
91 struct jim_nvp
**result
);
92 int jim_nvp_value2name_obj(Jim_Interp
*interp
,
93 const struct jim_nvp
*nvp_table
,
95 struct jim_nvp
**result
);
97 /** prints a nice 'unknown' parameter error message to the 'result' */
98 void jim_set_result_nvp_unknown(Jim_Interp
*interp
,
100 Jim_Obj
*param_value
,
101 const struct jim_nvp
*nvp_table
);
103 /** Debug: convert argc/argv into a printable string for printf() debug
105 * \param interp - the interpreter
106 * \param argc - arg count
107 * \param argv - the objects
109 * \returns string pointer holding the text.
111 * Note, next call to this function will free the old (last) string.
113 * For example might want do this:
115 * fp = fopen("some.file.log", "a");
116 * fprintf(fp, "PARAMS are: %s\n", Jim_DebugArgvString(interp, argc, argv));
120 const char *jim_debug_argv_string(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
123 /** A TCL -ish GetOpt like code.
125 * Some TCL objects have various "configuration" values.
126 * For example - in Tcl/Tk the "buttons" have many options.
128 * Useful when dealing with command options.
129 * that may come in any order...
131 * Does not support "-foo = 123" type options.
132 * Only supports tcl type options, like "-foo 123"
135 struct jim_getopt_info
{
138 Jim_Obj
*const *argv
;
139 int isconfigure
; /* non-zero if configure */
144 * Example (short and incomplete):
146 * struct jim_getopt_info goi;
148 * jim_getopt_setup(&goi, interp, argc, argv);
151 * e = jim_getopt_nvp(&goi, nvp_options, &n);
153 * jim_getopt_nvp_unknown(&goi, nvp_options, 0);
157 * switch (n->value) {
159 * printf("Option ALIVE specified\n");
162 * if (goi.argc < 1) {
163 * .. not enough args error ..
165 * jim_getopt_string(&goi, &cp, NULL);
166 * printf("FIRSTNAME: %s\n", cp);
168 * jim_getopt_wide(&goi, &w);
169 * printf("AGE: %d\n", (int)(w));
172 * e = jim_getopt_nvp(&goi, nvp_politics, &n);
174 * jim_getopt_nvp_unknown(&goi, nvp_politics, 1);
186 * \param goi - get opt info to be initialized
187 * \param interp - jim interp
188 * \param argc - argc count.
189 * \param argv - argv (will be copied)
192 * struct jim_getopt_info goi;
194 * Jim_GetOptSetup(&goi, interp, argc, argv);
198 int jim_getopt_setup(struct jim_getopt_info
*goi
,
201 Jim_Obj
*const *argv
);
204 /** Debug - Dump parameters to stderr
205 * \param goi - current parameters
207 void jim_getopt_debug(struct jim_getopt_info
*goi
);
209 /** Remove argv[0] from the list.
211 * \param goi - get opt info
212 * \param puthere - where param is put
215 int jim_getopt_obj(struct jim_getopt_info
*goi
, Jim_Obj
**puthere
);
217 /** Remove argv[0] as string.
219 * \param goi - get opt info
220 * \param puthere - where param is put
221 * \param len - return its length
223 int jim_getopt_string(struct jim_getopt_info
*goi
, const char **puthere
, int *len
);
225 /** Remove argv[0] as double.
227 * \param goi - get opt info
228 * \param puthere - where param is put.
231 int jim_getopt_double(struct jim_getopt_info
*goi
, double *puthere
);
233 /** Remove argv[0] as wide.
235 * \param goi - get opt info
236 * \param puthere - where param is put.
238 int jim_getopt_wide(struct jim_getopt_info
*goi
, jim_wide
*puthere
);
240 /** Remove argv[0] as NVP.
242 * \param goi - get opt info
243 * \param lookup - nvp lookup table
244 * \param puthere - where param is put.
247 int jim_getopt_nvp(struct jim_getopt_info
*goi
, const struct jim_nvp
*lookup
, struct jim_nvp
**puthere
);
249 /** Create an appropriate error message for an NVP.
251 * \param goi - options info
252 * \param lookup - the NVP table that was used.
253 * \param hadprefix - 0 or 1 if the option had a prefix.
255 * This function will set the "interp->result" to a human readable
256 * error message listing the available options.
258 * This function assumes the previous option argv[-1] is the unknown string.
260 * If this option had some prefix, then pass "hadprefix = 1" else pass "hadprefix = 0"
266 * // Get the next option
267 * e = jim_getopt_nvp(&goi, cmd_options, &n);
269 * // option was not recognized
270 * // pass 'hadprefix = 0' because there is no prefix
271 * jim_getopt_nvp_unknown(&goi, cmd_options, 0);
275 * switch (n->value) {
277 * // handle: --sex male | female | lots | needmore
278 * e = jim_getopt_nvp(&goi, &nvp_sex, &n);
280 * jim_getopt_nvp_unknown(&ogi, nvp_sex, 1);
283 * printf("Code: (%d) is %s\n", n->value, n->name);
292 void jim_getopt_nvp_unknown(struct jim_getopt_info
*goi
, const struct jim_nvp
*lookup
, int hadprefix
);
295 /** Remove argv[0] as Enum
297 * \param goi - get opt info
298 * \param lookup - lookup table.
299 * \param puthere - where param is put.
302 int jim_getopt_enum(struct jim_getopt_info
*goi
, const char *const *lookup
, int *puthere
);
304 #endif /* OPENOCD_HELPER_JIM_NVP_H */