=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.236 retrieving revision 1.243 diff -u -p -r1.236 -r1.243 --- mandoc/main.c 2015/04/19 15:10:46 1.236 +++ mandoc/main.c 2015/07/21 03:26:21 1.243 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.236 2015/04/19 15:10:46 schwarze Exp $ */ +/* $Id: main.c,v 1.243 2015/07/21 03:26:21 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze @@ -39,6 +39,7 @@ #include "roff.h" #include "mdoc.h" #include "man.h" +#include "tag.h" #include "main.h" #include "manconf.h" #include "mansearch.h" @@ -352,13 +353,17 @@ main(int argc, char *argv[]) sz = 0; #endif - if (sz == 0 && search.argmode == ARG_NAME) - fs_search(&search, &conf.manpath, - argc, argv, &res, &sz); + if (sz == 0) { + if (search.argmode == ARG_NAME) + fs_search(&search, &conf.manpath, + argc, argv, &res, &sz); + else + fprintf(stderr, + "%s: nothing appropriate\n", + progname); + } if (sz == 0) { - fprintf(stderr, "%s: nothing appropriate\n", - progname); rc = MANDOCLEVEL_BADARG; goto out; } @@ -414,6 +419,9 @@ main(int argc, char *argv[]) if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths)) return((int)MANDOCLEVEL_BADARG); + if (pager_pid == 1 && isatty(STDOUT_FILENO) == 0) + pager_pid = 0; + curp.mchars = mchars_alloc(); curp.mp = mparse_alloc(options, curp.wlevel, mmsg, curp.mchars, defos); @@ -425,7 +433,7 @@ main(int argc, char *argv[]) mparse_keep(curp.mp); if (argc < 1) { - if (pager_pid == 1 && isatty(STDOUT_FILENO)) + if (pager_pid == 1) pager_pid = spawn_pager(); parse(&curp, STDIN_FILENO, ""); } @@ -437,7 +445,7 @@ main(int argc, char *argv[]) rc = rctmp; if (fd != -1) { - if (pager_pid == 1 && isatty(STDOUT_FILENO)) + if (pager_pid == 1) pager_pid = spawn_pager(); if (resp == NULL) @@ -450,10 +458,6 @@ main(int argc, char *argv[]) passthrough(resp->file, fd, conf.output.synopsisonly); - rctmp = mparse_wait(curp.mp); - if (rc < rctmp) - rc = rctmp; - if (argc > 1 && curp.outtype <= OUTT_UTF8) ascii_sepline(curp.outdata); } @@ -493,7 +497,9 @@ out: if (pager_pid != 0 && pager_pid != 1) { fclose(stdout); + tag_write(); waitpid(pager_pid, NULL, 0); + tag_unlink(); } return((int)rc); @@ -563,7 +569,7 @@ fs_lookup(const struct manpaths *paths, size_t ipath, free(file); } - mandoc_asprintf(&file, "%s/man%s/%s.*", + mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*", paths->paths[ipath], sec, name); globres = glob(file, 0, NULL, &globinfo); if (globres != 0 && globres != GLOB_NOMATCH) @@ -955,10 +961,53 @@ spawn_pager(void) char *argv[MAX_PAGER_ARGS]; const char *pager; char *cp; + size_t cmdlen; int fildes[2]; int argc; pid_t pager_pid; + pager = getenv("MANPAGER"); + if (pager == NULL || *pager == '\0') + pager = getenv("PAGER"); + if (pager == NULL || *pager == '\0') + pager = "more -s"; + cp = mandoc_strdup(pager); + + /* + * Parse the pager command into words. + * Intentionally do not do anything fancy here. + */ + + argc = 0; + while (argc + 4 < MAX_PAGER_ARGS) { + argv[argc++] = cp; + cp = strchr(cp, ' '); + if (cp == NULL) + break; + *cp++ = '\0'; + while (*cp == ' ') + cp++; + if (*cp == '\0') + break; + } + + /* Read all text right away and use the tag file. */ + + for (;;) { + if ((cmdlen = strlen(argv[0])) < 4) + break; + cp = argv[0] + cmdlen - 4; + if (strcmp(cp, "less") && strcmp(cp, "more")) + break; + if ((cp = tag_init()) == NULL) + break; + argv[argc++] = mandoc_strdup("+G1G"); + argv[argc++] = mandoc_strdup("-T"); + argv[argc++] = cp; + break; + } + argv[argc] = NULL; + if (pipe(fildes) == -1) { fprintf(stderr, "%s: pipe: %s\n", progname, strerror(errno)); @@ -994,36 +1043,10 @@ spawn_pager(void) } close(fildes[0]); - pager = getenv("MANPAGER"); - if (pager == NULL || *pager == '\0') - pager = getenv("PAGER"); - if (pager == NULL || *pager == '\0') - pager = "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)); + fprintf(stderr, "%s: exec %s: %s\n", + progname, argv[0], strerror(errno)); exit((int)MANDOCLEVEL_SYSERR); }