=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mandoc/main.c,v retrieving revision 1.129 retrieving revision 1.130 diff -u -r1.129 -r1.130 --- src/usr.bin/mandoc/main.c 2015/03/10 03:00:48 1.129 +++ src/usr.bin/mandoc/main.c 2015/03/10 13:48:57 1.130 @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.129 2015/03/10 03:00:48 schwarze Exp $ */ +/* $OpenBSD: main.c,v 1.130 2015/03/10 13:48:57 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze @@ -93,7 +93,7 @@ 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 *); @@ -122,9 +122,9 @@ 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"; @@ -165,7 +165,7 @@ options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1; defos = NULL; - use_pager = 1; + pager_pid = 1; show_usage = 0; synopsis_only = 0; outmode = OUTMODE_DEF; @@ -180,7 +180,7 @@ conf_file = optarg; break; case 'c': - use_pager = 0; + pager_pid = 0; break; case 'f': search.argmode = ARG_WORD; @@ -188,7 +188,7 @@ case 'h': (void)strlcat(curp.outopts, "synopsis,", BUFSIZ); synopsis_only = 1; - use_pager = 0; + pager_pid = 0; outmode = OUTMODE_ALL; break; case 'I': @@ -263,7 +263,7 @@ switch (search.argmode) { case ARG_FILE: outmode = OUTMODE_ALL; - use_pager = 0; + pager_pid = 0; break; case ARG_NAME: outmode = OUTMODE_ONE; @@ -397,8 +397,8 @@ 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, "", &rc); } @@ -409,9 +409,8 @@ 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); @@ -460,20 +459,16 @@ 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 (pager_pid != 0 && pager_pid != 1) { + fclose(stdout); + waitpid(pager_pid, NULL, 0); + } - /* - * If we spawned a pager, wait for the user to close it. - * Otherwise, this call fails with no adverse effect. - */ - - wait(NULL); - return((int)rc); } @@ -920,7 +915,7 @@ fputc('\n', stderr); } -static void +static pid_t spawn_pager(void) { #define MAX_PAGER_ARGS 16 @@ -929,14 +924,15 @@ 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)); @@ -951,7 +947,7 @@ exit((int)MANDOCLEVEL_SYSERR); } close(fildes[1]); - return; + return(pager_pid); } /* The child process becomes the pager. */