aboutsummaryrefslogtreecommitdiff
path: root/src/helper/jim-nvp.c
blob: d13bdfba4caea1c4328efd510cf263a0214c068a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* Jim - A small embeddable Tcl interpreter
 *
 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
 * Copyright 2008 Steve Bennett <steveb@workware.net.au>
 * Copyright 2009 Nico Coesel <ncoesel@dealogic.nl>
 * Copyright 2009 Zachary T Welch zw@superlucidity.net
 * Copyright 2009 David Brownell
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation
 * are those of the authors and should not be interpreted as representing
 * official policies, either expressed or implied, of the Jim Tcl Project.
 */

#include <string.h>
#include <jim-nvp.h>

int Jim_GetNvp(Jim_Interp *interp,
	Jim_Obj *objPtr, const Jim_Nvp *nvp_table, const Jim_Nvp **result)
{
	Jim_Nvp *n;
	int e;

	e = Jim_Nvp_name2value_obj(interp, nvp_table, objPtr, &n);
	if (e == JIM_ERR)
		return e;

	/* Success? found? */
	if (n->name) {
		/* remove const */
		*result = (Jim_Nvp *) n;
		return JIM_OK;
	} else
		return JIM_ERR;
}

Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp *p, const char *name)
{
	while (p->name) {
		if (0 == strcmp(name, p->name))
			break;
		p++;
	}
	return (Jim_Nvp *) (p);
}

Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp *p, const char *name)
{
	while (p->name) {
		if (0 == strcasecmp(name, p->name))
			break;
		p++;
	}
	return (Jim_Nvp *) (p);
}

int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp *p, Jim_Obj *o, Jim_Nvp **result)
{
	return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
}

int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp *_p, const char *name, Jim_Nvp **result)
{
	const Jim_Nvp *p;

	p = Jim_Nvp_name2value_simple(_p, name);

	/* result */
	if (result)
		*result = (Jim_Nvp *) (p);

	/* found? */
	if (p->name)
		return JIM_OK;
	else
		return JIM_ERR;
}

int Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp,
	const Jim_Nvp *p,
	Jim_Obj *o,
	Jim_Nvp **puthere)
{
	return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
}

int Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp *_p, const char *name,
	Jim_Nvp **puthere)
{
	const Jim_Nvp *p;

	p = Jim_Nvp_name2value_nocase_simple(_p, name);

	if (puthere)
		*puthere = (Jim_Nvp *) (p);
						/* found */
	if (p->name)
		return JIM_OK;
	else
		return JIM_ERR;
}

int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp *p, Jim_Obj *o, Jim_Nvp **result)
{
	int e;
	jim_wide w;

	e = Jim_GetWide(interp, o, &w);
	if (e != JIM_OK)
		return e;

	return Jim_Nvp_value2name(interp, p, w, result);
}

Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp *p, int value)
{
	while (p->name) {
		if (value == p->value)
			break;
		p++;
	}
	return (Jim_Nvp *) (p);
}

int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp *_p, int value, Jim_Nvp **result)
{
	const Jim_Nvp *p;

	p = Jim_Nvp_value2name_simple(_p, value);

	if (result)
		*result = (Jim_Nvp *) (p);

	if (p->name)
		return JIM_OK;
	else
		return JIM_ERR;
}

int Jim_GetOpt_Setup(Jim_GetOptInfo *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	memset(p, 0, sizeof(*p));
	p->interp = interp;
	p->argc = argc;
	p->argv = argv;

	return JIM_OK;
}

void Jim_GetOpt_Debug(Jim_GetOptInfo *p)
{
	int x;

	fprintf(stderr, "---args---\n");
	for (x = 0; x < p->argc; x++)
		fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
	fprintf(stderr, "-------\n");
}

int Jim_GetOpt_Obj(Jim_GetOptInfo *goi, Jim_Obj **puthere)
{
	Jim_Obj *o;

	o = NULL;		/* failure */
	if (goi->argc) {
		/* success */
		o = goi->argv[0];
		goi->argc -= 1;
		goi->argv += 1;
	}
	if (puthere)
		*puthere = o;
	if (o != NULL)
		return JIM_OK;
	else
		return JIM_ERR;
}

int Jim_GetOpt_String(Jim_GetOptInfo *goi, const char **puthere, int *len)
{
	int r;
	Jim_Obj *o;
	const char *cp;

	r = Jim_GetOpt_Obj(goi, &o);
	if (r == JIM_OK) {
		cp = Jim_GetString(o, len);
		if (puthere) {
			*puthere = cp;
		}
	}
	return r;
}

int Jim_GetOpt_Double(Jim_GetOptInfo *goi, double *puthere)
{
	int r;
	Jim_Obj *o;
	double _safe;

	if (puthere == NULL)
		puthere = &_safe;

	r = Jim_GetOpt_Obj(goi, &o);
	if (r == JIM_OK) {
		r = Jim_GetDouble(goi->interp, o, puthere);
		if (r != JIM_OK)
			Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
	}
	return r;
}

int Jim_GetOpt_Wide(Jim_GetOptInfo *goi, jim_wide *puthere)
{
	int r;
	Jim_Obj *o;
	jim_wide _safe;

	if (puthere == NULL)
		puthere = &_safe;

	r = Jim_GetOpt_Obj(goi, &o);
	if (r == JIM_OK)
		r = Jim_GetWide(goi->interp, o, puthere);
	return r;
}

int Jim_GetOpt_Nvp(Jim_GetOptInfo *goi, const Jim_Nvp *nvp, Jim_Nvp **puthere)
{
	Jim_Nvp *_safe;
	Jim_Obj *o;
	int e;

	if (puthere == NULL)
		puthere = &_safe;

	e = Jim_GetOpt_Obj(goi, &o);
	if (e == JIM_OK)
		e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);

	return e;
}

void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo *goi, const Jim_Nvp *nvptable, int hadprefix)
{
	if (hadprefix)
		Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
	else
		Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
}

int Jim_GetOpt_Enum(Jim_GetOptInfo *goi, const char *const *lookup, int *puthere)
{
	int _safe;
	Jim_Obj *o;
	int e;

	if (puthere == NULL)
		puthere = &_safe;
	e = Jim_GetOpt_Obj(goi, &o);
	if (e == JIM_OK)
		e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
	return e;
}

void Jim_SetResult_NvpUnknown(Jim_Interp *interp,
	Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp *nvp)
{
	if (param_name)
		Jim_SetResultFormatted(interp,
			"%#s: Unknown: %#s, try one of: ",
			param_name,
			param_value);
	else
		Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
	while (nvp->name) {
		const char *a;
		const char *b;

		if ((nvp + 1)->name) {
			a = nvp->name;
			b = ", ";
		} else {
			a = "or ";
			b = nvp->name;
		}
		Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
		nvp++;
	}
}

const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	static Jim_Obj *debug_string_obj;

	int x;

	if (debug_string_obj)
		Jim_FreeObj(interp, debug_string_obj);

	debug_string_obj = Jim_NewEmptyStringObj(interp);
	for (x = 0; x < argc; x++)
		Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);

	return Jim_String(debug_string_obj);
}

int Jim_nvpInit(Jim_Interp *interp)
{
	/* This is really a helper library, not an extension, but this is the easy way */
	return JIM_OK;
}