-static void mdoc_free1(struct mdoc *);
-static int mdoc_alloc1(struct mdoc *);
-static struct mdoc_node *node_alloc(struct mdoc *, int, int,
- int, enum mdoc_type);
-static int 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 *);
-static int perr(struct mdoc *, int, int, enum merr);
-
-#define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
-
-/*
- * Get the first (root) node of the parse tree.
- */
-const struct mdoc_node *
-mdoc_node(const struct mdoc *m)
-{
-
- return(MDOC_HALT & m->flags ? NULL : m->first);
-}
-
-
-const struct mdoc_meta *
-mdoc_meta(const struct mdoc *m)
-{
-
- return(MDOC_HALT & m->flags ? NULL : &m->meta);
-}
-
-
-static void
-mdoc_free1(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);
-}
-
-
-static int
-mdoc_alloc1(struct mdoc *mdoc)
-{
-
- 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)
- return(0);
-
- mdoc->first = mdoc->last;
- mdoc->last->type = MDOC_ROOT;
- mdoc->next = MDOC_NEXT_CHILD;
- return(1);
-}
-
-
-/*
- * Free up all resources contributed by a parse: the node tree,
- * meta-data and so on. Then reallocate the root node for another
- * parse.
- */
-int
-mdoc_reset(struct mdoc *mdoc)
-{
-
- mdoc_free1(mdoc);
- return(mdoc_alloc1(mdoc));
-}
-
-
-/*
- * Completely free up all resources.
- */
-void
-mdoc_free(struct mdoc *mdoc)
-{
-
- mdoc_free1(mdoc);
- if (mdoc->htab)
- mdoc_hash_free(mdoc->htab);
- free(mdoc);
-}
-
-
-struct mdoc *
-mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
-{
- struct mdoc *p;
-
- if (NULL == (p = calloc(1, sizeof(struct mdoc))))
- return(NULL);
- if (cb)
- (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
-
- p->data = data;
- p->pflags = pflags;
-
- if (NULL == (p->htab = mdoc_hash_alloc())) {
- free(p);
- return(NULL);
- } else if (mdoc_alloc1(p))
- return(p);
-
- free(p);
- return(NULL);
-}
-
-
-/*
- * Climb back up the parse tree, validating open scopes. Mostly calls
- * through to macro_end in macro.c.
- */
-int
-mdoc_endparse(struct mdoc *m)
-{
-
- if (MDOC_HALT & m->flags)
- return(0);
- else if (mdoc_macroend(m))
- return(1);
- m->flags |= MDOC_HALT;
- return(0);
-}