1 /* $Id: read.c,v 1.214 2019/07/10 19:39:01 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2019 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
36 #include "mandoc_aux.h"
41 #include "mandoc_parse.h"
42 #include "libmandoc.h"
45 #define REPARSE_LIMIT 1000
48 struct roff *roff; /* roff parser (!NULL) */
49 struct roff_man *man; /* man parser */
50 struct buf *primary; /* buffer currently being parsed */
51 struct buf *secondary; /* copy of top level input */
52 struct buf *loop; /* open .while request line */
53 const char *os_s; /* default operating system */
54 int options; /* parser options */
55 int gzip; /* current input file is gzipped */
56 int filenc; /* encoding of the current file */
57 int reparse_count; /* finite interp. stack */
58 int line; /* line number in the file */
61 static void choose_parser(struct mparse *);
62 static void free_buf_list(struct buf *);
63 static void resize_buf(struct buf *, size_t);
64 static int mparse_buf_r(struct mparse *, struct buf, size_t, int);
65 static int read_whole_file(struct mparse *, int, struct buf *, int *);
66 static void mparse_end(struct mparse *);
70 resize_buf(struct buf *buf, size_t initial)
73 buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
74 buf->buf = mandoc_realloc(buf->buf, buf->sz);
78 free_buf_list(struct buf *buf)
91 choose_parser(struct mparse *curp)
97 * If neither command line arguments -mdoc or -man select
98 * a parser nor the roff parser found a .Dd or .TH macro
99 * yet, look ahead in the main input buffer.
102 if ((format = roff_getformat(curp->roff)) == 0) {
103 cp = curp->primary->buf;
104 ep = cp + curp->primary->sz;
106 if (*cp == '.' || *cp == '\'') {
108 if (cp[0] == 'D' && cp[1] == 'd') {
109 format = MPARSE_MDOC;
112 if (cp[0] == 'T' && cp[1] == 'H') {
117 cp = memchr(cp, '\n', ep - cp);
124 if (format == MPARSE_MDOC) {
125 curp->man->meta.macroset = MACROSET_MDOC;
126 if (curp->man->mdocmac == NULL)
127 curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
129 curp->man->meta.macroset = MACROSET_MAN;
130 if (curp->man->manmac == NULL)
131 curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
133 curp->man->meta.first->tok = TOKEN_NONE;
137 * Main parse routine for a buffer.
138 * It assumes encoding and line numbering are already set up.
139 * It can recurse directly (for invocations of user-defined
140 * macros, inline equations, and input line traps)
141 * and indirectly (for .so file inclusion).
144 mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
147 struct buf *firstln, *lastln, *thisln, *loop;
149 size_t pos; /* byte number in the ln buffer */
150 int line_result, result;
152 int lnn; /* line number in the real file */
154 int inloop; /* Saw .while on this level. */
158 ln.buf = mandoc_malloc(ln.sz);
160 firstln = lastln = loop = NULL;
166 while (i < blk.sz && (blk.buf[i] != '\0' || pos != 0)) {
169 curp->reparse_count = 0;
172 curp->filenc & MPARSE_UTF8 &&
173 curp->filenc & MPARSE_LATIN1)
174 curp->filenc = preconv_cue(&blk, i);
177 while (i < blk.sz && (start || blk.buf[i] != '\0')) {
180 * When finding an unescaped newline character,
181 * leave the character loop to process the line.
182 * Skip a preceding carriage return, if any.
185 if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
186 '\n' == blk.buf[i + 1])
188 if ('\n' == blk.buf[i]) {
195 * Make sure we have space for the worst
196 * case of 12 bytes: "\\[u10ffff]\n\0"
199 if (pos + 12 > ln.sz)
200 resize_buf(&ln, 256);
203 * Encode 8-bit input.
208 if ( ! (curp->filenc && preconv_encode(
209 &blk, &i, &ln, &pos, &curp->filenc))) {
210 mandoc_msg(MANDOCERR_CHAR_BAD,
211 curp->line, pos, "0x%x", c);
219 * Exclude control characters.
222 if (c == 0x7f || (c < 0x20 && c != 0x09)) {
223 mandoc_msg(c == 0x00 || c == 0x04 ||
224 c > 0x0a ? MANDOCERR_CHAR_BAD :
225 MANDOCERR_CHAR_UNSUPP,
226 curp->line, pos, "0x%x", c);
233 ln.buf[pos++] = blk.buf[i++];
238 * Maintain a lookaside buffer of all lines.
239 * parsed from this input source.
242 thisln = mandoc_malloc(sizeof(*thisln));
243 thisln->buf = mandoc_strdup(ln.buf);
244 thisln->sz = strlen(ln.buf) + 1;
246 if (firstln == NULL) {
247 firstln = lastln = thisln;
248 if (curp->secondary == NULL)
249 curp->secondary = firstln;
251 lastln->next = thisln;
255 /* XXX Ugly hack to mark the end of the input. */
257 if (i == blk.sz || blk.buf[i] == '\0') {
259 resize_buf(&ln, 256);
260 ln.buf[pos++] = '\n';
265 * A significant amount of complexity is contained by
266 * the roff preprocessor. It's line-oriented but can be
267 * expressed on one line, so we need at times to
268 * readjust our starting point and re-run it. The roff
269 * preprocessor can also readjust the buffers with new
270 * data, so we pass them in wholesale.
275 line_result = roff_parseln(curp->roff, curp->line, &ln, &of);
277 /* Process options. */
279 if (line_result & ROFF_APPEND)
280 assert(line_result == (ROFF_IGN | ROFF_APPEND));
282 if (line_result & ROFF_USERCALL)
283 assert((line_result & ROFF_MASK) == ROFF_REPARSE);
285 if (line_result & ROFF_USERRET) {
286 assert(line_result == (ROFF_IGN | ROFF_USERRET));
288 /* Return from the current macro. */
289 result = ROFF_USERRET;
294 switch (line_result & ROFF_LOOPMASK) {
298 if (curp->loop != NULL) {
299 if (loop == curp->loop)
301 mandoc_msg(MANDOCERR_WHILE_NEST,
302 curp->line, pos, NULL);
310 if (curp->loop == NULL) {
311 mandoc_msg(MANDOCERR_WHILE_FAIL,
312 curp->line, pos, NULL);
316 mandoc_msg(MANDOCERR_WHILE_INTO,
317 curp->line, pos, NULL);
318 curp->loop = loop = NULL;
321 if (line_result & ROFF_LOOPCONT)
324 curp->loop = loop = NULL;
332 /* Process the main instruction from the roff parser. */
334 switch (line_result & ROFF_MASK) {
338 if (curp->man->meta.macroset == MACROSET_NONE)
340 if ((curp->man->meta.macroset == MACROSET_MDOC ?
341 mdoc_parseln(curp->man, curp->line, ln.buf, of) :
342 man_parseln(curp->man, curp->line, ln.buf, of)
349 if (++curp->reparse_count > REPARSE_LIMIT) {
350 /* Abort and return to the top level. */
352 mandoc_msg(MANDOCERR_ROFFLOOP,
353 curp->line, pos, NULL);
356 result = mparse_buf_r(curp, ln, of, 0);
357 if (line_result & ROFF_USERCALL) {
358 roff_userret(curp->roff);
359 /* Continue normally. */
360 if (result & ROFF_USERRET)
363 if (start == 0 && result != ROFF_CONT)
367 if ( ! (curp->options & MPARSE_SO) &&
368 (i >= blk.sz || blk.buf[i] == '\0')) {
369 curp->man->meta.sodest =
370 mandoc_strdup(ln.buf + of);
373 if ((fd = mparse_open(curp, ln.buf + of)) != -1) {
374 mparse_readfd(curp, fd, ln.buf + of);
377 mandoc_msg(MANDOCERR_SO_FAIL,
378 curp->line, of, ".so %s: %s",
379 ln.buf + of, strerror(errno));
380 ln.sz = mandoc_asprintf(&cp,
381 ".sp\nSee the file %s.\n.sp",
386 mparse_buf_r(curp, ln, of, 0);
393 /* Start the next input line. */
396 (line_result & ROFF_LOOPMASK) == ROFF_IGN)
400 if ((line_result & ROFF_APPEND) == 0)
402 if (ln.sz < loop->sz)
403 resize_buf(&ln, loop->sz);
404 (void)strlcat(ln.buf, loop->buf, ln.sz);
409 pos = (line_result & ROFF_APPEND) ? strlen(ln.buf) : 0;
413 if (result != ROFF_USERRET)
414 mandoc_msg(MANDOCERR_WHILE_OUTOF,
415 curp->line, pos, NULL);
419 if (firstln != curp->secondary)
420 free_buf_list(firstln);
425 read_whole_file(struct mparse *curp, int fd, struct buf *fb, int *with_mmap)
431 int gzerrnum, retval;
433 if (fstat(fd, &st) == -1) {
434 mandoc_msg(MANDOCERR_FSTAT, 0, 0, "%s", strerror(errno));
439 * If we're a regular file, try just reading in the whole entry
440 * via mmap(). This is faster than reading it into blocks, and
441 * since each file is only a few bytes to begin with, I'm not
442 * concerned that this is going to tank any machines.
445 if (curp->gzip == 0 && S_ISREG(st.st_mode)) {
446 if (st.st_size > 0x7fffffff) {
447 mandoc_msg(MANDOCERR_TOOLARGE, 0, 0, NULL);
451 fb->sz = (size_t)st.st_size;
452 fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
453 if (fb->buf != MAP_FAILED)
459 * Duplicating the file descriptor is required
460 * because we will have to call gzclose(3)
461 * to free memory used internally by zlib,
462 * but that will also close the file descriptor,
463 * which this function must not do.
465 if ((fd = dup(fd)) == -1) {
466 mandoc_msg(MANDOCERR_DUP, 0, 0,
467 "%s", strerror(errno));
470 if ((gz = gzdopen(fd, "rb")) == NULL) {
471 mandoc_msg(MANDOCERR_GZDOPEN, 0, 0,
472 "%s", strerror(errno));
480 * If this isn't a regular file (like, say, stdin), then we must
481 * go the old way and just read things in bit by bit.
491 if (fb->sz == (1U << 31)) {
492 mandoc_msg(MANDOCERR_TOOLARGE, 0, 0, NULL);
495 resize_buf(fb, 65536);
498 gzread(gz, fb->buf + (int)off, fb->sz - off) :
499 read(fd, fb->buf + (int)off, fb->sz - off);
507 (void)gzerror(gz, &gzerrnum);
508 mandoc_msg(MANDOCERR_READ, 0, 0, "%s",
509 curp->gzip && gzerrnum != Z_ERRNO ?
510 zError(gzerrnum) : strerror(errno));
516 if (curp->gzip && (gzerrnum = gzclose(gz)) != Z_OK)
517 mandoc_msg(MANDOCERR_GZCLOSE, 0, 0, "%s",
518 gzerrnum == Z_ERRNO ? strerror(errno) :
528 mparse_end(struct mparse *curp)
530 if (curp->man->meta.macroset == MACROSET_NONE)
531 curp->man->meta.macroset = MACROSET_MAN;
532 if (curp->man->meta.macroset == MACROSET_MDOC)
533 mdoc_endparse(curp->man);
535 man_endparse(curp->man);
536 roff_endparse(curp->roff);
540 * Read the whole file into memory and call the parsers.
541 * Called recursively when an .so request is encountered.
544 mparse_readfd(struct mparse *curp, int fd, const char *filename)
546 static int recursion_depth;
549 struct buf *save_primary;
550 const char *save_filename;
552 int save_filenc, save_lineno;
555 if (recursion_depth > 64) {
556 mandoc_msg(MANDOCERR_ROFFLOOP, curp->line, 0, NULL);
559 if (read_whole_file(curp, fd, &blk, &with_mmap) == -1)
563 * Save some properties of the parent file.
566 save_primary = curp->primary;
567 save_filenc = curp->filenc;
568 save_lineno = curp->line;
569 save_filename = mandoc_msg_getinfilename();
571 curp->primary = &blk;
572 curp->filenc = curp->options & (MPARSE_UTF8 | MPARSE_LATIN1);
574 mandoc_msg_setinfilename(filename);
576 /* Skip an UTF-8 byte order mark. */
577 if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
578 (unsigned char)blk.buf[0] == 0xef &&
579 (unsigned char)blk.buf[1] == 0xbb &&
580 (unsigned char)blk.buf[2] == 0xbf) {
582 curp->filenc &= ~MPARSE_LATIN1;
587 mparse_buf_r(curp, blk, offset, 1);
588 if (--recursion_depth == 0)
592 * Clean up and restore saved parent properties.
596 munmap(blk.buf, blk.sz);
600 curp->primary = save_primary;
601 curp->filenc = save_filenc;
602 curp->line = save_lineno;
603 if (save_filename != NULL)
604 mandoc_msg_setinfilename(save_filename);
608 mparse_open(struct mparse *curp, const char *file)
613 cp = strrchr(file, '.');
614 curp->gzip = (cp != NULL && ! strcmp(cp + 1, "gz"));
616 /* First try to use the filename as it is. */
618 if ((fd = open(file, O_RDONLY)) != -1)
622 * If that doesn't work and the filename doesn't
623 * already end in .gz, try appending .gz.
628 mandoc_asprintf(&cp, "%s.gz", file);
629 fd = open(cp, O_RDONLY);
638 /* Neither worked, give up. */
644 mparse_alloc(int options, enum mandoc_os os_e, const char *os_s)
648 curp = mandoc_calloc(1, sizeof(struct mparse));
650 curp->options = options;
653 curp->roff = roff_alloc(options);
654 curp->man = roff_man_alloc(curp->roff, curp->os_s,
655 curp->options & MPARSE_QUICK ? 1 : 0);
656 if (curp->options & MPARSE_MDOC) {
657 curp->man->meta.macroset = MACROSET_MDOC;
658 if (curp->man->mdocmac == NULL)
659 curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
660 } else if (curp->options & MPARSE_MAN) {
661 curp->man->meta.macroset = MACROSET_MAN;
662 if (curp->man->manmac == NULL)
663 curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
665 curp->man->meta.first->tok = TOKEN_NONE;
666 curp->man->meta.os_e = os_e;
671 mparse_reset(struct mparse *curp)
673 roff_reset(curp->roff);
674 roff_man_reset(curp->man);
675 free_buf_list(curp->secondary);
676 curp->secondary = NULL;
681 mparse_free(struct mparse *curp)
683 roffhash_free(curp->man->mdocmac);
684 roffhash_free(curp->man->manmac);
685 roff_man_free(curp->man);
686 roff_free(curp->roff);
687 free_buf_list(curp->secondary);
692 mparse_result(struct mparse *curp)
694 roff_state_reset(curp->man);
695 if (curp->options & MPARSE_VALIDATE) {
696 if (curp->man->meta.macroset == MACROSET_MDOC)
697 mdoc_validate(curp->man);
699 man_validate(curp->man);
701 return &curp->man->meta;
705 mparse_copy(const struct mparse *p)
709 for (buf = p->secondary; buf != NULL; buf = buf->next)