rewrote Makefile + copyright and license infos
[smdp.git] / mdp.c
1 /*
2  * mpd -- A command-line based markdown presentation tool.
3  * Copyright (C) 2014 Michael Goehler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "include/mdp.h"
27
28 void usage() {
29     fprintf(stderr, "%s", "Usage: mdp [OPTION]... [FILE]\n");
30     fprintf(stderr, "%s", "A command-line based markdown presentation tool.\n\n");
31     fprintf(stderr, "%s", "  -d, --debug   enable debug messages on STDERR\n");
32     fprintf(stderr, "%s", "                add it multiple times to increases debug level\n\n");
33     fprintf(stderr, "%s", "  -h, --help    display this help and exit\n");
34     fprintf(stderr, "%s", "\nWith no FILE, or when FILE is -, read standard input.\n\n");
35     exit(EXIT_FAILURE);
36 }
37
38 void version() {
39     printf("mdp %d.%d\n", MAJOR_VERSION, MINOR_VERSION);
40     printf("Copyright (C) 2014 Michael Goehler\n");
41     printf("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
42     printf("This is free software: you are free to change and redistribute it.\n");
43     printf("There is NO WARRANTY, to the extent permitted by law.\n");
44     printf("\nWritten by Michael Goehler and others, see <https://github.com/visit1985/mdp/blob/master/AUTHORS>.\n");
45     exit(EXIT_SUCCESS);
46 }
47
48 int main(int argc, char *argv[]) {
49
50     // define command-line options
51     struct option longopts[] = {
52         { "debug",   no_argument, 0, 'd' },
53         { "help",    no_argument, 0, 'h' },
54         { "version", no_argument, 0, 'v' },
55         { 0, 0, 0, 0 }
56     };
57
58     // parse command-line options
59     int opt, debug = 0;
60     while ((opt = getopt_long(argc, argv, ":dhv", longopts, NULL)) != -1) {
61         switch(opt) {
62             case 'd': debug += 1; break;
63             case 'h': usage(); break;
64             case 'v': version(); break;
65             case ':': fprintf(stderr, "%s: '%c' requires an argument\n", argv[0], optopt); usage(); break;
66             case '?':
67             default : fprintf(stderr, "%s: option '%c' is invalid\n", argv[0], optopt); usage(); break;
68         }
69     }
70
71     // open file or set input to STDIN
72     char *file;
73     FILE *input;
74     if (optind < argc) {
75         do {
76             file = argv[optind];
77         } while(++optind < argc);
78
79         if(!strcmp(file, "-")) {
80             input = stdin;
81         } else {
82             input = fopen(file,"r");
83             if(!input) {
84                 fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno));
85                 exit(EXIT_FAILURE);
86             }
87         }
88     } else {
89         input = stdin;
90     }
91
92     // load deck object from input
93     deck_t *deck;
94     deck = markdown_load(input);
95
96     // close file
97     fclose(input);
98
99     if(debug > 0) {
100         markdown_debug(deck, debug);
101     }
102
103     ncurses_display(deck, 0, 0);
104
105     return(EXIT_SUCCESS);
106 }
107