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

version 1.1, 2008/11/29 14:14:21 version 1.12, 2009/01/02 09:29:06
Line 16 
Line 16 
  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR   * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  * PERFORMANCE OF THIS SOFTWARE.   * PERFORMANCE OF THIS SOFTWARE.
  */   */
 #include <sys/param.h>  
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  
 #include <err.h>  
 #include <stdio.h>  
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  
   
 #include "libmdocml.h"  
 #include "private.h"  #include "private.h"
   
 #ifdef  __linux__ /* FIXME */  /* FIXME: `.St' can only have one argument set. */
 #define strlcat         strncat  
 #endif  
   
 struct  md_valid {  typedef int     (*v_args_sz)(struct mdoc *, int, int, int);
         const struct md_args    *args;  typedef int     (*v_args)(struct mdoc *, int, int, int,
         const struct md_rbuf    *rbuf;                          const char *[], int, const struct mdoc_arg *);
         struct md_mbuf  *mbuf;  typedef int     (*v_post)(struct mdoc *, int, int);
         struct rofftree *tree;  
   
         size_t           indent;  static  int       need_head_child(struct mdoc *, int, int);
         size_t           pos;  static  int       no_head_child(struct mdoc *, int, int);
   
         int              flags;  static  int       assert_eq0(struct mdoc *, int, int, int);
 #define MD_LITERAL      (1 << 0)  static  int       assert_le1(struct mdoc *, int, int, int);
   static  int       need_eq0(struct mdoc *, int, int, int);
   static  int       need_eq1(struct mdoc *, int, int, int);
   static  int       need_ge1(struct mdoc *, int, int, int);
   static  int       need_le2(struct mdoc *, int, int, int);
   static  int       want_eq0(struct mdoc *, int, int, int);
   static  int       want_ge1(struct mdoc *, int, int, int);
   
   static  int       args_ref(struct mdoc *, int, int, int,
                           const char *[], int, const struct mdoc_arg *);
   static  int       args_bool(struct mdoc *, int, int, int,
                           const char *[], int, const struct mdoc_arg *);
   static  int       args_Sh(struct mdoc *, int, int, int,
                           const char *[], int, const struct mdoc_arg *);
   static  int       args_blocknest(struct mdoc *, int, int, int,
                           const char *[], int, const struct mdoc_arg *);
   static  int       args_At(struct mdoc *, int, int, int,
                           const char *[], int, const struct mdoc_arg *);
   static  int       args_Xr(struct mdoc *, int, int, int,
                           const char *[], int, const struct mdoc_arg *);
   
   struct  valids {
           v_args_sz sz;
           v_args    args;
           v_post    post;
 };  };
   
 static  void             roffmsg(void *arg, enum roffmsg,  
                                 const char *, const char *, char *);  
 static  int              roffhead(void *);  
 static  int              rofftail(void *);  
 static  int              roffin(void *, int, int *, char **);  
 static  int              roffdata(void *, char *);  
 static  int              roffout(void *, int);  
 static  int              roffblkin(void *, int);  
 static  int              roffblkout(void *, int);  
 static  int              roffspecial(void *, int);  
   
 static  int              mbuf_newline(struct md_valid *);  /*
 static  int              mbuf_indent(struct md_valid *);   * FIXME: have arrays of function pointers in case we want multiple
 static  int              mbuf_data(struct md_valid *, char *);   * check callbacks per macro.
    */
   
   const   struct valids mdoc_valids[MDOC_MAX] = {
           { NULL, NULL, NULL }, /* \" */
           { NULL, NULL, NULL }, /* Dd */ /* TODO */
           { NULL, NULL, NULL }, /* Dt */ /* TODO */
           { NULL, NULL, NULL }, /* Os */ /* TODO */
           { NULL, args_Sh, NULL }, /* Sh */
           { NULL, NULL, NULL }, /* Ss */
           { want_eq0, NULL, NULL }, /* Pp */
           { NULL, args_blocknest, need_head_child }, /* D1 */
           { NULL, args_blocknest, need_head_child }, /* Dl */
           { NULL, args_blocknest, NULL }, /* Bd */
           { NULL, NULL, no_head_child }, /* Ed */
           { NULL, NULL, NULL }, /* Bl */
           { NULL, NULL, no_head_child }, /* El */
           { NULL, NULL, NULL }, /* It */
           { need_ge1, NULL, NULL }, /* Ad */
           { NULL, NULL, NULL }, /* An */
           { NULL, NULL, NULL }, /* Ar */
           { need_ge1, NULL, NULL }, /* Cd */
           { NULL, NULL, NULL }, /* Cm */
           { need_ge1, NULL, NULL }, /* Dv */
           { need_ge1, NULL, NULL }, /* Er */
           { need_ge1, NULL, NULL }, /* Ev */
           { NULL, NULL, NULL }, /* Ex */
           { need_ge1, NULL, NULL }, /* Fa */
           { NULL, NULL, NULL }, /* Fd */
           { NULL, NULL, NULL }, /* Fl */
           { need_ge1, NULL, NULL }, /* Fn */
           { want_ge1, NULL, NULL }, /* Ft */
           { need_ge1, NULL, NULL }, /* Ic */
           { need_eq1, NULL, NULL }, /* In */
           { want_ge1, NULL, NULL }, /* Li */
           { want_ge1, NULL, NULL }, /* Nd */
           { NULL, NULL, NULL }, /* Nm */
           { NULL, NULL, NULL }, /* Op */
           { NULL, NULL, NULL }, /* Ot */
           { want_ge1, NULL, NULL }, /* Pa */
           { NULL, NULL, NULL }, /* Rv */
           { NULL, NULL, NULL }, /* St */
           { need_ge1, NULL, NULL }, /* Va */
           { need_ge1, NULL, NULL }, /* Vt */
           { need_le2, args_Xr, NULL }, /* Xr */
           { need_ge1, args_ref, NULL }, /* %A */
           { need_ge1, args_ref, NULL }, /* %B */
           { need_ge1, args_ref, NULL }, /* %D */
           { need_ge1, args_ref, NULL }, /* %I */
           { need_ge1, args_ref, NULL }, /* %J */
           { need_ge1, args_ref, NULL }, /* %N */
           { need_ge1, args_ref, NULL }, /* %O */
           { need_ge1, args_ref, NULL }, /* %P */
           { need_ge1, args_ref, NULL }, /* %R */
           { need_ge1, args_ref, NULL }, /* %T */
           { need_ge1, args_ref, NULL }, /* %V */
           { NULL, NULL, NULL }, /* Ac */
           { NULL, NULL, NULL }, /* Ao */
           { NULL, NULL, NULL }, /* Aq */
           { need_le2, args_At, NULL }, /* At */
           { NULL, NULL, NULL }, /* Bc */
           { NULL, NULL, NULL }, /* Bf */
           { NULL, NULL, NULL }, /* Bo */
           { NULL, NULL, NULL }, /* Bq */
           { assert_le1, NULL, NULL }, /* Bsx */
           { assert_le1, NULL, NULL }, /* Bx */
           { need_eq1, args_bool, NULL }, /* Db */
           { NULL, NULL, NULL }, /* Dc */
           { NULL, NULL, NULL }, /* Do */
           { NULL, NULL, NULL }, /* Dq */
           { NULL, NULL, NULL }, /* Ec */
           { NULL, NULL, NULL }, /* Ef */
           { need_ge1, NULL, NULL }, /* Em */
           { NULL, NULL, NULL }, /* Eo */
           { assert_le1, NULL, NULL }, /* Fx */
           { want_ge1, NULL, NULL }, /* Ms */
           { NULL, NULL, NULL }, /* No */
           { NULL, NULL, NULL }, /* Ns */
           { assert_le1, NULL, NULL }, /* Nx */
           { assert_le1, NULL, NULL }, /* Ox */
           { NULL, NULL, NULL }, /* Pc */
           { NULL, NULL, NULL }, /* Pf */
           { NULL, NULL, NULL }, /* Po */
           { NULL, NULL, NULL }, /* Pq */
           { NULL, NULL, NULL }, /* Qc */
           { NULL, NULL, NULL }, /* Ql */
           { NULL, NULL, NULL }, /* Qo */
           { NULL, NULL, NULL }, /* Qq */
           { NULL, NULL, NULL }, /* Re */
           { NULL, NULL, NULL }, /* Rs */
           { NULL, NULL, NULL }, /* Sc */
           { NULL, NULL, NULL }, /* So */
           { NULL, NULL, NULL }, /* Sq */
           { need_eq1, args_bool, NULL }, /* Sm */
           { need_ge1, NULL, NULL }, /* Sx */
           { need_ge1, NULL, NULL }, /* Sy */
           { want_ge1, NULL, NULL }, /* Tn */
           { assert_eq0, NULL, NULL }, /* Ux */
           { NULL, NULL, NULL }, /* Xc */
           { NULL, NULL, NULL }, /* Xo */
           { NULL, NULL, NULL }, /* Fo */
           { NULL, NULL, NULL }, /* Fc */
           { NULL, NULL, NULL }, /* Oo */
           { NULL, NULL, NULL }, /* Oc */
           { NULL, NULL, NULL }, /* Bk */
           { NULL, NULL, NULL }, /* Ek */
           { need_eq0, NULL, NULL }, /* Bt */
           { need_eq1, NULL, NULL }, /* Hf */
           { NULL, NULL, NULL }, /* Fr */
           { need_eq0, NULL, NULL }, /* Ud */
   };
   
   
 static int  static int
 mbuf_indent(struct md_valid *p)  need_le2(struct mdoc *mdoc, int tok, int pos, int sz)
 {  {
         size_t           i;          if (sz <= 2)
                   return(1);
           return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE2));
   }
   
         assert(0 == p->pos);  
   
         for (i = 0; i < MIN(p->indent, 4); i++)  static int
                 if ( ! md_buf_putstring(p->mbuf, "    "))  want_ge1(struct mdoc *mdoc, int tok, int pos, int sz)
                         return(0);  {
           if (sz > 0)
         p->pos = i * 4;                  return(1);
         return(1);          return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1));
 }  }
   
   
 static int  static int
 mbuf_atnewline(struct md_valid *p)  want_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
 {  {
           if (sz == 0)
         return(p->pos == MIN(4, p->indent));                  return(1);
           return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0));
 }  }
   
   
 static int  static int
 mbuf_newline(struct md_valid *p)  need_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
 {  {
           if (sz == 0)
         if (mbuf_atnewline(p))  
                 return(1);                  return(1);
         if ( ! md_buf_putchar(p->mbuf, '\n'))          return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ0));
                 return(0);  
   
         p->pos = 0;  
         return(mbuf_indent(p));  
 }  }
   
   
 static int  static int
 mbuf_data(struct md_valid *p, char *buf)  assert_le1(struct mdoc *mdoc, int tok, int pos, int sz)
 {  {
         size_t           sz;  
         char            *bufp;  
   
         assert(p->mbuf);          assert(sz <= 1);
         assert(0 != p->indent);  
   
         if (MD_LITERAL & p->flags)  
                 return(md_buf_putstring(p->mbuf, buf));  
   
         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.  
          */  
   
         while (*buf) {  
                 while (*buf && isspace(*buf))  
                         buf++;  
   
                 if (0 == *buf)  
                         break;  
   
                 bufp = buf;  
                 while (*buf && ! isspace(*buf))  
                         buf++;  
   
                 if (0 != *buf)  
                         *buf++ = 0;  
   
                 /* Process word. */  
   
                 sz = strlen(bufp);  
   
                 if (sz + p->pos < 72) {  
                         if ( ! md_buf_putstring(p->mbuf, bufp))  
                                 return(0);  
   
                         /* FIXME: check punctuation. */  
   
                         if ( ! md_buf_putchar(p->mbuf, ' '))  
                                 return(0);  
                         p->pos += sz + 1;  
                         continue;  
                 }  
   
                 if ( ! mbuf_newline(p))  
                         return(0);  
   
                 if ( ! md_buf_putstring(p->mbuf, bufp))  
                         return(0);  
   
                 /* FIXME: check punctuation. */  
   
                 if ( ! md_buf_putchar(p->mbuf, ' '))  
                         return(0);  
                 p->pos += sz + 1;  
         }  
   
         return(1);          return(1);
 }  }
   
   
 int  static int
 md_line_valid(void *arg, char *buf)  assert_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
 {  {
         struct md_valid *p;  
   
         p = (struct md_valid *)arg;          assert(sz == 0);
         return(roff_engine(p->tree, buf));          return(1);
 }  }
   
   
 int  static int
 md_exit_valid(void *data, int flush)  need_eq1(struct mdoc *mdoc, int tok, int pos, int sz)
 {  {
         int              c;          if (sz == 1)
         struct md_valid *p;                  return(1);
           return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ1));
         p = (struct md_valid *)data;  
         c = roff_free(p->tree, flush);  
         free(p);  
   
         return(c);  
 }  }
   
   
 void *  static int
 md_init_valid(const struct md_args *args,  need_ge1(struct mdoc *mdoc, int tok, int pos, int sz)
                 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)  
 {  {
         struct roffcb    cb;          if (sz > 0)
         struct md_valid *p;                  return(1);
           return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
         cb.roffhead = roffhead;  
         cb.rofftail = rofftail;  
         cb.roffin = roffin;  
         cb.roffout = roffout;  
         cb.roffblkin = roffblkin;  
         cb.roffblkout = roffblkout;  
         cb.roffspecial = roffspecial;  
         cb.roffmsg = roffmsg;  
         cb.roffdata = roffdata;  
   
         if (NULL == (p = calloc(1, sizeof(struct md_valid))))  
                 err(1, "malloc");  
   
         p->args = args;  
         p->mbuf = mbuf;  
         p->rbuf = rbuf;  
   
         assert(mbuf);  
   
         if (NULL == (p->tree = roff_alloc(&cb, p))) {  
                 free(p);  
                 return(NULL);  
         }  
   
         return(p);  
 }  }
   
   
 /* ARGSUSED */  
 static int  static int
 roffhead(void *arg)  no_head_child(struct mdoc *mdoc, int tok, int pos)
 {  {
   
           /* TODO */
         return(1);          return(1);
 }  }
   
   
 static int  static int
 rofftail(void *arg)  need_head_child(struct mdoc *mdoc, int tok, int pos)
 {  {
         struct md_valid *p;          struct mdoc_node *n;
   
         assert(arg);          assert(mdoc->last);
         p = (struct md_valid *)arg;          n = mdoc->last;
   
         if (mbuf_atnewline(p))          assert(MDOC_BLOCK == n->type);
                 return(1);          assert(n->child);
   
         return(md_buf_putchar(p->mbuf, '\n'));          n = n->child;
   
           if (MDOC_HEAD != n->type)
                   return(mdoc_err(mdoc, tok, pos, ERR_CHILD_HEAD));
           if (n->child)
                   return(1);
           return(mdoc_err(mdoc, tok, pos, ERR_CHILD_HEAD));
 }  }
   
   
 static int  static int
 roffspecial(void *arg, int tok)  args_Sh(struct mdoc *mdoc, int tok, int pos,
                   int sz, const char *args[],
                   int argc, const struct mdoc_arg *argv)
 {  {
           enum mdoc_sec    sec;
   
           sec = mdoc_atosec((size_t)sz, args);
           if (SEC_CUSTOM != sec && sec < mdoc->sec_lastn)
                   if ( ! mdoc_warn(mdoc, tok, pos, WARN_SEC_OO))
                           return(0);
           if (SEC_BODY == mdoc->sec_last && SEC_NAME != sec)
                   return(mdoc_err(mdoc, tok, pos, ERR_SEC_NAME));
   
         return(1);          return(1);
 }  }
   
   
 static int  static int
 roffblkin(void *arg, int tok)  args_bool(struct mdoc *mdoc, int tok, int pos,
                   int sz, const char *args[],
                   int argc, const struct mdoc_arg *argv)
 {  {
         struct md_valid *p;  
   
         assert(arg);          assert(1 == sz);
         p = (struct md_valid *)arg;          if (xstrcmp(args[0], "on"))
                   return(1);
         if ( ! mbuf_atnewline(p)) {          if (xstrcmp(args[0], "off"))
                 if ( ! md_buf_putchar(p->mbuf, '\n'))                  return(1);
                         return(0);          return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGBAD));
                 p->pos = 0;  
                 if ( ! mbuf_indent(p))  
                         return(0);  
         }  
   
         if ( ! md_buf_putstring(p->mbuf, toknames[tok]))  
                 return(0);  
   
         if ( ! md_buf_putchar(p->mbuf, '\n'))  
                 return(0);  
   
         p->pos = 0;  
         p->indent++;  
   
         return(mbuf_indent(p));  
 }  }
   
   
 static int  static int
 roffblkout(void *arg, int tok)  args_ref(struct mdoc *mdoc, int tok, int pos,
                   int sz, const char *args[],
                   int argc, const struct mdoc_arg *argv)
 {  {
         struct md_valid *p;          struct mdoc_node *n;
   
         assert(arg);          assert(mdoc->last);
         p = (struct md_valid *)arg;          for (n = mdoc->last ; n; n = n->parent) {
                   if (MDOC_BLOCK != n->type)
                           continue;
                   if (MDOC_Rs != n->data.block.tok)
                           break;
                   return(1);
           }
   
         if ( ! md_buf_putchar(p->mbuf, '\n'))          return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NOCTX));
                 return(0);  
   
         p->pos = 0;  
         p->indent--;  
   
         return(mbuf_indent(p));  
 }  }
   
   
 static int  static int
 roffin(void *arg, int tok, int *argcp, char **argvp)  args_Xr(struct mdoc *mdoc, int tok, int pos,
                   int sz, const char *args[],
                   int argc, const struct mdoc_arg *argv)
 {  {
   
           if (1 == sz)
                   return(1);
           if (0 == sz)
                   return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
   
           if (MSEC_DEFAULT == mdoc_atomsec(args[1]))
                   return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGFORM));
         return(1);          return(1);
 }  }
   
   
 static int  static int
 roffout(void *arg, int tok)  args_At(struct mdoc *mdoc, int tok, int pos,
                   int sz, const char *args[],
                   int argc, const struct mdoc_arg *argv)
 {  {
           int              i;
   
           if (0 == sz)
                   return(1);
   
           i = 0;
           if (ATT_DEFAULT == mdoc_atoatt(args[i]))
                   i++;
   
           for ( ; i < sz; i++) {
                   if ( ! mdoc_isdelim(args[i]))
                           continue;
                   return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_NOPUNCT));
           }
         return(1);          return(1);
 }  }
   
   
   static int
 static void  args_blocknest(struct mdoc *mdoc, int tok, int pos,
 roffmsg(void *arg, enum roffmsg lvl,                  int sz, const char *args[],
                 const char *buf, const char *pos, char *msg)                  int argc, const struct mdoc_arg *argv)
 {  {
         char            *level;          struct mdoc_node *node;
         struct md_valid *p;  
   
         assert(arg);          /*
         p = (struct md_valid *)arg;           * We can't be nested within any other block displays (or really
            * any other kind of display, although Bd is the only multi-line
            * one that will show up).
            */
           assert(mdoc->last);
   
         switch (lvl) {          /* LINTED */
         case (ROFF_WARN):          for (node = mdoc->last; node; node = node->parent) {
                 if ( ! (MD_WARN_ALL & p->args->warnings))                  if (node->type != MDOC_BLOCK)
                         return;                          continue;
                 level = "warning";                  if (node->data.block.tok != MDOC_Bd)
                           continue;
                 break;                  break;
         case (ROFF_ERROR):  
                 level = "error";  
                 break;  
         default:  
                 abort();  
         }          }
           if (NULL == node)
         if (pos)                  return(1);
                 (void)fprintf(stderr, "%s:%zu: %s: %s\n",          return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NONEST));
                                 p->rbuf->name, p->rbuf->line, level, msg);  }
         else  
                 (void)fprintf(stderr, "%s: %s: %s\n",  
                                 p->rbuf->name, level, msg);  
   
   
   int
   mdoc_valid_pre(struct mdoc *mdoc, int tok, int pos,
                   int sz, const char *args[],
                   int argc, const struct mdoc_arg *argv)
   {
   
           assert(tok < MDOC_MAX);
           if (mdoc_valids[tok].sz)
                   if ( ! (*mdoc_valids[tok].sz)(mdoc, tok, pos, sz))
                           return(0);
   
           if (NULL == mdoc_valids[tok].args)
                   return(1);
           return((*mdoc_valids[tok].args)(mdoc,
                           tok, pos, sz, args, argc, argv));
 }  }
   
   
 static int  int
 roffdata(void *arg, char *buf)  mdoc_valid_post(struct mdoc *mdoc, int tok, int pos)
 {  {
         struct md_valid *p;  
   
         assert(arg);          if (NULL == mdoc_valids[tok].post)
         p = (struct md_valid *)arg;                  return(1);
         return(mbuf_data(p, buf));          return((*mdoc_valids[tok].post)(mdoc, tok, pos));
 }  }

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

CVSweb