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