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 struct command
*parent
, const char *name
);
54 static char *command_name(struct command
*c
, char delim
);
55 static int command_unknown(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
);
56 static int help_add_command(struct command_context
*cmd_ctx
,
57 const char *cmd_name
, const char *help_text
, const char *usage_text
);
58 static int help_del_command(struct command_context
*cmd_ctx
, const char *cmd_name
);
60 /* set of functions to wrap jimtcl internal data */
61 static inline bool jimcmd_is_proc(Jim_Cmd
*cmd
)
66 static inline bool jimcmd_is_ocd_command(Jim_Cmd
*cmd
)
68 return !cmd
->isproc
&& cmd
->u
.native
.cmdProc
== command_unknown
;
71 static inline void *jimcmd_privdata(Jim_Cmd
*cmd
)
73 return cmd
->isproc
? NULL
: cmd
->u
.native
.privData
;
76 static void tcl_output(void *privData
, const char *file
, unsigned line
,
77 const char *function
, const char *string
)
79 struct log_capture_state
*state
= privData
;
80 Jim_AppendString(state
->interp
, state
->output
, string
, strlen(string
));
83 static struct log_capture_state
*command_log_capture_start(Jim_Interp
*interp
)
85 /* capture log output and return it. A garbage collect can
86 * happen, so we need a reference count to this object */
87 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
88 if (NULL
== tclOutput
)
91 struct log_capture_state
*state
= malloc(sizeof(*state
));
95 state
->interp
= interp
;
96 Jim_IncrRefCount(tclOutput
);
97 state
->output
= tclOutput
;
99 log_add_callback(tcl_output
, state
);
104 /* Classic openocd commands provide progress output which we
105 * will capture and return as a Tcl return value.
107 * However, if a non-openocd command has been invoked, then it
108 * makes sense to return the tcl return value from that command.
110 * The tcl return value is empty for openocd commands that provide
113 * Therefore we set the tcl return value only if we actually
116 static void command_log_capture_finish(struct log_capture_state
*state
)
121 log_remove_callback(tcl_output
, state
);
124 Jim_GetString(state
->output
, &length
);
127 Jim_SetResult(state
->interp
, state
->output
);
129 /* No output captured, use tcl return value (which could
132 Jim_DecrRefCount(state
->interp
, state
->output
);
138 * FIXME: workaround for memory leak in jimtcl 0.80
139 * Jim API Jim_CreateCommand() converts the command name in a Jim object and
140 * does not free the object. Fixed for jimtcl 0.81 by e4416cf86f0b
141 * Use the internal jimtcl API Jim_CreateCommandObj, not exported by jim.h,
142 * and override the bugged API through preprocessor's macro.
143 * This workaround works only when jimtcl is compiled as OpenOCD submodule.
144 * If jimtcl is linked-in from a precompiled library, either static or dynamic,
145 * the symbol Jim_CreateCommandObj is not exported and the build will use the
147 * To be removed when OpenOCD will switch to jimtcl 0.81
149 #if JIM_VERSION == 80
150 static int workaround_createcommand(Jim_Interp
*interp
, const char *cmdName
,
151 Jim_CmdProc
*cmdProc
, void *privData
, Jim_DelCmdProc
*delProc
);
152 int Jim_CreateCommandObj(Jim_Interp
*interp
, Jim_Obj
*cmdNameObj
,
153 Jim_CmdProc
*cmdProc
, void *privData
, Jim_DelCmdProc
*delProc
)
154 __attribute__((weak
, alias("workaround_createcommand")));
155 static int workaround_createcommand(Jim_Interp
*interp
, const char *cmdName
,
156 Jim_CmdProc
*cmdProc
, void *privData
, Jim_DelCmdProc
*delProc
)
158 if ((void *)Jim_CreateCommandObj
== (void *)workaround_createcommand
)
159 return Jim_CreateCommand(interp
, cmdName
, cmdProc
, privData
, delProc
);
161 Jim_Obj
*cmd_name
= Jim_NewStringObj(interp
, cmdName
, -1);
162 Jim_IncrRefCount(cmd_name
);
163 int retval
= Jim_CreateCommandObj(interp
, cmd_name
, cmdProc
, privData
, delProc
);
164 Jim_DecrRefCount(interp
, cmd_name
);
167 #define Jim_CreateCommand workaround_createcommand
168 #endif /* JIM_VERSION == 80 */
169 /* FIXME: end of workaround for memory leak in jimtcl 0.80 */
171 static int command_retval_set(Jim_Interp
*interp
, int retval
)
173 int *return_retval
= Jim_GetAssocData(interp
, "retval");
174 if (return_retval
!= NULL
)
175 *return_retval
= retval
;
177 return (retval
== ERROR_OK
) ? JIM_OK
: retval
;
180 extern struct command_context
*global_cmd_ctx
;
182 /* dump a single line to the log for the command.
183 * Do nothing in case we are not at debug level 3 */
184 void script_debug(Jim_Interp
*interp
, unsigned int argc
, Jim_Obj
* const *argv
)
186 if (debug_level
< LOG_LVL_DEBUG
)
189 char *dbg
= alloc_printf("command -");
190 for (unsigned i
= 0; i
< argc
; i
++) {
192 const char *w
= Jim_GetString(argv
[i
], &len
);
193 char *t
= alloc_printf("%s %s", dbg
, w
);
197 LOG_DEBUG("%s", dbg
);
201 static void script_command_args_free(char **words
, unsigned nwords
)
203 for (unsigned i
= 0; i
< nwords
; i
++)
208 static char **script_command_args_alloc(
209 unsigned argc
, Jim_Obj
* const *argv
, unsigned *nwords
)
211 char **words
= malloc(argc
* sizeof(char *));
216 for (i
= 0; i
< argc
; i
++) {
218 const char *w
= Jim_GetString(argv
[i
], &len
);
219 words
[i
] = strdup(w
);
220 if (words
[i
] == NULL
) {
221 script_command_args_free(words
, i
);
229 struct command_context
*current_command_context(Jim_Interp
*interp
)
231 /* grab the command context from the associated data */
232 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
233 if (NULL
== cmd_ctx
) {
234 /* Tcl can invoke commands directly instead of via command_run_line(). This would
235 * happen when the Jim Tcl interpreter is provided by eCos or if we are running
236 * commands in a startup script.
238 * A telnet or gdb server would provide a non-default command context to
239 * handle piping of error output, have a separate current target, etc.
241 cmd_ctx
= global_cmd_ctx
;
247 * Find a openocd command from fullname.
248 * @returns Returns the named command if it is registred in interp.
249 * Returns NULL otherwise.
251 static struct command
*command_find_from_name(Jim_Interp
*interp
, const char *name
)
256 Jim_Obj
*jim_name
= Jim_NewStringObj(interp
, name
, -1);
257 Jim_IncrRefCount(jim_name
);
258 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, jim_name
, JIM_NONE
);
259 Jim_DecrRefCount(interp
, jim_name
);
260 if (!cmd
|| jimcmd_is_proc(cmd
) || !jimcmd_is_ocd_command(cmd
))
263 return jimcmd_privdata(cmd
);
267 * Find a command by name from a list of commands.
268 * @returns Returns the named command if it exists in the list.
269 * Returns NULL otherwise.
271 static struct command
*command_find(struct command
*head
, const char *name
)
273 for (struct command
*cc
= head
; cc
; cc
= cc
->next
) {
274 if (strcmp(cc
->name
, name
) == 0)
281 * Add the command into the linked list, sorted by name.
282 * @param head Address to head of command list pointer, which may be
283 * updated if @c c gets inserted at the beginning of the list.
284 * @param c The command to add to the list pointed to by @c head.
286 static void command_add_child(struct command
**head
, struct command
*c
)
294 while ((*head
)->next
&& (strcmp(c
->name
, (*head
)->name
) > 0))
295 head
= &(*head
)->next
;
297 if (strcmp(c
->name
, (*head
)->name
) > 0) {
298 c
->next
= (*head
)->next
;
306 static struct command
**command_list_for_parent(
307 struct command_context
*cmd_ctx
, struct command
*parent
)
309 return parent
? &parent
->children
: &cmd_ctx
->commands
;
312 static void command_free(struct command
*c
)
314 /** @todo if command has a handler, unregister its jim command! */
316 while (NULL
!= c
->children
) {
317 struct command
*tmp
= c
->children
;
318 c
->children
= tmp
->next
;
326 static struct command
*command_new(struct command_context
*cmd_ctx
,
327 struct command
*parent
, const struct command_registration
*cr
)
332 * If it is a non-jim command with no .usage specified,
335 * strlen(.usage) == 0 means that the command takes no
338 if ((cr
->jim_handler
== NULL
) && (cr
->usage
== NULL
)) {
339 LOG_ERROR("BUG: command '%s%s%s' does not have the "
340 "'.usage' field filled out",
341 parent
&& parent
->name
? parent
->name
: "",
342 parent
&& parent
->name
? " " : "",
346 struct command
*c
= calloc(1, sizeof(struct command
));
350 c
->name
= strdup(cr
->name
);
352 goto command_new_error
;
355 c
->handler
= cr
->handler
;
356 c
->jim_handler
= cr
->jim_handler
;
359 command_add_child(command_list_for_parent(cmd_ctx
, parent
), c
);
361 if (cr
->help
|| cr
->usage
) {
362 char *full_name
= command_name(c
, ' ');
363 help_add_command(cmd_ctx
, full_name
, cr
->help
, cr
->usage
);
374 static struct command
*register_command(struct command_context
*context
,
375 struct command
*parent
, const struct command_registration
*cr
)
377 if (!context
|| !cr
->name
)
380 const char *name
= cr
->name
;
381 struct command
**head
= command_list_for_parent(context
, parent
);
382 struct command
*c
= command_find(*head
, name
);
384 /* TODO: originally we treated attempting to register a cmd twice as an error
385 * Sometimes we need this behaviour, such as with flash banks.
386 * http://www.mail-archive.com/openocd-development@lists.berlios.de/msg11152.html */
387 LOG_DEBUG("command '%s' is already registered in '%s' context",
388 name
, parent
? parent
->name
: "<global>");
392 c
= command_new(context
, parent
, cr
);
396 char *full_name
= command_name(c
, ' ');
397 LOG_DEBUG("registering '%s'...", full_name
);
398 int retval
= Jim_CreateCommand(context
->interp
, full_name
,
399 command_unknown
, c
, NULL
);
400 if (retval
!= JIM_OK
) {
401 unregister_command(context
, parent
, name
);
407 static int ___register_commands(struct command_context
*cmd_ctx
, struct command
*parent
,
408 const struct command_registration
*cmds
, void *data
,
409 struct target
*override_target
)
411 int retval
= ERROR_OK
;
413 for (i
= 0; cmds
[i
].name
|| cmds
[i
].chain
; i
++) {
414 const struct command_registration
*cr
= cmds
+ i
;
416 struct command
*c
= NULL
;
417 if (NULL
!= cr
->name
) {
418 c
= register_command(cmd_ctx
, parent
, cr
);
423 c
->jim_handler_data
= data
;
424 c
->jim_override_target
= override_target
;
426 if (NULL
!= cr
->chain
) {
427 struct command
*p
= c
? : parent
;
428 retval
= ___register_commands(cmd_ctx
, p
, cr
->chain
, data
, override_target
);
429 if (ERROR_OK
!= retval
)
433 if (ERROR_OK
!= retval
) {
434 for (unsigned j
= 0; j
< i
; j
++)
435 unregister_command(cmd_ctx
, parent
, cmds
[j
].name
);
440 int __register_commands(struct command_context
*cmd_ctx
, const char *cmd_prefix
,
441 const struct command_registration
*cmds
, void *data
,
442 struct target
*override_target
)
444 struct command
*parent
= NULL
;
447 parent
= command_find(cmd_ctx
->commands
, cmd_prefix
);
449 return ___register_commands(cmd_ctx
, parent
, cmds
, data
, override_target
);
452 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 2, 3)))
453 int unregister_commands_match(struct command_context
*cmd_ctx
, const char *format
, ...)
455 Jim_Interp
*interp
= cmd_ctx
->interp
;
458 va_start(ap
, format
);
459 char *query
= alloc_vprintf(format
, ap
);
464 char *query_cmd
= alloc_printf("info commands {%s}", query
);
469 int retval
= Jim_EvalSource(interp
, __THIS__FILE__
, __LINE__
, query_cmd
);
471 if (retval
!= JIM_OK
)
474 Jim_Obj
*list
= Jim_GetResult(interp
);
475 Jim_IncrRefCount(list
);
477 int len
= Jim_ListLength(interp
, list
);
478 for (int i
= 0; i
< len
; i
++) {
479 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, list
, i
);
480 Jim_IncrRefCount(elem
);
482 const char *name
= Jim_GetString(elem
, NULL
);
483 struct command
*c
= command_find_from_name(interp
, name
);
485 /* not openocd command */
486 Jim_DecrRefCount(interp
, elem
);
489 LOG_DEBUG("delete command \"%s\"", name
);
490 Jim_DeleteCommand(interp
, elem
);
492 help_del_command(cmd_ctx
, name
);
494 Jim_DecrRefCount(interp
, elem
);
497 Jim_DecrRefCount(interp
, list
);
501 int unregister_all_commands(struct command_context
*context
,
502 const char *cmd_prefix
)
504 struct command
*parent
= NULL
;
509 if (!cmd_prefix
|| !*cmd_prefix
) {
510 int retval
= unregister_commands_match(context
, "*");
511 if (retval
!= ERROR_OK
)
514 Jim_Cmd
*cmd
= Jim_GetCommand(context
->interp
, Jim_NewStringObj(context
->interp
, cmd_prefix
, -1), JIM_NONE
);
515 if (cmd
&& jimcmd_is_ocd_command(cmd
))
516 parent
= jimcmd_privdata(cmd
);
518 int retval
= unregister_commands_match(context
, "%s *", cmd_prefix
);
519 if (retval
!= ERROR_OK
)
521 retval
= unregister_commands_match(context
, "%s", cmd_prefix
);
522 if (retval
!= ERROR_OK
)
526 struct command
**head
= command_list_for_parent(context
, parent
);
527 while (NULL
!= *head
) {
528 struct command
*tmp
= *head
;
536 static int unregister_command(struct command_context
*context
,
537 struct command
*parent
, const char *name
)
539 if ((!context
) || (!name
))
540 return ERROR_COMMAND_SYNTAX_ERROR
;
542 struct command
*p
= NULL
;
543 struct command
**head
= command_list_for_parent(context
, parent
);
544 for (struct command
*c
= *head
; NULL
!= c
; p
= c
, c
= c
->next
) {
545 if (strcmp(name
, c
->name
) != 0)
548 char *full_name
= command_name(c
, ' ');
550 int retval
= unregister_commands_match(context
, "%s", full_name
);
551 if (retval
!= ERROR_OK
)
568 void command_output_text(struct command_context
*context
, const char *data
)
570 if (context
&& context
->output_handler
&& data
)
571 context
->output_handler(context
, data
);
574 void command_print_sameline(struct command_invocation
*cmd
, const char *format
, ...)
579 va_start(ap
, format
);
581 string
= alloc_vprintf(format
, ap
);
582 if (string
!= NULL
&& cmd
) {
583 /* we want this collected in the log + we also want to pick it up as a tcl return
586 * The latter bit isn't precisely neat, but will do for now.
588 Jim_AppendString(cmd
->ctx
->interp
, cmd
->output
, string
, -1);
589 /* We already printed it above
590 * command_output_text(context, string); */
597 void command_print(struct command_invocation
*cmd
, const char *format
, ...)
602 va_start(ap
, format
);
604 string
= alloc_vprintf(format
, ap
);
605 if (string
!= NULL
&& cmd
) {
606 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one
608 /* we want this collected in the log + we also want to pick it up as a tcl return
611 * The latter bit isn't precisely neat, but will do for now.
613 Jim_AppendString(cmd
->ctx
->interp
, cmd
->output
, string
, -1);
614 /* We already printed it above
615 * command_output_text(context, string); */
622 static char *__command_name(struct command
*c
, char delim
, unsigned extra
)
625 unsigned len
= strlen(c
->name
);
626 if (NULL
== c
->parent
) {
627 /* allocate enough for the name, child names, and '\0' */
628 name
= malloc(len
+ extra
+ 1);
630 LOG_ERROR("Out of memory");
633 strcpy(name
, c
->name
);
635 /* parent's extra must include both the space and name */
636 name
= __command_name(c
->parent
, delim
, 1 + len
+ extra
);
637 char dstr
[2] = { delim
, 0 };
639 strcat(name
, c
->name
);
644 static char *command_name(struct command
*c
, char delim
)
646 return __command_name(c
, delim
, 0);
649 static bool command_can_run(struct command_context
*cmd_ctx
, struct command
*c
, const char *full_name
)
651 if (c
->mode
== COMMAND_ANY
|| c
->mode
== cmd_ctx
->mode
)
654 /* Many commands may be run only before/after 'init' */
663 /* handle the impossible with humor; it guarantees a bug report! */
665 when
= "if Cthulhu is summoned by";
668 LOG_ERROR("The '%s' command must be used %s 'init'.",
669 full_name
? full_name
: c
->name
, when
);
673 static int run_command(struct command_context
*context
,
674 struct command
*c
, const char **words
, unsigned num_words
)
676 struct command_invocation cmd
= {
680 .argc
= num_words
- 1,
684 cmd
.output
= Jim_NewEmptyStringObj(context
->interp
);
685 Jim_IncrRefCount(cmd
.output
);
687 int retval
= c
->handler(&cmd
);
688 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
) {
689 /* Print help for command */
690 command_run_linef(context
, "usage %s", words
[0]);
691 } else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
) {
692 /* just fall through for a shutdown request */
694 if (retval
!= ERROR_OK
)
695 LOG_DEBUG("Command '%s' failed with error code %d",
697 /* Use the command output as the Tcl result */
698 Jim_SetResult(context
->interp
, cmd
.output
);
700 Jim_DecrRefCount(context
->interp
, cmd
.output
);
705 int command_run_line(struct command_context
*context
, char *line
)
707 /* all the parent commands have been registered with the interpreter
708 * so, can just evaluate the line as a script and check for
711 /* run the line thru a script engine */
712 int retval
= ERROR_FAIL
;
714 /* Beware! This code needs to be reentrant. It is also possible
715 * for OpenOCD commands to be invoked directly from Tcl. This would
716 * happen when the Jim Tcl interpreter is provided by eCos for
719 struct target
*saved_target_override
= context
->current_target_override
;
720 context
->current_target_override
= NULL
;
722 Jim_Interp
*interp
= context
->interp
;
723 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
724 Jim_DeleteAssocData(interp
, "context");
725 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
726 if (retcode
== JIM_OK
) {
727 /* associated the return value */
728 Jim_DeleteAssocData(interp
, "retval");
729 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
730 if (retcode
== JIM_OK
) {
731 retcode
= Jim_Eval_Named(interp
, line
, 0, 0);
733 Jim_DeleteAssocData(interp
, "retval");
735 Jim_DeleteAssocData(interp
, "context");
736 int inner_retcode
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
737 if (retcode
== JIM_OK
)
738 retcode
= inner_retcode
;
740 context
->current_target_override
= saved_target_override
;
741 if (retcode
== JIM_OK
) {
745 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
747 command_output_text(context
, result
);
748 command_output_text(context
, "\n");
751 } else if (retcode
== JIM_EXIT
) {
753 * exit(Jim_GetExitCode(interp)); */
754 } else if (retcode
== ERROR_COMMAND_CLOSE_CONNECTION
) {
757 Jim_MakeErrorMessage(interp
);
758 /* error is broadcast */
759 LOG_USER("%s", Jim_GetString(Jim_GetResult(interp
), NULL
));
761 if (retval
== ERROR_OK
) {
762 /* It wasn't a low level OpenOCD command that failed */
771 int command_run_linef(struct command_context
*context
, const char *format
, ...)
773 int retval
= ERROR_FAIL
;
776 va_start(ap
, format
);
777 string
= alloc_vprintf(format
, ap
);
778 if (string
!= NULL
) {
779 retval
= command_run_line(context
, string
);
786 void command_set_output_handler(struct command_context
*context
,
787 command_output_handler_t output_handler
, void *priv
)
789 context
->output_handler
= output_handler
;
790 context
->output_handler_priv
= priv
;
793 struct command_context
*copy_command_context(struct command_context
*context
)
795 struct command_context
*copy_context
= malloc(sizeof(struct command_context
));
797 *copy_context
= *context
;
802 void command_done(struct command_context
*cmd_ctx
)
810 /* find full path to file */
811 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
815 const char *file
= Jim_GetString(argv
[1], NULL
);
816 char *full_path
= find_file(file
);
817 if (full_path
== NULL
)
819 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
822 Jim_SetResult(interp
, result
);
826 COMMAND_HANDLER(jim_echo
)
828 if (CMD_ARGC
== 2 && !strcmp(CMD_ARGV
[0], "-n")) {
829 LOG_USER_N("%s", CMD_ARGV
[1]);
834 LOG_USER("%s", CMD_ARGV
[0]);
838 /* Capture progress output and return as tcl return value. If the
839 * progress output was empty, return tcl return value.
841 static int jim_capture(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
846 struct log_capture_state
*state
= command_log_capture_start(interp
);
848 /* disable polling during capture. This avoids capturing output
851 * This is necessary in order to avoid accidentally getting a non-empty
852 * string for tcl fn's.
854 bool save_poll
= jtag_poll_get_enabled();
856 jtag_poll_set_enabled(false);
858 const char *str
= Jim_GetString(argv
[1], NULL
);
859 int retcode
= Jim_Eval_Named(interp
, str
, __THIS__FILE__
, __LINE__
);
861 jtag_poll_set_enabled(save_poll
);
863 command_log_capture_finish(state
);
875 static COMMAND_HELPER(command_help_show
, struct help_entry
*c
,
876 bool show_help
, const char *cmd_match
);
878 static COMMAND_HELPER(command_help_show_list
, bool show_help
, const char *cmd_match
)
880 struct help_entry
*entry
;
882 list_for_each_entry(entry
, CMD_CTX
->help_list
, lh
)
883 CALL_COMMAND_HANDLER(command_help_show
, entry
, show_help
, cmd_match
);
887 #define HELP_LINE_WIDTH(_n) (int)(76 - (2 * _n))
889 static void command_help_show_indent(unsigned n
)
891 for (unsigned i
= 0; i
< n
; i
++)
894 static void command_help_show_wrap(const char *str
, unsigned n
, unsigned n2
)
896 const char *cp
= str
, *last
= str
;
898 const char *next
= last
;
903 } while (*next
!= ' ' && *next
!= '\t' && *next
!= '\0');
904 } while ((next
- last
< HELP_LINE_WIDTH(n
)) && *next
!= '\0');
905 if (next
- last
< HELP_LINE_WIDTH(n
))
907 command_help_show_indent(n
);
908 LOG_USER("%.*s", (int)(cp
- last
), last
);
914 static COMMAND_HELPER(command_help_show
, struct help_entry
*c
,
915 bool show_help
, const char *cmd_match
)
918 for (const char *s
= strchr(c
->cmd_name
, ' '); s
; s
= strchr(s
+ 1, ' '))
921 /* If the match string occurs anywhere, we print out
922 * stuff for this command. */
923 bool is_match
= (strstr(c
->cmd_name
, cmd_match
) != NULL
) ||
924 ((c
->usage
!= NULL
) && (strstr(c
->usage
, cmd_match
) != NULL
)) ||
925 ((c
->help
!= NULL
) && (strstr(c
->help
, cmd_match
) != NULL
));
928 command_help_show_indent(n
);
929 LOG_USER_N("%s", c
->cmd_name
);
931 if (c
->usage
&& strlen(c
->usage
) > 0) {
933 command_help_show_wrap(c
->usage
, 0, n
+ 5);
938 if (is_match
&& show_help
) {
941 /* TODO: factorize jim_command_mode() to avoid running jim command here */
942 char *request
= alloc_printf("command mode %s", c
->cmd_name
);
944 LOG_ERROR("Out of memory");
947 int retval
= Jim_Eval(CMD_CTX
->interp
, request
);
949 enum command_mode mode
= COMMAND_UNKNOWN
;
950 if (retval
!= JIM_ERR
) {
951 const char *result
= Jim_GetString(Jim_GetResult(CMD_CTX
->interp
), NULL
);
952 if (!strcmp(result
, "any"))
954 else if (!strcmp(result
, "config"))
955 mode
= COMMAND_CONFIG
;
956 else if (!strcmp(result
, "exec"))
960 /* Normal commands are runtime-only; highlight exceptions */
961 if (mode
!= COMMAND_EXEC
) {
962 const char *stage_msg
= "";
966 stage_msg
= " (configuration command)";
969 stage_msg
= " (command valid any time)";
972 stage_msg
= " (?mode error?)";
975 msg
= alloc_printf("%s%s", c
->help
? : "", stage_msg
);
977 msg
= alloc_printf("%s", c
->help
? : "");
980 command_help_show_wrap(msg
, n
+ 3, n
+ 3);
989 COMMAND_HANDLER(handle_help_command
)
991 bool full
= strcmp(CMD_NAME
, "help") == 0;
996 cmd_match
= strdup("");
999 cmd_match
= strdup(CMD_ARGV
[0]);
1001 for (unsigned int i
= 1; i
< CMD_ARGC
&& cmd_match
; ++i
) {
1002 char *prev
= cmd_match
;
1003 cmd_match
= alloc_printf("%s %s", prev
, CMD_ARGV
[i
]);
1008 if (cmd_match
== NULL
) {
1009 LOG_ERROR("unable to build search string");
1012 retval
= CALL_COMMAND_HANDLER(command_help_show_list
, full
, cmd_match
);
1018 static char *alloc_concatenate_strings(int argc
, Jim_Obj
* const *argv
)
1025 all
= strdup(Jim_GetString(argv
[0], NULL
));
1027 LOG_ERROR("Out of memory");
1031 for (i
= 1; i
< argc
; ++i
) {
1033 all
= alloc_printf("%s %s", all
, Jim_GetString(argv
[i
], NULL
));
1036 LOG_ERROR("Out of memory");
1044 static int exec_command(Jim_Interp
*interp
, struct command_context
*cmd_ctx
,
1045 struct command
*c
, int argc
, Jim_Obj
* const *argv
)
1048 return c
->jim_handler(interp
, argc
, argv
);
1050 /* use c->handler */
1051 unsigned int nwords
;
1052 char **words
= script_command_args_alloc(argc
, argv
, &nwords
);
1056 int retval
= run_command(cmd_ctx
, c
, (const char **)words
, nwords
);
1057 script_command_args_free(words
, nwords
);
1058 return command_retval_set(interp
, retval
);
1061 static int command_unknown(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1063 script_debug(interp
, argc
, argv
);
1065 /* check subcommands */
1067 char *s
= alloc_printf("%s %s", Jim_GetString(argv
[0], NULL
), Jim_GetString(argv
[1], NULL
));
1068 Jim_Obj
*js
= Jim_NewStringObj(interp
, s
, -1);
1069 Jim_IncrRefCount(js
);
1071 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, js
, JIM_NONE
);
1073 int retval
= Jim_EvalObjPrefix(interp
, js
, argc
- 2, argv
+ 2);
1074 Jim_DecrRefCount(interp
, js
);
1077 Jim_DecrRefCount(interp
, js
);
1080 struct command
*c
= jim_to_command(interp
);
1081 if (!c
->jim_handler
&& !c
->handler
) {
1082 Jim_EvalObjPrefix(interp
, Jim_NewStringObj(interp
, "usage", -1), 1, argv
);
1086 struct command_context
*cmd_ctx
= current_command_context(interp
);
1088 if (!command_can_run(cmd_ctx
, c
, Jim_GetString(argv
[0], NULL
)))
1091 target_call_timer_callbacks_now();
1094 * Black magic of overridden current target:
1095 * If the command we are going to handle has a target prefix,
1096 * override the current target temporarily for the time
1097 * of processing the command.
1098 * current_target_override is used also for event handlers
1099 * therefore we prevent touching it if command has no prefix.
1100 * Previous override is saved and restored back to ensure
1101 * correct work when command_unknown() is re-entered.
1103 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
1104 if (c
->jim_override_target
)
1105 cmd_ctx
->current_target_override
= c
->jim_override_target
;
1107 int retval
= exec_command(interp
, cmd_ctx
, c
, argc
, argv
);
1109 if (c
->jim_override_target
)
1110 cmd_ctx
->current_target_override
= saved_target_override
;
1115 static int jim_command_mode(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
1117 struct command_context
*cmd_ctx
= current_command_context(interp
);
1118 enum command_mode mode
;
1121 char *full_name
= alloc_concatenate_strings(argc
- 1, argv
+ 1);
1124 Jim_Obj
*s
= Jim_NewStringObj(interp
, full_name
, -1);
1125 Jim_IncrRefCount(s
);
1126 Jim_Cmd
*cmd
= Jim_GetCommand(interp
, s
, JIM_NONE
);
1127 Jim_DecrRefCount(interp
, s
);
1129 if (!cmd
|| !(jimcmd_is_proc(cmd
) || jimcmd_is_ocd_command(cmd
))) {
1130 Jim_SetResultString(interp
, "unknown", -1);
1134 if (jimcmd_is_proc(cmd
)) {
1138 struct command
*c
= jimcmd_privdata(cmd
);
1143 mode
= cmd_ctx
->mode
;
1145 const char *mode_str
;
1150 case COMMAND_CONFIG
:
1151 mode_str
= "config";
1157 mode_str
= "unknown";
1160 Jim_SetResultString(interp
, mode_str
, -1);
1164 int help_del_all_commands(struct command_context
*cmd_ctx
)
1166 struct help_entry
*curr
, *n
;
1168 list_for_each_entry_safe(curr
, n
, cmd_ctx
->help_list
, lh
) {
1169 list_del(&curr
->lh
);
1170 free(curr
->cmd_name
);
1178 static int help_del_command(struct command_context
*cmd_ctx
, const char *cmd_name
)
1180 struct help_entry
*curr
;
1182 list_for_each_entry(curr
, cmd_ctx
->help_list
, lh
) {
1183 if (!strcmp(cmd_name
, curr
->cmd_name
)) {
1184 list_del(&curr
->lh
);
1185 free(curr
->cmd_name
);
1196 static int help_add_command(struct command_context
*cmd_ctx
,
1197 const char *cmd_name
, const char *help_text
, const char *usage_text
)
1199 int cmp
= -1; /* add after curr */
1200 struct help_entry
*curr
;
1202 list_for_each_entry_reverse(curr
, cmd_ctx
->help_list
, lh
) {
1203 cmp
= strcmp(cmd_name
, curr
->cmd_name
);
1208 struct help_entry
*entry
;
1210 entry
= calloc(1, sizeof(*entry
));
1212 LOG_ERROR("Out of memory");
1215 entry
->cmd_name
= strdup(cmd_name
);
1216 if (!entry
->cmd_name
) {
1217 LOG_ERROR("Out of memory");
1221 list_add(&entry
->lh
, &curr
->lh
);
1227 char *text
= strdup(help_text
);
1229 LOG_ERROR("Out of memory");
1237 char *text
= strdup(usage_text
);
1239 LOG_ERROR("Out of memory");
1243 entry
->usage
= text
;
1249 COMMAND_HANDLER(handle_help_add_command
)
1252 return ERROR_COMMAND_SYNTAX_ERROR
;
1254 const char *help
= !strcmp(CMD_NAME
, "add_help_text") ? CMD_ARGV
[1] : NULL
;
1255 const char *usage
= !strcmp(CMD_NAME
, "add_usage_text") ? CMD_ARGV
[1] : NULL
;
1256 if (!help
&& !usage
) {
1257 LOG_ERROR("command name '%s' is unknown", CMD_NAME
);
1258 return ERROR_COMMAND_SYNTAX_ERROR
;
1260 const char *cmd_name
= CMD_ARGV
[0];
1261 return help_add_command(CMD_CTX
, cmd_name
, help
, usage
);
1264 /* sleep command sleeps for <n> milliseconds
1265 * this is useful in target startup scripts
1267 COMMAND_HANDLER(handle_sleep_command
)
1270 if (CMD_ARGC
== 2) {
1271 if (strcmp(CMD_ARGV
[1], "busy") == 0)
1274 return ERROR_COMMAND_SYNTAX_ERROR
;
1275 } else if (CMD_ARGC
< 1 || CMD_ARGC
> 2)
1276 return ERROR_COMMAND_SYNTAX_ERROR
;
1278 unsigned long duration
= 0;
1279 int retval
= parse_ulong(CMD_ARGV
[0], &duration
);
1280 if (ERROR_OK
!= retval
)
1284 int64_t then
= timeval_ms();
1285 while (timeval_ms() - then
< (int64_t)duration
) {
1286 target_call_timer_callbacks_now();
1290 busy_sleep(duration
);
1295 static const struct command_registration command_subcommand_handlers
[] = {
1298 .mode
= COMMAND_ANY
,
1299 .jim_handler
= jim_command_mode
,
1300 .usage
= "[command_name ...]",
1301 .help
= "Returns the command modes allowed by a command: "
1302 "'any', 'config', or 'exec'. If no command is "
1303 "specified, returns the current command mode. "
1304 "Returns 'unknown' if an unknown command is given. "
1305 "Command can be multiple tokens.",
1307 COMMAND_REGISTRATION_DONE
1310 static const struct command_registration command_builtin_handlers
[] = {
1313 .mode
= COMMAND_ANY
,
1314 .jim_handler
= jim_find
,
1315 .help
= "find full path to file",
1320 .mode
= COMMAND_ANY
,
1321 .jim_handler
= jim_capture
,
1322 .help
= "Capture progress output and return as tcl return value. If the "
1323 "progress output was empty, return tcl return value.",
1328 .handler
= jim_echo
,
1329 .mode
= COMMAND_ANY
,
1330 .help
= "Logs a message at \"user\" priority. "
1331 "Output message to stdout. "
1332 "Option \"-n\" suppresses trailing newline",
1333 .usage
= "[-n] string",
1336 .name
= "add_help_text",
1337 .handler
= handle_help_add_command
,
1338 .mode
= COMMAND_ANY
,
1339 .help
= "Add new command help text; "
1340 "Command can be multiple tokens.",
1341 .usage
= "command_name helptext_string",
1344 .name
= "add_usage_text",
1345 .handler
= handle_help_add_command
,
1346 .mode
= COMMAND_ANY
,
1347 .help
= "Add new command usage text; "
1348 "command can be multiple tokens.",
1349 .usage
= "command_name usage_string",
1353 .handler
= handle_sleep_command
,
1354 .mode
= COMMAND_ANY
,
1355 .help
= "Sleep for specified number of milliseconds. "
1356 "\"busy\" will busy wait instead (avoid this).",
1357 .usage
= "milliseconds ['busy']",
1361 .handler
= handle_help_command
,
1362 .mode
= COMMAND_ANY
,
1363 .help
= "Show full command help; "
1364 "command can be multiple tokens.",
1365 .usage
= "[command_name]",
1369 .handler
= handle_help_command
,
1370 .mode
= COMMAND_ANY
,
1371 .help
= "Show basic command usage; "
1372 "command can be multiple tokens.",
1373 .usage
= "[command_name]",
1377 .mode
= COMMAND_ANY
,
1378 .help
= "core command group (introspection)",
1379 .chain
= command_subcommand_handlers
,
1382 COMMAND_REGISTRATION_DONE
1385 struct command_context
*command_init(const char *startup_tcl
, Jim_Interp
*interp
)
1387 struct command_context
*context
= calloc(1, sizeof(struct command_context
));
1390 context
->mode
= COMMAND_EXEC
;
1392 /* context can be duplicated. Put list head on separate mem-chunk to keep list consistent */
1393 context
->help_list
= malloc(sizeof(*context
->help_list
));
1394 INIT_LIST_HEAD(context
->help_list
);
1396 /* Create a jim interpreter if we were not handed one */
1397 if (interp
== NULL
) {
1398 /* Create an interpreter */
1399 interp
= Jim_CreateInterp();
1400 /* Add all the Jim core commands */
1401 Jim_RegisterCoreCommands(interp
);
1402 Jim_InitStaticExtensions(interp
);
1405 context
->interp
= interp
;
1407 /* Stick to lowercase for HostOS strings. */
1408 #if defined(_MSC_VER)
1409 /* WinXX - is generic, the forward
1410 * looking problem is this:
1412 * "win32" or "win64"
1414 * "winxx" is generic.
1417 #elif defined(__linux__)
1419 #elif defined(__APPLE__) || defined(__DARWIN__)
1421 #elif defined(__CYGWIN__)
1423 #elif defined(__MINGW32__)
1425 #elif defined(__ECOS)
1427 #elif defined(__FreeBSD__)
1429 #elif defined(__NetBSD__)
1431 #elif defined(__OpenBSD__)
1434 #warning "Unrecognized host OS..."
1437 Jim_SetGlobalVariableStr(interp
, "ocd_HOSTOS",
1438 Jim_NewStringObj(interp
, HostOs
, strlen(HostOs
)));
1440 register_commands(context
, NULL
, command_builtin_handlers
);
1442 Jim_SetAssocData(interp
, "context", NULL
, context
);
1443 if (Jim_Eval_Named(interp
, startup_tcl
, "embedded:startup.tcl", 1) == JIM_ERR
) {
1444 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD)");
1445 Jim_MakeErrorMessage(interp
);
1446 LOG_USER_N("%s", Jim_GetString(Jim_GetResult(interp
), NULL
));
1449 Jim_DeleteAssocData(interp
, "context");
1454 void command_exit(struct command_context
*context
)
1459 Jim_FreeInterp(context
->interp
);
1460 free(context
->help_list
);
1461 command_done(context
);
1464 int command_context_mode(struct command_context
*cmd_ctx
, enum command_mode mode
)
1467 return ERROR_COMMAND_SYNTAX_ERROR
;
1469 cmd_ctx
->mode
= mode
;
1473 void process_jim_events(struct command_context
*cmd_ctx
)
1475 static int recursion
;
1480 Jim_ProcessEvents(cmd_ctx
->interp
, JIM_ALL_EVENTS
| JIM_DONT_WAIT
);
1484 #define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max) \
1485 int parse ## name(const char *str, type * ul) \
1488 LOG_ERROR("Invalid command argument"); \
1489 return ERROR_COMMAND_ARGUMENT_INVALID; \
1493 *ul = func(str, &end, 0); \
1495 LOG_ERROR("Invalid command argument"); \
1496 return ERROR_COMMAND_ARGUMENT_INVALID; \
1498 if ((max == *ul) && (ERANGE == errno)) { \
1499 LOG_ERROR("Argument overflow"); \
1500 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1502 if (min && (min == *ul) && (ERANGE == errno)) { \
1503 LOG_ERROR("Argument underflow"); \
1504 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1508 DEFINE_PARSE_NUM_TYPE(_ulong
, unsigned long, strtoul
, 0, ULONG_MAX
)
1509 DEFINE_PARSE_NUM_TYPE(_ullong
, unsigned long long, strtoull
, 0, ULLONG_MAX
)
1510 DEFINE_PARSE_NUM_TYPE(_long
, long, strtol
, LONG_MIN
, LONG_MAX
)
1511 DEFINE_PARSE_NUM_TYPE(_llong
, long long, strtoll
, LLONG_MIN
, LLONG_MAX
)
1513 #define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
1514 int parse ## name(const char *str, type * ul) \
1517 int retval = parse ## funcname(str, &n); \
1518 if (ERROR_OK != retval) \
1521 return ERROR_COMMAND_ARGUMENT_OVERFLOW; \
1523 return ERROR_COMMAND_ARGUMENT_UNDERFLOW; \
1528 #define DEFINE_PARSE_ULONGLONG(name, type, min, max) \
1529 DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
1530 DEFINE_PARSE_ULONGLONG(_uint
, unsigned, 0, UINT_MAX
)
1531 DEFINE_PARSE_ULONGLONG(_u64
, uint64_t, 0, UINT64_MAX
)
1532 DEFINE_PARSE_ULONGLONG(_u32
, uint32_t, 0, UINT32_MAX
)
1533 DEFINE_PARSE_ULONGLONG(_u16
, uint16_t, 0, UINT16_MAX
)
1534 DEFINE_PARSE_ULONGLONG(_u8
, uint8_t, 0, UINT8_MAX
)
1536 DEFINE_PARSE_ULONGLONG(_target_addr
, target_addr_t
, 0, TARGET_ADDR_MAX
)
1538 #define DEFINE_PARSE_LONGLONG(name, type, min, max) \
1539 DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
1540 DEFINE_PARSE_LONGLONG(_int
, int, n
< INT_MIN
, INT_MAX
)
1541 DEFINE_PARSE_LONGLONG(_s64
, int64_t, n
< INT64_MIN
, INT64_MAX
)
1542 DEFINE_PARSE_LONGLONG(_s32
, int32_t, n
< INT32_MIN
, INT32_MAX
)
1543 DEFINE_PARSE_LONGLONG(_s16
, int16_t, n
< INT16_MIN
, INT16_MAX
)
1544 DEFINE_PARSE_LONGLONG(_s8
, int8_t, n
< INT8_MIN
, INT8_MAX
)
1546 static int command_parse_bool(const char *in
, bool *out
,
1547 const char *on
, const char *off
)
1549 if (strcasecmp(in
, on
) == 0)
1551 else if (strcasecmp(in
, off
) == 0)
1554 return ERROR_COMMAND_SYNTAX_ERROR
;
1558 int command_parse_bool_arg(const char *in
, bool *out
)
1560 if (command_parse_bool(in
, out
, "on", "off") == ERROR_OK
)
1562 if (command_parse_bool(in
, out
, "enable", "disable") == ERROR_OK
)
1564 if (command_parse_bool(in
, out
, "true", "false") == ERROR_OK
)
1566 if (command_parse_bool(in
, out
, "yes", "no") == ERROR_OK
)
1568 if (command_parse_bool(in
, out
, "1", "0") == ERROR_OK
)
1570 return ERROR_COMMAND_SYNTAX_ERROR
;
1573 COMMAND_HELPER(handle_command_parse_bool
, bool *out
, const char *label
)
1577 const char *in
= CMD_ARGV
[0];
1578 if (command_parse_bool_arg(in
, out
) != ERROR_OK
) {
1579 LOG_ERROR("%s: argument '%s' is not valid", CMD_NAME
, in
);
1580 return ERROR_COMMAND_SYNTAX_ERROR
;
1585 LOG_INFO("%s is %s", label
, *out
? "enabled" : "disabled");
1588 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)