From 0689e3dd6752f45f493eceb3edf040fbc7849846 Mon Sep 17 00:00:00 2001
From: oharboe <oharboe@b42882b7-edfa-0310-969c-e2dbd0fdcd60>
Date: Thu, 28 Feb 2008 08:11:18 +0000
Subject: - Added TARGET_REQ_DEBUGCHAR target_request debugmsg. This   provides
 a better impeadance match for debug output char fn's, e.g. eCos. - Line
 endings are now added at the caller site of command_print*(). command_print()
   still adds a line ending - echo of commands in scripts are now available
 via debug_level instead of   forced echo - Added a USER_SAMELINE() for
 printing without a lineend.

git-svn-id: svn://svn.berlios.de/openocd/trunk@364 b42882b7-edfa-0310-969c-e2dbd0fdcd60
---
 src/helper/command.c        | 47 ++++++++++++++++++++++-----------------------
 src/helper/command.h        |  2 +-
 src/helper/interpreter.c    |  6 ------
 src/helper/log.c            | 47 ++++++++++++++++++++++++++++++++-------------
 src/helper/log.h            | 21 ++++++++++++++------
 src/server/gdb_server.c     | 13 ++++++-------
 src/server/telnet_server.c  | 35 +++++++++++++++++++--------------
 src/target/target_request.c | 10 ++++++++++
 src/target/target_request.h |  1 +
 9 files changed, 111 insertions(+), 71 deletions(-)

(limited to 'src')

diff --git a/src/helper/command.c b/src/helper/command.c
index 15cddcdc..3d496b68 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -260,7 +260,7 @@ int parse_line(char *line, char *words[], int max_words)
 	return nwords;
 }
 
-void command_print(command_context_t *context, char *format, ...)
+static void command_printv(command_context_t *context, char *format, va_list ap)
 {
 	char *buffer = NULL;
 	int n, size = 0;
@@ -269,8 +269,6 @@ void command_print(command_context_t *context, char *format, ...)
 	/* process format string */
 	for (;;)
 	{
-		va_list ap;
-		va_start(ap, format);
 		if (!buffer || (n = vsnprintf(buffer, size, format, ap)) >= size)
 		{
 			/* increase buffer until it fits the whole string */
@@ -279,16 +277,13 @@ void command_print(command_context_t *context, char *format, ...)
 				/* gotta free up */
 				if (buffer)
 					free(buffer);
-				va_end(ap);
 				return;
 			}
 	
 			buffer = p;
 			
-			va_end(ap);
 			continue;
 		}
-		va_end(ap);
 		break;
 	}
 	
@@ -300,23 +295,31 @@ void command_print(command_context_t *context, char *format, ...)
 		return;
 	}
 
-	p = buffer;
+	context->output_handler(context, buffer);
 	
-	/* process lines in buffer */
-	do {
-		char *next = strchr(p, '\n');
-		
-		if (next)
-			*next++ = 0;
+	if (buffer)
+		free(buffer);
+}
 
-		if (context->output_handler)
-			context->output_handler(context, p);
+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);
+}
 
-		p = next;
-	} while (p);
+void command_print(command_context_t *context, char *format, ...)
+{
+	char *t=malloc(strlen(format)+2);
+	strcpy(t, format);
+	strcat(t, "\n");
+	va_list ap;
+	va_start(ap, format);
+	command_printv(context, t, ap); 
+	va_end(ap);
+	free(t);
 	
-	if (buffer)
-		free(buffer);
 }
 
 int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word)
@@ -396,10 +399,7 @@ int command_run_line(command_context_t *context, char *line)
 	if (*line && (line[0] == '#'))
 		return ERROR_OK;
 	
-	if (context->echo)
-	{
-		command_print(context, "%s", line);
-	}
+	DEBUG("%s", line);
 
 	nwords = parse_line(line, words, sizeof(words) / sizeof(words[0]));
 	
@@ -550,7 +550,6 @@ command_context_t* command_init()
 	context->mode = COMMAND_EXEC;
 	context->commands = NULL;
 	context->current_target = 0;
