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

Diff for /mandoc/eqn.c between version 1.8 and 1.16

version 1.8, 2011/07/17 14:08:49 version 1.16, 2011/07/21 13:18:24
Line 28 
Line 28 
 #include "libmandoc.h"  #include "libmandoc.h"
 #include "libroff.h"  #include "libroff.h"
   
 #define EQN_ARGS         struct eqn_node *ep, \  #define EQN_NEST_MAX     128 /* maximum nesting of defines */
                          int ln, \  #define EQN_MSG(t, x)    mandoc_msg((t), (x)->parse, (x)->eqn.ln, (x)->eqn.pos, NULL)
                          int pos, \  
                          const char **end  
   
 struct  eqnpart {  struct  eqnpart {
         const char      *name;          const char      *name;
         size_t           sz;          size_t           sz;
         int             (*fp)(EQN_ARGS);          int             (*fp)(struct eqn_node *);
 };  };
   
   struct  eqnmark {
           const char      *name;
           size_t           sz;
   };
   
 enum    eqnpartt {  enum    eqnpartt {
         EQN_DEFINE = 0,          EQN_DEFINE = 0,
         EQN_SET,          EQN_SET,
Line 46  enum eqnpartt {
Line 49  enum eqnpartt {
         EQN__MAX          EQN__MAX
 };  };
   
 static  int              eqn_do_define(EQN_ARGS);  static  void             eqn_box_free(struct eqn_box *);
 static  int              eqn_do_set(EQN_ARGS);  static  struct eqn_def  *eqn_def_find(struct eqn_node *,
 static  int              eqn_do_undef(EQN_ARGS);                                  const char *, size_t);
 static  const char      *eqn_nexttok(struct mparse *, int, int,  static  int              eqn_do_define(struct eqn_node *);
                                 const char **, size_t *);  static  int              eqn_do_set(struct eqn_node *);
   static  int              eqn_do_undef(struct eqn_node *);
   static  const char      *eqn_nexttok(struct eqn_node *, size_t *);
   static  const char      *eqn_nextrawtok(struct eqn_node *, size_t *);
   static  const char      *eqn_next(struct eqn_node *,
                                   char, size_t *, int);
   static  int              eqn_box(struct eqn_node *,
                                   struct eqn_box *, struct eqn_box **);
   
 static  const struct eqnpart eqnparts[EQN__MAX] = {  static  const struct eqnpart eqnparts[EQN__MAX] = {
         { "define", 6, eqn_do_define }, /* EQN_DEFINE */          { "define", 6, eqn_do_define }, /* EQN_DEFINE */
Line 58  static const struct eqnpart eqnparts[EQN__MAX] = {
Line 68  static const struct eqnpart eqnparts[EQN__MAX] = {
         { "undef", 5, eqn_do_undef }, /* EQN_UNDEF */          { "undef", 5, eqn_do_undef }, /* EQN_UNDEF */
 };  };
   
   static  const struct eqnmark eqnmarks[EQNMARK__MAX] = {
           { "", 0 }, /* EQNMARK_NONE */
           { "dot", 3 }, /* EQNMARK_DOT */
           { "dotdot", 6 }, /* EQNMARK_DOTDOT */
           { "hat", 3 }, /* EQNMARK_HAT */
           { "tilde", 5 }, /* EQNMARK_TILDE */
           { "vec", 3 }, /* EQNMARK_VEC */
           { "dyad", 4 }, /* EQNMARK_DYAD */
           { "bar", 3 }, /* EQNMARK_BAR */
           { "under", 5 }, /* EQNMARK_UNDER */
   };
   
 /* ARGSUSED */  /* ARGSUSED */
 enum rofferr  enum rofferr
 eqn_read(struct eqn_node **epp, int ln,  eqn_read(struct eqn_node **epp, int ln,
Line 65  eqn_read(struct eqn_node **epp, int ln, 
Line 87  eqn_read(struct eqn_node **epp, int ln, 
 {  {
         size_t           sz;          size_t           sz;
         struct eqn_node *ep;          struct eqn_node *ep;
         struct mparse   *mp;          enum rofferr     er;
         const char      *start, *end;  
         int              i, c;  
   
           ep = *epp;
   
           /*
            * If we're the terminating mark, unset our equation status and
            * validate the full equation.
            */
   
         if (0 == strcmp(p, ".EN")) {          if (0 == strcmp(p, ".EN")) {
                   er = eqn_end(ep);
                 *epp = NULL;                  *epp = NULL;
                 return(ROFF_EQN);                  return(er);
         }          }
   
         ep = *epp;          /*
         mp = ep->parse;           * Build up the full string, replacing all newlines with regular
         end = p + pos;           * whitespace.
            */
   
         if (NULL == (start = eqn_nexttok(mp, ln, pos, &end, &sz)))          sz = strlen(p + pos) + 1;
           ep->data = mandoc_realloc(ep->data, ep->sz + sz + 1);
   
           /* First invocation: nil terminate the string. */
   
           if (0 == ep->sz)
                   *ep->data = '\0';
   
           ep->sz += sz;
           strlcat(ep->data, p + pos, ep->sz + 1);
           strlcat(ep->data, " ", ep->sz + 1);
           return(ROFF_IGN);
   }
   
   struct eqn_node *
   eqn_alloc(int pos, int line, struct mparse *parse)
   {
           struct eqn_node *p;
   
           p = mandoc_calloc(1, sizeof(struct eqn_node));
           p->parse = parse;
           p->eqn.ln = line;
           p->eqn.pos = pos;
   
           return(p);
   }
   
   enum rofferr
   eqn_end(struct eqn_node *ep)
   {
           struct eqn_box  *root, *last;
           int              c;
   
           ep->eqn.root = root =
                   mandoc_calloc(1, sizeof(struct eqn_box));
           root->type = EQN_ROOT;
   
           if (0 == ep->sz)
                 return(ROFF_IGN);                  return(ROFF_IGN);
   
           /*
            * Run the parser.
            * If we return before reaching the end of our input, our scope
            * is still open somewhere.
            * If we return alright but don't have a symmetric scoping, then
            * something's not right either.
            * Otherwise, return the equation.
            */
   
           if (0 == (c = eqn_box(ep, root, &last))) {
                   if (last != root) {
                           EQN_MSG(MANDOCERR_EQNSCOPE, ep);
                           c = 0;
                   }
           } else if (c > 0)
                   EQN_MSG(MANDOCERR_EQNNSCOPE, ep);
   
           return(0 == c ? ROFF_EQN : ROFF_IGN);
   }
   
   static int
   eqn_box(struct eqn_node *ep, struct eqn_box *last, struct eqn_box **sv)
   {
           size_t           sz;
           const char      *start;
           int              c, i, nextc;
           struct eqn_box  *bp;
   
           /*
            * Mark our last level of subexpression.
            * Also mark whether that the next node should be a
            * subexpression node.
            */
   
           *sv = last;
           nextc = 1;
   again:
           if (NULL == (start = eqn_nexttok(ep, &sz)))
                   return(0);
   
         for (i = 0; i < (int)EQN__MAX; i++) {          for (i = 0; i < (int)EQN__MAX; i++) {
                 if (eqnparts[i].sz != sz)                  if (eqnparts[i].sz != sz)
                         continue;                          continue;
                 if (strncmp(eqnparts[i].name, start, sz))                  if (strncmp(eqnparts[i].name, start, sz))
                         continue;                          continue;
                   if ( ! (*eqnparts[i].fp)(ep))
                           return(-1);
   
                 if ((c = (*eqnparts[i].fp)(ep, ln, pos, &end)) < 0)                  goto again;
                         return(ROFF_ERR);  
                 else if (0 == c || '\0' == *end)  
                         return(ROFF_IGN);  
   
                 /*  
                  * Re-calculate offset and rerun, if trailing text.  
                  * This allows multiple definitions (say) on each line.  
                  */  
   
                 *offs = end - (p + pos);  
                 return(ROFF_RERUN);  
         }          }
   
         end = p + pos;          for (i = 0; i < (int)EQNMARK__MAX; i++) {
         while (NULL != (start = eqn_nexttok(mp, ln, pos, &end, &sz))) {                  if (eqnmarks[i].sz != sz)
                 if (0 == sz)  
                         continue;                          continue;
                   if (strncmp(eqnmarks[i].name, start, sz))
                           continue;
                   last->mark = (enum eqn_markt)i;
                   goto again;
           }
   
                 for (i = 0; i < (int)ep->defsz; i++) {          /* Exit this [hopefully] subexpression. */
                         if (0 == ep->defs[i].keysz)  
                                 continue;  
                         if (ep->defs[i].keysz != sz)  
                                 continue;  
                         if (strncmp(ep->defs[i].key, start, sz))  
                                 continue;  
                         start = ep->defs[i].val;  
                         sz = ep->defs[i].valsz;  
                         break;  
                 }  
   
                 ep->eqn.data = mandoc_realloc          if (sz == 1 && 0 == strncmp("}", start, 1))
                         (ep->eqn.data, ep->eqn.sz + sz + 1);                  return(1);
   
                 if (0 == ep->eqn.sz)          bp = mandoc_calloc(1, sizeof(struct eqn_box));
                         *ep->eqn.data = '\0';          if (nextc)
                   last->child = bp;
           else
                   last->next = bp;
   
                 ep->eqn.sz += sz;          last = bp;
                 strlcat(ep->eqn.data, start, ep->eqn.sz + 1);  
         }  
   
         return(ROFF_IGN);          /*
 }           * See if we're to open a new subexpression.
            * If so, mark our node as such and descend.
            */
   
 struct eqn_node *          if (sz == 1 && 0 == strncmp("{", start, 1)) {
 eqn_alloc(int pos, int line, struct mparse *parse)                  bp->type = EQN_SUBEXPR;
 {                  c = eqn_box(ep, bp, sv);
         struct eqn_node *p;  
   
         p = mandoc_calloc(1, sizeof(struct eqn_node));                  nextc = 0;
         p->parse = parse;                  goto again;
         p->eqn.line = line;          }
         p->eqn.pos = pos;  
   
         return(p);          /* A regular text node. */
 }  
   
 /* ARGSUSED */          bp->type = EQN_TEXT;
 void          bp->text = mandoc_malloc(sz + 1);
 eqn_end(struct eqn_node *e)          *bp->text = '\0';
 {          strlcat(bp->text, start, sz + 1);
   
         /* Nothing to do. */          nextc = 0;
           goto again;
 }  }
   
 void  void
Line 157  eqn_free(struct eqn_node *p)
Line 249  eqn_free(struct eqn_node *p)
 {  {
         int              i;          int              i;
   
         free(p->eqn.data);          eqn_box_free(p->eqn.root);
   
         for (i = 0; i < (int)p->defsz; i++) {          for (i = 0; i < (int)p->defsz; i++) {
                 free(p->defs[i].key);                  free(p->defs[i].key);
                 free(p->defs[i].val);                  free(p->defs[i].val);
         }          }
   
           free(p->data);
         free(p->defs);          free(p->defs);
         free(p);          free(p);
 }  }
   
 /*  static void
  * Return the current equation token setting "next" on the next one,  eqn_box_free(struct eqn_box *bp)
  * setting the token size in "sz".  {
  * This does the Right Thing for quoted strings, too.  
  * Returns NULL if no more tokens exist.          if (bp->child)
  */                  eqn_box_free(bp->child);
           if (bp->next)
                   eqn_box_free(bp->next);
   
           free(bp->text);
           free(bp);
   }
   
 static const char *  static const char *
 eqn_nexttok(struct mparse *mp, int ln, int pos,  eqn_nextrawtok(struct eqn_node *ep, size_t *sz)
                 const char **next, size_t *sz)  
 {  {
         const char      *start;  
         int              q;  
   
         start = *next;          return(eqn_next(ep, '"', sz, 0));
   }
   
   static const char *
   eqn_nexttok(struct eqn_node *ep, size_t *sz)
   {
   
           return(eqn_next(ep, '"', sz, 1));
   }
   
   static const char *
   eqn_next(struct eqn_node *ep, char quote, size_t *sz, int repl)
   {
           char            *start, *next;
           int              q, diff, lim;
           size_t           sv, ssz;
           struct eqn_def  *def;
   
           if (NULL == sz)
                   sz = &ssz;
   
           lim = 0;
           sv = ep->cur;
   again:
           /* Prevent self-definitions. */
   
           if (lim >= EQN_NEST_MAX) {
                   EQN_MSG(MANDOCERR_EQNNEST, ep);
                   return(NULL);
           }
   
           ep->cur = sv;
           start = &ep->data[(int)ep->cur];
         q = 0;          q = 0;
   
         if ('\0' == *start)          if ('\0' == *start)
                 return(NULL);                  return(NULL);
   
         if ('"' == *start) {          if (quote == *start) {
                 start++;                  ep->cur++;
                 q = 1;                  q = 1;
         }          }
   
         *next = q ? strchr(start, '"') : strchr(start, ' ');          start = &ep->data[(int)ep->cur];
           next = q ? strchr(start, quote) : strchr(start, ' ');
   
         if (NULL != *next) {          if (NULL != next) {
                 *sz = (size_t)(*next - start);                  *sz = (size_t)(next - start);
                   ep->cur += *sz;
                 if (q)                  if (q)
                         (*next)++;                          ep->cur++;
                 while (' ' == **next)                  while (' ' == ep->data[(int)ep->cur])
                         (*next)++;                          ep->cur++;
         } else {          } else {
                 /*  
                  * XXX: groff gets confused by this and doesn't always  
                  * do the "right thing" (just terminate it and warn  
                  * about it).  
                  */  
                 if (q)                  if (q)
                         mandoc_msg(MANDOCERR_BADQUOTE,                          EQN_MSG(MANDOCERR_BADQUOTE, ep);
                                         mp, ln, pos, NULL);                  next = strchr(start, '\0');
                 *next = strchr(start, '\0');                  *sz = (size_t)(next - start);
                 *sz = (size_t)(*next - start);                  ep->cur += *sz;
         }          }
   
           /* Quotes aren't expanded for values. */
   
           if (q || ! repl)
                   return(start);
   
           if (NULL != (def = eqn_def_find(ep, start, *sz))) {
                   diff = def->valsz - *sz;
   
                   if (def->valsz > *sz) {
                           ep->sz += diff;
                           ep->data = mandoc_realloc(ep->data, ep->sz + 1);
                           ep->data[ep->sz] = '\0';
                           start = &ep->data[(int)sv];
                   }
   
                   diff = def->valsz - *sz;
                   memmove(start + *sz + diff, start + *sz,
                                   (strlen(start) - *sz) + 1);
                   memcpy(start, def->val, def->valsz);
                   goto again;
           }
   
         return(start);          return(start);
 }  }
   
 static int  static int
 eqn_do_set(struct eqn_node *ep, int ln, int pos, const char **end)  eqn_do_set(struct eqn_node *ep)
 {  {
         const char      *start;          const char      *start;
         struct mparse   *mp;  
         size_t           sz;  
   
         mp = ep->parse;          if (NULL == (start = eqn_nextrawtok(ep, NULL)))
                   EQN_MSG(MANDOCERR_EQNARGS, ep);
           else if (NULL == (start = eqn_nextrawtok(ep, NULL)))
                   EQN_MSG(MANDOCERR_EQNARGS, ep);
           else
                   return(1);
   
         start = eqn_nexttok(ep->parse, ln, pos, end, &sz);          return(0);
         if (NULL == start || 0 == sz) {  
                 mandoc_msg(MANDOCERR_EQNARGS, mp, ln, pos, NULL);  
                 return(0);  
         }  
   
         start = eqn_nexttok(ep->parse, ln, pos, end, &sz);  
         if (NULL == start || 0 == sz) {  
                 mandoc_msg(MANDOCERR_EQNARGS, mp, ln, pos, NULL);  
                 return(0);  
         }  
   
         return(1);  
 }  }
   
 static int  static int
 eqn_do_define(struct eqn_node *ep, int ln, int pos, const char **end)  eqn_do_define(struct eqn_node *ep)
 {  {
         const char      *start;          const char      *start;
         struct mparse   *mp;  
         size_t           sz;          size_t           sz;
           struct eqn_def  *def;
         int              i;          int              i;
   
         mp = ep->parse;          if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
                   EQN_MSG(MANDOCERR_EQNARGS, ep);
         start = eqn_nexttok(mp, ln, pos, end, &sz);  
         if (NULL == start || 0 == sz) {  
                 mandoc_msg(MANDOCERR_EQNARGS, mp, ln, pos, NULL);  
                 return(0);                  return(0);
         }          }
   
         /* TODO: merge this code with roff_getstr(). */  
   
         /*          /*
          * Search for a key that already exists.           * Search for a key that already exists.
          * Note that the string array can have "holes" (null key).           * Create a new key if none is found.
          */           */
   
         for (i = 0; i < (int)ep->defsz; i++)  {          if (NULL == (def = eqn_def_find(ep, start, sz))) {
                 if (0 == ep->defs[i].keysz || ep->defs[i].keysz != sz)  
                         continue;  
                 if (0 == strncmp(ep->defs[i].key, start, sz))  
                         break;  
         }  
   
         /* Create a new key. */  
   
         if (i == (int)ep->defsz) {  
                 /* Find holes in string array. */                  /* Find holes in string array. */
                 for (i = 0; i < (int)ep->defsz; i++)                  for (i = 0; i < (int)ep->defsz; i++)
                         if (0 == ep->defs[i].keysz)                          if (0 == ep->defs[i].keysz)
Line 283  eqn_do_define(struct eqn_node *ep, int ln, int pos, co
Line 408  eqn_do_define(struct eqn_node *ep, int ln, int pos, co
                         ep->defs = mandoc_realloc                          ep->defs = mandoc_realloc
                                 (ep->defs, ep->defsz *                                  (ep->defs, ep->defsz *
                                  sizeof(struct eqn_def));                                   sizeof(struct eqn_def));
                           ep->defs[i].key = ep->defs[i].val = NULL;
                 }                  }
   
                 ep->defs[i].keysz = sz;                  ep->defs[i].keysz = sz;
Line 291  eqn_do_define(struct eqn_node *ep, int ln, int pos, co
Line 417  eqn_do_define(struct eqn_node *ep, int ln, int pos, co
   
                 memcpy(ep->defs[i].key, start, sz);                  memcpy(ep->defs[i].key, start, sz);
                 ep->defs[i].key[(int)sz] = '\0';                  ep->defs[i].key[(int)sz] = '\0';
                 ep->defs[i].val = NULL;                  def = &ep->defs[i];
                 ep->defs[i].valsz = 0;  
         }          }
   
         start = eqn_nexttok(mp, ln, pos, end, &sz);          start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);
   
         if (NULL == start || 0 == sz) {          if (NULL == start) {
                 ep->defs[i].keysz = 0;                  EQN_MSG(MANDOCERR_EQNARGS, ep);
                 mandoc_msg(MANDOCERR_EQNARGS, mp, ln, pos, NULL);  
                 return(0);                  return(0);
         }          }
   
         ep->defs[i].valsz = sz;          def->valsz = sz;
         ep->defs[i].val = mandoc_realloc          def->val = mandoc_realloc(def->val, sz + 1);
                 (ep->defs[i].val, sz + 1);          memcpy(def->val, start, sz);
         memcpy(ep->defs[i].val, start, sz);          def->val[(int)sz] = '\0';
         ep->defs[i].val[(int)sz] = '\0';          return(1);
   
         return(sz ? 1 : 0);  
 }  }
   
 static int  static int
 eqn_do_undef(struct eqn_node *ep, int ln, int pos, const char **end)  eqn_do_undef(struct eqn_node *ep)
 {  {
         const char      *start;          const char      *start;
         struct mparse   *mp;          struct eqn_def  *def;
         size_t           sz;          size_t           sz;
         int              i;  
   
         mp = ep->parse;          if (NULL == (start = eqn_nextrawtok(ep, &sz))) {
                   EQN_MSG(MANDOCERR_EQNARGS, ep);
         start = eqn_nexttok(mp, ln, pos, end, &sz);  
         if (NULL == start || 0 == sz) {  
                 mandoc_msg(MANDOCERR_EQNARGS, mp, ln, pos, NULL);  
                 return(0);                  return(0);
         }          } else if (NULL != (def = eqn_def_find(ep, start, sz)))
                   def->keysz = 0;
   
         for (i = 0; i < (int)ep->defsz; i++)  {  
                 if (0 == ep->defs[i].keysz || ep->defs[i].keysz != sz)  
                         continue;  
                 if (strncmp(ep->defs[i].key, start, sz))  
                         continue;  
                 ep->defs[i].keysz = 0;  
                 break;  
         }  
   
         return(1);          return(1);
   }
   
   static struct eqn_def *
   eqn_def_find(struct eqn_node *ep, const char *key, size_t sz)
   {
           int              i;
   
           for (i = 0; i < (int)ep->defsz; i++)
                   if (ep->defs[i].keysz && ep->defs[i].keysz == sz &&
                                   0 == strncmp(ep->defs[i].key, key, sz))
                           return(&ep->defs[i]);
   
           return(NULL);
 }  }

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.16

CVSweb