]> git.cameronkatri.com Git - mandoc.git/blob - libmdocml.c
Considerable fixes.
[mandoc.git] / libmdocml.c
1 /* $Id: libmdocml.c,v 1.11 2008/11/27 16:54:58 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 "libmdocml.h"
29 #include "private.h"
30
31 #define BUFFER_LINE BUFSIZ /* Default line-buffer size. */
32
33 static int md_run_enter(const struct md_args *,
34 struct md_mbuf *, struct md_rbuf *, void *);
35 static int md_run_leave(const struct md_args *, struct md_mbuf *,
36 struct md_rbuf *, int, void *);
37
38 static ssize_t md_buf_fill(struct md_rbuf *);
39 static int md_buf_flush(struct md_mbuf *);
40
41
42 static ssize_t
43 md_buf_fill(struct md_rbuf *in)
44 {
45 ssize_t ssz;
46
47 assert(in);
48 assert(in->buf);
49 assert(in->bufsz > 0);
50 assert(in->name);
51
52 if (-1 == (ssz = read(in->fd, in->buf, in->bufsz)))
53 warn("%s", in->name);
54
55 return(ssz);
56 }
57
58
59 static int
60 md_buf_flush(struct md_mbuf *buf)
61 {
62 ssize_t sz;
63
64 assert(buf);
65 assert(buf->buf);
66 assert(buf->name);
67
68 if (0 == buf->pos)
69 return(1);
70
71 sz = write(buf->fd, buf->buf, buf->pos);
72
73 if (-1 == sz) {
74 warn("%s", buf->name);
75 return(0);
76 } else if ((size_t)sz != buf->pos) {
77 warnx("%s: short write", buf->name);
78 return(0);
79 }
80
81 buf->pos = 0;
82 return(1);
83 }
84
85
86 int
87 md_buf_putchar(struct md_mbuf *buf, char c)
88 {
89 return(md_buf_puts(buf, &c, 1));
90 }
91
92
93 int
94 md_buf_putstring(struct md_mbuf *buf, const char *p)
95 {
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_HTML4_STRICT):
140 if ( ! md_exit_html4_strict(data, -1 == c ? 0 : 1))
141 c = -1;
142 break;
143 default:
144 if ( ! md_exit_dummy(data, -1 == c ? 0 : 1))
145 c = -1;
146 break;
147 }
148
149 /* Make final flush of buffer. */
150 if ( ! 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 char line[BUFFER_LINE];
163 size_t pos;
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_HTML4_STRICT):
173 fp = md_line_html4_strict;
174 break;
175 default:
176 fp = md_line_dummy;
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 /*
193 if ( ! isascii(rbuf->buf[i])) {
194 warnx("%s: non-ascii char (line %zu, col %zu)",
195 rbuf->name, rbuf->line, pos);
196 return(md_run_leave(args, mbuf, rbuf, -1, p));
197 }
198 */
199 if ('\n' != rbuf->buf[i]) {
200 if (pos < BUFFER_LINE) {
201 /* LINTED */
202 line[pos++] = rbuf->buf[i];
203 continue;
204 }
205 warnx("%s: line %zu too long",
206 rbuf->name, rbuf->line);
207 return(md_run_leave(args, mbuf, rbuf, -1, p));
208 }
209
210 line[pos] = 0;
211 if ( ! (*fp)(p, line, pos))
212 return(md_run_leave(args, mbuf, rbuf, -1, p));
213 rbuf->line++;
214 pos = 0;
215 }
216
217 goto again;
218 /* NOTREACHED */
219 }
220
221
222 int
223 md_run(const struct md_args *args,
224 const struct md_buf *out, const struct md_buf *in)
225 {
226 struct md_mbuf mbuf;
227 struct md_rbuf rbuf;
228 void *data;
229
230 assert(args);
231 assert(in);
232 assert(out);
233
234 (void)memcpy(&mbuf, out, sizeof(struct md_buf));
235 (void)memcpy(&rbuf, in, sizeof(struct md_buf));
236
237 mbuf.pos = 0;
238 rbuf.line = 1;
239
240 /* Run initialisers. */
241 switch (args->type) {
242 case (MD_HTML4_STRICT):
243 data = md_init_html4_strict
244 (args, &mbuf, &rbuf);
245 break;
246 default:
247 data = md_init_dummy
248 (args, &mbuf, &rbuf);
249 break;
250 }
251
252 /* Go into mainline. */
253 return(md_run_enter(args, &mbuf, &rbuf, data));
254 }