37af9bc13b87a6041225d3074e3898af60f431e5
[smdp.git] / src / main.c
1 /*
2  * mdp -- 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 "main.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");
33     fprintf(stderr, "%s", "  -f, --nofade    disable color fading in 256 color mode\n");
34     fprintf(stderr, "%s", "  -h, --help      display this help and exit\n");
35     fprintf(stderr, "%s", "  -i, --invert    swap black and white color\n");
36     fprintf(stderr, "%s", "  -t, --notrans   disable transparency in transparent terminal\n");
37     fprintf(stderr, "%s", "\nWith no FILE, or when FILE is -, read standard input.\n\n");
38     exit(EXIT_FAILURE);
39 }
40
41 void version() {
42     printf("mdp %d.%d.%d\n", MDP_VER_MAJOR, MDP_VER_MINOR, MDP_VER_REVISION);
43     printf("Copyright (C) 2014 Michael Goehler\n");
44     printf("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
45     printf("This is free software: you are free to change and redistribute it.\n");
46     printf("There is NO WARRANTY, to the extent permitted by law.\n");
47     printf("\nWritten by Michael Goehler and others, see <https://github.com/visit1985/mdp/blob/master/AUTHORS>.\n");
48     exit(EXIT_SUCCESS);
49 }
50
51 int main(int argc, char *argv[]) {
52     int notrans = 0;   // disable transparency
53     int nofade = 0;    // disable fading
54     int invert = 0;    // invert color (black on white)
55     int reload = 0;    // reload page N (0 means no reload)
56     int noreload = 1;  // reload disabled until we know input is a file
57
58     // define command-line options
59     struct option longopts[] = {
60         { "debug",   no_argument, 0, 'd' },
61         { "nofade",  no_argument, 0, 'f' },
62         { "help",    no_argument, 0, 'h' },
63         { "invert",  no_argument, 0, 'i' },
64         { "notrans", no_argument, 0, 't' },
65         { "version", no_argument, 0, 'v' },
66         { 0, 0, 0, 0 }
67     };
68
69     // parse command-line options
70     int opt, debug = 0;
71     while ((opt = getopt_long(argc, argv, ":dfhitv", longopts, NULL)) != -1) {
72         switch(opt) {
73             case 'd': debug += 1; break;
74             case 'f': nofade = 1; break;
75             case 'h': usage(); break;
76             case 'i': invert = 1; break;
77             case 't': notrans = 1; break;
78             case 'v': version(); break;
79             case ':': fprintf(stderr, "%s: '%c' requires an argument\n", argv[0], optopt); usage(); break;
80             case '?':
81             default : fprintf(stderr, "%s: option '%c' is invalid\n", argv[0], optopt); usage(); break;
82         }
83     }
84
85     // open file or set input to STDIN
86     char *file = NULL;
87     FILE *input;
88     if (optind < argc) {
89         do {
90             file = argv[optind];
91         } while(++optind < argc);
92
93         if(!strcmp(file, "-")) {
94             input = stdin;
95         } else {
96             input = fopen(file,"r");
97             if(!input) {
98                 fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno));
99                 exit(EXIT_FAILURE);
100             }
101             // enable reload because input is a file
102             noreload = 0;
103         }
104     } else {
105         input = stdin;
106     }
107
108     // reload loop
109     do {
110
111         // reopen input file on reload
112         if(noreload == 0 && reload > 0) {
113             if(file) {
114                 input = fopen(file,"r");
115                 if(!input) {
116                     fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno));
117                     exit(EXIT_FAILURE);
118                 }
119             } else {
120                 fprintf(stderr, "%s: %s\n", argv[0], "no input file");
121                 exit(EXIT_FAILURE);
122             }
123         }
124
125         // load deck object from input
126         deck_t *deck;
127         deck = markdown_load(input);
128
129         // close file
130         fclose(input);
131
132         // replace stdin with current tty if input was a pipe
133         // if input was a pipe reload is disabled, so we simply check that
134         if(noreload == 1) {
135             input = freopen("/dev/tty", "rw", stdin);
136             if(!input) {
137                 fprintf(stderr, "%s: %s: %s\n", argv[0], "/dev/tty", strerror(errno));
138                 exit(EXIT_FAILURE);
139             }
140         }
141
142         if(debug > 0) {
143             markdown_debug(deck, debug);
144         }
145
146         reload = ncurses_display(deck, notrans, nofade, invert, reload, noreload);
147
148         free_deck(deck);
149
150     // reload if supported and requested
151     } while(noreload == 0 && reload > 0);
152
153     return EXIT_SUCCESS;
154 }