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

Diff for /mandoc/Attic/validate.c between version 1.1 and 1.6

version 1.1, 2008/11/29 14:14:21 version 1.6, 2008/11/30 20:53:34
Line 28 
Line 28 
 #include "libmdocml.h"  #include "libmdocml.h"
 #include "private.h"  #include "private.h"
   
   #define INDENT           4
   #define COLUMNS          72
   
 #ifdef  __linux__ /* FIXME */  #ifdef  __linux__ /* FIXME */
 #define strlcat         strncat  #define strlcat          strncat
 #endif  #endif
   
   enum    md_tok {
           MD_BLKIN,
           MD_BLKOUT,
           MD_IN,
           MD_OUT,
           MD_TEXT
   };
   
 struct  md_valid {  struct  md_valid {
         const struct md_args    *args;          const struct md_args    *args;
         const struct md_rbuf    *rbuf;          const struct md_rbuf    *rbuf;
   
         struct md_mbuf  *mbuf;          struct md_mbuf  *mbuf;
         struct rofftree *tree;          struct rofftree *tree;
   
         size_t           indent;          size_t           indent;
         size_t           pos;          size_t           pos;
           enum md_tok      last;
         int              flags;          int              flags;
 #define MD_LITERAL      (1 << 0)  #define MD_LITERAL      (1 << 0) /* FIXME */
 };  };
   
 static  void             roffmsg(void *arg, enum roffmsg,  static  void             roffmsg(void *arg, enum roffmsg,
                                 const char *, const char *, char *);                                  const char *, const char *, char *);
 static  int              roffhead(void *);  static  int              roffhead(void *);
 static  int              rofftail(void *);  static  int              rofftail(void *);
 static  int              roffin(void *, int, int *, char **);  static  int              roffin(void *, int, int, int *, char **);
 static  int              roffdata(void *, char *);  static  int              roffdata(void *, int, char *);
 static  int              roffout(void *, int);  static  int              roffout(void *, int);
 static  int              roffblkin(void *, int);  static  int              roffblkin(void *, int, int *, char **);
 static  int              roffblkout(void *, int);  static  int              roffblkout(void *, int);
 static  int              roffspecial(void *, int);  static  int              roffspecial(void *, int);
   
 static  int              mbuf_newline(struct md_valid *);  static  int              mbuf_newline(struct md_valid *);
 static  int              mbuf_indent(struct md_valid *);  static  int              mbuf_indent(struct md_valid *);
 static  int              mbuf_data(struct md_valid *, char *);  static  int              mbuf_data(struct md_valid *, int, char *);
   static  int              mbuf_putstring(struct md_valid *,
                                   const char *);
   static  int              mbuf_nputstring(struct md_valid *,
                                   const char *, size_t);
   
   
 static int  static int
 mbuf_indent(struct md_valid *p)  mbuf_putstring(struct md_valid *p, const char *buf)
 {  {
         size_t           i;  
   
         assert(0 == p->pos);          return(mbuf_nputstring(p, buf, strlen(buf)));
   }
   
         for (i = 0; i < MIN(p->indent, 4); i++)  
                 if ( ! md_buf_putstring(p->mbuf, "    "))  
                         return(0);  
   
         p->pos = i * 4;  static int
         return(1);  mbuf_nputstring(struct md_valid *p, const char *buf, size_t sz)
   {
   
           p->pos += sz;
           return(md_buf_puts(p->mbuf, buf, sz));
 }  }
   
   
 static int  static int
 mbuf_atnewline(struct md_valid *p)  mbuf_indent(struct md_valid *p)
 {  {
           size_t           i;
   
         return(p->pos == MIN(4, p->indent));          assert(p->pos == 0);
   
           /* LINTED */
           for (i = 0; i < MIN(p->indent, INDENT); i++)
                   if ( ! md_buf_putstring(p->mbuf, "    "))
                           return(0);
   
           p->pos += i * INDENT;
           return(1);
 }  }
   
   
