]>
git.cameronkatri.com Git - mandoc.git/blob - read.c
1 /* $Id: read.c,v 1.7 2011/03/28 21:49:42 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.
31 #include "libmandoc.h"
39 #define REPARSE_LIMIT 1000
42 char *buf
; /* binary input buffer */
43 size_t sz
; /* size of binary buffer */
47 enum mandoclevel file_status
; /* status of current parse */
48 enum mandoclevel wlevel
; /* ignore messages below this */
49 int line
; /* line number in the file */
50 enum mparset inttype
; /* which parser to use */
51 struct man
*pman
; /* persistent man parser */
52 struct mdoc
*pmdoc
; /* persistent mdoc parser */
53 struct man
*man
; /* man parser */
54 struct mdoc
*mdoc
; /* mdoc parser */
55 struct roff
*roff
; /* roff parser (!NULL) */
56 struct regset regs
; /* roff registers */
57 int reparse_count
; /* finite interp. stack */
58 mandocmsg mmsg
; /* warning/error message handler */
59 void *arg
; /* argument to mmsg */
63 static void resize_buf(struct buf
*, size_t);
64 static void mparse_buf_r(struct mparse
*, struct buf
, int);
65 static void mparse_readfd_r(struct mparse
*, int, const char *, int);
66 static void pset(const char *, int, struct mparse
*);
67 static void pdesc(struct mparse
*, const char *, int);
68 static int read_whole_file(const char *, int, struct buf
*, int *);
69 static void mparse_end(struct mparse
*);
71 static const enum mandocerr mandoclimits
[MANDOCLEVEL_MAX
] = {
81 static const char * const mandocerrs
[MANDOCERR_MAX
] = {
86 /* related to the prologue */
87 "no title in document",
88 "document title should be all caps",
89 "unknown manual section",
90 "date missing, using today's date",
91 "cannot parse date, using it verbatim",
92 "prologue macros out of order",
93 "duplicate prologue macro",
94 "macro not allowed in prologue",
95 "macro not allowed in body",
97 /* related to document structure */
98 ".so is fragile, better use ln(1)",
99 "NAME section must come first",
100 "bad NAME section contents",
101 "manual name not yet set",
102 "sections out of conventional order",
103 "duplicate section name",
104 "section not in conventional manual section",
106 /* related to macros and nesting */
107 "skipping obsolete macro",
108 "skipping paragraph macro",
109 "skipping no-space macro",
110 "blocks badly nested",
111 "child violates parent syntax",
112 "nested displays are not portable",
113 "already in literal mode",
116 /* related to missing macro arguments */
117 "skipping empty macro",
118 "argument count wrong",
119 "missing display type",
120 "list type must come first",
121 "tag lists require a width argument",
123 "skipping end of block that is not open",
125 /* related to bad macro arguments */
127 "duplicate argument",
128 "duplicate display type",
129 "duplicate list type",
130 "unknown AT&T UNIX version",
133 "unknown standard specifier",
134 "bad width argument",
136 /* related to plain text */
137 "blank line in non-literal context",
138 "tab in non-literal context",
139 "end of line whitespace",
141 "unknown escape sequence",
142 "unterminated quoted string",
146 /* related to tables */
150 "no table layout cells specified",
151 "no table data cells specified",
152 "ignore data in cell",
153 "data block still open",
154 "ignoring extra data cells",
156 "input stack limit exceeded, infinite loop?",
157 "skipping bad character",
158 "escaped character not allowed in a name",
159 "skipping text before the first section header",
160 "skipping unknown macro",
161 "NOT IMPLEMENTED, please use groff: skipping request",
162 "argument count wrong",
163 "skipping end of block that is not open",
164 "missing end of block",
165 "scope open on exit",
166 "uname(3) system call failed",
167 "macro requires line argument(s)",
168 "macro requires body argument(s)",
169 "macro requires argument(s)",
171 "line argument(s) will be lost",
172 "body argument(s) will be lost",
174 "generic fatal error",
177 "column syntax is inconsistent",
178 "NOT IMPLEMENTED: .Bd -file",
179 "line scope broken, syntax violated",
180 "argument count wrong, violates syntax",
181 "child violates parent syntax",
182 "argument count wrong, violates syntax",
183 "NOT IMPLEMENTED: .so with absolute path or \"..\"",
185 "no document prologue",
186 "static buffer exhausted",
189 static const char * const mandoclevels
[MANDOCLEVEL_MAX
] = {
200 resize_buf(struct buf
*buf
, size_t initial
)
203 buf
->sz
= buf
->sz
> initial
/2 ? 2 * buf
->sz
: initial
;
204 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
208 pset(const char *buf
, int pos
, struct mparse
*curp
)
213 * Try to intuit which kind of manual parser should be used. If
214 * passed in by command-line (-man, -mdoc), then use that
215 * explicitly. If passed as -mandoc, then try to guess from the
216 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
217 * default to -man, which is more lenient.
219 * Separate out pmdoc/pman from mdoc/man: the first persists
220 * through all parsers, while the latter is used per-parse.
223 if ('.' == buf
[0] || '\'' == buf
[0]) {
224 for (i
= 1; buf
[i
]; i
++)
225 if (' ' != buf
[i
] && '\t' != buf
[i
])
231 switch (curp
->inttype
) {
233 if (NULL
== curp
->pmdoc
)
234 curp
->pmdoc
= mdoc_alloc(&curp
->regs
, curp
);
236 curp
->mdoc
= curp
->pmdoc
;
239 if (NULL
== curp
->pman
)
240 curp
->pman
= man_alloc(&curp
->regs
, curp
);
242 curp
->man
= curp
->pman
;
248 if (pos
>= 3 && 0 == memcmp(buf
, ".Dd", 3)) {
249 if (NULL
== curp
->pmdoc
)
250 curp
->pmdoc
= mdoc_alloc(&curp
->regs
, curp
);
252 curp
->mdoc
= curp
->pmdoc
;
256 if (NULL
== curp
->pman
)
257 curp
->pman
= man_alloc(&curp
->regs
, curp
);
259 curp
->man
= curp
->pman
;
263 * Main parse routine for an opened file. This is called for each
264 * opened file and simply loops around the full input file, possibly
265 * nesting (i.e., with `so').
268 mparse_buf_r(struct mparse
*curp
, struct buf blk
, int start
)
270 const struct tbl_span
*span
;
274 int pos
; /* byte number in the ln buffer */
275 int lnn
; /* line number in the real file */
278 memset(&ln
, 0, sizeof(struct buf
));
283 for (i
= 0; i
< (int)blk
.sz
; ) {
284 if (0 == pos
&& '\0' == blk
.buf
[i
])
289 curp
->reparse_count
= 0;
292 while (i
< (int)blk
.sz
&& (start
|| '\0' != blk
.buf
[i
])) {
295 * When finding an unescaped newline character,
296 * leave the character loop to process the line.
297 * Skip a preceding carriage return, if any.
300 if ('\r' == blk
.buf
[i
] && i
+ 1 < (int)blk
.sz
&&
301 '\n' == blk
.buf
[i
+ 1])
303 if ('\n' == blk
.buf
[i
]) {
310 * Warn about bogus characters. If you're using
311 * non-ASCII encoding, you're screwing your
312 * readers. Since I'd rather this not happen,
313 * I'll be helpful and drop these characters so
314 * we don't display gibberish. Note to manual
315 * writers: use special characters.
318 c
= (unsigned char) blk
.buf
[i
];
320 if ( ! (isascii(c
) &&
321 (isgraph(c
) || isblank(c
)))) {
322 mandoc_msg(MANDOCERR_BADCHAR
, curp
,
323 curp
->line
, pos
, "ignoring byte");
328 /* Trailing backslash = a plain char. */
330 if ('\\' != blk
.buf
[i
] || i
+ 1 == (int)blk
.sz
) {
331 if (pos
>= (int)ln
.sz
)
332 resize_buf(&ln
, 256);
333 ln
.buf
[pos
++] = blk
.buf
[i
++];
338 * Found escape and at least one other character.
339 * When it's a newline character, skip it.
340 * When there is a carriage return in between,
341 * skip that one as well.
344 if ('\r' == blk
.buf
[i
+ 1] && i
+ 2 < (int)blk
.sz
&&
345 '\n' == blk
.buf
[i
+ 2])
347 if ('\n' == blk
.buf
[i
+ 1]) {
353 if ('"' == blk
.buf
[i
+ 1]) {
355 /* Comment, skip to end of line */
356 for (; i
< (int)blk
.sz
; ++i
) {
357 if ('\n' == blk
.buf
[i
]) {
364 /* Backout trailing whitespaces */
365 for (; pos
> 0; --pos
) {
366 if (ln
.buf
[pos
- 1] != ' ')
368 if (pos
> 2 && ln
.buf
[pos
- 2] == '\\')
374 /* Some other escape sequence, copy & cont. */
376 if (pos
+ 1 >= (int)ln
.sz
)
377 resize_buf(&ln
, 256);
379 ln
.buf
[pos
++] = blk
.buf
[i
++];
380 ln
.buf
[pos
++] = blk
.buf
[i
++];
383 if (pos
>= (int)ln
.sz
)
384 resize_buf(&ln
, 256);
389 * A significant amount of complexity is contained by
390 * the roff preprocessor. It's line-oriented but can be
391 * expressed on one line, so we need at times to
392 * readjust our starting point and re-run it. The roff
393 * preprocessor can also readjust the buffers with new
394 * data, so we pass them in wholesale.
401 (curp
->roff
, curp
->line
,
402 &ln
.buf
, &ln
.sz
, of
, &of
);
406 if (REPARSE_LIMIT
>= ++curp
->reparse_count
)
407 mparse_buf_r(curp
, ln
, 0);
409 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
,
410 curp
->line
, pos
, NULL
);
414 pos
= (int)strlen(ln
.buf
);
422 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
425 mparse_readfd_r(curp
, -1, ln
.buf
+ of
, 1);
426 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
435 * If we encounter errors in the recursive parse, make
436 * sure we don't continue parsing.
439 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
443 * If input parsers have not been allocated, do so now.
444 * We keep these instanced betwen parsers, but set them
445 * locally per parse routine since we can use different
446 * parsers with each one.
449 if ( ! (curp
->man
|| curp
->mdoc
))
450 pset(ln
.buf
+ of
, pos
- of
, curp
);
453 * Lastly, push down into the parsers themselves. One
454 * of these will have already been set in the pset()
456 * If libroff returns ROFF_TBL, then add it to the
457 * currently open parse. Since we only get here if
458 * there does exist data (see tbl_data.c), we're
459 * guaranteed that something's been allocated.
460 * Do the same for ROFF_EQN.
466 while (NULL
!= (span
= roff_span(curp
->roff
))) {
468 man_addspan(curp
->man
, span
) :
469 mdoc_addspan(curp
->mdoc
, span
);
473 else if (ROFF_EQN
== rr
)
475 mdoc_addeqn(curp
->mdoc
,
476 roff_eqn(curp
->roff
)) :
477 man_addeqn(curp
->man
,
478 roff_eqn(curp
->roff
));
479 else if (curp
->man
|| curp
->mdoc
)
481 man_parseln(curp
->man
,
482 curp
->line
, ln
.buf
, of
) :
483 mdoc_parseln(curp
->mdoc
,
484 curp
->line
, ln
.buf
, of
);
487 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
491 /* Temporary buffers typically are not full. */
493 if (0 == start
&& '\0' == blk
.buf
[i
])
496 /* Start the next input line. */
505 pdesc(struct mparse
*curp
, const char *file
, int fd
)
511 * Run for each opened file; may be called more than once for
512 * each full parse sequence if the opened file is nested (i.e.,
513 * from `so'). Simply sucks in the whole file and moves into
514 * the parse phase for the file.
517 if ( ! read_whole_file(file
, fd
, &blk
, &with_mmap
)) {
518 curp
->file_status
= MANDOCLEVEL_SYSERR
;
522 /* Line number is per-file. */
526 mparse_buf_r(curp
, blk
, 1);
529 munmap(blk
.buf
, blk
.sz
);
535 read_whole_file(const char *file
, int fd
, struct buf
*fb
, int *with_mmap
)
541 if (-1 == fstat(fd
, &st
)) {
547 * If we're a regular file, try just reading in the whole entry
548 * via mmap(). This is faster than reading it into blocks, and
549 * since each file is only a few bytes to begin with, I'm not
550 * concerned that this is going to tank any machines.
553 if (S_ISREG(st
.st_mode
)) {
554 if (st
.st_size
>= (1U << 31)) {
555 fprintf(stderr
, "%s: input too large\n", file
);
559 fb
->sz
= (size_t)st
.st_size
;
560 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
,
561 MAP_FILE
|MAP_SHARED
, fd
, 0);
562 if (fb
->buf
!= MAP_FAILED
)
567 * If this isn't a regular file (like, say, stdin), then we must
568 * go the old way and just read things in bit by bit.
577 if (fb
->sz
== (1U << 31)) {
578 fprintf(stderr
, "%s: input too large\n", file
);
581 resize_buf(fb
, 65536);
583 ssz
= read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
601 mparse_end(struct mparse
*curp
)
604 if (MANDOCLEVEL_FATAL
<= curp
->file_status
)
607 if (curp
->mdoc
&& ! mdoc_endparse(curp
->mdoc
)) {
608 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
612 if (curp
->man
&& ! man_endparse(curp
->man
)) {
613 assert(MANDOCLEVEL_FATAL
<= curp
->file_status
);
617 if ( ! (curp
->man
|| curp
->mdoc
)) {
618 mandoc_msg(MANDOCERR_NOTMANUAL
, curp
, 1, 0, NULL
);
619 curp
->file_status
= MANDOCLEVEL_FATAL
;
623 roff_endparse(curp
->roff
);
627 mparse_readfd_r(struct mparse
*curp
, int fd
, const char *file
, int re
)
632 if (-1 == (fd
= open(file
, O_RDONLY
, 0))) {
634 curp
->file_status
= MANDOCLEVEL_SYSERR
;
641 pdesc(curp
, file
, fd
);
643 if (0 == re
&& MANDOCLEVEL_FATAL
> curp
->file_status
)
646 if (STDIN_FILENO
!= fd
&& -1 == close(fd
))
653 mparse_readfd(struct mparse
*curp
, int fd
, const char *file
)
656 mparse_readfd_r(curp
, fd
, file
, 0);
657 return(curp
->file_status
);
661 mparse_alloc(enum mparset inttype
, enum mandoclevel wlevel
, mandocmsg mmsg
, void *arg
)
665 curp
= mandoc_calloc(1, sizeof(struct mparse
));
667 curp
->wlevel
= wlevel
;
670 curp
->inttype
= inttype
;
672 curp
->roff
= roff_alloc(&curp
->regs
, curp
);
677 mparse_reset(struct mparse
*curp
)
680 memset(&curp
->regs
, 0, sizeof(struct regset
));
682 roff_reset(curp
->roff
);
685 mdoc_reset(curp
->mdoc
);
687 man_reset(curp
->man
);
689 curp
->file_status
= MANDOCLEVEL_OK
;
695 mparse_free(struct mparse
*curp
)
699 mdoc_free(curp
->pmdoc
);
701 man_free(curp
->pman
);
703 roff_free(curp
->roff
);
709 mparse_result(struct mparse
*curp
, struct mdoc
**mdoc
, struct man
**man
)
717 mandoc_vmsg(enum mandocerr t
, struct mparse
*m
,
718 int ln
, int pos
, const char *fmt
, ...)
724 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
727 mandoc_msg(t
, m
, ln
, pos
, buf
);
731 mandoc_msg(enum mandocerr er
, struct mparse
*m
,
732 int ln
, int col
, const char *msg
)
734 enum mandoclevel level
;
736 level
= MANDOCLEVEL_FATAL
;
737 while (er
< mandoclimits
[level
])
740 if (level
< m
->wlevel
)
743 (*m
->mmsg
)(er
, level
, m
->file
, ln
, col
, msg
);
745 if (m
->file_status
< level
)
746 m
->file_status
= level
;
750 mparse_strerror(enum mandocerr er
)
753 return(mandocerrs
[er
]);
757 mparse_strlevel(enum mandoclevel lvl
)
759 return(mandoclevels
[lvl
]);