1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * part of this file is taken from libcli (libcli.sourceforge.net) *
6 * Copyright (C) David Parrish (david@dparrish.com) *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
30 #include "configuration.h"
33 #include "time_support.h"
43 int fast_and_dangerous
= 0;
44 Jim_Interp
*interp
= NULL
;
46 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
47 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
49 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
);
51 static void tcl_output(void *privData
, const char *file
, int line
, const char *function
, const char *string
)
53 Jim_Obj
*tclOutput
=(Jim_Obj
*)privData
;
55 Jim_AppendString(interp
, tclOutput
, string
, strlen(string
));
58 extern command_context_t
*global_cmd_ctx
;
61 static int script_command(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
63 /* the private data is stashed in the interp structure */
65 command_context_t
*context
;
71 target_call_timer_callbacks_now();
72 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
74 c
= interp
->cmdPrivData
;
75 LOG_DEBUG("script_command - %s", c
->name
);
77 words
= malloc(sizeof(char *) * argc
);
78 for (i
= 0; i
< argc
; i
++)
81 char *w
=Jim_GetString(argv
[i
], &len
);
84 /* hit an end of line comment */
92 LOG_DEBUG("script_command - %s, argv[%u]=%s", c
->name
, i
, words
[i
]);
96 /* grab the command context from the associated data */
97 context
= Jim_GetAssocData(interp
, "context");
100 /* Tcl can invoke commands directly instead of via command_run_line(). This would
101 * happen when the Jim Tcl interpreter is provided by eCos.
103 context
= global_cmd_ctx
;
106 /* capture log output and return it */
107 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
108 /* a garbage collect can happen, so we need a reference count to this object */
109 Jim_IncrRefCount(tclOutput
);
111 log_add_callback(tcl_output
, tclOutput
);
113 retval
= run_command(context
, c
, words
, nwords
);
115 log_remove_callback(tcl_output
, tclOutput
);
117 /* We dump output into this local variable */
118 Jim_SetVariableStr(interp
, "ocd_output", tclOutput
);
119 Jim_DecrRefCount(interp
, tclOutput
);
121 for (i
= 0; i
< nwords
; i
++)
125 int *return_retval
= Jim_GetAssocData(interp
, "retval");
126 if (return_retval
!= NULL
)
128 *return_retval
= retval
;
131 return (retval
==ERROR_OK
)?JIM_OK
:JIM_ERR
;
134 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
)
138 if (!context
|| !name
)
141 c
= malloc(sizeof(command_t
));
143 c
->name
= strdup(name
);
146 c
->handler
= handler
;
152 /* place command in tree */
155 if (parent
->children
)
157 /* find last child */
158 for (p
= parent
->children
; p
&& p
->next
; p
= p
->next
);
164 parent
->children
= c
;
169 if (context
->commands
)
171 /* find last command */
172 for (p
= context
->commands
; p
&& p
->next
; p
= p
->next
);
178 context
->commands
= c
;
182 /* just a placeholder, no handler */
183 if (c
->handler
==NULL
)
186 /* If this is a two level command, e.g. "flash banks", then the
187 * "unknown" proc in startup.tcl must redirect to this command.
189 * "flash banks" is translated by "unknown" to "flash_banks"
190 * if such a proc exists
192 /* Print help for command */
196 /* maximum of two levels :-) */
203 const char *full_name
=alloc_printf("ocd_%s%s%s", t1
, t2
, t3
);
204 Jim_CreateCommand(interp
, full_name
, script_command
, c
, NULL
);
205 free((void *)full_name
);
207 /* we now need to add an overrideable proc */
208 const char *override_name
=alloc_printf("proc %s%s%s {args} {return [eval \"ocd_%s%s%s $args\"]}", t1
, t2
, t3
, t1
, t2
, t3
);
209 Jim_Eval(interp
, override_name
);
210 free((void *)override_name
);
212 /* accumulate help text in Tcl helptext list. */
213 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
214 if (Jim_IsShared(helptext
))
215 helptext
= Jim_DuplicateObj(interp
, helptext
);
216 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
218 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
220 /* maximum of two levels :-) */
223 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->parent
->name
, -1));
225 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->name
, -1));
227 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
228 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
229 Jim_ListAppendElement(interp
, helptext
, cmd_entry
);
233 int unregister_all_commands(command_context_t
*context
)
240 while(NULL
!= context
->commands
)
242 c
= context
->commands
;
244 while(NULL
!= c
->children
)
247 c
->children
= c
->children
->next
;
254 context
->commands
= context
->commands
->next
;
265 int unregister_command(command_context_t
*context
, char *name
)
267 command_t
*c
, *p
= NULL
, *c2
;
269 if ((!context
) || (!name
))
270 return ERROR_INVALID_ARGUMENTS
;
273 for (c
= context
->commands
; c
; c
= c
->next
)
275 if (strcmp(name
, c
->name
) == 0)
284 context
->commands
= c
->next
;
287 /* unregister children */
290 for (c2
= c
->children
; c2
; c2
= c2
->next
)
302 /* remember the last command for unlinking */
309 void command_output_text(command_context_t
*context
, const char *data
)
311 if( context
&& context
->output_handler
&& data
){
312 context
->output_handler( context
, data
);
316 void command_print_n(command_context_t
*context
, char *format
, ...)
321 va_start(ap
, format
);
323 string
= alloc_vprintf(format
, ap
);
326 /* we want this collected in the log + we also want to pick it up as a tcl return
329 * The latter bit isn't precisely neat, but will do for now.
331 LOG_USER_N("%s", string
);
332 // We already printed it above
333 //command_output_text(context, string);
340 void command_print(command_context_t
*context
, char *format
, ...)
345 va_start(ap
, format
);
347 string
= alloc_vprintf(format
, ap
);
350 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
351 /* we want this collected in the log + we also want to pick it up as a tcl return
354 * The latter bit isn't precisely neat, but will do for now.
356 LOG_USER_N("%s", string
);
357 // We already printed it above
358 //command_output_text(context, string);
365 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
)
368 if (!((context
->mode
== COMMAND_CONFIG
) || (c
->mode
== COMMAND_ANY
) || (c
->mode
== context
->mode
) ))
370 /* Config commands can not run after the config stage */
371 LOG_ERROR("Illegal mode for command");
375 int retval
= c
->handler(context
, c
->name
, words
+ start_word
+ 1, num_words
- start_word
- 1);
376 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
)
378 /* Print help for command */
382 /* maximum of two levels :-) */
389 command_run_linef(context
, "help {%s%s%s}", t1
, t2
, t3
);
391 else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
393 /* just fall through for a shutdown request */
395 else if (retval
!= ERROR_OK
)
397 /* we do not print out an error message because the command *should*
398 * have printed out an error
400 LOG_DEBUG("Command failed with error code %d", retval
);
406 int command_run_line(command_context_t
*context
, char *line
)
408 /* all the parent commands have been registered with the interpreter
409 * so, can just evaluate the line as a script and check for
412 /* run the line thru a script engine */
413 int retval
=ERROR_FAIL
;
415 /* Beware! This code needs to be reentrant. It is also possible
416 * for OpenOCD commands to be invoked directly from Tcl. This would
417 * happen when the Jim Tcl interpreter is provided by eCos for
420 Jim_DeleteAssocData(interp
, "context");
421 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
422 if (retcode
== JIM_OK
)
424 /* associated the return value */
425 Jim_DeleteAssocData(interp
, "retval");
426 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
427 if (retcode
== JIM_OK
)
429 retcode
= Jim_Eval(interp
, line
);
431 Jim_DeleteAssocData(interp
, "retval");
433 Jim_DeleteAssocData(interp
, "context");
435 if (retcode
== JIM_ERR
) {
436 if (retval
!=ERROR_COMMAND_CLOSE_CONNECTION
)
438 /* We do not print the connection closed error message */
439 Jim_PrintErrorMessage(interp
);
441 if (retval
==ERROR_OK
)
443 /* It wasn't a low level OpenOCD command that failed */
447 } else if (retcode
== JIM_EXIT
) {
449 /* exit(Jim_GetExitCode(interp)); */
454 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
458 for (i
= 0; i
< reslen
; i
+= 256)
464 strncpy(buff
, result
+i
, chunk
);
466 LOG_USER_N("%s", buff
);
468 LOG_USER_N("%s", "\n");
474 int command_run_linef(command_context_t
*context
, char *format
, ...)
476 int retval
=ERROR_FAIL
;
479 va_start(ap
, format
);
480 string
= alloc_vprintf(format
, ap
);
483 retval
=command_run_line(context
, string
);
489 void command_set_output_handler(command_context_t
* context
, int (*output_handler
)(struct command_context_s
*context
, const char* line
), void *priv
)
491 context
->output_handler
= output_handler
;
492 context
->output_handler_priv
= priv
;
495 command_context_t
* copy_command_context(command_context_t
* context
)
497 command_context_t
* copy_context
= malloc(sizeof(command_context_t
));
499 *copy_context
= *context
;
504 int command_done(command_context_t
*context
)
512 /* find full path to file */
513 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
517 const char *file
= Jim_GetString(argv
[1], NULL
);
518 char *full_path
= find_file(file
);
519 if (full_path
== NULL
)
521 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
524 Jim_SetResult(interp
, result
);
528 static int jim_echo(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
532 const char *str
= Jim_GetString(argv
[1], NULL
);
537 static size_t openocd_jim_fwrite(const void *_ptr
, size_t size
, size_t n
, void *cookie
)
542 command_context_t
*context
;
544 /* make it a char easier to read code */
548 if (ptr
== NULL
|| interp
== NULL
|| nbytes
== 0) {
552 context
= Jim_GetAssocData(interp
, "context");
555 LOG_ERROR("openocd_jim_fwrite: no command context");
556 /* TODO: Where should this go? */
560 /* do we have to chunk it? */
561 if (ptr
[nbytes
] == 0)
563 /* no it is a C style string */
564 command_output_text(context
, ptr
);
567 /* GRR we must chunk - not null terminated */
577 memcpy(chunk
, ptr
, x
);
581 command_output_text(context
, chunk
);
589 static size_t openocd_jim_fread(void *ptr
, size_t size
, size_t n
, void *cookie
)
591 /* TCL wants to read... tell him no */
595 static int openocd_jim_vfprintf(void *cookie
, const char *fmt
, va_list ap
)
600 command_context_t
*context
;
607 context
= Jim_GetAssocData(interp
, "context");
610 LOG_ERROR("openocd_jim_vfprintf: no command context");
614 cp
= alloc_vprintf(fmt
, ap
);
617 command_output_text(context
, cp
);
624 static int openocd_jim_fflush(void *cookie
)
626 /* nothing to flush */
630 static char* openocd_jim_fgets(char *s
, int size
, void *cookie
)
637 command_context_t
* command_init()
639 command_context_t
* context
= malloc(sizeof(command_context_t
));
640 extern unsigned const char startup_tcl
[];
642 context
->mode
= COMMAND_EXEC
;
643 context
->commands
= NULL
;
644 context
->current_target
= 0;
645 context
->output_handler
= NULL
;
646 context
->output_handler_priv
= NULL
;
650 /* Create an interpreter */
651 interp
= Jim_CreateInterp();
652 /* Add all the Jim core commands */
653 Jim_RegisterCoreCommands(interp
);
656 Jim_CreateCommand(interp
, "ocd_find", jim_find
, NULL
, NULL
);
657 Jim_CreateCommand(interp
, "echo", jim_echo
, NULL
, NULL
);
659 /* Set Jim's STDIO */
660 interp
->cookie_stdin
= interp
;
661 interp
->cookie_stdout
= interp
;
662 interp
->cookie_stderr
= interp
;
663 interp
->cb_fwrite
= openocd_jim_fwrite
;
664 interp
->cb_fread
= openocd_jim_fread
;
665 interp
->cb_vfprintf
= openocd_jim_vfprintf
;
666 interp
->cb_fflush
= openocd_jim_fflush
;
667 interp
->cb_fgets
= openocd_jim_fgets
;
671 if (Jim_Eval(interp
, startup_tcl
)==JIM_ERR
)
673 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
674 Jim_PrintErrorMessage(interp
);
678 register_command(context
, NULL
, "sleep", handle_sleep_command
,
679 COMMAND_ANY
, "sleep for <n> milliseconds");
681 register_command(context
, NULL
, "fast", handle_fast_command
,
682 COMMAND_ANY
, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
687 int command_context_mode(command_context_t
*cmd_ctx
, enum command_mode mode
)
690 return ERROR_INVALID_ARGUMENTS
;
692 cmd_ctx
->mode
= mode
;
696 /* sleep command sleeps for <n> miliseconds
697 * this is useful in target startup scripts
699 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
701 unsigned long duration
= 0;
705 duration
= strtoul(args
[0], NULL
, 0);
706 usleep(duration
* 1000);
712 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
715 return ERROR_COMMAND_SYNTAX_ERROR
;
717 fast_and_dangerous
= strcmp("enable", args
[0])==0;
722 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
)
724 Jim_CreateCommand(interp
, name
, cmd
, NULL
, NULL
);
726 /* FIX!!! it would be prettier to invoke add_help_text...
727 accumulate help text in Tcl helptext list. */
728 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
729 if (Jim_IsShared(helptext
))
730 helptext
= Jim_DuplicateObj(interp
, helptext
);
732 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
734 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
735 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, name
, -1));
737 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
738 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
739 Jim_ListAppendElement(interp
, helptext
, cmd_entry
);
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)