flash/nor/rp2040: check target halted before flash operation
[openocd.git] / src / helper / jim-nvp.h
blob11824cabf4e0d272a3b31da2271a98dcea233329
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
22 #include <jim.h>
24 /** Name Value Pairs, aka: NVP
25 * - Given a string - return the associated int.
26 * - Given a number - return the associated string.
27 * .
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
31 * thing.
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.
38 * Example:
39 * \code
40 * const struct jim_nvp yn[] = {
41 * { "yes", 1 },
42 * { "no" , 0 },
43 * { "yep", 1 },
44 * { "nope", 0 },
45 * { NULL, -1 },
46 * };
48 * struct jim_nvp *result
49 * e = jim_nvp_name2value(interp, yn, "y", &result);
50 * returns &yn[0];
51 * e = jim_nvp_name2value(interp, yn, "n", &result);
52 * returns &yn[1];
53 * e = jim_nvp_name2value(interp, yn, "Blah", &result);
54 * returns &yn[4];
55 * \endcode
57 * During the number2name operation, the first matching value is returned.
59 struct jim_nvp {
60 const char *name;
61 int value;
64 int jim_get_nvp(Jim_Interp *interp,
65 Jim_Obj *objptr,
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,
76 const char *name,
77 struct jim_nvp **result);
78 int jim_nvp_name2value_nocase(Jim_Interp *interp,
79 const struct jim_nvp *nvp_table,
80 const char *name,
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,
86 Jim_Obj *name_obj,
87 struct jim_nvp **result);
88 int jim_nvp_name2value_obj_nocase(Jim_Interp *interp,
89 const struct jim_nvp *nvp_table,
90 Jim_Obj *name_obj,
91 struct jim_nvp **result);
92 int jim_nvp_value2name_obj(Jim_Interp *interp,
93 const struct jim_nvp *nvp_table,
94 Jim_Obj *value_obj,
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,
99 Jim_Obj *param_name,
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:
114 * \code
115 * fp = fopen("some.file.log", "a");
116 * fprintf(fp, "PARAMS are: %s\n", Jim_DebugArgvString(interp, argc, argv));
117 * fclose(fp);
118 * \endcode
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 {
136 Jim_Interp *interp;
137 int argc;
138 Jim_Obj *const *argv;
139 int isconfigure; /* non-zero if configure */
142 /** GetOpt - how to.
144 * Example (short and incomplete):
145 * \code
146 * struct jim_getopt_info goi;
148 * jim_getopt_setup(&goi, interp, argc, argv);
150 * while (goi.argc) {
151 * e = jim_getopt_nvp(&goi, nvp_options, &n);
152 * if (e != JIM_OK) {
153 * jim_getopt_nvp_unknown(&goi, nvp_options, 0);
154 * return e;
157 * switch (n->value) {
158 * case ALIVE:
159 * printf("Option ALIVE specified\n");
160 * break;
161 * case FIRST:
162 * if (goi.argc < 1) {
163 * .. not enough args error ..
165 * jim_getopt_string(&goi, &cp, NULL);
166 * printf("FIRSTNAME: %s\n", cp);
167 * case AGE:
168 * jim_getopt_wide(&goi, &w);
169 * printf("AGE: %d\n", (int)(w));
170 * break;
171 * case POLITICS:
172 * e = jim_getopt_nvp(&goi, nvp_politics, &n);
173 * if (e != JIM_OK) {
174 * jim_getopt_nvp_unknown(&goi, nvp_politics, 1);
175 * return e;
180 * \endcode
184 /** Setup GETOPT
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)
191 * \code
192 * struct jim_getopt_info goi;
194 * Jim_GetOptSetup(&goi, interp, argc, argv);
195 * \endcode
198 int jim_getopt_setup(struct jim_getopt_info *goi,
199 Jim_Interp *interp,
200 int argc,
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"
262 * Example:
263 * \code
265 * while (goi.argc) {
266 * // Get the next option
267 * e = jim_getopt_nvp(&goi, cmd_options, &n);
268 * if (e != JIM_OK) {
269 * // option was not recognized
270 * // pass 'hadprefix = 0' because there is no prefix
271 * jim_getopt_nvp_unknown(&goi, cmd_options, 0);
272 * return e;
275 * switch (n->value) {
276 * case OPT_SEX:
277 * // handle: --sex male | female | lots | needmore
278 * e = jim_getopt_nvp(&goi, &nvp_sex, &n);
279 * if (e != JIM_OK) {
280 * jim_getopt_nvp_unknown(&ogi, nvp_sex, 1);
281 * return e;
283 * printf("Code: (%d) is %s\n", n->value, n->name);
284 * break;
285 * case ...:
286 * [snip]
289 * \endcode
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 */