source@mandoc.bsd.lv
 help / color / Atom feed
* mandoc: Make the code more readable by introducing  symbolic constants 
@ 2020-01-20 10:37 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2020-01-20 10:37 UTC (permalink / raw)
  To: source

Log Message:
-----------
Make the code more readable by introducing 
symbolic constants for tagging priorities.
This review also made me find a minor bug: do not upgrade 
TAG_FALLBACK to TAG_WEAK when there is trailing whitespace.

Modified Files:
--------------
    mandoc:
        main.c
        man_term.c
        mandoc_headers.3
        mdoc_term.c
        tag.c
        tag.h

Revision Data
-------------
Index: mdoc_term.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/mdoc_term.c,v
retrieving revision 1.375
retrieving revision 1.376
diff -Lmdoc_term.c -Lmdoc_term.c -u -p -r1.375 -r1.376
--- mdoc_term.c
+++ mdoc_term.c
@@ -249,7 +249,7 @@ static const struct mdoc_term_act mdoc_t
 	{ termp_tg_pre, NULL }, /* Tg */
 };
 
-static	int	 fn_prio;
+static	int	 fn_prio = TAG_STRONG;
 
 
 void
@@ -1294,7 +1294,7 @@ termp_sh_pre(DECL_ARGS)
 		term_tab_set(p, ".5i");
 		switch (n->sec) {
 		case SEC_DESCRIPTION:
-			fn_prio = 0;
+			fn_prio = TAG_STRONG;
 			break;
 		case SEC_AUTHORS:
 			p->flags &= ~(TERMP_SPLIT|TERMP_NOSPLIT);
@@ -1383,7 +1383,7 @@ termp_fn_pre(DECL_ARGS)
 	term_fontpop(p);
 
 	if (n->sec == SEC_DESCRIPTION || n->sec == SEC_CUSTOM)
-		tag_put(n->string, ++fn_prio, p->line);
+		tag_put(n->string, fn_prio++, p->line);
 
 	if (pretty) {
 		term_flushln(p);
@@ -1614,7 +1614,7 @@ termp_in_post(DECL_ARGS)
 static int
 termp_pp_pre(DECL_ARGS)
 {
-	fn_prio = 0;
+	fn_prio = TAG_STRONG;
 	term_vspace(p);
 	return 0;
 }
@@ -2039,7 +2039,7 @@ termp_em_pre(DECL_ARGS)
 {
 	if (n->child != NULL &&
 	    n->child->type == ROFFT_TEXT)
-		tag_put(n->child->string, 0, p->line);
+		tag_put(n->child->string, TAG_FALLBACK, p->line);
 	term_fontpush(p, TERMFONT_UNDER);
 	return 1;
 }
@@ -2049,7 +2049,7 @@ termp_sy_pre(DECL_ARGS)
 {
 	if (n->child != NULL &&
 	    n->child->type == ROFFT_TEXT)
-		tag_put(n->child->string, 0, p->line);
+		tag_put(n->child->string, TAG_FALLBACK, p->line);
 	term_fontpush(p, TERMFONT_BOLD);
 	return 1;
 }
@@ -2062,7 +2062,7 @@ termp_er_pre(DECL_ARGS)
 	    (n->parent->tok == MDOC_It ||
 	     (n->parent->tok == MDOC_Bq &&
 	      n->parent->parent->parent->tok == MDOC_It)))
-		tag_put(n->child->string, 1, p->line);
+		tag_put(n->child->string, TAG_STRONG, p->line);
 	return 1;
 }
 
@@ -2079,14 +2079,14 @@ termp_tag_pre(DECL_ARGS)
 	     (n->parent->tok == MDOC_Xo &&
 	      n->parent->parent->prev == NULL &&
 	      n->parent->parent->parent->tok == MDOC_It)))
-		tag_put(n->child->string, 1, p->line);
+		tag_put(n->child->string, TAG_STRONG, p->line);
 	return 1;
 }
 
 static int
 termp_tg_pre(DECL_ARGS)
 {
-	tag_put(n->child->string, -2, p->line);
+	tag_put(n->child->string, TAG_MANUAL, p->line);
 	return 0;
 }
 
Index: tag.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/tag.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -Ltag.c -Ltag.c -u -p -r1.26 -r1.27
--- tag.c
+++ tag.c
@@ -18,6 +18,7 @@
 
 #include <sys/types.h>
 
+#include <assert.h>
 #include <errno.h>
 #include <limits.h>
 #include <signal.h>
