aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2009-01-17 14:04:25 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2009-01-17 14:04:25 +0000
commit3ef2ed38138f74f57e8bac7be877cd7f644c6fa5 (patch)
tree41a91c061344080a3c65e68a0c7679e5c511b4d8
parent9b91d2941b32c93371d933a186b43a00cb02c3c0 (diff)
downloadmandoc-3ef2ed38138f74f57e8bac7be877cd7f644c6fa5.tar.gz
mandoc-3ef2ed38138f74f57e8bac7be877cd7f644c6fa5.tar.zst
mandoc-3ef2ed38138f74f57e8bac7be877cd7f644c6fa5.zip
Separating output filters.
-rw-r--r--Makefile18
-rw-r--r--mdoc.h5
-rw-r--r--mdocml.c44
-rw-r--r--private.h4
-rw-r--r--tree.c48
5 files changed, 84 insertions, 35 deletions
diff --git a/Makefile b/Makefile
index e95a785a..b0fcf0a3 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/mdoc.h b/mdoc.h
index cac9fee9..d4db03a8 100644
--- a/mdoc.h
+++ b/mdoc.h
@@ -1,4 +1,4 @@
-/* $Id: mdoc.h,v 1.23 2009/01/16 12:23:25 kristaps Exp $ */
+/* $Id: mdoc.h,v 1.24 2009/01/17 14:04:25 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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*/
diff --git a/mdocml.c b/mdocml.c
index 26aa81ea..6168a898 100644
--- a/mdocml.c
+++ b/mdocml.c
@@ -1,4 +1,4 @@
-/* $Id: mdocml.c,v 1.47 2009/01/16 15:58:50 kristaps Exp $ */
+/* $Id: mdocml.c,v 1.48 2009/01/17 14:04:25 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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);
}
diff --git a/private.h b/private.h
index a46a5612..59601605 100644
--- a/private.h
+++ b/private.h
@@ -1,4 +1,4 @@
-/* $Id: private.h,v 1.65 2009/01/16 12:23:25 kristaps Exp $ */
+/* $Id: private.h,v 1.66 2009/01/17 14:04:25 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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 *);
diff --git a/tree.c b/tree.c
index 484b9bf2..5403fc41 100644
--- a/tree.c
+++ b/tree.c
@@ -1,4 +1,4 @@
-/* $Id: tree.c,v 1.1 2009/01/16 15:58:50 kristaps Exp $ */
+/* $Id: tree.c,v 1.2 2009/01/17 14:04:25 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -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);
}