]> git.cameronkatri.com Git - mandoc.git/blob - libmdocml.c
Inclusion of "real" validation code.
[mandoc.git] / libmdocml.c
1 /* $Id: libmdocml.c,v 1.14 2008/11/29 14:14:21 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 md_buf_flush(struct md_mbuf *buf)
60 {
61 ssize_t sz;
62
63 assert(buf);
64 assert(buf->buf);
65 assert(buf->name);
66
67 if (0 == buf->pos)
68 return(1);
69
70 sz = write(buf->fd, buf->buf, buf->pos);
71
72 if (-1 == sz) {
73 warn("%s", buf->name);
74 return(0);
75 } else if ((size_t)sz != buf->pos) {
76 warnx("%s: short write", buf->name);
77 return(0);
78 }
79
80 buf->pos = 0;
81 return(1);
82 }
83
84
85 int
86 md_buf_putchar(struct md_mbuf *buf, char c)
87 {
88
89 assert(buf);
90 return(md_buf_puts(buf, &c, 1));
91 }
92
93
94 int
95 md_buf_putstring(struct md_mbuf *buf, const char *p)
96 {
97
98 assert(buf);
99 return(md_buf_puts(buf, p, strlen(p)));
100 }
101
102
103 int
104 md_buf_puts(struct md_mbuf *buf, const char *p, size_t sz)
105 {
106 size_t ssz;
107
108 assert(p);
109 assert(buf);
110 assert(buf->buf);
111
112 /* LINTED */
113 while (buf->pos + sz > buf->bufsz) {
114 ssz = buf->bufsz - buf->pos;
115 (void)memcpy(/* LINTED */
116 buf->buf + buf->pos, p, ssz);
117 p += (long)ssz;
118 sz -= ssz;
119 buf->pos += ssz;
120
121 if ( ! md_buf_flush(buf))
122 return(0);
123 }
124
125 (void)memcpy(/* LINTED */
126 buf->buf + buf->pos, p, sz);
127 buf->pos += sz;
128 return(1);
129 }
130
131
132 static int
133 md_run_leave(const struct md_args *args, struct md_mbuf *mbuf,
134 struct md_rbuf *rbuf, int c, void *data)
135 {
136 assert(args);
137 assert(mbuf);
138 assert(rbuf);
139
140 /* Run exiters. */
141 switch (args->type) {
142 case (MD_HTML4_STRICT):
143 if ( ! md_exit_html4_strict(data, -1 == c ? 0 : 1))
144 c = -1;
145 break;
146 default:
147 if ( ! md_exit_valid(data, -1 == c ? 0 : 1))
148 c = -1;
149 break;
150 }
151
152 /* Make final flush of buffer. */
153 if ( ! md_buf_flush(mbuf))
154 return(-1);
155
156 return(c);
157 }
158
159
160 static int
161 md_run_enter(const struct md_args *args, struct md_mbuf *mbuf,
162 struct md_rbuf *rbuf, void *p)
163 {
164 ssize_t sz, i;
165 char line[BUFFER_LINE];
166 size_t pos;
167 md_line fp;
168
169 assert(args);
170 assert(mbuf);
171 assert(rbuf);
172
173 /* Function ptrs to line-parsers. */
174 switch (args->type) {
175 case (MD_HTML4_STRICT):
176 fp = md_line_html4_strict;
177 break;
178 default:
179 fp = md_line_valid;
180 break;
181 }
182
183 pos = 0;
184
185 again:
186 if (-1 == (sz = md_buf_fill(rbuf))) {
187 return(md_run_leave(args, mbuf, rbuf, -1, p));
188 } else if (0 == sz && 0 != pos) {
189 warnx("%s: no newline at end of file", rbuf->name);
190 return(md_run_leave(args, mbuf, rbuf, -1, p));
191 } else if (0 == sz)
192 return(md_run_leave(args, mbuf, rbuf, 0, p));
193
194 for (i = 0; i < sz; i++) {
195 /*
196 if ( ! isascii(rbuf->buf[i])) {
197 warnx("%s: non-ascii char (line %zu, col %zu)",
198 rbuf->name, rbuf->line, pos);
199 return(md_run_leave(args, mbuf, rbuf, -1, p));
200 }
201 */
202 if ('\n' != rbuf->buf[i]) {
203 if (pos < BUFFER_LINE) {
204 /* LINTED */
205 line[pos++] = rbuf->buf[i];
206 continue;
207 }
208 warnx("%s: line %zu too long",
209 rbuf->name, rbuf->line);
210 return(md_run_leave(args, mbuf, rbuf, -1, p));
211 }
212
213 line[pos] = 0;
214 if ( ! (*fp)(p, line))
215 return(md_run_leave(args, mbuf, rbuf, -1, p));
216 rbuf->line++;
217 pos = 0;
218 }
219
220 goto again;
221 /* NOTREACHED */
222 }
223
224
225 int
226 md_run(const struct md_args *args,
227 const struct md_buf *out, const struct md_buf *in)
228 {
229 struct md_mbuf mbuf;
230 struct md_rbuf rbuf;
231 void *data;
232
233 assert(args);
234 assert(in);
235 assert(out);
236
237 (void)memcpy(&mbuf, out, sizeof(struct md_buf));
238 (void)memcpy(&rbuf, in, sizeof(struct md_buf));
239
240 mbuf.pos = 0;
241 rbuf.line = 1;
242
243 /* Run initialisers. */
244 switch (args->type) {
245 case (MD_HTML4_STRICT):
246 data = md_init_html4_strict
247 (args, &mbuf, &rbuf);
248 break;
249 default:
250 data = md_init_valid
251 (args, &mbuf, &rbuf);
252 break;
253 }
254
255 /* Go into mainline. */
256 return(md_run_enter(args, &mbuf, &rbuf, data));
257 }