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