public
Description: An implementation of markdown in C, using a PEG grammar
Clone URL: git://github.com/jgm/peg-markdown.git
Search Repo:
Code cleanup.
jgm (author)
Mon May 19 10:27:13 -0700 2008
commit  6611a90a7de08f9394f5bc4eaa061ad84be872e6
tree    261f5c2a92a527f0e7c6ff8ccc19c7472f15c527
parent  7290fa28d02a26f6a998f3db87cac5b012484cb2
...
153
154
155
156
157
 
158
159
160
161
162
...
153
154
155
 
 
156
157
 
158
159
160
0
@@ -153,10 +153,8 @@
0
         case 'o': /* -output=FILE */
0
             outfilename = optarg;
0
             /* we allow "-" as a synonym for stdout here */
0
- if (! strcmp(optarg, "-"))
0
- {
0
+ if (strcmp(optarg, "-") == 0)
0
                 outfilename = 0;
0
- }
0
             break;
0
         default: /* something unexpected has happened */
0
             usage(progname);
...
25
26
27
 
 
 
 
 
 
 
 
 
 
28
29
30
...
41
42
43
44
 
45
46
47
48
49
50
51
52
53
54
55
...
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
...
92
93
94
95
 
96
97
98
99
100
101
102
103
104
105
106
107
108
109
...
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
...
146
147
148
149
 
150
151
 
152
153
154
155
156
157
158
...
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
...
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
...
273
274
275
276
277
278
279
280
 
281
282
283
284
...
292
293
294
295
 
296
297
298
299
 
300
301
302
...
306
307
308
309
 
310
311
312
313
314
315
316
317
318
319
...
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
...
348
349
350
351
 
352
353
354
355
356
357
358
359
360
361
362
363
364
...
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
385
 
386
387
388
 
389
390
 
391
392
393
394
395
396
397
...
395
396
397
398
 
399
400
401
402
403
404
 
405
406
 
407
408
409
 
410
411
 
412
413
414
415
416
417
418
419
...
425
426
427
428
 
429
430
431
 
 
432
433
 
434
435
436
437
438
 
439
440
 
441
 
442
443
444
...
451
452
453
454
 
455
456
457
458
459
460
461
462
463
464
465
466
467
468
...
459
460
461
462
 
463
464
 
465
466
467
468
469
 
470
471
472
473
 
474
475
476
477
478
 
479
480
481
482
 
483
484
485
486
487
 
488
489
490
 
491
492
493
494
 
495
496
497
 
498
499
500
501
502
503
504
 
505
506
507
 
508
509
510
...
514
515
516
517
518
519
520
521
522
523
...
527
528
529
530
 
531
532
533
534
 
535
536
537
538
539
540
541
542
543
544
545
 
546
547
548
...
552
553
554
555
 
556
557
558
...
562
563
564
565
 
566
567
568
569
570
571
572
573
574
575
576
577
...
570
571
572
573
 
574
575
576
 
577
578
579
 
580
581
582
 
583
584
585
 
586
587
 
588
589
590
 
591
592
 
593
594
595
 
596
597
 
598
599
600
601
602
603
604
605
...
606
607
608
609
 
610
611
 
612
613
614
615
616
 
617
618
 
619
620
621
622
 
623
624
 
625
626
627
628
...
653
654
655
656
 
657
658
659
660
661
662
 
663
664
665
666
667
668
669
670
671
...
667
668
669
670
 
671
672
 
673
674
675
676
677
 
678
679
680
681
 
682
683
684
685
686
 
687
688
689
690
 
691
692
693
694
695
 
696
697
698
699
700
701
...
700
701
702
703
 
704
705
706
707
 
708
709
710
711
712
713
714
715
 
 
716
717
718
719
 
720
721
722
...
726
727
728
729
730
731
732
...
742
743
744
745
 
746
747
748
...
754
755
756
757
758
759
...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
...
51
52
53
 
