X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=src%2Fmain.c;h=e762fbbb4a60d14f1b79cb8c07a74e59f5c0859d;hb=4b8ad1b3b464836eca2e876dd418a8e8d87bd721;hp=ef86cdbdd00cfb3f7667184528865785de4d13c3;hpb=a10fcae3cba4f58550b24b0029f3932615dadbc3;p=smdp.git diff --git a/src/main.c b/src/main.c index ef86cdb..e762fbb 100644 --- a/src/main.c +++ b/src/main.c @@ -19,6 +19,7 @@ #include #include +#include // setlocale #include #include #include @@ -49,9 +50,11 @@ void version() { } int main(int argc, char *argv[]) { - int notrans = 0; - int nofade = 0; - int invert = 0; + int notrans = 0; // disable transparency + int nofade = 0; // disable fading + int invert = 0; // invert color (black on white) + int reload = 0; // reload page N (0 means no reload) + int noreload = 1; // reload disabled until we know input is a file // define command-line options struct option longopts[] = { @@ -80,8 +83,11 @@ int main(int argc, char *argv[]) { } } + // set locale to read and display UTF-8 correctly in ncurses + setlocale(LC_CTYPE, "en_US.UTF8"); + // open file or set input to STDIN - char *file; + char *file = NULL; FILE *input; if (optind < argc) { do { @@ -96,32 +102,57 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno)); exit(EXIT_FAILURE); } + // enable reload because input is a file + noreload = 0; } } else { input = stdin; } - // load deck object from input - deck_t *deck; - deck = markdown_load(input); + // reload loop + do { + + // reopen input file on reload + if(noreload == 0 && reload > 0) { + if(file) { + input = fopen(file,"r"); + if(!input) { + fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno)); + exit(EXIT_FAILURE); + } + } else { + fprintf(stderr, "%s: %s\n", argv[0], "no input file"); + exit(EXIT_FAILURE); + } + } + + // load deck object from input + deck_t *deck; + deck = markdown_load(input); + + // close file + fclose(input); - // close file - fclose(input); + // replace stdin with current tty if input was a pipe + // if input was a pipe reload is disabled, so we simply check that + if(noreload == 1) { + input = freopen("/dev/tty", "rw", stdin); + if(!input) { + fprintf(stderr, "%s: %s: %s\n", argv[0], "/dev/tty", strerror(errno)); + exit(EXIT_FAILURE); + } + } - // replace stdin with current tty if input was a pipe - if(input == stdin) { - input = freopen("/dev/tty", "rw", stdin); - if(!input) { - fprintf(stderr, "%s: %s: %s\n", argv[0], "/dev/tty", strerror(errno)); - exit(EXIT_FAILURE); + if(debug > 0) { + markdown_debug(deck, debug); } - } - if(debug > 0) { - markdown_debug(deck, debug); - } + reload = ncurses_display(deck, notrans, nofade, invert, reload, noreload); + + free_deck(deck); - ncurses_display(deck, notrans, nofade, invert); + // reload if supported and requested + } while(noreload == 0 && reload > 0); - return(EXIT_SUCCESS); + return EXIT_SUCCESS; }