=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.183 retrieving revision 1.185 diff -u -p -r1.183 -r1.185 --- mandoc/main.c 2014/08/22 03:42:18 1.183 +++ mandoc/main.c 2014/08/22 18:07:15 1.185 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.183 2014/08/22 03:42:18 schwarze Exp $ */ +/* $Id: main.c,v 1.185 2014/08/22 18:07:15 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010, 2011, 2012, 2014 Ingo Schwarze @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -227,6 +228,8 @@ main(int argc, char *argv[]) if (show_usage) usage(search.argmode); + /* Postprocess options. */ + if (outmode == OUTMODE_DEF) { switch (search.argmode) { case ARG_FILE: @@ -242,12 +245,25 @@ main(int argc, char *argv[]) } } + /* Parse arguments. */ + argc -= optind; argv += optind; #if HAVE_SQLITE3 auxargv = NULL; #endif + /* Quirk for a man(1) section argument without -s. */ + + if (search.argmode == ARG_NAME && + argv[0] != NULL && + isdigit((unsigned char)argv[0][0]) && + (argv[0][1] == '\0' || !strcmp(argv[0], "3p"))) { + search.sec = argv[0]; + argv++; + argc--; + } + rc = MANDOCLEVEL_OK; /* man(1), whatis(1), apropos(1) */ @@ -637,7 +653,12 @@ mmsg(enum mandocerr t, enum mandoclevel lvl, static void spawn_pager(void) { - int fildes[2]; +#define MAX_PAGER_ARGS 16 + char *argv[MAX_PAGER_ARGS]; + const char *pager; + char *cp; + int fildes[2]; + int argc; if (pipe(fildes) == -1) { fprintf(stderr, "%s: pipe: %s\n", @@ -659,15 +680,48 @@ spawn_pager(void) } return; default: - close(fildes[1]); - if (dup2(fildes[0], STDIN_FILENO) == -1) { - fprintf(stderr, "%s: dup input: %s\n", - progname, strerror(errno)); - } else { - execlp("more", "more", "-s", NULL); - fprintf(stderr, "%s: exec: %s\n", - progname, strerror(errno)); - } + break; + } + + /* The original process becomes the pager. */ + + close(fildes[1]); + if (dup2(fildes[0], STDIN_FILENO) == -1) { + fprintf(stderr, "%s: dup input: %s\n", + progname, strerror(errno)); exit((int)MANDOCLEVEL_SYSERR); } + + pager = getenv("MANPAGER"); + if (pager == NULL || *pager == '\0') + pager = getenv("PAGER"); + if (pager == NULL || *pager == '\0') + pager = "/usr/bin/more -s"; + cp = mandoc_strdup(pager); + + /* + * Parse the pager command into words. + * Intentionally do not do anything fancy here. + */ + + argc = 0; + while (argc + 1 < MAX_PAGER_ARGS) { + argv[argc++] = cp; + cp = strchr(cp, ' '); + if (cp == NULL) + break; + *cp++ = '\0'; + while (*cp == ' ') + cp++; + if (*cp == '\0') + break; + } + argv[argc] = NULL; + + /* Hand over to the pager. */ + + execvp(argv[0], argv); + fprintf(stderr, "%s: exec: %s\n", + progname, strerror(errno)); + exit((int)MANDOCLEVEL_SYSERR); }