]>
git.cameronkatri.com Git - mandoc.git/blob - read.c
f0cf5b0c15a351a934d6877e2004c3ab10609e2c
1 /* $Id: read.c,v 1.172 2017/06/07 23:29:48 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2017 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>
39 #include "mandoc_aux.h"
44 #include "libmandoc.h"
47 #define REPARSE_LIMIT 1000
50 struct roff
*roff
; /* roff parser (!NULL) */
51 struct roff_man
*man
; /* man parser */
52 char *sodest
; /* filename pointed to by .so */
53 const char *file
; /* filename of current input file */
54 struct buf
*primary
; /* buffer currently being parsed */
55 struct buf
*secondary
; /* preprocessed copy of input */
56 const char *defos
; /* default operating system */
57 mandocmsg mmsg
; /* warning/error message handler */
58 enum mandoclevel file_status
; /* status of current parse */
59 enum mandoclevel wlevel
; /* ignore messages below this */
60 int options
; /* parser options */
61 int gzip
; /* current input file is gzipped */
62 int filenc
; /* encoding of the current file */
63 int reparse_count
; /* finite interp. stack */
64 int line
; /* line number in the file */
67 static void choose_parser(struct mparse
*);
68 static void resize_buf(struct buf
*, size_t);
69 static int mparse_buf_r(struct mparse
*, struct buf
, size_t, int);
70 static int read_whole_file(struct mparse
*, const char *, int,
72 static void mparse_end(struct mparse
*);
73 static void mparse_parse_buffer(struct mparse
*, struct buf
,
76 static const enum mandocerr mandoclimits
[MANDOCLEVEL_MAX
] = {
86 static const char * const mandocerrs
[MANDOCERR_MAX
] = {
89 "generic style suggestion",
92 "consider using OS macro",
93 "errnos out of order",
95 "description line ends with a full stop",
99 /* related to the prologue */
100 "missing manual title, using UNTITLED",
101 "missing manual title, using \"\"",
102 "lower case character in document title",
103 "missing manual section, using \"\"",
104 "unknown manual section",
105 "missing date, using today's date",
106 "cannot parse date, using it verbatim",
107 "missing Os macro, using \"\"",
108 "duplicate prologue macro",
109 "late prologue macro",
110 "skipping late title macro",
111 "prologue macros out of order",
113 /* related to document structure */
114 ".so is fragile, better use ln(1)",
116 "content before first section header",
117 "first section is not \"NAME\"",
118 "NAME section without Nm before Nd",
119 "NAME section without description",
120 "description not at the end of NAME",
121 "bad NAME section content",
122 "missing comma before name",
123 "missing description line, using \"\"",
124 "description line outside NAME section",
125 "sections out of conventional order",
126 "duplicate section title",
127 "unexpected section",
129 "unusual Xr punctuation",
130 "AUTHORS section without An macro",
132 /* related to macros and nesting */
134 "macro neither callable nor escaped",
135 "skipping paragraph macro",
136 "moving paragraph macro out of list",
137 "skipping no-space macro",
138 "blocks badly nested",
139 "nested displays are not portable",
140 "moving content out of list",
141 "fill mode already enabled, skipping",
142 "fill mode already disabled, skipping",
144 "skipping blank line in line scope",
146 /* related to missing macro arguments */
147 "skipping empty request",
148 "conditional request controls empty scope",
149 "skipping empty macro",
151 "empty argument, using 0n",
152 "missing display type, using -ragged",
153 "list type is not the first argument",
154 "missing -width in -tag list, using 6n",
155 "missing utility name, using \"\"",
156 "missing function name, using \"\"",
157 "empty head in list item",
159 "missing font type, using \\fR",
160 "unknown font type, using \\fR",
161 "nothing follows prefix",
162 "empty reference block",
163 "missing section argument",
164 "missing -std argument, adding it",
165 "missing option string, using \"\"",
166 "missing resource identifier, using \"\"",
167 "missing eqn box, using \"\"",
169 /* related to bad macro arguments */
170 "unterminated quoted argument",
171 "duplicate argument",
172 "skipping duplicate argument",
173 "skipping duplicate display type",
174 "skipping duplicate list type",
175 "skipping -width argument",
176 "wrong number of cells",
177 "unknown AT&T UNIX version",
178 "comma in function argument",
179 "parenthesis in function name",
180 "invalid content in Rs block",
181 "invalid Boolean argument",
182 "unknown font, skipping request",
183 "odd number of characters in request",
185 /* related to plain text */
186 "blank line in fill mode, using .sp",
187 "tab in filled text",
188 "whitespace at end of input line",
189 "new sentence, new line",
191 "invalid escape sequence",
192 "undefined string, using \"\"",
194 /* related to tables */
195 "tbl line starts with span",
196 "tbl column starts with span",
197 "skipping vertical bar in tbl layout",
201 /* related to tables */
202 "non-alphabetic character in tbl options",
203 "skipping unknown tbl option",
204 "missing tbl option argument",
205 "wrong tbl option argument size",
207 "invalid character in tbl layout",
208 "unmatched parenthesis in tbl layout",
209 "tbl without any data cells",
210 "ignoring data in spanned tbl cell",
211 "ignoring extra tbl data cells",
212 "data block open at end of tbl",
214 /* related to document structure and macros */
216 "input stack limit exceeded, infinite loop?",
217 "skipping bad character",
218 "skipping unknown macro",
219 "skipping insecure request",
220 "skipping item outside list",
221 "skipping column outside column list",
222 "skipping end of block that is not open",
223 "fewer RS blocks open, skipping",
224 "inserting missing end of block",
225 "appending missing end of block",
227 /* related to request and macro arguments */
228 "escaped character not allowed in a name",
229 "NOT IMPLEMENTED: Bd -file",
230 "skipping display without arguments",
231 "missing list type, using -item",
232 "argument is not numeric, using 1",
233 "missing manual name, using \"\"",
234 "uname(3) system call failed, using UNKNOWN",
235 "unknown standard specifier",
236 "skipping request without numeric argument",
237 "NOT IMPLEMENTED: .so with absolute path or \"..\"",
238 ".so request failed",
239 "skipping all arguments",
240 "skipping excess arguments",
243 "unsupported feature",
245 "unsupported control character",
246 "unsupported roff request",
247 "eqn delim option in tbl",
248 "unsupported tbl layout modifier",
249 "ignoring macro in table",
252 static const char * const mandoclevels
[MANDOCLEVEL_MAX
] = {
264 resize_buf(struct buf
*buf
, size_t initial
)
267 buf
->sz
= buf
->sz
> initial
/2 ? 2 * buf
->sz
: initial
;
268 buf
->buf
= mandoc_realloc(buf
->buf
, buf
->sz
);
272 choose_parser(struct mparse
*curp
)
278 * If neither command line arguments -mdoc or -man select
279 * a parser nor the roff parser found a .Dd or .TH macro
280 * yet, look ahead in the main input buffer.
283 if ((format
= roff_getformat(curp
->roff
)) == 0) {
284 cp
= curp
->primary
->buf
;
285 ep
= cp
+ curp
->primary
->sz
;
287 if (*cp
== '.' || *cp
== '\'') {
289 if (cp
[0] == 'D' && cp
[1] == 'd') {
290 format
= MPARSE_MDOC
;
293 if (cp
[0] == 'T' && cp
[1] == 'H') {
298 cp
= memchr(cp
, '\n', ep
- cp
);
305 if (format
== MPARSE_MDOC
) {
306 curp
->man
->macroset
= MACROSET_MDOC
;
307 if (curp
->man
->mdocmac
== NULL
)
308 curp
->man
->mdocmac
= roffhash_alloc(MDOC_Dd
, MDOC_MAX
);
310 curp
->man
->macroset
= MACROSET_MAN
;
311 if (curp
->man
->manmac
== NULL
)
312 curp
->man
->manmac
= roffhash_alloc(MAN_TH
, MAN_MAX
);
314 curp
->man
->first
->tok
= TOKEN_NONE
;
318 * Main parse routine for a buffer.
319 * It assumes encoding and line numbering are already set up.
320 * It can recurse directly (for invocations of user-defined
321 * macros, inline equations, and input line traps)
322 * and indirectly (for .so file inclusion).
325 mparse_buf_r(struct mparse
*curp
, struct buf blk
, size_t i
, int start
)
327 const struct tbl_span
*span
;
329 const char *save_file
;
331 size_t pos
; /* byte number in the ln buffer */
334 int lnn
; /* line number in the real file */
338 memset(&ln
, 0, sizeof(ln
));
344 if (0 == pos
&& '\0' == blk
.buf
[i
])
349 curp
->reparse_count
= 0;
352 curp
->filenc
& MPARSE_UTF8
&&
353 curp
->filenc
& MPARSE_LATIN1
)
354 curp
->filenc
= preconv_cue(&blk
, i
);
357 while (i
< blk
.sz
&& (start
|| blk
.buf
[i
] != '\0')) {
360 * When finding an unescaped newline character,
361 * leave the character loop to process the line.
362 * Skip a preceding carriage return, if any.
365 if ('\r' == blk
.buf
[i
] && i
+ 1 < blk
.sz
&&
366 '\n' == blk
.buf
[i
+ 1])
368 if ('\n' == blk
.buf
[i
]) {
375 * Make sure we have space for the worst
376 * case of 11 bytes: "\\[u10ffff]\0"
379 if (pos
+ 11 > ln
.sz
)
380 resize_buf(&ln
, 256);
383 * Encode 8-bit input.
388 if ( ! (curp
->filenc
&& preconv_encode(
389 &blk
, &i
, &ln
, &pos
, &curp
->filenc
))) {
390 mandoc_vmsg(MANDOCERR_CHAR_BAD
, curp
,
391 curp
->line
, pos
, "0x%x", c
);
399 * Exclude control characters.
402 if (c
== 0x7f || (c
< 0x20 && c
!= 0x09)) {
403 mandoc_vmsg(c
== 0x00 || c
== 0x04 ||
404 c
> 0x0a ? MANDOCERR_CHAR_BAD
:
405 MANDOCERR_CHAR_UNSUPP
,
406 curp
, curp
->line
, pos
, "0x%x", c
);
413 ln
.buf
[pos
++] = blk
.buf
[i
++];
416 if (pos
+ 1 >= ln
.sz
)
417 resize_buf(&ln
, 256);
419 if (i
== blk
.sz
|| blk
.buf
[i
] == '\0')
420 ln
.buf
[pos
++] = '\n';
424 * A significant amount of complexity is contained by
425 * the roff preprocessor. It's line-oriented but can be
426 * expressed on one line, so we need at times to
427 * readjust our starting point and re-run it. The roff
428 * preprocessor can also readjust the buffers with new
429 * data, so we pass them in wholesale.
435 * Maintain a lookaside buffer of all parsed lines. We
436 * only do this if mparse_keep() has been invoked (the
437 * buffer may be accessed with mparse_getkeep()).
440 if (curp
->secondary
) {
441 curp
->secondary
->buf
= mandoc_realloc(
442 curp
->secondary
->buf
,
443 curp
->secondary
->sz
+ pos
+ 2);
444 memcpy(curp
->secondary
->buf
+
447 curp
->secondary
->sz
+= pos
;
449 [curp
->secondary
->sz
] = '\n';
450 curp
->secondary
->sz
++;
452 [curp
->secondary
->sz
] = '\0';
455 rr
= roff_parseln(curp
->roff
, curp
->line
, &ln
, &of
);
459 if (++curp
->reparse_count
> REPARSE_LIMIT
)
460 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
,
461 curp
->line
, pos
, NULL
);
462 else if (mparse_buf_r(curp
, ln
, of
, 0) == 1 ||
470 pos
= strlen(ln
.buf
);
478 if ( ! (curp
->options
& MPARSE_SO
) &&
479 (i
>= blk
.sz
|| blk
.buf
[i
] == '\0')) {
480 curp
->sodest
= mandoc_strdup(ln
.buf
+ of
);
485 * We remove `so' clauses from our lookaside
486 * buffer because we're going to descend into
487 * the file recursively.
490 curp
->secondary
->sz
-= pos
+ 1;
491 save_file
= curp
->file
;
492 if ((fd
= mparse_open(curp
, ln
.buf
+ of
)) != -1) {
493 mparse_readfd(curp
, fd
, ln
.buf
+ of
);
495 curp
->file
= save_file
;
497 curp
->file
= save_file
;
498 mandoc_vmsg(MANDOCERR_SO_FAIL
,
499 curp
, curp
->line
, pos
,
500 ".so %s", ln
.buf
+ of
);
501 ln
.sz
= mandoc_asprintf(&cp
,
502 ".sp\nSee the file %s.\n.sp",
507 mparse_buf_r(curp
, ln
, of
, 0);
515 if (curp
->man
->macroset
== MACROSET_NONE
)
519 * Lastly, push down into the parsers themselves.
520 * If libroff returns ROFF_TBL, then add it to the
521 * currently open parse. Since we only get here if
522 * there does exist data (see tbl_data.c), we're
523 * guaranteed that something's been allocated.
524 * Do the same for ROFF_EQN.
528 while ((span
= roff_span(curp
->roff
)) != NULL
)
529 roff_addtbl(curp
->man
, span
);
530 else if (rr
== ROFF_EQN
)
531 roff_addeqn(curp
->man
, roff_eqn(curp
->roff
));
532 else if ((curp
->man
->macroset
== MACROSET_MDOC
?
533 mdoc_parseln(curp
->man
, curp
->line
, ln
.buf
, of
) :
534 man_parseln(curp
->man
, curp
->line
, ln
.buf
, of
)) == 2)
537 /* Temporary buffers typically are not full. */
539 if (0 == start
&& '\0' == blk
.buf
[i
])
542 /* Start the next input line. */
552 read_whole_file(struct mparse
*curp
, const char *file
, int fd
,
553 struct buf
*fb
, int *with_mmap
)
560 if (fstat(fd
, &st
) == -1)
561 err((int)MANDOCLEVEL_SYSERR
, "%s", file
);
564 * If we're a regular file, try just reading in the whole entry
565 * via mmap(). This is faster than reading it into blocks, and
566 * since each file is only a few bytes to begin with, I'm not
567 * concerned that this is going to tank any machines.
570 if (curp
->gzip
== 0 && S_ISREG(st
.st_mode
)) {
571 if (st
.st_size
> 0x7fffffff) {
572 mandoc_msg(MANDOCERR_TOOLARGE
, curp
, 0, 0, NULL
);
576 fb
->sz
= (size_t)st
.st_size
;
577 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
, MAP_SHARED
, fd
, 0);
578 if (fb
->buf
!= MAP_FAILED
)
583 if ((gz
= gzdopen(fd
, "rb")) == NULL
)
584 err((int)MANDOCLEVEL_SYSERR
, "%s", file
);
589 * If this isn't a regular file (like, say, stdin), then we must
590 * go the old way and just read things in bit by bit.
599 if (fb
->sz
== (1U << 31)) {
600 mandoc_msg(MANDOCERR_TOOLARGE
, curp
,
604 resize_buf(fb
, 65536);
607 gzread(gz
, fb
->buf
+ (int)off
, fb
->sz
- off
) :
608 read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
614 err((int)MANDOCLEVEL_SYSERR
, "%s", file
);
624 mparse_end(struct mparse
*curp
)
626 if (curp
->man
->macroset
== MACROSET_NONE
)
627 curp
->man
->macroset
= MACROSET_MAN
;
628 if (curp
->man
->macroset
== MACROSET_MDOC
)
629 mdoc_endparse(curp
->man
);
631 man_endparse(curp
->man
);
632 roff_endparse(curp
->roff
);
636 mparse_parse_buffer(struct mparse
*curp
, struct buf blk
, const char *file
)
638 struct buf
*svprimary
;
641 static int recursion_depth
;
643 if (64 < recursion_depth
) {
644 mandoc_msg(MANDOCERR_ROFFLOOP
, curp
, curp
->line
, 0, NULL
);
648 /* Line number is per-file. */
651 svprimary
= curp
->primary
;
652 curp
->primary
= &blk
;
656 /* Skip an UTF-8 byte order mark. */
657 if (curp
->filenc
& MPARSE_UTF8
&& blk
.sz
> 2 &&
658 (unsigned char)blk
.buf
[0] == 0xef &&
659 (unsigned char)blk
.buf
[1] == 0xbb &&
660 (unsigned char)blk
.buf
[2] == 0xbf) {
662 curp
->filenc
&= ~MPARSE_LATIN1
;
666 mparse_buf_r(curp
, blk
, offset
, 1);
668 if (--recursion_depth
== 0)
671 curp
->primary
= svprimary
;
676 mparse_readmem(struct mparse
*curp
, void *buf
, size_t len
,
684 mparse_parse_buffer(curp
, blk
, file
);
685 return curp
->file_status
;
689 * Read the whole file into memory and call the parsers.
690 * Called recursively when an .so request is encountered.
693 mparse_readfd(struct mparse
*curp
, int fd
, const char *file
)
699 if (read_whole_file(curp
, file
, fd
, &blk
, &with_mmap
)) {
700 save_filenc
= curp
->filenc
;
701 curp
->filenc
= curp
->options
&
702 (MPARSE_UTF8
| MPARSE_LATIN1
);
703 mparse_parse_buffer(curp
, blk
, file
);
704 curp
->filenc
= save_filenc
;
706 munmap(blk
.buf
, blk
.sz
);
710 return curp
->file_status
;
714 mparse_open(struct mparse
*curp
, const char *file
)
720 cp
= strrchr(file
, '.');
721 curp
->gzip
= (cp
!= NULL
&& ! strcmp(cp
+ 1, "gz"));
723 /* First try to use the filename as it is. */
725 if ((fd
= open(file
, O_RDONLY
)) != -1)
729 * If that doesn't work and the filename doesn't
730 * already end in .gz, try appending .gz.
734 mandoc_asprintf(&cp
, "%s.gz", file
);
735 fd
= open(cp
, O_RDONLY
);
743 /* Neither worked, give up. */
745 mandoc_msg(MANDOCERR_FILE
, curp
, 0, 0, strerror(errno
));
750 mparse_alloc(int options
, enum mandoclevel wlevel
, mandocmsg mmsg
,
755 curp
= mandoc_calloc(1, sizeof(struct mparse
));
757 curp
->options
= options
;
758 curp
->wlevel
= wlevel
;
762 curp
->roff
= roff_alloc(curp
, options
);
763 curp
->man
= roff_man_alloc( curp
->roff
, curp
, curp
->defos
,
764 curp
->options
& MPARSE_QUICK
? 1 : 0);
765 if (curp
->options
& MPARSE_MDOC
) {
766 curp
->man
->macroset
= MACROSET_MDOC
;
767 if (curp
->man
->mdocmac
== NULL
)
768 curp
->man
->mdocmac
= roffhash_alloc(MDOC_Dd
, MDOC_MAX
);
769 } else if (curp
->options
& MPARSE_MAN
) {
770 curp
->man
->macroset
= MACROSET_MAN
;
771 if (curp
->man
->manmac
== NULL
)
772 curp
->man
->manmac
= roffhash_alloc(MAN_TH
, MAN_MAX
);
774 curp
->man
->first
->tok
= TOKEN_NONE
;
779 mparse_reset(struct mparse
*curp
)
781 roff_reset(curp
->roff
);
782 roff_man_reset(curp
->man
);
788 curp
->secondary
->sz
= 0;
790 curp
->file_status
= MANDOCLEVEL_OK
;
795 mparse_free(struct mparse
*curp
)
798 roffhash_free(curp
->man
->mdocmac
);
799 roffhash_free(curp
->man
->manmac
);
800 roff_man_free(curp
->man
);
801 roff_free(curp
->roff
);
803 free(curp
->secondary
->buf
);
805 free(curp
->secondary
);
811 mparse_result(struct mparse
*curp
, struct roff_man
**man
,
815 if (sodest
&& NULL
!= (*sodest
= curp
->sodest
)) {
824 mparse_updaterc(struct mparse
*curp
, enum mandoclevel
*rc
)
826 if (curp
->file_status
> *rc
)
827 *rc
= curp
->file_status
;
831 mandoc_vmsg(enum mandocerr t
, struct mparse
*m
,
832 int ln
, int pos
, const char *fmt
, ...)
838 (void)vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
841 mandoc_msg(t
, m
, ln
, pos
, buf
);
845 mandoc_msg(enum mandocerr er
, struct mparse
*m
,
846 int ln
, int col
, const char *msg
)
848 enum mandoclevel level
;
850 level
= MANDOCLEVEL_UNSUPP
;
851 while (er
< mandoclimits
[level
])
854 if (level
< m
->wlevel
&& er
!= MANDOCERR_FILE
)
858 (*m
->mmsg
)(er
, level
, m
->file
, ln
, col
, msg
);
860 if (m
->file_status
< level
)
861 m
->file_status
= level
;
865 mparse_strerror(enum mandocerr er
)
868 return mandocerrs
[er
];
872 mparse_strlevel(enum mandoclevel lvl
)
874 return mandoclevels
[lvl
];
878 mparse_keep(struct mparse
*p
)
881 assert(NULL
== p
->secondary
);
882 p
->secondary
= mandoc_calloc(1, sizeof(struct buf
));
886 mparse_getkeep(const struct mparse
*p
)
889 assert(p
->secondary
);
890 return p
->secondary
->sz
? p
->secondary
->buf
: NULL
;