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

Diff for /mandoc/main.c between version 1.3 and 1.236

version 1.3, 2009/03/19 16:40:49 version 1.236, 2015/04/19 15:10:46
Line 1 
Line 1 
 /* $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>   * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
    * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the   * purpose with or without fee is hereby granted, provided that the above
  * above copyright notice and this permission notice appear in all   * copyright notice and this permission notice appear in all copies.
  * copies.  
  *   *
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  * PERFORMANCE OF THIS SOFTWARE.  
  */   */
 #include <sys/stat.h>  #include "config.h"
   
   #include <sys/types.h>
   #include <sys/param.h>  /* MACHINE */
   #include <sys/wait.h>
   
 #include <assert.h>  #include <assert.h>
 #include <err.h>  #include <ctype.h>
   #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
   #include <glob.h>
   #include <signal.h>
 #include <stdio.h>  #include <stdio.h>
   #include <stdint.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   
   #include "mandoc_aux.h"
   #include "mandoc.h"
   #include "roff.h"
 #include "mdoc.h"  #include "mdoc.h"
   #include "man.h"
   #include "main.h"
   #include "manconf.h"
   #include "mansearch.h"
   
 #ifdef __linux__  #if !defined(__GNUC__) || (__GNUC__ < 2)
 extern  int               getsubopt(char **, char * const *, char **);  # if !defined(lint)
 # ifndef __dead  #  define __attribute__(x)
 #  define __dead __attribute__((__noreturn__))  
 # endif  # endif
 #endif  #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
   
 #define WARN_WALL         0x03          /* All-warnings mask. */  enum    outmode {
 #define WARN_WCOMPAT     (1 << 0)       /* Compatibility warnings. */          OUTMODE_DEF = 0,
 #define WARN_WSYNTAX     (1 << 1)       /* Syntax warnings. */          OUTMODE_FLN,
 #define WARN_WERR        (1 << 2)       /* Warnings->errors. */          OUTMODE_LST,
           OUTMODE_ALL,
 enum outt {          OUTMODE_INT,
         OUTT_ASCII,          OUTMODE_ONE
         OUTT_LATIN1,  
         OUTT_UTF8,  
         OUTT_TREE,  
         OUTT_LINT  
 };  };
   
 typedef int             (*out_run)(void *, const struct mdoc *);  typedef void            (*out_mdoc)(void *, const struct roff_man *);
   typedef void            (*out_man)(void *, const struct roff_man *);
 typedef void            (*out_free)(void *);  typedef void            (*out_free)(void *);
   
 extern  char             *__progname;  enum    outt {
           OUTT_ASCII = 0, /* -Tascii */
           OUTT_LOCALE,    /* -Tlocale */
           OUTT_UTF8,      /* -Tutf8 */
           OUTT_TREE,      /* -Ttree */
           OUTT_MAN,       /* -Tman */
           OUTT_HTML,      /* -Thtml */
           OUTT_LINT,      /* -Tlint */
           OUTT_PS,        /* -Tps */
           OUTT_PDF        /* -Tpdf */
   };
   
 extern  void             *ascii_alloc(void);  struct  curparse {
 extern  void             *latin1_alloc(void);          struct mparse    *mp;
 extern  void             *utf8_alloc(void);          struct mchars    *mchars;       /* character table */
 extern  int               terminal_run(void *, const struct mdoc *);          enum mandoclevel  wlevel;       /* ignore messages below this */
 extern  int               tree_run(void *, const struct mdoc *);          int               wstop;        /* stop after a file with a warning */
 extern  void              terminal_free(void *);          enum outt         outtype;      /* which output to use */
           out_mdoc          outmdoc;      /* mdoc output ptr */
           out_man           outman;       /* man output ptr */
           out_free          outfree;      /* free output ptr */
           void             *outdata;      /* data for output */
           struct manoutput *outopts;      /* output options */
   };
   
 __dead  static void       version(void);  static  int               fs_lookup(const struct manpaths *,
 __dead  static void       usage(void);                                  size_t ipath, const char *,
 static  int               foptions(int *, char *);                                  const char *, const char *,
 static  int               toptions(enum outt *, char *);                                  struct manpage **, size_t *);
 static  int               woptions(int *, char *);  static  void              fs_search(const struct mansearch *,
 static  int               merr(void *, int, int, const char *);                                  const struct manpaths *, int, char**,
 static  int               mwarn(void *, int, int,                                  struct manpage **, size_t *);
                                 enum mdoc_warn, const char *);  static  void              handle_sigpipe(int);
 static  int               file(char **, size_t *, char **, size_t *,  static  int               koptions(int *, char *);
                                 const char *, struct mdoc *);  #if HAVE_SQLITE3
 static  int               fdesc(char **, size_t *, char **, size_t *,  int                       mandocdb(int, char**);
                                 const char *, int, struct mdoc *);  #endif
   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 *);
   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));
   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  int
 main(int argc, char *argv[])  main(int argc, char *argv[])
 {  {
         int              c, rc, fflags, wflags;          struct manconf   conf;
         struct mdoc_cb   cb;          struct curparse  curp;
         struct mdoc     *mdoc;          struct mansearch search;
         char            *buf, *line;          char            *auxpaths;
         size_t           bufsz, linesz;          char            *defos;
         void            *outdata;          unsigned char   *uc;
         enum outt        outtype;          struct manpage  *res, *resp;
         out_run          outrun;          char            *conf_file, *defpaths;
         out_free         outfree;          size_t           isec, i, sz;
           int              prio, best_prio;
           char             sec;
           enum mandoclevel rctmp;
           enum outmode     outmode;
           int              fd;
           int              show_usage;
           int              options;
           int              c;
           pid_t            pager_pid;  /* 0: don't use; 1: not yet spawned. */
   
         fflags = wflags = 0;          if (argc < 1)
         outtype = OUTT_ASCII;                  progname = "mandoc";
           else if ((progname = strrchr(argv[0], '/')) == NULL)
                   progname = argv[0];
           else
                   ++progname;
   
         /* LINTED */  #if HAVE_SQLITE3
         while (-1 != (c = getopt(argc, argv, "f:VW:T:")))          if (strcmp(progname, BINM_MAKEWHATIS) == 0)
                   return(mandocdb(argc, argv));
   #endif
   
           /* Search options. */
   
           memset(&conf, 0, sizeof(conf));
           conf_file = defpaths = NULL;
           auxpaths = NULL;
   
           memset(&search, 0, sizeof(struct mansearch));
           search.outkey = "Nd";
   
           if (strcmp(progname, BINM_MAN) == 0)
                   search.argmode = ARG_NAME;
           else if (strcmp(progname, BINM_APROPOS) == 0)
                   search.argmode = ARG_EXPR;
           else if (strcmp(progname, BINM_WHATIS) == 0)
                   search.argmode = ARG_WORD;
           else if (strncmp(progname, "help", 4) == 0)
                   search.argmode = ARG_NAME;
           else
                   search.argmode = ARG_FILE;
   
           /* Parser and formatter options. */
   
           memset(&curp, 0, sizeof(struct curparse));
           curp.outtype = OUTT_LOCALE;
           curp.wlevel  = MANDOCLEVEL_BADARG;
           curp.outopts = &conf.output;
           options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
           defos = NULL;
   
           pager_pid = 1;
           show_usage = 0;
           outmode = OUTMODE_DEF;
   
           while (-1 != (c = getopt(argc, argv,
                           "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
                 switch (c) {                  switch (c) {
                 case ('f'):                  case 'a':
                         if ( ! foptions(&fflags, optarg))                          outmode = OUTMODE_ALL;
                                 return(0);  
                         break;                          break;
                 case ('T'):                  case 'C':
                         if ( ! toptions(&outtype, optarg))                          conf_file = optarg;
                                 return(0);  
                         break;                          break;
                 case ('W'):                  case 'c':
                         if ( ! woptions(&wflags, optarg))                          pager_pid = 0;
                                 return(0);  
                         break;                          break;
                 case ('V'):                  case 'f':
                         version();                          search.argmode = ARG_WORD;
                         /* NOTREACHED */                          break;
                   case 'h':
                           conf.output.synopsisonly = 1;
                           pager_pid = 0;
                           outmode = OUTMODE_ALL;
                           break;
                   case 'I':
                           if (strncmp(optarg, "os=", 3)) {
                                   fprintf(stderr,
                                       "%s: -I %s: Bad argument\n",
                                       progname, optarg);
                                   return((int)MANDOCLEVEL_BADARG);
                           }
                           if (defos) {
                                   fprintf(stderr,
                                       "%s: -I %s: Duplicate argument\n",
                                       progname, optarg);
                                   return((int)MANDOCLEVEL_BADARG);
                           }
                           defos = mandoc_strdup(optarg + 3);
                           break;
                   case 'i':
                           outmode = OUTMODE_INT;
                           break;
                   case 'K':
                           if ( ! koptions(&options, optarg))
                                   return((int)MANDOCLEVEL_BADARG);
                           break;
                   case 'k':
                           search.argmode = ARG_EXPR;
                           break;
                   case 'l':
                           search.argmode = ARG_FILE;
                           outmode = OUTMODE_ALL;
                           break;
                   case 'M':
                           defpaths = optarg;
                           break;
                   case 'm':
                           auxpaths = optarg;
                           break;
                   case 'O':
                           search.outkey = optarg;
                           while (optarg != NULL)
                                   manconf_output(&conf.output,
                                       strsep(&optarg, ","));
                           break;
                   case 'S':
                           search.arch = optarg;
                           break;
                   case 's':
                           search.sec = optarg;
                           break;
                   case 'T':
                           if ( ! toptions(&curp, optarg))
                                   return((int)MANDOCLEVEL_BADARG);
                           break;
                   case 'W':
                           if ( ! woptions(&curp, optarg))
                                   return((int)MANDOCLEVEL_BADARG);
                           break;
                   case 'w':
                           outmode = OUTMODE_FLN;
                           break;
                 default:                  default:
                         usage();                          show_usage = 1;
                         /* NOTREACHED */                          break;
                 }                  }
           }
   
         argc -= optind;          if (show_usage)
         argv += optind;                  usage(search.argmode);
   
           /* Postprocess options. */
   
           if (outmode == OUTMODE_DEF) {
                   switch (search.argmode) {
                   case ARG_FILE:
                           outmode = OUTMODE_ALL;
                           pager_pid = 0;
                           break;
                   case ARG_NAME:
                           outmode = OUTMODE_ONE;
                           break;
                   default:
                           outmode = OUTMODE_LST;
                           break;
                   }
           }
   
           /* Parse arguments. */
   
           if (argc > 0) {
                   argc -= optind;
                   argv += optind;
           }
           resp = NULL;
   
         /*          /*
          * Allocate the appropriate front-end.  Note that utf8, ascii           * Quirks for help(1)
          * and latin1 all resolve to the terminal front-end with           * and for a man(1) section argument without -s.
          * different encodings (see terminal.c).  Not all frontends have  
          * cleanup or alloc routines.  
          */           */
   
         switch (outtype) {          if (search.argmode == ARG_NAME) {
         case (OUTT_LATIN1):                  if (*progname == 'h') {
                 outdata = latin1_alloc();                          if (argc == 0) {
                 outrun = terminal_run;                                  argv = help_argv;
                 outfree = terminal_free;                                  argc = 1;
                 break;                          }
         case (OUTT_UTF8):                  } else if (argc > 1 &&
                 outdata = utf8_alloc();                      ((uc = (unsigned char *)argv[0]) != NULL) &&
                 outrun = terminal_run;                      ((isdigit(uc[0]) && (uc[1] == '\0' ||
                 outfree = terminal_free;                        (isalpha(uc[1]) && uc[2] == '\0'))) ||
                 break;                       (uc[0] == 'n' && uc[1] == '\0'))) {
         case (OUTT_TREE):                          search.sec = (char *)uc;
                 outdata = NULL;                          argv++;
                 outrun = tree_run;                          argc--;
                 outfree = NULL;                  }
                 break;                  if (search.arch == NULL)
         case (OUTT_LINT):                          search.arch = getenv("MACHINE");
                 outdata = NULL;  #ifdef MACHINE
                 outrun = NULL;                  if (search.arch == NULL)
                 outfree = NULL;                          search.arch = MACHINE;
                 break;  #endif
         default:  
                 outdata = ascii_alloc();  
                 outrun = terminal_run;  
                 outfree = terminal_free;  
                 break;  
         }          }
   
           rc = MANDOCLEVEL_OK;
   
           /* man(1), whatis(1), apropos(1) */
   
           if (search.argmode != ARG_FILE) {
                   if (argc == 0)
                           usage(search.argmode);
   
                   if (search.argmode == ARG_NAME &&
                       outmode == OUTMODE_ONE)
                           search.firstmatch = 1;
   
                   /* Access the mandoc database. */
   
                   manconf_parse(&conf, conf_file, defpaths, auxpaths);
   #if HAVE_SQLITE3
                   mansearch_setup(1);
                   if ( ! mansearch(&search, &conf.manpath,
                       argc, argv, &res, &sz))
                           usage(search.argmode);
   #else
                   if (search.argmode != ARG_NAME) {
                           fputs("mandoc: database support not compiled in\n",
                               stderr);
                           return((int)MANDOCLEVEL_BADARG);
                   }
                   sz = 0;
   #endif
   
                   if (sz == 0 && search.argmode == ARG_NAME)
                           fs_search(&search, &conf.manpath,
                               argc, argv, &res, &sz);
   
                   if (sz == 0) {
                           fprintf(stderr, "%s: nothing appropriate\n",
                               progname);
                           rc = MANDOCLEVEL_BADARG;
                           goto out;
                   }
   
                   /*
                    * For standard man(1) and -a output mode,
                    * prepare for copying filename pointers
                    * into the program parameter array.
                    */
   
                   if (outmode == OUTMODE_ONE) {
                           argc = 1;
                           best_prio = 10;
                   } else if (outmode == OUTMODE_ALL)
                           argc = (int)sz;
   
                   /* Iterate all matching manuals. */
   
                   resp = res;
                   for (i = 0; i < sz; i++) {
                           if (outmode == OUTMODE_FLN)
                                   puts(res[i].file);
                           else if (outmode == OUTMODE_LST)
                                   printf("%s - %s\n", res[i].names,
                                       res[i].output == NULL ? "" :
                                       res[i].output);
                           else if (outmode == OUTMODE_ONE) {
                                   /* Search for the best section. */
                                   isec = strcspn(res[i].file, "123456789");
                                   sec = res[i].file[isec];
                                   if ('\0' == sec)
                                           continue;
                                   prio = sec_prios[sec - '1'];
                                   if (prio >= best_prio)
                                           continue;
                                   best_prio = prio;
                                   resp = res + i;
                           }
                   }
   
                   /*
                    * For man(1), -a and -i output mode, fall through
                    * to the main mandoc(1) code iterating files
                    * and running the parsers on each of them.
                    */
   
                   if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
                           goto out;
           }
   
           /* mandoc(1) */
   
           if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
                   return((int)MANDOCLEVEL_BADARG);
   
           curp.mchars = mchars_alloc();
           curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
               curp.mchars, defos);
   
         /*          /*
          * All callbacks route into here, where we print them onto the           * Conditionally start up the lookaside buffer before parsing.
          * screen.  XXX - for now, no path for debugging messages.  
          */           */
           if (OUTT_MAN == curp.outtype)
                   mparse_keep(curp.mp);
   
         cb.mdoc_msg = NULL;          if (argc < 1) {
         cb.mdoc_err = merr;                  if (pager_pid == 1 && isatty(STDOUT_FILENO))
         cb.mdoc_warn = mwarn;                          pager_pid = spawn_pager();
                   parse(&curp, STDIN_FILENO, "<stdin>");
           }
   
         buf = line = NULL;          while (argc > 0) {
         bufsz = linesz = 0;                  rctmp = mparse_open(curp.mp, &fd,
                       resp != NULL ? resp->file : *argv);
                   if (rc < rctmp)
                           rc = rctmp;
   
         mdoc = mdoc_alloc(&wflags, fflags, &cb);                  if (fd != -1) {
                           if (pager_pid == 1 && isatty(STDOUT_FILENO))
                                   pager_pid = spawn_pager();
   
         while (*argv) {                          if (resp == NULL)
                 if ( ! file(&line, &linesz, &buf, &bufsz, *argv, mdoc))                                  parse(&curp, fd, *argv);
                           else if (resp->form & FORM_SRC) {
                                   /* For .so only; ignore failure. */
                                   chdir(conf.manpath.paths[resp->ipath]);
                                   parse(&curp, fd, resp->file);
                           } else
                                   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);
                   }
   
                   if (MANDOCLEVEL_OK != rc && curp.wstop)
                         break;                          break;
                 if (outrun && ! (*outrun)(outdata, mdoc))  
                         break;  
   
                 /* Reset the parser for another file. */                  if (resp != NULL)
                 mdoc_reset(mdoc);                          resp++;
                 argv++;                  else
                           argv++;
                   if (--argc)
                           mparse_reset(curp.mp);
         }          }
   
         rc = NULL == *argv;          if (curp.outfree)
                   (*curp.outfree)(curp.outdata);
           mparse_free(curp.mp);
           mchars_free(curp.mchars);
   
         if (buf)  out:
                 free(buf);          if (search.argmode != ARG_FILE) {
         if (line)                  manconf_free(&conf);
                 free(line);  #if HAVE_SQLITE3
         if (outfree)                  mansearch_free(res, sz);
                 (*outfree)(outdata);                  mansearch_setup(0);
   #endif
           }
   
         mdoc_free(mdoc);          free(defos);
   
         return(rc ? EXIT_SUCCESS : EXIT_FAILURE);          /*
 }           * 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.
            */
   
           if (pager_pid != 0 && pager_pid != 1) {
                   fclose(stdout);
                   waitpid(pager_pid, NULL, 0);
           }
   
 __dead static void          return((int)rc);
 version(void)  
 {  
   
         (void)printf("%s %s\n", __progname, VERSION);  
         exit(0);  
         /* NOTREACHED */  
 }  }
   
   static void
 __dead static void  usage(enum argmode argmode)
 usage(void)  
 {  {
   
         (void)fprintf(stderr, "usage: %s\n", __progname);          switch (argmode) {
         exit(1);          case ARG_FILE:
         /* NOTREACHED */                  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] "
                       "[-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 [-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 [-acfhklw] [-C file] "
                       "[-M path] [-m path] [-O outkey] [-S arch]\n"
                       "\t       [-s section] expression ...\n", stderr);
                   break;
           }
           exit((int)MANDOCLEVEL_BADARG);
 }  }
   
   
 static int  static int
 file(char **ln, size_t *lnsz, char **buf, size_t *bufsz,  fs_lookup(const struct manpaths *paths, size_t ipath,
                 const char *file, struct mdoc *mdoc)          const char *sec, const char *arch, const char *name,
           struct manpage **res, size_t *ressz)
 {  {
         int              fd, c;          glob_t           globinfo;
           struct manpage  *page;
           char            *file;
           int              form, globres;
   
         if (-1 == (fd = open(file, O_RDONLY, 0))) {          form = FORM_SRC;
                 warn("%s", file);          mandoc_asprintf(&file, "%s/man%s/%s.%s",
                 return(0);              paths->paths[ipath], sec, name, sec);
           if (access(file, R_OK) != -1)
                   goto found;
           free(file);
   
           mandoc_asprintf(&file, "%s/cat%s/%s.0",
               paths->paths[ipath], sec, name);
           if (access(file, R_OK) != -1) {
                   form = FORM_CAT;
                   goto found;
         }          }
           free(file);
   
         c = fdesc(ln, lnsz, buf, bufsz, file, fd, mdoc);          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)
                           goto found;
                   free(file);
           }
   
         if (-1 == close(fd))          mandoc_asprintf(&file, "%s/man%s/%s.*",
                 warn("%s", file);              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);
   
         return(c);  found:
   #if HAVE_SQLITE3
           fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run "
               "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;
           page->names = NULL;
           page->output = NULL;
           page->ipath = ipath;
           page->bits = NAME_FILE & NAME_MASK;
           page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
           page->form = form;
           return(1);
 }  }
   
   static void
   fs_search(const struct mansearch *cfg, const struct manpaths *paths,
           int argc, char **argv, struct manpage **res, size_t *ressz)
   {
           const char *const sections[] =
               {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
           const size_t nsec = sizeof(sections)/sizeof(sections[0]);
   
 static int          size_t           ipath, isec, lastsz;
 fdesc(char **lnp, size_t *lnsz, char **bufp, size_t *bufsz,  
                 const char *f, int fd, struct mdoc *mdoc)          assert(cfg->argmode == ARG_NAME);
   
           *res = NULL;
           *ressz = lastsz = 0;
           while (argc) {
                   for (ipath = 0; ipath < paths->sz; ipath++) {
                           if (cfg->sec != NULL) {
                                   if (fs_lookup(paths, ipath, cfg->sec,
                                       cfg->arch, *argv, res, ressz) &&
                                       cfg->firstmatch)
                                           return;
                           } else for (isec = 0; isec < nsec; isec++)
                                   if (fs_lookup(paths, ipath, sections[isec],
                                       cfg->arch, *argv, res, ressz) &&
                                       cfg->firstmatch)
                                           return;
                   }
                   if (*ressz == lastsz)
                           fprintf(stderr,
                               "%s: No entry for %s in the manual.\n",
                               progname, *argv);
                   lastsz = *ressz;
                   argv++;
                   argc--;
           }
   }
   
   static void
   parse(struct curparse *curp, int fd, const char *file)
 {  {
         size_t           sz;          enum mandoclevel  rctmp;
         ssize_t          ssz;          struct roff_man  *man;
         struct stat      st;  
         int              j, i, pos, lnn;  
         char            *ln, *buf;  
   
         buf = *bufp;          /* Begin by parsing the file itself. */
         ln = *lnp;  
   
           assert(file);
           assert(fd >= -1);
   
           rctmp = mparse_readfd(curp->mp, fd, file);
           if (rc < rctmp)
                   rc = rctmp;
   
         /*          /*
          * Two buffers: ln and buf.  buf is the input buffer, optimised           * With -Wstop and warnings or errors of at least the requested
          * for each file's block size.  ln is a line buffer.  Both           * level, do not produce output.
          * growable, hence passed in by ptr-ptr.  
          */           */
   
         if (-1 == fstat(fd, &st)) {          if (rctmp != MANDOCLEVEL_OK && curp->wstop)
                 warnx("%s", f);                  return;
                 sz = BUFSIZ;  
         } else  
                 sz = (unsigned)BUFSIZ > st.st_blksize ?  
                         (size_t)BUFSIZ : st.st_blksize;  
   
         if (sz > *bufsz) {          /* If unset, allocate output dev now (if applicable). */
                 if (NULL == (buf = realloc(buf, sz)))  
                         err(1, "realloc");  
                 *bufp = buf;  
                 *bufsz = sz;  
         }  
   
         /*          if ( ! (curp->outman && curp->outmdoc)) {
          * Fill buf with file blocksize and parse newlines into ln.                  switch (curp->outtype) {
          */                  case OUTT_HTML:
                           curp->outdata = html_alloc(curp->mchars,
                               curp->outopts);
                           curp->outfree = html_free;
                           break;
                   case OUTT_UTF8:
                           curp->outdata = utf8_alloc(curp->mchars,
                               curp->outopts);
                           curp->outfree = ascii_free;
                           break;
                   case OUTT_LOCALE:
                           curp->outdata = locale_alloc(curp->mchars,
                               curp->outopts);
                           curp->outfree = ascii_free;
                           break;
                   case OUTT_ASCII:
                           curp->outdata = ascii_alloc(curp->mchars,
                               curp->outopts);
                           curp->outfree = ascii_free;
                           break;
                   case OUTT_PDF:
                           curp->outdata = pdf_alloc(curp->mchars,
                               curp->outopts);
                           curp->outfree = pspdf_free;
                           break;
                   case OUTT_PS:
                           curp->outdata = ps_alloc(curp->mchars,
                               curp->outopts);
                           curp->outfree = pspdf_free;
                           break;
                   default:
                           break;
                   }
   
         for (lnn = 1, pos = 0; ; ) {                  switch (curp->outtype) {
                 if (-1 == (ssz = read(fd, buf, sz))) {                  case OUTT_HTML:
                         warn("%s", f);                          curp->outman = html_man;
                         return(0);                          curp->outmdoc = html_mdoc;
                 } else if (0 == ssz)  
                         break;                          break;
                   case OUTT_TREE:
                           curp->outman = tree_man;
                           curp->outmdoc = tree_mdoc;
                           break;
                   case OUTT_MAN:
                           curp->outmdoc = man_mdoc;
                           curp->outman = man_man;
                           break;
                   case OUTT_PDF:
                           /* FALLTHROUGH */
                   case OUTT_ASCII:
                           /* FALLTHROUGH */
                   case OUTT_UTF8:
                           /* FALLTHROUGH */
                   case OUTT_LOCALE:
                           /* FALLTHROUGH */
                   case OUTT_PS:
                           curp->outman = terminal_man;
                           curp->outmdoc = terminal_mdoc;
                           break;
                   default:
                           break;
                   }
           }
   
                 for (i = 0; i < (int)ssz; i++) {          mparse_result(curp->mp, &man, NULL);
                         if (pos >= (int)*lnsz) {  
                                 *lnsz += 256; /* Step-size. */  
                                 ln = realloc(ln, *lnsz);  
                                 if (NULL == ln)  
                                         err(1, "realloc");  
                                 *lnp = ln;  
                         }  
   
                         if ('\n' != buf[i]) {          /* Execute the out device, if it exists. */
                                 ln[pos++] = buf[i];  
                                 continue;  
                         }  
   
                         /* Check for CPP-escaped newline.  */          if (man == NULL)
                   return;
           if (curp->outmdoc != NULL && man->macroset == MACROSET_MDOC)
                   (*curp->outmdoc)(curp->outdata, man);
           if (curp->outman != NULL && man->macroset == MACROSET_MAN)
                   (*curp->outman)(curp->outdata, man);
   }
   
                         if (pos > 0 && '\\' == ln[pos - 1]) {  static void
                                 for (j = pos - 1; j >= 0; j--)  passthrough(const char *file, int fd, int synopsis_only)
                                         if ('\\' != ln[j])  {
                                                 break;          const char       synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
           const char       synr[] = "SYNOPSIS";
   
                                 if ( ! ((pos - j) % 2)) {          FILE            *stream;
                                         pos--;          const char      *syscall;
                                         lnn++;          char            *line;
                                         continue;          size_t           len, off;
           ssize_t          nw;
           int              print;
   
           fflush(stdout);
   
           if ((stream = fdopen(fd, "r")) == NULL) {
                   close(fd);
                   syscall = "fdopen";
                   goto fail;
           }
   
           print = 0;
           while ((line = fgetln(stream, &len)) != NULL) {
                   if (synopsis_only) {
                           if (print) {
                                   if ( ! isspace((unsigned char)*line))
                                           goto done;
                                   while (len &&
                                       isspace((unsigned char)*line)) {
                                           line++;
                                           len--;
                                 }                                  }
                           } else {
                                   if ((len == sizeof(synb) &&
                                        ! strncmp(line, synb, len - 1)) ||
                                       (len == sizeof(synr) &&
                                        ! strncmp(line, synr, len - 1)))
                                           print = 1;
                                   continue;
                         }                          }
   
                         ln[pos] = 0;  
                         if ( ! mdoc_parseln(mdoc, lnn, ln))  
                                 return(0);  
                         lnn++;  
                         pos = 0;  
                 }                  }
                   for (off = 0; off < len; off += nw)
                           if ((nw = write(STDOUT_FILENO, line + off,
                               len - off)) == -1 || nw == 0) {
                                   fclose(stream);
                                   syscall = "write";
                                   goto fail;
                           }
         }          }
   
         return(mdoc_endparse(mdoc));          if (ferror(stream)) {
                   fclose(stream);
                   syscall = "fgetln";
                   goto fail;
           }
   
   done:
           fclose(stream);
           return;
   
   fail:
           fprintf(stderr, "%s: %s: SYSERR: %s: %s",
               progname, file, syscall, strerror(errno));
           if (rc < MANDOCLEVEL_SYSERR)
                   rc = MANDOCLEVEL_SYSERR;
 }  }
   
   static int
   koptions(int *options, char *arg)
   {
   
           if ( ! strcmp(arg, "utf-8")) {
                   *options |=  MPARSE_UTF8;
                   *options &= ~MPARSE_LATIN1;
           } else if ( ! strcmp(arg, "iso-8859-1")) {
                   *options |=  MPARSE_LATIN1;
                   *options &= ~MPARSE_UTF8;
           } else if ( ! strcmp(arg, "us-ascii")) {
                   *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
           } else {
                   fprintf(stderr, "%s: -K %s: Bad argument\n",
                       progname, arg);
                   return(0);
           }
           return(1);
   }
   
 static int  static int
 toptions(enum outt *tflags, char *arg)  moptions(int *options, char *arg)
 {  {
   
         if (0 == strcmp(arg, "ascii"))          if (arg == NULL)
                 *tflags = OUTT_ASCII;                  /* nothing to do */;
         else if (0 == strcmp(arg, "latin1"))          else if (0 == strcmp(arg, "doc"))
                 *tflags = OUTT_LATIN1;                  *options |= MPARSE_MDOC;
         else if (0 == strcmp(arg, "utf8"))          else if (0 == strcmp(arg, "andoc"))
                 *tflags = OUTT_UTF8;                  /* nothing to do */;
         else if (0 == strcmp(arg, "lint"))          else if (0 == strcmp(arg, "an"))
                 *tflags = OUTT_LINT;                  *options |= MPARSE_MAN;
         else if (0 == strcmp(arg, "tree"))  
                 *tflags = OUTT_TREE;  
         else {          else {
                 warnx("bad argument: -T%s", arg);                  fprintf(stderr, "%s: -m %s: Bad argument\n",
                       progname, arg);
                 return(0);                  return(0);
         }          }
   
         return(1);          return(1);
 }  }
   
   
 /*  
  * Parse out the options for [-fopt...] setting compiler options.  These  
  * can be comma-delimited or called again.  
  */  
 static int  static int
 foptions(int *fflags, char *arg)  toptions(struct curparse *curp, char *arg)
 {  {
         char            *v;  
         char            *toks[4];  
   
         toks[0] = "ign-scope";          if (0 == strcmp(arg, "ascii"))
         toks[1] = "ign-escape";                  curp->outtype = OUTT_ASCII;
         toks[2] = "ign-macro";          else if (0 == strcmp(arg, "lint")) {
         toks[3] = NULL;                  curp->outtype = OUTT_LINT;
                   curp->wlevel  = MANDOCLEVEL_WARNING;
           } else if (0 == strcmp(arg, "tree"))
                   curp->outtype = OUTT_TREE;
           else if (0 == strcmp(arg, "man"))
                   curp->outtype = OUTT_MAN;
           else if (0 == strcmp(arg, "html"))
                   curp->outtype = OUTT_HTML;
           else if (0 == strcmp(arg, "utf8"))
                   curp->outtype = OUTT_UTF8;
           else if (0 == strcmp(arg, "locale"))
                   curp->outtype = OUTT_LOCALE;
           else if (0 == strcmp(arg, "xhtml"))
                   curp->outtype = OUTT_HTML;
           else if (0 == strcmp(arg, "ps"))
                   curp->outtype = OUTT_PS;
           else if (0 == strcmp(arg, "pdf"))
                   curp->outtype = OUTT_PDF;
           else {
                   fprintf(stderr, "%s: -T %s: Bad argument\n",
                       progname, arg);
                   return(0);
           }
   
         while (*arg)  
                 switch (getsubopt(&arg, toks, &v)) {  
                 case (0):  
                         *fflags |= MDOC_IGN_SCOPE;  
                         break;  
                 case (1):  
                         *fflags |= MDOC_IGN_ESCAPE;  
                         break;  
                 case (2):  
                         *fflags |= MDOC_IGN_MACRO;  
                         break;  
                 default:  
                         warnx("bad argument: -f%s", arg);  
                         return(0);  
                 }  
   
         return(1);          return(1);
 }  }
   
   
 /*  
  * Parse out the options for [-Werr...], which sets warning modes.  
  * These can be comma-delimited or called again.  
  */  
 static int  static int
 woptions(int *wflags, char *arg)  woptions(struct curparse *curp, char *arg)
 {  {
         char            *v;          char            *v, *o;
         char            *toks[5];          const char      *toks[7];
   
         toks[0] = "all";          toks[0] = "stop";
         toks[1] = "compat";          toks[1] = "all";
         toks[2] = "syntax";          toks[2] = "warning";
         toks[3] = "error";          toks[3] = "error";
         toks[4] = NULL;          toks[4] = "unsupp";
           toks[5] = "fatal";
           toks[6] = NULL;
   
         while (*arg)          while (*arg) {
                 switch (getsubopt(&arg, toks, &v)) {                  o = arg;
                 case (0):                  switch (getsubopt(&arg, UNCONST(toks), &v)) {
                         *wflags |= WARN_WALL;                  case 0:
                           curp->wstop = 1;
                         break;                          break;
                 case (1):                  case 1:
                         *wflags |= WARN_WCOMPAT;                          /* FALLTHROUGH */
                   case 2:
                           curp->wlevel = MANDOCLEVEL_WARNING;
                         break;                          break;
                 case (2):                  case 3:
                         *wflags |= WARN_WSYNTAX;                          curp->wlevel = MANDOCLEVEL_ERROR;
                         break;                          break;
                 case (3):                  case 4:
                         *wflags |= WARN_WERR;                          curp->wlevel = MANDOCLEVEL_UNSUPP;
                         break;                          break;
                   case 5:
                           curp->wlevel = MANDOCLEVEL_BADARG;
                           break;
                 default:                  default:
                         warnx("bad argument: -W%s", arg);                          fprintf(stderr, "%s: -W %s: Bad argument\n",
                               progname, o);
                         return(0);                          return(0);
                 }                  }
           }
   
         return(1);          return(1);
 }  }
   
   static void
 /* ARGSUSED */  mmsg(enum mandocerr t, enum mandoclevel lvl,
 static int                  const char *file, int line, int col, const char *msg)
 merr(void *arg, int line, int col, const char *msg)  
 {  {
           const char      *mparse_msg;
   
         warnx("error: %s (line %d, column %d)", msg, line, col);          fprintf(stderr, "%s: %s:", progname, file);
         return(0);  
           if (line)
                   fprintf(stderr, "%d:%d:", line, col + 1);
   
           fprintf(stderr, " %s", mparse_strlevel(lvl));
   
           if (NULL != (mparse_msg = mparse_strerror(t)))
                   fprintf(stderr, ": %s", mparse_msg);
   
           if (msg)
                   fprintf(stderr, ": %s", msg);
   
           fputc('\n', stderr);
 }  }
   
   static void
   handle_sigpipe(int signum)
   {
   
 static int          exit((int)rc);
 mwarn(void *arg, int line, int col,  }
                 enum mdoc_warn type, const char *msg)  
   static pid_t
   spawn_pager(void)
 {  {
         int              flags;  #define MAX_PAGER_ARGS 16
         char            *wtype;          char            *argv[MAX_PAGER_ARGS];
           const char      *pager;
           char            *cp;
           int              fildes[2];
           int              argc;
           pid_t            pager_pid;
   
         flags = *(int *)arg;          if (pipe(fildes) == -1) {
         wtype = NULL;                  fprintf(stderr, "%s: pipe: %s\n",
                       progname, strerror(errno));
                   return(0);
           }
   
         switch (type) {          switch (pager_pid = fork()) {
         case (WARN_COMPAT):          case -1:
                 wtype = "compat";                  fprintf(stderr, "%s: fork: %s\n",
                 if (flags & WARN_WCOMPAT)                      progname, strerror(errno));
                         break;                  exit((int)MANDOCLEVEL_SYSERR);
                 return(1);          case 0:
         case (WARN_SYNTAX):                  break;
                 wtype = "syntax";          default:
                 if (flags & WARN_WSYNTAX)                  close(fildes[0]);
                         break;                  if (dup2(fildes[1], STDOUT_FILENO) == -1) {
                 return(1);                          fprintf(stderr, "%s: dup output: %s\n",
                               progname, strerror(errno));
                           exit((int)MANDOCLEVEL_SYSERR);
                   }
                   close(fildes[1]);
                   signal(SIGPIPE, handle_sigpipe);
                   return(pager_pid);
         }          }
   
         assert(wtype);          /* The child process becomes the pager. */
         warnx("%s warning: %s (line %d, column %d)",  
                         wtype, msg, line, col);  
   
         if ( ! (flags & WARN_WERR))          close(fildes[1]);
                 return(1);          if (dup2(fildes[0], STDIN_FILENO) == -1) {
                   fprintf(stderr, "%s: dup input: %s\n",
                       progname, strerror(errno));
                   exit((int)MANDOCLEVEL_SYSERR);
           }
           close(fildes[0]);
   
         warnx("%s: considering warnings as errors",          pager = getenv("MANPAGER");
                         __progname);          if (pager == NULL || *pager == '\0')
         return(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));
           exit((int)MANDOCLEVEL_SYSERR);
   }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.236

CVSweb