Annotation of mandoc/libmdocml.c, Revision 1.12
1.12 ! kristaps 1: /* $Id: libmdocml.c,v 1.11 2008/11/27 16:54:58 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.3 kristaps 59: static int
1.2 kristaps 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:
1.5 kristaps 86: int
1.2 kristaps 87: md_buf_putchar(struct md_mbuf *buf, char c)
88: {
89: return(md_buf_puts(buf, &c, 1));
90: }
91:
92:
1.5 kristaps 93: int
1.4 kristaps 94: md_buf_putstring(struct md_mbuf *buf, const char *p)
95: {
96: return(md_buf_puts(buf, p, strlen(p)));
97: }
98:
99:
1.5 kristaps 100: int
1.2 kristaps 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:
1.3 kristaps 129: static int
1.5 kristaps 130: md_run_leave(const struct md_args *args, struct md_mbuf *mbuf,
131: struct md_rbuf *rbuf, int c, void *data)
1.3 kristaps 132: {
133: assert(args);
134: assert(mbuf);
135: assert(rbuf);
136:
137: /* Run exiters. */
138: switch (args->type) {
139: case (MD_HTML4_STRICT):
1.9 kristaps 140: if ( ! md_exit_html4_strict(data, -1 == c ? 0 : 1))
141: c = -1;
1.3 kristaps 142: break;
1.9 kristaps 143: default:
1.12 ! kristaps 144: if ( ! md_exit_valid(data, -1 == c ? 0 : 1))
1.9 kristaps 145: c = -1;
1.3 kristaps 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
1.5 kristaps 158: md_run_enter(const struct md_args *args, struct md_mbuf *mbuf,
159: struct md_rbuf *rbuf, void *p)
1.2 kristaps 160: {
161: ssize_t sz, i;
162: char line[BUFFER_LINE];
163: size_t pos;
1.3 kristaps 164: md_line fp;
1.2 kristaps 165:
1.3 kristaps 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;
1.7 kristaps 175: default:
1.12 ! kristaps 176: fp = md_line_valid;
1.3 kristaps 177: break;
178: }
1.2 kristaps 179:
1.7 kristaps 180: pos = 0;
1.2 kristaps 181:
1.7 kristaps 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));
1.2 kristaps 190:
1.7 kristaps 191: for (i = 0; i < sz; i++) {
1.11 kristaps 192: /*
1.10 kristaps 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: }
1.11 kristaps 198: */
1.7 kristaps 199: if ('\n' != rbuf->buf[i]) {
1.2 kristaps 200: if (pos < BUFFER_LINE) {
201: /* LINTED */
1.3 kristaps 202: line[pos++] = rbuf->buf[i];
1.2 kristaps 203: continue;
204: }
205: warnx("%s: line %zu too long",
1.3 kristaps 206: rbuf->name, rbuf->line);
1.5 kristaps 207: return(md_run_leave(args, mbuf, rbuf, -1, p));
1.2 kristaps 208: }
1.7 kristaps 209:
1.8 kristaps 210: line[pos] = 0;
1.9 kristaps 211: if ( ! (*fp)(p, line, pos))
1.7 kristaps 212: return(md_run_leave(args, mbuf, rbuf, -1, p));
213: rbuf->line++;
214: pos = 0;
1.2 kristaps 215: }
216:
1.7 kristaps 217: goto again;
218: /* NOTREACHED */
1.3 kristaps 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;
1.5 kristaps 228: void *data;
1.3 kristaps 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):
1.9 kristaps 243: data = md_init_html4_strict
244: (args, &mbuf, &rbuf);
1.3 kristaps 245: break;
1.9 kristaps 246: default:
1.12 ! kristaps 247: data = md_init_valid
1.9 kristaps 248: (args, &mbuf, &rbuf);
1.3 kristaps 249: break;
250: }
1.2 kristaps 251:
1.3 kristaps 252: /* Go into mainline. */
1.5 kristaps 253: return(md_run_enter(args, &mbuf, &rbuf, data));
1.3 kristaps 254: }
CVSweb