[BACK]Return to main.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/main.c between version 1.224 and 1.229

version 1.224, 2015/03/10 03:02:28 version 1.229, 2015/03/27 17:37:25
Line 8 
Line 8 
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.   * 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   * 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   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Line 27 
Line 27 
 #include <errno.h>  #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <glob.h>  #include <glob.h>
   #include <signal.h>
 #include <stdio.h>  #include <stdio.h>
 #include <stdint.h>  #include <stdint.h>
 #include <stdlib.h>  #include <stdlib.h>
Line 38 
Line 39 
 #include "main.h"  #include "main.h"
 #include "mdoc.h"  #include "mdoc.h"
 #include "man.h"  #include "man.h"
 #include "manpath.h"  #include "manconf.h"
 #include "mansearch.h"  #include "mansearch.h"
   
 #if !defined(__GNUC__) || (__GNUC__ < 2)  #if !defined(__GNUC__) || (__GNUC__ < 2)
Line 92  static int    fs_lookup(const struct manpaths *,
Line 93  static int    fs_lookup(const struct manpaths *,
 static  void              fs_search(const struct mansearch *,  static  void              fs_search(const struct mansearch *,
                                 const struct manpaths *, int, char**,                                  const struct manpaths *, int, char**,
                                 struct manpage **, size_t *);                                  struct manpage **, size_t *);
   static  void              handle_sigpipe(int);
 static  int               koptions(int *, char *);  static  int               koptions(int *, char *);
 #if HAVE_SQLITE3  #if HAVE_SQLITE3
 int                       mandocdb(int, char**);  int                       mandocdb(int, char**);
Line 99  int     mandocdb(int, char**);
Line 101  int     mandocdb(int, char**);
 static  int               moptions(int *, char *);  static  int               moptions(int *, char *);
 static  void              mmsg(enum mandocerr, enum mandoclevel,  static  void              mmsg(enum mandocerr, enum mandoclevel,
                                 const char *, int, int, const char *);                                  const char *, int, int, const char *);
 static  void              parse(struct curparse *, int,  static  void              parse(struct curparse *, int, const char *);
                                 const char *, enum mandoclevel *);  static  void              passthrough(const char *, int, int);
 static  enum mandoclevel  passthrough(const char *, int, int);  static  pid_t             spawn_pager(void);
 static  void              spawn_pager(void);  
 static  int               toptions(struct curparse *, char *);  static  int               toptions(struct curparse *, char *);
 static  void              usage(enum argmode) __attribute__((noreturn));  static  void              usage(enum argmode) __attribute__((noreturn));
 static  int               woptions(struct curparse *, char *);  static  int               woptions(struct curparse *, char *);
Line 111  static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2
Line 112  static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2
 static  char              help_arg[] = "help";  static  char              help_arg[] = "help";
 static  char             *help_argv[] = {help_arg, NULL};  static  char             *help_argv[] = {help_arg, NULL};
 static  const char       *progname;  static  const char       *progname;
   static  enum mandoclevel  rc;
   
   
 int  int
 main(int argc, char *argv[])  main(int argc, char *argv[])
 {  {
           struct manconf   conf;
         struct curparse  curp;          struct curparse  curp;
         struct mansearch search;          struct mansearch search;
         struct manpaths  paths;  
         char            *auxpaths;          char            *auxpaths;
         char            *defos;          char            *defos;
         unsigned char   *uc;          unsigned char   *uc;
Line 127  main(int argc, char *argv[])
Line 129  main(int argc, char *argv[])
         size_t           isec, i, sz;          size_t           isec, i, sz;
         int              prio, best_prio, synopsis_only;          int              prio, best_prio, synopsis_only;
         char             sec;          char             sec;
         enum mandoclevel rc, rctmp;          enum mandoclevel rctmp;
         enum outmode     outmode;          enum outmode     outmode;
         int              fd;          int              fd;
         int              show_usage;          int              show_usage;
         int              use_pager;  
         int              options;          int              options;
         int              c;          int              c;
           pid_t            pager_pid;  /* 0: don't use; 1: not yet spawned. */
   
         if (argc < 1)          if (argc < 1)
                 progname = "mandoc";                  progname = "mandoc";
Line 149  main(int argc, char *argv[])
Line 151  main(int argc, char *argv[])
   
         /* Search options. */          /* Search options. */
   
         memset(&paths, 0, sizeof(struct manpaths));          memset(&conf, 0, sizeof(conf));
         conf_file = defpaths = NULL;          conf_file = defpaths = NULL;
         auxpaths = NULL;          auxpaths = NULL;
   
Line 175  main(int argc, char *argv[])
Line 177  main(int argc, char *argv[])
         options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;          options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
         defos = NULL;          defos = NULL;
   
         use_pager = 1;          pager_pid = 1;
         show_usage = 0;          show_usage = 0;
         synopsis_only = 0;          synopsis_only = 0;
         outmode = OUTMODE_DEF;          outmode = OUTMODE_DEF;
Line 190  main(int argc, char *argv[])
Line 192  main(int argc, char *argv[])
                         conf_file = optarg;                          conf_file = optarg;
                         break;                          break;
                 case 'c':                  case 'c':
                         use_pager = 0;                          pager_pid = 0;
                         break;                          break;
                 case 'f':                  case 'f':
                         search.argmode = ARG_WORD;                          search.argmode = ARG_WORD;
Line 198  main(int argc, char *argv[])
Line 200  main(int argc, char *argv[])
                 case 'h':                  case 'h':
                         (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);                          (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
                         synopsis_only = 1;                          synopsis_only = 1;
                         use_pager = 0;                          pager_pid = 0;
                         outmode = OUTMODE_ALL;                          outmode = OUTMODE_ALL;
                         break;                          break;
                 case 'I':                  case 'I':
Line 273  main(int argc, char *argv[])
Line 275  main(int argc, char *argv[])
                 switch (search.argmode) {                  switch (search.argmode) {
                 case ARG_FILE:                  case ARG_FILE:
                         outmode = OUTMODE_ALL;                          outmode = OUTMODE_ALL;
                         use_pager = 0;                          pager_pid = 0;
                         break;                          break;
                 case ARG_NAME:                  case ARG_NAME:
                         outmode = OUTMODE_ONE;                          outmode = OUTMODE_ONE;
Line 334  main(int argc, char *argv[])
Line 336  main(int argc, char *argv[])
   
                 /* Access the mandoc database. */                  /* Access the mandoc database. */
   
                 manpath_parse(&paths, conf_file, defpaths, auxpaths);                  manconf_parse(&conf, conf_file, defpaths, auxpaths);
 #if HAVE_SQLITE3  #if HAVE_SQLITE3
                 mansearch_setup(1);                  mansearch_setup(1);
                 if( ! mansearch(&search, &paths, argc, argv, &res, &sz))                  if ( ! mansearch(&search, &conf.manpath,
                       argc, argv, &res, &sz))
                         usage(search.argmode);                          usage(search.argmode);
 #else  #else
                 if (search.argmode != ARG_NAME) {                  if (search.argmode != ARG_NAME) {
Line 349  main(int argc, char *argv[])
Line 352  main(int argc, char *argv[])
 #endif  #endif
   
                 if (sz == 0 && search.argmode == ARG_NAME)                  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) {                  if (sz == 0) {
                         rc = MANDOCLEVEL_BADARG;                          rc = MANDOCLEVEL_BADARG;
Line 418  main(int argc, char *argv[])
Line 422  main(int argc, char *argv[])
                 mparse_keep(curp.mp);                  mparse_keep(curp.mp);
   
         if (argc < 1) {          if (argc < 1) {
                 if (use_pager && isatty(STDOUT_FILENO))                  if (pager_pid == 1 && isatty(STDOUT_FILENO))
                         spawn_pager();                          pager_pid = spawn_pager();
                 parse(&curp, STDIN_FILENO, "<stdin>", &rc);                  parse(&curp, STDIN_FILENO, "<stdin>");
         }          }
   
         while (argc > 0) {          while (argc > 0) {
Line 430  main(int argc, char *argv[])
Line 434  main(int argc, char *argv[])
                         rc = rctmp;                          rc = rctmp;
   
                 if (fd != -1) {                  if (fd != -1) {
                         if (use_pager && isatty(STDOUT_FILENO))                          if (pager_pid == 1 && isatty(STDOUT_FILENO))
                                 spawn_pager();                                  pager_pid = spawn_pager();
                         use_pager = 0;  
   
                         if (resp == NULL)                          if (resp == NULL)
                                 parse(&curp, fd, *argv, &rc);                                  parse(&curp, fd, *argv);
                         else if (resp->form & FORM_SRC) {                          else if (resp->form & FORM_SRC) {
                                 /* For .so only; ignore failure. */                                  /* For .so only; ignore failure. */
                                 chdir(paths.paths[resp->ipath]);                                  chdir(conf.manpath.paths[resp->ipath]);
                                 parse(&curp, fd, resp->file, &rc);                                  parse(&curp, fd, resp->file);
                         } else {                          } else
                                 rctmp = passthrough(resp->file, fd,                                  passthrough(resp->file, fd, synopsis_only);
                                     synopsis_only);  
                                 if (rc < rctmp)  
                                         rc = rctmp;  
                         }  
   
                         rctmp = mparse_wait(curp.mp);                          rctmp = mparse_wait(curp.mp);
                         if (rc < rctmp)                          if (rc < rctmp)
Line 473  main(int argc, char *argv[])
Line 472  main(int argc, char *argv[])
   
 out:  out:
         if (search.argmode != ARG_FILE) {          if (search.argmode != ARG_FILE) {
                 manpath_free(&paths);                  manconf_free(&conf);
 #if HAVE_SQLITE3  #if HAVE_SQLITE3
                 mansearch_free(res, sz);                  mansearch_free(res, sz);
                 mansearch_setup(0);                  mansearch_setup(0);
Line 483  out:
Line 482  out:
         free(defos);          free(defos);
   
         /*          /*
          * Flush the output and signal end of file.           * If a pager is attached, flush the pipe leading to it
          * If a pager is attached, it allows browsing to the end.           * and signal end of file such that the user can browse
          * Otherwise, it does no harm, we are about to exit anyway.           * to the end.  Then wait for the user to close the pager.
          */           */
   
         fclose(stdout);          if (pager_pid != 0 && pager_pid != 1) {
                   fclose(stdout);
                   waitpid(pager_pid, NULL, 0);
           }
   
         /*  
          * If we spawned a pager, wait for the user to close it.  
          * Otherwise, this call fails with no adverse effect.  
          */  
   
         wait(NULL);  
   
         return((int)rc);          return((int)rc);
 }  }
   
Line 506  usage(enum argmode argmode)
Line 501  usage(enum argmode argmode)
   
         switch (argmode) {          switch (argmode) {
         case ARG_FILE:          case ARG_FILE:
                 fputs("usage: mandoc [-acfhkl] [-Ios=name] "                  fputs("usage: mandoc [-acfhkl] [-I os=name] "
                     "[-Kencoding] [-mformat] [-Ooption]\n"                      "[-K encoding] [-mformat] [-O option]\n"
                     "\t      [-Toutput] [-Wlevel] [file ...]\n", stderr);                      "\t      [-T output] [-W level] [file ...]\n", stderr);
                 break;                  break;
         case ARG_NAME:          case ARG_NAME:
                 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "                  fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
Line 634  fs_search(const struct mansearch *cfg, const struct ma
Line 629  fs_search(const struct mansearch *cfg, const struct ma
 }  }
   
 static void  static void
 parse(struct curparse *curp, int fd, const char *file,  parse(struct curparse *curp, int fd, const char *file)
         enum mandoclevel *level)  
 {  {
         enum mandoclevel  rc;          enum mandoclevel  rctmp;
         struct mdoc      *mdoc;          struct mdoc      *mdoc;
         struct man       *man;          struct man       *man;
   
Line 646  parse(struct curparse *curp, int fd, const char *file,
Line 640  parse(struct curparse *curp, int fd, const char *file,
         assert(file);          assert(file);
         assert(fd >= -1);          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           * With -Wstop and warnings or errors of at least the requested
          * level, do not produce output.           * level, do not produce output.
          */           */
   
         if (MANDOCLEVEL_OK != rc && curp->wstop)          if (rctmp != MANDOCLEVEL_OK && curp->wstop)
                 goto cleanup;                  return;
   
         /* If unset, allocate output dev now (if applicable). */          /* If unset, allocate output dev now (if applicable). */
   
Line 732  parse(struct curparse *curp, int fd, const char *file,
Line 728  parse(struct curparse *curp, int fd, const char *file,
                 (*curp->outman)(curp->outdata, man);                  (*curp->outman)(curp->outdata, man);
         if (mdoc && curp->outmdoc)          if (mdoc && curp->outmdoc)
                 (*curp->outmdoc)(curp->outdata, mdoc);                  (*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)  passthrough(const char *file, int fd, int synopsis_only)
 {  {
         const char       synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";          const char       synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
Line 796  passthrough(const char *file, int fd, int synopsis_onl
Line 788  passthrough(const char *file, int fd, int synopsis_onl
   
 done:  done:
         fclose(stream);          fclose(stream);
         return(MANDOCLEVEL_OK);          return;
   
 fail:  fail:
         fprintf(stderr, "%s: %s: SYSERR: %s: %s",          fprintf(stderr, "%s: %s: SYSERR: %s: %s",
             progname, file, syscall, strerror(errno));              progname, file, syscall, strerror(errno));
         return(MANDOCLEVEL_SYSERR);          if (rc < MANDOCLEVEL_SYSERR)
                   rc = MANDOCLEVEL_SYSERR;
 }  }
   
 static int  static int
Line 946  mmsg(enum mandocerr t, enum mandoclevel lvl,
Line 939  mmsg(enum mandocerr t, enum mandoclevel lvl,
 }  }
   
 static void  static void
   handle_sigpipe(int signum)
   {
   
           exit((int)rc);
   }
   
   static pid_t
 spawn_pager(void)  spawn_pager(void)
 {  {
 #define MAX_PAGER_ARGS 16  #define MAX_PAGER_ARGS 16
Line 954  spawn_pager(void)
Line 954  spawn_pager(void)
         char            *cp;          char            *cp;
         int              fildes[2];          int              fildes[2];
         int              argc;          int              argc;
           pid_t            pager_pid;
   
         if (pipe(fildes) == -1) {          if (pipe(fildes) == -1) {
                 fprintf(stderr, "%s: pipe: %s\n",                  fprintf(stderr, "%s: pipe: %s\n",
                     progname, strerror(errno));                      progname, strerror(errno));
                 return;                  return(0);
         }          }
   
         switch (fork()) {          switch (pager_pid = fork()) {
         case -1:          case -1:
                 fprintf(stderr, "%s: fork: %s\n",                  fprintf(stderr, "%s: fork: %s\n",
                     progname, strerror(errno));                      progname, strerror(errno));
Line 976  spawn_pager(void)
Line 977  spawn_pager(void)
                         exit((int)MANDOCLEVEL_SYSERR);                          exit((int)MANDOCLEVEL_SYSERR);
                 }                  }
                 close(fildes[1]);                  close(fildes[1]);
                 return;                  signal(SIGPIPE, handle_sigpipe);
                   return(pager_pid);
         }          }
   
         /* The child process becomes the pager. */          /* The child process becomes the pager. */

Legend:
Removed from v.1.224  
changed lines
  Added in v.1.229

CVSweb