source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: We can keep track of the pager PID without additional
Date: Tue, 10 Mar 2015 08:50:33 -0500 (EST)	[thread overview]
Message-ID: <5432139736324535464.enqueue@fantadrom.bsd.lv> (raw)

Log Message:
-----------
We can keep track of the pager PID without additional complexity.
No functional change for now, but more robust in case anybody should
ever add additional child processes.

Modified Files:
--------------
    mdocml:
        main.c

Revision Data
-------------
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/main.c,v
retrieving revision 1.224
retrieving revision 1.225
diff -Lmain.c -Lmain.c -u -p -r1.224 -r1.225
--- main.c
+++ main.c
@@ -102,7 +102,7 @@ static	void		  mmsg(enum mandocerr, enum
 static	void		  parse(struct curparse *, int,
 				const char *, enum mandoclevel *);
 static	enum mandoclevel  passthrough(const char *, int, int);
-static	void		  spawn_pager(void);
+static	pid_t		  spawn_pager(void);
 static	int		  toptions(struct curparse *, char *);
 static	void		  usage(enum argmode) __attribute__((noreturn));
 static	int		  woptions(struct curparse *, char *);
@@ -131,9 +131,9 @@ main(int argc, char *argv[])
 	enum outmode	 outmode;
 	int		 fd;
 	int		 show_usage;
-	int		 use_pager;
 	int		 options;
 	int		 c;
+	pid_t		 pager_pid;  /* 0: don't use; 1: not yet spawned. */
 
 	if (argc < 1)
 		progname = "mandoc";
@@ -175,7 +175,7 @@ main(int argc, char *argv[])
 	options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
 	defos = NULL;
 
-	use_pager = 1;
+	pager_pid = 1;
 	show_usage = 0;
 	synopsis_only = 0;
 	outmode = OUTMODE_DEF;
@@ -190,7 +190,7 @@ main(int argc, char *argv[])
 			conf_file = optarg;
 			break;
 		case 'c':
-			use_pager = 0;
+			pager_pid = 0;
 			break;
 		case 'f':
 			search.argmode = ARG_WORD;
@@ -198,7 +198,7 @@ main(int argc, char *argv[])
 		case 'h':
 			(void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
 			synopsis_only = 1;
-			use_pager = 0;
+			pager_pid = 0;
 			outmode = OUTMODE_ALL;
 			break;
 		case 'I':
@@ -273,7 +273,7 @@ main(int argc, char *argv[])
 		switch (search.argmode) {
 		case ARG_FILE:
 			outmode = OUTMODE_ALL;
-			use_pager = 0;
+			pager_pid = 0;
 			break;
 		case ARG_NAME:
 			outmode = OUTMODE_ONE;
@@ -418,8 +418,8 @@ main(int argc, char *argv[])
 		mparse_keep(curp.mp);
 
 	if (argc < 1) {
-		if (use_pager && isatty(STDOUT_FILENO))
-			spawn_pager();
+		if (pager_pid == 1 && isatty(STDOUT_FILENO))
+			pager_pid = spawn_pager();
 		parse(&curp, STDIN_FILENO, "<stdin>", &rc);
 	}
 
@@ -430,9 +430,8 @@ main(int argc, char *argv[])
 			rc = rctmp;
 
 		if (fd != -1) {
-			if (use_pager && isatty(STDOUT_FILENO))
-				spawn_pager();
-			use_pager = 0;
+			if (pager_pid == 1 && isatty(STDOUT_FILENO))
+				pager_pid = spawn_pager();
 
 			if (resp == NULL)
 				parse(&curp, fd, *argv, &rc);
@@ -483,19 +482,15 @@ out:
 	free(defos);
 
 	/*
-	 * Flush the output and signal end of file.
-	 * If a pager is attached, it allows browsing to the end.
-	 * Otherwise, it does no harm, we are about to exit anyway.
+	 * If a pager is attached, flush the pipe leading to it
+	 * and signal end of file such that the user can browse
+	 * to the end.  Then wait for the user to close the pager.
 	 */
 
-	fclose(stdout);
-
-	/*
-	 * If we spawned a pager, wait for the user to close it.
-	 * Otherwise, this call fails with no adverse effect.
-	 */
-
-	wait(NULL);
+	if (pager_pid != 0 && pager_pid != 1) {
+		fclose(stdout);
+		waitpid(pager_pid, NULL, 0);
+	}
 
 	return((int)rc);
 }
@@ -945,7 +940,7 @@ mmsg(enum mandocerr t, enum mandoclevel 
 	fputc('\n', stderr);
 }
 
-static void
+static pid_t
 spawn_pager(void)
 {
 #define MAX_PAGER_ARGS 16
@@ -954,14 +949,15 @@ spawn_pager(void)
 	char		*cp;
 	int		 fildes[2];
 	int		 argc;
+	pid_t		 pager_pid;
 
 	if (pipe(fildes) == -1) {
 		fprintf(stderr, "%s: pipe: %s\n",
 		    progname, strerror(errno));
-		return;
+		return(0);
 	}
 
-	switch (fork()) {
+	switch (pager_pid = fork()) {
 	case -1:
 		fprintf(stderr, "%s: fork: %s\n",
 		    progname, strerror(errno));
@@ -976,7 +972,7 @@ spawn_pager(void)
 			exit((int)MANDOCLEVEL_SYSERR);
 		}
 		close(fildes[1]);
-		return;
+		return(pager_pid);
 	}
 
 	/* The child process becomes the pager. */
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2015-03-10 13:50 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=5432139736324535464.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).