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"
41 #include <openocd_tcl.h>
43 int fast_and_dangerous = 0;
44 extern command_context_t *active_cmd_ctx;
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);
48 static void tcl_output(void *privData, const char *file, int line, const char *function, const char *string)
50 Jim_Obj *tclOutput=(Jim_Obj *)privData;
52 Jim_AppendString(interp, tclOutput, string, strlen(string));
55 static int script_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
57 /* the private data is stashed in the interp structure */
59 command_context_t *context;
65 target_call_timer_callbacks_now();
66 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
68 c = interp->cmdPrivData;
69 LOG_DEBUG("script_command - %s", c->name);
72 words = malloc(sizeof(char *) * nwords);
73 for (i = 0; i < nwords; i++)
77 words[i] = strdup(Jim_GetString(argv[i], &len));
82 LOG_DEBUG("script_command - %s, argv[%u]=%s", c->name, i, words[i]);
85 /* grab the command context from the associated data */
86 context = Jim_GetAssocData(interp, "context");
87 retval = Jim_GetAssocData(interp, "retval");
88 if (context != NULL && retval != NULL)
90 /* capture log output and return it */
91 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
92 log_add_callback(tcl_output, tclOutput);
94 *retval = run_command(context, c, words, nwords);
96 log_remove_callback(tcl_output, tclOutput);
97 Jim_SetResult(interp, 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 context->output_handler(context, string);
305 void command_print(command_context_t *context, char *format, ...)
310 va_start(ap, format);
312 string = alloc_vprintf(format, ap);
315 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
316 context->output_handler(context, string);
323 int run_command(command_context_t *context, command_t *c, char *words[], int num_words)
326 if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) ))
328 /* Config commands can not run after the config stage */
332 int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
333 if (retval == ERROR_COMMAND_SYNTAX_ERROR)
335 /* Print help for command */
339 /* maximum of two levels :-) */
346 command_run_linef(context, "help {%s%s%s}", t1, t2, t3);
348 else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
350 /* just fall through for a shutdown request */
352 else if (retval != ERROR_OK)
354 /* we do not print out an error message because the command *should*
355 * have printed out an error
357 LOG_DEBUG("Command failed with error code %d", retval);
363 int command_run_line(command_context_t *context, char *line)
365 /* all the parent commands have been registered with the interpreter
366 * so, can just evaluate the line as a script and check for
369 /* run the line thru a script engine */
373 Jim_DeleteAssocData(interp, "context"); /* remove existing */
374 retcode = Jim_SetAssocData(interp, "context", NULL, context);
375 if (retcode != JIM_OK)
378 /* associated the return value */
380 Jim_DeleteAssocData(interp, "retval"); /* remove existing */
381 retcode = Jim_SetAssocData(interp, "retval", NULL, &retval);
382 if (retcode != JIM_OK)
385 retcode = Jim_Eval(interp, line);
386 if (retcode == JIM_ERR) {
387 Jim_PrintErrorMessage(interp);
388 } else if (retcode == JIM_EXIT) {
390 /* exit(Jim_GetExitCode(interp)); */
395 result = Jim_GetString(Jim_GetResult(interp), &reslen);
399 for (i = 0; i < reslen; i += 256)
405 strncpy(buff, result+i, chunk);
407 LOG_USER_N("%s", buff);
409 LOG_USER_N("%s", "\n");
416 int command_run_linef(command_context_t *context, char *format, ...)
418 int retval=ERROR_FAIL;
421 va_start(ap, format);
422 string = alloc_vprintf(format, ap);
425 retval=command_run_line(context, string);
433 void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, const char* line), void *priv)
435 context->output_handler = output_handler;
436 context->output_handler_priv = priv;
439 command_context_t* copy_command_context(command_context_t* context)
441 command_context_t* copy_context = malloc(sizeof(command_context_t));
443 *copy_context = *context;
448 int command_done(command_context_t *context)
456 command_context_t* command_init()
458 command_context_t* context = malloc(sizeof(command_context_t));
460 context->mode = COMMAND_EXEC;
461 context->commands = NULL;
462 context->current_target = 0;
463 context->output_handler = NULL;
464 context->output_handler_priv = NULL;
466 register_command(context, NULL, "sleep", handle_sleep_command,
467 COMMAND_ANY, "sleep for <n> milliseconds");
469 register_command(context, NULL, "fast", handle_fast_command,
470 COMMAND_ANY, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
475 int command_context_mode(command_context_t *cmd_ctx, enum command_mode mode)
478 return ERROR_INVALID_ARGUMENTS;
480 cmd_ctx->mode = mode;
484 /* sleep command sleeps for <n> miliseconds
485 * this is useful in target startup scripts
487 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
489 unsigned long duration = 0;
493 duration = strtoul(args[0], NULL, 0);
494 usleep(duration * 1000);
500 int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
503 return ERROR_COMMAND_SYNTAX_ERROR;
505 fast_and_dangerous = strcmp("enable", args[0])==0;