--- a/main.c
+++ b/main.c
@@ -38,6 +38,9 @@
 
 #include "parsers/binary.h"
 #include "parsers/hex.h"
+
+FILE *fp_stdout;
+FILE *fp_stderr;
 
 /* device globals */
 serial_t	*serial		= NULL;
@@ -55,6 +58,7 @@
 int 		eb 		= 0;
 int		dtr_reset	= 0;
 int		cpm_reset_flag	= 0;
+int 	        redirect_stderr_stdout = 0;
 
 int		npages		= 0xFF;
 char		verify		= 0;
@@ -75,6 +79,7 @@
 {
 	system("cpm -N STM8_RESET -V 1");
 	system("cpm -N STM8_RESET -V 0");
+	usleep(50000);
 }
 #endif
 
@@ -95,10 +100,20 @@
 	int ret = 1;
 	parser_err_t perr;
 
-
-	printf("stm8flash based on stm32flash - http://stm32flash.googlecode.com/\n\n");
+	
+
+	fp_stdout = stdout; fp_stderr = stderr;
+
+
+	fprintf(fp_stdout,"stm8flash based on stm32flash - http://stm32flash.googlecode.com/\n\n");
 	if (parse_options(argc, argv) != 0)
 		goto close;
+
+	if(redirect_stderr_stdout)
+	{
+		fp_stdout=fopen("/tmp/stm8flasher.stdout","a");
+		fp_stderr=fopen("/tmp/stm8flasher.stderr","a");	
+	}
 
 	if (wr) {
 		/* first try hex */
@@ -106,7 +121,7 @@
 			parser = &PARSER_HEX;
 			p_st = parser->init();
 			if (!p_st) {
-				fprintf(stderr, "%s Parser failed to initialize\n", parser->name);
+				fprintf(fp_stderr, "%s Parser failed to initialize\n", parser->name);
 				goto close;
 			}
 		}
@@ -122,7 +137,7 @@
 				parser = &PARSER_BINARY;
 				p_st = parser->init();
 				if (!p_st) {
-					fprintf(stderr, "%s Parser failed to initialize\n", parser->name);
+					fprintf(fp_stderr, "%s Parser failed to initialize\n", parser->name);
 					goto close;
 				}
 				perr = parser->open(p_st, filename, 0);
@@ -130,18 +145,18 @@
 
 			/* if still have an error, fail */
 			if (perr != PARSER_ERR_OK) {
-				fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr));
+				fprintf(fp_stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr));
 				if (perr == PARSER_ERR_SYSTEM) perror(filename);
 				goto close;
 			}
 		}
 
-		fprintf(stdout, "Using Parser : %s\n", parser->name);
+		fprintf(fp_stdout, "Using Parser : %s\n", parser->name);
 	} else {
 		parser = &PARSER_BINARY;
 		p_st = parser->init();
 		if (!p_st) {
-			fprintf(stderr, "%s Parser failed to initialize\n", parser->name);
+			fprintf(fp_stderr, "%s Parser failed to initialize\n", parser->name);
 			goto close;
 		}
 	}
@@ -178,18 +193,18 @@
 		cpm_reset();
 	}
 #endif
-
-	printf("Serial Config: %s\n", serial_get_setup_str(serial));
+	
+	fprintf(fp_stdout,"Serial Config: %s\n", serial_get_setup_str(serial));
 	if (!(stm = stm8_init(serial, init_flag))) goto close;
 
