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