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"
32 #include "time_support.h"
42 int fast_and_dangerous
= 0;
43 Jim_Interp
*interp
= NULL
;
45 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
46 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
47 static void tcl_output(void *privData
, const char *file
, int line
, const char *function
, const char *string
)
49 Jim_Obj
*tclOutput
=(Jim_Obj
*)privData
;
51 Jim_AppendString(interp
, tclOutput
, string
, strlen(string
));
54 static int script_command(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
56 /* the private data is stashed in the interp structure */
58 command_context_t
*context
;
64 target_call_timer_callbacks_now();
65 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
67 c
= interp
->cmdPrivData
;
68 LOG_DEBUG("script_command - %s", c
->name
);
71 words
= malloc(sizeof(char *) * nwords
);
72 for (i
= 0; i
< nwords
; i
++)
76 words
[i
] = strdup(Jim_GetString(argv
[i
], &len
));
81 LOG_DEBUG("script_command - %s, argv[%u]=%s", c
->name
, i
, words
[i
]);
84 /* grab the command context from the associated data */
85 context
= Jim_GetAssocData(interp
, "context");
86 retval
= Jim_GetAssocData(interp
, "retval");
87 if (context
!= NULL
&& retval
!= NULL
)
89 /* capture log output and return it */
90 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
91 log_add_callback(tcl_output
, tclOutput
);
93 *retval
= run_command(context
, c
, words
, nwords
);
95 log_remove_callback(tcl_output
, tclOutput
);
97 /* We dump output into this local variable */
98 Jim_SetVariableStr(interp
, "openocd_output", tclOutput
);
101 for (i
= 0; i
< nwords
; i
++)
105 return (*retval
==ERROR_OK
)?JIM_OK
:JIM_ERR
;
108 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
)
112 if (!context
|| !name
)
115 c
= malloc(sizeof(command_t
));
117 c
->name
= strdup(name
);
120 c
->handler
= handler
;
126 /* place command in tree */
129 if (parent
->children
)
131 /* find last child */
132 for (p
= parent
->children
; p
&& p
->next
; p
= p
->next
);
138 parent
->children
= c
;
143 if (context
->commands
)
145 /* find last command */
146 for (p
= context
->commands
; p
&& p
->next
; p
= p
->next
);
152 context
->commands
= c
;
156 /* just a placeholder, no handler */
157 if (c
->handler
==NULL
)
160 /* If this is a two level command, e.g. "flash banks", then the
161 * "unknown" proc in startup.tcl must redirect to this command.
163 * "flash banks" is translated by "unknown" to "flash_banks"
164 * if such a proc exists
166 /* Print help for command */
170 /* maximum of two levels :-) */
177 const char *full_name
=alloc_printf("%s%s%s", t1
, t2
, t3
);
178 Jim_CreateCommand(interp
, full_name
, script_command
, c
, NULL
);
179 free((void *)full_name
);
182 /* accumulate help text in Tcl helptext list. */
183 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
184 if (Jim_IsShared(helptext
))
185 helptext
= Jim_DuplicateObj(interp
, helptext
);
186 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
188 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
190 /* maximum of two levels :-) */
193 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->parent
->name
, -1));
195 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->name
, -1));
197 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
198 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
199 Jim_ListAppendElement(interp
, helptext
, cmd_entry
);
203 int unregister_all_commands(command_context_t
*context
)
211 while(NULL
!= context
->commands
)
213 c
= context
->commands
;
215 while(NULL
!= c
->children
)
218 c
->children
= c
->children
->next
;
225 context
->commands
= context
->commands
->next
;
236 int unregister_command(command_context_t
*context
, char *name
)
238 command_t
*c
, *p
= NULL
, *c2
;
240 if ((!context
) || (!name
))
241 return ERROR_INVALID_ARGUMENTS
;
244 for (c
= context
->commands
; c
; c
= c
->next
)
246 if (strcmp(name
, c
->name
) == 0)
255 context
->commands
= c
->next
;
258 /* unregister children */
261 for (c2
= c
->children
; c2
; c2
= c2
->next
)
273 /* remember the last command for unlinking */
281 void command_output_text(command_context_t
*context
, const char *data
)
283 if( context
&& context
->output_handler
&& data
){
284 context
->output_handler( context
, data
);
288 void command_print_n(command_context_t
*context
, char *format
, ...)
293 va_start(ap
, format
);
295 string
= alloc_vprintf(format
, ap
);
298 /* we want this collected in the log + we also want to pick it up as a tcl return
301 * The latter bit isn't precisely neat, but will do for now.
303 LOG_USER_N("%s", string
);
304 // We already printed it above
305 //command_output_text(context, string);
312 void command_print(command_context_t
*context
, char *format
, ...)
317 va_start(ap
, format
);
319 string
= alloc_vprintf(format
, ap
);
322 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
323 /* we want this collected in the log + we also want to pick it up as a tcl return
326 * The latter bit isn't precisely neat, but will do for now.
328 LOG_USER_N("%s", string
);
329 // We already printed it above
330 //command_output_text(context, string);
337 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
)
340 if (!((context
->mode
== COMMAND_CONFIG
) || (c
->mode
== COMMAND_ANY
) || (c
->mode
== context
->mode
) ))
342 /* Config commands can not run after the config stage */
346 int retval
= c
->handler(context
, c
->name
, words
+ start_word
+ 1, num_words
- start_word
- 1);
347 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
)
349 /* Print help for command */
353 /* maximum of two levels :-) */
360 command_run_linef(context
, "help {%s%s%s}", t1
, t2
, t3
);
362 else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
364 /* just fall through for a shutdown request */
366 else if (retval
!= ERROR_OK
)
368 /* we do not print out an error message because the command *should*
369 * have printed out an error
371 LOG_DEBUG("Command failed with error code %d", retval
);
377 int command_run_line(command_context_t
*context
, char *line
)
379 /* all the parent commands have been registered with the interpreter
380 * so, can just evaluate the line as a script and check for
383 /* run the line thru a script engine */
386 Jim_DeleteAssocData(interp
, "context"); /* remove existing */
387 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
388 if (retcode
!= JIM_OK
)
391 /* associated the return value */
393 Jim_DeleteAssocData(interp
, "retval"); /* remove existing */
394 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
395 if (retcode
!= JIM_OK
)
398 retcode
= Jim_Eval(interp
, line
);
399 if (retcode
== JIM_ERR
) {
400 if (retval
!=ERROR_COMMAND_CLOSE_CONNECTION
)
402 /* We do not print the connection closed error message */
403 Jim_PrintErrorMessage(interp
);
405 if (retval
==ERROR_OK
)
407 /* It wasn't a low level OpenOCD command that failed */
411 } else if (retcode
== JIM_EXIT
) {
413 /* exit(Jim_GetExitCode(interp)); */
418 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
422 for (i
= 0; i
< reslen
; i
+= 256)
428 strncpy(buff
, result
+i
, chunk
);
430 LOG_USER_N("%s", buff
);
432 LOG_USER_N("%s", "\n");
439 int command_run_linef(command_context_t
*context
, char *format
, ...)
441 int retval
=ERROR_FAIL
;
444 va_start(ap
, format
);
445 string
= alloc_vprintf(format
, ap
);
448 retval
=command_run_line(context
, string
);
456 void command_set_output_handler(command_context_t
* context
, int (*output_handler
)(struct command_context_s
*context
, const char* line
), void *priv
)
458 context
->output_handler
= output_handler
;
459 context
->output_handler_priv
= priv
;
462 command_context_t
* copy_command_context(command_context_t
* context
)
464 command_context_t
* copy_context
= malloc(sizeof(command_context_t
));
466 *copy_context
= *context
;
471 int command_done(command_context_t
*context
)
480 /* find full path to file */
481 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
485 const char *file
= Jim_GetString(argv
[1], NULL
);
486 char *full_path
= find_file(file
);
487 if (full_path
== NULL
)
489 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
492 Jim_SetResult(interp
, result
);
496 static int jim_echo(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
500 const char *str
= Jim_GetString(argv
[1], NULL
);
507 static size_t openocd_jim_fwrite(const void *_ptr
, size_t size
, size_t n
, void *cookie
)
512 command_context_t
*context
;
514 /* make it a char easier to read code */
518 if (ptr
== NULL
|| interp
== NULL
|| nbytes
== 0) {
522 context
= Jim_GetAssocData(interp
, "context");
525 LOG_ERROR("openocd_jim_fwrite: no command context");
526 /* TODO: Where should this go? */
530 /* do we have to chunk it? */
531 if (ptr
[nbytes
] == 0)
533 /* no it is a C style string */
534 command_output_text(context
, ptr
);
537 /* GRR we must chunk - not null terminated */
547 memcpy(chunk
, ptr
, x
);
551 command_output_text(context
, chunk
);
559 static size_t openocd_jim_fread(void *ptr
, size_t size
, size_t n
, void *cookie
)
561 /* TCL wants to read... tell him no */
565 static int openocd_jim_vfprintf(void *cookie
, const char *fmt
, va_list ap
)
570 command_context_t
*context
;
577 context
= Jim_GetAssocData(interp
, "context");
580 LOG_ERROR("openocd_jim_vfprintf: no command context");
584 cp
= alloc_vprintf(fmt
, ap
);
587 command_output_text(context
, cp
);
594 static int openocd_jim_fflush(void *cookie
)
596 /* nothing to flush */
600 static char* openocd_jim_fgets(char *s
, int size
, void *cookie
)
607 command_context_t
* command_init()
609 command_context_t
* context
= malloc(sizeof(command_context_t
));
610 extern unsigned const char startup_tcl
[];
612 context
->mode
= COMMAND_EXEC
;
613 context
->commands
= NULL
;
614 context
->current_target
= 0;
615 context
->output_handler
= NULL
;
616 context
->output_handler_priv
= NULL
;
620 /* Create an interpreter */
621 interp
= Jim_CreateInterp();
622 /* Add all the Jim core commands */
623 Jim_RegisterCoreCommands(interp
);
626 Jim_CreateCommand(interp
, "openocd_find", jim_find
, NULL
, NULL
);
627 Jim_CreateCommand(interp
, "echo", jim_echo
, NULL
, NULL
);
629 /* Set Jim's STDIO */
630 interp
->cookie_stdin
= interp
;
631 interp
->cookie_stdout
= interp
;
632 interp
->cookie_stderr
= interp
;
633 interp
->cb_fwrite
= openocd_jim_fwrite
;
634 interp
->cb_fread
= openocd_jim_fread
;
635 interp
->cb_vfprintf
= openocd_jim_vfprintf
;
636 interp
->cb_fflush
= openocd_jim_fflush
;
637 interp
->cb_fgets
= openocd_jim_fgets
;
641 if (Jim_Eval(interp
, startup_tcl
)==JIM_ERR
)
643 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
644 Jim_PrintErrorMessage(interp
);
648 register_command(context
, NULL
, "sleep", handle_sleep_command
,
649 COMMAND_ANY
, "sleep for <n> milliseconds");
651 register_command(context
, NULL
, "fast", handle_fast_command
,
652 COMMAND_ANY
, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
657 int command_context_mode(command_context_t
*cmd_ctx
, enum command_mode mode
)
660 return ERROR_INVALID_ARGUMENTS
;
662 cmd_ctx
->mode
= mode
;
666 /* sleep command sleeps for <n> miliseconds
667 * this is useful in target startup scripts
669 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
671 unsigned long duration
= 0;
675 duration
= strtoul(args
[0], NULL
, 0);
676 usleep(duration
* 1000);
682 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
685 return ERROR_COMMAND_SYNTAX_ERROR
;
687 fast_and_dangerous
= strcmp("enable", args
[0])==0;
692 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
)
694 Jim_CreateCommand(interp
, name
, cmd
, NULL
, NULL
);
696 /* FIX!!! it would be prettier to invoke add_help_text...
697 accumulate help text in Tcl helptext list. */
698 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
699 if (Jim_IsShared(helptext
))
700 helptext
= Jim_DuplicateObj(interp
, helptext
);
702 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
704 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
705 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, name
, -1));
707 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
708 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
709 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)