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