X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=tmp.c;h=a806ff446234c39c9bd610167e29896ca96e4c5e;hb=f92a7609f7b4a6c72a892b16d371528213eca283;hp=b908834d8eb14439e7ad46a87c66835d91fc3310;hpb=eb9313d1407c4b7190d0f43e03bff5d94885a82b;p=smdp.git diff --git a/tmp.c b/tmp.c index b908834..a806ff4 100644 --- a/tmp.c +++ b/tmp.c @@ -1,22 +1,57 @@ +#include +#include #include #include -#include +#include -#include "include/markdown.h" +#include "include/parser.h" +#include "include/viewer.h" + +void usage() { + fprintf(stderr, "Usage: tmp [OPTION]... [FILE]\n"); + fprintf(stderr, "A command-line based markdown presentation tool.\n\n"); + fprintf(stderr, " -d, --debug enable debug messages on STDERR\n"); + fprintf(stderr, " add it multiple times to increases debug level\n\n"); + fprintf(stderr, " -h, --help display this help and exit\n"); + fprintf(stderr, "\nWith no FILE, or when FILE is -, read standard input.\n\n"); + exit(EXIT_FAILURE); +} int main(int argc, char *argv[]) { + // define command-line options + struct option longopts[] = { + { "debug", no_argument, 0, 'd' }, + { "help", no_argument, 0, 'h' }, + { 0, 0, 0, 0 } + }; + + // parse command-line options + int opt, debug = 0; + while ((opt = getopt_long(argc, argv, ":dh", longopts, NULL)) != -1) { + switch(opt) { + case 'd': debug += 1; break; + case 'h': usage(); break; + case ':': fprintf(stderr, "%s: '%c' requires an argument\n", argv[0], optopt); usage(); break; + case '?': + default : fprintf(stderr, "%s: option '%c' is invalid\n", argv[0], optopt); usage(); break; + } + } + + // open file or set input to STDIN + char *file; FILE *input; - document_t *doc; + if (optind < argc) { + do { + file = argv[optind]; + } while(++optind < argc); - if (argc > 1) { - if(!strcmp(argv[1], "-")) { + if(!strcmp(file, "-")) { input = stdin; } else { - input = fopen(argv[1],"r"); + input = fopen(file,"r"); if(!input) { - fprintf(stderr, "Unable to open '%s': %s\n", - argv[1], strerror(errno)); + fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno)); exit(EXIT_FAILURE); } } @@ -24,22 +59,18 @@ int main(int argc, char *argv[]) { input = stdin; } - doc = markdown_load(input); - - // test line/page load - int cp = 0, cl = 0; - page_t *page = doc->page; - line_t *line; - while(page) { - cp++; - line = page->line; - cl = 0; - while(line) { - cl++; - line = line->next; - } - printf("page %i: %i lines\n", cp, cl); - page = page->next; + // load deck object from input + deck_t *deck; + deck = markdown_load(input); + + //TODO close file + + if(debug > 0) { + markdown_debug(deck, debug); } + + ncurses_display(deck, 0, 0); + + return(EXIT_SUCCESS); }