Annotation of mandoc/read.c, Revision 1.179
1.179 ! schwarze 1: /* $Id: read.c,v 1.178 2017/06/17 23:07:00 schwarze Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.156 schwarze 4: * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
1.42 schwarze 5: * Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
1.1 kristaps 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: *
1.133 schwarze 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 kristaps 12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.133 schwarze 13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 kristaps 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: */
1.11 kristaps 19: #include "config.h"
20:
1.80 schwarze 21: #include <sys/types.h>
1.82 schwarze 22: #include <sys/mman.h>
1.80 schwarze 23: #include <sys/stat.h>
1.1 kristaps 24:
25: #include <assert.h>
26: #include <ctype.h>
1.146 schwarze 27: #if HAVE_ERR
1.143 schwarze 28: #include <err.h>
1.146 schwarze 29: #endif
1.40 schwarze 30: #include <errno.h>
1.1 kristaps 31: #include <fcntl.h>
1.3 kristaps 32: #include <stdarg.h>
1.1 kristaps 33: #include <stdio.h>
34: #include <stdlib.h>
35: #include <string.h>
36: #include <unistd.h>
1.140 schwarze 37: #include <zlib.h>
1.1 kristaps 38:
1.133 schwarze 39: #include "mandoc_aux.h"
1.1 kristaps 40: #include "mandoc.h"
1.133 schwarze 41: #include "roff.h"
1.1 kristaps 42: #include "mdoc.h"
43: #include "man.h"
1.133 schwarze 44: #include "libmandoc.h"
1.139 schwarze 45: #include "roff_int.h"
1.1 kristaps 46:
47: #define REPARSE_LIMIT 1000
48:
49: struct mparse {
1.160 schwarze 50: struct roff *roff; /* roff parser (!NULL) */
1.134 schwarze 51: struct roff_man *man; /* man parser */
1.45 schwarze 52: char *sodest; /* filename pointed to by .so */
1.83 schwarze 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 */
1.179 ! schwarze 56: const char *os_s; /* default operating system */
1.83 schwarze 57: mandocmsg mmsg; /* warning/error message handler */
58: enum mandoclevel file_status; /* status of current parse */
1.179 ! schwarze 59: enum mandocerr mmin; /* ignore messages below this */
1.83 schwarze 60: int options; /* parser options */
1.140 schwarze 61: int gzip; /* current input file is gzipped */
1.93 schwarze 62: int filenc; /* encoding of the current file */
1.1 kristaps 63: int reparse_count; /* finite interp. stack */
1.83 schwarze 64: int line; /* line number in the file */
1.1 kristaps 65: };
66:
1.84 schwarze 67: static void choose_parser(struct mparse *);
1.1 kristaps 68: static void resize_buf(struct buf *, size_t);
1.163 schwarze 69: static int mparse_buf_r(struct mparse *, struct buf, size_t, int);
1.40 schwarze 70: static int read_whole_file(struct mparse *, const char *, int,
71: struct buf *, int *);
1.1 kristaps 72: static void mparse_end(struct mparse *);
1.37 schwarze 73: static void mparse_parse_buffer(struct mparse *, struct buf,
74: const char *);
1.1 kristaps 75:
1.3 kristaps 76: static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = {
77: MANDOCERR_OK,
1.165 schwarze 78: MANDOCERR_STYLE,
1.3 kristaps 79: MANDOCERR_WARNING,
80: MANDOCERR_ERROR,
1.112 schwarze 81: MANDOCERR_UNSUPP,
1.3 kristaps 82: MANDOCERR_MAX,
83: MANDOCERR_MAX
84: };
85:
1.7 kristaps 86: static const char * const mandocerrs[MANDOCERR_MAX] = {
87: "ok",
88:
1.179 ! schwarze 89: "base system convention",
1.166 schwarze 90:
1.176 schwarze 91: "Mdocdate found",
92: "Mdocdate missing",
1.179 ! schwarze 93: "RCS id missing",
! 94:
! 95: "generic style suggestion",
! 96:
1.176 schwarze 97: "legacy man(7) date format",
1.178 schwarze 98: "duplicate RCS id",
1.166 schwarze 99: "useless macro",
1.167 schwarze 100: "consider using OS macro",
1.172 schwarze 101: "errnos out of order",
102: "duplicate errno",
1.168 schwarze 103: "description line ends with a full stop",
1.174 schwarze 104: "no blank before trailing delimiter",
1.175 schwarze 105: "function name without markup",
1.166 schwarze 106:
1.7 kristaps 107: "generic warning",
108:
109: /* related to the prologue */
1.79 schwarze 110: "missing manual title, using UNTITLED",
111: "missing manual title, using \"\"",
1.54 schwarze 112: "lower case character in document title",
1.79 schwarze 113: "missing manual section, using \"\"",
1.7 kristaps 114: "unknown manual section",
1.54 schwarze 115: "missing date, using today's date",
1.7 kristaps 116: "cannot parse date, using it verbatim",
1.79 schwarze 117: "missing Os macro, using \"\"",
118: "duplicate prologue macro",
119: "late prologue macro",
120: "skipping late title macro",
1.7 kristaps 121: "prologue macros out of order",
122:
123: /* related to document structure */
124: ".so is fragile, better use ln(1)",
1.50 schwarze 125: "no document body",
1.54 schwarze 126: "content before first section header",
127: "first section is not \"NAME\"",
1.156 schwarze 128: "NAME section without Nm before Nd",
1.128 schwarze 129: "NAME section without description",
130: "description not at the end of NAME",
131: "bad NAME section content",
1.156 schwarze 132: "missing comma before name",
1.121 schwarze 133: "missing description line, using \"\"",
1.162 schwarze 134: "description line outside NAME section",
1.7 kristaps 135: "sections out of conventional order",
1.54 schwarze 136: "duplicate section title",
137: "unexpected section",
1.87 schwarze 138: "unusual Xr order",
139: "unusual Xr punctuation",
1.86 schwarze 140: "AUTHORS section without An macro",
1.7 kristaps 141:
142: /* related to macros and nesting */
1.55 schwarze 143: "obsolete macro",
1.102 schwarze 144: "macro neither callable nor escaped",
1.7 kristaps 145: "skipping paragraph macro",
1.31 schwarze 146: "moving paragraph macro out of list",
1.7 kristaps 147: "skipping no-space macro",
148: "blocks badly nested",
149: "nested displays are not portable",
1.57 schwarze 150: "moving content out of list",
1.78 schwarze 151: "fill mode already enabled, skipping",
152: "fill mode already disabled, skipping",
1.7 kristaps 153: "line scope broken",
1.169 schwarze 154: "skipping blank line in line scope",
1.7 kristaps 155:
156: /* related to missing macro arguments */
1.58 schwarze 157: "skipping empty request",
158: "conditional request controls empty scope",
1.7 kristaps 159: "skipping empty macro",
1.124 schwarze 160: "empty block",
1.62 schwarze 161: "empty argument, using 0n",
1.60 schwarze 162: "missing display type, using -ragged",
163: "list type is not the first argument",
1.152 schwarze 164: "missing -width in -tag list, using 6n",
1.78 schwarze 165: "missing utility name, using \"\"",
1.123 schwarze 166: "missing function name, using \"\"",
1.60 schwarze 167: "empty head in list item",
168: "empty list item",
1.61 schwarze 169: "missing font type, using \\fR",
170: "unknown font type, using \\fR",
1.103 schwarze 171: "nothing follows prefix",
1.122 schwarze 172: "empty reference block",
1.155 schwarze 173: "missing section argument",
1.60 schwarze 174: "missing -std argument, adding it",
1.125 schwarze 175: "missing option string, using \"\"",
176: "missing resource identifier, using \"\"",
1.90 schwarze 177: "missing eqn box, using \"\"",
1.7 kristaps 178:
179: /* related to bad macro arguments */
1.64 schwarze 180: "unterminated quoted argument",
1.7 kristaps 181: "duplicate argument",
1.76 schwarze 182: "skipping duplicate argument",
1.63 schwarze 183: "skipping duplicate display type",
184: "skipping duplicate list type",
1.76 schwarze 185: "skipping -width argument",
1.126 schwarze 186: "wrong number of cells",
1.7 kristaps 187: "unknown AT&T UNIX version",
1.88 schwarze 188: "comma in function argument",
1.89 schwarze 189: "parenthesis in function name",
1.173 schwarze 190: "unknown library name",
1.67 schwarze 191: "invalid content in Rs block",
1.63 schwarze 192: "invalid Boolean argument",
193: "unknown font, skipping request",
1.126 schwarze 194: "odd number of characters in request",
1.7 kristaps 195:
196: /* related to plain text */
1.64 schwarze 197: "blank line in fill mode, using .sp",
198: "tab in filled text",
199: "whitespace at end of input line",
1.158 schwarze 200: "new sentence, new line",
1.7 kristaps 201: "bad comment style",
1.64 schwarze 202: "invalid escape sequence",
203: "undefined string, using \"\"",
1.16 kristaps 204:
1.118 schwarze 205: /* related to tables */
206: "tbl line starts with span",
207: "tbl column starts with span",
208: "skipping vertical bar in tbl layout",
209:
1.7 kristaps 210: "generic error",
211:
212: /* related to tables */
1.116 schwarze 213: "non-alphabetic character in tbl options",
214: "skipping unknown tbl option",
215: "missing tbl option argument",
216: "wrong tbl option argument size",
1.118 schwarze 217: "empty tbl layout",
218: "invalid character in tbl layout",
219: "unmatched parenthesis in tbl layout",
1.119 schwarze 220: "tbl without any data cells",
221: "ignoring data in spanned tbl cell",
222: "ignoring extra tbl data cells",
223: "data block open at end of tbl",
1.7 kristaps 224:
1.68 schwarze 225: /* related to document structure and macros */
1.108 schwarze 226: NULL,
1.7 kristaps 227: "input stack limit exceeded, infinite loop?",
228: "skipping bad character",
1.68 schwarze 229: "skipping unknown macro",
1.112 schwarze 230: "skipping insecure request",
1.73 schwarze 231: "skipping item outside list",
1.68 schwarze 232: "skipping column outside column list",
233: "skipping end of block that is not open",
1.115 schwarze 234: "fewer RS blocks open, skipping",
1.68 schwarze 235: "inserting missing end of block",
236: "appending missing end of block",
237:
238: /* related to request and macro arguments */
1.7 kristaps 239: "escaped character not allowed in a name",
1.99 schwarze 240: "NOT IMPLEMENTED: Bd -file",
1.145 schwarze 241: "skipping display without arguments",
1.71 schwarze 242: "missing list type, using -item",
1.171 schwarze 243: "argument is not numeric, using 1",
1.70 schwarze 244: "missing manual name, using \"\"",
1.71 schwarze 245: "uname(3) system call failed, using UNKNOWN",
1.63 schwarze 246: "unknown standard specifier",
1.71 schwarze 247: "skipping request without numeric argument",
1.109 schwarze 248: "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.110 schwarze 249: ".so request failed",
1.61 schwarze 250: "skipping all arguments",
251: "skipping excess arguments",
1.92 kristaps 252: "divide by zero",
1.112 schwarze 253:
254: "unsupported feature",
255: "input too large",
1.114 schwarze 256: "unsupported control character",
1.112 schwarze 257: "unsupported roff request",
1.119 schwarze 258: "eqn delim option in tbl",
1.118 schwarze 259: "unsupported tbl layout modifier",
1.112 schwarze 260: "ignoring macro in table",
1.7 kristaps 261: };
262:
263: static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
264: "SUCCESS",
1.165 schwarze 265: "STYLE",
1.7 kristaps 266: "WARNING",
267: "ERROR",
1.112 schwarze 268: "UNSUPP",
1.7 kristaps 269: "BADARG",
270: "SYSERR"
271: };
272:
1.47 schwarze 273:
1.1 kristaps 274: static void
275: resize_buf(struct buf *buf, size_t initial)
276: {
277:
278: buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
279: buf->buf = mandoc_realloc(buf->buf, buf->sz);
280: }
281:
282: static void
1.84 schwarze 283: choose_parser(struct mparse *curp)
1.1 kristaps 284: {
1.83 schwarze 285: char *cp, *ep;
286: int format;
1.1 kristaps 287:
1.83 schwarze 288: /*
289: * If neither command line arguments -mdoc or -man select
290: * a parser nor the roff parser found a .Dd or .TH macro
291: * yet, look ahead in the main input buffer.
292: */
293:
294: if ((format = roff_getformat(curp->roff)) == 0) {
295: cp = curp->primary->buf;
296: ep = cp + curp->primary->sz;
297: while (cp < ep) {
1.85 schwarze 298: if (*cp == '.' || *cp == '\'') {
1.83 schwarze 299: cp++;
300: if (cp[0] == 'D' && cp[1] == 'd') {
301: format = MPARSE_MDOC;
302: break;
303: }
304: if (cp[0] == 'T' && cp[1] == 'H') {
305: format = MPARSE_MAN;
306: break;
307: }
308: }
309: cp = memchr(cp, '\n', ep - cp);
310: if (cp == NULL)
311: break;
312: cp++;
313: }
1.1 kristaps 314: }
315:
1.83 schwarze 316: if (format == MPARSE_MDOC) {
1.137 schwarze 317: curp->man->macroset = MACROSET_MDOC;
1.164 schwarze 318: if (curp->man->mdocmac == NULL)
319: curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137 schwarze 320: } else {
321: curp->man->macroset = MACROSET_MAN;
1.164 schwarze 322: if (curp->man->manmac == NULL)
323: curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.47 schwarze 324: }
1.164 schwarze 325: curp->man->first->tok = TOKEN_NONE;
1.1 kristaps 326: }
327:
328: /*
1.95 schwarze 329: * Main parse routine for a buffer.
330: * It assumes encoding and line numbering are already set up.
331: * It can recurse directly (for invocations of user-defined
332: * macros, inline equations, and input line traps)
333: * and indirectly (for .so file inclusion).
1.1 kristaps 334: */
1.163 schwarze 335: static int
1.95 schwarze 336: mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
1.1 kristaps 337: {
338: const struct tbl_span *span;
339: struct buf ln;
1.113 schwarze 340: const char *save_file;
1.110 schwarze 341: char *cp;
1.95 schwarze 342: size_t pos; /* byte number in the ln buffer */
1.1 kristaps 343: enum rofferr rr;
1.100 schwarze 344: int of;
1.1 kristaps 345: int lnn; /* line number in the real file */
1.110 schwarze 346: int fd;
1.1 kristaps 347: unsigned char c;
348:
1.95 schwarze 349: memset(&ln, 0, sizeof(ln));
1.1 kristaps 350:
1.47 schwarze 351: lnn = curp->line;
352: pos = 0;
1.1 kristaps 353:
1.95 schwarze 354: while (i < blk.sz) {
1.1 kristaps 355: if (0 == pos && '\0' == blk.buf[i])
356: break;
357:
358: if (start) {
359: curp->line = lnn;
360: curp->reparse_count = 0;
1.93 schwarze 361:
362: if (lnn < 3 &&
363: curp->filenc & MPARSE_UTF8 &&
1.95 schwarze 364: curp->filenc & MPARSE_LATIN1)
365: curp->filenc = preconv_cue(&blk, i);
1.1 kristaps 366: }
367:
1.95 schwarze 368: while (i < blk.sz && (start || blk.buf[i] != '\0')) {
1.1 kristaps 369:
370: /*
371: * When finding an unescaped newline character,
372: * leave the character loop to process the line.
373: * Skip a preceding carriage return, if any.
374: */
375:
1.95 schwarze 376: if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
1.1 kristaps 377: '\n' == blk.buf[i + 1])
378: ++i;
379: if ('\n' == blk.buf[i]) {
380: ++i;
381: ++lnn;
382: break;
383: }
384:
1.35 schwarze 385: /*
1.93 schwarze 386: * Make sure we have space for the worst
387: * case of 11 bytes: "\\[u10ffff]\0"
1.35 schwarze 388: */
389:
1.95 schwarze 390: if (pos + 11 > ln.sz)
1.35 schwarze 391: resize_buf(&ln, 256);
392:
1.47 schwarze 393: /*
1.93 schwarze 394: * Encode 8-bit input.
1.1 kristaps 395: */
396:
1.93 schwarze 397: c = blk.buf[i];
398: if (c & 0x80) {
1.95 schwarze 399: if ( ! (curp->filenc && preconv_encode(
400: &blk, &i, &ln, &pos, &curp->filenc))) {
1.114 schwarze 401: mandoc_vmsg(MANDOCERR_CHAR_BAD, curp,
402: curp->line, pos, "0x%x", c);
1.93 schwarze 403: ln.buf[pos++] = '?';
404: i++;
405: }
406: continue;
407: }
408:
409: /*
410: * Exclude control characters.
411: */
1.1 kristaps 412:
1.93 schwarze 413: if (c == 0x7f || (c < 0x20 && c != 0x09)) {
1.114 schwarze 414: mandoc_vmsg(c == 0x00 || c == 0x04 ||
415: c > 0x0a ? MANDOCERR_CHAR_BAD :
416: MANDOCERR_CHAR_UNSUPP,
417: curp, curp->line, pos, "0x%x", c);
1.1 kristaps 418: i++;
1.127 schwarze 419: if (c != '\r')
420: ln.buf[pos++] = '?';
1.1 kristaps 421: continue;
422: }
423:
424: ln.buf[pos++] = blk.buf[i++];
425: }
426:
1.170 schwarze 427: if (pos + 1 >= ln.sz)
1.1 kristaps 428: resize_buf(&ln, 256);
429:
1.170 schwarze 430: if (i == blk.sz || blk.buf[i] == '\0')
431: ln.buf[pos++] = '\n';
1.1 kristaps 432: ln.buf[pos] = '\0';
433:
434: /*
435: * A significant amount of complexity is contained by
436: * the roff preprocessor. It's line-oriented but can be
437: * expressed on one line, so we need at times to
438: * readjust our starting point and re-run it. The roff
439: * preprocessor can also readjust the buffers with new
440: * data, so we pass them in wholesale.
441: */
442:
443: of = 0;
444:
1.24 kristaps 445: /*
446: * Maintain a lookaside buffer of all parsed lines. We
447: * only do this if mparse_keep() has been invoked (the
448: * buffer may be accessed with mparse_getkeep()).
449: */
450:
451: if (curp->secondary) {
1.47 schwarze 452: curp->secondary->buf = mandoc_realloc(
453: curp->secondary->buf,
454: curp->secondary->sz + pos + 2);
455: memcpy(curp->secondary->buf +
456: curp->secondary->sz,
457: ln.buf, pos);
1.24 kristaps 458: curp->secondary->sz += pos;
459: curp->secondary->buf
460: [curp->secondary->sz] = '\n';
461: curp->secondary->sz++;
462: curp->secondary->buf
463: [curp->secondary->sz] = '\0';
464: }
1.1 kristaps 465: rerun:
1.96 schwarze 466: rr = roff_parseln(curp->roff, curp->line, &ln, &of);
1.1 kristaps 467:
468: switch (rr) {
1.47 schwarze 469: case ROFF_REPARSE:
1.163 schwarze 470: if (++curp->reparse_count > REPARSE_LIMIT)
1.3 kristaps 471: mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.47 schwarze 472: curp->line, pos, NULL);
1.163 schwarze 473: else if (mparse_buf_r(curp, ln, of, 0) == 1 ||
474: start == 1) {
475: pos = 0;
476: continue;
477: }
478: free(ln.buf);
479: return 0;
1.47 schwarze 480: case ROFF_APPEND:
1.95 schwarze 481: pos = strlen(ln.buf);
1.1 kristaps 482: continue;
1.47 schwarze 483: case ROFF_RERUN:
1.1 kristaps 484: goto rerun;
1.47 schwarze 485: case ROFF_IGN:
1.1 kristaps 486: pos = 0;
487: continue;
1.47 schwarze 488: case ROFF_SO:
1.95 schwarze 489: if ( ! (curp->options & MPARSE_SO) &&
490: (i >= blk.sz || blk.buf[i] == '\0')) {
1.45 schwarze 491: curp->sodest = mandoc_strdup(ln.buf + of);
492: free(ln.buf);
1.163 schwarze 493: return 1;
1.45 schwarze 494: }
1.24 kristaps 495: /*
496: * We remove `so' clauses from our lookaside
497: * buffer because we're going to descend into
498: * the file recursively.
499: */
1.47 schwarze 500: if (curp->secondary)
1.25 kristaps 501: curp->secondary->sz -= pos + 1;
1.113 schwarze 502: save_file = curp->file;
1.148 schwarze 503: if ((fd = mparse_open(curp, ln.buf + of)) != -1) {
1.110 schwarze 504: mparse_readfd(curp, fd, ln.buf + of);
1.147 schwarze 505: close(fd);
1.113 schwarze 506: curp->file = save_file;
507: } else {
508: curp->file = save_file;
1.52 schwarze 509: mandoc_vmsg(MANDOCERR_SO_FAIL,
510: curp, curp->line, pos,
511: ".so %s", ln.buf + of);
1.110 schwarze 512: ln.sz = mandoc_asprintf(&cp,
513: ".sp\nSee the file %s.\n.sp",
514: ln.buf + of);
515: free(ln.buf);
516: ln.buf = cp;
517: of = 0;
518: mparse_buf_r(curp, ln, of, 0);
1.52 schwarze 519: }
1.1 kristaps 520: pos = 0;
521: continue;
522: default:
523: break;
524: }
525:
1.150 schwarze 526: if (curp->man->macroset == MACROSET_NONE)
1.84 schwarze 527: choose_parser(curp);
1.1 kristaps 528:
1.47 schwarze 529: /*
1.84 schwarze 530: * Lastly, push down into the parsers themselves.
1.1 kristaps 531: * If libroff returns ROFF_TBL, then add it to the
532: * currently open parse. Since we only get here if
533: * there does exist data (see tbl_data.c), we're
534: * guaranteed that something's been allocated.
535: * Do the same for ROFF_EQN.
536: */
537:
1.139 schwarze 538: if (rr == ROFF_TBL)
1.100 schwarze 539: while ((span = roff_span(curp->roff)) != NULL)
1.139 schwarze 540: roff_addtbl(curp->man, span);
541: else if (rr == ROFF_EQN)
542: roff_addeqn(curp->man, roff_eqn(curp->roff));
543: else if ((curp->man->macroset == MACROSET_MDOC ?
1.135 schwarze 544: mdoc_parseln(curp->man, curp->line, ln.buf, of) :
1.100 schwarze 545: man_parseln(curp->man, curp->line, ln.buf, of)) == 2)
546: break;
1.1 kristaps 547:
548: /* Temporary buffers typically are not full. */
549:
550: if (0 == start && '\0' == blk.buf[i])
551: break;
552:
553: /* Start the next input line. */
554:
555: pos = 0;
556: }
557:
558: free(ln.buf);
1.163 schwarze 559: return 1;
1.1 kristaps 560: }
561:
562: static int
1.40 schwarze 563: read_whole_file(struct mparse *curp, const char *file, int fd,
564: struct buf *fb, int *with_mmap)
1.1 kristaps 565: {
1.161 schwarze 566: struct stat st;
1.140 schwarze 567: gzFile gz;
1.1 kristaps 568: size_t off;
569: ssize_t ssz;
1.143 schwarze 570:
571: if (fstat(fd, &st) == -1)
572: err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.1 kristaps 573:
574: /*
575: * If we're a regular file, try just reading in the whole entry
576: * via mmap(). This is faster than reading it into blocks, and
577: * since each file is only a few bytes to begin with, I'm not
578: * concerned that this is going to tank any machines.
579: */
580:
1.140 schwarze 581: if (curp->gzip == 0 && S_ISREG(st.st_mode)) {
1.131 schwarze 582: if (st.st_size > 0x7fffffff) {
1.111 schwarze 583: mandoc_msg(MANDOCERR_TOOLARGE, curp, 0, 0, NULL);
1.142 schwarze 584: return 0;
1.1 kristaps 585: }
586: *with_mmap = 1;
587: fb->sz = (size_t)st.st_size;
1.37 schwarze 588: fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1 kristaps 589: if (fb->buf != MAP_FAILED)
1.142 schwarze 590: return 1;
1.1 kristaps 591: }
592:
1.140 schwarze 593: if (curp->gzip) {
1.143 schwarze 594: if ((gz = gzdopen(fd, "rb")) == NULL)
595: err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.140 schwarze 596: } else
597: gz = NULL;
598:
1.1 kristaps 599: /*
600: * If this isn't a regular file (like, say, stdin), then we must
601: * go the old way and just read things in bit by bit.
602: */
603:
604: *with_mmap = 0;
605: off = 0;
606: fb->sz = 0;
607: fb->buf = NULL;
608: for (;;) {
609: if (off == fb->sz) {
610: if (fb->sz == (1U << 31)) {
1.111 schwarze 611: mandoc_msg(MANDOCERR_TOOLARGE, curp,
612: 0, 0, NULL);
1.1 kristaps 613: break;
614: }
615: resize_buf(fb, 65536);
616: }
1.140 schwarze 617: ssz = curp->gzip ?
618: gzread(gz, fb->buf + (int)off, fb->sz - off) :
619: read(fd, fb->buf + (int)off, fb->sz - off);
1.1 kristaps 620: if (ssz == 0) {
621: fb->sz = off;
1.142 schwarze 622: return 1;
1.1 kristaps 623: }
1.143 schwarze 624: if (ssz == -1)
625: err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.1 kristaps 626: off += (size_t)ssz;
627: }
628:
629: free(fb->buf);
630: fb->buf = NULL;
1.142 schwarze 631: return 0;
1.1 kristaps 632: }
633:
634: static void
635: mparse_end(struct mparse *curp)
636: {
1.135 schwarze 637: if (curp->man->macroset == MACROSET_NONE)
638: curp->man->macroset = MACROSET_MAN;
639: if (curp->man->macroset == MACROSET_MDOC)
640: mdoc_endparse(curp->man);
641: else
1.111 schwarze 642: man_endparse(curp->man);
1.1 kristaps 643: roff_endparse(curp->roff);
644: }
645:
646: static void
1.36 schwarze 647: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.28 joerg 648: {
1.85 schwarze 649: struct buf *svprimary;
1.28 joerg 650: const char *svfile;
1.95 schwarze 651: size_t offset;
1.36 schwarze 652: static int recursion_depth;
653:
654: if (64 < recursion_depth) {
655: mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
656: return;
657: }
1.28 joerg 658:
659: /* Line number is per-file. */
660: svfile = curp->file;
661: curp->file = file;
1.85 schwarze 662: svprimary = curp->primary;
1.83 schwarze 663: curp->primary = &blk;
1.28 joerg 664: curp->line = 1;
1.36 schwarze 665: recursion_depth++;
1.28 joerg 666:
1.93 schwarze 667: /* Skip an UTF-8 byte order mark. */
668: if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
669: (unsigned char)blk.buf[0] == 0xef &&
670: (unsigned char)blk.buf[1] == 0xbb &&
671: (unsigned char)blk.buf[2] == 0xbf) {
1.95 schwarze 672: offset = 3;
1.93 schwarze 673: curp->filenc &= ~MPARSE_LATIN1;
1.95 schwarze 674: } else
675: offset = 0;
1.93 schwarze 676:
1.95 schwarze 677: mparse_buf_r(curp, blk, offset, 1);
1.28 joerg 678:
1.111 schwarze 679: if (--recursion_depth == 0)
1.28 joerg 680: mparse_end(curp);
681:
1.85 schwarze 682: curp->primary = svprimary;
1.28 joerg 683: curp->file = svfile;
684: }
685:
686: enum mandoclevel
1.104 schwarze 687: mparse_readmem(struct mparse *curp, void *buf, size_t len,
1.28 joerg 688: const char *file)
689: {
690: struct buf blk;
691:
1.104 schwarze 692: blk.buf = buf;
1.28 joerg 693: blk.sz = len;
694:
1.36 schwarze 695: mparse_parse_buffer(curp, blk, file);
1.142 schwarze 696: return curp->file_status;
1.28 joerg 697: }
698:
1.98 schwarze 699: /*
700: * Read the whole file into memory and call the parsers.
701: * Called recursively when an .so request is encountered.
702: */
1.36 schwarze 703: enum mandoclevel
704: mparse_readfd(struct mparse *curp, int fd, const char *file)
1.1 kristaps 705: {
1.28 joerg 706: struct buf blk;
707: int with_mmap;
1.93 schwarze 708: int save_filenc;
1.1 kristaps 709:
1.91 schwarze 710: if (read_whole_file(curp, file, fd, &blk, &with_mmap)) {
1.93 schwarze 711: save_filenc = curp->filenc;
712: curp->filenc = curp->options &
713: (MPARSE_UTF8 | MPARSE_LATIN1);
1.91 schwarze 714: mparse_parse_buffer(curp, blk, file);
1.93 schwarze 715: curp->filenc = save_filenc;
1.91 schwarze 716: if (with_mmap)
717: munmap(blk.buf, blk.sz);
718: else
719: free(blk.buf);
720: }
1.142 schwarze 721: return curp->file_status;
1.82 schwarze 722: }
723:
1.148 schwarze 724: int
725: mparse_open(struct mparse *curp, const char *file)
1.82 schwarze 726: {
727: char *cp;
1.148 schwarze 728: int fd;
1.82 schwarze 729:
730: curp->file = file;
1.140 schwarze 731: cp = strrchr(file, '.');
732: curp->gzip = (cp != NULL && ! strcmp(cp + 1, "gz"));
1.98 schwarze 733:
1.140 schwarze 734: /* First try to use the filename as it is. */
1.98 schwarze 735:
1.148 schwarze 736: if ((fd = open(file, O_RDONLY)) != -1)
737: return fd;
1.98 schwarze 738:
1.140 schwarze 739: /*
740: * If that doesn't work and the filename doesn't
741: * already end in .gz, try appending .gz.
742: */
1.98 schwarze 743:
1.140 schwarze 744: if ( ! curp->gzip) {
1.98 schwarze 745: mandoc_asprintf(&cp, "%s.gz", file);
1.149 schwarze 746: fd = open(cp, O_RDONLY);
1.108 schwarze 747: free(cp);
1.148 schwarze 748: if (fd != -1) {
1.140 schwarze 749: curp->gzip = 1;
1.148 schwarze 750: return fd;
1.82 schwarze 751: }
752: }
753:
1.140 schwarze 754: /* Neither worked, give up. */
1.97 schwarze 755:
1.140 schwarze 756: mandoc_msg(MANDOCERR_FILE, curp, 0, 0, strerror(errno));
1.148 schwarze 757: return -1;
1.1 kristaps 758: }
759:
760: struct mparse *
1.179 ! schwarze 761: mparse_alloc(int options, enum mandocerr mmin, mandocmsg mmsg,
! 762: enum mandoc_os os_e, const char *os_s)
1.1 kristaps 763: {
764: struct mparse *curp;
1.10 kristaps 765:
1.1 kristaps 766: curp = mandoc_calloc(1, sizeof(struct mparse));
767:
1.44 schwarze 768: curp->options = options;
1.179 ! schwarze 769: curp->mmin = mmin;
1.1 kristaps 770: curp->mmsg = mmsg;
1.179 ! schwarze 771: curp->os_s = os_s;
1.1 kristaps 772:
1.144 schwarze 773: curp->roff = roff_alloc(curp, options);
1.179 ! schwarze 774: curp->man = roff_man_alloc(curp->roff, curp, curp->os_s,
1.137 schwarze 775: curp->options & MPARSE_QUICK ? 1 : 0);
1.136 schwarze 776: if (curp->options & MPARSE_MDOC) {
1.137 schwarze 777: curp->man->macroset = MACROSET_MDOC;
1.164 schwarze 778: if (curp->man->mdocmac == NULL)
779: curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137 schwarze 780: } else if (curp->options & MPARSE_MAN) {
781: curp->man->macroset = MACROSET_MAN;
1.164 schwarze 782: if (curp->man->manmac == NULL)
783: curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.136 schwarze 784: }
1.138 schwarze 785: curp->man->first->tok = TOKEN_NONE;
1.179 ! schwarze 786: curp->man->meta.os_e = os_e;
1.142 schwarze 787: return curp;
1.1 kristaps 788: }
789:
790: void
791: mparse_reset(struct mparse *curp)
792: {
793: roff_reset(curp->roff);
1.150 schwarze 794: roff_man_reset(curp->man);
1.160 schwarze 795:
796: free(curp->sodest);
797: curp->sodest = NULL;
798:
1.24 kristaps 799: if (curp->secondary)
800: curp->secondary->sz = 0;
1.1 kristaps 801:
802: curp->file_status = MANDOCLEVEL_OK;
1.159 schwarze 803: curp->gzip = 0;
1.1 kristaps 804: }
805:
806: void
807: mparse_free(struct mparse *curp)
808: {
809:
1.164 schwarze 810: roffhash_free(curp->man->mdocmac);
811: roffhash_free(curp->man->manmac);
1.137 schwarze 812: roff_man_free(curp->man);
1.160 schwarze 813: roff_free(curp->roff);
1.24 kristaps 814: if (curp->secondary)
815: free(curp->secondary->buf);
1.1 kristaps 816:
1.24 kristaps 817: free(curp->secondary);
1.45 schwarze 818: free(curp->sodest);
1.1 kristaps 819: free(curp);
820: }
821:
822: void
1.135 schwarze 823: mparse_result(struct mparse *curp, struct roff_man **man,
824: char **sodest)
1.1 kristaps 825: {
826:
1.45 schwarze 827: if (sodest && NULL != (*sodest = curp->sodest)) {
828: *man = NULL;
829: return;
830: }
1.9 kristaps 831: if (man)
832: *man = curp->man;
1.157 schwarze 833: }
834:
835: void
836: mparse_updaterc(struct mparse *curp, enum mandoclevel *rc)
837: {
838: if (curp->file_status > *rc)
839: *rc = curp->file_status;
1.3 kristaps 840: }
841:
842: void
843: mandoc_vmsg(enum mandocerr t, struct mparse *m,
844: int ln, int pos, const char *fmt, ...)
845: {
846: char buf[256];
847: va_list ap;
848:
849: va_start(ap, fmt);
1.48 schwarze 850: (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.3 kristaps 851: va_end(ap);
852:
853: mandoc_msg(t, m, ln, pos, buf);
854: }
855:
856: void
1.47 schwarze 857: mandoc_msg(enum mandocerr er, struct mparse *m,
1.3 kristaps 858: int ln, int col, const char *msg)
859: {
860: enum mandoclevel level;
861:
1.179 ! schwarze 862: if (er < m->mmin && er != MANDOCERR_FILE)
! 863: return;
! 864:
1.112 schwarze 865: level = MANDOCLEVEL_UNSUPP;
1.3 kristaps 866: while (er < mandoclimits[level])
867: level--;
868:
1.8 kristaps 869: if (m->mmsg)
870: (*m->mmsg)(er, level, m->file, ln, col, msg);
1.3 kristaps 871:
872: if (m->file_status < level)
873: m->file_status = level;
1.7 kristaps 874: }
875:
876: const char *
877: mparse_strerror(enum mandocerr er)
878: {
879:
1.142 schwarze 880: return mandocerrs[er];
1.7 kristaps 881: }
882:
883: const char *
884: mparse_strlevel(enum mandoclevel lvl)
885: {
1.142 schwarze 886: return mandoclevels[lvl];
1.24 kristaps 887: }
888:
889: void
890: mparse_keep(struct mparse *p)
891: {
892:
893: assert(NULL == p->secondary);
894: p->secondary = mandoc_calloc(1, sizeof(struct buf));
895: }
896:
897: const char *
898: mparse_getkeep(const struct mparse *p)
899: {
900:
901: assert(p->secondary);
1.142 schwarze 902: return p->secondary->sz ? p->secondary->buf : NULL;
1.1 kristaps 903: }
CVSweb