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