54
55
56
57
58
59
60
61
62
63
64
65
...
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
...
101
102
103
 
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
...
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
...
155
156
157
 
158
159
 
160
161
162
163
164
165
166
167
...
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
...
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
...
282
283
284
 
285
286
287
 
288
289
290
291
292
...
300
301
302
 
303
304
305
306
 
307
308
309
310
...
314
315
316
 
317
318
319
320
321
322
323
324
325
326
327
...
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
...
355
356
357
 
358
359
360
361
362
363
364
365
366
367
368
369
370
371
...
364
365
366
 
367
368
369
370
371
372
 
373
374
375
 
376
377
378
 
379
380
381
 
382
383
384
 
385
386
 
387
388
389
 
390
391
 
392
393
394
 
395
396
 
397
398
399
400
401
402
403
404
...
402
403
404
 
405
406
407
408
409
410
 
411
412
 
413
414
415
 
416
417
 
418
419
420
421
422
423
424
425
426
...
432
433
434
 
435
436
 
 
437
438
439
 
440
441
442
443
444
 
445
446
 
447
448
449
450
451
452
...
459
460
461
 
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
...
467
468
469
 
470
471
 
472
473
474
475
476
 
477
478
479
480
 
481
482
483
484
485
 
486
487
488
489
 
490
491
492
493
494
 
495
496
497
 
498
499
500
501
 
502
503
504
 
505
506
507
508
509
510
511
 
512
513
514
 
515
516
517
518
...
522
523
524
 
525
526
527
528
529
530
...
534
535
536
 
537
538
539
540
 
541
542
543
544
 
545
546
547
548
549
550
 
551
552
553
554
...
558
559
560
 
561
562
563
564
...
568
569
570
 
571
572
573
574
575
576
577
578
579
580
581
582
583
...
576
577
578
 
579
580
581
 
582
583
584
 
585
586
587
 
588
589
590
 
591
592
 
593
594
595
 
596
597
 
598
599
600
 
601
602
 
603
604
605
606
607
608
609
610
611
...
612
613
614
 
615
616
 
617
618
619
620
621
 
622
623
 
624
625
626
627
 
628
629
 
630
631
632
633
634
...
659
660
661
 
662
663
664
665
666
667
 
668
669
670
671
672
673
674
675
676
677
...
673
674
675
 
676
677
 
678
679
680
681
682
 
683
684
685
686
 
687
688
689
690
691
 
692
693
694
695
 
696
697
698
699
700
 
701
702
703
704
705
706
707
...
706
707
708
 
709
710
711
712
 
713
714
715
716
717
718
719
 
 
720
721
722
723
724
 
725
726
727
728
...
732
733
734
 
735
736
737
...
747
748
749
 
750
751
752
753
...
759
760
761
 
762
763
0
@@ -25,6 +25,16 @@
0
 /* TODO remove */
0
 static extensions = 0;
0
 
0
+static void print_html_string(char *str, bool obfuscate);
0
+static void print_html_element_list(element *list, bool obfuscate);
0
+static void print_html_element(element elt, bool obfuscate);
0
+static void print_latex_string(char *str);
0
+static void print_latex_element_list(element *list);
0
+static void print_latex_element(element elt);
0
+static void print_groff_string(char *str);
0
+static void print_groff_mm_element_list(element *list);
0
+static void print_groff_mm_element(element elt, int count);
0
+
0
 /**********************************************************************
0
 
0
   Utility functions for printing
0
@@ -41,7 +51,7 @@
0
 /* pad - add newlines if needed */
0
 static void pad(int num) {
0
     while (num-- > padded)
0
- printf("\n");
0
+ printf("%s","\n");;
0
     padded = num;
0
 }
0
 
0
0
0
0
0
0
0
0
0
@@ -53,38 +63,37 @@
0
 
0
 /* print_html_string - print string, escaping for HTML
0
  * If obfuscate selected, convert characters to hex or decimal entities at random */
