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