-	context->echo = 0;
 	context->output_handler = NULL;
 	context->output_handler_priv = NULL;
 	
diff --git a/src/helper/command.h b/src/helper/command.h
index de90d4ae..3acb8b18 100644
--- a/src/helper/command.h
+++ b/src/helper/command.h
@@ -34,7 +34,6 @@ typedef struct command_context_s
 	enum command_mode mode;
 	struct command_s *commands;
 	int current_target;
-	int echo;
 	int (*output_handler)(struct command_context_s *context, char* line);
 	void *output_handler_priv;
 } command_context_t;
@@ -58,6 +57,7 @@ extern command_context_t* copy_command_context(command_context_t* context);
 extern command_context_t* command_init();
 extern int command_done(command_context_t *context);
 extern void command_print(command_context_t *context, char *format, ...);
+extern void command_print_sameline(command_context_t *context, char *format, ...);
 extern int command_run_line(command_context_t *context, char *line);
 extern int command_run_file(command_context_t *context, FILE *file, enum command_mode mode);
 
diff --git a/src/helper/interpreter.c b/src/helper/interpreter.c
index aa331fda..abf7b093 100644
--- a/src/helper/interpreter.c
+++ b/src/helper/interpreter.c
@@ -217,7 +217,6 @@ int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **ar
 int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
 	FILE *script_file;
-	int echo;
 
 	if (argc != 1)
 		return ERROR_COMMAND_SYNTAX_ERROR;
@@ -229,14 +228,9 @@ int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **a
 		command_print(cmd_ctx, "couldn't open script file %s", args[0]);
 		return ERROR_OK;
 	}
-
-	echo = cmd_ctx->echo;
-	cmd_ctx->echo = 1;
 	
 	command_run_file(cmd_ctx, script_file, cmd_ctx->mode);
 	
-	cmd_ctx->echo = echo;
-	
 	fclose(script_file);
 
 	return ERROR_OK;
diff --git a/src/helper/log.c b/src/helper/log.c
index a5432aa9..c55fd572 100644
--- a/src/helper/log.c
+++ b/src/helper/log.c
@@ -46,20 +46,14 @@ static char *log_strings[5] =
 	"Debug:  "
 };
 
