fbsplash: more compact support for named pipes + EOF scenario
function old new delta fbsplash_main 1121 1043 -78
This commit is contained in:
parent
7b6d9d687d
commit
7f8f0fafdb
@ -360,6 +360,8 @@ int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
{
|
{
|
||||||
const char *fb_device, *cfg_filename, *fifo_filename;
|
const char *fb_device, *cfg_filename, *fifo_filename;
|
||||||
FILE *fp = fp; // for compiler
|
FILE *fp = fp; // for compiler
|
||||||
|
char *num_buf;
|
||||||
|
unsigned num;
|
||||||
bool bCursorOff;
|
bool bCursorOff;
|
||||||
|
|
||||||
INIT_G();
|
INIT_G();
|
||||||
@ -392,11 +394,20 @@ int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
fp = xfopen_stdin(fifo_filename);
|
fp = xfopen_stdin(fifo_filename);
|
||||||
|
if (fp != stdin) {
|
||||||
while (1) {
|
// For named pipes, we want to support this:
|
||||||
struct stat statbuf;
|
// mkfifo cmd_pipe
|
||||||
unsigned num;
|
// fbsplash -f cmd_pipe .... &
|
||||||
char *num_buf;
|
// ...
|
||||||
|
// echo 33 >cmd_pipe
|
||||||
|
// ...
|
||||||
|
// echo 66 >cmd_pipe
|
||||||
|
// This means that we don't want fbsplash to get EOF
|
||||||
|
// when last writer closes input end.
|
||||||
|
// The simplest way is to open fifo for writing too
|
||||||
|
// and become an additional writer :)
|
||||||
|
open(fifo_filename, O_WRONLY); // errors are ignored
|
||||||
|
}
|
||||||
|
|
||||||
fb_drawprogressbar(0);
|
fb_drawprogressbar(0);
|
||||||
// Block on read, waiting for some input.
|
// Block on read, waiting for some input.
|
||||||
@ -406,12 +417,7 @@ int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
|
while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
|
||||||
if (strncmp(num_buf, "exit", 4) == 0) {
|
if (strncmp(num_buf, "exit", 4) == 0) {
|
||||||
DEBUG_MESSAGE("exit");
|
DEBUG_MESSAGE("exit");
|
||||||
exit_cmd:
|
break;
|
||||||
if (bCursorOff) {
|
|
||||||
// restore cursor
|
|
||||||
full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
|
|
||||||
}
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
}
|
||||||
num = atoi(num_buf);
|
num = atoi(num_buf);
|
||||||
if (isdigit(num_buf[0]) && (num <= 100)) {
|
if (isdigit(num_buf[0]) && (num <= 100)) {
|
||||||
@ -424,28 +430,9 @@ int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
}
|
}
|
||||||
free(num_buf);
|
free(num_buf);
|
||||||
}
|
}
|
||||||
// We got EOF/error on fp
|
|
||||||
if (ferror(fp))
|
if (bCursorOff) // restore cursor
|
||||||
goto exit_cmd;
|
full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
|
||||||
fclose(fp);
|
|
||||||
if (LONE_DASH(fifo_filename)
|
|
||||||
|| stat(fifo_filename, &statbuf) != 0
|
|
||||||
|| !S_ISFIFO(statbuf.st_mode)
|
|
||||||
) {
|
|
||||||
goto exit_cmd;
|
|
||||||
}
|
|
||||||
// It's really a named pipe!
|
|
||||||
// For named pipes, we want to support this:
|
|
||||||
// mkfifo cmd_pipe
|
|
||||||
// fbsplash -f cmd_pipe .... &
|
|
||||||
// ...
|
|
||||||
// echo 33 >cmd_pipe
|
|
||||||
// ...
|
|
||||||
// echo 66 >cmd_pipe
|
|
||||||
// This means that on EOF, we need to close/open cmd_pipe
|
|
||||||
// (just reading again works too, but it hogs CPU)
|
|
||||||
fp = xfopen_stdin(fifo_filename); // blocks on open
|
|
||||||
} // end of while (1)
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user