You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

339 lines
6.8 KiB

  1. #include <string.h>
  2. #include <jim-nvp.h>
  3. int Jim_GetNvp(Jim_Interp *interp,
  4. Jim_Obj *objPtr, const Jim_Nvp * nvp_table, const Jim_Nvp ** result)
  5. {
  6. Jim_Nvp *n;
  7. int e;
  8. e = Jim_Nvp_name2value_obj(interp, nvp_table, objPtr, &n);
  9. if (e == JIM_ERR) {
  10. return e;
  11. }
  12. /* Success? found? */
  13. if (n->name) {
  14. /* remove const */
  15. *result = (Jim_Nvp *) n;
  16. return JIM_OK;
  17. }
  18. else {
  19. return JIM_ERR;
  20. }
  21. }
  22. Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp * p, const char *name)
  23. {
  24. while (p->name) {
  25. if (0 == strcmp(name, p->name)) {
  26. break;
  27. }
  28. p++;
  29. }
  30. return ((Jim_Nvp *) (p));
  31. }
  32. Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp * p, const char *name)
  33. {
  34. while (p->name) {
  35. if (0 == strcasecmp(name, p->name)) {
  36. break;
  37. }
  38. p++;
  39. }
  40. return ((Jim_Nvp *) (p));
  41. }
  42. int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
  43. {
  44. return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
  45. }
  46. int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp * _p, const char *name, Jim_Nvp ** result)
  47. {
  48. const Jim_Nvp *p;
  49. p = Jim_Nvp_name2value_simple(_p, name);
  50. /* result */
  51. if (result) {
  52. *result = (Jim_Nvp *) (p);
  53. }
  54. /* found? */
  55. if (p->name) {
  56. return JIM_OK;
  57. }
  58. else {
  59. return JIM_ERR;
  60. }
  61. }
  62. int
  63. Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** puthere)
  64. {
  65. return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
  66. }
  67. int
  68. Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp * _p, const char *name,
  69. Jim_Nvp ** puthere)
  70. {
  71. const Jim_Nvp *p;
  72. p = Jim_Nvp_name2value_nocase_simple(_p, name);
  73. if (puthere) {
  74. *puthere = (Jim_Nvp *) (p);
  75. }
  76. /* found */
  77. if (p->name) {
  78. return JIM_OK;
  79. }
  80. else {
  81. return JIM_ERR;
  82. }
  83. }
  84. int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
  85. {
  86. int e;;
  87. jim_wide w;
  88. e = Jim_GetWide(interp, o, &w);
  89. if (e != JIM_OK) {
  90. return e;
  91. }
  92. return Jim_Nvp_value2name(interp, p, w, result);
  93. }
  94. Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp * p, int value)
  95. {
  96. while (p->name) {
  97. if (value == p->value) {
  98. break;
  99. }
  100. p++;
  101. }
  102. return ((Jim_Nvp *) (p));
  103. }
  104. int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp * _p, int value, Jim_Nvp ** result)
  105. {
  106. const Jim_Nvp *p;
  107. p = Jim_Nvp_value2name_simple(_p, value);
  108. if (result) {
  109. *result = (Jim_Nvp *) (p);
  110. }
  111. if (p->name) {
  112. return JIM_OK;
  113. }
  114. else {
  115. return JIM_ERR;
  116. }
  117. }
  118. int Jim_GetOpt_Setup(Jim_GetOptInfo * p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  119. {
  120. memset(p, 0, sizeof(*p));
  121. p->interp = interp;
  122. p->argc = argc;
  123. p->argv = argv;
  124. return JIM_OK;
  125. }
  126. void Jim_GetOpt_Debug(Jim_GetOptInfo * p)
  127. {
  128. int x;
  129. fprintf(stderr, "---args---\n");
  130. for (x = 0; x < p->argc; x++) {
  131. fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
  132. }
  133. fprintf(stderr, "-------\n");
  134. }
  135. int Jim_GetOpt_Obj(Jim_GetOptInfo * goi, Jim_Obj **puthere)
  136. {
  137. Jim_Obj *o;
  138. o = NULL; // failure
  139. if (goi->argc) {
  140. // success
  141. o = goi->argv[0];
  142. goi->argc -= 1;
  143. goi->argv += 1;
  144. }
  145. if (puthere) {
  146. *puthere = o;
  147. }
  148. if (o != NULL) {
  149. return JIM_OK;
  150. }
  151. else {
  152. return JIM_ERR;
  153. }
  154. }
  155. int Jim_GetOpt_String(Jim_GetOptInfo * goi, char **puthere, int *len)
  156. {
  157. int r;
  158. Jim_Obj *o;
  159. const char *cp;
  160. r = Jim_GetOpt_Obj(goi, &o);
  161. if (r == JIM_OK) {
  162. cp = Jim_GetString(o, len);
  163. if (puthere) {
  164. /* remove const */
  165. *puthere = (char *)(cp);
  166. }
  167. }
  168. return r;
  169. }
  170. int Jim_GetOpt_Double(Jim_GetOptInfo * goi, double *puthere)
  171. {
  172. int r;
  173. Jim_Obj *o;
  174. double _safe;
  175. if (puthere == NULL) {
  176. puthere = &_safe;
  177. }
  178. r = Jim_GetOpt_Obj(goi, &o);
  179. if (r == JIM_OK) {
  180. r = Jim_GetDouble(goi->interp, o, puthere);
  181. if (r != JIM_OK) {
  182. Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
  183. }
  184. }
  185. return r;
  186. }
  187. int Jim_GetOpt_Wide(Jim_GetOptInfo * goi, jim_wide * puthere)
  188. {
  189. int r;
  190. Jim_Obj *o;
  191. jim_wide _safe;
  192. if (puthere == NULL) {
  193. puthere = &_safe;
  194. }
  195. r = Jim_GetOpt_Obj(goi, &o);
  196. if (r == JIM_OK) {
  197. r = Jim_GetWide(goi->interp, o, puthere);
  198. }
  199. return r;
  200. }
  201. int Jim_GetOpt_Nvp(Jim_GetOptInfo * goi, const Jim_Nvp * nvp, Jim_Nvp ** puthere)
  202. {
  203. Jim_Nvp *_safe;
  204. Jim_Obj *o;
  205. int e;
  206. if (puthere == NULL) {
  207. puthere = &_safe;
  208. }
  209. e = Jim_GetOpt_Obj(goi, &o);
  210. if (e == JIM_OK) {
  211. e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);
  212. }
  213. return e;
  214. }
  215. void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo * goi, const Jim_Nvp * nvptable, int hadprefix)
  216. {
  217. if (hadprefix) {
  218. Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
  219. }
  220. else {
  221. Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
  222. }
  223. }
  224. int Jim_GetOpt_Enum(Jim_GetOptInfo * goi, const char *const *lookup, int *puthere)
  225. {
  226. int _safe;
  227. Jim_Obj *o;
  228. int e;
  229. if (puthere == NULL) {
  230. puthere = &_safe;
  231. }
  232. e = Jim_GetOpt_Obj(goi, &o);
  233. if (e == JIM_OK) {
  234. e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
  235. }
  236. return e;
  237. }
  238. void
  239. Jim_SetResult_NvpUnknown(Jim_Interp *interp,
  240. Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp * nvp)
  241. {
  242. if (param_name) {
  243. Jim_SetResultFormatted(interp, "%#s: Unknown: %#s, try one of: ", param_name, param_value);
  244. }
  245. else {
  246. Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
  247. }
  248. while (nvp->name) {
  249. const char *a;
  250. const char *b;
  251. if ((nvp + 1)->name) {
  252. a = nvp->name;
  253. b = ", ";
  254. }
  255. else {
  256. a = "or ";
  257. b = nvp->name;
  258. }
  259. Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
  260. nvp++;
  261. }
  262. }
  263. const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  264. {
  265. static Jim_Obj *debug_string_obj;
  266. int x;
  267. if (debug_string_obj) {
  268. Jim_FreeObj(interp, debug_string_obj);
  269. }
  270. debug_string_obj = Jim_NewEmptyStringObj(interp);
  271. for (x = 0; x < argc; x++) {
  272. Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
  273. }
  274. return Jim_String(debug_string_obj);
  275. }
  276. int Jim_nvpInit(Jim_Interp *interp)
  277. {
  278. /* This is really a helper library, not an extension, but this is the easy way */
  279. return JIM_OK;
  280. }