1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * part of this file is taken from libcli (libcli.sourceforge.net) *
12 * Copyright (C) David Parrish (david@dparrish.com) *
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. *
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. *
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 ***************************************************************************/
32 /* see Embedded-HOWTO.txt in Jim Tcl project hosted on BerliOS*/
35 /* @todo the inclusion of target.h here is a layering violation */
36 #include <jtag/jtag.h>
37 #include <target/target.h>
39 #include "configuration.h"
41 #include "time_support.h"
42 #include "jim-eventloop.h"
44 /* nice short description of source file */
45 #define __THIS__FILE__ "command.c"
47 struct log_capture_state
{
52 static int unregister_command(struct command_context
*context
,
53 const char *cmd_prefix
, const char *name
);
54 static int jim_command_dispatch(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
);
55 static int help_add_command(struct command_context
*cmd_ctx
,
56 const char *cmd_name
, const char *help_text
, const char *usage_text
);
57 static int help_del_command(struct command_context
*cmd_ctx
, const char *cmd_name
);
59 /* set of functions to wrap jimtcl internal data */
60 static inline bool jimcmd_is_proc(Jim_Cmd
*cmd
)
65 bool jimcmd_is_oocd_command(Jim_Cmd
*cmd
)
67 return !cmd
->isproc
&& cmd
->u
.native
.cmdProc
== jim_command_dispatch
;
70 void *jimcmd_privdata(Jim_Cmd
*cmd
)
72 return cmd
->isproc
? NULL
: cmd
->u
.native
.privData
;
75 static void tcl_output(void *privData
, const char *file
, unsigned line
,
76 const char *function
, const char *string
)
78 struct log_capture_state
*state
= privData
;
79 Jim_AppendString(state
->interp
, state
->output
, string
, strlen(string
));
82 static struct log_capture_state
*command_log_capture_start(Jim_Interp
*interp
)
84 /* capture log output and return it. A garbage collect can
85 * happen, so we need a reference count to this object */
86 Jim_Obj
*jim_output
= Jim_NewStringObj(interp
, "", 0);
90 Jim_IncrRefCount(jim_output
);
92 struct log_capture_state
*state
= malloc(sizeof(*state
));
94 LOG_ERROR("Out of memory");
95 Jim_DecrRefCount(interp
, jim_output
);
99 state
->interp
= interp
;
100 state
->output
= jim_output
;
102 log_add_callback(tcl_output
, state
);
107 /* Classic openocd commands provide progress output which we
108 * will capture and return as a Tcl return value.
110 * However, if a non-openocd command has been invoked, then it
111 * makes sense to return the tcl return value from that command.
113 * The tcl return value is empty for openocd commands that provide
116 * Therefore we set the tcl return value only if we actually
119 static void command_log_capture_finish(struct log_capture_state
*state
)
124 log_remove_callback(tcl_output
, state
);
127 Jim_GetString(state
->output
, &length
);
130 Jim_SetResult(state
->interp
, state
->output
);
132 /* No output captured, use tcl return value (which could
135 Jim_DecrRefCount(state
->interp
, state
->output
);
140 static int command_retval_set(Jim_Interp
*interp
, int retval
)
142 int *return_retval
= Jim_GetAssocData(interp
, "retval");
144 *return_retval
= retval
;
146 return (retval
== ERROR_OK
) ? JIM_OK
: retval
;
149 extern struct command_context
*global_cmd_ctx
;
151 /* dump a single line to the log for the command.
152 * Do nothing in case we are not at debug level 3 */
153 static void script_debug(Jim_Interp
*interp
, unsigned int argc
, Jim_Obj
* const *argv
)
155 if (debug_level
< LOG_LVL_DEBUG
)
158 char *dbg
= alloc_printf("command -");
159 for (unsigned i
= 0; i
< argc
; i
++) {
161 const char *w
= Jim_GetString(argv
[i
], &len
);
162 char *t
= alloc_printf("%s %s", dbg
, w
);
166 LOG_DEBUG("%s", dbg
);
170 static void script_command_args_free(char **words
, unsigned nwords
)
172 for (unsigned i
= 0; i
< nwords
; i
++)
177 static char **script_command_args_alloc(
178 unsigned argc
, Jim_Obj
* const *argv
, unsigned *nwords
)
180 char **words
= malloc(argc
* sizeof(char *));
185 for (i
= 0; i
< argc
; i
++) {
187 const char *w
= Jim_GetString(argv
[i
], &len
);
188 words
[i
] = strdup(w
);
190 script_command_args_free(words
, i
);
198 struct command_context
*current_command_context(Jim_Interp
*interp
)
200 /* grab the command context from the associated data */
201 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
203 /* Tcl can invoke commands directly instead of via command_run_line(). This would
204 * happen when the Jim Tcl interpreter is provided by eCos or if we are running
205 * commands in a startup script.
207 * A telnet or gdb server would provide a non-default command context to
208 * handle piping of error output, have a separate current target, etc.
210 cmd_ctx
= global_cmd_ctx
;
216 * Find a openocd command from fullname.
217 * @returns Returns the named command if it is registred in interp.
218 * Returns NULL otherwise.
220 static struct command
*command_find_from_name(Jim_Interp
*interp
, const char *name
)
225 Jim_Obj
*jim_name
= Jim_NewStringObj(interp
, name
, -1);
226 Jim_IncrRefCount(jim_name
);
227 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, jim_name
, JIM_NONE
);
228 Jim_DecrRefCount(interp
, jim_name
);
229 if (!cmd
|| jimcmd_is_proc(cmd
) || !jimcmd_is_oocd_command(cmd
))
232 return jimcmd_privdata(cmd
);
235 static struct command
*command_new(struct command_context
*cmd_ctx
,
236 const char *full_name
, const struct command_registration
*cr
)
241 * If it is a non-jim command with no .usage specified,
244 * strlen(.usage) == 0 means that the command takes no
247 if (!cr
->jim_handler
&& !cr
->usage
)
248 LOG_ERROR("BUG: command '%s' does not have the "
249 "'.usage' field filled out",
252 struct command
*c
= calloc(1, sizeof(struct command
));
256 c
->name
= strdup(cr
->name
);
262 c
->handler
= cr
->handler
;
263 c
->jim_handler
= cr
->jim_handler
;
266 if (cr
->help
|| cr
->usage
)
267 help_add_command(cmd_ctx
, full_name
, cr
->help
, cr
->usage
);
272 static void command_free(struct Jim_Interp
*interp
, void *priv
)
274 struct command
*c
= priv
;
280 static struct command
*register_command(struct command_context
*context
,
281 const char *cmd_prefix
, const struct command_registration
*cr
)
285 if (!context
|| !cr
->name
)
289 full_name
= alloc_printf("%s %s", cmd_prefix
, cr
->name
);
291 full_name
= strdup(cr
->name
);
295 struct command
*c
= command_find_from_name(context
->interp
, full_name
);
297 /* TODO: originally we treated attempting to register a cmd twice as an error
298 * Sometimes we need this behaviour, such as with flash banks.
299 * http://www.mail-archive.com/openocd-development@lists.berlios.de/msg11152.html */
300 LOG_DEBUG("command '%s' is already registered", full_name
);
305 c
= command_new(context
, full_name
, cr
);
311 if (false) /* too noisy with debug_level 3 */
312 LOG_DEBUG("registering '%s'...", full_name
);
313 int retval
= Jim_CreateCommand(context
->interp
, full_name
,
314 jim_command_dispatch
, c
, command_free
);
315 if (retval
!= JIM_OK
) {
316 command_run_linef(context
, "del_help_text {%s}", full_name
);
317 command_run_linef(context
, "del_usage_text {%s}", full_name
);
327 int __register_commands(struct command_context
*cmd_ctx
, const char *cmd_prefix
,
328 const struct command_registration
*cmds
, void *data
,
329 struct target
*override_target
)
331 int retval
= ERROR_OK
;
333 for (i
= 0; cmds
[i
].name
|| cmds
[i
].chain
; i
++) {
334 const struct command_registration
*cr
= cmds
+ i
;
336 struct command
*c
= NULL
;
338 c
= register_command(cmd_ctx
, cmd_prefix
, cr
);
343 c
->jim_handler_data
= data
;
344 c
->jim_override_target
= override_target
;
349 char *new_prefix
= alloc_printf("%s %s", cmd_prefix
, cr
->name
);
354 retval
= __register_commands(cmd_ctx
, new_prefix
, cr
->chain
, data
, override_target
);
357 retval
= __register_commands(cmd_ctx
, cr
->name
, cr
->chain
, data
, override_target
);
360 retval
= __register_commands(cmd_ctx
, cmd_prefix
, cr
->chain
, data
, override_target
);
362 if (retval
!= ERROR_OK
)
366 if (retval
!= ERROR_OK
) {
367 for (unsigned j
= 0; j
< i
; j
++)
368 unregister_command(cmd_ctx
, cmd_prefix
, cmds
[j
].name
);
373 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 2, 3)))
374 int unregister_commands_match(struct command_context
*cmd_ctx
, const char *format
, ...)
376 Jim_Interp
*interp
= cmd_ctx
->interp
;
379 va_start(ap
, format
);
380 char *query
= alloc_vprintf(format
, ap
);
385 char *query_cmd
= alloc_printf("info commands {%s}", query
);
390 int retval
= Jim_EvalSource(interp
, __THIS__FILE__
, __LINE__
, query_cmd
);
392 if (retval
!= JIM_OK
)
395 Jim_Obj
*list
= Jim_GetResult(interp
);
396 Jim_IncrRefCount(list
);
398 int len
= Jim_ListLength(interp
, list
);
399 for (int i
= 0; i
< len
; i
++) {
400 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, list
, i
);
401 Jim_IncrRefCount(elem
);
403 const char *name
= Jim_GetString(elem
, NULL
);
404 struct command
*c
= command_find_from_name(interp
, name
);
406 /* not openocd command */
407 Jim_DecrRefCount(interp
, elem
);
410 if (false) /* too noisy with debug_level 3 */
411 LOG_DEBUG("delete command \"%s\"", name
);
412 #if JIM_VERSION >= 80
413 Jim_DeleteCommand(interp
, elem
);
415 Jim_DeleteCommand(interp
, name
);
418 help_del_command(cmd_ctx
, name
);
420 Jim_DecrRefCount(interp
, elem
);
423 Jim_DecrRefCount(interp
, list
);
427 int unregister_all_commands(struct command_context
*context
,
428 const char *cmd_prefix
)
433 if (!cmd_prefix
|| !*cmd_prefix
)
434 return unregister_commands_match(context
, "*");
436 int retval
= unregister_commands_match(context
, "%s *", cmd_prefix
);
437 if (retval
!= ERROR_OK
)
440 return unregister_commands_match(context
, "%s", cmd_prefix
);
443 static int unregister_command(struct command_context
*context
,
444 const char *cmd_prefix
, const char *name
)
446 if (!context
|| !name
)
447 return ERROR_COMMAND_SYNTAX_ERROR
;
449 if (!cmd_prefix
|| !*cmd_prefix
)
450 return unregister_commands_match(context
, "%s", name
);
452 return unregister_commands_match(context
, "%s %s", cmd_prefix
, name
);
455 void command_output_text(struct command_context
*context
, const char *data
)
457 if (context
&& context
->output_handler
&& data
)
458 context
->output_handler(context
, data
);
461 void command_print_sameline(struct command_invocation
*cmd
, const char *format
, ...)
466 va_start(ap
, format
);
468 string
= alloc_vprintf(format
, ap
);
470 /* we want this collected in the log + we also want to pick it up as a tcl return
473 * The latter bit isn't precisely neat, but will do for now.
475 Jim_AppendString(cmd
->ctx
->interp
, cmd
->output
, string
, -1);
476 /* We already printed it above
477 * command_output_text(context, string); */
484 void command_print(struct command_invocation
*cmd
, const char *format
, ...)
489 va_start(ap
, format
);
491 string
= alloc_vprintf(format
, ap
);
493 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one
495 /* we want this collected in the log + we also want to pick it up as a tcl return
498 * The latter bit isn't precisely neat, but will do for now.
500 Jim_AppendString(cmd
->ctx
->interp
, cmd
->output
, string
, -1);
501 /* We already printed it above
502 * command_output_text(context, string); */
509 static bool command_can_run(struct command_context
*cmd_ctx
, struct command
*c
, const char *full_name
)
511 if (c
->mode
== COMMAND_ANY
|| c
->mode
== cmd_ctx
->mode
)
514 /* Many commands may be run only before/after 'init' */
523 /* handle the impossible with humor; it guarantees a bug report! */
525 when
= "if Cthulhu is summoned by";
528 LOG_ERROR("The '%s' command must be used %s 'init'.",
529 full_name
? full_name
: c
->name
, when
);
533 static int run_command(struct command_context
*context
,
534 struct command
*c
, const char **words
, unsigned num_words
)
536 struct command_invocation cmd
= {
540 .argc
= num_words
- 1,
544 cmd
.output
= Jim_NewEmptyStringObj(context
->interp
);
545 Jim_IncrRefCount(cmd
.output
);
547 int retval
= c
->handler(&cmd
);
548 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
) {
549 /* Print help for command */
550 command_run_linef(context
, "usage %s", words
[0]);
551 } else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
) {
552 /* just fall through for a shutdown request */
554 if (retval
!= ERROR_OK
)
555 LOG_DEBUG("Command '%s' failed with error code %d",
557 /* Use the command output as the Tcl result */
558 Jim_SetResult(context
->interp
, cmd
.output
);
560 Jim_DecrRefCount(context
->interp
, cmd
.output
);
565 int command_run_line(struct command_context
*context
, char *line
)
567 /* all the parent commands have been registered with the interpreter
568 * so, can just evaluate the line as a script and check for
571 /* run the line thru a script engine */
572 int retval
= ERROR_FAIL
;
574 /* Beware! This code needs to be reentrant. It is also possible
575 * for OpenOCD commands to be invoked directly from Tcl. This would
576 * happen when the Jim Tcl interpreter is provided by eCos for
579 struct target
*saved_target_override
= context
->current_target_override
;
580 context
->current_target_override
= NULL
;
582 Jim_Interp
*interp
= context
->interp
;
583 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
584 Jim_DeleteAssocData(interp
, "context");
585 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
586 if (retcode
== JIM_OK
) {
587 /* associated the return value */
588 Jim_DeleteAssocData(interp
, "retval");
589 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
590 if (retcode
== JIM_OK
) {
591 retcode
= Jim_Eval_Named(interp
, line
, 0, 0);
593 Jim_DeleteAssocData(interp
, "retval");
595 Jim_DeleteAssocData(interp
, "context");
596 int inner_retcode
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
597 if (retcode
== JIM_OK
)
598 retcode
= inner_retcode
;
600 context
->current_target_override
= saved_target_override
;
601 if (retcode
== JIM_OK
) {
605 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
607 command_output_text(context
, result
);
608 command_output_text(context
, "\n");
611 } else if (retcode
== JIM_EXIT
) {
613 * exit(Jim_GetExitCode(interp)); */
614 } else if (retcode
== ERROR_COMMAND_CLOSE_CONNECTION
) {
617 Jim_MakeErrorMessage(interp
);
618 /* error is broadcast */
619 LOG_USER("%s", Jim_GetString(Jim_GetResult(interp
), NULL
));
621 if (retval
== ERROR_OK
) {
622 /* It wasn't a low level OpenOCD command that failed */
631 int command_run_linef(struct command_context
*context
, const char *format
, ...)
633 int retval
= ERROR_FAIL
;
636 va_start(ap
, format
);
637 string
= alloc_vprintf(format
, ap
);
639 retval
= command_run_line(context
, string
);
646 void command_set_output_handler(struct command_context
*context
,
647 command_output_handler_t output_handler
, void *priv
)
649 context
->output_handler
= output_handler
;
650 context
->output_handler_priv
= priv
;
653 struct command_context
*copy_command_context(struct command_context
*context
)
655 struct command_context
*copy_context
= malloc(sizeof(struct command_context
));
657 *copy_context
= *context
;
662 void command_done(struct command_context
*cmd_ctx
)
670 /* find full path to file */
671 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
675 const char *file
= Jim_GetString(argv
[1], NULL
);
676 char *full_path
= find_file(file
);
679 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
682 Jim_SetResult(interp
, result
);
686 COMMAND_HANDLER(handle_echo
)
688 if (CMD_ARGC
== 2 && !strcmp(CMD_ARGV
[0], "-n")) {
689 LOG_USER_N("%s", CMD_ARGV
[1]);
696 LOG_USER("%s", CMD_ARGV
[0]);
700 /* Capture progress output and return as tcl return value. If the
701 * progress output was empty, return tcl return value.
703 static int jim_capture(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
708 struct log_capture_state
*state
= command_log_capture_start(interp
);
710 /* disable polling during capture. This avoids capturing output
713 * This is necessary in order to avoid accidentally getting a non-empty
714 * string for tcl fn's.
716 bool save_poll
= jtag_poll_get_enabled();
718 jtag_poll_set_enabled(false);
720 const char *str
= Jim_GetString(argv
[1], NULL
);
721 int retcode
= Jim_Eval_Named(interp
, str
, __THIS__FILE__
, __LINE__
);
723 jtag_poll_set_enabled(save_poll
);
725 command_log_capture_finish(state
);
737 static COMMAND_HELPER(command_help_show
, struct help_entry
*c
,
738 bool show_help
, const char *cmd_match
);
740 static COMMAND_HELPER(command_help_show_list
, bool show_help
, const char *cmd_match
)
742 struct help_entry
*entry
;
744 list_for_each_entry(entry
, CMD_CTX
->help_list
, lh
)
745 CALL_COMMAND_HANDLER(command_help_show
, entry
, show_help
, cmd_match
);
749 #define HELP_LINE_WIDTH(_n) (int)(76 - (2 * _n))
751 static void command_help_show_indent(unsigned n
)
753 for (unsigned i
= 0; i
< n
; i
++)
756 static void command_help_show_wrap(const char *str
, unsigned n
, unsigned n2
)
758 const char *cp
= str
, *last
= str
;
760 const char *next
= last
;
765 } while (*next
!= ' ' && *next
!= '\t' && *next
!= '\0');
766 } while ((next
- last
< HELP_LINE_WIDTH(n
)) && *next
!= '\0');
767 if (next
- last
< HELP_LINE_WIDTH(n
))
769 command_help_show_indent(n
);
770 LOG_USER("%.*s", (int)(cp
- last
), last
);
776 static COMMAND_HELPER(command_help_show
, struct help_entry
*c
,
777 bool show_help
, const char *cmd_match
)
780 for (const char *s
= strchr(c
->cmd_name
, ' '); s
; s
= strchr(s
+ 1, ' '))
783 /* If the match string occurs anywhere, we print out
784 * stuff for this command. */
785 bool is_match
= strstr(c
->cmd_name
, cmd_match
) ||
786 (c
->usage
&& strstr(c
->usage
, cmd_match
)) ||
787 (c
->help
&& strstr(c
->help
, cmd_match
));
790 if (c
->usage
&& strlen(c
->usage
) > 0) {
791 char *msg
= alloc_printf("%s %s", c
->cmd_name
, c
->usage
);
792 command_help_show_wrap(msg
, n
, n
+ 5);
795 command_help_show_wrap(c
->cmd_name
, n
, n
+ 5);
799 if (is_match
&& show_help
) {
802 /* TODO: factorize jim_command_mode() to avoid running jim command here */
803 char *request
= alloc_printf("command mode %s", c
->cmd_name
);
805 LOG_ERROR("Out of memory");
808 int retval
= Jim_Eval(CMD_CTX
->interp
, request
);
810 enum command_mode mode
= COMMAND_UNKNOWN
;
811 if (retval
!= JIM_ERR
) {
812 const char *result
= Jim_GetString(Jim_GetResult(CMD_CTX
->interp
), NULL
);
813 if (!strcmp(result
, "any"))
815 else if (!strcmp(result
, "config"))
816 mode
= COMMAND_CONFIG
;
817 else if (!strcmp(result
, "exec"))
821 /* Normal commands are runtime-only; highlight exceptions */
822 if (mode
!= COMMAND_EXEC
) {
823 const char *stage_msg
= "";
827 stage_msg
= " (configuration command)";
830 stage_msg
= " (command valid any time)";
833 stage_msg
= " (?mode error?)";
836 msg
= alloc_printf("%s%s", c
->help
? c
->help
: "", stage_msg
);
838 msg
= alloc_printf("%s", c
->help
? c
->help
: "");
841 command_help_show_wrap(msg
, n
+ 3, n
+ 3);
850 COMMAND_HANDLER(handle_help_command
)
852 bool full
= strcmp(CMD_NAME
, "help") == 0;
857 cmd_match
= strdup("");
860 cmd_match
= strdup(CMD_ARGV
[0]);
862 for (unsigned int i
= 1; i
< CMD_ARGC
&& cmd_match
; ++i
) {
863 char *prev
= cmd_match
;
864 cmd_match
= alloc_printf("%s %s", prev
, CMD_ARGV
[i
]);
870 LOG_ERROR("unable to build search string");
873 retval
= CALL_COMMAND_HANDLER(command_help_show_list
, full
, cmd_match
);
879 static char *alloc_concatenate_strings(int argc
, Jim_Obj
* const *argv
)
886 all
= strdup(Jim_GetString(argv
[0], NULL
));
888 LOG_ERROR("Out of memory");
892 for (i
= 1; i
< argc
; ++i
) {
894 all
= alloc_printf("%s %s", all
, Jim_GetString(argv
[i
], NULL
));
897 LOG_ERROR("Out of memory");
905 static int exec_command(Jim_Interp
*interp
, struct command_context
*cmd_ctx
,
906 struct command
*c
, int argc
, Jim_Obj
* const *argv
)
909 return c
->jim_handler(interp
, argc
, argv
);
913 char **words
= script_command_args_alloc(argc
, argv
, &nwords
);
917 int retval
= run_command(cmd_ctx
, c
, (const char **)words
, nwords
);
918 script_command_args_free(words
, nwords
);
919 return command_retval_set(interp
, retval
);
922 static int jim_command_dispatch(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
924 /* check subcommands */
926 char *s
= alloc_printf("%s %s", Jim_GetString(argv
[0], NULL
), Jim_GetString(argv
[1], NULL
));
927 Jim_Obj
*js
= Jim_NewStringObj(interp
, s
, -1);
928 Jim_IncrRefCount(js
);
930 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, js
, JIM_NONE
);
932 int retval
= Jim_EvalObjPrefix(interp
, js
, argc
- 2, argv
+ 2);
933 Jim_DecrRefCount(interp
, js
);
936 Jim_DecrRefCount(interp
, js
);
939 script_debug(interp
, argc
, argv
);
941 struct command
*c
= jim_to_command(interp
);
942 if (!c
->jim_handler
&& !c
->handler
) {
943 Jim_EvalObjPrefix(interp
, Jim_NewStringObj(interp
, "usage", -1), 1, argv
);
947 struct command_context
*cmd_ctx
= current_command_context(interp
);
949 if (!command_can_run(cmd_ctx
, c
, Jim_GetString(argv
[0], NULL
)))
952 target_call_timer_callbacks_now();
955 * Black magic of overridden current target:
956 * If the command we are going to handle has a target prefix,
957 * override the current target temporarily for the time
958 * of processing the command.
959 * current_target_override is used also for event handlers
960 * therefore we prevent touching it if command has no prefix.
961 * Previous override is saved and restored back to ensure
962 * correct work when jim_command_dispatch() is re-entered.
964 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
965 if (c
->jim_override_target
)
966 cmd_ctx
->current_target_override
= c
->jim_override_target
;
968 int retval
= exec_command(interp
, cmd_ctx
, c
, argc
, argv
);
970 if (c
->jim_override_target
)
971 cmd_ctx
->current_target_override
= saved_target_override
;
976 static int jim_command_mode(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
978 struct command_context
*cmd_ctx
= current_command_context(interp
);
979 enum command_mode mode
;
982 char *full_name
= alloc_concatenate_strings(argc
- 1, argv
+ 1);
985 Jim_Obj
*s
= Jim_NewStringObj(interp
, full_name
, -1);
987 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, s
, JIM_NONE
);
988 Jim_DecrRefCount(interp
, s
);
990 if (!cmd
|| !(jimcmd_is_proc(cmd
) || jimcmd_is_oocd_command(cmd
))) {
991 Jim_SetResultString(interp
, "unknown", -1);
995 if (jimcmd_is_proc(cmd
)) {
999 struct command
*c
= jimcmd_privdata(cmd
);
1004 mode
= cmd_ctx
->mode
;
1006 const char *mode_str
;
1011 case COMMAND_CONFIG
:
1012 mode_str
= "config";
1018 mode_str
= "unknown";
1021 Jim_SetResultString(interp
, mode_str
, -1);
1025 int help_del_all_commands(struct command_context
*cmd_ctx
)
1027 struct help_entry
*curr
, *n
;
1029 list_for_each_entry_safe(curr
, n
, cmd_ctx
->help_list
, lh
) {
1030 list_del(&curr
->lh
);
1031 free(curr
->cmd_name
);
1039 static int help_del_command(struct command_context
*cmd_ctx
, const char *cmd_name
)
1041 struct help_entry
*curr
;
1043 list_for_each_entry(curr
, cmd_ctx
->help_list
, lh
) {
1044 if (!strcmp(cmd_name
, curr
->cmd_name
)) {
1045 list_del(&curr
->lh
);
1046 free(curr
->cmd_name
);
1057 static int help_add_command(struct command_context
*cmd_ctx
,
1058 const char *cmd_name
, const char *help_text
, const char *usage_text
)
1060 int cmp
= -1; /* add after curr */
1061 struct help_entry
*curr
;
1063 list_for_each_entry_reverse(curr
, cmd_ctx
->help_list
, lh
) {
1064 cmp
= strcmp(cmd_name
, curr
->cmd_name
);
1069 struct help_entry
*entry
;
1071 entry
= calloc(1, sizeof(*entry
));
1073 LOG_ERROR("Out of memory");
1076 entry
->cmd_name
= strdup(cmd_name
);
1077 if (!entry
->cmd_name
) {
1078 LOG_ERROR("Out of memory");
1082 list_add(&entry
->lh
, &curr
->lh
);
1088 char *text
= strdup(help_text
);
1090 LOG_ERROR("Out of memory");
1098 char *text
= strdup(usage_text
);
1100 LOG_ERROR("Out of memory");
1104 entry
->usage
= text
;
1110 COMMAND_HANDLER(handle_help_add_command
)
1113 return ERROR_COMMAND_SYNTAX_ERROR
;
1115 const char *help
= !strcmp(CMD_NAME
, "add_help_text") ? CMD_ARGV
[1] : NULL
;
1116 const char *usage
= !strcmp(CMD_NAME
, "add_usage_text") ? CMD_ARGV
[1] : NULL
;
1117 if (!help
&& !usage
) {
1118 LOG_ERROR("command name '%s' is unknown", CMD_NAME
);
1119 return ERROR_COMMAND_SYNTAX_ERROR
;
1121 const char *cmd_name
= CMD_ARGV
[0];
1122 return help_add_command(CMD_CTX
, cmd_name
, help
, usage
);
1125 /* sleep command sleeps for <n> milliseconds
1126 * this is useful in target startup scripts
1128 COMMAND_HANDLER(handle_sleep_command
)
1131 if (CMD_ARGC
== 2) {
1132 if (strcmp(CMD_ARGV
[1], "busy") == 0)
1135 return ERROR_COMMAND_SYNTAX_ERROR
;
1136 } else if (CMD_ARGC
< 1 || CMD_ARGC
> 2)
1137 return ERROR_COMMAND_SYNTAX_ERROR
;
1139 unsigned long duration
= 0;
1140 int retval
= parse_ulong(CMD_ARGV
[0], &duration
);
1141 if (retval
!= ERROR_OK
)
1145 int64_t then
= timeval_ms();
1146 while (timeval_ms() - then
< (int64_t)duration
) {
1147 target_call_timer_callbacks_now();
1151 busy_sleep(duration
);
1156 static const struct command_registration command_subcommand_handlers
[] = {
1159 .mode
= COMMAND_ANY
,
1160 .jim_handler
= jim_command_mode
,
1161 .usage
= "[command_name ...]",
1162 .help
= "Returns the command modes allowed by a command: "
1163 "'any', 'config', or 'exec'. If no command is "
1164 "specified, returns the current command mode. "
1165 "Returns 'unknown' if an unknown command is given. "
1166 "Command can be multiple tokens.",
1168 COMMAND_REGISTRATION_DONE
1171 static const struct command_registration command_builtin_handlers
[] = {
1174 .mode
= COMMAND_ANY
,
1175 .jim_handler
= jim_find
,
1176 .help
= "find full path to file",
1181 .mode
= COMMAND_ANY
,
1182 .jim_handler
= jim_capture
,
1183 .help
= "Capture progress output and return as tcl return value. If the "
1184 "progress output was empty, return tcl return value.",
1189 .handler
= handle_echo
,
1190 .mode
= COMMAND_ANY
,
1191 .help
= "Logs a message at \"user\" priority. "
1192 "Option \"-n\" suppresses trailing newline",
1193 .usage
= "[-n] string",
1196 .name
= "add_help_text",
1197 .handler
= handle_help_add_command
,
1198 .mode
= COMMAND_ANY
,
1199 .help
= "Add new command help text; "
1200 "Command can be multiple tokens.",
1201 .usage
= "command_name helptext_string",
1204 .name
= "add_usage_text",
1205 .handler
= handle_help_add_command
,
1206 .mode
= COMMAND_ANY
,
1207 .help
= "Add new command usage text; "
1208 "command can be multiple tokens.",
1209 .usage
= "command_name usage_string",
1213 .handler
= handle_sleep_command
,
1214 .mode
= COMMAND_ANY
,
1215 .help
= "Sleep for specified number of milliseconds. "
1216 "\"busy\" will busy wait instead (avoid this).",
1217 .usage
= "milliseconds ['busy']",
1221 .handler
= handle_help_command
,
1222 .mode
= COMMAND_ANY
,
1223 .help
= "Show full command help; "
1224 "command can be multiple tokens.",
1225 .usage
= "[command_name]",
1229 .handler
= handle_help_command
,
1230 .mode
= COMMAND_ANY
,
1231 .help
= "Show basic command usage; "
1232 "command can be multiple tokens.",
1233 .usage
= "[command_name]",
1237 .mode
= COMMAND_ANY
,
1238 .help
= "core command group (introspection)",
1239 .chain
= command_subcommand_handlers
,
1242 COMMAND_REGISTRATION_DONE
1245 struct command_context
*command_init(const char *startup_tcl
, Jim_Interp
*interp
)
1247 struct command_context
*context
= calloc(1, sizeof(struct command_context
));
1249 context
->mode
= COMMAND_EXEC
;
1251 /* context can be duplicated. Put list head on separate mem-chunk to keep list consistent */
1252 context
->help_list
= malloc(sizeof(*context
->help_list
));
1253 INIT_LIST_HEAD(context
->help_list
);
1255 /* Create a jim interpreter if we were not handed one */
1257 /* Create an interpreter */
1258 interp
= Jim_CreateInterp();
1259 /* Add all the Jim core commands */
1260 Jim_RegisterCoreCommands(interp
);
1261 Jim_InitStaticExtensions(interp
);
1264 context
->interp
= interp
;
1266 register_commands(context
, NULL
, command_builtin_handlers
);
1268 Jim_SetAssocData(interp
, "context", NULL
, context
);
1269 if (Jim_Eval_Named(interp
, startup_tcl
, "embedded:startup.tcl", 1) == JIM_ERR
) {
1270 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
1271 Jim_MakeErrorMessage(interp
);
1272 LOG_USER_N("%s", Jim_GetString(Jim_GetResult(interp
), NULL
));
1275 Jim_DeleteAssocData(interp
, "context");
1280 void command_exit(struct command_context
*context
)
1285 Jim_FreeInterp(context
->interp
);
1286 free(context
->help_list
);
1287 command_done(context
);
1290 int command_context_mode(struct command_context
*cmd_ctx
, enum command_mode mode
)
1293 return ERROR_COMMAND_SYNTAX_ERROR
;
1295 cmd_ctx
->mode
= mode
;
1299 void process_jim_events(struct command_context
*cmd_ctx
)
1301 static int recursion
;
1306 Jim_ProcessEvents(cmd_ctx
->interp
, JIM_ALL_EVENTS
| JIM_DONT_WAIT
);
1310 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
1311 int parse ## name(const char *str, type * ul) \
1314 LOG_ERROR("Invalid command argument"); \
1315 return ERROR_COMMAND_ARGUMENT_INVALID; \
1319 *ul = func(str, &end, 0); \
1321 LOG_ERROR("Invalid command argument"); \
1322 return ERROR_COMMAND_ARGUMENT_INVALID; \
1324 if ((max == *ul) && (errno == ERANGE)) { \
1325 LOG_ERROR("Argument overflow"); \
1326 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1328 if (min && (min == *ul) && (errno == ERANGE)) { \
1329 LOG_ERROR("Argument underflow"); \
1330 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1334 DEFINE_PARSE_NUM_TYPE(_ulong
, unsigned long, strtoul
, 0, ULONG_MAX
)
1335 DEFINE_PARSE_NUM_TYPE(_ullong
, unsigned long long, strtoull
, 0, ULLONG_MAX
)
1336 DEFINE_PARSE_NUM_TYPE(_long
, long, strtol
, LONG_MIN
, LONG_MAX
)
1337 DEFINE_PARSE_NUM_TYPE(_llong
, long long, strtoll
, LLONG_MIN
, LLONG_MAX
)
1339 #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
1340 int parse ## name(const char *str, type * ul) \
1343 int retval = parse ## funcname(str, &n); \
1344 if (retval != ERROR_OK) \
1347 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1349 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1354 #define DEFINE_PARSE_ULONGLONG(name, type, min, max) \
1355 DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
1356 DEFINE_PARSE_ULONGLONG(_uint
, unsigned, 0, UINT_MAX
)
1357 DEFINE_PARSE_ULONGLONG(_u64
, uint64_t, 0, UINT64_MAX
)
1358 DEFINE_PARSE_ULONGLONG(_u32
, uint32_t, 0, UINT32_MAX
)
1359 DEFINE_PARSE_ULONGLONG(_u16
, uint16_t, 0, UINT16_MAX
)
1360 DEFINE_PARSE_ULONGLONG(_u8
, uint8_t, 0, UINT8_MAX
)
1362 DEFINE_PARSE_ULONGLONG(_target_addr
, target_addr_t
, 0, TARGET_ADDR_MAX
)
1364 #define DEFINE_PARSE_LONGLONG(name, type, min, max) \
1365 DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
1366 DEFINE_PARSE_LONGLONG(_int
, int, n
< INT_MIN
, INT_MAX
)
1367 DEFINE_PARSE_LONGLONG(_s64
, int64_t, n
< INT64_MIN
, INT64_MAX
)
1368 DEFINE_PARSE_LONGLONG(_s32
, int32_t, n
< INT32_MIN
, INT32_MAX
)
1369 DEFINE_PARSE_LONGLONG(_s16
, int16_t, n
< INT16_MIN
, INT16_MAX
)
1370 DEFINE_PARSE_LONGLONG(_s8
, int8_t, n
< INT8_MIN
, INT8_MAX
)
1372 static int command_parse_bool(const char *in
, bool *out
,
1373 const char *on
, const char *off
)
1375 if (strcasecmp(in
, on
) == 0)
1377 else if (strcasecmp(in
, off
) == 0)
1380 return ERROR_COMMAND_SYNTAX_ERROR
;
1384 int command_parse_bool_arg(const char *in
, bool *out
)
1386 if (command_parse_bool(in
, out
, "on", "off") == ERROR_OK
)
1388 if (command_parse_bool(in
, out
, "enable", "disable") == ERROR_OK
)
1390 if (command_parse_bool(in
, out
, "true", "false") == ERROR_OK
)
1392 if (command_parse_bool(in
, out
, "yes", "no") == ERROR_OK
)
1394 if (command_parse_bool(in
, out
, "1", "0") == ERROR_OK
)
1396 return ERROR_COMMAND_SYNTAX_ERROR
;
1399 COMMAND_HELPER(handle_command_parse_bool
, bool *out
, const char *label
)
1403 const char *in
= CMD_ARGV
[0];
1404 if (command_parse_bool_arg(in
, out
) != ERROR_OK
) {
1405 LOG_ERROR("%s: argument '%s' is not valid", CMD_NAME
, in
);
1406 return ERROR_COMMAND_SYNTAX_ERROR
;
1411 LOG_INFO("%s is %s", label
, *out
? "enabled" : "disabled");
1414 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)