@@ -146,6 +147,7 @@ tag_put(const char *s, int prio, size_t 
 	size_t			 len;
 	unsigned int		 slot;
 
+	assert(prio <= TAG_FALLBACK);
 	if (tag_files.tfd <= 0)
 		return;
 
@@ -162,8 +164,8 @@ tag_put(const char *s, int prio, size_t 
 		return;
 
 	se = s + len;
-	if (*se != '\0')
-		prio = INT_MAX;
+	if (*se != '\0' && prio < TAG_WEAK)
+		prio = TAG_WEAK;
 
 	slot = ohash_qlookupi(&tag_data, s, &se);
 	entry = ohash_find(&tag_data, slot);
@@ -183,26 +185,25 @@ tag_put(const char *s, int prio, size_t 
 
 		/*
 		 * Lower priority numbers take precedence,
-		 * but 0 is special.
-		 * A tag with priority 0 is only used
+		 * but TAG_FALLBACK is special.
+		 * A tag with priority TAG_FALLBACK is only used
 		 * if the tag occurs exactly once.
 		 */
 
-		if (prio == 0) {
-			if (entry->prio == 0)
-				entry->prio = -1;
+		if (prio == TAG_FALLBACK) {
+			if (entry->prio == TAG_FALLBACK)
+				entry->prio = TAG_DELETE;
 			return;
 		}
 
 		/* A better entry is already present, ignore the new one. */
 
-		if (entry->prio != -1 && entry->prio < prio)
+		if (entry->prio < prio)
 			return;
 
 		/* The existing entry is worse, clear it. */
 
-		if (entry->prio == -1 || entry->prio == 0 ||
-		    entry->prio > prio)
+		if (entry->prio > prio)
 			entry->nlines = 0;
 	}
 
@@ -242,7 +243,7 @@ tag_write(void)
 	empty = 1;
 	entry = ohash_first(&tag_data, &slot);
 	while (entry != NULL) {
-		if (stream != NULL && entry->prio != -1) {
+		if (stream != NULL && entry->prio < TAG_DELETE) {
 			for (i = 0; i < entry->nlines; i++) {
 				fprintf(stream, "%s %s %zu\n",
 				    entry->s, tag_files.ofn, entry->lines[i]);
Index: tag.h
===================================================================
RCS file: /home/cvs/mandoc/mandoc/tag.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -Ltag.h -Ltag.h -u -p -r1.9 -r1.10
--- tag.h
+++ tag.h
@@ -1,6 +1,6 @@
 /*      $Id$    */
 /*
- * Copyright (c) 2015, 2018, 2019 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2015, 2018, 2019, 2020 Ingo Schwarze <schwarze@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -14,6 +14,17 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+
+/*
+ * Tagging priorities.
+ * Lower numbers indicate higher importance.
+ */
+#define	TAG_MANUAL	1		/* Set with a .Tg macro. */
+#define	TAG_STRONG	2		/* Good automatic tagging. */
+#define	TAG_WEAK	(INT_MAX - 2)	/* Dubious automatic tagging. */
+#define	TAG_FALLBACK	(INT_MAX - 1)	/* Tag only used if unique. */
+#define	TAG_DELETE	(INT_MAX)	/* Tag not used at all. */
+
 
 struct	tag_files {
 	char	 ofn[20];
Index: main.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/main.c,v
retrieving revision 1.340
retrieving revision 1.341
diff -Lmain.c -Lmain.c -u -p -r1.340 -r1.341
--- main.c
+++ main.c
@@ -32,6 +32,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <glob.h>
+#include <limits.h>
 #if HAVE_SANDBOX_INIT
 #include <sandbox.h>
 #endif
Index: mandoc_headers.3
===================================================================
RCS file: /home/cvs/mandoc/mandoc/mandoc_headers.3,v
retrieving revision 1.31
retrieving revision 1.32
diff -Lmandoc_headers.3 -Lmandoc_headers.3 -u -p -r1.31 -r1.32
--- mandoc_headers.3
+++ mandoc_headers.3
@@ -636,7 +636,11 @@ or
 Requires
 .In sys/types.h
 for
-.Vt size_t .
+.Vt size_t
+and
+.In limits.h
+for
+.Dv INT_MAX .
 .Pp
 Provides an interface to generate
 .Xr ctags 1
Index: man_term.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/man_term.c,v
retrieving revision 1.232
retrieving revision 1.233
diff -Lman_term.c -Lman_term.c -u -p -r1.232 -r1.233
--- man_term.c
+++ man_term.c
@@ -1,7 +1,7 @@
 /*	$Id$ */
 /*
  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2010-2015, 2017-2019 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2010-2015, 2017-2020 Ingo Schwarze <schwarze@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -1180,12 +1180,12 @@ tag_man(struct termp *p, struct roff_nod
 
 	assert(n->type == ROFFT_TEXT);
 	cp = n->string;
-	prio = 1;
+	prio = TAG_STRONG;
 	for (;;) {
 		switch (*cp) {
 		case ' ':
 		case '\t':
-			prio = INT_MAX;
+			prio = TAG_WEAK;
 			/* FALLTHROUGH */
 		case '-':
 			cp++;
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

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

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-20 10:37 mandoc: Make the code more readable by introducing symbolic constants schwarze

source@mandoc.bsd.lv

Archives are clonable: git clone --mirror http://inbox.vuxu.org/mandoc-source

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.mandoc.source


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git