- if ( ! read_whole_file(curp, &blk, &with_mmap))
- return;
-
- if (NULL == curp->roff)
- curp->roff = roff_init(curp);
- if (NULL == (roff = curp->roff))
- goto bailout;
-
- for (i = 0, lnn = 1; i < (int)blk.sz;) {
- pos = 0;
- lnn_start = lnn;
- while (i < (int)blk.sz) {
- if ('\n' == blk.buf[i]) {
- ++i;
- ++lnn;
- break;
- }
- /* Trailing backslash is like a plain character. */
- if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
- if (pos >= (int)ln.sz)
- if (! resize_buf(&ln, 256))
- goto bailout;
- ln.buf[pos++] = blk.buf[i++];
- continue;
- }
- /* Found an escape and at least one other character. */
- if ('\n' == blk.buf[i + 1]) {
- /* Escaped newlines are skipped over */
- i += 2;
- ++lnn;
- continue;
- }
- if ('"' == blk.buf[i + 1]) {
- i += 2;
- /* Comment, skip to end of line */
- for (; i < (int)blk.sz; ++i) {
- if ('\n' == blk.buf[i]) {
- ++i;
- ++lnn;
- break;
- }
- }
- /* Backout trailing whitespaces */
- for (; pos > 0; --pos) {
- if (ln.buf[pos - 1] != ' ')
- break;
- if (pos > 2 && ln.buf[pos - 2] == '\\')
- break;
- }
- break;
- }
- /* Some other escape sequence, copy and continue. */
- if (pos + 1 >= (int)ln.sz)
- if (! resize_buf(&ln, 256))
- goto bailout;
-
- ln.buf[pos++] = blk.buf[i++];
- ln.buf[pos++] = blk.buf[i++];
- }
-
- if (pos >= (int)ln.sz)
- if (! resize_buf(&ln, 256))
- goto bailout;
- ln.buf[pos] = '\0';
-
- /*
- * A significant amount of complexity is contained by
- * the roff preprocessor. It's line-oriented but can be
- * expressed on one line, so we need at times to
- * readjust our starting point and re-run it. The roff
- * preprocessor can also readjust the buffers with new
- * data, so we pass them in wholesale.
- */
-
- of = 0;
- do {
- re = roff_parseln(roff, lnn_start,
- &ln.buf, &ln.sz, of, &of);
- } while (ROFF_RERUN == re);
-
- if (ROFF_IGN == re)
- continue;
- else if (ROFF_ERR == re)
- goto bailout;
-
- /*
- * If input parsers have not been allocated, do so now.
- * We keep these instanced betwen parsers, but set them
- * locally per parse routine since we can use different
- * parsers with each one.
- */
-
- if ( ! (man || mdoc))
- if ( ! pset(ln.buf + of, pos - of, curp, &man, &mdoc))
- goto bailout;
-
- /* Lastly, push down into the parsers themselves. */
-
- if (man && ! man_parseln(man, lnn_start, ln.buf, of))
- goto bailout;
- if (mdoc && ! mdoc_parseln(mdoc, lnn_start, ln.buf, of))
- goto bailout;
- }
-
- /* NOTE a parser may not have been assigned, yet. */
-
- if ( ! (man || mdoc)) {
- fprintf(stderr, "%s: Not a manual\n", curp->file);
- goto bailout;
- }
-
- /* Clean up the parse routine ASTs. */
-
- if (mdoc && ! mdoc_endparse(mdoc))
- goto bailout;
- if (man && ! man_endparse(man))
- goto bailout;
- if (roff && ! roff_endparse(roff))
- goto bailout;