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, "openocd_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("%s%s%s", t1, t2, t3);
182 Jim_CreateCommand(interp, full_name, script_command, c, NULL);
183 free((void *)full_name);
185 /* accumulate help text in Tcl helptext list. */
186 Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
187 if (Jim_IsShared(helptext))
188 helptext = Jim_DuplicateObj(interp, helptext);
189 Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
191 Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
193 /* maximum of two levels :-) */
196 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->parent->name, -1));
198 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->name, -1));
200 Jim_ListAppendElement(interp, cmd_entry, cmd_list);
201 Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
202 Jim_ListAppendElement(interp, helptext, cmd_entry);
206 int unregister_all_commands(command_context_t *context)
213 while(NULL != context->commands)
215 c = context->commands;
217 while(NULL != c->children)
220 c->children = c->children->next;
227 context->commands = context->commands->next;
238 int unregister_command(command_context_t *context, char *name)
240 command_t *c, *p = NULL, *c2;
242 if ((!context) || (!name))
243 return ERROR_INVALID_ARGUMENTS;
246 for (c = context->commands; c; c = c->next)
248 if (strcmp(name, c->name) == 0)
257 context->commands = c->next;
260 /* unregister children */
263 for (c2 = c->children; c2; c2 = c2->next)
275 /* remember the last command for unlinking */
282 void command_output_text(command_context_t *context, const char *data)
284 if( context && context->output_handler && data ){
285 context->output_handler( context, data );
289 void command_print_n(command_context_t *context, char *format, ...)
294 va_start(ap, format);
296 string = alloc_vprintf(format, ap);
299 /* we want this collected in the log + we also want to pick it up as a tcl return
302 * The latter bit isn't precisely neat, but will do for now.
304 LOG_USER_N("%s", string);
305 // We already printed it above
306 //command_output_text(context, string);
313 void command_print(command_context_t *context, char *format, ...)
318 va_start(ap, format);
320 string = alloc_vprintf(format, ap);
323 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
324 /* we want this collected in the log + we also want to pick it up as a tcl return
327 * The latter bit isn't precisely neat, but will do for now.
329 LOG_USER_N("%s", string);
330 // We already printed it above
331 //command_output_text(context, string);
338 int run_command(command_context_t *context, command_t *c, char *words[], int num_words)
341 if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) ))
343 /* Config commands can not run after the config stage */
347 int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
348 if (retval == ERROR_COMMAND_SYNTAX_ERROR)
350 /* Print help for command */
354 /* maximum of two levels :-) */
361 command_run_linef(context, "help {%s%s%s}", t1, t2, t3);
363 else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
365 /* just fall through for a shutdown request */
367 else if (retval != ERROR_OK)
369 /* we do not print out an error message because the command *should*
370 * have printed out an error
372 LOG_DEBUG("Command failed with error code %d", retval);
378 int command_run_line(command_context_t *context, char *line)
380 /* all the parent commands have been registered with the interpreter
381 * so, can just evaluate the line as a script and check for
384 /* run the line thru a script engine */
387 Jim_DeleteAssocData(interp, "context"); /* remove existing */
388 retcode = Jim_SetAssocData(interp, "context", NULL, context);
389 if (retcode != JIM_OK)
392 /* associated the return value */
394 Jim_DeleteAssocData(interp, "retval"); /* remove existing */
395 retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
396 if (retcode != JIM_OK)
399 retcode = Jim_Eval(interp, line);
400 if (retcode == JIM_ERR) {
401 if (retval!=ERROR_COMMAND_CLOSE_CONNECTION)
403 /* We do not print the connection closed error message */
404 Jim_PrintErrorMessage(interp);
406 if (retval==ERROR_OK)
408 /* It wasn't a low level OpenOCD command that failed */
412 } else if (retcode == JIM_EXIT) {
414 /* exit(Jim_GetExitCode(interp)); */
419 result = Jim_GetString(Jim_GetResult(interp), &reslen);
423 for (i = 0; i < reslen; i += 256)
429 strncpy(buff, result+i, chunk);
431 LOG_USER_N("%s", buff);
433 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);
454 void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, const char* line), void *priv)
456 context->output_handler = output_handler;
457 context->output_handler_priv = priv;
460 command_context_t* copy_command_context(command_context_t* context)
462 command_context_t* copy_context = malloc(sizeof(command_context_t));
464 *copy_context = *context;
469 int command_done(command_context_t *context)
477 /* find full path to file */
478 static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
482 const char *file = Jim_GetString(argv[1], NULL);
483 char *full_path = find_file(file);
484 if (full_path == NULL)
486 Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path));
489 Jim_SetResult(interp, result);
493 static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
497 const char *str = Jim_GetString(argv[1], NULL);
502 static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
507 command_context_t *context;
509 /* make it a char easier to read code */
513 if (ptr == NULL || interp == NULL || nbytes == 0) {
517 context = Jim_GetAssocData(interp, "context");
520 LOG_ERROR("openocd_jim_fwrite: no command context");
521 /* TODO: Where should this go? */
525 /* do we have to chunk it? */
526 if (ptr[nbytes] == 0)
528 /* no it is a C style string */
529 command_output_text(context, ptr);
532 /* GRR we must chunk - not null terminated */
542 memcpy(chunk, ptr, x);
546 command_output_text(context, chunk);
554 static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
556 /* TCL wants to read... tell him no */
560 static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
565 command_context_t *context;
572 context = Jim_GetAssocData(interp, "context");
575 LOG_ERROR("openocd_jim_vfprintf: no command context");
579 cp = alloc_vprintf(fmt, ap);
582 command_output_text(context, cp);
589 static int openocd_jim_fflush(void *cookie)
591 /* nothing to flush */
595 static char* openocd_jim_fgets(char *s, int size, void *cookie)
602 command_context_t* command_init()
604 command_context_t* context = malloc(sizeof(command_context_t));
605 extern unsigned const char startup_tcl[];
607 context->mode = COMMAND_EXEC;
608 context->commands = NULL;
609 context->current_target = 0;
610 context->output_handler = NULL;
611 context->output_handler_priv = NULL;
615 /* Create an interpreter */
616 interp = Jim_CreateInterp();
617 /* Add all the Jim core commands */
618 Jim_RegisterCoreCommands(interp);
621 Jim_CreateCommand(interp, "openocd_find", jim_find, NULL, NULL);
622 Jim_CreateCommand(interp, "echo", jim_echo, NULL, NULL);
624 /* Set Jim's STDIO */
625 interp->cookie_stdin = interp;
626 interp->cookie_stdout = interp;
627 interp->cookie_stderr = interp;
628 interp->cb_fwrite = openocd_jim_fwrite;
629 interp->cb_fread = openocd_jim_fread ;
630 interp->cb_vfprintf = openocd_jim_vfprintf;
631 interp->cb_fflush = openocd_jim_fflush;
632 interp->cb_fgets = openocd_jim_fgets;
636 if (Jim_Eval(interp, startup_tcl)==JIM_ERR)
638 LOG_ERROR("Failed to run startup.tcl (embedded into OpenOCD compile time)");
639 Jim_PrintErrorMessage(interp);
643 register_command(context, NULL, "sleep", handle_sleep_command,
644 COMMAND_ANY, "sleep for <n> milliseconds");
646 register_command(context, NULL, "fast", handle_fast_command,
647 COMMAND_ANY, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
652 int command_context_mode(command_context_t *cmd_ctx, enum command_mode mode)
655 return ERROR_INVALID_ARGUMENTS;
657 cmd_ctx->mode = mode;
661 /* sleep command sleeps for <n> miliseconds
662 * this is useful in target startup scripts
664 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
666 unsigned long duration = 0;
670 duration = strtoul(args[0], NULL, 0);
671 usleep(duration * 1000);
677 int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
680 return ERROR_COMMAND_SYNTAX_ERROR;
682 fast_and_dangerous = strcmp("enable", args[0])==0;
687 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)
689 Jim_CreateCommand(interp, name, cmd, NULL, NULL);
691 /* FIX!!! it would be prettier to invoke add_help_text...
692 accumulate help text in Tcl helptext list. */
693 Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
694 if (Jim_IsShared(helptext))
695 helptext = Jim_DuplicateObj(interp, helptext);
697 Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
699 Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
700 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, name, -1));
702 Jim_ListAppendElement(interp, cmd_entry, cmd_list);
703 Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
704 Jim_ListAppendElement(interp, helptext, cmd_entry);