]> git.cameronkatri.com Git - mandoc.git/blob - xml.c
*** empty log message ***
[mandoc.git] / xml.c
1 /* $Id: xml.c,v 1.16 2008/12/05 19:45:15 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 int xml_begin(struct md_mbuf *,
38 const struct md_args *,
39 const struct tm *,
40 const char *, const char *,
41 const char *, const char *);
42 static int xml_end(struct md_mbuf *,
43 const struct md_args *);
44 static ssize_t xml_printtagname(struct md_mbuf *,
45 enum md_ns, int);
46 static ssize_t xml_printtagargs(struct md_mbuf *,
47 const int *, const char **);
48
49
50 static ssize_t
51 xml_printtagargs(struct md_mbuf *mbuf, const int *argc,
52 const char **argv)
53 {
54 int i, c;
55 size_t res;
56
57 if (NULL == argc || NULL == argv)
58 return(0);
59 assert(argc && argv);
60
61 for (res = 0, i = 0; ROFF_ARGMAX != (c = argc[i]); i++) {
62 if ( ! ml_nputs(mbuf, " ", 1, &res))
63 return(-1);
64
65 if ( ! ml_puts(mbuf, tokargnames[c], &res))
66 return(-1);
67 if ( ! ml_nputs(mbuf, "=\"", 2, &res))
68 return(-1);
69 if (argv[i]) {
70 if ( ! ml_putstring(mbuf, argv[i], &res))
71 return(-1);
72 } else if ( ! ml_nputs(mbuf, "true", 4, &res))
73 return(-1);
74 if ( ! ml_nputs(mbuf, "\"", 1, &res))
75 return(-1);
76 }
77
78 return((ssize_t)res);
79 }
80
81
82 static ssize_t
83 xml_printtagname(struct md_mbuf *mbuf, enum md_ns ns, int tok)
84 {
85 size_t res;
86
87 res = 0;
88 switch (ns) {
89 case (MD_NS_BLOCK):
90 if ( ! ml_nputs(mbuf, "block:", 6, &res))
91 return(-1);
92 break;
93 case (MD_NS_INLINE):
94 if ( ! ml_nputs(mbuf, "inline:", 7, &res))
95 return(-1);
96 break;
97 case (MD_NS_BODY):
98 if ( ! ml_nputs(mbuf, "body:", 5, &res))
99 return(-1);
100 break;
101 case (MD_NS_HEAD):
102 if ( ! ml_nputs(mbuf, "head:", 5, &res))
103 return(-1);
104 break;
105 default:
106 break;
107 }
108
109 if ( ! ml_puts(mbuf, toknames[tok], &res))
110 return(-1);
111 return((ssize_t)res);
112 }
113
114
115 /* ARGSUSED */
116 static int
117 xml_begin(struct md_mbuf *mbuf, const struct md_args *args,
118 const struct tm *tm, const char *os,
119 const char *title, const char *section,
120 const char *vol)
121 {
122
123 if ( ! ml_puts(mbuf, "<?xml version=\"1.0\" "
124 "encoding=\"UTF-8\"?>\n", NULL))
125 return(0);
126 return(ml_puts(mbuf, "<mdoc xmlns:block=\"block\" "
127 "xmlns:special=\"special\" "
128 "xmlns:inline=\"inline\">", NULL));
129 }
130
131
132 /* ARGSUSED */
133 static int
134 xml_end(struct md_mbuf *mbuf, const struct md_args *args)
135 {
136
137 return(ml_puts(mbuf, "</mdoc>", NULL));
138 }
139
140
141 /* ARGSUSED */
142 static ssize_t
143 xml_begintag(struct md_mbuf *mbuf, void *data,
144 const struct md_args *args, enum md_ns ns,
145 int tok, const int *argc, const char **argv)
146 {
147 ssize_t res, sz;
148
149 if (-1 == (res = xml_printtagname(mbuf, ns, tok)))
150 return(-1);
151 if (-1 == (sz = xml_printtagargs(mbuf, argc, argv)))
152 return(-1);
153 return(res + sz);
154 }
155
156
157 /* ARGSUSED */
158 static ssize_t
159 xml_endtag(struct md_mbuf *mbuf, void *data,
160 const struct md_args *args, enum md_ns ns, int tok)
161 {
162
163 return(xml_printtagname(mbuf, ns, tok));
164 }
165
166
167 /* ARGSUSED */
168 int
169 xml_alloc(void **p)
170 {
171
172 return(1);
173 }
174
175
176 /* ARGSUSED */
177 void
178 xml_free(void *p)
179 {
180
181 /* Do nothing. */
182 }
183
184
185 int
186 md_line_xml(void *data, char *buf)
187 {
188
189 return(mlg_line((struct md_mlg *)data, buf));
190 }
191
192
193 int
194 md_exit_xml(void *data, int flush)
195 {
196
197 return(mlg_exit((struct md_mlg *)data, flush));
198 }
199
200
201 void *
202 md_init_xml(const struct md_args *args,
203 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
204 {
205 struct ml_cbs cbs;
206
207 cbs.ml_alloc = xml_alloc;
208 cbs.ml_free = xml_free;
209 cbs.ml_begintag = xml_begintag;
210 cbs.ml_endtag = xml_endtag;
211 cbs.ml_begin = xml_begin;
212 cbs.ml_end = xml_end;
213
214 return(mlg_alloc(args, rbuf, mbuf, &cbs));
215 }
216