2022-08-30 10:01:12 -05:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause-Views
|
2022-06-27 03:31:09 -05:00
|
|
|
|
2011-10-21 03:51:21 -05:00
|
|
|
/* 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
|
2022-06-27 03:31:09 -05:00
|
|
|
* Copyright (c) 2005-2011 Jim Tcl Project. All rights reserved.
|
2011-10-21 03:51:21 -05:00
|
|
|
*/
|
|
|
|
|
2022-02-24 04:39:15 -06:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2021-08-29 04:18:01 -05:00
|
|
|
#include "jim-nvp.h"
|
2011-09-29 01:12:41 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_get_nvp(Jim_Interp *interp,
|
|
|
|
Jim_Obj *objptr, const struct jim_nvp *nvp_table, const struct jim_nvp **result)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2021-04-24 17:06:00 -05:00
|
|
|
struct jim_nvp *n;
|
2012-01-30 08:31:21 -06:00
|
|
|
int e;
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
e = jim_nvp_name2value_obj(interp, nvp_table, objptr, &n);
|
2012-01-30 08:31:21 -06:00
|
|
|
if (e == JIM_ERR)
|
|
|
|
return e;
|
|
|
|
|
|
|
|
/* Success? found? */
|
|
|
|
if (n->name) {
|
|
|
|
/* remove const */
|
2021-04-24 17:06:00 -05:00
|
|
|
*result = (struct jim_nvp *)n;
|
2012-01-30 08:31:21 -06:00
|
|
|
return JIM_OK;
|
|
|
|
} else
|
|
|
|
return JIM_ERR;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
struct jim_nvp *jim_nvp_name2value_simple(const struct jim_nvp *p, const char *name)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
while (p->name) {
|
2021-07-03 10:18:53 -05:00
|
|
|
if (strcmp(name, p->name) == 0)
|
2012-01-30 08:31:21 -06:00
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
}
|
2021-04-24 17:06:00 -05:00
|
|
|
return (struct jim_nvp *)p;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
struct jim_nvp *jim_nvp_name2value_nocase_simple(const struct jim_nvp *p, const char *name)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
while (p->name) {
|
2021-07-03 10:18:53 -05:00
|
|
|
if (strcasecmp(name, p->name) == 0)
|
2012-01-30 08:31:21 -06:00
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
}
|
2021-04-24 17:06:00 -05:00
|
|
|
return (struct jim_nvp *)p;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_nvp_name2value_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2021-04-24 17:06:00 -05:00
|
|
|
return jim_nvp_name2value(interp, p, Jim_String(o), result);
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_nvp_name2value(Jim_Interp *interp, const struct jim_nvp *_p, const char *name, struct jim_nvp **result)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2021-04-24 17:06:00 -05:00
|
|
|
const struct jim_nvp *p;
|
2012-01-30 08:31:21 -06:00
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
p = jim_nvp_name2value_simple(_p, name);
|
2012-01-30 08:31:21 -06:00
|
|
|
|
|
|
|
/* result */
|
|
|
|
if (result)
|
2021-04-24 17:06:00 -05:00
|
|
|
*result = (struct jim_nvp *)p;
|
2012-01-30 08:31:21 -06:00
|
|
|
|
|
|
|
/* found? */
|
|
|
|
if (p->name)
|
|
|
|
return JIM_OK;
|
|
|
|
else
|
|
|
|
return JIM_ERR;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_nvp_name2value_obj_nocase(Jim_Interp *interp,
|
|
|
|
const struct jim_nvp *p,
|
2012-01-30 08:31:21 -06:00
|
|
|
Jim_Obj *o,
|
2021-04-24 17:06:00 -05:00
|
|
|
struct jim_nvp **puthere)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2021-04-24 17:06:00 -05:00
|
|
|
return jim_nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_nvp_name2value_nocase(Jim_Interp *interp, const struct jim_nvp *_p, const char *name,
|
|
|
|
struct jim_nvp **puthere)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2021-04-24 17:06:00 -05:00
|
|
|
const struct jim_nvp *p;
|
2012-01-30 08:31:21 -06:00
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
p = jim_nvp_name2value_nocase_simple(_p, name);
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
if (puthere)
|
2021-04-24 17:06:00 -05:00
|
|
|
*puthere = (struct jim_nvp *)p;
|
2012-01-30 08:31:21 -06:00
|
|
|
/* found */
|
|
|
|
if (p->name)
|
|
|
|
return JIM_OK;
|
|
|
|
else
|
|
|
|
return JIM_ERR;
|
|
|
|
}
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_nvp_value2name_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
int e;
|
|
|
|
jim_wide w;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
e = Jim_GetWide(interp, o, &w);
|
|
|
|
if (e != JIM_OK)
|
|
|
|
return e;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
return jim_nvp_value2name(interp, p, w, result);
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
struct jim_nvp *jim_nvp_value2name_simple(const struct jim_nvp *p, int value)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
while (p->name) {
|
|
|
|
if (value == p->value)
|
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
}
|
2021-04-24 17:06:00 -05:00
|
|
|
return (struct jim_nvp *)p;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_nvp_value2name(Jim_Interp *interp, const struct jim_nvp *_p, int value, struct jim_nvp **result)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2021-04-24 17:06:00 -05:00
|
|
|
const struct jim_nvp *p;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
p = jim_nvp_value2name_simple(_p, value);
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
if (result)
|
2021-04-24 17:06:00 -05:00
|
|
|
*result = (struct jim_nvp *)p;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
if (p->name)
|
|
|
|
return JIM_OK;
|
|
|
|
else
|
|
|
|
return JIM_ERR;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_getopt_setup(struct jim_getopt_info *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
memset(p, 0, sizeof(*p));
|
|
|
|
p->interp = interp;
|
|
|
|
p->argc = argc;
|
|
|
|
p->argv = argv;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
return JIM_OK;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
void jim_getopt_debug(struct jim_getopt_info *p)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
int x;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
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");
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_getopt_obj(struct jim_getopt_info *goi, Jim_Obj **puthere)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
Jim_Obj *o;
|
|
|
|
|
|
|
|
o = NULL; /* failure */
|
|
|
|
if (goi->argc) {
|
|
|
|
/* success */
|
|
|
|
o = goi->argv[0];
|
|
|
|
goi->argc -= 1;
|
|
|
|
goi->argv += 1;
|
|
|
|
}
|
|
|
|
if (puthere)
|
|
|
|
*puthere = o;
|
2021-07-03 11:51:20 -05:00
|
|
|
if (o)
|
2012-01-30 08:31:21 -06:00
|
|
|
return JIM_OK;
|
|
|
|
else
|
|
|
|
return JIM_ERR;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_getopt_string(struct jim_getopt_info *goi, const char **puthere, int *len)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
int r;
|
|
|
|
Jim_Obj *o;
|
|
|
|
const char *cp;
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
r = jim_getopt_obj(goi, &o);
|
2012-01-30 08:31:21 -06:00
|
|
|
if (r == JIM_OK) {
|
|
|
|
cp = Jim_GetString(o, len);
|
|
|
|
if (puthere) {
|
2015-12-13 15:18:14 -06:00
|
|
|
*puthere = cp;
|
2012-01-30 08:31:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_getopt_double(struct jim_getopt_info *goi, double *puthere)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
int r;
|
|
|
|
Jim_Obj *o;
|
|
|
|
double _safe;
|
|
|
|
|
2021-07-03 11:51:20 -05:00
|
|
|
if (!puthere)
|
2012-01-30 08:31:21 -06:00
|
|
|
puthere = &_safe;
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
r = jim_getopt_obj(goi, &o);
|
2012-01-30 08:31:21 -06:00
|
|
|
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;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_getopt_wide(struct jim_getopt_info *goi, jim_wide *puthere)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
int r;
|
|
|
|
Jim_Obj *o;
|
|
|
|
jim_wide _safe;
|
|
|
|
|
2021-07-03 11:51:20 -05:00
|
|
|
if (!puthere)
|
2012-01-30 08:31:21 -06:00
|
|
|
puthere = &_safe;
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
r = jim_getopt_obj(goi, &o);
|
2012-01-30 08:31:21 -06:00
|
|
|
if (r == JIM_OK)
|
|
|
|
r = Jim_GetWide(goi->interp, o, puthere);
|
|
|
|
return r;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_getopt_nvp(struct jim_getopt_info *goi, const struct jim_nvp *nvp, struct jim_nvp **puthere)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2021-04-24 17:06:00 -05:00
|
|
|
struct jim_nvp *_safe;
|
2012-01-30 08:31:21 -06:00
|
|
|
Jim_Obj *o;
|
|
|
|
int e;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2021-07-03 11:51:20 -05:00
|
|
|
if (!puthere)
|
2012-01-30 08:31:21 -06:00
|
|
|
puthere = &_safe;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
e = jim_getopt_obj(goi, &o);
|
2012-01-30 08:31:21 -06:00
|
|
|
if (e == JIM_OK)
|
2021-04-24 17:06:00 -05:00
|
|
|
e = jim_nvp_name2value_obj(goi->interp, nvp, o, puthere);
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
return e;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
void jim_getopt_nvp_unknown(struct jim_getopt_info *goi, const struct jim_nvp *nvptable, int hadprefix)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
if (hadprefix)
|
2021-04-24 17:06:00 -05:00
|
|
|
jim_set_result_nvp_unknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
|
2012-01-30 08:31:21 -06:00
|
|
|
else
|
2021-04-24 17:06:00 -05:00
|
|
|
jim_set_result_nvp_unknown(goi->interp, NULL, goi->argv[-1], nvptable);
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
int jim_getopt_enum(struct jim_getopt_info *goi, const char *const *lookup, int *puthere)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
int _safe;
|
|
|
|
Jim_Obj *o;
|
|
|
|
int e;
|
|
|
|
|
2021-07-03 11:51:20 -05:00
|
|
|
if (!puthere)
|
2012-01-30 08:31:21 -06:00
|
|
|
puthere = &_safe;
|
2021-04-24 17:06:00 -05:00
|
|
|
e = jim_getopt_obj(goi, &o);
|
2012-01-30 08:31:21 -06:00
|
|
|
if (e == JIM_OK)
|
|
|
|
e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
|
|
|
|
return e;
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
void jim_set_result_nvp_unknown(Jim_Interp *interp,
|
|
|
|
Jim_Obj *param_name, Jim_Obj *param_value, const struct jim_nvp *nvp)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
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++;
|
|
|
|
}
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|
|
|
|
|
2021-04-24 17:06:00 -05:00
|
|
|
const char *jim_debug_argv_string(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
|
2011-09-29 01:12:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
static Jim_Obj *debug_string_obj;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
int x;
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
if (debug_string_obj)
|
|
|
|
Jim_FreeObj(interp, debug_string_obj);
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
debug_string_obj = Jim_NewEmptyStringObj(interp);
|
|
|
|
for (x = 0; x < argc; x++)
|
|
|
|
Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
|
2011-09-29 01:12:41 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
return Jim_String(debug_string_obj);
|
2011-09-29 01:12:41 -05:00
|
|
|
}
|