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