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 /* see Embedded-HOWTO.txt in Jim Tcl project hosted on BerliOS*/
24 /* @todo the inclusion of target.h here is a layering violation */
25 #include <jtag/jtag.h>
26 #include <target/target.h>
28 #include "configuration.h"
30 #include "time_support.h"
31 #include "jim-eventloop.h"
33 /* nice short description of source file */
34 #define __THIS__FILE__ "command.c"
36 struct log_capture_state
{
41 static int unregister_command(struct command_context
*context
,
42 const char *cmd_prefix
, const char *name
);
43 static int jim_command_dispatch(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
);
44 static int help_add_command(struct command_context
*cmd_ctx
,
45 const char *cmd_name
, const char *help_text
, const char *usage_text
);
46 static int help_del_command(struct command_context
*cmd_ctx
, const char *cmd_name
);
48 /* set of functions to wrap jimtcl internal data */
49 static inline bool jimcmd_is_proc(Jim_Cmd
*cmd
)
54 bool jimcmd_is_oocd_command(Jim_Cmd
*cmd
)
56 return !cmd
->isproc
&& cmd
->u
.native
.cmdProc
== jim_command_dispatch
;
59 void *jimcmd_privdata(Jim_Cmd
*cmd
)
61 return cmd
->isproc
? NULL
: cmd
->u
.native
.privData
;
64 static void tcl_output(void *privData
, const char *file
, unsigned line
,
65 const char *function
, const char *string
)
67 struct log_capture_state
*state
= privData
;
68 Jim_AppendString(state
->interp
, state
->output
, string
, strlen(string
));
71 static struct log_capture_state
*command_log_capture_start(Jim_Interp
*interp
)
73 /* capture log output and return it. A garbage collect can
74 * happen, so we need a reference count to this object */
75 Jim_Obj
*jim_output
= Jim_NewStringObj(interp
, "", 0);
79 Jim_IncrRefCount(jim_output
);
81 struct log_capture_state
*state
= malloc(sizeof(*state
));
83 LOG_ERROR("Out of memory");
84 Jim_DecrRefCount(interp
, jim_output
);
88 state
->interp
= interp
;
89 state
->output
= jim_output
;
91 log_add_callback(tcl_output
, state
);
96 /* Classic openocd commands provide progress output which we
97 * will capture and return as a Tcl return value.
99 * However, if a non-openocd command has been invoked, then it
100 * makes sense to return the tcl return value from that command.
102 * The tcl return value is empty for openocd commands that provide
105 * Therefore we set the tcl return value only if we actually
108 static void command_log_capture_finish(struct log_capture_state
*state
)
113 log_remove_callback(tcl_output
, state
);
116 Jim_GetString(state
->output
, &length
);
119 Jim_SetResult(state
->interp
, state
->output
);
121 /* No output captured, use tcl return value (which could
124 Jim_DecrRefCount(state
->interp
, state
->output
);
129 static int command_retval_set(Jim_Interp
*interp
, int retval
)
131 int *return_retval
= Jim_GetAssocData(interp
, "retval");
133 *return_retval
= retval
;
135 return (retval
== ERROR_OK
) ? JIM_OK
: retval
;
138 extern struct command_context
*global_cmd_ctx
;
140 /* dump a single line to the log for the command.
141 * Do nothing in case we are not at debug level 3 */
142 static void script_debug(Jim_Interp
*interp
, unsigned int argc
, Jim_Obj
* const *argv
)
144 if (debug_level
< LOG_LVL_DEBUG
)
147 char *dbg
= alloc_printf("command -");
148 for (unsigned i
= 0; i
< argc
; i
++) {
150 const char *w
= Jim_GetString(argv
[i
], &len
);
151 char *t
= alloc_printf("%s %s", dbg
, w
);
155 LOG_DEBUG("%s", dbg
);
159 static void script_command_args_free(char **words
, unsigned nwords
)
161 for (unsigned i
= 0; i
< nwords
; i
++)
166 static char **script_command_args_alloc(
167 unsigned argc
, Jim_Obj
* const *argv
, unsigned *nwords
)
169 char **words
= malloc(argc
* sizeof(char *));
174 for (i
= 0; i
< argc
; i
++) {
176 const char *w
= Jim_GetString(argv
[i
], &len
);
177 words
[i
] = strdup(w
);
179 script_command_args_free(words
, i
);
187 struct command_context
*current_command_context(Jim_Interp
*interp
)
189 /* grab the command context from the associated data */
190 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
192 /* Tcl can invoke commands directly instead of via command_run_line(). This would
193 * happen when the Jim Tcl interpreter is provided by eCos or if we are running
194 * commands in a startup script.
196 * A telnet or gdb server would provide a non-default command context to
197 * handle piping of error output, have a separate current target, etc.
199 cmd_ctx
= global_cmd_ctx
;
205 * Find a openocd command from fullname.
206 * @returns Returns the named command if it is registred in interp.
207 * Returns NULL otherwise.
209 static struct command
*command_find_from_name(Jim_Interp
*interp
, const char *name
)
214 Jim_Obj
*jim_name
= Jim_NewStringObj(interp
, name
, -1);
215 Jim_IncrRefCount(jim_name
);
216 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, jim_name
, JIM_NONE
);
217 Jim_DecrRefCount(interp
, jim_name
);
218 if (!cmd
|| jimcmd_is_proc(cmd
) || !jimcmd_is_oocd_command(cmd
))
221 return jimcmd_privdata(cmd
);
224 static struct command
*command_new(struct command_context
*cmd_ctx
,
225 const char *full_name
, const struct command_registration
*cr
)
230 * If it is a non-jim command with no .usage specified,
233 * strlen(.usage) == 0 means that the command takes no
236 if (!cr
->jim_handler
&& !cr
->usage
)
237 LOG_ERROR("BUG: command '%s' does not have the "
238 "'.usage' field filled out",
241 struct command
*c
= calloc(1, sizeof(struct command
));
245 c
->name
= strdup(cr
->name
);
251 c
->handler
= cr
->handler
;
252 c
->jim_handler
= cr
->jim_handler
;
255 if (cr
->help
|| cr
->usage
)
256 help_add_command(cmd_ctx
, full_name
, cr
->help
, cr
->usage
);
261 static void command_free(struct Jim_Interp
*interp
, void *priv
)
263 struct command
*c
= priv
;
269 static struct command
*register_command(struct command_context
*context
,
270 const char *cmd_prefix
, const struct command_registration
*cr
)
274 if (!context
|| !cr
->name
)
278 full_name
= alloc_printf("%s %s", cmd_prefix
, cr
->name
);
280 full_name
= strdup(cr
->name
);
284 struct command
*c
= command_find_from_name(context
->interp
, full_name
);
286 /* TODO: originally we treated attempting to register a cmd twice as an error
287 * Sometimes we need this behaviour, such as with flash banks.
288 * http://www.mail-archive.com/openocd-development@lists.berlios.de/msg11152.html */
289 LOG_DEBUG("command '%s' is already registered", full_name
);
294 c
= command_new(context
, full_name
, cr
);
300 if (false) /* too noisy with debug_level 3 */
301 LOG_DEBUG("registering '%s'...", full_name
);
302 int retval
= Jim_CreateCommand(context
->interp
, full_name
,
303 jim_command_dispatch
, c
, command_free
);
304 if (retval
!= JIM_OK
) {
305 command_run_linef(context
, "del_help_text {%s}", full_name
);
306 command_run_linef(context
, "del_usage_text {%s}", full_name
);
316 int __register_commands(struct command_context
*cmd_ctx
, const char *cmd_prefix
,
317 const struct command_registration
*cmds
, void *data
,
318 struct target
*override_target
)
320 int retval
= ERROR_OK
;
322 for (i
= 0; cmds
[i
].name
|| cmds
[i
].chain
; i
++) {
323 const struct command_registration
*cr
= cmds
+ i
;
325 struct command
*c
= NULL
;
327 c
= register_command(cmd_ctx
, cmd_prefix
, cr
);
332 c
->jim_handler_data
= data
;
333 c
->jim_override_target
= override_target
;
338 char *new_prefix
= alloc_printf("%s %s", cmd_prefix
, cr
->name
);
343 retval
= __register_commands(cmd_ctx
, new_prefix
, cr
->chain
, data
, override_target
);
346 retval
= __register_commands(cmd_ctx
, cr
->name
, cr
->chain
, data
, override_target
);
349 retval
= __register_commands(cmd_ctx
, cmd_prefix
, cr
->chain
, data
, override_target
);
351 if (retval
!= ERROR_OK
)
355 if (retval
!= ERROR_OK
) {
356 for (unsigned j
= 0; j
< i
; j
++)
357 unregister_command(cmd_ctx
, cmd_prefix
, cmds
[j
].name
);
362 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 2, 3)))
363 int unregister_commands_match(struct command_context
*cmd_ctx
, const char *format
, ...)
365 Jim_Interp
*interp
= cmd_ctx
->interp
;
368 va_start(ap
, format
);
369 char *query
= alloc_vprintf(format
, ap
);
374 char *query_cmd
= alloc_printf("info commands {%s}", query
);
379 int retval
= Jim_EvalSource(interp
, __THIS__FILE__
, __LINE__
, query_cmd
);
381 if (retval
!= JIM_OK
)
384 Jim_Obj
*list
= Jim_GetResult(interp
);
385 Jim_IncrRefCount(list
);
387 int len
= Jim_ListLength(interp
, list
);
388 for (int i
= 0; i
< len
; i
++) {
389 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, list
, i
);
390 Jim_IncrRefCount(elem
);
392 const char *name
= Jim_GetString(elem
, NULL
);
393 struct command
*c
= command_find_from_name(interp
, name
);
395 /* not openocd command */
396 Jim_DecrRefCount(interp
, elem
);
399 if (false) /* too noisy with debug_level 3 */
400 LOG_DEBUG("delete command \"%s\"", name
);
401 #if JIM_VERSION >= 80
402 Jim_DeleteCommand(interp
, elem
);
404 Jim_DeleteCommand(interp
, name
);
407 help_del_command(cmd_ctx
, name
);
409 Jim_DecrRefCount(interp
, elem
);
412 Jim_DecrRefCount(interp
, list
);
416 int unregister_all_commands(struct command_context
*context
,
417 const char *cmd_prefix
)
422 if (!cmd_prefix
|| !*cmd_prefix
)
423 return unregister_commands_match(context
, "*");
425 int retval
= unregister_commands_match(context
, "%s *", cmd_prefix
);
426 if (retval
!= ERROR_OK
)
429 return unregister_commands_match(context
, "%s", cmd_prefix
);
432 static int unregister_command(struct command_context
*context
,
433 const char *cmd_prefix
, const char *name
)
435 if (!context
|| !name
)
436 return ERROR_COMMAND_SYNTAX_ERROR
;
438 if (!cmd_prefix
|| !*cmd_prefix
)
439 return unregister_commands_match(context
, "%s", name
);
441 return unregister_commands_match(context
, "%s %s", cmd_prefix
, name
);
444 void command_output_text(struct command_context
*context
, const char *data
)
446 if (context
&& context
->output_handler
&& data
)
447 context
->output_handler(context
, data
);
450 void command_print_sameline(struct command_invocation
*cmd
, const char *format
, ...)
455 va_start(ap
, format
);
457 string
= alloc_vprintf(format
, ap
);
459 /* we want this collected in the log + we also want to pick it up as a tcl return
462 * The latter bit isn't precisely neat, but will do for now.
464 Jim_AppendString(cmd
->ctx
->interp
, cmd
->output
, string
, -1);
465 /* We already printed it above
466 * command_output_text(context, string); */
473 void command_print(struct command_invocation
*cmd
, const char *format
, ...)
478 va_start(ap
, format
);
480 string
= alloc_vprintf(format
, ap
);
482 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one
484 /* we want this collected in the log + we also want to pick it up as a tcl return
487 * The latter bit isn't precisely neat, but will do for now.
489 Jim_AppendString(cmd
->ctx
->interp
, cmd
->output
, string
, -1);
490 /* We already printed it above
491 * command_output_text(context, string); */
498 static bool command_can_run(struct command_context
*cmd_ctx
, struct command
*c
, const char *full_name
)
500 if (c
->mode
== COMMAND_ANY
|| c
->mode
== cmd_ctx
->mode
)
503 /* Many commands may be run only before/after 'init' */
512 /* handle the impossible with humor; it guarantees a bug report! */
514 when
= "if Cthulhu is summoned by";
517 LOG_ERROR("The '%s' command must be used %s 'init'.",
518 full_name
? full_name
: c
->name
, when
);
522 static int run_command(struct command_context
*context
,
523 struct command
*c
, const char **words
, unsigned num_words
)
525 struct command_invocation cmd
= {
529 .argc
= num_words
- 1,
533 cmd
.output
= Jim_NewEmptyStringObj(context
->interp
);
534 Jim_IncrRefCount(cmd
.output
);
536 int retval
= c
->handler(&cmd
);
537 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
) {
538 /* Print help for command */
539 command_run_linef(context
, "usage %s", words
[0]);
540 } else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
) {
541 /* just fall through for a shutdown request */
543 if (retval
!= ERROR_OK
)
544 LOG_DEBUG("Command '%s' failed with error code %d",
547 * Use the command output as the Tcl result.
548 * Drop last '\n' to allow command output concatenation
549 * while keep using command_print() everywhere.
551 const char *output_txt
= Jim_String(cmd
.output
);
552 int len
= strlen(output_txt
);
553 if (len
&& output_txt
[len
- 1] == '\n')
555 Jim_SetResultString(context
->interp
, output_txt
, len
);
557 Jim_DecrRefCount(context
->interp
, cmd
.output
);
562 int command_run_line(struct command_context
*context
, char *line
)
564 /* all the parent commands have been registered with the interpreter
565 * so, can just evaluate the line as a script and check for
568 /* run the line thru a script engine */
569 int retval
= ERROR_FAIL
;
571 /* Beware! This code needs to be reentrant. It is also possible
572 * for OpenOCD commands to be invoked directly from Tcl. This would
573 * happen when the Jim Tcl interpreter is provided by eCos for
576 struct target
*saved_target_override
= context
->current_target_override
;
577 context
->current_target_override
= NULL
;
579 Jim_Interp
*interp
= context
->interp
;
580 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
581 Jim_DeleteAssocData(interp
, "context");
582 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
583 if (retcode
== JIM_OK
) {
584 /* associated the return value */
585 Jim_DeleteAssocData(interp
, "retval");
586 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
587 if (retcode
== JIM_OK
) {
588 retcode
= Jim_Eval_Named(interp
, line
, 0, 0);
590 Jim_DeleteAssocData(interp
, "retval");
592 Jim_DeleteAssocData(interp
, "context");
593 int inner_retcode
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
594 if (retcode
== JIM_OK
)
595 retcode
= inner_retcode
;
597 context
->current_target_override
= saved_target_override
;
598 if (retcode
== JIM_OK
) {
602 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
604 command_output_text(context
, result
);
605 command_output_text(context
, "\n");
608 } else if (retcode
== JIM_EXIT
) {
610 * exit(Jim_GetExitCode(interp)); */
611 } else if (retcode
== ERROR_COMMAND_CLOSE_CONNECTION
) {
614 Jim_MakeErrorMessage(interp
);
615 /* error is broadcast */
616 LOG_USER("%s", Jim_GetString(Jim_GetResult(interp
), NULL
));
618 if (retval
== ERROR_OK
) {
619 /* It wasn't a low level OpenOCD command that failed */
628 int command_run_linef(struct command_context
*context
, const char *format
, ...)
630 int retval
= ERROR_FAIL
;
633 va_start(ap
, format
);
634 string
= alloc_vprintf(format
, ap
);
636 retval
= command_run_line(context
, string
);
643 void command_set_output_handler(struct command_context
*context
,
644 command_output_handler_t output_handler
, void *priv
)
646 context
->output_handler
= output_handler
;
647 context
->output_handler_priv
= priv
;
650 struct command_context
*copy_command_context(struct command_context
*context
)
652 struct command_context
*copy_context
= malloc(sizeof(struct command_context
));
654 *copy_context
= *context
;
659 void command_done(struct command_context
*cmd_ctx
)
667 /* find full path to file */
668 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
672 const char *file
= Jim_GetString(argv
[1], NULL
);
673 char *full_path
= find_file(file
);
676 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
679 Jim_SetResult(interp
, result
);
683 COMMAND_HANDLER(handle_echo
)
685 if (CMD_ARGC
== 2 && !strcmp(CMD_ARGV
[0], "-n")) {
686 LOG_USER_N("%s", CMD_ARGV
[1]);
693 LOG_USER("%s", CMD_ARGV
[0]);
697 /* Capture progress output and return as tcl return value. If the
698 * progress output was empty, return tcl return value.
700 static int jim_capture(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
705 struct log_capture_state
*state
= command_log_capture_start(interp
);
707 /* disable polling during capture. This avoids capturing output
710 * This is necessary in order to avoid accidentally getting a non-empty
711 * string for tcl fn's.
713 bool save_poll_mask
= jtag_poll_mask();
715 const char *str
= Jim_GetString(argv
[1], NULL
);
716 int retcode
= Jim_Eval_Named(interp
, str
, __THIS__FILE__
, __LINE__
);
718 jtag_poll_unmask(save_poll_mask
);
720 command_log_capture_finish(state
);
732 static COMMAND_HELPER(command_help_show
, struct help_entry
*c
,
733 bool show_help
, const char *cmd_match
);
735 static COMMAND_HELPER(command_help_show_list
, bool show_help
, const char *cmd_match
)
737 struct help_entry
*entry
;
739 list_for_each_entry(entry
, CMD_CTX
->help_list
, lh
)
740 CALL_COMMAND_HANDLER(command_help_show
, entry
, show_help
, cmd_match
);
744 #define HELP_LINE_WIDTH(_n) (int)(76 - (2 * _n))
746 static void command_help_show_indent(unsigned n
)
748 for (unsigned i
= 0; i
< n
; i
++)
751 static void command_help_show_wrap(const char *str
, unsigned n
, unsigned n2
)
753 const char *cp
= str
, *last
= str
;
755 const char *next
= last
;
760 } while (*next
!= ' ' && *next
!= '\t' && *next
!= '\0');
761 } while ((next
- last
< HELP_LINE_WIDTH(n
)) && *next
!= '\0');
762 if (next
- last
< HELP_LINE_WIDTH(n
))
764 command_help_show_indent(n
);
765 LOG_USER("%.*s", (int)(cp
- last
), last
);
771 static COMMAND_HELPER(command_help_show
, struct help_entry
*c
,
772 bool show_help
, const char *cmd_match
)
775 for (const char *s
= strchr(c
->cmd_name
, ' '); s
; s
= strchr(s
+ 1, ' '))
778 /* If the match string occurs anywhere, we print out
779 * stuff for this command. */
780 bool is_match
= strstr(c
->cmd_name
, cmd_match
) ||
781 (c
->usage
&& strstr(c
->usage
, cmd_match
)) ||
782 (c
->help
&& strstr(c
->help
, cmd_match
));
785 if (c
->usage
&& strlen(c
->usage
) > 0) {
786 char *msg
= alloc_printf("%s %s", c
->cmd_name
, c
->usage
);
787 command_help_show_wrap(msg
, n
, n
+ 5);
790 command_help_show_wrap(c
->cmd_name
, n
, n
+ 5);
794 if (is_match
&& show_help
) {
797 /* TODO: factorize jim_command_mode() to avoid running jim command here */
798 char *request
= alloc_printf("command mode %s", c
->cmd_name
);
800 LOG_ERROR("Out of memory");
803 int retval
= Jim_Eval(CMD_CTX
->interp
, request
);
805 enum command_mode mode
= COMMAND_UNKNOWN
;
806 if (retval
!= JIM_ERR
) {
807 const char *result
= Jim_GetString(Jim_GetResult(CMD_CTX
->interp
), NULL
);
808 if (!strcmp(result
, "any"))
810 else if (!strcmp(result
, "config"))
811 mode
= COMMAND_CONFIG
;
812 else if (!strcmp(result
, "exec"))
816 /* Normal commands are runtime-only; highlight exceptions */
817 if (mode
!= COMMAND_EXEC
) {
818 const char *stage_msg
= "";
822 stage_msg
= " (configuration command)";
825 stage_msg
= " (command valid any time)";
828 stage_msg
= " (?mode error?)";
831 msg
= alloc_printf("%s%s", c
->help
? c
->help
: "", stage_msg
);
833 msg
= alloc_printf("%s", c
->help
? c
->help
: "");
836 command_help_show_wrap(msg
, n
+ 3, n
+ 3);
845 COMMAND_HANDLER(handle_help_command
)
847 bool full
= strcmp(CMD_NAME
, "help") == 0;
852 cmd_match
= strdup("");
855 cmd_match
= strdup(CMD_ARGV
[0]);
857 for (unsigned int i
= 1; i
< CMD_ARGC
&& cmd_match
; ++i
) {
858 char *prev
= cmd_match
;
859 cmd_match
= alloc_printf("%s %s", prev
, CMD_ARGV
[i
]);
865 LOG_ERROR("unable to build search string");
868 retval
= CALL_COMMAND_HANDLER(command_help_show_list
, full
, cmd_match
);
874 static char *alloc_concatenate_strings(int argc
, Jim_Obj
* const *argv
)
881 all
= strdup(Jim_GetString(argv
[0], NULL
));
883 LOG_ERROR("Out of memory");
887 for (i
= 1; i
< argc
; ++i
) {
889 all
= alloc_printf("%s %s", all
, Jim_GetString(argv
[i
], NULL
));
892 LOG_ERROR("Out of memory");
900 static int exec_command(Jim_Interp
*interp
, struct command_context
*cmd_ctx
,
901 struct command
*c
, int argc
, Jim_Obj
* const *argv
)
904 return c
->jim_handler(interp
, argc
, argv
);
908 char **words
= script_command_args_alloc(argc
, argv
, &nwords
);
912 int retval
= run_command(cmd_ctx
, c
, (const char **)words
, nwords
);
913 script_command_args_free(words
, nwords
);
914 return command_retval_set(interp
, retval
);
917 static int jim_command_dispatch(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
919 /* check subcommands */
921 char *s
= alloc_printf("%s %s", Jim_GetString(argv
[0], NULL
), Jim_GetString(argv
[1], NULL
));
922 Jim_Obj
*js
= Jim_NewStringObj(interp
, s
, -1);
923 Jim_IncrRefCount(js
);
925 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, js
, JIM_NONE
);
927 int retval
= Jim_EvalObjPrefix(interp
, js
, argc
- 2, argv
+ 2);
928 Jim_DecrRefCount(interp
, js
);
931 Jim_DecrRefCount(interp
, js
);
934 script_debug(interp
, argc
, argv
);
936 struct command
*c
= jim_to_command(interp
);
937 if (!c
->jim_handler
&& !c
->handler
) {
938 Jim_EvalObjPrefix(interp
, Jim_NewStringObj(interp
, "usage", -1), 1, argv
);
942 struct command_context
*cmd_ctx
= current_command_context(interp
);
944 if (!command_can_run(cmd_ctx
, c
, Jim_GetString(argv
[0], NULL
)))
947 target_call_timer_callbacks();
950 * Black magic of overridden current target:
951 * If the command we are going to handle has a target prefix,
952 * override the current target temporarily for the time
953 * of processing the command.
954 * current_target_override is used also for event handlers
955 * therefore we prevent touching it if command has no prefix.
956 * Previous override is saved and restored back to ensure
957 * correct work when jim_command_dispatch() is re-entered.
959 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
960 if (c
->jim_override_target
)
961 cmd_ctx
->current_target_override
= c
->jim_override_target
;
963 int retval
= exec_command(interp
, cmd_ctx
, c
, argc
, argv
);
965 if (c
->jim_override_target
)
966 cmd_ctx
->current_target_override
= saved_target_override
;
971 static int jim_command_mode(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
973 struct command_context
*cmd_ctx
= current_command_context(interp
);
974 enum command_mode mode
;
977 char *full_name
= alloc_concatenate_strings(argc
- 1, argv
+ 1);
980 Jim_Obj
*s
= Jim_NewStringObj(interp
, full_name
, -1);
982 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, s
, JIM_NONE
);
983 Jim_DecrRefCount(interp
, s
);
985 if (!cmd
|| !(jimcmd_is_proc(cmd
) || jimcmd_is_oocd_command(cmd
))) {
986 Jim_SetResultString(interp
, "unknown", -1);
990 if (jimcmd_is_proc(cmd
)) {
994 struct command
*c
= jimcmd_privdata(cmd
);
999 mode
= cmd_ctx
->mode
;
1001 const char *mode_str
;
1006 case COMMAND_CONFIG
:
1007 mode_str
= "config";
1013 mode_str
= "unknown";
1016 Jim_SetResultString(interp
, mode_str
, -1);
1020 int help_del_all_commands(struct command_context
*cmd_ctx
)
1022 struct help_entry
*curr
, *n
;
1024 list_for_each_entry_safe(curr
, n
, cmd_ctx
->help_list
, lh
) {
1025 list_del(&curr
->lh
);
1026 free(curr
->cmd_name
);
1034 static int help_del_command(struct command_context
*cmd_ctx
, const char *cmd_name
)
1036 struct help_entry
*curr
;
1038 list_for_each_entry(curr
, cmd_ctx
->help_list
, lh
) {
1039 if (!strcmp(cmd_name
, curr
->cmd_name
)) {
1040 list_del(&curr
->lh
);
1041 free(curr
->cmd_name
);
1052 static int help_add_command(struct command_context
*cmd_ctx
,
1053 const char *cmd_name
, const char *help_text
, const char *usage_text
)
1055 int cmp
= -1; /* add after curr */
1056 struct help_entry
*curr
;
1058 list_for_each_entry_reverse(curr
, cmd_ctx
->help_list
, lh
) {
1059 cmp
= strcmp(cmd_name
, curr
->cmd_name
);
1064 struct help_entry
*entry
;
1066 entry
= calloc(1, sizeof(*entry
));
1068 LOG_ERROR("Out of memory");
1071 entry
->cmd_name
= strdup(cmd_name
);
1072 if (!entry
->cmd_name
) {
1073 LOG_ERROR("Out of memory");
1077 list_add(&entry
->lh
, &curr
->lh
);
1083 char *text
= strdup(help_text
);
1085 LOG_ERROR("Out of memory");
1093 char *text
= strdup(usage_text
);
1095 LOG_ERROR("Out of memory");
1099 entry
->usage
= text
;
1105 COMMAND_HANDLER(handle_help_add_command
)
1108 return ERROR_COMMAND_SYNTAX_ERROR
;
1110 const char *help
= !strcmp(CMD_NAME
, "add_help_text") ? CMD_ARGV
[1] : NULL
;
1111 const char *usage
= !strcmp(CMD_NAME
, "add_usage_text") ? CMD_ARGV
[1] : NULL
;
1112 if (!help
&& !usage
) {
1113 LOG_ERROR("command name '%s' is unknown", CMD_NAME
);
1114 return ERROR_COMMAND_SYNTAX_ERROR
;
1116 const char *cmd_name
= CMD_ARGV
[0];
1117 return help_add_command(CMD_CTX
, cmd_name
, help
, usage
);
1120 /* sleep command sleeps for <n> milliseconds
1121 * this is useful in target startup scripts
1123 COMMAND_HANDLER(handle_sleep_command
)
1126 if (CMD_ARGC
== 2) {
1127 if (strcmp(CMD_ARGV
[1], "busy") == 0)
1130 return ERROR_COMMAND_SYNTAX_ERROR
;
1131 } else if (CMD_ARGC
< 1 || CMD_ARGC
> 2)
1132 return ERROR_COMMAND_SYNTAX_ERROR
;
1134 unsigned long duration
= 0;
1135 int retval
= parse_ulong(CMD_ARGV
[0], &duration
);
1136 if (retval
!= ERROR_OK
)
1140 int64_t then
= timeval_ms();
1141 while (timeval_ms() - then
< (int64_t)duration
) {
1142 target_call_timer_callbacks_now();
1147 busy_sleep(duration
);
1152 static const struct command_registration command_subcommand_handlers
[] = {
1155 .mode
= COMMAND_ANY
,
1156 .jim_handler
= jim_command_mode
,
1157 .usage
= "[command_name ...]",
1158 .help
= "Returns the command modes allowed by a command: "
1159 "'any', 'config', or 'exec'. If no command is "
1160 "specified, returns the current command mode. "
1161 "Returns 'unknown' if an unknown command is given. "
1162 "Command can be multiple tokens.",
1164 COMMAND_REGISTRATION_DONE
1167 static const struct command_registration command_builtin_handlers
[] = {
1170 .mode
= COMMAND_ANY
,
1171 .jim_handler
= jim_find
,
1172 .help
= "find full path to file",
1177 .mode
= COMMAND_ANY
,
1178 .jim_handler
= jim_capture
,
1179 .help
= "Capture progress output and return as tcl return value. If the "
1180 "progress output was empty, return tcl return value.",
1185 .handler
= handle_echo
,
1186 .mode
= COMMAND_ANY
,
1187 .help
= "Logs a message at \"user\" priority. "
1188 "Option \"-n\" suppresses trailing newline",
1189 .usage
= "[-n] string",
1192 .name
= "add_help_text",
1193 .handler
= handle_help_add_command
,
1194 .mode
= COMMAND_ANY
,
1195 .help
= "Add new command help text; "
1196 "Command can be multiple tokens.",
1197 .usage
= "command_name helptext_string",
1200 .name
= "add_usage_text",
1201 .handler
= handle_help_add_command
,
1202 .mode
= COMMAND_ANY
,
1203 .help
= "Add new command usage text; "
1204 "command can be multiple tokens.",
1205 .usage
= "command_name usage_string",
1209 .handler
= handle_sleep_command
,
1210 .mode
= COMMAND_ANY
,
1211 .help
= "Sleep for specified number of milliseconds. "
1212 "\"busy\" will busy wait instead (avoid this).",
1213 .usage
= "milliseconds ['busy']",
1217 .handler
= handle_help_command
,
1218 .mode
= COMMAND_ANY
,
1219 .help
= "Show full command help; "
1220 "command can be multiple tokens.",
1221 .usage
= "[command_name]",
1225 .handler
= handle_help_command
,
1226 .mode
= COMMAND_ANY
,
1227 .help
= "Show basic command usage; "
1228 "command can be multiple tokens.",
1229 .usage
= "[command_name]",
1233 .mode
= COMMAND_ANY
,
1234 .help
= "core command group (introspection)",
1235 .chain
= command_subcommand_handlers
,
1238 COMMAND_REGISTRATION_DONE
1241 struct command_context
*command_init(const char *startup_tcl
, Jim_Interp
*interp
)
1243 struct command_context
*context
= calloc(1, sizeof(struct command_context
));
1245 context
->mode
= COMMAND_EXEC
;
1247 /* context can be duplicated. Put list head on separate mem-chunk to keep list consistent */
1248 context
->help_list
= malloc(sizeof(*context
->help_list
));
1249 INIT_LIST_HEAD(context
->help_list
);
1251 /* Create a jim interpreter if we were not handed one */
1253 /* Create an interpreter */
1254 interp
= Jim_CreateInterp();
1255 /* Add all the Jim core commands */
1256 Jim_RegisterCoreCommands(interp
);
1257 Jim_InitStaticExtensions(interp
);
1260 context
->interp
= interp
;
1262 register_commands(context
, NULL
, command_builtin_handlers
);
1264 Jim_SetAssocData(interp
, "context", NULL
, context
);
1265 if (Jim_Eval_Named(interp
, startup_tcl
, "embedded:startup.tcl", 1) == JIM_ERR
) {
1266 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
1267 Jim_MakeErrorMessage(interp
);
1268 LOG_USER_N("%s", Jim_GetString(Jim_GetResult(interp
), NULL
));
1271 Jim_DeleteAssocData(interp
, "context");
1276 void command_exit(struct command_context
*context
)
1281 Jim_FreeInterp(context
->interp
);
1282 free(context
->help_list
);
1283 command_done(context
);
1286 int command_context_mode(struct command_context
*cmd_ctx
, enum command_mode mode
)
1289 return ERROR_COMMAND_SYNTAX_ERROR
;
1291 cmd_ctx
->mode
= mode
;
1295 void process_jim_events(struct command_context
*cmd_ctx
)
1297 static int recursion
;
1302 Jim_ProcessEvents(cmd_ctx
->interp
, JIM_ALL_EVENTS
| JIM_DONT_WAIT
);
1306 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
1307 int parse ## name(const char *str, type * ul) \
1310 LOG_ERROR("Invalid command argument"); \
1311 return ERROR_COMMAND_ARGUMENT_INVALID; \
1315 *ul = func(str, &end, 0); \
1317 LOG_ERROR("Invalid command argument"); \
1318 return ERROR_COMMAND_ARGUMENT_INVALID; \
1320 if ((max == *ul) && (errno == ERANGE)) { \
1321 LOG_ERROR("Argument overflow"); \
1322 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1324 if (min && (min == *ul) && (errno == ERANGE)) { \
1325 LOG_ERROR("Argument underflow"); \
1326 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1330 DEFINE_PARSE_NUM_TYPE(_ulong
, unsigned long, strtoul
, 0, ULONG_MAX
)
1331 DEFINE_PARSE_NUM_TYPE(_ullong
, unsigned long long, strtoull
, 0, ULLONG_MAX
)
1332 DEFINE_PARSE_NUM_TYPE(_long
, long, strtol
, LONG_MIN
, LONG_MAX
)
1333 DEFINE_PARSE_NUM_TYPE(_llong
, long long, strtoll
, LLONG_MIN
, LLONG_MAX
)
1335 #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
1336 int parse ## name(const char *str, type * ul) \
1339 int retval = parse ## funcname(str, &n); \
1340 if (retval != ERROR_OK) \
1343 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1345 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1350 #define DEFINE_PARSE_ULONGLONG(name, type, min, max) \
1351 DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
1352 DEFINE_PARSE_ULONGLONG(_uint
, unsigned, 0, UINT_MAX
)
1353 DEFINE_PARSE_ULONGLONG(_u64
, uint64_t, 0, UINT64_MAX
)
1354 DEFINE_PARSE_ULONGLONG(_u32
, uint32_t, 0, UINT32_MAX
)
1355 DEFINE_PARSE_ULONGLONG(_u16
, uint16_t, 0, UINT16_MAX
)
1356 DEFINE_PARSE_ULONGLONG(_u8
, uint8_t, 0, UINT8_MAX
)
1358 DEFINE_PARSE_ULONGLONG(_target_addr
, target_addr_t
, 0, TARGET_ADDR_MAX
)
1360 #define DEFINE_PARSE_LONGLONG(name, type, min, max) \
1361 DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
1362 DEFINE_PARSE_LONGLONG(_int
, int, n
< INT_MIN
, INT_MAX
)
1363 DEFINE_PARSE_LONGLONG(_s64
, int64_t, n
< INT64_MIN
, INT64_MAX
)
1364 DEFINE_PARSE_LONGLONG(_s32
, int32_t, n
< INT32_MIN
, INT32_MAX
)
1365 DEFINE_PARSE_LONGLONG(_s16
, int16_t, n
< INT16_MIN
, INT16_MAX
)
1366 DEFINE_PARSE_LONGLONG(_s8
, int8_t, n
< INT8_MIN
, INT8_MAX
)
1368 static int command_parse_bool(const char *in
, bool *out
,
1369 const char *on
, const char *off
)
1371 if (strcasecmp(in
, on
) == 0)
1373 else if (strcasecmp(in
, off
) == 0)
1376 return ERROR_COMMAND_SYNTAX_ERROR
;
1380 int command_parse_bool_arg(const char *in
, bool *out
)
1382 if (command_parse_bool(in
, out
, "on", "off") == ERROR_OK
)
1384 if (command_parse_bool(in
, out
, "enable", "disable") == ERROR_OK
)
1386 if (command_parse_bool(in
, out
, "true", "false") == ERROR_OK
)
1388 if (command_parse_bool(in
, out
, "yes", "no") == ERROR_OK
)
1390 if (command_parse_bool(in
, out
, "1", "0") == ERROR_OK
)
1392 return ERROR_COMMAND_SYNTAX_ERROR
;
1395 COMMAND_HELPER(handle_command_parse_bool
, bool *out
, const char *label
)
1399 const char *in
= CMD_ARGV
[0];
1400 if (command_parse_bool_arg(in
, out
) != ERROR_OK
) {
1401 LOG_ERROR("%s: argument '%s' is not valid", CMD_NAME
, in
);
1402 return ERROR_COMMAND_SYNTAX_ERROR
;
1407 LOG_INFO("%s is %s", label
, *out
? "enabled" : "disabled");
1410 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)