From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from scc-mailout.scc.kit.edu (scc-mailout.scc.kit.edu [129.13.185.201]) by krisdoz.my.domain (8.14.3/8.14.3) with ESMTP id p8PIFnXx014171 for ; Sun, 25 Sep 2011 14:15:50 -0400 (EDT) Received: from hekate.usta.de (asta-nat.asta.uni-karlsruhe.de [172.22.63.82]) by scc-mailout-01.scc.kit.edu with esmtp (Exim 4.72 #1) id 1R7tEw-0003ms-VC; Sun, 25 Sep 2011 20:15:46 +0200 Received: from donnerwolke.usta.de ([172.24.96.3]) by hekate.usta.de with esmtp (Exim 4.72) (envelope-from ) id 1R7tEx-0001Pf-0e; Sun, 25 Sep 2011 20:15:47 +0200 Received: from iris.usta.de ([172.24.96.5] helo=usta.de) by donnerwolke.usta.de with esmtp (Exim 4.69) (envelope-from ) id 1R7tEw-0001E3-Uq; Sun, 25 Sep 2011 20:15:46 +0200 Received: from schwarze by usta.de with local (Exim 4.72) (envelope-from ) id 1R7t23-0001VD-Kn; Sun, 25 Sep 2011 20:02:27 +0200 Date: Sun, 25 Sep 2011 20:02:27 +0200 From: Ingo Schwarze To: discuss@mdocml.bsd.lv Cc: espie@openbsd.org, naddy@openbsd.org Subject: Let mandoc pipe to less? Message-ID: <20110925180227.GJ4867@iris.usta.de> References: <20110921122358.1e15e3f2@greg.bestnet.kharkov.ua> <4E7BD897.5020403@Leviacomm.net> <20110925101243.GC4867@iris.usta.de> <20110925111746.GA14018@lain.home> X-Mailinglist: mdocml-discuss Reply-To: discuss@mdocml.bsd.lv MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20110925111746.GA14018@lain.home> User-Agent: Mutt/1.5.21 (2010-09-15) Hi, espie@ just brought up the idea to automatically pipe mandoc output to less(1) if output is a tty. My first, instinctive reaction was "ugh, i don't want no magic". But trying to find cases where this might get into the way, i couldn't at first come up with any. I'm not yet convinced there are indeed none. This could also solve naddy@'s old wish to get rid of the notorious 2>&1 | less dance after -Tlint. Do we want this? Maybe i'm even slightly in favour, but not yet really sure. Yours, Ingo Index: main.c =================================================================== RCS file: /cvs/src/usr.bin/mandoc/main.c,v retrieving revision 1.78 diff -u -p -r1.78 main.c --- main.c 17 Sep 2011 14:45:22 -0000 1.78 +++ main.c 25 Sep 2011 17:58:25 -0000 @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -57,6 +58,7 @@ struct curparse { char outopts[BUFSIZ]; /* buf of output opts */ }; +static void fork_pager(int); static int moptions(enum mparset *, char *); static void mmsg(enum mandocerr, enum mandoclevel, const char *, int, int, const char *); @@ -72,7 +74,7 @@ static const char *progname; int main(int argc, char *argv[]) { - int c; + int c, fd; struct curparse curp; enum mparset type; enum mandoclevel rc; @@ -116,6 +118,10 @@ main(int argc, char *argv[]) /* NOTREACHED */ } + fd = OUTT_LINT == curp.outtype ? STDERR_FILENO : STDOUT_FILENO; + if (isatty(fd)) + fork_pager(fd); + curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp); argc -= optind; @@ -382,4 +388,54 @@ mmsg(enum mandocerr t, enum mandoclevel fprintf(stderr, ": %s", msg); fputc('\n', stderr); +} + +static void +fork_pager(int std_fd) +{ + char *pager; + int fildes[2]; + + if (-1 == pipe(fildes)) { + warn("pipe"); + return; + } + switch (fork()) { + case (-1): + warn("fork"); + return; + case (0): /* The child process stays mandoc. */ + close(fildes[0]); + if (-1 == dup2(fildes[1], std_fd)) + err(1, "dup2 in the formatter"); + /* No return here, or we might get double output. */ + close(fildes[1]); + return; + default: + break; + } + /* + * The parent process becomes the pager. + * In case of communication problems, + * it does the work itself, by simply returning. + */ + close(fildes[1]); + if (-1 == dup2(fildes[0], STDIN_FILENO)) { + warn("dup2 in the pager"); + return; + } + close(fildes[0]); + do { + pager = getenv("MANPAGER"); + if (NULL != pager && '\0' != *pager) + break; + pager = getenv("PAGER"); + if (NULL != pager && '\0' != *pager) + break; + pager = "/usr/bin/less"; + } while (0); + execl(pager, "mandoc [pager]", NULL); + warn("execl: %s", pager); + close(STDIN_FILENO); + return; } Index: mandoc.1 =================================================================== RCS file: /cvs/src/usr.bin/mandoc/mandoc.1,v retrieving revision 1.47 diff -u -p -r1.47 mandoc.1 --- mandoc.1 18 Sep 2011 10:25:28 -0000 1.47 +++ mandoc.1 25 Sep 2011 17:58:25 -0000 @@ -108,6 +108,18 @@ text from stdin, implying and produces .Fl T Ns Cm ascii output. +If the output is connected to a terminal, it is automatically piped to the +pager selected by the +.Ev MANPAGER +environment variable, or by +.Ev PAGER +if the former is undefined or empty, or to +.Pa /usr/bin/less +by default. +If the +.Fl T Ns Cm lint +output mode is selected, the standard error output is piped in place +of the standard output. .Ss Input Formats The .Nm @@ -363,6 +375,16 @@ See .Sx HTML Output for details; beyond generating XHTML tags instead of HTML tags, these output modes are identical. +.Sh ENVIRONMENT +.Bl -tag -width MANPAGER +.It Ev MANPAGER +The full path to the pager utility. +.It Ev PAGER +The same if +.Ev MANPAGER +is undefined or empty; defaults to +.Pa /usr/bin/less . +.El .Sh EXIT STATUS The .Nm -- To unsubscribe send an email to discuss+unsubscribe@mdocml.bsd.lv