Annotation of mandoc/main.c, Revision 1.153
1.153 ! kristaps 1: /* $Id: main.c,v 1.152 2011/03/17 08:49:34 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.153 ! kristaps 236: static void mmsg(enum mandocerr, void *,
1.71 kristaps 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.152 kristaps 408: buf->buf = mandoc_realloc(buf->buf, buf->sz);
1.68 joerg 409: }
410:
411:
412: static int
1.66 kristaps 413: read_whole_file(struct curparse *curp, struct buf *fb, int *with_mmap)
414: {
415: struct stat st;
1.68 joerg 416: size_t off;
1.66 kristaps 417: ssize_t ssz;
418:
419: if (-1 == fstat(curp->fd, &st)) {
420: perror(curp->file);
421: return(0);
422: }
423:
424: /*
425: * If we're a regular file, try just reading in the whole entry
426: * via mmap(). This is faster than reading it into blocks, and
427: * since each file is only a few bytes to begin with, I'm not
428: * concerned that this is going to tank any machines.
429: */
430:
431: if (S_ISREG(st.st_mode)) {
432: if (st.st_size >= (1U << 31)) {
433: fprintf(stderr, "%s: input too large\n",
434: curp->file);
435: return(0);
436: }
437: *with_mmap = 1;
1.71 kristaps 438: fb->sz = (size_t)st.st_size;
1.66 kristaps 439: fb->buf = mmap(NULL, fb->sz, PROT_READ,
1.83 joerg 440: MAP_FILE|MAP_SHARED, curp->fd, 0);
1.66 kristaps 441: if (fb->buf != MAP_FAILED)
442: return(1);
443: }
444:
445: /*
446: * If this isn't a regular file (like, say, stdin), then we must
447: * go the old way and just read things in bit by bit.
448: */
449:
450: *with_mmap = 0;
451: off = 0;
452: fb->sz = 0;
453: fb->buf = NULL;
454: for (;;) {
455: if (off == fb->sz) {
456: if (fb->sz == (1U << 31)) {
457: fprintf(stderr, "%s: input too large\n",
458: curp->file);
459: break;
460: }
1.103 schwarze 461: resize_buf(fb, 65536);
1.66 kristaps 462: }
1.71 kristaps 463: ssz = read(curp->fd, fb->buf + (int)off, fb->sz - off);
1.66 kristaps 464: if (ssz == 0) {
465: fb->sz = off;
466: return(1);
467: }
468: if (ssz == -1) {
469: perror(curp->file);
470: break;
471: }
1.71 kristaps 472: off += (size_t)ssz;
1.66 kristaps 473: }
474:
475: free(fb->buf);
476: fb->buf = NULL;
477: return(0);
478: }
479:
480:
1.64 kristaps 481: static void
1.66 kristaps 482: fdesc(struct curparse *curp)
1.1 kristaps 483: {
1.112 kristaps 484:
485: /*
486: * Called once per file with an opened file descriptor. All
487: * pre-file-parse operations (whether stdin or a file) should go
488: * here.
489: *
490: * This calls down into the nested parser, which drills down and
491: * fully parses a file and all its dependences (i.e., `so'). It
492: * then runs the cleanup validators and pushes to output.
493: */
494:
495: /* Zero the parse type. */
496:
497: curp->mdoc = NULL;
498: curp->man = NULL;
1.148 kristaps 499: curp->file_status = MANDOCLEVEL_OK;
1.112 kristaps 500:
501: /* Make sure the mandotory roff parser is initialised. */
502:
503: if (NULL == curp->roff) {
504: curp->roff = roff_alloc(&curp->regs, curp, mmsg);
505: assert(curp->roff);
506: }
507:
508: /* Fully parse the file. */
1.10 kristaps 509:
1.111 kristaps 510: pdesc(curp);
1.92 kristaps 511:
1.148 kristaps 512: if (MANDOCLEVEL_FATAL <= curp->file_status)
1.111 kristaps 513: goto cleanup;
514:
515: /* NOTE a parser may not have been assigned, yet. */
516:
1.112 kristaps 517: if ( ! (curp->man || curp->mdoc)) {
1.111 kristaps 518: fprintf(stderr, "%s: Not a manual\n", curp->file);
1.148 kristaps 519: curp->file_status = MANDOCLEVEL_FATAL;
1.111 kristaps 520: goto cleanup;
521: }
522:
523: /* Clean up the parse routine ASTs. */
524:
1.112 kristaps 525: if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
1.148 kristaps 526: assert(MANDOCLEVEL_FATAL <= curp->file_status);
1.111 kristaps 527: goto cleanup;
528: }
1.112 kristaps 529:
530: if (curp->man && ! man_endparse(curp->man)) {
1.148 kristaps 531: assert(MANDOCLEVEL_FATAL <= curp->file_status);
1.111 kristaps 532: goto cleanup;
533: }
1.112 kristaps 534:
535: assert(curp->roff);
1.130 kristaps 536: roff_endparse(curp->roff);
1.1 kristaps 537:
538: /*
1.111 kristaps 539: * With -Wstop and warnings or errors of at least
540: * the requested level, do not produce output.
1.1 kristaps 541: */
542:
1.148 kristaps 543: if (MANDOCLEVEL_OK != curp->file_status && curp->wstop)
1.111 kristaps 544: goto cleanup;
545:
546: /* If unset, allocate output dev now (if applicable). */
547:
548: if ( ! (curp->outman && curp->outmdoc)) {
549: switch (curp->outtype) {
550: case (OUTT_XHTML):
551: curp->outdata = xhtml_alloc(curp->outopts);
552: break;
553: case (OUTT_HTML):
554: curp->outdata = html_alloc(curp->outopts);
555: break;
556: case (OUTT_ASCII):
557: curp->outdata = ascii_alloc(curp->outopts);
558: curp->outfree = ascii_free;
559: break;
560: case (OUTT_PDF):
561: curp->outdata = pdf_alloc(curp->outopts);
562: curp->outfree = pspdf_free;
563: break;
564: case (OUTT_PS):
565: curp->outdata = ps_alloc(curp->outopts);
566: curp->outfree = pspdf_free;
567: break;
568: default:
569: break;
570: }
571:
572: switch (curp->outtype) {
573: case (OUTT_HTML):
574: /* FALLTHROUGH */
575: case (OUTT_XHTML):
576: curp->outman = html_man;
577: curp->outmdoc = html_mdoc;
578: curp->outfree = html_free;
579: break;
580: case (OUTT_TREE):
581: curp->outman = tree_man;
582: curp->outmdoc = tree_mdoc;
583: break;
584: case (OUTT_PDF):
585: /* FALLTHROUGH */
586: case (OUTT_ASCII):
587: /* FALLTHROUGH */
588: case (OUTT_PS):
589: curp->outman = terminal_man;
590: curp->outmdoc = terminal_mdoc;
591: break;
592: default:
593: break;
594: }
595: }
596:
597: /* Execute the out device, if it exists. */
598:
1.112 kristaps 599: if (curp->man && curp->outman)
600: (*curp->outman)(curp->outdata, curp->man);
601: if (curp->mdoc && curp->outmdoc)
602: (*curp->outmdoc)(curp->outdata, curp->mdoc);
1.111 kristaps 603:
604: cleanup:
1.112 kristaps 605:
1.111 kristaps 606: memset(&curp->regs, 0, sizeof(struct regset));
1.112 kristaps 607:
608: /* Reset the current-parse compilers. */
609:
610: if (curp->mdoc)
611: mdoc_reset(curp->mdoc);
612: if (curp->man)
613: man_reset(curp->man);
614:
615: assert(curp->roff);
616: roff_reset(curp->roff);
1.111 kristaps 617:
1.151 kristaps 618: if (curp->exit_status < curp->file_status)
619: curp->exit_status = curp->file_status;
1.115 schwarze 620:
1.111 kristaps 621: return;
622: }
623:
624: static void
625: pdesc(struct curparse *curp)
626: {
627: struct buf blk;
628: int with_mmap;
629:
1.112 kristaps 630: /*
631: * Run for each opened file; may be called more than once for
632: * each full parse sequence if the opened file is nested (i.e.,
633: * from `so'). Simply sucks in the whole file and moves into
634: * the parse phase for the file.
635: */
636:
1.103 schwarze 637: if ( ! read_whole_file(curp, &blk, &with_mmap)) {
1.148 kristaps 638: curp->file_status = MANDOCLEVEL_SYSERR;
1.64 kristaps 639: return;
1.103 schwarze 640: }
1.1 kristaps 641:
1.112 kristaps 642: /* Line number is per-file. */
1.111 kristaps 643:
644: curp->line = 1;
1.112 kristaps 645:
1.111 kristaps 646: parsebuf(curp, blk, 1);
647:
648: if (with_mmap)
649: munmap(blk.buf, blk.sz);
650: else
651: free(blk.buf);
652: }
653:
1.148 kristaps 654: /*
655: * Main parse routine for an opened file. This is called for each
656: * opened file and simply loops around the full input file, possibly
657: * nesting (i.e., with `so').
658: */
1.111 kristaps 659: static void
660: parsebuf(struct curparse *curp, struct buf blk, int start)
661: {
1.141 schwarze 662: const struct tbl_span *span;
1.111 kristaps 663: struct buf ln;
1.114 kristaps 664: enum rofferr rr;
1.112 kristaps 665: int i, of, rc;
666: int pos; /* byte number in the ln buffer */
667: int lnn; /* line number in the real file */
1.111 kristaps 668: unsigned char c;
1.112 kristaps 669:
1.111 kristaps 670: memset(&ln, 0, sizeof(struct buf));
671:
1.112 kristaps 672: lnn = curp->line;
673: pos = 0;
1.111 kristaps 674:
1.112 kristaps 675: for (i = 0; i < (int)blk.sz; ) {
1.111 kristaps 676: if (0 == pos && '\0' == blk.buf[i])
677: break;
1.112 kristaps 678:
1.122 schwarze 679: if (start) {
1.111 kristaps 680: curp->line = lnn;
1.122 schwarze 681: curp->reparse_count = 0;
682: }
1.111 kristaps 683:
684: while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
1.140 schwarze 685:
686: /*
687: * When finding an unescaped newline character,
688: * leave the character loop to process the line.
689: * Skip a preceding carriage return, if any.
690: */
691:
692: if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz &&
693: '\n' == blk.buf[i + 1])
694: ++i;
1.70 joerg 695: if ('\n' == blk.buf[i]) {
696: ++i;
697: ++lnn;
698: break;
699: }
1.99 kristaps 700:
701: /*
702: * Warn about bogus characters. If you're using
703: * non-ASCII encoding, you're screwing your
704: * readers. Since I'd rather this not happen,
705: * I'll be helpful and drop these characters so
706: * we don't display gibberish. Note to manual
707: * writers: use special characters.
708: */
709:
1.102 schwarze 710: c = (unsigned char) blk.buf[i];
1.112 kristaps 711:
712: if ( ! (isascii(c) &&
713: (isgraph(c) || isblank(c)))) {
1.103 schwarze 714: mmsg(MANDOCERR_BADCHAR, curp,
1.111 kristaps 715: curp->line, pos, "ignoring byte");
1.99 kristaps 716: i++;
717: continue;
718: }
719:
1.112 kristaps 720: /* Trailing backslash = a plain char. */
721:
1.70 joerg 722: if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
723: if (pos >= (int)ln.sz)
1.103 schwarze 724: resize_buf(&ln, 256);
1.70 joerg 725: ln.buf[pos++] = blk.buf[i++];
1.67 joerg 726: continue;
1.70 joerg 727: }
1.112 kristaps 728:
1.140 schwarze 729: /*
730: * Found escape and at least one other character.
731: * When it's a newline character, skip it.
732: * When there is a carriage return in between,
733: * skip that one as well.
734: */
1.112 kristaps 735:
1.140 schwarze 736: if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz &&
737: '\n' == blk.buf[i + 2])
738: ++i;
1.70 joerg 739: if ('\n' == blk.buf[i + 1]) {
1.112 kristaps 740: i += 2;
1.70 joerg 741: ++lnn;
1.67 joerg 742: continue;
743: }
1.112 kristaps 744:
1.70 joerg 745: if ('"' == blk.buf[i + 1]) {
746: i += 2;
747: /* Comment, skip to end of line */
748: for (; i < (int)blk.sz; ++i) {
749: if ('\n' == blk.buf[i]) {
750: ++i;
751: ++lnn;
752: break;
753: }
754: }
1.112 kristaps 755:
1.70 joerg 756: /* Backout trailing whitespaces */
757: for (; pos > 0; --pos) {
758: if (ln.buf[pos - 1] != ' ')
759: break;
760: if (pos > 2 && ln.buf[pos - 2] == '\\')
761: break;
762: }
763: break;
764: }
1.112 kristaps 765:
766: /* Some other escape sequence, copy & cont. */
767:
1.70 joerg 768: if (pos + 1 >= (int)ln.sz)
1.103 schwarze 769: resize_buf(&ln, 256);
1.1 kristaps 770:
1.70 joerg 771: ln.buf[pos++] = blk.buf[i++];
772: ln.buf[pos++] = blk.buf[i++];
1.67 joerg 773: }
1.1 kristaps 774:
1.70 joerg 775: if (pos >= (int)ln.sz)
1.103 schwarze 776: resize_buf(&ln, 256);
1.112 kristaps 777:
1.71 kristaps 778: ln.buf[pos] = '\0';
779:
1.76 kristaps 780: /*
781: * A significant amount of complexity is contained by
782: * the roff preprocessor. It's line-oriented but can be
783: * expressed on one line, so we need at times to
784: * readjust our starting point and re-run it. The roff
785: * preprocessor can also readjust the buffers with new
786: * data, so we pass them in wholesale.
787: */
788:
789: of = 0;
1.112 kristaps 790:
1.111 kristaps 791: rerun:
1.114 kristaps 792: rr = roff_parseln
1.112 kristaps 793: (curp->roff, curp->line,
794: &ln.buf, &ln.sz, of, &of);
795:
1.114 kristaps 796: switch (rr) {
1.111 kristaps 797: case (ROFF_REPARSE):
1.122 schwarze 798: if (REPARSE_LIMIT >= ++curp->reparse_count)
799: parsebuf(curp, ln, 0);
800: else
801: mmsg(MANDOCERR_ROFFLOOP, curp,
802: curp->line, pos, NULL);
1.111 kristaps 803: pos = 0;
804: continue;
805: case (ROFF_APPEND):
1.150 kristaps 806: pos = (int)strlen(ln.buf);
1.111 kristaps 807: continue;
808: case (ROFF_RERUN):
809: goto rerun;
810: case (ROFF_IGN):
811: pos = 0;
1.71 kristaps 812: continue;
1.111 kristaps 813: case (ROFF_ERR):
1.148 kristaps 814: assert(MANDOCLEVEL_FATAL <= curp->file_status);
1.111 kristaps 815: break;
816: case (ROFF_SO):
817: if (pfile(ln.buf + of, curp)) {
818: pos = 0;
819: continue;
820: } else
821: break;
1.128 kristaps 822: default:
1.111 kristaps 823: break;
1.103 schwarze 824: }
1.132 kristaps 825:
826: /*
827: * If we encounter errors in the recursive parsebuf()
828: * call, make sure we don't continue parsing.
829: */
830:
1.148 kristaps 831: if (MANDOCLEVEL_FATAL <= curp->file_status)
1.132 kristaps 832: break;
1.29 kristaps 833:
1.76 kristaps 834: /*
835: * If input parsers have not been allocated, do so now.
836: * We keep these instanced betwen parsers, but set them
837: * locally per parse routine since we can use different
838: * parsers with each one.
839: */
1.19 kristaps 840:
1.112 kristaps 841: if ( ! (curp->man || curp->mdoc))
842: pset(ln.buf + of, pos - of, curp);
1.19 kristaps 843:
1.112 kristaps 844: /*
845: * Lastly, push down into the parsers themselves. One
846: * of these will have already been set in the pset()
847: * routine.
1.128 kristaps 848: * If libroff returns ROFF_TBL, then add it to the
849: * currently open parse. Since we only get here if
850: * there does exist data (see tbl_data.c), we're
851: * guaranteed that something's been allocated.
1.146 kristaps 852: * Do the same for ROFF_EQN.
1.112 kristaps 853: */
854:
1.146 kristaps 855: rc = -1;
856:
857: if (ROFF_TBL == rr)
1.141 schwarze 858: while (NULL != (span = roff_span(curp->roff))) {
1.146 kristaps 859: rc = curp->man ?
860: man_addspan(curp->man, span) :
1.141 schwarze 861: mdoc_addspan(curp->mdoc, span);
1.146 kristaps 862: if (0 == rc)
863: break;
1.141 schwarze 864: }
1.146 kristaps 865: else if (ROFF_EQN == rr)
1.145 kristaps 866: rc = curp->mdoc ?
867: mdoc_addeqn(curp->mdoc,
868: roff_eqn(curp->roff)) :
869: man_addeqn(curp->man,
870: roff_eqn(curp->roff));
1.146 kristaps 871: else if (curp->man || curp->mdoc)
1.112 kristaps 872: rc = curp->man ?
873: man_parseln(curp->man,
874: curp->line, ln.buf, of) :
875: mdoc_parseln(curp->mdoc,
876: curp->line, ln.buf, of);
1.30 kristaps 877:
1.146 kristaps 878: if (0 == rc) {
1.148 kristaps 879: assert(MANDOCLEVEL_FATAL <= curp->file_status);
1.146 kristaps 880: break;
1.103 schwarze 881: }
1.19 kristaps 882:
1.111 kristaps 883: /* Temporary buffers typically are not full. */
1.112 kristaps 884:
1.111 kristaps 885: if (0 == start && '\0' == blk.buf[i])
1.59 kristaps 886: break;
1.85 kristaps 887:
1.111 kristaps 888: /* Start the next input line. */
1.112 kristaps 889:
1.111 kristaps 890: pos = 0;
1.22 kristaps 891: }
892:
1.111 kristaps 893: free(ln.buf);
1.19 kristaps 894: }
895:
1.103 schwarze 896: static void
1.112 kristaps 897: pset(const char *buf, int pos, struct curparse *curp)
1.19 kristaps 898: {
1.29 kristaps 899: int i;
1.19 kristaps 900:
901: /*
902: * Try to intuit which kind of manual parser should be used. If
903: * passed in by command-line (-man, -mdoc), then use that
904: * explicitly. If passed as -mandoc, then try to guess from the
1.30 kristaps 905: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.19 kristaps 906: * default to -man, which is more lenient.
1.112 kristaps 907: *
908: * Separate out pmdoc/pman from mdoc/man: the first persists
909: * through all parsers, while the latter is used per-parse.
1.19 kristaps 910: */
911:
1.75 kristaps 912: if ('.' == buf[0] || '\'' == buf[0]) {
1.29 kristaps 913: for (i = 1; buf[i]; i++)
914: if (' ' != buf[i] && '\t' != buf[i])
915: break;
1.103 schwarze 916: if ('\0' == buf[i])
917: return;
1.29 kristaps 918: }
1.10 kristaps 919:
1.19 kristaps 920: switch (curp->inttype) {
921: case (INTT_MDOC):
1.112 kristaps 922: if (NULL == curp->pmdoc)
923: curp->pmdoc = mdoc_alloc
924: (&curp->regs, curp, mmsg);
925: assert(curp->pmdoc);
926: curp->mdoc = curp->pmdoc;
1.103 schwarze 927: return;
1.19 kristaps 928: case (INTT_MAN):
1.112 kristaps 929: if (NULL == curp->pman)
930: curp->pman = man_alloc
931: (&curp->regs, curp, mmsg);
932: assert(curp->pman);
933: curp->man = curp->pman;
1.103 schwarze 934: return;
1.19 kristaps 935: default:
936: break;
937: }
938:
939: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
1.112 kristaps 940: if (NULL == curp->pmdoc)
941: curp->pmdoc = mdoc_alloc
942: (&curp->regs, curp, mmsg);
943: assert(curp->pmdoc);
944: curp->mdoc = curp->pmdoc;
1.103 schwarze 945: return;
1.19 kristaps 946: }
947:
1.112 kristaps 948: if (NULL == curp->pman)
949: curp->pman = man_alloc(&curp->regs, curp, mmsg);
950: assert(curp->pman);
951: curp->man = curp->pman;
1.10 kristaps 952: }
953:
954: static int
955: moptions(enum intt *tflags, char *arg)
956: {
957:
1.17 kristaps 958: if (0 == strcmp(arg, "doc"))
1.10 kristaps 959: *tflags = INTT_MDOC;
1.19 kristaps 960: else if (0 == strcmp(arg, "andoc"))
961: *tflags = INTT_AUTO;
1.17 kristaps 962: else if (0 == strcmp(arg, "an"))
1.10 kristaps 963: *tflags = INTT_MAN;
964: else {
1.57 kristaps 965: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 966: return(0);
967: }
968:
969: return(1);
1.1 kristaps 970: }
971:
972: static int
1.60 kristaps 973: toptions(struct curparse *curp, char *arg)
1.1 kristaps 974: {
975:
976: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 977: curp->outtype = OUTT_ASCII;
978: else if (0 == strcmp(arg, "lint")) {
979: curp->outtype = OUTT_LINT;
1.103 schwarze 980: curp->wlevel = MANDOCLEVEL_WARNING;
1.60 kristaps 981: }
1.1 kristaps 982: else if (0 == strcmp(arg, "tree"))
1.60 kristaps 983: curp->outtype = OUTT_TREE;
1.43 kristaps 984: else if (0 == strcmp(arg, "html"))
1.60 kristaps 985: curp->outtype = OUTT_HTML;
1.59 kristaps 986: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 987: curp->outtype = OUTT_XHTML;
1.85 kristaps 988: else if (0 == strcmp(arg, "ps"))
989: curp->outtype = OUTT_PS;
1.100 kristaps 990: else if (0 == strcmp(arg, "pdf"))
991: curp->outtype = OUTT_PDF;
1.1 kristaps 992: else {
1.57 kristaps 993: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 994: return(0);
995: }
996:
997: return(1);
998: }
999:
1000: static int
1.103 schwarze 1001: woptions(struct curparse *curp, char *arg)
1.1 kristaps 1002: {
1.34 kristaps 1003: char *v, *o;
1.103 schwarze 1004: const char *toks[6];
1.1 kristaps 1005:
1.103 schwarze 1006: toks[0] = "stop";
1007: toks[1] = "all";
1008: toks[2] = "warning";
1009: toks[3] = "error";
1010: toks[4] = "fatal";
1011: toks[5] = NULL;
1.1 kristaps 1012:
1.34 kristaps 1013: while (*arg) {
1014: o = arg;
1.45 kristaps 1015: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 1016: case (0):
1.103 schwarze 1017: curp->wstop = 1;
1.1 kristaps 1018: break;
1019: case (1):
1.103 schwarze 1020: /* FALLTHROUGH */
1.1 kristaps 1021: case (2):
1.103 schwarze 1022: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 1023: break;
1.20 kristaps 1024: case (3):
1.103 schwarze 1025: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 1026: break;
1027: case (4):
1.103 schwarze 1028: curp->wlevel = MANDOCLEVEL_FATAL;
1.50 kristaps 1029: break;
1.1 kristaps 1030: default:
1.103 schwarze 1031: fprintf(stderr, "-W%s: Bad argument\n", o);
1.1 kristaps 1032: return(0);
1033: }
1.34 kristaps 1034: }
1.1 kristaps 1035:
1036: return(1);
1037: }
1038:
1.153 ! kristaps 1039: static void
1.71 kristaps 1040: mmsg(enum mandocerr t, void *arg, int ln, int col, const char *msg)
1041: {
1042: struct curparse *cp;
1.103 schwarze 1043: enum mandoclevel level;
1044:
1045: level = MANDOCLEVEL_FATAL;
1046: while (t < mandoclimits[level])
1.105 kristaps 1047: /* LINTED */
1.103 schwarze 1048: level--;
1.71 kristaps 1049:
1050: cp = (struct curparse *)arg;
1.103 schwarze 1051: if (level < cp->wlevel)
1.153 ! kristaps 1052: return;
1.73 kristaps 1053:
1.103 schwarze 1054: fprintf(stderr, "%s:%d:%d: %s: %s",
1055: cp->file, ln, col + 1, mandoclevels[level], mandocerrs[t]);
1.73 kristaps 1056: if (msg)
1057: fprintf(stderr, ": %s", msg);
1058: fputc('\n', stderr);
1.79 kristaps 1059:
1.148 kristaps 1060: if (cp->file_status < level)
1061: cp->file_status = level;
1.71 kristaps 1062: }
CVSweb