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

Diff for /mandoc/eqn.c between version 1.52 and 1.57

version 1.52, 2014/10/12 19:31:41 version 1.57, 2015/01/28 21:11:53
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 30 
Line 31 
 #include "libmandoc.h"  #include "libmandoc.h"
 #include "libroff.h"  #include "libroff.h"
   
 #define EQN_MSG(t, x) \  
         mandoc_msg((t), (x)->parse, (x)->eqn.ln, (x)->eqn.pos, NULL)  
 #define EQN_NEST_MAX     128 /* maximum nesting of defines */  #define EQN_NEST_MAX     128 /* maximum nesting of defines */
 #define STRNEQ(p1, sz1, p2, sz2) \  #define STRNEQ(p1, sz1, p2, sz2) \
         ((sz1) == (sz2) && 0 == strncmp((p1), (p2), (sz1)))          ((sz1) == (sz2) && 0 == strncmp((p1), (p2), (sz1)))
 #define EQNSTREQ(x, p, sz) \  
         STRNEQ((x)->name, (x)->sz, (p), (sz))  
   
 enum    eqn_tok {  enum    eqn_tok {
         EQN_TOK_DYAD = 0,          EQN_TOK_DYAD = 0,
Line 267  static const struct eqnsym eqnsyms[EQNSYM__MAX] = {
Line 264  static const struct eqnsym eqnsyms[EQNSYM__MAX] = {
         { ">=", ">=" }, /* EQNSYM_moreequal */          { ">=", ">=" }, /* EQNSYM_moreequal */
 };  };
   
   static  struct eqn_box  *eqn_box_alloc(struct eqn_node *, struct eqn_box *);
   static  void             eqn_box_free(struct eqn_box *);
   static  struct eqn_box  *eqn_box_makebinary(struct eqn_node *,
                                   enum eqn_post, struct eqn_box *);
   static  void             eqn_def(struct eqn_node *);
   static  struct eqn_def  *eqn_def_find(struct eqn_node *, const char *, size_t);
   static  void             eqn_delim(struct eqn_node *);
   static  const char      *eqn_next(struct eqn_node *, char, size_t *, int);
   static  const char      *eqn_nextrawtok(struct eqn_node *, size_t *);
   static  const char      *eqn_nexttok(struct eqn_node *, size_t *);
   static  enum rofferr     eqn_parse(struct eqn_node *, struct eqn_box *);
   static  enum eqn_tok     eqn_tok_parse(struct eqn_node *, char **);
   static  void             eqn_undef(struct eqn_node *);
   
   
 enum rofferr  enum rofferr
 eqn_read(struct eqn_node **epp, int ln,  eqn_read(struct eqn_node **epp, int ln,
                 const char *p, int pos, int *offs)                  const char *p, int pos, int *offs)
Line 314  eqn_read(struct eqn_node **epp, int ln,
Line 326  eqn_read(struct eqn_node **epp, int ln,
 }  }
   
 struct eqn_node *  struct eqn_node *
 eqn_alloc(const char *name, int pos, int line, struct mparse *parse)  eqn_alloc(int pos, int line, struct mparse *parse)
 {  {
         struct eqn_node *p;          struct eqn_node *p;
         size_t           sz;  
         const char      *end;  
   
         p = mandoc_calloc(1, sizeof(struct eqn_node));          p = mandoc_calloc(1, sizeof(struct eqn_node));
   
         if (name && '\0' != *name) {  
                 sz = strlen(name);  
                 assert(sz);  
                 do {  
                         sz--;  
                         end = name + (int)sz;  
                 } while (' ' == *end || '\t' == *end);  
                 p->eqn.name = mandoc_strndup(name, sz + 1);  
         }  
   
         p->parse = parse;          p->parse = parse;
         p->eqn.ln = line;          p->eqn.ln = line;
         p->eqn.pos = pos;          p->eqn.pos = pos;
Line 378  again:
Line 378  again:
         /* Prevent self-definitions. */          /* Prevent self-definitions. */
   
         if (lim >= EQN_NEST_MAX) {          if (lim >= EQN_NEST_MAX) {
                 EQN_MSG(MANDOCERR_ROFFLOOP, ep);                  mandoc_msg(MANDOCERR_ROFFLOOP, ep->parse,
                       ep->eqn.ln, ep->eqn.pos, NULL);
                 return(NULL);                  return(NULL);
         }          }
   
Line 419  again:
Line 420  again:
                         ep->cur++;                          ep->cur++;
         } else {          } else {
                 if (q)                  if (q)
                         EQN_MSG(MANDOCERR_ARG_QUOTE, ep);                          mandoc_msg(MANDOCERR_ARG_QUOTE, ep->parse,
                               ep->eqn.ln, ep->eqn.pos, NULL);
                 next = strchr(start, '\0');                  next = strchr(start, '\0');
                 *sz = (size_t)(next - start);                  *sz = (size_t)(next - start);
                 ep->cur += *sz;                  ep->cur += *sz;
Line 486  eqn_tok_parse(struct eqn_node *ep, char **p)
Line 488  eqn_tok_parse(struct eqn_node *ep, char **p)
 {  {
         const char      *start;          const char      *start;
         size_t           i, sz;          size_t           i, sz;
           int              quoted;
   
         if (NULL != p)          if (NULL != p)
                 *p = NULL;                  *p = NULL;
   
           quoted = ep->data[ep->cur] == '"';
   
         if (NULL == (start = eqn_nexttok(ep, &sz)))          if (NULL == (start = eqn_nexttok(ep, &sz)))
                 return(EQN_TOK_EOF);                  return(EQN_TOK_EOF);
   
           if (quoted) {
                   if (p != NULL)
                           *p = mandoc_strndup(start, sz);
                   return(EQN_TOK__MAX);
           }
   
         for (i = 0; i < EQN_TOK__MAX; i++) {          for (i = 0; i < EQN_TOK__MAX; i++) {
                 if (NULL == eqn_toks[i])                  if (NULL == eqn_toks[i])
                         continue;                          continue;
Line 578  eqn_box_makebinary(struct eqn_node *ep,
Line 589  eqn_box_makebinary(struct eqn_node *ep,
 }  }
   
 /*  /*
    * Parse the "delim" control statement.
    */
   static void
   eqn_delim(struct eqn_node *ep)
   {
           const char      *start;
           size_t           sz;
   
           if ((start = eqn_nextrawtok(ep, &sz)) == NULL)
                   mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
                       ep->eqn.ln, ep->eqn.pos, "delim");
           else if (strncmp(start, "off", 3) == 0)
                   ep->delim = 0;
           else if (strncmp(start, "on", 2) == 0) {
                   if (ep->odelim && ep->cdelim)
                           ep->delim = 1;
           } else if (start[1] != '\0') {
                   ep->odelim = start[0];
                   ep->cdelim = start[1];
                   ep->delim = 1;
           }
   }
   
   /*
  * Undefine a previously-defined string.   * Undefine a previously-defined string.
  */   */
 static int  static void
 eqn_undef(struct eqn_node *ep)  eqn_undef(struct eqn_node *ep)
 {  {
         const char      *start;          const char      *start;
         struct eqn_def  *def;          struct eqn_def  *def;
         size_t           sz;          size_t           sz;
   
         if (NULL == (start = eqn_nextrawtok(ep, &sz))) {          if ((start = eqn_nextrawtok(ep, &sz)) == NULL) {
                 EQN_MSG(MANDOCERR_EQNEOF, ep);                  mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
                 return(0);                      ep->eqn.ln, ep->eqn.pos, "undef");
         } else if (NULL != (def = eqn_def_find(ep, start, sz)))                  return;
                 def->keysz = 0;          }
           if ((def = eqn_def_find(ep, start, sz)) == NULL)
         return(1);                  return;
           free(def->key);
           free(def->val);
           def->key = def->val = NULL;
           def->keysz = def->valsz = 0;
 }  }
   
 static int  static void
 eqn_def(struct eqn_node *ep)  eqn_def(struct eqn_node *ep)
 {  {
         const char      *start;          const char      *start;
Line 604  eqn_def(struct eqn_node *ep)
Line 643  eqn_def(struct eqn_node *ep)
         struct eqn_def  *def;          struct eqn_def  *def;
         int              i;          int              i;
   
         if (NULL == (start = eqn_nextrawtok(ep, &sz))) {          if ((start = eqn_nextrawtok(ep, &sz)) == NULL) {
                 EQN_MSG(MANDOCERR_EQNEOF, ep);                  mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
                 return(0);                      ep->eqn.ln, ep->eqn.pos, "define");
                   return;
         }          }
   
         /*          /*
Line 626  eqn_def(struct eqn_node *ep)
Line 666  eqn_def(struct eqn_node *ep)
                         ep->defs[i].key = ep->defs[i].val = NULL;                          ep->defs[i].key = ep->defs[i].val = NULL;
                 }                  }
   
                 ep->defs[i].keysz = sz;                  def = ep->defs + i;
                 ep->defs[i].key = mandoc_realloc(                  free(def->key);
                     ep->defs[i].key, sz + 1);                  def->key = mandoc_strndup(start, sz);
                   def->keysz = sz;
                 memcpy(ep->defs[i].key, start, sz);  
                 ep->defs[i].key[(int)sz] = '\0';  
                 def = &ep->defs[i];  
         }          }
   
         start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);          start = eqn_next(ep, ep->data[(int)ep->cur], &sz, 0);
           if (start == NULL) {
         if (NULL == start) {                  mandoc_vmsg(MANDOCERR_REQ_EMPTY, ep->parse,
                 EQN_MSG(MANDOCERR_EQNEOF, ep);                      ep->eqn.ln, ep->eqn.pos, "define %s", def->key);
                 return(-1);                  free(def->key);
                   free(def->val);
                   def->key = def->val = NULL;
                   def->keysz = def->valsz = 0;
                   return;
         }          }
           free(def->val);
           def->val = mandoc_strndup(start, sz);
         def->valsz = sz;          def->valsz = sz;
         def->val = mandoc_realloc(def->val, sz + 1);  
         memcpy(def->val, start, sz);  
         def->val[(int)sz] = '\0';  
         return(1);  
 }  }
   
 /*  /*
  * Recursively parse an eqn(7) expression.   * Recursively parse an eqn(7) expression.
  */   */
 static int  static enum rofferr
 eqn_parse(struct eqn_node *ep, struct eqn_box *parent)  eqn_parse(struct eqn_node *ep, struct eqn_box *parent)
 {  {
           char             sym[64];
           struct eqn_box  *cur;
           const char      *start;
         char            *p;          char            *p;
           size_t           i, sz;
         enum eqn_tok     tok, subtok;          enum eqn_tok     tok, subtok;
         enum eqn_post    pos;          enum eqn_post    pos;
         struct eqn_box  *cur;          int              size;
         int              rc, size;  
         size_t           i, sz;  
         char             sym[64];  
         const char      *start;  
   
         assert(NULL != parent);          assert(parent != NULL);
   
           /*
            * Empty equation.
            * Do not add it to the high-level syntax tree.
            */
   
           if (ep->data == NULL)
                   return(ROFF_IGN);
   
 next_tok:  next_tok:
         tok = eqn_tok_parse(ep, &p);          tok = eqn_tok_parse(ep, &p);
   
 this_tok:  this_tok:
         switch (tok) {          switch (tok) {
         case (EQN_TOK_UNDEF):          case (EQN_TOK_UNDEF):
                 if ((rc = eqn_undef(ep)) <= 0)                  eqn_undef(ep);
                         return(rc);  
                 break;                  break;
         case (EQN_TOK_NDEFINE):          case (EQN_TOK_NDEFINE):
         case (EQN_TOK_DEFINE):          case (EQN_TOK_DEFINE):
                 if ((rc = eqn_def(ep)) <= 0)                  eqn_def(ep);
                         return(rc);  
                 break;                  break;
         case (EQN_TOK_TDEFINE):          case (EQN_TOK_TDEFINE):
                 if (NULL == eqn_nextrawtok(ep, NULL))                  if (eqn_nextrawtok(ep, NULL) == NULL ||
                         EQN_MSG(MANDOCERR_EQNEOF, ep);                      eqn_next(ep, ep->data[(int)ep->cur], NULL, 0) == NULL)
                 else if (NULL == eqn_next(ep,                          mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
                                 ep->data[(int)ep->cur], NULL, 0))                              ep->eqn.ln, ep->eqn.pos, "tdefine");
                         EQN_MSG(MANDOCERR_EQNEOF, ep);  
                 break;                  break;
         case (EQN_TOK_DELIM):          case (EQN_TOK_DELIM):
                   eqn_delim(ep);
                   break;
         case (EQN_TOK_GFONT):          case (EQN_TOK_GFONT):
                 if (eqn_nextrawtok(ep, NULL) == NULL)                  if (eqn_nextrawtok(ep, NULL) == NULL)
                         mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,                          mandoc_msg(MANDOCERR_REQ_EMPTY, ep->parse,
Line 1013  this_tok:
Line 1058  this_tok:
                  * End of file!                   * End of file!
                  * TODO: make sure we're not in an open subexpression.                   * TODO: make sure we're not in an open subexpression.
                  */                   */
                 return(0);                  return(ROFF_EQN);
         default:          default:
                 assert(tok == EQN_TOK__MAX);                  assert(tok == EQN_TOK__MAX);
                 assert(NULL != p);                  assert(NULL != p);
Line 1057  eqn_end(struct eqn_node **epp)
Line 1102  eqn_end(struct eqn_node **epp)
   
         ep->eqn.root = mandoc_calloc(1, sizeof(struct eqn_box));          ep->eqn.root = mandoc_calloc(1, sizeof(struct eqn_box));
         ep->eqn.root->expectargs = UINT_MAX;          ep->eqn.root->expectargs = UINT_MAX;
         return(0 == eqn_parse(ep, ep->eqn.root) ? ROFF_EQN : ROFF_IGN);          return(eqn_parse(ep, ep->eqn.root));
 }  }
   
 void  void
Line 1072  eqn_free(struct eqn_node *p)
Line 1117  eqn_free(struct eqn_node *p)
                 free(p->defs[i].val);                  free(p->defs[i].val);
         }          }
   
         free(p->eqn.name);  
         free(p->data);          free(p->data);
         free(p->defs);          free(p->defs);
         free(p);          free(p);

Legend:
Removed from v.1.52  
changed lines
  Added in v.1.57

CVSweb