*** empty log message ***
[mandoc.git] / libmdocml.c
1 /* $Id: libmdocml.c,v 1.20 2008/12/09 17:09:12 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 <ctype.h>
21 #include <fcntl.h>
22 #include <err.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "private.h"
29
30 static int md_run_enter(const struct md_args *,
31 struct md_mbuf *, struct md_rbuf *, void *);
32 static int md_run_leave(const struct md_args *, struct md_mbuf *,
33 struct md_rbuf *, int, void *);
34
35 static ssize_t md_buf_fill(struct md_rbuf *);
36 static int md_buf_flush(struct md_mbuf *);
37
38
39 static ssize_t
40 md_buf_fill(struct md_rbuf *in)
41 {
42 ssize_t ssz;
43
44 assert(in);
45 assert(in->buf);
46 assert(in->bufsz > 0);
47 assert(in->name);
48
49 if (-1 == (ssz = read(in->fd, in->buf, in->bufsz)))
50 warn("%s", in->name);
51
52 return(ssz);
53 }
54
55
56 static int md_buf_flush(struct md_mbuf *buf)
57 {
58 ssize_t sz;
59
60 assert(buf);
61 assert(buf->buf);
62 assert(buf->name);
63
64 if (0 == buf->pos)
65 return(1);
66
67 sz = write(buf->fd, buf->buf, buf->pos);
68
69 if (-1 == sz) {
70 warn("%s", buf->name);
71 return(0);
72 } else if ((size_t)sz != buf->pos) {
73 warnx("%s: short write", buf->name);
74 return(0);
75 }
76
77 buf->pos = 0;
78 return(1);
79 }
80
81
82 int
83 md_buf_putchar(struct md_mbuf *buf, char c)
84 {
85
86 assert(buf);
87 return(md_buf_puts(buf, &c, 1));
88 }
89
90
91 int
92 md_buf_putstring(struct md_mbuf *buf, const char *p)
93 {
94
95 assert(buf);
96 return(md_buf_puts(buf, p, strlen(p)));
97 }
98
99
100 int
101 md_buf_puts(struct md_mbuf *buf, const char *p, size_t sz)
102 {
103 size_t ssz;
104
105 assert(p);
106 assert(buf);
107 assert(buf->buf);
108
109 /* LINTED */
110 while (buf->pos + sz > buf->bufsz) {
111 ssz = buf->bufsz - buf->pos;
112 (void)memcpy(/* LINTED */
113 buf->buf + buf->pos, p, ssz);
114 p += (long)ssz;
115 sz -= ssz;
116 buf->pos += ssz;
117
118 if ( ! md_buf_flush(buf))
119 return(0);
120 }
121
122 (void)memcpy(/* LINTED */
123 buf->buf + buf->pos, p, sz);
124 buf->pos += sz;
125 return(1);
126 }
127
128
129 static int
130 md_run_leave(const struct md_args *args, struct md_mbuf *mbuf,
131 struct md_rbuf *rbuf, int c, void *data)
132 {
133 assert(args);
134 assert(mbuf);
135 assert(rbuf);
136
137 /* Run exiters. */
138 switch (args->type) {
139 case (MD_HTML):
140 if ( ! md_exit_html(data, -1 == c ? 0 : 1))
141 c = -1;
142 break;
143 default:
144 if ( ! md_exit_xml(data, -1 == c ? 0 : 1))
145 c = -1;
146 break;
147 }
148
149 /* Make final flush of buffer. */
150 if (-1 != c && ! md_buf_flush(mbuf))
151 return(-1);
152
153 return(c);
154 }
155
156
157 static int
158 md_run_enter(const struct md_args *args, struct md_mbuf *mbuf,
159 struct md_rbuf *rbuf, void *p)
160 {
161 ssize_t sz, i;
162 size_t pos;
163 char line[MD_LINE];
164 md_line fp;
165
166 assert(args);
167 assert(mbuf);
168 assert(rbuf);
169
170 /* Function ptrs to line-parsers. */
171 switch (args->type) {
172 case (MD_HTML):
173 fp = md_line_html;
174 break;
175 default:
176 fp = md_line_xml;
177 break;
178 }
179
180 pos = 0;
181
182 again:
183 if (-1 == (sz = md_buf_fill(rbuf))) {
184 return(md_run_leave(args, mbuf, rbuf, -1, p));
185 } else if (0 == sz && 0 != pos) {
186 warnx("%s: no newline at end of file", rbuf->name);
187 return(md_run_leave(args, mbuf, rbuf, -1, p));
188 } else if (0 == sz)
189 return(md_run_leave(args, mbuf, rbuf, 0, p));
190
191 for (i = 0; i < sz; i++) {
192 if ('\n' != rbuf->buf[i]) {
193 if (pos < MD_LINE) {
194 /* LINTED */
195 rbuf->linebuf[pos++] = rbuf->buf[i];
196 continue;
197 }
198 warnx("%s: line %zu too long",
199 rbuf->name, rbuf->line);
200 return(md_run_leave(args, mbuf, rbuf, -1, p));
201 }
202
203 rbuf->linebuf[(int)pos] = 0;
204 (void)memcpy(line, rbuf->linebuf, sizeof(line));
205 if ( ! (*fp)(p, line))
206 return(md_run_leave(args, mbuf, rbuf, -1, p));
207 rbuf->line++;
208 pos = 0;
209 }
210
211 goto again;
212 /* NOTREACHED */
213 }
214
215
216 int
217 md_run(const struct md_args *args,
218 const struct md_buf *out, const struct md_buf *in)
219 {
220 struct md_mbuf mbuf;
221 struct md_rbuf rbuf;
222 void *data;
223
224 assert(args);
225 assert(in);
226 assert(out);
227
228 (void)memcpy(&mbuf, out, sizeof(struct md_buf));
229 (void)memcpy(&rbuf, in, sizeof(struct md_buf));
230
231 mbuf.pos = 0;
232 rbuf.line = 1;
233
234 /* Run initialisers. */
235 switch (args->type) {
236 case (MD_HTML):
237 data = md_init_html(args, &mbuf, &rbuf);
238 break;
239 default:
240 data = md_init_xml(args, &mbuf, &rbuf);
241 break;
242 }
243
244 /* Go into mainline. */
245 return(md_run_enter(args, &mbuf, &rbuf, data));
246 }