source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Unify {mdoc,man}_{alloc,reset,free}() into
@ 2015-04-18 17:29 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-04-18 17:29 UTC (permalink / raw)
  To: source

Log Message:
-----------
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.

Modified Files:
--------------
    mdocml:
        libman.h
        libmandoc.h
        libmdoc.h
        man.c
        mdoc.c
        read.c
        roff.c

Revision Data
-------------
Index: read.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/read.c,v
retrieving revision 1.136
retrieving revision 1.137
diff -Lread.c -Lread.c -u -p -r1.136 -r1.137
--- read.c
+++ read.c
@@ -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 mandoclev
 
 	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)
Index: roff.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/roff.c,v
retrieving revision 1.264
retrieving revision 1.265
diff -Lroff.c -Lroff.c -u -p -r1.264 -r1.265
--- roff.c
+++ roff.c
@@ -1,6 +1,6 @@
 /*	$Id$ */
 /*
- * Copyright (c) 2010, 2011, 2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2009-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
  *
  * 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 st
 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 s
 	roffhash_init();
 
 	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);
 }
 
 /*
Index: libmdoc.h
===================================================================
RCS file: /home/cvs/mdocml/mdocml/libmdoc.h,v
retrieving revision 1.101
retrieving revision 1.102
diff -Llibmdoc.h -Llibmdoc.h -u -p -r1.101 -r1.102
--- libmdoc.h
+++ libmdoc.h
@@ -78,7 +78,6 @@ void		  mdoc_tail_alloc(struct roff_man 
 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 *);
Index: libmandoc.h
===================================================================
RCS file: /home/cvs/mdocml/mdocml/libmandoc.h,v
retrieving revision 1.57
retrieving revision 1.58
diff -Llibmandoc.h -Llibmandoc.h -u -p -r1.57 -r1.58
--- libmandoc.h
+++ libmandoc.h
@@ -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_
 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);
Index: libman.h
===================================================================
RCS file: /home/cvs/mdocml/mdocml/libman.h,v
retrieving revision 1.72
retrieving revision 1.73
diff -Llibman.h -Llibman.h -u -p -r1.72 -r1.73
--- libman.h
+++ libman.h
@@ -42,7 +42,6 @@ void		  man_block_alloc(struct roff_man 
 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 *);
Index: mdoc.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mdoc.c,v
retrieving revision 1.244
retrieving revision 1.245
diff -Lmdoc.c -Lmdoc.c -u -p -r1.244 -r1.245
--- mdoc.c
+++ mdoc.c
@@ -86,8 +86,6 @@ const	char * const *mdoc_argnames = __md
 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
Index: man.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/man.c,v
retrieving revision 1.155
retrieving revision 1.156
diff -Lman.c -Lman.c -u -p -r1.155 -r1.156
--- man.c
+++ man.c
@@ -49,10 +49,8 @@ const	char *const __man_macronames[MAN_M
 
 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 *,
@@ -79,38 +77,6 @@ man_meta(const struct roff_man *man)
 }
 
 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)
 {
 
@@ -128,34 +94,6 @@ man_parseln(struct roff_man *man, int ln
 	    man_pmacro(man, ln, buf, 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)
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-04-18 17:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-18 17:29 mdocml: Unify {mdoc,man}_{alloc,reset,free}() into schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).