From acfef3c388fe4c84eaad68e8f398c7873f1bb732 Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Sat, 18 Apr 2015 17:28:36 +0000 Subject: Unify {mdoc,man}_{alloc,reset,free}() into roff_man_{alloc,reset,free}(). Minus 80 lines of code, no functional change. Written on the train from Koeln to Wolfsburg returning from p2k15. --- libman.h | 3 +-- libmandoc.h | 17 ++++++------ libmdoc.h | 3 +-- man.c | 64 +-------------------------------------------- mdoc.c | 86 +------------------------------------------------------------ read.c | 64 ++++++++++++++++++--------------------------- roff.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 107 insertions(+), 202 deletions(-) diff --git a/libman.h b/libman.h index 916d9576..3e0ade1a 100644 --- a/libman.h +++ b/libman.h @@ -1,4 +1,4 @@ -/* $Id: libman.h,v 1.72 2015/04/18 17:01:58 schwarze Exp $ */ +/* $Id: libman.h,v 1.73 2015/04/18 17:28:36 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2014, 2015 Ingo Schwarze @@ -42,7 +42,6 @@ void man_block_alloc(struct roff_man *, int, int, int); void man_head_alloc(struct roff_man *, int, int, int); void man_body_alloc(struct roff_man *, int, int, int); void man_elem_alloc(struct roff_man *, int, int, int); -void man_node_delete(struct roff_man *, struct roff_node *); int man_hash_find(const char *); void man_macroend(struct roff_man *); void man_valid_post(struct roff_man *); diff --git a/libmandoc.h b/libmandoc.h index c16cf4d3..27275d95 100644 --- a/libmandoc.h +++ b/libmandoc.h @@ -1,4 +1,4 @@ -/* $Id: libmandoc.h,v 1.57 2015/04/18 17:01:58 schwarze Exp $ */ +/* $Id: libmandoc.h,v 1.58 2015/04/18 17:28:36 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011, 2012 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -40,6 +40,7 @@ struct tbl_span; struct eqn; struct roff; struct roff_man; +struct roff_node; void mandoc_msg(enum mandocerr, struct mparse *, int, int, const char *); @@ -54,21 +55,15 @@ int mandoc_eos(const char *, size_t); int mandoc_strntoi(const char *, size_t, int); const char *mandoc_a2msec(const char*); -void mdoc_free(struct roff_man *); -struct roff_man *mdoc_alloc(struct roff *, struct mparse *, - const char *, int); -void mdoc_reset(struct roff_man *); void mdoc_hash_init(void); +void mdoc_node_delete(struct roff_man *, struct roff_node *); int mdoc_parseln(struct roff_man *, int, char *, int); void mdoc_endparse(struct roff_man *); void mdoc_addspan(struct roff_man *, const struct tbl_span *); void mdoc_addeqn(struct roff_man *, const struct eqn *); -void man_free(struct roff_man *); -struct roff_man *man_alloc(struct roff *, struct mparse *, - const char *, int); -void man_reset(struct roff_man *); void man_hash_init(void); +void man_node_delete(struct roff_man *, struct roff_node *); int man_parseln(struct roff_man *, int, char *, int); void man_endparse(struct roff_man *); void man_addspan(struct roff_man *, const struct tbl_span *); @@ -81,6 +76,10 @@ int preconv_encode(struct buf *, size_t *, void roff_free(struct roff *); struct roff *roff_alloc(struct mparse *, const struct mchars *, int); void roff_reset(struct roff *); +void roff_man_free(struct roff_man *); +struct roff_man *roff_man_alloc(struct roff *, struct mparse *, + const char *, int); +void roff_man_reset(struct roff_man *); enum rofferr roff_parseln(struct roff *, int, struct buf *, int *); void roff_endparse(struct roff *); void roff_setreg(struct roff *, const char *, int, char sign); diff --git a/libmdoc.h b/libmdoc.h index 98123f71..02c18372 100644 --- a/libmdoc.h +++ b/libmdoc.h @@ -1,4 +1,4 @@ -/* $Id: libmdoc.h,v 1.101 2015/04/18 17:01:58 schwarze Exp $ */ +/* $Id: libmdoc.h,v 1.102 2015/04/18 17:28:36 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -78,7 +78,6 @@ void mdoc_tail_alloc(struct roff_man *, int, int, int); struct roff_node *mdoc_body_alloc(struct roff_man *, int, int, int); struct roff_node *mdoc_endbody_alloc(struct roff_man *, int, int, int, struct roff_node *, enum mdoc_endbody); -void mdoc_node_delete(struct roff_man *, struct roff_node *); void mdoc_node_relink(struct roff_man *, struct roff_node *); int mdoc_hash_find(const char *); const char *mdoc_a2att(const char *); diff --git a/man.c b/man.c index f2ee0c18..608ef6cf 100644 --- a/man.c +++ b/man.c @@ -1,4 +1,4 @@ -/* $Id: man.c,v 1.155 2015/04/18 17:01:58 schwarze Exp $ */ +/* $Id: man.c,v 1.156 2015/04/18 17:28:36 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -49,10 +49,8 @@ const char *const __man_macronames[MAN_MAX] = { const char * const *man_macronames = __man_macronames; -static void man_alloc1(struct roff_man *); static void man_breakscope(struct roff_man *, int); static void man_descope(struct roff_man *, int, int); -static void man_free1(struct roff_man *); static struct roff_node *man_node_alloc(struct roff_man *, int, int, enum roff_type, int); static void man_node_append(struct roff_man *, @@ -78,38 +76,6 @@ man_meta(const struct roff_man *man) return(&man->meta); } -void -man_reset(struct roff_man *man) -{ - - man_free1(man); - man_alloc1(man); -} - -void -man_free(struct roff_man *man) -{ - - man_free1(man); - free(man); -} - -struct roff_man * -man_alloc(struct roff *roff, struct mparse *parse, - const char *defos, int quick) -{ - struct roff_man *p; - - p = mandoc_calloc(1, sizeof(*p)); - p->parse = parse; - p->defos = defos; - p->quick = quick; - p->roff = roff; - - man_alloc1(p); - return(p); -} - void man_endparse(struct roff_man *man) { @@ -129,34 +95,6 @@ man_parseln(struct roff_man *man, int ln, char *buf, int offs) man_ptext(man, ln, buf, offs)); } -static void -man_free1(struct roff_man *man) -{ - - if (man->first) - man_node_delete(man, man->first); - free(man->meta.title); - free(man->meta.os); - free(man->meta.date); - free(man->meta.vol); - free(man->meta.msec); -} - -static void -man_alloc1(struct roff_man *man) -{ - - memset(&man->meta, 0, sizeof(man->meta)); - man->macroset = MACROSET_MAN; - man->flags = 0; - man->last = mandoc_calloc(1, sizeof(*man->last)); - man->first = man->last; - man->last->type = ROFFT_ROOT; - man->last->tok = MAN_MAX; - man->next = ROFF_NEXT_CHILD; -} - - static void man_node_append(struct roff_man *man, struct roff_node *p) { diff --git a/mdoc.c b/mdoc.c index 69abb376..c3c36421 100644 --- a/mdoc.c +++ b/mdoc.c @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.244 2015/04/18 17:01:58 schwarze Exp $ */ +/* $Id: mdoc.c,v 1.245 2015/04/18 17:28:36 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2012-2015 Ingo Schwarze @@ -86,8 +86,6 @@ 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 void mdoc_free1(struct roff_man *); -static void mdoc_alloc1(struct roff_man *); 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 *); @@ -109,88 +107,6 @@ mdoc_meta(const struct roff_man *mdoc) return(&mdoc->meta); } -/* - * Frees volatile resources (parse tree, meta-data, fields). - */ -static void -mdoc_free1(struct roff_man *mdoc) -{ - - if (mdoc->first) - mdoc_node_delete(mdoc, mdoc->first); - free(mdoc->meta.msec); - free(mdoc->meta.vol); - free(mdoc->meta.arch); - free(mdoc->meta.date); - free(mdoc->meta.title); - free(mdoc->meta.os); - free(mdoc->meta.name); -} - -/* - * Allocate all volatile resources (parse tree, meta-data, fields). - */ -static void -mdoc_alloc1(struct roff_man *mdoc) -{ - - memset(&mdoc->meta, 0, sizeof(mdoc->meta)); - mdoc->macroset = MACROSET_MDOC; - mdoc->flags = 0; - mdoc->lastnamed = mdoc->lastsec = SEC_NONE; - mdoc->last = mandoc_calloc(1, sizeof(*mdoc->last)); - mdoc->first = mdoc->last; - mdoc->last->type = ROFFT_ROOT; - mdoc->last->tok = MDOC_MAX; - mdoc->next = ROFF_NEXT_CHILD; -} - -/* - * Free up volatile resources (see mdoc_free1()) then re-initialises the - * data with mdoc_alloc1(). After invocation, parse data has been reset - * and the parser is ready for re-invocation on a new tree; however, - * cross-parse non-volatile data is kept intact. - */ -void -mdoc_reset(struct roff_man *mdoc) -{ - - mdoc_free1(mdoc); - mdoc_alloc1(mdoc); -} - -/* - * Completely free up all volatile and non-volatile parse resources. - * After invocation, the pointer is no longer usable. - */ -void -mdoc_free(struct roff_man *mdoc) -{ - - mdoc_free1(mdoc); - free(mdoc); -} - -/* - * Allocate volatile and non-volatile parse resources. - */ -struct roff_man * -mdoc_alloc(struct roff *roff, struct mparse *parse, - const char *defos, int quick) -{ - struct roff_man *p; - - p = mandoc_calloc(1, sizeof(*p)); - - p->parse = parse; - p->defos = defos; - p->quick = quick; - p->roff = roff; - - mdoc_alloc1(p); - return(p); -} - void mdoc_endparse(struct roff_man *mdoc) { diff --git a/read.c b/read.c index 1417aeca..6ca9232a 100644 --- a/read.c +++ b/read.c @@ -1,4 +1,4 @@ -/* $Id: read.c,v 1.136 2015/04/18 17:01:58 schwarze Exp $ */ +/* $Id: read.c,v 1.137 2015/04/18 17:28:36 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze @@ -289,26 +289,22 @@ choose_parser(struct mparse *curp) } } - if (format == MPARSE_MDOC) { - if (curp->man == NULL) - curp->man = mdoc_alloc( - curp->roff, curp, curp->defos, - MPARSE_QUICK & curp->options ? 1 : 0); - else - curp->man->macroset = MACROSET_MDOC; - mdoc_hash_init(); - return; + if (curp->man == NULL) { + curp->man = roff_man_alloc(curp->roff, curp, curp->defos, + curp->options & MPARSE_QUICK ? 1 : 0); + curp->man->macroset = MACROSET_MAN; + curp->man->first->tok = MDOC_MAX; } - /* Fall back to man(7) as a last resort. */ - - if (curp->man == NULL) - curp->man = man_alloc( - curp->roff, curp, curp->defos, - MPARSE_QUICK & curp->options ? 1 : 0); - else + if (format == MPARSE_MDOC) { + mdoc_hash_init(); + curp->man->macroset = MACROSET_MDOC; + curp->man->first->tok = MDOC_MAX; + } else { + man_hash_init(); curp->man->macroset = MACROSET_MAN; - man_hash_init(); + curp->man->first->tok = MAN_MAX; + } } /* @@ -690,7 +686,7 @@ mparse_end(struct mparse *curp) { if (curp->man == NULL && curp->sodest == NULL) - curp->man = man_alloc(curp->roff, curp, curp->defos, + curp->man = roff_man_alloc(curp->roff, curp, curp->defos, curp->options & MPARSE_QUICK ? 1 : 0); if (curp->man->macroset == MACROSET_NONE) curp->man->macroset = MACROSET_MAN; @@ -892,19 +888,17 @@ mparse_alloc(int options, enum mandoclevel wlevel, mandocmsg mmsg, curp->mchars = mchars; curp->roff = roff_alloc(curp, curp->mchars, options); + curp->man = roff_man_alloc( curp->roff, curp, curp->defos, + curp->options & MPARSE_QUICK ? 1 : 0); if (curp->options & MPARSE_MDOC) { - curp->man = mdoc_alloc( - curp->roff, curp, curp->defos, - curp->options & MPARSE_QUICK ? 1 : 0); mdoc_hash_init(); - } - if (curp->options & MPARSE_MAN) { - curp->man = man_alloc( - curp->roff, curp, curp->defos, - curp->options & MPARSE_QUICK ? 1 : 0); + curp->man->macroset = MACROSET_MDOC; + curp->man->first->tok = MDOC_MAX; + } else if (curp->options & MPARSE_MAN) { man_hash_init(); + curp->man->macroset = MACROSET_MAN; + curp->man->first->tok = MAN_MAX; } - return(curp); } @@ -914,13 +908,8 @@ mparse_reset(struct mparse *curp) roff_reset(curp->roff); - if (curp->man != NULL) { - if (curp->man->macroset == MACROSET_MDOC) - mdoc_reset(curp->man); - else - man_reset(curp->man); - curp->man->macroset = MACROSET_NONE; - } + if (curp->man != NULL) + roff_man_reset(curp->man); if (curp->secondary) curp->secondary->sz = 0; @@ -934,10 +923,7 @@ void mparse_free(struct mparse *curp) { - if (curp->man->macroset == MACROSET_MDOC) - mdoc_free(curp->man); - if (curp->man->macroset == MACROSET_MAN) - man_free(curp->man); + roff_man_free(curp->man); if (curp->roff) roff_free(curp->roff); if (curp->secondary) diff --git a/roff.c b/roff.c index 86daa3cc..5ea9478a 100644 --- a/roff.c +++ b/roff.c @@ -1,6 +1,6 @@ -/* $Id: roff.c,v 1.264 2015/04/04 13:53:42 schwarze Exp $ */ +/* $Id: roff.c,v 1.265 2015/04/18 17:28:36 schwarze Exp $ */ /* - * Copyright (c) 2010, 2011, 2012, 2014 Kristaps Dzonsons + * Copyright (c) 2009-2012, 2014 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any @@ -28,6 +28,7 @@ #include "mandoc.h" #include "mandoc_aux.h" +#include "roff.h" #include "libmandoc.h" #include "libroff.h" @@ -412,6 +413,8 @@ static const char *roff_getstrn(const struct roff *, static enum rofferr roff_insec(ROFF_ARGS); static enum rofferr roff_it(ROFF_ARGS); static enum rofferr roff_line_ignore(ROFF_ARGS); +static void roff_man_alloc1(struct roff_man *); +static void roff_man_free1(struct roff_man *); static enum rofferr roff_nr(ROFF_ARGS); static enum rofft roff_parse(struct roff *, char *, int *, int, int); @@ -898,6 +901,71 @@ roff_alloc(struct mparse *parse, const struct mchars *mchars, int options) return(r); } +static void +roff_man_free1(struct roff_man *man) +{ + + if (man->first != NULL) { + if (man->macroset == MACROSET_MDOC) + mdoc_node_delete(man, man->first); + else + man_node_delete(man, man->first); + } + free(man->meta.msec); + free(man->meta.vol); + free(man->meta.os); + free(man->meta.arch); + free(man->meta.title); + free(man->meta.name); + free(man->meta.date); +} + +static void +roff_man_alloc1(struct roff_man *man) +{ + + memset(&man->meta, 0, sizeof(man->meta)); + man->first = mandoc_calloc(1, sizeof(*man->first)); + man->first->type = ROFFT_ROOT; + man->last = man->first; + man->last_es = NULL; + man->flags = 0; + man->macroset = MACROSET_NONE; + man->lastsec = man->lastnamed = SEC_NONE; + man->next = ROFF_NEXT_CHILD; +} + +void +roff_man_reset(struct roff_man *man) +{ + + roff_man_free1(man); + roff_man_alloc1(man); +} + +void +roff_man_free(struct roff_man *man) +{ + + roff_man_free1(man); + free(man); +} + +struct roff_man * +roff_man_alloc(struct roff *roff, struct mparse *parse, + const char *defos, int quick) +{ + struct roff_man *man; + + man = mandoc_calloc(1, sizeof(*man)); + man->parse = parse; + man->roff = roff; + man->defos = defos; + man->quick = quick; + roff_man_alloc1(man); + return(man); +} + /* * In the current line, expand escape sequences that tend to get * used in numerical expressions and conditional requests. -- cgit v1.2.3-56-ge451