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