Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / usbmodeswitch / jim / jim-nvp.c
blobbe94f15911d59a20bd8ff16a37e07a1401b3aae5
1 #include <string.h>
2 #include <jim-nvp.h>
4 int Jim_GetNvp(Jim_Interp *interp,
5 Jim_Obj *objPtr, const Jim_Nvp * nvp_table, const Jim_Nvp ** result)
7 Jim_Nvp *n;
8 int e;
10 e = Jim_Nvp_name2value_obj(interp, nvp_table, objPtr, &n);
11 if (e == JIM_ERR) {
12 return e;
15 /* Success? found? */
16 if (n->name) {
17 /* remove const */
18 *result = (Jim_Nvp *) n;
19 return JIM_OK;
21 else {
22 return JIM_ERR;
26 Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp * p, const char *name)
28 while (p->name) {
29 if (0 == strcmp(name, p->name)) {
30 break;
32 p++;
34 return ((Jim_Nvp *) (p));
37 Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp * p, const char *name)
39 while (p->name) {
40 if (0 == strcasecmp(name, p->name)) {
41 break;
43 p++;
45 return ((Jim_Nvp *) (p));
48 int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
50 return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
54 int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp * _p, const char *name, Jim_Nvp ** result)
56 const Jim_Nvp *p;
58 p = Jim_Nvp_name2value_simple(_p, name);
60 /* result */
61 if (result) {
62 *result = (Jim_Nvp *) (p);
65 /* found? */
66 if (p->name) {
67 return JIM_OK;
69 else {
70 return JIM_ERR;
74 int
75 Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** puthere)
77 return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
80 int
81 Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp * _p, const char *name,
82 Jim_Nvp ** puthere)
84 const Jim_Nvp *p;
86 p = Jim_Nvp_name2value_nocase_simple(_p, name);
88 if (puthere) {
89 *puthere = (Jim_Nvp *) (p);
91 /* found */
92 if (p->name) {
93 return JIM_OK;
95 else {
96 return JIM_ERR;
101 int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
103 int e;;
104 jim_wide w;
106 e = Jim_GetWide(interp, o, &w);
107 if (e != JIM_OK) {
108 return e;
111 return Jim_Nvp_value2name(interp, p, w, result);
114 Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp * p, int value)
116 while (p->name) {
117 if (value == p->value) {
118 break;
120 p++;
122 return ((Jim_Nvp *) (p));
126 int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp * _p, int value, Jim_Nvp ** result)
128 const Jim_Nvp *p;
130 p = Jim_Nvp_value2name_simple(_p, value);
132 if (result) {
133 *result = (Jim_Nvp *) (p);
136 if (p->name) {
137 return JIM_OK;
139 else {
140 return JIM_ERR;
145 int Jim_GetOpt_Setup(Jim_GetOptInfo * p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
147 memset(p, 0, sizeof(*p));
148 p->interp = interp;
149 p->argc = argc;
150 p->argv = argv;
152 return JIM_OK;
155 void Jim_GetOpt_Debug(Jim_GetOptInfo * p)
157 int x;
159 fprintf(stderr, "---args---\n");
160 for (x = 0; x < p->argc; x++) {
161 fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
163 fprintf(stderr, "-------\n");
167 int Jim_GetOpt_Obj(Jim_GetOptInfo * goi, Jim_Obj **puthere)
169 Jim_Obj *o;
171 o = NULL; // failure
172 if (goi->argc) {
173 // success
174 o = goi->argv[0];
175 goi->argc -= 1;
176 goi->argv += 1;
178 if (puthere) {
179 *puthere = o;
181 if (o != NULL) {
182 return JIM_OK;
184 else {
185 return JIM_ERR;
189 int Jim_GetOpt_String(Jim_GetOptInfo * goi, char **puthere, int *len)
191 int r;
192 Jim_Obj *o;
193 const char *cp;
196 r = Jim_GetOpt_Obj(goi, &o);
197 if (r == JIM_OK) {
198 cp = Jim_GetString(o, len);
199 if (puthere) {
200 /* remove const */
201 *puthere = (char *)(cp);
204 return r;
207 int Jim_GetOpt_Double(Jim_GetOptInfo * goi, double *puthere)
209 int r;
210 Jim_Obj *o;
211 double _safe;
213 if (puthere == NULL) {
214 puthere = &_safe;
217 r = Jim_GetOpt_Obj(goi, &o);
218 if (r == JIM_OK) {
219 r = Jim_GetDouble(goi->interp, o, puthere);
220 if (r != JIM_OK) {
221 Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
224 return r;
227 int Jim_GetOpt_Wide(Jim_GetOptInfo * goi, jim_wide * puthere)
229 int r;
230 Jim_Obj *o;
231 jim_wide _safe;
233 if (puthere == NULL) {
234 puthere = &_safe;
237 r = Jim_GetOpt_Obj(goi, &o);
238 if (r == JIM_OK) {
239 r = Jim_GetWide(goi->interp, o, puthere);
241 return r;
244 int Jim_GetOpt_Nvp(Jim_GetOptInfo * goi, const Jim_Nvp * nvp, Jim_Nvp ** puthere)
246 Jim_Nvp *_safe;
247 Jim_Obj *o;
248 int e;
250 if (puthere == NULL) {
251 puthere = &_safe;
254 e = Jim_GetOpt_Obj(goi, &o);
255 if (e == JIM_OK) {
256 e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);
259 return e;
262 void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo * goi, const Jim_Nvp * nvptable, int hadprefix)
264 if (hadprefix) {
265 Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
267 else {
268 Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
273 int Jim_GetOpt_Enum(Jim_GetOptInfo * goi, const char *const *lookup, int *puthere)
275 int _safe;
276 Jim_Obj *o;
277 int e;
279 if (puthere == NULL) {
280 puthere = &_safe;
282 e = Jim_GetOpt_Obj(goi, &o);
283 if (e == JIM_OK) {
284 e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
286 return e;
289 void
290 Jim_SetResult_NvpUnknown(Jim_Interp *interp,
291 Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp * nvp)
293 if (param_name) {
294 Jim_SetResultFormatted(interp, "%#s: Unknown: %#s, try one of: ", param_name, param_value);
296 else {
297 Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
299 while (nvp->name) {
300 const char *a;
301 const char *b;
303 if ((nvp + 1)->name) {
304 a = nvp->name;
305 b = ", ";
307 else {
308 a = "or ";
309 b = nvp->name;
311 Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
312 nvp++;
316 const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
318 static Jim_Obj *debug_string_obj;
320 int x;
322 if (debug_string_obj) {
323 Jim_FreeObj(interp, debug_string_obj);
326 debug_string_obj = Jim_NewEmptyStringObj(interp);
327 for (x = 0; x < argc; x++) {
328 Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
331 return Jim_String(debug_string_obj);
334 int Jim_nvpInit(Jim_Interp *interp)
336 /* This is really a helper library, not an extension, but this is the easy way */
337 return JIM_OK;