]>
git.cameronkatri.com Git - mandoc.git/blob - read.c
1 /* $Id: read.c,v 1.81 2014/08/16 19:00:01 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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>
39 #include "mandoc_aux.h"
40 #include "libmandoc.h"
45 #define REPARSE_LIMIT 1000
48 char *buf
; /* binary input buffer */
49 size_t sz
; /* size of binary buffer */
53 enum mandoclevel file_status
; /* status of current parse */
54 enum mandoclevel wlevel
; /* ignore messages below this */
55 int line
; /* line number in the file */
56 int options
; /* parser options */
57 struct man
*pman
; /* persistent man parser */
58 struct mdoc
*pmdoc
; /* persistent mdoc parser */
59 struct man
*man
; /* man parser */
60 struct mdoc
*mdoc
; /* mdoc parser */
61 struct roff
*roff
; /* roff parser (!NULL) */
62 char *sodest
; /* filename pointed to by .so */
63 int reparse_count
; /* finite interp. stack */
64 mandocmsg mmsg
; /* warning/error message handler */
66 struct buf
*secondary
;
67 const char *defos
; /* default operating system */
70 static void resize_buf(struct buf
*, size_t);
71 static void mparse_buf_r(struct mparse
*, struct buf
, int);
72 static void pset(const char *, int, struct mparse
*);
73 static int read_whole_file(struct mparse
*, const char *, int,
75 static void mparse_end(struct mparse
*);
76 static void mparse_parse_buffer(struct mparse
*, struct buf
,
79 static const enum mandocerr mandoclimits
[MANDOCLEVEL_MAX
] = {
89 static const char * const mandocerrs
[MANDOCERR_MAX
] = {
94 /* related to the prologue */
95 "missing manual title, using UNTITLED",
96 "missing manual title, using \"\"",
97 "lower case character in document title",
98 "missing manual section, using \"\"",
99 "unknown manual section",
100 "unknown manual volume or arch",
101 "missing date, using today's date",
102 "cannot parse date, using it verbatim",
103 "missing Os macro, using \"\"",
104 "duplicate prologue macro",
105 "late prologue macro",
106 "skipping late title macro",
107 "prologue macros out of order",
109 /* related to document structure */
110 ".so is fragile, better use ln(1)",
112 "content before first section header",
113 "first section is not \"NAME\"",
114 "bad NAME section contents",
115 "sections out of conventional order",
116 "duplicate section title",
117 "unexpected section",
119 /* related to macros and nesting */
121 "skipping paragraph macro",
122 "moving paragraph macro out of list",
123 "skipping no-space macro",
124 "blocks badly nested",
125 "nested displays are not portable",
126 "moving content out of list",
127 ".Vt block has child macro",
128 "fill mode already enabled, skipping",
129 "fill mode already disabled, skipping",
132 /* related to missing macro arguments */
133 "skipping empty request",
134 "conditional request controls empty scope",
135 "skipping empty macro",
136 "empty argument, using 0n",
137 "argument count wrong",
138 "missing display type, using -ragged",
139 "list type is not the first argument",
140 "missing -width in -tag list, using 8n",
141 "missing utility name, using \"\"",
142 "empty head in list item",
144 "missing font type, using \\fR",
145 "unknown font type, using \\fR",
146 "missing -std argument, adding it",
148 /* related to bad macro arguments */
149 "unterminated quoted argument",
150 "duplicate argument",
151 "skipping duplicate argument",
152 "skipping duplicate display type",
153 "skipping duplicate list type",
154 "skipping -width argument",
155 "unknown AT&T UNIX version",
156 "invalid content in Rs block",
157 "invalid Boolean argument",
158 "unknown font, skipping request",
160 /* related to plain text */
161 "blank line in fill mode, using .sp",
162 "tab in filled text",
163 "whitespace at end of input line",
165 "invalid escape sequence",
166 "undefined string, using \"\"",
170 /* related to equations */
171 "unexpected equation scope closure",
172 "equation scope open on exit",
173 "overlapping equation scopes",
174 "unexpected end of equation",
175 "equation syntax error",
177 /* related to tables */
181 "no table layout cells specified",
182 "no table data cells specified",
183 "ignore data in cell",
184 "data block still open",
185 "ignoring extra data cells",
187 /* related to document structure and macros */
188 "input stack limit exceeded, infinite loop?",
189 "skipping bad character",
190 "skipping unknown macro",
191 "skipping item outside list",
192 "skipping column outside column list",
193 "skipping end of block that is not open",
194 "inserting missing end of block",
195 "appending missing end of block",
197 /* related to request and macro arguments */
198 "escaped character not allowed in a name",
199 "argument count wrong",
200 "missing list type, using -item",
201 "missing manual name, using \"\"",
202 "uname(3) system call failed, using UNKNOWN",
203 "unknown standard specifier",
204 "skipping request without numeric argument",
205 "skipping all arguments",
206 "skipping excess arguments",
208 "generic fatal error",
211 "NOT IMPLEMENTED: Bd -file",
212 "NOT IMPLEMENTED: .so with absolute path or \"..\"",
213 ".so request failed",
221 static const char * const mandoclevels
[MANDOCLEVEL_MAX
] = {
233 resize_buf(struct buf
*buf
, size_t initial
)
236 buf
->sz
= buf
->sz
> initial
/2 ? 2 * buf
->sz
: initial
;
237 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
241 pset(const char *buf
, int pos
, struct mparse
*curp
)
246 * Try to intuit which kind of manual parser should be used. If
247 * passed in by command-line (-man, -mdoc), then use that
248 * explicitly. If passed as -mandoc, then try to guess from the
249 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
250 * default to -man, which is more lenient.
252 * Separate out pmdoc/pman from mdoc/man: the first persists
253 * through all parsers, while the latter is used per-parse.
256 if ('.' == buf
[0] || '\'' == buf
[0]) {
257 for (i
= 1; buf
[i
]; i
++)
258 if (' ' != buf
[i
] && '\t' != buf
[i
])
264 if (MPARSE_MDOC
& curp
->options
) {
265 curp
->mdoc
= curp
->pmdoc
;
267 } else if (MPARSE_MAN
& curp
->options
) {
268 curp
->man
= curp
->pman
;
272 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
273 if (NULL
== curp
->pmdoc
)
274 curp
->pmdoc
= mdoc_alloc(
275 curp
->roff
, curp
, curp
->defos
,
276 MPARSE_QUICK
& curp
->options
? 1 : 0);
278 curp
->mdoc
= curp
->pmdoc
;
282 if (NULL
== curp
->pman
)
283 curp
->pman
= man_alloc(curp
->roff
, curp
,
284 MPARSE_QUICK
& curp
->options
? 1 : 0);
286 curp
->man
= curp
->pman
;
290 * Main parse routine for an opened file. This is called for each
291 * opened file and simply loops around the full input file, possibly
292 * nesting (i.e., with `so').
295 mparse_buf_r(struct mparse
*curp
, struct buf blk
, int start
)
297 const struct tbl_span
*span
;
301 int pos
; /* byte number in the ln buffer */
302 int lnn
; /* line number in the real file */
305 memset(&ln
, 0, sizeof(struct buf
));
310 for (i
= 0; i
< (int)blk
.sz
; ) {
311 if (0 == pos
&& '\0' == blk
.buf
[i
])
316 curp
->reparse_count
= 0;
319 while (i
< (int)blk
.sz
&& (start
|| '\0' != blk
.buf
[i
])) {
322 * When finding an unescaped newline character,
323 * leave the character loop to process the line.
324 * Skip a preceding carriage return, if any.
327 if ('\r' == blk
.buf
[i
] && i
+ 1 < (int)blk
.sz
&&
328 '\n' == blk
.buf
[i
+ 1])
330 if ('\n' == blk
.buf
[i
]) {
337 * Make sure we have space for at least
338 * one backslash and one other character
339 * and the trailing NUL byte.
342 if (pos
+ 2 >= (int)ln
.sz
)
343 resize_buf(&ln
, 256);
346 * Warn about bogus characters. If you're using
347 * non-ASCII encoding, you're screwing your
348 * readers. Since I'd rather this not happen,
349 * I'll be helpful and replace these characters
350 * with "?", so we don't display gibberish.
351 * Note to manual writers: use special characters.
354 c
= (unsigned char) blk
.buf
[i
];
356 if ( ! (isascii(c
) &&
357 (isgraph(c
) || isblank(c
)))) {
358 mandoc_vmsg(MANDOCERR_BADCHAR
, curp
,
359 curp
->line
, pos
, "0x%x", c
);
365 /* Trailing backslash = a plain char. */
367 if ('\\' != blk
.buf
[i
] || i
+ 1 == (int)blk
.sz
) {
368 ln
.buf
[pos
++] = blk
.buf
[i
++];
373 * Found escape and at least one other character.
374 * When it's a newline character, skip it.
375 * When there is a carriage return in between,
376 * skip that one as well.
379 if ('\r' == blk
.buf
[i
+ 1] && i
+ 2 < (int)blk
.sz
&&
380 '\n' == blk
.buf
[i
+ 2])
382 if ('\n' == blk
.buf
[i
+ 1]) {
388 if ('"' == blk
.buf
[i
+ 1] || '#' == blk
.buf
[i
+ 1]) {
390 /* Comment, skip to end of line */
391 for (; i
< (int)blk
.sz
; ++i
) {
392 if ('\n' == blk
.buf
[i
]) {
399 /* Backout trailing whitespaces */
400 for (; pos
> 0; --pos
) {
401 if (ln
.buf
[pos
- 1] != ' ')
403 if (pos
> 2 && ln
.buf
[pos
- 2] == '\\')
409 /* Catch escaped bogus characters. */
411 c
= (unsigned char) blk
.buf
[i
+1];
413 if ( ! (isascii(c
) &&
414 (isgraph(c
) || isblank(c
)))) {
415 mandoc_vmsg(MANDOCERR_BADCHAR
, curp
,
416 curp
->line
, pos
, "0x%x", c
);
422 /* Some other escape sequence, copy & cont. */
424 ln
.buf
[pos
++] = blk
.buf
[i
++];
425 ln
.buf
[pos
++] = blk
.buf
[i
++];
428 if (pos
>= (int)ln
.sz
)
429 resize_buf(&ln
, 256);
434 * A significant amount of complexity is contained by
435 * the roff preprocessor. It's line-oriented but can be
436 * expressed on one line, so we need at times to
437 * readjust our starting point and re-run it. The roff
438 * preprocessor can also readjust the buffers with new
439 * data, so we pass them in wholesale.
445 * Maintain a lookaside buffer of all parsed lines. We
446 * only do this if mparse_keep() has been invoked (the
447 * buffer may be accessed with mparse_getkeep()).
450 if (curp
->secondary
) {
451 curp
->secondary
->buf
= mandoc_realloc(
452 curp
->secondary
->buf
,
453 curp
->secondary
->sz
+ pos
+ 2);
454 memcpy(curp
->secondary
->buf
+
457 curp
->secondary
->sz
+= pos
;
459 [curp
->secondary
->sz
] = '\n';
460 curp
->secondary
->sz
++;
462 [curp
->secondary
->sz
] = '\0';
465 rr
= roff_parseln(curp
->roff
, curp
->line
,
466 &ln
.buf
, &ln
.sz
, of
, &of
);
470 if (REPARSE_LIMIT
>= ++curp
->reparse_count
)
471 mparse_buf_r(curp
, ln
, 0);
473 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
,
474 curp
->line
, pos
, NULL
);
478 pos
= (int)strlen(ln
.buf
);
486 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
489 if (0 == (MPARSE_SO
& curp
->options
) &&
490 (i
>= (int)blk
.sz
|| '\0' == blk
.buf
[i
])) {
491 curp
->sodest
= mandoc_strdup(ln
.buf
+ of
);
496 * We remove `so' clauses from our lookaside
497 * buffer because we're going to descend into
498 * the file recursively.
501 curp
->secondary
->sz
-= pos
+ 1;
502 mparse_readfd(curp
, -1, ln
.buf
+ of
);
503 if (MANDOCLEVEL_FATAL
<= curp
->file_status
) {
504 mandoc_vmsg(MANDOCERR_SO_FAIL
,
505 curp
, curp
->line
, pos
,
506 ".so %s", ln
.buf
+ of
);
516 * If we encounter errors in the recursive parse, make
517 * sure we don't continue parsing.
520 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
524 * If input parsers have not been allocated, do so now.
525 * We keep these instanced between parsers, but set them
526 * locally per parse routine since we can use different
527 * parsers with each one.
530 if ( ! (curp
->man
|| curp
->mdoc
))
531 pset(ln
.buf
+ of
, pos
- of
, curp
);
534 * Lastly, push down into the parsers themselves. One
535 * of these will have already been set in the pset()
537 * If libroff returns ROFF_TBL, then add it to the
538 * currently open parse. Since we only get here if
539 * there does exist data (see tbl_data.c), we're
540 * guaranteed that something's been allocated.
541 * Do the same for ROFF_EQN.
547 while (NULL
!= (span
= roff_span(curp
->roff
))) {
549 man_addspan(curp
->man
, span
) :
550 mdoc_addspan(curp
->mdoc
, span
);
554 else if (ROFF_EQN
== rr
)
556 mdoc_addeqn(curp
->mdoc
,
557 roff_eqn(curp
->roff
)) :
558 man_addeqn(curp
->man
,
559 roff_eqn(curp
->roff
));
560 else if (curp
->man
|| curp
->mdoc
)
562 man_parseln(curp
->man
,
563 curp
->line
, ln
.buf
, of
) :
564 mdoc_parseln(curp
->mdoc
,
565 curp
->line
, ln
.buf
, of
);
568 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
573 /* Temporary buffers typically are not full. */
575 if (0 == start
&& '\0' == blk
.buf
[i
])
578 /* Start the next input line. */
587 read_whole_file(struct mparse
*curp
, const char *file
, int fd
,
588 struct buf
*fb
, int *with_mmap
)
595 if (-1 == fstat(fd
, &st
)) {
596 curp
->file_status
= MANDOCLEVEL_SYSERR
;
598 (*curp
->mmsg
)(MANDOCERR_SYSSTAT
, curp
->file_status
,
599 file
, 0, 0, strerror(errno
));
604 * If we're a regular file, try just reading in the whole entry
605 * via mmap(). This is faster than reading it into blocks, and
606 * since each file is only a few bytes to begin with, I'm not
607 * concerned that this is going to tank any machines.
610 if (S_ISREG(st
.st_mode
)) {
611 if (st
.st_size
>= (1U << 31)) {
612 curp
->file_status
= MANDOCLEVEL_FATAL
;
614 (*curp
->mmsg
)(MANDOCERR_TOOLARGE
,
615 curp
->file_status
, file
, 0, 0, NULL
);
619 fb
->sz
= (size_t)st
.st_size
;
620 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
, MAP_SHARED
, fd
, 0);
621 if (fb
->buf
!= MAP_FAILED
)
627 * If this isn't a regular file (like, say, stdin), then we must
628 * go the old way and just read things in bit by bit.
637 if (fb
->sz
== (1U << 31)) {
638 curp
->file_status
= MANDOCLEVEL_FATAL
;
640 (*curp
->mmsg
)(MANDOCERR_TOOLARGE
,
645 resize_buf(fb
, 65536);
647 ssz
= read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
653 curp
->file_status
= MANDOCLEVEL_SYSERR
;
655 (*curp
->mmsg
)(MANDOCERR_SYSREAD
,
656 curp
->file_status
, file
, 0, 0,
669 mparse_end(struct mparse
*curp
)
672 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
675 if (curp
->mdoc
== NULL
&&
677 curp
->sodest
== NULL
) {
678 if (curp
->options
& MPARSE_MDOC
)
679 curp
->mdoc
= curp
->pmdoc
;
681 if (curp
->pman
== NULL
)
682 curp
->pman
= man_alloc(curp
->roff
, curp
,
683 curp
->options
& MPARSE_QUICK
? 1 : 0);
684 curp
->man
= curp
->pman
;
688 if (curp
->mdoc
&& ! mdoc_endparse(curp
->mdoc
)) {
689 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
693 if (curp
->man
&& ! man_endparse(curp
->man
)) {
694 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
698 roff_endparse(curp
->roff
);
702 mparse_parse_buffer(struct mparse
*curp
, struct buf blk
, const char *file
)
705 static int recursion_depth
;
707 if (64 < recursion_depth
) {
708 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
, curp
->line
, 0, NULL
);
712 /* Line number is per-file. */
718 mparse_buf_r(curp
, blk
, 1);
720 if (0 == --recursion_depth
&& MANDOCLEVEL_FATAL
> curp
->file_status
)
727 mparse_readmem(struct mparse
*curp
, const void *buf
, size_t len
,
732 blk
.buf
= UNCONST(buf
);
735 mparse_parse_buffer(curp
, blk
, file
);
736 return(curp
->file_status
);
740 mparse_readfd(struct mparse
*curp
, int fd
, const char *file
)
745 if (-1 == fd
&& -1 == (fd
= open(file
, O_RDONLY
, 0))) {
746 curp
->file_status
= MANDOCLEVEL_SYSERR
;
748 (*curp
->mmsg
)(MANDOCERR_SYSOPEN
,
750 file
, 0, 0, strerror(errno
));
755 * Run for each opened file; may be called more than once for
756 * each full parse sequence if the opened file is nested (i.e.,
757 * from `so'). Simply sucks in the whole file and moves into
758 * the parse phase for the file.
761 if ( ! read_whole_file(curp
, file
, fd
, &blk
, &with_mmap
))
764 mparse_parse_buffer(curp
, blk
, file
);
768 munmap(blk
.buf
, blk
.sz
);
773 if (STDIN_FILENO
!= fd
&& -1 == close(fd
))
776 return(curp
->file_status
);
780 mparse_alloc(int options
, enum mandoclevel wlevel
,
781 mandocmsg mmsg
, const char *defos
)
785 assert(wlevel
<= MANDOCLEVEL_FATAL
);
787 curp
= mandoc_calloc(1, sizeof(struct mparse
));
789 curp
->options
= options
;
790 curp
->wlevel
= wlevel
;
794 curp
->roff
= roff_alloc(curp
, options
);
795 if (curp
->options
& MPARSE_MDOC
)
796 curp
->pmdoc
= mdoc_alloc(
797 curp
->roff
, curp
, curp
->defos
,
798 curp
->options
& MPARSE_QUICK
? 1 : 0);
799 if (curp
->options
& MPARSE_MAN
)
800 curp
->pman
= man_alloc(curp
->roff
, curp
,
801 curp
->options
& MPARSE_QUICK
? 1 : 0);
807 mparse_reset(struct mparse
*curp
)
810 roff_reset(curp
->roff
);
813 mdoc_reset(curp
->mdoc
);
815 man_reset(curp
->man
);
817 curp
->secondary
->sz
= 0;
819 curp
->file_status
= MANDOCLEVEL_OK
;
828 mparse_free(struct mparse
*curp
)
832 mdoc_free(curp
->pmdoc
);
834 man_free(curp
->pman
);
836 roff_free(curp
->roff
);
838 free(curp
->secondary
->buf
);
840 free(curp
->secondary
);
846 mparse_result(struct mparse
*curp
,
847 struct mdoc
**mdoc
, struct man
**man
, char **sodest
)
850 if (sodest
&& NULL
!= (*sodest
= curp
->sodest
)) {
862 mandoc_vmsg(enum mandocerr t
, struct mparse
*m
,
863 int ln
, int pos
, const char *fmt
, ...)
869 (void)vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
872 mandoc_msg(t
, m
, ln
, pos
, buf
);
876 mandoc_msg(enum mandocerr er
, struct mparse
*m
,
877 int ln
, int col
, const char *msg
)
879 enum mandoclevel level
;
881 level
= MANDOCLEVEL_FATAL
;
882 while (er
< mandoclimits
[level
])
885 if (level
< m
->wlevel
)
889 (*m
->mmsg
)(er
, level
, m
->file
, ln
, col
, msg
);
891 if (m
->file_status
< level
)
892 m
->file_status
= level
;
896 mparse_strerror(enum mandocerr er
)
899 return(mandocerrs
[er
]);
903 mparse_strlevel(enum mandoclevel lvl
)
905 return(mandoclevels
[lvl
]);
909 mparse_keep(struct mparse
*p
)
912 assert(NULL
== p
->secondary
);
913 p
->secondary
= mandoc_calloc(1, sizeof(struct buf
));
917 mparse_getkeep(const struct mparse
*p
)
920 assert(p
->secondary
);
921 return(p
->secondary
->sz
? p
->secondary
->buf
: NULL
);