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