-void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
+static int count = 0;
+
+static void log_printfv(enum log_levels level, const char *file, int line, const char *function, const char *format, va_list args)
 {
-	static int count = 0;
-	count++;
-	va_list args;
 	char buffer[512];
 	log_callback_t *cb;
 
-	if (level > debug_level)
-		return;
-
-	va_start(args, format);
 	vsnprintf(buffer, 512, format, args);
-	va_end(args);
 
 	if (level == LOG_OUTPUT)
 	{
@@ -76,8 +70,8 @@ void log_printf(enum log_levels level, const char *file, int line, const char *f
 	if (debug_level >= LOG_DEBUG)
 	{
 		/* print with count and time information */
-		time_t t=time(NULL)-start;
-		fprintf(log_output, "%s %d %ld %s:%d %s(): %s\n", log_strings[level+1], count, t, file, line, function, buffer);
+		int t=(int)(time(NULL)-start);
+		fprintf(log_output, "%s %d %d %s:%d %s(): %s\n", log_strings[level+1], count, t, file, line, function, buffer);
 	}
 	else
 	{
@@ -92,13 +86,40 @@ void log_printf(enum log_levels level, const char *file, int line, const char *f
 	{
 		for (cb = log_callbacks; cb; cb = cb->next)
 		{
-			va_start(args, format);
 			cb->fn(cb->priv, file, line, function, format, args);
-			va_end(args);
 		}
 	}
 }
 
+void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
+{
+	count++;
+	if (level > debug_level)
+		return;
+
+	va_list args;
+	va_start(args, format);
+	log_printfv(level, file, line, function, format, args);
+	va_end(args);
+	
+}
+
+void log_printfnl(enum log_levels level, const char *file, int line, const char *function, const char *format, ...)
+{
+	count++;
+	if (level > debug_level)
+		return;
+	
+	char *t=malloc(strlen(format)+2);
+	strcpy(t, format);
+	strcat(t, "\n");
+	
+	va_list args;
+	va_start(args, format);
+	log_printfv(level, file, line, function, t, args);
+	va_end(args);
+}
+
 /* change the current debug level on the fly
  * 0: only ERRORS
  * 1: + WARNINGS
diff --git a/src/helper/log.h b/src/helper/log.h
index 6c7c3de6..48b43d8e 100644
--- a/src/helper/log.h
+++ b/src/helper/log.h
@@ -46,7 +46,10 @@ enum log_levels
 
 extern void log_printf(enum log_levels level, const char *file, int line, 
 	const char *function, const char *format, ...) 
-	__attribute__ ((format (printf, 5, 6)));
+__attribute__ ((format (printf, 5, 6)));
+extern void log_printfnl(enum log_levels level, const char *file, int line, 
+	const char *function, const char *format, ...) 
+__attribute__ ((format (printf, 5, 6)));
 extern int log_register_commands(struct command_context_s *cmd_ctx);
 extern int log_init(struct command_context_s *cmd_ctx);
 extern int set_log_output(struct command_context_s *cmd_ctx, FILE *output);
@@ -71,34 +74,40 @@ extern int debug_level;
 /* Avoid fn call and building parameter list if we're not outputting the information.
  * Matters on feeble CPUs for DEBUG/INFO statements that are involved frequently */
 
+
 #define DEBUG(expr ...) \
 	do { if (debug_level >= LOG_DEBUG) \
-		log_printf (LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, expr); \
+		log_printfnl (LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, expr); \
 	} while(0)
 
 #define INFO(expr ...) \
 	do { if (debug_level >= LOG_INFO) \
-		log_printf (LOG_INFO, __FILE__, __LINE__, __FUNCTION__, expr); \
+		log_printfnl (LOG_INFO, __FILE__, __LINE__, __FUNCTION__, expr); \
 	} while(0)
 
 #define WARNING(expr ...) \
 	do { \
-		log_printf (LOG_WARNING, __FILE__, __LINE__, __FUNCTION__, expr); \
+		log_printfnl (LOG_WARNING, __FILE__, __LINE__, __FUNCTION__, expr); \
 	} while(0)
 
 #define ERROR(expr ...) \
 	do { \
-		log_printf (LOG_ERROR, __FILE__, __LINE__, __FUNCTION__, expr); \
+		log_printfnl (LOG_ERROR, __FILE__, __LINE__, __FUNCTION__, expr); \
 	} while(0)
 
 #define USER(expr ...) \
+	do { \
+		log_printfnl (LOG_USER, __FILE__, __LINE__, __FUNCTION__, expr); \
+	} while(0)
+
+#define USER_SAMELINE(expr ...) \
 	do { \
 		log_printf (LOG_USER, __FILE__, __LINE__, __FUNCTION__, expr); \
 	} while(0)
 
 #define OUTPUT(expr ...) \
 	do { \
-		log_printf (LOG_OUTPUT, __FILE__, __LINE__, __FUNCTION__, expr); \
+		log_printfnl (LOG_OUTPUT, __FILE__, __LINE__, __FUNCTION__, expr); \
 	} while(0)
 
 
diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c
index 335a4cd2..6c9936ee 100644
--- a/src/server/gdb_server.c
+++ b/src/server/gdb_server.c
@@ -511,16 +511,14 @@ int gdb_output_con(connection_t *connection, char* line)
 
 	bin_size = strlen(line);
 
-	hex_buffer = malloc(bin_size*2 + 4);
+	hex_buffer = malloc(bin_size*2 + 2);
 
 	hex_buffer[0] = 'O';
 	for (i=0; i<bin_size; i++)
 		snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
-	hex_buffer[bin_size*2+1] = '0';
-	hex_buffer[bin_size*2+2] = 'a';
-	hex_buffer[bin_size*2+3] = 0x0;
+	hex_buffer[bin_size*2+1] = 0;
 
-	gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
+	gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
 
 	free(hex_buffer);
 	return ERROR_OK;
@@ -529,7 +527,7 @@ int gdb_output_con(connection_t *connection, char* line)
 int gdb_output(struct command_context_s *context, char* line)
 {
 	/* this will be dumped to the log and also sent as an O packet if possible */
-	USER(line); 
+	USER_SAMELINE(line); 
 	return ERROR_OK;
 }
 
@@ -605,7 +603,8 @@ int gdb_target_callback_event_handler(struct target_s *target, enum target_event
 
 	return ERROR_OK;
 }
-
+
+
 int gdb_new_connection(connection_t *connection)
 {
 	gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
diff --git a/src/server/telnet_server.c b/src/server/telnet_server.c
index 5cfb0ab4..ea5ec575 100644
--- a/src/server/telnet_server.c
+++ b/src/server/telnet_server.c
@@ -77,8 +77,26 @@ int telnet_prompt(connection_t *connection)
 
 int telnet_outputline(connection_t *connection, char* line)
 {
-	telnet_write(connection, line, strlen(line));
-	return telnet_write(connection, "\r\n\0", 3);
+	
+	/* process lines in buffer */
+	char *p=line;
+	do {
+		char *next = strchr(p, '\n');
+		
+		if (next)
+			*next++ = 0;
+
+		
+		telnet_write(connection, p, strlen(p));
+		if (next)
+		{
+			telnet_write(connection, "\r\n\0", 3);
+		}
+
+		p = next;
+	} while (p);
+	
+	return ERROR_OK;
 }
 
 int telnet_output(struct command_context_s *cmd_ctx, char* line)
@@ -93,20 +111,9 @@ void telnet_log_callback(void *priv, const char *file, int line,
 {
 	connection_t *connection = priv;
 	char *t = alloc_printf(format, args);
-	char *t2;
 	if (t == NULL)
 		return;
-	t2=t;
-	char *endline;
-	do 
-	{
-		if ((endline=strchr(t2, '\n'))!=NULL)
-		{
-			*endline=0;
-		}
-		telnet_outputline(connection, t2);
-		t2=endline+1;
-	} while (endline);
+	telnet_outputline(connection, t);
 	
 	free(t);
 }
diff --git a/src/target/target_request.c b/src/target/target_request.c
index e23a0f7d..020ee790 100644
--- a/src/target/target_request.c
+++ b/src/target/target_request.c
@@ -53,6 +53,13 @@ int target_asciimsg(target_t *target, u32 length)
 	return ERROR_OK;
 }
 
+int target_charmsg(target_t *target, u8 msg)
+{
+	USER_SAMELINE("%c", msg);
+	
+	return ERROR_OK;
+}
+
 int target_hexmsg(target_t *target, int size, u32 length)
 {
 	u8 *data = malloc(CEIL(length * size, 4) * 4);
@@ -122,6 +129,9 @@ int target_request(target_t *target, u32 request)
 				target_hexmsg(target, (request & 0xff00) >> 8, (request & 0xffff0000) >> 16);
 			}
 			break;
+		case TARGET_REQ_DEBUGCHAR:
+			target_charmsg(target, (request & 0x00ff0000) >> 16);
+			break;
 /*		case TARGET_REQ_SEMIHOSTING:
  *			break;
  */
diff --git a/src/target/target_request.h b/src/target/target_request.h
index 6aacaed7..0547c1d3 100644
--- a/src/target/target_request.h
+++ b/src/target/target_request.h
@@ -26,6 +26,7 @@ typedef enum target_req_cmd
 {
 	TARGET_REQ_TRACEMSG,
 	TARGET_REQ_DEBUGMSG,
+	TARGET_REQ_DEBUGCHAR,
 /*	TARGET_REQ_SEMIHOSTING, */
 } target_req_cmd_t;
 
-- 
cgit v1.2.3