diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 190 |
1 files changed, 88 insertions, 102 deletions
@@ -27,12 +27,17 @@ #include <unistd.h> #include "mdoc.h" +#include "man.h" #ifdef __linux__ extern int getsubopt(char **, char * const *, char **); # ifndef __dead # define __dead __attribute__((__noreturn__)) # endif +#elif defined(__FreeBSD__) +# ifndef __dead +# define __dead __dead2 +# endif #endif struct buf { @@ -49,8 +54,13 @@ struct curparse { #define WARN_WERR (1 << 2) /* Warnings->errors. */ }; -enum outt { - OUTT_ASCII, +enum intt { + INTT_MDOC = 0, + INTT_MAN +}; + +enum outt { + OUTT_ASCII = 0, OUTT_LATIN1, OUTT_UTF8, OUTT_TREE, @@ -69,18 +79,22 @@ extern int terminal_run(void *, const struct mdoc *); extern int tree_run(void *, const struct mdoc *); extern void terminal_free(void *); -__dead static void version(void); -__dead static void usage(void); static int foptions(int *, char *); static int toptions(enum outt *, char *); +static int moptions(enum intt *, char *); static int woptions(int *, char *); static int merr(void *, int, int, const char *); static int mwarn(void *, int, int, enum mdoc_warn, const char *); -static int file(struct buf *, struct buf *, - const char *, struct mdoc *); +static int file(struct buf *, struct buf *, + const char *, + struct man *, struct mdoc *); static int fdesc(struct buf *, struct buf *, - const char *, int, struct mdoc *); + const char *, int, + struct man *, struct mdoc *); + +__dead static void version(void); +__dead static void usage(void); int @@ -88,9 +102,11 @@ main(int argc, char *argv[]) { int c, rc, fflags; struct mdoc_cb cb; + struct man *man; struct mdoc *mdoc; void *outdata; enum outt outtype; + enum intt inttype; struct buf ln, blk; out_run outrun; out_free outfree; @@ -98,16 +114,21 @@ main(int argc, char *argv[]) fflags = 0; outtype = OUTT_ASCII; + inttype = INTT_MDOC; bzero(&curp, sizeof(struct curparse)); /* LINTED */ - while (-1 != (c = getopt(argc, argv, "f:VW:T:"))) + while (-1 != (c = getopt(argc, argv, "f:m:VW:T:"))) switch (c) { case ('f'): if ( ! foptions(&fflags, optarg)) return(0); break; + case ('m'): + if ( ! moptions(&inttype, optarg)) + return(0); + break; case ('T'): if ( ! toptions(&outtype, optarg)) return(0); @@ -174,7 +195,17 @@ main(int argc, char *argv[]) bzero(&ln, sizeof(struct buf)); bzero(&blk, sizeof(struct buf)); - mdoc = mdoc_alloc(&curp, fflags, &cb); + man = NULL; + mdoc = NULL; + + switch (inttype) { + case (INTT_MAN): + man = man_alloc(); + break; + default: + mdoc = mdoc_alloc(&curp, fflags, &cb); + break; + } /* * Loop around available files. @@ -182,22 +213,31 @@ main(int argc, char *argv[]) if (NULL == *argv) { curp.file = "<stdin>"; - c = fdesc(&blk, &ln, "stdin", STDIN_FILENO, mdoc); rc = 0; + c = fdesc(&blk, &ln, "stdin", + STDIN_FILENO, man, mdoc); + if (c && NULL == outrun) rc = 1; +#if 0 else if (c && outrun && (*outrun)(outdata, mdoc)) rc = 1; +#endif } else { while (*argv) { curp.file = *argv; - c = file(&blk, &ln, *argv, mdoc); + c = file(&blk, &ln, *argv, man, mdoc); if ( ! c) break; +#if 0 if (outrun && ! (*outrun)(outdata, mdoc)) break; - /* Reset the parser for another file. */ - mdoc_reset(mdoc); +#endif + if (man) + man_reset(man); + if (mdoc) + mdoc_reset(mdoc); + argv++; } rc = NULL == *argv; @@ -209,8 +249,10 @@ main(int argc, char *argv[]) free(ln.buf); if (outfree) (*outfree)(outdata); - - mdoc_free(mdoc); + if (mdoc) + mdoc_free(mdoc); + if (man) + man_free(man); return(rc ? EXIT_SUCCESS : EXIT_FAILURE); } @@ -237,8 +279,8 @@ usage(void) static int -file(struct buf *blk, struct buf *ln, - const char *file, struct mdoc *mdoc) +file(struct buf *blk, struct buf *ln, const char *file, + struct man *man, struct mdoc *mdoc) { int fd, c; @@ -247,7 +289,7 @@ file(struct buf *blk, struct buf *ln, return(0); } - c = fdesc(blk, ln, file, fd, mdoc); + c = fdesc(blk, ln, file, fd, man, mdoc); if (-1 == close(fd)) warn("%s", file); @@ -258,15 +300,15 @@ file(struct buf *blk, struct buf *ln, static int fdesc(struct buf *blk, struct buf *ln, - const char *f, int fd, struct mdoc *mdoc) + const char *f, int fd, + struct man *man, struct mdoc *mdoc) { size_t sz; ssize_t ssz; struct stat st; int j, i, pos, lnn; -#ifdef STRIP_XO - int macro, xo, xeoln; -#endif + + assert( ! (man && mdoc)); /* * Two buffers: ln and buf. buf is the input buffer, optimised @@ -291,9 +333,6 @@ fdesc(struct buf *blk, struct buf *ln, /* * Fill buf with file blocksize and parse newlines into ln. */ -#ifdef STRIP_XO - macro = xo = xeoln = 0; -#endif for (lnn = 1, pos = 0; ; ) { if (-1 == (ssz = read(fd, blk->buf, sz))) { @@ -311,66 +350,6 @@ fdesc(struct buf *blk, struct buf *ln, } if ('\n' != blk->buf[i]) { - /* - * Ugly of uglies. Here we handle the - * dreaded `Xo/Xc' scoping. Cover the - * eyes of any nearby children. This - * makes `Xo/Xc' enclosures look like - * one huge line. - */ -#ifdef STRIP_XO - /* - * First, note whether we're in a macro - * line. - */ - if (0 == pos && '.' == blk->buf[i]) - macro = 1; - - /* - * If we're in an `Xo' context and just - * nixed a newline, remove the control - * character for new macro lines: - * they're going to show up as all part - * of the same line. - */ - if (xo && xeoln && '.' == blk->buf[i]) { - xeoln = 0; - continue; - } - xeoln = 0; - - /* - * If we've parsed `Xo', enter an xo - * context. `Xo' must be in a parsable - * state. This is the ugly part. IT IS - * NOT SMART ENOUGH TO HANDLE ESCAPED - * WHITESPACE. - */ - if (macro && pos && 'o' == blk->buf[i]) { - if (xo && 'X' == ln->buf[pos - 1]) { - if (' ' == ln->buf[pos - 2]) - xo++; - } else if ('X' == ln->buf[pos - 1]) { - if (2 == pos && '.' == ln->buf[pos - 2]) - xo++; - else if (' ' == ln->buf[pos - 2]) - xo++; - } - } - - /* - * If we're parsed `Xc', leave an xo - * context if one's already pending. - * `Xc' must be in a parsable state. - * THIS IS NOT SMART ENOUGH TO HANDLE - * ESCAPED WHITESPACE. - */ - if (macro && pos && 'c' == blk->buf[i]) - if (xo && 'X' == ln->buf[pos - 1]) - if (' ' == ln->buf[pos - 2]) - xo--; -#endif /* STRIP_XO */ - ln->buf[pos++] = blk->buf[i]; continue; } @@ -389,30 +368,37 @@ fdesc(struct buf *blk, struct buf *ln, } } -#ifdef STRIP_XO - /* - * If we're in an xo context, put a space in - * place of the newline and continue parsing. - * Mark that we just did a newline. - */ - if (xo) { - xeoln = 1; - ln->buf[pos++] = ' '; - lnn++; - continue; - } - macro = 0; -#endif /* STRIP_XO */ - ln->buf[pos] = 0; - if ( ! mdoc_parseln(mdoc, lnn, ln->buf)) + if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf)) + return(0); + if (man && ! man_parseln(man, lnn, ln->buf)) return(0); lnn++; pos = 0; } } - return(mdoc_endparse(mdoc)); + if (mdoc) + return(mdoc_endparse(mdoc)); + + return(man_endparse(man)); +} + + +static int +moptions(enum intt *tflags, char *arg) +{ + + if (0 == strcmp(arg, "mdoc")) + *tflags = INTT_MDOC; + else if (0 == strcmp(arg, "man")) + *tflags = INTT_MAN; + else { + warnx("bad argument: -m%s", arg); + return(0); + } + + return(1); } |