source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mandoc.bsd.lv
To: source@mandoc.bsd.lv
Subject: mandoc: Make the code more readable by introducing  symbolic constants
Date: Mon, 20 Jan 2020 05:37:46 -0500 (EST)	[thread overview]
Message-ID: <ba40c24740c49d25@mandoc.bsd.lv> (raw)

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

                 reply	other threads:[~2020-01-20 10:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ba40c24740c49d25@mandoc.bsd.lv \
    --to=schwarze@mandoc.bsd.lv \
    --cc=source@mandoc.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).