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