diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2008-11-28 15:25:49 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2008-11-28 15:25:49 +0000 |
commit | 53836d6f5d78b6d01bab9dc293ffa2649b8a0341 (patch) | |
tree | 307942f0b7401c422d6c732fffc6169363c59216 | |
parent | cc802a810a035a369db520aaa7cba82e0b3686a2 (diff) | |
download | mandoc-53836d6f5d78b6d01bab9dc293ffa2649b8a0341.tar.gz |
Removed superfluous structures from rofftree.
Added roffdata for generalised text processing.
-rw-r--r-- | libmdocml.c | 3 | ||||
-rw-r--r-- | private.h | 24 | ||||
-rw-r--r-- | roff.c | 139 |
3 files changed, 59 insertions, 107 deletions
diff --git a/libmdocml.c b/libmdocml.c index 95a0ba2b..3b7b67f2 100644 --- a/libmdocml.c +++ b/libmdocml.c @@ -56,8 +56,7 @@ md_buf_fill(struct md_rbuf *in) } -static int -md_buf_flush(struct md_mbuf *buf) +static int md_buf_flush(struct md_mbuf *buf) { ssize_t sz; @@ -209,16 +209,16 @@ extern const char *const *tokargnames; enum roffmsg { ROFF_WARN, ROFF_ERROR }; struct roffcb { - void (*roffmsg)(const struct md_args *, enum roffmsg, - const char *, const char *, const char *, - int, char *); - int (*roffhead)(const struct md_args *); - int (*rofftail)(const struct md_args *); - int (*roffin)(const struct md_args *, int, int *, char **); - int (*roffout)(const struct md_args *, int); - int (*roffblkin)(const struct md_args *, int); - int (*roffblkout)(const struct md_args *, int); - int (*roffspecial)(const struct md_args *, int); + void (*roffmsg)(void *, enum roffmsg, + const char *, const char *, char *); + int (*roffhead)(void *); + int (*rofftail)(void *); + int (*roffin)(void *, int, int *, char **); + int (*roffdata)(void *, const char *); + int (*roffout)(void *, int); + int (*roffblkin)(void *, int); + int (*roffblkout)(void *, int); + int (*roffspecial)(void *, int); }; __BEGIN_DECLS @@ -244,9 +244,7 @@ int md_buf_putstring(struct md_mbuf *, const char *); struct rofftree; -struct rofftree *roff_alloc(const struct md_args *, - struct md_mbuf *, const struct md_rbuf *, - const struct roffcb *); +struct rofftree *roff_alloc(const struct roffcb *, void *); int roff_engine(struct rofftree *, char *, size_t); int roff_free(struct rofftree *, int); @@ -80,7 +80,6 @@ struct roffarg { struct roffnode { int tok; /* Token id. */ struct roffnode *parent; /* Parent (or NULL). */ - size_t line; /* Parsed at line. */ }; struct rofftree { @@ -100,10 +99,8 @@ struct rofftree { #define ROFF_PRELUDE_Dd (1 << 4) /* `Dd' is parsed. */ #define ROFF_BODY (1 << 5) /* In roff body. */ - struct md_mbuf *mbuf; /* Output (or NULL). */ - const struct md_args *args; /* Global args. */ - const struct md_rbuf *rbuf; /* Input. */ - const struct roffcb *cb; + struct roffcb cb; + void *arg; }; static int roff_Dd(ROFFCALL_ARGS); @@ -117,7 +114,7 @@ static int roff_close(ROFFCALL_ARGS); static int roff_special(ROFFCALL_ARGS); static struct roffnode *roffnode_new(int, struct rofftree *); -static void roffnode_free(int, struct rofftree *); +static void roffnode_free(struct rofftree *); static void roff_warn(const struct rofftree *, const char *, char *, ...); @@ -360,47 +357,33 @@ const char *const *tokargnames = tokargnamesp; int roff_free(struct rofftree *tree, int flush) { - int error, tok; + int error; + struct roffnode *n; - assert(tree->mbuf); - if ( ! flush) - tree->mbuf = NULL; + error = 0; - /* LINTED */ - while (tree->last) { - if (tree->last->parent) { - tok = tree->last->tok; - if (tokens[tok].ctx == 0) { - warnx("%s: closing out explicit scope " - "of `%s' from line %zu", - tree->rbuf->name, - toknames[tok], - tree->last->line); - tree->mbuf = NULL; - } + for (n = tree->last; n->parent; n = n->parent) + if (tokens[n->tok].ctx == 0) { + roff_warn(tree, NULL, "closing explicit scope " + "of `%s'", toknames[n->tok]); + error = 1; } - if ( ! (*tokens[tree->last->tok].cb) - (tree->last->tok, tree, NULL, ROFF_EXIT)) - /* Disallow flushing. */ - tree->mbuf = NULL; - } - error = tree->mbuf ? 0 : 1; - - if (tree->mbuf && (ROFF_PRELUDE & tree->state)) { - warnx("%s: prelude never finished", - tree->rbuf->name); + if (0 == error && (ROFF_PRELUDE & tree->state)) { + roff_warn(tree, NULL, "prelude never finished"); error = 1; } + while (tree->last) + roffnode_free(tree); + free(tree); return(error ? 0 : 1); } struct rofftree * -roff_alloc(const struct md_args *args, struct md_mbuf *out, - const struct md_rbuf *in, const struct roffcb *cb) +roff_alloc(const struct roffcb *cb, void *args) { struct rofftree *tree; @@ -408,10 +391,9 @@ roff_alloc(const struct md_args *args, struct md_mbuf *out, err(1, "calloc"); tree->state = ROFF_PRELUDE; - tree->args = args; - tree->mbuf = out; - tree->rbuf = in; - tree->cb = cb; + tree->arg = args; + + (void)memcpy(&tree->cb, cb, sizeof(struct roffcb)); return(tree); } @@ -550,18 +532,16 @@ roffparse(struct rofftree *tree, char *buf, size_t sz) */ if ( ! roffscan(tree->last->tok, tokens[tok].parents)) { - roff_err(tree, *argvp, "`%s' has invalid parent `%s' " - "from line %zu", toknames[tok], - toknames[tree->rbuf->line], - tree->rbuf->line); + roff_err(tree, *argvp, "`%s' has invalid parent `%s'", + toknames[tok], + toknames[tree->last->tok]); return(0); } if ( ! roffscan(tok, tokens[tree->last->tok].children)) { - roff_err(tree, *argvp, "`%s' is invalid child for " - "`%s' from line %zu", toknames[tok], - toknames[tree->rbuf->line], - tree->rbuf->line); + roff_err(tree, *argvp, "`%s' is invalid child `%s'", + toknames[tree->last->tok], + toknames[tok]); return(0); } @@ -717,7 +697,6 @@ roffnode_new(int tokid, struct rofftree *tree) if (NULL == (p = malloc(sizeof(struct roffnode)))) err(1, "malloc"); - p->line = tree->rbuf->line; p->tok = tokid; p->parent = tree->last; tree->last = p; @@ -743,12 +722,11 @@ roffargok(int tokid, int argid) static void -roffnode_free(int tokid, struct rofftree *tree) +roffnode_free(struct rofftree *tree) { struct roffnode *p; assert(tree->last); - assert(tree->last->tok == tokid); p = tree->last; tree->last = tree->last->parent; @@ -908,12 +886,10 @@ roff_layout(ROFFCALL_ARGS) } if (ROFF_EXIT == type) { - roffnode_free(tok, tree); - return((*tree->cb->roffblkout)(tree->args, tok)); + roffnode_free(tree); + return((*tree->cb.roffblkout)(tree->arg, tok)); } - assert(tree->mbuf); - i = 0; argv++; @@ -933,23 +909,19 @@ roff_layout(ROFFCALL_ARGS) if (NULL == roffnode_new(tok, tree)) return(0); - if ( ! (*tree->cb->roffin)(tree->args, tok, argcp, argvp)) + if ( ! (*tree->cb.roffin)(tree->arg, tok, argcp, argvp)) return(0); if ( ! (ROFF_PARSED & tokens[tok].flags)) { while (*argv) { - if ( ! md_buf_putstring(tree->mbuf, *argv++)) - return(0); - if ( ! md_buf_putchar(tree->mbuf, ' ')) + if ( ! (*tree->cb.roffdata) + (tree->arg, *argv++)) return(0); } - if ( ! md_buf_putchar(tree->mbuf, '\n')) - return(0); - - if ( ! ((*tree->cb->roffout)(tree->args, tok))) + if ( ! ((*tree->cb.roffout)(tree->arg, tok))) return(0); - return((*tree->cb->roffblkin)(tree->args, tok)); + return((*tree->cb.roffblkin)(tree->arg, tok)); } while (*argv) { @@ -965,18 +937,13 @@ roff_layout(ROFFCALL_ARGS) break; } - if ( ! md_buf_putstring(tree->mbuf, *argv++)) - return(0); - if ( ! md_buf_putchar(tree->mbuf, ' ')) + if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) return(0); } - if (NULL == *argv && ! md_buf_putchar(tree->mbuf, '\n')) - return(0); - - if ( ! ((*tree->cb->roffout)(tree->args, tok))) + if ( ! ((*tree->cb.roffout)(tree->arg, tok))) return(0); - return((*tree->cb->roffblkin)(tree->args, tok)); + return((*tree->cb.roffblkin)(tree->arg, tok)); } @@ -987,8 +954,6 @@ roff_text(ROFFCALL_ARGS) int i, c, argcp[ROFF_MAXARG]; char *v, *argvp[ROFF_MAXARG]; - assert(tree->mbuf); - if (ROFF_PRELUDE & tree->state) { roff_err(tree, *argv, "`%s' disallowed in prelude", toknames[tok]); @@ -1011,19 +976,15 @@ roff_text(ROFFCALL_ARGS) argcp[i] = ROFF_ARGMAX; argvp[i] = NULL; - if ( ! (*tree->cb->roffin)(tree->args, tok, argcp, argvp)) + if ( ! (*tree->cb.roffin)(tree->arg, tok, argcp, argvp)) return(0); if ( ! (ROFF_PARSED & tokens[tok].flags)) { while (*argv) { - if ( ! md_buf_putstring(tree->mbuf, *argv++)) - return(0); - if ( ! md_buf_putchar(tree->mbuf, ' ')) + if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) return(0); } - if ( ! md_buf_putchar(tree->mbuf, '\n')) - return(0); - return((*tree->cb->roffout)(tree->args, tok)); + return((*tree->cb.roffout)(tree->arg, tok)); } while (*argv) { @@ -1039,17 +1000,11 @@ roff_text(ROFFCALL_ARGS) return(0); break; } - - if ( ! md_buf_putstring(tree->mbuf, *argv++)) - return(0); - if ( ! md_buf_putchar(tree->mbuf, ' ')) + if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) return(0); } - if (NULL == *argv && ! md_buf_putchar(tree->mbuf, '\n')) - return(0); - - return((*tree->cb->roffout)(tree->args, tok)); + return((*tree->cb.roffout)(tree->arg, tok)); } @@ -1076,7 +1031,7 @@ static int roff_special(ROFFCALL_ARGS) { - return((*tree->cb->roffspecial)(tree->args, tok)); + return((*tree->cb.roffspecial)(tree->arg, tok)); } @@ -1090,8 +1045,8 @@ roff_warn(const struct rofftree *tree, const char *pos, char *fmt, ...) (void)vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - (*tree->cb->roffmsg)(tree->args, ROFF_WARN, tree->cur, pos, - tree->rbuf->name, tree->rbuf->line, buf); + (*tree->cb.roffmsg)(tree->arg, + ROFF_WARN, tree->cur, pos, buf); } @@ -1105,6 +1060,6 @@ roff_err(const struct rofftree *tree, const char *pos, char *fmt, ...) (void)vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - (*tree->cb->roffmsg)(tree->args, ROFF_ERROR, tree->cur, pos, - tree->rbuf->name, tree->rbuf->line, buf); + (*tree->cb.roffmsg)(tree->arg, + ROFF_ERROR, tree->cur, pos, buf); } |