]> git.cameronkatri.com Git - mandoc.git/blob - xml.c
*** empty log message ***
[mandoc.git] / xml.c
1 /* $Id: xml.c,v 1.18 2008/12/07 21:30:49 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "libmdocml.h"
24 #include "private.h"
25 #include "ml.h"
26
27
28 static int xml_alloc(void **);
29 static void xml_free(void *);
30 static ssize_t xml_endtag(struct md_mbuf *, void *,
31 const struct md_args *,
32 enum md_ns, int);
33 static ssize_t xml_begintag(struct md_mbuf *, void *,
34 const struct md_args *,
35 enum md_ns, int,
36 const int *, const char **);
37 static ssize_t xml_beginstring(struct md_mbuf *,
38 const struct md_args *,
39 const char *, size_t);
40 static ssize_t xml_endstring(struct md_mbuf *,
41 const struct md_args *,
42 const char *, size_t);
43 static int xml_begin(struct md_mbuf *,
44 const struct md_args *,
45 const struct tm *,
46 const char *, const char *,
47 const char *, const char *);
48 static int xml_end(struct md_mbuf *,
49 const struct md_args *);
50 static ssize_t xml_printtagname(struct md_mbuf *,
51 enum md_ns, int);
52 static ssize_t xml_printtagargs(struct md_mbuf *,
53 const int *, const char **);
54
55
56 static ssize_t
57 xml_printtagargs(struct md_mbuf *mbuf, const int *argc,
58 const char **argv)
59 {
60 int i, c;
61 size_t res;
62
63 if (NULL == argc || NULL == argv)
64 return(0);
65 assert(argc && argv);
66
67 /* LINTED */
68 for (res = 0, i = 0; ROFF_ARGMAX != (c = argc[i]); i++) {
69 if ( ! ml_nputs(mbuf, " ", 1, &res))
70 return(-1);
71
72 if ( ! ml_puts(mbuf, tokargnames[c], &res))
73 return(-1);
74 if ( ! ml_nputs(mbuf, "=\"", 2, &res))
75 return(-1);
76 if (argv[i]) {
77 if ( ! ml_putstring(mbuf, argv[i], &res))
78 return(-1);
79 } else if ( ! ml_nputs(mbuf, "true", 4, &res))
80 return(-1);
81 if ( ! ml_nputs(mbuf, "\"", 1, &res))
82 return(-1);
83 }
84
85 return((ssize_t)res);
86 }
87
88
89 static ssize_t
90 xml_printtagname(struct md_mbuf *mbuf, enum md_ns ns, int tok)
91 {
92 size_t res;
93
94 res = 0;
95 switch (ns) {
96 case (MD_NS_BLOCK):
97 if ( ! ml_nputs(mbuf, "block:", 6, &res))
98 return(-1);
99 break;
100 case (MD_NS_INLINE):
101 if ( ! ml_nputs(mbuf, "inline:", 7, &res))
102 return(-1);
103 break;
104 case (MD_NS_BODY):
105 if ( ! ml_nputs(mbuf, "body:", 5, &res))
106 return(-1);
107 break;
108 case (MD_NS_HEAD):
109 if ( ! ml_nputs(mbuf, "head:", 5, &res))
110 return(-1);
111 break;
112 default:
113 break;
114 }
115
116 if ( ! ml_puts(mbuf, toknames[tok], &res))
117 return(-1);
118 return((ssize_t)res);
119 }
120
121
122 /* ARGSUSED */
123 static int
124 xml_begin(struct md_mbuf *mbuf, const struct md_args *args,
125 const struct tm *tm, const char *os,
126 const char *title, const char *section,
127 const char *vol)
128 {
129
130 if ( ! ml_puts(mbuf, "<?xml version=\"1.0\" "
131 "encoding=\"UTF-8\"?>\n", NULL))
132 return(0);
133 return(ml_puts(mbuf, "<mdoc xmlns:block=\"block\" "
134 "xmlns:special=\"special\" "
135 "xmlns:inline=\"inline\">", NULL));
136 }
137
138
139 /* ARGSUSED */
140 static int
141 xml_end(struct md_mbuf *mbuf, const struct md_args *args)
142 {
143
144 return(ml_puts(mbuf, "</mdoc>", NULL));
145 }
146
147
148 /* ARGSUSED */
149 static ssize_t
150 xml_beginstring(struct md_mbuf *mbuf,
151 const struct md_args *args,
152 const char *buf, size_t sz)
153 {
154
155 return(0);
156 }
157
158
159 /* ARGSUSED */
160 static ssize_t
161 xml_endstring(struct md_mbuf *mbuf,
162 const struct md_args *args,
163 const char *buf, size_t sz)
164 {
165
166 return(0);
167 }
168
169
170 /* ARGSUSED */
171 static ssize_t
172 xml_begintag(struct md_mbuf *mbuf, void *data,
173 const struct md_args *args, enum md_ns ns,
174 int tok, const int *argc, const char **argv)
175 {
176 ssize_t res, sz;
177
178 if (-1 == (res = xml_printtagname(mbuf, ns, tok)))
179 return(-1);
180 if (-1 == (sz = xml_printtagargs(mbuf, argc, argv)))
181 return(-1);
182 return(res + sz);
183 }
184
185
186 /* ARGSUSED */
187 static ssize_t
188 xml_endtag(struct md_mbuf *mbuf, void *data,
189 const struct md_args *args, enum md_ns ns, int tok)
190 {
191
192 return(xml_printtagname(mbuf, ns, tok));
193 }
194
195
196 /* ARGSUSED */
197 int
198 xml_alloc(void **p)
199 {
200
201 return(1);
202 }
203
204
205 /* ARGSUSED */
206 void
207 xml_free(void *p)
208 {
209
210 /* Do nothing. */
211 }
212
213
214 int
215 md_line_xml(void *data, char *buf)
216 {
217
218 return(mlg_line((struct md_mlg *)data, buf));
219 }
220
221
222 int
223 md_exit_xml(void *data, int flush)
224 {
225
226 return(mlg_exit((struct md_mlg *)data, flush));
227 }
228
229
230 void *
231 md_init_xml(const struct md_args *args,
232 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
233 {
234 struct ml_cbs cbs;
235
236 cbs.ml_alloc = xml_alloc;
237 cbs.ml_free = xml_free;
238 cbs.ml_begintag = xml_begintag;
239 cbs.ml_endtag = xml_endtag;
240 cbs.ml_begin = xml_begin;
241 cbs.ml_end = xml_end;
242 cbs.ml_beginstring = xml_beginstring;
243 cbs.ml_endstring = xml_endstring;
244
245 return(mlg_alloc(args, rbuf, mbuf, &cbs));
246 }
247