From 4ecbeb26f11905d17a91ae50bb0f510f59332c8b Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Sat, 18 Apr 2015 17:28:09 +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. --- usr.bin/mandoc/libman.h | 3 +- usr.bin/mandoc/libmandoc.h | 17 +++++---- usr.bin/mandoc/libmdoc.h | 3 +- usr.bin/mandoc/man.c | 64 +--------------------------------- usr.bin/mandoc/mdoc.c | 86 +--------------------------------------------- usr.bin/mandoc/read.c | 64 ++++++++++++++-------------------- usr.bin/mandoc/roff.c | 72 ++++++++++++++++++++++++++++++++++++-- 7 files changed, 107 insertions(+), 202 deletions(-) diff --git a/usr.bin/mandoc/libman.h b/usr.bin/mandoc/libman.h index 573af9d4c4d..d1448c922c3 100644 --- a/usr.bin/mandoc/libman.h +++ b/usr.bin/mandoc/libman.h @@ -1,4 +1,4 @@ -/* $OpenBSD: libman.h,v 1.47 2015/04/18 17:01:28 schwarze Exp $ */ +/* $OpenBSD: libman.h,v 1.48 2015/04/18 17:28:08 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/usr.bin/mandoc/libmandoc.h b/usr.bin/mandoc/libmandoc.h index 6034e158fa0..c2c172e1c9b 100644 --- a/usr.bin/mandoc/libmandoc.h +++ b/usr.bin/mandoc/libmandoc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: libmandoc.h,v 1.43 2015/04/18 17:01:28 schwarze Exp $ */ +/* $OpenBSD: libmandoc.h,v 1.44 2015/04/18 17:28:08 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 *); @@ -51,21 +52,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 *); @@ -78,6 +73,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/usr.bin/mandoc/libmdoc.h b/usr.bin/mandoc/libmdoc.h index 054b38f786c..7f013c8b4e9 100644 --- a/usr.bin/mandoc/libmdoc.h +++ b/usr.bin/mandoc/libmdoc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: libmdoc.h,v 1.72 2015/04/18 17:01:28 schwarze Exp $ */ +/* $OpenBSD: libmdoc.h,v 1.73 2015/04/18 17:28:08 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/usr.bin/mandoc/man.c b/usr.bin/mandoc/man.c index 62c0faea752..950e7318f95 100644 --- a/usr.bin/mandoc/man.c +++ b/usr.bin/mandoc/man.c @@ -1,4 +1,4 @@ -/* $OpenBSD: man.c,v 1.103 2015/04/18 17:01:28 schwarze Exp $ */ +/* $OpenBSD: man.c,v 1.104 2015/04/18 17:28:08 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -47,10 +47,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 *, @@ -76,38 +74,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) { @@ -127,34 +93,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/usr.bin/mandoc/mdoc.c b/usr.bin/mandoc/mdoc.c index b0ce838de8f..40162bac117 100644 --- a/usr.bin/mandoc/mdoc.c +++ b/usr.bin/mandoc/mdoc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdoc.c,v 1.133 2015/04/18 17:01:28 schwarze Exp $ */ +/* $OpenBSD: mdoc.c,v 1.134 2015/04/18 17:28:08 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2012-2015 Ingo Schwarze @@ -84,8 +84,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 *); @@ -107,88 +105,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/usr.bin/mandoc/read.c b/usr.bin/mandoc/read.c index 5d849d2fc8c..2e11570c2e5 100644 --- a/usr.bin/mandoc/read.c +++ b/usr.bin/mandoc/read.c @@ -1,4 +1,4 @@ -/* $OpenBSD: read.c,v 1.111 2015/04/18 17:01:28 schwarze Exp $ */ +/* $OpenBSD: read.c,v 1.112 2015/04/18 17:28:08 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze @@ -284,26 +284,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; + } } /* @@ -683,7 +679,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; @@ -870,19 +866,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); } @@ -892,13 +886,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; @@ -912,10 +901,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/usr.bin/mandoc/roff.c b/usr.bin/mandoc/roff.c index 32b7f1d30c4..12b4906122c 100644 --- a/usr.bin/mandoc/roff.c +++ b/usr.bin/mandoc/roff.c @@ -1,6 +1,6 @@ -/* $OpenBSD: roff.c,v 1.136 2015/04/04 13:52:59 schwarze Exp $ */ +/* $OpenBSD: roff.c,v 1.137 2015/04/18 17:28:08 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 @@ -26,6 +26,7 @@ #include "mandoc.h" #include "mandoc_aux.h" +#include "roff.h" #include "libmandoc.h" #include "libroff.h" @@ -410,6 +411,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); @@ -896,6 +899,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