source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mandoc.bsd.lv
To: source@mandoc.bsd.lv
Subject: docbook2mdoc: Implement lint and tree dump output modes.
Date: Fri, 12 Apr 2019 14:15:21 -0500 (EST)	[thread overview]
Message-ID: <e3fdbaad1f9d96bb@fantadrom.bsd.lv> (raw)

Log Message:
-----------
Implement lint and tree dump output modes.
Thanks to the previously committed node property infrastructure 
in node.c, this needs only 110 lines of code (including the license
and the documentation).

Modified Files:
--------------
    docbook2mdoc:
        Makefile
        docbook2mdoc.1
        docbook2mdoc.c
        format.h
        main.c
        node.c
        node.h

Added Files:
-----------
    docbook2mdoc:
        tree.c

Revision Data
-------------
Index: Makefile
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/Makefile,v
retrieving revision 1.23
retrieving revision 1.24
diff -LMakefile -LMakefile -u -p -r1.23 -r1.24
--- Makefile
+++ Makefile
@@ -4,8 +4,8 @@ WWWPREFIX = /usr/vhosts/mdocml.bsd.lv/ww
 PREFIX = /usr/local
 
 HEADS =	node.h parse.h macro.h format.h
-SRCS =	node.c parse.c macro.c docbook2mdoc.c main.c
-OBJS =	node.o parse.o macro.o docbook2mdoc.o main.o
+SRCS =	node.c parse.c macro.c docbook2mdoc.c tree.c main.c
+OBJS =	node.o parse.o macro.o docbook2mdoc.o tree.o main.o
 
 all: docbook2mdoc
 
@@ -42,6 +42,7 @@ node.o: node.h
 parse.o: node.h parse.h
 macro.o: node.h macro.h
 docbook2mdoc.o: node.h macro.h format.h
+tree.o: node.h format.h
 main.o: node.h parse.h format.h
 
 index.html: index.xml
--- /dev/null
+++ tree.c
@@ -0,0 +1,49 @@
+/* $Id: tree.c,v 1.1 2019/04/12 19:14:50 schwarze Exp $ */
+/*
+ * Copyright (c) 2019 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
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#include <stdio.h>
+
+#include "node.h"
+#include "format.h"
+
+/*
+ * The implementation of the parse tree dumper.
+ */
+
+void
+print_node(struct pnode *n, int indent)
+{
+	struct pnode	*nc;
+	struct pattr	*a;
+
+	printf("%*s%c%s", indent, "", n->spc ? ' ' : '-',
+	    pnode_name(n->node));
+	if (pnode_class(n->node) == CLASS_TEXT) {
+		putchar(' ');
+		fputs(n->b, stdout);
+	}
+	TAILQ_FOREACH(a, &n->attrq, child)
+		printf(" %s='%s'", attrkey_name(a->key), attr_getval(a));
+	putchar('\n');
+	TAILQ_FOREACH(nc, &n->childq, child)
+		print_node(nc, indent + 2);
+}
+
+void
+ptree_print_tree(struct ptree *tree)
+{
+	print_node(tree->root, 0);
+}
Index: docbook2mdoc.1
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/docbook2mdoc.1,v
retrieving revision 1.11
retrieving revision 1.12
diff -Ldocbook2mdoc.1 -Ldocbook2mdoc.1 -u -p -r1.11 -r1.12
--- docbook2mdoc.1
+++ docbook2mdoc.1
@@ -24,6 +24,7 @@
 .Sh SYNOPSIS
 .Nm docbook2mdoc
 .Op Fl W
+.Op Fl T Cm mdoc | tree | lint
 .Op Ar file
 .Sh DESCRIPTION
 The
@@ -40,8 +41,36 @@ is
 .Sq Cm \-
 or omitted, standard input is used.
 .Pp
-The arguments are as follows:
-.Bl -tag -width Ds
+The options are as follows:
+.Bl -tag -width 2n
+.It Fl T
+Select the output mode.
+The following arguments are supported:
+.Bl -tag -width 4n
+.It Cm mdoc
+Translate the input to
+.Xr mdoc 7 .
+This is the default.
+.It Cm tree
+Dump a human-readable representation of the parse tree.
+Each output line shows one tree node.
+Child nodes are indented with respect to their parent node.
+The columns are:
+.Bl -enum -width 2n
+.It
+A hyphen if the node follows the previous node
+without intervening whitespace.
+.It
+The node type.
+.It
+For text nodes, the text contents.
+For other nodes, the attributes, if any.
+.El
+.It Cm lint
+Do not produce any output, only error messages.
+Can be combined with
+.Fl W .
+.El
 .It Fl W
 Report warnings on standard error output, and if any occur, raise the
 .Sx EXIT STATUS
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/main.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -Lmain.c -Lmain.c -u -p -r1.5 -r1.6
--- main.c
+++ main.c
@@ -28,6 +28,12 @@
  * The steering function of the docbook2mdoc(1) program.
  */
 
