From 142d3c43402e6904480a35ff0f970621b9a5f700 Mon Sep 17 00:00:00 2001 From: Kristaps Dzonsons Date: Wed, 10 Dec 2008 14:42:45 +0000 Subject: Made noop the default filter. --- Makefile | 44 ++++----- index.7 | 2 +- libmdocml.c | 31 +++++-- libmdocml.h | 10 +-- mdocml.1 | 37 +++++--- mdocml.c | 6 +- noop.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ private.h | 6 +- 8 files changed, 379 insertions(+), 49 deletions(-) create mode 100644 noop.c diff --git a/Makefile b/Makefile index d44977ae..1c78b441 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,20 @@ -VERSION = 1.0.2 +VERSION = 1.0.3 # FIXME CFLAGS += -W -Wall -Wno-unused-parameter -g -DDEBUG LNS = mdocml.ln html.ln xml.ln libmdocml.ln roff.ln ml.ln mlg.ln \ - compat.ln tokens.ln literals.ln tags.ln + compat.ln tokens.ln literals.ln tags.ln noop.ln LLNS = llib-lmdocml.ln LIBS = libmdocml.a OBJS = mdocml.o html.o xml.o libmdocml.o roff.o ml.o mlg.o \ - compat.o tokens.o literals.o tags.o + compat.o tokens.o literals.o tags.o noop.o SRCS = mdocml.c html.c xml.c libmdocml.c roff.c ml.c mlg.c \ - compat.c tokens.c literals.c tags.c + compat.c tokens.c literals.c tags.c noop.c HEADS = libmdocml.h private.h ml.h roff.h html.h @@ -106,48 +106,52 @@ mdocml-port.tgz: $(INSTALL) ( cd .dist/ && tar zcf ../$@ mdocml/ ) rm -rf .dist/ -llib-lmdocml.ln: mdocml.ln libmdocml.ln html.ln xml.ln roff.ln ml.ln mlg.ln compat.ln tokens.ln literals.ln tags.ln - $(LINT) $(LINTFLAGS) -Cmdocml mdocml.ln libmdocml.ln html.ln xml.ln roff.ln ml.ln mlg.ln compat.ln tokens.ln literals.ln tags.ln +llib-lmdocml.ln: mdocml.ln libmdocml.ln html.ln xml.ln roff.ln ml.ln mlg.ln compat.ln tokens.ln literals.ln tags.ln noop.ln + $(LINT) $(LINTFLAGS) -Cmdocml mdocml.ln libmdocml.ln html.ln xml.ln roff.ln ml.ln mlg.ln compat.ln tokens.ln literals.ln tags.ln noop.ln mdocml.ln: mdocml.c libmdocml.h mdocml.o: mdocml.c libmdocml.h -libmdocml.a: libmdocml.o html.o xml.o roff.o ml.o mlg.o compat.o tokens.o literals.o tags.o - $(AR) rs $@ libmdocml.o html.o xml.o roff.o ml.o mlg.o compat.o tokens.o literals.o tags.o +libmdocml.a: libmdocml.o html.o xml.o roff.o ml.o mlg.o compat.o tokens.o literals.o tags.o noop.o + $(AR) rs $@ libmdocml.o html.o xml.o roff.o ml.o mlg.o compat.o tokens.o literals.o tags.o noop.o -xml.ln: xml.c private.h libmdocml.h ml.h +xml.ln: xml.c ml.h -xml.o: xml.c private.h libmdocml.h ml.h +xml.o: xml.c ml.h -html.ln: html.c private.h libmdocml.h +html.ln: html.c private.h -html.o: html.c private.h libmdocml.h +html.o: html.c private.h tags.ln: tags.c html.h tags.o: tags.c html.h -roff.ln: roff.c private.h roff.h libmdocml.h +roff.ln: roff.c private.h -roff.o: roff.c private.h roff.h libmdocml.h +roff.o: roff.c private.h -libmdocml.ln: libmdocml.c private.h libmdocml.h +libmdocml.ln: libmdocml.c private.h -libmdocml.o: libmdocml.c private.h libmdocml.h +libmdocml.o: libmdocml.c private.h -ml.ln: ml.c private.h libmdocml.h ml.h +ml.ln: ml.c ml.h -ml.o: ml.c private.h libmdocml.h ml.h +ml.o: ml.c ml.h -mlg.ln: mlg.c private.h libmdocml.h ml.h +mlg.ln: mlg.c ml.h -mlg.o: mlg.c private.h libmdocml.h ml.h +mlg.o: mlg.c ml.h compat.ln: compat.c compat.o: compat.c +noop.ln: noop.c private.h + +noop.o: noop.c private.h + html.h: ml.h ml.h: private.h diff --git a/index.7 b/index.7 index 718b1e25..fb5b7341 100644 --- a/index.7 +++ b/index.7 @@ -107,7 +107,7 @@ Download at http://mdocml.bsd.lv/mdocml.tgz .Ns . .\" - UPDATE ME WITH EVERY RELEASE. ---------------------------------- -The current version is 1.0.2, dated 10/12/2008. +The current version is 1.0.3, dated 10/12/2008. .\" ------------------------------------------------------------------ .Pp Previous versions are archived as mdocml-x.y.z.tgz, with the appropriate diff --git a/libmdocml.c b/libmdocml.c index 965a65e8..1a7f7c55 100644 --- a/libmdocml.c +++ b/libmdocml.c @@ -1,4 +1,4 @@ -/* $Id: libmdocml.c,v 1.20 2008/12/09 17:09:12 kristaps Exp $ */ +/* $Id: libmdocml.c,v 1.21 2008/12/10 14:42:46 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -130,6 +130,8 @@ static int md_run_leave(const struct md_args *args, struct md_mbuf *mbuf, struct md_rbuf *rbuf, int c, void *data) { + md_exit fp; + assert(args); assert(mbuf); assert(rbuf); @@ -137,15 +139,19 @@ md_run_leave(const struct md_args *args, struct md_mbuf *mbuf, /* Run exiters. */ switch (args->type) { case (MD_HTML): - if ( ! md_exit_html(data, -1 == c ? 0 : 1)) - c = -1; + fp = md_exit_html; + break; + case (MD_XML): + fp = md_exit_xml; break; default: - if ( ! md_exit_xml(data, -1 == c ? 0 : 1)) - c = -1; + fp = md_exit_noop; break; } + if ( ! (*fp)(data, -1 == c ? 0 : 1)) + c = -1; + /* Make final flush of buffer. */ if (-1 != c && ! md_buf_flush(mbuf)) return(-1); @@ -172,9 +178,12 @@ md_run_enter(const struct md_args *args, struct md_mbuf *mbuf, case (MD_HTML): fp = md_line_html; break; - default: + case (MD_XML): fp = md_line_xml; break; + default: + fp = md_line_noop; + break; } pos = 0; @@ -217,6 +226,7 @@ int md_run(const struct md_args *args, const struct md_buf *out, const struct md_buf *in) { + md_init fp; struct md_mbuf mbuf; struct md_rbuf rbuf; void *data; @@ -234,13 +244,18 @@ md_run(const struct md_args *args, /* Run initialisers. */ switch (args->type) { case (MD_HTML): - data = md_init_html(args, &mbuf, &rbuf); + fp = md_init_html; + break; + case (MD_XML): + fp = md_init_xml; break; default: - data = md_init_xml(args, &mbuf, &rbuf); + fp = md_init_noop; break; } + data = (*fp)(args, &mbuf, &rbuf); + /* Go into mainline. */ return(md_run_enter(args, &mbuf, &rbuf, data)); } diff --git a/libmdocml.h b/libmdocml.h index 12839fb6..55ce8edb 100644 --- a/libmdocml.h +++ b/libmdocml.h @@ -1,4 +1,4 @@ -/* $Id: libmdocml.h,v 1.13 2008/12/09 00:27:17 kristaps Exp $ */ +/* $Id: libmdocml.h,v 1.14 2008/12/10 14:42:46 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -21,10 +21,6 @@ #include -struct md_params_xml { - int dummy; -}; - struct md_params_html { char *css; int flags; @@ -32,13 +28,13 @@ struct md_params_html { }; union md_params { - struct md_params_xml xml; struct md_params_html html; }; enum md_type { MD_XML, /* XML. */ - MD_HTML /* HTML4.01-strict. */ + MD_HTML, /* HTML4.01-strict. */ + MD_NOOP /* Validates only. */ }; struct md_args { diff --git a/mdocml.1 b/mdocml.1 index 176faf5b..6493e411 100644 --- a/mdocml.1 +++ b/mdocml.1 @@ -20,21 +20,24 @@ The .Nm utility parses mdoc formatted manual source and passes results into an output filter. The current output filters are -.Ar html +.Fl f Ar html and -.Ar xml , -the default. Arguments common to all filters follow: +.Fl f Ar xml . +By default, +.Nm +only validates its input. This may be explicitly directed with +.Fl f Ar noop . +Arguments common to all filters follow: .Bl -tag -width "\-o outfile" .It Fl f Ar filter -The output filter name. This -.Em must -be declared before any other options. +The output filter name. .It Fl o Ar outfile Write output to .Ar outfile , which may be .Dq \- -for stdout. +for stdout. This is meaningless for +.Fl f Ar noop . .It Fl W Ns Ar err... Print warning messages. If set to .Fl W Ns Ar all , @@ -56,14 +59,21 @@ for stdin. .Pp By default, .Nm -reads from stdin and writes to stdout using the xml filter. +reads from stdin and writes to stdout. .Pp .Ex -std mdocml .\" +.Ss Noop Filter +The default noop +.Dq validating +filter simply validates its input; it produces no output beyond error +and warning messages. +.\" .Ss XML Filter The XML filter, specified by .Fl f Ar xml , -is the default filter. This filter has no additional arguments. +produces correctly-formatted XML output. This filter has no additional +arguments. .Pp The XML filter creates an XML document where element names are their respective roff macro names. Each element name has an associated @@ -88,6 +98,7 @@ namespace is for block bodies; and the .Dq inline namespace is for in-line elements (such as .Sq .Em ) . +.\" .Ss HTML Filter The HTML filter, specified by .Fl f Ar html , @@ -99,6 +110,10 @@ The CSS file location, which defaults to .It Fl e Whether to embed the CSS file into the HTML prologue. .El +.Pp +By default, the HTML filter produces HTML-4.01 strict mark-up. The +default CSS document styles the page as it would appear in a terminal +window. .\" .Sh EXAMPLES To produce an HTML4-strict document @@ -118,7 +133,9 @@ with the default namespace identifiers and .Li inline : .Pp -.D1 % mdocml mdocml.1 +.D1 % mdocml -Wall,error -fxml mdocml.1 +.Pp +The previous example will also halt on input document warnings. .\" .Sh SEE ALSO .Xr groff 1 , diff --git a/mdocml.c b/mdocml.c index d255d07e..3198b4b6 100644 --- a/mdocml.c +++ b/mdocml.c @@ -1,4 +1,4 @@ -/* $Id: mdocml.c,v 1.19 2008/12/09 17:09:12 kristaps Exp $ */ +/* $Id: mdocml.c,v 1.20 2008/12/10 14:42:46 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -71,7 +71,7 @@ main(int argc, char *argv[]) (void)memset(&args, 0, sizeof(struct md_args)); - args.type = MD_XML; + args.type = MD_NOOP; while (-1 != (c = getopt(argc, argv, "c:ef:o:vW:"))) switch (c) { @@ -90,6 +90,8 @@ main(int argc, char *argv[]) args.type = MD_HTML; else if (0 == strcmp(optarg, "xml")) args.type = MD_XML; + else if (0 == strcmp(optarg, "noop")) + args.type = MD_NOOP; else errx(1, "invalid filter type"); break; diff --git a/noop.c b/noop.c new file mode 100644 index 00000000..9214ddd4 --- /dev/null +++ b/noop.c @@ -0,0 +1,292 @@ +/* $Id: noop.c,v 1.1 2008/12/10 14:42:46 kristaps Exp $ */ +/* + * Copyright (c) 2008 Kristaps Dzonsons + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the + * above copyright notice and this permission notice appear in all + * copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ +#include +#include +#include + +#include "private.h" + + +struct md_noop { + const struct md_args *args; + const struct md_rbuf *rbuf; + struct rofftree *tree; +}; + + +static int noop_roffmsg(void *arg, + enum roffmsg, const char *, + const char *, const char *); +static int noop_roffhead(void *, const struct tm *, + const char *, const char *, + enum roffmsec, enum roffvol); +static int noop_rofftail(void *, const struct tm *, + const char *, const char *, + enum roffmsec, enum roffvol); +static int noop_roffin(void *, int, + int *, const char **); +static int noop_roffdata(void *, int, + const char *, const char *); +static int noop_roffout(void *, int); +static int noop_roffblkin(void *, int, int *, + const char **); +static int noop_roffblkout(void *, int); +static int noop_roffspecial(void *, int, + const char *, const int *, + const char **, const char **); +static int noop_roffblkheadin(void *, int, + int *, const char **); +static int noop_roffblkheadout(void *, int); +static int noop_roffblkbodyin(void *, int, + int *, const char **); +static int noop_roffblkbodyout(void *, int); + +#ifdef __linux__ +extern size_t strlcat(char *, const char *, size_t); +extern size_t strlcpy(char *, const char *, size_t); +#endif + + +int +md_exit_noop(void *data, int flush) +{ + struct md_noop *noop; + int c; + + noop = (struct md_noop *)data; + c = roff_free(noop->tree, flush); + free(noop); + return(c); +} + + +int +md_line_noop(void *data, char *buf) +{ + struct md_noop *noop; + + noop = (struct md_noop *)data; + return(roff_engine(noop->tree, buf)); +} + + +void * +md_init_noop(const struct md_args *args, + struct md_mbuf *mbuf, const struct md_rbuf *rbuf) +{ + struct roffcb cb; + struct md_noop *noop; + + if (NULL == (noop = calloc(1, sizeof(struct md_noop)))) + err(1, "calloc"); + + noop->args = args; + noop->rbuf = rbuf; + + cb.roffhead = noop_roffhead; + cb.rofftail = noop_rofftail; + cb.roffin = noop_roffin; + cb.roffout = noop_roffout; + cb.roffblkin = noop_roffblkin; + cb.roffblkheadin = noop_roffblkheadin; + cb.roffblkheadout = noop_roffblkheadout; + cb.roffblkbodyin = noop_roffblkbodyin; + cb.roffblkbodyout = noop_roffblkbodyout; + cb.roffblkout = noop_roffblkout; + cb.roffspecial = noop_roffspecial; + cb.roffmsg = noop_roffmsg; + cb.roffdata = noop_roffdata; + + if (NULL == (noop->tree = roff_alloc(&cb, noop))) { + free(noop); + return(NULL); + } + return(noop); +} + + +/* ARGSUSED */ +static int +noop_roffhead(void *arg, const struct tm *tm, const char *os, + const char *title, enum roffmsec sec, enum roffvol vol) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_rofftail(void *arg, const struct tm *tm, const char *os, + const char *title, enum roffmsec sec, enum roffvol vol) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffspecial(void *arg, int tok, const char *start, + const int *argc, const char **argv, const char **more) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffblkin(void *arg, int tok, + int *argc, const char **argv) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffblkout(void *arg, int tok) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffblkbodyin(void *arg, int tok, + int *argc, const char **argv) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffblkbodyout(void *arg, int tok) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffblkheadin(void *arg, int tok, + int *argc, const char **argv) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffblkheadout(void *arg, int tok) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffin(void *arg, int tok, int *argc, const char **argv) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffout(void *arg, int tok) +{ + + return(1); +} + + +/* ARGSUSED */ +static int +noop_roffdata(void *arg, int tok, + const char *start, const char *buf) +{ + + return(1); +} + + +static int +noop_roffmsg(void *arg, enum roffmsg lvl, + const char *buf, const char *pos, const char *msg) +{ + struct md_noop *p; + char *level; + char b[256]; + int i; + + p = (struct md_noop *)arg; + assert(p); + + switch (lvl) { + case (ROFF_WARN): + level = "warning"; + if ( ! (MD_WARN_ALL & p->args->warnings)) + return(1); + break; + case (ROFF_ERROR): + level = "error"; + break; + default: + abort(); + /* NOTREACHED */ + } + + if (pos) { + assert(pos >= buf); + if (0 < p->args->verbosity) { + (void)snprintf(b, sizeof(b), + "%s:%zu: %s: %s\n", + p->rbuf->name, p->rbuf->line, + level, msg); + (void)strlcat(b, "Error at: ", sizeof(b)); + (void)strlcat(b, p->rbuf->linebuf, sizeof(b)); + + (void)strlcat(b, "\n ", sizeof(b)); + for (i = 0; i < pos - buf; i++) + (void)strlcat(b, " ", sizeof(b)); + (void)strlcat(b, "^", sizeof(b)); + + } else + (void)snprintf(b, sizeof(b), + "%s:%zu: %s: %s (col %zu)", + p->rbuf->name, p->rbuf->line, + level, msg, pos - buf); + } else + (void)snprintf(b, sizeof(b), "%s: %s: %s", + p->rbuf->name, level, msg); + + (void)fprintf(stderr, "%s\n", b); + return(lvl == ROFF_WARN ? 1 : 0); +} + diff --git a/private.h b/private.h index a8c16208..05281758 100644 --- a/private.h +++ b/private.h @@ -1,4 +1,4 @@ -/* $Id: private.h,v 1.39 2008/12/10 13:15:55 kristaps Exp $ */ +/* $Id: private.h,v 1.40 2008/12/10 14:42:46 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -376,6 +376,10 @@ void *md_init_html(const struct md_args *, struct md_mbuf *, const struct md_rbuf *); int md_line_html(void *, char *); int md_exit_html(void *, int); +void *md_init_noop(const struct md_args *, + struct md_mbuf *, const struct md_rbuf *); +int md_line_noop(void *, char *); +int md_exit_noop(void *, int); void *md_init_xml(const struct md_args *, struct md_mbuf *, const struct md_rbuf *); int md_line_xml(void *, char *); -- cgit v1.2.3-56-ge451