=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.218 retrieving revision 1.226 diff -u -p -r1.218 -r1.226 --- mandoc/main.c 2015/02/03 21:16:02 1.218 +++ mandoc/main.c 2015/03/17 07:33:07 1.226 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.218 2015/02/03 21:16:02 schwarze Exp $ */ +/* $Id: main.c,v 1.226 2015/03/17 07:33:07 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze @@ -20,11 +20,14 @@ #include #include /* MACHINE */ +#include #include #include #include #include +#include +#include #include #include #include @@ -90,6 +93,7 @@ static int fs_lookup(const struct manpaths *, static void fs_search(const struct mansearch *, const struct manpaths *, int, char**, struct manpage **, size_t *); +static void handle_sigpipe(int); static int koptions(int *, char *); #if HAVE_SQLITE3 int mandocdb(int, char**); @@ -100,16 +104,16 @@ static void mmsg(enum mandocerr, enum mandoclevel, 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 void version(void) __attribute__((noreturn)); static int woptions(struct curparse *, char *); static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; static char help_arg[] = "help"; static char *help_argv[] = {help_arg, NULL}; static const char *progname; +static enum mandoclevel rc; int @@ -126,16 +130,17 @@ main(int argc, char *argv[]) size_t isec, i, sz; int prio, best_prio, synopsis_only; char sec; - enum mandoclevel rc, rctmp; + enum mandoclevel rctmp; 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. */ - progname = strrchr(argv[0], '/'); - if (progname == NULL) + if (argc < 1) + progname = "mandoc"; + else if ((progname = strrchr(argv[0], '/')) == NULL) progname = argv[0]; else ++progname; @@ -173,7 +178,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; @@ -188,7 +193,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; @@ -196,7 +201,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': @@ -256,9 +261,6 @@ main(int argc, char *argv[]) case 'w': outmode = OUTMODE_FLN; break; - case 'V': - version(); - /* NOTREACHED */ default: show_usage = 1; break; @@ -274,7 +276,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; @@ -287,8 +289,10 @@ main(int argc, char *argv[]) /* Parse arguments. */ - argc -= optind; - argv += optind; + if (argc > 0) { + argc -= optind; + argv += optind; + } resp = NULL; /* @@ -303,18 +307,20 @@ main(int argc, char *argv[]) argc = 1; } } else if (argc > 1 && - ((uc = argv[0]) != NULL) && + ((uc = (unsigned char *)argv[0]) != NULL) && ((isdigit(uc[0]) && (uc[1] == '\0' || (isalpha(uc[1]) && uc[2] == '\0'))) || (uc[0] == 'n' && uc[1] == '\0'))) { - search.sec = uc; + search.sec = (char *)uc; argv++; argc--; } if (search.arch == NULL) search.arch = getenv("MACHINE"); +#ifdef MACHINE if (search.arch == NULL) search.arch = MACHINE; +#endif } rc = MANDOCLEVEL_OK; @@ -414,22 +420,21 @@ main(int argc, char *argv[]) if (OUTT_MAN == curp.outtype) mparse_keep(curp.mp); - if (argc == 0) { - if (use_pager && isatty(STDOUT_FILENO)) - spawn_pager(); + if (argc < 1) { + if (pager_pid == 1 && isatty(STDOUT_FILENO)) + pager_pid = spawn_pager(); parse(&curp, STDIN_FILENO, "", &rc); } - while (argc) { + while (argc > 0) { rctmp = mparse_open(curp.mp, &fd, resp != NULL ? resp->file : *argv); if (rc < rctmp) 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); @@ -479,15 +484,18 @@ out: free(defos); - return((int)rc); -} + /* + * 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. + */ -static void -version(void) -{ + if (pager_pid != 0 && pager_pid != 1) { + fclose(stdout); + waitpid(pager_pid, NULL, 0); + } - printf("mandoc %s\n", VERSION); - exit((int)MANDOCLEVEL_OK); + return((int)rc); } static void @@ -496,24 +504,24 @@ usage(enum argmode argmode) switch (argmode) { case ARG_FILE: - fputs("usage: mandoc [-acfhklV] [-Ios=name] " + fputs("usage: mandoc [-acfhkl] [-Ios=name] " "[-Kencoding] [-mformat] [-Ooption]\n" "\t [-Toutput] [-Wlevel] [file ...]\n", stderr); break; case ARG_NAME: - fputs("usage: man [-acfhklVw] [-C file] [-I os=name] " + fputs("usage: man [-acfhklw] [-C file] [-I os=name] " "[-K encoding] [-M path] [-m path]\n" "\t [-O option=value] [-S subsection] [-s section] " "[-T output] [-W level]\n" "\t [section] name ...\n", stderr); break; case ARG_WORD: - fputs("usage: whatis [-acfhklVw] [-C file] " + fputs("usage: whatis [-acfhklw] [-C file] " "[-M path] [-m path] [-O outkey] [-S arch]\n" "\t [-s section] name ...\n", stderr); break; case ARG_EXPR: - fputs("usage: apropos [-acfhklVw] [-C file] " + fputs("usage: apropos [-acfhklw] [-C file] " "[-M path] [-m path] [-O outkey] [-S arch]\n" "\t [-s section] expression ...\n", stderr); break; @@ -526,16 +534,16 @@ fs_lookup(const struct manpaths *paths, size_t ipath, const char *sec, const char *arch, const char *name, struct manpage **res, size_t *ressz) { + glob_t globinfo; struct manpage *page; char *file; - int form; + int form, globres; + form = FORM_SRC; mandoc_asprintf(&file, "%s/man%s/%s.%s", paths->paths[ipath], sec, name, sec); - if (access(file, R_OK) != -1) { - form = FORM_SRC; + if (access(file, R_OK) != -1) goto found; - } free(file); mandoc_asprintf(&file, "%s/cat%s/%s.0", @@ -549,21 +557,31 @@ fs_lookup(const struct manpaths *paths, size_t ipath, if (arch != NULL) { mandoc_asprintf(&file, "%s/man%s/%s/%s.%s", paths->paths[ipath], sec, arch, name, sec); - if (access(file, R_OK) != -1) { - form = FORM_SRC; + if (access(file, R_OK) != -1) goto found; - } free(file); } - return(0); + mandoc_asprintf(&file, "%s/man%s/%s.*", + paths->paths[ipath], sec, name); + globres = glob(file, 0, NULL, &globinfo); + if (globres != 0 && globres != GLOB_NOMATCH) + fprintf(stderr, "%s: %s: glob: %s\n", + progname, file, strerror(errno)); + free(file); + if (globres == 0) + file = mandoc_strdup(*globinfo.gl_pathv); + globfree(&globinfo); + if (globres != 0) + return(0); + found: #if HAVE_SQLITE3 fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n" " consider running # makewhatis %s\n", progname, name, sec, paths->paths[ipath]); #endif - + *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage)); page = *res + (*ressz - 1); page->file = file; @@ -926,6 +944,13 @@ mmsg(enum mandocerr t, enum mandoclevel lvl, } static void +handle_sigpipe(int signum) +{ + + exit(rc); +} + +static pid_t spawn_pager(void) { #define MAX_PAGER_ARGS 16 @@ -934,31 +959,34 @@ 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)); exit((int)MANDOCLEVEL_SYSERR); case 0: + break; + default: close(fildes[0]); if (dup2(fildes[1], STDOUT_FILENO) == -1) { fprintf(stderr, "%s: dup output: %s\n", progname, strerror(errno)); exit((int)MANDOCLEVEL_SYSERR); } - return; - default: - break; + close(fildes[1]); + signal(SIGPIPE, handle_sigpipe); + return(pager_pid); } - /* The original process becomes the pager. */ + /* The child process becomes the pager. */ close(fildes[1]); if (dup2(fildes[0], STDIN_FILENO) == -1) { @@ -966,6 +994,7 @@ spawn_pager(void) progname, strerror(errno)); exit((int)MANDOCLEVEL_SYSERR); } + close(fildes[0]); pager = getenv("MANPAGER"); if (pager == NULL || *pager == '\0')