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