=================================================================== RCS file: /cvs/mandoc/mdoc.c,v retrieving revision 1.68 retrieving revision 1.72 diff -u -p -r1.68 -r1.72 --- mandoc/mdoc.c 2009/03/20 15:14:01 1.68 +++ mandoc/mdoc.c 2009/03/23 15:41:09 1.72 @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.68 2009/03/20 15:14:01 kristaps Exp $ */ +/* $Id: mdoc.c,v 1.72 2009/03/23 15:41:09 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -24,7 +24,7 @@ #include #include -#include "private.h" +#include "libmdoc.h" /* * Main caller in the libmdoc library. This begins the parsing routine, @@ -32,15 +32,6 @@ * 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,35 +85,40 @@ 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) { - if (MDOC_HALT & mdoc->flags) - return(NULL); - if (mdoc->first) - assert(MDOC_ROOT == mdoc->first->type); - 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) { - if (MDOC_HALT & mdoc->flags) - return(NULL); - 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. + * 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) @@ -143,9 +140,10 @@ mdoc_reset(struct mdoc *mdoc) bzero(&mdoc->meta, sizeof(struct mdoc_meta)); mdoc->flags = 0; mdoc->lastnamed = mdoc->lastsec = 0; - - mdoc->first = mdoc->last = - xcalloc(1, sizeof(struct mdoc_node)); + 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; } @@ -183,14 +181,16 @@ 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 = p->first = - 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->pflags = pflags; p->next = MDOC_NEXT_CHILD; @@ -204,20 +204,15 @@ mdoc_alloc(void *data, int pflags, const struct mdoc_c * 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); } @@ -360,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); } @@ -369,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); @@ -434,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) { @@ -499,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)); } @@ -627,12 +623,6 @@ parsemacro(struct mdoc *m, int ln, char *buf) if ( ! mdoc_macro(m, c, ln, 1, &i, buf)) goto err; - - /* - * If we're in literal mode, then add a newline to the end of - * macro lines. Our frontends will interpret this correctly - * (it's documented in mdoc.3). - */ return(1);