stm32f1x: fix bug in flash loader and restrict instruction set to armv6-m
[openocd/andreasf.git] / src / helper / jim-nvp.h
blob4428f5d2fc62bfe255fe21b52fc002ecc89ec6b7
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
17 * are met:
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.
44 #ifndef JIM_NVP_H
45 #define JIM_NVP_H
47 #include <jim.h>
49 /** Name Value Pairs, aka: NVP
50 * - Given a string - return the associated int.
51 * - Given a number - return the associated string.
52 * .
54 * Very useful when the number is not a simple index into an array of
55 * known string, or there may be multiple strings (aliases) that mean then same
56 * thing.
58 * An NVP Table is terminated with ".name = NULL".
60 * During the 'name2value' operation, if no matching string is found
61 * the pointer to the terminal element (with p->name == NULL) is returned.
63 * Example:
64 * \code
65 * const Jim_Nvp yn[] = {
66 * { "yes", 1 },
67 * { "no" , 0 },
68 * { "yep", 1 },
69 * { "nope", 0 },
70 * { NULL, -1 },
71 * };
73 * Jim_Nvp *result
74 * e = Jim_Nvp_name2value(interp, yn, "y", &result);
75 * returns &yn[0];
76 * e = Jim_Nvp_name2value(interp, yn, "n", &result);
77 * returns &yn[1];
78 * e = Jim_Nvp_name2value(interp, yn, "Blah", &result);
79 * returns &yn[4];
80 * \endcode
82 * During the number2name operation, the first matching value is returned.
84 typedef struct {
85 const char *name;
86 int value;
87 } Jim_Nvp;
90 int Jim_GetNvp (Jim_Interp *interp,
91 Jim_Obj *objPtr,
92 const Jim_Nvp *nvp_table,
93 const Jim_Nvp **result);
95 /* Name Value Pairs Operations */
96 Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp *nvp_table, const char *name);
97 Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp *nvp_table, const char *name);
98 Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp *nvp_table, int v);
100 int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp *nvp_table, const char *name, Jim_Nvp **result);
101 int Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp *nvp_table, const char *name, Jim_Nvp **result);
102 int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp *nvp_table, int value, Jim_Nvp **result);
104 int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *name_obj, Jim_Nvp **result);
105 int Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *name_obj, Jim_Nvp **result);
106 int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *value_obj, Jim_Nvp **result);
108 /** prints a nice 'unknown' parameter error message to the 'result' */
109 void Jim_SetResult_NvpUnknown(Jim_Interp *interp,
110 Jim_Obj *param_name,
111 Jim_Obj *param_value,
112 const Jim_Nvp *nvp_table);
115 /** Debug: convert argc/argv into a printable string for printf() debug
117 * \param interp - the interpeter
118 * \param argc - arg count
119 * \param argv - the objects
121 * \returns string pointer holding the text.
123 * Note, next call to this function will free the old (last) string.
125 * For example might want do this:
126 * \code
127 * fp = fopen("some.file.log", "a");
128 * fprintf(fp, "PARAMS are: %s\n", Jim_DebugArgvString(interp, argc, argv));
129 * fclose(fp);
130 * \endcode
132 const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
135 /** A TCL -ish GetOpt like code.
137 * Some TCL objects have various "configuration" values.
138 * For example - in Tcl/Tk the "buttons" have many options.
140 * Usefull when dealing with command options.
141 * that may come in any order...
143 * Does not support "-foo = 123" type options.
144 * Only supports tcl type options, like "-foo 123"
147 typedef struct jim_getopt {
148 Jim_Interp *interp;
149 int argc;
150 Jim_Obj * const * argv;
151 int isconfigure; /* non-zero if configure */
152 } Jim_GetOptInfo;
154 /** GetOpt - how to.
156 * Example (short and incomplete):
157 * \code
158 * Jim_GetOptInfo goi;
160 * Jim_GetOpt_Setup(&goi, interp, argc, argv);
162 * while (goi.argc) {
163 * e = Jim_GetOpt_Nvp(&goi, nvp_options, &n);
164 * if (e != JIM_OK) {
165 * Jim_GetOpt_NvpUnknown(&goi, nvp_options, 0);
166 * return e;
169 * switch (n->value) {
170 * case ALIVE:
171 * printf("Option ALIVE specified\n");
172 * break;
173 * case FIRST:
174 * if (goi.argc < 1) {
175 * .. not enough args error ..
177 * Jim_GetOpt_String(&goi, &cp, NULL);
178 * printf("FIRSTNAME: %s\n", cp);
179 * case AGE:
180 * Jim_GetOpt_Wide(&goi, &w);
181 * printf("AGE: %d\n", (int)(w));
182 * break;
183 * case POLITICS:
184 * e = Jim_GetOpt_Nvp(&goi, nvp_politics, &n);
185 * if (e != JIM_OK) {
186 * Jim_GetOpt_NvpUnknown(&goi, nvp_politics, 1);
187 * return e;
192 * \endcode
196 /** Setup GETOPT
198 * \param goi - get opt info to be initialized
199 * \param interp - jim interp
200 * \param argc - argc count.
201 * \param argv - argv (will be copied)
203 * \code
204 * Jim_GetOptInfo goi;
206 * Jim_GetOptSetup(&goi, interp, argc, argv);
207 * \endcode
210 int Jim_GetOpt_Setup(Jim_GetOptInfo *goi,
211 Jim_Interp *interp,
212 int argc,
213 Jim_Obj * const * argv);
216 /** Debug - Dump parameters to stderr
217 * \param goi - current parameters
219 void Jim_GetOpt_Debug(Jim_GetOptInfo *goi);
223 /** Remove argv[0] from the list.
225 * \param goi - get opt info
226 * \param puthere - where param is put
229 int Jim_GetOpt_Obj(Jim_GetOptInfo *goi, Jim_Obj **puthere);
231 /** Remove argv[0] as string.
233 * \param goi - get opt info
234 * \param puthere - where param is put
235 * \param len - return its length
237 int Jim_GetOpt_String(Jim_GetOptInfo *goi, char **puthere, int *len);
239 /** Remove argv[0] as double.
241 * \param goi - get opt info
242 * \param puthere - where param is put.
245 int Jim_GetOpt_Double(Jim_GetOptInfo *goi, double *puthere);
247 /** Remove argv[0] as wide.
249 * \param goi - get opt info
250 * \param puthere - where param is put.
252 int Jim_GetOpt_Wide(Jim_GetOptInfo *goi, jim_wide *puthere);
254 /** Remove argv[0] as NVP.
256 * \param goi - get opt info
257 * \param lookup - nvp lookup table
258 * \param puthere - where param is put.
261 int Jim_GetOpt_Nvp(Jim_GetOptInfo *goi, const Jim_Nvp *lookup, Jim_Nvp **puthere);
263 /** Create an appropriate error message for an NVP.
265 * \param goi - options info
266 * \param lookup - the NVP table that was used.
267 * \param hadprefix - 0 or 1 if the option had a prefix.
269 * This function will set the "interp->result" to a human readable
270 * error message listing the available options.
272 * This function assumes the previous option argv[-1] is the unknown string.
274 * If this option had some prefix, then pass "hadprefix = 1" else pass "hadprefix = 0"
276 * Example:
277 * \code
279 * while (goi.argc) {
280 * // Get the next option
281 * e = Jim_GetOpt_Nvp(&goi, cmd_options, &n);
282 * if (e != JIM_OK) {
283 * // option was not recognized
284 * // pass 'hadprefix = 0' because there is no prefix
285 * Jim_GetOpt_NvpUnknown(&goi, cmd_options, 0);
286 * return e;
289 * switch (n->value) {
290 * case OPT_SEX:
291 * // handle: --sex male | female | lots | needmore
292 * e = Jim_GetOpt_Nvp(&goi, &nvp_sex, &n);
293 * if (e != JIM_OK) {
294 * Jim_GetOpt_NvpUnknown(&ogi, nvp_sex, 1);
295 * return e;
297 * printf("Code: (%d) is %s\n", n->value, n->name);
298 * break;
299 * case ...:
300 * [snip]
303 * \endcode
306 void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo *goi, const Jim_Nvp *lookup, int hadprefix);
309 /** Remove argv[0] as Enum
311 * \param goi - get opt info
312 * \param lookup - lookup table.
313 * \param puthere - where param is put.
316 int Jim_GetOpt_Enum(Jim_GetOptInfo *goi, const char * const * lookup, int *puthere);
318 #endif