source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Profit from the unified struct roff_man and reduce the number of
@ 2015-04-18 16:34 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-04-18 16:34 UTC (permalink / raw)
  To: source

Log Message:
-----------
Profit from the unified struct roff_man and reduce the number of
arguments of mparse_result() by one.  No functional change.
Written on the ICE Bruxelles-Koeln on the way back from p2k15.

Modified Files:
--------------
    mdocml:
        cgi.c
        demandoc.c
        main.c
        man.c
        mandoc.h
        mandocdb.c
        mdoc.c
        read.c
        roff.h

Revision Data
-------------
Index: read.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/read.c,v
retrieving revision 1.134
retrieving revision 1.135
diff -Lread.c -Lread.c -u -p -r1.134 -r1.135
--- read.c
+++ read.c
@@ -47,10 +47,7 @@
 #define	REPARSE_LIMIT	1000
 
 struct	mparse {
-	struct roff_man	 *pman; /* persistent man parser */
-	struct roff_man	 *pmdoc; /* persistent mdoc parser */
 	struct roff_man	 *man; /* man parser */
-	struct roff_man	 *mdoc; /* mdoc parser */
 	struct roff	 *roff; /* roff parser (!NULL) */
 	const struct mchars *mchars; /* character table */
 	char		 *sodest; /* filename pointed to by .so */
@@ -293,23 +290,23 @@ choose_parser(struct mparse *curp)
 	}
 
 	if (format == MPARSE_MDOC) {
-		if (NULL == curp->pmdoc)
-			curp->pmdoc = mdoc_alloc(
+		if (curp->man == NULL)
+			curp->man = mdoc_alloc(
 			    curp->roff, curp, curp->defos,
 			    MPARSE_QUICK & curp->options ? 1 : 0);
-		assert(curp->pmdoc);
-		curp->mdoc = curp->pmdoc;
+		else
+			curp->man->macroset = MACROSET_MDOC;
 		return;
 	}
 
 	/* Fall back to man(7) as a last resort. */
 
-	if (NULL == curp->pman)
-		curp->pman = man_alloc(
+	if (curp->man == NULL)
+		curp->man = man_alloc(
 		    curp->roff, curp, curp->defos,
 		    MPARSE_QUICK & curp->options ? 1 : 0);
-	assert(curp->pman);
-	curp->man = curp->pman;
+	else
+		curp->man->macroset = MACROSET_MAN;
 }
 
 /*
@@ -575,7 +572,8 @@ rerun:
 		 * parsers with each one.
 		 */
 
-		if ( ! (curp->man || curp->mdoc))
+		if (curp->man == NULL ||
+		    curp->man->macroset == MACROSET_NONE)
 			choose_parser(curp);
 
 		/*
@@ -589,17 +587,17 @@ rerun:
 
 		if (rr == ROFF_TBL) {
 			while ((span = roff_span(curp->roff)) != NULL)
-				if (curp->man == NULL)
-					mdoc_addspan(curp->mdoc, span);
+				if (curp->man->macroset == MACROSET_MDOC)
+					mdoc_addspan(curp->man, span);
 				else
 					man_addspan(curp->man, span);
 		} else if (rr == ROFF_EQN) {
-			if (curp->man == NULL)
-				mdoc_addeqn(curp->mdoc, roff_eqn(curp->roff));
+			if (curp->man->macroset == MACROSET_MDOC)
+				mdoc_addeqn(curp->man, roff_eqn(curp->roff));
 			else
 				man_addeqn(curp->man, roff_eqn(curp->roff));
-		} else if ((curp->man == NULL ?
-		    mdoc_parseln(curp->mdoc, curp->line, ln.buf, of) :
+		} else if ((curp->man->macroset == MACROSET_MDOC ?
+		    mdoc_parseln(curp->man, curp->line, ln.buf, of) :
 		    man_parseln(curp->man, curp->line, ln.buf, of)) == 2)
 				break;
 
@@ -689,22 +687,14 @@ static void
 mparse_end(struct mparse *curp)
 {
 
-	if (curp->mdoc == NULL &&
-	    curp->man == NULL &&
-	    curp->sodest == NULL) {
-		if (curp->options & MPARSE_MDOC)
-			curp->mdoc = curp->pmdoc;
-		else {
-			if (curp->pman == NULL)
-				curp->pman = man_alloc(
-				    curp->roff, curp, curp->defos,
-				    curp->options & MPARSE_QUICK ? 1 : 0);
-			curp->man = curp->pman;
-		}
-	}
-	if (curp->mdoc)
-		mdoc_endparse(curp->mdoc);
-	if (curp->man)
+	if (curp->man == NULL && curp->sodest == NULL)
+		curp->man = man_alloc(curp->roff, curp, curp->defos,
+		    curp->options & MPARSE_QUICK ? 1 : 0);
+	if (curp->man->macroset == MACROSET_NONE)
+		curp->man->macroset = MACROSET_MAN;
+	if (curp->man->macroset == MACROSET_MDOC)
+		mdoc_endparse(curp->man);
+	else
 		man_endparse(curp->man);
 	roff_endparse(curp->roff);
 }
@@ -901,11 +891,11 @@ mparse_alloc(int options, enum mandoclev
 	curp->mchars = mchars;
 	curp->roff = roff_alloc(curp, curp->mchars, options);
 	if (curp->options & MPARSE_MDOC)
-		curp->pmdoc = mdoc_alloc(
+		curp->man = mdoc_alloc(
 		    curp->roff, curp, curp->defos,
 		    curp->options & MPARSE_QUICK ? 1 : 0);
 	if (curp->options & MPARSE_MAN)
-		curp->pman = man_alloc(
+		curp->man = man_alloc(
 		    curp->roff, curp, curp->defos,
 		    curp->options & MPARSE_QUICK ? 1 : 0);
 
@@ -918,16 +908,17 @@ mparse_reset(struct mparse *curp)
 
 	roff_reset(curp->roff);
 
-	if (curp->mdoc)
-		mdoc_reset(curp->mdoc);
-	if (curp->man)
-		man_reset(curp->man);
+	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->secondary)
 		curp->secondary->sz = 0;
 
 	curp->file_status = MANDOCLEVEL_OK;
-	curp->mdoc = NULL;
-	curp->man = NULL;
 
 	free(curp->sodest);
 	curp->sodest = NULL;
@@ -937,10 +928,10 @@ void
 mparse_free(struct mparse *curp)
 {
 
-	if (curp->pmdoc)
-		mdoc_free(curp->pmdoc);
-	if (curp->pman)
-		man_free(curp->pman);
+	if (curp->man->macroset == MACROSET_MDOC)
+		mdoc_free(curp->man);
+	if (curp->man->macroset == MACROSET_MAN)
+		man_free(curp->man);
 	if (curp->roff)
 		roff_free(curp->roff);
 	if (curp->secondary)
@@ -952,17 +943,14 @@ mparse_free(struct mparse *curp)
 }
 
 void
-mparse_result(struct mparse *curp, struct roff_man **mdoc,
-	struct roff_man **man, char **sodest)
+mparse_result(struct mparse *curp, struct roff_man **man,
+	char **sodest)
 {
 
 	if (sodest && NULL != (*sodest = curp->sodest)) {
-		*mdoc = NULL;
 		*man = NULL;
 		return;
 	}
-	if (mdoc)
-		*mdoc = curp->mdoc;
 	if (man)
 		*man = curp->man;
 }
Index: demandoc.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/demandoc.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -Ldemandoc.c -Ldemandoc.c -u -p -r1.18 -r1.19
--- demandoc.c
+++ demandoc.c
@@ -110,21 +110,20 @@ usage(void)
 static void
 pmandoc(struct mparse *mp, int fd, const char *fn, int list)
 {
-	struct roff_man	*mdoc;
 	struct roff_man	*man;
 	int		 line, col;
 
 	mparse_readfd(mp, fd, fn);
-	mparse_result(mp, &mdoc, &man, NULL);
+	mparse_result(mp, &man, NULL);
 	line = 1;
 	col = 0;
 
-	if (mdoc)
-		pmdoc(mdoc_node(mdoc), &line, &col, list);
-	else if (man)
-		pman(man_node(man), &line, &col, list);
-	else
+	if (man == NULL)
 		return;
+	if (man->macroset == MACROSET_MDOC)
+		pmdoc(mdoc_node(man), &line, &col, list);
+	else
+		pman(man_node(man), &line, &col, list);
 
 	if ( ! list)
 		putchar('\n');
Index: mandocdb.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandocdb.c,v
retrieving revision 1.191
retrieving revision 1.192
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.191 -r1.192
--- mandocdb.c
+++ mandocdb.c
@@ -1107,7 +1107,6 @@ mpages_merge(struct mparse *mp)
 	struct ohash_info	 str_info;
 	struct mpage		*mpage, *mpage_dest;
 	struct mlink		*mlink, *mlink_dest;
-	struct roff_man		*mdoc;
 	struct roff_man		*man;
 	char			*sodest;
 	char			*cp;
@@ -1135,7 +1134,6 @@ mpages_merge(struct mparse *mp)
 		ohash_init(&names, 4, &str_info);
 		ohash_init(&strings, 6, &str_info);
 		mparse_reset(mp);
-		mdoc = NULL;
 		man = NULL;
 		sodest = NULL;
 
@@ -1151,7 +1149,7 @@ mpages_merge(struct mparse *mp)
 		 */
 		if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) {
 			mparse_readfd(mp, fd, mlink->file);
-			mparse_result(mp, &mdoc, &man, &sodest);
+			mparse_result(mp, &man, &sodest);
 		}
 
 		if (sodest != NULL) {
@@ -1195,17 +1193,17 @@ mpages_merge(struct mparse *mp)
 				mpage->mlinks = NULL;
 			}
 			goto nextpage;
