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