Debian Codegraph @

ext/pgm_options.c

raw symbols SCIP
1 /* markdown: a C implementation of John Gruber's Markdown markup language.
2 *
3 * Copyright (C) 2007-2011 David L Parsons.
4 * The redistribution terms are provided in the COPYRIGHT file that must
5 * be distributed with this source code.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <limits.h>
11 #include <mkdio.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <stdarg.h>
15
16 #include "config.h"
17 #include "amalloc.h"
18
19 #if HAVE_LIBGEN_H
20 #include <libgen.h>
21 #endif
22
23 static struct _opt {
24 char *name;
25 char *desc;
26 int off;
27 int skip; /* this opt is a synonym */
28 int sayenable;
29 mkd_flag_t flag;
30 } opts[] = {
31 { "tabstop", "default (4-space) tabstops", 0, 0, 1, MKD_TABSTOP },
32 { "image", "images", 1, 0, 1, MKD_NOIMAGE },
33 { "links", "links", 1, 0, 1, MKD_NOLINKS },
34 { "relax", "Markdown.pl compatibility", 1, 1, 1, MKD_STRICT },
35 { "strict", "Markdown.pl compatibility", 0, 0, 1, MKD_STRICT },
36 { "tables", "tables", 1, 0, 1, MKD_NOTABLES },
37 { "header", "pandoc-style headers", 1, 0, 1, MKD_NOHEADER },
38 { "html", "raw html", 1, 0, 1, MKD_NOHTML },
39 { "ext", "extended protocols", 1, 0, 1, MKD_NO_EXT },
40 { "cdata", "generate cdata", 0, 0, 0, MKD_CDATA },
41 { "smarty", "smartypants", 1, 0, 1, MKD_NOPANTS },
42 { "pants", "smartypants", 1, 1, 1, MKD_NOPANTS },
43 { "toc", "tables of contents", 0, 0, 1, MKD_TOC },
44 { "autolink", "autolinking", 0, 0, 1, MKD_AUTOLINK },
45 { "safelink", "safe links", 0, 0, 1, MKD_SAFELINK },
46 { "strikethrough", "strikethrough", 1, 0, 1, MKD_NOSTRIKETHROUGH },
47 { "del", "strikethrough", 1, 1, 1, MKD_NOSTRIKETHROUGH },
48 { "superscript", "superscript", 1, 0, 1, MKD_NOSUPERSCRIPT },
49 { "emphasis", "emphasis inside words", 0, 0, 1, MKD_NORELAXED },
50 { "divquote", ">%class% blockquotes", 1, 0, 1, MKD_NODIVQUOTE },
51 { "alphalist", "alpha lists", 1, 0, 1, MKD_NOALPHALIST },
52 { "definitionlist","definition lists", 1, 0, 1, MKD_NODLIST },
53 { "1.0", "markdown 1.0 compatibility", 0, 0, 1, MKD_1_COMPAT },
54 { "footnotes", "markdown extra footnotes", 0, 0, 1, MKD_EXTRA_FOOTNOTE },
55 { "footnote", "markdown extra footnotes", 0, 1, 1, MKD_EXTRA_FOOTNOTE },
56 { "style", "extract style blocks", 1, 0, 1, MKD_NOSTYLE },
57 { "dldiscount", "discount-style definition lists", 1, 0, 1, MKD_NODLDISCOUNT },
58 { "dlextra", "extra-style definition lists", 0, 0, 1, MKD_DLEXTRA },
59 { "fencedcode", "fenced code blocks", 0, 0, 1, MKD_FENCEDCODE },
60 { "idanchor", "id= anchors in TOC", 0, 0, 1, MKD_IDANCHOR },
61 { "githubtags", "permit - and _ in element names", 0, 0, 0, MKD_GITHUBTAGS },
62 { "urlencodedanchor", "html5-style anchors", 0, 0, 0, MKD_URLENCODEDANCHOR },
63 { "html5anchor", "html5-style anchors", 0, 1, 0, MKD_URLENCODEDANCHOR },
64 { "latex", "handle LaTeX escapes", 0, 0, 1, MKD_LATEX },
65 { "explicitlist", "do not merge adjacent numeric/bullet lists", 0, 0, 1, MKD_EXPLICITLIST },
66 } ;
67
68 #define NR(x) (sizeof x / sizeof x[0])
69
70
71 typedef int (*stfu)(const void *, const void *);
72
73 int
74 sort_by_name(struct _opt *a, struct _opt *b)
75 {
76 return strcmp(a->name,b->name);
77 }
78
79 int
80 sort_by_flag(struct _opt *a, struct _opt *b)
81 {
82 return a->flag - b->flag;
83 }
84
85
86 void
87 show_flags(int byname, int verbose)
88 {
89 int i;
90
91 if ( byname ) {
92 qsort(opts, NR(opts), sizeof(opts[0]), (stfu)sort_by_name);
93
94 for (i=0; i < NR(opts); i++)
95 if ( verbose || !opts[i].skip )
96 fprintf(stderr, "%16s : %s\n", opts[i].name, opts[i].desc);
97 }
98 else {
99 qsort(opts, NR(opts), sizeof(opts[0]), (stfu)sort_by_flag);
100
101 for (i=0; i < NR(opts); i++)
102 if ( !opts[i].skip ) {
103 fprintf(stderr, "%08lx : ", (long)opts[i].flag);
104 if ( opts[i].sayenable )
105 fprintf(stderr, opts[i].off ? "disable " : "enable ");
106 fprintf(stderr, "%s\n", opts[i].desc);
107 }
108 }
109 }
110
111
112 char *
113 set_flag(mkd_flag_t *flags, char *optionstring)
114 {
115 int i;
116 int enable;
117 char *arg;
118
119 for ( arg = strtok(optionstring, ","); arg; arg = strtok(NULL, ",") ) {
120 if ( *arg == '+' || *arg == '-' )
121 enable = (*arg++ == '+') ? 1 : 0;
122 else if ( strncasecmp(arg, "no", 2) == 0 ) {
123 arg += 2;
124 enable = 0;
125 }
126 else
127 enable = 1;
128
129 for ( i=0; i < NR(opts); i++ )
130 if ( strcasecmp(arg, opts[i].name) == 0 )
131 break;
132
133 if ( i < NR(opts) ) {
134 if ( opts[i].off )
135 enable = !enable;
136
137 if ( enable )
138 *flags |= opts[i].flag;
139 else
140 *flags &= ~opts[i].flag;
141 }
142 else
143 return arg;
144 }
145 return 0;
146 }