1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008, Duane Ellis *
11 * openocd@duaneeellis.com *
13 * part of this file is taken from libcli (libcli.sourceforge.net) *
14 * Copyright (C) David Parrish (david@dparrish.com) *
15 ***************************************************************************/
21 /* @todo the inclusion of target.h here is a layering violation */
22 #include <jtag/jtag.h>
23 #include <target/target.h>
25 #include "configuration.h"
27 #include "time_support.h"
28 #include "jim-eventloop.h"
30 /* nice short description of source file */
31 #define __THIS__FILE__ "command.c"
33 struct log_capture_state
{
38 static int unregister_command(struct command_context
*context
,
39 const char *cmd_prefix
, const char *name
);
40 static int jim_command_dispatch(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
);
41 static int help_add_command(struct command_context
*cmd_ctx
,
42 const char *cmd_name
, const char *help_text
, const char *usage_text
);
43 static int help_del_command(struct command_context
*cmd_ctx
, const char *cmd_name
);
45 /* set of functions to wrap jimtcl internal data */
46 static inline bool jimcmd_is_proc(Jim_Cmd
*cmd
)
51 bool jimcmd_is_oocd_command(Jim_Cmd
*cmd
)
53 return !cmd
->isproc
&& cmd
->u
.native
.cmdProc
== jim_command_dispatch
;
56 void *jimcmd_privdata(Jim_Cmd
*cmd
)
58 return cmd
->isproc
? NULL
: cmd
->u
.native
.privData
;
61 static void tcl_output(void *privData
, const char *file
, unsigned line
,
62 const char *function
, const char *string
)
64 struct log_capture_state
*state
= privData
;
65 Jim_AppendString(state
->interp
, state
->output
, string
, strlen(string
));
68 static struct log_capture_state
*command_log_capture_start(Jim_Interp
*interp
)
70 /* capture log output and return it. A garbage collect can
71 * happen, so we need a reference count to this object */
72 Jim_Obj
*jim_output
= Jim_NewStringObj(interp
, "", 0);
76 Jim_IncrRefCount(jim_output
);
78 struct log_capture_state
*state
= malloc(sizeof(*state
));
80 LOG_ERROR("Out of memory");
81 Jim_DecrRefCount(interp
, jim_output
);
85 state
->interp
= interp
;
86 state
->output
= jim_output
;
88 log_add_callback(tcl_output
, state
);
93 /* Classic openocd commands provide progress output which we
94 * will capture and return as a Tcl return value.
96 * However, if a non-openocd command has been invoked, then it
97 * makes sense to return the tcl return value from that command.
99 * The tcl return value is empty for openocd commands that provide
102 * Therefore we set the tcl return value only if we actually
105 static void command_log_capture_finish(struct log_capture_state
*state
)
110 log_remove_callback(tcl_output
, state
);
113 Jim_GetString(state
->output
, &length
);
116 Jim_SetResult(state
->interp
, state
->output
);
118 /* No output captured, use tcl return value (which could
121 Jim_DecrRefCount(state
->interp
, state
->output
);
126 static int command_retval_set(Jim_Interp
*interp
, int retval
)
128 int *return_retval
= Jim_GetAssocData(interp
, "retval");
130 *return_retval
= retval
;
132 return (retval
== ERROR_OK
) ? JIM_OK
: retval
;
135 extern struct command_context
*global_cmd_ctx
;
137 /* dump a single line to the log for the command.
138 * Do nothing in case we are not at debug level 3 */
139 static void script_debug(Jim_Interp
*interp
, unsigned int argc
, Jim_Obj
* const *argv
)
141 if (debug_level
< LOG_LVL_DEBUG
)
144 char *dbg
= alloc_printf("command -");
145 for (unsigned i
= 0; i
< argc
; i
++) {
147 const char *w
= Jim_GetString(argv
[i
], &len
);
148 char *t
= alloc_printf("%s %s", dbg
, w
);
152 LOG_DEBUG("%s", dbg
);
156 static void script_command_args_free(char **words
, unsigned nwords
)
158 for (unsigned i
= 0; i
< nwords
; i
++)
163 static char **script_command_args_alloc(
164 unsigned argc
, Jim_Obj
* const *argv
, unsigned *nwords
)
166 char **words
= malloc(argc
* sizeof(char *));
171 for (i
= 0; i
< argc
; i
++) {
173 const char *w
= Jim_GetString(argv
[i
], &len
);
174 words
[i
] = strdup(w
);
176 script_command_args_free(words
, i
);
184 struct command_context
*current_command_context(Jim_Interp
*interp
)
186 /* grab the command context from the associated data */
187 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
189 /* Tcl can invoke commands directly instead of via command_run_line(). This would
190 * happen when the Jim Tcl interpreter is provided by eCos or if we are running
191 * commands in a startup script.
193 * A telnet or gdb server would provide a non-default command context to
194 * handle piping of error output, have a separate current target, etc.
196 cmd_ctx
= global_cmd_ctx
;
202 * Find a openocd command from fullname.
203 * @returns Returns the named command if it is registred in interp.
204 * Returns NULL otherwise.
206 static struct command
*command_find_from_name(Jim_Interp
*interp
, const char *name
)
211 Jim_Obj
*jim_name
= Jim_NewStringObj(interp
, name
, -1);
212 Jim_IncrRefCount(jim_name
);
213 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, jim_name
, JIM_NONE
);
214 Jim_DecrRefCount(interp
, jim_name
);
215 if (!cmd
|| jimcmd_is_proc(cmd
) || !jimcmd_is_oocd_command(cmd
))
218 return jimcmd_privdata(cmd
);
221 static struct command
*command_new(struct command_context
*cmd_ctx
,
222 const char *full_name
, const struct command_registration
*cr
)
227 * If it is a non-jim command with no .usage specified,
230 * strlen(.usage) == 0 means that the command takes no
233 if (!cr
->jim_handler
&& !cr
->usage
)
234 LOG_ERROR("BUG: command '%s' does not have the "
235 "'.usage' field filled out",
238 struct command
*c
= calloc(1, sizeof(struct command
));
242 c
->name
= strdup(cr
->name
);
248 c
->handler
= cr
->handler
;
249 c
->jim_handler
= cr
->jim_handler
;
252 if (cr
->help
|| cr
->usage
)
253 help_add_command(cmd_ctx
, full_name
, cr
->help
, cr
->usage
);
258 static void command_free(struct Jim_Interp
*interp
, void *priv
)
260 struct command
*c
= priv
;
266 static struct command
*register_command(struct command_context
*context
,
267 const char *cmd_prefix
, const struct command_registration
*cr
)
271 if (!context
|| !cr
->name
)
275 full_name
= alloc_printf("%s %s", cmd_prefix
, cr
->name
);
277 full_name
= strdup(cr
->name
);
281 struct command
*c
= command_find_from_name(context
->interp
, full_name
);
283 /* TODO: originally we treated attempting to register a cmd twice as an error
284 * Sometimes we need this behaviour, such as with flash banks.
285 * http://www.mail-archive.com/openocd-development@lists.berlios.de/msg11152.html */
286 LOG_DEBUG("command '%s' is already registered", full_name
);
291 c
= command_new(context
, full_name
, cr
);
297 if (false) /* too noisy with debug_level 3 */
298 LOG_DEBUG("registering '%s'...", full_name
);
299 int retval
= Jim_CreateCommand(context
->interp
, full_name
,
300 jim_command_dispatch
, c
, command_free
);
301 if (retval
!= JIM_OK
) {
302 command_run_linef(context
, "del_help_text {%s}", full_name
);
303 command_run_linef(context
, "del_usage_text {%s}", full_name
);
313 int __register_commands(struct command_context
*cmd_ctx
, const char *cmd_prefix
,
314 const struct command_registration
*cmds
, void *data
,
315 struct target
*override_target
)
317 int retval
= ERROR_OK
;
319 for (i
= 0; cmds
[i
].name
|| cmds
[i
].chain
; i
++) {
320 const struct command_registration
*cr
= cmds
+ i
;
322 struct command
*c
= NULL
;
324 c
= register_command(cmd_ctx
, cmd_prefix
, cr
);
329 c
->jim_handler_data
= data
;
330 c
->jim_override_target
= override_target
;
335 char *new_prefix
= alloc_printf("%s %s", cmd_prefix
, cr
->name
);
340 retval
= __register_commands(cmd_ctx
, new_prefix
, cr
->chain
, data
, override_target
);
343 retval
= __register_commands(cmd_ctx
, cr
->name
, cr
->chain
, data
, override_target
);
346 retval
= __register_commands(cmd_ctx
, cmd_prefix
, cr
->chain
, data
, override_target
);
348 if (retval
!= ERROR_OK
)
352 if (retval
!= ERROR_OK
) {
353 for (unsigned j
= 0; j
< i
; j
++)
354 unregister_command(cmd_ctx
, cmd_prefix
, cmds
[j
].name
);
359 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 2, 3)))
360 int unregister_commands_match(struct command_context
*cmd_ctx
, const char *format
, ...)
362 Jim_Interp
*interp
= cmd_ctx
->interp
;
365 va_start(ap
, format
);
366 char *query
= alloc_vprintf(format
, ap
);
371 char *query_cmd
= alloc_printf("info commands {%s}", query
);
376 int retval
= Jim_EvalSource(interp
, __THIS__FILE__
, __LINE__
, query_cmd
);
378 if (retval
!= JIM_OK
)
381 Jim_Obj
*list
= Jim_GetResult(interp
);
382 Jim_IncrRefCount(list
);
384 int len
= Jim_ListLength(interp
, list
);
385 for (int i
= 0; i
< len
; i
++) {
386 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, list
, i
);
387 Jim_IncrRefCount(elem
);
389 const char *name
= Jim_GetString(elem
, NULL
);
390 struct command
*c
= command_find_from_name(interp
, name
);
392 /* not openocd command */
393 Jim_DecrRefCount(interp
, elem
);
396 if (false) /* too noisy with debug_level 3 */
397 LOG_DEBUG("delete command \"%s\"", name
);
398 #if JIM_VERSION >= 80
399 Jim_DeleteCommand(interp
, elem
);
401 Jim_DeleteCommand(interp
, name
);
404 help_del_command(cmd_ctx
, name
);
406 Jim_DecrRefCount(interp
, elem
);
409 Jim_DecrRefCount(interp
, list
);
413 int unregister_all_commands(struct command_context
*context
,
414 const char *cmd_prefix
)
419 if (!cmd_prefix
|| !*cmd_prefix
)
420 return unregister_commands_match(context
, "*");
422 int retval
= unregister_commands_match(context
, "%s *", cmd_prefix
);
423 if (retval
!= ERROR_OK
)
426 return unregister_commands_match(context
, "%s", cmd_prefix
);
429 static int unregister_command(struct command_context
*context
,
430 const char *cmd_prefix
, const char *name
)
432 if (!context
|| !name
)
433 return ERROR_COMMAND_SYNTAX_ERROR
;
435 if (!cmd_prefix
|| !*cmd_prefix
)
436 return unregister_commands_match(context
, "%s", name
);
438 return unregister_commands_match(context
, "%s %s", cmd_prefix
, name
);
441 void command_output_text(struct command_context
*context
, const char *data
)
443 if (context
&& context
->output_handler
&& data
)
444 context
->output_handler(context
, data
);
447 void command_print_sameline(struct command_invocation
*cmd
, const char *format
, ...)
452 va_start(ap
, format
);
454 string
= alloc_vprintf(format
, ap
);
456 /* we want this collected in the log + we also want to pick it up as a tcl return
459 * The latter bit isn't precisely neat, but will do for now.
461 Jim_AppendString(cmd
->ctx
->interp
, cmd
->output
, string
, -1);
462 /* We already printed it above
463 * command_output_text(context, string); */
470 void command_print(struct command_invocation
*cmd
, const char *format
, ...)
475 va_start(ap
, format
);
477 string
= alloc_vprintf(format
, ap
);
479 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one
481 /* we want this collected in the log + we also want to pick it up as a tcl return
484 * The latter bit isn't precisely neat, but will do for now.
486 Jim_AppendString(cmd
->ctx
->interp
, cmd
->output
, string
, -1);
487 /* We already printed it above
488 * command_output_text(context, string); */
495 static bool command_can_run(struct command_context
*cmd_ctx
, struct command
*c
, const char *full_name
)
497 if (c
->mode
== COMMAND_ANY
|| c
->mode
== cmd_ctx
->mode
)
500 /* Many commands may be run only before/after 'init' */
509 /* handle the impossible with humor; it guarantees a bug report! */
511 when
= "if Cthulhu is summoned by";
514 LOG_ERROR("The '%s' command must be used %s 'init'.",
515 full_name
? full_name
: c
->name
, when
);
519 static int run_command(struct command_context
*context
,
520 struct command
*c
, const char **words
, unsigned num_words
)
522 struct command_invocation cmd
= {
526 .argc
= num_words
- 1,
530 cmd
.output
= Jim_NewEmptyStringObj(context
->interp
);
531 Jim_IncrRefCount(cmd
.output
);
533 int retval
= c
->handler(&cmd
);
534 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
) {
535 /* Print help for command */
536 command_run_linef(context
, "usage %s", words
[0]);
537 } else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
) {
538 /* just fall through for a shutdown request */
540 if (retval
!= ERROR_OK
)
541 LOG_DEBUG("Command '%s' failed with error code %d",
544 * Use the command output as the Tcl result.
545 * Drop last '\n' to allow command output concatenation
546 * while keep using command_print() everywhere.
548 const char *output_txt
= Jim_String(cmd
.output
);
549 int len
= strlen(output_txt
);
550 if (len
&& output_txt
[len
- 1] == '\n')
552 Jim_SetResultString(context
->interp
, output_txt
, len
);
554 Jim_DecrRefCount(context
->interp
, cmd
.output
);
559 int command_run_line(struct command_context
*context
, char *line
)
561 /* all the parent commands have been registered with the interpreter
562 * so, can just evaluate the line as a script and check for
565 /* run the line thru a script engine */
566 int retval
= ERROR_FAIL
;
568 /* Beware! This code needs to be reentrant. It is also possible
569 * for OpenOCD commands to be invoked directly from Tcl. This would
570 * happen when the Jim Tcl interpreter is provided by eCos for
573 struct target
*saved_target_override
= context
->current_target_override
;
574 context
->current_target_override
= NULL
;
576 Jim_Interp
*interp
= context
->interp
;
577 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
578 Jim_DeleteAssocData(interp
, "context");
579 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
580 if (retcode
== JIM_OK
) {
581 /* associated the return value */
582 Jim_DeleteAssocData(interp
, "retval");
583 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
584 if (retcode
== JIM_OK
) {
585 retcode
= Jim_Eval_Named(interp
, line
, 0, 0);
587 Jim_DeleteAssocData(interp
, "retval");
589 Jim_DeleteAssocData(interp
, "context");
590 int inner_retcode
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
591 if (retcode
== JIM_OK
)
592 retcode
= inner_retcode
;
594 context
->current_target_override
= saved_target_override
;
595 if (retcode
== JIM_OK
) {
599 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
601 command_output_text(context
, result
);
602 command_output_text(context
, "\n");
605 } else if (retcode
== JIM_EXIT
) {
607 * exit(Jim_GetExitCode(interp)); */
608 } else if (retcode
== ERROR_COMMAND_CLOSE_CONNECTION
) {
611 Jim_MakeErrorMessage(interp
);
612 /* error is broadcast */
613 LOG_USER("%s", Jim_GetString(Jim_GetResult(interp
), NULL
));
615 if (retval
== ERROR_OK
) {
616 /* It wasn't a low level OpenOCD command that failed */
625 int command_run_linef(struct command_context
*context
, const char *format
, ...)
627 int retval
= ERROR_FAIL
;
630 va_start(ap
, format
);
631 string
= alloc_vprintf(format
, ap
);
633 retval
= command_run_line(context
, string
);
640 void command_set_output_handler(struct command_context
*context
,
641 command_output_handler_t output_handler
, void *priv
)
643 context
->output_handler
= output_handler
;
644 context
->output_handler_priv
= priv
;
647 struct command_context
*copy_command_context(struct command_context
*context
)
649 struct command_context
*copy_context
= malloc(sizeof(struct command_context
));
651 *copy_context
= *context
;
656 void command_done(struct command_context
*cmd_ctx
)
664 /* find full path to file */
665 COMMAND_HANDLER(handle_find
)
668 return ERROR_COMMAND_SYNTAX_ERROR
;
670 char *full_path
= find_file(CMD_ARGV
[0]);
672 return ERROR_COMMAND_ARGUMENT_INVALID
;
674 command_print(CMD
, "%s", full_path
);
680 COMMAND_HANDLER(handle_echo
)
682 if (CMD_ARGC
== 2 && !strcmp(CMD_ARGV
[0], "-n")) {
683 LOG_USER_N("%s", CMD_ARGV
[1]);
690 LOG_USER("%s", CMD_ARGV
[0]);
694 /* Capture progress output and return as tcl return value. If the
695 * progress output was empty, return tcl return value.
697 static int jim_capture(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
702 struct log_capture_state
*state
= command_log_capture_start(interp
);
704 /* disable polling during capture. This avoids capturing output
707 * This is necessary in order to avoid accidentally getting a non-empty
708 * string for tcl fn's.
710 bool save_poll_mask
= jtag_poll_mask();
712 const char *str
= Jim_GetString(argv
[1], NULL
);
713 int retcode
= Jim_Eval_Named(interp
, str
, __THIS__FILE__
, __LINE__
);
715 jtag_poll_unmask(save_poll_mask
);
717 command_log_capture_finish(state
);
729 static COMMAND_HELPER(command_help_show
, struct help_entry
*c
,
730 bool show_help
, const char *cmd_match
);
732 static COMMAND_HELPER(command_help_show_list
, bool show_help
, const char *cmd_match
)
734 struct help_entry
*entry
;
736 list_for_each_entry(entry
, CMD_CTX
->help_list
, lh
)
737 CALL_COMMAND_HANDLER(command_help_show
, entry
, show_help
, cmd_match
);
741 #define HELP_LINE_WIDTH(_n) (int)(76 - (2 * _n))
743 static void command_help_show_indent(unsigned n
)
745 for (unsigned i
= 0; i
< n
; i
++)
748 static void command_help_show_wrap(const char *str
, unsigned n
, unsigned n2
)
750 const char *cp
= str
, *last
= str
;
752 const char *next
= last
;
757 } while (*next
!= ' ' && *next
!= '\t' && *next
!= '\0');
758 } while ((next
- last
< HELP_LINE_WIDTH(n
)) && *next
!= '\0');
759 if (next
- last
< HELP_LINE_WIDTH(n
))
761 command_help_show_indent(n
);
762 LOG_USER("%.*s", (int)(cp
- last
), last
);
768 static COMMAND_HELPER(command_help_show
, struct help_entry
*c
,
769 bool show_help
, const char *cmd_match
)
772 for (const char *s
= strchr(c
->cmd_name
, ' '); s
; s
= strchr(s
+ 1, ' '))
775 /* If the match string occurs anywhere, we print out
776 * stuff for this command. */
777 bool is_match
= strstr(c
->cmd_name
, cmd_match
) ||
778 (c
->usage
&& strstr(c
->usage
, cmd_match
)) ||
779 (c
->help
&& strstr(c
->help
, cmd_match
));
782 if (c
->usage
&& strlen(c
->usage
) > 0) {
783 char *msg
= alloc_printf("%s %s", c
->cmd_name
, c
->usage
);
784 command_help_show_wrap(msg
, n
, n
+ 5);
787 command_help_show_wrap(c
->cmd_name
, n
, n
+ 5);
791 if (is_match
&& show_help
) {
794 /* TODO: factorize jim_command_mode() to avoid running jim command here */
795 char *request
= alloc_printf("command mode %s", c
->cmd_name
);
797 LOG_ERROR("Out of memory");
800 int retval
= Jim_Eval(CMD_CTX
->interp
, request
);
802 enum command_mode mode
= COMMAND_UNKNOWN
;
803 if (retval
!= JIM_ERR
) {
804 const char *result
= Jim_GetString(Jim_GetResult(CMD_CTX
->interp
), NULL
);
805 if (!strcmp(result
, "any"))
807 else if (!strcmp(result
, "config"))
808 mode
= COMMAND_CONFIG
;
809 else if (!strcmp(result
, "exec"))
813 /* Normal commands are runtime-only; highlight exceptions */
814 if (mode
!= COMMAND_EXEC
) {
815 const char *stage_msg
= "";
819 stage_msg
= " (configuration command)";
822 stage_msg
= " (command valid any time)";
825 stage_msg
= " (?mode error?)";
828 msg
= alloc_printf("%s%s", c
->help
? c
->help
: "", stage_msg
);
830 msg
= alloc_printf("%s", c
->help
? c
->help
: "");
833 command_help_show_wrap(msg
, n
+ 3, n
+ 3);
842 COMMAND_HANDLER(handle_help_command
)
844 bool full
= strcmp(CMD_NAME
, "help") == 0;
849 cmd_match
= strdup("");
852 cmd_match
= strdup(CMD_ARGV
[0]);
854 for (unsigned int i
= 1; i
< CMD_ARGC
&& cmd_match
; ++i
) {
855 char *prev
= cmd_match
;
856 cmd_match
= alloc_printf("%s %s", prev
, CMD_ARGV
[i
]);
862 LOG_ERROR("unable to build search string");
865 retval
= CALL_COMMAND_HANDLER(command_help_show_list
, full
, cmd_match
);
871 static char *alloc_concatenate_strings(int argc
, Jim_Obj
* const *argv
)
878 all
= strdup(Jim_GetString(argv
[0], NULL
));
880 LOG_ERROR("Out of memory");
884 for (i
= 1; i
< argc
; ++i
) {
886 all
= alloc_printf("%s %s", all
, Jim_GetString(argv
[i
], NULL
));
889 LOG_ERROR("Out of memory");
897 static int exec_command(Jim_Interp
*interp
, struct command_context
*cmd_ctx
,
898 struct command
*c
, int argc
, Jim_Obj
* const *argv
)
901 return c
->jim_handler(interp
, argc
, argv
);
905 char **words
= script_command_args_alloc(argc
, argv
, &nwords
);
909 int retval
= run_command(cmd_ctx
, c
, (const char **)words
, nwords
);
910 script_command_args_free(words
, nwords
);
911 return command_retval_set(interp
, retval
);
914 static int jim_command_dispatch(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
916 /* check subcommands */
918 char *s
= alloc_printf("%s %s", Jim_GetString(argv
[0], NULL
), Jim_GetString(argv
[1], NULL
));
919 Jim_Obj
*js
= Jim_NewStringObj(interp
, s
, -1);
920 Jim_IncrRefCount(js
);
922 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, js
, JIM_NONE
);
924 int retval
= Jim_EvalObjPrefix(interp
, js
, argc
- 2, argv
+ 2);
925 Jim_DecrRefCount(interp
, js
);
928 Jim_DecrRefCount(interp
, js
);
931 script_debug(interp
, argc
, argv
);
933 struct command
*c
= jim_to_command(interp
);
934 if (!c
->jim_handler
&& !c
->handler
) {
935 Jim_EvalObjPrefix(interp
, Jim_NewStringObj(interp
, "usage", -1), 1, argv
);
939 struct command_context
*cmd_ctx
= current_command_context(interp
);
941 if (!command_can_run(cmd_ctx
, c
, Jim_GetString(argv
[0], NULL
)))
944 target_call_timer_callbacks();
947 * Black magic of overridden current target:
948 * If the command we are going to handle has a target prefix,
949 * override the current target temporarily for the time
950 * of processing the command.
951 * current_target_override is used also for event handlers
952 * therefore we prevent touching it if command has no prefix.
953 * Previous override is saved and restored back to ensure
954 * correct work when jim_command_dispatch() is re-entered.
956 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
957 if (c
->jim_override_target
)
958 cmd_ctx
->current_target_override
= c
->jim_override_target
;
960 int retval
= exec_command(interp
, cmd_ctx
, c
, argc
, argv
);
962 if (c
->jim_override_target
)
963 cmd_ctx
->current_target_override
= saved_target_override
;
968 static int jim_command_mode(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
970 struct command_context
*cmd_ctx
= current_command_context(interp
);
971 enum command_mode mode
;
974 char *full_name
= alloc_concatenate_strings(argc
- 1, argv
+ 1);
977 Jim_Obj
*s
= Jim_NewStringObj(interp
, full_name
, -1);
979 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, s
, JIM_NONE
);
980 Jim_DecrRefCount(interp
, s
);
982 if (!cmd
|| !(jimcmd_is_proc(cmd
) || jimcmd_is_oocd_command(cmd
))) {
983 Jim_SetResultString(interp
, "unknown", -1);
987 if (jimcmd_is_proc(cmd
)) {
991 struct command
*c
= jimcmd_privdata(cmd
);
996 mode
= cmd_ctx
->mode
;
998 const char *mode_str
;
1003 case COMMAND_CONFIG
:
1004 mode_str
= "config";
1010 mode_str
= "unknown";
1013 Jim_SetResultString(interp
, mode_str
, -1);
1017 int help_del_all_commands(struct command_context
*cmd_ctx
)
1019 struct help_entry
*curr
, *n
;
1021 list_for_each_entry_safe(curr
, n
, cmd_ctx
->help_list
, lh
) {
1022 list_del(&curr
->lh
);
1023 free(curr
->cmd_name
);
1031 static int help_del_command(struct command_context
*cmd_ctx
, const char *cmd_name
)
1033 struct help_entry
*curr
;
1035 list_for_each_entry(curr
, cmd_ctx
->help_list
, lh
) {
1036 if (!strcmp(cmd_name
, curr
->cmd_name
)) {
1037 list_del(&curr
->lh
);
1038 free(curr
->cmd_name
);
1049 static int help_add_command(struct command_context
*cmd_ctx
,
1050 const char *cmd_name
, const char *help_text
, const char *usage_text
)
1052 int cmp
= -1; /* add after curr */
1053 struct help_entry
*curr
;
1055 list_for_each_entry_reverse(curr
, cmd_ctx
->help_list
, lh
) {
1056 cmp
= strcmp(cmd_name
, curr
->cmd_name
);
1061 struct help_entry
*entry
;
1063 entry
= calloc(1, sizeof(*entry
));
1065 LOG_ERROR("Out of memory");
1068 entry
->cmd_name
= strdup(cmd_name
);
1069 if (!entry
->cmd_name
) {
1070 LOG_ERROR("Out of memory");
1074 list_add(&entry
->lh
, &curr
->lh
);
1080 char *text
= strdup(help_text
);
1082 LOG_ERROR("Out of memory");
1090 char *text
= strdup(usage_text
);
1092 LOG_ERROR("Out of memory");
1096 entry
->usage
= text
;
1102 COMMAND_HANDLER(handle_help_add_command
)
1105 return ERROR_COMMAND_SYNTAX_ERROR
;
1107 const char *help
= !strcmp(CMD_NAME
, "add_help_text") ? CMD_ARGV
[1] : NULL
;
1108 const char *usage
= !strcmp(CMD_NAME
, "add_usage_text") ? CMD_ARGV
[1] : NULL
;
1109 if (!help
&& !usage
) {
1110 LOG_ERROR("command name '%s' is unknown", CMD_NAME
);
1111 return ERROR_COMMAND_SYNTAX_ERROR
;
1113 const char *cmd_name
= CMD_ARGV
[0];
1114 return help_add_command(CMD_CTX
, cmd_name
, help
, usage
);
1117 /* sleep command sleeps for <n> milliseconds
1118 * this is useful in target startup scripts
1120 COMMAND_HANDLER(handle_sleep_command
)
1123 if (CMD_ARGC
== 2) {
1124 if (strcmp(CMD_ARGV
[1], "busy") == 0)
1127 return ERROR_COMMAND_SYNTAX_ERROR
;
1128 } else if (CMD_ARGC
< 1 || CMD_ARGC
> 2)
1129 return ERROR_COMMAND_SYNTAX_ERROR
;
1131 unsigned long duration
= 0;
1132 int retval
= parse_ulong(CMD_ARGV
[0], &duration
);
1133 if (retval
!= ERROR_OK
)
1137 int64_t then
= timeval_ms();
1138 while (timeval_ms() - then
< (int64_t)duration
) {
1139 target_call_timer_callbacks_now();
1144 busy_sleep(duration
);
1149 static const struct command_registration command_subcommand_handlers
[] = {
1152 .mode
= COMMAND_ANY
,
1153 .jim_handler
= jim_command_mode
,
1154 .usage
= "[command_name ...]",
1155 .help
= "Returns the command modes allowed by a command: "
1156 "'any', 'config', or 'exec'. If no command is "
1157 "specified, returns the current command mode. "
1158 "Returns 'unknown' if an unknown command is given. "
1159 "Command can be multiple tokens.",
1161 COMMAND_REGISTRATION_DONE
1164 static const struct command_registration command_builtin_handlers
[] = {
1167 .mode
= COMMAND_ANY
,
1168 .handler
= handle_find
,
1169 .help
= "find full path to file",
1174 .mode
= COMMAND_ANY
,
1175 .jim_handler
= jim_capture
,
1176 .help
= "Capture progress output and return as tcl return value. If the "
1177 "progress output was empty, return tcl return value.",
1182 .handler
= handle_echo
,
1183 .mode
= COMMAND_ANY
,
1184 .help
= "Logs a message at \"user\" priority. "
1185 "Option \"-n\" suppresses trailing newline",
1186 .usage
= "[-n] string",
1189 .name
= "add_help_text",
1190 .handler
= handle_help_add_command
,
1191 .mode
= COMMAND_ANY
,
1192 .help
= "Add new command help text; "
1193 "Command can be multiple tokens.",
1194 .usage
= "command_name helptext_string",
1197 .name
= "add_usage_text",
1198 .handler
= handle_help_add_command
,
1199 .mode
= COMMAND_ANY
,
1200 .help
= "Add new command usage text; "
1201 "command can be multiple tokens.",
1202 .usage
= "command_name usage_string",
1206 .handler
= handle_sleep_command
,
1207 .mode
= COMMAND_ANY
,
1208 .help
= "Sleep for specified number of milliseconds. "
1209 "\"busy\" will busy wait instead (avoid this).",
1210 .usage
= "milliseconds ['busy']",
1214 .handler
= handle_help_command
,
1215 .mode
= COMMAND_ANY
,
1216 .help
= "Show full command help; "
1217 "command can be multiple tokens.",
1218 .usage
= "[command_name]",
1222 .handler
= handle_help_command
,
1223 .mode
= COMMAND_ANY
,
1224 .help
= "Show basic command usage; "
1225 "command can be multiple tokens.",
1226 .usage
= "[command_name]",
1230 .mode
= COMMAND_ANY
,
1231 .help
= "core command group (introspection)",
1232 .chain
= command_subcommand_handlers
,
1235 COMMAND_REGISTRATION_DONE
1238 struct command_context
*command_init(const char *startup_tcl
, Jim_Interp
*interp
)
1240 struct command_context
*context
= calloc(1, sizeof(struct command_context
));
1242 context
->mode
= COMMAND_EXEC
;
1244 /* context can be duplicated. Put list head on separate mem-chunk to keep list consistent */
1245 context
->help_list
= malloc(sizeof(*context
->help_list
));
1246 INIT_LIST_HEAD(context
->help_list
);
1248 /* Create a jim interpreter if we were not handed one */
1250 /* Create an interpreter */
1251 interp
= Jim_CreateInterp();
1252 /* Add all the Jim core commands */
1253 Jim_RegisterCoreCommands(interp
);
1254 Jim_InitStaticExtensions(interp
);
1257 context
->interp
= interp
;
1259 register_commands(context
, NULL
, command_builtin_handlers
);
1261 Jim_SetAssocData(interp
, "context", NULL
, context
);
1262 if (Jim_Eval_Named(interp
, startup_tcl
, "embedded:startup.tcl", 1) == JIM_ERR
) {
1263 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
1264 Jim_MakeErrorMessage(interp
);
1265 LOG_USER_N("%s", Jim_GetString(Jim_GetResult(interp
), NULL
));
1268 Jim_DeleteAssocData(interp
, "context");
1273 void command_exit(struct command_context
*context
)
1278 Jim_FreeInterp(context
->interp
);
1279 free(context
->help_list
);
1280 command_done(context
);
1283 int command_context_mode(struct command_context
*cmd_ctx
, enum command_mode mode
)
1286 return ERROR_COMMAND_SYNTAX_ERROR
;
1288 cmd_ctx
->mode
= mode
;
1292 void process_jim_events(struct command_context
*cmd_ctx
)
1294 static int recursion
;
1299 Jim_ProcessEvents(cmd_ctx
->interp
, JIM_ALL_EVENTS
| JIM_DONT_WAIT
);
1303 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
1304 int parse ## name(const char *str, type * ul) \
1307 LOG_ERROR("Invalid command argument"); \
1308 return ERROR_COMMAND_ARGUMENT_INVALID; \
1312 *ul = func(str, &end, 0); \
1314 LOG_ERROR("Invalid command argument"); \
1315 return ERROR_COMMAND_ARGUMENT_INVALID; \
1317 if ((max == *ul) && (errno == ERANGE)) { \
1318 LOG_ERROR("Argument overflow"); \
1319 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1321 if (min && (min == *ul) && (errno == ERANGE)) { \
1322 LOG_ERROR("Argument underflow"); \
1323 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1327 DEFINE_PARSE_NUM_TYPE(_ulong
, unsigned long, strtoul
, 0, ULONG_MAX
)
1328 DEFINE_PARSE_NUM_TYPE(_ullong
, unsigned long long, strtoull
, 0, ULLONG_MAX
)
1329 DEFINE_PARSE_NUM_TYPE(_long
, long, strtol
, LONG_MIN
, LONG_MAX
)
1330 DEFINE_PARSE_NUM_TYPE(_llong
, long long, strtoll
, LLONG_MIN
, LLONG_MAX
)
1332 #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
1333 int parse ## name(const char *str, type * ul) \
1336 int retval = parse ## funcname(str, &n); \
1337 if (retval != ERROR_OK) \
1340 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1342 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1347 #define DEFINE_PARSE_ULONGLONG(name, type, min, max) \
1348 DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
1349 DEFINE_PARSE_ULONGLONG(_uint
, unsigned, 0, UINT_MAX
)
1350 DEFINE_PARSE_ULONGLONG(_u64
, uint64_t, 0, UINT64_MAX
)
1351 DEFINE_PARSE_ULONGLONG(_u32
, uint32_t, 0, UINT32_MAX
)
1352 DEFINE_PARSE_ULONGLONG(_u16
, uint16_t, 0, UINT16_MAX
)
1353 DEFINE_PARSE_ULONGLONG(_u8
, uint8_t, 0, UINT8_MAX
)
1355 DEFINE_PARSE_ULONGLONG(_target_addr
, target_addr_t
, 0, TARGET_ADDR_MAX
)
1357 #define DEFINE_PARSE_LONGLONG(name, type, min, max) \
1358 DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
1359 DEFINE_PARSE_LONGLONG(_int
, int, n
< INT_MIN
, INT_MAX
)
1360 DEFINE_PARSE_LONGLONG(_s64
, int64_t, n
< INT64_MIN
, INT64_MAX
)
1361 DEFINE_PARSE_LONGLONG(_s32
, int32_t, n
< INT32_MIN
, INT32_MAX
)
1362 DEFINE_PARSE_LONGLONG(_s16
, int16_t, n
< INT16_MIN
, INT16_MAX
)
1363 DEFINE_PARSE_LONGLONG(_s8
, int8_t, n
< INT8_MIN
, INT8_MAX
)
1365 static int command_parse_bool(const char *in
, bool *out
,
1366 const char *on
, const char *off
)
1368 if (strcasecmp(in
, on
) == 0)
1370 else if (strcasecmp(in
, off
) == 0)
1373 return ERROR_COMMAND_SYNTAX_ERROR
;
1377 int command_parse_bool_arg(const char *in
, bool *out
)
1379 if (command_parse_bool(in
, out
, "on", "off") == ERROR_OK
)
1381 if (command_parse_bool(in
, out
, "enable", "disable") == ERROR_OK
)
1383 if (command_parse_bool(in
, out
, "true", "false") == ERROR_OK
)
1385 if (command_parse_bool(in
, out
, "yes", "no") == ERROR_OK
)
1387 if (command_parse_bool(in
, out
, "1", "0") == ERROR_OK
)
1389 return ERROR_COMMAND_SYNTAX_ERROR
;
1392 COMMAND_HELPER(handle_command_parse_bool
, bool *out
, const char *label
)
1396 const char *in
= CMD_ARGV
[0];
1397 if (command_parse_bool_arg(in
, out
) != ERROR_OK
) {
1398 LOG_ERROR("%s: argument '%s' is not valid", CMD_NAME
, in
);
1399 return ERROR_COMMAND_SYNTAX_ERROR
;
1404 LOG_INFO("%s is %s", label
, *out
? "enabled" : "disabled");
1407 return ERROR_COMMAND_SYNTAX_ERROR
;
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)