Annotation of mandoc/main.c, Revision 1.127
1.127 ! kristaps 1: /* $Id: main.c,v 1.126 2010/12/29 01:16:57 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.122 schwarze 186: "input stack limit exceeded, infinite loop?",
1.121 kristaps 187: "skipping bad character",
188: "skipping text before the first section header",
189: "skipping unknown macro",
1.110 kristaps 190: "NOT IMPLEMENTED: skipping request",
1.79 kristaps 191: "line scope broken",
192: "argument count wrong",
1.121 kristaps 193: "skipping end of block that is not open",
194: "missing end of block",
1.106 schwarze 195: "scope open on exit",
1.117 kristaps 196: "uname(3) system call failed",
1.79 kristaps 197: "macro requires line argument(s)",
198: "macro requires body argument(s)",
199: "macro requires argument(s)",
1.82 kristaps 200: "missing list type",
1.79 kristaps 201: "line argument(s) will be lost",
202: "body argument(s) will be lost",
1.94 schwarze 203:
204: "generic fatal error",
205:
1.80 kristaps 206: "column syntax is inconsistent",
1.121 kristaps 207: "NOT IMPLEMENTED: .Bd -file",
1.79 kristaps 208: "line scope broken, syntax violated",
209: "argument count wrong, violates syntax",
210: "child violates parent syntax",
211: "argument count wrong, violates syntax",
1.113 kristaps 212: "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.79 kristaps 213: "no document body",
214: "no document prologue",
1.103 schwarze 215: "static buffer exhausted",
1.5 kristaps 216: };
1.1 kristaps 217:
1.111 kristaps 218: static void parsebuf(struct curparse *, struct buf, int);
219: static void pdesc(struct curparse *);
1.66 kristaps 220: static void fdesc(struct curparse *);
221: static void ffile(const char *, struct curparse *);
1.111 kristaps 222: static int pfile(const char *, struct curparse *);
1.10 kristaps 223: static int moptions(enum intt *, char *);
1.71 kristaps 224: static int mmsg(enum mandocerr, void *,
225: int, int, const char *);
1.112 kristaps 226: static void pset(const char *, int, struct curparse *);
1.66 kristaps 227: static int toptions(struct curparse *, char *);
228: static void usage(void) __attribute__((noreturn));
1.55 kristaps 229: static void version(void) __attribute__((noreturn));
1.103 schwarze 230: static int woptions(struct curparse *, char *);
1.1 kristaps 231:
1.54 kristaps 232: static const char *progname;
1.115 schwarze 233: static enum mandoclevel file_status = MANDOCLEVEL_OK;
1.103 schwarze 234: static enum mandoclevel exit_status = MANDOCLEVEL_OK;
1.1 kristaps 235:
236: int
237: main(int argc, char *argv[])
238: {
1.64 kristaps 239: int c;
1.5 kristaps 240: struct curparse curp;
1.1 kristaps 241:
1.54 kristaps 242: progname = strrchr(argv[0], '/');
243: if (progname == NULL)
244: progname = argv[0];
245: else
246: ++progname;
247:
1.51 kristaps 248: memset(&curp, 0, sizeof(struct curparse));
1.5 kristaps 249:
1.19 kristaps 250: curp.inttype = INTT_AUTO;
1.22 kristaps 251: curp.outtype = OUTT_ASCII;
1.103 schwarze 252: curp.wlevel = MANDOCLEVEL_FATAL;
1.19 kristaps 253:
1.1 kristaps 254: /* LINTED */
1.103 schwarze 255: while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
1.1 kristaps 256: switch (c) {
1.10 kristaps 257: case ('m'):
1.19 kristaps 258: if ( ! moptions(&curp.inttype, optarg))
1.105 kristaps 259: return((int)MANDOCLEVEL_BADARG);
1.10 kristaps 260: break;
1.48 kristaps 261: case ('O'):
1.49 kristaps 262: (void)strlcat(curp.outopts, optarg, BUFSIZ);
263: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 264: break;
1.1 kristaps 265: case ('T'):
1.60 kristaps 266: if ( ! toptions(&curp, optarg))
1.105 kristaps 267: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 268: break;
269: case ('W'):
1.103 schwarze 270: if ( ! woptions(&curp, optarg))
1.105 kristaps 271: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 272: break;
273: case ('V'):
274: version();
275: /* NOTREACHED */
276: default:
277: usage();
278: /* NOTREACHED */
279: }
280:
281: argc -= optind;
282: argv += optind;
283:
1.30 kristaps 284: if (NULL == *argv) {
285: curp.file = "<stdin>";
286: curp.fd = STDIN_FILENO;
1.39 kristaps 287:
1.66 kristaps 288: fdesc(&curp);
1.64 kristaps 289: }
290:
291: while (*argv) {
1.66 kristaps 292: ffile(*argv, &curp);
1.103 schwarze 293: if (MANDOCLEVEL_OK != exit_status && curp.wstop)
1.64 kristaps 294: break;
295: ++argv;
1.1 kristaps 296: }
297:
1.22 kristaps 298: if (curp.outfree)
299: (*curp.outfree)(curp.outdata);
1.112 kristaps 300: if (curp.pmdoc)
301: mdoc_free(curp.pmdoc);
302: if (curp.pman)
303: man_free(curp.pman);
1.71 kristaps 304: if (curp.roff)
305: roff_free(curp.roff);
1.1 kristaps 306:
1.105 kristaps 307: return((int)exit_status);
1.1 kristaps 308: }
309:
310:
1.55 kristaps 311: static void
1.1 kristaps 312: version(void)
313: {
314:
1.54 kristaps 315: (void)printf("%s %s\n", progname, VERSION);
1.105 kristaps 316: exit((int)MANDOCLEVEL_OK);
1.1 kristaps 317: }
318:
319:
1.55 kristaps 320: static void
1.1 kristaps 321: usage(void)
322: {
323:
1.112 kristaps 324: (void)fprintf(stderr, "usage: %s "
325: "[-V] "
326: "[-foption] "
327: "[-mformat] "
328: "[-Ooption] "
329: "[-Toutput] "
330: "[-Werr] "
331: "[file...]\n",
332: progname);
333:
1.105 kristaps 334: exit((int)MANDOCLEVEL_BADARG);
1.19 kristaps 335: }
336:
1.64 kristaps 337: static void
1.66 kristaps 338: ffile(const char *file, struct curparse *curp)
1.1 kristaps 339: {
340:
1.112 kristaps 341: /*
342: * Called once per input file. Get the file ready for reading,
343: * pass it through to the parser-driver, then close it out.
344: * XXX: don't do anything special as this is only called for
345: * files; stdin goes directly to fdesc().
346: */
347:
1.19 kristaps 348: curp->file = file;
1.112 kristaps 349:
1.19 kristaps 350: if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
1.53 kristaps 351: perror(curp->file);
1.103 schwarze 352: exit_status = MANDOCLEVEL_SYSERR;
1.64 kristaps 353: return;
1.1 kristaps 354: }
355:
1.66 kristaps 356: fdesc(curp);
1.1 kristaps 357:
1.19 kristaps 358: if (-1 == close(curp->fd))
1.53 kristaps 359: perror(curp->file);
1.1 kristaps 360: }
361:
1.111 kristaps 362: static int
363: pfile(const char *file, struct curparse *curp)
364: {
365: const char *savefile;
366: int fd, savefd;
367:
368: if (-1 == (fd = open(file, O_RDONLY, 0))) {
369: perror(file);
1.115 schwarze 370: file_status = MANDOCLEVEL_SYSERR;
1.111 kristaps 371: return(0);
372: }
373:
374: savefile = curp->file;
375: savefd = curp->fd;
376:
377: curp->file = file;
378: curp->fd = fd;
379:
380: pdesc(curp);
381:
382: curp->file = savefile;
383: curp->fd = savefd;
384:
385: if (-1 == close(fd))
386: perror(file);
387:
1.115 schwarze 388: return(MANDOCLEVEL_FATAL > file_status ? 1 : 0);
1.111 kristaps 389: }
390:
1.1 kristaps 391:
1.103 schwarze 392: static void
1.68 joerg 393: resize_buf(struct buf *buf, size_t initial)
394: {
395:
1.124 schwarze 396: buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
1.103 schwarze 397: buf->buf = realloc(buf->buf, buf->sz);
398: if (NULL == buf->buf) {
1.68 joerg 399: perror(NULL);
1.105 kristaps 400: exit((int)MANDOCLEVEL_SYSERR);
1.68 joerg 401: }
402: }
403:
404:
405: static int
1.66 kristaps 406: read_whole_file(struct curparse *curp, struct buf *fb, int *with_mmap)
407: {
408: struct stat st;
1.68 joerg 409: size_t off;
1.66 kristaps 410: ssize_t ssz;
411:
412: if (-1 == fstat(curp->fd, &st)) {
413: perror(curp->file);
414: return(0);
415: }
416:
417: /*
418: * If we're a regular file, try just reading in the whole entry
419: * via mmap(). This is faster than reading it into blocks, and
420: * since each file is only a few bytes to begin with, I'm not
421: * concerned that this is going to tank any machines.
422: */
423:
424: if (S_ISREG(st.st_mode)) {
425: if (st.st_size >= (1U << 31)) {
426: fprintf(stderr, "%s: input too large\n",
427: curp->file);
428: return(0);
429: }
430: *with_mmap = 1;
1.71 kristaps 431: fb->sz = (size_t)st.st_size;
1.66 kristaps 432: fb->buf = mmap(NULL, fb->sz, PROT_READ,
1.83 joerg 433: MAP_FILE|MAP_SHARED, curp->fd, 0);
1.66 kristaps 434: if (fb->buf != MAP_FAILED)
435: return(1);
436: }
437:
438: /*
439: * If this isn't a regular file (like, say, stdin), then we must
440: * go the old way and just read things in bit by bit.
441: */
442:
443: *with_mmap = 0;
444: off = 0;
445: fb->sz = 0;
446: fb->buf = NULL;
447: for (;;) {
448: if (off == fb->sz) {
449: if (fb->sz == (1U << 31)) {
450: fprintf(stderr, "%s: input too large\n",
451: curp->file);
452: break;
453: }
1.103 schwarze 454: resize_buf(fb, 65536);
1.66 kristaps 455: }
1.71 kristaps 456: ssz = read(curp->fd, fb->buf + (int)off, fb->sz - off);
1.66 kristaps 457: if (ssz == 0) {
458: fb->sz = off;
459: return(1);
460: }
461: if (ssz == -1) {
462: perror(curp->file);
463: break;
464: }
1.71 kristaps 465: off += (size_t)ssz;
1.66 kristaps 466: }
467:
468: free(fb->buf);
469: fb->buf = NULL;
470: return(0);
471: }
472:
473:
1.64 kristaps 474: static void
1.66 kristaps 475: fdesc(struct curparse *curp)
1.1 kristaps 476: {
1.112 kristaps 477:
478: /*
479: * Called once per file with an opened file descriptor. All
480: * pre-file-parse operations (whether stdin or a file) should go
481: * here.
482: *
483: * This calls down into the nested parser, which drills down and
484: * fully parses a file and all its dependences (i.e., `so'). It
485: * then runs the cleanup validators and pushes to output.
486: */
487:
488: /* Zero the parse type. */
489:
490: curp->mdoc = NULL;
491: curp->man = NULL;
1.115 schwarze 492: file_status = MANDOCLEVEL_OK;
1.112 kristaps 493:
494: /* Make sure the mandotory roff parser is initialised. */
495:
496: if (NULL == curp->roff) {
497: curp->roff = roff_alloc(&curp->regs, curp, mmsg);
498: assert(curp->roff);
499: }
500:
501: /* Fully parse the file. */
1.10 kristaps 502:
1.111 kristaps 503: pdesc(curp);
1.92 kristaps 504:
1.115 schwarze 505: if (MANDOCLEVEL_FATAL <= file_status)
1.111 kristaps 506: goto cleanup;
507:
508: /* NOTE a parser may not have been assigned, yet. */
509:
1.112 kristaps 510: if ( ! (curp->man || curp->mdoc)) {
1.111 kristaps 511: fprintf(stderr, "%s: Not a manual\n", curp->file);
1.115 schwarze 512: file_status = MANDOCLEVEL_FATAL;
1.111 kristaps 513: goto cleanup;
514: }
515:
516: /* Clean up the parse routine ASTs. */
517:
1.112 kristaps 518: if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
1.115 schwarze 519: assert(MANDOCLEVEL_FATAL <= file_status);
1.111 kristaps 520: goto cleanup;
521: }
1.112 kristaps 522:
523: if (curp->man && ! man_endparse(curp->man)) {
1.115 schwarze 524: assert(MANDOCLEVEL_FATAL <= file_status);
1.111 kristaps 525: goto cleanup;
526: }
1.112 kristaps 527:
528: assert(curp->roff);
529: if ( ! roff_endparse(curp->roff)) {
1.115 schwarze 530: assert(MANDOCLEVEL_FATAL <= file_status);
1.111 kristaps 531: goto cleanup;
532: }
1.1 kristaps 533:
534: /*
1.111 kristaps 535: * With -Wstop and warnings or errors of at least
536: * the requested level, do not produce output.
1.1 kristaps 537: */
538:
1.115 schwarze 539: if (MANDOCLEVEL_OK != file_status && curp->wstop)
1.111 kristaps 540: goto cleanup;
541:
542: /* If unset, allocate output dev now (if applicable). */
543:
544: if ( ! (curp->outman && curp->outmdoc)) {
545: switch (curp->outtype) {
546: case (OUTT_XHTML):
547: curp->outdata = xhtml_alloc(curp->outopts);
548: break;
549: case (OUTT_HTML):
550: curp->outdata = html_alloc(curp->outopts);
551: break;
552: case (OUTT_ASCII):
553: curp->outdata = ascii_alloc(curp->outopts);
554: curp->outfree = ascii_free;
555: break;
556: case (OUTT_PDF):
557: curp->outdata = pdf_alloc(curp->outopts);
558: curp->outfree = pspdf_free;
559: break;
560: case (OUTT_PS):
561: curp->outdata = ps_alloc(curp->outopts);
562: curp->outfree = pspdf_free;
563: break;
564: default:
565: break;
566: }
567:
568: switch (curp->outtype) {
569: case (OUTT_HTML):
570: /* FALLTHROUGH */
571: case (OUTT_XHTML):
572: curp->outman = html_man;
573: curp->outmdoc = html_mdoc;
574: curp->outfree = html_free;
575: break;
576: case (OUTT_TREE):
577: curp->outman = tree_man;
578: curp->outmdoc = tree_mdoc;
579: break;
580: case (OUTT_PDF):
581: /* FALLTHROUGH */
582: case (OUTT_ASCII):
583: /* FALLTHROUGH */
584: case (OUTT_PS):
585: curp->outman = terminal_man;
586: curp->outmdoc = terminal_mdoc;
587: break;
588: default:
589: break;
590: }
591: }
592:
593: /* Execute the out device, if it exists. */
594:
1.112 kristaps 595: if (curp->man && curp->outman)
596: (*curp->outman)(curp->outdata, curp->man);
597: if (curp->mdoc && curp->outmdoc)
598: (*curp->outmdoc)(curp->outdata, curp->mdoc);
1.111 kristaps 599:
600: cleanup:
1.112 kristaps 601:
1.111 kristaps 602: memset(&curp->regs, 0, sizeof(struct regset));
1.112 kristaps 603:
604: /* Reset the current-parse compilers. */
605:
606: if (curp->mdoc)
607: mdoc_reset(curp->mdoc);
608: if (curp->man)
609: man_reset(curp->man);
610:
611: assert(curp->roff);
612: roff_reset(curp->roff);
1.111 kristaps 613:
1.115 schwarze 614: if (exit_status < file_status)
615: exit_status = file_status;
616:
1.111 kristaps 617: return;
618: }
619:
620: static void
621: pdesc(struct curparse *curp)
622: {
623: struct buf blk;
624: int with_mmap;
625:
1.112 kristaps 626: /*
627: * Run for each opened file; may be called more than once for
628: * each full parse sequence if the opened file is nested (i.e.,
629: * from `so'). Simply sucks in the whole file and moves into
630: * the parse phase for the file.
631: */
632:
1.103 schwarze 633: if ( ! read_whole_file(curp, &blk, &with_mmap)) {
1.115 schwarze 634: file_status = MANDOCLEVEL_SYSERR;
1.64 kristaps 635: return;
1.103 schwarze 636: }
1.1 kristaps 637:
1.112 kristaps 638: /* Line number is per-file. */
1.111 kristaps 639:
640: curp->line = 1;
1.112 kristaps 641:
1.111 kristaps 642: parsebuf(curp, blk, 1);
643:
644: if (with_mmap)
645: munmap(blk.buf, blk.sz);
646: else
647: free(blk.buf);
648: }
649:
650: static void
651: parsebuf(struct curparse *curp, struct buf blk, int start)
652: {
653: struct buf ln;
1.114 kristaps 654: enum rofferr rr;
1.112 kristaps 655: int i, of, rc;
656: int pos; /* byte number in the ln buffer */
657: int lnn; /* line number in the real file */
1.111 kristaps 658: unsigned char c;
1.112 kristaps 659:
660: /*
661: * Main parse routine for an opened file. This is called for
662: * each opened file and simply loops around the full input file,
663: * possibly nesting (i.e., with `so').
664: */
1.71 kristaps 665:
1.111 kristaps 666: memset(&ln, 0, sizeof(struct buf));
667:
1.112 kristaps 668: lnn = curp->line;
669: pos = 0;
1.111 kristaps 670:
1.112 kristaps 671: for (i = 0; i < (int)blk.sz; ) {
1.111 kristaps 672: if (0 == pos && '\0' == blk.buf[i])
673: break;
1.112 kristaps 674:
1.122 schwarze 675: if (start) {
1.111 kristaps 676: curp->line = lnn;
1.122 schwarze 677: curp->reparse_count = 0;
678: }
1.111 kristaps 679:
680: while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
1.70 joerg 681: if ('\n' == blk.buf[i]) {
682: ++i;
683: ++lnn;
684: break;
685: }
1.99 kristaps 686:
687: /*
688: * Warn about bogus characters. If you're using
689: * non-ASCII encoding, you're screwing your
690: * readers. Since I'd rather this not happen,
691: * I'll be helpful and drop these characters so
692: * we don't display gibberish. Note to manual
693: * writers: use special characters.
694: */
695:
1.102 schwarze 696: c = (unsigned char) blk.buf[i];
1.112 kristaps 697:
698: if ( ! (isascii(c) &&
699: (isgraph(c) || isblank(c)))) {
1.103 schwarze 700: mmsg(MANDOCERR_BADCHAR, curp,
1.111 kristaps 701: curp->line, pos, "ignoring byte");
1.99 kristaps 702: i++;
703: continue;
704: }
705:
1.112 kristaps 706: /* Trailing backslash = a plain char. */
707:
1.70 joerg 708: if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
709: if (pos >= (int)ln.sz)
1.103 schwarze 710: resize_buf(&ln, 256);
1.70 joerg 711: ln.buf[pos++] = blk.buf[i++];
1.67 joerg 712: continue;
1.70 joerg 713: }
1.112 kristaps 714:
715: /* Found escape & at least one other char. */
716:
1.70 joerg 717: if ('\n' == blk.buf[i + 1]) {
1.112 kristaps 718: i += 2;
1.70 joerg 719: /* Escaped newlines are skipped over */
720: ++lnn;
1.67 joerg 721: continue;
722: }
1.112 kristaps 723:
1.70 joerg 724: if ('"' == blk.buf[i + 1]) {
725: i += 2;
726: /* Comment, skip to end of line */
727: for (; i < (int)blk.sz; ++i) {
728: if ('\n' == blk.buf[i]) {
729: ++i;
730: ++lnn;
731: break;
732: }
733: }
1.112 kristaps 734:
1.70 joerg 735: /* Backout trailing whitespaces */
736: for (; pos > 0; --pos) {
737: if (ln.buf[pos - 1] != ' ')
738: break;
739: if (pos > 2 && ln.buf[pos - 2] == '\\')
740: break;
741: }
742: break;
743: }
1.112 kristaps 744:
745: /* Some other escape sequence, copy & cont. */
746:
1.70 joerg 747: if (pos + 1 >= (int)ln.sz)
1.103 schwarze 748: resize_buf(&ln, 256);
1.1 kristaps 749:
1.70 joerg 750: ln.buf[pos++] = blk.buf[i++];
751: ln.buf[pos++] = blk.buf[i++];
1.67 joerg 752: }
1.1 kristaps 753:
1.70 joerg 754: if (pos >= (int)ln.sz)
1.103 schwarze 755: resize_buf(&ln, 256);
1.112 kristaps 756:
1.71 kristaps 757: ln.buf[pos] = '\0';
758:
1.76 kristaps 759: /*
760: * A significant amount of complexity is contained by
761: * the roff preprocessor. It's line-oriented but can be
762: * expressed on one line, so we need at times to
763: * readjust our starting point and re-run it. The roff
764: * preprocessor can also readjust the buffers with new
765: * data, so we pass them in wholesale.
766: */
767:
768: of = 0;
1.112 kristaps 769:
1.111 kristaps 770: rerun:
1.114 kristaps 771: rr = roff_parseln
1.112 kristaps 772: (curp->roff, curp->line,
773: &ln.buf, &ln.sz, of, &of);
774:
1.114 kristaps 775: switch (rr) {
1.111 kristaps 776: case (ROFF_REPARSE):
1.122 schwarze 777: if (REPARSE_LIMIT >= ++curp->reparse_count)
778: parsebuf(curp, ln, 0);
779: else
780: mmsg(MANDOCERR_ROFFLOOP, curp,
781: curp->line, pos, NULL);
1.111 kristaps 782: pos = 0;
783: continue;
784: case (ROFF_APPEND):
785: pos = strlen(ln.buf);
786: continue;
787: case (ROFF_RERUN):
788: goto rerun;
789: case (ROFF_IGN):
790: pos = 0;
1.71 kristaps 791: continue;
1.111 kristaps 792: case (ROFF_ERR):
1.115 schwarze 793: assert(MANDOCLEVEL_FATAL <= file_status);
1.111 kristaps 794: break;
795: case (ROFF_SO):
796: if (pfile(ln.buf + of, curp)) {
797: pos = 0;
798: continue;
799: } else
800: break;
801: case (ROFF_CONT):
802: break;
1.103 schwarze 803: }
1.29 kristaps 804:
1.76 kristaps 805: /*
806: * If input parsers have not been allocated, do so now.
807: * We keep these instanced betwen parsers, but set them
808: * locally per parse routine since we can use different
809: * parsers with each one.
810: */
1.19 kristaps 811:
1.112 kristaps 812: if ( ! (curp->man || curp->mdoc))
813: pset(ln.buf + of, pos - of, curp);
1.19 kristaps 814:
1.112 kristaps 815: /*
816: * Lastly, push down into the parsers themselves. One
817: * of these will have already been set in the pset()
818: * routine.
819: */
820:
821: if (curp->man || curp->mdoc) {
822: rc = curp->man ?
823: man_parseln(curp->man,
824: curp->line, ln.buf, of) :
825: mdoc_parseln(curp->mdoc,
826: curp->line, ln.buf, of);
1.30 kristaps 827:
1.112 kristaps 828: if ( ! rc) {
1.115 schwarze 829: assert(MANDOCLEVEL_FATAL <= file_status);
1.112 kristaps 830: break;
831: }
1.103 schwarze 832: }
1.19 kristaps 833:
1.111 kristaps 834: /* Temporary buffers typically are not full. */
1.112 kristaps 835:
1.111 kristaps 836: if (0 == start && '\0' == blk.buf[i])
1.59 kristaps 837: break;
1.85 kristaps 838:
1.111 kristaps 839: /* Start the next input line. */
1.112 kristaps 840:
1.111 kristaps 841: pos = 0;
1.22 kristaps 842: }
843:
1.111 kristaps 844: free(ln.buf);
1.19 kristaps 845: }
846:
1.103 schwarze 847: static void
1.112 kristaps 848: pset(const char *buf, int pos, struct curparse *curp)
1.19 kristaps 849: {
1.29 kristaps 850: int i;
1.19 kristaps 851:
852: /*
853: * Try to intuit which kind of manual parser should be used. If
854: * passed in by command-line (-man, -mdoc), then use that
855: * explicitly. If passed as -mandoc, then try to guess from the
1.30 kristaps 856: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.19 kristaps 857: * default to -man, which is more lenient.
1.112 kristaps 858: *
859: * Separate out pmdoc/pman from mdoc/man: the first persists
860: * through all parsers, while the latter is used per-parse.
1.19 kristaps 861: */
862:
1.75 kristaps 863: if ('.' == buf[0] || '\'' == buf[0]) {
1.29 kristaps 864: for (i = 1; buf[i]; i++)
865: if (' ' != buf[i] && '\t' != buf[i])
866: break;
1.103 schwarze 867: if ('\0' == buf[i])
868: return;
1.29 kristaps 869: }
1.10 kristaps 870:
1.19 kristaps 871: switch (curp->inttype) {
872: case (INTT_MDOC):
1.112 kristaps 873: if (NULL == curp->pmdoc)
874: curp->pmdoc = mdoc_alloc
875: (&curp->regs, curp, mmsg);
876: assert(curp->pmdoc);
877: curp->mdoc = curp->pmdoc;
1.103 schwarze 878: return;
1.19 kristaps 879: case (INTT_MAN):
1.112 kristaps 880: if (NULL == curp->pman)
881: curp->pman = man_alloc
882: (&curp->regs, curp, mmsg);
883: assert(curp->pman);
884: curp->man = curp->pman;
1.103 schwarze 885: return;
1.19 kristaps 886: default:
887: break;
888: }
889:
890: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
1.112 kristaps 891: if (NULL == curp->pmdoc)
892: curp->pmdoc = mdoc_alloc
893: (&curp->regs, curp, mmsg);
894: assert(curp->pmdoc);
895: curp->mdoc = curp->pmdoc;
1.103 schwarze 896: return;
1.19 kristaps 897: }
898:
1.112 kristaps 899: if (NULL == curp->pman)
900: curp->pman = man_alloc(&curp->regs, curp, mmsg);
901: assert(curp->pman);
902: curp->man = curp->pman;
1.10 kristaps 903: }
904:
905: static int
906: moptions(enum intt *tflags, char *arg)
907: {
908:
1.17 kristaps 909: if (0 == strcmp(arg, "doc"))
1.10 kristaps 910: *tflags = INTT_MDOC;
1.19 kristaps 911: else if (0 == strcmp(arg, "andoc"))
912: *tflags = INTT_AUTO;
1.17 kristaps 913: else if (0 == strcmp(arg, "an"))
1.10 kristaps 914: *tflags = INTT_MAN;
915: else {
1.57 kristaps 916: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 917: return(0);
918: }
919:
920: return(1);
1.1 kristaps 921: }
922:
923: static int
1.60 kristaps 924: toptions(struct curparse *curp, char *arg)
1.1 kristaps 925: {
926:
927: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 928: curp->outtype = OUTT_ASCII;
929: else if (0 == strcmp(arg, "lint")) {
930: curp->outtype = OUTT_LINT;
1.103 schwarze 931: curp->wlevel = MANDOCLEVEL_WARNING;
1.60 kristaps 932: }
1.1 kristaps 933: else if (0 == strcmp(arg, "tree"))
1.60 kristaps 934: curp->outtype = OUTT_TREE;
1.43 kristaps 935: else if (0 == strcmp(arg, "html"))
1.60 kristaps 936: curp->outtype = OUTT_HTML;
1.59 kristaps 937: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 938: curp->outtype = OUTT_XHTML;
1.85 kristaps 939: else if (0 == strcmp(arg, "ps"))
940: curp->outtype = OUTT_PS;
1.100 kristaps 941: else if (0 == strcmp(arg, "pdf"))
942: curp->outtype = OUTT_PDF;
1.1 kristaps 943: else {
1.57 kristaps 944: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 945: return(0);
946: }
947:
948: return(1);
949: }
950:
951: static int
1.103 schwarze 952: woptions(struct curparse *curp, char *arg)
1.1 kristaps 953: {
1.34 kristaps 954: char *v, *o;
1.103 schwarze 955: const char *toks[6];
1.1 kristaps 956:
1.103 schwarze 957: toks[0] = "stop";
958: toks[1] = "all";
959: toks[2] = "warning";
960: toks[3] = "error";
961: toks[4] = "fatal";
962: toks[5] = NULL;
1.1 kristaps 963:
1.34 kristaps 964: while (*arg) {
965: o = arg;
1.45 kristaps 966: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 967: case (0):
1.103 schwarze 968: curp->wstop = 1;
1.1 kristaps 969: break;
970: case (1):
1.103 schwarze 971: /* FALLTHROUGH */
1.1 kristaps 972: case (2):
1.103 schwarze 973: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 974: break;
1.20 kristaps 975: case (3):
1.103 schwarze 976: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 977: break;
978: case (4):
1.103 schwarze 979: curp->wlevel = MANDOCLEVEL_FATAL;
1.50 kristaps 980: break;
1.1 kristaps 981: default:
1.103 schwarze 982: fprintf(stderr, "-W%s: Bad argument\n", o);
1.1 kristaps 983: return(0);
984: }
1.34 kristaps 985: }
1.1 kristaps 986:
987: return(1);
988: }
989:
1.71 kristaps 990: static int
991: mmsg(enum mandocerr t, void *arg, int ln, int col, const char *msg)
992: {
993: struct curparse *cp;
1.103 schwarze 994: enum mandoclevel level;
995:
996: level = MANDOCLEVEL_FATAL;
997: while (t < mandoclimits[level])
1.105 kristaps 998: /* LINTED */
1.103 schwarze 999: level--;
1.71 kristaps 1000:
1001: cp = (struct curparse *)arg;
1.103 schwarze 1002: if (level < cp->wlevel)
1003: return(1);
1.73 kristaps 1004:
1.103 schwarze 1005: fprintf(stderr, "%s:%d:%d: %s: %s",
1006: cp->file, ln, col + 1, mandoclevels[level], mandocerrs[t]);
1.73 kristaps 1007: if (msg)
1008: fprintf(stderr, ": %s", msg);
1009: fputc('\n', stderr);
1.79 kristaps 1010:
1.115 schwarze 1011: if (file_status < level)
1012: file_status = level;
1.103 schwarze 1013:
1014: return(level < MANDOCLEVEL_FATAL);
1.71 kristaps 1015: }
CVSweb