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
);
75 words
= malloc(sizeof(char *) * nwords
);
76 for (i
= 0; i
< nwords
; i
++)
80 words
[i
] = strdup(Jim_GetString(argv
[i
], &len
));
85 LOG_DEBUG("script_command - %s, argv[%u]=%s", c
->name
, i
, words
[i
]);
88 /* grab the command context from the associated data */
89 context
= Jim_GetAssocData(interp
, "context");
90 retval
= Jim_GetAssocData(interp
, "retval");
91 if (context
!= NULL
&& retval
!= NULL
)
93 /* capture log output and return it */
94 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
95 log_add_callback(tcl_output
, tclOutput
);
97 *retval
= run_command(context
, c
, words
, nwords
);
99 log_remove_callback(tcl_output
, tclOutput
);
101 /* We dump output into this local variable */
102 Jim_SetVariableStr(interp
, "ocd_output", tclOutput
);
105 for (i
= 0; i
< nwords
; i
++)
109 return (*retval
==ERROR_OK
)?JIM_OK
:JIM_ERR
;
112 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
)
116 if (!context
|| !name
)
119 c
= malloc(sizeof(command_t
));
121 c
->name
= strdup(name
);
124 c
->handler
= handler
;
130 /* place command in tree */
133 if (parent
->children
)
135 /* find last child */
136 for (p
= parent
->children
; p
&& p
->next
; p
= p
->next
);
142 parent
->children
= c
;
147 if (context
->commands
)
149 /* find last command */
150 for (p
= context
->commands
; p
&& p
->next
; p
= p
->next
);
156 context
->commands
= c
;
160 /* just a placeholder, no handler */
161 if (c
->handler
==NULL
)
164 /* If this is a two level command, e.g. "flash banks", then the
165 * "unknown" proc in startup.tcl must redirect to this command.
167 * "flash banks" is translated by "unknown" to "flash_banks"
168 * if such a proc exists
170 /* Print help for command */
174 /* maximum of two levels :-) */
181 const char *full_name
=alloc_printf("ocd_%s%s%s", t1
, t2
, t3
);
182 Jim_CreateCommand(interp
, full_name
, script_command
, c
, NULL
);
183 free((void *)full_name
);
185 /* we now need to add an overrideable proc */
186 const char *override_name
=alloc_printf("proc %s%s%s {args} {return [eval \"ocd_%s%s%s $args\"]}", t1
, t2
, t3
, t1
, t2
, t3
);
187 Jim_Eval(interp
, override_name
);
188 free((void *)override_name
);
190 /* accumulate help text in Tcl helptext list. */
191 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
192 if (Jim_IsShared(helptext
))
193 helptext
= Jim_DuplicateObj(interp
, helptext
);
194 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
196 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
198 /* maximum of two levels :-) */
201 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->parent
->name
, -1));
203 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, c
->name
, -1));
205 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
206 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
207 Jim_ListAppendElement(interp
, helptext
, cmd_entry
);
211 int unregister_all_commands(command_context_t
*context
)
218 while(NULL
!= context
->commands
)
220 c
= context
->commands
;
222 while(NULL
!= c
->children
)
225 c
->children
= c
->children
->next
;
232 context
->commands
= context
->commands
->next
;
243 int unregister_command(command_context_t
*context
, char *name
)
245 command_t
*c
, *p
= NULL
, *c2
;
247 if ((!context
) || (!name
))
248 return ERROR_INVALID_ARGUMENTS
;
251 for (c
= context
->commands
; c
; c
= c
->next
)
253 if (strcmp(name
, c
->name
) == 0)
262 context
->commands
= c
->next
;
265 /* unregister children */
268 for (c2
= c
->children
; c2
; c2
= c2
->next
)
280 /* remember the last command for unlinking */
287 void command_output_text(command_context_t
*context
, const char *data
)
289 if( context
&& context
->output_handler
&& data
){
290 context
->output_handler( context
, data
);
294 void command_print_n(command_context_t
*context
, char *format
, ...)
299 va_start(ap
, format
);
301 string
= alloc_vprintf(format
, ap
);
304 /* we want this collected in the log + we also want to pick it up as a tcl return
307 * The latter bit isn't precisely neat, but will do for now.
309 LOG_USER_N("%s", string
);
310 // We already printed it above
311 //command_output_text(context, string);
318 void command_print(command_context_t
*context
, char *format
, ...)
323 va_start(ap
, format
);
325 string
= alloc_vprintf(format
, ap
);
328 strcat(string
, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
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 int run_command(command_context_t
*context
, command_t
*c
, char *words
[], int num_words
)
346 if (!((context
->mode
== COMMAND_CONFIG
) || (c
->mode
== COMMAND_ANY
) || (c
->mode
== context
->mode
) ))
348 /* Config commands can not run after the config stage */
349 LOG_ERROR("Illegal mode for command");
353 int retval
= c
->handler(context
, c
->name
, words
+ start_word
+ 1, num_words
- start_word
- 1);
354 if (retval
== ERROR_COMMAND_SYNTAX_ERROR
)
356 /* Print help for command */
360 /* maximum of two levels :-) */
367 command_run_linef(context
, "help {%s%s%s}", t1
, t2
, t3
);
369 else if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
371 /* just fall through for a shutdown request */
373 else if (retval
!= ERROR_OK
)
375 /* we do not print out an error message because the command *should*
376 * have printed out an error
378 LOG_DEBUG("Command failed with error code %d", retval
);
384 int command_run_line(command_context_t
*context
, char *line
)
386 /* all the parent commands have been registered with the interpreter
387 * so, can just evaluate the line as a script and check for
390 /* run the line thru a script engine */
393 Jim_DeleteAssocData(interp
, "context"); /* remove existing */
394 retcode
= Jim_SetAssocData(interp
, "context", NULL
, context
);
395 if (retcode
!= JIM_OK
)
398 /* associated the return value */
400 Jim_DeleteAssocData(interp
, "retval"); /* remove existing */
401 retcode
= Jim_SetAssocData(interp
, "retval", NULL
, &retval
);
402 if (retcode
!= JIM_OK
)
405 retcode
= Jim_Eval(interp
, line
);
406 if (retcode
== JIM_ERR
) {
407 if (retval
!=ERROR_COMMAND_CLOSE_CONNECTION
)
409 /* We do not print the connection closed error message */
410 Jim_PrintErrorMessage(interp
);
412 if (retval
==ERROR_OK
)
414 /* It wasn't a low level OpenOCD command that failed */
418 } else if (retcode
== JIM_EXIT
) {
420 /* exit(Jim_GetExitCode(interp)); */
425 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
429 for (i
= 0; i
< reslen
; i
+= 256)
435 strncpy(buff
, result
+i
, chunk
);
437 LOG_USER_N("%s", buff
);
439 LOG_USER_N("%s", "\n");
445 int command_run_linef(command_context_t
*context
, char *format
, ...)
447 int retval
=ERROR_FAIL
;
450 va_start(ap
, format
);
451 string
= alloc_vprintf(format
, ap
);
454 retval
=command_run_line(context
, string
);
460 void command_set_output_handler(command_context_t
* context
, int (*output_handler
)(struct command_context_s
*context
, const char* line
), void *priv
)
462 context
->output_handler
= output_handler
;
463 context
->output_handler_priv
= priv
;
466 command_context_t
* copy_command_context(command_context_t
* context
)
468 command_context_t
* copy_context
= malloc(sizeof(command_context_t
));
470 *copy_context
= *context
;
475 int command_done(command_context_t
*context
)
483 /* find full path to file */
484 static int jim_find(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
488 const char *file
= Jim_GetString(argv
[1], NULL
);
489 char *full_path
= find_file(file
);
490 if (full_path
== NULL
)
492 Jim_Obj
*result
= Jim_NewStringObj(interp
, full_path
, strlen(full_path
));
495 Jim_SetResult(interp
, result
);
499 static int jim_echo(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
503 const char *str
= Jim_GetString(argv
[1], NULL
);
508 static size_t openocd_jim_fwrite(const void *_ptr
, size_t size
, size_t n
, void *cookie
)
513 command_context_t
*context
;
515 /* make it a char easier to read code */
519 if (ptr
== NULL
|| interp
== NULL
|| nbytes
== 0) {
523 context
= Jim_GetAssocData(interp
, "context");
526 LOG_ERROR("openocd_jim_fwrite: no command context");
527 /* TODO: Where should this go? */
531 /* do we have to chunk it? */
532 if (ptr
[nbytes
] == 0)
534 /* no it is a C style string */
535 command_output_text(context
, ptr
);
538 /* GRR we must chunk - not null terminated */
548 memcpy(chunk
, ptr
, x
);
552 command_output_text(context
, chunk
);
560 static size_t openocd_jim_fread(void *ptr
, size_t size
, size_t n
, void *cookie
)
562 /* TCL wants to read... tell him no */
566 static int openocd_jim_vfprintf(void *cookie
, const char *fmt
, va_list ap
)
571 command_context_t
*context
;
578 context
= Jim_GetAssocData(interp
, "context");
581 LOG_ERROR("openocd_jim_vfprintf: no command context");
585 cp
= alloc_vprintf(fmt
, ap
);
588 command_output_text(context
, cp
);
595 static int openocd_jim_fflush(void *cookie
)
597 /* nothing to flush */
601 static char* openocd_jim_fgets(char *s
, int size
, void *cookie
)
608 command_context_t
* command_init()
610 command_context_t
* context
= malloc(sizeof(command_context_t
));
611 extern unsigned const char startup_tcl
[];
613 context
->mode
= COMMAND_EXEC
;
614 context
->commands
= NULL
;
615 context
->current_target
= 0;
616 context
->output_handler
= NULL
;
617 context
->output_handler_priv
= NULL
;
621 /* Create an interpreter */
622 interp
= Jim_CreateInterp();
623 /* Add all the Jim core commands */
624 Jim_RegisterCoreCommands(interp
);
627 Jim_CreateCommand(interp
, "ocd_find", jim_find
, NULL
, NULL
);
628 Jim_CreateCommand(interp
, "echo", jim_echo
, NULL
, NULL
);
630 /* Set Jim's STDIO */
631 interp
->cookie_stdin
= interp
;
632 interp
->cookie_stdout
= interp
;
633 interp
->cookie_stderr
= interp
;
634 interp
->cb_fwrite
= openocd_jim_fwrite
;
635 interp
->cb_fread
= openocd_jim_fread
;
636 interp
->cb_vfprintf
= openocd_jim_vfprintf
;
637 interp
->cb_fflush
= openocd_jim_fflush
;
638 interp
->cb_fgets
= openocd_jim_fgets
;
642 if (Jim_Eval(interp
, startup_tcl
)==JIM_ERR
)
644 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
645 Jim_PrintErrorMessage(interp
);
649 register_command(context
, NULL
, "sleep", handle_sleep_command
,
650 COMMAND_ANY
, "sleep for <n> milliseconds");
652 register_command(context
, NULL
, "fast", handle_fast_command
,
653 COMMAND_ANY
, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
658 int command_context_mode(command_context_t
*cmd_ctx
, enum command_mode mode
)
661 return ERROR_INVALID_ARGUMENTS
;
663 cmd_ctx
->mode
= mode
;
667 /* sleep command sleeps for <n> miliseconds
668 * this is useful in target startup scripts
670 int handle_sleep_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
672 unsigned long duration
= 0;
676 duration
= strtoul(args
[0], NULL
, 0);
677 usleep(duration
* 1000);
683 int handle_fast_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
686 return ERROR_COMMAND_SYNTAX_ERROR
;
688 fast_and_dangerous
= strcmp("enable", args
[0])==0;
693 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
)
695 Jim_CreateCommand(interp
, name
, cmd
, NULL
, NULL
);
697 /* FIX!!! it would be prettier to invoke add_help_text...
698 accumulate help text in Tcl helptext list. */
699 Jim_Obj
*helptext
=Jim_GetGlobalVariableStr(interp
, "ocd_helptext", JIM_ERRMSG
);
700 if (Jim_IsShared(helptext
))
701 helptext
= Jim_DuplicateObj(interp
, helptext
);
703 Jim_Obj
*cmd_entry
=Jim_NewListObj(interp
, NULL
, 0);
705 Jim_Obj
*cmd_list
=Jim_NewListObj(interp
, NULL
, 0);
706 Jim_ListAppendElement(interp
, cmd_list
, Jim_NewStringObj(interp
, name
, -1));
708 Jim_ListAppendElement(interp
, cmd_entry
, cmd_list
);
709 Jim_ListAppendElement(interp
, cmd_entry
, Jim_NewStringObj(interp
, help
, -1));
710 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)