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, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
33 #include "replacements.h"
36 #include "configuration.h"
39 #include "time_support.h"
40 #include "jim-eventloop.h"
50 int fast_and_dangerous
= 0;
51 Jim_Interp
*interp
= NULL
;
53 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
54 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
56 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
);
58 static void tcl_output(void *privData
, const char *file
, int line
, const char *function
, const char *string
)
60 Jim_Obj
*tclOutput
=(Jim_Obj
*)privData
;
62 Jim_AppendString(interp
, tclOutput
, string
, strlen(string
));
65 extern command_context_t
*global_cmd_ctx
;
67 static int script_command(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
69 /* the private data is stashed in the interp structure */
71 command_context_t
*context
;
77 /* DANGER!!!! be careful what we invoke here, since interp->cmdPrivData might
78 * get overwritten by running other Jim commands! Treat it as an
79 * emphemeral global variable that is used in lieu of an argument
80 * to the fn and fish it out manually.
82 c
= interp
->cmdPrivData
;
85 LOG_ERROR("BUG: interp->cmdPrivData==NULL");
88 target_call_timer_callbacks_now();
89 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
91 LOG_DEBUG("script_command - %s", c
->name
);
93 words
= malloc(sizeof(char *) * argc
);
94 for (i
= 0; i
< argc
; i
++)
97 const char *w
=Jim_GetString(argv
[i
], &len
);
100 /* hit an end of line comment */
103 words
[i
] = strdup(w
);
104 if (words
[i
] == NULL
)
108 LOG_DEBUG("script_command - %s, argv[%u]=%s", c
->name
, i
, words
[i
]);
112 /* grab the command context from the associated data */
113 context
= Jim_GetAssocData(interp
, "context");
116 /* Tcl can invoke commands directly instead of via command_run_line(). This would
117 * happen when the Jim Tcl interpreter is provided by eCos.
119 context
= global_cmd_ctx
;
122 /* capture log output and return it */
123 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
124 /* a garbage collect can happen, so we need a reference count to this object */
125 Jim_IncrRefCount(tclOutput
);
127 log_add_callback(tcl_output
, tclOutput
);
129 retval
= run_command(context
, c
, words
, nwords
);
131 log_remove_callback(tcl_output
, tclOutput
);
133 /* We dump output into this local variable */
134 Jim_SetResult(interp
, tclOutput
);
135 Jim_DecrRefCount(interp
, tclOutput
);
137 for (i
= 0; i
< nwords
; i
++)
141 int *return_retval
= Jim_GetAssocData(interp
, "retval");
142 if (return_retval
!= NULL
)
144 *return_retval
= retval
;
147 return (retval
==ERROR_OK
)?JIM_OK
:JIM_ERR
;
150 command_t
* register_command(command_context_t
*context
, command_t
*parent
, char *name
, int (*handler
)(struct command_context_s
*context
, char* name
, char** args
, int argc
), enum command_mode mode
, char *help
)
154 if (!context
|| !name
)
157 c
= malloc(sizeof(command_t
));
159 c
->name
= strdup(name
);
162 c
->handler
= handler
;
168 /* place command in tree */
171 if (parent
->children
)
173 /* find last child */
174 for (p
= parent
->children
; p
&& p
->next
; p
= p
->next
);
180 parent
->children
= c
;
185 if (context
->commands
)
187 /* find last command */
188 for (p
= context
->commands
; p
&& p
->next
; p
= p
->next
);
194 context
->commands
= c
;
198 /* just a placeholder, no handler */
199 if (c
->handler
==NULL
)
202 /* If this is a two level command, e.g. "flash banks", then the
203 * "unknown" proc in startup.tcl must redirect to this command.
205 * "flash banks" is translated by "unknown" to "flash_banks"
206 * if such a proc exists
208 /* Print help for command */
212 /* maximum of two levels :-) */
219 const char *full_name
=alloc_printf("ocd_%s%s%s", t1
, t2
, t3
);
220 Jim_CreateCommand(interp
, full_name
, script_command
, c
, NULL
);
221 free((void *)full_name
);
223 /* we now need to add an overrideable proc */
224 const char *override_name
=alloc_printf("proc %s%s%s {args} {if {[catch {eval \"ocd_%s%s%s $args\"}]==0} {return \"\"} else { return -code error }", t1
, t2
, t3
, t1
, t2
, t3
);
225 Jim_Eval_Named(interp
, override_name
, __FILE__
, __LINE__
);
226 free((void *)override_name
);
228 /* accumulate help text in Tcl helptext list. */
229 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
230 if (Jim_IsShared(helptext
))
231 helptext
= Jim_DuplicateObj(interp
, helptext
);
232 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
234 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
236 /* maximum of two levels :-) */
239 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->parent
->name
, -1));
241 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->name
, -1));
243 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
244 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
245 Jim_ListAppendElement(interp
, helptext
, cmd_entry
);
249 int unregister_all_commands(command_context_t
*context
)
256 while(NULL
!= context
->commands
)
258 c
= context
->commands
;
260 while(NULL
!= c
->children
)
263 c
->children
= c
->children
->next
;
270 context
->commands
= context
->commands
->next
;
281 int unregister_command(command_context_t
*context
, char *name
)
283 command_t
*c
, *p
= NULL
, *c2
;
285 if ((!context
) || (!name
))
286 return ERROR_INVALID_ARGUMENTS
;
289 for (c
= context
->commands
; c
; c
= c
->next
)
291 if (strcmp(name
, c
->name
) == 0)
300 context
->commands
= c
->next
;
303 /* unregister children */
306 for (c2
= c
->children
; c2
; c2
= c2
->next
)
318 /* remember the last command for unlinking */
325 void command_output_text(command_context_t
*context
, const char *data
)
327 if( context
&& context
->output_handler
&& data
){
328 context
->output_handler( context
, data
);
332 void command_print_n(command_context_t
*context
, char *format
, ...)
337 va_start(ap
, format
);
339 string
= alloc_vprintf(format
, ap
);
342 /* we want this collected in the log + we also want to pick it up as a tcl return
345 * The latter bit isn't precisely neat, but will do for now.
347 LOG_USER_N("%s", string
);
348 // We already printed it above
349 //command_output_text(context, string);
356 void command_print(command_context_t
*context
, char *format
, ...)
361 va_start(ap
, format
);
363 string
= alloc_vprintf(format
, ap
);
366 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
367 /* we want this collected in the log + we also want to pick it up as a tcl return
370 * The latter bit isn't precisely neat, but will do for now.
372 LOG_USER_N("%s", string
);
373 // We already printed it above
374 //command_output_text(context, string);
381 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
)
384 if (!((context
->mode
== COMMAND_CONFIG
) || (c
->mode
== COMMAND_ANY
) || (c
->mode
== context
->mode
) ))
386 /* Config commands can not run after the config stage */
387 LOG_ERROR("Illegal mode for command");
391 int retval
= c
->handler(context
, c
->name
, words
+ start_word
+ 1, num_words
- start_word
- 1);
392 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
)
394 /* Print help for command */
398 /* maximum of two levels :-) */
405 command_run_linef(context
, "help {%s%s%s}", t1
, t2
, t3
);
407 else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
409 /* just fall through for a shutdown request */
411 else if (retval
!= ERROR_OK
)
413 /* we do not print out an error message because the command *should*
414 * have printed out an error
416 LOG_DEBUG("Command failed with error code %d", retval
);
422 int command_run_line(command_context_t
*context
, char *line
)
424 /* all the parent commands have been registered with the interpreter
425 * so, can just evaluate the line as a script and check for
428 /* run the line thru a script engine */
429 int retval
=ERROR_FAIL
;
431 /* Beware! This code needs to be reentrant. It is also possible
432 * for OpenOCD commands to be invoked directly from Tcl. This would
433 * happen when the Jim Tcl interpreter is provided by eCos for
436 Jim_DeleteAssocData(interp
, "context");
437 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
438 if (retcode
== JIM_OK
)
440 /* associated the return value */
441 Jim_DeleteAssocData(interp
, "retval");
442 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
443 if (retcode
== JIM_OK
)
445 retcode
= Jim_Eval_Named(interp
, line
, __FILE__
, __LINE__
);
447 Jim_DeleteAssocData(interp
, "retval");
449 Jim_DeleteAssocData(interp
, "context");
451 if (retcode
== JIM_ERR
) {
452 if (retval
!=ERROR_COMMAND_CLOSE_CONNECTION
)
454 /* We do not print the connection closed error message */
455 Jim_PrintErrorMessage(interp
);
457 if (retval
==ERROR_OK
)
459 /* It wasn't a low level OpenOCD command that failed */
463 } else if (retcode
== JIM_EXIT
) {
465 /* exit(Jim_GetExitCode(interp)); */
470 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
474 for (i
= 0; i
< reslen
; i
+= 256)
480 strncpy(buff
, result
+i
, chunk
);
482 LOG_USER_N("%s", buff
);
484 LOG_USER_N("%s", "\n");
490 int command_run_linef(command_context_t
*context
, char *format
, ...)
492 int retval
=ERROR_FAIL
;
495 va_start(ap
, format
);
496 string
= alloc_vprintf(format
, ap
);
499 retval
=command_run_line(context
, string
);
505 void command_set_output_handler(command_context_t
* context
, int (*output_handler
)(struct command_context_s
*context
, const char* line
), void *priv
)
507 context
->output_handler
= output_handler
;
508 context
->output_handler_priv
= priv
;
511 command_context_t
* copy_command_context(command_context_t
* context
)
513 command_context_t
* copy_context
= malloc(sizeof(command_context_t
));
515 *copy_context
= *context
;
520 int command_done(command_context_t
*context
)
528 /* find full path to file */
529 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
533 const char *file
= Jim_GetString(argv
[1], NULL
);
534 char *full_path
= find_file(file
);
535 if (full_path
== NULL
)
537 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
540 Jim_SetResult(interp
, result
);
544 static int jim_echo(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
548 const char *str
= Jim_GetString(argv
[1], NULL
);
553 static size_t openocd_jim_fwrite(const void *_ptr
, size_t size
, size_t n
, void *cookie
)
559 /* make it a char easier to read code */
563 if (ptr
== NULL
|| interp
== NULL
|| nbytes
== 0) {
567 /* do we have to chunk it? */
568 if (ptr
[nbytes
] == 0)
570 /* no it is a C style string */
571 LOG_USER_N("%s", ptr
);
574 /* GRR we must chunk - not null terminated */
584 memcpy(chunk
, ptr
, x
);
588 LOG_USER_N("%s", chunk
);
596 static size_t openocd_jim_fread(void *ptr
, size_t size
, size_t n
, void *cookie
)
598 /* TCL wants to read... tell him no */
602 static int openocd_jim_vfprintf(void *cookie
, const char *fmt
, va_list ap
)
613 cp
= alloc_vprintf(fmt
, ap
);
616 LOG_USER_N("%s", cp
);
623 static int openocd_jim_fflush(void *cookie
)
625 /* nothing to flush */
629 static char* openocd_jim_fgets(char *s
, int size
, void *cookie
)
637 static int jim_capture(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
642 const char *str
= Jim_GetString(argv
[1], NULL
);
644 /* capture log output and return it */
645 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
646 /* a garbage collect can happen, so we need a reference count to this object */
647 Jim_IncrRefCount(tclOutput
);
649 log_add_callback(tcl_output
, tclOutput
);
651 retcode
= Jim_Eval_Named(interp
, str
, __FILE__
, __LINE__
);
653 log_remove_callback(tcl_output
, tclOutput
);
655 /* We dump output into this local variable */
656 Jim_SetResult(interp
, tclOutput
);
657 Jim_DecrRefCount(interp
, tclOutput
);
662 command_context_t
* command_init()
664 command_context_t
* context
= malloc(sizeof(command_context_t
));
665 extern const char startup_tcl
[];
667 context
->mode
= COMMAND_EXEC
;
668 context
->commands
= NULL
;
669 context
->current_target
= 0;
670 context
->output_handler
= NULL
;
671 context
->output_handler_priv
= NULL
;
675 /* Create an interpreter */
676 interp
= Jim_CreateInterp();
677 /* Add all the Jim core commands */
678 Jim_RegisterCoreCommands(interp
);
681 Jim_CreateCommand(interp
, "ocd_find", jim_find
, NULL
, NULL
);
682 Jim_CreateCommand(interp
, "echo", jim_echo
, NULL
, NULL
);
683 Jim_CreateCommand(interp
, "capture", jim_capture
, NULL
, NULL
);
685 /* Set Jim's STDIO */
686 interp
->cookie_stdin
= interp
;
687 interp
->cookie_stdout
= interp
;
688 interp
->cookie_stderr
= interp
;
689 interp
->cb_fwrite
= openocd_jim_fwrite
;
690 interp
->cb_fread
= openocd_jim_fread
;
691 interp
->cb_vfprintf
= openocd_jim_vfprintf
;
692 interp
->cb_fflush
= openocd_jim_fflush
;
693 interp
->cb_fgets
= openocd_jim_fgets
;
698 Jim_EventLoopOnLoad(interp
);
700 if (Jim_Eval_Named(interp
, startup_tcl
, "embedded:startup.tcl",1)==JIM_ERR
)
702 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
703 Jim_PrintErrorMessage(interp
);
707 register_command(context
, NULL
, "sleep", handle_sleep_command
,
708 COMMAND_ANY
, "<n> [busy] - sleep for n milliseconds. \"busy\" means busy wait");
710 register_command(context
, NULL
, "fast", handle_fast_command
,
711 COMMAND_ANY
, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
716 int command_context_mode(command_context_t
*cmd_ctx
, enum command_mode mode
)
719 return ERROR_INVALID_ARGUMENTS
;
721 cmd_ctx
->mode
= mode
;
725 /* sleep command sleeps for <n> miliseconds
726 * this is useful in target startup scripts
728 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
730 unsigned long duration
= 0;
738 if (strcmp(args
[1], "busy")!=0)
739 return ERROR_COMMAND_SYNTAX_ERROR
;
743 return ERROR_COMMAND_SYNTAX_ERROR
;
746 duration
= strtoul(args
[0], NULL
, 0);
750 busy_sleep(duration
);
753 long long then
=timeval_ms();
754 while ((timeval_ms()-then
)<duration
)
756 target_call_timer_callbacks_now();
764 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
767 return ERROR_COMMAND_SYNTAX_ERROR
;
769 fast_and_dangerous
= strcmp("enable", args
[0])==0;
774 void process_jim_events(void)
777 static int recursion
= 0;
782 Jim_ProcessEvents (interp
, JIM_ALL_EVENTS
|JIM_DONT_WAIT
);
788 void register_jim(struct command_context_s
*cmd_ctx
, const char *name
, int (*cmd
)(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
), const char *help
)
790 Jim_CreateCommand(interp
, name
, cmd
, NULL
, NULL
);
792 /* FIX!!! it would be prettier to invoke add_help_text...
793 accumulate help text in Tcl helptext list. */
794 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
795 if (Jim_IsShared(helptext
))
796 helptext
= Jim_DuplicateObj(interp
, helptext
);
798 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
800 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
801 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, name
, -1));
803 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
804 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
805 Jim_ListAppendElement(interp
, helptext
, cmd_entry
);
809 /* return global variable long value or 0 upon failure */
810 long jim_global_long(const char *variable
)
812 Jim_Obj
*objPtr
=Jim_GetGlobalVariableStr(interp
, variable
, JIM_ERRMSG
);
814 if (Jim_GetLong(interp
, objPtr
, &t
)==JIM_OK
)
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)