-	printf("BL-Version   : 0x%02x\n", stm->bl_version);
-/*	printf("Option 1     : 0x%02x\n", stm->option1);
-	printf("Option 2     : 0x%02x\n", stm->option2);
-	printf("Device ID    : 0x%04x (%s)\n", stm->pid, stm->dev->name);
-	printf("RAM          : %dKiB  (%db reserved by bootloader)\n", (stm->dev->ram_end - 0x20000000) / 1024, stm->dev->ram_start - 0x20000000);
-	printf("Flash        : %dKiB (sector size: %dx%d)\n", (stm->dev->fl_end - stm->dev->fl_start ) / 1024, stm->dev->fl_pps, stm->dev->fl_ps);
-	printf("Option RAM   : %db\n", stm->dev->opt_end - stm->dev->opt_start);
-	printf("System RAM   : %dKiB\n", (stm->dev->mem_end - stm->dev->mem_start) / 1024);
+	fprintf(fp_stdout,"BL-Version   : 0x%02x\n", stm->bl_version);
+/*	fprintf(fp_stdout,"Option 1     : 0x%02x\n", stm->option1);
+	fprintf(fp_stdout,"Option 2     : 0x%02x\n", stm->option2);
+	fprintf(fp_stdout,"Device ID    : 0x%04x (%s)\n", stm->pid, stm->dev->name);
+	fprintf(fp_stdout,"RAM          : %dKiB  (%db reserved by bootloader)\n", (stm->dev->ram_end - 0x20000000) / 1024, stm->dev->ram_start - 0x20000000);
+	fprintf(fp_stdout,"Flash        : %dKiB (sector size: %dx%d)\n", (stm->dev->fl_end - stm->dev->fl_start ) / 1024, stm->dev->fl_pps, stm->dev->fl_ps);
+	fprintf(fp_stdout,"Option RAM   : %db\n", stm->dev->opt_end - stm->dev->opt_start);
+	fprintf(fp_stdout,"System RAM   : %dKiB\n", (stm->dev->mem_end - stm->dev->mem_start) / 1024);
 */
 
 	uint8_t		buffer[256];
@@ -199,57 +214,57 @@
 	int		failed = 0;
 
 	if (rd) {
-		printf("\n");
+		fprintf(fp_stdout,"\n");
 
 		if ((perr = parser->open(p_st, filename, 1)) != PARSER_ERR_OK) {
-			fprintf(stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr));
+			fprintf(fp_stderr, "%s ERROR: %s\n", parser->name, parser_errstr(perr));
 			if (perr == PARSER_ERR_SYSTEM) perror(filename);
 			goto close;
 		}
 
 		addr = stm->dev->fl_start;
