[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.10

version 1.9, 2009/03/22 19:10:48 version 1.10, 2009/03/23 14:22:11
Line 27 
Line 27 
 #include <unistd.h>  #include <unistd.h>
   
 #include "mdoc.h"  #include "mdoc.h"
   #include "man.h"
   
 #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(__FreeBSD__)
   # ifndef __dead
   #  define __dead __dead2
   # endif
 #endif  #endif
   
 struct  buf {  struct  buf {
Line 49  struct curparse {
Line 54  struct curparse {
 #define WARN_WERR        (1 << 2)       /* Warnings->errors. */  #define WARN_WERR        (1 << 2)       /* Warnings->errors. */
 };  };
   
 enum outt {  enum    intt {
         OUTT_ASCII,          INTT_MDOC = 0,
           INTT_MAN
   };
   
   enum    outt {
           OUTT_ASCII = 0,
         OUTT_LATIN1,          OUTT_LATIN1,
         OUTT_UTF8,          OUTT_UTF8,
         OUTT_TREE,          OUTT_TREE,
Line 69  extern int    terminal_run(void *, const struct mdoc *
Line 79  extern int    terminal_run(void *, const struct mdoc *
 extern  int               tree_run(void *, const struct mdoc *);  extern  int               tree_run(void *, 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               mwarn(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;
         struct mdoc_cb   cb;          struct mdoc_cb   cb;
           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 114  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 174  main(int argc, char *argv[])
Line 195  main(int argc, char *argv[])
         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;
   
           switch (inttype) {
           case (INTT_MAN):
                   man = man_alloc();
                   break;
           default:
                   mdoc = mdoc_alloc(&curp, fflags, &cb);
                   break;
           }
   
         /*          /*
          * Loop around available files.           * 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;
   #if 0
                 else if (c && outrun && (*outrun)(outdata, mdoc))                  else if (c && outrun && (*outrun)(outdata, mdoc))
                         rc = 1;                          rc = 1;
   #endif
         } 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 0
                         if (outrun && ! (*outrun)(outdata, mdoc))                          if (outrun && ! (*outrun)(outdata, mdoc))
                                 break;                                  break;
                         /* Reset the parser for another file. */  #endif
                         mdoc_reset(mdoc);                          if (man)
                                   man_reset(man);
                           if (mdoc)
                                   mdoc_reset(mdoc);
   
                         argv++;                          argv++;
                 }                  }
                 rc = NULL == *argv;                  rc = NULL == *argv;
Line 209  main(int argc, char *argv[])
Line 249  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 237  usage(void)
Line 279  usage(void)
   
   
 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 289  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 300  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 333  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 350  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 368  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
   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);
 }  }
   
   

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

CVSweb