-
Notifications
You must be signed in to change notification settings - Fork 156
/
main.c
384 lines (326 loc) · 8.26 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* markdown: convert a single markdown document into html
*/
/*
* Copyright (C) 2007 Jessica L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <mkdio.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/wait.h>
#include "config.h"
#include "amalloc.h"
#include "pgm_options.h"
#include "tags.h"
#include "gethopt.h"
#if HAVE_LIBGEN_H
#include <libgen.h>
#endif
#ifndef HAVE_BASENAME
char*
basename(char *p)
{
char *ret = strrchr(p, '/');
return ret ? (1+ret) : p;
}
#endif
char *pgm = "markdown";
char *
e_flags(const char *text, const int size, void *context)
{
return (char*)context;
}
void
complain(char *fmt, ...)
{
va_list ptr;
fprintf(stderr, "%s: ", pgm);
va_start(ptr, fmt);
vfprintf(stderr, fmt, ptr);
va_end(ptr);
fputc('\n', stderr);
fflush(stderr);
}
void
callback_free(char *input, int size, void *ctx)
{
if (input)
free(input);
}
char *
anchor_format(char *input, void *ctx)
{
int i, j, size;
char* ret;
if ( !input )
return NULL;
size = strlen(input);
ret = malloc(1+size);
if ( !ret )
return NULL;
while ( size && isspace(input[size-1]) )
--size;
for ( j=i=0; i < size; i++ ) {
if (isalnum(input[i]) || strchr("-_+", input[i]) )
ret[j++] = input[i];
else if ( input[i] == ' ' )
ret[j++] = '-';
}
ret[j++] = 0;
return ret;
}
char *external_formatter = 0;
#define RECEIVER 0
#define SENDER 1
char *
external_codefmt(char *src, int len, char *lang)
{
int child_status;
int size, bufsize, curr;
char *res;
pid_t child;
int tochild[2], toparent[2];
if ( pipe(tochild) != 0 || pipe(toparent) != 0 ) {
perror("external_codefmt (pipe)");
res = malloc(len+1);
strcpy(res, src);
return res;
}
if ( (child = fork()) > 0 ) {
close(tochild[RECEIVER]);
close(toparent[SENDER]);
bufsize = 1000;
res = malloc(1+bufsize);
curr = 0;
/* parent */
write(tochild[SENDER], src, len);
close(tochild[SENDER]);
while ( (size = read(toparent[RECEIVER], res+curr, 1000)) > 0 ) {
curr += size;
res = realloc(res, bufsize += 1000);
}
res[curr] = 0;
waitpid(child, &child_status, 0);
close(toparent[RECEIVER]);
if ( WIFEXITED(child_status) )
return res;
else
free(res); /* something failed; just return the original string */
}
else if ( child == 0 ) {
close(tochild[SENDER]);
close(toparent[RECEIVER]);
close(1); dup2(toparent[SENDER], 1);
close(0); dup2(tochild[RECEIVER], 0);
system(external_formatter);
close(0); close(tochild[RECEIVER]);
close(1); close(toparent[SENDER]);
exit(0);
}
res = malloc(len+1);
strcpy(res, src);
return res;
}
struct h_opt opts[] = {
{ 0, "html5", '5', 0, "recognise html5 block elements" },
{ 0, "base", 'b', "url-base", "URL prefix" },
{ 0, "debug", 'd', 0, "debugging" },
{ 0, "version",'V', 0, "show version info" },
{ 0, 0, 'E', "flags", "url flags" },
{ 0, 0, 'F', "bitmap", "set/show hex flags" },
{ 0, 0, 'f', "{+-}flags", "set/show named flags" },
{ 0, 0, 'G', 0, "github flavoured markdown" },
{ 0, 0, 'n', 0, "don't write generated html" },
{ 0, 0, 's', "text", "format `text`" },
{ 0, "style", 'S', 0, "output <style> blocks" },
{ 0, 0, 't', "text", "format `text` with mkd_line()" },
{ 0, "toc", 'T', 0, "output a TOC" },
{ 0, 0, 'C', "prefix", "prefix for markdown extra footnotes" },
{ 0, 0, 'o', "file", "write output to file" },
{ 0, "squash", 'x', 0, "squash toc labels to be more like github" },
{ 0, "codefmt",'X', "command", "use an external code formatter" },
{ 0, "help", '?', 0, "print a detailed usage message" },
};
#define NROPTS (sizeof opts/sizeof opts[0])
int
main(int argc, char **argv)
{
int rc;
int debug = 0;
int toc = 0;
int content = 1;
int version = 0;
int with_html5 = 0;
int styles = 0;
int use_mkd_line = 0;
int use_e_codefmt = 0;
int github_flavoured = 0;
int squash = 0;
char *extra_footnote_prefix = 0;
char *urlflags = 0;
char *text = 0;
char *ofile = 0;
char *urlbase = 0;
char *q;
MMIOT *doc;
struct h_context blob;
struct h_opt *opt;
mkd_flag_t *flags = mkd_flags();
if ( !flags )
perror("new_flags");
hoptset(&blob, argc, argv);
hopterr(&blob, 1);
pgm = basename(argv[0]);
if ( q = getenv("MARKDOWN_FLAGS") )
mkd_set_flag_bitmap(flags, strtol(q,0,0));
while ( opt=gethopt(&blob, opts, NROPTS) ) {
if ( opt == HOPTERR ) {
hoptusage(pgm, opts, NROPTS, "[file]");
exit(1);
}
switch (opt->optchar) {
case '5': with_html5 = 1;
break;
case 'b': urlbase = hoptarg(&blob);
break;
case 'd': debug++;
break;
case 'V': version++;
break;
case 'E': urlflags = hoptarg(&blob);
break;
case 'f': q = hoptarg(&blob);
if ( strcmp(q, "?") == 0 ) {
show_flags(1, version, 0);
exit(0);
}
else if ( strcmp(q, "??") == 0 ) {
show_flags(1, version, flags);
exit(0);
}
else if ( q=mkd_set_flag_string(flags, hoptarg(&blob)) )
complain("unknown option <%s>", q);
break;
case 'F': q = hoptarg(&blob);
if ( strcmp(q, "?") == 0 ) {
show_flags(0, 0, 0);
exit(0);
}
else if ( strcmp(q, "??") == 0 ) {
show_flags(0, version, flags);
exit(0);
}
else
mkd_set_flag_bitmap(flags,strtol(q, 0, 0));
break;
case 'G': github_flavoured = 1;
break;
case 'n': content = 0;
break;
case 's': text = hoptarg(&blob);
break;
case 'S': styles = 1;
break;
case 't': text = hoptarg(&blob);
use_mkd_line = 1;
break;
case 'T': mkd_set_flag_num(flags, MKD_TOC);
toc = 1;
break;
case 'C': extra_footnote_prefix = hoptarg(&blob);
break;
case 'o': if ( ofile ) {
complain("Too many -o options");
exit(1);
}
if ( !freopen(ofile = hoptarg(&blob), "w", stdout) ) {
perror(ofile);
exit(1);
}
break;
case 'x': squash = 1;
break;
case 'X': use_e_codefmt = 1;
mkd_set_flag_num(flags, MKD_FENCEDCODE);
external_formatter = hoptarg(&blob);
fprintf(stderr, "selected external formatter (%s)\n", external_formatter);
break;
case '?': hoptdescribe(pgm, opts, NROPTS, "[file]", 1);
return 0;
}
}
if ( version ) {
printf("%s: discount %s%s", pgm, markdown_version,
with_html5 ? " +html5":"");
if ( version == 2 )
mkd_flags_are(stdout, flags, 0);
putchar('\n');
exit(0);
}
argc -= hoptind(&blob);
argv += hoptind(&blob);
if ( with_html5 )
mkd_with_html5_tags();
if ( use_mkd_line )
rc = mkd_generateline( text, strlen(text), stdout, flags);
else {
if ( text ) {
doc = github_flavoured ? gfm_string(text, strlen(text), flags)
: mkd_string(text, strlen(text), flags) ;
if ( !doc ) {
perror(text);
exit(1);
}
}
else {
if ( argc && !freopen(argv[0], "r", stdin) ) {
perror(argv[0]);
exit(1);
}
doc = github_flavoured ? gfm_in(stdin,flags)
: mkd_in(stdin,flags);
if ( !doc ) {
perror(argc ? argv[0] : "stdin");
exit(1);
}
}
if ( urlbase )
mkd_basename(doc, urlbase);
if ( urlflags )
mkd_e_flags(doc, e_flags, NULL, urlflags);
if ( squash )
mkd_e_anchor(doc, (mkd_callback_t) anchor_format, callback_free, 0);
if ( use_e_codefmt )
mkd_e_code_format(doc, (mkd_callback_t)external_codefmt, callback_free, 0);
if ( extra_footnote_prefix )
mkd_ref_prefix(doc, extra_footnote_prefix);
if ( debug ) {
rc = mkd_dump(doc, stdout, flags, argc ? basename(argv[0]) : "stdin");
}
else {
rc = 1;
if ( mkd_compile(doc, flags) ) {
rc = 0;
if ( styles )
mkd_generatecss(doc, stdout);
if ( toc )
mkd_generatetoc(doc, stdout);
if ( content )
mkd_generatehtml(doc, stdout);
}
}
mkd_cleanup(doc);
}
mkd_deallocate_tags();
mkd_free_flags(flags);
adump();
exit( (rc == 0) ? 0 : errno );
}