]>
git.cameronkatri.com Git - mandoc.git/blob - read.c
e516cdaffc6f006d958a92697b67aeeb17f0f675
1 /* $Id: read.c,v 1.150 2016/07/19 16:22:52 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2016 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>
42 #include "mandoc_aux.h"
47 #include "libmandoc.h"
50 #define REPARSE_LIMIT 1000
53 struct roff_man
*man
; /* man parser */
54 struct roff
*roff
; /* roff parser (!NULL) */
55 char *sodest
; /* filename pointed to by .so */
56 const char *file
; /* filename of current input file */
57 struct buf
*primary
; /* buffer currently being parsed */
58 struct buf
*secondary
; /* preprocessed copy of input */
59 const char *defos
; /* default operating system */
60 mandocmsg mmsg
; /* warning/error message handler */
61 enum mandoclevel file_status
; /* status of current parse */
62 enum mandoclevel wlevel
; /* ignore messages below this */
63 int options
; /* parser options */
64 int gzip
; /* current input file is gzipped */
65 int filenc
; /* encoding of the current file */
66 int reparse_count
; /* finite interp. stack */
67 int line
; /* line number in the file */
70 static void choose_parser(struct mparse
*);
71 static void resize_buf(struct buf
*, size_t);
72 static void mparse_buf_r(struct mparse
*, struct buf
, size_t, int);
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 "missing date, using today's date",
101 "cannot parse date, using it verbatim",
102 "missing Os macro, using \"\"",
103 "duplicate prologue macro",
104 "late prologue macro",
105 "skipping late title macro",
106 "prologue macros out of order",
108 /* related to document structure */
109 ".so is fragile, better use ln(1)",
111 "content before first section header",
112 "first section is not \"NAME\"",
113 "NAME section without name",
114 "NAME section without description",
115 "description not at the end of NAME",
116 "bad NAME section content",
117 "missing description line, using \"\"",
118 "sections out of conventional order",
119 "duplicate section title",
120 "unexpected section",
122 "unusual Xr punctuation",
123 "AUTHORS section without An macro",
125 /* related to macros and nesting */
127 "macro neither callable nor escaped",
128 "skipping paragraph macro",
129 "moving paragraph macro out of list",
130 "skipping no-space macro",
131 "blocks badly nested",
132 "nested displays are not portable",
133 "moving content out of list",
134 "fill mode already enabled, skipping",
135 "fill mode already disabled, skipping",
138 /* related to missing macro arguments */
139 "skipping empty request",
140 "conditional request controls empty scope",
141 "skipping empty macro",
143 "empty argument, using 0n",
144 "missing display type, using -ragged",
145 "list type is not the first argument",
146 "missing -width in -tag list, using 8n",
147 "missing utility name, using \"\"",
148 "missing function name, using \"\"",
149 "empty head in list item",
151 "missing font type, using \\fR",
152 "unknown font type, using \\fR",
153 "nothing follows prefix",
154 "empty reference block",
155 "missing -std argument, adding it",
156 "missing option string, using \"\"",
157 "missing resource identifier, using \"\"",
158 "missing eqn box, using \"\"",
160 /* related to bad macro arguments */
161 "unterminated quoted argument",
162 "duplicate argument",
163 "skipping duplicate argument",
164 "skipping duplicate display type",
165 "skipping duplicate list type",
166 "skipping -width argument",
167 "wrong number of cells",
168 "unknown AT&T UNIX version",
169 "comma in function argument",
170 "parenthesis in function name",
171 "invalid content in Rs block",
172 "invalid Boolean argument",
173 "unknown font, skipping request",
174 "odd number of characters in request",
176 /* related to plain text */
177 "blank line in fill mode, using .sp",
178 "tab in filled text",
179 "whitespace at end of input line",
181 "invalid escape sequence",
182 "undefined string, using \"\"",
184 /* related to tables */
185 "tbl line starts with span",
186 "tbl column starts with span",
187 "skipping vertical bar in tbl layout",
191 /* related to tables */
192 "non-alphabetic character in tbl options",
193 "skipping unknown tbl option",
194 "missing tbl option argument",
195 "wrong tbl option argument size",
197 "invalid character in tbl layout",
198 "unmatched parenthesis in tbl layout",
199 "tbl without any data cells",
200 "ignoring data in spanned tbl cell",
201 "ignoring extra tbl data cells",
202 "data block open at end of tbl",
204 /* related to document structure and macros */
206 "input stack limit exceeded, infinite loop?",
207 "skipping bad character",
208 "skipping unknown macro",
209 "skipping insecure request",
210 "skipping item outside list",
211 "skipping column outside column list",
212 "skipping end of block that is not open",
213 "fewer RS blocks open, skipping",
214 "inserting missing end of block",
215 "appending missing end of block",
217 /* related to request and macro arguments */
218 "escaped character not allowed in a name",
219 "NOT IMPLEMENTED: Bd -file",
220 "skipping display without arguments",
221 "missing list type, using -item",
222 "missing manual name, using \"\"",
223 "uname(3) system call failed, using UNKNOWN",
224 "unknown standard specifier",
225 "skipping request without numeric argument",
226 "NOT IMPLEMENTED: .so with absolute path or \"..\"",
227 ".so request failed",
228 "skipping all arguments",
229 "skipping excess arguments",
232 "unsupported feature",
234 "unsupported control character",
235 "unsupported roff request",
236 "eqn delim option in tbl",
237 "unsupported tbl layout modifier",
238 "ignoring macro in table",
241 static const char * const mandoclevels
[MANDOCLEVEL_MAX
] = {
253 resize_buf(struct buf
*buf
, size_t initial
)
256 buf
->sz
= buf
->sz
> initial
/2 ? 2 * buf
->sz
: initial
;
257 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
261 choose_parser(struct mparse
*curp
)
267 * If neither command line arguments -mdoc or -man select
268 * a parser nor the roff parser found a .Dd or .TH macro
269 * yet, look ahead in the main input buffer.
272 if ((format
= roff_getformat(curp
->roff
)) == 0) {
273 cp
= curp
->primary
->buf
;
274 ep
= cp
+ curp
->primary
->sz
;
276 if (*cp
== '.' || *cp
== '\'') {
278 if (cp
[0] == 'D' && cp
[1] == 'd') {
279 format
= MPARSE_MDOC
;
282 if (cp
[0] == 'T' && cp
[1] == 'H') {
287 cp
= memchr(cp
, '\n', ep
- cp
);
294 if (format
== MPARSE_MDOC
) {
296 curp
->man
->macroset
= MACROSET_MDOC
;
297 curp
->man
->first
->tok
= TOKEN_NONE
;
300 curp
->man
->macroset
= MACROSET_MAN
;
301 curp
->man
->first
->tok
= TOKEN_NONE
;
306 * Main parse routine for a buffer.
307 * It assumes encoding and line numbering are already set up.
308 * It can recurse directly (for invocations of user-defined
309 * macros, inline equations, and input line traps)
310 * and indirectly (for .so file inclusion).
313 mparse_buf_r(struct mparse
*curp
, struct buf blk
, size_t i
, int start
)
315 const struct tbl_span
*span
;
317 const char *save_file
;
319 size_t pos
; /* byte number in the ln buffer */
322 int lnn
; /* line number in the real file */
326 memset(&ln
, 0, sizeof(ln
));
332 if (0 == pos
&& '\0' == blk
.buf
[i
])
337 curp
->reparse_count
= 0;
340 curp
->filenc
& MPARSE_UTF8
&&
341 curp
->filenc
& MPARSE_LATIN1
)
342 curp
->filenc
= preconv_cue(&blk
, i
);
345 while (i
< blk
.sz
&& (start
|| blk
.buf
[i
] != '\0')) {
348 * When finding an unescaped newline character,
349 * leave the character loop to process the line.
350 * Skip a preceding carriage return, if any.
353 if ('\r' == blk
.buf
[i
] && i
+ 1 < blk
.sz
&&
354 '\n' == blk
.buf
[i
+ 1])
356 if ('\n' == blk
.buf
[i
]) {
363 * Make sure we have space for the worst
364 * case of 11 bytes: "\\[u10ffff]\0"
367 if (pos
+ 11 > ln
.sz
)
368 resize_buf(&ln
, 256);
371 * Encode 8-bit input.
376 if ( ! (curp
->filenc
&& preconv_encode(
377 &blk
, &i
, &ln
, &pos
, &curp
->filenc
))) {
378 mandoc_vmsg(MANDOCERR_CHAR_BAD
, curp
,
379 curp
->line
, pos
, "0x%x", c
);
387 * Exclude control characters.
390 if (c
== 0x7f || (c
< 0x20 && c
!= 0x09)) {
391 mandoc_vmsg(c
== 0x00 || c
== 0x04 ||
392 c
> 0x0a ? MANDOCERR_CHAR_BAD
:
393 MANDOCERR_CHAR_UNSUPP
,
394 curp
, curp
->line
, pos
, "0x%x", c
);
401 /* Trailing backslash = a plain char. */
403 if (blk
.buf
[i
] != '\\' || i
+ 1 == blk
.sz
) {
404 ln
.buf
[pos
++] = blk
.buf
[i
++];
409 * Found escape and at least one other character.
410 * When it's a newline character, skip it.
411 * When there is a carriage return in between,
412 * skip that one as well.
415 if ('\r' == blk
.buf
[i
+ 1] && i
+ 2 < blk
.sz
&&
416 '\n' == blk
.buf
[i
+ 2])
418 if ('\n' == blk
.buf
[i
+ 1]) {
424 if ('"' == blk
.buf
[i
+ 1] || '#' == blk
.buf
[i
+ 1]) {
426 /* Comment, skip to end of line */
427 for (; i
< blk
.sz
; ++i
) {
428 if ('\n' == blk
.buf
[i
]) {
435 /* Backout trailing whitespaces */
436 for (; pos
> 0; --pos
) {
437 if (ln
.buf
[pos
- 1] != ' ')
439 if (pos
> 2 && ln
.buf
[pos
- 2] == '\\')
445 /* Catch escaped bogus characters. */
447 c
= (unsigned char) blk
.buf
[i
+1];
449 if ( ! (isascii(c
) &&
450 (isgraph(c
) || isblank(c
)))) {
451 mandoc_vmsg(MANDOCERR_CHAR_BAD
, curp
,
452 curp
->line
, pos
, "0x%x", c
);
458 /* Some other escape sequence, copy & cont. */
460 ln
.buf
[pos
++] = blk
.buf
[i
++];
461 ln
.buf
[pos
++] = blk
.buf
[i
++];
465 resize_buf(&ln
, 256);
470 * A significant amount of complexity is contained by
471 * the roff preprocessor. It's line-oriented but can be
472 * expressed on one line, so we need at times to
473 * readjust our starting point and re-run it. The roff
474 * preprocessor can also readjust the buffers with new
475 * data, so we pass them in wholesale.
481 * Maintain a lookaside buffer of all parsed lines. We
482 * only do this if mparse_keep() has been invoked (the
483 * buffer may be accessed with mparse_getkeep()).
486 if (curp
->secondary
) {
487 curp
->secondary
->buf
= mandoc_realloc(
488 curp
->secondary
->buf
,
489 curp
->secondary
->sz
+ pos
+ 2);
490 memcpy(curp
->secondary
->buf
+
493 curp
->secondary
->sz
+= pos
;
495 [curp
->secondary
->sz
] = '\n';
496 curp
->secondary
->sz
++;
498 [curp
->secondary
->sz
] = '\0';
501 rr
= roff_parseln(curp
->roff
, curp
->line
, &ln
, &of
);
505 if (REPARSE_LIMIT
>= ++curp
->reparse_count
)
506 mparse_buf_r(curp
, ln
, of
, 0);
508 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
,
509 curp
->line
, pos
, NULL
);
513 pos
= strlen(ln
.buf
);
521 if ( ! (curp
->options
& MPARSE_SO
) &&
522 (i
>= blk
.sz
|| blk
.buf
[i
] == '\0')) {
523 curp
->sodest
= mandoc_strdup(ln
.buf
+ of
);
528 * We remove `so' clauses from our lookaside
529 * buffer because we're going to descend into
530 * the file recursively.
533 curp
->secondary
->sz
-= pos
+ 1;
534 save_file
= curp
->file
;
535 if ((fd
= mparse_open(curp
, ln
.buf
+ of
)) != -1) {
536 mparse_readfd(curp
, fd
, ln
.buf
+ of
);
538 curp
->file
= save_file
;
540 curp
->file
= save_file
;
541 mandoc_vmsg(MANDOCERR_SO_FAIL
,
542 curp
, curp
->line
, pos
,
543 ".so %s", ln
.buf
+ of
);
544 ln
.sz
= mandoc_asprintf(&cp
,
545 ".sp\nSee the file %s.\n.sp",
550 mparse_buf_r(curp
, ln
, of
, 0);
558 if (curp
->man
->macroset
== MACROSET_NONE
)
562 * Lastly, push down into the parsers themselves.
563 * If libroff returns ROFF_TBL, then add it to the
564 * currently open parse. Since we only get here if
565 * there does exist data (see tbl_data.c), we're
566 * guaranteed that something's been allocated.
567 * Do the same for ROFF_EQN.
571 while ((span
= roff_span(curp
->roff
)) != NULL
)
572 roff_addtbl(curp
->man
, span
);
573 else if (rr
== ROFF_EQN
)
574 roff_addeqn(curp
->man
, roff_eqn(curp
->roff
));
575 else if ((curp
->man
->macroset
== MACROSET_MDOC
?
576 mdoc_parseln(curp
->man
, curp
->line
, ln
.buf
, of
) :
577 man_parseln(curp
->man
, curp
->line
, ln
.buf
, of
)) == 2)
580 /* Temporary buffers typically are not full. */
582 if (0 == start
&& '\0' == blk
.buf
[i
])
585 /* Start the next input line. */
594 read_whole_file(struct mparse
*curp
, const char *file
, int fd
,
595 struct buf
*fb
, int *with_mmap
)
604 if (fstat(fd
, &st
) == -1)
605 err((int)MANDOCLEVEL_SYSERR
, "%s", file
);
608 * If we're a regular file, try just reading in the whole entry
609 * via mmap(). This is faster than reading it into blocks, and
610 * since each file is only a few bytes to begin with, I'm not
611 * concerned that this is going to tank any machines.
614 if (curp
->gzip
== 0 && S_ISREG(st
.st_mode
)) {
615 if (st
.st_size
> 0x7fffffff) {
616 mandoc_msg(MANDOCERR_TOOLARGE
, curp
, 0, 0, NULL
);
620 fb
->sz
= (size_t)st
.st_size
;
621 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
, MAP_SHARED
, fd
, 0);
622 if (fb
->buf
!= MAP_FAILED
)
628 if ((gz
= gzdopen(fd
, "rb")) == NULL
)
629 err((int)MANDOCLEVEL_SYSERR
, "%s", file
);
634 * If this isn't a regular file (like, say, stdin), then we must
635 * go the old way and just read things in bit by bit.
644 if (fb
->sz
== (1U << 31)) {
645 mandoc_msg(MANDOCERR_TOOLARGE
, curp
,
649 resize_buf(fb
, 65536);
652 gzread(gz
, fb
->buf
+ (int)off
, fb
->sz
- off
) :
653 read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
659 err((int)MANDOCLEVEL_SYSERR
, "%s", file
);
669 mparse_end(struct mparse
*curp
)
671 if (curp
->man
->macroset
== MACROSET_NONE
)
672 curp
->man
->macroset
= MACROSET_MAN
;
673 if (curp
->man
->macroset
== MACROSET_MDOC
)
674 mdoc_endparse(curp
->man
);
676 man_endparse(curp
->man
);
677 roff_endparse(curp
->roff
);
681 mparse_parse_buffer(struct mparse
*curp
, struct buf blk
, const char *file
)
683 struct buf
*svprimary
;
686 static int recursion_depth
;
688 if (64 < recursion_depth
) {
689 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
, curp
->line
, 0, NULL
);
693 /* Line number is per-file. */
696 svprimary
= curp
->primary
;
697 curp
->primary
= &blk
;
701 /* Skip an UTF-8 byte order mark. */
702 if (curp
->filenc
& MPARSE_UTF8
&& blk
.sz
> 2 &&
703 (unsigned char)blk
.buf
[0] == 0xef &&
704 (unsigned char)blk
.buf
[1] == 0xbb &&
705 (unsigned char)blk
.buf
[2] == 0xbf) {
707 curp
->filenc
&= ~MPARSE_LATIN1
;
711 mparse_buf_r(curp
, blk
, offset
, 1);
713 if (--recursion_depth
== 0)
716 curp
->primary
= svprimary
;
721 mparse_readmem(struct mparse
*curp
, void *buf
, size_t len
,
729 mparse_parse_buffer(curp
, blk
, file
);
730 return curp
->file_status
;
734 * Read the whole file into memory and call the parsers.
735 * Called recursively when an .so request is encountered.
738 mparse_readfd(struct mparse
*curp
, int fd
, const char *file
)
744 if (read_whole_file(curp
, file
, fd
, &blk
, &with_mmap
)) {
745 save_filenc
= curp
->filenc
;
746 curp
->filenc
= curp
->options
&
747 (MPARSE_UTF8
| MPARSE_LATIN1
);
748 mparse_parse_buffer(curp
, blk
, file
);
749 curp
->filenc
= save_filenc
;
752 munmap(blk
.buf
, blk
.sz
);
757 return curp
->file_status
;
761 mparse_open(struct mparse
*curp
, const char *file
)
767 cp
= strrchr(file
, '.');
768 curp
->gzip
= (cp
!= NULL
&& ! strcmp(cp
+ 1, "gz"));
770 /* First try to use the filename as it is. */
772 if ((fd
= open(file
, O_RDONLY
)) != -1)
776 * If that doesn't work and the filename doesn't
777 * already end in .gz, try appending .gz.
781 mandoc_asprintf(&cp
, "%s.gz", file
);
782 fd
= open(cp
, O_RDONLY
);
790 /* Neither worked, give up. */
792 mandoc_msg(MANDOCERR_FILE
, curp
, 0, 0, strerror(errno
));
797 mparse_alloc(int options
, enum mandoclevel wlevel
, mandocmsg mmsg
,
802 curp
= mandoc_calloc(1, sizeof(struct mparse
));
804 curp
->options
= options
;
805 curp
->wlevel
= wlevel
;
809 curp
->roff
= roff_alloc(curp
, options
);
810 curp
->man
= roff_man_alloc( curp
->roff
, curp
, curp
->defos
,
811 curp
->options
& MPARSE_QUICK
? 1 : 0);
812 if (curp
->options
& MPARSE_MDOC
) {
814 curp
->man
->macroset
= MACROSET_MDOC
;
815 } else if (curp
->options
& MPARSE_MAN
) {
817 curp
->man
->macroset
= MACROSET_MAN
;
819 curp
->man
->first
->tok
= TOKEN_NONE
;
824 mparse_reset(struct mparse
*curp
)
826 roff_reset(curp
->roff
);
827 roff_man_reset(curp
->man
);
829 curp
->secondary
->sz
= 0;
831 curp
->file_status
= MANDOCLEVEL_OK
;
838 mparse_free(struct mparse
*curp
)
841 roff_man_free(curp
->man
);
843 roff_free(curp
->roff
);
845 free(curp
->secondary
->buf
);
847 free(curp
->secondary
);
853 mparse_result(struct mparse
*curp
, struct roff_man
**man
,
857 if (sodest
&& NULL
!= (*sodest
= curp
->sodest
)) {
866 mandoc_vmsg(enum mandocerr t
, struct mparse
*m
,
867 int ln
, int pos
, const char *fmt
, ...)
873 (void)vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
876 mandoc_msg(t
, m
, ln
, pos
, buf
);
880 mandoc_msg(enum mandocerr er
, struct mparse
*m
,
881 int ln
, int col
, const char *msg
)
883 enum mandoclevel level
;
885 level
= MANDOCLEVEL_UNSUPP
;
886 while (er
< mandoclimits
[level
])
889 if (level
< m
->wlevel
&& er
!= MANDOCERR_FILE
)
893 (*m
->mmsg
)(er
, level
, m
->file
, ln
, col
, msg
);
895 if (m
->file_status
< level
)
896 m
->file_status
= level
;
900 mparse_strerror(enum mandocerr er
)
903 return mandocerrs
[er
];
907 mparse_strlevel(enum mandoclevel lvl
)
909 return mandoclevels
[lvl
];
913 mparse_keep(struct mparse
*p
)
916 assert(NULL
== p
->secondary
);
917 p
->secondary
= mandoc_calloc(1, sizeof(struct buf
));
921 mparse_getkeep(const struct mparse
*p
)
924 assert(p
->secondary
);
925 return p
->secondary
->sz
? p
->secondary
->buf
: NULL
;