=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.225 retrieving revision 1.229 diff -u -p -r1.225 -r1.229 --- mandoc/main.c 2015/03/10 13:50:03 1.225 +++ mandoc/main.c 2015/03/27 17:37:25 1.229 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.225 2015/03/10 13:50:03 schwarze Exp $ */ +/* $Id: main.c,v 1.229 2015/03/27 17:37:25 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze @@ -8,9 +8,9 @@ * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +39,7 @@ #include "main.h" #include "mdoc.h" #include "man.h" -#include "manpath.h" +#include "manconf.h" #include "mansearch.h" #if !defined(__GNUC__) || (__GNUC__ < 2) @@ -92,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**); @@ -99,9 +101,8 @@ int mandocdb(int, char**); static int moptions(int *, char *); static void mmsg(enum mandocerr, enum mandoclevel, const char *, int, int, const char *); -static void parse(struct curparse *, int, - const char *, enum mandoclevel *); -static enum mandoclevel passthrough(const char *, int, int); +static void parse(struct curparse *, int, const char *); +static void passthrough(const char *, int, int); static pid_t spawn_pager(void); static int toptions(struct curparse *, char *); static void usage(enum argmode) __attribute__((noreturn)); @@ -111,14 +112,15 @@ static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2 static char help_arg[] = "help"; static char *help_argv[] = {help_arg, NULL}; static const char *progname; +static enum mandoclevel rc; int main(int argc, char *argv[]) { + struct manconf conf; struct curparse curp; struct mansearch search; - struct manpaths paths; char *auxpaths; char *defos; unsigned char *uc; @@ -127,7 +129,7 @@ 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; @@ -149,7 +151,7 @@ main(int argc, char *argv[]) /* Search options. */ - memset(&paths, 0, sizeof(struct manpaths)); + memset(&conf, 0, sizeof(conf)); conf_file = defpaths = NULL; auxpaths = NULL; @@ -334,10 +336,11 @@ main(int argc, char *argv[]) /* Access the mandoc database. */ - manpath_parse(&paths, conf_file, defpaths, auxpaths); + manconf_parse(&conf, conf_file, defpaths, auxpaths); #if HAVE_SQLITE3 mansearch_setup(1); - if( ! mansearch(&search, &paths, argc, argv, &res, &sz)) + if ( ! mansearch(&search, &conf.manpath, + argc, argv, &res, &sz)) usage(search.argmode); #else if (search.argmode != ARG_NAME) { @@ -349,7 +352,8 @@ main(int argc, char *argv[]) #endif if (sz == 0 && search.argmode == ARG_NAME) - fs_search(&search, &paths, argc, argv, &res, &sz); + fs_search(&search, &conf.manpath, + argc, argv, &res, &sz); if (sz == 0) { rc = MANDOCLEVEL_BADARG; @@ -420,7 +424,7 @@ main(int argc, char *argv[]) if (argc < 1) { if (pager_pid == 1 && isatty(STDOUT_FILENO)) pager_pid = spawn_pager(); - parse(&curp, STDIN_FILENO, "", &rc); + parse(&curp, STDIN_FILENO, ""); } while (argc > 0) { @@ -434,17 +438,13 @@ main(int argc, char *argv[]) pager_pid = spawn_pager(); if (resp == NULL) - parse(&curp, fd, *argv, &rc); + parse(&curp, fd, *argv); else if (resp->form & FORM_SRC) { /* For .so only; ignore failure. */ - chdir(paths.paths[resp->ipath]); - parse(&curp, fd, resp->file, &rc); - } else { - rctmp = passthrough(resp->file, fd, - synopsis_only); - if (rc < rctmp) - rc = rctmp; - } + chdir(conf.manpath.paths[resp->ipath]); + parse(&curp, fd, resp->file); + } else + passthrough(resp->file, fd, synopsis_only); rctmp = mparse_wait(curp.mp); if (rc < rctmp) @@ -472,7 +472,7 @@ main(int argc, char *argv[]) out: if (search.argmode != ARG_FILE) { - manpath_free(&paths); + manconf_free(&conf); #if HAVE_SQLITE3 mansearch_free(res, sz); mansearch_setup(0); @@ -501,9 +501,9 @@ usage(enum argmode argmode) switch (argmode) { case ARG_FILE: - fputs("usage: mandoc [-acfhkl] [-Ios=name] " - "[-Kencoding] [-mformat] [-Ooption]\n" - "\t [-Toutput] [-Wlevel] [file ...]\n", stderr); + fputs("usage: mandoc [-acfhkl] [-I os=name] " + "[-K encoding] [-mformat] [-O option]\n" + "\t [-T output] [-W level] [file ...]\n", stderr); break; case ARG_NAME: fputs("usage: man [-acfhklw] [-C file] [-I os=name] " @@ -629,10 +629,9 @@ fs_search(const struct mansearch *cfg, const struct ma } static void -parse(struct curparse *curp, int fd, const char *file, - enum mandoclevel *level) +parse(struct curparse *curp, int fd, const char *file) { - enum mandoclevel rc; + enum mandoclevel rctmp; struct mdoc *mdoc; struct man *man; @@ -641,15 +640,17 @@ parse(struct curparse *curp, int fd, const char *file, assert(file); assert(fd >= -1); - rc = mparse_readfd(curp->mp, fd, file); + rctmp = mparse_readfd(curp->mp, fd, file); + if (rc < rctmp) + rc = rctmp; /* * With -Wstop and warnings or errors of at least the requested * level, do not produce output. */ - if (MANDOCLEVEL_OK != rc && curp->wstop) - goto cleanup; + if (rctmp != MANDOCLEVEL_OK && curp->wstop) + return; /* If unset, allocate output dev now (if applicable). */ @@ -727,13 +728,9 @@ parse(struct curparse *curp, int fd, const char *file, (*curp->outman)(curp->outdata, man); if (mdoc && curp->outmdoc) (*curp->outmdoc)(curp->outdata, mdoc); - -cleanup: - if (*level < rc) - *level = rc; } -static enum mandoclevel +static void passthrough(const char *file, int fd, int synopsis_only) { const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"; @@ -791,12 +788,13 @@ passthrough(const char *file, int fd, int synopsis_onl done: fclose(stream); - return(MANDOCLEVEL_OK); + return; fail: fprintf(stderr, "%s: %s: SYSERR: %s: %s", progname, file, syscall, strerror(errno)); - return(MANDOCLEVEL_SYSERR); + if (rc < MANDOCLEVEL_SYSERR) + rc = MANDOCLEVEL_SYSERR; } static int @@ -940,6 +938,13 @@ mmsg(enum mandocerr t, enum mandoclevel lvl, fputc('\n', stderr); } +static void +handle_sigpipe(int signum) +{ + + exit((int)rc); +} + static pid_t spawn_pager(void) { @@ -972,6 +977,7 @@ spawn_pager(void) exit((int)MANDOCLEVEL_SYSERR); } close(fildes[1]); + signal(SIGPIPE, handle_sigpipe); return(pager_pid); }