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

Diff for /mandoc/main.c between version 1.9 and 1.16

version 1.9, 2009/03/22 19:10:48 version 1.16, 2009/03/25 21:46:24
Line 27 
Line 27 
 #include <unistd.h>  #include <unistd.h>
   
 #include "mdoc.h"  #include "mdoc.h"
   #include "man.h"
   
   /* Account for FreeBSD and Linux in our declarations. */
   
 #ifdef __linux__  #ifdef __linux__
 extern  int               getsubopt(char **, char * const *, char **);  extern  int               getsubopt(char **, char * const *, char **);
 # ifndef __dead  # ifndef __dead
 #  define __dead __attribute__((__noreturn__))  #  define __dead __attribute__((__noreturn__))
 # endif  # endif
   #elif defined(__dead2)
   # ifndef __dead
   #  define __dead __dead2
   # endif
 #endif  #endif
   
 struct  buf {  struct  buf {
Line 49  struct curparse {
Line 56  struct curparse {
 #define WARN_WERR        (1 << 2)       /* Warnings->errors. */  #define WARN_WERR        (1 << 2)       /* Warnings->errors. */
 };  };
   
 enum outt {  #define IGN_SCOPE        (1 << 0)       /* Ignore scope errors. */
         OUTT_ASCII,  #define IGN_ESCAPE       (1 << 1)       /* Ignore bad escapes. */
         OUTT_LATIN1,  #define IGN_MACRO        (1 << 2)       /* Ignore unknown macros. */
         OUTT_UTF8,  
   enum    intt {
           INTT_MDOC = 0,
           INTT_MAN
   };
   
   enum    outt {
           OUTT_ASCII = 0,
         OUTT_TREE,          OUTT_TREE,
         OUTT_LINT          OUTT_LINT
 };  };
   
 typedef int             (*out_run)(void *, const struct mdoc *);  typedef int             (*out_run)(void *, const struct man *,
                                   const struct mdoc *);
 typedef void            (*out_free)(void *);  typedef void            (*out_free)(void *);
   
 extern  char             *__progname;  extern  char             *__progname;
   
 extern  void             *ascii_alloc(void);  extern  void             *ascii_alloc(void);
 extern  void             *latin1_alloc(void);  extern  int               terminal_run(void *, const struct man *,
 extern  void             *utf8_alloc(void);                                  const struct mdoc *);
 extern  int               terminal_run(void *, const struct mdoc *);  extern  int               tree_run(void *, const struct man *,
 extern  int               tree_run(void *, const struct mdoc *);                                  const struct mdoc *);
 extern  void              terminal_free(void *);  extern  void              terminal_free(void *);
   
 __dead  static void       version(void);  
 __dead  static void       usage(void);  
 static  int               foptions(int *, char *);  static  int               foptions(int *, char *);
 static  int               toptions(enum outt *, char *);  static  int               toptions(enum outt *, char *);
   static  int               moptions(enum intt *, char *);
 static  int               woptions(int *, char *);  static  int               woptions(int *, char *);
 static  int               merr(void *, int, int, const char *);  static  int               merr(void *, int, int, const char *);
 static  int               mwarn(void *, int, int,  static  int               manwarn(void *, int, int, const char *);
   static  int               mdocwarn(void *, int, int,
                                 enum mdoc_warn, const char *);                                  enum mdoc_warn, const char *);
 static  int               file(struct buf *, struct buf *,  static  int               file(struct buf *, struct buf *,
                                 const char *, struct mdoc *);                                  const char *,
                                   struct man *, struct mdoc *);
 static  int               fdesc(struct buf *, struct buf *,  static  int               fdesc(struct buf *, struct buf *,
                                 const char *, int, struct mdoc *);                                  const char *, int,
                                   struct man *, struct mdoc *);
   __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, fflags;          int              c, rc, fflags, pflags;
         struct mdoc_cb   cb;          struct mdoc_cb   mdoccb;
           struct man_cb    mancb;
           struct man      *man;
         struct mdoc     *mdoc;          struct mdoc     *mdoc;
         void            *outdata;          void            *outdata;
         enum outt        outtype;          enum outt        outtype;
           enum intt        inttype;
         struct buf       ln, blk;          struct buf       ln, blk;
         out_run          outrun;          out_run          outrun;
         out_free         outfree;          out_free         outfree;
Line 98  main(int argc, char *argv[])
Line 120  main(int argc, char *argv[])
   
         fflags = 0;          fflags = 0;
         outtype = OUTT_ASCII;          outtype = OUTT_ASCII;
           inttype = INTT_MDOC;
   
         bzero(&curp, sizeof(struct curparse));          bzero(&curp, sizeof(struct curparse));
   
         /* LINTED */          /* LINTED */
         while (-1 != (c = getopt(argc, argv, "f:VW:T:")))          while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))
                 switch (c) {                  switch (c) {
                 case ('f'):                  case ('f'):
                         if ( ! foptions(&fflags, optarg))                          if ( ! foptions(&fflags, optarg))
                                 return(0);                                  return(0);
                         break;                          break;
                   case ('m'):
                           if ( ! moptions(&inttype, optarg))
                                   return(0);
                           break;
                 case ('T'):                  case ('T'):
                         if ( ! toptions(&outtype, optarg))                          if ( ! toptions(&outtype, optarg))
                                 return(0);                                  return(0);
Line 128  main(int argc, char *argv[])
Line 155  main(int argc, char *argv[])
         argv += optind;          argv += optind;
   
         /*          /*
          * Allocate the appropriate front-end.  Note that utf8, ascii           * Allocate the appropriate front-end.  Note that utf8, latin1
          * and latin1 all resolve to the terminal front-end with           * (both not yet implemented) and ascii all resolve to the
          * different encodings (see terminal.c).  Not all frontends have           * terminal front-end with different encodings (see terminal.c).
          * cleanup or alloc routines.           * Not all frontends have cleanup or alloc routines.
          */           */
   
         switch (outtype) {          switch (outtype) {
         case (OUTT_LATIN1):  
                 outdata = latin1_alloc();  
                 outrun = terminal_run;  
                 outfree = terminal_free;  
                 break;  
         case (OUTT_UTF8):  
                 outdata = utf8_alloc();  
                 outrun = terminal_run;  
                 outfree = terminal_free;  
                 break;  
         case (OUTT_TREE):          case (OUTT_TREE):
                 outdata = NULL;                  outdata = NULL;
                 outrun = tree_run;                  outrun = tree_run;
Line 167  main(int argc, char *argv[])
Line 184  main(int argc, char *argv[])
          * screen.  XXX - for now, no path for debugging messages.           * screen.  XXX - for now, no path for debugging messages.
          */           */
   
         cb.mdoc_msg = NULL;          mdoccb.mdoc_msg = NULL;
         cb.mdoc_err = merr;          mdoccb.mdoc_err = merr;
         cb.mdoc_warn = mwarn;          mdoccb.mdoc_warn = mdocwarn;
   
           mancb.man_err = merr;
           mancb.man_warn = manwarn;
   
           /* Configure buffers. */
   
         bzero(&ln, sizeof(struct buf));          bzero(&ln, sizeof(struct buf));
         bzero(&blk, sizeof(struct buf));          bzero(&blk, sizeof(struct buf));
   
         mdoc = mdoc_alloc(&curp, fflags, &cb);          man = NULL;
           mdoc = NULL;
           pflags = 0;
   
         /*          /*
          * Loop around available files.           * Allocate the parser.  There are two kinds of parser: libman
            * and libmdoc.  We must separately copy over the flags that
            * we'll use internally.
          */           */
   
           switch (inttype) {
           case (INTT_MAN):
                   if (fflags & IGN_MACRO)
                           pflags |= MAN_IGN_MACRO;
                   man = man_alloc(&curp, pflags, &mancb);
                   break;
           default:
                   if (fflags & IGN_SCOPE)
                           pflags |= MDOC_IGN_SCOPE;
                   if (fflags & IGN_ESCAPE)
                           pflags |= MDOC_IGN_ESCAPE;
                   if (fflags & IGN_MACRO)
                           pflags |= MDOC_IGN_MACRO;
                   mdoc = mdoc_alloc(&curp, pflags, &mdoccb);
                   break;
           }
   
           /*
            * Main loop around available files.
            */
   
         if (NULL == *argv) {          if (NULL == *argv) {
                 curp.file = "<stdin>";                  curp.file = "<stdin>";
                 c = fdesc(&blk, &ln, "stdin", STDIN_FILENO, mdoc);  
                 rc = 0;                  rc = 0;
                   c = fdesc(&blk, &ln, "stdin", STDIN_FILENO, man, mdoc);
   
                 if (c && NULL == outrun)                  if (c && NULL == outrun)
                         rc = 1;                          rc = 1;
                 else if (c && outrun && (*outrun)(outdata, mdoc))                  else if (c && outrun && (*outrun)(outdata, man, mdoc))
                         rc = 1;                          rc = 1;
         } else {          } else {
                 while (*argv) {                  while (*argv) {
                         curp.file = *argv;                          curp.file = *argv;
                         c = file(&blk, &ln, *argv, mdoc);                          c = file(&blk, &ln, *argv, man, mdoc);
                         if ( ! c)                          if ( ! c)
                                 break;                                  break;
                         if (outrun && ! (*outrun)(outdata, mdoc))                          if (outrun && ! (*outrun)(outdata, man, mdoc))
                                 break;                                  break;
                         /* Reset the parser for another file. */                          if (man)
                         mdoc_reset(mdoc);                                  man_reset(man);
                           if (mdoc)
                                   mdoc_reset(mdoc);
                         argv++;                          argv++;
                 }                  }
                 rc = NULL == *argv;                  rc = NULL == *argv;
Line 209  main(int argc, char *argv[])
Line 259  main(int argc, char *argv[])
                 free(ln.buf);                  free(ln.buf);
         if (outfree)          if (outfree)
                 (*outfree)(outdata);                  (*outfree)(outdata);
           if (mdoc)
                   mdoc_free(mdoc);
           if (man)
                   man_free(man);
   
         mdoc_free(mdoc);  
   
         return(rc ? EXIT_SUCCESS : EXIT_FAILURE);          return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
 }  }
   
Line 230  __dead static void
Line 282  __dead static void
 usage(void)  usage(void)
 {  {
   
         (void)fprintf(stderr, "usage: %s\n", __progname);          (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
                           "[-mformat] [-Toutput] [-Werr...]\n",
                           __progname);
         exit(1);          exit(1);
         /* NOTREACHED */          /* NOTREACHED */
 }  }
   
   
 static int  static int
 file(struct buf *blk, struct buf *ln,  file(struct buf *blk, struct buf *ln, const char *file,
                 const char *file, struct mdoc *mdoc)                  struct man *man, struct mdoc *mdoc)
 {  {
         int              fd, c;          int              fd, c;
   
Line 247  file(struct buf *blk, struct buf *ln,
Line 301  file(struct buf *blk, struct buf *ln,
                 return(0);                  return(0);
         }          }
   
         c = fdesc(blk, ln, file, fd, mdoc);          c = fdesc(blk, ln, file, fd, man, mdoc);
   
         if (-1 == close(fd))          if (-1 == close(fd))
                 warn("%s", file);                  warn("%s", file);
Line 258  file(struct buf *blk, struct buf *ln,
Line 312  file(struct buf *blk, struct buf *ln,
   
 static int  static int
 fdesc(struct buf *blk, struct buf *ln,  fdesc(struct buf *blk, struct buf *ln,
                 const char *f, int fd, struct mdoc *mdoc)                  const char *f, int fd,
                   struct man *man, struct mdoc *mdoc)
 {  {
         size_t           sz;          size_t           sz;
         ssize_t          ssz;          ssize_t          ssz;
         struct stat      st;          struct stat      st;
         int              j, i, pos, lnn;          int              j, i, pos, lnn;
 #ifdef  STRIP_XO  
         int              macro, xo, xeoln;  
 #endif  
   
           assert( ! (man && mdoc));
   
         /*          /*
          * Two buffers: ln and buf.  buf is the input buffer, optimised           * Two buffers: ln and buf.  buf is the input buffer, optimised
          * for each file's block size.  ln is a line buffer.  Both           * for each file's block size.  ln is a line buffer.  Both
Line 291  fdesc(struct buf *blk, struct buf *ln,
Line 345  fdesc(struct buf *blk, struct buf *ln,
         /*          /*
          * Fill buf with file blocksize and parse newlines into ln.           * Fill buf with file blocksize and parse newlines into ln.
          */           */
 #ifdef  STRIP_XO  
         macro = xo = xeoln = 0;  
 #endif  
   
         for (lnn = 1, pos = 0; ; ) {          for (lnn = 1, pos = 0; ; ) {
                 if (-1 == (ssz = read(fd, blk->buf, sz))) {                  if (-1 == (ssz = read(fd, blk->buf, sz))) {
Line 311  fdesc(struct buf *blk, struct buf *ln,
Line 362  fdesc(struct buf *blk, struct buf *ln,
                         }                          }
   
                         if ('\n' != blk->buf[i]) {                          if ('\n' != blk->buf[i]) {
                                 /*  
                                  * Ugly of uglies.  Here we handle the  
                                  * dreaded `Xo/Xc' scoping.  Cover the  
                                  * eyes of any nearby children.  This  
                                  * makes `Xo/Xc' enclosures look like  
                                  * one huge line.  
                                  */  
 #ifdef  STRIP_XO  
                                 /*  
                                  * First, note whether we're in a macro  
                                  * line.  
                                  */  
                                 if (0 == pos && '.' == blk->buf[i])  
                                         macro = 1;  
   
                                 /*  
                                  * If we're in an `Xo' context and just  
                                  * nixed a newline, remove the control  
                                  * character for new macro lines:  
                                  * they're going to show up as all part  
                                  * of the same line.  
                                  */  
                                 if (xo && xeoln && '.' == blk->buf[i]) {  
                                         xeoln = 0;  
                                         continue;  
                                 }  
                                 xeoln = 0;  
   
                                 /*  
                                  * If we've parsed `Xo', enter an xo  
                                  * context.  `Xo' must be in a parsable  
                                  * state.  This is the ugly part.  IT IS  
                                  * NOT SMART ENOUGH TO HANDLE ESCAPED  
                                  * WHITESPACE.  
                                  */  
                                 if (macro && pos && 'o' == blk->buf[i]) {  
                                         if (xo && 'X' == ln->buf[pos - 1])  {  
                                                 if (' ' == ln->buf[pos - 2])  
                                                         xo++;  
                                         } else if ('X' == ln->buf[pos - 1]) {  
                                                 if (2 == pos && '.' == ln->buf[pos - 2])  
                                                         xo++;  
                                                 else if (' ' == ln->buf[pos - 2])  
                                                         xo++;  
                                         }  
                                 }  
   
                                 /*  
                                  * If we're parsed `Xc', leave an xo  
                                  * context if one's already pending.  
                                  * `Xc' must be in a parsable state.  
                                  * THIS IS NOT SMART ENOUGH TO HANDLE  
                                  * ESCAPED WHITESPACE.  
                                  */  
                                 if (macro && pos && 'c' == blk->buf[i])  
                                         if (xo && 'X' == ln->buf[pos - 1])  
                                                 if (' ' == ln->buf[pos - 2])  
                                                         xo--;  
 #endif  /* STRIP_XO */  
   
                                 ln->buf[pos++] = blk->buf[i];                                  ln->buf[pos++] = blk->buf[i];
                                 continue;                                  continue;
                         }                          }
Line 389  fdesc(struct buf *blk, struct buf *ln,
Line 380  fdesc(struct buf *blk, struct buf *ln,
                                 }                                  }
                         }                          }
   
 #ifdef  STRIP_XO  
                         /*  
                          * If we're in an xo context, put a space in  
                          * place of the newline and continue parsing.  
                          * Mark that we just did a newline.  
                          */  
                         if (xo) {  
                                 xeoln = 1;  
                                 ln->buf[pos++] = ' ';  
                                 lnn++;  
                                 continue;  
                         }  
                         macro = 0;  
 #endif  /* STRIP_XO */  
   
                         ln->buf[pos] = 0;                          ln->buf[pos] = 0;
                         if ( ! mdoc_parseln(mdoc, lnn, ln->buf))                          if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
                                 return(0);                                  return(0);
                           if (man && ! man_parseln(man, lnn, ln->buf))
                                   return(0);
                         lnn++;                          lnn++;
                         pos = 0;                          pos = 0;
                 }                  }
         }          }
   
         return(mdoc_endparse(mdoc));          if (mdoc)
                  return(mdoc_endparse(mdoc));
   
           return(man_endparse(man));
 }  }
   
   
 static int  static int
   moptions(enum intt *tflags, char *arg)
   {
   
           if (0 == strcmp(arg, "mdoc"))
                   *tflags = INTT_MDOC;
           else if (0 == strcmp(arg, "man"))
                   *tflags = INTT_MAN;
           else {
                   warnx("bad argument: -m%s", arg);
                   return(0);
           }
   
           return(1);
   }
   
   
   static int
 toptions(enum outt *tflags, char *arg)  toptions(enum outt *tflags, char *arg)
 {  {
   
         if (0 == strcmp(arg, "ascii"))          if (0 == strcmp(arg, "ascii"))
                 *tflags = OUTT_ASCII;                  *tflags = OUTT_ASCII;
         else if (0 == strcmp(arg, "latin1"))  
                 *tflags = OUTT_LATIN1;  
         else if (0 == strcmp(arg, "utf8"))  
                 *tflags = OUTT_UTF8;  
         else if (0 == strcmp(arg, "lint"))          else if (0 == strcmp(arg, "lint"))
                 *tflags = OUTT_LINT;                  *tflags = OUTT_LINT;
         else if (0 == strcmp(arg, "tree"))          else if (0 == strcmp(arg, "tree"))
Line 457  foptions(int *fflags, char *arg)
Line 451  foptions(int *fflags, char *arg)
         while (*arg)          while (*arg)
                 switch (getsubopt(&arg, toks, &v)) {                  switch (getsubopt(&arg, toks, &v)) {
                 case (0):                  case (0):
                         *fflags |= MDOC_IGN_SCOPE;                          *fflags |= IGN_SCOPE;
                         break;                          break;
                 case (1):                  case (1):
                         *fflags |= MDOC_IGN_ESCAPE;                          *fflags |= IGN_ESCAPE;
                         break;                          break;
                 case (2):                  case (2):
                         *fflags |= MDOC_IGN_MACRO;                          *fflags |= IGN_MACRO;
                         break;                          break;
                 default:                  default:
                         warnx("bad argument: -f%s", arg);                          warnx("bad argument: -f%s", arg);
Line 528  merr(void *arg, int line, int col, const char *msg)
Line 522  merr(void *arg, int line, int col, const char *msg)
   
   
 static int  static int
 mwarn(void *arg, int line, int col,  mdocwarn(void *arg, int line, int col,
                 enum mdoc_warn type, const char *msg)                  enum mdoc_warn type, const char *msg)
 {  {
         struct curparse *curp;          struct curparse *curp;
Line 563  mwarn(void *arg, int line, int col, 
Line 557  mwarn(void *arg, int line, int col, 
 }  }
   
   
   static int
   manwarn(void *arg, int line, int col, const char *msg)
   {
           struct curparse *curp;
   
           curp = (struct curparse *)arg;
   
           if ( ! (curp->wflags & WARN_WSYNTAX))
                   return(1);
   
           warnx("%s:%d: syntax warning: %s (column %d)",
                           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.9  
changed lines
  Added in v.1.16

CVSweb