-man_alloc1(struct man *man)
-{
-
- memset(&man->meta, 0, sizeof(struct man_meta));
- man->flags = 0;
- man->last = mandoc_calloc(1, sizeof(struct man_node));
- man->first = man->last;
- man->last->type = MAN_ROOT;
- man->last->tok = MAN_MAX;
- man->next = MAN_NEXT_CHILD;
-}
-
-
-static int
-man_node_append(struct man *man, struct man_node *p)
-{
-
- assert(man->last);
- assert(man->first);
- assert(MAN_ROOT != p->type);
-
- switch (man->next) {
- case MAN_NEXT_SIBLING:
- man->last->next = p;
- p->prev = man->last;
- p->parent = man->last->parent;
- break;
- case MAN_NEXT_CHILD:
- man->last->child = p;
- p->parent = man->last;
- break;
- default:
- abort();
- /* NOTREACHED */
- }
-
- assert(p->parent);
- p->parent->nchild++;
-
- if ( ! man_valid_pre(man, p))
- return(0);
-
- switch (p->type) {
- case MAN_HEAD:
- assert(MAN_BLOCK == p->parent->type);
- p->parent->head = p;
- break;
- case MAN_TAIL:
- assert(MAN_BLOCK == p->parent->type);
- p->parent->tail = p;
- break;
- case MAN_BODY:
- assert(MAN_BLOCK == p->parent->type);
- p->parent->body = p;
- break;
- default:
- break;
- }
-
- man->last = p;
-
- switch (p->type) {
- case MAN_TBL:
- /* FALLTHROUGH */
- case MAN_TEXT:
- if ( ! man_valid_post(man))
- return(0);
- break;
- default:
- break;
- }
-
- return(1);
-}
-
-static struct man_node *
-man_node_alloc(struct man *man, int line, int pos,
- enum man_type type, enum mant tok)
-{
- struct man_node *p;
-
- p = mandoc_calloc(1, sizeof(struct man_node));
- p->line = line;
- p->pos = pos;
- p->type = type;
- p->tok = tok;
-
- if (MAN_NEWLINE & man->flags)
- p->flags |= MAN_LINE;
- man->flags &= ~MAN_NEWLINE;
- return(p);
-}
-
-int
-man_elem_alloc(struct man *man, int line, int pos, enum mant tok)
-{
- struct man_node *p;
-
- p = man_node_alloc(man, line, pos, MAN_ELEM, tok);
- if ( ! man_node_append(man, p))
- return(0);
- man->next = MAN_NEXT_CHILD;
- return(1);
-}
-
-int
-man_tail_alloc(struct man *man, int line, int pos, enum mant tok)
-{
- struct man_node *p;
-
- p = man_node_alloc(man, line, pos, MAN_TAIL, tok);
- if ( ! man_node_append(man, p))
- return(0);
- man->next = MAN_NEXT_CHILD;
- return(1);
-}
-
-int
-man_head_alloc(struct man *man, int line, int pos, enum mant tok)
-{
- struct man_node *p;
-
- p = man_node_alloc(man, line, pos, MAN_HEAD, tok);
- if ( ! man_node_append(man, p))
- return(0);
- man->next = MAN_NEXT_CHILD;
- return(1);
-}
-
-int
-man_body_alloc(struct man *man, int line, int pos, enum mant tok)
-{
- struct man_node *p;
-
- p = man_node_alloc(man, line, pos, MAN_BODY, tok);
- if ( ! man_node_append(man, p))
- return(0);
- man->next = MAN_NEXT_CHILD;
- return(1);
-}
-
-int
-man_block_alloc(struct man *man, int line, int pos, enum mant tok)
-{
- struct man_node *p;
-
- p = man_node_alloc(man, line, pos, MAN_BLOCK, tok);
- if ( ! man_node_append(man, p))
- return(0);
- man->next = MAN_NEXT_CHILD;
- return(1);
-}
-
-int
-man_word_alloc(struct man *man, int line, int pos, const char *word)
-{
- struct man_node *n;
-
- n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX);
- n->string = roff_strdup(man->roff, word);
-
- if ( ! man_node_append(man, n))
- return(0);
-
- man->next = MAN_NEXT_SIBLING;
- return(1);
-}
-
-/*
- * Free all of the resources held by a node. This does NOT unlink a
- * node from its context; for that, see man_node_unlink().
- */
-static void
-man_node_free(struct man_node *p)
-{
-
- if (p->string)
- free(p->string);
- free(p);
-}
-
-void
-man_node_delete(struct man *man, struct man_node *p)
-{
-
- while (p->child)
- man_node_delete(man, p->child);
-
- man_node_unlink(man, p);
- man_node_free(p);
-}
-
-int
-man_addeqn(struct man *man, const struct eqn *ep)
-{
- struct man_node *n;
-
- assert( ! (MAN_HALT & man->flags));
-
- n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX);
- n->eqn = ep;
-
- if ( ! man_node_append(man, n))
- return(0);
-
- man->next = MAN_NEXT_SIBLING;
- return(man_descope(man, ep->ln, ep->pos));
-}
-
-int
-man_addspan(struct man *man, const struct tbl_span *sp)
-{
- struct man_node *n;
-
- assert( ! (MAN_HALT & man->flags));
-
- n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX);
- n->span = sp;
-
- if ( ! man_node_append(man, n))
- return(0);
-
- man->next = MAN_NEXT_SIBLING;
- return(man_descope(man, sp->line, 0));
-}
-
-static int
-man_descope(struct man *man, int line, int offs)