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.
 
 
 
 
 
 

831 lines
20 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008, Duane Ellis *
  9. * openocd@duaneeellis.com *
  10. * *
  11. * part of this file is taken from libcli (libcli.sourceforge.net) *
  12. * Copyright (C) David Parrish (david@dparrish.com) *
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. * This program is distributed in the hope that it will be useful, *
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  22. * GNU General Public License for more details. *
  23. * *
  24. * You should have received a copy of the GNU General Public License *
  25. * along with this program; if not, write to the *
  26. * Free Software Foundation, Inc., *
  27. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  28. ***************************************************************************/
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include "replacements.h"
  33. #include "target.h"
  34. #include "command.h"
  35. #include "configuration.h"
  36. #include "log.h"
  37. #include "time_support.h"
  38. #include "jim-eventloop.h"
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include <stdarg.h>
  43. #include <stdio.h>
  44. #include <unistd.h>
  45. #include <errno.h>
  46. int fast_and_dangerous = 0;
  47. Jim_Interp *interp = NULL;
  48. int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  49. int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  50. int run_command(command_context_t *context, command_t *c, char *words[], int num_words);
  51. static void tcl_output(void *privData, const char *file, int line, const char *function, const char *string)
  52. {
  53. Jim_Obj *tclOutput=(Jim_Obj *)privData;
  54. Jim_AppendString(interp, tclOutput, string, strlen(string));
  55. }
  56. extern command_context_t *global_cmd_ctx;
  57. static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  58. {
  59. /* the private data is stashed in the interp structure */
  60. command_t *c;
  61. command_context_t *context;
  62. int retval;
  63. int i;
  64. int nwords;
  65. char **words;
  66. /* DANGER!!!! be careful what we invoke here, since interp->cmdPrivData might
  67. * get overwritten by running other Jim commands! Treat it as an
  68. * emphemeral global variable that is used in lieu of an argument
  69. * to the fn and fish it out manually.
  70. */
  71. c = interp->cmdPrivData;
  72. if (c==NULL)
  73. {
  74. LOG_ERROR("BUG: interp->cmdPrivData==NULL");
  75. return JIM_ERR;
  76. }
  77. target_call_timer_callbacks_now();
  78. LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
  79. LOG_DEBUG("script_command - %s", c->name);
  80. words = malloc(sizeof(char *) * argc);
  81. for (i = 0; i < argc; i++)
  82. {
  83. int len;
  84. const char *w=Jim_GetString(argv[i], &len);
  85. if (*w=='#')
  86. {
  87. /* hit an end of line comment */
  88. break;
  89. }
  90. words[i] = strdup(w);
  91. if (words[i] == NULL)
  92. {
  93. return JIM_ERR;
  94. }
  95. LOG_DEBUG("script_command - %s, argv[%u]=%s", c->name, i, words[i]);
  96. }
  97. nwords = i;
  98. /* grab the command context from the associated data */
  99. context = Jim_GetAssocData(interp, "context");
  100. if (context == NULL)
  101. {
  102. /* Tcl can invoke commands directly instead of via command_run_line(). This would
  103. * happen when the Jim Tcl interpreter is provided by eCos.
  104. */
  105. context = global_cmd_ctx;
  106. }
  107. /* capture log output and return it */
  108. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  109. /* a garbage collect can happen, so we need a reference count to this object */
  110. Jim_IncrRefCount(tclOutput);
  111. log_add_callback(tcl_output, tclOutput);
  112. retval = run_command(context, c, words, nwords);
  113. log_remove_callback(tcl_output, tclOutput);
  114. /* We dump output into this local variable */
  115. Jim_SetResult(interp, tclOutput);
  116. Jim_DecrRefCount(interp, tclOutput);
  117. for (i = 0; i < nwords; i++)
  118. free(words[i]);
  119. free(words);
  120. int *return_retval = Jim_GetAssocData(interp, "retval");
  121. if (return_retval != NULL)
  122. {
  123. *return_retval = retval;
  124. }
  125. return (retval==ERROR_OK)?JIM_OK:JIM_ERR;
  126. }
  127. /* nice short description of source file */
  128. #define __THIS__FILE__ "command.c"
  129. command_t* register_command(command_context_t *context, command_t *parent, char *name, int (*handler)(struct command_context_s *context, char* name, char** args, int argc), enum command_mode mode, char *help)
  130. {
  131. command_t *c, *p;
  132. if (!context || !name)
  133. return NULL;
  134. c = malloc(sizeof(command_t));
  135. c->name = strdup(name);
  136. c->parent = parent;
  137. c->children = NULL;
  138. c->handler = handler;
  139. c->mode = mode;
  140. if (!help)
  141. help="";
  142. c->next = NULL;
  143. /* place command in tree */
  144. if (parent)
  145. {
  146. if (parent->children)
  147. {
  148. /* find last child */
  149. for (p = parent->children; p && p->next; p = p->next);
  150. if (p)
  151. p->next = c;
  152. }
  153. else
  154. {
  155. parent->children = c;
  156. }
  157. }
  158. else
  159. {
  160. if (context->commands)
  161. {
  162. /* find last command */
  163. for (p = context->commands; p && p->next; p = p->next);
  164. if (p)
  165. p->next = c;
  166. }
  167. else
  168. {
  169. context->commands = c;
  170. }
  171. }
  172. /* just a placeholder, no handler */
  173. if (c->handler==NULL)
  174. return c;
  175. /* If this is a two level command, e.g. "flash banks", then the
  176. * "unknown" proc in startup.tcl must redirect to this command.
  177. *
  178. * "flash banks" is translated by "unknown" to "flash_banks"
  179. * if such a proc exists
  180. */
  181. /* Print help for command */
  182. const char *t1="";
  183. const char *t2="";
  184. const char *t3="";
  185. /* maximum of two levels :-) */
  186. if (c->parent!=NULL)
  187. {
  188. t1=c->parent->name;
  189. t2="_";
  190. }
  191. t3=c->name;
  192. const char *full_name=alloc_printf("ocd_%s%s%s", t1, t2, t3);
  193. Jim_CreateCommand(interp, full_name, script_command, c, NULL);
  194. free((void *)full_name);
  195. /* we now need to add an overrideable proc */
  196. const char *override_name=alloc_printf("proc %s%s%s {args} {if {[catch {eval \"ocd_%s%s%s $args\"}]==0} {return \"\"} else { return -code error }", t1, t2, t3, t1, t2, t3);
  197. Jim_Eval_Named(interp, override_name, __THIS__FILE__, __LINE__ );
  198. free((void *)override_name);
  199. /* accumulate help text in Tcl helptext list. */
  200. Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
  201. if (Jim_IsShared(helptext))
  202. helptext = Jim_DuplicateObj(interp, helptext);
  203. Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
  204. Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
  205. /* maximum of two levels :-) */
  206. if (c->parent!=NULL)
  207. {
  208. Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->parent->name, -1));
  209. }
  210. Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->name, -1));
  211. Jim_ListAppendElement(interp, cmd_entry, cmd_list);
  212. Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
  213. Jim_ListAppendElement(interp, helptext, cmd_entry);
  214. return c;
  215. }
  216. int unregister_all_commands(command_context_t *context)
  217. {
  218. command_t *c, *c2;
  219. if (context == NULL)
  220. return ERROR_OK;
  221. while(NULL != context->commands)
  222. {
  223. c = context->commands;
  224. while(NULL != c->children)
  225. {
  226. c2 = c->children;
  227. c->children = c->children->next;
  228. free(c2->name);
  229. c2->name = NULL;
  230. free(c2);
  231. c2 = NULL;
  232. }
  233. context->commands = context->commands->next;
  234. free(c->name);
  235. c->name = NULL;
  236. free(c);
  237. c = NULL;
  238. }
  239. return ERROR_OK;
  240. }
  241. int unregister_command(command_context_t *context, char *name)
  242. {
  243. command_t *c, *p = NULL, *c2;
  244. if ((!context) || (!name))
  245. return ERROR_INVALID_ARGUMENTS;
  246. /* find command */
  247. c = context->commands;
  248. while(NULL != c)
  249. {
  250. if (strcmp(name, c->name) == 0)
  251. {
  252. /* unlink command */
  253. if (p)
  254. {
  255. p->next = c->next;
  256. }
  257. else
  258. {
  259. /* first element in command list */
  260. context->commands = c->next;
  261. }
  262. /* unregister children */
  263. while(NULL != c->children)
  264. {
  265. c2 = c->children;
  266. c->children = c->children->next;
  267. free(c2->name);
  268. c2->name = NULL;
  269. free(c2);
  270. c2 = NULL;
  271. }
  272. /* delete command */
  273. free(c->name);
  274. c->name = NULL;
  275. free(c);
  276. c = NULL;
  277. return ERROR_OK;
  278. }
  279. /* remember the last command for unlinking */
  280. p = c;
  281. c = c->next;
  282. }
  283. return ERROR_OK;
  284. }
  285. void command_output_text(command_context_t *context, const char *data)
  286. {
  287. if( context && context->output_handler && data ){
  288. context->output_handler( context, data );
  289. }
  290. }
  291. void command_print_n(command_context_t *context, char *format, ...)
  292. {
  293. char *string;
  294. va_list ap;
  295. va_start(ap, format);
  296. string = alloc_vprintf(format, ap);
  297. if (string != NULL)
  298. {
  299. /* we want this collected in the log + we also want to pick it up as a tcl return
  300. * value.
  301. *
  302. * The latter bit isn't precisely neat, but will do for now.
  303. */
  304. LOG_USER_N("%s", string);
  305. // We already printed it above
  306. //command_output_text(context, string);
  307. free(string);
  308. }
  309. va_end(ap);
  310. }
  311. void command_print(command_context_t *context, char *format, ...)
  312. {
  313. char *string;
  314. va_list ap;
  315. va_start(ap, format);
  316. string = alloc_vprintf(format, ap);
  317. if (string != NULL)
  318. {
  319. strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
  320. /* we want this collected in the log + we also want to pick it up as a tcl return
  321. * value.
  322. *
  323. * The latter bit isn't precisely neat, but will do for now.
  324. */
  325. LOG_USER_N("%s", string);
  326. // We already printed it above
  327. //command_output_text(context, string);
  328. free(string);
  329. }
  330. va_end(ap);
  331. }
  332. int run_command(command_context_t *context, command_t *c, char *words[], int num_words)
  333. {
  334. int start_word=0;
  335. if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) ))
  336. {
  337. /* Config commands can not run after the config stage */
  338. LOG_ERROR("Illegal mode for command");
  339. return ERROR_FAIL;
  340. }
  341. int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
  342. if (retval == ERROR_COMMAND_SYNTAX_ERROR)
  343. {
  344. /* Print help for command */
  345. const char *t1="";
  346. const char *t2="";
  347. const char *t3="";
  348. /* maximum of two levels :-) */
  349. if (c->parent!=NULL)
  350. {
  351. t1=c->parent->name;
  352. t2=" ";
  353. }
  354. t3=c->name;
  355. command_run_linef(context, "help {%s%s%s}", t1, t2, t3);
  356. }
  357. else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
  358. {
  359. /* just fall through for a shutdown request */
  360. }
  361. else if (retval != ERROR_OK)
  362. {
  363. /* we do not print out an error message because the command *should*
  364. * have printed out an error
  365. */
  366. LOG_DEBUG("Command failed with error code %d", retval);
  367. }
  368. return retval;
  369. }
  370. int command_run_line(command_context_t *context, char *line)
  371. {
  372. /* all the parent commands have been registered with the interpreter
  373. * so, can just evaluate the line as a script and check for
  374. * results
  375. */
  376. /* run the line thru a script engine */
  377. int retval=ERROR_FAIL;
  378. int retcode;
  379. /* Beware! This code needs to be reentrant. It is also possible
  380. * for OpenOCD commands to be invoked directly from Tcl. This would
  381. * happen when the Jim Tcl interpreter is provided by eCos for
  382. * instance.
  383. */
  384. Jim_DeleteAssocData(interp, "context");
  385. retcode = Jim_SetAssocData(interp, "context", NULL, context);
  386. if (retcode == JIM_OK)
  387. {
  388. /* associated the return value */
  389. Jim_DeleteAssocData(interp, "retval");
  390. retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
  391. if (retcode == JIM_OK)
  392. {
  393. retcode = Jim_Eval_Named(interp, line, __THIS__FILE__, __LINE__ );
  394. Jim_DeleteAssocData(interp, "retval");
  395. }
  396. Jim_DeleteAssocData(interp, "context");
  397. }
  398. if (retcode == JIM_ERR) {
  399. if (retval!=ERROR_COMMAND_CLOSE_CONNECTION)
  400. {
  401. /* We do not print the connection closed error message */
  402. Jim_PrintErrorMessage(interp);
  403. }
  404. if (retval==ERROR_OK)
  405. {
  406. /* It wasn't a low level OpenOCD command that failed */
  407. return ERROR_FAIL;
  408. }
  409. return retval;
  410. } else if (retcode == JIM_EXIT) {
  411. /* ignore. */
  412. /* exit(Jim_GetExitCode(interp)); */
  413. } else {
  414. const char *result;
  415. int reslen;
  416. result = Jim_GetString(Jim_GetResult(interp), &reslen);
  417. if (reslen) {
  418. int i;
  419. char buff[256+1];
  420. for (i = 0; i < reslen; i += 256)
  421. {
  422. int chunk;
  423. chunk = reslen - i;
  424. if (chunk > 256)
  425. chunk = 256;
  426. strncpy(buff, result+i, chunk);
  427. buff[chunk] = 0;
  428. LOG_USER_N("%s", buff);
  429. }
  430. LOG_USER_N("%s", "\n");
  431. }
  432. }
  433. return retval;
  434. }
  435. int command_run_linef(command_context_t *context, char *format, ...)
  436. {
  437. int retval=ERROR_FAIL;
  438. char *string;
  439. va_list ap;
  440. va_start(ap, format);
  441. string = alloc_vprintf(format, ap);
  442. if (string!=NULL)
  443. {
  444. retval=command_run_line(context, string);
  445. }
  446. va_end(ap);
  447. return retval;
  448. }
  449. void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, const char* line), void *priv)
  450. {
  451. context->output_handler = output_handler;
  452. context->output_handler_priv = priv;
  453. }
  454. command_context_t* copy_command_context(command_context_t* context)
  455. {
  456. command_context_t* copy_context = malloc(sizeof(command_context_t));
  457. *copy_context = *context;
  458. return copy_context;
  459. }
  460. int command_done(command_context_t *context)
  461. {
  462. free(context);
  463. context = NULL;
  464. return ERROR_OK;
  465. }
  466. /* find full path to file */
  467. static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  468. {
  469. if (argc != 2)
  470. return JIM_ERR;
  471. const char *file = Jim_GetString(argv[1], NULL);
  472. char *full_path = find_file(file);
  473. if (full_path == NULL)
  474. return JIM_ERR;
  475. Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
  476. free(full_path);
  477. Jim_SetResult(interp, result);
  478. return JIM_OK;
  479. }
  480. static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  481. {
  482. if (argc != 2)
  483. return JIM_ERR;
  484. const char *str = Jim_GetString(argv[1], NULL);
  485. LOG_USER("%s", str);
  486. return JIM_OK;
  487. }
  488. static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
  489. {
  490. size_t nbytes;
  491. const char *ptr;
  492. Jim_Interp *interp;
  493. /* make it a char easier to read code */
  494. ptr = _ptr;
  495. interp = cookie;
  496. nbytes = size * n;
  497. if (ptr == NULL || interp == NULL || nbytes == 0) {
  498. return 0;
  499. }
  500. /* do we have to chunk it? */
  501. if (ptr[nbytes] == 0)
  502. {
  503. /* no it is a C style string */
  504. LOG_USER_N("%s", ptr);
  505. return strlen(ptr);
  506. }
  507. /* GRR we must chunk - not null terminated */
  508. while (nbytes) {
  509. char chunk[128+1];
  510. int x;
  511. x = nbytes;
  512. if (x > 128) {
  513. x = 128;
  514. }
  515. /* copy it */
  516. memcpy(chunk, ptr, x);
  517. /* terminate it */
  518. chunk[n] = 0;
  519. /* output it */
  520. LOG_USER_N("%s", chunk);
  521. ptr += x;
  522. nbytes -= x;
  523. }
  524. return n;
  525. }
  526. static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
  527. {
  528. /* TCL wants to read... tell him no */
  529. return 0;
  530. }
  531. static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
  532. {
  533. char *cp;
  534. int n;
  535. Jim_Interp *interp;
  536. n = -1;
  537. interp = cookie;
  538. if (interp == NULL)
  539. return n;
  540. cp = alloc_vprintf(fmt, ap);
  541. if (cp)
  542. {
  543. LOG_USER_N("%s", cp);
  544. n = strlen(cp);
  545. free(cp);
  546. }
  547. return n;
  548. }
  549. static int openocd_jim_fflush(void *cookie)
  550. {
  551. /* nothing to flush */
  552. return 0;
  553. }
  554. static char* openocd_jim_fgets(char *s, int size, void *cookie)
  555. {
  556. /* not supported */
  557. errno = ENOTSUP;
  558. return NULL;
  559. }
  560. static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  561. {
  562. if (argc != 2)
  563. return JIM_ERR;
  564. int retcode;
  565. const char *str = Jim_GetString(argv[1], NULL);
  566. /* capture log output and return it */
  567. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  568. /* a garbage collect can happen, so we need a reference count to this object */
  569. Jim_IncrRefCount(tclOutput);
  570. log_add_callback(tcl_output, tclOutput);
  571. retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__ );
  572. log_remove_callback(tcl_output, tclOutput);
  573. /* We dump output into this local variable */
  574. Jim_SetResult(interp, tclOutput);
  575. Jim_DecrRefCount(interp, tclOutput);
  576. return retcode;
  577. }
  578. command_context_t* command_init()
  579. {
  580. command_context_t* context = malloc(sizeof(command_context_t));
  581. extern const char startup_tcl[];
  582. context->mode = COMMAND_EXEC;
  583. context->commands = NULL;
  584. context->current_target = 0;
  585. context->output_handler = NULL;
  586. context->output_handler_priv = NULL;
  587. #ifdef JIM_EMBEDDED
  588. Jim_InitEmbedded();
  589. /* Create an interpreter */
  590. interp = Jim_CreateInterp();
  591. /* Add all the Jim core commands */
  592. Jim_RegisterCoreCommands(interp);
  593. #endif
  594. Jim_CreateCommand(interp, "ocd_find", jim_find, NULL, NULL);
  595. Jim_CreateCommand(interp, "echo", jim_echo, NULL, NULL);
  596. Jim_CreateCommand(interp, "capture", jim_capture, NULL, NULL);
  597. /* Set Jim's STDIO */
  598. interp->cookie_stdin = interp;
  599. interp->cookie_stdout = interp;
  600. interp->cookie_stderr = interp;
  601. interp->cb_fwrite = openocd_jim_fwrite;
  602. interp->cb_fread = openocd_jim_fread ;
  603. interp->cb_vfprintf = openocd_jim_vfprintf;
  604. interp->cb_fflush = openocd_jim_fflush;
  605. interp->cb_fgets = openocd_jim_fgets;
  606. add_default_dirs();
  607. #ifdef JIM_EMBEDDED
  608. Jim_EventLoopOnLoad(interp);
  609. #endif
  610. if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl",1)==JIM_ERR)
  611. {
  612. LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
  613. Jim_PrintErrorMessage(interp);
  614. exit(-1);
  615. }
  616. register_command(context, NULL, "sleep", handle_sleep_command,
  617. COMMAND_ANY, "<n> [busy] - sleep for n milliseconds. \"busy\" means busy wait");
  618. register_command(context, NULL, "fast", handle_fast_command,
  619. COMMAND_ANY, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
  620. return context;
  621. }
  622. int command_context_mode(command_context_t *cmd_ctx, enum command_mode mode)
  623. {
  624. if (!cmd_ctx)
  625. return ERROR_INVALID_ARGUMENTS;
  626. cmd_ctx->mode = mode;
  627. return ERROR_OK;
  628. }
  629. /* sleep command sleeps for <n> miliseconds
  630. * this is useful in target startup scripts
  631. */
  632. int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  633. {
  634. unsigned long duration = 0;
  635. int busy = 0;
  636. if (argc==1)
  637. {
  638. } else if (argc==2)
  639. {
  640. if (strcmp(args[1], "busy")!=0)
  641. return ERROR_COMMAND_SYNTAX_ERROR;
  642. busy = 1;
  643. } else
  644. {
  645. return ERROR_COMMAND_SYNTAX_ERROR;
  646. }
  647. duration = strtoul(args[0], NULL, 0);
  648. if (busy)
  649. {
  650. busy_sleep(duration);
  651. } else
  652. {
  653. long long then=timeval_ms();
  654. while ((timeval_ms()-then)<duration)
  655. {
  656. target_call_timer_callbacks_now();
  657. usleep(1000);
  658. }
  659. }
  660. return ERROR_OK;
  661. }
  662. int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  663. {
  664. if (argc!=1)
  665. return ERROR_COMMAND_SYNTAX_ERROR;
  666. fast_and_dangerous = strcmp("enable", args[0])==0;
  667. return ERROR_OK;
  668. }
  669. void process_jim_events(void)
  670. {
  671. #ifdef JIM_EMBEDDED
  672. static int recursion = 0;
  673. if (!recursion)
  674. {
  675. recursion++;
  676. Jim_ProcessEvents (interp, JIM_ALL_EVENTS|JIM_DONT_WAIT);
  677. recursion--;
  678. }
  679. #endif
  680. }
  681. void register_jim(struct command_context_s *cmd_ctx, const char *name, int (*cmd)(Jim_Interp *interp, int argc, Jim_Obj *const *argv), const char *help)
  682. {
  683. Jim_CreateCommand(interp, name, cmd, NULL, NULL);
  684. /* FIX!!! it would be prettier to invoke add_help_text...
  685. accumulate help text in Tcl helptext list. */
  686. Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
  687. if (Jim_IsShared(helptext))
  688. helptext = Jim_DuplicateObj(interp, helptext);
  689. Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
  690. Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
  691. Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, name, -1));
  692. Jim_ListAppendElement(interp, cmd_entry, cmd_list);
  693. Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
  694. Jim_ListAppendElement(interp, helptext, cmd_entry);
  695. }
  696. /* return global variable long value or 0 upon failure */
  697. long jim_global_long(const char *variable)
  698. {
  699. Jim_Obj *objPtr=Jim_GetGlobalVariableStr(interp, variable, JIM_ERRMSG);
  700. long t;
  701. if (Jim_GetLong(interp, objPtr, &t)==JIM_OK)
  702. {
  703. return t;
  704. }
  705. return 0;
  706. }