=================================================================== RCS file: /cvs/mandoc/mdoc.c,v retrieving revision 1.66 retrieving revision 1.72 diff -u -p -r1.66 -r1.72 --- mandoc/mdoc.c 2009/03/16 23:37:28 1.66 +++ mandoc/mdoc.c 2009/03/23 15:41:09 1.72 @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.66 2009/03/16 23:37:28 kristaps Exp $ */ +/* $Id: mdoc.c,v 1.72 2009/03/23 15:41:09 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -24,23 +24,14 @@ #include #include -#include "private.h" +#include "libmdoc.h" /* * Main caller in the libmdoc library. This begins the parsing routine, * handles allocation of data, and so forth. Most of the "work" is done - * in macro.c and validate.c. + * in macro.c, validate.c and action.c. */ -static struct mdoc_node *mdoc_node_alloc(const struct mdoc *); -static int mdoc_node_append(struct mdoc *, - struct mdoc_node *); - -static int parsetext(struct mdoc *, int, char *); -static int parsemacro(struct mdoc *, int, char *); -static int macrowarn(struct mdoc *, int, const char *); - - const char *const __mdoc_macronames[MDOC_MAX] = { "\\\"", "Dd", "Dt", "Os", "Sh", "Ss", "Pp", "D1", @@ -75,7 +66,8 @@ const char *const __mdoc_macronames[MDOC_MAX] = { "Lp", "Lk", "Mt", "Brq", /* LINTED */ "Bro", "Brc", "\%C", "Es", - "En", "Dx" + /* LINTED */ + "En", "Dx", "\%Q" }; const char *const __mdoc_argnames[MDOC_ARG_MAX] = { @@ -93,31 +85,79 @@ const char *const __mdoc_argnames[MDOC_ARG_MAX] = { const char * const *mdoc_macronames = __mdoc_macronames; const char * const *mdoc_argnames = __mdoc_argnames; +/* FIXME: have this accept line/pos/tok. */ +/* FIXME: mdoc_alloc1 and mdoc_free1 like in man.c. */ +static struct mdoc_node *mdoc_node_alloc(const struct mdoc *); +static int mdoc_node_append(struct mdoc *, + struct mdoc_node *); +static int parsetext(struct mdoc *, int, char *); +static int parsemacro(struct mdoc *, int, char *); +static int macrowarn(struct mdoc *, int, const char *); + + +/* + * Get the first (root) node of the parse tree. + */ const struct mdoc_node * -mdoc_node(const struct mdoc *mdoc) +mdoc_node(const struct mdoc *m) { - return(mdoc->first); + return(MDOC_HALT & m->flags ? NULL : m->first); } const struct mdoc_meta * -mdoc_meta(const struct mdoc *mdoc) +mdoc_meta(const struct mdoc *m) { - return(&mdoc->meta); + return(MDOC_HALT & m->flags ? NULL : &m->meta); } +/* + * Free up all resources contributed by a parse: the node tree, + * meta-data and so on. Then reallocate the root node for another + * parse. + */ void +mdoc_reset(struct mdoc *mdoc) +{ + + if (mdoc->first) + mdoc_node_freelist(mdoc->first); + if (mdoc->meta.title) + free(mdoc->meta.title); + if (mdoc->meta.os) + free(mdoc->meta.os); + if (mdoc->meta.name) + free(mdoc->meta.name); + if (mdoc->meta.arch) + free(mdoc->meta.arch); + if (mdoc->meta.vol) + free(mdoc->meta.vol); + + bzero(&mdoc->meta, sizeof(struct mdoc_meta)); + mdoc->flags = 0; + mdoc->lastnamed = mdoc->lastsec = 0; + mdoc->last = calloc(1, sizeof(struct mdoc_node)); + if (NULL == mdoc->last) + err(1, "calloc"); + mdoc->first = mdoc->last; + mdoc->last->type = MDOC_ROOT; + mdoc->next = MDOC_NEXT_CHILD; +} + + +/* + * Completely free up all resources. + */ +void mdoc_free(struct mdoc *mdoc) { if (mdoc->first) mdoc_node_freelist(mdoc->first); - if (mdoc->htab) - mdoc_tokhash_free(mdoc->htab); if (mdoc->meta.title) free(mdoc->meta.title); if (mdoc->meta.os) @@ -129,6 +169,9 @@ mdoc_free(struct mdoc *mdoc) if (mdoc->meta.vol) free(mdoc->meta.vol); + if (mdoc->htab) + mdoc_tokhash_free(mdoc->htab); + free(mdoc); } @@ -138,38 +181,38 @@ mdoc_alloc(void *data, int pflags, const struct mdoc_c { struct mdoc *p; - p = xcalloc(1, sizeof(struct mdoc)); + if (NULL == (p = calloc(1, sizeof(struct mdoc)))) + err(1, "calloc"); p->data = data; if (cb) (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); - p->last = xcalloc(1, sizeof(struct mdoc_node)); + if (NULL == (p->first = calloc(1, sizeof(struct mdoc_node)))) + err(1, "calloc"); + p->last = p->first; p->last->type = MDOC_ROOT; - p->first = p->last; p->pflags = pflags; p->next = MDOC_NEXT_CHILD; p->htab = mdoc_tokhash_alloc(); - return(p); } +/* + * Climb back up the parse tree, validating open scopes. Mostly calls + * through to macro_end in macro.c. + */ int -mdoc_endparse(struct mdoc *mdoc) +mdoc_endparse(struct mdoc *m) { - if (MDOC_HALT & mdoc->flags) + if (MDOC_HALT & m->flags) return(0); - if (NULL == mdoc->first) + else if (mdoc_macroend(m)) return(1); - - assert(mdoc->last); - if ( ! macro_end(mdoc)) { - mdoc->flags |= MDOC_HALT; - return(0); - } - return(1); + m->flags |= MDOC_HALT; + return(0); } @@ -291,6 +334,8 @@ mdoc_node_append(struct mdoc *mdoc, struct mdoc_node * if ( ! mdoc_valid_pre(mdoc, p)) return(0); + if ( ! mdoc_action_pre(mdoc, p)) + return(0); switch (p->type) { case (MDOC_HEAD): @@ -310,6 +355,18 @@ mdoc_node_append(struct mdoc *mdoc, struct mdoc_node * } mdoc->last = p; + + switch (p->type) { + case (MDOC_TEXT): + if ( ! mdoc_valid_post(mdoc)) + return(0); + if ( ! mdoc_action_post(mdoc)) + return(0); + break; + default: + break; + } + return(1); } @@ -319,7 +376,8 @@ mdoc_node_alloc(const struct mdoc *mdoc) { struct mdoc_node *p; - p = xcalloc(1, sizeof(struct mdoc_node)); + if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) + err(1, "calloc"); p->sec = mdoc->lastsec; return(p); @@ -384,19 +442,6 @@ mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int -mdoc_root_alloc(struct mdoc *mdoc) -{ - struct mdoc_node *p; - - p = mdoc_node_alloc(mdoc); - - p->type = MDOC_ROOT; - - return(mdoc_node_append(mdoc, p)); -} - - -int mdoc_block_alloc(struct mdoc *mdoc, int line, int pos, int tok, struct mdoc_arg *args) { @@ -449,7 +494,8 @@ mdoc_word_alloc(struct mdoc *mdoc, p->line = line; p->pos = pos; p->type = MDOC_TEXT; - p->string = xstrdup(word); + if (NULL == (p->string = strdup(word))) + err(1, "strdup"); return(mdoc_node_append(mdoc, p)); } @@ -485,17 +531,21 @@ mdoc_node_freelist(struct mdoc_node *p) * control character. */ static int -parsetext(struct mdoc *mdoc, int line, char *buf) +parsetext(struct mdoc *m, int line, char *buf) { - if (SEC_PROLOGUE == mdoc->lastnamed) - return(mdoc_perr(mdoc, line, 0, + if (SEC_PROLOGUE == m->lastnamed) + return(mdoc_perr(m, line, 0, "text disallowed in prologue")); - if ( ! mdoc_word_alloc(mdoc, line, 0, buf)) + if (0 == buf[0] && ! (MDOC_LITERAL & m->flags)) + return(mdoc_perr(m, line, 0, + "blank lines only in literal context")); + + if ( ! mdoc_word_alloc(m, line, 0, buf)) return(0); - mdoc->next = MDOC_NEXT_SIBLING; + m->next = MDOC_NEXT_SIBLING; return(1); }