+enum	outt {
+	OUTT_MDOC = 0,
+	OUTT_TREE,
+	OUTT_LINT
+};
+
 int
 main(int argc, char *argv[])
 {
@@ -36,6 +42,7 @@ main(int argc, char *argv[])
 	const char	*progname;
 	const char	*fname;
 	int		 ch, fd, rc, warn;
+	enum outt	 outtype;
 
 	if ((progname = strrchr(argv[0], '/')) == NULL)
 		progname = argv[0];
@@ -43,8 +50,22 @@ main(int argc, char *argv[])
 		progname++;
 
 	warn = 0;
-	while ((ch = getopt(argc, argv, "W")) != -1) {
+	outtype = OUTT_MDOC;
+	while ((ch = getopt(argc, argv, "T:W")) != -1) {
 		switch (ch) {
+		case 'T':
+			if (strcmp(optarg, "mdoc") == 0)
+				outtype = OUTT_MDOC;
+			else if (strcmp(optarg, "tree") == 0)
+				outtype = OUTT_TREE;
+			else if (strcmp(optarg, "lint") == 0)
+				outtype = OUTT_LINT;
+			else {
+				fprintf(stderr, "%s: Bad argument\n",
+				    optarg);
+				goto usage;
+			}
+			break;
 		case 'W':
 			warn = 1;
 			break;
@@ -82,10 +103,13 @@ main(int argc, char *argv[])
 
 	/* Format. */
 
-	if (tree->root != NULL) {
+	if (outtype != OUTT_LINT && tree->root != NULL) {
 		if (rc > 2)
 			fputc('\n', stderr);
-		ptree_print(tree);
+		if (outtype == OUTT_MDOC)
+			ptree_print_mdoc(tree);
+		else
+			ptree_print_tree(tree);
 		if (rc > 2)
 			fputs("\nThe output may be incomplete, see the "
 			    "parse error reported above.\n\n", stderr);
@@ -94,6 +118,7 @@ main(int argc, char *argv[])
 	return rc;
 
 usage:
-	fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname);
+	fprintf(stderr, "usage: %s [-W] [-T mdoc | tree | lint] "
+	    "[input_filename]\n", progname);
 	return 5;
 }
Index: format.h
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/format.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lformat.h -Lformat.h -u -p -r1.1 -r1.2
--- format.h
+++ format.h
@@ -19,4 +19,5 @@
  * The interface of the mdoc(7) formatter.
  */
 
-void	 ptree_print(struct ptree *);
+void	 ptree_print_mdoc(struct ptree *);
+void	 ptree_print_tree(struct ptree *);
Index: node.h
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/node.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -Lnode.h -Lnode.h -u -p -r1.20 -r1.21
--- node.h
+++ node.h
@@ -238,7 +238,9 @@ struct	ptree {
 
 
 enum attrkey	 attrkey_parse(const char *);
+const char	*attrkey_name(enum attrkey);
 enum attrval	 attrval_parse(const char *);
+const char	*attr_getval(const struct pattr *a);
 enum nodeid	 pnode_parse(const char *name);
 const char	*pnode_name(enum nodeid);
 enum nodeclass	 pnode_class(enum nodeid);
Index: node.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/node.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -Lnode.c -Lnode.c -u -p -r1.10 -r1.11
--- node.c
+++ node.c
@@ -144,8 +144,8 @@ static	const struct nodeprop properties[
 	{ "wordasword",		CLASS_TRANS },
 	{ "year",		CLASS_TRANS },
 	{ "[UNKNOWN]",		CLASS_VOID },
-	{ "[TEXT]",		CLASS_TEXT },
-	{ "[ESCAPE]",		CLASS_TEXT }
+	{ "(t)",		CLASS_TEXT },
+	{ "(e)",		CLASS_TEXT }
 };
 
 static	const char *const attrkeys[ATTRKEY__MAX] = {
@@ -187,6 +187,12 @@ attrkey_parse(const char *name)
 	return key;
 }
 
+const char *
+attrkey_name(enum attrkey key)
+{
+	return attrkeys[key];
+}
+
 enum attrval
 attrval_parse(const char *name)
 {
@@ -196,6 +202,12 @@ attrval_parse(const char *name)
 		if (strcmp(name, attrvals[val]) == 0)
 			break;
 	return val;
+}
+
+const char *
+attr_getval(const struct pattr *a)
+{
+	return a->val == ATTRVAL__MAX ? a->rawval : attrvals[a->val];
 }
 
 enum nodeid
Index: docbook2mdoc.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/docbook2mdoc.c,v
retrieving revision 1.113
retrieving revision 1.114
diff -Ldocbook2mdoc.c -Ldocbook2mdoc.c -u -p -r1.113 -r1.114
--- docbook2mdoc.c
+++ docbook2mdoc.c
@@ -1133,7 +1133,7 @@ pnode_print(struct format *f, struct pno
 }
 
 void
-ptree_print(struct ptree *tree)
+ptree_print_mdoc(struct ptree *tree)
 {
 	struct format	 formatter;
 
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

                 reply	other threads:[~2019-04-12 19:15 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=e3fdbaad1f9d96bb@fantadrom.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).