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 ***************************************************************************/
34 int handle_sleep_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
36 int build_unique_lengths(command_context_t *context, command_t *commands)
40 /* iterate through all commands */
41 for (c = commands; c; c = c->next)
43 /* find out how many characters are required to uniquely identify a command */
44 for (c->unique_len = 1; c->unique_len <= strlen(c->name); c->unique_len++)
48 /* for every command, see if the current length is enough */
49 for (p = commands; p; p = p->next)
51 /* ignore the command itself */
55 /* compare commands up to the current length */
56 if (strncmp(p->name, c->name, c->unique_len) == 0)
60 /* when none of the commands matched, we've found the minimum length required */
65 /* if the current command has children, build the unique lengths for them */
67 build_unique_lengths(context, c->children);
73 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)
77 if (!context || !name)
80 c = malloc(sizeof(command_t));
82 c->name = strdup(name);
88 c->help = strdup(help);
94 /* place command in tree */
100 for (p = parent->children; p && p->next; p = p->next);
106 parent->children = c;
111 if (context->commands)
113 /* find last command */
114 for (p = context->commands; p && p->next; p = p->next);
120 context->commands = c;
124 /* update unique lengths */
125 build_unique_lengths(context, (parent) ? parent : context->commands);
130 int unregister_command(command_context_t *context, char *name)
132 command_t *c, *p = NULL, *c2;
134 if ((!context) || (!name))
135 return ERROR_INVALID_ARGUMENTS;
138 for (c = context->commands; c; c = c->next)
140 if (strcmp(name, c->name) == 0)
149 context->commands = c->next;
152 /* unregister children */
155 for (c2 = c->children; c2; c2 = c2->next)
171 /* remember the last command for unlinking */
178 int parse_line(char *line, char *words[], int max_words)
182 char *word_start = line;
185 while (nwords < max_words - 1)
187 /* check if we reached
189 * a matching closing quote character " or '
190 * we're inside a word but not a quote, and the current character is whitespace
192 if (!*p || *p == inquote || (word_start && !inquote && isspace(*p)))
194 /* we're inside a word or quote, and reached its end*/
197 int len = p - word_start;
200 memcpy(words[nwords] = malloc(len + 1), word_start, len);
201 /* add terminating NUL */
202 words[nwords++][len] = 0;
205 /* we're done parsing the line */
209 /* skip over trailing quote or whitespace*/
210 if (inquote || isspace(*p))
216 else if (*p == '"' || *p == '\'')
218 /* we've reached the beginning of a quote */
224 /* we've reached the beginning of a new word */
228 /* normal character, skip */
236 void command_print(command_context_t *context, char *format, ...)
243 va_start(ap, format);
245 /* process format string */
246 /* TODO: possible bug. va_list is undefined after the first call to vsnprintf */
247 while (!buffer || (n = vsnprintf(buffer, size, format, ap)) >= size)
249 /* increase buffer until it fits the whole string */
250 if (!(p = realloc(buffer, size += 4096)))
256 /* vsnprintf failed */
262 /* process lines in buffer */
264 char *next = strchr(p, '\n');
269 if (context->output_handler)
270 context->output_handler(context, p);
281 int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word)
285 for (c = commands; c; c = c->next)
287 if (strncasecmp(c->name, words[start_word], c->unique_len))
290 if (strncasecmp(c->name, words[start_word], strlen(words[start_word])))
293 if ((c->mode == context->mode) || (c->mode == COMMAND_ANY))
299 command_print(context, "No handler for command");
304 return c->handler(context, c->name, words + start_word + 1, num_words - start_word - 1);
309 if (start_word == num_words - 1)
311 command_print(context, "Incomplete command");
314 return find_and_run_command(context, c->children, words, num_words, start_word + 1);
319 command_print(context, "Command %s not found", words[start_word]);
323 int command_run_line(command_context_t *context, char *line)
326 char *words[128] = {0};
330 if ((!context) || (!line))
331 return ERROR_INVALID_ARGUMENTS;
333 /* skip preceding whitespace */
334 while (isspace(*line))
337 /* empty line, ignore */
343 command_print(context, "%s", line);
346 nwords = parse_line(line, words, sizeof(words) / sizeof(words[0]));
349 retval = find_and_run_command(context, context->commands, words, nwords, 0);
351 return ERROR_INVALID_ARGUMENTS;
353 for (i = 0; i < nwords; i++)
359 int command_run_file(command_context_t *context, FILE *file, enum command_mode mode)
362 int old_command_mode;
365 old_command_mode = context->mode;
366 context->mode = mode;
368 while (fgets(buffer, 4096, file))
373 /* stop processing line after a comment (#, !) or a LF, CR were encountered */
374 if ((p = strpbrk(buffer, "#!\r\n")))
377 /* skip over leading whitespace */
379 while (isspace(*cmd))
382 /* empty (all whitespace) line? */
386 /* search the end of the current line, ignore trailing whitespace */
387 for (p = end = cmd; *p; p++)
393 if (strcasecmp(cmd, "quit") == 0)
397 if (command_run_line(context, cmd) == ERROR_COMMAND_CLOSE_CONNECTION)
401 context->mode = old_command_mode;
406 void command_print_help_line(command_context_t* context, struct command_s *command, int indent)
409 char indents[32] = {0};
410 char *help = "no help available";
414 for (i = 0; i < indent; i+=2)
417 indents[i*2+1] = '-';
421 if ((command->mode == COMMAND_EXEC) || (command->mode == COMMAND_ANY))
424 help = command->help;
426 snprintf(name_buf, 64, command->name);
427 strncat(name_buf, indents, 64);
428 command_print(context, "%20s\t%s", name_buf, help);
431 if (command->children)
433 for (c = command->children; c; c = c->next)
435 command_print_help_line(context, c, indent + 1);
440 int command_print_help(command_context_t* context, char* name, char** args, int argc)
444 for (c = context->commands; c; c = c->next)
448 if (strncasecmp(c->name, args[0], c->unique_len))
451 if (strncasecmp(c->name, args[0], strlen(args[0])))
455 command_print_help_line(context, c, 0);
461 void command_set_output_handler(command_context_t* context, int (*output_handler)(struct command_context_s *context, char* line), void *priv)
463 context->output_handler = output_handler;
464 context->output_handler_priv = priv;
467 command_context_t* copy_command_context(command_context_t* context)
469 command_context_t* copy_context = malloc(sizeof(command_context_t));
471 *copy_context = *context;
476 int command_done(command_context_t *context)
483 command_context_t* command_init()
485 command_context_t* context = malloc(sizeof(command_context_t));
487 context->mode = COMMAND_EXEC;
488 context->commands = NULL;
489 context->current_target = 0;
491 context->output_handler = NULL;
492 context->output_handler_priv = NULL;
494 register_command(context, NULL, "help", command_print_help,
495 COMMAND_EXEC, "display this help");
497 register_command(context, NULL, "sleep", handle_sleep_command,
498 COMMAND_ANY, "sleep for <n> milliseconds");
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);