Annotation of src/usr.bin/mandoc/main.c, Revision 1.11
1.11 ! schwarze 1: /* $Id: main.c,v 1.10 2009/07/07 00:16:21 schwarze Exp $ */
1.1 kristaps 2: /*
1.2 schwarze 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.2 schwarze 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
17: #include <sys/stat.h>
18:
19: #include <assert.h>
20: #include <err.h>
21: #include <fcntl.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
25: #include <unistd.h>
26:
27: #include "mdoc.h"
28: #include "man.h"
29:
30: typedef int (*out_mdoc)(void *, const struct mdoc *);
31: typedef int (*out_man)(void *, const struct man *);
32: typedef void (*out_free)(void *);
33:
34: struct buf {
35: char *buf;
36: size_t sz;
37: };
38:
39: enum intt {
40: INTT_AUTO,
41: INTT_MDOC,
42: INTT_MAN
43: };
44:
45: enum outt {
46: OUTT_ASCII = 0,
47: OUTT_TREE,
48: OUTT_LINT
49: };
50:
51: struct curparse {
52: const char *file; /* Current parse. */
53: int fd; /* Current parse. */
54: int wflags;
55: #define WARN_WALL 0x03 /* All-warnings mask. */
56: #define WARN_WCOMPAT (1 << 0) /* Compatibility warnings. */
57: #define WARN_WSYNTAX (1 << 1) /* Syntax warnings. */
58: #define WARN_WERR (1 << 2) /* Warnings->errors. */
59: int fflags;
60: #define IGN_SCOPE (1 << 0) /* Ignore scope errors. */
61: #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
62: #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
63: #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
1.6 schwarze 64: enum intt inttype; /* Input parsers... */
1.1 kristaps 65: struct man *man;
66: struct man *lastman;
67: struct mdoc *mdoc;
68: struct mdoc *lastmdoc;
1.6 schwarze 69: enum outt outtype; /* Output devices... */
1.1 kristaps 70: out_mdoc outmdoc;
71: out_man outman;
72: out_free outfree;
73: void *outdata;
74: };
75:
76: extern void *ascii_alloc(void);
77: extern int tree_mdoc(void *, const struct mdoc *);
78: extern int tree_man(void *, const struct man *);
79: extern int terminal_mdoc(void *, const struct mdoc *);
80: extern int terminal_man(void *, const struct man *);
81: extern void terminal_free(void *);
82:
83: static int foptions(int *, char *);
84: static int toptions(enum outt *, char *);
85: static int moptions(enum intt *, char *);
86: static int woptions(int *, char *);
87: static int merr(void *, int, int, const char *);
88: static int manwarn(void *, int, int, const char *);
89: static int mdocwarn(void *, int, int,
90: enum mdoc_warn, const char *);
91: static int ffile(struct buf *, struct buf *,
92: const char *, struct curparse *);
93: static int fdesc(struct buf *, struct buf *,
94: struct curparse *);
95: static int pset(const char *, int, struct curparse *,
96: struct man **, struct mdoc **);
97: static struct man *man_init(struct curparse *);
98: static struct mdoc *mdoc_init(struct curparse *);
1.3 schwarze 99: __dead static void version(void);
1.1 kristaps 100: __dead static void usage(void);
101:
102: extern char *__progname;
103:
104:
105: int
106: main(int argc, char *argv[])
107: {
108: int c, rc;
109: struct buf ln, blk;
110: struct curparse curp;
111:
112: bzero(&curp, sizeof(struct curparse));
113:
114: curp.inttype = INTT_AUTO;
115: curp.outtype = OUTT_ASCII;
116:
117: /* LINTED */
1.3 schwarze 118: while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))
1.1 kristaps 119: switch (c) {
120: case ('f'):
121: if ( ! foptions(&curp.fflags, optarg))
1.9 schwarze 122: return(EXIT_FAILURE);
1.1 kristaps 123: break;
124: case ('m'):
125: if ( ! moptions(&curp.inttype, optarg))
1.9 schwarze 126: return(EXIT_FAILURE);
1.1 kristaps 127: break;
128: case ('T'):
129: if ( ! toptions(&curp.outtype, optarg))
1.9 schwarze 130: return(EXIT_FAILURE);
1.1 kristaps 131: break;
132: case ('W'):
133: if ( ! woptions(&curp.wflags, optarg))
1.9 schwarze 134: return(EXIT_FAILURE);
1.1 kristaps 135: break;
1.3 schwarze 136: case ('V'):
137: version();
138: /* NOTREACHED */
1.1 kristaps 139: default:
140: usage();
141: /* NOTREACHED */
142: }
143:
144: argc -= optind;
145: argv += optind;
146:
147: bzero(&ln, sizeof(struct buf));
148: bzero(&blk, sizeof(struct buf));
149:
150: rc = 1;
151:
1.7 schwarze 152: if (NULL == *argv) {
153: curp.file = "<stdin>";
154: curp.fd = STDIN_FILENO;
155: if ( ! fdesc(&blk, &ln, &curp))
1.1 kristaps 156: rc = 0;
1.7 schwarze 157: }
1.1 kristaps 158:
159: while (rc && *argv) {
160: if ( ! ffile(&blk, &ln, *argv, &curp))
161: rc = 0;
162: argv++;
163: if (*argv && rc) {
164: if (curp.lastman)
165: if ( ! man_reset(curp.lastman))
166: rc = 0;
167: if (curp.lastmdoc)
168: if ( ! mdoc_reset(curp.lastmdoc))
169: rc = 0;
170: curp.lastman = NULL;
171: curp.lastmdoc = NULL;
172: }
173: }
174:
175: if (blk.buf)
176: free(blk.buf);
177: if (ln.buf)
178: free(ln.buf);
179: if (curp.outfree)
180: (*curp.outfree)(curp.outdata);
181: if (curp.mdoc)
182: mdoc_free(curp.mdoc);
183: if (curp.man)
184: man_free(curp.man);
185:
186: return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
187: }
188:
189:
190: __dead static void
1.3 schwarze 191: version(void)
192: {
193:
194: (void)printf("%s %s\n", __progname, VERSION);
195: exit(EXIT_SUCCESS);
196: }
197:
198:
199: __dead static void
1.1 kristaps 200: usage(void)
201: {
202:
1.3 schwarze 203: (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
1.1 kristaps 204: "[-mformat] [-Toutput] [-Werr...]\n",
205: __progname);
206: exit(EXIT_FAILURE);
207: }
208:
209:
210: static struct man *
211: man_init(struct curparse *curp)
212: {
213: int pflags;
214: struct man *man;
215: struct man_cb mancb;
216:
217: mancb.man_err = merr;
218: mancb.man_warn = manwarn;
219:
1.6 schwarze 220: /* Defaults from mandoc.1. */
1.2 schwarze 221:
1.11 ! schwarze 222: pflags = MAN_IGN_MACRO | MAN_IGN_ESCAPE | MAN_IGN_CHARS;
1.2 schwarze 223:
1.1 kristaps 224: if (curp->fflags & NO_IGN_MACRO)
225: pflags &= ~MAN_IGN_MACRO;
1.8 schwarze 226: if (curp->fflags & NO_IGN_CHARS)
227: pflags &= ~MAN_IGN_CHARS;
1.11 ! schwarze 228: if (curp->fflags & NO_IGN_ESCAPE)
! 229: pflags &= ~MAN_IGN_ESCAPE;
1.1 kristaps 230:
231: if (NULL == (man = man_alloc(curp, pflags, &mancb)))
232: warnx("memory exhausted");
233:
234: return(man);
235: }
236:
237:
238: static struct mdoc *
239: mdoc_init(struct curparse *curp)
240: {
241: int pflags;
242: struct mdoc *mdoc;
243: struct mdoc_cb mdoccb;
244:
245: mdoccb.mdoc_err = merr;
246: mdoccb.mdoc_warn = mdocwarn;
247:
1.6 schwarze 248: /* Defaults from mandoc.1. */
1.2 schwarze 249:
1.1 kristaps 250: pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
251:
252: if (curp->fflags & IGN_SCOPE)
253: pflags |= MDOC_IGN_SCOPE;
254: if (curp->fflags & NO_IGN_ESCAPE)
255: pflags &= ~MDOC_IGN_ESCAPE;
256: if (curp->fflags & NO_IGN_MACRO)
257: pflags &= ~MDOC_IGN_MACRO;
258: if (curp->fflags & NO_IGN_CHARS)
259: pflags &= ~MDOC_IGN_CHARS;
260:
261: if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))
262: warnx("memory exhausted");
263:
264: return(mdoc);
265: }
266:
267:
268: static int
269: ffile(struct buf *blk, struct buf *ln,
270: const char *file, struct curparse *curp)
271: {
272: int c;
273:
274: curp->file = file;
275: if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
276: warn("%s", curp->file);
277: return(0);
278: }
279:
280: c = fdesc(blk, ln, curp);
281:
282: if (-1 == close(curp->fd))
283: warn("%s", curp->file);
284:
285: return(c);
286: }
287:
288:
289: static int
290: fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
291: {
292: size_t sz;
293: ssize_t ssz;
294: struct stat st;
1.5 schwarze 295: int j, i, pos, lnn, comment;
1.1 kristaps 296: struct man *man;
297: struct mdoc *mdoc;
298:
299: sz = BUFSIZ;
300: man = NULL;
301: mdoc = NULL;
302:
303: /*
304: * Two buffers: ln and buf. buf is the input buffer optimised
305: * here for each file's block size. ln is a line buffer. Both
306: * growable, hence passed in by ptr-ptr.
307: */
308:
309: if (-1 == fstat(curp->fd, &st))
1.9 schwarze 310: warn("%s", curp->file);
1.1 kristaps 311: else if ((size_t)st.st_blksize > sz)
312: sz = st.st_blksize;
313:
314: if (sz > blk->sz) {
315: blk->buf = realloc(blk->buf, sz);
316: if (NULL == blk->buf) {
317: warn("realloc");
318: return(0);
319: }
320: blk->sz = sz;
321: }
322:
323: /* Fill buf with file blocksize. */
324:
1.5 schwarze 325: for (lnn = pos = comment = 0; ; ) {
1.1 kristaps 326: if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
327: warn("%s", curp->file);
328: return(0);
329: } else if (0 == ssz)
330: break;
331:
332: /* Parse the read block into partial or full lines. */
333:
334: for (i = 0; i < (int)ssz; i++) {
335: if (pos >= (int)ln->sz) {
336: ln->sz += 256; /* Step-size. */
337: ln->buf = realloc(ln->buf, ln->sz);
338: if (NULL == ln->buf) {
339: warn("realloc");
340: return(0);
341: }
342: }
343:
344: if ('\n' != blk->buf[i]) {
1.5 schwarze 345: if (comment)
346: continue;
1.1 kristaps 347: ln->buf[pos++] = blk->buf[i];
1.5 schwarze 348:
349: /* Handle in-line `\"' comments. */
350:
351: if (1 == pos || '\"' != ln->buf[pos - 1])
352: continue;
353:
354: for (j = pos - 2; j >= 0; j--)
355: if ('\\' != ln->buf[j])
356: break;
357:
358: if ( ! ((pos - 2 - j) % 2))
359: continue;
360:
361: comment = 1;
362: pos -= 2;
1.1 kristaps 363: continue;
1.5 schwarze 364: }
1.1 kristaps 365:
1.5 schwarze 366: /* Handle escaped `\\n' newlines. */
1.1 kristaps 367:
1.5 schwarze 368: if (pos > 0 && 0 == comment &&
369: '\\' == ln->buf[pos - 1]) {
1.1 kristaps 370: for (j = pos - 1; j >= 0; j--)
371: if ('\\' != ln->buf[j])
372: break;
373: if ( ! ((pos - j) % 2)) {
374: pos--;
375: lnn++;
376: continue;
377: }
378: }
379:
380: ln->buf[pos] = 0;
381: lnn++;
1.5 schwarze 382:
383: /* If unset, assign parser in pset(). */
1.1 kristaps 384:
385: if ( ! (man || mdoc) && ! pset(ln->buf,
386: pos, curp, &man, &mdoc))
387: return(0);
388:
1.5 schwarze 389: pos = comment = 0;
390:
391: /* Pass down into parsers. */
1.1 kristaps 392:
393: if (man && ! man_parseln(man, lnn, ln->buf))
394: return(0);
395: if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
396: return(0);
397: }
398: }
399:
1.5 schwarze 400: /* NOTE a parser may not have been assigned, yet. */
1.1 kristaps 401:
402: if ( ! (man || mdoc)) {
403: warnx("%s: not a manual", curp->file);
404: return(0);
405: }
406:
407: if (mdoc && ! mdoc_endparse(mdoc))
408: return(0);
409: if (man && ! man_endparse(man))
410: return(0);
411:
1.5 schwarze 412: /* If unset, allocate output dev now (if applicable). */
1.1 kristaps 413:
414: if ( ! (curp->outman && curp->outmdoc)) {
415: switch (curp->outtype) {
416: case (OUTT_TREE):
417: curp->outman = tree_man;
418: curp->outmdoc = tree_mdoc;
419: break;
420: case (OUTT_LINT):
421: break;
422: default:
423: curp->outdata = ascii_alloc();
424: curp->outman = terminal_man;
425: curp->outmdoc = terminal_mdoc;
426: curp->outfree = terminal_free;
427: break;
428: }
429: }
430:
431: /* Execute the out device, if it exists. */
432:
433: if (man && curp->outman)
434: if ( ! (*curp->outman)(curp->outdata, man))
435: return(0);
436: if (mdoc && curp->outmdoc)
437: if ( ! (*curp->outmdoc)(curp->outdata, mdoc))
438: return(0);
439:
440: return(1);
441: }
442:
443:
444: static int
445: pset(const char *buf, int pos, struct curparse *curp,
446: struct man **man, struct mdoc **mdoc)
447: {
1.5 schwarze 448: int i;
1.1 kristaps 449:
450: /*
451: * Try to intuit which kind of manual parser should be used. If
452: * passed in by command-line (-man, -mdoc), then use that
453: * explicitly. If passed as -mandoc, then try to guess from the
1.5 schwarze 454: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.1 kristaps 455: * default to -man, which is more lenient.
456: */
457:
1.5 schwarze 458: if (buf[0] == '.') {
459: for (i = 1; buf[i]; i++)
460: if (' ' != buf[i] && '\t' != buf[i])
461: break;
462: if (0 == buf[i])
463: return(1);
464: }
1.1 kristaps 465:
466: switch (curp->inttype) {
467: case (INTT_MDOC):
468: if (NULL == curp->mdoc)
469: curp->mdoc = mdoc_init(curp);
470: if (NULL == (*mdoc = curp->mdoc))
471: return(0);
472: curp->lastmdoc = *mdoc;
473: return(1);
474: case (INTT_MAN):
475: if (NULL == curp->man)
476: curp->man = man_init(curp);
477: if (NULL == (*man = curp->man))
478: return(0);
479: curp->lastman = *man;
480: return(1);
481: default:
482: break;
483: }
484:
485: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
486: if (NULL == curp->mdoc)
487: curp->mdoc = mdoc_init(curp);
488: if (NULL == (*mdoc = curp->mdoc))
489: return(0);
490: curp->lastmdoc = *mdoc;
491: return(1);
492: }
493:
494: if (NULL == curp->man)
495: curp->man = man_init(curp);
496: if (NULL == (*man = curp->man))
497: return(0);
498: curp->lastman = *man;
499: return(1);
500: }
501:
502:
503: static int
504: moptions(enum intt *tflags, char *arg)
505: {
506:
507: if (0 == strcmp(arg, "doc"))
508: *tflags = INTT_MDOC;
509: else if (0 == strcmp(arg, "andoc"))
510: *tflags = INTT_AUTO;
511: else if (0 == strcmp(arg, "an"))
512: *tflags = INTT_MAN;
513: else {
514: warnx("bad argument: -m%s", arg);
515: return(0);
516: }
517:
518: return(1);
519: }
520:
521:
522: static int
523: toptions(enum outt *tflags, char *arg)
524: {
525:
526: if (0 == strcmp(arg, "ascii"))
527: *tflags = OUTT_ASCII;
528: else if (0 == strcmp(arg, "lint"))
529: *tflags = OUTT_LINT;
530: else if (0 == strcmp(arg, "tree"))
531: *tflags = OUTT_TREE;
532: else {
533: warnx("bad argument: -T%s", arg);
534: return(0);
535: }
536:
537: return(1);
538: }
539:
540:
541: static int
542: foptions(int *fflags, char *arg)
543: {
1.10 schwarze 544: char *v, *o;
1.1 kristaps 545: char *toks[6];
546:
547: toks[0] = "ign-scope";
548: toks[1] = "no-ign-escape";
549: toks[2] = "no-ign-macro";
550: toks[3] = "no-ign-chars";
551: toks[4] = "strict";
552: toks[5] = NULL;
553:
1.10 schwarze 554: while (*arg) {
555: o = arg;
1.1 kristaps 556: switch (getsubopt(&arg, toks, &v)) {
557: case (0):
558: *fflags |= IGN_SCOPE;
559: break;
560: case (1):
561: *fflags |= NO_IGN_ESCAPE;
562: break;
563: case (2):
564: *fflags |= NO_IGN_MACRO;
565: break;
566: case (3):
567: *fflags |= NO_IGN_CHARS;
568: break;
569: case (4):
570: *fflags |= NO_IGN_ESCAPE |
571: NO_IGN_MACRO | NO_IGN_CHARS;
572: break;
573: default:
1.10 schwarze 574: warnx("bad argument: -f%s", o);
1.1 kristaps 575: return(0);
576: }
1.10 schwarze 577: }
1.1 kristaps 578:
579: return(1);
580: }
581:
582:
583: static int
584: woptions(int *wflags, char *arg)
585: {
1.10 schwarze 586: char *v, *o;
1.1 kristaps 587: char *toks[5];
588:
589: toks[0] = "all";
590: toks[1] = "compat";
591: toks[2] = "syntax";
592: toks[3] = "error";
593: toks[4] = NULL;
594:
1.10 schwarze 595: while (*arg) {
596: o = arg;
1.1 kristaps 597: switch (getsubopt(&arg, toks, &v)) {
598: case (0):
599: *wflags |= WARN_WALL;
600: break;
601: case (1):
602: *wflags |= WARN_WCOMPAT;
603: break;
604: case (2):
605: *wflags |= WARN_WSYNTAX;
606: break;
607: case (3):
608: *wflags |= WARN_WERR;
609: break;
610: default:
1.10 schwarze 611: warnx("bad argument: -W%s", o);
1.1 kristaps 612: return(0);
613: }
1.10 schwarze 614: }
1.1 kristaps 615:
616: return(1);
617: }
618:
619:
620: /* ARGSUSED */
621: static int
622: merr(void *arg, int line, int col, const char *msg)
623: {
624: struct curparse *curp;
625:
626: curp = (struct curparse *)arg;
627: warnx("%s:%d: error: %s (column %d)",
628: curp->file, line, msg, col);
1.2 schwarze 629:
1.1 kristaps 630: return(0);
631: }
632:
633:
634: static int
635: mdocwarn(void *arg, int line, int col,
636: enum mdoc_warn type, const char *msg)
637: {
638: struct curparse *curp;
639: char *wtype;
640:
641: curp = (struct curparse *)arg;
642: wtype = NULL;
643:
644: switch (type) {
645: case (WARN_COMPAT):
646: wtype = "compat";
647: if (curp->wflags & WARN_WCOMPAT)
648: break;
649: return(1);
650: case (WARN_SYNTAX):
651: wtype = "syntax";
652: if (curp->wflags & WARN_WSYNTAX)
653: break;
654: return(1);
655: }
656:
657: assert(wtype);
658: warnx("%s:%d: %s warning: %s (column %d)",
659: curp->file, line, wtype, msg, col);
660:
661: if ( ! (curp->wflags & WARN_WERR))
662: return(1);
1.2 schwarze 663:
1.6 schwarze 664: warnx("considering warnings as errors");
1.1 kristaps 665: return(0);
666: }
667:
668:
669: static int
670: manwarn(void *arg, int line, int col, const char *msg)
671: {
672: struct curparse *curp;
673:
674: curp = (struct curparse *)arg;
675:
676: if ( ! (curp->wflags & WARN_WSYNTAX))
677: return(1);
678:
679: warnx("%s:%d: syntax warning: %s (column %d)",
680: curp->file, line, msg, col);
681:
682: if ( ! (curp->wflags & WARN_WERR))
683: return(1);
1.2 schwarze 684:
1.6 schwarze 685: warnx("considering warnings as errors");
1.1 kristaps 686: return(0);
687: }