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