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