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