source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* docbook2mdoc: Simple warn_msg() and error_msg() functions to avoid
@ 2019-03-28 15:06 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2019-03-28 15:06 UTC (permalink / raw)
  To: source

Log Message:
-----------
Simple warn_msg() and error_msg() functions to avoid repetitive code.
While here, drop the warning about unknown attributes.
Those are just too abundant and hardly matter.

Modified Files:
--------------
    docbook2mdoc:
        parse.c

Revision Data
-------------
Index: parse.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/parse.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -Lparse.c -Lparse.c -u -p -r1.5 -r1.6
--- parse.c
+++ parse.c
@@ -17,6 +17,7 @@
  */
 #include <assert.h>
 #include <ctype.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -187,6 +188,34 @@ static	const struct element elements[] =
 	{ NULL,			NODE_IGNORE }
 };
 
+static void
+error_msg(struct parse *p, const char *fmt, ...)
+{
+	va_list		 ap;
+
+	fprintf(stderr, "%s:%d:%d: ", p->fname, p->line, p->col);
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+	fputc('\n', stderr);
+	p->tree->flags |= TREE_FAIL;
+}
+
+static void
+warn_msg(struct parse *p, const char *fmt, ...)
+{
+	va_list		 ap;
+
+	if (p->warn == 0)
+		return;
+
+	fprintf(stderr, "%s:%d:%d: warning: ", p->fname, p->line, p->col);
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+	fputc('\n', stderr);
+}
+
 /*
  * Process a string of characters.
  * If a text node is already open, append to it.
@@ -201,9 +230,7 @@ xml_char(struct parse *ps, const char *p
 		return;
 
 	if (ps->cur == NULL) {
-		fprintf(stderr, "%s:%d:%d: discarding text before docum"
-		    "ent: %.*s\n", ps->fname, ps->line, ps->col, sz, p);
-		ps->tree->flags |= TREE_FAIL;
+		error_msg(ps, "discarding text before document: %.*s", sz, p);
 		return;
 	}
 
@@ -221,10 +248,8 @@ xml_char(struct parse *ps, const char *p
 	}
 
 	if (ps->tree->flags & TREE_CLOSED &&
-	    ps->cur->parent == ps->tree->root && ps->warn)
-		fprintf(stderr, "%s:%d:%d: warning: "
-		    "text after end of document: %.*s\n",
-		    ps->fname, ps->line, ps->col, sz, p);
+	    ps->cur->parent == ps->tree->root)
+		warn_msg(ps, "text after end of document: %.*s", sz, p);
 
 	/* Append to the current text node. */
 
@@ -280,19 +305,14 @@ xml_elem_start(struct parse *ps, const c
 		if (strcmp(elem->name, name) == 0)
 			break;
 
-	if (elem->name == NULL) {
-		fprintf(stderr, "%s:%d:%d: unknown element <%s>\n",
-			ps->fname, ps->line, ps->col, name);
-		ps->tree->flags |= TREE_FAIL;
-	}
+	if (elem->name == NULL)
+		error_msg(ps, "unknown element <%s>", name);
+
 	ps->ncur = elem->node;
 
 	switch (ps->ncur) {
 	case NODE_DELETE_WARN:
-		if (ps->warn)
-			fprintf(stderr, "%s:%d:%d: warning: "
-			    "skipping element <%s>\n",
-			    ps->fname, ps->line, ps->col, name);
+		warn_msg(ps, "skipping element <%s>", name);
 		/* FALLTHROUGH */
 	case NODE_DELETE:
 		ps->del = 1;
@@ -306,11 +326,8 @@ xml_elem_start(struct parse *ps, const c
 		break;
 	}
 
-	if (ps->tree->flags & TREE_CLOSED &&
-	    ps->cur->parent == NULL && ps->warn)
-		fprintf(stderr, "%s:%d:%d: warning: "
-		    "element after end of document: %s\n",
-		    ps->fname, ps->line, ps->col, name);
+	if (ps->tree->flags & TREE_CLOSED && ps->cur->parent == NULL)
+		warn_msg(ps, "element after end of document: <%s>", name);
 
 	if ((dat = calloc(1, sizeof(*dat))) == NULL) {
 		perror(NULL);
@@ -338,10 +355,6 @@ xml_attrkey(struct parse *ps, const char
 	if (ps->del > 0 || *name == '\0')
 		return;
 	if ((key = attrkey_parse(name)) == ATTRKEY__MAX) {
-		if (ps->warn)
-			fprintf(stderr, "%s:%d:%d: warning: "
-			    "unknown attribute \"%s\"\n",
-			    ps->fname, ps->line, ps->col, name);
 		ps->attr = 0;
 		return;
 	}
@@ -415,10 +428,7 @@ xml_elem_end(struct parse *ps, const cha
 		break;
 	default:
 		if (ps->cur == NULL || node != ps->cur->node) {
-			if (ps->warn)
-				fprintf(stderr, "%s:%d:%d: warning: "
-				    "element not open: </%s>\n",
-				    ps->fname, ps->line, ps->col, name);
+			warn_msg(ps, "element not open: </%s>", name);
 			break;
 		}
 
@@ -677,8 +687,7 @@ parse_file(struct parse *p, int fd, cons
 		pnode_trim(p->cur);
 		p->cur = p->cur->parent;
 	}
-	if ((p->tree->flags & TREE_CLOSED) == 0 && p->warn)
-		fprintf(stderr, "%s:%d:%d: warning: document not closed\n",
-		    p->fname, p->line, p->col);
+	if ((p->tree->flags & TREE_CLOSED) == 0)
+		warn_msg(p, "document not closed");
 	return p->tree;
 }
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

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

only message in thread, other threads:[~2019-03-28 15:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-28 15:06 docbook2mdoc: Simple warn_msg() and error_msg() functions to avoid 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).