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.
 
 
 
 
 
 

1502 lines
39 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, see <http://www.gnu.org/licenses/>. *
  26. ***************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. /* see Embedded-HOWTO.txt in Jim Tcl project hosted on BerliOS*/
  31. #define JIM_EMBEDDED
  32. /* @todo the inclusion of target.h here is a layering violation */
  33. #include <jtag/jtag.h>
  34. #include <target/target.h>
  35. #include "command.h"
  36. #include "configuration.h"
  37. #include "log.h"
  38. #include "time_support.h"
  39. #include "jim-eventloop.h"
  40. /* nice short description of source file */
  41. #define __THIS__FILE__ "command.c"
  42. static int run_command(struct command_context *context,
  43. struct command *c, const char *words[], unsigned num_words);
  44. struct log_capture_state {
  45. Jim_Interp *interp;
  46. Jim_Obj *output;
  47. };
  48. static void tcl_output(void *privData, const char *file, unsigned line,
  49. const char *function, const char *string)
  50. {
  51. struct log_capture_state *state = privData;
  52. Jim_AppendString(state->interp, state->output, string, strlen(string));
  53. }
  54. static struct log_capture_state *command_log_capture_start(Jim_Interp *interp)
  55. {
  56. /* capture log output and return it. A garbage collect can
  57. * happen, so we need a reference count to this object */
  58. Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
  59. if (NULL == tclOutput)
  60. return NULL;
  61. struct log_capture_state *state = malloc(sizeof(*state));
  62. if (NULL == state)
  63. return NULL;
  64. state->interp = interp;
  65. Jim_IncrRefCount(tclOutput);
  66. state->output = tclOutput;
  67. log_add_callback(tcl_output, state);
  68. return state;
  69. }
  70. /* Classic openocd commands provide progress output which we
  71. * will capture and return as a Tcl return value.
  72. *
  73. * However, if a non-openocd command has been invoked, then it
  74. * makes sense to return the tcl return value from that command.
  75. *
  76. * The tcl return value is empty for openocd commands that provide
  77. * progress output.
  78. *
  79. * Therefore we set the tcl return value only if we actually
  80. * captured output.
  81. */
  82. static void command_log_capture_finish(struct log_capture_state *state)
  83. {
  84. if (NULL == state)
  85. return;
  86. log_remove_callback(tcl_output, state);
  87. int length;
  88. Jim_GetString(state->output, &length);
  89. if (length > 0)
  90. Jim_SetResult(state->interp, state->output);
  91. else {
  92. /* No output captured, use tcl return value (which could
  93. * be empty too). */
  94. }
  95. Jim_DecrRefCount(state->interp, state->output);
  96. free(state);
  97. }
  98. static int command_retval_set(Jim_Interp *interp, int retval)
  99. {
  100. int *return_retval = Jim_GetAssocData(interp, "retval");
  101. if (return_retval != NULL)
  102. *return_retval = retval;
  103. return (retval == ERROR_OK) ? JIM_OK : retval;
  104. }
  105. extern struct command_context *global_cmd_ctx;
  106. /* dump a single line to the log for the command.
  107. * Do nothing in case we are not at debug level 3 */
  108. void script_debug(Jim_Interp *interp, const char *name,
  109. unsigned argc, Jim_Obj * const *argv)
  110. {
  111. if (debug_level < LOG_LVL_DEBUG)
  112. return;
  113. char *dbg = alloc_printf("command - %s", name);
  114. for (unsigned i = 0; i < argc; i++) {
  115. int len;
  116. const char *w = Jim_GetString(argv[i], &len);
  117. char *t = alloc_printf("%s %s", dbg, w);
  118. free(dbg);
  119. dbg = t;
  120. }
  121. LOG_DEBUG("%s", dbg);
  122. free(dbg);
  123. }
  124. static void script_command_args_free(char **words, unsigned nwords)
  125. {
  126. for (unsigned i = 0; i < nwords; i++)
  127. free(words[i]);
  128. free(words);
  129. }
  130. static char **script_command_args_alloc(
  131. unsigned argc, Jim_Obj * const *argv, unsigned *nwords)
  132. {
  133. char **words = malloc(argc * sizeof(char *));
  134. if (NULL == words)
  135. return NULL;
  136. unsigned i;
  137. for (i = 0; i < argc; i++) {
  138. int len;
  139. const char *w = Jim_GetString(argv[i], &len);
  140. words[i] = strdup(w);
  141. if (words[i] == NULL) {
  142. script_command_args_free(words, i);
  143. return NULL;
  144. }
  145. }
  146. *nwords = i;
  147. return words;
  148. }
  149. struct command_context *current_command_context(Jim_Interp *interp)
  150. {
  151. /* grab the command context from the associated data */
  152. struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
  153. if (NULL == cmd_ctx) {
  154. /* Tcl can invoke commands directly instead of via command_run_line(). This would
  155. * happen when the Jim Tcl interpreter is provided by eCos or if we are running
  156. * commands in a startup script.
  157. *
  158. * A telnet or gdb server would provide a non-default command context to
  159. * handle piping of error output, have a separate current target, etc.
  160. */
  161. cmd_ctx = global_cmd_ctx;
  162. }
  163. return cmd_ctx;
  164. }
  165. static int script_command_run(Jim_Interp *interp,
  166. int argc, Jim_Obj * const *argv, struct command *c, bool capture)
  167. {
  168. target_call_timer_callbacks_now();
  169. LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
  170. unsigned nwords;
  171. char **words = script_command_args_alloc(argc, argv, &nwords);
  172. if (NULL == words)
  173. return JIM_ERR;
  174. struct log_capture_state *state = NULL;
  175. if (capture)
  176. state = command_log_capture_start(interp);
  177. struct command_context *cmd_ctx = current_command_context(interp);
  178. int retval = run_command(cmd_ctx, c, (const char **)words, nwords);
  179. command_log_capture_finish(state);
  180. script_command_args_free(words, nwords);
  181. return command_retval_set(interp, retval);
  182. }
  183. static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  184. {
  185. /* the private data is stashed in the interp structure */
  186. struct command *c = interp->cmdPrivData;
  187. assert(c);
  188. script_debug(interp, c->name, argc, argv);
  189. return script_command_run(interp, argc, argv, c, true);
  190. }
  191. static struct command *command_root(struct command *c)
  192. {
  193. while (NULL != c->parent)
  194. c = c->parent;
  195. return c;
  196. }
  197. /**
  198. * Find a command by name from a list of commands.
  199. * @returns Returns the named command if it exists in the list.
  200. * Returns NULL otherwise.
  201. */
  202. static struct command *command_find(struct command *head, const char *name)
  203. {
  204. for (struct command *cc = head; cc; cc = cc->next) {
  205. if (strcmp(cc->name, name) == 0)
  206. return cc;
  207. }
  208. return NULL;
  209. }
  210. struct command *command_find_in_context(struct command_context *cmd_ctx,
  211. const char *name)
  212. {
  213. return command_find(cmd_ctx->commands, name);
  214. }
  215. struct command *command_find_in_parent(struct command *parent,
  216. const char *name)
  217. {
  218. return command_find(parent->children, name);
  219. }
  220. /**
  221. * Add the command into the linked list, sorted by name.
  222. * @param head Address to head of command list pointer, which may be
  223. * updated if @c c gets inserted at the beginning of the list.
  224. * @param c The command to add to the list pointed to by @c head.
  225. */
  226. static void command_add_child(struct command **head, struct command *c)
  227. {
  228. assert(head);
  229. if (NULL == *head) {
  230. *head = c;
  231. return;
  232. }
  233. while ((*head)->next && (strcmp(c->name, (*head)->name) > 0))
  234. head = &(*head)->next;
  235. if (strcmp(c->name, (*head)->name) > 0) {
  236. c->next = (*head)->next;
  237. (*head)->next = c;
  238. } else {
  239. c->next = *head;
  240. *head = c;
  241. }
  242. }
  243. static struct command **command_list_for_parent(
  244. struct command_context *cmd_ctx, struct command *parent)
  245. {
  246. return parent ? &parent->children : &cmd_ctx->commands;
  247. }
  248. static void command_free(struct command *c)
  249. {
  250. /** @todo if command has a handler, unregister its jim command! */
  251. while (NULL != c->children) {
  252. struct command *tmp = c->children;
  253. c->children = tmp->next;
  254. command_free(tmp);
  255. }
  256. free(c->name);
  257. free(c->help);
  258. free(c->usage);
  259. free(c);
  260. }
  261. static struct command *command_new(struct command_context *cmd_ctx,
  262. struct command *parent, const struct command_registration *cr)
  263. {
  264. assert(cr->name);
  265. /*
  266. * If it is a non-jim command with no .usage specified,
  267. * log an error.
  268. *
  269. * strlen(.usage) == 0 means that the command takes no
  270. * arguments.
  271. */
  272. if ((cr->jim_handler == NULL) && (cr->usage == NULL)) {
  273. LOG_DEBUG("BUG: command '%s%s%s' does not have the "
  274. "'.usage' field filled out",
  275. parent && parent->name ? parent->name : "",
  276. parent && parent->name ? " " : "",
  277. cr->name);
  278. }
  279. struct command *c = calloc(1, sizeof(struct command));
  280. if (NULL == c)
  281. return NULL;
  282. c->name = strdup(cr->name);
  283. if (cr->help)
  284. c->help = strdup(cr->help);
  285. if (cr->usage)
  286. c->usage = strdup(cr->usage);
  287. if (!c->name || (cr->help && !c->help) || (cr->usage && !c->usage))
  288. goto command_new_error;
  289. c->parent = parent;
  290. c->handler = cr->handler;
  291. c->jim_handler = cr->jim_handler;
  292. c->jim_handler_data = cr->jim_handler_data;
  293. c->mode = cr->mode;
  294. command_add_child(command_list_for_parent(cmd_ctx, parent), c);
  295. return c;
  296. command_new_error:
  297. command_free(c);
  298. return NULL;
  299. }
  300. static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  301. static int register_command_handler(struct command_context *cmd_ctx,
  302. struct command *c)
  303. {
  304. Jim_Interp *interp = cmd_ctx->interp->interp;
  305. char *ocd_name = alloc_printf("ocd_%s", c->name);
  306. if (NULL == ocd_name)
  307. return JIM_ERR;
  308. LOG_DEBUG("registering '%s'...", ocd_name);
  309. Jim_CmdProc *func = c->handler ? &script_command : &command_unknown;
  310. int retval = Jim_CreateCommand(interp, ocd_name, func, c, NULL);
  311. free(ocd_name);
  312. if (JIM_OK != retval)
  313. return retval;
  314. /* we now need to add an overrideable proc */
  315. char *override_name = alloc_printf(
  316. "proc %s {args} {eval ocd_bouncer %s $args}",
  317. c->name, c->name);
  318. if (NULL == override_name)
  319. return JIM_ERR;
  320. retval = Jim_Eval_Named(interp, override_name, 0, 0);
  321. free(override_name);
  322. return retval;
  323. }
  324. struct command *register_command(struct command_context *context,
  325. struct command *parent, const struct command_registration *cr)
  326. {
  327. if (!context || !cr->name)
  328. return NULL;
  329. const char *name = cr->name;
  330. struct command **head = command_list_for_parent(context, parent);
  331. struct command *c = command_find(*head, name);
  332. if (NULL != c) {
  333. /* TODO: originally we treated attempting to register a cmd twice as an error
  334. * Sometimes we need this behaviour, such as with flash banks.
  335. * http://www.mail-archive.com/openocd-development@lists.berlios.de/msg11152.html */
  336. LOG_DEBUG("command '%s' is already registered in '%s' context",
  337. name, parent ? parent->name : "<global>");
  338. return c;
  339. }
  340. c = command_new(context, parent, cr);
  341. if (NULL == c)
  342. return NULL;
  343. int retval = ERROR_OK;
  344. if (NULL != cr->jim_handler && NULL == parent) {
  345. retval = Jim_CreateCommand(context->interp->interp, cr->name,
  346. cr->jim_handler, cr->jim_handler_data, NULL);
  347. } else if (NULL != cr->handler || NULL != parent)
  348. retval = register_command_handler(context, command_root(c));
  349. if (ERROR_OK != retval) {
  350. unregister_command(context, parent, name);
  351. c = NULL;
  352. }
  353. return c;
  354. }
  355. int register_commands(struct command_context *cmd_ctx, struct command *parent,
  356. const struct command_registration *cmds)
  357. {
  358. int retval = ERROR_OK;
  359. unsigned i;
  360. for (i = 0; cmds[i].name || cmds[i].chain; i++) {
  361. const struct command_registration *cr = cmds + i;
  362. struct command *c = NULL;
  363. if (NULL != cr->name) {
  364. c = register_command(cmd_ctx, parent, cr);
  365. if (NULL == c) {
  366. retval = ERROR_FAIL;
  367. break;
  368. }
  369. }
  370. if (NULL != cr->chain) {
  371. struct command *p = c ? : parent;
  372. retval = register_commands(cmd_ctx, p, cr->chain);
  373. if (ERROR_OK != retval)
  374. break;
  375. }
  376. }
  377. if (ERROR_OK != retval) {
  378. for (unsigned j = 0; j < i; j++)
  379. unregister_command(cmd_ctx, parent, cmds[j].name);
  380. }
  381. return retval;
  382. }
  383. int unregister_all_commands(struct command_context *context,
  384. struct command *parent)
  385. {
  386. if (context == NULL)
  387. return ERROR_OK;
  388. struct command **head = command_list_for_parent(context, parent);
  389. while (NULL != *head) {
  390. struct command *tmp = *head;
  391. *head = tmp->next;
  392. command_free(tmp);
  393. }
  394. return ERROR_OK;
  395. }
  396. int unregister_command(struct command_context *context,
  397. struct command *parent, const char *name)
  398. {
  399. if ((!context) || (!name))
  400. return ERROR_COMMAND_SYNTAX_ERROR;
  401. struct command *p = NULL;
  402. struct command **head = command_list_for_parent(context, parent);
  403. for (struct command *c = *head; NULL != c; p = c, c = c->next) {
  404. if (strcmp(name, c->name) != 0)
  405. continue;
  406. if (p)
  407. p->next = c->next;
  408. else
  409. *head = c->next;
  410. command_free(c);
  411. return ERROR_OK;
  412. }
  413. return ERROR_OK;
  414. }
  415. void command_set_handler_data(struct command *c, void *p)
  416. {
  417. if (NULL != c->handler || NULL != c->jim_handler)
  418. c->jim_handler_data = p;
  419. for (struct command *cc = c->children; NULL != cc; cc = cc->next)
  420. command_set_handler_data(cc, p);
  421. }
  422. void command_output_text(struct command_context *context, const char *data)
  423. {
  424. if (context && context->output_handler && data)
  425. context->output_handler(context, data);
  426. }
  427. void command_print_sameline(struct command_context *context, const char *format, ...)
  428. {
  429. char *string;
  430. va_list ap;
  431. va_start(ap, format);
  432. string = alloc_vprintf(format, ap);
  433. if (string != NULL) {
  434. /* we want this collected in the log + we also want to pick it up as a tcl return
  435. * value.
  436. *
  437. * The latter bit isn't precisely neat, but will do for now.
  438. */
  439. LOG_USER_N("%s", string);
  440. /* We already printed it above
  441. * command_output_text(context, string); */
  442. free(string);
  443. }
  444. va_end(ap);
  445. }
  446. void command_print(struct command_context *context, const char *format, ...)
  447. {
  448. char *string;
  449. va_list ap;
  450. va_start(ap, format);
  451. string = alloc_vprintf(format, ap);
  452. if (string != NULL) {
  453. strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one
  454. *char longer */
  455. /* we want this collected in the log + we also want to pick it up as a tcl return
  456. * value.
  457. *
  458. * The latter bit isn't precisely neat, but will do for now.
  459. */
  460. LOG_USER_N("%s", string);
  461. /* We already printed it above
  462. * command_output_text(context, string); */
  463. free(string);
  464. }
  465. va_end(ap);
  466. }
  467. static char *__command_name(struct command *c, char delim, unsigned extra)
  468. {
  469. char *name;
  470. unsigned len = strlen(c->name);
  471. if (NULL == c->parent) {
  472. /* allocate enough for the name, child names, and '\0' */
  473. name = malloc(len + extra + 1);
  474. strcpy(name, c->name);
  475. } else {
  476. /* parent's extra must include both the space and name */
  477. name = __command_name(c->parent, delim, 1 + len + extra);
  478. char dstr[2] = { delim, 0 };
  479. strcat(name, dstr);
  480. strcat(name, c->name);
  481. }
  482. return name;
  483. }
  484. char *command_name(struct command *c, char delim)
  485. {
  486. return __command_name(c, delim, 0);
  487. }
  488. static bool command_can_run(struct command_context *cmd_ctx, struct command *c)
  489. {
  490. return c->mode == COMMAND_ANY || c->mode == cmd_ctx->mode;
  491. }
  492. static int run_command(struct command_context *context,
  493. struct command *c, const char *words[], unsigned num_words)
  494. {
  495. if (!command_can_run(context, c)) {
  496. /* Many commands may be run only before/after 'init' */
  497. const char *when;
  498. switch (c->mode) {
  499. case COMMAND_CONFIG:
  500. when = "before";
  501. break;
  502. case COMMAND_EXEC:
  503. when = "after";
  504. break;
  505. /* handle the impossible with humor; it guarantees a bug report! */
  506. default:
  507. when = "if Cthulhu is summoned by";
  508. break;
  509. }
  510. LOG_ERROR("The '%s' command must be used %s 'init'.",
  511. c->name, when);
  512. return ERROR_FAIL;
  513. }
  514. struct command_invocation cmd = {
  515. .ctx = context,
  516. .current = c,
  517. .name = c->name,
  518. .argc = num_words - 1,
  519. .argv = words + 1,
  520. };
  521. int retval = c->handler(&cmd);
  522. if (retval == ERROR_COMMAND_SYNTAX_ERROR) {
  523. /* Print help for command */
  524. char *full_name = command_name(c, ' ');
  525. if (NULL != full_name) {
  526. command_run_linef(context, "usage %s", full_name);
  527. free(full_name);
  528. } else
  529. retval = -ENOMEM;
  530. } else if (retval == ERROR_COMMAND_CLOSE_CONNECTION) {
  531. /* just fall through for a shutdown request */
  532. } else if (retval != ERROR_OK) {
  533. /* we do not print out an error message because the command *should*
  534. * have printed out an error
  535. */
  536. LOG_DEBUG("Command failed with error code %d", retval);
  537. }
  538. return retval;
  539. }
  540. int command_run_line(struct command_context *context, char *line)
  541. {
  542. /* all the parent commands have been registered with the interpreter
  543. * so, can just evaluate the line as a script and check for
  544. * results
  545. */
  546. /* run the line thru a script engine */
  547. int retval = ERROR_FAIL;
  548. int retcode;
  549. /* Beware! This code needs to be reentrant. It is also possible
  550. * for OpenOCD commands to be invoked directly from Tcl. This would
  551. * happen when the Jim Tcl interpreter is provided by eCos for
  552. * instance.
  553. */
  554. Jim_Interp *interp = context->interp->interp;
  555. Jim_DeleteAssocData(interp, "context");
  556. retcode = Jim_SetAssocData(interp, "context", NULL, context);
  557. if (retcode == JIM_OK) {
  558. /* associated the return value */
  559. Jim_DeleteAssocData(interp, "retval");
  560. retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
  561. if (retcode == JIM_OK) {
  562. retcode = Jim_Eval_Named(interp, line, 0, 0);
  563. Jim_DeleteAssocData(interp, "retval");
  564. }
  565. Jim_DeleteAssocData(interp, "context");
  566. }
  567. if (retcode == JIM_OK) {
  568. const char *result;
  569. int reslen;
  570. result = Jim_GetString(Jim_GetResult(interp), &reslen);
  571. if (reslen > 0) {
  572. int i;
  573. char buff[256 + 1];
  574. for (i = 0; i < reslen; i += 256) {
  575. int chunk;
  576. chunk = reslen - i;
  577. if (chunk > 256)
  578. chunk = 256;
  579. strncpy(buff, result + i, chunk);
  580. buff[chunk] = 0;
  581. LOG_USER_N("%s", buff);
  582. }
  583. LOG_USER_N("\n");
  584. }
  585. retval = ERROR_OK;
  586. } else if (retcode == JIM_EXIT) {
  587. /* ignore.
  588. * exit(Jim_GetExitCode(interp)); */
  589. } else if (retcode == ERROR_COMMAND_CLOSE_CONNECTION) {
  590. return retcode;
  591. } else {
  592. Jim_MakeErrorMessage(interp);
  593. LOG_USER("%s", Jim_GetString(Jim_GetResult(interp), NULL));
  594. if (retval == ERROR_OK) {
  595. /* It wasn't a low level OpenOCD command that failed */
  596. return ERROR_FAIL;
  597. }
  598. return retval;
  599. }
  600. return retval;
  601. }
  602. int command_run_linef(struct command_context *context, const char *format, ...)
  603. {
  604. int retval = ERROR_FAIL;
  605. char *string;
  606. va_list ap;
  607. va_start(ap, format);
  608. string = alloc_vprintf(format, ap);
  609. if (string != NULL) {
  610. retval = command_run_line(context, string);
  611. free(string);
  612. }
  613. va_end(ap);
  614. return retval;
  615. }
  616. void command_set_output_handler(struct command_context *context,
  617. command_output_handler_t output_handler, void *priv)
  618. {
  619. context->output_handler = output_handler;
  620. context->output_handler_priv = priv;
  621. }
  622. struct command_context *copy_command_context(struct command_context *context)
  623. {
  624. struct command_context *copy_context;
  625. copy_context = malloc(sizeof(struct command_context));
  626. if (!copy_context)
  627. return NULL;
  628. context->interp->refcnt++;
  629. *copy_context = *context;
  630. return copy_context;
  631. }
  632. void command_done(struct command_context *cmd_ctx)
  633. {
  634. if (cmd_ctx == NULL)
  635. return;
  636. cmd_ctx->interp->refcnt--;
  637. if (!cmd_ctx->interp->refcnt) {
  638. if (cmd_ctx->interp->free)
  639. Jim_FreeInterp(cmd_ctx->interp->interp);
  640. free(cmd_ctx->interp);
  641. }
  642. free(cmd_ctx);
  643. }
  644. /* find full path to file */
  645. static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  646. {
  647. if (argc != 2)
  648. return JIM_ERR;
  649. const char *file = Jim_GetString(argv[1], NULL);
  650. char *full_path = find_file(file);
  651. if (full_path == NULL)
  652. return JIM_ERR;
  653. Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
  654. free(full_path);
  655. Jim_SetResult(interp, result);
  656. return JIM_OK;
  657. }
  658. COMMAND_HANDLER(jim_echo)
  659. {
  660. if (CMD_ARGC == 2 && !strcmp(CMD_ARGV[0], "-n")) {
  661. LOG_USER_N("%s", CMD_ARGV[1]);
  662. return JIM_OK;
  663. }
  664. if (CMD_ARGC != 1)
  665. return JIM_ERR;
  666. LOG_USER("%s", CMD_ARGV[0]);
  667. return JIM_OK;
  668. }
  669. /* Capture progress output and return as tcl return value. If the
  670. * progress output was empty, return tcl return value.
  671. */
  672. static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  673. {
  674. if (argc != 2)
  675. return JIM_ERR;
  676. struct log_capture_state *state = command_log_capture_start(interp);
  677. /* disable polling during capture. This avoids capturing output
  678. * from polling.
  679. *
  680. * This is necessary in order to avoid accidentally getting a non-empty
  681. * string for tcl fn's.
  682. */
  683. bool save_poll = jtag_poll_get_enabled();
  684. jtag_poll_set_enabled(false);
  685. const char *str = Jim_GetString(argv[1], NULL);
  686. int retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__);
  687. jtag_poll_set_enabled(save_poll);
  688. command_log_capture_finish(state);
  689. return retcode;
  690. }
  691. static COMMAND_HELPER(command_help_find, struct command *head,
  692. struct command **out)
  693. {
  694. if (0 == CMD_ARGC)
  695. return ERROR_COMMAND_SYNTAX_ERROR;
  696. *out = command_find(head, CMD_ARGV[0]);
  697. if (NULL == *out && strncmp(CMD_ARGV[0], "ocd_", 4) == 0)
  698. *out = command_find(head, CMD_ARGV[0] + 4);
  699. if (NULL == *out)
  700. return ERROR_COMMAND_SYNTAX_ERROR;
  701. if (--CMD_ARGC == 0)
  702. return ERROR_OK;
  703. CMD_ARGV++;
  704. return CALL_COMMAND_HANDLER(command_help_find, (*out)->children, out);
  705. }
  706. static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
  707. bool show_help, const char *cmd_match);
  708. static COMMAND_HELPER(command_help_show_list, struct command *head, unsigned n,
  709. bool show_help, const char *cmd_match)
  710. {
  711. for (struct command *c = head; NULL != c; c = c->next)
  712. CALL_COMMAND_HANDLER(command_help_show, c, n, show_help, cmd_match);
  713. return ERROR_OK;
  714. }
  715. #define HELP_LINE_WIDTH(_n) (int)(76 - (2 * _n))
  716. static void command_help_show_indent(unsigned n)
  717. {
  718. for (unsigned i = 0; i < n; i++)
  719. LOG_USER_N(" ");
  720. }
  721. static void command_help_show_wrap(const char *str, unsigned n, unsigned n2)
  722. {
  723. const char *cp = str, *last = str;
  724. while (*cp) {
  725. const char *next = last;
  726. do {
  727. cp = next;
  728. do {
  729. next++;
  730. } while (*next != ' ' && *next != '\t' && *next != '\0');
  731. } while ((next - last < HELP_LINE_WIDTH(n)) && *next != '\0');
  732. if (next - last < HELP_LINE_WIDTH(n))
  733. cp = next;
  734. command_help_show_indent(n);
  735. LOG_USER("%.*s", (int)(cp - last), last);
  736. last = cp + 1;
  737. n = n2;
  738. }
  739. }
  740. static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
  741. bool show_help, const char *cmd_match)
  742. {
  743. char *cmd_name = command_name(c, ' ');
  744. if (NULL == cmd_name)
  745. return -ENOMEM;
  746. /* If the match string occurs anywhere, we print out
  747. * stuff for this command. */
  748. bool is_match = (strstr(cmd_name, cmd_match) != NULL) ||
  749. ((c->usage != NULL) && (strstr(c->usage, cmd_match) != NULL)) ||
  750. ((c->help != NULL) && (strstr(c->help, cmd_match) != NULL));
  751. if (is_match) {
  752. command_help_show_indent(n);
  753. LOG_USER_N("%s", cmd_name);
  754. }
  755. free(cmd_name);
  756. if (is_match) {
  757. if (c->usage && strlen(c->usage) > 0) {
  758. LOG_USER_N(" ");
  759. command_help_show_wrap(c->usage, 0, n + 5);
  760. } else
  761. LOG_USER_N("\n");
  762. }
  763. if (is_match && show_help) {
  764. char *msg;
  765. /* Normal commands are runtime-only; highlight exceptions */
  766. if (c->mode != COMMAND_EXEC) {
  767. const char *stage_msg = "";
  768. switch (c->mode) {
  769. case COMMAND_CONFIG:
  770. stage_msg = " (configuration command)";
  771. break;
  772. case COMMAND_ANY:
  773. stage_msg = " (command valid any time)";
  774. break;
  775. default:
  776. stage_msg = " (?mode error?)";
  777. break;
  778. }
  779. msg = alloc_printf("%s%s", c->help ? : "", stage_msg);
  780. } else
  781. msg = alloc_printf("%s", c->help ? : "");
  782. if (NULL != msg) {
  783. command_help_show_wrap(msg, n + 3, n + 3);
  784. free(msg);
  785. } else
  786. return -ENOMEM;
  787. }
  788. if (++n > 5) {
  789. LOG_ERROR("command recursion exceeded");
  790. return ERROR_FAIL;
  791. }
  792. return CALL_COMMAND_HANDLER(command_help_show_list,
  793. c->children, n, show_help, cmd_match);
  794. }
  795. COMMAND_HANDLER(handle_help_command)
  796. {
  797. bool full = strcmp(CMD_NAME, "help") == 0;
  798. int retval;
  799. struct command *c = CMD_CTX->commands;
  800. char *cmd_match = NULL;
  801. if (CMD_ARGC == 0)
  802. cmd_match = "";
  803. else if (CMD_ARGC >= 1) {
  804. unsigned i;
  805. for (i = 0; i < CMD_ARGC; ++i) {
  806. if (NULL != cmd_match) {
  807. char *prev = cmd_match;
  808. cmd_match = alloc_printf("%s %s", cmd_match, CMD_ARGV[i]);
  809. free(prev);
  810. if (NULL == cmd_match) {
  811. LOG_ERROR("unable to build search string");
  812. return -ENOMEM;
  813. }
  814. } else {
  815. cmd_match = alloc_printf("%s", CMD_ARGV[i]);
  816. if (NULL == cmd_match) {
  817. LOG_ERROR("unable to build search string");
  818. return -ENOMEM;
  819. }
  820. }
  821. }
  822. } else
  823. return ERROR_COMMAND_SYNTAX_ERROR;
  824. retval = CALL_COMMAND_HANDLER(command_help_show_list,
  825. c, 0, full, cmd_match);
  826. if (CMD_ARGC >= 1)
  827. free(cmd_match);
  828. return retval;
  829. }
  830. static int command_unknown_find(unsigned argc, Jim_Obj *const *argv,
  831. struct command *head, struct command **out, bool top_level)
  832. {
  833. if (0 == argc)
  834. return argc;
  835. const char *cmd_name = Jim_GetString(argv[0], NULL);
  836. struct command *c = command_find(head, cmd_name);
  837. if (NULL == c && top_level && strncmp(cmd_name, "ocd_", 4) == 0)
  838. c = command_find(head, cmd_name + 4);
  839. if (NULL == c)
  840. return argc;
  841. *out = c;
  842. return command_unknown_find(--argc, ++argv, (*out)->children, out, false);
  843. }
  844. static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  845. {
  846. const char *cmd_name = Jim_GetString(argv[0], NULL);
  847. if (strcmp(cmd_name, "unknown") == 0) {
  848. if (argc == 1)
  849. return JIM_OK;
  850. argc--;
  851. argv++;
  852. }
  853. script_debug(interp, cmd_name, argc, argv);
  854. struct command_context *cmd_ctx = current_command_context(interp);
  855. struct command *c = cmd_ctx->commands;
  856. int remaining = command_unknown_find(argc, argv, c, &c, true);
  857. /* if nothing could be consumed, then it's really an unknown command */
  858. if (remaining == argc) {
  859. const char *cmd = Jim_GetString(argv[0], NULL);
  860. LOG_ERROR("Unknown command:\n %s", cmd);
  861. return JIM_OK;
  862. }
  863. bool found = true;
  864. Jim_Obj *const *start;
  865. unsigned count;
  866. if (c->handler || c->jim_handler) {
  867. /* include the command name in the list */
  868. count = remaining + 1;
  869. start = argv + (argc - remaining - 1);
  870. } else {
  871. c = command_find(cmd_ctx->commands, "usage");
  872. if (NULL == c) {
  873. LOG_ERROR("unknown command, but usage is missing too");
  874. return JIM_ERR;
  875. }
  876. count = argc - remaining;
  877. start = argv;
  878. found = false;
  879. }
  880. /* pass the command through to the intended handler */
  881. if (c->jim_handler) {
  882. interp->cmdPrivData = c->jim_handler_data;
  883. return (*c->jim_handler)(interp, count, start);
  884. }
  885. return script_command_run(interp, count, start, c, found);
  886. }
  887. static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  888. {
  889. struct command_context *cmd_ctx = current_command_context(interp);
  890. enum command_mode mode;
  891. if (argc > 1) {
  892. struct command *c = cmd_ctx->commands;
  893. int remaining = command_unknown_find(argc - 1, argv + 1, c, &c, true);
  894. /* if nothing could be consumed, then it's an unknown command */
  895. if (remaining == argc - 1) {
  896. Jim_SetResultString(interp, "unknown", -1);
  897. return JIM_OK;
  898. }
  899. mode = c->mode;
  900. } else
  901. mode = cmd_ctx->mode;
  902. const char *mode_str;
  903. switch (mode) {
  904. case COMMAND_ANY:
  905. mode_str = "any";
  906. break;
  907. case COMMAND_CONFIG:
  908. mode_str = "config";
  909. break;
  910. case COMMAND_EXEC:
  911. mode_str = "exec";
  912. break;
  913. default:
  914. mode_str = "unknown";
  915. break;
  916. }
  917. Jim_SetResultString(interp, mode_str, -1);
  918. return JIM_OK;
  919. }
  920. static int jim_command_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  921. {
  922. if (1 == argc)
  923. return JIM_ERR;
  924. struct command_context *cmd_ctx = current_command_context(interp);
  925. struct command *c = cmd_ctx->commands;
  926. int remaining = command_unknown_find(argc - 1, argv + 1, c, &c, true);
  927. /* if nothing could be consumed, then it's an unknown command */
  928. if (remaining == argc - 1) {
  929. Jim_SetResultString(interp, "unknown", -1);
  930. return JIM_OK;
  931. }
  932. if (c->jim_handler)
  933. Jim_SetResultString(interp, "native", -1);
  934. else if (c->handler)
  935. Jim_SetResultString(interp, "simple", -1);
  936. else if (remaining == 0)
  937. Jim_SetResultString(interp, "group", -1);
  938. else
  939. Jim_SetResultString(interp, "unknown", -1);
  940. return JIM_OK;
  941. }
  942. int help_add_command(struct command_context *cmd_ctx, struct command *parent,
  943. const char *cmd_name, const char *help_text, const char *usage)
  944. {
  945. struct command **head = command_list_for_parent(cmd_ctx, parent);
  946. struct command *nc = command_find(*head, cmd_name);
  947. if (NULL == nc) {
  948. /* add a new command with help text */
  949. struct command_registration cr = {
  950. .name = cmd_name,
  951. .mode = COMMAND_ANY,
  952. .help = help_text,
  953. .usage = usage,
  954. };
  955. nc = register_command(cmd_ctx, parent, &cr);
  956. if (NULL == nc) {
  957. LOG_ERROR("failed to add '%s' help text", cmd_name);
  958. return ERROR_FAIL;
  959. }
  960. LOG_DEBUG("added '%s' help text", cmd_name);
  961. return ERROR_OK;
  962. }
  963. if (help_text) {
  964. bool replaced = false;
  965. if (nc->help) {
  966. free(nc->help);
  967. replaced = true;
  968. }
  969. nc->help = strdup(help_text);
  970. if (replaced)
  971. LOG_INFO("replaced existing '%s' help", cmd_name);
  972. else
  973. LOG_DEBUG("added '%s' help text", cmd_name);
  974. }
  975. if (usage) {
  976. bool replaced = false;
  977. if (nc->usage) {
  978. free(nc->usage);
  979. replaced = true;
  980. }
  981. nc->usage = strdup(usage);
  982. if (replaced)
  983. LOG_INFO("replaced existing '%s' usage", cmd_name);
  984. else
  985. LOG_DEBUG("added '%s' usage text", cmd_name);
  986. }
  987. return ERROR_OK;
  988. }
  989. COMMAND_HANDLER(handle_help_add_command)
  990. {
  991. if (CMD_ARGC < 2) {
  992. LOG_ERROR("%s: insufficient arguments", CMD_NAME);
  993. return ERROR_COMMAND_SYNTAX_ERROR;
  994. }
  995. /* save help text and remove it from argument list */
  996. const char *str = CMD_ARGV[--CMD_ARGC];
  997. const char *help = !strcmp(CMD_NAME, "add_help_text") ? str : NULL;
  998. const char *usage = !strcmp(CMD_NAME, "add_usage_text") ? str : NULL;
  999. if (!help && !usage) {
  1000. LOG_ERROR("command name '%s' is unknown", CMD_NAME);
  1001. return ERROR_COMMAND_SYNTAX_ERROR;
  1002. }
  1003. /* likewise for the leaf command name */
  1004. const char *cmd_name = CMD_ARGV[--CMD_ARGC];
  1005. struct command *c = NULL;
  1006. if (CMD_ARGC > 0) {
  1007. c = CMD_CTX->commands;
  1008. int retval = CALL_COMMAND_HANDLER(command_help_find, c, &c);
  1009. if (ERROR_OK != retval)
  1010. return retval;
  1011. }
  1012. return help_add_command(CMD_CTX, c, cmd_name, help, usage);
  1013. }
  1014. /* sleep command sleeps for <n> milliseconds
  1015. * this is useful in target startup scripts
  1016. */
  1017. COMMAND_HANDLER(handle_sleep_command)
  1018. {
  1019. bool busy = false;
  1020. if (CMD_ARGC == 2) {
  1021. if (strcmp(CMD_ARGV[1], "busy") == 0)
  1022. busy = true;
  1023. else
  1024. return ERROR_COMMAND_SYNTAX_ERROR;
  1025. } else if (CMD_ARGC < 1 || CMD_ARGC > 2)
  1026. return ERROR_COMMAND_SYNTAX_ERROR;
  1027. unsigned long duration = 0;
  1028. int retval = parse_ulong(CMD_ARGV[0], &duration);
  1029. if (ERROR_OK != retval)
  1030. return retval;
  1031. if (!busy) {
  1032. int64_t then = timeval_ms();
  1033. while (timeval_ms() - then < (int64_t)duration) {
  1034. target_call_timer_callbacks_now();
  1035. usleep(1000);
  1036. }
  1037. } else
  1038. busy_sleep(duration);
  1039. return ERROR_OK;
  1040. }
  1041. static const struct command_registration command_subcommand_handlers[] = {
  1042. {
  1043. .name = "mode",
  1044. .mode = COMMAND_ANY,
  1045. .jim_handler = jim_command_mode,
  1046. .usage = "[command_name ...]",
  1047. .help = "Returns the command modes allowed by a command:"
  1048. "'any', 'config', or 'exec'. If no command is"
  1049. "specified, returns the current command mode. "
  1050. "Returns 'unknown' if an unknown command is given. "
  1051. "Command can be multiple tokens.",
  1052. },
  1053. {
  1054. .name = "type",
  1055. .mode = COMMAND_ANY,
  1056. .jim_handler = jim_command_type,
  1057. .usage = "command_name [...]",
  1058. .help = "Returns the type of built-in command:"
  1059. "'native', 'simple', 'group', or 'unknown'. "
  1060. "Command can be multiple tokens.",
  1061. },
  1062. COMMAND_REGISTRATION_DONE
  1063. };
  1064. static const struct command_registration command_builtin_handlers[] = {
  1065. {
  1066. .name = "echo",
  1067. .handler = jim_echo,
  1068. .mode = COMMAND_ANY,
  1069. .help = "Logs a message at \"user\" priority. "
  1070. "Output message to stdout. "
  1071. "Option \"-n\" suppresses trailing newline",
  1072. .usage = "[-n] string",
  1073. },
  1074. {
  1075. .name = "add_help_text",
  1076. .handler = handle_help_add_command,
  1077. .mode = COMMAND_ANY,
  1078. .help = "Add new command help text; "
  1079. "Command can be multiple tokens.",
  1080. .usage = "command_name helptext_string",
  1081. },
  1082. {
  1083. .name = "add_usage_text",
  1084. .handler = handle_help_add_command,
  1085. .mode = COMMAND_ANY,
  1086. .help = "Add new command usage text; "
  1087. "command can be multiple tokens.",
  1088. .usage = "command_name usage_string",
  1089. },
  1090. {
  1091. .name = "sleep",
  1092. .handler = handle_sleep_command,
  1093. .mode = COMMAND_ANY,
  1094. .help = "Sleep for specified number of milliseconds. "
  1095. "\"busy\" will busy wait instead (avoid this).",
  1096. .usage = "milliseconds ['busy']",
  1097. },
  1098. {
  1099. .name = "help",
  1100. .handler = handle_help_command,
  1101. .mode = COMMAND_ANY,
  1102. .help = "Show full command help; "
  1103. "command can be multiple tokens.",
  1104. .usage = "[command_name]",
  1105. },
  1106. {
  1107. .name = "usage",
  1108. .handler = handle_help_command,
  1109. .mode = COMMAND_ANY,
  1110. .help = "Show basic command usage; "
  1111. "command can be multiple tokens.",
  1112. .usage = "[command_name]",
  1113. },
  1114. {
  1115. .name = "command",
  1116. .mode = COMMAND_ANY,
  1117. .help = "core command group (introspection)",
  1118. .chain = command_subcommand_handlers,
  1119. },
  1120. COMMAND_REGISTRATION_DONE
  1121. };
  1122. struct command_context *command_init(const char *startup_tcl, Jim_Interp *interp)
  1123. {
  1124. struct command_context *context;
  1125. const char *HostOs;
  1126. context = malloc(sizeof(struct command_context));
  1127. if (!context) {
  1128. LOG_ERROR("Failed to allocate command context.");
  1129. return NULL;
  1130. }
  1131. context->mode = COMMAND_EXEC;
  1132. context->commands = NULL;
  1133. context->current_target = 0;
  1134. context->output_handler = NULL;
  1135. context->output_handler_priv = NULL;
  1136. context->interp = malloc(sizeof(struct command_interpreter));
  1137. if (!context->interp) {
  1138. LOG_ERROR("Failed to allocate command interpreter.");
  1139. free(context);
  1140. return NULL;
  1141. }
  1142. /* Create a jim interpreter if we were not handed one */
  1143. if (interp == NULL) {
  1144. /* Create an interpreter */
  1145. interp = Jim_CreateInterp();
  1146. /* Add all the Jim core commands */
  1147. Jim_RegisterCoreCommands(interp);
  1148. Jim_InitStaticExtensions(interp);
  1149. context->interp->free = true;
  1150. } else {
  1151. context->interp->free = false;
  1152. }
  1153. context->interp->interp = interp;
  1154. context->interp->refcnt = 1;
  1155. /* Stick to lowercase for HostOS strings. */
  1156. #if defined(_MSC_VER)
  1157. /* WinXX - is generic, the forward
  1158. * looking problem is this:
  1159. *
  1160. * "win32" or "win64"
  1161. *
  1162. * "winxx" is generic.
  1163. */
  1164. HostOs = "winxx";
  1165. #elif defined(__linux__)
  1166. HostOs = "linux";
  1167. #elif defined(__APPLE__) || defined(__DARWIN__)
  1168. HostOs = "darwin";
  1169. #elif defined(__CYGWIN__)
  1170. HostOs = "cygwin";
  1171. #elif defined(__MINGW32__)
  1172. HostOs = "mingw32";
  1173. #elif defined(__ECOS)
  1174. HostOs = "ecos";
  1175. #elif defined(__FreeBSD__)
  1176. HostOs = "freebsd";
  1177. #elif defined(__NetBSD__)
  1178. HostOs = "netbsd";
  1179. #elif defined(__OpenBSD__)
  1180. HostOs = "openbsd";
  1181. #else
  1182. #warning "Unrecognized host OS..."
  1183. HostOs = "other";
  1184. #endif
  1185. Jim_SetGlobalVariableStr(interp, "ocd_HOSTOS",
  1186. Jim_NewStringObj(interp, HostOs, strlen(HostOs)));
  1187. Jim_CreateCommand(interp, "ocd_find", jim_find, NULL, NULL);
  1188. Jim_CreateCommand(interp, "capture", jim_capture, NULL, NULL);
  1189. register_commands(context, NULL, command_builtin_handlers);
  1190. Jim_SetAssocData(interp, "context", NULL, context);
  1191. if (Jim_Eval_Named(interp, startup_tcl, "embedded:startup.tcl", 1) == JIM_ERR) {
  1192. LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
  1193. Jim_MakeErrorMessage(interp);
  1194. LOG_USER_N("%s", Jim_GetString(Jim_GetResult(interp), NULL));
  1195. exit(-1);
  1196. }
  1197. Jim_DeleteAssocData(interp, "context");
  1198. return context;
  1199. }
  1200. int command_context_mode(struct command_context *cmd_ctx, enum command_mode mode)
  1201. {
  1202. if (!cmd_ctx)
  1203. return ERROR_COMMAND_SYNTAX_ERROR;
  1204. cmd_ctx->mode = mode;
  1205. return ERROR_OK;
  1206. }
  1207. void process_jim_events(struct command_context *cmd_ctx)
  1208. {
  1209. static int recursion;
  1210. if (recursion)
  1211. return;
  1212. recursion++;
  1213. Jim_ProcessEvents(cmd_ctx->interp->interp, JIM_ALL_EVENTS | JIM_DONT_WAIT);
  1214. recursion--;
  1215. }
  1216. #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
  1217. int parse ## name(const char *str, type * ul) \
  1218. { \
  1219. if (!*str) { \
  1220. LOG_ERROR("Invalid command argument"); \
  1221. return ERROR_COMMAND_ARGUMENT_INVALID; \
  1222. } \
  1223. char *end; \
  1224. *ul = func(str, &end, 0); \
  1225. if (*end) { \
  1226. LOG_ERROR("Invalid command argument"); \
  1227. return ERROR_COMMAND_ARGUMENT_INVALID; \
  1228. } \
  1229. if ((max == *ul) && (ERANGE == errno)) { \
  1230. LOG_ERROR("Argument overflow"); \
  1231. return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
  1232. } \
  1233. if (min && (min == *ul) && (ERANGE == errno)) { \
  1234. LOG_ERROR("Argument underflow"); \
  1235. return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
  1236. } \
  1237. return ERROR_OK; \
  1238. }
  1239. DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long, strtoul, 0, ULONG_MAX)
  1240. DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, 0, ULLONG_MAX)
  1241. DEFINE_PARSE_NUM_TYPE(_long, long, strtol, LONG_MIN, LONG_MAX)
  1242. DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MIN, LLONG_MAX)
  1243. #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
  1244. int parse ## name(const char *str, type * ul) \
  1245. { \
  1246. functype n; \
  1247. int retval = parse ## funcname(str, &n); \
  1248. if (ERROR_OK != retval) \
  1249. return retval; \
  1250. if (n > max) \
  1251. return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
  1252. if (min) \
  1253. return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
  1254. *ul = n; \
  1255. return ERROR_OK; \
  1256. }
  1257. #define DEFINE_PARSE_ULONGLONG(name, type, min, max) \
  1258. DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
  1259. DEFINE_PARSE_ULONGLONG(_uint, unsigned, 0, UINT_MAX)
  1260. DEFINE_PARSE_ULONGLONG(_u64, uint64_t, 0, UINT64_MAX)
  1261. DEFINE_PARSE_ULONGLONG(_u32, uint32_t, 0, UINT32_MAX)
  1262. DEFINE_PARSE_ULONGLONG(_u16, uint16_t, 0, UINT16_MAX)
  1263. DEFINE_PARSE_ULONGLONG(_u8, uint8_t, 0, UINT8_MAX)
  1264. DEFINE_PARSE_ULONGLONG(_target_addr, target_addr_t, 0, TARGET_ADDR_MAX)
  1265. #define DEFINE_PARSE_LONGLONG(name, type, min, max) \
  1266. DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
  1267. DEFINE_PARSE_LONGLONG(_int, int, n < INT_MIN, INT_MAX)
  1268. DEFINE_PARSE_LONGLONG(_s64, int64_t, n < INT64_MIN, INT64_MAX)
  1269. DEFINE_PARSE_LONGLONG(_s32, int32_t, n < INT32_MIN, INT32_MAX)
  1270. DEFINE_PARSE_LONGLONG(_s16, int16_t, n < INT16_MIN, INT16_MAX)
  1271. DEFINE_PARSE_LONGLONG(_s8, int8_t, n < INT8_MIN, INT8_MAX)
  1272. static int command_parse_bool(const char *in, bool *out,
  1273. const char *on, const char *off)
  1274. {
  1275. if (strcasecmp(in, on) == 0)
  1276. *out = true;
  1277. else if (strcasecmp(in, off) == 0)
  1278. *out = false;
  1279. else
  1280. return ERROR_COMMAND_SYNTAX_ERROR;
  1281. return ERROR_OK;
  1282. }
  1283. int command_parse_bool_arg(const char *in, bool *out)
  1284. {
  1285. if (command_parse_bool(in, out, "on", "off") == ERROR_OK)
  1286. return ERROR_OK;
  1287. if (command_parse_bool(in, out, "enable", "disable") == ERROR_OK)
  1288. return ERROR_OK;
  1289. if (command_parse_bool(in, out, "true", "false") == ERROR_OK)
  1290. return ERROR_OK;
  1291. if (command_parse_bool(in, out, "yes", "no") == ERROR_OK)
  1292. return ERROR_OK;
  1293. if (command_parse_bool(in, out, "1", "0") == ERROR_OK)
  1294. return ERROR_OK;
  1295. return ERROR_COMMAND_SYNTAX_ERROR;
  1296. }
  1297. COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label)
  1298. {
  1299. switch (CMD_ARGC) {
  1300. case 1: {
  1301. const char *in = CMD_ARGV[0];
  1302. if (command_parse_bool_arg(in, out) != ERROR_OK) {
  1303. LOG_ERROR("%s: argument '%s' is not valid", CMD_NAME, in);
  1304. return ERROR_COMMAND_SYNTAX_ERROR;
  1305. }
  1306. /* fall through */
  1307. }
  1308. case 0:
  1309. LOG_INFO("%s is %s", label, *out ? "enabled" : "disabled");
  1310. break;
  1311. default:
  1312. return ERROR_COMMAND_SYNTAX_ERROR;
  1313. }
  1314. return ERROR_OK;
  1315. }