Line 89  static int
Line 114  static int
 mbuf_newline(struct md_valid *p)  mbuf_newline(struct md_valid *p)
 {  {
   
         if (mbuf_atnewline(p))  
                 return(1);  
         if ( ! md_buf_putchar(p->mbuf, '\n'))          if ( ! md_buf_putchar(p->mbuf, '\n'))
                 return(0);                  return(0);
   
         p->pos = 0;          p->pos = 0;
         return(mbuf_indent(p));          return(1);
 }  }
   
   
 static int  static int
 mbuf_data(struct md_valid *p, char *buf)  mbuf_data(struct md_valid *p, int space, char *buf)
 {  {
         size_t           sz;          size_t           sz;
         char            *bufp;          char            *bufp;
Line 108  mbuf_data(struct md_valid *p, char *buf)
Line 131  mbuf_data(struct md_valid *p, char *buf)
         assert(p->mbuf);          assert(p->mbuf);
         assert(0 != p->indent);          assert(0 != p->indent);
   
         if (MD_LITERAL & p->flags)          /*
                 return(md_buf_putstring(p->mbuf, buf));           * FIXME: punctuation/no-space stuff shouldn't have a newline
            * before it.
         if (0 == p->pos)  
                 mbuf_indent(p);  
   
         /*  
          * Indent if we're at the beginning of a line.  Don't indent  
          * more than 16 or so characters.  
          */           */
   
           if (MD_LITERAL & p->flags)
                   return(mbuf_putstring(p, buf));
   
         while (*buf) {          while (*buf) {
                 while (*buf && isspace(*buf))                  while (*buf && isspace(*buf))
                         buf++;                          buf++;
Line 133  mbuf_data(struct md_valid *p, char *buf)
Line 153  mbuf_data(struct md_valid *p, char *buf)
                 if (0 != *buf)                  if (0 != *buf)
                         *buf++ = 0;                          *buf++ = 0;
   
                 /* Process word. */  
   
                 sz = strlen(bufp);                  sz = strlen(bufp);
   
                 if (sz + p->pos < 72) {  
                         if ( ! md_buf_putstring(p->mbuf, bufp))  
                                 return(0);  
   
                         /* FIXME: check punctuation. */                  if (0 == p->pos) {
                           if ( ! mbuf_indent(p))
                         if ( ! md_buf_putchar(p->mbuf, ' '))  
                                 return(0);                                  return(0);
                         p->pos += sz + 1;                          if ( ! mbuf_nputstring(p, bufp, sz))
                                   return(0);
                           if (p->indent * INDENT + sz >= COLUMNS) {
                                   if ( ! mbuf_newline(p))
                                           return(0);
                                   continue;
                           }
                         continue;                          continue;
                 }                  }
   
                 if ( ! mbuf_newline(p))                  if (sz + p->pos >= COLUMNS) {
                         return(0);                          if ( ! mbuf_newline(p))
                                   return(0);
                           if ( ! mbuf_indent(p))
                                   return(0);
                   } else if (space)
                           if ( ! mbuf_nputstring(p, " ", 1))
                                   return(0);
   
                 if ( ! md_buf_putstring(p->mbuf, bufp))                  if ( ! mbuf_nputstring(p, bufp, sz))
                         return(0);                          return(0);
   
                 /* FIXME: check punctuation. */  
   
                 if ( ! md_buf_putchar(p->mbuf, ' '))  
                         return(0);  
                 p->pos += sz + 1;  
         }          }
   
         return(1);          return(1);
Line 229  md_init_valid(const struct md_args *args,
Line 248  md_init_valid(const struct md_args *args,
 static int  static int
 roffhead(void *arg)  roffhead(void *arg)
 {  {
           struct md_valid *p;
   
         return(1);          assert(arg);
           p = (struct md_valid *)arg;
   
           if ( ! mbuf_putstring(p, "<?xml version=\"1.0\" "
                                   "encoding=\"UTF-8\"?>\n"))
                   return(0);
           if ( ! mbuf_nputstring(p, "<mdoc>", 6))
                   return(0);
   
           p->indent++;
           return(mbuf_newline(p));
 }  }
   
   
Line 242  rofftail(void *arg)
Line 272  rofftail(void *arg)
         assert(arg);          assert(arg);
         p = (struct md_valid *)arg;          p = (struct md_valid *)arg;
   
         if (mbuf_atnewline(p))          if (0 != p->pos && ! mbuf_newline(p))
                 return(1);                  return(0);
   
         return(md_buf_putchar(p->mbuf, '\n'));          if ( ! mbuf_nputstring(p, "</mdoc>", 7))
                   return(0);
           return(mbuf_newline(p));
 }  }
   
   
   /* ARGSUSED */
 static int  static int
 roffspecial(void *arg, int tok)  roffspecial(void *arg, int tok)
 {  {
Line 258  roffspecial(void *arg, int tok)
Line 291  roffspecial(void *arg, int tok)
   
   
 static int  static int
 roffblkin(void *arg, int tok)  roffblkin(void *arg, int tok, int *argc, char **argv)
 {  {
         struct md_valid *p;          struct md_valid *p;
           int              i;
   
         assert(arg);          assert(arg);
         p = (struct md_valid *)arg;          p = (struct md_valid *)arg;
   
         if ( ! mbuf_atnewline(p)) {          if (0 != p->pos) {
                 if ( ! md_buf_putchar(p->mbuf, '\n'))                  if ( ! mbuf_newline(p))
                         return(0);                          return(0);
                 p->pos = 0;  
                 if ( ! mbuf_indent(p))                  if ( ! mbuf_indent(p))
                         return(0);                          return(0);
         }          } else if ( ! mbuf_indent(p))
                   return(0);
   
         if ( ! md_buf_putstring(p->mbuf, toknames[tok]))          if ( ! mbuf_nputstring(p, "<", 1))
                 return(0);                  return(0);
           if ( ! mbuf_putstring(p, toknames[tok]))
                   return(0);
   
         if ( ! md_buf_putchar(p->mbuf, '\n'))          for (i = 0; ROFF_ARGMAX != argc[i]; i++) {
                   if ( ! mbuf_nputstring(p, " ", 1))
                           return(0);
                   if ( ! mbuf_putstring(p, tokargnames[argc[i]]))
                           return(0);
                   if ( ! mbuf_nputstring(p, "=\"", 2))
                           return(0);
                   if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true"))
                           return(0);
                   if ( ! mbuf_nputstring(p, "\"", 1))
                           return(0);
           }
   
           if ( ! mbuf_nputstring(p, ">", 1))
                 return(0);                  return(0);
           if ( ! mbuf_newline(p))
                   return(0);
   
         p->pos = 0;  
         p->indent++;          p->indent++;
           return(1);
         return(mbuf_indent(p));  
 }  }
   
   
Line 294  roffblkout(void *arg, int tok)
Line 343  roffblkout(void *arg, int tok)
         assert(arg);          assert(arg);
         p = (struct md_valid *)arg;          p = (struct md_valid *)arg;
   
         if ( ! md_buf_putchar(p->mbuf, '\n'))          p->indent--;
   
           if (0 != p->pos) {
                   if ( ! mbuf_newline(p))
                           return(0);
                   if ( ! mbuf_indent(p))
                           return(0);
           } else if ( ! mbuf_indent(p))
                 return(0);                  return(0);
   
         p->pos = 0;          if ( ! mbuf_nputstring(p, "</", 2))
         p->indent--;                  return(0);
           if ( ! mbuf_putstring(p, toknames[tok]))
                   return(0);
           if ( ! mbuf_nputstring(p, ">", 1))
                   return(0);
           if ( ! mbuf_newline(p))
                   return(0);
   
         return(mbuf_indent(p));          return(1);
 }  }
   
   
 static int  static int
 roffin(void *arg, int tok, int *argcp, char **argvp)  roffin(void *arg, int tok, int space, int *argc, char **argv)
 {  {
           struct md_valid *p;
           int              i;
   
         return(1);          assert(arg);
           p = (struct md_valid *)arg;
   
           if (0 == p->pos && ! mbuf_indent(p))
                   return(0);
   
           /*
            * FIXME: put into a buffer before writing (check line length).
            */
   
           if (space && ! mbuf_nputstring(p, " ", 1))
                   return(0);
           if ( ! mbuf_nputstring(p, "<", 1))
                   return(0);
           if ( ! mbuf_putstring(p, toknames[tok]))
                   return(0);
   
           for (i = 0; ROFF_ARGMAX != argc[i]; i++) {
                   if ( ! mbuf_nputstring(p, " ", 1))
                           return(0);
                   if ( ! mbuf_putstring(p, tokargnames[argc[i]]))
                           return(0);
                   if ( ! mbuf_nputstring(p, "=\"", 2))
                           return(0);
                   if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true"))
                           return(0);
                   if ( ! mbuf_nputstring(p, "\"", 1))
                           return(0);
           }
   
           return(mbuf_nputstring(p, ">", 1));
 }  }
   
   
 static int  static int
 roffout(void *arg, int tok)  roffout(void *arg, int tok)
 {  {
           struct md_valid *p;
   
         return(1);          assert(arg);
 }          p = (struct md_valid *)arg;
   
           if (0 == p->pos && ! mbuf_indent(p))
                   return(0);
   
           if ( ! mbuf_nputstring(p, "</", 2))
                   return(0);
           if ( ! mbuf_putstring(p, toknames[tok]))
                   return(0);
           return(mbuf_nputstring(p, ">", 1));
   }
   
   
 static void  static void
 roffmsg(void *arg, enum roffmsg lvl,  roffmsg(void *arg, enum roffmsg lvl,
                 const char *buf, const char *pos, char *msg)                  const char *buf, const char *pos, char *msg)
Line 345  roffmsg(void *arg, enum roffmsg lvl, 
Line 449  roffmsg(void *arg, enum roffmsg lvl, 
         }          }
   
         if (pos)          if (pos)
                 (void)fprintf(stderr, "%s:%zu: %s: %s\n",                  (void)fprintf(stderr, "%s:%zu: %s: %s (column %zu)\n",
                                 p->rbuf->name, p->rbuf->line, level, msg);                                  p->rbuf->name, p->rbuf->line, level,
                                   msg, pos - buf);
         else          else
                 (void)fprintf(stderr, "%s: %s: %s\n",                  (void)fprintf(stderr, "%s: %s: %s\n",
                                 p->rbuf->name, level, msg);                                  p->rbuf->name, level, msg);
Line 355  roffmsg(void *arg, enum roffmsg lvl, 
Line 460  roffmsg(void *arg, enum roffmsg lvl, 
   
   
 static int  static int
 roffdata(void *arg, char *buf)  roffdata(void *arg, int space, char *buf)
 {  {
         struct md_valid *p;          struct md_valid *p;
   
         assert(arg);          assert(arg);
         p = (struct md_valid *)arg;          p = (struct md_valid *)arg;
         return(mbuf_data(p, buf));          return(mbuf_data(p, space, buf));
 }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.6

CVSweb