diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-02-21 15:34:46 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-02-21 15:34:46 +0000 |
commit | 258474969d9b8106ce94ac7945ea0635c4726b95 (patch) | |
tree | 421c11e38e2617c2ba66957f2551deb54ecdcd16 | |
parent | 3f750ebfbcf6d91d72265ebaf5febeeb85e41eaf (diff) | |
download | mandoc-258474969d9b8106ce94ac7945ea0635c4726b95.tar.gz |
A few more macros in place.
-rw-r--r-- | mdocml.1 | 21 | ||||
-rw-r--r-- | mdocml.c | 38 | ||||
-rw-r--r-- | term.c | 44 | ||||
-rw-r--r-- | validate.c | 1 |
4 files changed, 80 insertions, 24 deletions
@@ -9,6 +9,7 @@ .\" SECTION .Sh SYNOPSIS .Nm mdocml +.Op Fl f Ns Ar filter .Op Fl v .Op Fl W Ns Ar err... .Op Ar infile @@ -18,10 +19,20 @@ The .Nm utility interfaces the .Xr mdoc 3 -library to scan, parse and validate mdoc-macro documents. Arguments -follow: +library to scan, parse, validate and output mdoc-macro documents. +Arguments follow: .Bl -tag -width "\-Werr... " .\" ITEM +.It Fl f Ns Ar filter +Pipe the parsed syntax tree into an output filter. May be either +.Ar tree +for the parse tree or +.Ar term +for a terminal-encoded, formatted manual page. +.\" ITEM +.It Fl v +Print verbose parsing output. +.\" ITEM .It Fl W Ns Ar err... Print warning messages. May be set to .Fl W Ns Ar all @@ -37,9 +48,6 @@ termination. Multiple arguments may be comma-separated, such as .Fl W Ns Ar error,all . .\" ITEM -.It Fl v -Print verbose parsing output. -.\" ITEM .It Ar infile Read input from .Ar infile , @@ -58,8 +66,7 @@ manuals. .Pp By default, .Nm -reads from stdin, writes messages to stdout, and writes errors and -warnings to stderr. +reads from stdin and only validates its input. .\" PARAGRAPH .Pp .Ex -std mdocml @@ -32,6 +32,9 @@ #define MD_LINE_SZ (256) /* Max input line size. */ +typedef int (*md_print)(const struct mdoc_node *, + const struct mdoc_meta *); + struct md_parse { int warn; /* Warning flags. */ #define MD_WARN_SYNTAX (1 << 0) /* Show syntax warnings. */ @@ -44,6 +47,7 @@ struct md_parse { u_long bufsz; /* Input buffer size. */ char *in; /* Input file name. */ int fdin; /* Input file desc. */ + md_print fp; }; extern char *__progname; @@ -144,10 +148,25 @@ parse_opts(struct md_parse *p, int argc, char *argv[]) extern char *optarg; extern int optind; + extern int termprint(const struct mdoc_node *, + const struct mdoc_meta *); + extern int treeprint(const struct mdoc_node *, + const struct mdoc_meta *); + p->in = "-"; - while (-1 != (c = getopt(argc, argv, "vW:"))) + while (-1 != (c = getopt(argc, argv, "f:vW:"))) switch (c) { + case ('f'): + if (0 == strcmp(optarg, "tree")) { + p->fp = treeprint; + break; + } else if (0 == strcmp(optarg, "term")) { + p->fp = termprint; + break; + } + warnx("unknown filter: %s", optarg); + return(0); case ('v'): p->dbg++; break; @@ -224,10 +243,7 @@ buf_begin(struct md_parse *p) static int parse_leave(struct md_parse *p, int code) { - extern int termprint(const struct mdoc_node *, - const struct mdoc_meta *); - /*extern int treeprint(const struct mdoc_node *, - const struct mdoc_meta *);*/ + md_print fp; if (NULL == p->mdoc) return(code); @@ -235,11 +251,10 @@ parse_leave(struct md_parse *p, int code) if ( ! mdoc_endparse(p->mdoc)) code = 0; - /* TODO */ - if (code && ! termprint(mdoc_node(p->mdoc), mdoc_meta(p->mdoc))) - code = 0; - /*if (code && ! treeprint(mdoc_node(p->mdoc), mdoc_meta(p->mdoc))) - code = 0;*/ + if (code && (fp = p->fp)) { + if ( ! (*fp)(mdoc_node(p->mdoc), mdoc_meta(p->mdoc))) + code = 0; + } mdoc_free(p->mdoc); return(code); @@ -364,6 +379,7 @@ static void usage(void) { - warnx("usage: %s [-v] [-Wwarn...] [infile]", __progname); + warnx("usage: %s [-ffilter] [-v] [-Wwarn...] [infile]", + __progname); } @@ -91,6 +91,7 @@ decl_pre(termp_ns); decl_pre(termp_op); decl_pre(termp_pp); decl_pre(termp_sh); +decl_pre(termp_xr); decl_post(termp_bl); decl_post(termp_it); @@ -144,7 +145,7 @@ const struct termact termacts[MDOC_MAX] = { { NULL, NULL }, /* St */ { NULL, NULL }, /* Va */ { NULL, NULL }, /* Vt */ - { NULL, NULL }, /* Xr */ + { termp_xr_pre, NULL }, /* Xr */ { NULL, NULL }, /* %A */ { NULL, NULL }, /* %B */ { NULL, NULL }, /* %D */ @@ -682,7 +683,7 @@ termp_op_post(struct termp *p, const struct mdoc_meta *meta, switch (node->type) { case (MDOC_BODY): p->flags |= TERMP_NOSPACE; - word(p, "\\(rB"); + word(p, "\\]"); break; default: break; @@ -715,6 +716,34 @@ termp_sh_post(struct termp *p, const struct mdoc_meta *meta, /* ARGSUSED */ static int +termp_xr_pre(struct termp *p, const struct mdoc_meta *meta, + const struct mdoc_node *node) +{ + const struct mdoc_node *n; + + n = node->child; + assert(n); + + assert(MDOC_TEXT == n->type); + word(p, n->data.text.string); + + if (NULL == (n = n->next)) + return(0); + + assert(MDOC_TEXT == n->type); + p->flags |= TERMP_NOSPACE; + word(p, "\\("); + p->flags |= TERMP_NOSPACE; + word(p, n->data.text.string); + p->flags |= TERMP_NOSPACE; + word(p, "\\)"); + + return(0); +} + + +/* ARGSUSED */ +static int termp_sh_pre(struct termp *p, const struct mdoc_meta *meta, const struct mdoc_node *node) { @@ -742,7 +771,7 @@ termp_op_pre(struct termp *p, const struct mdoc_meta *meta, switch (node->type) { case (MDOC_BODY): - word(p, "\\(lB"); + word(p, "\\["); p->flags |= TERMP_NOSPACE; break; default: @@ -769,19 +798,22 @@ static void termprint_r(struct termp *p, const struct mdoc_meta *meta, const struct mdoc_node *node) { + int dochild; /* Pre-processing ----------------- */ + dochild = 1; + if (MDOC_TEXT != node->type) { if (termacts[node->tok].pre) if ( ! (*termacts[node->tok].pre)(p, meta, node)) - return; + dochild = 0; } else /* MDOC_TEXT == node->type */ word(p, node->data.text.string); /* Children ---------------------- */ - if (NULL == node->child) { + if (dochild && NULL == node->child) { /* No-child processing. */ switch (node->type) { case (MDOC_ELEM): @@ -799,7 +831,7 @@ termprint_r(struct termp *p, const struct mdoc_meta *meta, default: break; } - } else + } else if (dochild) termprint_r(p, meta, node->child); /* Post-processing --------------- */ @@ -709,6 +709,7 @@ pre_it(struct mdoc *mdoc, struct mdoc_node *node) { /* TODO: -width attribute must be specified for -tag. */ + /* TODO: children too big for -width? */ if (MDOC_BLOCK != node->type) return(1); |