=================================================================== RCS file: /cvs/mandoc/mdoc.c,v retrieving revision 1.246 retrieving revision 1.247 diff -u -p -r1.246 -r1.247 --- mandoc/mdoc.c 2015/04/18 17:53:21 1.246 +++ mandoc/mdoc.c 2015/04/19 13:50:26 1.247 @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.246 2015/04/18 17:53:21 schwarze Exp $ */ +/* $Id: mdoc.c,v 1.247 2015/04/19 13:50:26 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2012-2015 Ingo Schwarze @@ -32,6 +32,7 @@ #include "roff.h" #include "mdoc.h" #include "libmandoc.h" +#include "roff_int.h" #include "libmdoc.h" const char *const __mdoc_macronames[MDOC_MAX + 1] = { @@ -83,12 +84,6 @@ const char *const __mdoc_argnames[MDOC_ARG_MAX] = { const char * const *mdoc_macronames = __mdoc_macronames; const char * const *mdoc_argnames = __mdoc_argnames; -static void mdoc_node_free(struct roff_node *); -static void mdoc_node_unlink(struct roff_man *, - struct roff_node *); -static struct roff_node *node_alloc(struct roff_man *, int, int, - int, enum roff_type); -static void node_append(struct roff_man *, struct roff_node *); static int mdoc_ptext(struct roff_man *, int, char *, int); static int mdoc_pmacro(struct roff_man *, int, char *, int); @@ -105,11 +100,11 @@ mdoc_addeqn(struct roff_man *mdoc, const struct eqn *e { struct roff_node *n; - n = node_alloc(mdoc, ep->ln, ep->pos, MDOC_MAX, ROFFT_EQN); + n = roff_node_alloc(mdoc, ep->ln, ep->pos, ROFFT_EQN, MDOC_MAX); n->eqn = ep; if (ep->ln > mdoc->last->line) n->flags |= MDOC_LINE; - node_append(mdoc, n); + roff_node_append(mdoc, n); mdoc->next = ROFF_NEXT_SIBLING; } @@ -118,9 +113,10 @@ mdoc_addspan(struct roff_man *mdoc, const struct tbl_s { struct roff_node *n; - n = node_alloc(mdoc, sp->line, 0, MDOC_MAX, ROFFT_TBL); + n = roff_node_alloc(mdoc, sp->line, 0, ROFFT_TBL, MDOC_MAX); n->span = sp; - node_append(mdoc, n); + roff_node_append(mdoc, n); + mdoc_valid_post(mdoc); mdoc->next = ROFF_NEXT_SIBLING; } @@ -177,146 +173,17 @@ mdoc_macro(MACRO_PROT_ARGS) (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf); } - -static void -node_append(struct roff_man *mdoc, struct roff_node *p) -{ - - assert(mdoc->last); - assert(mdoc->first); - assert(p->type != ROFFT_ROOT); - - switch (mdoc->next) { - case ROFF_NEXT_SIBLING: - mdoc->last->next = p; - p->prev = mdoc->last; - p->parent = mdoc->last->parent; - break; - case ROFF_NEXT_CHILD: - mdoc->last->child = p; - p->parent = mdoc->last; - break; - default: - abort(); - /* NOTREACHED */ - } - - p->parent->nchild++; - - /* - * Copy over the normalised-data pointer of our parent. Not - * everybody has one, but copying a null pointer is fine. - */ - - switch (p->type) { - case ROFFT_BODY: - if (ENDBODY_NOT != p->end) - break; - /* FALLTHROUGH */ - case ROFFT_TAIL: - /* FALLTHROUGH */ - case ROFFT_HEAD: - p->norm = p->parent->norm; - break; - default: - break; - } - - mdoc_valid_pre(mdoc, p); - - switch (p->type) { - case ROFFT_HEAD: - assert(p->parent->type == ROFFT_BLOCK); - p->parent->head = p; - break; - case ROFFT_TAIL: - assert(p->parent->type == ROFFT_BLOCK); - p->parent->tail = p; - break; - case ROFFT_BODY: - if (p->end) - break; - assert(p->parent->type == ROFFT_BLOCK); - p->parent->body = p; - break; - default: - break; - } - - mdoc->last = p; - - switch (p->type) { - case ROFFT_TBL: - /* FALLTHROUGH */ - case ROFFT_TEXT: - mdoc_valid_post(mdoc); - break; - default: - break; - } -} - -static struct roff_node * -node_alloc(struct roff_man *mdoc, int line, int pos, - int tok, enum roff_type type) -{ - struct roff_node *p; - - p = mandoc_calloc(1, sizeof(*p)); - p->sec = mdoc->lastsec; - p->line = line; - p->pos = pos; - p->tok = tok; - p->type = type; - - /* Flag analysis. */ - - if (MDOC_SYNOPSIS & mdoc->flags) - p->flags |= MDOC_SYNPRETTY; - else - p->flags &= ~MDOC_SYNPRETTY; - if (MDOC_NEWLINE & mdoc->flags) - p->flags |= MDOC_LINE; - mdoc->flags &= ~MDOC_NEWLINE; - - return(p); -} - void mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok) { struct roff_node *p; - p = node_alloc(mdoc, line, pos, tok, ROFFT_TAIL); - node_append(mdoc, p); + p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok); + roff_node_append(mdoc, p); mdoc->next = ROFF_NEXT_CHILD; } struct roff_node * -mdoc_head_alloc(struct roff_man *mdoc, int line, int pos, int tok) -{ - struct roff_node *p; - - assert(mdoc->first); - assert(mdoc->last); - p = node_alloc(mdoc, line, pos, tok, ROFFT_HEAD); - node_append(mdoc, p); - mdoc->next = ROFF_NEXT_CHILD; - return(p); -} - -struct roff_node * -mdoc_body_alloc(struct roff_man *mdoc, int line, int pos, int tok) -{ - struct roff_node *p; - - p = node_alloc(mdoc, line, pos, tok, ROFFT_BODY); - node_append(mdoc, p); - mdoc->next = ROFF_NEXT_CHILD; - return(p); -} - -struct roff_node * mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok, struct roff_node *body, enum mdoc_endbody end) { @@ -324,11 +191,11 @@ mdoc_endbody_alloc(struct roff_man *mdoc, int line, in body->flags |= MDOC_ENDED; body->parent->flags |= MDOC_ENDED; - p = node_alloc(mdoc, line, pos, tok, ROFFT_BODY); + p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok); p->body = body; p->norm = body->norm; p->end = end; - node_append(mdoc, p); + roff_node_append(mdoc, p); mdoc->next = ROFF_NEXT_SIBLING; return(p); } @@ -339,7 +206,7 @@ mdoc_block_alloc(struct roff_man *mdoc, int line, int { struct roff_node *p; - p = node_alloc(mdoc, line, pos, tok, ROFFT_BLOCK); + p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok); p->args = args; if (p->args) (args->refcnt)++; @@ -359,7 +226,7 @@ mdoc_block_alloc(struct roff_man *mdoc, int line, int default: break; } - node_append(mdoc, p); + roff_node_append(mdoc, p); mdoc->next = ROFF_NEXT_CHILD; return(p); } @@ -370,7 +237,7 @@ mdoc_elem_alloc(struct roff_man *mdoc, int line, int p { struct roff_node *p; - p = node_alloc(mdoc, line, pos, tok, ROFFT_ELEM); + p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok); p->args = args; if (p->args) (args->refcnt)++; @@ -382,7 +249,7 @@ mdoc_elem_alloc(struct roff_man *mdoc, int line, int p default: break; } - node_append(mdoc, p); + roff_node_append(mdoc, p); mdoc->next = ROFF_NEXT_CHILD; } @@ -391,9 +258,10 @@ mdoc_word_alloc(struct roff_man *mdoc, int line, int p { struct roff_node *n; - n = node_alloc(mdoc, line, pos, MDOC_MAX, ROFFT_TEXT); + n = roff_node_alloc(mdoc, line, pos, ROFFT_TEXT, MDOC_MAX); n->string = roff_strdup(mdoc->roff, p); - node_append(mdoc, n); + roff_node_append(mdoc, n); + mdoc_valid_post(mdoc); mdoc->next = ROFF_NEXT_SIBLING; } @@ -412,76 +280,12 @@ mdoc_word_append(struct roff_man *mdoc, const char *p) mdoc->next = ROFF_NEXT_SIBLING; } -static void -mdoc_node_free(struct roff_node *p) -{ - - if (p->type == ROFFT_BLOCK || p->type == ROFFT_ELEM) - free(p->norm); - if (p->string) - free(p->string); - if (p->args) - mdoc_argv_free(p->args); - free(p); -} - -static void -mdoc_node_unlink(struct roff_man *mdoc, struct roff_node *n) -{ - - /* Adjust siblings. */ - - if (n->prev) - n->prev->next = n->next; - if (n->next) - n->next->prev = n->prev; - - /* Adjust parent. */ - - if (n->parent) { - n->parent->nchild--; - if (n->parent->child == n) - n->parent->child = n->prev ? n->prev : n->next; - if (n->parent->last == n) - n->parent->last = n->prev ? n->prev : NULL; - } - - /* Adjust parse point, if applicable. */ - - if (mdoc && mdoc->last == n) { - if (n->prev) { - mdoc->last = n->prev; - mdoc->next = ROFF_NEXT_SIBLING; - } else { - mdoc->last = n->parent; - mdoc->next = ROFF_NEXT_CHILD; - } - } - - if (mdoc && mdoc->first == n) - mdoc->first = NULL; -} - void -mdoc_node_delete(struct roff_man *mdoc, struct roff_node *p) -{ - - while (p->child) { - assert(p->nchild); - mdoc_node_delete(mdoc, p->child); - } - assert(0 == p->nchild); - - mdoc_node_unlink(mdoc, p); - mdoc_node_free(p); -} - -void mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p) { - mdoc_node_unlink(mdoc, p); - node_append(mdoc, p); + roff_node_unlink(mdoc, p); + roff_node_append(mdoc, p); } /*