source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* docbook2mdoc: Make the function parse_file() fit for recursion: * Save
@ 2019-04-08 22:48 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2019-04-08 22:48 UTC (permalink / raw)
  To: source

Log Message:
-----------
Make the function parse_file() fit for recursion:
* Save and restore reporting data around the read loop.
* Open the file inside the function, not before calling it.
* On the top level, change directory on a best-effort basis.
* Finalize the parse tree only on the top level.
No new functionality yet.

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

Revision Data
-------------
Index: parse.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/parse.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -Lparse.c -Lparse.c -u -p -r1.23 -r1.24
--- parse.c
+++ parse.c
@@ -17,6 +17,9 @@
  */
 #include <assert.h>
 #include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -273,6 +276,7 @@ static	const struct entity entities[] = 
 
 static size_t	 parse_string(struct parse *, char *, size_t,
 			 enum pstate *, int);
+static void	 parse_fd(struct parse *, int);
 
 
 static void
@@ -957,8 +961,18 @@ parse_string(struct parse *p, char *b, s
 	return poff;
 }
 
-struct ptree *
-parse_file(struct parse *p, int fd, const char *fname)
+
+/*
+ * The read loop.
+ * If the previous token was incomplete and asked for more input,
+ * we have to enter the read loop once more even on EOF.
+ * Once rsz is 0, incomplete tokens will no longer ask for more input
+ * but instead use whatever there is, and then exit the read loop.
+ * The minus one on the size limit for read(2) is needed such that
+ * advance() can set b[rlen] to NUL when needed.
+ */
+static void
+parse_fd(struct parse *p, int fd)
 {
 	char		 b[4096];
 	ssize_t		 rsz;	/* Return value from read(2). */
@@ -966,24 +980,8 @@ parse_file(struct parse *p, int fd, cons
 	size_t		 poff;  /* Parse offset in b[]. */
 	enum pstate	 pstate;
 
-	p->fname = fname;
-	p->nline = 1;
-	p->ncol = 1;
-	pstate = PARSE_ELEM;
 	rlen = 0;
-
-	/*
-	 * Read loop.
-	 *
-         * If the previous token was incomplete and asked for more
-         * input, we have to enter the read loop once more even on EOF.
-	 * Once rsz is 0, incomplete tokens will no longer ask
-	 * for more input but instead use whatever there is,
-	 * and then exit the read loop.
-	 * The minus one on the size limit for read(2) is needed
-	 * such that advance() can set b[rlen] to NUL when needed.
-	 */
-
+	pstate = PARSE_ELEM;
 	while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 &&
 	    (rlen += rsz) > 0) {
 		poff = parse_string(p, b, rlen, &pstate, rsz > 0);
@@ -992,13 +990,70 @@ parse_file(struct parse *p, int fd, cons
 		rlen -= poff;
 		memmove(b, b + poff, rlen);
 	}
-	if (rsz < 0) {
-		perror(fname);
-		p->tree->flags |= TREE_FAIL;
+	if (rsz < 0)
+		error_msg(p, "read: %s", strerror(errno));
+}
+
+/*
+ * Open and parse a file.
+ */
+struct ptree *
+parse_file(struct parse *p, int fd, const char *fname)
+{
+	const char	*save_fname;
+	int		 save_line, save_col;
+
+	/* Save and initialize reporting data. */
+
+	save_fname = p->fname;
+	save_line = p->nline;
+	save_col = p->ncol;
+	p->fname = fname;
+	p->line = 0;
+	p->col = 0;
+
+	/* Open the file, unless it is already open. */
+
+	if (fd == -1 && (fd = open(fname, O_RDONLY, 0)) == -1) {
+		error_msg(p, "open: %s", strerror(errno));
+		p->fname = save_fname;
+		return p->tree;
 	}
-	pnode_closetext(p);
-	if ((p->tree->flags & TREE_CLOSED) == 0)
-		warn_msg(p, "document not closed");
-	pnode_unlink(p->doctype);
+
+	/*
+	 * After opening the starting file, change to the directory it
+	 * is located in, in case it wants to include any further files,
+	 * which are typically given with relative paths in DocBook.
+	 * Do this on a best-effort basis; don't complain about failure.
+	 */
+
+	if (save_fname == NULL && (fname = dirname(fname)) != NULL &&
+	    strcmp(fname, ".") != 0)
+		(void)chdir(fname);
+
+	/* Run the read loop. */
+
+	p->nline = 1;
+	p->ncol = 1;
+	parse_fd(p, fd);
+
+	/* On the top level, finalize the parse tree. */
+
+	if (save_fname == NULL) {
+		pnode_closetext(p);
+		if (p->tree->root == NULL)
+			error_msg(p, "empty document");
+		else if ((p->tree->flags & TREE_CLOSED) == 0)
+			warn_msg(p, "document not closed");
+		pnode_unlink(p->doctype);
+	}
+
+	/* Clean up. */
+
+	if (fd != STDIN_FILENO)
+		close(fd);
+	p->fname = save_fname;
+	p->nline = save_line;
+	p->ncol = save_col;
 	return p->tree;
 }
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/docbook2mdoc/main.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -Lmain.c -Lmain.c -u -p -r1.3 -r1.4
--- main.c
+++ main.c
@@ -15,7 +15,6 @@
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
-#include <fcntl.h>
 #include <getopt.h>
 #include <stdio.h>
 #include <string.h>
@@ -64,40 +63,34 @@ main(int argc, char *argv[])
 	if (argc > 1) {
 		fprintf(stderr, "%s: Too many arguments\n", argv[1]);
 		goto usage;
-	} else
-		fname = argc > 0 ? argv[0] : "-";
+	} else if (argc == 1) {
+		fname = argv[0];
+		fd = -1;
+	} else {
+		fname = "<stdin>";
+		fd = STDIN_FILENO;
+	}
 
-	fd = strcmp(fname, "-") == 0 ?
-		STDIN_FILENO : open(fname, O_RDONLY, 0);
+	/* Parse. */
 
-	if (fd == -1) {
-		perror(fname);
+	if ((parser = parse_alloc(warn)) == NULL) {
+		perror(NULL);
 		return 1;
 	}
+	tree = parse_file(parser, fd, fname);
+	rc = tree->flags & TREE_FAIL ? 1 : 0;
 
-	/* Parse and format. */
-
-	rc = 1;
-	if ((parser = parse_alloc(warn)) != NULL) {
-		if ((tree = parse_file(parser, fd, fname)) != NULL) {
-			if (tree->flags & TREE_FAIL)
-				fputc('\n', stderr);
-			else
-				rc = 0;
-			ptree_print(tree);
-			if (tree->flags & TREE_FAIL)
-				fputs("\nThe output may be incomplete, see"
-				    " the parse error reported above.\n\n",
-				    stderr);
-			pnode_unlink(tree->root);
-			tree->root = NULL;
-		}
-		parse_free(parser);
-	} else
-		perror(NULL);
+	/* Format. */
 
-	if (fd != STDIN_FILENO)
-		close(fd);
+	if (tree->root != NULL) {
+		if (rc)
+			fputc('\n', stderr);
+		ptree_print(tree);
+		if (rc)
+			fputs("\nThe output may be incomplete, see the "
+			    "parse error reported above.\n\n", stderr);
+	}
+	parse_free(parser);
 	return rc;
 
 usage:
--
 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-04-08 22:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-08 22:48 docbook2mdoc: Make the function parse_file() fit for recursion: * Save 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).