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