1 /***************************************************************************
\r
2 * Copyright (C) 2005 by Dominic Rath *
\r
3 * Dominic.Rath@gmx.de *
\r
5 * part of this file is taken from libcli (libcli.sourceforge.net) *
\r
6 * Copyright (C) David Parrish (david@dparrish.com) *
\r
8 * This program is free software; you can redistribute it and/or modify *
\r
9 * it under the terms of the GNU General Public License as published by *
\r
10 * the Free Software Foundation; either version 2 of the License, or *
\r
11 * (at your option) any later version. *
\r
13 * This program is distributed in the hope that it will be useful, *
\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
16 * GNU General Public License for more details. *
\r
18 * You should have received a copy of the GNU General Public License *
\r
19 * along with this program; if not, write to the *
\r
20 * Free Software Foundation, Inc., *
\r
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
\r
22 ***************************************************************************/
\r
23 #ifdef HAVE_CONFIG_H
\r
27 #include "replacements.h"
\r
29 #include "command.h"
\r
32 #include "time_support.h"
\r
41 void command_print_help_line(command_context_t* context, struct command_s *command, int indent);
\r
43 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
44 int handle_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
46 int build_unique_lengths(command_context_t *context, command_t *commands)
\r
50 /* iterate through all commands */
\r
51 for (c = commands; c; c = c->next)
\r
53 /* find out how many characters are required to uniquely identify a command */
\r
54 for (c->unique_len = 1; c->unique_len <= strlen(c->name); c->unique_len++)
\r
58 /* for every command, see if the current length is enough */
\r
59 for (p = commands; p; p = p->next)
\r
61 /* ignore the command itself */
\r
65 /* compare commands up to the current length */
\r
66 if (strncmp(p->name, c->name, c->unique_len) == 0)
\r
70 /* when none of the commands matched, we've found the minimum length required */
\r
75 /* if the current command has children, build the unique lengths for them */
\r
77 build_unique_lengths(context, c->children);
\r
83 /* Avoid evaluating this each time we add a command. Reduces overhead from O(n^2) to O(n).
\r
84 * Makes a difference on ARM7 types machines and is not observable on GHz machines.
\r
86 static int unique_length_dirty = 1;
\r
88 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)
\r
91 unique_length_dirty = 1;
\r
93 if (!context || !name)
\r
96 c = malloc(sizeof(command_t));
\r
98 c->name = strdup(name);
\r
100 c->children = NULL;
\r
101 c->handler = handler;
\r
104 c->help = strdup(help);
\r
110 /* place command in tree */
\r
113 if (parent->children)
\r
115 /* find last child */
\r
116 for (p = parent->children; p && p->next; p = p->next);
\r
122 parent->children = c;
\r
127 if (context->commands)
\r
129 /* find last command */
\r
130 for (p = context->commands; p && p->next; p = p->next);
\r
136 context->commands = c;
\r
143 int unregister_command(command_context_t *context, char *name)
\r
145 unique_length_dirty = 1;
\r
147 command_t *c, *p = NULL, *c2;
\r
149 if ((!context) || (!name))
\r
150 return ERROR_INVALID_ARGUMENTS;
\r
153 for (c = context->commands; c; c = c->next)
\r
155 if (strcmp(name, c->name) == 0)
\r
157 /* unlink command */
\r
164 context->commands = c->next;
\r
167 /* unregister children */
\r
170 for (c2 = c->children; c2; c2 = c2->next)
\r
179 /* delete command */
\r
186 /* remember the last command for unlinking */
\r
193 int parse_line(char *line, char *words[], int max_words)
\r
197 char *word_start = line;
\r
200 while (nwords < max_words - 1)
\r
202 /* check if we reached
\r
203 * a terminating NUL
\r
204 * a matching closing quote character " or '
\r
205 * we're inside a word but not a quote, and the current character is whitespace
\r
207 if (!*p || *p == inquote || (word_start && !inquote && isspace(*p)))
\r
209 /* we're inside a word or quote, and reached its end*/
\r
215 /* This will handle extra whitespace within quotes */
\r
216 while (isspace(*word_start)&&(word_start<word_end))
\r
218 while (isspace(*(word_end-1))&&(word_start<word_end))
\r
220 len = word_end - word_start;
\r
224 /* copy the word */
\r
225 memcpy(words[nwords] = malloc(len + 1), word_start, len);
\r
226 /* add terminating NUL */
\r
227 words[nwords++][len] = 0;
\r
230 /* we're done parsing the line */
\r
234 /* skip over trailing quote or whitespace*/
\r
235 if (inquote || isspace(*p))
\r
237 while (isspace(*p))
\r
243 else if (*p == '"' || *p == '\'')
\r
245 /* we've reached the beginning of a quote */
\r
251 /* we've reached the beginning of a new word */
\r
255 /* normal character, skip */
\r
263 void command_print(command_context_t *context, char *format, ...)
\r
265 char *buffer = NULL;
\r
269 /* process format string */
\r
273 va_start(ap, format);
\r
274 if (!buffer || (n = vsnprintf(buffer, size, format, ap)) >= size)
\r
276 /* increase buffer until it fits the whole string */
\r
277 if (!(p = realloc(buffer, size += 4096)))
\r
279 /* gotta free up */
\r
295 /* vsnprintf failed */
\r
305 /* process lines in buffer */
\r
307 char *next = strchr(p, '\n');
\r
312 if (context->output_handler)
\r
313 context->output_handler(context, p);
\r
322 int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word)
\r
326 if (unique_length_dirty)
\r
328 unique_length_dirty = 0;
\r
329 /* update unique lengths */
\r
330 build_unique_lengths(context, context->commands);
\r
333 for (c = commands; c; c = c->next)
\r
335 if (strncasecmp(c->name, words[start_word], c->unique_len))
\r
338 if (strncasecmp(c->name, words[start_word], strlen(words[start_word])))
\r
341 if ((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode) )
\r
347 command_print(context, "No handler for command");
\r
352 int retval = c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
\r
353 if (retval == ERROR_COMMAND_SYNTAX_ERROR)
\r
355 command_print(context, "Syntax error:");
\r
356 command_print_help_line(context, c, 0);
\r
363 if (start_word == num_words - 1)
\r
365 command_print(context, "Incomplete command");
\r
368 return find_and_run_command(context, c->children, words, num_words, start_word + 1);
\r
373 command_print(context, "Command %s not found", words[start_word]);
\r
377 int command_run_line(command_context_t *context, char *line)
\r
380 char *words[128] = {0};
\r
384 if ((!context) || (!line))
\r
385 return ERROR_INVALID_ARGUMENTS;
\r
387 /* skip preceding whitespace */
\r
388 while (isspace(*line))
\r
391 /* empty line, ignore */
\r
395 /* ignore comments */
\r
396 if (*line && (line[0] == '#'))
\r
401 command_print(context, "%s", line);
\r
404 nwords = parse_line(line, words, sizeof(words) / sizeof(words[0]));
\r
407 retval = find_and_run_command(context, context->commands, words, nwords, 0);
\r
409 return ERROR_INVALID_ARGUMENTS;
\r
411 for (i = 0; i < nwords; i++)
\r
417 int command_run_file(command_context_t *context, FILE *file, enum command_mode mode)
\r
419 int retval = ERROR_OK;
\r
420 int old_command_mode;
\r
421 char *buffer=malloc(4096);
\r
424 return ERROR_INVALID_ARGUMENTS;
\r
427 old_command_mode = context->mode;
\r
428 context->mode = mode;
\r
430 while (fgets(buffer, 4096, file))
\r
435 /* stop processing line after a comment (#, !) or a LF, CR were encountered */
\r
436 if ((p = strpbrk(buffer, "#!\r\n")))
\r
439 /* skip over leading whitespace */
\r
441 while (isspace(*cmd))
\r
444 /* empty (all whitespace) line? */
\r
448 /* search the end of the current line, ignore trailing whitespace */
\r
449 for (p = end = cmd; *p; p++)
\r
453 /* terminate end */
\r
455 if (strcasecmp(cmd, "quit") == 0)
\r
459 if ((retval = command_run_line(context, cmd)) == ERROR_COMMAND_CLOSE_CONNECTION)
\r
463 context->mode = old_command_mode;
\r
471 void command_print_help_line(command_context_t* context, struct command_s *command, int indent)
\r
474 char indents[32] = {0};
\r
475 char *help = "no help available";
\r
479 for (i = 0; i < indent; i+=2)
\r
481 indents[i*2] = ' ';
\r
482 indents[i*2+1] = '-';
\r
487 help = command->help;
\r
489 snprintf(name_buf, 64, command->name);
\r
490 strncat(name_buf, indents, 64);
\r
491 command_print(context, "%20s\t%s", name_buf, help);
\r
493 if (command->children)
\r
495 for (c = command->children; c; c = c->next)
\r
497 command_print_help_line(context, c, indent + 1);
\r
502 int command_print_help(command_context_t* context, char* name, char** args, int argc)
\r
506 for (c = context->commands; c; c = c->next)
\r
510 if (strncasecmp(c->name, args[0], c->unique_len))
\r
513 if (strncasecmp(c->name, args[0], strlen(args[0])))
\r
517 command_print_help_line(context, c, 0);
\r
523 void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, char* line), void *priv)
\r
525 context->output_handler = output_handler;
\r
526 context->output_handler_priv = priv;
\r
529 command_context_t* copy_command_context(command_context_t* context)
\r
531 command_context_t* copy_context = malloc(sizeof(command_context_t));
\r
533 *copy_context = *context;
\r
535 return copy_context;
\r
538 int command_done(command_context_t *context)
\r
546 command_context_t* command_init()
\r
548 command_context_t* context = malloc(sizeof(command_context_t));
\r
550 context->mode = COMMAND_EXEC;
\r
551 context->commands = NULL;
\r
552 context->current_target = 0;
\r
554 context->output_handler = NULL;
\r
555 context->output_handler_priv = NULL;
\r
557 register_command(context, NULL, "help", command_print_help,
\r
558 COMMAND_EXEC, "display this help");
\r
560 register_command(context, NULL, "sleep", handle_sleep_command,
\r
561 COMMAND_ANY, "sleep for <n> milliseconds");
\r
563 register_command(context, NULL, "time", handle_time_command,
\r
564 COMMAND_ANY, "time <cmd + args> - execute <cmd + args> and print time it took");
\r
569 /* sleep command sleeps for <n> miliseconds
\r
570 * this is useful in target startup scripts
\r
572 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
574 unsigned long duration = 0;
\r
578 duration = strtoul(args[0], NULL, 0);
\r
579 usleep(duration * 1000);
\r
585 int handle_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
588 return ERROR_COMMAND_SYNTAX_ERROR;
\r
590 duration_t duration;
\r
591 char *duration_text;
\r
594 duration_start_measure(&duration);
\r
596 retval = find_and_run_command(cmd_ctx, cmd_ctx->commands, args, argc, 0);
\r
598 duration_stop_measure(&duration, &duration_text);
\r
600 float t=duration.duration.tv_sec;
\r
601 t+=((float)duration.duration.tv_usec / 1000000.0);
\r
602 command_print(cmd_ctx, "%s took %fs", args[0], t);
\r
604 free(duration_text);
\r