Annotation of mandoc/mdocml.c, Revision 1.3
1.3 ! kristaps 1: /* $Id: mdocml.c,v 1.2 2008/11/22 16:55:02 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: */
19: #include <sys/param.h>
20: #include <sys/stat.h>
21:
22: #include <assert.h>
23: #include <err.h>
24: #include <fcntl.h>
25: #include <getopt.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
30:
31: #include "libmdocml.h"
32:
1.3 ! kristaps 33: #define BUFFER_IN_DEF BUFSIZ
! 34: #define BUFFER_OUT_DEF BUFSIZ
! 35: #define BUFFER_LINE BUFSIZ
! 36:
! 37: struct md_rbuf {
1.1 kristaps 38: int fd;
39: const char *name;
40: char *buf;
41: size_t bufsz;
42: size_t line;
43: };
44:
1.2 kristaps 45: struct md_mbuf {
1.3 ! kristaps 46: int fd;
! 47: const char *name;
! 48: char *buf;
! 49: size_t bufsz;
1.2 kristaps 50: size_t pos;
51: };
52:
1.1 kristaps 53: static void usage(void);
54:
55: static int md_begin(const char *, const char *);
56: static int md_begin_io(const char *, const char *);
1.3 ! kristaps 57: static int md_begin_bufs(struct md_mbuf *, struct md_rbuf *);
! 58: static int md_run(struct md_mbuf *, struct md_rbuf *);
! 59: static int md_line(struct md_mbuf *, const struct md_rbuf *,
1.1 kristaps 60: const char *, size_t);
61:
1.3 ! kristaps 62: static ssize_t md_buf_fill(struct md_rbuf *);
1.2 kristaps 63: static int md_buf_flush(struct md_mbuf *);
64:
65: static int md_buf_putchar(struct md_mbuf *, char);
66: static int md_buf_puts(struct md_mbuf *,
67: const char *, size_t);
1.1 kristaps 68:
69:
70: int
71: main(int argc, char *argv[])
72: {
73: int c;
74: char *out, *in;
75:
76: extern char *optarg;
77: extern int optind;
78:
79: out = NULL;
80:
81: while (-1 != (c = getopt(argc, argv, "o:")))
82: switch (c) {
83: case ('o'):
84: out = optarg;
85: break;
86: default:
87: usage();
88: return(1);
89: }
90:
91: argv += optind;
92: if (1 != (argc -= optind)) {
93: usage();
94: return(1);
95: }
96:
97: argc--;
98: in = *argv++;
99:
100: return(md_begin(out, in));
101: }
102:
103:
104: static int
105: md_begin(const char *out, const char *in)
106: {
107: char buf[MAXPATHLEN];
108:
109: assert(in);
110: if (out)
111: return(md_begin_io(out, in));
112:
113: if (strlcpy(buf, in, MAXPATHLEN) >= MAXPATHLEN)
114: warnx("output filename too long");
115: else if (strlcat(buf, ".html", MAXPATHLEN) >= MAXPATHLEN)
116: warnx("output filename too long");
117: else
118: return(md_begin_io(buf, in));
119:
120: return(1);
121: }
122:
123:
124: static int
125: md_begin_io(const char *out, const char *in)
126: {
127: int c;
1.3 ! kristaps 128: struct md_rbuf fin;
! 129: struct md_mbuf fout;
1.1 kristaps 130:
131: assert(out);
132: assert(in);
133:
1.2 kristaps 134: /* TODO: accept "-" as both input and output. */
1.1 kristaps 135:
136: fin.name = in;
137:
138: if (-1 == (fin.fd = open(fin.name, O_RDONLY, 0))) {
139: warn("%s", fin.name);
140: return(1);
141: }
142:
143: fout.name = out;
144:
1.2 kristaps 145: fout.fd = open(fout.name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1.1 kristaps 146: if (-1 == fout.fd) {
147: warn("%s", fout.name);
148: if (-1 == close(fin.fd))
149: warn("%s", fin.name);
150: return(1);
151: }
152:
153: c = md_begin_bufs(&fout, &fin);
154:
155: if (-1 == close(fin.fd)) {
156: warn("%s", in);
157: c = 1;
158: }
159: if (-1 == close(fout.fd)) {
160: warn("%s", out);
161: c = 1;
162: }
163:
164: return(c);
165: }
166:
167:
168: static int
1.3 ! kristaps 169: md_begin_bufs(struct md_mbuf *out, struct md_rbuf *in)
1.1 kristaps 170: {
171: struct stat stin, stout;
172: int c;
173:
174: assert(in);
175: assert(out);
176:
177: if (-1 == fstat(in->fd, &stin)) {
1.3 ! kristaps 178: warn("%s", in->name);
1.1 kristaps 179: return(1);
180: } else if (-1 == fstat(out->fd, &stout)) {
1.3 ! kristaps 181: warn("%s", out->name);
1.1 kristaps 182: return(1);
183: }
184:
1.3 ! kristaps 185: in->bufsz = MAX(stin.st_blksize, BUFFER_IN_DEF);
! 186:
! 187: out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF);
1.1 kristaps 188:
1.3 ! kristaps 189: if (NULL == (in->buf = malloc(in->bufsz))) {
1.1 kristaps 190: warn("malloc");
191: return(1);
1.3 ! kristaps 192: } else if (NULL == (out->buf = malloc(out->bufsz))) {
1.1 kristaps 193: warn("malloc");
1.3 ! kristaps 194: free(in->buf);
1.1 kristaps 195: return(1);
196: }
197:
1.3 ! kristaps 198: c = md_run(out, in);
1.1 kristaps 199:
1.3 ! kristaps 200: free(in->buf);
! 201: free(out->buf);
1.1 kristaps 202:
203: return(c);
204: }
205:
206:
207: static ssize_t
1.3 ! kristaps 208: md_buf_fill(struct md_rbuf *in)
1.1 kristaps 209: {
210: ssize_t ssz;
211:
212: assert(in);
213: assert(in->buf);
214: assert(in->bufsz > 0);
1.3 ! kristaps 215: assert(in->name);
1.1 kristaps 216:
1.3 ! kristaps 217: if (-1 == (ssz = read(in->fd, in->buf, in->bufsz)))
! 218: warn("%s", in->name);
1.1 kristaps 219:
220: return(ssz);
221: }
222:
223:
224: static int
1.3 ! kristaps 225: md_run(struct md_mbuf *out, struct md_rbuf *in)
1.1 kristaps 226: {
227: ssize_t sz, i;
1.3 ! kristaps 228: char line[BUFFER_LINE];
1.1 kristaps 229: size_t pos;
230:
231: assert(in);
232: assert(out);
233:
1.3 ! kristaps 234: out->pos = 0;
! 235: in->line = 1;
1.2 kristaps 236:
1.1 kristaps 237: /* LINTED */
238: for (pos = 0; ; ) {
239: if (-1 == (sz = md_buf_fill(in)))
240: return(1);
241: else if (0 == sz)
242: break;
243:
244: for (i = 0; i < sz; i++) {
245: if ('\n' == in->buf[i]) {
1.3 ! kristaps 246: if (md_line(out, in, line, pos))
1.1 kristaps 247: return(1);
248: in->line++;
249: pos = 0;
250: continue;
251: }
252:
1.3 ! kristaps 253: if (pos < BUFFER_LINE) {
1.1 kristaps 254: /* LINTED */
255: line[pos++] = in->buf[i];
256: continue;
257: }
258:
259: warnx("%s: line %zu too long",
1.3 ! kristaps 260: in->name, in->line);
1.1 kristaps 261: return(1);
262: }
263: }
264:
1.3 ! kristaps 265: if (0 != pos && md_line(out, in, line, pos))
1.2 kristaps 266: return(1);
267:
1.3 ! kristaps 268: return(md_buf_flush(out) ? 0 : 1);
1.2 kristaps 269: }
270:
271:
272: static int
273: md_buf_flush(struct md_mbuf *buf)
274: {
275: ssize_t sz;
276:
277: assert(buf);
278: assert(buf->buf);
1.3 ! kristaps 279: assert(buf->name);
1.2 kristaps 280:
281: if (0 == buf->pos)
282: return(1);
283:
1.3 ! kristaps 284: sz = write(buf->fd, buf->buf, buf->pos);
1.2 kristaps 285:
286: if (-1 == sz) {
1.3 ! kristaps 287: warn("%s", buf->name);
1.2 kristaps 288: return(0);
289: } else if ((size_t)sz != buf->pos) {
1.3 ! kristaps 290: warnx("%s: short write", buf->name);
1.2 kristaps 291: return(0);
292: }
293:
294: buf->pos = 0;
295: return(1);
296: }
297:
1.1 kristaps 298:
1.2 kristaps 299: static int
300: md_buf_putchar(struct md_mbuf *buf, char c)
301: {
302: return(md_buf_puts(buf, &c, 1));
303: }
304:
305:
306: static int
307: md_buf_puts(struct md_mbuf *buf, const char *p, size_t sz)
308: {
309: size_t ssz;
310:
311: assert(p);
312: assert(buf);
313: assert(buf->buf);
314:
1.3 ! kristaps 315: while (buf->pos + sz > buf->bufsz) {
! 316: ssz = buf->bufsz - buf->pos;
! 317: (void)memcpy(buf->buf + buf->pos, p, ssz);
1.2 kristaps 318: p += ssz;
319: sz -= ssz;
320: buf->pos += ssz;
321:
322: if ( ! md_buf_flush(buf))
323: return(0);
324: }
325:
1.3 ! kristaps 326: (void)memcpy(buf->buf + buf->pos, p, sz);
1.2 kristaps 327: buf->pos += sz;
328: return(1);
1.1 kristaps 329: }
330:
331:
332: static int
1.3 ! kristaps 333: md_line(struct md_mbuf *out, const struct md_rbuf *in,
1.1 kristaps 334: const char *buf, size_t sz)
335: {
336:
1.3 ! kristaps 337: /* FIXME: this is just a placeholder function. */
! 338:
1.1 kristaps 339: assert(buf);
340: assert(out);
341: assert(in);
342:
1.2 kristaps 343: if ( ! md_buf_puts(out, buf, sz))
344: return(1);
345: if ( ! md_buf_putchar(out, '\n'))
346: return(1);
1.1 kristaps 347:
348: return(0);
349: }
350:
351:
352: static void
353: usage(void)
354: {
355: extern char *__progname;
356:
357: (void)printf("usage: %s [-o outfile] infile\n", __progname);
358: }
CVSweb