]>
git.cameronkatri.com Git - mandoc.git/blob - read.c
1 /* $Id: read.c,v 1.13 2011/04/11 21:59:39 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 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.
35 #include "libmandoc.h"
43 #define REPARSE_LIMIT 1000
46 char *buf
; /* binary input buffer */
47 size_t sz
; /* size of binary buffer */
51 enum mandoclevel file_status
; /* status of current parse */
52 enum mandoclevel wlevel
; /* ignore messages below this */
53 int line
; /* line number in the file */
54 enum mparset inttype
; /* which parser to use */
55 struct man
*pman
; /* persistent man parser */
56 struct mdoc
*pmdoc
; /* persistent mdoc parser */
57 struct man
*man
; /* man parser */
58 struct mdoc
*mdoc
; /* mdoc parser */
59 struct roff
*roff
; /* roff parser (!NULL) */
60 struct regset regs
; /* roff registers */
61 int reparse_count
; /* finite interp. stack */
62 mandocmsg mmsg
; /* warning/error message handler */
63 void *arg
; /* argument to mmsg */
67 static void resize_buf(struct buf
*, size_t);
68 static void mparse_buf_r(struct mparse
*, struct buf
, int);
69 static void mparse_readfd_r(struct mparse
*, int, const char *, int);
70 static void pset(const char *, int, struct mparse
*);
71 static void pdesc(struct mparse
*, const char *, int);
72 static int read_whole_file(const char *, int, struct buf
*, int *);
73 static void mparse_end(struct mparse
*);
75 static const enum mandocerr mandoclimits
[MANDOCLEVEL_MAX
] = {
85 static const char * const mandocerrs
[MANDOCERR_MAX
] = {
90 /* related to the prologue */
91 "no title in document",
92 "document title should be all caps",
93 "unknown manual section",
94 "date missing, using today's date",
95 "cannot parse date, using it verbatim",
96 "prologue macros out of order",
97 "duplicate prologue macro",
98 "macro not allowed in prologue",
99 "macro not allowed in body",
101 /* related to document structure */
102 ".so is fragile, better use ln(1)",
103 "NAME section must come first",
104 "bad NAME section contents",
105 "manual name not yet set",
106 "sections out of conventional order",
107 "duplicate section name",
108 "section not in conventional manual section",
110 /* related to macros and nesting */
111 "skipping obsolete macro",
112 "skipping paragraph macro",
113 "skipping no-space macro",
114 "blocks badly nested",
115 "child violates parent syntax",
116 "nested displays are not portable",
117 "already in literal mode",
120 /* related to missing macro arguments */
121 "skipping empty macro",
122 "argument count wrong",
123 "missing display type",
124 "list type must come first",
125 "tag lists require a width argument",
127 "skipping end of block that is not open",
129 /* related to bad macro arguments */
131 "duplicate argument",
132 "duplicate display type",
133 "duplicate list type",
134 "unknown AT&T UNIX version",
137 "unknown standard specifier",
138 "bad width argument",
140 /* related to plain text */
141 "blank line in non-literal context",
142 "tab in non-literal context",
143 "end of line whitespace",
145 "bad escape sequence",
146 "unterminated quoted string",
150 /* related to tables */
154 "no table layout cells specified",
155 "no table data cells specified",
156 "ignore data in cell",
157 "data block still open",
158 "ignoring extra data cells",
160 "input stack limit exceeded, infinite loop?",
161 "skipping bad character",
162 "escaped character not allowed in a name",
163 "skipping text before the first section header",
164 "skipping unknown macro",
165 "NOT IMPLEMENTED, please use groff: skipping request",
166 "argument count wrong",
167 "skipping end of block that is not open",
168 "missing end of block",
169 "scope open on exit",
170 "uname(3) system call failed",
171 "macro requires line argument(s)",
172 "macro requires body argument(s)",
173 "macro requires argument(s)",
175 "line argument(s) will be lost",
176 "body argument(s) will be lost",
178 "generic fatal error",
181 "column syntax is inconsistent",
182 "NOT IMPLEMENTED: .Bd -file",
183 "line scope broken, syntax violated",
184 "argument count wrong, violates syntax",
185 "child violates parent syntax",
186 "argument count wrong, violates syntax",
187 "NOT IMPLEMENTED: .so with absolute path or \"..\"",
189 "no document prologue",
190 "static buffer exhausted",
193 static const char * const mandoclevels
[MANDOCLEVEL_MAX
] = {
204 resize_buf(struct buf
*buf
, size_t initial
)
207 buf
->sz
= buf
->sz
> initial
/2 ? 2 * buf
->sz
: initial
;
208 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
212 pset(const char *buf
, int pos
, struct mparse
*curp
)
217 * Try to intuit which kind of manual parser should be used. If
218 * passed in by command-line (-man, -mdoc), then use that
219 * explicitly. If passed as -mandoc, then try to guess from the
220 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
221 * default to -man, which is more lenient.
223 * Separate out pmdoc/pman from mdoc/man: the first persists
224 * through all parsers, while the latter is used per-parse.
227 if ('.' == buf
[0] || '\'' == buf
[0]) {
228 for (i
= 1; buf
[i
]; i
++)
229 if (' ' != buf
[i
] && '\t' != buf
[i
])
235 switch (curp
->inttype
) {
237 if (NULL
== curp
->pmdoc
)
238 curp
->pmdoc
= mdoc_alloc(&curp
->regs
, curp
);
240 curp
->mdoc
= curp
->pmdoc
;
243 if (NULL
== curp
->pman
)
244 curp
->pman
= man_alloc(&curp
->regs
, curp
);
246 curp
->man
= curp
->pman
;
252 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
253 if (NULL
== curp
->pmdoc
)
254 curp
->pmdoc
= mdoc_alloc(&curp
->regs
, curp
);
256 curp
->mdoc
= curp
->pmdoc
;
260 if (NULL
== curp
->pman
)
261 curp
->pman
= man_alloc(&curp
->regs
, curp
);
263 curp
->man
= curp
->pman
;
267 * Main parse routine for an opened file. This is called for each
268 * opened file and simply loops around the full input file, possibly
269 * nesting (i.e., with `so').
272 mparse_buf_r(struct mparse
*curp
, struct buf blk
, int start
)
274 const struct tbl_span
*span
;
278 int pos
; /* byte number in the ln buffer */
279 int lnn
; /* line number in the real file */
282 memset(&ln
, 0, sizeof(struct buf
));
287 for (i
= 0; i
< (int)blk
.sz
; ) {
288 if (0 == pos
&& '\0' == blk
.buf
[i
])
293 curp
->reparse_count
= 0;
296 while (i
< (int)blk
.sz
&& (start
|| '\0' != blk
.buf
[i
])) {
299 * When finding an unescaped newline character,
300 * leave the character loop to process the line.
301 * Skip a preceding carriage return, if any.
304 if ('\r' == blk
.buf
[i
] && i
+ 1 < (int)blk
.sz
&&
305 '\n' == blk
.buf
[i
+ 1])
307 if ('\n' == blk
.buf
[i
]) {
314 * Warn about bogus characters. If you're using
315 * non-ASCII encoding, you're screwing your
316 * readers. Since I'd rather this not happen,
317 * I'll be helpful and drop these characters so
318 * we don't display gibberish. Note to manual
319 * writers: use special characters.
322 c
= (unsigned char) blk
.buf
[i
];
324 if ( ! (isascii(c
) &&
325 (isgraph(c
) || isblank(c
)))) {
326 mandoc_msg(MANDOCERR_BADCHAR
, curp
,
327 curp
->line
, pos
, "ignoring byte");
332 /* Trailing backslash = a plain char. */
334 if ('\\' != blk
.buf
[i
] || i
+ 1 == (int)blk
.sz
) {
335 if (pos
>= (int)ln
.sz
)
336 resize_buf(&ln
, 256);
337 ln
.buf
[pos
++] = blk
.buf
[i
++];
342 * Found escape and at least one other character.
343 * When it's a newline character, skip it.
344 * When there is a carriage return in between,
345 * skip that one as well.
348 if ('\r' == blk
.buf
[i
+ 1] && i
+ 2 < (int)blk
.sz
&&
349 '\n' == blk
.buf
[i
+ 2])
351 if ('\n' == blk
.buf
[i
+ 1]) {
357 if ('"' == blk
.buf
[i
+ 1] || '#' == blk
.buf
[i
+ 1]) {
359 /* Comment, skip to end of line */
360 for (; i
< (int)blk
.sz
; ++i
) {
361 if ('\n' == blk
.buf
[i
]) {
368 /* Backout trailing whitespaces */
369 for (; pos
> 0; --pos
) {
370 if (ln
.buf
[pos
- 1] != ' ')
372 if (pos
> 2 && ln
.buf
[pos
- 2] == '\\')
378 /* Some other escape sequence, copy & cont. */
380 if (pos
+ 1 >= (int)ln
.sz
)
381 resize_buf(&ln
, 256);
383 ln
.buf
[pos
++] = blk
.buf
[i
++];
384 ln
.buf
[pos
++] = blk
.buf
[i
++];
387 if (pos
>= (int)ln
.sz
)
388 resize_buf(&ln
, 256);
393 * A significant amount of complexity is contained by
394 * the roff preprocessor. It's line-oriented but can be
395 * expressed on one line, so we need at times to
396 * readjust our starting point and re-run it. The roff
397 * preprocessor can also readjust the buffers with new
398 * data, so we pass them in wholesale.
405 (curp
->roff
, curp
->line
,
406 &ln
.buf
, &ln
.sz
, of
, &of
);
410 if (REPARSE_LIMIT
>= ++curp
->reparse_count
)
411 mparse_buf_r(curp
, ln
, 0);
413 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
,
414 curp
->line
, pos
, NULL
);
418 pos
= (int)strlen(ln
.buf
);
426 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
429 mparse_readfd_r(curp
, -1, ln
.buf
+ of
, 1);
430 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
439 * If we encounter errors in the recursive parse, make
440 * sure we don't continue parsing.
443 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
447 * If input parsers have not been allocated, do so now.
448 * We keep these instanced betwen parsers, but set them
449 * locally per parse routine since we can use different
450 * parsers with each one.
453 if ( ! (curp
->man
|| curp
->mdoc
))
454 pset(ln
.buf
+ of
, pos
- of
, curp
);
457 * Lastly, push down into the parsers themselves. One
458 * of these will have already been set in the pset()
460 * If libroff returns ROFF_TBL, then add it to the
461 * currently open parse. Since we only get here if
462 * there does exist data (see tbl_data.c), we're
463 * guaranteed that something's been allocated.
464 * Do the same for ROFF_EQN.
470 while (NULL
!= (span
= roff_span(curp
->roff
))) {
472 man_addspan(curp
->man
, span
) :
473 mdoc_addspan(curp
->mdoc
, span
);
477 else if (ROFF_EQN
== rr
)
479 mdoc_addeqn(curp
->mdoc
,
480 roff_eqn(curp
->roff
)) :
481 man_addeqn(curp
->man
,
482 roff_eqn(curp
->roff
));
483 else if (curp
->man
|| curp
->mdoc
)
485 man_parseln(curp
->man
,
486 curp
->line
, ln
.buf
, of
) :
487 mdoc_parseln(curp
->mdoc
,
488 curp
->line
, ln
.buf
, of
);
491 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
495 /* Temporary buffers typically are not full. */
497 if (0 == start
&& '\0' == blk
.buf
[i
])
500 /* Start the next input line. */
509 pdesc(struct mparse
*curp
, const char *file
, int fd
)
515 * Run for each opened file; may be called more than once for
516 * each full parse sequence if the opened file is nested (i.e.,
517 * from `so'). Simply sucks in the whole file and moves into
518 * the parse phase for the file.
521 if ( ! read_whole_file(file
, fd
, &blk
, &with_mmap
)) {
522 curp
->file_status
= MANDOCLEVEL_SYSERR
;
526 /* Line number is per-file. */
530 mparse_buf_r(curp
, blk
, 1);
533 munmap(blk
.buf
, blk
.sz
);
539 read_whole_file(const char *file
, int fd
, struct buf
*fb
, int *with_mmap
)
545 if (-1 == fstat(fd
, &st
)) {
551 * If we're a regular file, try just reading in the whole entry
552 * via mmap(). This is faster than reading it into blocks, and
553 * since each file is only a few bytes to begin with, I'm not
554 * concerned that this is going to tank any machines.
557 if (S_ISREG(st
.st_mode
)) {
558 if (st
.st_size
>= (1U << 31)) {
559 fprintf(stderr
, "%s: input too large\n", file
);
563 fb
->sz
= (size_t)st
.st_size
;
564 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
,
565 MAP_FILE
|MAP_SHARED
, fd
, 0);
566 if (fb
->buf
!= MAP_FAILED
)
571 * If this isn't a regular file (like, say, stdin), then we must
572 * go the old way and just read things in bit by bit.
581 if (fb
->sz
== (1U << 31)) {
582 fprintf(stderr
, "%s: input too large\n", file
);
585 resize_buf(fb
, 65536);
587 ssz
= read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
605 mparse_end(struct mparse
*curp
)
608 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
611 if (curp
->mdoc
&& ! mdoc_endparse(curp
->mdoc
)) {
612 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
616 if (curp
->man
&& ! man_endparse(curp
->man
)) {
617 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
621 if ( ! (curp
->man
|| curp
->mdoc
)) {
622 mandoc_msg(MANDOCERR_NOTMANUAL
, curp
, 1, 0, NULL
);
623 curp
->file_status
= MANDOCLEVEL_FATAL
;
627 roff_endparse(curp
->roff
);
631 mparse_readfd_r(struct mparse
*curp
, int fd
, const char *file
, int re
)
636 if (-1 == (fd
= open(file
, O_RDONLY
, 0))) {
638 curp
->file_status
= MANDOCLEVEL_SYSERR
;
645 pdesc(curp
, file
, fd
);
647 if (0 == re
&& MANDOCLEVEL_FATAL
> curp
->file_status
)
650 if (STDIN_FILENO
!= fd
&& -1 == close(fd
))
657 mparse_readfd(struct mparse
*curp
, int fd
, const char *file
)
660 mparse_readfd_r(curp
, fd
, file
, 0);
661 return(curp
->file_status
);
665 mparse_alloc(enum mparset inttype
, enum mandoclevel wlevel
, mandocmsg mmsg
, void *arg
)
669 assert(wlevel
<= MANDOCLEVEL_FATAL
);
671 curp
= mandoc_calloc(1, sizeof(struct mparse
));
673 curp
->wlevel
= wlevel
;
676 curp
->inttype
= inttype
;
678 curp
->roff
= roff_alloc(&curp
->regs
, curp
);
683 mparse_reset(struct mparse
*curp
)
686 memset(&curp
->regs
, 0, sizeof(struct regset
));
688 roff_reset(curp
->roff
);
691 mdoc_reset(curp
->mdoc
);
693 man_reset(curp
->man
);
695 curp
->file_status
= MANDOCLEVEL_OK
;
701 mparse_free(struct mparse
*curp
)
705 mdoc_free(curp
->pmdoc
);
707 man_free(curp
->pman
);
709 roff_free(curp
->roff
);
715 mparse_result(struct mparse
*curp
, struct mdoc
**mdoc
, struct man
**man
)
725 mandoc_vmsg(enum mandocerr t
, struct mparse
*m
,
726 int ln
, int pos
, const char *fmt
, ...)
732 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
735 mandoc_msg(t
, m
, ln
, pos
, buf
);
739 mandoc_msg(enum mandocerr er
, struct mparse
*m
,
740 int ln
, int col
, const char *msg
)
742 enum mandoclevel level
;
744 level
= MANDOCLEVEL_FATAL
;
745 while (er
< mandoclimits
[level
])
748 if (level
< m
->wlevel
)
752 (*m
->mmsg
)(er
, level
, m
->file
, ln
, col
, msg
);
754 if (m
->file_status
< level
)
755 m
->file_status
= level
;
759 mparse_strerror(enum mandocerr er
)
762 return(mandocerrs
[er
]);
766 mparse_strlevel(enum mandoclevel lvl
)
768 return(mandoclevels
[lvl
]);