source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Make scan for text tokens in a line recursive.
@ 2011-07-01 10:17 kristaps
  0 siblings, 0 replies; only message in thread
From: kristaps @ 2011-07-01 10:17 UTC (permalink / raw)
  To: source

Log Message:
-----------
Make scan for text tokens in a line recursive.  This is really only for
the benefit of `Nd', which is the only [to date] node that can consist
of sub-nodes.

Modified Files:
--------------
    mdocml:
        makewhatis.c

Revision Data
-------------
Index: makewhatis.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/makewhatis.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -Lmakewhatis.c -Lmakewhatis.c -u -p -r1.12 -r1.13
--- makewhatis.c
+++ makewhatis.c
@@ -75,6 +75,8 @@ struct	buf {
 			  const struct mdoc_node *n, \
 			  const struct mdoc_meta *m
 
+static	void		  buf_appendmdoc(struct buf *, 
+				const struct mdoc_node *);
 static	void		  buf_append(struct buf *, const char *);
 static	void		  buf_appendb(struct buf *, 
 				const void *, size_t);
@@ -524,6 +526,24 @@ buf_append(struct buf *buf, const char *
 	buf_appendb(buf, cp, sz + 1);
 }
 
+/*
+ * Recursively add all text from a given node.  
+ * This is optimised for general mdoc nodes in this context, which do
+ * not consist of subexpressions and having a recursive call for n->next
+ * would be wasteful.
+ */
+static void
+buf_appendmdoc(struct buf *buf, const struct mdoc_node *n)
+{
+
+	for ( ; n; n = n->next) {
+		if (n->child)
+			buf_appendmdoc(buf, n->child);
+		if (MDOC_TEXT == n->type)
+			buf_append(buf, n->string);
+	}
+}
+
 /* ARGSUSED */
 static void
 pmdoc_An(MDOC_ARGS)
@@ -532,10 +552,7 @@ pmdoc_An(MDOC_ARGS)
 	if (SEC_AUTHORS != n->sec)
 		return;
 
-	for (n = n->child; n; n = n->next)
-		if (MDOC_TEXT == n->type)
-			buf_append(buf, n->string);
-
+	buf_appendmdoc(buf, n->child);
 	hash_put(hash, buf, TYPE_AUTHOR);
 }
 
@@ -593,10 +610,7 @@ pmdoc_Cd(MDOC_ARGS)
 	if (SEC_SYNOPSIS != n->sec)
 		return;
 
-	for (n = n->child; n; n = n->next)
-		if (MDOC_TEXT == n->type)
-			buf_append(buf, n->string);
-
+	buf_appendmdoc(buf, n->child);
 	hash_put(hash, buf, TYPE_CONFIG);
 }
 
@@ -729,24 +743,22 @@ pmdoc_Fo(MDOC_ARGS)
 static void
 pmdoc_Nd(MDOC_ARGS)
 {
-	int		 first;
 	size_t		 sz;
-	
-	for (first = 1, n = n->child; n; n = n->next) {
-		if (MDOC_TEXT != n->type)
-			continue;
 
-		if (first) {
-			sz = strlen(n->string) + 1;
-			buf_appendb(dbuf, n->string, sz);
-			buf_appendb(buf, n->string, sz);
-		} else {
-			buf_append(dbuf, n->string);
-			buf_append(buf, n->string);
-		}
+	if (MDOC_BODY != n->type)
+		return;
+	else if (NULL == (n = n->child))
+		return;
 
-		first = 0;
-	}
+	/* FIXME: don't assume this. */
+	assert(MDOC_TEXT == n->type);
+
+	sz = strlen(n->string) + 1;
+	buf_appendb(dbuf, n->string, sz);
+	buf_appendb(buf, n->string, sz);
+	
+	buf_appendmdoc(dbuf, n->next);
+	buf_appendmdoc(buf, n->next);
 
 	hash_put(hash, buf, TYPE_DESC);
 }
@@ -759,10 +771,7 @@ pmdoc_Pa(MDOC_ARGS)
 	if (SEC_FILES != n->sec)
 		return;
 	
-	for (n = n->child; n; n = n->next)
-		if (MDOC_TEXT == n->type)
-			buf_append(buf, n->string);
-
+	buf_appendmdoc(buf, n->child);
 	hash_put(hash, buf, TYPE_PATH);
 }
 
@@ -772,9 +781,7 @@ pmdoc_Nm(MDOC_ARGS)
 {
 	
 	if (SEC_NAME == n->sec) {
-		for (n = n->child; n; n = n->next)
-			if (MDOC_TEXT == n->type)
-				buf_append(buf, n->string);
+		buf_appendmdoc(buf, n->child);
 		hash_put(hash, buf, TYPE_NAME);
 		return;
 	} else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
@@ -783,10 +790,7 @@ pmdoc_Nm(MDOC_ARGS)
 	if (NULL == n->child)
 		buf_append(buf, m->name);
 
-	for (n = n->child; n; n = n->next)
-		if (MDOC_TEXT == n->type)
-			buf_append(buf, n->string);
-
+	buf_appendmdoc(buf, n->child);
 	hash_put(hash, buf, TYPE_UTILITY);
 }
 
--
 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:[~2011-07-01 10:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-01 10:17 mdocml: Make scan for text tokens in a line recursive kristaps

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).