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.
 
 
 
 
 
 

955 lines
24 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. #if !BUILD_ECOSBOARD
  33. /* see Embedder-HOWTO.txt in Jim Tcl project hosted on BerliOS*/
  34. #define JIM_EMBEDDED
  35. #endif
  36. // @todo the inclusion of target.h here is a layering violation
  37. #include "target.h"
  38. #include "command.h"
  39. #include "configuration.h"
  40. #include "log.h"
  41. #include "time_support.h"
  42. #include "jim-eventloop.h"
  43. int fast_and_dangerous = 0;
  44. Jim_Interp *interp = NULL;
  45. static int run_command(struct command_context *context,
  46. command_t *c, const char *words[], unsigned num_words);
  47. static void tcl_output(void *privData, const char *file, unsigned line,
  48. const char *function, const char *string)
  49. {
  50. Jim_Obj *tclOutput = (Jim_Obj *)privData;
  51. Jim_AppendString(interp, tclOutput, string, strlen(string));
  52. }
  53. extern struct command_context *global_cmd_ctx;
  54. void script_debug(Jim_Interp *interp, const char *name,
  55. unsigned argc, Jim_Obj *const *argv)
  56. {
  57. LOG_DEBUG("command - %s", name);
  58. for (unsigned i = 0; i < argc; i++)
  59. {
  60. int len;
  61. const char *w = Jim_GetString(argv[i], &len);
  62. /* end of line comment? */
  63. if (*w == '#')
  64. break;
  65. LOG_DEBUG("%s - argv[%d]=%s", name, i, w);
  66. }
  67. }
  68. static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  69. {
  70. /* the private data is stashed in the interp structure */
  71. command_t *c;
  72. struct command_context *context;
  73. int retval;
  74. int i;
  75. int nwords;
  76. char **words;
  77. /* DANGER!!!! be careful what we invoke here, since interp->cmdPrivData might
  78. * get overwritten by running other Jim commands! Treat it as an
  79. * emphemeral global variable that is used in lieu of an argument
  80. * to the fn and fish it out manually.
  81. */
  82. c = interp->cmdPrivData;
  83. if (c == NULL)
  84. {
  85. LOG_ERROR("BUG: interp->cmdPrivData == NULL");
  86. return JIM_ERR;
  87. }
  88. target_call_timer_callbacks_now();
  89. LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
  90. script_debug(interp, c->name, argc, argv);
  91. words = malloc(sizeof(char *) * (argc + 1));
  92. words[0] = c->name;
  93. for (i = 0; i < argc; i++)
  94. {
  95. int len;
  96. const char *w = Jim_GetString(argv[i], &len);
  97. if (*w=='#')
  98. {
  99. /* hit an end of line comment */
  100. break;
  101. }
  102. words[i + 1] = strdup(w);
  103. if (words[i + 1] == NULL)
  104. {
  105. int j;
  106. for (j = 0; j < i; j++)
  107. free(words[j + 1]);
  108. free(words);
  109. return JIM_ERR;
  110. }
  111. }
  112. nwords = i;
  113. /* grab the command context from the associated data */
  114. context = Jim_GetAssocData(interp, "context");
  115. if (context == NULL)
  116. {
  117. /* Tcl can invoke commands directly instead of via command_run_line(). This would
  118. * happen when the Jim Tcl interpreter is provided by eCos.
  119. */
  120. context = global_cmd_ctx;
  121. }
  122. /* capture log output and return it */
  123. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  124. /* a garbage collect can happen, so we need a reference count to this object */
  125. Jim_IncrRefCount(tclOutput);
  126. log_add_callback(tcl_output, tclOutput);
  127. // turn words[0] into args[-1] with this cast
  128. retval = run_command(context, c, (const char **)words + 1, nwords);
  129. log_remove_callback(tcl_output, tclOutput);
  130. /* We dump output into this local variable */
  131. Jim_SetResult(interp, tclOutput);
  132. Jim_DecrRefCount(interp, tclOutput);
  133. for (i = 0; i < nwords; i++)
  134. free(words[i + 1]);
  135. free(words);
  136. int *return_retval = Jim_GetAssocData(interp, "retval");
  137. if (return_retval != NULL)
  138. {
  139. *return_retval = retval;
  140. }
  141. return (retval == ERROR_OK)?JIM_OK:JIM_ERR;
  142. }
  143. static Jim_Obj *command_name_list(struct command_s *c)
  144. {
  145. Jim_Obj *cmd_list = c->parent ?
  146. command_name_list(c->parent) :
  147. Jim_NewListObj(interp, NULL, 0);
  148. Jim_ListAppendElement(interp, cmd_list,
  149. Jim_NewStringObj(interp, c->name, -1));
  150. return cmd_list;
  151. }
  152. static void command_helptext_add(Jim_Obj *cmd_list, const char *help)
  153. {
  154. Jim_Obj *cmd_entry = Jim_NewListObj(interp, NULL, 0);
  155. Jim_ListAppendElement(interp, cmd_entry, cmd_list);
  156. Jim_ListAppendElement(interp, cmd_entry,
  157. Jim_NewStringObj(interp, help ? : "", -1));
  158. /* accumulate help text in Tcl helptext list. */
  159. Jim_Obj *helptext = Jim_GetGlobalVariableStr(interp,
  160. "ocd_helptext", JIM_ERRMSG);
  161. if (Jim_IsShared(helptext))
  162. helptext = Jim_DuplicateObj(interp, helptext);
  163. Jim_ListAppendElement(interp, helptext, cmd_entry);
  164. }
  165. /* nice short description of source file */
  166. #define __THIS__FILE__ "command.c"
  167. /**
  168. * Find a command by name from a list of commands.
  169. * @returns The named command if found, or NULL.
  170. */
  171. static struct command_s *command_find(struct command_s **head, const char *name)
  172. {
  173. assert(head);
  174. for (struct command_s *cc = *head; cc; cc = cc->next)
  175. {
  176. if (strcmp(cc->name, name) == 0)
  177. return cc;
  178. }
  179. return NULL;
  180. }
  181. /**
  182. * Add the command to the end of linked list.
  183. * @returns Returns false if the named command already exists in the list.
  184. * Returns true otherwise.
  185. */
  186. static void command_add_child(struct command_s **head, struct command_s *c)
  187. {
  188. assert(head);
  189. if (NULL == *head)
  190. {
  191. *head = c;
  192. return;
  193. }
  194. struct command_s *cc = *head;
  195. while (cc->next) cc = cc->next;
  196. cc->next = c;
  197. }
  198. command_t* register_command(struct command_context *context,
  199. command_t *parent, char *name, command_handler_t handler,
  200. enum command_mode mode, char *help)
  201. {
  202. if (!context || !name)
  203. return NULL;
  204. struct command_s **head = parent ? &parent->children : &context->commands;
  205. struct command_s *c = command_find(head, name);
  206. if (NULL != c)
  207. return c;
  208. c = malloc(sizeof(command_t));
  209. c->name = strdup(name);
  210. c->parent = parent;
  211. c->children = NULL;
  212. c->handler = handler;
  213. c->mode = mode;
  214. c->next = NULL;
  215. command_add_child(head, c);
  216. command_helptext_add(command_name_list(c), help);
  217. /* just a placeholder, no handler */
  218. if (c->handler == NULL)
  219. return c;
  220. const char *full_name = command_name(c, '_');
  221. const char *ocd_name = alloc_printf("ocd_%s", full_name);
  222. Jim_CreateCommand(interp, ocd_name, script_command, c, NULL);
  223. free((void *)ocd_name);
  224. /* we now need to add an overrideable proc */
  225. const char *override_name = alloc_printf("proc %s {args} {"
  226. "if {[catch {eval ocd_%s $args}] == 0} "
  227. "{return \"\"} else {return -code error}}",
  228. full_name, full_name);
  229. Jim_Eval_Named(interp, override_name, __THIS__FILE__, __LINE__);
  230. free((void *)override_name);
  231. free((void *)full_name);
  232. return c;
  233. }
  234. int unregister_all_commands(struct command_context *context)
  235. {
  236. command_t *c, *c2;
  237. if (context == NULL)
  238. return ERROR_OK;
  239. while (NULL != context->commands)
  240. {
  241. c = context->commands;
  242. while (NULL != c->children)
  243. {
  244. c2 = c->children;
  245. c->children = c->children->next;
  246. free(c2->name);
  247. c2->name = NULL;
  248. free(c2);
  249. c2 = NULL;
  250. }
  251. context->commands = context->commands->next;
  252. free(c->name);
  253. c->name = NULL;
  254. free(c);
  255. c = NULL;
  256. }
  257. return ERROR_OK;
  258. }
  259. int unregister_command(struct command_context *context, char *name)
  260. {
  261. command_t *c, *p = NULL, *c2;
  262. if ((!context) || (!name))
  263. return ERROR_INVALID_ARGUMENTS;
  264. /* find command */
  265. c = context->commands;
  266. while (NULL != c)
  267. {
  268. if (strcmp(name, c->name) == 0)
  269. {
  270. /* unlink command */
  271. if (p)
  272. {
  273. p->next = c->next;
  274. }
  275. else
  276. {
  277. /* first element in command list */
  278. context->commands = c->next;
  279. }
  280. /* unregister children */
  281. while (NULL != c->children)
  282. {
  283. c2 = c->children;
  284. c->children = c->children->next;
  285. free(c2->name);
  286. c2->name = NULL;
  287. free(c2);
  288. c2 = NULL;
  289. }
  290. /* delete command */
  291. free(c->name);
  292. c->name = NULL;
  293. free(c);
  294. c = NULL;
  295. return ERROR_OK;
  296. }
  297. /* remember the last command for unlinking */
  298. p = c;
  299. c = c->next;
  300. }
  301. return ERROR_OK;
  302. }
  303. void command_output_text(struct command_context *context, const char *data)
  304. {
  305. if (context && context->output_handler && data) {
  306. context->output_handler(context, data);
  307. }
  308. }
  309. void command_print_sameline(struct command_context *context, const char *format, ...)
  310. {
  311. char *string;
  312. va_list ap;
  313. va_start(ap, format);
  314. string = alloc_vprintf(format, ap);
  315. if (string != NULL)
  316. {
  317. /* we want this collected in the log + we also want to pick it up as a tcl return
  318. * value.
  319. *
  320. * The latter bit isn't precisely neat, but will do for now.
  321. */
  322. LOG_USER_N("%s", string);
  323. /* We already printed it above */
  324. /* command_output_text(context, string); */
  325. free(string);
  326. }
  327. va_end(ap);
  328. }
  329. void command_print(struct command_context *context, const char *format, ...)
  330. {
  331. char *string;
  332. va_list ap;
  333. va_start(ap, format);
  334. string = alloc_vprintf(format, ap);
  335. if (string != NULL)
  336. {
  337. strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
  338. /* we want this collected in the log + we also want to pick it up as a tcl return
  339. * value.
  340. *
  341. * The latter bit isn't precisely neat, but will do for now.
  342. */
  343. LOG_USER_N("%s", string);
  344. /* We already printed it above */
  345. /* command_output_text(context, string); */
  346. free(string);
  347. }
  348. va_end(ap);
  349. }
  350. static char *__command_name(struct command_s *c, char delim, unsigned extra)
  351. {
  352. char *name;
  353. unsigned len = strlen(c->name);
  354. if (NULL == c->parent) {
  355. // allocate enough for the name, child names, and '\0'
  356. name = malloc(len + extra + 1);
  357. strcpy(name, c->name);
  358. } else {
  359. // parent's extra must include both the space and name
  360. name = __command_name(c->parent, delim, 1 + len + extra);
  361. char dstr[2] = { delim, 0 };
  362. strcat(name, dstr);
  363. strcat(name, c->name);
  364. }
  365. return name;
  366. }
  367. char *command_name(struct command_s *c, char delim)
  368. {
  369. return __command_name(c, delim, 0);
  370. }
  371. static int run_command(struct command_context *context,
  372. command_t *c, const char *words[], unsigned num_words)
  373. {
  374. int start_word = 0;
  375. if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode)))
  376. {
  377. /* Config commands can not run after the config stage */
  378. LOG_ERROR("Command '%s' only runs during configuration stage", c->name);
  379. return ERROR_FAIL;
  380. }
  381. unsigned argc = num_words - start_word - 1;
  382. const char **args = words + start_word + 1;
  383. int retval = c->handler(context, args, argc);
  384. if (retval == ERROR_COMMAND_SYNTAX_ERROR)
  385. {
  386. /* Print help for command */
  387. char *full_name = command_name(c, ' ');
  388. if (NULL != full_name) {
  389. command_run_linef(context, "help %s", full_name);
  390. free(full_name);
  391. } else
  392. retval = -ENOMEM;
  393. }
  394. else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
  395. {
  396. /* just fall through for a shutdown request */
  397. }
  398. else if (retval != ERROR_OK)
  399. {
  400. /* we do not print out an error message because the command *should*
  401. * have printed out an error
  402. */
  403. LOG_DEBUG("Command failed with error code %d", retval);
  404. }
  405. return retval;
  406. }
  407. int command_run_line(struct command_context *context, char *line)
  408. {
  409. /* all the parent commands have been registered with the interpreter
  410. * so, can just evaluate the line as a script and check for
  411. * results
  412. */
  413. /* run the line thru a script engine */
  414. int retval = ERROR_FAIL;
  415. int retcode;
  416. /* Beware! This code needs to be reentrant. It is also possible
  417. * for OpenOCD commands to be invoked directly from Tcl. This would
  418. * happen when the Jim Tcl interpreter is provided by eCos for
  419. * instance.
  420. */
  421. Jim_DeleteAssocData(interp, "context");
  422. retcode = Jim_SetAssocData(interp, "context", NULL, context);
  423. if (retcode == JIM_OK)
  424. {
  425. /* associated the return value */
  426. Jim_DeleteAssocData(interp, "retval");
  427. retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
  428. if (retcode == JIM_OK)
  429. {
  430. retcode = Jim_Eval_Named(interp, line, __THIS__FILE__, __LINE__);
  431. Jim_DeleteAssocData(interp, "retval");
  432. }
  433. Jim_DeleteAssocData(interp, "context");
  434. }
  435. if (retcode == JIM_ERR) {
  436. if (retval != ERROR_COMMAND_CLOSE_CONNECTION)
  437. {
  438. /* We do not print the connection closed error message */
  439. Jim_PrintErrorMessage(interp);
  440. }
  441. if (retval == ERROR_OK)
  442. {
  443. /* It wasn't a low level OpenOCD command that failed */
  444. return ERROR_FAIL;
  445. }
  446. return retval;
  447. } else if (retcode == JIM_EXIT) {
  448. /* ignore. */
  449. /* exit(Jim_GetExitCode(interp)); */
  450. } else {
  451. const char *result;
  452. int reslen;
  453. result = Jim_GetString(Jim_GetResult(interp), &reslen);
  454. if (reslen > 0)
  455. {
  456. int i;
  457. char buff[256 + 1];
  458. for (i = 0; i < reslen; i += 256)
  459. {
  460. int chunk;
  461. chunk = reslen - i;
  462. if (chunk > 256)
  463. chunk = 256;
  464. strncpy(buff, result + i, chunk);
  465. buff[chunk] = 0;
  466. LOG_USER_N("%s", buff);
  467. }
  468. LOG_USER_N("%s", "\n");
  469. }
  470. retval = ERROR_OK;
  471. }
  472. return retval;
  473. }
  474. int command_run_linef(struct command_context *context, const char *format, ...)
  475. {
  476. int retval = ERROR_FAIL;
  477. char *string;
  478. va_list ap;
  479. va_start(ap, format);
  480. string = alloc_vprintf(format, ap);
  481. if (string != NULL)
  482. {
  483. retval = command_run_line(context, string);
  484. }
  485. va_end(ap);
  486. return retval;
  487. }
  488. void command_set_output_handler(struct command_context* context,
  489. command_output_handler_t output_handler, void *priv)
  490. {
  491. context->output_handler = output_handler;
  492. context->output_handler_priv = priv;
  493. }
  494. struct command_context* copy_command_context(struct command_context* context)
  495. {
  496. struct command_context* copy_context = malloc(sizeof(struct command_context));
  497. *copy_context = *context;
  498. return copy_context;
  499. }
  500. int command_done(struct command_context *context)
  501. {
  502. free(context);
  503. context = NULL;
  504. return ERROR_OK;
  505. }
  506. /* find full path to file */
  507. static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  508. {
  509. if (argc != 2)
  510. return JIM_ERR;
  511. const char *file = Jim_GetString(argv[1], NULL);
  512. char *full_path = find_file(file);
  513. if (full_path == NULL)
  514. return JIM_ERR;
  515. Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
  516. free(full_path);
  517. Jim_SetResult(interp, result);
  518. return JIM_OK;
  519. }
  520. static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  521. {
  522. if (argc != 2)
  523. return JIM_ERR;
  524. const char *str = Jim_GetString(argv[1], NULL);
  525. LOG_USER("%s", str);
  526. return JIM_OK;
  527. }
  528. static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
  529. {
  530. size_t nbytes;
  531. const char *ptr;
  532. Jim_Interp *interp;
  533. /* make it a char easier to read code */
  534. ptr = _ptr;
  535. interp = cookie;
  536. nbytes = size * n;
  537. if (ptr == NULL || interp == NULL || nbytes == 0) {
  538. return 0;
  539. }
  540. /* do we have to chunk it? */
  541. if (ptr[nbytes] == 0)
  542. {
  543. /* no it is a C style string */
  544. LOG_USER_N("%s", ptr);
  545. return strlen(ptr);
  546. }
  547. /* GRR we must chunk - not null terminated */
  548. while (nbytes) {
  549. char chunk[128 + 1];
  550. int x;
  551. x = nbytes;
  552. if (x > 128) {
  553. x = 128;
  554. }
  555. /* copy it */
  556. memcpy(chunk, ptr, x);
  557. /* terminate it */
  558. chunk[n] = 0;
  559. /* output it */
  560. LOG_USER_N("%s", chunk);
  561. ptr += x;
  562. nbytes -= x;
  563. }
  564. return n;
  565. }
  566. static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
  567. {
  568. /* TCL wants to read... tell him no */
  569. return 0;
  570. }
  571. static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
  572. {
  573. char *cp;
  574. int n;
  575. Jim_Interp *interp;
  576. n = -1;
  577. interp = cookie;
  578. if (interp == NULL)
  579. return n;
  580. cp = alloc_vprintf(fmt, ap);
  581. if (cp)
  582. {
  583. LOG_USER_N("%s", cp);
  584. n = strlen(cp);
  585. free(cp);
  586. }
  587. return n;
  588. }
  589. static int openocd_jim_fflush(void *cookie)
  590. {
  591. /* nothing to flush */
  592. return 0;
  593. }
  594. static char* openocd_jim_fgets(char *s, int size, void *cookie)
  595. {
  596. /* not supported */
  597. errno = ENOTSUP;
  598. return NULL;
  599. }
  600. static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  601. {
  602. if (argc != 2)
  603. return JIM_ERR;
  604. int retcode;
  605. const char *str = Jim_GetString(argv[1], NULL);
  606. /* capture log output and return it */
  607. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  608. /* a garbage collect can happen, so we need a reference count to this object */
  609. Jim_IncrRefCount(tclOutput);
  610. log_add_callback(tcl_output, tclOutput);
  611. retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__);
  612. log_remove_callback(tcl_output, tclOutput);
  613. /* We dump output into this local variable */
  614. Jim_SetResult(interp, tclOutput);
  615. Jim_DecrRefCount(interp, tclOutput);
  616. return retcode;
  617. }
  618. /* sleep command sleeps for <n> miliseconds
  619. * this is useful in target startup scripts
  620. */
  621. COMMAND_HANDLER(handle_sleep_command)
  622. {
  623. bool busy = false;
  624. if (argc == 2)
  625. {
  626. if (strcmp(args[1], "busy") == 0)
  627. busy = true;
  628. else
  629. return ERROR_COMMAND_SYNTAX_ERROR;
  630. }
  631. else if (argc < 1 || argc > 2)
  632. return ERROR_COMMAND_SYNTAX_ERROR;
  633. unsigned long duration = 0;
  634. int retval = parse_ulong(args[0], &duration);
  635. if (ERROR_OK != retval)
  636. return retval;
  637. if (!busy)
  638. {
  639. long long then = timeval_ms();
  640. while (timeval_ms() - then < (long long)duration)
  641. {
  642. target_call_timer_callbacks_now();
  643. usleep(1000);
  644. }
  645. }
  646. else
  647. busy_sleep(duration);
  648. return ERROR_OK;
  649. }
  650. COMMAND_HANDLER(handle_fast_command)
  651. {
  652. if (argc != 1)
  653. return ERROR_COMMAND_SYNTAX_ERROR;
  654. fast_and_dangerous = strcmp("enable", args[0]) == 0;
  655. return ERROR_OK;
  656. }
  657. struct command_context* command_init()
  658. {
  659. struct command_context* context = malloc(sizeof(struct command_context));
  660. extern const char startup_tcl[];
  661. const char *HostOs;
  662. context->mode = COMMAND_EXEC;
  663. context->commands = NULL;
  664. context->current_target = 0;
  665. context->output_handler = NULL;
  666. context->output_handler_priv = NULL;
  667. #if !BUILD_ECOSBOARD
  668. Jim_InitEmbedded();
  669. /* Create an interpreter */
  670. interp = Jim_CreateInterp();
  671. /* Add all the Jim core commands */
  672. Jim_RegisterCoreCommands(interp);
  673. #endif
  674. #if defined(_MSC_VER)
  675. /* WinXX - is generic, the forward
  676. * looking problem is this:
  677. *
  678. * "win32" or "win64"
  679. *
  680. * "winxx" is generic.
  681. */
  682. HostOs = "winxx";
  683. #elif defined(__linux__)
  684. HostOs = "linux";
  685. #elif defined(__DARWIN__)
  686. HostOs = "darwin";
  687. #elif defined(__CYGWIN__)
  688. HostOs = "cygwin";
  689. #elif defined(__MINGW32__)
  690. HostOs = "mingw32";
  691. #elif defined(__ECOS)
  692. HostOs = "ecos";
  693. #else
  694. #warn unrecognized host OS...
  695. HostOs = "other";
  696. #endif
  697. Jim_SetGlobalVariableStr(interp, "ocd_HOSTOS",
  698. Jim_NewStringObj(interp, HostOs , strlen(HostOs)));
  699. Jim_CreateCommand(interp, "ocd_find", jim_find, NULL, NULL);
  700. Jim_CreateCommand(interp, "echo", jim_echo, NULL, NULL);
  701. Jim_CreateCommand(interp, "capture", jim_capture, NULL, NULL);
  702. /* Set Jim's STDIO */
  703. interp->cookie_stdin = interp;
  704. interp->cookie_stdout = interp;
  705. interp->cookie_stderr = interp;
  706. interp->cb_fwrite = openocd_jim_fwrite;
  707. interp->cb_fread = openocd_jim_fread ;
  708. interp->cb_vfprintf = openocd_jim_vfprintf;
  709. interp->cb_fflush = openocd_jim_fflush;
  710. interp->cb_fgets = openocd_jim_fgets;
  711. #if !BUILD_ECOSBOARD
  712. Jim_EventLoopOnLoad(interp);
  713. #endif
  714. if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl",1) == JIM_ERR)
  715. {
  716. LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
  717. Jim_PrintErrorMessage(interp);
  718. exit(-1);
  719. }
  720. register_command(context, NULL, "sleep",
  721. handle_sleep_command, COMMAND_ANY,
  722. "<n> [busy] - sleep for n milliseconds. "
  723. "\"busy\" means busy wait");
  724. register_command(context, NULL, "fast",
  725. handle_fast_command, COMMAND_ANY,
  726. "fast <enable/disable> - place at beginning of "
  727. "config files. Sets defaults to fast and dangerous.");
  728. return context;
  729. }
  730. int command_context_mode(struct command_context *cmd_ctx, enum command_mode mode)
  731. {
  732. if (!cmd_ctx)
  733. return ERROR_INVALID_ARGUMENTS;
  734. cmd_ctx->mode = mode;
  735. return ERROR_OK;
  736. }
  737. void process_jim_events(void)
  738. {
  739. #if !BUILD_ECOSBOARD
  740. static int recursion = 0;
  741. if (!recursion)
  742. {
  743. recursion++;
  744. Jim_ProcessEvents (interp, JIM_ALL_EVENTS | JIM_DONT_WAIT);
  745. recursion--;
  746. }
  747. #endif
  748. }
  749. void register_jim(struct command_context *cmd_ctx, const char *name, int (*cmd)(Jim_Interp *interp, int argc, Jim_Obj *const *argv), const char *help)
  750. {
  751. Jim_CreateCommand(interp, name, cmd, NULL, NULL);
  752. Jim_Obj *cmd_list = Jim_NewListObj(interp, NULL, 0);
  753. Jim_ListAppendElement(interp, cmd_list,
  754. Jim_NewStringObj(interp, name, -1));
  755. command_helptext_add(cmd_list, help);
  756. }
  757. /* return global variable long value or 0 upon failure */
  758. long jim_global_long(const char *variable)
  759. {
  760. Jim_Obj *objPtr = Jim_GetGlobalVariableStr(interp, variable, JIM_ERRMSG);
  761. long t;
  762. if (Jim_GetLong(interp, objPtr, &t) == JIM_OK)
  763. {
  764. return t;
  765. }
  766. return 0;
  767. }
  768. #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
  769. int parse##name(const char *str, type *ul) \
  770. { \
  771. if (!*str) \
  772. { \
  773. LOG_ERROR("Invalid command argument"); \
  774. return ERROR_COMMAND_ARGUMENT_INVALID; \
  775. } \
  776. char *end; \
  777. *ul = func(str, &end, 0); \
  778. if (*end) \
  779. { \
  780. LOG_ERROR("Invalid command argument"); \
  781. return ERROR_COMMAND_ARGUMENT_INVALID; \
  782. } \
  783. if ((max == *ul) && (ERANGE == errno)) \
  784. { \
  785. LOG_ERROR("Argument overflow"); \
  786. return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
  787. } \
  788. if (min && (min == *ul) && (ERANGE == errno)) \
  789. { \
  790. LOG_ERROR("Argument underflow"); \
  791. return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
  792. } \
  793. return ERROR_OK; \
  794. }
  795. DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long , strtoul, 0, ULONG_MAX)
  796. DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, 0, ULLONG_MAX)
  797. DEFINE_PARSE_NUM_TYPE(_long, long , strtol, LONG_MIN, LONG_MAX)
  798. DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MIN, LLONG_MAX)
  799. #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
  800. int parse##name(const char *str, type *ul) \
  801. { \
  802. functype n; \
  803. int retval = parse##funcname(str, &n); \
  804. if (ERROR_OK != retval) \
  805. return retval; \
  806. if (n > max) \
  807. return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
  808. if (min) \
  809. return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
  810. *ul = n; \
  811. return ERROR_OK; \
  812. }
  813. #define DEFINE_PARSE_ULONG(name, type, min, max) \
  814. DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long, _ulong)
  815. DEFINE_PARSE_ULONG(_uint, unsigned, 0, UINT_MAX)
  816. DEFINE_PARSE_ULONG(_u32, uint32_t, 0, UINT32_MAX)
  817. DEFINE_PARSE_ULONG(_u16, uint16_t, 0, UINT16_MAX)
  818. DEFINE_PARSE_ULONG(_u8, uint8_t, 0, UINT8_MAX)
  819. #define DEFINE_PARSE_LONG(name, type, min, max) \
  820. DEFINE_PARSE_WRAPPER(name, type, min, max, long, _long)
  821. DEFINE_PARSE_LONG(_int, int, n < INT_MIN, INT_MAX)
  822. DEFINE_PARSE_LONG(_s32, int32_t, n < INT32_MIN, INT32_MAX)
  823. DEFINE_PARSE_LONG(_s16, int16_t, n < INT16_MIN, INT16_MAX)
  824. DEFINE_PARSE_LONG(_s8, int8_t, n < INT8_MIN, INT8_MAX)