-		fprintf(stdout, "\x1B[s");
-		fflush(stdout);
+		fprintf(fp_stdout, "\x1B[s");
+		fflush(fp_stdout);
 		while(addr < stm->dev->fl_end) {
 			uint32_t left	= stm->dev->fl_end - addr;
 			len		= sizeof(buffer) > left ? left : sizeof(buffer);
 			if (!stm8_read_memory(stm, addr, buffer, len)) {
-				fprintf(stderr, "Failed to read memory at address 0x%08x, target write-protected?\n", addr);
+				fprintf(fp_stderr, "Failed to read memory at address 0x%08x, target write-protected?\n", addr);
 				goto close;
 			}
 			assert(parser->write(p_st, buffer, len) == PARSER_ERR_OK);
 			addr += len;
 
-			fprintf(stdout,
+			fprintf(fp_stdout,
 				"\x1B[uRead address 0x%08x (%.2f%%) ",
 				addr,
 				(100.0f / (float)(stm->dev->fl_end - stm->dev->fl_start)) * (float)(addr - stm->dev->fl_start)
 			);
-			fflush(stdout);
-		}
-		fprintf(stdout,	"Done.\n");
+			fflush(fp_stdout);
+		}
+		fprintf(fp_stdout,	"Done.\n");
 		ret = 0;
 		goto close;
 
 	} else if (wu) {
 
 //FIXME: Write-unprotecting on STM8 ????
-//		fprintf(stdout, "Write-unprotecting flash\n");
+//		fprintf(fp_stdout, "Write-unprotecting flash\n");
 		/* the device automatically performs a reset after the sending the ACK */
 //		reset_flag = 0;
 //		stm8_wunprot_memory(stm);
-//		fprintf(stdout,	"Done.\n");
+//		fprintf(fp_stdout,	"Done.\n");
 //FIXME: END
 	} else if (eb) {
 
 		addr = 0x487E; //OPTBL
 		if (!stm8_write_memory(stm, addr, (uint8_t *)"\x55\xAA", 2)) {
-			fprintf(stderr, "Failed to OPTION Bytes memory at address 0x%08x\n", addr);
+			fprintf(fp_stderr, "Failed to OPTION Bytes memory at address 0x%08x\n", addr);
 			goto close;
 		}
 
 	} else if (wr) {
-		printf("\n");
+		fprintf(fp_stdout,"\n");
 
 		off_t 	offset = 0;
 		ssize_t r;
@@ -259,16 +274,16 @@
 		if(parser == &PARSER_HEX) size -= 0x8000;
 
 		if (size > stm->dev->fl_end - stm->dev->fl_start) {
-			fprintf(stderr,"Size: %d Flash-Start: %x Flash-End: %x\n", size, stm->dev->fl_start, stm->dev->fl_end);
-			fprintf(stderr, "File provided larger then available flash space.\n");
+			fprintf(fp_stderr,"Size: %d Flash-Start: %x Flash-End: %x\n", size, stm->dev->fl_start, stm->dev->fl_end);
+			fprintf(fp_stderr, "File provided larger then available flash space.\n");
 			goto close;
 		}
 
 		stm8_erase_memory(stm, npages);
 
 		addr = stm->dev->fl_start;
-		fprintf(stdout, "\x1B[s");
-		fflush(stdout);
+		fprintf(fp_stdout, "\x1B[s");
+		fflush(fp_stdout);
 		while(addr < stm->dev->fl_end && offset < size) {
 			uint32_t left	= stm->dev->fl_end - addr;
 			len		= sizeof(wbuffer) > left ? left : sizeof(wbuffer);
@@ -281,21 +296,21 @@
 			if(!isMemZero(wbuffer,len))
 			{
 				if (!stm8_write_memory(stm, addr, wbuffer, len)) {
-					fprintf(stderr, "Failed to write memory at address 0x%08x\n", addr);
+					fprintf(fp_stderr, "Failed to write memory at address 0x%08x\n", addr);
 					goto close;
 				}
 			}
 			if (verify) {
 				uint8_t compare[len];
 				if (!stm8_read_memory(stm, addr, compare, len)) {
-					fprintf(stderr, "Failed to read memory at address 0x%08x\n", addr);
+					fprintf(fp_stderr, "Failed to read memory at address 0x%08x\n", addr);
 					goto close;
 				}
 
 				for(r = 0; r < len; ++r)
 					if (wbuffer[r] != compare[r]) {
 						if (failed == retry) {
-							fprintf(stderr, "Failed to verify at address 0x%08x, expected 0x%02x and found 0x%02x\n",
+							fprintf(fp_stderr, "Failed to verify at address 0x%08x, expected 0x%02x and found 0x%02x\n",
 								(uint32_t)(addr + r),
 								wbuffer [r],
 								compare[r]
@@ -312,17 +327,17 @@
 			addr	+= len;
 			offset	+= len;
 
-			fprintf(stdout,
+			fprintf(fp_stdout,
 				"\x1B[uWrote %saddress 0x%08x (%.2f%%) ",
 				verify ? "and verified " : "",
 				addr,
 				(100.0f / size) * offset
 			);
-			fflush(stdout);
-
-		}
-
-		fprintf(stdout,	"Done.\n");
+			fflush(fp_stdout);
+
+		}
+
+		fprintf(fp_stdout,	"Done.\n");
 		ret = 0;
 		goto close;
 	} else
@@ -333,18 +348,18 @@
 		if (execute == 0)
 			execute = stm->dev->fl_start;
 
-		fprintf(stdout, "\nStarting execution at address 0x%08x... ", execute);
-		fflush(stdout);
+		fprintf(fp_stdout, "\nStarting execution at address 0x%08x... ", execute);
+		fflush(fp_stdout);
 		if (stm8_go(stm, execute)) {
 			reset_flag = 0;
-			fprintf(stdout, "done.\n");
+			fprintf(fp_stdout, "done.\n");
 		} else
-			fprintf(stdout, "failed.\n");
+			fprintf(fp_stdout, "failed.\n");
 	}
 
 	if (stm && reset_flag) {
-		fprintf(stdout, "\nResetting device... ");
-		fflush(stdout);
+		fprintf(fp_stdout, "\nResetting device... ");
+		fflush(fp_stdout);
 		if(dtr_reset)
 		{
 			serial_dtr_reset(serial);
@@ -355,30 +370,33 @@
 			usleep(30000);
 #endif		
 		} else {		
-			if (stm8_reset_device(stm))
-				fprintf(stdout, "done.\n");
-			else	fprintf(stdout, "failed.\n");
+			if (stm8_reset_device(stm)) {fprintf(fp_stdout, "done.\n");}
+			else { fprintf(fp_stdout, "failed.\n"); }
 		}
 	}
 
 	if (p_st  ) parser->close(p_st);
 	if (stm   ) stm8_close  (stm);
 	if (serial) serial_close (serial);
-
-	printf("\n");
+//	if (redirect_stderr_stdout)
+	{
+		fclose(fp_stderr);
+		fclose(fp_stdout);
+	}
+	fprintf(fp_stdout,"\n");
 	return ret;
 }
 
 int parse_options(int argc, char *argv[]) {
 	int c;
-	while((c = getopt(argc, argv, "b:r:w:e:vn:g:fchudsl")) != -1) {
+	while((c = getopt(argc, argv, "b:r:w:e:vn:g:fchudsql")) != -1) {
 		switch(c) {
 			case 'b':
 				baudRate = serial_get_baud(strtoul(optarg, NULL, 0));
 				if (baudRate == SERIAL_BAUD_INVALID) {
-					fprintf(stderr,	"Invalid baud rate, valid options are:\n");
+					fprintf(fp_stderr,	"Invalid baud rate, valid options are:\n");
 					for(baudRate = SERIAL_BAUD_1200; baudRate != SERIAL_BAUD_INVALID; ++baudRate)
-						fprintf(stderr, " %d\n", serial_get_baud_int(baudRate));
+						fprintf(fp_stderr, " %d\n", serial_get_baud_int(baudRate));
 					return 1;
 				}
 				break;
@@ -389,7 +407,7 @@
 				wr = wr || c == 'w';
 				eb = eb || c == 'l';
 				if ((rd && wr) || (rd && eb) || (wr && eb) ) {
-					fprintf(stderr, "ERROR: Invalid options, can't read & write or enable bootloader at the same time\n");
+					fprintf(fp_stderr, "ERROR: Invalid options, can't read & write or enable bootloader at the same time\n");
 					return 1;
 				}
 				filename = optarg;
@@ -397,14 +415,14 @@
 			case 'e':
 				npages = strtoul(optarg, NULL, 0);
 				if (npages > 0xFF || npages < 0) {
-					fprintf(stderr, "ERROR: You need to specify a page count between 0 and 255");
+					fprintf(fp_stderr, "ERROR: You need to specify a page count between 0 and 255");
 					return 1;
 				}
 				break;
 			case 'u':
 				wu = 1;
 				if (rd || wr) {
-					fprintf(stderr, "ERROR: Invalid options, can't write unprotect and read/write at the same time\n");
+					fprintf(fp_stderr, "ERROR: Invalid options, can't write unprotect and read/write at the same time\n");
 					return 1;
 				}
 				break;
@@ -434,7 +452,9 @@
 			case 's':
 				cpm_reset_flag = 1;
 				break;
-
+			case 'q':
+				redirect_stderr_stdout = 1;
+				break;
 			case 'h':
 				show_help(argv[0]);
 				return 1;
@@ -443,7 +463,7 @@
 
 	for (c = optind; c < argc; ++c) {
 		if (device) {
-			fprintf(stderr, "ERROR: Invalid parameter specified\n");
+			fprintf(fp_stderr, "ERROR: Invalid parameter specified\n");
 			show_help(argv[0]);
 			return 1;
 		}
@@ -451,13 +471,13 @@
 	}
 
 	if (device == NULL) {
-		fprintf(stderr, "ERROR: Device not specified\n");
+		fprintf(fp_stderr, "ERROR: Device not specified\n");
 		show_help(argv[0]);
 		return 1;
 	}
 
 	if (!wr && verify) {
-		fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n");
+		fprintf(fp_stderr, "ERROR: Invalid usage, -v is only valid when writing\n");
 		show_help(argv[0]);
 		return 1;
 	}
@@ -483,6 +503,7 @@
 #ifdef LANTRONIX_CPM
 		"	-s		OggStreamer/Lantronix XportPRO CPM STM8 Reset\n"
 #endif
+		"	-q		redirect stdout and stderr to /tmp/stm8flasher.stdout(stderr)\n"
 		"	-c		Resume the connection (don't send initial INIT)\n"
 		"			*Baud rate must be kept the same as the first init*\n"
 		"			This is useful if the reset fails\n"