Annotation of mandoc/main.c, Revision 1.130
1.130 ! kristaps 1: /* $Id: main.c,v 1.129 2011/01/01 15:45:18 kristaps Exp $ */
1.1 kristaps 2: /*
1.97 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.25 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.25 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.58 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.66 kristaps 22: #include <sys/mman.h>
1.1 kristaps 23: #include <sys/stat.h>
24:
25: #include <assert.h>
1.99 kristaps 26: #include <ctype.h>
1.1 kristaps 27: #include <fcntl.h>
28: #include <stdio.h>
1.45 kristaps 29: #include <stdint.h>
1.1 kristaps 30: #include <stdlib.h>
31: #include <string.h>
32: #include <unistd.h>
33:
1.71 kristaps 34: #include "mandoc.h"
1.90 kristaps 35: #include "main.h"
1.1 kristaps 36: #include "mdoc.h"
1.10 kristaps 37: #include "man.h"
1.71 kristaps 38: #include "roff.h"
1.101 joerg 39:
40: #ifndef MAP_FILE
41: #define MAP_FILE 0
42: #endif
1.1 kristaps 43:
1.122 schwarze 44: #define REPARSE_LIMIT 1000
1.45 kristaps 45: #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
46:
1.55 kristaps 47: /* FIXME: Intel's compiler? LLVM? pcc? */
48:
49: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56 kristaps 50: # if !defined(lint)
51: # define __attribute__(x)
52: # endif
1.55 kristaps 53: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16 kristaps 54:
1.42 kristaps 55: typedef void (*out_mdoc)(void *, const struct mdoc *);
56: typedef void (*out_man)(void *, const struct man *);
1.22 kristaps 57: typedef void (*out_free)(void *);
58:
1.5 kristaps 59: struct buf {
60: char *buf;
61: size_t sz;
62: };
63:
1.19 kristaps 64: enum intt {
65: INTT_AUTO,
66: INTT_MDOC,
67: INTT_MAN
68: };
69:
70: enum outt {
71: OUTT_ASCII = 0,
72: OUTT_TREE,
1.43 kristaps 73: OUTT_HTML,
1.59 kristaps 74: OUTT_XHTML,
1.85 kristaps 75: OUTT_LINT,
1.100 kristaps 76: OUTT_PS,
77: OUTT_PDF
1.19 kristaps 78: };
79:
1.5 kristaps 80: struct curparse {
1.22 kristaps 81: const char *file; /* Current parse. */
82: int fd; /* Current parse. */
1.111 kristaps 83: int line; /* Line number in the file. */
1.103 schwarze 84: enum mandoclevel wlevel; /* Ignore messages below this. */
85: int wstop; /* Stop after a file with a warning. */
1.79 kristaps 86: enum intt inttype; /* which parser to use */
1.112 kristaps 87: struct man *pman; /* persistent man parser */
88: struct mdoc *pmdoc; /* persistent mdoc parser */
1.79 kristaps 89: struct man *man; /* man parser */
90: struct mdoc *mdoc; /* mdoc parser */
91: struct roff *roff; /* roff parser (!NULL) */
1.92 kristaps 92: struct regset regs; /* roff registers */
1.122 schwarze 93: int reparse_count; /* finite interpolation stack */
1.79 kristaps 94: enum outt outtype; /* which output to use */
95: out_mdoc outmdoc; /* mdoc output ptr */
96: out_man outman; /* man output ptr */
97: out_free outfree; /* free output ptr */
98: void *outdata; /* data for output */
99: char outopts[BUFSIZ]; /* buf of output opts */
100: };
101:
1.103 schwarze 102: static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
103: "SUCCESS",
104: "RESERVED",
105: "WARNING",
106: "ERROR",
107: "FATAL",
108: "BADARG",
109: "SYSERR"
110: };
111:
112: static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = {
113: MANDOCERR_OK,
114: MANDOCERR_WARNING,
115: MANDOCERR_WARNING,
116: MANDOCERR_ERROR,
117: MANDOCERR_FATAL,
118: MANDOCERR_MAX,
119: MANDOCERR_MAX
120: };
121:
1.79 kristaps 122: static const char * const mandocerrs[MANDOCERR_MAX] = {
123: "ok",
1.94 schwarze 124:
125: "generic warning",
126:
1.121 kristaps 127: /* related to the prologue */
128: "no title in document",
129: "document title should be all caps",
130: "unknown manual section",
131: "cannot parse date argument",
132: "prologue macros out of order",
133: "duplicate prologue macro",
134: "macro not allowed in prologue",
135: "macro not allowed in body",
136:
137: /* related to document structure */
1.113 kristaps 138: ".so is fragile, better use ln(1)",
1.121 kristaps 139: "NAME section must come first",
140: "bad NAME section contents",
141: "manual name not yet set",
1.81 kristaps 142: "sections out of conventional order",
1.121 kristaps 143: "duplicate section name",
1.79 kristaps 144: "section not in conventional manual section",
1.121 kristaps 145:
146: /* related to macros and nesting */
147: "skipping obsolete macro",
1.125 kristaps 148: "skipping paragraph macro",
1.95 schwarze 149: "blocks badly nested",
1.121 kristaps 150: "child violates parent syntax",
151: "nested displays are not portable",
152: "already in literal mode",
1.94 schwarze 153:
1.121 kristaps 154: /* related to missing macro arguments */
155: "skipping empty macro",
156: "missing display type",
157: "list type must come first",
158: "tag lists require a width argument",
159: "missing font type",
1.94 schwarze 160:
1.121 kristaps 161: /* related to bad macro arguments */
162: "skipping argument",
163: "duplicate argument",
164: "duplicate display type",
165: "duplicate list type",
166: "unknown AT&T UNIX version",
1.79 kristaps 167: "bad Boolean value",
1.120 kristaps 168: "unknown font",
1.121 kristaps 169: "unknown standard specifier",
170: "bad width argument",
171:
172: /* related to plain text */
173: "blank line in non-literal context",
174: "tab in non-literal context",
175: "end of line whitespace",
1.79 kristaps 176: "bad comment style",
1.121 kristaps 177: "unknown escape sequence",
178: "unterminated quoted string",
179:
180: "generic error",
181:
1.126 kristaps 182: "bad table syntax",
183: "bad table option",
1.127 kristaps 184: "bad table layout",
185: "no table layout cells specified",
1.129 kristaps 186: "no table data cells specified",
1.122 schwarze 187: "input stack limit exceeded, infinite loop?",
1.121 kristaps 188: "skipping bad character",
189: "skipping text before the first section header",
190: "skipping unknown macro",
1.110 kristaps 191: "NOT IMPLEMENTED: skipping request",
1.79 kristaps 192: "line scope broken",
193: "argument count wrong",
1.121 kristaps 194: "skipping end of block that is not open",
195: "missing end of block",
1.106 schwarze 196: "scope open on exit",
1.117 kristaps 197: "uname(3) system call failed",
1.79 kristaps 198: "macro requires line argument(s)",
199: "macro requires body argument(s)",
200: "macro requires argument(s)",
1.82 kristaps 201: "missing list type",
1.79 kristaps 202: "line argument(s) will be lost",
203: "body argument(s) will be lost",
1.94 schwarze 204:
205: "generic fatal error",
206:
1.80 kristaps 207: "column syntax is inconsistent",
1.121 kristaps 208: "NOT IMPLEMENTED: .Bd -file",
1.79 kristaps 209: "line scope broken, syntax violated",
210: "argument count wrong, violates syntax",
211: "child violates parent syntax",
212: "argument count wrong, violates syntax",
1.113 kristaps 213: "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.79 kristaps 214: "no document body",
215: "no document prologue",
1.103 schwarze 216: "static buffer exhausted",
1.5 kristaps 217: };
1.1 kristaps 218:
1.111 kristaps 219: static void parsebuf(struct curparse *, struct buf, int);
220: static void pdesc(struct curparse *);
1.66 kristaps 221: static void fdesc(struct curparse *);
222: static void ffile(const char *, struct curparse *);
1.111 kristaps 223: static int pfile(const char *, struct curparse *);
1.10 kristaps 224: static int moptions(enum intt *, char *);
1.71 kristaps 225: static int mmsg(enum mandocerr, void *,
226: int, int, const char *);
1.112 kristaps 227: static void pset(const char *, int, struct curparse *);
1.66 kristaps 228: static int toptions(struct curparse *, char *);
229: static void usage(void) __attribute__((noreturn));
1.55 kristaps 230: static void version(void) __attribute__((noreturn));
1.103 schwarze 231: static int woptions(struct curparse *, char *);
1.1 kristaps 232:
1.54 kristaps 233: static const char *progname;
1.115 schwarze 234: static enum mandoclevel file_status = MANDOCLEVEL_OK;
1.103 schwarze 235: static enum mandoclevel exit_status = MANDOCLEVEL_OK;
1.1 kristaps 236:
237: int
238: main(int argc, char *argv[])
239: {
1.64 kristaps 240: int c;
1.5 kristaps 241: struct curparse curp;
1.1 kristaps 242:
1.54 kristaps 243: progname = strrchr(argv[0], '/');
244: if (progname == NULL)
245: progname = argv[0];
246: else
247: ++progname;
248:
1.51 kristaps 249: memset(&curp, 0, sizeof(struct curparse));
1.5 kristaps 250:
1.19 kristaps 251: curp.inttype = INTT_AUTO;
1.22 kristaps 252: curp.outtype = OUTT_ASCII;
1.103 schwarze 253: curp.wlevel = MANDOCLEVEL_FATAL;
1.19 kristaps 254:
1.1 kristaps 255: /* LINTED */
1.103 schwarze 256: while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
1.1 kristaps 257: switch (c) {
1.10 kristaps 258: case ('m'):
1.19 kristaps 259: if ( ! moptions(&curp.inttype, optarg))
1.105 kristaps 260: return((int)MANDOCLEVEL_BADARG);
1.10 kristaps 261: break;
1.48 kristaps 262: case ('O'):
1.49 kristaps 263: (void)strlcat(curp.outopts, optarg, BUFSIZ);
264: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 265: break;
1.1 kristaps 266: case ('T'):
1.60 kristaps 267: if ( ! toptions(&curp, optarg))
1.105 kristaps 268: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 269: break;
270: case ('W'):
1.103 schwarze 271: if ( ! woptions(&curp, optarg))
1.105 kristaps 272: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 273: break;
274: case ('V'):
275: version();
276: /* NOTREACHED */
277: default:
278: usage();
279: /* NOTREACHED */
280: }
281:
282: argc -= optind;
283: argv += optind;
284:
1.30 kristaps 285: if (NULL == *argv) {
286: curp.file = "<stdin>";
287: curp.fd = STDIN_FILENO;
1.39 kristaps 288:
1.66 kristaps 289: fdesc(&curp);
1.64 kristaps 290: }
291:
292: while (*argv) {
1.66 kristaps 293: ffile(*argv, &curp);
1.103 schwarze 294: if (MANDOCLEVEL_OK != exit_status && curp.wstop)
1.64 kristaps 295: break;
296: ++argv;
1.1 kristaps 297: }
298:
1.22 kristaps 299: if (curp.outfree)
300: (*curp.outfree)(curp.outdata);
1.112 kristaps 301: if (curp.pmdoc)
302: mdoc_free(curp.pmdoc);
303: if (curp.pman)
304: man_free(curp.pman);
1.71 kristaps 305: if (curp.roff)
306: roff_free(curp.roff);
1.1 kristaps 307:
1.105 kristaps 308: return((int)exit_status);
1.1 kristaps 309: }
310:
311:
1.55 kristaps 312: static void
1.1 kristaps 313: version(void)
314: {
315:
1.54 kristaps 316: (void)printf("%s %s\n", progname, VERSION);
1.105 kristaps 317: exit((int)MANDOCLEVEL_OK);
1.1 kristaps 318: }
319:
320:
1.55 kristaps 321: static void
1.1 kristaps 322: usage(void)
323: {
324:
1.112 kristaps 325: (void)fprintf(stderr, "usage: %s "
326: "[-V] "
327: "[-foption] "
328: "[-mformat] "
329: "[-Ooption] "
330: "[-Toutput] "
331: "[-Werr] "
332: "[file...]\n",
333: progname);
334:
1.105 kristaps 335: exit((int)MANDOCLEVEL_BADARG);
1.19 kristaps 336: }
337:
1.64 kristaps 338: static void
1.66 kristaps 339: ffile(const char *file, struct curparse *curp)
1.1 kristaps 340: {
341:
1.112 kristaps 342: /*
343: * Called once per input file. Get the file ready for reading,
344: * pass it through to the parser-driver, then close it out.
345: * XXX: don't do anything special as this is only called for
346: * files; stdin goes directly to fdesc().
347: */
348:
1.19 kristaps 349: curp->file = file;
1.112 kristaps 350:
1.19 kristaps 351: if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
1.53 kristaps 352: perror(curp->file);
1.103 schwarze 353: exit_status = MANDOCLEVEL_SYSERR;
1.64 kristaps 354: return;
1.1 kristaps 355: }
356:
1.66 kristaps 357: fdesc(curp);
1.1 kristaps 358:
1.19 kristaps 359: if (-1 == close(curp->fd))
1.53 kristaps 360: perror(curp->file);
1.1 kristaps 361: }
362:
1.111 kristaps 363: static int
364: pfile(const char *file, struct curparse *curp)
365: {
366: const char *savefile;
367: int fd, savefd;
368:
369: if (-1 == (fd = open(file, O_RDONLY, 0))) {
370: perror(file);
1.115 schwarze 371: file_status = MANDOCLEVEL_SYSERR;
1.111 kristaps 372: return(0);
373: }
374:
375: savefile = curp->file;
376: savefd = curp->fd;
377:
378: curp->file = file;
379: curp->fd = fd;
380:
381: pdesc(curp);
382:
383: curp->file = savefile;
384: curp->fd = savefd;
385:
386: if (-1 == close(fd))
387: perror(file);
388:
1.115 schwarze 389: return(MANDOCLEVEL_FATAL > file_status ? 1 : 0);
1.111 kristaps 390: }
391:
1.1 kristaps 392:
1.103 schwarze 393: static void
1.68 joerg 394: resize_buf(struct buf *buf, size_t initial)
395: {
396:
1.124 schwarze 397: buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
1.103 schwarze 398: buf->buf = realloc(buf->buf, buf->sz);
399: if (NULL == buf->buf) {
1.68 joerg 400: perror(NULL);
1.105 kristaps 401: exit((int)MANDOCLEVEL_SYSERR);
1.68 joerg 402: }
403: }
404:
405:
406: static int
1.66 kristaps 407: read_whole_file(struct curparse *curp, struct buf *fb, int *with_mmap)
408: {
409: struct stat st;
1.68 joerg 410: size_t off;
1.66 kristaps 411: ssize_t ssz;
412:
413: if (-1 == fstat(curp->fd, &st)) {
414: perror(curp->file);
415: return(0);
416: }
417:
418: /*
419: * If we're a regular file, try just reading in the whole entry
420: * via mmap(). This is faster than reading it into blocks, and
421: * since each file is only a few bytes to begin with, I'm not
422: * concerned that this is going to tank any machines.
423: */
424:
425: if (S_ISREG(st.st_mode)) {
426: if (st.st_size >= (1U << 31)) {
427: fprintf(stderr, "%s: input too large\n",
428: curp->file);
429: return(0);
430: }
431: *with_mmap = 1;
1.71 kristaps 432: fb->sz = (size_t)st.st_size;
1.66 kristaps 433: fb->buf = mmap(NULL, fb->sz, PROT_READ,
1.83 joerg 434: MAP_FILE|MAP_SHARED, curp->fd, 0);
1.66 kristaps 435: if (fb->buf != MAP_FAILED)
436: return(1);
437: }
438:
439: /*
440: * If this isn't a regular file (like, say, stdin), then we must
441: * go the old way and just read things in bit by bit.
442: */
443:
444: *with_mmap = 0;
445: off = 0;
446: fb->sz = 0;
447: fb->buf = NULL;
448: for (;;) {
449: if (off == fb->sz) {
450: if (fb->sz == (1U << 31)) {
451: fprintf(stderr, "%s: input too large\n",
452: curp->file);
453: break;
454: }
1.103 schwarze 455: resize_buf(fb, 65536);
1.66 kristaps 456: }
1.71 kristaps 457: ssz = read(curp->fd, fb->buf + (int)off, fb->sz - off);
1.66 kristaps 458: if (ssz == 0) {
459: fb->sz = off;
460: return(1);
461: }
462: if (ssz == -1) {
463: perror(curp->file);
464: break;
465: }
1.71 kristaps 466: off += (size_t)ssz;
1.66 kristaps 467: }
468:
469: free(fb->buf);
470: fb->buf = NULL;
471: return(0);
472: }
473:
474:
1.64 kristaps 475: static void
1.66 kristaps 476: fdesc(struct curparse *curp)
1.1 kristaps 477: {
1.112 kristaps 478:
479: /*
480: * Called once per file with an opened file descriptor. All
481: * pre-file-parse operations (whether stdin or a file) should go
482: * here.
483: *
484: * This calls down into the nested parser, which drills down and
485: * fully parses a file and all its dependences (i.e., `so'). It
486: * then runs the cleanup validators and pushes to output.
487: */
488:
489: /* Zero the parse type. */
490:
491: curp->mdoc = NULL;
492: curp->man = NULL;
1.115 schwarze 493: file_status = MANDOCLEVEL_OK;
1.112 kristaps 494:
495: /* Make sure the mandotory roff parser is initialised. */
496:
497: if (NULL == curp->roff) {
498: curp->roff = roff_alloc(&curp->regs, curp, mmsg);
499: assert(curp->roff);
500: }
501:
502: /* Fully parse the file. */
1.10 kristaps 503:
1.111 kristaps 504: pdesc(curp);
1.92 kristaps 505:
1.115 schwarze 506: if (MANDOCLEVEL_FATAL <= file_status)
1.111 kristaps 507: goto cleanup;
508:
509: /* NOTE a parser may not have been assigned, yet. */
510:
1.112 kristaps 511: if ( ! (curp->man || curp->mdoc)) {
1.111 kristaps 512: fprintf(stderr, "%s: Not a manual\n", curp->file);
1.115 schwarze 513: file_status = MANDOCLEVEL_FATAL;
1.111 kristaps 514: goto cleanup;
515: }
516:
517: /* Clean up the parse routine ASTs. */
518:
1.112 kristaps 519: if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
1.115 schwarze 520: assert(MANDOCLEVEL_FATAL <= file_status);
1.111 kristaps 521: goto cleanup;
522: }
1.112 kristaps 523:
524: if (curp->man && ! man_endparse(curp->man)) {
1.115 schwarze 525: assert(MANDOCLEVEL_FATAL <= file_status);
1.111 kristaps 526: goto cleanup;
527: }
1.112 kristaps 528:
529: assert(curp->roff);
1.130 ! kristaps 530: roff_endparse(curp->roff);
1.1 kristaps 531:
532: /*
1.111 kristaps 533: * With -Wstop and warnings or errors of at least
534: * the requested level, do not produce output.
1.1 kristaps 535: */
536:
1.115 schwarze 537: if (MANDOCLEVEL_OK != file_status && curp->wstop)
1.111 kristaps 538: goto cleanup;
539:
540: /* If unset, allocate output dev now (if applicable). */
541:
542: if ( ! (curp->outman && curp->outmdoc)) {
543: switch (curp->outtype) {
544: case (OUTT_XHTML):
545: curp->outdata = xhtml_alloc(curp->outopts);
546: break;
547: case (OUTT_HTML):
548: curp->outdata = html_alloc(curp->outopts);
549: break;
550: case (OUTT_ASCII):
551: curp->outdata = ascii_alloc(curp->outopts);
552: curp->outfree = ascii_free;
553: break;
554: case (OUTT_PDF):
555: curp->outdata = pdf_alloc(curp->outopts);
556: curp->outfree = pspdf_free;
557: break;
558: case (OUTT_PS):
559: curp->outdata = ps_alloc(curp->outopts);
560: curp->outfree = pspdf_free;
561: break;
562: default:
563: break;
564: }
565:
566: switch (curp->outtype) {
567: case (OUTT_HTML):
568: /* FALLTHROUGH */
569: case (OUTT_XHTML):
570: curp->outman = html_man;
571: curp->outmdoc = html_mdoc;
572: curp->outfree = html_free;
573: break;
574: case (OUTT_TREE):
575: curp->outman = tree_man;
576: curp->outmdoc = tree_mdoc;
577: break;
578: case (OUTT_PDF):
579: /* FALLTHROUGH */
580: case (OUTT_ASCII):
581: /* FALLTHROUGH */
582: case (OUTT_PS):
583: curp->outman = terminal_man;
584: curp->outmdoc = terminal_mdoc;
585: break;
586: default:
587: break;
588: }
589: }
590:
591: /* Execute the out device, if it exists. */
592:
1.112 kristaps 593: if (curp->man && curp->outman)
594: (*curp->outman)(curp->outdata, curp->man);
595: if (curp->mdoc && curp->outmdoc)
596: (*curp->outmdoc)(curp->outdata, curp->mdoc);
1.111 kristaps 597:
598: cleanup:
1.112 kristaps 599:
1.111 kristaps 600: memset(&curp->regs, 0, sizeof(struct regset));
1.112 kristaps 601:
602: /* Reset the current-parse compilers. */
603:
604: if (curp->mdoc)
605: mdoc_reset(curp->mdoc);
606: if (curp->man)
607: man_reset(curp->man);
608:
609: assert(curp->roff);
610: roff_reset(curp->roff);
1.111 kristaps 611:
1.115 schwarze 612: if (exit_status < file_status)
613: exit_status = file_status;
614:
1.111 kristaps 615: return;
616: }
617:
618: static void
619: pdesc(struct curparse *curp)
620: {
621: struct buf blk;
622: int with_mmap;
623:
1.112 kristaps 624: /*
625: * Run for each opened file; may be called more than once for
626: * each full parse sequence if the opened file is nested (i.e.,
627: * from `so'). Simply sucks in the whole file and moves into
628: * the parse phase for the file.
629: */
630:
1.103 schwarze 631: if ( ! read_whole_file(curp, &blk, &with_mmap)) {
1.115 schwarze 632: file_status = MANDOCLEVEL_SYSERR;
1.64 kristaps 633: return;
1.103 schwarze 634: }
1.1 kristaps 635:
1.112 kristaps 636: /* Line number is per-file. */
1.111 kristaps 637:
638: curp->line = 1;
1.112 kristaps 639:
1.111 kristaps 640: parsebuf(curp, blk, 1);
641:
642: if (with_mmap)
643: munmap(blk.buf, blk.sz);
644: else
645: free(blk.buf);
646: }
647:
648: static void
649: parsebuf(struct curparse *curp, struct buf blk, int start)
650: {
651: struct buf ln;
1.114 kristaps 652: enum rofferr rr;
1.112 kristaps 653: int i, of, rc;
654: int pos; /* byte number in the ln buffer */
655: int lnn; /* line number in the real file */
1.111 kristaps 656: unsigned char c;
1.112 kristaps 657:
658: /*
659: * Main parse routine for an opened file. This is called for
660: * each opened file and simply loops around the full input file,
661: * possibly nesting (i.e., with `so').
662: */
1.71 kristaps 663:
1.111 kristaps 664: memset(&ln, 0, sizeof(struct buf));
665:
1.112 kristaps 666: lnn = curp->line;
667: pos = 0;
1.111 kristaps 668:
1.112 kristaps 669: for (i = 0; i < (int)blk.sz; ) {
1.111 kristaps 670: if (0 == pos && '\0' == blk.buf[i])
671: break;
1.112 kristaps 672:
1.122 schwarze 673: if (start) {
1.111 kristaps 674: curp->line = lnn;
1.122 schwarze 675: curp->reparse_count = 0;
676: }
1.111 kristaps 677:
678: while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
1.70 joerg 679: if ('\n' == blk.buf[i]) {
680: ++i;
681: ++lnn;
682: break;
683: }
1.99 kristaps 684:
685: /*
686: * Warn about bogus characters. If you're using
687: * non-ASCII encoding, you're screwing your
688: * readers. Since I'd rather this not happen,
689: * I'll be helpful and drop these characters so
690: * we don't display gibberish. Note to manual
691: * writers: use special characters.
692: */
693:
1.102 schwarze 694: c = (unsigned char) blk.buf[i];
1.112 kristaps 695:
696: if ( ! (isascii(c) &&
697: (isgraph(c) || isblank(c)))) {
1.103 schwarze 698: mmsg(MANDOCERR_BADCHAR, curp,
1.111 kristaps 699: curp->line, pos, "ignoring byte");
1.99 kristaps 700: i++;
701: continue;
702: }
703:
1.112 kristaps 704: /* Trailing backslash = a plain char. */
705:
1.70 joerg 706: if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
707: if (pos >= (int)ln.sz)
1.103 schwarze 708: resize_buf(&ln, 256);
1.70 joerg 709: ln.buf[pos++] = blk.buf[i++];
1.67 joerg 710: continue;
1.70 joerg 711: }
1.112 kristaps 712:
713: /* Found escape & at least one other char. */
714:
1.70 joerg 715: if ('\n' == blk.buf[i + 1]) {
1.112 kristaps 716: i += 2;
1.70 joerg 717: /* Escaped newlines are skipped over */
718: ++lnn;
1.67 joerg 719: continue;
720: }
1.112 kristaps 721:
1.70 joerg 722: if ('"' == blk.buf[i + 1]) {
723: i += 2;
724: /* Comment, skip to end of line */
725: for (; i < (int)blk.sz; ++i) {
726: if ('\n' == blk.buf[i]) {
727: ++i;
728: ++lnn;
729: break;
730: }
731: }
1.112 kristaps 732:
1.70 joerg 733: /* Backout trailing whitespaces */
734: for (; pos > 0; --pos) {
735: if (ln.buf[pos - 1] != ' ')
736: break;
737: if (pos > 2 && ln.buf[pos - 2] == '\\')
738: break;
739: }
740: break;
741: }
1.112 kristaps 742:
743: /* Some other escape sequence, copy & cont. */
744:
1.70 joerg 745: if (pos + 1 >= (int)ln.sz)
1.103 schwarze 746: resize_buf(&ln, 256);
1.1 kristaps 747:
1.70 joerg 748: ln.buf[pos++] = blk.buf[i++];
749: ln.buf[pos++] = blk.buf[i++];
1.67 joerg 750: }
1.1 kristaps 751:
1.70 joerg 752: if (pos >= (int)ln.sz)
1.103 schwarze 753: resize_buf(&ln, 256);
1.112 kristaps 754:
1.71 kristaps 755: ln.buf[pos] = '\0';
756:
1.76 kristaps 757: /*
758: * A significant amount of complexity is contained by
759: * the roff preprocessor. It's line-oriented but can be
760: * expressed on one line, so we need at times to
761: * readjust our starting point and re-run it. The roff
762: * preprocessor can also readjust the buffers with new
763: * data, so we pass them in wholesale.
764: */
765:
766: of = 0;
1.112 kristaps 767:
1.111 kristaps 768: rerun:
1.114 kristaps 769: rr = roff_parseln
1.112 kristaps 770: (curp->roff, curp->line,
771: &ln.buf, &ln.sz, of, &of);
772:
1.114 kristaps 773: switch (rr) {
1.111 kristaps 774: case (ROFF_REPARSE):
1.122 schwarze 775: if (REPARSE_LIMIT >= ++curp->reparse_count)
776: parsebuf(curp, ln, 0);
777: else
778: mmsg(MANDOCERR_ROFFLOOP, curp,
779: curp->line, pos, NULL);
1.111 kristaps 780: pos = 0;
781: continue;
782: case (ROFF_APPEND):
783: pos = strlen(ln.buf);
784: continue;
785: case (ROFF_RERUN):
786: goto rerun;
787: case (ROFF_IGN):
788: pos = 0;
1.71 kristaps 789: continue;
1.111 kristaps 790: case (ROFF_ERR):
1.115 schwarze 791: assert(MANDOCLEVEL_FATAL <= file_status);
1.111 kristaps 792: break;
793: case (ROFF_SO):
794: if (pfile(ln.buf + of, curp)) {
795: pos = 0;
796: continue;
797: } else
798: break;
1.128 kristaps 799: default:
1.111 kristaps 800: break;
1.103 schwarze 801: }
1.29 kristaps 802:
1.76 kristaps 803: /*
804: * If input parsers have not been allocated, do so now.
805: * We keep these instanced betwen parsers, but set them
806: * locally per parse routine since we can use different
807: * parsers with each one.
808: */
1.19 kristaps 809:
1.112 kristaps 810: if ( ! (curp->man || curp->mdoc))
811: pset(ln.buf + of, pos - of, curp);
1.19 kristaps 812:
1.112 kristaps 813: /*
814: * Lastly, push down into the parsers themselves. One
815: * of these will have already been set in the pset()
816: * routine.
1.128 kristaps 817: * If libroff returns ROFF_TBL, then add it to the
818: * currently open parse. Since we only get here if
819: * there does exist data (see tbl_data.c), we're
820: * guaranteed that something's been allocated.
1.112 kristaps 821: */
822:
1.128 kristaps 823: if (ROFF_TBL == rr) {
824: assert(curp->man || curp->mdoc);
825: if (curp->man)
826: man_addspan(curp->man, roff_span(curp->roff));
827: else
828: mdoc_addspan(curp->mdoc, roff_span(curp->roff));
829:
830: } else if (curp->man || curp->mdoc) {
1.112 kristaps 831: rc = curp->man ?
832: man_parseln(curp->man,
833: curp->line, ln.buf, of) :
834: mdoc_parseln(curp->mdoc,
835: curp->line, ln.buf, of);
1.30 kristaps 836:
1.112 kristaps 837: if ( ! rc) {
1.115 schwarze 838: assert(MANDOCLEVEL_FATAL <= file_status);
1.112 kristaps 839: break;
840: }
1.103 schwarze 841: }
1.19 kristaps 842:
1.111 kristaps 843: /* Temporary buffers typically are not full. */
1.112 kristaps 844:
1.111 kristaps 845: if (0 == start && '\0' == blk.buf[i])
1.59 kristaps 846: break;
1.85 kristaps 847:
1.111 kristaps 848: /* Start the next input line. */
1.112 kristaps 849:
1.111 kristaps 850: pos = 0;
1.22 kristaps 851: }
852:
1.111 kristaps 853: free(ln.buf);
1.19 kristaps 854: }
855:
1.103 schwarze 856: static void
1.112 kristaps 857: pset(const char *buf, int pos, struct curparse *curp)
1.19 kristaps 858: {
1.29 kristaps 859: int i;
1.19 kristaps 860:
861: /*
862: * Try to intuit which kind of manual parser should be used. If
863: * passed in by command-line (-man, -mdoc), then use that
864: * explicitly. If passed as -mandoc, then try to guess from the
1.30 kristaps 865: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.19 kristaps 866: * default to -man, which is more lenient.
1.112 kristaps 867: *
868: * Separate out pmdoc/pman from mdoc/man: the first persists
869: * through all parsers, while the latter is used per-parse.
1.19 kristaps 870: */
871:
1.75 kristaps 872: if ('.' == buf[0] || '\'' == buf[0]) {
1.29 kristaps 873: for (i = 1; buf[i]; i++)
874: if (' ' != buf[i] && '\t' != buf[i])
875: break;
1.103 schwarze 876: if ('\0' == buf[i])
877: return;
1.29 kristaps 878: }
1.10 kristaps 879:
1.19 kristaps 880: switch (curp->inttype) {
881: case (INTT_MDOC):
1.112 kristaps 882: if (NULL == curp->pmdoc)
883: curp->pmdoc = mdoc_alloc
884: (&curp->regs, curp, mmsg);
885: assert(curp->pmdoc);
886: curp->mdoc = curp->pmdoc;
1.103 schwarze 887: return;
1.19 kristaps 888: case (INTT_MAN):
1.112 kristaps 889: if (NULL == curp->pman)
890: curp->pman = man_alloc
891: (&curp->regs, curp, mmsg);
892: assert(curp->pman);
893: curp->man = curp->pman;
1.103 schwarze 894: return;
1.19 kristaps 895: default:
896: break;
897: }
898:
899: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
1.112 kristaps 900: if (NULL == curp->pmdoc)
901: curp->pmdoc = mdoc_alloc
902: (&curp->regs, curp, mmsg);
903: assert(curp->pmdoc);
904: curp->mdoc = curp->pmdoc;
1.103 schwarze 905: return;
1.19 kristaps 906: }
907:
1.112 kristaps 908: if (NULL == curp->pman)
909: curp->pman = man_alloc(&curp->regs, curp, mmsg);
910: assert(curp->pman);
911: curp->man = curp->pman;
1.10 kristaps 912: }
913:
914: static int
915: moptions(enum intt *tflags, char *arg)
916: {
917:
1.17 kristaps 918: if (0 == strcmp(arg, "doc"))
1.10 kristaps 919: *tflags = INTT_MDOC;
1.19 kristaps 920: else if (0 == strcmp(arg, "andoc"))
921: *tflags = INTT_AUTO;
1.17 kristaps 922: else if (0 == strcmp(arg, "an"))
1.10 kristaps 923: *tflags = INTT_MAN;
924: else {
1.57 kristaps 925: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 926: return(0);
927: }
928:
929: return(1);
1.1 kristaps 930: }
931:
932: static int
1.60 kristaps 933: toptions(struct curparse *curp, char *arg)
1.1 kristaps 934: {
935:
936: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 937: curp->outtype = OUTT_ASCII;
938: else if (0 == strcmp(arg, "lint")) {
939: curp->outtype = OUTT_LINT;
1.103 schwarze 940: curp->wlevel = MANDOCLEVEL_WARNING;
1.60 kristaps 941: }
1.1 kristaps 942: else if (0 == strcmp(arg, "tree"))
1.60 kristaps 943: curp->outtype = OUTT_TREE;
1.43 kristaps 944: else if (0 == strcmp(arg, "html"))
1.60 kristaps 945: curp->outtype = OUTT_HTML;
1.59 kristaps 946: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 947: curp->outtype = OUTT_XHTML;
1.85 kristaps 948: else if (0 == strcmp(arg, "ps"))
949: curp->outtype = OUTT_PS;
1.100 kristaps 950: else if (0 == strcmp(arg, "pdf"))
951: curp->outtype = OUTT_PDF;
1.1 kristaps 952: else {
1.57 kristaps 953: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 954: return(0);
955: }
956:
957: return(1);
958: }
959:
960: static int
1.103 schwarze 961: woptions(struct curparse *curp, char *arg)
1.1 kristaps 962: {
1.34 kristaps 963: char *v, *o;
1.103 schwarze 964: const char *toks[6];
1.1 kristaps 965:
1.103 schwarze 966: toks[0] = "stop";
967: toks[1] = "all";
968: toks[2] = "warning";
969: toks[3] = "error";
970: toks[4] = "fatal";
971: toks[5] = NULL;
1.1 kristaps 972:
1.34 kristaps 973: while (*arg) {
974: o = arg;
1.45 kristaps 975: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 976: case (0):
1.103 schwarze 977: curp->wstop = 1;
1.1 kristaps 978: break;
979: case (1):
1.103 schwarze 980: /* FALLTHROUGH */
1.1 kristaps 981: case (2):
1.103 schwarze 982: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 983: break;
1.20 kristaps 984: case (3):
1.103 schwarze 985: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 986: break;
987: case (4):
1.103 schwarze 988: curp->wlevel = MANDOCLEVEL_FATAL;
1.50 kristaps 989: break;
1.1 kristaps 990: default:
1.103 schwarze 991: fprintf(stderr, "-W%s: Bad argument\n", o);
1.1 kristaps 992: return(0);
993: }
1.34 kristaps 994: }
1.1 kristaps 995:
996: return(1);
997: }
998:
1.71 kristaps 999: static int
1000: mmsg(enum mandocerr t, void *arg, int ln, int col, const char *msg)
1001: {
1002: struct curparse *cp;
1.103 schwarze 1003: enum mandoclevel level;
1004:
1005: level = MANDOCLEVEL_FATAL;
1006: while (t < mandoclimits[level])
1.105 kristaps 1007: /* LINTED */
1.103 schwarze 1008: level--;
1.71 kristaps 1009:
1010: cp = (struct curparse *)arg;
1.103 schwarze 1011: if (level < cp->wlevel)
1012: return(1);
1.73 kristaps 1013:
1.103 schwarze 1014: fprintf(stderr, "%s:%d:%d: %s: %s",
1015: cp->file, ln, col + 1, mandoclevels[level], mandocerrs[t]);
1.73 kristaps 1016: if (msg)
1017: fprintf(stderr, ": %s", msg);
1018: fputc('\n', stderr);
1.79 kristaps 1019:
1.115 schwarze 1020: if (file_status < level)
1021: file_status = level;
1.103 schwarze 1022:
1023: return(level < MANDOCLEVEL_FATAL);
1.71 kristaps 1024: }
CVSweb