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 static int script_command(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
60 /* the private data is stashed in the interp structure */
62 command_context_t
*context
;
68 target_call_timer_callbacks_now();
69 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
71 c
= interp
->cmdPrivData
;
72 LOG_DEBUG("script_command - %s", c
->name
);
74 words
= malloc(sizeof(char *) * argc
);
75 for (i
= 0; i
< argc
; i
++)
78 char *w
=Jim_GetString(argv
[i
], &len
);
81 /* hit an end of line comment */
89 LOG_DEBUG("script_command - %s, argv[%u]=%s", c
->name
, i
, words
[i
]);
93 /* grab the command context from the associated data */
94 context
= Jim_GetAssocData(interp
, "context");
95 retval
= Jim_GetAssocData(interp
, "retval");
96 if (context
!= NULL
&& retval
!= NULL
)
98 /* capture log output and return it */
99 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
100 log_add_callback(tcl_output
, tclOutput
);
102 *retval
= run_command(context
, c
, words
, nwords
);
104 log_remove_callback(tcl_output
, tclOutput
);
106 /* We dump output into this local variable */
107 Jim_SetVariableStr(interp
, "ocd_output", tclOutput
);
110 for (i
= 0; i
< nwords
; i
++)
114 return (*retval
==ERROR_OK
)?JIM_OK
:JIM_ERR
;
117 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
)
121 if (!context
|| !name
)
124 c
= malloc(sizeof(command_t
));
126 c
->name
= strdup(name
);
129 c
->handler
= handler
;
135 /* place command in tree */
138 if (parent
->children
)
140 /* find last child */
141 for (p
= parent
->children
; p
&& p
->next
; p
= p
->next
);
147 parent
->children
= c
;
152 if (context
->commands
)
154 /* find last command */
155 for (p
= context
->commands
; p
&& p
->next
; p
= p
->next
);
161 context
->commands
= c
;
165 /* just a placeholder, no handler */
166 if (c
->handler
==NULL
)
169 /* If this is a two level command, e.g. "flash banks", then the
170 * "unknown" proc in startup.tcl must redirect to this command.
172 * "flash banks" is translated by "unknown" to "flash_banks"
173 * if such a proc exists
175 /* Print help for command */
179 /* maximum of two levels :-) */
186 const char *full_name
=alloc_printf("ocd_%s%s%s", t1
, t2
, t3
);
187 Jim_CreateCommand(interp
, full_name
, script_command
, c
, NULL
);
188 free((void *)full_name
);
190 /* we now need to add an overrideable proc */
191 const char *override_name
=alloc_printf("proc %s%s%s {args} {return [eval \"ocd_%s%s%s $args\"]}", t1
, t2
, t3
, t1
, t2
, t3
);
192 Jim_Eval(interp
, override_name
);
193 free((void *)override_name
);
195 /* accumulate help text in Tcl helptext list. */
196 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
197 if (Jim_IsShared(helptext
))
198 helptext
= Jim_DuplicateObj(interp
, helptext
);
199 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
201 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
203 /* maximum of two levels :-) */
206 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->parent
->name
, -1));
208 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->name
, -1));
210 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
211 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
212 Jim_ListAppendElement(interp
, helptext
, cmd_entry
);
216 int unregister_all_commands(command_context_t
*context
)
223 while(NULL
!= context
->commands
)
225 c
= context
->commands
;
227 while(NULL
!= c
->children
)
230 c
->children
= c
->children
->next
;
237 context
->commands
= context
->commands
->next
;
248 int unregister_command(command_context_t
*context
, char *name
)
250 command_t
*c
, *p
= NULL
, *c2
;
252 if ((!context
) || (!name
))
253 return ERROR_INVALID_ARGUMENTS
;
256 for (c
= context
->commands
; c
; c
= c
->next
)
258 if (strcmp(name
, c
->name
) == 0)
267 context
->commands
= c
->next
;
270 /* unregister children */
273 for (c2
= c
->children
; c2
; c2
= c2
->next
)
285 /* remember the last command for unlinking */
292 void command_output_text(command_context_t
*context
, const char *data
)
294 if( context
&& context
->output_handler
&& data
){
295 context
->output_handler( context
, data
);
299 void command_print_n(command_context_t
*context
, char *format
, ...)
304 va_start(ap
, format
);
306 string
= alloc_vprintf(format
, ap
);
309 /* we want this collected in the log + we also want to pick it up as a tcl return
312 * The latter bit isn't precisely neat, but will do for now.
314 LOG_USER_N("%s", string
);
315 // We already printed it above
316 //command_output_text(context, string);
323 void command_print(command_context_t
*context
, char *format
, ...)
328 va_start(ap
, format
);
330 string
= alloc_vprintf(format
, ap
);
333 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
334 /* we want this collected in the log + we also want to pick it up as a tcl return
337 * The latter bit isn't precisely neat, but will do for now.
339 LOG_USER_N("%s", string
);
340 // We already printed it above
341 //command_output_text(context, string);
348 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
)
351 if (!((context
->mode
== COMMAND_CONFIG
) || (c
->mode
== COMMAND_ANY
) || (c
->mode
== context
->mode
) ))
353 /* Config commands can not run after the config stage */
354 LOG_ERROR("Illegal mode for command");
358 int retval
= c
->handler(context
, c
->name
, words
+ start_word
+ 1, num_words
- start_word
- 1);
359 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
)
361 /* Print help for command */
365 /* maximum of two levels :-) */
372 command_run_linef(context
, "help {%s%s%s}", t1
, t2
, t3
);
374 else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
376 /* just fall through for a shutdown request */
378 else if (retval
!= ERROR_OK
)
380 /* we do not print out an error message because the command *should*
381 * have printed out an error
383 LOG_DEBUG("Command failed with error code %d", retval
);
389 int command_run_line(command_context_t
*context
, char *line
)
391 /* all the parent commands have been registered with the interpreter
392 * so, can just evaluate the line as a script and check for
395 /* run the line thru a script engine */
398 Jim_DeleteAssocData(interp
, "context"); /* remove existing */
399 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
400 if (retcode
!= JIM_OK
)
403 /* associated the return value */
405 Jim_DeleteAssocData(interp
, "retval"); /* remove existing */
406 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
407 if (retcode
!= JIM_OK
)
410 retcode
= Jim_Eval(interp
, line
);
411 if (retcode
== JIM_ERR
) {
412 if (retval
!=ERROR_COMMAND_CLOSE_CONNECTION
)
414 /* We do not print the connection closed error message */
415 Jim_PrintErrorMessage(interp
);
417 if (retval
==ERROR_OK
)
419 /* It wasn't a low level OpenOCD command that failed */
423 } else if (retcode
== JIM_EXIT
) {
425 /* exit(Jim_GetExitCode(interp)); */
430 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
434 for (i
= 0; i
< reslen
; i
+= 256)
440 strncpy(buff
, result
+i
, chunk
);
442 LOG_USER_N("%s", buff
);
444 LOG_USER_N("%s", "\n");
450 int command_run_linef(command_context_t
*context
, char *format
, ...)
452 int retval
=ERROR_FAIL
;
455 va_start(ap
, format
);
456 string
= alloc_vprintf(format
, ap
);
459 retval
=command_run_line(context
, string
);
465 void command_set_output_handler(command_context_t
* context
, int (*output_handler
)(struct command_context_s
*context
, const char* line
), void *priv
)
467 context
->output_handler
= output_handler
;
468 context
->output_handler_priv
= priv
;
471 command_context_t
* copy_command_context(command_context_t
* context
)
473 command_context_t
* copy_context
= malloc(sizeof(command_context_t
));
475 *copy_context
= *context
;
480 int command_done(command_context_t
*context
)
488 /* find full path to file */
489 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
493 const char *file
= Jim_GetString(argv
[1], NULL
);
494 char *full_path
= find_file(file
);
495 if (full_path
== NULL
)
497 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
500 Jim_SetResult(interp
, result
);
504 static int jim_echo(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
508 const char *str
= Jim_GetString(argv
[1], NULL
);
513 static size_t openocd_jim_fwrite(const void *_ptr
, size_t size
, size_t n
, void *cookie
)
518 command_context_t
*context
;
520 /* make it a char easier to read code */
524 if (ptr
== NULL
|| interp
== NULL
|| nbytes
== 0) {
528 context
= Jim_GetAssocData(interp
, "context");
531 LOG_ERROR("openocd_jim_fwrite: no command context");
532 /* TODO: Where should this go? */
536 /* do we have to chunk it? */
537 if (ptr
[nbytes
] == 0)
539 /* no it is a C style string */
540 command_output_text(context
, ptr
);
543 /* GRR we must chunk - not null terminated */
553 memcpy(chunk
, ptr
, x
);
557 command_output_text(context
, chunk
);
565 static size_t openocd_jim_fread(void *ptr
, size_t size
, size_t n
, void *cookie
)
567 /* TCL wants to read... tell him no */
571 static int openocd_jim_vfprintf(void *cookie
, const char *fmt
, va_list ap
)
576 command_context_t
*context
;
583 context
= Jim_GetAssocData(interp
, "context");
586 LOG_ERROR("openocd_jim_vfprintf: no command context");
590 cp
= alloc_vprintf(fmt
, ap
);
593 command_output_text(context
, cp
);
600 static int openocd_jim_fflush(void *cookie
)
602 /* nothing to flush */
606 static char* openocd_jim_fgets(char *s
, int size
, void *cookie
)
613 command_context_t
* command_init()
615 command_context_t
* context
= malloc(sizeof(command_context_t
));
616 extern unsigned const char startup_tcl
[];
618 context
->mode
= COMMAND_EXEC
;
619 context
->commands
= NULL
;
620 context
->current_target
= 0;
621 context
->output_handler
= NULL
;
622 context
->output_handler_priv
= NULL
;
626 /* Create an interpreter */
627 interp
= Jim_CreateInterp();
628 /* Add all the Jim core commands */
629 Jim_RegisterCoreCommands(interp
);
632 Jim_CreateCommand(interp
, "ocd_find", jim_find
, NULL
, NULL
);
633 Jim_CreateCommand(interp
, "echo", jim_echo
, NULL
, NULL
);
635 /* Set Jim's STDIO */
636 interp
->cookie_stdin
= interp
;
637 interp
->cookie_stdout
= interp
;
638 interp
->cookie_stderr
= interp
;
639 interp
->cb_fwrite
= openocd_jim_fwrite
;
640 interp
->cb_fread
= openocd_jim_fread
;
641 interp
->cb_vfprintf
= openocd_jim_vfprintf
;
642 interp
->cb_fflush
= openocd_jim_fflush
;
643 interp
->cb_fgets
= openocd_jim_fgets
;
647 if (Jim_Eval(interp
, startup_tcl
)==JIM_ERR
)
649 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
650 Jim_PrintErrorMessage(interp
);
654 register_command(context
, NULL
, "sleep", handle_sleep_command
,
655 COMMAND_ANY
, "sleep for <n> milliseconds");
657 register_command(context
, NULL
, "fast", handle_fast_command
,
658 COMMAND_ANY
, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
663 int command_context_mode(command_context_t
*cmd_ctx
, enum command_mode mode
)
666 return ERROR_INVALID_ARGUMENTS
;
668 cmd_ctx
->mode
= mode
;
672 /* sleep command sleeps for <n> miliseconds
673 * this is useful in target startup scripts
675 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
677 unsigned long duration
= 0;
681 duration
= strtoul(args
[0], NULL
, 0);
682 usleep(duration
* 1000);
688 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
691 return ERROR_COMMAND_SYNTAX_ERROR
;
693 fast_and_dangerous
= strcmp("enable", args
[0])==0;
698 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
)
700 Jim_CreateCommand(interp
, name
, cmd
, NULL
, NULL
);
702 /* FIX!!! it would be prettier to invoke add_help_text...
703 accumulate help text in Tcl helptext list. */
704 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
705 if (Jim_IsShared(helptext
))
706 helptext
= Jim_DuplicateObj(interp
, helptext
);
708 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
710 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
711 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, name
, -1));
713 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
714 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
715 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)