source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: It was very surprising that a function called mparse_readfd()
Date: Thu, 7 Jan 2016 21:14:09 -0500 (EST)	[thread overview]
Message-ID: <12385450609235028203.enqueue@fantadrom.bsd.lv> (raw)

Log Message:
-----------
It was very surprising that a function called mparse_readfd()
closed the file descriptor passed to it after completing its work,
in particular considering the fact that it required its callers
to call open(2) or mparse_open() beforehand.

Change mparse_readfd() to not call close(2) and change the callers
to call close(2) afterwards, more or less bringing open and close
to the same level of the code and making review easier.  Note that
man.cgi(8) already did that, even though it was wrong in the past.

Small restructuring suggested by Christos Zoulas (NetBSD).

Modified Files:
--------------
    mdocml:
        demandoc.c
        main.c
        mandoc.3
        mandocdb.c
        read.c

Revision Data
-------------
Index: read.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/read.c,v
retrieving revision 1.146
retrieving revision 1.147
diff -Lread.c -Lread.c -u -p -r1.146 -r1.147
--- read.c
+++ read.c
@@ -542,6 +542,7 @@ rerun:
 			if (mparse_open(curp, &fd, ln.buf + of) ==
 			    MANDOCLEVEL_OK) {
 				mparse_readfd(curp, fd, ln.buf + of);
+				close(fd);
 				curp->file = save_file;
 			} else {
 				curp->file = save_file;
@@ -773,10 +774,6 @@ mparse_readfd(struct mparse *curp, int f
 #endif
 			free(blk.buf);
 	}
-
-	if (fd != STDIN_FILENO && close(fd) == -1)
-		perror(file);
-
 	return curp->file_status;
 }
 
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/main.c,v
retrieving revision 1.260
retrieving revision 1.261
diff -Lmain.c -Lmain.c -u -p -r1.260 -r1.261
--- main.c
+++ main.c
@@ -724,9 +724,11 @@ parse(struct curparse *curp, int fd, con
 	/* Begin by parsing the file itself. */
 
 	assert(file);
-	assert(fd >= -1);
+	assert(fd > 0);
 
 	rctmp = mparse_readfd(curp->mp, fd, file);
+	if (fd != STDIN_FILENO)
+		close(fd);
 	if (rc < rctmp)
 		rc = rctmp;
 
Index: mandocdb.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandocdb.c,v
retrieving revision 1.211
retrieving revision 1.212
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.211 -r1.212
--- mandocdb.c
+++ mandocdb.c
@@ -1146,6 +1146,7 @@ mpages_merge(struct mparse *mp)
 		 */
 		if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) {
 			mparse_readfd(mp, fd, mlink->file);
+			close(fd);
 			mparse_result(mp, &man, &sodest);
 		}
 
Index: mandoc.3
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandoc.3,v
retrieving revision 1.33
retrieving revision 1.34
diff -Lmandoc.3 -Lmandoc.3 -u -p -r1.33 -r1.34
--- mandoc.3
+++ mandoc.3
@@ -177,6 +177,9 @@ or
 parse it with
 .Fn mparse_readfd ;
 .It
+close it with
+.Xr close 2 ;
+.It
 retrieve the syntax tree with
 .Fn mparse_result ;
 .It
@@ -403,6 +406,8 @@ or
 Pass the associated filename in
 .Va fname .
 This function may be called multiple times with different parameters; however,
+.Xr close 2
+and
 .Fn mparse_reset
 should be invoked between parses.
 Declared in
Index: demandoc.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/demandoc.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -Ldemandoc.c -Ldemandoc.c -u -p -r1.24 -r1.25
--- demandoc.c
+++ demandoc.c
@@ -113,6 +113,7 @@ pmandoc(struct mparse *mp, int fd, const
 	int		 line, col;
 
 	mparse_readfd(mp, fd, fn);
+	close(fd);
 	mparse_result(mp, &man, NULL);
 	line = 1;
 	col = 0;
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2016-01-08  2:14 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=12385450609235028203.enqueue@fantadrom.bsd.lv \
    --to=schwarze@mdocml.bsd.lv \
    --cc=source@mdocml.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).