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