#include <errno.h>
#include <getopt.h>
+#include <locale.h> // setlocale
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
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[] = {
}
}
+ // 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 {
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);
+ }
+ }
- // close file
- fclose(input);
+ // load deck object from input
+ deck_t *deck;
+ deck = markdown_load(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);
- ncurses_display(deck, notrans, nofade, invert);
+ free_deck(deck);
- return(EXIT_SUCCESS);
-}
+ // reload if supported and requested
+ } while(noreload == 0 && reload > 0);
+ return EXIT_SUCCESS;
+}