source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: It was very surprising that a function called mparse_readfd()
@ 2016-01-08  2:14 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2016-01-08  2:14 UTC (permalink / raw)
  To: source

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

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

only message in thread, other threads:[~2016-01-08  2:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-08  2:14 mdocml: It was very surprising that a function called mparse_readfd() 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).