int unregister_command(command_context_t *context, char *name)
{
- unique_length_dirty = 1;
-
command_t *c, *p = NULL, *c2;
+ unique_length_dirty = 1;
+
if ((!context) || (!name))
return ERROR_INVALID_ARGUMENTS;
return nwords;
}
-static void command_printv(command_context_t *context, char *format, va_list ap)
+void command_print_n(command_context_t *context, char *format, ...)
{
- char *buffer = NULL;
- int n, size = 0;
- char *p;
-
- /* process format string */
- for (;;)
- {
- if (!buffer || (n = vsnprintf(buffer, size, format, ap)) >= size)
- {
- /* increase buffer until it fits the whole string */
- if (!(p = realloc(buffer, size += 4096)))
- {
- /* gotta free up */
- if (buffer)
- free(buffer);
- return;
- }
+ char *string;
- buffer = p;
-
- continue;
- }
- break;
- }
-
- /* vsnprintf failed */
- if (n < 0)
+ va_list ap;
+ va_start(ap, format);
+
+ string = alloc_vprintf(format, ap);
+ if (string != NULL)
{
- if (buffer)
- free(buffer);
- return;
+ context->output_handler(context, string);
+ free(string);
}
- context->output_handler(context, buffer);
-
- if (buffer)
- free(buffer);
-}
-
-void command_print_sameline(command_context_t *context, char *format, ...)
-{
- va_list ap;
- va_start(ap, format);
- command_printv(context, format, ap);
va_end(ap);
}
void command_print(command_context_t *context, char *format, ...)
{
- char *t=malloc(strlen(format)+2);
- strcpy(t, format);
- strcat(t, "\n");
+ char *string;
+
va_list ap;
va_start(ap, format);
- command_printv(context, t, ap);
+
+ string = alloc_vprintf(format, ap);
+ if (string != NULL)
+ {
+ strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
+ context->output_handler(context, string);
+ free(string);
+ }
+
va_end(ap);
- free(t);
-
}
int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word)
{
command_t *c;
+ int retval = ERROR_COMMAND_SYNTAX_ERROR;
if (unique_length_dirty)
{
if (!c->handler)
{
command_print(context, "No handler for command");
+ retval = ERROR_COMMAND_SYNTAX_ERROR;
break;
}
else
{
command_print(context, "Syntax error:");
command_print_help_line(context, c, 0);
+ } else if (retval != ERROR_OK)
+ {
+ /* we do not print out an error message because the command *should*
+ * have printed out an error
+ */
+ LOG_DEBUG("Command failed with error code %d", retval);
}
return retval;
}
}
command_print(context, "Command %s not found", words[start_word]);
- return ERROR_OK;
+ return retval;
}
int command_run_line(command_context_t *context, char *line)
if (*line && (line[0] == '#'))
return ERROR_OK;
- DEBUG("%s", line);
+ LOG_DEBUG("%s", line);
nwords = parse_line(line, words, sizeof(words) / sizeof(words[0]));
void command_print_help_line(command_context_t* context, struct command_s *command, int indent)
{
command_t *c;
- char indent_text[indent + 2];
+ char *indent_text=malloc(indent + 2);
+
char *help = "no help available";
char name_buf[64];
- int i;
if (indent)
{
command_print_help_line(context, c, indent + 1);
}
}
+ free(indent_text);
}
int command_print_help_match(command_context_t* context, command_t* c_first, char* name, char** args, int argc)
{
command_t * c;
- int i;
for (c = c_first; c; c = c->next)
{
int handle_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
- if (argc<1)
- return ERROR_COMMAND_SYNTAX_ERROR;
-
duration_t duration;
char *duration_text;
int retval;
+ float t;
+
+ if (argc<1)
+ return ERROR_COMMAND_SYNTAX_ERROR;
duration_start_measure(&duration);
duration_stop_measure(&duration, &duration_text);
- float t=duration.duration.tv_sec;
+ t=duration.duration.tv_sec;
t+=((float)duration.duration.tv_usec / 1000000.0);
command_print(cmd_ctx, "%s took %fs", args[0], t);