svn commit: trunk/busybox/miscutils

vda at busybox.net vda at busybox.net
Wed Mar 26 20:06:28 UTC 2008


Author: vda
Date: 2008-03-26 13:06:24 -0700 (Wed, 26 Mar 2008)
New Revision: 21509

Log:
fbsplash: fix broken handling of buffered case:
"{echo 45; echo 33; } | { sleep 1; fbsplash -f - ...; }"

function                                             old     new   delta
fb_drawprogressbar                                     -     413    +413
xmalloc_fgetline                                       -      46     +46
xmalloc_reads                                        184     183      -1
xmalloc_getline                                       46       -     -46
fbsplash_main                                       1472    1030    -442
------------------------------------------------------------------------------
(add/remove: 2/1 grow/shrink: 0/2 up/down: 459/-489)          Total: -30 bytes
   text    data     bss     dec     hex filename
 801181     641    7380  809202   c58f2 busybox_old
 801151     641    7380  809172   c58d4 busybox_unstripped



Modified:
   trunk/busybox/miscutils/fbsplash.c


Changeset:
Modified: trunk/busybox/miscutils/fbsplash.c
===================================================================
--- trunk/busybox/miscutils/fbsplash.c	2008-03-26 20:04:27 UTC (rev 21508)
+++ trunk/busybox/miscutils/fbsplash.c	2008-03-26 20:06:24 UTC (rev 21509)
@@ -97,7 +97,7 @@
  *	Draw hollow rectangle on framebuffer
  * \param nx1pos,ny1pos upper left position
  * \param nx2pos,ny2pos down right position
- * \param nred24,ngreen24,nblue24 rgb color
+ * \param nred,ngreen,nblue rgb color
  */
 static void fb_drawrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
 	unsigned char nred, unsigned char ngreen, unsigned char nblue)
@@ -135,7 +135,7 @@
  *	Draw filled rectangle on framebuffer
  * \param nx1pos,ny1pos upper left position
  * \param nx2pos,ny2pos down right position
- * \param nred24,ngreen24,nblue24 rgb color
+ * \param nred,ngreen,nblue rgb color
  */
 static void fb_drawfullrectangle(int nx1pos, int ny1pos, int nx2pos, int ny2pos,
 	unsigned char nred, unsigned char ngreen, unsigned char nblue)
@@ -165,11 +165,12 @@
 
 /**
  *	Draw a progress bar on framebuffer
- * \param nPercent percentage of loading
+ * \param percent percentage of loading
  */
-static void fb_drawprogressbar(unsigned nPercent)
+static void fb_drawprogressbar(unsigned percent)
 {
 	int i, left_x, top_y, width, height;
+
 	// outer box
 	left_x = G.nbar_posx;
 	top_y = G.nbar_posy;
@@ -195,9 +196,9 @@
 					left_x + width, top_y + height,
 			G.nbar_colr, G.nbar_colg, G.nbar_colb);
 
-	if (nPercent > 0) {
+	if (percent > 0) {
 		// actual progress bar
-		width = width*nPercent/100;
+		width = width * percent / 100;
 		i = height;
 		if (height == 0)
 			height++; // divide by 0 is bad
@@ -294,9 +295,9 @@
 	FILE *inifile;
 	char *buf;
 
-	inifile = xfopen(ini_filename, "r");
+	inifile = xfopen_stdin(ini_filename);
 
-	while ((buf = xmalloc_getline(inifile)) != NULL) {
+	while ((buf = xmalloc_fgetline(inifile)) != NULL) {
 		char *value_str;
 		int val;
 
@@ -360,10 +361,8 @@
 int fbsplash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
 {
-	char num_buf[16];
 	const char *fb_device, *ini_filename, *fifo_filename;
-	int fd = fd; // for compiler
-	int len, num;
+	FILE *fp = fp; // for compiler
 	bool bCursorOff;
 
 	INIT_G();
@@ -383,13 +382,8 @@
 	if (!G.image_filename)
 		bb_show_usage();
 
-	if (fifo_filename) {
-		fd = STDIN_FILENO;
-		if (NOT_LONE_DASH(fifo_filename)) {
-			// open command fifo/pipe
-			fd = xopen(fifo_filename, O_RDONLY | O_NOCTTY);
-		}
-	}
+	if (fifo_filename)
+		fp = xfopen_stdin(fifo_filename);
 
 	fb_open(fb_device);
 
@@ -401,44 +395,37 @@
 	fb_drawimage();
 
 	if (fifo_filename) {
-		num = 0;
-		goto draw_bar;
+		unsigned num;
+		char *num_buf;
 
-		while (1) {
-			// block on read, waiting for some input
-			len = safe_read(fd, num_buf, sizeof(num_buf) - 1);
-			if (len <= 0) // EOF/error
-				break;
-			num_buf[len] = '\0';
-			// parse command
+		fb_drawprogressbar(0);
+		// Block on read, waiting for some input.
+		// Use of <stdio.h> style I/O allows to correctly
+		// handle a case when we have many buffered lines
+		// already in the pipe.
+		while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
 			if (strncmp(num_buf, "exit", 4) == 0) {
 				DEBUG_MESSAGE("exit");
 				break;
 			}
 			num = atoi(num_buf);
-			if (isdigit(num_buf[0]) && (num >= 0) && (num <= 100)) {
+			if (isdigit(num_buf[0]) && (num <= 100)) {
 #if DEBUG
 				char strVal[10];
 				sprintf(strVal, "%d", num);
 				DEBUG_MESSAGE(strVal);
 #endif
- draw_bar:
 				fb_drawprogressbar(num);
 			}
+			free(num_buf);
 		}
 		if (bCursorOff) {
 			// restore cursor
 			full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
 		}
 		if (ENABLE_FEATURE_CLEAN_UP)
-			close(fd);
+			fclose(fp);
 	}
 
-#if DEBUG
-	if (ENABLE_FEATURE_CLEAN_UP)
-  		if (G.bdebug_messages)
-   			fclose(G.logfile_fd);
-#endif
-
 	return EXIT_SUCCESS;
 }




More information about the busybox-cvs mailing list