]>
git.cameronkatri.com Git - mandoc.git/blob - read.c
1 /* $Id: read.c,v 1.40 2014/01/02 16:29:55 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 # include <sys/stat.h>
24 # include <sys/mman.h>
39 #include "libmandoc.h"
44 #define REPARSE_LIMIT 1000
47 char *buf
; /* binary input buffer */
48 size_t sz
; /* size of binary buffer */
52 enum mandoclevel file_status
; /* status of current parse */
53 enum mandoclevel wlevel
; /* ignore messages below this */
54 int line
; /* line number in the file */
55 enum mparset inttype
; /* which parser to use */
56 struct man
*pman
; /* persistent man parser */
57 struct mdoc
*pmdoc
; /* persistent mdoc parser */
58 struct man
*man
; /* man parser */
59 struct mdoc
*mdoc
; /* mdoc parser */
60 struct roff
*roff
; /* roff parser (!NULL) */
61 int reparse_count
; /* finite interp. stack */
62 mandocmsg mmsg
; /* warning/error message handler */
63 void *arg
; /* argument to mmsg */
65 struct buf
*secondary
;
66 char *defos
; /* default operating system */
69 static void resize_buf(struct buf
*, size_t);
70 static void mparse_buf_r(struct mparse
*, struct buf
, int);
71 static void pset(const char *, int, struct mparse
*);
72 static int read_whole_file(struct mparse
*, const char *, int,
74 static void mparse_end(struct mparse
*);
75 static void mparse_parse_buffer(struct mparse
*, struct buf
,
78 static const enum mandocerr mandoclimits
[MANDOCLEVEL_MAX
] = {
88 static const char * const mandocerrs
[MANDOCERR_MAX
] = {
93 /* related to the prologue */
94 "no title in document",
95 "document title should be all caps",
96 "unknown manual section",
97 "unknown manual volume or arch",
98 "date missing, using today's date",
99 "cannot parse date, using it verbatim",
100 "prologue macros out of order",
101 "duplicate prologue macro",
102 "macro not allowed in prologue",
103 "macro not allowed in body",
105 /* related to document structure */
106 ".so is fragile, better use ln(1)",
107 "NAME section must come first",
108 "bad NAME section contents",
109 "sections out of conventional order",
110 "duplicate section name",
111 "section header suited to sections 2, 3, and 9 only",
113 /* related to macros and nesting */
114 "skipping obsolete macro",
115 "skipping paragraph macro",
116 "moving paragraph macro out of list",
117 "skipping no-space macro",
118 "blocks badly nested",
119 "child violates parent syntax",
120 "nested displays are not portable",
121 "already in literal mode",
124 /* related to missing macro arguments */
125 "skipping empty macro",
126 "argument count wrong",
127 "missing display type",
128 "list type must come first",
129 "tag lists require a width argument",
131 "skipping end of block that is not open",
133 /* related to bad macro arguments */
135 "duplicate argument",
136 "duplicate display type",
137 "duplicate list type",
138 "unknown AT&T UNIX version",
141 "unknown standard specifier",
142 "bad width argument",
144 /* related to plain text */
145 "blank line in non-literal context",
146 "tab in non-literal context",
147 "end of line whitespace",
149 "bad escape sequence",
150 "unterminated quoted string",
152 /* related to equations */
153 "unexpected literal in equation",
157 /* related to equations */
158 "unexpected equation scope closure",
159 "equation scope open on exit",
160 "overlapping equation scopes",
161 "unexpected end of equation",
162 "equation syntax error",
164 /* related to tables */
168 "no table layout cells specified",
169 "no table data cells specified",
170 "ignore data in cell",
171 "data block still open",
172 "ignoring extra data cells",
174 "input stack limit exceeded, infinite loop?",
175 "skipping bad character",
176 "escaped character not allowed in a name",
177 "manual name not yet set",
178 "skipping text before the first section header",
179 "skipping unknown macro",
180 "NOT IMPLEMENTED, please use groff: skipping request",
181 "argument count wrong",
182 "skipping column outside column list",
183 "skipping end of block that is not open",
184 "missing end of block",
185 "scope open on exit",
186 "uname(3) system call failed",
187 "macro requires line argument(s)",
188 "macro requires body argument(s)",
189 "macro requires argument(s)",
190 "request requires a numeric argument",
192 "line argument(s) will be lost",
193 "body argument(s) will be lost",
195 "generic fatal error",
199 "column syntax is inconsistent",
200 "NOT IMPLEMENTED: .Bd -file",
201 "argument count wrong, violates syntax",
202 "child violates parent syntax",
203 "argument count wrong, violates syntax",
204 "NOT IMPLEMENTED: .so with absolute path or \"..\"",
206 "no document prologue",
207 "static buffer exhausted",
215 static const char * const mandoclevels
[MANDOCLEVEL_MAX
] = {
226 resize_buf(struct buf
*buf
, size_t initial
)
229 buf
->sz
= buf
->sz
> initial
/2 ? 2 * buf
->sz
: initial
;
230 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
234 pset(const char *buf
, int pos
, struct mparse
*curp
)
239 * Try to intuit which kind of manual parser should be used. If
240 * passed in by command-line (-man, -mdoc), then use that
241 * explicitly. If passed as -mandoc, then try to guess from the
242 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
243 * default to -man, which is more lenient.
245 * Separate out pmdoc/pman from mdoc/man: the first persists
246 * through all parsers, while the latter is used per-parse.
249 if ('.' == buf
[0] || '\'' == buf
[0]) {
250 for (i
= 1; buf
[i
]; i
++)
251 if (' ' != buf
[i
] && '\t' != buf
[i
])
257 switch (curp
->inttype
) {
259 if (NULL
== curp
->pmdoc
)
260 curp
->pmdoc
= mdoc_alloc(curp
->roff
, curp
,
263 curp
->mdoc
= curp
->pmdoc
;
266 if (NULL
== curp
->pman
)
267 curp
->pman
= man_alloc(curp
->roff
, curp
);
269 curp
->man
= curp
->pman
;
275 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
276 if (NULL
== curp
->pmdoc
)
277 curp
->pmdoc
= mdoc_alloc(curp
->roff
, curp
,
280 curp
->mdoc
= curp
->pmdoc
;
284 if (NULL
== curp
->pman
)
285 curp
->pman
= man_alloc(curp
->roff
, curp
);
287 curp
->man
= curp
->pman
;
291 * Main parse routine for an opened file. This is called for each
292 * opened file and simply loops around the full input file, possibly
293 * nesting (i.e., with `so').
296 mparse_buf_r(struct mparse
*curp
, struct buf blk
, int start
)
298 const struct tbl_span
*span
;
302 int pos
; /* byte number in the ln buffer */
303 int lnn
; /* line number in the real file */
306 memset(&ln
, 0, sizeof(struct buf
));
311 for (i
= 0; i
< (int)blk
.sz
; ) {
312 if (0 == pos
&& '\0' == blk
.buf
[i
])
317 curp
->reparse_count
= 0;
320 while (i
< (int)blk
.sz
&& (start
|| '\0' != blk
.buf
[i
])) {
323 * When finding an unescaped newline character,
324 * leave the character loop to process the line.
325 * Skip a preceding carriage return, if any.
328 if ('\r' == blk
.buf
[i
] && i
+ 1 < (int)blk
.sz
&&
329 '\n' == blk
.buf
[i
+ 1])
331 if ('\n' == blk
.buf
[i
]) {
338 * Make sure we have space for at least
339 * one backslash and one other character
340 * and the trailing NUL byte.
343 if (pos
+ 2 >= (int)ln
.sz
)
344 resize_buf(&ln
, 256);
347 * Warn about bogus characters. If you're using
348 * non-ASCII encoding, you're screwing your
349 * readers. Since I'd rather this not happen,
350 * I'll be helpful and replace these characters
351 * with "?", so we don't display gibberish.
352 * Note to manual writers: use special characters.
355 c
= (unsigned char) blk
.buf
[i
];
357 if ( ! (isascii(c
) &&
358 (isgraph(c
) || isblank(c
)))) {
359 mandoc_msg(MANDOCERR_BADCHAR
, curp
,
360 curp
->line
, pos
, NULL
);
366 /* Trailing backslash = a plain char. */
368 if ('\\' != blk
.buf
[i
] || i
+ 1 == (int)blk
.sz
) {
369 ln
.buf
[pos
++] = blk
.buf
[i
++];
374 * Found escape and at least one other character.
375 * When it's a newline character, skip it.
376 * When there is a carriage return in between,
377 * skip that one as well.
380 if ('\r' == blk
.buf
[i
+ 1] && i
+ 2 < (int)blk
.sz
&&
381 '\n' == blk
.buf
[i
+ 2])
383 if ('\n' == blk
.buf
[i
+ 1]) {
389 if ('"' == blk
.buf
[i
+ 1] || '#' == blk
.buf
[i
+ 1]) {
391 /* Comment, skip to end of line */
392 for (; i
< (int)blk
.sz
; ++i
) {
393 if ('\n' == blk
.buf
[i
]) {
400 /* Backout trailing whitespaces */
401 for (; pos
> 0; --pos
) {
402 if (ln
.buf
[pos
- 1] != ' ')
404 if (pos
> 2 && ln
.buf
[pos
- 2] == '\\')
410 /* Catch escaped bogus characters. */
412 c
= (unsigned char) blk
.buf
[i
+1];
414 if ( ! (isascii(c
) &&
415 (isgraph(c
) || isblank(c
)))) {
416 mandoc_msg(MANDOCERR_BADCHAR
, curp
,
417 curp
->line
, pos
, NULL
);
423 /* Some other escape sequence, copy & cont. */
425 ln
.buf
[pos
++] = blk
.buf
[i
++];
426 ln
.buf
[pos
++] = blk
.buf
[i
++];
429 if (pos
>= (int)ln
.sz
)
430 resize_buf(&ln
, 256);
435 * A significant amount of complexity is contained by
436 * the roff preprocessor. It's line-oriented but can be
437 * expressed on one line, so we need at times to
438 * readjust our starting point and re-run it. The roff
439 * preprocessor can also readjust the buffers with new
440 * data, so we pass them in wholesale.
446 * Maintain a lookaside buffer of all parsed lines. We
447 * only do this if mparse_keep() has been invoked (the
448 * buffer may be accessed with mparse_getkeep()).
451 if (curp
->secondary
) {
452 curp
->secondary
->buf
=
454 (curp
->secondary
->buf
,
455 curp
->secondary
->sz
+ pos
+ 2);
456 memcpy(curp
->secondary
->buf
+
459 curp
->secondary
->sz
+= pos
;
461 [curp
->secondary
->sz
] = '\n';
462 curp
->secondary
->sz
++;
464 [curp
->secondary
->sz
] = '\0';
468 (curp
->roff
, curp
->line
,
469 &ln
.buf
, &ln
.sz
, of
, &of
);
473 if (REPARSE_LIMIT
>= ++curp
->reparse_count
)
474 mparse_buf_r(curp
, ln
, 0);
476 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
,
477 curp
->line
, pos
, NULL
);
481 pos
= (int)strlen(ln
.buf
);
489 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
493 * We remove `so' clauses from our lookaside
494 * buffer because we're going to descend into
495 * the file recursively.
498 curp
->secondary
->sz
-= pos
+ 1;
499 mparse_readfd(curp
, -1, ln
.buf
+ of
);
500 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
509 * If we encounter errors in the recursive parse, make
510 * sure we don't continue parsing.
513 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
517 * If input parsers have not been allocated, do so now.
518 * We keep these instanced between parsers, but set them
519 * locally per parse routine since we can use different
520 * parsers with each one.
523 if ( ! (curp
->man
|| curp
->mdoc
))
524 pset(ln
.buf
+ of
, pos
- of
, curp
);
527 * Lastly, push down into the parsers themselves. One
528 * of these will have already been set in the pset()
530 * If libroff returns ROFF_TBL, then add it to the
531 * currently open parse. Since we only get here if
532 * there does exist data (see tbl_data.c), we're
533 * guaranteed that something's been allocated.
534 * Do the same for ROFF_EQN.
540 while (NULL
!= (span
= roff_span(curp
->roff
))) {
542 man_addspan(curp
->man
, span
) :
543 mdoc_addspan(curp
->mdoc
, span
);
547 else if (ROFF_EQN
== rr
)
549 mdoc_addeqn(curp
->mdoc
,
550 roff_eqn(curp
->roff
)) :
551 man_addeqn(curp
->man
,
552 roff_eqn(curp
->roff
));
553 else if (curp
->man
|| curp
->mdoc
)
555 man_parseln(curp
->man
,
556 curp
->line
, ln
.buf
, of
) :
557 mdoc_parseln(curp
->mdoc
,
558 curp
->line
, ln
.buf
, of
);
561 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
565 /* Temporary buffers typically are not full. */
567 if (0 == start
&& '\0' == blk
.buf
[i
])
570 /* Start the next input line. */
579 read_whole_file(struct mparse
*curp
, const char *file
, int fd
,
580 struct buf
*fb
, int *with_mmap
)
587 if (-1 == fstat(fd
, &st
)) {
588 curp
->file_status
= MANDOCLEVEL_SYSERR
;
590 (*curp
->mmsg
)(MANDOCERR_SYSSTAT
, curp
->file_status
,
591 file
, 0, 0, strerror(errno
));
596 * If we're a regular file, try just reading in the whole entry
597 * via mmap(). This is faster than reading it into blocks, and
598 * since each file is only a few bytes to begin with, I'm not
599 * concerned that this is going to tank any machines.
602 if (S_ISREG(st
.st_mode
)) {
603 if (st
.st_size
>= (1U << 31)) {
604 curp
->file_status
= MANDOCLEVEL_FATAL
;
606 (*curp
->mmsg
)(MANDOCERR_TOOLARGE
,
607 curp
->file_status
, file
, 0, 0, NULL
);
611 fb
->sz
= (size_t)st
.st_size
;
612 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
, MAP_SHARED
, fd
, 0);
613 if (fb
->buf
!= MAP_FAILED
)
619 * If this isn't a regular file (like, say, stdin), then we must
620 * go the old way and just read things in bit by bit.
629 if (fb
->sz
== (1U << 31)) {
630 curp
->file_status
= MANDOCLEVEL_FATAL
;
632 (*curp
->mmsg
)(MANDOCERR_TOOLARGE
,
637 resize_buf(fb
, 65536);
639 ssz
= read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
645 curp
->file_status
= MANDOCLEVEL_SYSERR
;
647 (*curp
->mmsg
)(MANDOCERR_SYSREAD
,
648 curp
->file_status
, file
, 0, 0,
661 mparse_end(struct mparse
*curp
)
664 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
667 if (curp
->mdoc
&& ! mdoc_endparse(curp
->mdoc
)) {
668 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
672 if (curp
->man
&& ! man_endparse(curp
->man
)) {
673 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
677 if ( ! (curp
->man
|| curp
->mdoc
)) {
678 mandoc_msg(MANDOCERR_NOTMANUAL
, curp
, 1, 0, NULL
);
679 curp
->file_status
= MANDOCLEVEL_FATAL
;
683 roff_endparse(curp
->roff
);
687 mparse_parse_buffer(struct mparse
*curp
, struct buf blk
, const char *file
)
690 static int recursion_depth
;
692 if (64 < recursion_depth
) {
693 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
, curp
->line
, 0, NULL
);
697 /* Line number is per-file. */
703 mparse_buf_r(curp
, blk
, 1);
705 if (0 == --recursion_depth
&& MANDOCLEVEL_FATAL
> curp
->file_status
)
712 mparse_readmem(struct mparse
*curp
, const void *buf
, size_t len
,
717 blk
.buf
= UNCONST(buf
);
720 mparse_parse_buffer(curp
, blk
, file
);
721 return(curp
->file_status
);
725 mparse_readfd(struct mparse
*curp
, int fd
, const char *file
)
730 if (-1 == fd
&& -1 == (fd
= open(file
, O_RDONLY
, 0))) {
731 curp
->file_status
= MANDOCLEVEL_SYSERR
;
733 (*curp
->mmsg
)(MANDOCERR_SYSOPEN
,
735 file
, 0, 0, strerror(errno
));
740 * Run for each opened file; may be called more than once for
741 * each full parse sequence if the opened file is nested (i.e.,
742 * from `so'). Simply sucks in the whole file and moves into
743 * the parse phase for the file.
746 if ( ! read_whole_file(curp
, file
, fd
, &blk
, &with_mmap
))
749 mparse_parse_buffer(curp
, blk
, file
);
753 munmap(blk
.buf
, blk
.sz
);
758 if (STDIN_FILENO
!= fd
&& -1 == close(fd
))
761 return(curp
->file_status
);
765 mparse_alloc(enum mparset inttype
, enum mandoclevel wlevel
,
766 mandocmsg mmsg
, void *arg
, char *defos
)
770 assert(wlevel
<= MANDOCLEVEL_FATAL
);
772 curp
= mandoc_calloc(1, sizeof(struct mparse
));
774 curp
->wlevel
= wlevel
;
777 curp
->inttype
= inttype
;
780 curp
->roff
= roff_alloc(inttype
, curp
);
785 mparse_reset(struct mparse
*curp
)
788 roff_reset(curp
->roff
);
791 mdoc_reset(curp
->mdoc
);
793 man_reset(curp
->man
);
795 curp
->secondary
->sz
= 0;
797 curp
->file_status
= MANDOCLEVEL_OK
;
803 mparse_free(struct mparse
*curp
)
807 mdoc_free(curp
->pmdoc
);
809 man_free(curp
->pman
);
811 roff_free(curp
->roff
);
813 free(curp
->secondary
->buf
);
815 free(curp
->secondary
);
820 mparse_result(struct mparse
*curp
, struct mdoc
**mdoc
, struct man
**man
)
830 mandoc_vmsg(enum mandocerr t
, struct mparse
*m
,
831 int ln
, int pos
, const char *fmt
, ...)
837 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
840 mandoc_msg(t
, m
, ln
, pos
, buf
);
844 mandoc_msg(enum mandocerr er
, struct mparse
*m
,
845 int ln
, int col
, const char *msg
)
847 enum mandoclevel level
;
849 level
= MANDOCLEVEL_FATAL
;
850 while (er
< mandoclimits
[level
])
853 if (level
< m
->wlevel
)
857 (*m
->mmsg
)(er
, level
, m
->file
, ln
, col
, msg
);
859 if (m
->file_status
< level
)
860 m
->file_status
= level
;
864 mparse_strerror(enum mandocerr er
)
867 return(mandocerrs
[er
]);
871 mparse_strlevel(enum mandoclevel lvl
)
873 return(mandoclevels
[lvl
]);
877 mparse_keep(struct mparse
*p
)
880 assert(NULL
== p
->secondary
);
881 p
->secondary
= mandoc_calloc(1, sizeof(struct buf
));
885 mparse_getkeep(const struct mparse
*p
)
888 assert(p
->secondary
);
889 return(p
->secondary
->sz
? p
->secondary
->buf
: NULL
);