Annotation of src/usr.bin/mandoc/main.c, Revision 1.4
1.4 ! schwarze 1: /* $Id: main.c,v 1.3 2009/06/14 23:39:43 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. */
64: enum intt inttype; /* Input parsers. */
65: struct man *man;
66: struct man *lastman;
67: struct mdoc *mdoc;
68: struct mdoc *lastmdoc;
69: enum outt outtype; /* Output devices. */
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 fstdin(struct buf *, struct buf *,
92: struct curparse *);
93: static int ffile(struct buf *, struct buf *,
94: const char *, struct curparse *);
95: static int fdesc(struct buf *, struct buf *,
96: struct curparse *);
97: static int pset(const char *, int, struct curparse *,
98: struct man **, struct mdoc **);
99: static struct man *man_init(struct curparse *);
100: static struct mdoc *mdoc_init(struct curparse *);
1.3 schwarze 101: __dead static void version(void);
1.1 kristaps 102: __dead static void usage(void);
103:
104: extern char *__progname;
105:
106:
107: int
108: main(int argc, char *argv[])
109: {
110: int c, rc;
111: struct buf ln, blk;
112: struct curparse curp;
113:
114: bzero(&curp, sizeof(struct curparse));
115:
116: curp.inttype = INTT_AUTO;
117: curp.outtype = OUTT_ASCII;
118:
119: /* LINTED */
1.3 schwarze 120: while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))
1.1 kristaps 121: switch (c) {
122: case ('f'):
123: if ( ! foptions(&curp.fflags, optarg))
124: return(0);
125: break;
126: case ('m'):
127: if ( ! moptions(&curp.inttype, optarg))
128: return(0);
129: break;
130: case ('T'):
131: if ( ! toptions(&curp.outtype, optarg))
132: return(0);
133: break;
134: case ('W'):
135: if ( ! woptions(&curp.wflags, optarg))
136: return(0);
137: break;
1.3 schwarze 138: case ('V'):
139: version();
140: /* NOTREACHED */
1.1 kristaps 141: default:
142: usage();
143: /* NOTREACHED */
144: }
145:
146: argc -= optind;
147: argv += optind;
148:
149: /* Configure buffers. */
150:
151: bzero(&ln, sizeof(struct buf));
152: bzero(&blk, sizeof(struct buf));
153:
154: rc = 1;
155:
156: if (NULL == *argv)
157: if ( ! fstdin(&blk, &ln, &curp))
158: rc = 0;
159:
160: while (rc && *argv) {
161: if ( ! ffile(&blk, &ln, *argv, &curp))
162: rc = 0;
163: argv++;
164: if (*argv && rc) {
165: if (curp.lastman)
166: if ( ! man_reset(curp.lastman))
167: rc = 0;
168: if (curp.lastmdoc)
169: if ( ! mdoc_reset(curp.lastmdoc))
170: rc = 0;
171: curp.lastman = NULL;
172: curp.lastmdoc = NULL;
173: }
174: }
175:
176: if (blk.buf)
177: free(blk.buf);
178: if (ln.buf)
179: free(ln.buf);
1.2 schwarze 180:
181: /* TODO: have a curp_free routine. */
1.1 kristaps 182: if (curp.outfree)
183: (*curp.outfree)(curp.outdata);
184: if (curp.mdoc)
185: mdoc_free(curp.mdoc);
186: if (curp.man)
187: man_free(curp.man);
188:
189: return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
190: }
191:
192:
193: __dead static void
1.3 schwarze 194: version(void)
195: {
196:
197: (void)printf("%s %s\n", __progname, VERSION);
198: exit(EXIT_SUCCESS);
199: }
200:
201:
202: __dead static void
1.1 kristaps 203: usage(void)
204: {
205:
1.3 schwarze 206: (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
1.1 kristaps 207: "[-mformat] [-Toutput] [-Werr...]\n",
208: __progname);
209: exit(EXIT_FAILURE);
210: }
211:
212:
213: static struct man *
214: man_init(struct curparse *curp)
215: {
216: int pflags;
217: struct man *man;
218: struct man_cb mancb;
219:
220: mancb.man_err = merr;
221: mancb.man_warn = manwarn;
222:
1.2 schwarze 223: /*
224: * Default behaviour is to ignore unknown macros. This is
225: * specified in mandoc.1.
226: */
227:
228: pflags = MAN_IGN_MACRO;
229:
230: /* Override default behaviour... */
1.1 kristaps 231:
232: if (curp->fflags & NO_IGN_MACRO)
233: pflags &= ~MAN_IGN_MACRO;
234:
235: if (NULL == (man = man_alloc(curp, pflags, &mancb)))
236: warnx("memory exhausted");
237:
238: return(man);
239: }
240:
241:
242: static struct mdoc *
243: mdoc_init(struct curparse *curp)
244: {
245: int pflags;
246: struct mdoc *mdoc;
247: struct mdoc_cb mdoccb;
248:
249: mdoccb.mdoc_err = merr;
250: mdoccb.mdoc_warn = mdocwarn;
251:
1.2 schwarze 252: /*
253: * Default behaviour is to ignore unknown macros, escape
254: * sequences and characters (very liberal). This is specified
255: * in mandoc.1.
256: */
257:
1.1 kristaps 258: pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
259:
1.2 schwarze 260: /* Override default behaviour... */
261:
1.1 kristaps 262: if (curp->fflags & IGN_SCOPE)
263: pflags |= MDOC_IGN_SCOPE;
264: if (curp->fflags & NO_IGN_ESCAPE)
265: pflags &= ~MDOC_IGN_ESCAPE;
266: if (curp->fflags & NO_IGN_MACRO)
267: pflags &= ~MDOC_IGN_MACRO;
268: if (curp->fflags & NO_IGN_CHARS)
269: pflags &= ~MDOC_IGN_CHARS;
270:
271: if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))
272: warnx("memory exhausted");
273:
274: return(mdoc);
275: }
276:
277:
278: static int
279: fstdin(struct buf *blk, struct buf *ln, struct curparse *curp)
280: {
281:
282: curp->file = "<stdin>";
283: curp->fd = STDIN_FILENO;
284: return(fdesc(blk, ln, curp));
285: }
286:
287:
288: static int
289: ffile(struct buf *blk, struct buf *ln,
290: const char *file, struct curparse *curp)
291: {
292: int c;
293:
294: curp->file = file;
295: if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
296: warn("%s", curp->file);
297: return(0);
298: }
299:
300: c = fdesc(blk, ln, curp);
301:
302: if (-1 == close(curp->fd))
303: warn("%s", curp->file);
304:
305: return(c);
306: }
307:
308:
309: static int
310: fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
311: {
312: size_t sz;
313: ssize_t ssz;
314: struct stat st;
315: int j, i, pos, lnn;
316: struct man *man;
317: struct mdoc *mdoc;
318:
319: sz = BUFSIZ;
320: man = NULL;
321: mdoc = NULL;
322:
323: /*
324: * Two buffers: ln and buf. buf is the input buffer optimised
325: * here for each file's block size. ln is a line buffer. Both
326: * growable, hence passed in by ptr-ptr.
327: */
328:
329: if (-1 == fstat(curp->fd, &st))
330: warnx("%s", curp->file);
331: else if ((size_t)st.st_blksize > sz)
332: sz = st.st_blksize;
333:
334: if (sz > blk->sz) {
335: blk->buf = realloc(blk->buf, sz);
336: if (NULL == blk->buf) {
337: warn("realloc");
338: return(0);
339: }
340: blk->sz = sz;
341: }
342:
343: /* Fill buf with file blocksize. */
344:
345: for (lnn = 0, pos = 0; ; ) {
346: if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
347: warn("%s", curp->file);
348: return(0);
349: } else if (0 == ssz)
350: break;
351:
352: /* Parse the read block into partial or full lines. */
353:
354: for (i = 0; i < (int)ssz; i++) {
355: if (pos >= (int)ln->sz) {
356: ln->sz += 256; /* Step-size. */
357: ln->buf = realloc(ln->buf, ln->sz);
358: if (NULL == ln->buf) {
359: warn("realloc");
360: return(0);
361: }
362: }
363:
364: if ('\n' != blk->buf[i]) {
365: ln->buf[pos++] = blk->buf[i];
366: continue;
367: }
368:
1.2 schwarze 369: /* Check for CPP-escaped newline. */
1.1 kristaps 370:
371: if (pos > 0 && '\\' == ln->buf[pos - 1]) {
372: for (j = pos - 1; j >= 0; j--)
373: if ('\\' != ln->buf[j])
374: break;
375:
376: if ( ! ((pos - j) % 2)) {
377: pos--;
378: lnn++;
379: continue;
380: }
381: }
382:
383: ln->buf[pos] = 0;
384: lnn++;
385:
386: /*
387: * If no manual parser has been assigned, then
388: * try to assign one in pset(), which may do
389: * nothing at all. After this, parse the manual
390: * line accordingly.
391: */
392:
393: if ( ! (man || mdoc) && ! pset(ln->buf,
394: pos, curp, &man, &mdoc))
395: return(0);
396:
397: pos = 0;
398:
399: if (man && ! man_parseln(man, lnn, ln->buf))
400: return(0);
401: if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
402: return(0);
403: }
404: }
405:
406: /* Note that a parser may not have been assigned, yet. */
407:
408: if ( ! (man || mdoc)) {
409: warnx("%s: not a manual", curp->file);
410: return(0);
411: }
412:
413: if (mdoc && ! mdoc_endparse(mdoc))
414: return(0);
415: if (man && ! man_endparse(man))
416: return(0);
417:
418: /*
419: * If an output device hasn't been allocated, see if we should
420: * do so now. Note that not all outtypes have functions, so
421: * this switch statement may be superfluous, but it's
422: * low-overhead enough not to matter very much.
423: */
424:
425: if ( ! (curp->outman && curp->outmdoc)) {
426: switch (curp->outtype) {
427: case (OUTT_TREE):
428: curp->outman = tree_man;
429: curp->outmdoc = tree_mdoc;
430: break;
431: case (OUTT_LINT):
432: break;
433: default:
434: curp->outdata = ascii_alloc();
435: curp->outman = terminal_man;
436: curp->outmdoc = terminal_mdoc;
437: curp->outfree = terminal_free;
438: break;
439: }
440: }
441:
442: /* Execute the out device, if it exists. */
443:
444: if (man && curp->outman)
445: if ( ! (*curp->outman)(curp->outdata, man))
446: return(0);
447: if (mdoc && curp->outmdoc)
448: if ( ! (*curp->outmdoc)(curp->outdata, mdoc))
449: return(0);
450:
451: return(1);
452: }
453:
454:
455: static int
456: pset(const char *buf, int pos, struct curparse *curp,
457: struct man **man, struct mdoc **mdoc)
458: {
459:
460: /*
461: * Try to intuit which kind of manual parser should be used. If
462: * passed in by command-line (-man, -mdoc), then use that
463: * explicitly. If passed as -mandoc, then try to guess from the
464: * line: either skip comments, use -mdoc when finding `.Dt', or
465: * default to -man, which is more lenient.
466: */
467:
468: if (pos >= 3 && 0 == memcmp(buf, ".\\\"", 3))
469: return(1);
470:
471: switch (curp->inttype) {
472: case (INTT_MDOC):
473: if (NULL == curp->mdoc)
474: curp->mdoc = mdoc_init(curp);
475: if (NULL == (*mdoc = curp->mdoc))
476: return(0);
477: curp->lastmdoc = *mdoc;
478: return(1);
479: case (INTT_MAN):
480: if (NULL == curp->man)
481: curp->man = man_init(curp);
482: if (NULL == (*man = curp->man))
483: return(0);
484: curp->lastman = *man;
485: return(1);
486: default:
487: break;
488: }
489:
490: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
491: if (NULL == curp->mdoc)
492: curp->mdoc = mdoc_init(curp);
493: if (NULL == (*mdoc = curp->mdoc))
494: return(0);
495: curp->lastmdoc = *mdoc;
496: return(1);
497: }
498:
499: if (NULL == curp->man)
500: curp->man = man_init(curp);
501: if (NULL == (*man = curp->man))
502: return(0);
503: curp->lastman = *man;
504: return(1);
505: }
506:
507:
508: static int
509: moptions(enum intt *tflags, char *arg)
510: {
511:
512: if (0 == strcmp(arg, "doc"))
513: *tflags = INTT_MDOC;
514: else if (0 == strcmp(arg, "andoc"))
515: *tflags = INTT_AUTO;
516: else if (0 == strcmp(arg, "an"))
517: *tflags = INTT_MAN;
518: else {
519: warnx("bad argument: -m%s", arg);
520: return(0);
521: }
522:
523: return(1);
524: }
525:
526:
527: static int
528: toptions(enum outt *tflags, char *arg)
529: {
530:
531: if (0 == strcmp(arg, "ascii"))
532: *tflags = OUTT_ASCII;
533: else if (0 == strcmp(arg, "lint"))
534: *tflags = OUTT_LINT;
535: else if (0 == strcmp(arg, "tree"))
536: *tflags = OUTT_TREE;
537: else {
538: warnx("bad argument: -T%s", arg);
539: return(0);
540: }
541:
542: return(1);
543: }
544:
545:
546: /*
547: * Parse out the options for [-fopt...] setting compiler options. These
548: * can be comma-delimited or called again.
549: */
550: static int
551: foptions(int *fflags, char *arg)
552: {
553: char *v;
554: char *toks[6];
555:
556: toks[0] = "ign-scope";
557: toks[1] = "no-ign-escape";
558: toks[2] = "no-ign-macro";
559: toks[3] = "no-ign-chars";
560: toks[4] = "strict";
561: toks[5] = NULL;
562:
563: while (*arg)
564: switch (getsubopt(&arg, toks, &v)) {
565: case (0):
566: *fflags |= IGN_SCOPE;
567: break;
568: case (1):
569: *fflags |= NO_IGN_ESCAPE;
570: break;
571: case (2):
572: *fflags |= NO_IGN_MACRO;
573: break;
574: case (3):
575: *fflags |= NO_IGN_CHARS;
576: break;
577: case (4):
578: *fflags |= NO_IGN_ESCAPE |
579: NO_IGN_MACRO | NO_IGN_CHARS;
580: break;
581: default:
582: warnx("bad argument: -f%s", arg);
583: return(0);
584: }
585:
586: return(1);
587: }
588:
589:
590: /*
591: * Parse out the options for [-Werr...], which sets warning modes.
592: * These can be comma-delimited or called again.
593: */
594: static int
595: woptions(int *wflags, char *arg)
596: {
597: char *v;
598: char *toks[5];
599:
600: toks[0] = "all";
601: toks[1] = "compat";
602: toks[2] = "syntax";
603: toks[3] = "error";
604: toks[4] = NULL;
605:
606: while (*arg)
607: switch (getsubopt(&arg, toks, &v)) {
608: case (0):
609: *wflags |= WARN_WALL;
610: break;
611: case (1):
612: *wflags |= WARN_WCOMPAT;
613: break;
614: case (2):
615: *wflags |= WARN_WSYNTAX;
616: break;
617: case (3):
618: *wflags |= WARN_WERR;
619: break;
620: default:
621: warnx("bad argument: -W%s", arg);
622: return(0);
623: }
624:
625: return(1);
626: }
627:
628:
629: /* ARGSUSED */
630: static int
631: merr(void *arg, int line, int col, const char *msg)
632: {
633: struct curparse *curp;
634:
635: curp = (struct curparse *)arg;
636: warnx("%s:%d: error: %s (column %d)",
637: curp->file, line, msg, col);
1.2 schwarze 638:
639: /* Always exit on errors... */
1.1 kristaps 640: return(0);
641: }
642:
643:
644: static int
645: mdocwarn(void *arg, int line, int col,
646: enum mdoc_warn type, const char *msg)
647: {
648: struct curparse *curp;
649: char *wtype;
650:
651: curp = (struct curparse *)arg;
652: wtype = NULL;
653:
654: switch (type) {
655: case (WARN_COMPAT):
656: wtype = "compat";
657: if (curp->wflags & WARN_WCOMPAT)
658: break;
659: return(1);
660: case (WARN_SYNTAX):
661: wtype = "syntax";
662: if (curp->wflags & WARN_WSYNTAX)
663: break;
664: return(1);
665: }
666:
667: assert(wtype);
668: warnx("%s:%d: %s warning: %s (column %d)",
669: curp->file, line, wtype, msg, col);
670:
671: if ( ! (curp->wflags & WARN_WERR))
672: return(1);
1.2 schwarze 673:
674: /*
675: * If the -Werror flag is passed in, as in gcc, then all
676: * warnings are considered as errors.
677: */
1.1 kristaps 678:
679: warnx("%s: considering warnings as errors",
680: __progname);
681: return(0);
682: }
683:
684:
685: static int
686: manwarn(void *arg, int line, int col, const char *msg)
687: {
688: struct curparse *curp;
689:
690: curp = (struct curparse *)arg;
691:
692: if ( ! (curp->wflags & WARN_WSYNTAX))
693: return(1);
694:
695: warnx("%s:%d: syntax warning: %s (column %d)",
696: curp->file, line, msg, col);
697:
698: if ( ! (curp->wflags & WARN_WERR))
699: return(1);
1.2 schwarze 700:
701: /*
702: * If the -Werror flag is passed in, as in gcc, then all
703: * warnings are considered as errors.
704: */
1.1 kristaps 705:
706: warnx("%s: considering warnings as errors",
707: __progname);
708: return(0);
709: }