0
-void print_html_string(char *str, bool obfuscate) {
0
+static void print_html_string(char *str, bool obfuscate) {
0
     while (*str != '\0') {
0
         switch (*str) {
0
         case '&':
0
- printf("&");
0
+ printf("%s","&");;
0
             break;
0
         case '<':
0
- printf("&lt;");
0
+ printf("%s","&lt;");;
0
             break;
0
         case '>':
0
- printf("&gt;");
0
+ printf("%s","&gt;");;
0
             break;
0
         case '"':
0
- printf("&quot;");
0
+ printf("%s","&quot;");;
0
             break;
0
         default:
0
             if (obfuscate) {
0
                 if (rand() % 2 == 0)
0
- printf("&#%d;", *str);
0
+ printf("&#%d;", (int) *str);
0
                 else
0
- printf("&#x%x;", *str);
0
+ printf("&#x%x;", (unsigned int) *str);
0
             }
0
             else
0
                 putchar(*str);
0
- break;
0
         }
0
     str++;
0
     }
0
 }
0
 
0
 /* print_html_element_list - print a list of elements as HTML */
0
-void print_html_element_list(element *list, bool obfuscate) {
0
+static void print_html_element_list(element *list, bool obfuscate) {
0
     while (list != NULL) {
0
         print_html_element(*list, obfuscate);
0
         list = list->next;
0
@@ -92,7 +101,7 @@
0
 }
0
 
0
 /* print_html_element - print an element as HTML */
0
-void print_html_element(element elt, bool obfuscate) {
0
+static void print_html_element(element elt, bool obfuscate) {
0
     int lev;
0
     char *contents;
0
     switch (elt.key) {
0
0
0
0
0
0
0
0
0
0
0
0
@@ -100,40 +109,40 @@
0
         printf("%s", elt.contents.str);
0
         break;
0
     case LINEBREAK:
0
- printf("<br/>");
0
+ printf("%s","<br/>");;
0
         break;
0
     case STR:
0
         print_html_string(elt.contents.str, obfuscate);
0
         break;
0
     case ELLIPSIS:
0
- printf("&hellip;");
0
+ printf("%s","&hellip;");;
0
         break;
0
     case EMDASH:
0
- printf("&mdash;");
0
+ printf("%s","&mdash;");;
0
         break;
0
     case ENDASH:
0
- printf("&ndash;");
0
+ printf("%s","&ndash;");;
0
         break;
0
     case APOSTROPHE:
0
- printf("&rsquo;");
0
+ printf("%s","&rsquo;");;
0
         break;
0
     case SINGLEQUOTED:
0
- printf("&lsquo;");
0
+ printf("%s","&lsquo;");;
0
         print_html_element_list(elt.children, obfuscate);
0
- printf("&rsquo;");
0
+ printf("%s","&rsquo;");;
0
         break;
0
     case DOUBLEQUOTED:
0
- printf("&ldquo;");
0
+ printf("%s","&ldquo;");;
0
         print_html_element_list(elt.children, obfuscate);
0
- printf("&rdquo;");
0
+ printf("%s","&rdquo;");;
0
         break;
0
     case CODE:
0
- printf("<code>");
0
+ printf("%s","<code>");;
0
         print_html_string(elt.contents.str, obfuscate);
0
- printf("</code>");
0
+ printf("%s","</code>");;
0
         break;
0
     case HTML:
0
- printf(elt.contents.str);
0
+ printf("%s", elt.contents.str);
0
         break;
0
     case LINK:
0
         if (strstr(elt.contents.link.url, "mailto:") == elt.contents.link.url)
0
0
@@ -146,9 +155,9 @@
0
             print_html_string(elt.contents.link.title, obfuscate);
0
             printf("\"");
0
         }
0
- printf(">");
0
+ printf("%s",">");;
0
         print_html_element_list(elt.contents.link.label, obfuscate);
0
- printf("</a>");
0
+ printf("%s","</a>");;
0
         break;
0
     case IMAGE:
0
         printf("<img src=\"");
0
0
0
0
0
@@ -161,17 +170,17 @@
0
             print_html_string(elt.contents.link.title, obfuscate);
0
             printf("\"");
0
         }
0
- printf(" />");
0
+ printf("%s"," />");;
0
         break;
0
     case EMPH:
0
- printf("<em>");
0
+ printf("%s","<em>");;
0
         print_html_element_list(elt.children, obfuscate);
0
- printf("</em>");
0
+ printf("%s","</em>");;
0
         break;
0
     case STRONG:
0
- printf("<strong>");
0
+ printf("%s","<strong>");;
0
         print_html_element_list(elt.children, obfuscate);
0
- printf("</strong>");
0
+ printf("%s","</strong>");;
0
         break;
0
     case LIST:
0
         print_html_element_list(elt.children, obfuscate);
0
0
0
0
0
0
0
0
0
0
0
0
0
0
@@ -200,61 +209,61 @@
0
         break;
0
     case PARA:
0
         pad(2);
0
- printf("<p>");
0
+ printf("%s","<p>");;
0
         print_html_element_list(elt.children, obfuscate);
0
- printf("</p>");
0
+ printf("%s","</p>");;
0
         padded = 0;
0
         break;
0
     case HRULE:
0
         pad(2);
0
- printf("<hr />");
0
+ printf("%s","<hr />");;
0
         padded = 0;
0
         break;
0
     case HTMLBLOCK:
0
         pad(2);
0
- printf(elt.contents.str);
0
+ printf("%s", elt.contents.str);
0
         padded = 0;
0
         break;
0
     case VERBATIM:
0
         pad(2);
0
- printf(""<pre><code>");
0
+ printf("%s", "<pre><code>");
0
         print_html_string(elt.contents.str, obfuscate);
0
- printf(""</code></pre>");
0
+ printf("%s", "</code></pre>");
0
         padded = 0;
0
         break;
0
     case BULLETLIST:
0
         pad(2);
0
- printf(""<ul>");
0
+ printf("%s", "<ul>");
0
         padded = 0;
0
         print_html_element_list(elt.children, obfuscate);
0
         pad(1);
0
- printf(""</ul>");
0
+ printf("%s", "</ul>");
0
         padded = 0;
0
         break;
0
     case ORDEREDLIST:
0
         pad(2);
0
- printf(""<ol>");
0
+ printf("%s", "<ol>");
0
         padded = 0;
0
         print_html_element_list(elt.children, obfuscate);
0
         pad(1);
0
- printf("</ol>");
0
+ printf("%s","</ol>");;
0
         padded = 0;
0
         break;
0
     case LISTITEM:
0
         pad(1);
0
- printf("<li>");
0
+ printf("%s","<li>");;
0
         padded = 2;
0
         print_html_element_list(elt.children, obfuscate);
0
- printf("</li>");
0
+ printf("%s","</li>");;
0
         padded = 0;
0
         break;
0
     case BLOCKQUOTE:
0
         pad(2);
0
- printf("<blockquote>\n");
0
+ printf("%s","<blockquote>\n");;
0
         padded = 2;
0
         print_html_element_list(elt.children, obfuscate);
0
         pad(1);
0
- printf("</blockquote>");
0
+ printf("%s","</blockquote>");;
0
         padded = 0;
0
         break;
0
     case REFERENCE:
0
0
@@ -273,11 +282,10 @@
0
     default:
0
         fprintf(stderr, "print_html_element encountered unknown element key = %d\n", elt.key);
0
         exit(EXIT_FAILURE);
0
- break;
0
     }
0
 }
0
 
0
-void print_html_endnotes(element *endnotes) {
0
+static void print_html_endnotes(void) {
0
     int counter = 0;
0
     if (endnotes == NULL) {
0
         return;
0
0
@@ -292,11 +300,11 @@
0
         print_html_element_list(endnotes->children, false);
0
         printf(" <a href=\"#fnref%d\" title=\"Jump back to reference\">[back]</a>", counter);
0
         pad(1);
0
- printf(""</li>");
0
+ printf("%s","</li>");
0
         endnotes = endnotes->next;
0
     }
0
     pad(1);
0
- printf("</ol>");
0
+ printf("%s","</ol>");;
0
 }
0
 
0
 /**********************************************************************
0
@@ -306,7 +314,7 @@
0
  ***********************************************************************/
0
 
0
 /* print_latex_string - print string, escaping for LaTeX */
0
-void print_latex_string(char *str) {
0
+static void print_latex_string(char *str) {
0
     while (*str != '\0') {
0
         switch (*str) {
0
           case '{': case '}': case '$': case '%':
0
0
0
0
0
0
0
0
@@ -314,33 +322,32 @@
0
             printf("\\%c", *str);
0
             break;
0
         case '^':
0
- printf("\\^{}");
0
+ printf("%s","\\^{}");;
0
             break;
0
         case '\\':
0
- printf("\\textbackslash{}");
0
+ printf("%s","\\textbackslash{}");;
0
             break;
0
         case '~':
0
- printf("\\ensuremath{\\sim}");
0
+ printf("%s","\\ensuremath{\\sim}");;
0
             break;
0
         case '|':
0
- printf("\\textbar{}");
0
+ printf("%s","\\textbar{}");;
0
             break;
0
         case '<':
0
- printf("\\textless{}");
0
+ printf("%s","\\textless{}");;
0
             break;
0
         case '>':
0
- printf("\\textgreater{}");
0
+ printf("%s","\\textgreater{}");;
0
             break;
0
         default:
0
             putchar(*str);
0
- break;
0
         }
0
     str++;
0
     }
0
 }
0
 
0
 /* print_latex_element_list - print a list of elements as LaTeX */
0
-void print_latex_element_list(element *list) {
0
+static void print_latex_element_list(element *list) {
0
     while (list != NULL) {
0
         print_latex_element(*list);
0
         list = list->next;
0
@@ -348,7 +355,7 @@
0
 }
0
 
0
 /* print_latex_element - print an element as LaTeX */
0
-void print_latex_element(element elt) {
0
+static void print_latex_element(element elt) {
0
     int lev;
0
     int i;
0
     char *contents;
0
0
0
0
0
0
0
0
0
0
0
@@ -357,37 +364,37 @@
0
         printf("%s", elt.contents.str);
0
         break;
0
     case LINEBREAK:
0
- printf("\\\\\n");
0
+ printf("%s","\\\\\n");;
0
         break;
0
     case STR:
0
         print_latex_string(elt.contents.str);
0
         break;
0
     case ELLIPSIS:
0
- printf("\\ldots{}");
0
+ printf("%s","\\ldots{}");;
0
         break;
0
     case EMDASH:
0
- printf("---");
0
+ printf("%s","---");;
0
         break;
0
     case ENDASH:
0
- printf("--");
0
+ printf("%s","--");;
0
         break;
0
     case APOSTROPHE:
0
- printf("'");
0
+ printf("%s","'");;
0
         break;
0
     case SINGLEQUOTED:
0
- printf("`");
0
+ printf("%s","`");;
0
         print_latex_element_list(elt.children);
0
- printf("'");
0
+ printf("%s","'");;
0
         break;
0
     case DOUBLEQUOTED:
0
- printf("``");
0
+ printf("%s","``");;
0
         print_latex_element_list(elt.children);
0
- printf("''");
0
+ printf("%s","''");;
0
         break;
0
     case CODE:
0
- printf("\\texttt{");
0
+ printf("%s","\\texttt{");;
0
         print_latex_string(elt.contents.str);
0
- printf("}");
0
+ printf("%s","}");;
0
         break;
0
     case HTML:
0
       &n