]> git.cameronkatri.com Git - mandoc.git/blob - read.c
e516cdaffc6f006d958a92697b67aeeb17f0f675
[mandoc.git] / read.c
1 /* $Id: read.c,v 1.150 2016/07/19 16:22:52 schwarze Exp $ */
2 /*
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>
6 *
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.
10 *
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.
18 */
19 #include "config.h"
20
21 #include <sys/types.h>
22 #if HAVE_MMAP
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #endif
26
27 #include <assert.h>
28 #include <ctype.h>
29 #if HAVE_ERR
30 #include <err.h>
31 #endif
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdarg.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <zlib.h>
41
42 #include "mandoc_aux.h"
43 #include "mandoc.h"
44 #include "roff.h"
45 #include "mdoc.h"
46 #include "man.h"
47 #include "libmandoc.h"
48 #include "roff_int.h"
49
50 #define REPARSE_LIMIT 1000
51
52 struct mparse {
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 */
68 };
69
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,
74 struct buf *, int *);
75 static void mparse_end(struct mparse *);
76 static void mparse_parse_buffer(struct mparse *, struct buf,
77 const char *);
78
79 static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = {
80 MANDOCERR_OK,
81 MANDOCERR_WARNING,
82 MANDOCERR_WARNING,
83 MANDOCERR_ERROR,
84 MANDOCERR_UNSUPP,
85 MANDOCERR_MAX,
86 MANDOCERR_MAX
87 };
88
89 static const char * const mandocerrs[MANDOCERR_MAX] = {
90 "ok",
91
92 "generic warning",
93
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",
107
108 /* related to document structure */
109 ".so is fragile, better use ln(1)",
110 "no document body",
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",
121 "unusual Xr order",
122 "unusual Xr punctuation",
123 "AUTHORS section without An macro",
124
125 /* related to macros and nesting */
126 "obsolete macro",
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",
136 "line scope broken",
137
138 /* related to missing macro arguments */
139 "skipping empty request",
140 "conditional request controls empty scope",
141 "skipping empty macro",
142 "empty block",
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",
150 "empty 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 \"\"",
159
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",
175
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",
180 "bad comment style",
181 "invalid escape sequence",
182 "undefined string, using \"\"",
183
184 /* related to tables */
185 "tbl line starts with span",
186 "tbl column starts with span",
187 "skipping vertical bar in tbl layout",
188
189 "generic error",
190
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",
196 "empty tbl layout",
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",
203
204 /* related to document structure and macros */
205 NULL,
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",
216
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",
230 "divide by zero",
231
232 "unsupported feature",
233 "input too large",
234 "unsupported control character",
235 "unsupported roff request",
236 "eqn delim option in tbl",
237 "unsupported tbl layout modifier",
238 "ignoring macro in table",
239 };
240
241 static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
242 "SUCCESS",
243 "RESERVED",
244 "WARNING",
245 "ERROR",
246 "UNSUPP",
247 "BADARG",
248 "SYSERR"
249 };
250
251
252 static void
253 resize_buf(struct buf *buf, size_t initial)
254 {
255
256 buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
257 buf->buf = mandoc_realloc(buf->buf, buf->sz);
258 }
259
260 static void
261 choose_parser(struct mparse *curp)
262 {
263 char *cp, *ep;
264 int format;
265
266 /*
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.
270 */
271
272 if ((format = roff_getformat(curp->roff)) == 0) {
273 cp = curp->primary->buf;
274 ep = cp + curp->primary->sz;
275 while (cp < ep) {
276 if (*cp == '.' || *cp == '\'') {
277 cp++;
278 if (cp[0] == 'D' && cp[1] == 'd') {
279 format = MPARSE_MDOC;
280 break;
281 }
282 if (cp[0] == 'T' && cp[1] == 'H') {
283 format = MPARSE_MAN;
284 break;
285 }
286 }
287 cp = memchr(cp, '\n', ep - cp);
288 if (cp == NULL)
289 break;
290 cp++;
291 }
292 }
293
294 if (format == MPARSE_MDOC) {
295 mdoc_hash_init();
296 curp->man->macroset = MACROSET_MDOC;
297 curp->man->first->tok = TOKEN_NONE;
298 } else {
299 man_hash_init();
300 curp->man->macroset = MACROSET_MAN;
301 curp->man->first->tok = TOKEN_NONE;
302 }
303 }
304
305 /*
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).
311 */
312 static void
313 mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
314 {
315 const struct tbl_span *span;
316 struct buf ln;
317 const char *save_file;
318 char *cp;
319 size_t pos; /* byte number in the ln buffer */
320 enum rofferr rr;
321 int of;
322 int lnn; /* line number in the real file */
323 int fd;
324 unsigned char c;
325
326 memset(&ln, 0, sizeof(ln));
327
328 lnn = curp->line;
329 pos = 0;
330
331 while (i < blk.sz) {
332 if (0 == pos && '\0' == blk.buf[i])
333 break;
334
335 if (start) {
336 curp->line = lnn;
337 curp->reparse_count = 0;
338
339 if (lnn < 3 &&
340 curp->filenc & MPARSE_UTF8 &&
341 curp->filenc & MPARSE_LATIN1)
342 curp->filenc = preconv_cue(&blk, i);
343 }
344
345 while (i < blk.sz && (start || blk.buf[i] != '\0')) {
346
347 /*
348 * When finding an unescaped newline character,
349 * leave the character loop to process the line.
350 * Skip a preceding carriage return, if any.
351 */
352
353 if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
354 '\n' == blk.buf[i + 1])
355 ++i;
356 if ('\n' == blk.buf[i]) {
357 ++i;
358 ++lnn;
359 break;
360 }
361
362 /*
363 * Make sure we have space for the worst
364 * case of 11 bytes: "\\[u10ffff]\0"
365 */
366
367 if (pos + 11 > ln.sz)
368 resize_buf(&ln, 256);
369
370 /*
371 * Encode 8-bit input.
372 */
373
374 c = blk.buf[i];
375 if (c & 0x80) {
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);
380 ln.buf[pos++] = '?';
381 i++;
382 }
383 continue;
384 }
385
386 /*
387 * Exclude control characters.
388 */
389
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);
395 i++;
396 if (c != '\r')
397 ln.buf[pos++] = '?';
398 continue;
399 }
400
401 /* Trailing backslash = a plain char. */
402
403 if (blk.buf[i] != '\\' || i + 1 == blk.sz) {
404 ln.buf[pos++] = blk.buf[i++];
405 continue;
406 }
407
408 /*
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.
413 */
414
415 if ('\r' == blk.buf[i + 1] && i + 2 < blk.sz &&
416 '\n' == blk.buf[i + 2])
417 ++i;
418 if ('\n' == blk.buf[i + 1]) {
419 i += 2;
420 ++lnn;
421 continue;
422 }
423
424 if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
425 i += 2;
426 /* Comment, skip to end of line */
427 for (; i < blk.sz; ++i) {
428 if ('\n' == blk.buf[i]) {
429 ++i;
430 ++lnn;
431 break;
432 }
433 }
434
435 /* Backout trailing whitespaces */
436 for (; pos > 0; --pos) {
437 if (ln.buf[pos - 1] != ' ')
438 break;
439 if (pos > 2 && ln.buf[pos - 2] == '\\')
440 break;
441 }
442 break;
443 }
444
445 /* Catch escaped bogus characters. */
446
447 c = (unsigned char) blk.buf[i+1];
448
449 if ( ! (isascii(c) &&
450 (isgraph(c) || isblank(c)))) {
451 mandoc_vmsg(MANDOCERR_CHAR_BAD, curp,
452 curp->line, pos, "0x%x", c);
453 i += 2;
454 ln.buf[pos++] = '?';
455 continue;
456 }
457
458 /* Some other escape sequence, copy & cont. */
459
460 ln.buf[pos++] = blk.buf[i++];
461 ln.buf[pos++] = blk.buf[i++];
462 }
463
464 if (pos >= ln.sz)
465 resize_buf(&ln, 256);
466
467 ln.buf[pos] = '\0';
468
469 /*
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.
476 */
477
478 of = 0;
479
480 /*
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()).
484 */
485
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 +
491 curp->secondary->sz,
492 ln.buf, pos);
493 curp->secondary->sz += pos;
494 curp->secondary->buf
495 [curp->secondary->sz] = '\n';
496 curp->secondary->sz++;
497 curp->secondary->buf
498 [curp->secondary->sz] = '\0';
499 }
500 rerun:
501 rr = roff_parseln(curp->roff, curp->line, &ln, &of);
502
503 switch (rr) {
504 case ROFF_REPARSE:
505 if (REPARSE_LIMIT >= ++curp->reparse_count)
506 mparse_buf_r(curp, ln, of, 0);
507 else
508 mandoc_msg(MANDOCERR_ROFFLOOP, curp,
509 curp->line, pos, NULL);
510 pos = 0;
511 continue;
512 case ROFF_APPEND:
513 pos = strlen(ln.buf);
514 continue;
515 case ROFF_RERUN:
516 goto rerun;
517 case ROFF_IGN:
518 pos = 0;
519 continue;
520 case ROFF_SO:
521 if ( ! (curp->options & MPARSE_SO) &&
522 (i >= blk.sz || blk.buf[i] == '\0')) {
523 curp->sodest = mandoc_strdup(ln.buf + of);
524 free(ln.buf);
525 return;
526 }
527 /*
528 * We remove `so' clauses from our lookaside
529 * buffer because we're going to descend into
530 * the file recursively.
531 */
532 if (curp->secondary)
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);
537 close(fd);
538 curp->file = save_file;
539 } else {
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",
546 ln.buf + of);
547 free(ln.buf);
548 ln.buf = cp;
549 of = 0;
550 mparse_buf_r(curp, ln, of, 0);
551 }
552 pos = 0;
553 continue;
554 default:
555 break;
556 }
557
558 if (curp->man->macroset == MACROSET_NONE)
559 choose_parser(curp);
560
561 /*
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.
568 */
569
570 if (rr == ROFF_TBL)
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)
578 break;
579
580 /* Temporary buffers typically are not full. */
581
582 if (0 == start && '\0' == blk.buf[i])
583 break;
584
585 /* Start the next input line. */
586
587 pos = 0;
588 }
589
590 free(ln.buf);
591 }
592
593 static int
594 read_whole_file(struct mparse *curp, const char *file, int fd,
595 struct buf *fb, int *with_mmap)
596 {
597 gzFile gz;
598 size_t off;
599 ssize_t ssz;
600
601 #if HAVE_MMAP
602 struct stat st;
603
604 if (fstat(fd, &st) == -1)
605 err((int)MANDOCLEVEL_SYSERR, "%s", file);
606
607 /*
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.
612 */
613
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);
617 return 0;
618 }
619 *with_mmap = 1;
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)
623 return 1;
624 }
625 #endif
626
627 if (curp->gzip) {
628 if ((gz = gzdopen(fd, "rb")) == NULL)
629 err((int)MANDOCLEVEL_SYSERR, "%s", file);
630 } else
631 gz = NULL;
632
633 /*
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.
636 */
637
638 *with_mmap = 0;
639 off = 0;
640 fb->sz = 0;
641 fb->buf = NULL;
642 for (;;) {
643 if (off == fb->sz) {
644 if (fb->sz == (1U << 31)) {
645 mandoc_msg(MANDOCERR_TOOLARGE, curp,
646 0, 0, NULL);
647 break;
648 }
649 resize_buf(fb, 65536);
650 }
651 ssz = curp->gzip ?
652 gzread(gz, fb->buf + (int)off, fb->sz - off) :
653 read(fd, fb->buf + (int)off, fb->sz - off);
654 if (ssz == 0) {
655 fb->sz = off;
656 return 1;
657 }
658 if (ssz == -1)
659 err((int)MANDOCLEVEL_SYSERR, "%s", file);
660 off += (size_t)ssz;
661 }
662
663 free(fb->buf);
664 fb->buf = NULL;
665 return 0;
666 }
667
668 static void
669 mparse_end(struct mparse *curp)
670 {
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);
675 else
676 man_endparse(curp->man);
677 roff_endparse(curp->roff);
678 }
679
680 static void
681 mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
682 {
683 struct buf *svprimary;
684 const char *svfile;
685 size_t offset;
686 static int recursion_depth;
687
688 if (64 < recursion_depth) {
689 mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
690 return;
691 }
692
693 /* Line number is per-file. */
694 svfile = curp->file;
695 curp->file = file;
696 svprimary = curp->primary;
697 curp->primary = &blk;
698 curp->line = 1;
699 recursion_depth++;
700
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) {
706 offset = 3;
707 curp->filenc &= ~MPARSE_LATIN1;
708 } else
709 offset = 0;
710
711 mparse_buf_r(curp, blk, offset, 1);
712
713 if (--recursion_depth == 0)
714 mparse_end(curp);
715
716 curp->primary = svprimary;
717 curp->file = svfile;
718 }
719
720 enum mandoclevel
721 mparse_readmem(struct mparse *curp, void *buf, size_t len,
722 const char *file)
723 {
724 struct buf blk;
725
726 blk.buf = buf;
727 blk.sz = len;
728
729 mparse_parse_buffer(curp, blk, file);
730 return curp->file_status;
731 }
732
733 /*
734 * Read the whole file into memory and call the parsers.
735 * Called recursively when an .so request is encountered.
736 */
737 enum mandoclevel
738 mparse_readfd(struct mparse *curp, int fd, const char *file)
739 {
740 struct buf blk;
741 int with_mmap;
742 int save_filenc;
743
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;
750 #if HAVE_MMAP
751 if (with_mmap)
752 munmap(blk.buf, blk.sz);
753 else
754 #endif
755 free(blk.buf);
756 }
757 return curp->file_status;
758 }
759
760 int
761 mparse_open(struct mparse *curp, const char *file)
762 {
763 char *cp;
764 int fd;
765
766 curp->file = file;
767 cp = strrchr(file, '.');
768 curp->gzip = (cp != NULL && ! strcmp(cp + 1, "gz"));
769
770 /* First try to use the filename as it is. */
771
772 if ((fd = open(file, O_RDONLY)) != -1)
773 return fd;
774
775 /*
776 * If that doesn't work and the filename doesn't
777 * already end in .gz, try appending .gz.
778 */
779
780 if ( ! curp->gzip) {
781 mandoc_asprintf(&cp, "%s.gz", file);
782 fd = open(cp, O_RDONLY);
783 free(cp);
784 if (fd != -1) {
785 curp->gzip = 1;
786 return fd;
787 }
788 }
789
790 /* Neither worked, give up. */
791
792 mandoc_msg(MANDOCERR_FILE, curp, 0, 0, strerror(errno));
793 return -1;
794 }
795
796 struct mparse *
797 mparse_alloc(int options, enum mandoclevel wlevel, mandocmsg mmsg,
798 const char *defos)
799 {
800 struct mparse *curp;
801
802 curp = mandoc_calloc(1, sizeof(struct mparse));
803
804 curp->options = options;
805 curp->wlevel = wlevel;
806 curp->mmsg = mmsg;
807 curp->defos = defos;
808
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) {
813 mdoc_hash_init();
814 curp->man->macroset = MACROSET_MDOC;
815 } else if (curp->options & MPARSE_MAN) {
816 man_hash_init();
817 curp->man->macroset = MACROSET_MAN;
818 }
819 curp->man->first->tok = TOKEN_NONE;
820 return curp;
821 }
822
823 void
824 mparse_reset(struct mparse *curp)
825 {
826 roff_reset(curp->roff);
827 roff_man_reset(curp->man);
828 if (curp->secondary)
829 curp->secondary->sz = 0;
830
831 curp->file_status = MANDOCLEVEL_OK;
832
833 free(curp->sodest);
834 curp->sodest = NULL;
835 }
836
837 void
838 mparse_free(struct mparse *curp)
839 {
840
841 roff_man_free(curp->man);
842 if (curp->roff)
843 roff_free(curp->roff);
844 if (curp->secondary)
845 free(curp->secondary->buf);
846
847 free(curp->secondary);
848 free(curp->sodest);
849 free(curp);
850 }
851
852 void
853 mparse_result(struct mparse *curp, struct roff_man **man,
854 char **sodest)
855 {
856
857 if (sodest && NULL != (*sodest = curp->sodest)) {
858 *man = NULL;
859 return;
860 }
861 if (man)
862 *man = curp->man;
863 }
864
865 void
866 mandoc_vmsg(enum mandocerr t, struct mparse *m,
867 int ln, int pos, const char *fmt, ...)
868 {
869 char buf[256];
870 va_list ap;
871
872 va_start(ap, fmt);
873 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
874 va_end(ap);
875
876 mandoc_msg(t, m, ln, pos, buf);
877 }
878
879 void
880 mandoc_msg(enum mandocerr er, struct mparse *m,
881 int ln, int col, const char *msg)
882 {
883 enum mandoclevel level;
884
885 level = MANDOCLEVEL_UNSUPP;
886 while (er < mandoclimits[level])
887 level--;
888
889 if (level < m->wlevel && er != MANDOCERR_FILE)
890 return;
891
892 if (m->mmsg)
893 (*m->mmsg)(er, level, m->file, ln, col, msg);
894
895 if (m->file_status < level)
896 m->file_status = level;
897 }
898
899 const char *
900 mparse_strerror(enum mandocerr er)
901 {
902
903 return mandocerrs[er];
904 }
905
906 const char *
907 mparse_strlevel(enum mandoclevel lvl)
908 {
909 return mandoclevels[lvl];
910 }
911
912 void
913 mparse_keep(struct mparse *p)
914 {
915
916 assert(NULL == p->secondary);
917 p->secondary = mandoc_calloc(1, sizeof(struct buf));
918 }
919
920 const char *
921 mparse_getkeep(const struct mparse *p)
922 {
923
924 assert(p->secondary);
925 return p->secondary->sz ? p->secondary->buf : NULL;
926 }