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