added getopts argument parsing + usage + debug mode
[smdp.git] / tmp.c
1 #include <errno.h>
2 #include <getopt.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "include/markdown.h"
8
9 void usage() {
10     fprintf(stderr, "Usage: tmp [OPTION]... [FILE]\n");
11     fprintf(stderr, "A command-line based markdown presentation tool.\n\n");
12     fprintf(stderr, "  -d, --debug   enable debug messages on STDERR\n");
13     fprintf(stderr, "  -h, --help    display this help and exit\n");
14     fprintf(stderr, "\nWith no FILE, or when FILE is -, read standard input.\n\n");
15     exit(EXIT_FAILURE);
16 }
17
18 int main(int argc, char *argv[]) {
19
20     // define command-line options
21     struct option longopts[] = {
22         { "debug", no_argument, 0, 'd' },
23         { "help",  no_argument, 0, 'h' },
24         { 0, 0, 0, 0 }
25     };
26
27     // parse command-line options
28     int opt, debug = 0;
29     while ((opt = getopt_long(argc, argv, ":dh", longopts, NULL)) != -1) {
30         switch(opt) {
31             case 'd': debug = 1; break;
32             case 'h': usage(); break;
33             case ':': fprintf(stderr, "%s: '%c' requires an argument\n", argv[0], optopt); usage(); break;
34             case '?':
35             default : fprintf(stderr, "%s: option '%c' is invalid\n", argv[0], optopt); usage(); break;
36         }
37     }
38
39     // open file or set input to STDIN
40     char *file;
41     FILE *input;
42     if (optind < argc) {
43         do {
44             file = argv[optind];
45         } while(++optind < argc);
46
47         if(!strcmp(file, "-")) {
48             input = stdin;
49         } else {
50             input = fopen(file,"r");
51             if(!input) {
52                 fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno));
53                 exit(EXIT_FAILURE);
54             }
55         }
56     } else {
57         input = stdin;
58     }
59
60     // load document object from input
61     document_t *doc;
62     doc = markdown_load(input);
63
64     if(debug) {
65         // print header to STDERR
66         int offset;
67         line_t *header;
68         if(doc->header) {
69             header = doc->header;
70             while(header &&
71                 header->text->size > 0 &&
72                 header->text->text[0] == '%') {
73
74                 offset = next_blank(header->text, 0) + 1;
75                 printf("header: %s\n", &header->text->text[offset]);
76                 header = header->next;
77             }
78         }
79
80         // print page/line count to STDERR
81         int cp = 0, cl = 0;
82         page_t *page = doc->page;
83         line_t *line;
84         while(page) {
85             cp++;
86             line = page->line;
87             cl = 0;
88             while(line) {
89                 cl++;
90                 line = line->next;
91             }
92             printf("page %i: %i lines\n", cp, cl);
93             page = page->next;
94         }
95     }
96 }
97