From c29cfeaa5c6ab5c7e0c39415fe3c9c103c8bdf64 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michael=20G=C3=B6hler?= Date: Sun, 17 Aug 2014 14:56:21 +0200 Subject: [PATCH] added getopts argument parsing + usage + debug mode --- .gdbinit | 2 +- tmp.c | 103 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 72 insertions(+), 33 deletions(-) diff --git a/.gdbinit b/.gdbinit index cd479e1..9733b15 100644 --- a/.gdbinit +++ b/.gdbinit @@ -1 +1 @@ -set args sample.md +set args -d sample.md diff --git a/tmp.c b/tmp.c index 805cb3e..f2d7e32 100644 --- a/tmp.c +++ b/tmp.c @@ -1,22 +1,55 @@ +#include +#include #include #include -#include +#include #include "include/markdown.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, " -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,35 +57,41 @@ int main(int argc, char *argv[]) { input = stdin; } + // load document object from input + document_t *doc; doc = markdown_load(input); - // test line/page load - int offset; - line_t *header; - if(doc->header) { - header = doc->header; - while(header && - header->text->size > 0 && - header->text->text[0] == '%') { - - offset = next_blank(header->text, 0) + 1; - printf("header: %s\n", &header->text->text[offset]); - header = header->next; + if(debug) { + // print header to STDERR + int offset; + line_t *header; + if(doc->header) { + header = doc->header; + while(header && + header->text->size > 0 && + header->text->text[0] == '%') { + + offset = next_blank(header->text, 0) + 1; + printf("header: %s\n", &header->text->text[offset]); + header = header->next; + } } - } - 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; + + // print page/line count to STDERR + 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; } - printf("page %i: %i lines\n", cp, cl); - page = page->next; } } -- 2.20.1