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