Annotation of mandoc/libmdocml.c, Revision 1.13
1.13 ! kristaps 1: /* $Id: libmdocml.c,v 1.12 2008/11/28 11:21:12 kristaps Exp $ */
1.1 kristaps 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: */
1.2 kristaps 19: #include <assert.h>
1.10 kristaps 20: #include <ctype.h>
1.2 kristaps 21: #include <fcntl.h>
22: #include <err.h>
23: #include <stdio.h>
1.1 kristaps 24: #include <stdlib.h>
1.2 kristaps 25: #include <string.h>
26: #include <unistd.h>
1.1 kristaps 27:
28: #include "libmdocml.h"
1.5 kristaps 29: #include "private.h"
1.2 kristaps 30:
1.7 kristaps 31: #define BUFFER_LINE BUFSIZ /* Default line-buffer size. */
1.2 kristaps 32:
1.5 kristaps 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 *);
1.7 kristaps 37:
1.5 kristaps 38: static ssize_t md_buf_fill(struct md_rbuf *);
39: static int md_buf_flush(struct md_mbuf *);
1.2 kristaps 40:
41:
1.3 kristaps 42: static ssize_t
1.2 kristaps 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:
1.13 ! kristaps 59: static int md_buf_flush(struct md_mbuf *buf)
1.2 kristaps 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:
1.5 kristaps 85: int
1.2 kristaps 86: md_buf_putchar(struct md_mbuf *buf, char c)
87: {
88: return(md_buf_puts(buf, &c, 1));
89: }
90:
91:
1.5 kristaps 92: int
1.4 kristaps 93: md_buf_putstring(struct md_mbuf *buf, const char *p)
94: {
95: return(md_buf_puts(buf, p, strlen(p)));
96: }
97:
98:
1.5 kristaps 99: int
1.2 kristaps 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:
1.3 kristaps 128: static int
1.5 kristaps 129: md_run_leave(const struct md_args *args, struct md_mbuf *mbuf,
130: struct md_rbuf *rbuf, int c, void *data)
1.3 kristaps 131: {
132: assert(args);
133: assert(mbuf);
134: assert(rbuf);
135:
136: /* Run exiters. */
137: switch (args->type) {
138: case (MD_HTML4_STRICT):
1.9 kristaps 139: if ( ! md_exit_html4_strict(data, -1 == c ? 0 : 1))
140: c = -1;
1.3 kristaps 141: break;
1.9 kristaps 142: default:
1.12 kristaps 143: if ( ! md_exit_valid(data, -1 == c ? 0 : 1))
1.9 kristaps 144: c = -1;
1.3 kristaps 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
1.5 kristaps 157: md_run_enter(const struct md_args *args, struct md_mbuf *mbuf,
158: struct md_rbuf *rbuf, void *p)
1.2 kristaps 159: {
160: ssize_t sz, i;
161: char line[BUFFER_LINE];
162: size_t pos;
1.3 kristaps 163: md_line fp;
1.2 kristaps 164:
1.3 kristaps 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;
1.7 kristaps 174: default:
1.12 kristaps 175: fp = md_line_valid;
1.3 kristaps 176: break;
177: }
1.2 kristaps 178:
1.7 kristaps 179: pos = 0;
1.2 kristaps 180:
1.7 kristaps 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));
1.2 kristaps 189:
1.7 kristaps 190: for (i = 0; i < sz; i++) {
1.11 kristaps 191: /*
1.10 kristaps 192: if ( ! isascii(rbuf->buf[i])) {
193: warnx("%s: non-ascii char (line %zu, col %zu)",
194: rbuf->name, rbuf->line, pos);
195: return(md_run_leave(args, mbuf, rbuf, -1, p));
196: }
1.11 kristaps 197: */
1.7 kristaps 198: if ('\n' != rbuf->buf[i]) {
1.2 kristaps 199: if (pos < BUFFER_LINE) {
200: /* LINTED */
1.3 kristaps 201: line[pos++] = rbuf->buf[i];
1.2 kristaps 202: continue;
203: }
204: warnx("%s: line %zu too long",
1.3 kristaps 205: rbuf->name, rbuf->line);
1.5 kristaps 206: return(md_run_leave(args, mbuf, rbuf, -1, p));
1.2 kristaps 207: }
1.7 kristaps 208:
1.8 kristaps 209: line[pos] = 0;
1.9 kristaps 210: if ( ! (*fp)(p, line, pos))
1.7 kristaps 211: return(md_run_leave(args, mbuf, rbuf, -1, p));
212: rbuf->line++;
213: pos = 0;
1.2 kristaps 214: }
215:
1.7 kristaps 216: goto again;
217: /* NOTREACHED */
1.3 kristaps 218: }
219:
220:
221: int
222: md_run(const struct md_args *args,
223: const struct md_buf *out, const struct md_buf *in)
224: {
225: struct md_mbuf mbuf;
226: struct md_rbuf rbuf;
1.5 kristaps 227: void *data;
1.3 kristaps 228:
229: assert(args);
230: assert(in);
231: assert(out);
232:
233: (void)memcpy(&mbuf, out, sizeof(struct md_buf));
234: (void)memcpy(&rbuf, in, sizeof(struct md_buf));
235:
236: mbuf.pos = 0;
237: rbuf.line = 1;
238:
239: /* Run initialisers. */
240: switch (args->type) {
241: case (MD_HTML4_STRICT):
1.9 kristaps 242: data = md_init_html4_strict
243: (args, &mbuf, &rbuf);
1.3 kristaps 244: break;
1.9 kristaps 245: default:
1.12 kristaps 246: data = md_init_valid
1.9 kristaps 247: (args, &mbuf, &rbuf);
1.3 kristaps 248: break;
249: }
1.2 kristaps 250:
1.3 kristaps 251: /* Go into mainline. */
1.5 kristaps 252: return(md_run_enter(args, &mbuf, &rbuf, data));
1.3 kristaps 253: }
CVSweb