diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-01-17 14:04:25 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-01-17 14:04:25 +0000 |
commit | 56f0b4a0fb2fb74f3bf1c6f8ed87ce2b716bcf52 (patch) | |
tree | 336c6f8871c8ab7b939a25b3e18e651fabbaf704 | |
parent | 50c7e68a675c2b7d573e94e93564b37785d2cad6 (diff) | |
download | mandoc-56f0b4a0fb2fb74f3bf1c6f8ed87ce2b716bcf52.tar.gz |
Separating output filters.
-rw-r--r-- | Makefile | 18 | ||||
-rw-r--r-- | mdoc.h | 3 | ||||
-rw-r--r-- | mdocml.c | 42 | ||||
-rw-r--r-- | private.h | 2 | ||||
-rw-r--r-- | tree.c | 46 |
5 files changed, 80 insertions, 31 deletions
@@ -2,15 +2,15 @@ VERSION = 1.1.0 CFLAGS += -W -Wall -Wno-unused-parameter -g -LNS = macro.ln mdoc.ln mdocml.ln hash.ln strings.ln xstd.ln argv.ln validate.ln action.ln +LNS = macro.ln mdoc.ln mdocml.ln hash.ln strings.ln xstd.ln argv.ln validate.ln action.ln tree.ln LLNS = llib-llibmdoc.ln llib-lmdocml.ln LIBS = libmdoc.a -OBJS = macro.o mdoc.o mdocml.o hash.o strings.o xstd.o argv.o validate.o action.o +OBJS = macro.o mdoc.o mdocml.o hash.o strings.o xstd.o argv.o validate.o action.o tree.o -SRCS = macro.c mdoc.c mdocml.c hash.c strings.c xstd.c argv.c validate.c action.c +SRCS = macro.c mdoc.c mdocml.c hash.c strings.c xstd.c argv.c validate.c action.c tree.c HEADS = mdoc.h @@ -22,8 +22,8 @@ all: $(BINS) lint: $(LLNS) -mdocml: mdocml.o libmdoc.a - $(CC) $(CFLAGS) -o $@ mdocml.o libmdoc.a +mdocml: mdocml.o tree.o libmdoc.a + $(CC) $(CFLAGS) -o $@ mdocml.o tree.o libmdoc.a clean: rm -f $(CLEAN) @@ -31,8 +31,8 @@ clean: llib-llibmdoc.ln: macro.ln mdoc.ln hash.ln strings.ln xstd.ln argv.ln validate.ln action.ln $(LINT) $(LINTFLAGS) -Clibmdoc mdoc.ln macro.ln hash.ln strings.ln xstd.ln argv.ln validate.ln action.ln -llib-lmdocml.ln: mdocml.ln llib-llibmdoc.ln - $(LINT) $(LINTFLAGS) -Cmdocml mdocml.ln llib-llibmdoc.ln +llib-lmdocml.ln: mdocml.ln tree.ln llib-llibmdoc.ln + $(LINT) $(LINTFLAGS) -Cmdocml mdocml.ln tree.ln llib-llibmdoc.ln macro.ln: macro.c private.h @@ -42,6 +42,10 @@ strings.ln: strings.c private.h strings.o: strings.c private.h +tree.ln: tree.c mdoc.h + +tree.o: tree.c mdoc.h + hash.ln: hash.c private.h hash.o: hash.c private.h @@ -420,9 +420,6 @@ const struct mdoc_node *mdoc_result(struct mdoc *); /* Signal end of parse sequence (boolean retval). */ int mdoc_endparse(struct mdoc *); -/* Node type to static string. */ -char *mdoc_type2a(enum mdoc_type); - __END_DECLS #endif /*!MDOC_H*/ @@ -31,11 +31,25 @@ #include "mdoc.h" #define xfprintf (void)fprintf -#define xprintf (void)printf -#define xvfprintf (void)fvprintf #define MD_LINE_SZ (256) /* Max input line size. */ +/* + * Put this into a mdoctrans.h, which has: + * + * struct mdoc_trans; (opaque) + * + * struct mdoc_trans *mdoc_trans_alloc(const char *filter); + * + * mdoc_trans_free(struct mdoc_trans *); + * + * int mdoc_trans_getopt(struct mdoc_trans *, char *); + * + * int mdoc_trans_print(struct mdoc_trans *, const struct mdoc_node *); + */ + +typedef int (*mdocprint)(const struct mdoc_node *); + struct md_parse { int warn; /* Warning flags. */ @@ -49,12 +63,12 @@ struct md_parse { u_long bufsz; /* Input buffer size. */ char *name; /* Input file name. */ int fd; /* Input file desc. */ - int (*fp)(const struct mdoc_node *, const char *); + mdocprint print; /* Node-print function. */ }; extern char *__progname; -extern int +extern int treeprint(const struct mdoc_node *); static void usage(void); @@ -79,7 +93,7 @@ main(int argc, char *argv[]) { int c; struct md_parse parser; - char *opts, *v, *filter, *output; + char *opts, *v, *filter; #define ALL 0 #define COMPAT 1 #define SYNTAX 2 @@ -90,18 +104,15 @@ main(int argc, char *argv[]) extern char *optarg; extern int optind; - output = filter = NULL; + filter = NULL; (void)memset(&parser, 0, sizeof(struct md_parse)); - while (-1 != (c = getopt(argc, argv, "f:vW:o:"))) + while (-1 != (c = getopt(argc, argv, "f:vW:"))) switch (c) { case ('f'): filter = optarg; break; - case ('o'): - output = optarg; - break; case ('v'): parser.dbg++; break; @@ -138,6 +149,11 @@ main(int argc, char *argv[]) if (1 == argc) parser.name = *argv++; + if (filter) { + if (0 == strcmp(filter, "tree")) + parser.print = treeprint; + } + if ( ! io_begin(&parser)) return(EXIT_FAILURE); @@ -216,8 +232,8 @@ parse_leave(struct md_parse *p, int code) if ( ! mdoc_endparse(p->mdoc)) code = 0; - if (p->fp && (n = mdoc_result(p->mdoc))) - (*p->fp)(n, NULL); + if (p->print && (n = mdoc_result(p->mdoc))) + (*p->print)(n); mdoc_free(p->mdoc); @@ -336,6 +352,6 @@ usage(void) { xfprintf(stderr, "usage: %s [-v] [-Wwarn...] [-ffilter] " - "[-o outfile] [infile]\n", __progname); + "[infile]\n", __progname); } @@ -129,6 +129,8 @@ enum mdoc_arch mdoc_atoarch(const char *); enum mdoc_att mdoc_atoatt(const char *); time_t mdoc_atotime(const char *); +char *mdoc_type2a(enum mdoc_type); + int mdoc_valid_pre(struct mdoc *, struct mdoc_node *); int mdoc_valid_post(struct mdoc *); int mdoc_action_pre(struct mdoc *, struct mdoc_node *); @@ -17,14 +17,16 @@ * PERFORMANCE OF THIS SOFTWARE. */ #include <stdlib.h> +#include <stdio.h> #include "mdoc.h" +#define xprintf (void)printf + +static void treeprint_r(const struct mdoc_node *, int); -#if 0 -/* TODO: remove this to a print-tree output filter. */ static void -print_node(const struct mdoc_node *n, int indent) +treeprint_r(const struct mdoc_node *n, int indent) { const char *p, *t; int i, j; @@ -36,7 +38,32 @@ print_node(const struct mdoc_node *n, int indent) argc = sz = 0; params = NULL; - t = mdoc_type2a(n->type); + switch (n->type) { + case (MDOC_ROOT): + t = "root"; + break; + case (MDOC_BLOCK): + t = "block"; + break; + case (MDOC_HEAD): + t = "block-head"; + break; + case (MDOC_BODY): + t = "block-body"; + break; + case (MDOC_TAIL): + t = "block-tail"; + break; + case (MDOC_ELEM): + t = "elem"; + break; + case (MDOC_TEXT): + t = "text"; + break; + default: + abort(); + /* NOTREACHED */ + } switch (n->type) { case (MDOC_TEXT): @@ -89,13 +116,16 @@ print_node(const struct mdoc_node *n, int indent) xprintf(" %d:%d\n", n->line, n->pos); if (n->child) - print_node(n->child, indent + 1); + treeprint_r(n->child, indent + 1); if (n->next) - print_node(n->next, indent); + treeprint_r(n->next, indent); } -#endif + int -treeprint(const struct mdoc_node *node, const char *out) +treeprint(const struct mdoc_node *node) { + + treeprint_r(node, 0); + return(1); } |