Annotation of src/usr.bin/mandoc/main.c, Revision 1.10
1.10 ! schwarze 1: /* $Id: main.c,v 1.9 2009/07/06 22:12:38 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.8 schwarze 222: pflags = MAN_IGN_MACRO | 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.1 kristaps 228:
229: if (NULL == (man = man_alloc(curp, pflags, &mancb)))
230: warnx("memory exhausted");
231:
232: return(man);
233: }
234:
235:
236: static struct mdoc *
237: mdoc_init(struct curparse *curp)
238: {
239: int pflags;
240: struct mdoc *mdoc;
241: struct mdoc_cb mdoccb;
242:
243: mdoccb.mdoc_err = merr;
244: mdoccb.mdoc_warn = mdocwarn;
245:
1.6 schwarze 246: /* Defaults from mandoc.1. */
1.2 schwarze 247:
1.1 kristaps 248: pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
249:
250: if (curp->fflags & IGN_SCOPE)
251: pflags |= MDOC_IGN_SCOPE;
252: if (curp->fflags & NO_IGN_ESCAPE)
253: pflags &= ~MDOC_IGN_ESCAPE;
254: if (curp->fflags & NO_IGN_MACRO)
255: pflags &= ~MDOC_IGN_MACRO;
256: if (curp->fflags & NO_IGN_CHARS)
257: pflags &= ~MDOC_IGN_CHARS;
258:
259: if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))
260: warnx("memory exhausted");
261:
262: return(mdoc);
263: }
264:
265:
266: static int
267: ffile(struct buf *blk, struct buf *ln,
268: const char *file, struct curparse *curp)
269: {
270: int c;
271:
272: curp->file = file;
273: if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
274: warn("%s", curp->file);
275: return(0);
276: }
277:
278: c = fdesc(blk, ln, curp);
279:
280: if (-1 == close(curp->fd))
281: warn("%s", curp->file);
282:
283: return(c);
284: }
285:
286:
287: static int
288: fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
289: {
290: size_t sz;
291: ssize_t ssz;
292: struct stat st;
1.5 schwarze 293: int j, i, pos, lnn, comment;
1.1 kristaps 294: struct man *man;
295: struct mdoc *mdoc;
296:
297: sz = BUFSIZ;
298: man = NULL;
299: mdoc = NULL;
300:
301: /*
302: * Two buffers: ln and buf. buf is the input buffer optimised
303: * here for each file's block size. ln is a line buffer. Both
304: * growable, hence passed in by ptr-ptr.
305: */
306:
307: if (-1 == fstat(curp->fd, &st))
1.9 schwarze 308: warn("%s", curp->file);
1.1 kristaps 309: else if ((size_t)st.st_blksize > sz)
310: sz = st.st_blksize;
311:
312: if (sz > blk->sz) {
313: blk->buf = realloc(blk->buf, sz);
314: if (NULL == blk->buf) {
315: warn("realloc");
316: return(0);
317: }
318: blk->sz = sz;
319: }
320:
321: /* Fill buf with file blocksize. */
322:
1.5 schwarze 323: for (lnn = pos = comment = 0; ; ) {
1.1 kristaps 324: if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
325: warn("%s", curp->file);
326: return(0);
327: } else if (0 == ssz)
328: break;
329:
330: /* Parse the read block into partial or full lines. */
331:
332: for (i = 0; i < (int)ssz; i++) {
333: if (pos >= (int)ln->sz) {
334: ln->sz += 256; /* Step-size. */
335: ln->buf = realloc(ln->buf, ln->sz);
336: if (NULL == ln->buf) {
337: warn("realloc");
338: return(0);
339: }
340: }
341:
342: if ('\n' != blk->buf[i]) {
1.5 schwarze 343: if (comment)
344: continue;
1.1 kristaps 345: ln->buf[pos++] = blk->buf[i];
1.5 schwarze 346:
347: /* Handle in-line `\"' comments. */
348:
349: if (1 == pos || '\"' != ln->buf[pos - 1])
350: continue;
351:
352: for (j = pos - 2; j >= 0; j--)
353: if ('\\' != ln->buf[j])
354: break;
355:
356: if ( ! ((pos - 2 - j) % 2))
357: continue;
358:
359: comment = 1;
360: pos -= 2;
1.1 kristaps 361: continue;
1.5 schwarze 362: }
1.1 kristaps 363:
1.5 schwarze 364: /* Handle escaped `\\n' newlines. */
1.1 kristaps 365:
1.5 schwarze 366: if (pos > 0 && 0 == comment &&
367: '\\' == ln->buf[pos - 1]) {
1.1 kristaps 368: for (j = pos - 1; j >= 0; j--)
369: if ('\\' != ln->buf[j])
370: break;
371: if ( ! ((pos - j) % 2)) {
372: pos--;
373: lnn++;
374: continue;
375: }
376: }
377:
378: ln->buf[pos] = 0;
379: lnn++;
1.5 schwarze 380:
381: /* If unset, assign parser in pset(). */
1.1 kristaps 382:
383: if ( ! (man || mdoc) && ! pset(ln->buf,
384: pos, curp, &man, &mdoc))
385: return(0);
386:
1.5 schwarze 387: pos = comment = 0;
388:
389: /* Pass down into parsers. */
1.1 kristaps 390:
391: if (man && ! man_parseln(man, lnn, ln->buf))
392: return(0);
393: if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
394: return(0);
395: }
396: }
397:
1.5 schwarze 398: /* NOTE a parser may not have been assigned, yet. */
1.1 kristaps 399:
400: if ( ! (man || mdoc)) {
401: warnx("%s: not a manual", curp->file);
402: return(0);
403: }
404:
405: if (mdoc && ! mdoc_endparse(mdoc))
406: return(0);
407: if (man && ! man_endparse(man))
408: return(0);
409:
1.5 schwarze 410: /* If unset, allocate output dev now (if applicable). */
1.1 kristaps 411:
412: if ( ! (curp->outman && curp->outmdoc)) {
413: switch (curp->outtype) {
414: case (OUTT_TREE):
415: curp->outman = tree_man;
416: curp->outmdoc = tree_mdoc;
417: break;
418: case (OUTT_LINT):
419: break;
420: default:
421: curp->outdata = ascii_alloc();
422: curp->outman = terminal_man;
423: curp->outmdoc = terminal_mdoc;
424: curp->outfree = terminal_free;
425: break;
426: }
427: }
428:
429: /* Execute the out device, if it exists. */
430:
431: if (man && curp->outman)
432: if ( ! (*curp->outman)(curp->outdata, man))
433: return(0);
434: if (mdoc && curp->outmdoc)
435: if ( ! (*curp->outmdoc)(curp->outdata, mdoc))
436: return(0);
437:
438: return(1);
439: }
440:
441:
442: static int
443: pset(const char *buf, int pos, struct curparse *curp,
444: struct man **man, struct mdoc **mdoc)
445: {
1.5 schwarze 446: int i;
1.1 kristaps 447:
448: /*
449: * Try to intuit which kind of manual parser should be used. If
450: * passed in by command-line (-man, -mdoc), then use that
451: * explicitly. If passed as -mandoc, then try to guess from the
1.5 schwarze 452: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.1 kristaps 453: * default to -man, which is more lenient.
454: */
455:
1.5 schwarze 456: if (buf[0] == '.') {
457: for (i = 1; buf[i]; i++)
458: if (' ' != buf[i] && '\t' != buf[i])
459: break;
460: if (0 == buf[i])
461: return(1);
462: }
1.1 kristaps 463:
464: switch (curp->inttype) {
465: case (INTT_MDOC):
466: if (NULL == curp->mdoc)
467: curp->mdoc = mdoc_init(curp);
468: if (NULL == (*mdoc = curp->mdoc))
469: return(0);
470: curp->lastmdoc = *mdoc;
471: return(1);
472: case (INTT_MAN):
473: if (NULL == curp->man)
474: curp->man = man_init(curp);
475: if (NULL == (*man = curp->man))
476: return(0);
477: curp->lastman = *man;
478: return(1);
479: default:
480: break;
481: }
482:
483: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
484: if (NULL == curp->mdoc)
485: curp->mdoc = mdoc_init(curp);
486: if (NULL == (*mdoc = curp->mdoc))
487: return(0);
488: curp->lastmdoc = *mdoc;
489: return(1);
490: }
491:
492: if (NULL == curp->man)
493: curp->man = man_init(curp);
494: if (NULL == (*man = curp->man))
495: return(0);
496: curp->lastman = *man;
497: return(1);
498: }
499:
500:
501: static int
502: moptions(enum intt *tflags, char *arg)
503: {
504:
505: if (0 == strcmp(arg, "doc"))
506: *tflags = INTT_MDOC;
507: else if (0 == strcmp(arg, "andoc"))
508: *tflags = INTT_AUTO;
509: else if (0 == strcmp(arg, "an"))
510: *tflags = INTT_MAN;
511: else {
512: warnx("bad argument: -m%s", arg);
513: return(0);
514: }
515:
516: return(1);
517: }
518:
519:
520: static int
521: toptions(enum outt *tflags, char *arg)
522: {
523:
524: if (0 == strcmp(arg, "ascii"))
525: *tflags = OUTT_ASCII;
526: else if (0 == strcmp(arg, "lint"))
527: *tflags = OUTT_LINT;
528: else if (0 == strcmp(arg, "tree"))
529: *tflags = OUTT_TREE;
530: else {
531: warnx("bad argument: -T%s", arg);
532: return(0);
533: }
534:
535: return(1);
536: }
537:
538:
539: static int
540: foptions(int *fflags, char *arg)
541: {
1.10 ! schwarze 542: char *v, *o;
1.1 kristaps 543: char *toks[6];
544:
545: toks[0] = "ign-scope";
546: toks[1] = "no-ign-escape";
547: toks[2] = "no-ign-macro";
548: toks[3] = "no-ign-chars";
549: toks[4] = "strict";
550: toks[5] = NULL;
551:
1.10 ! schwarze 552: while (*arg) {
! 553: o = arg;
1.1 kristaps 554: switch (getsubopt(&arg, toks, &v)) {
555: case (0):
556: *fflags |= IGN_SCOPE;
557: break;
558: case (1):
559: *fflags |= NO_IGN_ESCAPE;
560: break;
561: case (2):
562: *fflags |= NO_IGN_MACRO;
563: break;
564: case (3):
565: *fflags |= NO_IGN_CHARS;
566: break;
567: case (4):
568: *fflags |= NO_IGN_ESCAPE |
569: NO_IGN_MACRO | NO_IGN_CHARS;
570: break;
571: default:
1.10 ! schwarze 572: warnx("bad argument: -f%s", o);
1.1 kristaps 573: return(0);
574: }
1.10 ! schwarze 575: }
1.1 kristaps 576:
577: return(1);
578: }
579:
580:
581: static int
582: woptions(int *wflags, char *arg)
583: {
1.10 ! schwarze 584: char *v, *o;
1.1 kristaps 585: char *toks[5];
586:
587: toks[0] = "all";
588: toks[1] = "compat";
589: toks[2] = "syntax";
590: toks[3] = "error";
591: toks[4] = NULL;
592:
1.10 ! schwarze 593: while (*arg) {
! 594: o = arg;
1.1 kristaps 595: switch (getsubopt(&arg, toks, &v)) {
596: case (0):
597: *wflags |= WARN_WALL;
598: break;
599: case (1):
600: *wflags |= WARN_WCOMPAT;
601: break;
602: case (2):
603: *wflags |= WARN_WSYNTAX;
604: break;
605: case (3):
606: *wflags |= WARN_WERR;
607: break;
608: default:
1.10 ! schwarze 609: warnx("bad argument: -W%s", o);
1.1 kristaps 610: return(0);
611: }
1.10 ! schwarze 612: }
1.1 kristaps 613:
614: return(1);
615: }
616:
617:
618: /* ARGSUSED */
619: static int
620: merr(void *arg, int line, int col, const char *msg)
621: {
622: struct curparse *curp;
623:
624: curp = (struct curparse *)arg;
625: warnx("%s:%d: error: %s (column %d)",
626: curp->file, line, msg, col);
1.2 schwarze 627:
1.1 kristaps 628: return(0);
629: }
630:
631:
632: static int
633: mdocwarn(void *arg, int line, int col,
634: enum mdoc_warn type, const char *msg)
635: {
636: struct curparse *curp;
637: char *wtype;
638:
639: curp = (struct curparse *)arg;
640: wtype = NULL;
641:
642: switch (type) {
643: case (WARN_COMPAT):
644: wtype = "compat";
645: if (curp->wflags & WARN_WCOMPAT)
646: break;
647: return(1);
648: case (WARN_SYNTAX):
649: wtype = "syntax";
650: if (curp->wflags & WARN_WSYNTAX)
651: break;
652: return(1);
653: }
654:
655: assert(wtype);
656: warnx("%s:%d: %s warning: %s (column %d)",
657: curp->file, line, wtype, msg, col);
658:
659: if ( ! (curp->wflags & WARN_WERR))
660: return(1);
1.2 schwarze 661:
1.6 schwarze 662: warnx("considering warnings as errors");
1.1 kristaps 663: return(0);
664: }
665:
666:
667: static int
668: manwarn(void *arg, int line, int col, const char *msg)
669: {
670: struct curparse *curp;
671:
672: curp = (struct curparse *)arg;
673:
674: if ( ! (curp->wflags & WARN_WSYNTAX))
675: return(1);
676:
677: warnx("%s:%d: syntax warning: %s (column %d)",
678: curp->file, line, msg, col);
679:
680: if ( ! (curp->wflags & WARN_WERR))
681: return(1);
1.2 schwarze 682:
1.6 schwarze 683: warnx("considering warnings as errors");
1.1 kristaps 684: return(0);
685: }