diff options
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | html.c | 99 | ||||
-rw-r--r-- | ml.c | 14 | ||||
-rw-r--r-- | ml.h | 26 | ||||
-rw-r--r-- | mlg.c | 51 | ||||
-rw-r--r-- | xml.c | 165 |
6 files changed, 205 insertions, 160 deletions
@@ -37,8 +37,14 @@ lint: llib-lmdocml.ln dist: mdocml.tgz regress: mdocml - @for f in $(FAIL); do ./mdocml $$f 1>/dev/null 2>/dev/null || continue ; done - @for f in $(SUCCEED); do ./mdocml $$f 1>/dev/null || exit 1 ; done + @for f in $(FAIL); do \ + echo "./mdocml $$f" ; \ + ./mdocml $$f 1>/dev/null 2>/dev/null || continue ; \ + done + @for f in $(SUCCEED); do \ + echo "./mdocml $$f" ; \ + ./mdocml $$f 1>/dev/null || exit 1 ; \ + done mdocml: mdocml.o libmdocml.a $(CC) $(CFLAGS) -o $@ mdocml.o libmdocml.a @@ -33,7 +33,6 @@ /* TODO: allow head/tail-less invocations (just "div" start). */ -/* FIXME: free htmlq. */ struct htmlnode { int tok; @@ -49,11 +48,10 @@ struct htmlq { }; -static void htmlnode_free(struct htmlnode *); -static void htmlnode_free(struct htmlnode *); - static int html_loadcss(struct md_mbuf *, const char *); +static int html_alloc(void **); +static void html_free(void *); static ssize_t html_endtag(struct md_mbuf *, void *, const struct md_args *, enum md_ns, int); @@ -210,13 +208,8 @@ html_begin(struct md_mbuf *mbuf, const struct md_args *args, static int html_end(struct md_mbuf *mbuf, const struct md_args *args) { - size_t res; - - res = 0; - if ( ! ml_puts(mbuf, "</div></body>\n</html>", &res)) - return(0); - return(1); + return(ml_puts(mbuf, "</div></body>\n</html>", NULL)); } @@ -230,8 +223,6 @@ html_blockbodytagname(struct md_mbuf *mbuf, } - - /* ARGSUSED */ static int html_blockheadtagname(struct md_mbuf *mbuf, @@ -252,11 +243,11 @@ html_blocktagname(struct md_mbuf *mbuf, } +/* ARGSUSED */ static int html_printargs(struct md_mbuf *mbuf, int tok, const char *ns, const int *argc, const char **argv, size_t *res) { - int i, c; if ( ! ml_puts(mbuf, " class=\"", res)) return(0); @@ -266,35 +257,7 @@ html_printargs(struct md_mbuf *mbuf, int tok, const char *ns, return(0); if ( ! ml_puts(mbuf, toknames[tok], res)) return(0); - if ( ! ml_puts(mbuf, "\"", res)) - return(0); - - if (NULL == argv || NULL == argc) - return(1); - assert(argv && argc); - - /* FIXME: ignores values. */ - - for (i = 0; ROFF_ARGMAX != (c = argc[i]); i++) { - if (argv[i]) - continue; - if ( ! ml_puts(mbuf, " class=\"", res)) - return(0); - if ( ! ml_puts(mbuf, ns, res)) - return(0); - if ( ! ml_puts(mbuf, "-", res)) - return(0); - if ( ! ml_puts(mbuf, toknames[tok], res)) - return(0); - if ( ! ml_puts(mbuf, "-", res)) - return(0); - if ( ! ml_puts(mbuf, tokargnames[c], res)) - return(0); - if ( ! ml_puts(mbuf, "\"", res)) - return(0); - } - - return(1); + return(ml_puts(mbuf, "\"", res)); } @@ -352,9 +315,10 @@ html_inlinetagname(struct md_mbuf *mbuf, case (ROFF_Pp): return(ml_puts(mbuf, "div", res)); default: - return(ml_puts(mbuf, "span", res)); + break; } - return(1); + + return(ml_puts(mbuf, "span", res)); } @@ -455,12 +419,42 @@ html_endtag(struct md_mbuf *mbuf, void *data, node = q->last; q->last = node->parent; - htmlnode_free(node); + free(node); return((ssize_t)res); } +static int +html_alloc(void **p) +{ + + if (NULL == (*p = calloc(1, sizeof(struct htmlq)))) { + warn("calloc"); + return(0); + } + return(1); +} + + +static void +html_free(void *p) +{ + struct htmlq *q; + struct htmlnode *n; + + assert(p); + q = (struct htmlq *)p; + + while ((n = q->last)) { + q->last = n->parent; + free(n); + } + + free(q); +} + + int md_line_html(void *data, char *buf) { @@ -481,14 +475,15 @@ void * md_init_html(const struct md_args *args, struct md_mbuf *mbuf, const struct md_rbuf *rbuf) { - struct htmlq *q; + struct ml_cbs cbs; - if (NULL == (q = calloc(1, sizeof(struct htmlq)))) { - warn("calloc"); - return(NULL); - } + cbs.ml_alloc = html_alloc; + cbs.ml_free = html_free; + cbs.ml_begintag = html_begintag; + cbs.ml_endtag = html_endtag; + cbs.ml_begin = html_begin; + cbs.ml_end = html_end; - return(mlg_alloc(args, q, rbuf, mbuf, html_begintag, - html_endtag, html_begin, html_end)); + return(mlg_alloc(args, rbuf, mbuf, &cbs)); } @@ -51,10 +51,8 @@ ml_nputstring(struct md_mbuf *p, /* Escaped value. */ case ('\\'): - if (-1 == (v = rofftok_scan(buf, &i))) { - /* TODO: error. */ + if (-1 == (v = rofftok_scan(buf, &i))) return(0); - } switch (v) { case (ROFFTok_Sp_A): @@ -178,8 +176,7 @@ ml_nputstring(struct md_mbuf *p, ssz = 0; break; default: - /* TODO: print error. */ - return(-1); + return(0); } break; @@ -230,7 +227,8 @@ ml_nputs(struct md_mbuf *p, const char *buf, size_t sz, size_t *pos) if ( ! md_buf_puts(p, buf, sz)) return(0); - *pos += sz; + if (pos) + *pos += sz; return(1); } @@ -245,7 +243,9 @@ ml_puts(struct md_mbuf *p, const char *buf, size_t *pos) if ( ! md_buf_puts(p, buf, sz)) return(0); - *pos += sz; + + if (pos) + *pos += sz; return(1); } @@ -29,16 +29,24 @@ enum md_ns { MD_NS_DEFAULT, }; -typedef int (*ml_begin)(struct md_mbuf *, const struct md_args *, - const struct tm *, const char *, const char *, +struct ml_cbs { + int (*ml_begin)(struct md_mbuf *, + const struct md_args *, + const struct tm *, + const char *, const char *, const char *, const char *); -typedef int (*ml_end)(struct md_mbuf *, + int (*ml_end)(struct md_mbuf *, const struct md_args *); -typedef ssize_t (*ml_endtag)(struct md_mbuf *, void *, - const struct md_args *, enum md_ns, int); -typedef ssize_t (*ml_begintag)(struct md_mbuf *, void *, - const struct md_args *, enum md_ns, int, + ssize_t (*ml_endtag)(struct md_mbuf *, + void *, const struct md_args *, + enum md_ns, int); + ssize_t (*ml_begintag)(struct md_mbuf *, + void *, const struct md_args *, + enum md_ns, int, const int *, const char **); + int (*ml_alloc)(void **); + void (*ml_free)(void *); +}; __BEGIN_DECLS @@ -52,9 +60,9 @@ int ml_puts(struct md_mbuf *, const char *, size_t *); int ml_putchars(struct md_mbuf *, char, size_t, size_t *); -struct md_mlg *mlg_alloc(const struct md_args *, void *, +struct md_mlg *mlg_alloc(const struct md_args *, const struct md_rbuf *, struct md_mbuf *, - ml_begintag, ml_endtag, ml_begin, ml_end); + const struct ml_cbs *); int mlg_exit(struct md_mlg *, int); int mlg_line(struct md_mlg *, char *); @@ -51,10 +51,7 @@ struct md_mlg { size_t pos; enum md_tok last; void *arg; - ml_begintag begintag; - ml_endtag endtag; - ml_begin begin; - ml_end end; + struct ml_cbs cbs; int flags; #define ML_OVERRIDE_ONE (1 << 0) #define ML_OVERRIDE_ALL (1 << 1) @@ -145,7 +142,7 @@ mlg_begintag(struct md_mlg *p, enum md_ns ns, int tok, if ( ! ml_nputs(p->mbuf, "<", 1, &p->pos)) return(0); - res = (*p->begintag)(p->mbuf, p->data, p->args, ns, tok, + res = (*p->cbs.ml_begintag)(p->mbuf, p->data, p->args, ns, tok, argc, (const char **)argv); if (-1 == res) return(0); @@ -194,7 +191,7 @@ mlg_endtag(struct md_mlg *p, enum md_ns ns, int tok) if ( ! ml_nputs(p->mbuf, "</", 2, &p->pos)) return(0); - res = (*p->endtag)(p->mbuf, p->data, p->args, ns, tok); + res = (*p->cbs.ml_endtag)(p->mbuf, p->data, p->args, ns, tok); if (-1 == res) return(0); @@ -234,12 +231,9 @@ mlg_indent(struct md_mlg *p) static int mlg_newline(struct md_mlg *p) { - size_t dummy; - if ( ! ml_nputs(p->mbuf, "\n", 1, &dummy)) - return(0); p->pos = 0; - return(1); + return(ml_nputs(p->mbuf, "\n", 1, NULL)); } @@ -329,16 +323,18 @@ mlg_exit(struct md_mlg *p, int flush) c = roff_free(p->tree, flush); free(p); + + (*p->cbs.ml_free)(p->data); + return(c); } struct md_mlg * -mlg_alloc(const struct md_args *args, void *data, +mlg_alloc(const struct md_args *args, const struct md_rbuf *rbuf, struct md_mbuf *mbuf, - ml_begintag begintag, ml_endtag endtag, - ml_begin begin, ml_end end) + const struct ml_cbs *cbs) { struct roffcb cb; struct md_mlg *p; @@ -363,18 +359,17 @@ mlg_alloc(const struct md_args *args, void *data, p->args = args; p->mbuf = mbuf; p->rbuf = rbuf; - p->begintag = begintag; - p->endtag = endtag; - p->begin = begin; - p->end = end; - p->data = data; - if (NULL == (p->tree = roff_alloc(&cb, p))) { + (void)memcpy(&p->cbs, cbs, sizeof(struct ml_cbs)); + + if (NULL == (p->tree = roff_alloc(&cb, p))) free(p); - return(NULL); - } + else if ( ! (*p->cbs.ml_alloc)(&p->data)) + free(p); + else + return(p); - return(p); + return(NULL); } @@ -388,7 +383,8 @@ mlg_roffhead(void *arg, const struct tm *tm, const char *os, p = (struct md_mlg *)arg; mlg_mode(p, MD_BLK_IN); - if ( ! (*p->begin)(p->mbuf, p->args, tm, os, title, sec, vol)) + + if ( ! (*p->cbs.ml_begin)(p->mbuf, p->args, tm, os, title, sec, vol)) return(0); p->indent++; @@ -404,12 +400,14 @@ mlg_rofftail(void *arg) assert(arg); p = (struct md_mlg *)arg; - if (0 != p->pos && ! mlg_newline(p)) + if (0 != p->pos) + if ( ! mlg_newline(p)) + return(0); + + if ( ! (*p->cbs.ml_end)(p->mbuf, p->args)) return(0); mlg_mode(p, MD_BLK_OUT); - if ( ! (*p->end)(p->mbuf, p->args)) - return(0); return(mlg_newline(p)); } @@ -557,6 +555,7 @@ mlg_roffdata(void *arg, int space, const char *start, char *buf) return(0); mlg_mode(p, MD_TEXT); + return(1); } @@ -16,6 +16,7 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ +#include <assert.h> #include <stdlib.h> #include <string.h> @@ -24,6 +25,8 @@ #include "ml.h" +static int xml_alloc(void **); +static void xml_free(void *); static ssize_t xml_endtag(struct md_mbuf *, void *, const struct md_args *, enum md_ns, int); @@ -38,60 +41,59 @@ static int xml_begin(struct md_mbuf *, const char *, const char *); static int xml_end(struct md_mbuf *, const struct md_args *); +static ssize_t xml_printtagname(struct md_mbuf *, + enum md_ns, int); +static ssize_t xml_printtagargs(struct md_mbuf *, + const int *, const char **); -/* ARGSUSED */ -static int -xml_begin(struct md_mbuf *mbuf, const struct md_args *args, - const struct tm *tm, const char *os, - const char *title, const char *section, - const char *vol) +static ssize_t +xml_printtagargs(struct md_mbuf *mbuf, const int *argc, + const char **argv) { + int i, c; size_t res; - if ( ! ml_puts(mbuf, "<?xml version=\"1.0\" " - "encoding=\"UTF-8\"?>\n", &res)) - return(0); - if ( ! ml_puts(mbuf, "<mdoc xmlns:block=\"block\" " - "xmlns:special=\"special\" " - "xmlns:inline=\"inline\">", &res)) + if (NULL == argc || NULL == argv) return(0); + assert(argc && argv); - return(1); -} - - -/* ARGSUSED */ -static int -xml_end(struct md_mbuf *mbuf, const struct md_args *args) -{ - size_t res; + for (res = 0, i = 0; ROFF_ARGMAX != (c = argc[i]); i++) { + if ( ! ml_nputs(mbuf, " ", 1, &res)) + return(-1); - res = 0; - if ( ! ml_puts(mbuf, "</mdoc>", &res)) - return(0); + if ( ! ml_puts(mbuf, tokargnames[c], &res)) + return(-1); + if ( ! ml_nputs(mbuf, "=\"", 2, &res)) + return(-1); + if (argv[i]) { + if ( ! ml_putstring(mbuf, argv[i], &res)) + return(-1); + } else if ( ! ml_nputs(mbuf, "true", 4, &res)) + return(-1); + if ( ! ml_nputs(mbuf, "\"", 1, &res)) + return(-1); + } - return(1); + return((ssize_t)res); } -/* ARGSUSED */ static ssize_t -xml_begintag(struct md_mbuf *mbuf, void *data, - const struct md_args *args, enum md_ns ns, - int tok, const int *argc, const char **argv) +xml_printtagname(struct md_mbuf *mbuf, enum md_ns ns, int tok) { size_t res; - /* FIXME: doesn't print arguments! */ - res = 0; - switch (ns) { case (MD_NS_BLOCK): if ( ! ml_nputs(mbuf, "block:", 6, &res)) return(-1); break; + case (MD_NS_INLINE): + if ( ! ml_nputs(mbuf, "inline:", 7, &res)) + return(-1); + break; case (MD_NS_BODY): if ( ! ml_nputs(mbuf, "body:", 5, &res)) return(-1); @@ -100,55 +102,83 @@ xml_begintag(struct md_mbuf *mbuf, void *data, if ( ! ml_nputs(mbuf, "head:", 5, &res)) return(-1); break; - case (MD_NS_INLINE): - if ( ! ml_nputs(mbuf, "inline:", 7, &res)) - return(-1); - break; default: break; } if ( ! ml_puts(mbuf, toknames[tok], &res)) return(-1); - return((ssize_t)res); } /* ARGSUSED */ +static int +xml_begin(struct md_mbuf *mbuf, const struct md_args *args, + const struct tm *tm, const char *os, + const char *title, const char *section, + const char *vol) +{ + + if ( ! ml_puts(mbuf, "<?xml version=\"1.0\" " + "encoding=\"UTF-8\"?>\n", NULL)) + return(0); + return(ml_puts(mbuf, "<mdoc xmlns:block=\"block\" " + "xmlns:special=\"special\" " + "xmlns:inline=\"inline\">", NULL)); +} + + +/* ARGSUSED */ +static int +xml_end(struct md_mbuf *mbuf, const struct md_args *args) +{ + + return(ml_puts(mbuf, "</mdoc>", NULL)); +} + + +/* ARGSUSED */ +static ssize_t +xml_begintag(struct md_mbuf *mbuf, void *data, + const struct md_args *args, enum md_ns ns, + int tok, const int *argc, const char **argv) +{ + ssize_t res, sz; + + if (-1 == (res = xml_printtagname(mbuf, ns, tok))) + return(-1); + if (-1 == (sz = xml_printtagargs(mbuf, argc, argv))) + return(-1); + return(res + sz); +} + + +/* ARGSUSED */ static ssize_t xml_endtag(struct md_mbuf *mbuf, void *data, const struct md_args *args, enum md_ns ns, int tok) { - size_t res; - res = 0; + return(xml_printtagname(mbuf, ns, tok)); +} - switch (ns) { - case (MD_NS_BLOCK): - if ( ! ml_nputs(mbuf, "block:", 6, &res)) - return(-1); - break; - case (MD_NS_INLINE): - if ( ! ml_nputs(mbuf, "inline:", 7, &res)) - return(-1); - break; - case (MD_NS_BODY): - if ( ! ml_nputs(mbuf, "body:", 5, &res)) - return(-1); - break; - case (MD_NS_HEAD): - if ( ! ml_nputs(mbuf, "head:", 5, &res)) - return(-1); - break; - default: - break; - } - if ( ! ml_puts(mbuf, toknames[tok], &res)) - return(-1); +/* ARGSUSED */ +int +xml_alloc(void **p) +{ - return((ssize_t)res); + return(1); +} + + +/* ARGSUSED */ +void +xml_free(void *p) +{ + + /* Do nothing. */ } @@ -172,8 +202,15 @@ void * md_init_xml(const struct md_args *args, struct md_mbuf *mbuf, const struct md_rbuf *rbuf) { + struct ml_cbs cbs; + + cbs.ml_alloc = xml_alloc; + cbs.ml_free = xml_free; + cbs.ml_begintag = xml_begintag; + cbs.ml_endtag = xml_endtag; + cbs.ml_begin = xml_begin; + cbs.ml_end = xml_end; - return(mlg_alloc(args, NULL, rbuf, mbuf, xml_begintag, - xml_endtag, xml_begin, xml_end)); + return(mlg_alloc(args, rbuf, mbuf, &cbs)); } |