- struct md_parse p;
- struct mdoc_cb cb;
- struct stat st;
- int c;
- extern char *optarg;
- extern int optind;
-
- (void)memset(&p, 0, sizeof(struct md_parse));
-
- while (-1 != (c = getopt(argc, argv, "vW:")))
- switch (c) {
- case ('v'):
- p.dbg++;
- break;
- case ('W'):
- if ( ! getsopts(&p, optarg))
- return(0);
- break;
- default:
- usage();
- return(0);
- }
-
- argv += optind;
- argc -= optind;
-
- /* Initialise the input file. */
-
- p.in = "-";
- p.fdin = STDIN_FILENO;
-
- if (argc > 0) {
- p.in = *argv++;
- p.fdin = open(p.in, O_RDONLY, 0);
- if (-1 == p.fdin)
- err(1, "%s", p.in);
- }
-
- /* Allocate a buffer to be BUFSIZ/block size. */
-
- if (-1 == fstat(p.fdin, &st)) {
- warn("%s", p.in);
- p.bufsz = BUFSIZ;
- } else
- p.bufsz = MAX(st.st_blksize, BUFSIZ);
-
- p.buf = malloc(p.bufsz);
- if (NULL == p.buf)
- err(1, "malloc");
-
- /* Allocate the parser. */
-
- cb.mdoc_err = msg_err;
- cb.mdoc_warn = msg_warn;
- cb.mdoc_msg = msg_msg;
-
- p.mdoc = mdoc_alloc(&p, &cb);
-
- /* Parse the input file. */
-
- c = parse(&p);
- free(p.buf);
-
- if (STDIN_FILENO != p.fdin && -1 == close(p.fdin))
- warn("%s", p.in);
-
- if (0 == c) {
- mdoc_free(p.mdoc);
- return(EXIT_FAILURE);
- }
-
- /* If the parse succeeded, print it out. */
-
- treeprint(mdoc_node(p.mdoc), mdoc_meta(p.mdoc));
- mdoc_free(p.mdoc);
-
- return(EXIT_SUCCESS);
-}
-
-
-static int
-getsopts(struct md_parse *p, char *arg)
-{
- char *v;
- char *toks[] = { "all", "compat",
- "syntax", "error", NULL };