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"
37 #include "jim-eventloop.h"
47 int fast_and_dangerous = 0;
48 Jim_Interp *interp = NULL;
50 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int run_command(command_context_t *context, command_t *c, char *words[], int num_words);
55 static void tcl_output(void *privData, const char *file, int line, const char *function, const char *string)
57 Jim_Obj *tclOutput=(Jim_Obj *)privData;
59 Jim_AppendString(interp, tclOutput, string, strlen(string));
62 extern command_context_t *global_cmd_ctx;
65 static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
67 /* the private data is stashed in the interp structure */
69 command_context_t *context;
75 target_call_timer_callbacks_now();
76 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
78 c = interp->cmdPrivData;
79 LOG_DEBUG("script_command - %s", c->name);
81 words = malloc(sizeof(char *) * argc);
82 for (i = 0; i < argc; i++)
85 const char *w=Jim_GetString(argv[i], &len);
88 /* hit an end of line comment */
96 LOG_DEBUG("script_command - %s, argv[%u]=%s", c->name, i, words[i]);
100 /* grab the command context from the associated data */
101 context = Jim_GetAssocData(interp, "context");
104 /* Tcl can invoke commands directly instead of via command_run_line(). This would
105 * happen when the Jim Tcl interpreter is provided by eCos.
107 context = global_cmd_ctx;
110 /* capture log output and return it */
111 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
112 /* a garbage collect can happen, so we need a reference count to this object */
113 Jim_IncrRefCount(tclOutput);
115 log_add_callback(tcl_output, tclOutput);
117 retval = run_command(context, c, words, nwords);
119 log_remove_callback(tcl_output, tclOutput);
121 /* We dump output into this local variable */
122 Jim_SetResult(interp, tclOutput);
123 Jim_DecrRefCount(interp, tclOutput);
125 for (i = 0; i < nwords; i++)
129 int *return_retval = Jim_GetAssocData(interp, "retval");
130 if (return_retval != NULL)
132 *return_retval = retval;
135 return (retval==ERROR_OK)?JIM_OK:JIM_ERR;
138 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)
142 if (!context || !name)
145 c = malloc(sizeof(command_t));
147 c->name = strdup(name);
150 c->handler = handler;
156 /* place command in tree */
159 if (parent->children)
161 /* find last child */
162 for (p = parent->children; p && p->next; p = p->next);
168 parent->children = c;
173 if (context->commands)
175 /* find last command */
176 for (p = context->commands; p && p->next; p = p->next);
182 context->commands = c;
186 /* just a placeholder, no handler */
187 if (c->handler==NULL)
190 /* If this is a two level command, e.g. "flash banks", then the
191 * "unknown" proc in startup.tcl must redirect to this command.
193 * "flash banks" is translated by "unknown" to "flash_banks"
194 * if such a proc exists
196 /* Print help for command */
200 /* maximum of two levels :-) */
207 const char *full_name=alloc_printf("ocd_%s%s%s", t1, t2, t3);
208 Jim_CreateCommand(interp, full_name, script_command, c, NULL);
209 free((void *)full_name);
211 /* we now need to add an overrideable proc */
212 const char *override_name=alloc_printf("proc %s%s%s {args} {if {[catch {eval \"ocd_%s%s%s $args\"}]==0} {return \"\"} else { return -code error }", t1, t2, t3, t1, t2, t3);
213 Jim_Eval(interp, override_name);
214 free((void *)override_name);
216 /* accumulate help text in Tcl helptext list. */
217 Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
218 if (Jim_IsShared(helptext))
219 helptext = Jim_DuplicateObj(interp, helptext);
220 Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
222 Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
224 /* maximum of two levels :-) */
227 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->parent->name, -1));
229 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->name, -1));
231 Jim_ListAppendElement(interp, cmd_entry, cmd_list);
232 Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
233 Jim_ListAppendElement(interp, helptext, cmd_entry);
237 int unregister_all_commands(command_context_t *context)
244 while(NULL != context->commands)
246 c = context->commands;
248 while(NULL != c->children)
251 c->children = c->children->next;
258 context->commands = context->commands->next;
269 int unregister_command(command_context_t *context, char *name)
271 command_t *c, *p = NULL, *c2;
273 if ((!context) || (!name))
274 return ERROR_INVALID_ARGUMENTS;
277 for (c = context->commands; c; c = c->next)
279 if (strcmp(name, c->name) == 0)
288 context->commands = c->next;
291 /* unregister children */
294 for (c2 = c->children; c2; c2 = c2->next)
306 /* remember the last command for unlinking */
313 void command_output_text(command_context_t *context, const char *data)
315 if( context && context->output_handler && data ){
316 context->output_handler( context, data );
320 void command_print_n(command_context_t *context, char *format, ...)
325 va_start(ap, format);
327 string = alloc_vprintf(format, ap);
330 /* we want this collected in the log + we also want to pick it up as a tcl return
333 * The latter bit isn't precisely neat, but will do for now.
335 LOG_USER_N("%s", string);
336 // We already printed it above
337 //command_output_text(context, string);
344 void command_print(command_context_t *context, char *format, ...)
349 va_start(ap, format);
351 string = alloc_vprintf(format, ap);
354 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
355 /* we want this collected in the log + we also want to pick it up as a tcl return
358 * The latter bit isn't precisely neat, but will do for now.
360 LOG_USER_N("%s", string);
361 // We already printed it above
362 //command_output_text(context, string);
369 int run_command(command_context_t *context, command_t *c, char *words[], int num_words)
372 if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) ))
374 /* Config commands can not run after the config stage */
375 LOG_ERROR("Illegal mode for command");
379 int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
380 if (retval == ERROR_COMMAND_SYNTAX_ERROR)
382 /* Print help for command */
386 /* maximum of two levels :-) */
393 command_run_linef(context, "help {%s%s%s}", t1, t2, t3);
395 else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
397 /* just fall through for a shutdown request */
399 else if (retval != ERROR_OK)
401 /* we do not print out an error message because the command *should*
402 * have printed out an error
404 LOG_DEBUG("Command failed with error code %d", retval);
410 int command_run_line(command_context_t *context, char *line)
412 /* all the parent commands have been registered with the interpreter
413 * so, can just evaluate the line as a script and check for
416 /* run the line thru a script engine */
417 int retval=ERROR_FAIL;
419 /* Beware! This code needs to be reentrant. It is also possible
420 * for OpenOCD commands to be invoked directly from Tcl. This would
421 * happen when the Jim Tcl interpreter is provided by eCos for
424 Jim_DeleteAssocData(interp, "context");
425 retcode = Jim_SetAssocData(interp, "context", NULL, context);
426 if (retcode == JIM_OK)
428 /* associated the return value */
429 Jim_DeleteAssocData(interp, "retval");
430 retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
431 if (retcode == JIM_OK)
433 retcode = Jim_Eval(interp, line);
435 Jim_DeleteAssocData(interp, "retval");
437 Jim_DeleteAssocData(interp, "context");
439 if (retcode == JIM_ERR) {
440 if (retval!=ERROR_COMMAND_CLOSE_CONNECTION)
442 /* We do not print the connection closed error message */
443 Jim_PrintErrorMessage(interp);
445 if (retval==ERROR_OK)
447 /* It wasn't a low level OpenOCD command that failed */
451 } else if (retcode == JIM_EXIT) {
453 /* exit(Jim_GetExitCode(interp)); */
458 result = Jim_GetString(Jim_GetResult(interp), &reslen);
462 for (i = 0; i < reslen; i += 256)
468 strncpy(buff, result+i, chunk);
470 LOG_USER_N("%s", buff);
472 LOG_USER_N("%s", "\n");
478 int command_run_linef(command_context_t *context, char *format, ...)
480 int retval=ERROR_FAIL;
483 va_start(ap, format);
484 string = alloc_vprintf(format, ap);
487 retval=command_run_line(context, string);
493 void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, const char* line), void *priv)
495 context->output_handler = output_handler;
496 context->output_handler_priv = priv;
499 command_context_t* copy_command_context(command_context_t* context)
501 command_context_t* copy_context = malloc(sizeof(command_context_t));
503 *copy_context = *context;
508 int command_done(command_context_t *context)
516 /* find full path to file */
517 static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
521 const char *file = Jim_GetString(argv[1], NULL);
522 char *full_path = find_file(file);
523 if (full_path == NULL)
525 Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
528 Jim_SetResult(interp, result);
532 static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
536 const char *str = Jim_GetString(argv[1], NULL);
541 static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
547 /* make it a char easier to read code */
551 if (ptr == NULL || interp == NULL || nbytes == 0) {
555 /* do we have to chunk it? */
556 if (ptr[nbytes] == 0)
558 /* no it is a C style string */
559 LOG_USER_N("%s", ptr);
562 /* GRR we must chunk - not null terminated */
572 memcpy(chunk, ptr, x);
576 LOG_USER_N("%s", chunk);
584 static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
586 /* TCL wants to read... tell him no */
590 static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
601 cp = alloc_vprintf(fmt, ap);
604 LOG_USER_N("%s", cp);
611 static int openocd_jim_fflush(void *cookie)
613 /* nothing to flush */
617 static char* openocd_jim_fgets(char *s, int size, void *cookie)
624 command_context_t* command_init()
626 command_context_t* context = malloc(sizeof(command_context_t));
627 extern unsigned const char startup_tcl[];
629 context->mode = COMMAND_EXEC;
630 context->commands = NULL;
631 context->current_target = 0;
632 context->output_handler = NULL;
633 context->output_handler_priv = NULL;
637 /* Create an interpreter */
638 interp = Jim_CreateInterp();
639 /* Add all the Jim core commands */
640 Jim_RegisterCoreCommands(interp);
643 Jim_CreateCommand(interp, "ocd_find", jim_find, NULL, NULL);
644 Jim_CreateCommand(interp, "echo", jim_echo, NULL, NULL);
646 /* Set Jim's STDIO */
647 interp->cookie_stdin = interp;
648 interp->cookie_stdout = interp;
649 interp->cookie_stderr = interp;
650 interp->cb_fwrite = openocd_jim_fwrite;
651 interp->cb_fread = openocd_jim_fread ;
652 interp->cb_vfprintf = openocd_jim_vfprintf;
653 interp->cb_fflush = openocd_jim_fflush;
654 interp->cb_fgets = openocd_jim_fgets;
659 Jim_EventLoopOnLoad(interp);
661 if (Jim_Eval(interp, startup_tcl)==JIM_ERR)
663 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
664 Jim_PrintErrorMessage(interp);
668 register_command(context, NULL, "sleep", handle_sleep_command,
669 COMMAND_ANY, "sleep for <n> milliseconds");
671 register_command(context, NULL, "fast", handle_fast_command,
672 COMMAND_ANY, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
677 int command_context_mode(command_context_t *cmd_ctx, enum command_mode mode)
680 return ERROR_INVALID_ARGUMENTS;
682 cmd_ctx->mode = mode;
686 /* sleep command sleeps for <n> miliseconds
687 * this is useful in target startup scripts
689 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
691 unsigned long duration = 0;
695 duration = strtoul(args[0], NULL, 0);
696 usleep(duration * 1000);
702 int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
705 return ERROR_COMMAND_SYNTAX_ERROR;
707 fast_and_dangerous = strcmp("enable", args[0])==0;
712 void process_jim_events()
715 static int recursion = 0;
720 Jim_ProcessEvents (interp, JIM_ALL_EVENTS|JIM_DONT_WAIT);
726 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)
728 Jim_CreateCommand(interp, name, cmd, NULL, NULL);
730 /* FIX!!! it would be prettier to invoke add_help_text...
731 accumulate help text in Tcl helptext list. */
732 Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
733 if (Jim_IsShared(helptext))
734 helptext = Jim_DuplicateObj(interp, helptext);
736 Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
738 Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
739 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, name, -1));
741 Jim_ListAppendElement(interp, cmd_entry, cmd_list);
742 Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
743 Jim_ListAppendElement(interp, helptext, cmd_entry);