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