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