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