OpenOCD
jim-nvp.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-2-Clause-Views
2 
3 /* Jim - A small embeddable Tcl interpreter
4  *
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.
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "jim-nvp.h"
24 #include <stdio.h>
25 #include <string.h>
26 
27 int jim_get_nvp(Jim_Interp *interp,
28  Jim_Obj *objptr, const struct jim_nvp *nvp_table, const struct jim_nvp **result)
29 {
30  struct jim_nvp *n;
31  int e;
32 
33  e = jim_nvp_name2value_obj(interp, nvp_table, objptr, &n);
34  if (e == JIM_ERR)
35  return e;
36 
37  /* Success? found? */
38  if (n->name) {
39  /* remove const */
40  *result = (struct jim_nvp *)n;
41  return JIM_OK;
42  } else
43  return JIM_ERR;
44 }
45 
46 struct jim_nvp *jim_nvp_name2value_simple(const struct jim_nvp *p, const char *name)
47 {
48  while (p->name) {
49  if (strcmp(name, p->name) == 0)
50  break;
51  p++;
52  }
53  return (struct jim_nvp *)p;
54 }
55 
56 struct jim_nvp *jim_nvp_name2value_nocase_simple(const struct jim_nvp *p, const char *name)
57 {
58  while (p->name) {
59  if (strcasecmp(name, p->name) == 0)
60  break;
61  p++;
62  }
63  return (struct jim_nvp *)p;
64 }
65 
66 int jim_nvp_name2value_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
67 {
68  return jim_nvp_name2value(interp, p, Jim_String(o), result);
69 }
70 
71 int jim_nvp_name2value(Jim_Interp *interp, const struct jim_nvp *_p, const char *name, struct jim_nvp **result)
72 {
73  const struct jim_nvp *p;
74 
76 
77  /* result */
78  if (result)
79  *result = (struct jim_nvp *)p;
80 
81  /* found? */
82  if (p->name)
83  return JIM_OK;
84  else
85  return JIM_ERR;
86 }
87 
88 int jim_nvp_name2value_obj_nocase(Jim_Interp *interp,
89  const struct jim_nvp *p,
90  Jim_Obj *o,
91  struct jim_nvp **puthere)
92 {
93  return jim_nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
94 }
95 
96 int jim_nvp_name2value_nocase(Jim_Interp *interp, const struct jim_nvp *_p, const char *name,
97  struct jim_nvp **puthere)
98 {
99  const struct jim_nvp *p;
100 
102 
103  if (puthere)
104  *puthere = (struct jim_nvp *)p;
105  /* found */
106  if (p->name)
107  return JIM_OK;
108  else
109  return JIM_ERR;
110 }
111 
112 int jim_nvp_value2name_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
113 {
114  int e;
115  jim_wide w;
116 
117  e = Jim_GetWide(interp, o, &w);
118  if (e != JIM_OK)
119  return e;
120 
121  return jim_nvp_value2name(interp, p, w, result);
122 }
123 
124 struct jim_nvp *jim_nvp_value2name_simple(const struct jim_nvp *p, int value)
125 {
126  while (p->name) {
127  if (value == p->value)
128  break;
129  p++;
130  }
131  return (struct jim_nvp *)p;
132 }
133 
134 int jim_nvp_value2name(Jim_Interp *interp, const struct jim_nvp *_p, int value, struct jim_nvp **result)
135 {
136  const struct jim_nvp *p;
137 
139 
140  if (result)
141  *result = (struct jim_nvp *)p;
142 
143  if (p->name)
144  return JIM_OK;
145  else
146  return JIM_ERR;
147 }
148 
149 int jim_getopt_setup(struct jim_getopt_info *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
150 {
151  memset(p, 0, sizeof(*p));
152  p->interp = interp;
153  p->argc = argc;
154  p->argv = argv;
155 
156  return JIM_OK;
157 }
158 
160 {
161  int x;
162 
163  fprintf(stderr, "---args---\n");
164  for (x = 0; x < p->argc; x++)
165  fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
166  fprintf(stderr, "-------\n");
167 }
168 
169 int jim_getopt_obj(struct jim_getopt_info *goi, Jim_Obj **puthere)
170 {
171  Jim_Obj *o;
172 
173  o = NULL; /* failure */
174  if (goi->argc) {
175  /* success */
176  o = goi->argv[0];
177  goi->argc -= 1;
178  goi->argv += 1;
179  }
180  if (puthere)
181  *puthere = o;
182  if (o)
183  return JIM_OK;
184  else
185  return JIM_ERR;
186 }
187 
188 int jim_getopt_string(struct jim_getopt_info *goi, const char **puthere, int *len)
189 {
190  int r;
191  Jim_Obj *o;
192  const char *cp;
193 
194  r = jim_getopt_obj(goi, &o);
195  if (r == JIM_OK) {
196  cp = Jim_GetString(o, len);
197  if (puthere) {
198  *puthere = cp;
199  }
200  }
201  return r;
202 }
203 
204 int jim_getopt_double(struct jim_getopt_info *goi, double *puthere)
205 {
206  int r;
207  Jim_Obj *o;
208  double _safe;
209 
210  if (!puthere)
211  puthere = &_safe;
212 
213  r = jim_getopt_obj(goi, &o);
214  if (r == JIM_OK) {
215  r = Jim_GetDouble(goi->interp, o, puthere);
216  if (r != JIM_OK)
217  Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
218  }
219  return r;
220 }
221 
222 int jim_getopt_wide(struct jim_getopt_info *goi, jim_wide *puthere)
223 {
224  int r;
225  Jim_Obj *o;
226  jim_wide _safe;
227 
228  if (!puthere)
229  puthere = &_safe;
230 
231  r = jim_getopt_obj(goi, &o);
232  if (r == JIM_OK)
233  r = Jim_GetWide(goi->interp, o, puthere);
234  return r;
235 }
236 
237 int jim_getopt_nvp(struct jim_getopt_info *goi, const struct jim_nvp *nvp, struct jim_nvp **puthere)
238 {
239  struct jim_nvp *_safe;
240  Jim_Obj *o;
241  int e;
242 
243  if (!puthere)
244  puthere = &_safe;
245 
246  e = jim_getopt_obj(goi, &o);
247  if (e == JIM_OK)
248  e = jim_nvp_name2value_obj(goi->interp, nvp, o, puthere);
249 
250  return e;
251 }
252 
253 void jim_getopt_nvp_unknown(struct jim_getopt_info *goi, const struct jim_nvp *nvptable, int hadprefix)
254 {
255  if (hadprefix)
256  jim_set_result_nvp_unknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
257  else
258  jim_set_result_nvp_unknown(goi->interp, NULL, goi->argv[-1], nvptable);
259 }
260 
261 int jim_getopt_enum(struct jim_getopt_info *goi, const char *const *lookup, int *puthere)
262 {
263  int _safe;
264  Jim_Obj *o;
265  int e;
266 
267  if (!puthere)
268  puthere = &_safe;
269  e = jim_getopt_obj(goi, &o);
270  if (e == JIM_OK)
271  e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
272  return e;
273 }
274 
275 void jim_set_result_nvp_unknown(Jim_Interp *interp,
276  Jim_Obj *param_name, Jim_Obj *param_value, const struct jim_nvp *nvp)
277 {
278  if (param_name)
279  Jim_SetResultFormatted(interp,
280  "%#s: Unknown: %#s, try one of: ",
281  param_name,
282  param_value);
283  else
284  Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
285  while (nvp->name) {
286  const char *a;
287  const char *b;
288 
289  if ((nvp + 1)->name) {
290  a = nvp->name;
291  b = ", ";
292  } else {
293  a = "or ";
294  b = nvp->name;
295  }
296  Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
297  nvp++;
298  }
299 }
300 
301 const char *jim_debug_argv_string(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
302 {
303  static Jim_Obj *debug_string_obj;
304 
305  int x;
306 
307  if (debug_string_obj)
308  Jim_FreeObj(interp, debug_string_obj);
309 
310  debug_string_obj = Jim_NewEmptyStringObj(interp);
311  for (x = 0; x < argc; x++)
312  Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
313 
314  return Jim_String(debug_string_obj);
315 }
const char * name
Definition: armv4_5.c:76
int jim_nvp_value2name_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
Definition: jim-nvp.c:112
int jim_nvp_name2value_obj_nocase(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **puthere)
Definition: jim-nvp.c:88
int jim_getopt_wide(struct jim_getopt_info *goi, jim_wide *puthere)
Remove argv[0] as wide.
Definition: jim-nvp.c:222
int jim_getopt_setup(struct jim_getopt_info *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
GetOpt - how to.
Definition: jim-nvp.c:149
int jim_get_nvp(Jim_Interp *interp, Jim_Obj *objptr, const struct jim_nvp *nvp_table, const struct jim_nvp **result)
Definition: jim-nvp.c:27
int jim_nvp_name2value_nocase(Jim_Interp *interp, const struct jim_nvp *_p, const char *name, struct jim_nvp **puthere)
Definition: jim-nvp.c:96
struct jim_nvp * jim_nvp_name2value_simple(const struct jim_nvp *p, const char *name)
Definition: jim-nvp.c:46
int jim_getopt_string(struct jim_getopt_info *goi, const char **puthere, int *len)
Remove argv[0] as string.
Definition: jim-nvp.c:188
struct jim_nvp * jim_nvp_name2value_nocase_simple(const struct jim_nvp *p, const char *name)
Definition: jim-nvp.c:56
int jim_getopt_enum(struct jim_getopt_info *goi, const char *const *lookup, int *puthere)
Remove argv[0] as Enum.
Definition: jim-nvp.c:261
int jim_getopt_nvp(struct jim_getopt_info *goi, const struct jim_nvp *nvp, struct jim_nvp **puthere)
Remove argv[0] as NVP.
Definition: jim-nvp.c:237
int jim_nvp_name2value(Jim_Interp *interp, const struct jim_nvp *_p, const char *name, struct jim_nvp **result)
Definition: jim-nvp.c:71
void jim_getopt_nvp_unknown(struct jim_getopt_info *goi, const struct jim_nvp *nvptable, int hadprefix)
Create an appropriate error message for an NVP.
Definition: jim-nvp.c:253
int jim_nvp_name2value_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
Definition: jim-nvp.c:66
void jim_getopt_debug(struct jim_getopt_info *p)
Debug - Dump parameters to stderr.
Definition: jim-nvp.c:159
int jim_getopt_obj(struct jim_getopt_info *goi, Jim_Obj **puthere)
Remove argv[0] from the list.
Definition: jim-nvp.c:169
struct jim_nvp * jim_nvp_value2name_simple(const struct jim_nvp *p, int value)
Definition: jim-nvp.c:124
const char * jim_debug_argv_string(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Debug: convert argc/argv into a printable string for printf() debug.
Definition: jim-nvp.c:301
int jim_nvp_value2name(Jim_Interp *interp, const struct jim_nvp *_p, int value, struct jim_nvp **result)
Definition: jim-nvp.c:134
void jim_set_result_nvp_unknown(Jim_Interp *interp, Jim_Obj *param_name, Jim_Obj *param_value, const struct jim_nvp *nvp)
prints a nice 'unknown' parameter error message to the 'result'
Definition: jim-nvp.c:275
int jim_getopt_double(struct jim_getopt_info *goi, double *puthere)
Remove argv[0] as double.
Definition: jim-nvp.c:204
A TCL -ish GetOpt like code.
Definition: jim-nvp.h:135
Jim_Interp * interp
Definition: jim-nvp.h:136
Jim_Obj *const * argv
Definition: jim-nvp.h:138
Name Value Pairs, aka: NVP.
Definition: jim-nvp.h:59
const char * name
Definition: jim-nvp.h:60
int value
Definition: jim-nvp.h:61
Name Value Pairs, aka: NVP.
Definition: nvp.h:61
const char * name
Definition: nvp.h:62
#define NULL
Definition: usb.h:16