Annotation of mandoc/read.c, Revision 1.4
1.4 ! kristaps 1: /* $Id: read.c,v 1.3 2011/03/20 16:02:05 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.2 kristaps 18: #include <sys/stat.h>
1.1 kristaps 19: #include <sys/mman.h>
20:
21: #include <assert.h>
22: #include <ctype.h>
23: #include <fcntl.h>
1.3 kristaps 24: #include <stdarg.h>
1.1 kristaps 25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28: #include <unistd.h>
29:
30: #include "mandoc.h"
1.3 kristaps 31: #include "libmandoc.h"
1.1 kristaps 32: #include "mdoc.h"
33: #include "man.h"
34: #include "roff.h"
35:
36: #ifndef MAP_FILE
37: #define MAP_FILE 0
38: #endif
39:
40: #define REPARSE_LIMIT 1000
41:
42: struct buf {
43: char *buf; /* binary input buffer */
44: size_t sz; /* size of binary buffer */
45: };
46:
47: struct mparse {
48: enum mandoclevel file_status; /* status of current parse */
1.3 kristaps 49: enum mandoclevel wlevel; /* ignore messages below this */
1.1 kristaps 50: int line; /* line number in the file */
51: enum mparset inttype; /* which parser to use */
52: struct man *pman; /* persistent man parser */
53: struct mdoc *pmdoc; /* persistent mdoc parser */
54: struct man *man; /* man parser */
55: struct mdoc *mdoc; /* mdoc parser */
56: struct roff *roff; /* roff parser (!NULL) */
57: struct regset regs; /* roff registers */
58: int reparse_count; /* finite interp. stack */
59: mandocmsg mmsg; /* warning/error message handler */
60: void *arg; /* argument to mmsg */
1.3 kristaps 61: const char *file;
1.1 kristaps 62: };
63:
64: static void resize_buf(struct buf *, size_t);
65: static void mparse_buf_r(struct mparse *, struct buf, int);
66: static void mparse_readfd_r(struct mparse *, int, const char *, int);
67: static void pset(const char *, int, struct mparse *);
68: static void pdesc(struct mparse *, const char *, int);
69: static int read_whole_file(const char *, int, struct buf *, int *);
70: static void mparse_end(struct mparse *);
71:
1.3 kristaps 72: static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = {
73: MANDOCERR_OK,
74: MANDOCERR_WARNING,
75: MANDOCERR_WARNING,
76: MANDOCERR_ERROR,
77: MANDOCERR_FATAL,
78: MANDOCERR_MAX,
79: MANDOCERR_MAX
80: };
81:
1.1 kristaps 82: static void
83: resize_buf(struct buf *buf, size_t initial)
84: {
85:
86: buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
87: buf->buf = mandoc_realloc(buf->buf, buf->sz);
88: }
89:
90: static void
91: pset(const char *buf, int pos, struct mparse *curp)
92: {
93: int i;
94:
95: /*
96: * Try to intuit which kind of manual parser should be used. If
97: * passed in by command-line (-man, -mdoc), then use that
98: * explicitly. If passed as -mandoc, then try to guess from the
99: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
100: * default to -man, which is more lenient.
101: *
102: * Separate out pmdoc/pman from mdoc/man: the first persists
103: * through all parsers, while the latter is used per-parse.
104: */
105:
106: if ('.' == buf[0] || '\'' == buf[0]) {
107: for (i = 1; buf[i]; i++)
108: if (' ' != buf[i] && '\t' != buf[i])
109: break;
110: if ('\0' == buf[i])
111: return;
112: }
113:
114: switch (curp->inttype) {
115: case (MPARSE_MDOC):
116: if (NULL == curp->pmdoc)
1.3 kristaps 117: curp->pmdoc = mdoc_alloc(&curp->regs, curp);
1.1 kristaps 118: assert(curp->pmdoc);
119: curp->mdoc = curp->pmdoc;
120: return;
121: case (MPARSE_MAN):
122: if (NULL == curp->pman)
1.3 kristaps 123: curp->pman = man_alloc(&curp->regs, curp);
1.1 kristaps 124: assert(curp->pman);
125: curp->man = curp->pman;
126: return;
127: default:
128: break;
129: }
130:
131: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
132: if (NULL == curp->pmdoc)
1.3 kristaps 133: curp->pmdoc = mdoc_alloc(&curp->regs, curp);
1.1 kristaps 134: assert(curp->pmdoc);
135: curp->mdoc = curp->pmdoc;
136: return;
137: }
138:
139: if (NULL == curp->pman)
1.3 kristaps 140: curp->pman = man_alloc(&curp->regs, curp);
1.1 kristaps 141: assert(curp->pman);
142: curp->man = curp->pman;
143: }
144:
145: /*
146: * Main parse routine for an opened file. This is called for each
147: * opened file and simply loops around the full input file, possibly
148: * nesting (i.e., with `so').
149: */
150: static void
151: mparse_buf_r(struct mparse *curp, struct buf blk, int start)
152: {
153: const struct tbl_span *span;
154: struct buf ln;
155: enum rofferr rr;
156: int i, of, rc;
157: int pos; /* byte number in the ln buffer */
158: int lnn; /* line number in the real file */
159: unsigned char c;
160:
161: memset(&ln, 0, sizeof(struct buf));
162:
163: lnn = curp->line;
164: pos = 0;
165:
166: for (i = 0; i < (int)blk.sz; ) {
167: if (0 == pos && '\0' == blk.buf[i])
168: break;
169:
170: if (start) {
171: curp->line = lnn;
172: curp->reparse_count = 0;
173: }
174:
175: while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
176:
177: /*
178: * When finding an unescaped newline character,
179: * leave the character loop to process the line.
180: * Skip a preceding carriage return, if any.
181: */
182:
183: if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz &&
184: '\n' == blk.buf[i + 1])
185: ++i;
186: if ('\n' == blk.buf[i]) {
187: ++i;
188: ++lnn;
189: break;
190: }
191:
192: /*
193: * Warn about bogus characters. If you're using
194: * non-ASCII encoding, you're screwing your
195: * readers. Since I'd rather this not happen,
196: * I'll be helpful and drop these characters so
197: * we don't display gibberish. Note to manual
198: * writers: use special characters.
199: */
200:
201: c = (unsigned char) blk.buf[i];
202:
203: if ( ! (isascii(c) &&
204: (isgraph(c) || isblank(c)))) {
1.3 kristaps 205: mandoc_msg(MANDOCERR_BADCHAR, curp,
1.1 kristaps 206: curp->line, pos, "ignoring byte");
207: i++;
208: continue;
209: }
210:
211: /* Trailing backslash = a plain char. */
212:
213: if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
214: if (pos >= (int)ln.sz)
215: resize_buf(&ln, 256);
216: ln.buf[pos++] = blk.buf[i++];
217: continue;
218: }
219:
220: /*
221: * Found escape and at least one other character.
222: * When it's a newline character, skip it.
223: * When there is a carriage return in between,
224: * skip that one as well.
225: */
226:
227: if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz &&
228: '\n' == blk.buf[i + 2])
229: ++i;
230: if ('\n' == blk.buf[i + 1]) {
231: i += 2;
232: ++lnn;
233: continue;
234: }
235:
236: if ('"' == blk.buf[i + 1]) {
237: i += 2;
238: /* Comment, skip to end of line */
239: for (; i < (int)blk.sz; ++i) {
240: if ('\n' == blk.buf[i]) {
241: ++i;
242: ++lnn;
243: break;
244: }
245: }
246:
247: /* Backout trailing whitespaces */
248: for (; pos > 0; --pos) {
249: if (ln.buf[pos - 1] != ' ')
250: break;
251: if (pos > 2 && ln.buf[pos - 2] == '\\')
252: break;
253: }
254: break;
255: }
256:
257: /* Some other escape sequence, copy & cont. */
258:
259: if (pos + 1 >= (int)ln.sz)
260: resize_buf(&ln, 256);
261:
262: ln.buf[pos++] = blk.buf[i++];
263: ln.buf[pos++] = blk.buf[i++];
264: }
265:
266: if (pos >= (int)ln.sz)
267: resize_buf(&ln, 256);
268:
269: ln.buf[pos] = '\0';
270:
271: /*
272: * A significant amount of complexity is contained by
273: * the roff preprocessor. It's line-oriented but can be
274: * expressed on one line, so we need at times to
275: * readjust our starting point and re-run it. The roff
276: * preprocessor can also readjust the buffers with new
277: * data, so we pass them in wholesale.
278: */
279:
280: of = 0;
281:
282: rerun:
283: rr = roff_parseln
284: (curp->roff, curp->line,
285: &ln.buf, &ln.sz, of, &of);
286:
287: switch (rr) {
288: case (ROFF_REPARSE):
289: if (REPARSE_LIMIT >= ++curp->reparse_count)
290: mparse_buf_r(curp, ln, 0);
291: else
1.3 kristaps 292: mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.1 kristaps 293: curp->line, pos, NULL);
294: pos = 0;
295: continue;
296: case (ROFF_APPEND):
297: pos = (int)strlen(ln.buf);
298: continue;
299: case (ROFF_RERUN):
300: goto rerun;
301: case (ROFF_IGN):
302: pos = 0;
303: continue;
304: case (ROFF_ERR):
305: assert(MANDOCLEVEL_FATAL <= curp->file_status);
306: break;
307: case (ROFF_SO):
308: mparse_readfd_r(curp, -1, ln.buf + of, 1);
309: if (MANDOCLEVEL_FATAL <= curp->file_status)
310: break;
311: pos = 0;
312: continue;
313: default:
314: break;
315: }
316:
317: /*
318: * If we encounter errors in the recursive parse, make
319: * sure we don't continue parsing.
320: */
321:
322: if (MANDOCLEVEL_FATAL <= curp->file_status)
323: break;
324:
325: /*
326: * If input parsers have not been allocated, do so now.
327: * We keep these instanced betwen parsers, but set them
328: * locally per parse routine since we can use different
329: * parsers with each one.
330: */
331:
332: if ( ! (curp->man || curp->mdoc))
333: pset(ln.buf + of, pos - of, curp);
334:
335: /*
336: * Lastly, push down into the parsers themselves. One
337: * of these will have already been set in the pset()
338: * routine.
339: * If libroff returns ROFF_TBL, then add it to the
340: * currently open parse. Since we only get here if
341: * there does exist data (see tbl_data.c), we're
342: * guaranteed that something's been allocated.
343: * Do the same for ROFF_EQN.
344: */
345:
346: rc = -1;
347:
348: if (ROFF_TBL == rr)
349: while (NULL != (span = roff_span(curp->roff))) {
350: rc = curp->man ?
351: man_addspan(curp->man, span) :
352: mdoc_addspan(curp->mdoc, span);
353: if (0 == rc)
354: break;
355: }
356: else if (ROFF_EQN == rr)
357: rc = curp->mdoc ?
358: mdoc_addeqn(curp->mdoc,
359: roff_eqn(curp->roff)) :
360: man_addeqn(curp->man,
361: roff_eqn(curp->roff));
362: else if (curp->man || curp->mdoc)
363: rc = curp->man ?
364: man_parseln(curp->man,
365: curp->line, ln.buf, of) :
366: mdoc_parseln(curp->mdoc,
367: curp->line, ln.buf, of);
368:
369: if (0 == rc) {
370: assert(MANDOCLEVEL_FATAL <= curp->file_status);
371: break;
372: }
373:
374: /* Temporary buffers typically are not full. */
375:
376: if (0 == start && '\0' == blk.buf[i])
377: break;
378:
379: /* Start the next input line. */
380:
381: pos = 0;
382: }
383:
384: free(ln.buf);
385: }
386:
387: static void
388: pdesc(struct mparse *curp, const char *file, int fd)
389: {
390: struct buf blk;
391: int with_mmap;
392:
393: /*
394: * Run for each opened file; may be called more than once for
395: * each full parse sequence if the opened file is nested (i.e.,
396: * from `so'). Simply sucks in the whole file and moves into
397: * the parse phase for the file.
398: */
399:
400: if ( ! read_whole_file(file, fd, &blk, &with_mmap)) {
401: curp->file_status = MANDOCLEVEL_SYSERR;
402: return;
403: }
404:
405: /* Line number is per-file. */
406:
407: curp->line = 1;
408:
409: mparse_buf_r(curp, blk, 1);
410:
411: if (with_mmap)
412: munmap(blk.buf, blk.sz);
413: else
414: free(blk.buf);
415: }
416:
417: static int
418: read_whole_file(const char *file, int fd, struct buf *fb, int *with_mmap)
419: {
420: struct stat st;
421: size_t off;
422: ssize_t ssz;
423:
424: if (-1 == fstat(fd, &st)) {
425: perror(file);
426: return(0);
427: }
428:
429: /*
430: * If we're a regular file, try just reading in the whole entry
431: * via mmap(). This is faster than reading it into blocks, and
432: * since each file is only a few bytes to begin with, I'm not
433: * concerned that this is going to tank any machines.
434: */
435:
436: if (S_ISREG(st.st_mode)) {
437: if (st.st_size >= (1U << 31)) {
438: fprintf(stderr, "%s: input too large\n", file);
439: return(0);
440: }
441: *with_mmap = 1;
442: fb->sz = (size_t)st.st_size;
443: fb->buf = mmap(NULL, fb->sz, PROT_READ,
444: MAP_FILE|MAP_SHARED, fd, 0);
445: if (fb->buf != MAP_FAILED)
446: return(1);
447: }
448:
449: /*
450: * If this isn't a regular file (like, say, stdin), then we must
451: * go the old way and just read things in bit by bit.
452: */
453:
454: *with_mmap = 0;
455: off = 0;
456: fb->sz = 0;
457: fb->buf = NULL;
458: for (;;) {
459: if (off == fb->sz) {
460: if (fb->sz == (1U << 31)) {
461: fprintf(stderr, "%s: input too large\n", file);
462: break;
463: }
464: resize_buf(fb, 65536);
465: }
466: ssz = read(fd, fb->buf + (int)off, fb->sz - off);
467: if (ssz == 0) {
468: fb->sz = off;
469: return(1);
470: }
471: if (ssz == -1) {
472: perror(file);
473: break;
474: }
475: off += (size_t)ssz;
476: }
477:
478: free(fb->buf);
479: fb->buf = NULL;
480: return(0);
481: }
482:
483: static void
484: mparse_end(struct mparse *curp)
485: {
486:
487: if (MANDOCLEVEL_FATAL <= curp->file_status)
488: return;
489:
490: if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
491: assert(MANDOCLEVEL_FATAL <= curp->file_status);
492: return;
493: }
494:
495: if (curp->man && ! man_endparse(curp->man)) {
496: assert(MANDOCLEVEL_FATAL <= curp->file_status);
497: return;
498: }
499:
500: #if 0
1.3 kristaps 501: /* FIXME: NOTE a parser may not have been assigned, yet. */
1.1 kristaps 502:
503: if ( ! (curp->man || curp->mdoc)) {
504: /* FIXME: make into an mandoc.h error. */
505: fprintf(stderr, "%s: Not a manual\n", curp->file);
506: curp->file_status = MANDOCLEVEL_FATAL;
507: goto cleanup;
508: }
509: #endif
510:
511: roff_endparse(curp->roff);
512: }
513:
514: static void
515: mparse_readfd_r(struct mparse *curp, int fd, const char *file, int re)
516: {
517: const char *svfile;
518:
519: if (-1 == fd)
520: if (-1 == (fd = open(file, O_RDONLY, 0))) {
521: perror(file);
522: curp->file_status = MANDOCLEVEL_SYSERR;
523: return;
524: }
525:
1.3 kristaps 526: svfile = curp->file;
527: curp->file = file;
1.1 kristaps 528:
529: pdesc(curp, file, fd);
530:
531: if (0 == re && MANDOCLEVEL_FATAL > curp->file_status)
532: mparse_end(curp);
533:
534: if (STDIN_FILENO != fd && -1 == close(fd))
535: perror(file);
536:
1.3 kristaps 537: curp->file = svfile;
1.1 kristaps 538: }
539:
540: enum mandoclevel
541: mparse_readfd(struct mparse *curp, int fd, const char *file)
542: {
543:
544: mparse_readfd_r(curp, fd, file, 0);
545: return(curp->file_status);
546: }
547:
548: struct mparse *
1.4 ! kristaps 549: mparse_alloc(enum mparset inttype, enum mandoclevel wlevel, mandocmsg mmsg, void *arg)
1.1 kristaps 550: {
551: struct mparse *curp;
552:
553: curp = mandoc_calloc(1, sizeof(struct mparse));
554:
1.3 kristaps 555: curp->wlevel = wlevel;
1.1 kristaps 556: curp->mmsg = mmsg;
557: curp->arg = arg;
558: curp->inttype = inttype;
559:
1.3 kristaps 560: curp->roff = roff_alloc(&curp->regs, curp);
1.1 kristaps 561: return(curp);
562: }
563:
564: void
565: mparse_reset(struct mparse *curp)
566: {
567:
568: memset(&curp->regs, 0, sizeof(struct regset));
569:
570: roff_reset(curp->roff);
571:
572: if (curp->mdoc)
573: mdoc_reset(curp->mdoc);
574: if (curp->man)
575: man_reset(curp->man);
576:
577: curp->file_status = MANDOCLEVEL_OK;
578: curp->mdoc = NULL;
579: curp->man = NULL;
580: }
581:
582: void
583: mparse_free(struct mparse *curp)
584: {
585:
586: if (curp->pmdoc)
587: mdoc_free(curp->pmdoc);
588: if (curp->pman)
589: man_free(curp->pman);
590: if (curp->roff)
591: roff_free(curp->roff);
592:
593: free(curp);
594: }
595:
596: void
597: mparse_result(struct mparse *curp, struct mdoc **mdoc, struct man **man)
598: {
599:
600: *mdoc = curp->mdoc;
601: *man = curp->man;
1.3 kristaps 602: }
603:
604: void
605: mandoc_vmsg(enum mandocerr t, struct mparse *m,
606: int ln, int pos, const char *fmt, ...)
607: {
608: char buf[256];
609: va_list ap;
610:
611: va_start(ap, fmt);
612: vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
613: va_end(ap);
614:
615: mandoc_msg(t, m, ln, pos, buf);
616: }
617:
618: void
619: mandoc_msg(enum mandocerr er, struct mparse *m,
620: int ln, int col, const char *msg)
621: {
622: enum mandoclevel level;
623:
624: level = MANDOCLEVEL_FATAL;
625: while (er < mandoclimits[level])
626: level--;
627:
628: if (level < m->wlevel)
629: return;
630:
631: (*m->mmsg)(er, level, m->file, ln, col, msg);
632:
633: if (m->file_status < level)
634: m->file_status = level;
1.1 kristaps 635: }
CVSweb