]>
git.cameronkatri.com Git - mandoc.git/blob - read.c
1 /* $Id: read.c,v 1.208 2018/12/20 21:30:32 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2018 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 char *sodest
; /* filename pointed to by .so */
51 struct buf
*primary
; /* buffer currently being parsed */
52 struct buf
*secondary
; /* copy of top level input */
53 struct buf
*loop
; /* open .while request line */
54 const char *os_s
; /* default operating system */
55 int options
; /* parser options */
56 int gzip
; /* current input file is gzipped */
57 int filenc
; /* encoding of the current file */
58 int reparse_count
; /* finite interp. stack */
59 int line
; /* line number in the file */
62 static void choose_parser(struct mparse
*);
63 static void free_buf_list(struct buf
*);
64 static void resize_buf(struct buf
*, size_t);
65 static int mparse_buf_r(struct mparse
*, struct buf
, size_t, int);
66 static int read_whole_file(struct mparse
*, int, struct buf
*, int *);
67 static void mparse_end(struct mparse
*);
71 resize_buf(struct buf
*buf
, size_t initial
)
74 buf
->sz
= buf
->sz
> initial
/2 ? 2 * buf
->sz
: initial
;
75 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
79 free_buf_list(struct buf
*buf
)
92 choose_parser(struct mparse
*curp
)
98 * If neither command line arguments -mdoc or -man select
99 * a parser nor the roff parser found a .Dd or .TH macro
100 * yet, look ahead in the main input buffer.
103 if ((format
= roff_getformat(curp
->roff
)) == 0) {
104 cp
= curp
->primary
->buf
;
105 ep
= cp
+ curp
->primary
->sz
;
107 if (*cp
== '.' || *cp
== '\'') {
109 if (cp
[0] == 'D' && cp
[1] == 'd') {
110 format
= MPARSE_MDOC
;
113 if (cp
[0] == 'T' && cp
[1] == 'H') {
118 cp
= memchr(cp
, '\n', ep
- cp
);
125 if (format
== MPARSE_MDOC
) {
126 curp
->man
->macroset
= MACROSET_MDOC
;
127 if (curp
->man
->mdocmac
== NULL
)
128 curp
->man
->mdocmac
= roffhash_alloc(MDOC_Dd
, MDOC_MAX
);
130 curp
->man
->macroset
= MACROSET_MAN
;
131 if (curp
->man
->manmac
== NULL
)
132 curp
->man
->manmac
= roffhash_alloc(MAN_TH
, MAN_MAX
);
134 curp
->man
->first
->tok
= TOKEN_NONE
;
138 * Main parse routine for a buffer.
139 * It assumes encoding and line numbering are already set up.
140 * It can recurse directly (for invocations of user-defined
141 * macros, inline equations, and input line traps)
142 * and indirectly (for .so file inclusion).
145 mparse_buf_r(struct mparse
*curp
, struct buf blk
, size_t i
, int start
)
148 struct buf
*firstln
, *lastln
, *thisln
, *loop
;
150 size_t pos
; /* byte number in the ln buffer */
151 int line_result
, result
;
153 int lnn
; /* line number in the real file */
155 int inloop
; /* Saw .while on this level. */
159 ln
.buf
= mandoc_malloc(ln
.sz
);
161 firstln
= loop
= NULL
;
167 while (i
< blk
.sz
&& (blk
.buf
[i
] != '\0' || pos
!= 0)) {
170 curp
->reparse_count
= 0;
173 curp
->filenc
& MPARSE_UTF8
&&
174 curp
->filenc
& MPARSE_LATIN1
)
175 curp
->filenc
= preconv_cue(&blk
, i
);
178 while (i
< blk
.sz
&& (start
|| blk
.buf
[i
] != '\0')) {
181 * When finding an unescaped newline character,
182 * leave the character loop to process the line.
183 * Skip a preceding carriage return, if any.
186 if ('\r' == blk
.buf
[i
] && i
+ 1 < blk
.sz
&&
187 '\n' == blk
.buf
[i
+ 1])
189 if ('\n' == blk
.buf
[i
]) {
196 * Make sure we have space for the worst
197 * case of 12 bytes: "\\[u10ffff]\n\0"
200 if (pos
+ 12 > ln
.sz
)
201 resize_buf(&ln
, 256);
204 * Encode 8-bit input.
209 if ( ! (curp
->filenc
&& preconv_encode(
210 &blk
, &i
, &ln
, &pos
, &curp
->filenc
))) {
211 mandoc_msg(MANDOCERR_CHAR_BAD
,
212 curp
->line
, pos
, "0x%x", c
);
220 * Exclude control characters.
223 if (c
== 0x7f || (c
< 0x20 && c
!= 0x09)) {
224 mandoc_msg(c
== 0x00 || c
== 0x04 ||
225 c
> 0x0a ? MANDOCERR_CHAR_BAD
:
226 MANDOCERR_CHAR_UNSUPP
,
227 curp
->line
, pos
, "0x%x", c
);
234 ln
.buf
[pos
++] = blk
.buf
[i
++];
239 * Maintain a lookaside buffer of all lines.
240 * parsed from this input source.
243 thisln
= mandoc_malloc(sizeof(*thisln
));
244 thisln
->buf
= mandoc_strdup(ln
.buf
);
245 thisln
->sz
= strlen(ln
.buf
) + 1;
247 if (firstln
== NULL
) {
248 firstln
= lastln
= thisln
;
249 if (curp
->secondary
== NULL
)
250 curp
->secondary
= firstln
;
252 lastln
->next
= thisln
;
256 /* XXX Ugly hack to mark the end of the input. */
258 if (i
== blk
.sz
|| blk
.buf
[i
] == '\0') {
259 ln
.buf
[pos
++] = '\n';
264 * A significant amount of complexity is contained by
265 * the roff preprocessor. It's line-oriented but can be
266 * expressed on one line, so we need at times to
267 * readjust our starting point and re-run it. The roff
268 * preprocessor can also readjust the buffers with new
269 * data, so we pass them in wholesale.
274 line_result
= roff_parseln(curp
->roff
, curp
->line
, &ln
, &of
);
276 /* Process options. */
278 if (line_result
& ROFF_APPEND
)
279 assert(line_result
== (ROFF_IGN
| ROFF_APPEND
));
281 if (line_result
& ROFF_USERCALL
)
282 assert((line_result
& ROFF_MASK
) == ROFF_REPARSE
);
284 if (line_result
& ROFF_USERRET
) {
285 assert(line_result
== (ROFF_IGN
| ROFF_USERRET
));
287 /* Return from the current macro. */
288 result
= ROFF_USERRET
;
293 switch (line_result
& ROFF_LOOPMASK
) {
297 if (curp
->loop
!= NULL
) {
298 if (loop
== curp
->loop
)
300 mandoc_msg(MANDOCERR_WHILE_NEST
,
301 curp
->line
, pos
, NULL
);
309 if (curp
->loop
== NULL
) {
310 mandoc_msg(MANDOCERR_WHILE_FAIL
,
311 curp
->line
, pos
, NULL
);
315 mandoc_msg(MANDOCERR_WHILE_INTO
,
316 curp
->line
, pos
, NULL
);
317 curp
->loop
= loop
= NULL
;
320 if (line_result
& ROFF_LOOPCONT
)
323 curp
->loop
= loop
= NULL
;
331 /* Process the main instruction from the roff parser. */
333 switch (line_result
& ROFF_MASK
) {
337 if (curp
->man
->macroset
== MACROSET_NONE
)
339 if ((curp
->man
->macroset
== MACROSET_MDOC
?
340 mdoc_parseln(curp
->man
, curp
->line
, ln
.buf
, of
) :
341 man_parseln(curp
->man
, curp
->line
, ln
.buf
, of
)
348 if (++curp
->reparse_count
> REPARSE_LIMIT
) {
349 /* Abort and return to the top level. */
351 mandoc_msg(MANDOCERR_ROFFLOOP
,
352 curp
->line
, pos
, NULL
);
355 result
= mparse_buf_r(curp
, ln
, of
, 0);
356 if (line_result
& ROFF_USERCALL
) {
357 roff_userret(curp
->roff
);
358 /* Continue normally. */
359 if (result
& ROFF_USERRET
)
362 if (start
== 0 && result
!= ROFF_CONT
)
366 if ( ! (curp
->options
& MPARSE_SO
) &&
367 (i
>= blk
.sz
|| blk
.buf
[i
] == '\0')) {
368 curp
->sodest
= mandoc_strdup(ln
.buf
+ of
);
371 if ((fd
= mparse_open(curp
, ln
.buf
+ of
)) != -1) {
372 mparse_readfd(curp
, fd
, ln
.buf
+ of
);
375 mandoc_msg(MANDOCERR_SO_FAIL
,
376 curp
->line
, of
, ".so %s: %s",
377 ln
.buf
+ of
, strerror(errno
));
378 ln
.sz
= mandoc_asprintf(&cp
,
379 ".sp\nSee the file %s.\n.sp",
384 mparse_buf_r(curp
, ln
, of
, 0);
391 /* Start the next input line. */
394 (line_result
& ROFF_LOOPMASK
) == ROFF_IGN
)
398 if ((line_result
& ROFF_APPEND
) == 0)
400 if (ln
.sz
< loop
->sz
)
401 resize_buf(&ln
, loop
->sz
);
402 (void)strlcat(ln
.buf
, loop
->buf
, ln
.sz
);
407 pos
= (line_result
& ROFF_APPEND
) ? strlen(ln
.buf
) : 0;
411 if (result
!= ROFF_USERRET
)
412 mandoc_msg(MANDOCERR_WHILE_OUTOF
,
413 curp
->line
, pos
, NULL
);
417 if (firstln
!= curp
->secondary
)
418 free_buf_list(firstln
);
423 read_whole_file(struct mparse
*curp
, int fd
, struct buf
*fb
, int *with_mmap
)
429 int gzerrnum
, retval
;
431 if (fstat(fd
, &st
) == -1) {
432 mandoc_msg(MANDOCERR_FILE
, 0, 0,
433 "fstat: %s", strerror(errno
));
438 * If we're a regular file, try just reading in the whole entry
439 * via mmap(). This is faster than reading it into blocks, and
440 * since each file is only a few bytes to begin with, I'm not
441 * concerned that this is going to tank any machines.
444 if (curp
->gzip
== 0 && S_ISREG(st
.st_mode
)) {
445 if (st
.st_size
> 0x7fffffff) {
446 mandoc_msg(MANDOCERR_TOOLARGE
, 0, 0, NULL
);
450 fb
->sz
= (size_t)st
.st_size
;
451 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
, MAP_SHARED
, fd
, 0);
452 if (fb
->buf
!= MAP_FAILED
)
458 * Duplicating the file descriptor is required
459 * because we will have to call gzclose(3)
460 * to free memory used internally by zlib,
461 * but that will also close the file descriptor,
462 * which this function must not do.
464 if ((fd
= dup(fd
)) == -1) {
465 mandoc_msg(MANDOCERR_FILE
, 0, 0,
466 "dup: %s", strerror(errno
));
469 if ((gz
= gzdopen(fd
, "rb")) == NULL
) {
470 mandoc_msg(MANDOCERR_FILE
, 0, 0,
471 "gzdopen: %s", strerror(errno
));
479 * If this isn't a regular file (like, say, stdin), then we must
480 * go the old way and just read things in bit by bit.
490 if (fb
->sz
== (1U << 31)) {
491 mandoc_msg(MANDOCERR_TOOLARGE
, 0, 0, NULL
);
494 resize_buf(fb
, 65536);
497 gzread(gz
, fb
->buf
+ (int)off
, fb
->sz
- off
) :
498 read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
506 (void)gzerror(gz
, &gzerrnum
);
507 mandoc_msg(MANDOCERR_FILE
, 0, 0, "read: %s",
508 curp
->gzip
&& gzerrnum
!= Z_ERRNO
?
509 zError(gzerrnum
) : strerror(errno
));
515 if (curp
->gzip
&& (gzerrnum
= gzclose(gz
)) != Z_OK
)
516 mandoc_msg(MANDOCERR_FILE
, 0, 0, "gzclose: %s",
517 gzerrnum
== Z_ERRNO
? strerror(errno
) :
527 mparse_end(struct mparse
*curp
)
529 if (curp
->man
->macroset
== MACROSET_NONE
)
530 curp
->man
->macroset
= MACROSET_MAN
;
531 if (curp
->man
->macroset
== MACROSET_MDOC
)
532 mdoc_endparse(curp
->man
);
534 man_endparse(curp
->man
);
535 roff_endparse(curp
->roff
);
539 * Read the whole file into memory and call the parsers.
540 * Called recursively when an .so request is encountered.
543 mparse_readfd(struct mparse
*curp
, int fd
, const char *filename
)
545 static int recursion_depth
;
548 struct buf
*save_primary
;
549 const char *save_filename
;
551 int save_filenc
, save_lineno
;
554 if (recursion_depth
> 64) {
555 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
->line
, 0, NULL
);
558 if (read_whole_file(curp
, fd
, &blk
, &with_mmap
) == 0)
562 * Save some properties of the parent file.
565 save_primary
= curp
->primary
;
566 save_filenc
= curp
->filenc
;
567 save_lineno
= curp
->line
;
568 save_filename
= mandoc_msg_getinfilename();
570 curp
->primary
= &blk
;
571 curp
->filenc
= curp
->options
& (MPARSE_UTF8
| MPARSE_LATIN1
);
573 mandoc_msg_setinfilename(filename
);
575 /* Skip an UTF-8 byte order mark. */
576 if (curp
->filenc
& MPARSE_UTF8
&& blk
.sz
> 2 &&
577 (unsigned char)blk
.buf
[0] == 0xef &&
578 (unsigned char)blk
.buf
[1] == 0xbb &&
579 (unsigned char)blk
.buf
[2] == 0xbf) {
581 curp
->filenc
&= ~MPARSE_LATIN1
;
586 mparse_buf_r(curp
, blk
, offset
, 1);
587 if (--recursion_depth
== 0)
591 * Clean up and restore saved parent properties.
595 munmap(blk
.buf
, blk
.sz
);
599 curp
->primary
= save_primary
;
600 curp
->filenc
= save_filenc
;
601 curp
->line
= save_lineno
;
602 if (save_filename
!= NULL
)
603 mandoc_msg_setinfilename(save_filename
);
607 mparse_open(struct mparse
*curp
, const char *file
)
612 cp
= strrchr(file
, '.');
613 curp
->gzip
= (cp
!= NULL
&& ! strcmp(cp
+ 1, "gz"));
615 /* First try to use the filename as it is. */
617 if ((fd
= open(file
, O_RDONLY
)) != -1)
621 * If that doesn't work and the filename doesn't
622 * already end in .gz, try appending .gz.
626 mandoc_asprintf(&cp
, "%s.gz", file
);
627 fd
= open(cp
, O_RDONLY
);
635 /* Neither worked, give up. */
641 mparse_alloc(int options
, enum mandoc_os os_e
, const char *os_s
)
645 curp
= mandoc_calloc(1, sizeof(struct mparse
));
647 curp
->options
= options
;
650 curp
->roff
= roff_alloc(options
);
651 curp
->man
= roff_man_alloc(curp
->roff
, curp
->os_s
,
652 curp
->options
& MPARSE_QUICK
? 1 : 0);
653 if (curp
->options
& MPARSE_MDOC
) {
654 curp
->man
->macroset
= MACROSET_MDOC
;
655 if (curp
->man
->mdocmac
== NULL
)
656 curp
->man
->mdocmac
= roffhash_alloc(MDOC_Dd
, MDOC_MAX
);
657 } else if (curp
->options
& MPARSE_MAN
) {
658 curp
->man
->macroset
= MACROSET_MAN
;
659 if (curp
->man
->manmac
== NULL
)
660 curp
->man
->manmac
= roffhash_alloc(MAN_TH
, MAN_MAX
);
662 curp
->man
->first
->tok
= TOKEN_NONE
;
663 curp
->man
->meta
.os_e
= os_e
;
668 mparse_reset(struct mparse
*curp
)
670 roff_reset(curp
->roff
);
671 roff_man_reset(curp
->man
);
672 free_buf_list(curp
->secondary
);
673 curp
->secondary
= NULL
;
680 mparse_free(struct mparse
*curp
)
682 roffhash_free(curp
->man
->mdocmac
);
683 roffhash_free(curp
->man
->manmac
);
684 roff_man_free(curp
->man
);
685 roff_free(curp
->roff
);
686 free_buf_list(curp
->secondary
);
692 mparse_result(struct mparse
*curp
, struct roff_man
**man
,
696 if (sodest
&& NULL
!= (*sodest
= curp
->sodest
)) {
705 mparse_copy(const struct mparse
*p
)
709 for (buf
= p
->secondary
; buf
!= NULL
; buf
= buf
->next
)