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;
45 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_time_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 /* forward declaration of jim_command */
50 extern int jim_command(command_context_t *context, char *line);
54 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)
58 if (!context || !name)
61 c = malloc(sizeof(command_t));
63 c->name = strdup(name);
72 /* place command in tree */
78 for (p = parent->children; p && p->next; p = p->next);
89 if (context->commands)
91 /* find last command */
92 for (p = context->commands; p && p->next; p = p->next);
98 context->commands = c;
101 /* accumulate help text in Tcl helptext list. */
102 Jim_Obj *helptext=Jim_GetGlobalVariableStr(interp, "ocd_helptext", JIM_ERRMSG);
103 if (Jim_IsShared(helptext))
104 helptext = Jim_DuplicateObj(interp, helptext);
105 Jim_Obj *cmd_entry=Jim_NewListObj(interp, NULL, 0);
107 Jim_Obj *cmd_list=Jim_NewListObj(interp, NULL, 0);
109 /* maximum of two levels :-) */
112 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->parent->name, -1));
114 Jim_ListAppendElement(interp, cmd_list, Jim_NewStringObj(interp, c->name, -1));
116 Jim_ListAppendElement(interp, cmd_entry, cmd_list);
117 Jim_ListAppendElement(interp, cmd_entry, Jim_NewStringObj(interp, help, -1));
118 Jim_ListAppendElement(interp, helptext, cmd_entry);
122 int unregister_all_commands(command_context_t *context)
130 while(NULL != context->commands)
132 c = context->commands;
134 while(NULL != c->children)
137 c->children = c->children->next;
144 context->commands = context->commands->next;
155 int unregister_command(command_context_t *context, char *name)
157 command_t *c, *p = NULL, *c2;
159 if ((!context) || (!name))
160 return ERROR_INVALID_ARGUMENTS;
163 for (c = context->commands; c; c = c->next)
165 if (strcmp(name, c->name) == 0)
174 context->commands = c->next;
177 /* unregister children */
180 for (c2 = c->children; c2; c2 = c2->next)
192 /* remember the last command for unlinking */
199 int parse_line(char *line, char *words[], int max_words)
203 char *word_start = line;
206 while (nwords < max_words - 1)
208 /* check if we reached
210 * a matching closing quote character " or '
211 * we're inside a word but not a quote, and the current character is whitespace
213 if (!*p || *p == inquote || (word_start && !inquote && isspace(*p)))
215 /* we're inside a word or quote, and reached its end*/
221 /* This will handle extra whitespace within quotes */
222 while (isspace(*word_start)&&(word_start<word_end))
224 while (isspace(*(word_end-1))&&(word_start<word_end))
226 len = word_end - word_start;
231 memcpy(words[nwords] = malloc(len + 1), word_start, len);
232 /* add terminating NUL */
233 words[nwords++][len] = 0;
236 /* we're done parsing the line */
240 /* skip over trailing quote or whitespace*/
241 if (inquote || isspace(*p))
249 else if (*p == '"' || *p == '\'')
251 /* we've reached the beginning of a quote */
257 /* we've reached the beginning of a new word */
261 /* normal character, skip */
269 void command_output_text(command_context_t *context, const char *data)
271 if( context && context->output_handler && data ){
272 context->output_handler( context, data );
276 void command_print_n(command_context_t *context, char *format, ...)
281 va_start(ap, format);
283 string = alloc_vprintf(format, ap);
286 context->output_handler(context, string);
293 void command_print(command_context_t *context, char *format, ...)
298 va_start(ap, format);
300 string = alloc_vprintf(format, ap);
303 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
304 context->output_handler(context, string);
311 command_t *find_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word, int *new_start_word)
315 for (c = commands; c; c = c->next)
317 if (strcasecmp(c->name, words[start_word]))
320 if ((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) )
330 *new_start_word=start_word;
336 if (start_word == num_words - 1)
340 return find_command(context, c->children, words, num_words, start_word + 1, new_start_word);
347 int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words)
351 c = find_command(context, commands, words, num_words, start_word, &start_word);
354 /* just return command not found */
355 return ERROR_COMMAND_NOTFOUND;
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);
376 else if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
378 /* just fall through for a shutdown request */
380 else if (retval != ERROR_OK)
382 /* we do not print out an error message because the command *should*
383 * have printed out an error
385 LOG_DEBUG("Command failed with error code %d", retval);
391 int command_run_line_internal(command_context_t *context, char *line)
393 LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
396 char *words[128] = {0};
400 /* skip preceding whitespace */
401 while (isspace(*line))
404 /* empty line, ignore */
408 /* ignore comments */
409 if (*line && (line[0] == '#'))
412 LOG_DEBUG("%s", line);
414 nwords = parse_line(line, words, sizeof(words) / sizeof(words[0]));
418 retval = find_and_run_command(context, context->commands, words, nwords);
421 return ERROR_INVALID_ARGUMENTS;
423 for (i = 0; i < nwords; i++)
429 int command_run_line(command_context_t *context, char *line)
431 /* if a command is unknown to the "unknown" proc in tcl/commands.tcl will
432 * redirect it to OpenOCD.
434 * This avoids having to type the "openocd" prefix and makes OpenOCD
435 * commands "native" to Tcl.
437 return jim_command(context, line);
441 int command_run_linef(command_context_t *context, char *format, ...)
443 int retval=ERROR_FAIL;
446 va_start(ap, format);
447 string = alloc_vprintf(format, ap);
450 retval=command_run_line(context, string);
458 void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, const char* line), void *priv)
460 context->output_handler = output_handler;
461 context->output_handler_priv = priv;
464 command_context_t* copy_command_context(command_context_t* context)
466 command_context_t* copy_context = malloc(sizeof(command_context_t));
468 *copy_context = *context;
473 int command_done(command_context_t *context)
481 command_context_t* command_init()
483 command_context_t* context = malloc(sizeof(command_context_t));
485 context->mode = COMMAND_EXEC;
486 context->commands = NULL;
487 context->current_target = 0;
488 context->output_handler = NULL;
489 context->output_handler_priv = NULL;
491 register_command(context, NULL, "sleep", handle_sleep_command,
492 COMMAND_ANY, "sleep for <n> milliseconds");
494 register_command(context, NULL, "time", handle_time_command,
495 COMMAND_ANY, "time <cmd + args> - execute <cmd + args> and print time it took");
497 register_command(context, NULL, "fast", handle_fast_command,
498 COMMAND_ANY, "fast <enable/disable> - place at beginning of config files. Sets defaults to fast and dangerous.");
503 /* sleep command sleeps for <n> miliseconds
504 * this is useful in target startup scripts
506 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
508 unsigned long duration = 0;
512 duration = strtoul(args[0], NULL, 0);
513 usleep(duration * 1000);
519 int handle_fast_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
522 return ERROR_COMMAND_SYNTAX_ERROR;
524 fast_and_dangerous = strcmp("enable", args[0])==0;
530 int handle_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
538 return ERROR_COMMAND_SYNTAX_ERROR;
540 duration_start_measure(&duration);
542 retval = find_and_run_command(cmd_ctx, cmd_ctx->commands, args, argc);
543 if (retval == ERROR_COMMAND_NOTFOUND)
545 command_print(cmd_ctx, "Command %s not found", args[0]);
548 duration_stop_measure(&duration, &duration_text);
550 t=duration.duration.tv_sec;
551 t+=((float)duration.duration.tv_usec / 1000000.0);
552 command_print(cmd_ctx, "%s took %fs", args[0], t);