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

Diff for /mandoc/main.c between version 1.20 and 1.212

version 1.20, 2009/04/02 16:37:40 version 1.212, 2015/01/13 13:22:22
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 AUTHOR DISCLAIMS 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 AUTHOR 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 <assert.h>  #include <assert.h>
 #include <err.h>  #include <ctype.h>
   #include <errno.h>
 #include <fcntl.h>  #include <fcntl.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.h"
   #include "mandoc_aux.h"
   #include "main.h"
 #include "mdoc.h"  #include "mdoc.h"
 #include "man.h"  #include "man.h"
   #include "manpath.h"
   #include "mansearch.h"
   
 /* Account for FreeBSD and Linux in our declarations. */  #if !defined(__GNUC__) || (__GNUC__ < 2)
   # if !defined(lint)
 #ifdef __linux__  #  define __attribute__(x)
 extern  int               getsubopt(char **, char * const *, char **);  
 # ifndef __dead  
 #  define __dead __attribute__((__noreturn__))  
 # endif  # endif
 #elif defined(__dead2)  #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
 # ifndef __dead  
 #  define __dead __dead2  
 # endif  
 #endif  
   
 struct  buf {  enum    outmode {
         char             *buf;          OUTMODE_DEF = 0,
         size_t            sz;          OUTMODE_FLN,
           OUTMODE_LST,
           OUTMODE_ALL,
           OUTMODE_INT,
           OUTMODE_ONE
 };  };
   
 enum    intt {  typedef void            (*out_mdoc)(void *, const struct mdoc *);
         INTT_AUTO,  typedef void            (*out_man)(void *, const struct man *);
         INTT_MDOC,  typedef void            (*out_free)(void *);
         INTT_MAN  
 };  
   
 enum    outt {  enum    outt {
         OUTT_ASCII = 0,          OUTT_ASCII = 0, /* -Tascii */
         OUTT_TREE,          OUTT_LOCALE,    /* -Tlocale */
         OUTT_LINT          OUTT_UTF8,      /* -Tutf8 */
           OUTT_TREE,      /* -Ttree */
           OUTT_MAN,       /* -Tman */
           OUTT_HTML,      /* -Thtml */
           OUTT_LINT,      /* -Tlint */
           OUTT_PS,        /* -Tps */
           OUTT_PDF        /* -Tpdf */
 };  };
   
 struct  curparse {  struct  curparse {
         const char       *file;          struct mparse    *mp;
         int               fd;          struct mchars    *mchars;       /* character table */
         int               wflags;          enum mandoclevel  wlevel;       /* ignore messages below this */
 #define WARN_WALL         0x03          /* All-warnings mask. */          int               wstop;        /* stop after a file with a warning */
 #define WARN_WCOMPAT     (1 << 0)       /* Compatibility warnings. */          enum outt         outtype;      /* which output to use */
 #define WARN_WSYNTAX     (1 << 1)       /* Syntax warnings. */          out_mdoc          outmdoc;      /* mdoc output ptr */
 #define WARN_WERR        (1 << 2)       /* Warnings->errors. */          out_man           outman;       /* man output ptr */
         int               fflags;          out_free          outfree;      /* free output ptr */
         enum intt         inttype;          void             *outdata;      /* data for output */
         struct man       *man;          char              outopts[BUFSIZ]; /* buf of output opts */
         struct mdoc      *mdoc;  
 };  };
   
 #define IGN_SCOPE        (1 << 0)       /* Ignore scope errors. */  static  int               koptions(int *, char *);
 #define IGN_ESCAPE       (1 << 1)       /* Ignore bad escapes. */  #if HAVE_SQLITE3
 #define IGN_MACRO        (1 << 2)       /* Ignore unknown macros. */  int                       mandocdb(int, char**);
 #define NO_IGN_MACRO     (1 << 3)  #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 *, enum mandoclevel *);
   #if HAVE_SQLITE3
   static  enum mandoclevel  passthrough(const char *, int, int);
   #endif
   static  void              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 *);
   
 typedef int             (*out_run)(void *, const struct man *,  static  const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
                                 const struct mdoc *);  static  char              help_arg[] = "help";
 typedef void            (*out_free)(void *);  static  char             *help_argv[] = {help_arg, NULL};
   static  const char       *progname;
   
 extern  char             *__progname;  
   
 extern  void             *ascii_alloc(void);  
 extern  int               terminal_run(void *, const struct man *,  
                                 const struct mdoc *);  
 extern  int               tree_run(void *, const struct man *,  
                                 const struct mdoc *);  
 extern  void              terminal_free(void *);  
   
 static  int               foptions(int *, char *);  
 static  int               toptions(enum outt *, char *);  
 static  int               moptions(enum intt *, char *);  
 static  int               woptions(int *, char *);  
 static  int               merr(void *, int, int, const char *);  
 static  int               manwarn(void *, int, int, const char *);  
 static  int               mdocwarn(void *, int, int,  
                                 enum mdoc_warn, const char *);  
 static  int               fstdin(struct buf *, struct buf *,  
                                 struct curparse *);  
 static  int               ffile(struct buf *, struct buf *,  
                                 const char *, struct curparse *);  
 static  int               fdesc(struct buf *, struct buf *,  
                                 struct curparse *);  
 static  int               pset(const char *, size_t, struct curparse *,  
                                 struct man **, struct mdoc **);  
 static  struct man       *man_init(struct curparse *);  
 static  struct mdoc      *mdoc_init(struct curparse *);  
 __dead  static void       version(void);  
 __dead  static void       usage(void);  
   
   
 int  int
 main(int argc, char *argv[])  main(int argc, char *argv[])
 {  {
         int              c, rc;  
         void            *outdata;  
         enum outt        outtype;  
         struct buf       ln, blk;  
         out_run          outrun;  
         out_free         outfree;  
         struct curparse  curp;          struct curparse  curp;
           struct mansearch search;
           struct manpaths  paths;
           char            *auxpaths;
           char            *defos;
           unsigned char   *uc;
   #if HAVE_SQLITE3
           struct manpage  *res, *resp;
           char            *conf_file, *defpaths;
           size_t           isec, i, sz;
           int              prio, best_prio, synopsis_only;
           char             sec;
   #endif
           enum mandoclevel rc;
           enum outmode     outmode;
           int              fd;
           int              show_usage;
           int              use_pager;
           int              options;
           int              c;
   
         outtype = OUTT_ASCII;          progname = strrchr(argv[0], '/');
           if (progname == NULL)
                   progname = argv[0];
           else
                   ++progname;
   
         bzero(&curp, sizeof(struct curparse));  #if HAVE_SQLITE3
           if (strcmp(progname, BINM_MAKEWHATIS) == 0)
                   return(mandocdb(argc, argv));
   #endif
   
         curp.inttype = INTT_AUTO;          /* Search options. */
   
         /* LINTED */          memset(&paths, 0, sizeof(struct manpaths));
         while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))  #if HAVE_SQLITE3
           conf_file = defpaths = NULL;
   #endif
           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_FATAL;
           options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
           defos = NULL;
   
           use_pager = 1;
           show_usage = 0;
   #if HAVE_SQLITE3
           synopsis_only = 0;
   #endif
           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(&curp.fflags, optarg))                          outmode = OUTMODE_ALL;
                                 return(0);  
                         break;                          break;
                 case ('m'):                  case 'C':
                         if ( ! moptions(&curp.inttype, optarg))  #if HAVE_SQLITE3
                                 return(0);                          conf_file = optarg;
   #endif
                         break;                          break;
                 case ('T'):                  case 'c':
                         if ( ! toptions(&outtype, optarg))                          use_pager = 0;
                                 return(0);  
                         break;                          break;
                 case ('W'):                  case 'f':
                         if ( ! woptions(&curp.wflags, optarg))                          search.argmode = ARG_WORD;
                                 return(0);  
                         break;                          break;
                 case ('V'):                  case 'h':
                           (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
   #if HAVE_SQLITE3
                           synopsis_only = 1;
   #endif
                           use_pager = 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':
   #if HAVE_SQLITE3
                           defpaths = optarg;
   #endif
                           break;
                   case 'm':
                           auxpaths = optarg;
                           break;
                   case 'O':
                           search.outkey = optarg;
                           (void)strlcat(curp.outopts, optarg, BUFSIZ);
                           (void)strlcat(curp.outopts, ",", BUFSIZ);
                           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;
                   case 'V':
                         version();                          version();
                         /* NOTREACHED */                          /* NOTREACHED */
                 default:                  default:
                         usage();                          show_usage = 1;
                         /* NOTREACHED */                          break;
                 }                  }
           }
   
         argc -= optind;          if (show_usage)
         argv += optind;                  usage(search.argmode);
   
         /*          /* Postprocess options. */
          * Allocate the appropriate front-end.  Note that utf8, latin1  
          * (both not yet implemented) and ascii all resolve to the  
          * terminal front-end with different encodings (see terminal.c).  
          * Not all frontends have cleanup or alloc routines.  
          */  
   
         switch (outtype) {          if (outmode == OUTMODE_DEF) {
         case (OUTT_TREE):                  switch (search.argmode) {
                 outdata = NULL;                  case ARG_FILE:
                 outrun = tree_run;                          outmode = OUTMODE_ALL;
                 outfree = NULL;                          use_pager = 0;
                 break;                          break;
         case (OUTT_LINT):                  case ARG_NAME:
                 outdata = NULL;                          outmode = OUTMODE_ONE;
                 outrun = NULL;                          break;
                 outfree = NULL;                  default:
                 break;                          outmode = OUTMODE_LST;
         default:                          break;
                 outdata = ascii_alloc();                  }
                 outrun = terminal_run;  
                 outfree = terminal_free;  
                 break;  
         }          }
   
         /* Configure buffers. */          /* Parse arguments. */
   
         bzero(&ln, sizeof(struct buf));          argc -= optind;
         bzero(&blk, sizeof(struct buf));          argv += optind;
   #if HAVE_SQLITE3
           resp = NULL;
   #endif
   
         /*          /*
          * Main loop around available files.           * Quirks for help(1)
            * and for a man(1) section argument without -s.
          */           */
   
         if (NULL == *argv) {          if (search.argmode == ARG_NAME) {
                 rc = 0;                  if (*progname == 'h') {
                 c = fstdin(&blk, &ln, &curp);                          if (argc == 0) {
                                   argv = help_argv;
                 if (c && NULL == outrun)                                  argc = 1;
                         rc = 1;  
                 /*else if (c && outrun && (*outrun)(outdata, curp.man, curp.mdoc))  
                         rc = 1;*/  
         } else {  
                 while (*argv) {  
                         c = ffile(&blk, &ln, *argv, &curp);  
                         if ( ! c)  
                                 break;  
                         /*if (outrun && ! (*outrun)(outdata, curp.man, curp.mdoc))  
                                 break;*/  
                         if (curp.man)  
                                 man_reset(curp.man);  
                         if (curp.mdoc && ! mdoc_reset(curp.mdoc)) {  
                                 warnx("memory exhausted");  
                                 break;  
                         }                          }
                   } else if (argc > 1 &&
                       ((uc = argv[0]) != NULL) &&
                       ((isdigit(uc[0]) && (uc[1] == '\0' ||
                         (isalpha(uc[1]) && uc[2] == '\0'))) ||
                        (uc[0] == 'n' && uc[1] == '\0'))) {
                           search.sec = uc;
                         argv++;                          argv++;
                           argc--;
                 }                  }
                 rc = NULL == *argv;  
         }          }
   
         if (blk.buf)          rc = MANDOCLEVEL_OK;
                 free(blk.buf);  
         if (ln.buf)  
                 free(ln.buf);  
         if (outfree)  
                 (*outfree)(outdata);  
         if (curp.mdoc)  
                 mdoc_free(curp.mdoc);  
         if (curp.man)  
                 man_free(curp.man);  
   
         return(rc ? EXIT_SUCCESS : EXIT_FAILURE);          /* man(1), whatis(1), apropos(1) */
 }  
   
           if (search.argmode != ARG_FILE) {
   #if HAVE_SQLITE3
                   if (argc == 0)
                           usage(search.argmode);
   
 __dead static void                  if (search.argmode == ARG_NAME &&
 version(void)                      outmode == OUTMODE_ONE)
 {                          search.firstmatch = 1;
   
         (void)printf("%s %s\n", __progname, VERSION);                  /* Access the mandoc database. */
         exit(EXIT_SUCCESS);  
 }  
   
                   manpath_parse(&paths, conf_file, defpaths, auxpaths);
                   mansearch_setup(1);
                   if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
                           usage(search.argmode);
                   resp = res;
   
 __dead static void                  if (sz == 0) {
 usage(void)                          if (search.argmode == ARG_NAME)
 {                                  fprintf(stderr, "%s: No entry for %s "
                                       "in the manual.\n", progname, argv[0]);
                           rc = MANDOCLEVEL_BADARG;
                           goto out;
                   }
   
         (void)fprintf(stderr, "usage: %s [-V] [-foption...] "                  /*
                         "[-mformat] [-Toutput] [-Werr...]\n",                   * For standard man(1) and -a output mode,
                         __progname);                   * prepare for copying filename pointers
         exit(EXIT_FAILURE);                   * into the program parameter array.
 }                   */
   
                   if (outmode == OUTMODE_ONE) {
                           argc = 1;
                           best_prio = 10;
                   } else if (outmode == OUTMODE_ALL)
                           argc = (int)sz;
   
 static struct man *                  /* Iterate all matching manuals. */
 man_init(struct curparse *curp)  
 {  
         int              pflags;  
         struct man      *man;  
         struct man_cb    mancb;  
   
         mancb.man_err = merr;                  for (i = 0; i < sz; i++) {
         mancb.man_warn = manwarn;                          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;
                           }
                   }
   
         /* Set command defaults. */                  /*
         pflags = MAN_IGN_MACRO;                   * 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 (curp->fflags & IGN_MACRO)                  if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
                 pflags |= MAN_IGN_MACRO;                          goto out;
         if (curp->fflags & NO_IGN_MACRO)  #else
                 pflags &= ~MAN_IGN_MACRO;                  fputs("mandoc: database support not compiled in\n",
                       stderr);
                   return((int)MANDOCLEVEL_BADARG);
   #endif
           }
   
         if (NULL == (man = man_alloc(curp, pflags, &mancb)))          /* mandoc(1) */
                 warnx("memory exhausted");  
   
         return(man);          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);
   
 static struct mdoc *          /*
 mdoc_init(struct curparse *curp)           * Conditionally start up the lookaside buffer before parsing.
 {           */
         int              pflags;          if (OUTT_MAN == curp.outtype)
         struct mdoc     *mdoc;                  mparse_keep(curp.mp);
         struct mdoc_cb   mdoccb;  
   
         mdoccb.mdoc_msg = NULL;          if (argc == 0) {
         mdoccb.mdoc_err = merr;                  if (use_pager && isatty(STDOUT_FILENO))
         mdoccb.mdoc_warn = mdocwarn;                          spawn_pager();
                   parse(&curp, STDIN_FILENO, "<stdin>", &rc);
           }
   
         pflags = 0;          while (argc) {
                   rc = mparse_open(curp.mp, &fd,
   #if HAVE_SQLITE3
                       resp != NULL ? resp->file :
   #endif
                       *argv);
   
         if (curp->fflags & IGN_SCOPE)                  if (fd != -1) {
                 pflags |= MDOC_IGN_SCOPE;                          if (use_pager && isatty(STDOUT_FILENO))
         if (curp->fflags & IGN_ESCAPE)                                  spawn_pager();
                 pflags |= MDOC_IGN_ESCAPE;                          use_pager = 0;
         if (curp->fflags & IGN_MACRO)  
                 pflags |= MDOC_IGN_MACRO;  
   
         if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))  #if HAVE_SQLITE3
                 warnx("memory allocated");                          if (resp == NULL)
   #endif
                                   parse(&curp, fd, *argv, &rc);
   #if HAVE_SQLITE3
                           else if (resp->form & FORM_SRC) {
                                   /* For .so only; ignore failure. */
                                   chdir(paths.paths[resp->ipath]);
                                   parse(&curp, fd, resp->file, &rc);
                           } else
                                   rc = passthrough(resp->file, fd,
                                       synopsis_only);
   #endif
   
         return(mdoc);                          if (mparse_wait(curp.mp) != MANDOCLEVEL_OK)
 }                                  rc = MANDOCLEVEL_SYSERR;
   
                           if (argc > 1 && curp.outtype <= OUTT_UTF8)
                                   ascii_sepline(curp.outdata);
                   }
   
 static int                  if (MANDOCLEVEL_OK != rc && curp.wstop)
 fstdin(struct buf *blk, struct buf *ln, struct curparse *curp)                          break;
 {  
   
         curp->file = "<stdin>";  #if HAVE_SQLITE3
         curp->fd = STDIN_FILENO;                  if (resp != NULL)
         return(fdesc(blk, ln, curp));                          resp++;
                   else
   #endif
                           argv++;
                   if (--argc)
                           mparse_reset(curp.mp);
           }
   
           if (curp.outfree)
                   (*curp.outfree)(curp.outdata);
           mparse_free(curp.mp);
           mchars_free(curp.mchars);
   
   #if HAVE_SQLITE3
   out:
           if (search.argmode != ARG_FILE) {
                   manpath_free(&paths);
                   mansearch_free(res, sz);
                   mansearch_setup(0);
           }
   #endif
   
           free(defos);
   
           return((int)rc);
 }  }
   
   static void
   version(void)
   {
   
 static int          printf("mandoc %s\n", VERSION);
 ffile(struct buf *blk, struct buf *ln,          exit((int)MANDOCLEVEL_OK);
                 const char *file, struct curparse *curp)  }
   
   static void
   usage(enum argmode argmode)
 {  {
         int              c;  
   
         curp->file = file;          switch (argmode) {
         if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {          case ARG_FILE:
                 warn("%s", curp->file);                  fputs("usage: mandoc [-acfhklV] [-Ios=name] "
                 return(0);                      "[-Kencoding] [-mformat] [-Ooption]\n"
                       "\t      [-Toutput] [-Wlevel] [file ...]\n", stderr);
                   break;
           case ARG_NAME:
                   fputs("usage: man [-acfhklVw] [-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] "
                       "[-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] "
                       "[-M path] [-m path] [-O outkey] [-S arch]\n"
                       "\t       [-s section] expression ...\n", stderr);
                   break;
         }          }
           exit((int)MANDOCLEVEL_BADARG);
   }
   
         c = fdesc(blk, ln, curp);  static void
   parse(struct curparse *curp, int fd, const char *file,
           enum mandoclevel *level)
   {
           enum mandoclevel  rc;
           struct mdoc      *mdoc;
           struct man       *man;
   
         if (-1 == close(curp->fd))          /* Begin by parsing the file itself. */
                 warn("%s", curp->file);  
   
         return(c);          assert(file);
 }          assert(fd >= -1);
   
           rc = mparse_readfd(curp->mp, fd, file);
   
 static int          /* Stop immediately if the parse has failed. */
 fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)  
 {  
         size_t           sz;  
         ssize_t          ssz;  
         struct stat      st;  
         int              j, i, pos, lnn;  
         struct man      *man;  
         struct mdoc     *mdoc;  
   
         sz = BUFSIZ;          if (MANDOCLEVEL_FATAL <= rc)
         man = NULL;                  goto cleanup;
         mdoc = NULL;  
   
         /*          /*
          * Two buffers: ln and buf.  buf is the input buffer optimised           * With -Wstop and warnings or errors of at least the requested
          * here 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(curp->fd, &st))          if (MANDOCLEVEL_OK != rc && curp->wstop)
                 warnx("%s", curp->file);                  goto cleanup;
         else if ((size_t)st.st_blksize > sz)  
                 sz = st.st_blksize;  
   
         if (sz > blk->sz) {          /* If unset, allocate output dev now (if applicable). */
                 blk->buf = realloc(blk->buf, sz);  
                 if (NULL == blk->buf) {          if ( ! (curp->outman && curp->outmdoc)) {
                         warn("realloc");                  switch (curp->outtype) {
                         return(0);                  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;
                 }                  }
                 blk->sz = sz;  
         }  
   
         /* Fill buf with file blocksize. */                  switch (curp->outtype) {
                   case OUTT_HTML:
         for (lnn = 0, pos = 0; ; ) {                          curp->outman = html_man;
                 if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {                          curp->outmdoc = html_mdoc;
                         warn("%s", curp->file);  
                         return(0);  
                 } 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;
                   }
           }
   
                 /* Parse the read block into partial or full lines. */          mparse_result(curp->mp, &mdoc, &man, NULL);
   
                 for (i = 0; i < (int)ssz; i++) {          /* Execute the out device, if it exists. */
                         if (pos >= (int)ln->sz) {  
                                 ln->sz += 256; /* Step-size. */  
                                 ln->buf = realloc(ln->buf, ln->sz);  
                                 if (NULL == ln->buf) {  
                                         warn("realloc");  
                                         return(0);  
                                 }  
                         }  
   
                         if ('\n' != blk->buf[i]) {          if (man && curp->outman)
                                 ln->buf[pos++] = blk->buf[i];                  (*curp->outman)(curp->outdata, man);
                                 continue;          if (mdoc && curp->outmdoc)
                         }                  (*curp->outmdoc)(curp->outdata, mdoc);
   
                         /* Check for CPP-escaped newline.  */  cleanup:
           if (*level < rc)
                   *level = rc;
   }
   
                         if (pos > 0 && '\\' == ln->buf[pos - 1]) {  #if HAVE_SQLITE3
                                 for (j = pos - 1; j >= 0; j--)  static enum mandoclevel
                                         if ('\\' != ln->buf[j])  passthrough(const char *file, int fd, int synopsis_only)
                                                 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;
   
                         ln->buf[pos] = 0;          fflush(stdout);
                         lnn++;  
   
                         /*  
                          * If no manual parser has been assigned, then  
                          * try to assign one in pset(), which may do  
                          * nothing at all.  After this, parse the manual  
                          * line accordingly.  
                          */  
   
                         if ( ! (man || mdoc) && ! pset(ln->buf,          if ((stream = fdopen(fd, "r")) == NULL) {
                                                 pos, curp, &man, &mdoc))                  close(fd);
                                 return(0);                  syscall = "fdopen";
                   goto fail;
           }
   
                         pos = 0;          print = 0;
           while ((line = fgetln(stream, &len)) != NULL) {
                         if (man && ! man_parseln(man, lnn, ln->buf))                  if (synopsis_only) {
                                 return(0);                          if (print) {
                         if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))                                  if ( ! isspace((unsigned char)*line))
                                 return(0);                                          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;
                           }
                 }                  }
                   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;
                           }
         }          }
   
         /* Note that a parser may not have been assigned, yet. */          if (ferror(stream)) {
                   fclose(stream);
                   syscall = "fgetln";
                   goto fail;
           }
   
         if (mdoc)  done:
                return(mdoc_endparse(mdoc));          fclose(stream);
         if (man)          return(MANDOCLEVEL_OK);
                 return(man_endparse(man));  
   
         warnx("%s: not a manual", curp->file);  fail:
         return(0);          fprintf(stderr, "%s: %s: SYSERR: %s: %s",
               progname, file, syscall, strerror(errno));
           return(MANDOCLEVEL_SYSERR);
 }  }
   #endif
   
   
 static int  static int
 pset(const char *buf, size_t pos, struct curparse *curp,  koptions(int *options, char *arg)
                 struct man **man, struct mdoc **mdoc)  
 {  {
   
         /*          if ( ! strcmp(arg, "utf-8")) {
          * Try to intuit which kind of manual parser should be used.  If                  *options |=  MPARSE_UTF8;
          * passed in by command-line (-man, -mdoc), then use that                  *options &= ~MPARSE_LATIN1;
          * explicitly.  If passed as -mandoc, then try to guess from the          } else if ( ! strcmp(arg, "iso-8859-1")) {
          * line: either skip comments, use -mdoc when finding `.Dt', or                  *options |=  MPARSE_LATIN1;
          * default to -man, which is more lenient.                  *options &= ~MPARSE_UTF8;
          */          } else if ( ! strcmp(arg, "us-ascii")) {
                   *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
         if (pos >= 3 && 0 == memcmp(buf, ".\\\"", 3))          } else {
                 return(1);                  fprintf(stderr, "%s: -K %s: Bad argument\n",
                       progname, arg);
         switch (curp->inttype) {  
         case (INTT_MDOC):  
                 if (NULL == curp->mdoc)  
                         curp->mdoc = mdoc_init(curp);  
                 if (NULL == (*mdoc = curp->mdoc))  
                         return(0);  
                 warnx("inheriting -mdoc parser");  
                 return(1);  
         case (INTT_MAN):  
                 if (NULL == curp->man)  
                         curp->man = man_init(curp);  
                 if (NULL == (*man = curp->man))  
                         return(0);  
                 warnx("inheriting -man parser");  
                 return(1);  
         default:  
                 break;  
         }  
   
         if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {  
                 if (NULL == curp->mdoc)  
                         curp->mdoc = mdoc_init(curp);  
                 if (NULL == (*mdoc = curp->mdoc))  
                         return(0);  
                 return(1);  
         }  
   
         if (NULL == curp->man)  
                 curp->man = man_init(curp);  
         if (NULL == (*man = curp->man))  
                 return(0);                  return(0);
           }
         return(1);          return(1);
 }  }
   
   
 static int  static int
 moptions(enum intt *tflags, char *arg)  moptions(int *options, char *arg)
 {  {
   
         if (0 == strcmp(arg, "doc"))          if (arg == NULL)
                 *tflags = INTT_MDOC;                  /* nothing to do */;
           else if (0 == strcmp(arg, "doc"))
                   *options |= MPARSE_MDOC;
         else if (0 == strcmp(arg, "andoc"))          else if (0 == strcmp(arg, "andoc"))
                 *tflags = INTT_AUTO;                  /* nothing to do */;
         else if (0 == strcmp(arg, "an"))          else if (0 == strcmp(arg, "an"))
                 *tflags = INTT_MAN;                  *options |= MPARSE_MAN;
         else {          else {
                 warnx("bad argument: -m%s", arg);                  fprintf(stderr, "%s: -m %s: Bad argument\n",
                       progname, arg);
                 return(0);                  return(0);
         }          }
   
         return(1);          return(1);
 }  }
   
   
 static int  static int
 toptions(enum outt *tflags, char *arg)  toptions(struct curparse *curp, char *arg)
 {  {
   
         if (0 == strcmp(arg, "ascii"))          if (0 == strcmp(arg, "ascii"))
                 *tflags = OUTT_ASCII;                  curp->outtype = OUTT_ASCII;
         else if (0 == strcmp(arg, "lint"))          else if (0 == strcmp(arg, "lint")) {
                 *tflags = OUTT_LINT;                  curp->outtype = OUTT_LINT;
         else if (0 == strcmp(arg, "tree"))                  curp->wlevel  = MANDOCLEVEL_WARNING;
                 *tflags = OUTT_TREE;          } 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 {          else {
                 warnx("bad argument: -T%s", arg);                  fprintf(stderr, "%s: -T %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)  woptions(struct curparse *curp, char *arg)
 {  {
         char            *v;          char            *v, *o;
         char            *toks[5];          const char      *toks[6];
   
         toks[0] = "ign-scope";          toks[0] = "stop";
         toks[1] = "ign-escape";          toks[1] = "all";
         toks[2] = "ign-macro";          toks[2] = "warning";
         toks[4] = "no-ign-macro";          toks[3] = "error";
           toks[4] = "fatal";
         toks[5] = NULL;          toks[5] = NULL;
   
         while (*arg)          while (*arg) {
                 switch (getsubopt(&arg, toks, &v)) {                  o = arg;
                 case (0):                  switch (getsubopt(&arg, UNCONST(toks), &v)) {
                         *fflags |= IGN_SCOPE;                  case 0:
                           curp->wstop = 1;
                         break;                          break;
                 case (1):                  case 1:
                         *fflags |= IGN_ESCAPE;                          /* FALLTHROUGH */
                   case 2:
                           curp->wlevel = MANDOCLEVEL_WARNING;
                         break;                          break;
                 case (2):                  case 3:
                         *fflags |= IGN_MACRO;                          curp->wlevel = MANDOCLEVEL_ERROR;
                         break;                          break;
                 case (3):                  case 4:
                         *fflags |= NO_IGN_MACRO;                          curp->wlevel = MANDOCLEVEL_FATAL;
                         break;                          break;
                 default:                  default:
                         warnx("bad argument: -f%s", arg);                          fprintf(stderr, "%s: -W %s: Bad argument\n",
                               progname, o);
                         return(0);                          return(0);
                 }                  }
           }
   
         return(1);          return(1);
 }  }
   
   static void
 /*  mmsg(enum mandocerr t, enum mandoclevel lvl,
  * Parse out the options for [-Werr...], which sets warning modes.                  const char *file, int line, int col, const char *msg)
  * These can be comma-delimited or called again.  
  */  
 static int  
 woptions(int *wflags, char *arg)  
 {  {
         char            *v;          const char      *mparse_msg;
         char            *toks[5];  
   
         toks[0] = "all";          fprintf(stderr, "%s: %s:", progname, file);
         toks[1] = "compat";  
         toks[2] = "syntax";  
         toks[3] = "error";  
         toks[4] = NULL;  
   
         while (*arg)          if (line)
                 switch (getsubopt(&arg, toks, &v)) {                  fprintf(stderr, "%d:%d:", line, col + 1);
                 case (0):  
                         *wflags |= WARN_WALL;  
                         break;  
                 case (1):  
                         *wflags |= WARN_WCOMPAT;  
                         break;  
                 case (2):  
                         *wflags |= WARN_WSYNTAX;  
                         break;  
                 case (3):  
                         *wflags |= WARN_WERR;  
                         break;  
                 default:  
                         warnx("bad argument: -W%s", arg);  
                         return(0);  
                 }  
   
         return(1);          fprintf(stderr, " %s", mparse_strlevel(lvl));
 }  
   
           if (NULL != (mparse_msg = mparse_strerror(t)))
                   fprintf(stderr, ": %s", mparse_msg);
   
 /* ARGSUSED */          if (msg)
 static int                  fprintf(stderr, ": %s", msg);
 merr(void *arg, int line, int col, const char *msg)  
 {  
         struct curparse *curp;  
   
         curp = (struct curparse *)arg;          fputc('\n', stderr);
   
         warnx("%s:%d: error: %s (column %d)",  
                         curp->file, line, msg, col);  
         return(0);  
 }  }
   
   static void
 static int  spawn_pager(void)
 mdocwarn(void *arg, int line, int col,  
                 enum mdoc_warn type, const char *msg)  
 {  {
         struct curparse *curp;  #define MAX_PAGER_ARGS 16
         char            *wtype;          char            *argv[MAX_PAGER_ARGS];
           const char      *pager;
           char            *cp;
           int              fildes[2];
           int              argc;
   
         curp = (struct curparse *)arg;          if (pipe(fildes) == -1) {
         wtype = NULL;                  fprintf(stderr, "%s: pipe: %s\n",
                       progname, strerror(errno));
                   return;
           }
   
         switch (type) {          switch (fork()) {
         case (WARN_COMPAT):          case -1:
                 wtype = "compat";                  fprintf(stderr, "%s: fork: %s\n",
                 if (curp->wflags & WARN_WCOMPAT)                      progname, strerror(errno));
                         break;                  exit((int)MANDOCLEVEL_SYSERR);
                 return(1);          case 0:
         case (WARN_SYNTAX):                  close(fildes[0]);
                 wtype = "syntax";                  if (dup2(fildes[1], STDOUT_FILENO) == -1) {
                 if (curp->wflags & WARN_WSYNTAX)                          fprintf(stderr, "%s: dup output: %s\n",
                         break;                              progname, strerror(errno));
                 return(1);                          exit((int)MANDOCLEVEL_SYSERR);
                   }
                   return;
           default:
                   break;
         }          }
   
         assert(wtype);          /* The original process becomes the pager. */
         warnx("%s:%d: %s warning: %s (column %d)",  
                         curp->file, line, wtype, msg, col);  
   
         if ( ! (curp->wflags & 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);
           }
   
         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 = "/usr/bin/more -s";
           cp = mandoc_strdup(pager);
   
           /*
            * Parse the pager command into words.
            * Intentionally do not do anything fancy here.
            */
   
 static int          argc = 0;
 manwarn(void *arg, int line, int col, const char *msg)          while (argc + 1 < MAX_PAGER_ARGS) {
 {                  argv[argc++] = cp;
         struct curparse *curp;                  cp = strchr(cp, ' ');
                   if (cp == NULL)
                           break;
                   *cp++ = '\0';
                   while (*cp == ' ')
                           cp++;
                   if (*cp == '\0')
                           break;
           }
           argv[argc] = NULL;
   
         curp = (struct curparse *)arg;          /* Hand over to the pager. */
   
         if ( ! (curp->wflags & WARN_WSYNTAX))          execvp(argv[0], argv);
                 return(1);          fprintf(stderr, "%s: exec: %s\n",
               progname, strerror(errno));
         warnx("%s:%d: syntax warning: %s (column %d)",          exit((int)MANDOCLEVEL_SYSERR);
                         curp->file, line, msg, col);  
   
         if ( ! (curp->wflags & WARN_WERR))  
                 return(1);  
   
         warnx("%s: considering warnings as errors",  
                         __progname);  
         return(0);  
 }  }

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.212

CVSweb