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
This commit is contained in:
Denis Vlasenko 2008-03-26 20:06:24 +00:00
parent 8ee649a02e
commit 11b9f26610

View File

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