-		} else if (mdoc != NULL) {
+		} else if (man != NULL && man->macroset == MACROSET_MDOC) {
 			mpage->form = FORM_SRC;
-			mpage->sec = mdoc_meta(mdoc)->msec;
+			mpage->sec = mdoc_meta(man)->msec;
 			mpage->sec = mandoc_strdup(
 			    mpage->sec == NULL ? "" : mpage->sec);
-			mpage->arch = mdoc_meta(mdoc)->arch;
+			mpage->arch = mdoc_meta(man)->arch;
 			mpage->arch = mandoc_strdup(
 			    mpage->arch == NULL ? "" : mpage->arch);
 			mpage->title =
-			    mandoc_strdup(mdoc_meta(mdoc)->title);
-		} else if (man != NULL) {
+			    mandoc_strdup(mdoc_meta(man)->title);
+		} else if (man != NULL && man->macroset == MACROSET_MAN) {
 			mpage->form = FORM_SRC;
 			mpage->sec = mandoc_strdup(man_meta(man)->msec);
 			mpage->arch = mandoc_strdup(mlink->arch);
@@ -1231,8 +1229,8 @@ mpages_merge(struct mparse *mp)
 		}
 
 		assert(mpage->desc == NULL);
-		if (mdoc != NULL)
-			parse_mdoc(mpage, mdoc_meta(mdoc), mdoc_node(mdoc));
+		if (man != NULL && man->macroset == MACROSET_MDOC)
+			parse_mdoc(mpage, mdoc_meta(man), mdoc_node(man));
 		else if (man != NULL)
 			parse_man(mpage, man_meta(man), man_node(man));
 		else
