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 * part of this file is taken from libcli (libcli.sourceforge.net) *
9 * Copyright (C) David Parrish (david@dparrish.com) *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
30 #include "replacements.h"
33 #include "configuration.h"
36 #include "time_support.h"
46 int fast_and_dangerous
= 0;
47 Jim_Interp
*interp
= NULL
;
49 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
50 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
52 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
);
54 static void tcl_output(void *privData
, const char *file
, int line
, const char *function
, const char *string
)
56 Jim_Obj
*tclOutput
=(Jim_Obj
*)privData
;
58 Jim_AppendString(interp
, tclOutput
, string
, strlen(string
));
61 extern command_context_t
*global_cmd_ctx
;
64 static int script_command(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
66 /* the private data is stashed in the interp structure */
68 command_context_t
*context
;
74 target_call_timer_callbacks_now();
75 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
77 c
= interp
->cmdPrivData
;
78 LOG_DEBUG("script_command - %s", c
->name
);
80 words
= malloc(sizeof(char *) * argc
);
81 for (i
= 0; i
< argc
; i
++)
84 const char *w
=Jim_GetString(argv
[i
], &len
);
87 /* hit an end of line comment */
95 LOG_DEBUG("script_command - %s, argv[%u]=%s", c
->name
, i
, words
[i
]);
99 /* grab the command context from the associated data */
100 context
= Jim_GetAssocData(interp
, "context");
103 /* Tcl can invoke commands directly instead of via command_run_line(). This would
104 * happen when the Jim Tcl interpreter is provided by eCos.
106 context
= global_cmd_ctx
;
109 /* capture log output and return it */
110 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
111 /* a garbage collect can happen, so we need a reference count to this object */
112 Jim_IncrRefCount(tclOutput
);
114 log_add_callback(tcl_output
, tclOutput
);
116 retval
= run_command(context
, c
, words
, nwords
);
118 log_remove_callback(tcl_output
, tclOutput
);
120 /* We dump output into this local variable */
121 Jim_SetResult(interp
, tclOutput
);
122 Jim_DecrRefCount(interp
, tclOutput
);
124 for (i
= 0; i
< nwords
; i
++)
128 int *return_retval
= Jim_GetAssocData(interp
, "retval");
129 if (return_retval
!= NULL
)
131 *return_retval
= retval
;
134 return (retval
==ERROR_OK
)?JIM_OK
:JIM_ERR
;
137 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
)
141 if (!context
|| !name
)
144 c
= malloc(sizeof(command_t
));
146 c
->name
= strdup(name
);
149 c
->handler
= handler
;
155 /* place command in tree */
158 if (parent
->children
)
160 /* find last child */
161 for (p
= parent
->children
; p
&& p
->next
; p
= p
->next
);
167 parent
->children
= c
;
172 if (context
->commands
)
174 /* find last command */
175 for (p
= context
->commands
; p
&& p
->next
; p
= p
->next
);
181 context
->commands
= c
;
185 /* just a placeholder, no handler */
186 if (c
->handler
==NULL
)
189 /* If this is a two level command, e.g. "flash banks", then the
190 * "unknown" proc in startup.tcl must redirect to this command.
192 * "flash banks" is translated by "unknown" to "flash_banks"
193 * if such a proc exists
195 /* Print help for command */
199 /* maximum of two levels :-) */
206 const char *full_name
=alloc_printf("ocd_%s%s%s", t1
, t2
, t3
);
207 Jim_CreateCommand(interp
, full_name
, script_command
, c
, NULL
);
208 free((void *)full_name
);
210 /* we now need to add an overrideable proc */
211 const char *override_name
=alloc_printf("proc %s%s%s {args} {eval \"ocd_%s%s%s $args\";return \"\"}", t1
, t2
, t3
, t1
, t2
, t3
);
212 Jim_Eval(interp
, override_name
);
213 free((void *)override_name
);
215 /* accumulate help text in Tcl helptext list. */
216 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
217 if (Jim_IsShared(helptext
))
218 helptext
= Jim_DuplicateObj(interp
, helptext
);
219 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
221 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
223 /* maximum of two levels :-) */
226 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->parent
->name
, -1));
228 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->name
, -1));
230 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
231 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
232 Jim_ListAppendElement(interp
, helptext
, cmd_entry
);
236 int unregister_all_commands(command_context_t
*context
)
243 while(NULL
!= context
->commands
)
245 c
= context
->commands
;
247 while(NULL
!= c
->children
)
250 c
->children
= c
->children
->next
;
257 context
->commands
= context
->commands
->next
;
268 int unregister_command(command_context_t
*context
, char *name
)
270 command_t
*c
, *p
= NULL
, *c2
;
272 if ((!context
) || (!name
))
273 return ERROR_INVALID_ARGUMENTS
;
276 for (c
= context
->commands
; c
; c
= c
->next
)
278 if (strcmp(name
, c
->name
) == 0)
287 context
->commands
= c
->next
;
290 /* unregister children */
293 for (c2
= c
->children
; c2
; c2
= c2
->next
)
305 /* remember the last command for unlinking */
312 void command_output_text(command_context_t
*context
, const char *data
)
314 if( context
&& context
->output_handler
&& data
){
315 context
->output_handler( context
, data
);
319 void command_print_n(command_context_t
*context
, char *format
, ...)
324 va_start(ap
, format
);
326 string
= alloc_vprintf(format
, ap
);
329 /* we want this collected in the log + we also want to pick it up as a tcl return
332 * The latter bit isn't precisely neat, but will do for now.
334 LOG_USER_N("%s", string
);
335 // We already printed it above
336 //command_output_text(context, string);
343 void command_print(command_context_t
*context
, char *format
, ...)
348 va_start(ap
, format
);
350 string
= alloc_vprintf(format
, ap
);
353 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
354 /* we want this collected in the log + we also want to pick it up as a tcl return
357 * The latter bit isn't precisely neat, but will do for now.
359 LOG_USER_N("%s", string
);
360 // We already printed it above
361 //command_output_text(context, string);
368 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
)
371 if (!((context
->mode
== COMMAND_CONFIG
) || (c
->mode
== COMMAND_ANY
) || (c
->mode
== context
->mode
) ))
373 /* Config commands can not run after the config stage */
374 LOG_ERROR("Illegal mode for command");
378 int retval
= c
->handler(context
, c
->name
, words
+ start_word
+ 1, num_words
- start_word
- 1);
379 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
)
381 /* Print help for command */
385 /* maximum of two levels :-) */
392 command_run_linef(context
, "help {%s%s%s}", t1
, t2
, t3
);
394 else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
396 /* just fall through for a shutdown request */
398 else if (retval
!= ERROR_OK
)
400 /* we do not print out an error message because the command *should*
401 * have printed out an error
403 LOG_DEBUG("Command failed with error code %d", retval
);
409 int command_run_line(command_context_t
*context
, char *line
)
411 /* all the parent commands have been registered with the interpreter
412 * so, can just evaluate the line as a script and check for
415 /* run the line thru a script engine */
416 int retval
=ERROR_FAIL
;
418 /* Beware! This code needs to be reentrant. It is also possible
419 * for OpenOCD commands to be invoked directly from Tcl. This would
420 * happen when the Jim Tcl interpreter is provided by eCos for
423 Jim_DeleteAssocData(interp
, "context");
424 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
425 if (retcode
== JIM_OK
)
427 /* associated the return value */
428 Jim_DeleteAssocData(interp
, "retval");
429 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
430 if (retcode
== JIM_OK
)
432 retcode
= Jim_Eval(interp
, line
);
434 Jim_DeleteAssocData(interp
, "retval");
436 Jim_DeleteAssocData(interp
, "context");
438 if (retcode
== JIM_ERR
) {
439 if (retval
!=ERROR_COMMAND_CLOSE_CONNECTION
)
441 /* We do not print the connection closed error message */
442 Jim_PrintErrorMessage(interp
);
444 if (retval
==ERROR_OK
)
446 /* It wasn't a low level OpenOCD command that failed */
450 } else if (retcode
== JIM_EXIT
) {
452 /* exit(Jim_GetExitCode(interp)); */
457 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
461 for (i
= 0; i
< reslen
; i
+= 256)
467 strncpy(buff
, result
+i
, chunk
);
469 LOG_USER_N("%s", buff
);
471 LOG_USER_N("%s", "\n");
477 int command_run_linef(command_context_t
*context
, char *format
, ...)
479 int retval
=ERROR_FAIL
;
482 va_start(ap
, format
);
483 string
= alloc_vprintf(format
, ap
);
486 retval
=command_run_line(context
, string
);
492 void command_set_output_handler(command_context_t
* context
, int (*output_handler
)(struct command_context_s
*context
, const char* line
), void *priv
)
494 context
->output_handler
= output_handler
;
495 context
->output_handler_priv
= priv
;
498 command_context_t
* copy_command_context(command_context_t
* context
)
500 command_context_t
* copy_context
= malloc(sizeof(command_context_t
));
502 *copy_context
= *context
;
507 int command_done(command_context_t
*context
)
515 /* find full path to file */
516 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
520 const char *file
= Jim_GetString(argv
[1], NULL
);
521 char *full_path
= find_file(file
);
522 if (full_path
== NULL
)
524 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
527 Jim_SetResult(interp
, result
);
531 static int jim_echo(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
535 const char *str
= Jim_GetString(argv
[1], NULL
);
540 static size_t openocd_jim_fwrite(const void *_ptr
, size_t size
, size_t n
, void *cookie
)
546 /* make it a char easier to read code */
550 if (ptr
== NULL
|| interp
== NULL
|| nbytes
== 0) {
554 /* do we have to chunk it? */
555 if (ptr
[nbytes
] == 0)
557 /* no it is a C style string */
558 LOG_USER_N("%s", ptr
);
561 /* GRR we must chunk - not null terminated */
571 memcpy(chunk
, ptr
, x
);
575 LOG_USER_N("%s", chunk
);
583 static size_t openocd_jim_fread(void *ptr
, size_t size
, size_t n
, void *cookie
)
585 /* TCL wants to read... tell him no */
589 static int openocd_jim_vfprintf(void *cookie
, const char *fmt
, va_list ap
)
600 cp
= alloc_vprintf(fmt
, ap
);
603 LOG_USER_N("%s", cp
);
610 static int openocd_jim_fflush(void *cookie
)
612 /* nothing to flush */
616 static char* openocd_jim_fgets(char *s
, int size
, void *cookie
)
623 command_context_t
* command_init()
625 command_context_t
* context
= malloc(sizeof(command_context_t
));
626 extern unsigned const char startup_tcl
[];
628 context
->mode
= COMMAND_EXEC
;
629 context
->commands
= NULL
;
630 context
->current_target
= 0;
631 context
->output_handler
= NULL
;
632 context
->output_handler_priv
= NULL
;
636 /* Create an interpreter */
637 interp
= Jim_CreateInterp();
638 /* Add all the Jim core commands */
639 Jim_RegisterCoreCommands(interp
);
642 Jim_CreateCommand(interp
, "ocd_find", jim_find
, NULL
, NULL
);
643 Jim_CreateCommand(interp
, "echo", jim_echo
, NULL
, NULL
);
645 /* Set Jim's STDIO */
646 interp
->cookie_stdin
= interp
;
647 interp
->cookie_stdout
= interp
;
648 interp
->cookie_stderr
= interp
;
649 interp
->cb_fwrite
= openocd_jim_fwrite
;
650 interp
->cb_fread
= openocd_jim_fread
;
651 interp
->cb_vfprintf
= openocd_jim_vfprintf
;
652 interp
->cb_fflush
= openocd_jim_fflush
;
653 interp
->cb_fgets
= openocd_jim_fgets
;
657 if (Jim_Eval(interp
, startup_tcl
)==JIM_ERR
)
659 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
660 Jim_PrintErrorMessage(interp
);
664 register_command(context
, NULL
, "sleep", handle_sleep_command
,
665 COMMAND_ANY
, "sleep for <n> milliseconds");
667 register_command(context
, NULL
, "fast", handle_fast_command
,
668 COMMAND_ANY
, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
673 int command_context_mode(command_context_t
*cmd_ctx
, enum command_mode mode
)
676 return ERROR_INVALID_ARGUMENTS
;
678 cmd_ctx
->mode
= mode
;
682 /* sleep command sleeps for <n> miliseconds
683 * this is useful in target startup scripts
685 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
687 unsigned long duration
= 0;
691 duration
= strtoul(args
[0], NULL
, 0);
692 usleep(duration
* 1000);
698 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
701 return ERROR_COMMAND_SYNTAX_ERROR
;
703 fast_and_dangerous
= strcmp("enable", args
[0])==0;
708 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
)
710 Jim_CreateCommand(interp
, name
, cmd
, NULL
, NULL
);
712 /* FIX!!! it would be prettier to invoke add_help_text...
713 accumulate help text in Tcl helptext list. */
714 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
715 if (Jim_IsShared(helptext
))
716 helptext
= Jim_DuplicateObj(interp
, helptext
);
718 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
720 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
721 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, name
, -1));
723 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
724 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
725 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)