]> git.cameronkatri.com Git - mandoc.git/blob - xml.c
8d73301bc87d4b7504e9fc0ae997b7f97b890317
[mandoc.git] / xml.c
1 /* $Id: xml.c,v 1.23 2008/12/10 10:43:57 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 "private.h"
24 #include "ml.h"
25
26
27 static int xml_alloc(void **);
28 static void xml_free(void *);
29 static ssize_t xml_endtag(struct md_mbuf *, void *,
30 const struct md_args *,
31 enum md_ns, int);
32 static ssize_t xml_begintag(struct md_mbuf *, void *,
33 const struct md_args *,
34 enum md_ns, int,
35 const int *, const char **);
36 static ssize_t xml_beginstring(struct md_mbuf *,
37 const struct md_args *,
38 const char *, size_t);
39 static ssize_t xml_endstring(struct md_mbuf *,
40 const struct md_args *,
41 const char *, size_t);
42 static int xml_begin(struct md_mbuf *,
43 const struct md_args *,
44 const struct tm *,
45 const char *, const char *,
46 enum roffmsec, enum roffvol);
47 static int xml_end(struct md_mbuf *,
48 const struct md_args *);
49 static ssize_t xml_printtagname(struct md_mbuf *,
50 enum md_ns, int);
51 static ssize_t xml_printtagargs(struct md_mbuf *,
52 const int *, const char **);
53
54
55 static ssize_t
56 xml_printtagargs(struct md_mbuf *mbuf, const int *argc,
57 const char **argv)
58 {
59 int i, c;
60 size_t res;
61
62 if (NULL == argc || NULL == argv)
63 return(0);
64 assert(argc && argv);
65
66 /* LINTED */
67 for (res = 0, i = 0; ROFF_ARGMAX != (c = argc[i]); i++) {
68 if ( ! ml_nputs(mbuf, " ", 1, &res))
69 return(-1);
70
71 /* FIXME: should puke on some, no? */
72
73 if ( ! ml_puts(mbuf, tokargnames[c], &res))
74 return(-1);
75 if ( ! ml_nputs(mbuf, "=\"", 2, &res))
76 return(-1);
77 if (argv[i]) {
78 if ( ! ml_putstring(mbuf, argv[i], &res))
79 return(-1);
80 } else if ( ! ml_nputs(mbuf, "true", 4, &res))
81 return(-1);
82 if ( ! ml_nputs(mbuf, "\"", 1, &res))
83 return(-1);
84 }
85
86 return((ssize_t)res);
87 }
88
89
90 static ssize_t
91 xml_printtagname(struct md_mbuf *mbuf, enum md_ns ns, int tok)
92 {
93 size_t res;
94
95 res = 0;
96 switch (ns) {
97 case (MD_NS_BLOCK):
98 if ( ! ml_nputs(mbuf, "block:", 6, &res))
99 return(-1);
100 break;
101 case (MD_NS_INLINE):
102 if ( ! ml_nputs(mbuf, "inline:", 7, &res))
103 return(-1);
104 break;
105 case (MD_NS_BODY):
106 if ( ! ml_nputs(mbuf, "body:", 5, &res))
107 return(-1);
108 break;
109 case (MD_NS_HEAD):
110 if ( ! ml_nputs(mbuf, "head:", 5, &res))
111 return(-1);
112 break;
113 default:
114 break;
115 }
116
117 if ( ! ml_puts(mbuf, toknames[tok], &res))
118 return(-1);
119 return((ssize_t)res);
120 }
121
122
123 /* ARGSUSED */
124 static int
125 xml_begin(struct md_mbuf *mbuf, const struct md_args *args,
126 const struct tm *tm, const char *os,
127 const char *title, enum roffmsec sec, enum roffvol 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:body=\"body\" "
135 "xmlns:head=\"head\" "
136 "xmlns:inline=\"inline\">", NULL));
137 }
138
139
140 /* ARGSUSED */
141 static int
142 xml_end(struct md_mbuf *mbuf, const struct md_args *args)
143 {
144
145 return(ml_puts(mbuf, "</mdoc>", NULL));
146 }
147
148
149 /* ARGSUSED */
150 static ssize_t
151 xml_beginstring(struct md_mbuf *mbuf,
152 const struct md_args *args,
153 const char *buf, size_t sz)
154 {
155
156 return(0);
157 }
158
159
160 /* ARGSUSED */
161 static ssize_t
162 xml_endstring(struct md_mbuf *mbuf,
163 const struct md_args *args,
164 const char *buf, size_t sz)
165 {
166
167 return(0);
168 }
169
170
171 /* ARGSUSED */
172 static ssize_t
173 xml_begintag(struct md_mbuf *mbuf, void *data,
174 const struct md_args *args, enum md_ns ns,
175 int tok, const int *argc, const char **argv)
176 {
177 ssize_t res, sz;
178
179 if (-1 == (res = xml_printtagname(mbuf, ns, tok)))
180 return(-1);
181 if (-1 == (sz = xml_printtagargs(mbuf, argc, argv)))
182 return(-1);
183 return(res + sz);
184 }
185
186
187 /* ARGSUSED */
188 static ssize_t
189 xml_endtag(struct md_mbuf *mbuf, void *data,
190 const struct md_args *args, enum md_ns ns, int tok)
191 {
192
193 return(xml_printtagname(mbuf, ns, tok));
194 }
195
196
197 /* ARGSUSED */
198 int
199 xml_alloc(void **p)
200 {
201
202 *p = NULL;
203 return(1);
204 }
205
206
207 /* ARGSUSED */
208 void
209 xml_free(void *p)
210 {
211
212 /* Do nothing. */
213 }
214
215
216 int
217 md_line_xml(void *data, char *buf)
218 {
219
220 return(mlg_line((struct md_mlg *)data, buf));
221 }
222
223
224 int
225 md_exit_xml(void *data, int flush)
226 {
227
228 return(mlg_exit((struct md_mlg *)data, flush));
229 }
230
231
232 void *
233 md_init_xml(const struct md_args *args,
234 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
235 {
236 struct ml_cbs cbs;
237
238 cbs.ml_alloc = xml_alloc;
239 cbs.ml_free = xml_free;
240 cbs.ml_begintag = xml_begintag;
241 cbs.ml_endtag = xml_endtag;
242 cbs.ml_begin = xml_begin;
243 cbs.ml_end = xml_end;
244 cbs.ml_beginstring = xml_beginstring;
245 cbs.ml_endstring = xml_endstring;
246
247 return(mlg_alloc(args, rbuf, mbuf, &cbs));
248 }
249