Annotation of mandoc/read.c, Revision 1.219
1.219 ! schwarze 1: /* $Id: read.c,v 1.218 2020/04/18 20:40:10 schwarze Exp $ */
1.1 kristaps 2: /*
1.218 schwarze 3: * Copyright (c) 2010-2020 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 4: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
1.215 schwarze 18: *
19: * Top-level functions of the mandoc(3) parser:
20: * Parser and input encoding selection, decompression,
21: * handling of input bytes, characters, lines, and files,
22: * handling of roff(7) loops and file inclusion,
23: * and steering of the various parsers.
1.1 kristaps 24: */
1.11 kristaps 25: #include "config.h"
26:
1.80 schwarze 27: #include <sys/types.h>
1.82 schwarze 28: #include <sys/mman.h>
1.80 schwarze 29: #include <sys/stat.h>
1.1 kristaps 30:
31: #include <assert.h>
32: #include <ctype.h>
1.40 schwarze 33: #include <errno.h>
1.1 kristaps 34: #include <fcntl.h>
1.3 kristaps 35: #include <stdarg.h>
1.1 kristaps 36: #include <stdio.h>
37: #include <stdlib.h>
38: #include <string.h>
39: #include <unistd.h>
1.140 schwarze 40: #include <zlib.h>
1.1 kristaps 41:
1.133 schwarze 42: #include "mandoc_aux.h"
1.1 kristaps 43: #include "mandoc.h"
1.133 schwarze 44: #include "roff.h"
1.1 kristaps 45: #include "mdoc.h"
46: #include "man.h"
1.202 schwarze 47: #include "mandoc_parse.h"
1.133 schwarze 48: #include "libmandoc.h"
1.201 schwarze 49: #include "roff_int.h"
1.218 schwarze 50: #include "tag.h"
1.1 kristaps 51:
52: #define REPARSE_LIMIT 1000
53:
54: struct mparse {
1.160 schwarze 55: struct roff *roff; /* roff parser (!NULL) */
1.134 schwarze 56: struct roff_man *man; /* man parser */
1.83 schwarze 57: struct buf *primary; /* buffer currently being parsed */
1.198 schwarze 58: struct buf *secondary; /* copy of top level input */
1.199 schwarze 59: struct buf *loop; /* open .while request line */
1.179 schwarze 60: const char *os_s; /* default operating system */
1.83 schwarze 61: int options; /* parser options */
1.140 schwarze 62: int gzip; /* current input file is gzipped */
1.93 schwarze 63: int filenc; /* encoding of the current file */
1.1 kristaps 64: int reparse_count; /* finite interp. stack */
1.83 schwarze 65: int line; /* line number in the file */
1.1 kristaps 66: };
67:
1.84 schwarze 68: static void choose_parser(struct mparse *);
1.198 schwarze 69: static void free_buf_list(struct buf *);
1.1 kristaps 70: static void resize_buf(struct buf *, size_t);
1.199 schwarze 71: static int mparse_buf_r(struct mparse *, struct buf, size_t, int);
1.203 schwarze 72: static int read_whole_file(struct mparse *, int, struct buf *, int *);
1.1 kristaps 73: static void mparse_end(struct mparse *);
74:
1.47 schwarze 75:
1.1 kristaps 76: static void
77: resize_buf(struct buf *buf, size_t initial)
78: {
79:
80: buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
81: buf->buf = mandoc_realloc(buf->buf, buf->sz);
82: }
83:
84: static void
1.198 schwarze 85: free_buf_list(struct buf *buf)
86: {
87: struct buf *tmp;
88:
89: while (buf != NULL) {
90: tmp = buf;
91: buf = tmp->next;
92: free(tmp->buf);
93: free(tmp);
94: }
95: }
96:
97: static void
1.84 schwarze 98: choose_parser(struct mparse *curp)
1.1 kristaps 99: {
1.83 schwarze 100: char *cp, *ep;
101: int format;
1.1 kristaps 102:
1.83 schwarze 103: /*
104: * If neither command line arguments -mdoc or -man select
105: * a parser nor the roff parser found a .Dd or .TH macro
106: * yet, look ahead in the main input buffer.
107: */
108:
109: if ((format = roff_getformat(curp->roff)) == 0) {
110: cp = curp->primary->buf;
111: ep = cp + curp->primary->sz;
112: while (cp < ep) {
1.85 schwarze 113: if (*cp == '.' || *cp == '\'') {
1.83 schwarze 114: cp++;
115: if (cp[0] == 'D' && cp[1] == 'd') {
116: format = MPARSE_MDOC;
117: break;
118: }
119: if (cp[0] == 'T' && cp[1] == 'H') {
120: format = MPARSE_MAN;
121: break;
122: }
123: }
124: cp = memchr(cp, '\n', ep - cp);
125: if (cp == NULL)
126: break;
127: cp++;
128: }
1.1 kristaps 129: }
130:
1.83 schwarze 131: if (format == MPARSE_MDOC) {
1.209 schwarze 132: curp->man->meta.macroset = MACROSET_MDOC;
1.164 schwarze 133: if (curp->man->mdocmac == NULL)
134: curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137 schwarze 135: } else {
1.209 schwarze 136: curp->man->meta.macroset = MACROSET_MAN;
1.164 schwarze 137: if (curp->man->manmac == NULL)
138: curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.47 schwarze 139: }
1.209 schwarze 140: curp->man->meta.first->tok = TOKEN_NONE;
1.1 kristaps 141: }
142:
143: /*
1.95 schwarze 144: * Main parse routine for a buffer.
145: * It assumes encoding and line numbering are already set up.
146: * It can recurse directly (for invocations of user-defined
147: * macros, inline equations, and input line traps)
148: * and indirectly (for .so file inclusion).
1.1 kristaps 149: */
1.199 schwarze 150: static int
1.95 schwarze 151: mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
1.1 kristaps 152: {
153: struct buf ln;
1.199 schwarze 154: struct buf *firstln, *lastln, *thisln, *loop;
1.110 schwarze 155: char *cp;
1.95 schwarze 156: size_t pos; /* byte number in the ln buffer */
1.199 schwarze 157: int line_result, result;
1.100 schwarze 158: int of;
1.1 kristaps 159: int lnn; /* line number in the real file */
1.110 schwarze 160: int fd;
1.199 schwarze 161: int inloop; /* Saw .while on this level. */
1.1 kristaps 162: unsigned char c;
163:
1.198 schwarze 164: ln.sz = 256;
165: ln.buf = mandoc_malloc(ln.sz);
166: ln.next = NULL;
1.213 schwarze 167: firstln = lastln = loop = NULL;
1.47 schwarze 168: lnn = curp->line;
169: pos = 0;
1.199 schwarze 170: inloop = 0;
1.198 schwarze 171: result = ROFF_CONT;
1.1 kristaps 172:
1.199 schwarze 173: while (i < blk.sz && (blk.buf[i] != '\0' || pos != 0)) {
1.1 kristaps 174: if (start) {
175: curp->line = lnn;
176: curp->reparse_count = 0;
1.93 schwarze 177:
178: if (lnn < 3 &&
179: curp->filenc & MPARSE_UTF8 &&
1.95 schwarze 180: curp->filenc & MPARSE_LATIN1)
181: curp->filenc = preconv_cue(&blk, i);
1.1 kristaps 182: }
183:
1.95 schwarze 184: while (i < blk.sz && (start || blk.buf[i] != '\0')) {
1.1 kristaps 185:
186: /*
187: * When finding an unescaped newline character,
188: * leave the character loop to process the line.
189: * Skip a preceding carriage return, if any.
190: */
191:
1.95 schwarze 192: if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
1.1 kristaps 193: '\n' == blk.buf[i + 1])
194: ++i;
195: if ('\n' == blk.buf[i]) {
196: ++i;
197: ++lnn;
198: break;
199: }
200:
1.35 schwarze 201: /*
1.93 schwarze 202: * Make sure we have space for the worst
1.198 schwarze 203: * case of 12 bytes: "\\[u10ffff]\n\0"
1.35 schwarze 204: */
205:
1.198 schwarze 206: if (pos + 12 > ln.sz)
1.35 schwarze 207: resize_buf(&ln, 256);
208:
1.47 schwarze 209: /*
1.93 schwarze 210: * Encode 8-bit input.
1.1 kristaps 211: */
212:
1.93 schwarze 213: c = blk.buf[i];
214: if (c & 0x80) {
1.95 schwarze 215: if ( ! (curp->filenc && preconv_encode(
216: &blk, &i, &ln, &pos, &curp->filenc))) {
1.206 schwarze 217: mandoc_msg(MANDOCERR_CHAR_BAD,
1.114 schwarze 218: curp->line, pos, "0x%x", c);
1.93 schwarze 219: ln.buf[pos++] = '?';
220: i++;
221: }
222: continue;
223: }
224:
225: /*
226: * Exclude control characters.
227: */
1.1 kristaps 228:
1.93 schwarze 229: if (c == 0x7f || (c < 0x20 && c != 0x09)) {
1.206 schwarze 230: mandoc_msg(c == 0x00 || c == 0x04 ||
1.114 schwarze 231: c > 0x0a ? MANDOCERR_CHAR_BAD :
232: MANDOCERR_CHAR_UNSUPP,
1.206 schwarze 233: curp->line, pos, "0x%x", c);
1.1 kristaps 234: i++;
1.127 schwarze 235: if (c != '\r')
236: ln.buf[pos++] = '?';
1.1 kristaps 237: continue;
238: }
239:
240: ln.buf[pos++] = blk.buf[i++];
241: }
1.198 schwarze 242: ln.buf[pos] = '\0';
243:
244: /*
245: * Maintain a lookaside buffer of all lines.
246: * parsed from this input source.
247: */
248:
249: thisln = mandoc_malloc(sizeof(*thisln));
250: thisln->buf = mandoc_strdup(ln.buf);
251: thisln->sz = strlen(ln.buf) + 1;
252: thisln->next = NULL;
253: if (firstln == NULL) {
254: firstln = lastln = thisln;
255: if (curp->secondary == NULL)
256: curp->secondary = firstln;
257: } else {
258: lastln->next = thisln;
259: lastln = thisln;
260: }
1.1 kristaps 261:
1.198 schwarze 262: /* XXX Ugly hack to mark the end of the input. */
1.1 kristaps 263:
1.198 schwarze 264: if (i == blk.sz || blk.buf[i] == '\0') {
1.212 schwarze 265: if (pos + 2 > ln.sz)
266: resize_buf(&ln, 256);
1.170 schwarze 267: ln.buf[pos++] = '\n';
1.198 schwarze 268: ln.buf[pos] = '\0';
269: }
1.1 kristaps 270:
271: /*
272: * A significant amount of complexity is contained by
273: * the roff preprocessor. It's line-oriented but can be
274: * expressed on one line, so we need at times to
275: * readjust our starting point and re-run it. The roff
276: * preprocessor can also readjust the buffers with new
277: * data, so we pass them in wholesale.
278: */
279:
280: of = 0;
281: rerun:
1.197 schwarze 282: line_result = roff_parseln(curp->roff, curp->line, &ln, &of);
1.1 kristaps 283:
1.199 schwarze 284: /* Process options. */
285:
286: if (line_result & ROFF_APPEND)
287: assert(line_result == (ROFF_IGN | ROFF_APPEND));
288:
289: if (line_result & ROFF_USERCALL)
290: assert((line_result & ROFF_MASK) == ROFF_REPARSE);
291:
292: if (line_result & ROFF_USERRET) {
293: assert(line_result == (ROFF_IGN | ROFF_USERRET));
294: if (start == 0) {
295: /* Return from the current macro. */
296: result = ROFF_USERRET;
297: goto out;
298: }
299: }
300:
301: switch (line_result & ROFF_LOOPMASK) {
302: case ROFF_IGN:
303: break;
304: case ROFF_WHILE:
305: if (curp->loop != NULL) {
306: if (loop == curp->loop)
307: break;
308: mandoc_msg(MANDOCERR_WHILE_NEST,
1.206 schwarze 309: curp->line, pos, NULL);
1.199 schwarze 310: }
311: curp->loop = thisln;
312: loop = NULL;
313: inloop = 1;
314: break;
315: case ROFF_LOOPCONT:
316: case ROFF_LOOPEXIT:
317: if (curp->loop == NULL) {
318: mandoc_msg(MANDOCERR_WHILE_FAIL,
1.206 schwarze 319: curp->line, pos, NULL);
1.199 schwarze 320: break;
321: }
322: if (inloop == 0) {
323: mandoc_msg(MANDOCERR_WHILE_INTO,
1.206 schwarze 324: curp->line, pos, NULL);
1.199 schwarze 325: curp->loop = loop = NULL;
326: break;
327: }
328: if (line_result & ROFF_LOOPCONT)
329: loop = curp->loop;
330: else {
331: curp->loop = loop = NULL;
332: inloop = 0;
333: }
334: break;
335: default:
336: abort();
337: }
338:
339: /* Process the main instruction from the roff parser. */
340:
341: switch (line_result & ROFF_MASK) {
342: case ROFF_IGN:
343: break;
344: case ROFF_CONT:
1.209 schwarze 345: if (curp->man->meta.macroset == MACROSET_NONE)
1.199 schwarze 346: choose_parser(curp);
1.209 schwarze 347: if ((curp->man->meta.macroset == MACROSET_MDOC ?
1.199 schwarze 348: mdoc_parseln(curp->man, curp->line, ln.buf, of) :
349: man_parseln(curp->man, curp->line, ln.buf, of)
350: ) == 2)
351: goto out;
352: break;
353: case ROFF_RERUN:
354: goto rerun;
1.47 schwarze 355: case ROFF_REPARSE:
1.197 schwarze 356: if (++curp->reparse_count > REPARSE_LIMIT) {
1.199 schwarze 357: /* Abort and return to the top level. */
1.198 schwarze 358: result = ROFF_IGN;
1.206 schwarze 359: mandoc_msg(MANDOCERR_ROFFLOOP,
1.47 schwarze 360: curp->line, pos, NULL);
1.199 schwarze 361: goto out;
1.197 schwarze 362: }
1.199 schwarze 363: result = mparse_buf_r(curp, ln, of, 0);
364: if (line_result & ROFF_USERCALL) {
365: roff_userret(curp->roff);
366: /* Continue normally. */
367: if (result & ROFF_USERRET)
368: result = ROFF_CONT;
1.163 schwarze 369: }
1.199 schwarze 370: if (start == 0 && result != ROFF_CONT)
371: goto out;
372: break;
1.47 schwarze 373: case ROFF_SO:
1.95 schwarze 374: if ( ! (curp->options & MPARSE_SO) &&
375: (i >= blk.sz || blk.buf[i] == '\0')) {
1.209 schwarze 376: curp->man->meta.sodest =
377: mandoc_strdup(ln.buf + of);
1.198 schwarze 378: goto out;
1.45 schwarze 379: }
1.148 schwarze 380: if ((fd = mparse_open(curp, ln.buf + of)) != -1) {
1.110 schwarze 381: mparse_readfd(curp, fd, ln.buf + of);
1.147 schwarze 382: close(fd);
1.113 schwarze 383: } else {
1.208 schwarze 384: mandoc_msg(MANDOCERR_SO_FAIL,
385: curp->line, of, ".so %s: %s",
386: ln.buf + of, strerror(errno));
1.110 schwarze 387: ln.sz = mandoc_asprintf(&cp,
388: ".sp\nSee the file %s.\n.sp",
389: ln.buf + of);
390: free(ln.buf);
391: ln.buf = cp;
392: of = 0;
393: mparse_buf_r(curp, ln, of, 0);
1.52 schwarze 394: }
1.199 schwarze 395: break;
1.1 kristaps 396: default:
1.199 schwarze 397: abort();
1.1 kristaps 398: }
399:
1.199 schwarze 400: /* Start the next input line. */
1.1 kristaps 401:
1.199 schwarze 402: if (loop != NULL &&
403: (line_result & ROFF_LOOPMASK) == ROFF_IGN)
404: loop = loop->next;
405:
406: if (loop != NULL) {
407: if ((line_result & ROFF_APPEND) == 0)
408: *ln.buf = '\0';
409: if (ln.sz < loop->sz)
410: resize_buf(&ln, loop->sz);
411: (void)strlcat(ln.buf, loop->buf, ln.sz);
412: of = 0;
413: goto rerun;
414: }
1.1 kristaps 415:
1.199 schwarze 416: pos = (line_result & ROFF_APPEND) ? strlen(ln.buf) : 0;
1.1 kristaps 417: }
1.198 schwarze 418: out:
1.199 schwarze 419: if (inloop) {
420: if (result != ROFF_USERRET)
1.206 schwarze 421: mandoc_msg(MANDOCERR_WHILE_OUTOF,
1.199 schwarze 422: curp->line, pos, NULL);
423: curp->loop = NULL;
424: }
1.1 kristaps 425: free(ln.buf);
1.198 schwarze 426: if (firstln != curp->secondary)
427: free_buf_list(firstln);
428: return result;
1.1 kristaps 429: }
430:
431: static int
1.203 schwarze 432: read_whole_file(struct mparse *curp, int fd, struct buf *fb, int *with_mmap)
1.1 kristaps 433: {
1.161 schwarze 434: struct stat st;
1.140 schwarze 435: gzFile gz;
1.1 kristaps 436: size_t off;
437: ssize_t ssz;
1.194 schwarze 438: int gzerrnum, retval;
1.143 schwarze 439:
1.192 schwarze 440: if (fstat(fd, &st) == -1) {
1.214 schwarze 441: mandoc_msg(MANDOCERR_FSTAT, 0, 0, "%s", strerror(errno));
442: return -1;
1.192 schwarze 443: }
1.1 kristaps 444:
445: /*
446: * If we're a regular file, try just reading in the whole entry
447: * via mmap(). This is faster than reading it into blocks, and
448: * since each file is only a few bytes to begin with, I'm not
449: * concerned that this is going to tank any machines.
450: */
451:
1.140 schwarze 452: if (curp->gzip == 0 && S_ISREG(st.st_mode)) {
1.131 schwarze 453: if (st.st_size > 0x7fffffff) {
1.206 schwarze 454: mandoc_msg(MANDOCERR_TOOLARGE, 0, 0, NULL);
1.214 schwarze 455: return -1;
1.1 kristaps 456: }
457: *with_mmap = 1;
458: fb->sz = (size_t)st.st_size;
1.37 schwarze 459: fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1 kristaps 460: if (fb->buf != MAP_FAILED)
1.214 schwarze 461: return 0;
1.1 kristaps 462: }
463:
1.140 schwarze 464: if (curp->gzip) {
1.194 schwarze 465: /*
466: * Duplicating the file descriptor is required
467: * because we will have to call gzclose(3)
468: * to free memory used internally by zlib,
469: * but that will also close the file descriptor,
470: * which this function must not do.
471: */
472: if ((fd = dup(fd)) == -1) {
1.214 schwarze 473: mandoc_msg(MANDOCERR_DUP, 0, 0,
474: "%s", strerror(errno));
475: return -1;
1.194 schwarze 476: }
1.192 schwarze 477: if ((gz = gzdopen(fd, "rb")) == NULL) {
1.214 schwarze 478: mandoc_msg(MANDOCERR_GZDOPEN, 0, 0,
479: "%s", strerror(errno));
1.194 schwarze 480: close(fd);
1.214 schwarze 481: return -1;
1.192 schwarze 482: }
1.140 schwarze 483: } else
484: gz = NULL;
485:
1.1 kristaps 486: /*
487: * If this isn't a regular file (like, say, stdin), then we must
488: * go the old way and just read things in bit by bit.
489: */
490:
491: *with_mmap = 0;
492: off = 0;
1.214 schwarze 493: retval = -1;
1.1 kristaps 494: fb->sz = 0;
495: fb->buf = NULL;
496: for (;;) {
497: if (off == fb->sz) {
498: if (fb->sz == (1U << 31)) {
1.206 schwarze 499: mandoc_msg(MANDOCERR_TOOLARGE, 0, 0, NULL);
1.1 kristaps 500: break;
501: }
502: resize_buf(fb, 65536);
503: }
1.140 schwarze 504: ssz = curp->gzip ?
505: gzread(gz, fb->buf + (int)off, fb->sz - off) :
506: read(fd, fb->buf + (int)off, fb->sz - off);
1.1 kristaps 507: if (ssz == 0) {
508: fb->sz = off;
1.214 schwarze 509: retval = 0;
1.194 schwarze 510: break;
1.1 kristaps 511: }
1.192 schwarze 512: if (ssz == -1) {
1.194 schwarze 513: if (curp->gzip)
514: (void)gzerror(gz, &gzerrnum);
1.214 schwarze 515: mandoc_msg(MANDOCERR_READ, 0, 0, "%s",
1.194 schwarze 516: curp->gzip && gzerrnum != Z_ERRNO ?
517: zError(gzerrnum) : strerror(errno));
1.192 schwarze 518: break;
519: }
1.1 kristaps 520: off += (size_t)ssz;
521: }
522:
1.194 schwarze 523: if (curp->gzip && (gzerrnum = gzclose(gz)) != Z_OK)
1.214 schwarze 524: mandoc_msg(MANDOCERR_GZCLOSE, 0, 0, "%s",
1.194 schwarze 525: gzerrnum == Z_ERRNO ? strerror(errno) :
526: zError(gzerrnum));
1.214 schwarze 527: if (retval == -1) {
1.194 schwarze 528: free(fb->buf);
529: fb->buf = NULL;
530: }
531: return retval;
1.1 kristaps 532: }
533:
534: static void
535: mparse_end(struct mparse *curp)
536: {
1.209 schwarze 537: if (curp->man->meta.macroset == MACROSET_NONE)
538: curp->man->meta.macroset = MACROSET_MAN;
539: if (curp->man->meta.macroset == MACROSET_MDOC)
1.135 schwarze 540: mdoc_endparse(curp->man);
541: else
1.111 schwarze 542: man_endparse(curp->man);
1.1 kristaps 543: roff_endparse(curp->roff);
544: }
545:
1.205 schwarze 546: /*
547: * Read the whole file into memory and call the parsers.
548: * Called recursively when an .so request is encountered.
549: */
550: void
551: mparse_readfd(struct mparse *curp, int fd, const char *filename)
1.28 joerg 552: {
1.205 schwarze 553: static int recursion_depth;
554:
555: struct buf blk;
556: struct buf *save_primary;
1.219 ! schwarze 557: const char *save_filename, *cp;
1.95 schwarze 558: size_t offset;
1.205 schwarze 559: int save_filenc, save_lineno;
560: int with_mmap;
1.36 schwarze 561:
1.205 schwarze 562: if (recursion_depth > 64) {
1.206 schwarze 563: mandoc_msg(MANDOCERR_ROFFLOOP, curp->line, 0, NULL);
1.36 schwarze 564: return;
1.219 ! schwarze 565: } else if (recursion_depth == 0 &&
! 566: (cp = strrchr(filename, '.')) != NULL &&
! 567: cp[1] >= '1' && cp[1] <= '9')
! 568: curp->man->filesec = cp[1];
! 569: else
! 570: curp->man->filesec = '\0';
! 571:
1.214 schwarze 572: if (read_whole_file(curp, fd, &blk, &with_mmap) == -1)
1.205 schwarze 573: return;
574:
575: /*
576: * Save some properties of the parent file.
577: */
578:
579: save_primary = curp->primary;
580: save_filenc = curp->filenc;
581: save_lineno = curp->line;
582: save_filename = mandoc_msg_getinfilename();
1.28 joerg 583:
1.83 schwarze 584: curp->primary = &blk;
1.205 schwarze 585: curp->filenc = curp->options & (MPARSE_UTF8 | MPARSE_LATIN1);
1.28 joerg 586: curp->line = 1;
1.205 schwarze 587: mandoc_msg_setinfilename(filename);
1.28 joerg 588:
1.93 schwarze 589: /* Skip an UTF-8 byte order mark. */
590: if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
591: (unsigned char)blk.buf[0] == 0xef &&
592: (unsigned char)blk.buf[1] == 0xbb &&
593: (unsigned char)blk.buf[2] == 0xbf) {
1.95 schwarze 594: offset = 3;
1.93 schwarze 595: curp->filenc &= ~MPARSE_LATIN1;
1.95 schwarze 596: } else
597: offset = 0;
1.93 schwarze 598:
1.205 schwarze 599: recursion_depth++;
1.95 schwarze 600: mparse_buf_r(curp, blk, offset, 1);
1.111 schwarze 601: if (--recursion_depth == 0)
1.28 joerg 602: mparse_end(curp);
603:
1.205 schwarze 604: /*
605: * Clean up and restore saved parent properties.
606: */
1.28 joerg 607:
1.205 schwarze 608: if (with_mmap)
609: munmap(blk.buf, blk.sz);
610: else
611: free(blk.buf);
1.1 kristaps 612:
1.205 schwarze 613: curp->primary = save_primary;
614: curp->filenc = save_filenc;
615: curp->line = save_lineno;
616: if (save_filename != NULL)
617: mandoc_msg_setinfilename(save_filename);
1.82 schwarze 618: }
619:
1.148 schwarze 620: int
621: mparse_open(struct mparse *curp, const char *file)
1.82 schwarze 622: {
623: char *cp;
1.211 schwarze 624: int fd, save_errno;
1.82 schwarze 625:
1.140 schwarze 626: cp = strrchr(file, '.');
627: curp->gzip = (cp != NULL && ! strcmp(cp + 1, "gz"));
1.98 schwarze 628:
1.140 schwarze 629: /* First try to use the filename as it is. */
1.98 schwarze 630:
1.148 schwarze 631: if ((fd = open(file, O_RDONLY)) != -1)
632: return fd;
1.98 schwarze 633:
1.140 schwarze 634: /*
635: * If that doesn't work and the filename doesn't
636: * already end in .gz, try appending .gz.
637: */
1.98 schwarze 638:
1.140 schwarze 639: if ( ! curp->gzip) {
1.211 schwarze 640: save_errno = errno;
1.98 schwarze 641: mandoc_asprintf(&cp, "%s.gz", file);
1.149 schwarze 642: fd = open(cp, O_RDONLY);
1.108 schwarze 643: free(cp);
1.211 schwarze 644: errno = save_errno;
1.148 schwarze 645: if (fd != -1) {
1.140 schwarze 646: curp->gzip = 1;
1.148 schwarze 647: return fd;
1.82 schwarze 648: }
649: }
650:
1.140 schwarze 651: /* Neither worked, give up. */
1.97 schwarze 652:
1.148 schwarze 653: return -1;
1.1 kristaps 654: }
655:
656: struct mparse *
1.203 schwarze 657: mparse_alloc(int options, enum mandoc_os os_e, const char *os_s)
1.1 kristaps 658: {
659: struct mparse *curp;
1.10 kristaps 660:
1.1 kristaps 661: curp = mandoc_calloc(1, sizeof(struct mparse));
662:
1.44 schwarze 663: curp->options = options;
1.179 schwarze 664: curp->os_s = os_s;
1.1 kristaps 665:
1.207 schwarze 666: curp->roff = roff_alloc(options);
667: curp->man = roff_man_alloc(curp->roff, curp->os_s,
1.137 schwarze 668: curp->options & MPARSE_QUICK ? 1 : 0);
1.136 schwarze 669: if (curp->options & MPARSE_MDOC) {
1.209 schwarze 670: curp->man->meta.macroset = MACROSET_MDOC;
1.164 schwarze 671: if (curp->man->mdocmac == NULL)
672: curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137 schwarze 673: } else if (curp->options & MPARSE_MAN) {
1.209 schwarze 674: curp->man->meta.macroset = MACROSET_MAN;
1.164 schwarze 675: if (curp->man->manmac == NULL)
676: curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.136 schwarze 677: }
1.209 schwarze 678: curp->man->meta.first->tok = TOKEN_NONE;
1.179 schwarze 679: curp->man->meta.os_e = os_e;
1.215 schwarze 680: tag_alloc();
1.142 schwarze 681: return curp;
1.1 kristaps 682: }
683:
684: void
685: mparse_reset(struct mparse *curp)
686: {
1.216 schwarze 687: tag_free();
1.1 kristaps 688: roff_reset(curp->roff);
1.150 schwarze 689: roff_man_reset(curp->man);
1.198 schwarze 690: free_buf_list(curp->secondary);
691: curp->secondary = NULL;
1.159 schwarze 692: curp->gzip = 0;
1.216 schwarze 693: tag_alloc();
1.1 kristaps 694: }
695:
696: void
697: mparse_free(struct mparse *curp)
698: {
1.215 schwarze 699: tag_free();
1.164 schwarze 700: roffhash_free(curp->man->mdocmac);
701: roffhash_free(curp->man->manmac);
1.137 schwarze 702: roff_man_free(curp->man);
1.160 schwarze 703: roff_free(curp->roff);
1.198 schwarze 704: free_buf_list(curp->secondary);
1.1 kristaps 705: free(curp);
706: }
707:
1.209 schwarze 708: struct roff_meta *
709: mparse_result(struct mparse *curp)
1.1 kristaps 710: {
1.210 schwarze 711: roff_state_reset(curp->man);
1.209 schwarze 712: if (curp->options & MPARSE_VALIDATE) {
713: if (curp->man->meta.macroset == MACROSET_MDOC)
714: mdoc_validate(curp->man);
715: else
716: man_validate(curp->man);
1.218 schwarze 717: tag_postprocess(curp->man, curp->man->meta.first);
1.45 schwarze 718: }
1.209 schwarze 719: return &curp->man->meta;
1.24 kristaps 720: }
721:
722: void
1.198 schwarze 723: mparse_copy(const struct mparse *p)
1.24 kristaps 724: {
1.198 schwarze 725: struct buf *buf;
1.24 kristaps 726:
1.198 schwarze 727: for (buf = p->secondary; buf != NULL; buf = buf->next)
728: puts(buf->buf);
1.1 kristaps 729: }
CVSweb