source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mandoc.bsd.lv
To: source@mandoc.bsd.lv
Subject: mandoc: Fold mparse_parse_buffer() into mparse_readfd(), making the code
Date: Thu, 13 Dec 2018 21:16:51 -0500 (EST)	[thread overview]
Message-ID: <05f5dfb7f79aa7a5@fantadrom.bsd.lv> (raw)

Log Message:
-----------
Fold mparse_parse_buffer() into mparse_readfd(), making the code
considerably more readable.  This is possible now that i finally
deleted mparse_readmem() from mandoc portable - an unused function
that never existed in OpenBSD.

This cleanup already made me find a minor bug: after a recursive
parse, restoring the line number of the parent file was forgotten.
This is fixed now.

Modified Files:
--------------
    mandoc:
        read.c

Revision Data
-------------
Index: read.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/read.c,v
retrieving revision 1.204
retrieving revision 1.205
diff -Lread.c -Lread.c -u -p -r1.204 -r1.205
--- read.c
+++ read.c
@@ -65,8 +65,6 @@ static	void	  resize_buf(struct buf *, s
 static	int	  mparse_buf_r(struct mparse *, struct buf, size_t, int);
 static	int	  read_whole_file(struct mparse *, int, struct buf *, int *);
 static	void	  mparse_end(struct mparse *);
-static	void	  mparse_parse_buffer(struct mparse *, struct buf,
-			const char *);
 
 
 static void
@@ -538,26 +536,42 @@ mparse_end(struct mparse *curp)
 	roff_endparse(curp->roff);
 }
 
-static void
-mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
+/*
+ * Read the whole file into memory and call the parsers.
+ * Called recursively when an .so request is encountered.
+ */
+void
+mparse_readfd(struct mparse *curp, int fd, const char *filename)
 {
-	struct buf	*svprimary;
-	const char	*svfile;
-	size_t		 offset;
 	static int	 recursion_depth;
 
-	if (64 < recursion_depth) {
+	struct buf	 blk;
+	struct buf	*save_primary;
+	const char	*save_filename;
+	size_t		 offset;
+	int		 save_filenc, save_lineno;
+	int		 with_mmap;
+
+	if (recursion_depth > 64) {
 		mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
 		return;
 	}
+	if (read_whole_file(curp, fd, &blk, &with_mmap) == 0)
+		return;
+
+	/*
+	 * Save some properties of the parent file.
+	 */
+
+	save_primary = curp->primary;
+	save_filenc = curp->filenc;
+	save_lineno = curp->line;
+	save_filename = mandoc_msg_getinfilename();
 
-	/* Line number is per-file. */
-	svfile = mandoc_msg_getinfilename();
-	mandoc_msg_setinfilename(file);
-	svprimary = curp->primary;
 	curp->primary = &blk;
+	curp->filenc = curp->options & (MPARSE_UTF8 | MPARSE_LATIN1);
 	curp->line = 1;
-	recursion_depth++;
+	mandoc_msg_setinfilename(filename);
 
 	/* Skip an UTF-8 byte order mark. */
 	if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
@@ -569,38 +583,25 @@ mparse_parse_buffer(struct mparse *curp,
 	} else
 		offset = 0;
 
+	recursion_depth++;
 	mparse_buf_r(curp, blk, offset, 1);
-
 	if (--recursion_depth == 0)
 		mparse_end(curp);
 
-	curp->primary = svprimary;
-	if (svfile != NULL)
-		mandoc_msg_setinfilename(svfile);
-}
+	/*
+	 * Clean up and restore saved parent properties.
+	 */
 
-/*
- * Read the whole file into memory and call the parsers.
- * Called recursively when an .so request is encountered.
- */
-void
-mparse_readfd(struct mparse *curp, int fd, const char *file)
-{
-	struct buf	 blk;
-	int		 with_mmap;
-	int		 save_filenc;
+	if (with_mmap)
+		munmap(blk.buf, blk.sz);
+	else
+		free(blk.buf);
 
-	if (read_whole_file(curp, fd, &blk, &with_mmap)) {
-		save_filenc = curp->filenc;
-		curp->filenc = curp->options &
-		    (MPARSE_UTF8 | MPARSE_LATIN1);
-		mparse_parse_buffer(curp, blk, file);
-		curp->filenc = save_filenc;
-		if (with_mmap)
-			munmap(blk.buf, blk.sz);
-		else
-			free(blk.buf);
-	}
+	curp->primary = save_primary;
+	curp->filenc = save_filenc;
+	curp->line = save_lineno;
+	if (save_filename != NULL)
+		mandoc_msg_setinfilename(save_filename);
 }
 
 int
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv

                 reply	other threads:[~2018-12-14  2:16 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=05f5dfb7f79aa7a5@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).