summaryrefslogtreecommitdiff
path: root/src/helper/command.c
diff options
context:
space:
mode:
authoroharboe <oharboe@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2008-02-29 11:16:38 +0000
committeroharboe <oharboe@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2008-02-29 11:16:38 +0000
commit67e0aea25850b8286f750e6458e5de741e6cb3b5 (patch)
tree1225ab14f9cb5f79c12540b677a1d8aadcbce0bf /src/helper/command.c
parent4febcd8313cf46bea03bd5eacb3f287f19eb2961 (diff)
downloadopenocd_libswd-67e0aea25850b8286f750e6458e5de741e6cb3b5.tar.gz
openocd_libswd-67e0aea25850b8286f750e6458e5de741e6cb3b5.tar.bz2
openocd_libswd-67e0aea25850b8286f750e6458e5de741e6cb3b5.tar.xz
openocd_libswd-67e0aea25850b8286f750e6458e5de741e6cb3b5.zip
Summary: passing of variable argument list reduced, strings sent to logging are now formatted just once - more efficient.
As a result, ugly string malloc+strcpy are not needed anymore. git-svn-id: svn://svn.berlios.de/openocd/trunk@386 b42882b7-edfa-0310-969c-e2dbd0fdcd60
Diffstat (limited to 'src/helper/command.c')
-rw-r--r--src/helper/command.c67
1 files changed, 20 insertions, 47 deletions
diff --git a/src/helper/command.c b/src/helper/command.c
index 0ec54216..aa71f0ba 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -260,66 +260,39 @@ int parse_line(char *line, char *words[], int max_words)
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_printf(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_printf(format, ap);
+ if (string != NULL)
+ {
+ strcat(string, "\n"); /* alloc_printf 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)