]>
git.cameronkatri.com Git - mandoc.git/blob - read.c
1 /* $Id: read.c,v 1.219 2020/04/24 12:02:33 schwarze Exp $ */
3 * Copyright (c) 2010-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
19 * Top-level functions of the mandoc(3) parser:
20 * Parser and input encoding selection, decompression,
21 * handling of input bytes, characters, lines, and files,
22 * handling of roff(7) loops and file inclusion,
23 * and steering of the various parsers.
27 #include <sys/types.h>
42 #include "mandoc_aux.h"
47 #include "mandoc_parse.h"
48 #include "libmandoc.h"
52 #define REPARSE_LIMIT 1000
55 struct roff
*roff
; /* roff parser (!NULL) */
56 struct roff_man
*man
; /* man parser */
57 struct buf
*primary
; /* buffer currently being parsed */
58 struct buf
*secondary
; /* copy of top level input */
59 struct buf
*loop
; /* open .while request line */
60 const char *os_s
; /* default operating system */
61 int options
; /* parser options */
62 int gzip
; /* current input file is gzipped */
63 int filenc
; /* encoding of the current file */
64 int reparse_count
; /* finite interp. stack */
65 int line
; /* line number in the file */
68 static void choose_parser(struct mparse
*);
69 static void free_buf_list(struct buf
*);
70 static void resize_buf(struct buf
*, size_t);
71 static int mparse_buf_r(struct mparse
*, struct buf
, size_t, int);
72 static int read_whole_file(struct mparse
*, int, struct buf
*, int *);
73 static void mparse_end(struct mparse
*);
77 resize_buf(struct buf
*buf
, size_t initial
)
80 buf
->sz
= buf
->sz
> initial
/2 ? 2 * buf
->sz
: initial
;
81 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
85 free_buf_list(struct buf
*buf
)
98 choose_parser(struct mparse
*curp
)
104 * If neither command line arguments -mdoc or -man select
105 * a parser nor the roff parser found a .Dd or .TH macro
106 * yet, look ahead in the main input buffer.
109 if ((format
= roff_getformat(curp
->roff
)) == 0) {
110 cp
= curp
->primary
->buf
;
111 ep
= cp
+ curp
->primary
->sz
;
113 if (*cp
== '.' || *cp
== '\'') {
115 if (cp
[0] == 'D' && cp
[1] == 'd') {
116 format
= MPARSE_MDOC
;
119 if (cp
[0] == 'T' && cp
[1] == 'H') {
124 cp
= memchr(cp
, '\n', ep
- cp
);
131 if (format
== MPARSE_MDOC
) {
132 curp
->man
->meta
.macroset
= MACROSET_MDOC
;
133 if (curp
->man
->mdocmac
== NULL
)
134 curp
->man
->mdocmac
= roffhash_alloc(MDOC_Dd
, MDOC_MAX
);
136 curp
->man
->meta
.macroset
= MACROSET_MAN
;
137 if (curp
->man
->manmac
== NULL
)
138 curp
->man
->manmac
= roffhash_alloc(MAN_TH
, MAN_MAX
);
140 curp
->man
->meta
.first
->tok
= TOKEN_NONE
;
144 * Main parse routine for a buffer.
145 * It assumes encoding and line numbering are already set up.
146 * It can recurse directly (for invocations of user-defined
147 * macros, inline equations, and input line traps)
148 * and indirectly (for .so file inclusion).
151 mparse_buf_r(struct mparse
*curp
, struct buf blk
, size_t i
, int start
)
154 struct buf
*firstln
, *lastln
, *thisln
, *loop
;
156 size_t pos
; /* byte number in the ln buffer */
157 int line_result
, result
;
159 int lnn
; /* line number in the real file */
161 int inloop
; /* Saw .while on this level. */
165 ln
.buf
= mandoc_malloc(ln
.sz
);
167 firstln
= lastln
= loop
= NULL
;
173 while (i
< blk
.sz
&& (blk
.buf
[i
] != '\0' || pos
!= 0)) {
176 curp
->reparse_count
= 0;
179 curp
->filenc
& MPARSE_UTF8
&&
180 curp
->filenc
& MPARSE_LATIN1
)
181 curp
->filenc
= preconv_cue(&blk
, i
);
184 while (i
< blk
.sz
&& (start
|| blk
.buf
[i
] != '\0')) {
187 * When finding an unescaped newline character,
188 * leave the character loop to process the line.
189 * Skip a preceding carriage return, if any.
192 if ('\r' == blk
.buf
[i
] && i
+ 1 < blk
.sz
&&
193 '\n' == blk
.buf
[i
+ 1])
195 if ('\n' == blk
.buf
[i
]) {
202 * Make sure we have space for the worst
203 * case of 12 bytes: "\\[u10ffff]\n\0"
206 if (pos
+ 12 > ln
.sz
)
207 resize_buf(&ln
, 256);
210 * Encode 8-bit input.
215 if ( ! (curp
->filenc
&& preconv_encode(
216 &blk
, &i
, &ln
, &pos
, &curp
->filenc
))) {
217 mandoc_msg(MANDOCERR_CHAR_BAD
,
218 curp
->line
, pos
, "0x%x", c
);
226 * Exclude control characters.
229 if (c
== 0x7f || (c
< 0x20 && c
!= 0x09)) {
230 mandoc_msg(c
== 0x00 || c
== 0x04 ||
231 c
> 0x0a ? MANDOCERR_CHAR_BAD
:
232 MANDOCERR_CHAR_UNSUPP
,
233 curp
->line
, pos
, "0x%x", c
);
240 ln
.buf
[pos
++] = blk
.buf
[i
++];
245 * Maintain a lookaside buffer of all lines.
246 * parsed from this input source.
249 thisln
= mandoc_malloc(sizeof(*thisln
));
250 thisln
->buf
= mandoc_strdup(ln
.buf
);
251 thisln
->sz
= strlen(ln
.buf
) + 1;
253 if (firstln
== NULL
) {
254 firstln
= lastln
= thisln
;
255 if (curp
->secondary
== NULL
)
256 curp
->secondary
= firstln
;
258 lastln
->next
= thisln
;
262 /* XXX Ugly hack to mark the end of the input. */
264 if (i
== blk
.sz
|| blk
.buf
[i
] == '\0') {
266 resize_buf(&ln
, 256);
267 ln
.buf
[pos
++] = '\n';
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.
282 line_result
= roff_parseln(curp
->roff
, curp
->line
, &ln
, &of
);
284 /* Process options. */
286 if (line_result
& ROFF_APPEND
)
287 assert(line_result
== (ROFF_IGN
| ROFF_APPEND
));
289 if (line_result
& ROFF_USERCALL
)
290 assert((line_result
& ROFF_MASK
) == ROFF_REPARSE
);
292 if (line_result
& ROFF_USERRET
) {
293 assert(line_result
== (ROFF_IGN
| ROFF_USERRET
));
295 /* Return from the current macro. */
296 result
= ROFF_USERRET
;
301 switch (line_result
& ROFF_LOOPMASK
) {
305 if (curp
->loop
!= NULL
) {
306 if (loop
== curp
->loop
)
308 mandoc_msg(MANDOCERR_WHILE_NEST
,
309 curp
->line
, pos
, NULL
);
317 if (curp
->loop
== NULL
) {
318 mandoc_msg(MANDOCERR_WHILE_FAIL
,
319 curp
->line
, pos
, NULL
);
323 mandoc_msg(MANDOCERR_WHILE_INTO
,
324 curp
->line
, pos
, NULL
);
325 curp
->loop
= loop
= NULL
;
328 if (line_result
& ROFF_LOOPCONT
)
331 curp
->loop
= loop
= NULL
;
339 /* Process the main instruction from the roff parser. */
341 switch (line_result
& ROFF_MASK
) {
345 if (curp
->man
->meta
.macroset
== MACROSET_NONE
)
347 if ((curp
->man
->meta
.macroset
== MACROSET_MDOC
?
348 mdoc_parseln(curp
->man
, curp
->line
, ln
.buf
, of
) :
349 man_parseln(curp
->man
, curp
->line
, ln
.buf
, of
)
356 if (++curp
->reparse_count
> REPARSE_LIMIT
) {
357 /* Abort and return to the top level. */
359 mandoc_msg(MANDOCERR_ROFFLOOP
,
360 curp
->line
, pos
, NULL
);
363 result
= mparse_buf_r(curp
, ln
, of
, 0);
364 if (line_result
& ROFF_USERCALL
) {
365 roff_userret(curp
->roff
);
366 /* Continue normally. */
367 if (result
& ROFF_USERRET
)
370 if (start
== 0 && result
!= ROFF_CONT
)
374 if ( ! (curp
->options
& MPARSE_SO
) &&
375 (i
>= blk
.sz
|| blk
.buf
[i
] == '\0')) {
376 curp
->man
->meta
.sodest
=
377 mandoc_strdup(ln
.buf
+ of
);
380 if ((fd
= mparse_open(curp
, ln
.buf
+ of
)) != -1) {
381 mparse_readfd(curp
, fd
, ln
.buf
+ of
);
384 mandoc_msg(MANDOCERR_SO_FAIL
,
385 curp
->line
, of
, ".so %s: %s",
386 ln
.buf
+ of
, strerror(errno
));
387 ln
.sz
= mandoc_asprintf(&cp
,
388 ".sp\nSee the file %s.\n.sp",
393 mparse_buf_r(curp
, ln
, of
, 0);
400 /* Start the next input line. */
403 (line_result
& ROFF_LOOPMASK
) == ROFF_IGN
)
407 if ((line_result
& ROFF_APPEND
) == 0)
409 if (ln
.sz
< loop
->sz
)
410 resize_buf(&ln
, loop
->sz
);
411 (void)strlcat(ln
.buf
, loop
->buf
, ln
.sz
);
416 pos
= (line_result
& ROFF_APPEND
) ? strlen(ln
.buf
) : 0;
420 if (result
!= ROFF_USERRET
)
421 mandoc_msg(MANDOCERR_WHILE_OUTOF
,
422 curp
->line
, pos
, NULL
);
426 if (firstln
!= curp
->secondary
)
427 free_buf_list(firstln
);
432 read_whole_file(struct mparse
*curp
, int fd
, struct buf
*fb
, int *with_mmap
)
438 int gzerrnum
, retval
;
440 if (fstat(fd
, &st
) == -1) {
441 mandoc_msg(MANDOCERR_FSTAT
, 0, 0, "%s", strerror(errno
));
446 * If we're a regular file, try just reading in the whole entry
447 * via mmap(). This is faster than reading it into blocks, and
448 * since each file is only a few bytes to begin with, I'm not
449 * concerned that this is going to tank any machines.
452 if (curp
->gzip
== 0 && S_ISREG(st
.st_mode
)) {
453 if (st
.st_size
> 0x7fffffff) {
454 mandoc_msg(MANDOCERR_TOOLARGE
, 0, 0, NULL
);
458 fb
->sz
= (size_t)st
.st_size
;
459 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
, MAP_SHARED
, fd
, 0);
460 if (fb
->buf
!= MAP_FAILED
)
466 * Duplicating the file descriptor is required
467 * because we will have to call gzclose(3)
468 * to free memory used internally by zlib,
469 * but that will also close the file descriptor,
470 * which this function must not do.
472 if ((fd
= dup(fd
)) == -1) {
473 mandoc_msg(MANDOCERR_DUP
, 0, 0,
474 "%s", strerror(errno
));
477 if ((gz
= gzdopen(fd
, "rb")) == NULL
) {
478 mandoc_msg(MANDOCERR_GZDOPEN
, 0, 0,
479 "%s", strerror(errno
));
487 * If this isn't a regular file (like, say, stdin), then we must
488 * go the old way and just read things in bit by bit.
498 if (fb
->sz
== (1U << 31)) {
499 mandoc_msg(MANDOCERR_TOOLARGE
, 0, 0, NULL
);
502 resize_buf(fb
, 65536);
505 gzread(gz
, fb
->buf
+ (int)off
, fb
->sz
- off
) :
506 read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
514 (void)gzerror(gz
, &gzerrnum
);
515 mandoc_msg(MANDOCERR_READ
, 0, 0, "%s",
516 curp
->gzip
&& gzerrnum
!= Z_ERRNO
?
517 zError(gzerrnum
) : strerror(errno
));
523 if (curp
->gzip
&& (gzerrnum
= gzclose(gz
)) != Z_OK
)
524 mandoc_msg(MANDOCERR_GZCLOSE
, 0, 0, "%s",
525 gzerrnum
== Z_ERRNO
? strerror(errno
) :
535 mparse_end(struct mparse
*curp
)
537 if (curp
->man
->meta
.macroset
== MACROSET_NONE
)
538 curp
->man
->meta
.macroset
= MACROSET_MAN
;
539 if (curp
->man
->meta
.macroset
== MACROSET_MDOC
)
540 mdoc_endparse(curp
->man
);
542 man_endparse(curp
->man
);
543 roff_endparse(curp
->roff
);
547 * Read the whole file into memory and call the parsers.
548 * Called recursively when an .so request is encountered.
551 mparse_readfd(struct mparse
*curp
, int fd
, const char *filename
)
553 static int recursion_depth
;
556 struct buf
*save_primary
;
557 const char *save_filename
, *cp
;
559 int save_filenc
, save_lineno
;
562 if (recursion_depth
> 64) {
563 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
->line
, 0, NULL
);
565 } else if (recursion_depth
== 0 &&
566 (cp
= strrchr(filename
, '.')) != NULL
&&
567 cp
[1] >= '1' && cp
[1] <= '9')
568 curp
->man
->filesec
= cp
[1];
570 curp
->man
->filesec
= '\0';
572 if (read_whole_file(curp
, fd
, &blk
, &with_mmap
) == -1)
576 * Save some properties of the parent file.
579 save_primary
= curp
->primary
;
580 save_filenc
= curp
->filenc
;
581 save_lineno
= curp
->line
;
582 save_filename
= mandoc_msg_getinfilename();
584 curp
->primary
= &blk
;
585 curp
->filenc
= curp
->options
& (MPARSE_UTF8
| MPARSE_LATIN1
);
587 mandoc_msg_setinfilename(filename
);
589 /* Skip an UTF-8 byte order mark. */
590 if (curp
->filenc
& MPARSE_UTF8
&& blk
.sz
> 2 &&
591 (unsigned char)blk
.buf
[0] == 0xef &&
592 (unsigned char)blk
.buf
[1] == 0xbb &&
593 (unsigned char)blk
.buf
[2] == 0xbf) {
595 curp
->filenc
&= ~MPARSE_LATIN1
;
600 mparse_buf_r(curp
, blk
, offset
, 1);
601 if (--recursion_depth
== 0)
605 * Clean up and restore saved parent properties.
609 munmap(blk
.buf
, blk
.sz
);
613 curp
->primary
= save_primary
;
614 curp
->filenc
= save_filenc
;
615 curp
->line
= save_lineno
;
616 if (save_filename
!= NULL
)
617 mandoc_msg_setinfilename(save_filename
);
621 mparse_open(struct mparse
*curp
, const char *file
)
626 cp
= strrchr(file
, '.');
627 curp
->gzip
= (cp
!= NULL
&& ! strcmp(cp
+ 1, "gz"));
629 /* First try to use the filename as it is. */
631 if ((fd
= open(file
, O_RDONLY
)) != -1)
635 * If that doesn't work and the filename doesn't
636 * already end in .gz, try appending .gz.
641 mandoc_asprintf(&cp
, "%s.gz", file
);
642 fd
= open(cp
, O_RDONLY
);
651 /* Neither worked, give up. */
657 mparse_alloc(int options
, enum mandoc_os os_e
, const char *os_s
)
661 curp
= mandoc_calloc(1, sizeof(struct mparse
));
663 curp
->options
= options
;
666 curp
->roff
= roff_alloc(options
);
667 curp
->man
= roff_man_alloc(curp
->roff
, curp
->os_s
,
668 curp
->options
& MPARSE_QUICK
? 1 : 0);
669 if (curp
->options
& MPARSE_MDOC
) {
670 curp
->man
->meta
.macroset
= MACROSET_MDOC
;
671 if (curp
->man
->mdocmac
== NULL
)
672 curp
->man
->mdocmac
= roffhash_alloc(MDOC_Dd
, MDOC_MAX
);
673 } else if (curp
->options
& MPARSE_MAN
) {
674 curp
->man
->meta
.macroset
= MACROSET_MAN
;
675 if (curp
->man
->manmac
== NULL
)
676 curp
->man
->manmac
= roffhash_alloc(MAN_TH
, MAN_MAX
);
678 curp
->man
->meta
.first
->tok
= TOKEN_NONE
;
679 curp
->man
->meta
.os_e
= os_e
;
685 mparse_reset(struct mparse
*curp
)
688 roff_reset(curp
->roff
);
689 roff_man_reset(curp
->man
);
690 free_buf_list(curp
->secondary
);
691 curp
->secondary
= NULL
;
697 mparse_free(struct mparse
*curp
)
700 roffhash_free(curp
->man
->mdocmac
);
701 roffhash_free(curp
->man
->manmac
);
702 roff_man_free(curp
->man
);
703 roff_free(curp
->roff
);
704 free_buf_list(curp
->secondary
);
709 mparse_result(struct mparse
*curp
)
711 roff_state_reset(curp
->man
);
712 if (curp
->options
& MPARSE_VALIDATE
) {
713 if (curp
->man
->meta
.macroset
== MACROSET_MDOC
)
714 mdoc_validate(curp
->man
);
716 man_validate(curp
->man
);
717 tag_postprocess(curp
->man
, curp
->man
->meta
.first
);
719 return &curp
->man
->meta
;
723 mparse_copy(const struct mparse
*p
)
727 for (buf
= p
->secondary
; buf
!= NULL
; buf
= buf
->next
)