Index: mdoc.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mdoc.c,v
retrieving revision 1.242
retrieving revision 1.243
diff -Lmdoc.c -Lmdoc.c -u -p -r1.242 -r1.243
--- mdoc.c
+++ mdoc.c
@@ -135,6 +135,7 @@ 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));
Index: roff.h
===================================================================
RCS file: /home/cvs/mdocml/mdocml/roff.h,v
retrieving revision 1.31
retrieving revision 1.32
diff -Lroff.h -Lroff.h -u -p -r1.31 -r1.32
--- roff.h
+++ roff.h
@@ -19,6 +19,12 @@
 struct	mdoc_arg;
 union	mdoc_data;
 
+enum	roff_macroset {
+	MACROSET_NONE = 0,
+	MACROSET_MDOC,
+	MACROSET_MAN
+};
+
 enum	roff_sec {
 	SEC_NONE = 0,
 	SEC_NAME,
@@ -145,6 +151,7 @@ struct	roff_man {
 #define	MAN_BLINE	 (1 << 12) /* Next-line block scope. */
 #define	MAN_LITERAL	  MDOC_LITERAL
 #define	MAN_NEWLINE	  MDOC_NEWLINE
+	enum roff_macroset macroset; /* Kind of high-level macros used. */
 	enum roff_sec	  lastsec; /* Last section seen. */
 	enum roff_sec	  lastnamed; /* Last standard section seen. */
 	enum roff_next	  next;    /* Where to put the next node. */
Index: man.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/man.c,v
retrieving revision 1.153
retrieving revision 1.154
diff -Lman.c -Lman.c -u -p -r1.153 -r1.154
--- man.c
+++ man.c
@@ -149,6 +149,7 @@ 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;
Index: cgi.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/cgi.c,v
retrieving revision 1.107
retrieving revision 1.108
diff -Lcgi.c -Lcgi.c -u -p -r1.107 -r1.108
--- cgi.c
+++ cgi.c
@@ -30,8 +30,9 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "mandoc.h"
 #include "mandoc_aux.h"
+#include "mandoc.h"
+#include "roff.h"
 #include "main.h"
 #include "manconf.h"
 #include "mansearch.h"
@@ -819,7 +820,6 @@ format(const struct req *req, const char
 	struct manoutput conf;
 	struct mparse	*mp;
 	struct mchars	*mchars;
-	struct roff_man	*mdoc;
 	struct roff_man	*man;
 	void		*vp;
 	int		 fd;
@@ -846,8 +846,8 @@ format(const struct req *req, const char
 	    usepath	? "&manpath="    : "",
 	    usepath	? req->q.manpath : "");
 
-	mparse_result(mp, &mdoc, &man, NULL);
-	if (NULL == man && NULL == mdoc) {
+	mparse_result(mp, &man, NULL);
+	if (man == NULL) {
 		fprintf(stderr, "fatal mandoc error: %s/%s\n",
 		    req->q.manpath, file);
 		pg_error_internal();
@@ -858,8 +858,8 @@ format(const struct req *req, const char
 
 	vp = html_alloc(mchars, &conf);
 
-	if (NULL != mdoc)
-		html_mdoc(vp, mdoc);
+	if (man->macroset == MACROSET_MDOC)
+		html_mdoc(vp, man);
 	else
 		html_man(vp, man);
 
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/main.c,v
retrieving revision 1.234
retrieving revision 1.235
diff -Lmain.c -Lmain.c -u -p -r1.234 -r1.235
--- main.c
+++ main.c
@@ -632,7 +632,6 @@ static void
 parse(struct curparse *curp, int fd, const char *file)
 {
 	enum mandoclevel  rctmp;
-	struct roff_man	 *mdoc;
 	struct roff_man	 *man;
 
 	/* Begin by parsing the file itself. */
@@ -720,14 +719,16 @@ parse(struct curparse *curp, int fd, con
 		}
 	}
 
-	mparse_result(curp->mp, &mdoc, &man, NULL);
+	mparse_result(curp->mp, &man, NULL);
 
 	/* Execute the out device, if it exists. */
 
-	if (man && curp->outman)
+	if (man == NULL)
+		return;
+	if (curp->outmdoc != NULL && man->macroset == MACROSET_MDOC)
+		(*curp->outmdoc)(curp->outdata, man);
+	if (curp->outman != NULL && man->macroset == MACROSET_MAN)
 		(*curp->outman)(curp->outdata, man);
-	if (mdoc && curp->outmdoc)
-		(*curp->outmdoc)(curp->outdata, mdoc);
 }
 
 static void
Index: mandoc.h
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandoc.h,v
retrieving revision 1.202
retrieving revision 1.203
diff -Lmandoc.h -Lmandoc.h -u -p -r1.202 -r1.203
--- mandoc.h
+++ mandoc.h
@@ -433,7 +433,7 @@ enum mandoclevel  mparse_readfd(struct m
 enum mandoclevel  mparse_readmem(struct mparse *, void *, size_t,
 			const char *);
 void		  mparse_reset(struct mparse *);
-void		  mparse_result(struct mparse *, struct roff_man **,
+void		  mparse_result(struct mparse *,
 			struct roff_man **, char **);
 const char	 *mparse_getkeep(const struct mparse *);
 const char	 *mparse_strerror(enum mandocerr);
--
 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 16:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-18 16:34 mdocml: Profit from the unified struct roff_man and reduce the number of 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).