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

Diff for /mandoc/eqn.c between version 1.63 and 1.67

version 1.63, 2017/06/20 17:24:35 version 1.67, 2017/06/22 00:30:20
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>   * Copyright (c) 2014, 2015, 2017 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 20 
Line 20 
 #include <sys/types.h>  #include <sys/types.h>
   
 #include <assert.h>  #include <assert.h>
   #include <ctype.h>
 #include <limits.h>  #include <limits.h>
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
Line 80  enum eqn_tok {
Line 81  enum eqn_tok {
         EQN_TOK_TDEFINE,          EQN_TOK_TDEFINE,
         EQN_TOK_NDEFINE,          EQN_TOK_NDEFINE,
         EQN_TOK_UNDEF,          EQN_TOK_UNDEF,
         EQN_TOK_EOF,  
         EQN_TOK_ABOVE,          EQN_TOK_ABOVE,
         EQN_TOK__MAX          EQN_TOK__MAX,
           EQN_TOK_FUNC,
           EQN_TOK_QUOTED,
           EQN_TOK_SYM,
           EQN_TOK_EOF
 };  };
   
 static  const char *eqn_toks[EQN_TOK__MAX] = {  static  const char *eqn_toks[EQN_TOK__MAX] = {
Line 130  static const char *eqn_toks[EQN_TOK__MAX] = {
Line 134  static const char *eqn_toks[EQN_TOK__MAX] = {
         "tdefine", /* EQN_TOK_TDEFINE */          "tdefine", /* EQN_TOK_TDEFINE */
         "ndefine", /* EQN_TOK_NDEFINE */          "ndefine", /* EQN_TOK_NDEFINE */
         "undef", /* EQN_TOK_UNDEF */          "undef", /* EQN_TOK_UNDEF */
         NULL, /* EQN_TOK_EOF */  
         "above", /* EQN_TOK_ABOVE */          "above", /* EQN_TOK_ABOVE */
 };  };
   
   static  const char *const eqn_func[] = {
           "acos", "acsc", "and",  "arc",  "asec", "asin", "atan",
           "cos",  "cosh", "coth", "csc",  "det",  "exp",  "for",
           "if",   "lim",  "ln",   "log",  "max",  "min",
           "sec",  "sin",  "sinh", "tan",  "tanh", "Im",   "Re",
   };
   
 enum    eqn_symt {  enum    eqn_symt {
         EQNSYM_alpha,          EQNSYM_alpha,
         EQNSYM_beta,          EQNSYM_beta,
Line 498  eqn_tok_parse(struct eqn_node *ep, char **p)
Line 508  eqn_tok_parse(struct eqn_node *ep, char **p)
         size_t           i, sz;          size_t           i, sz;
         int              quoted;          int              quoted;
   
         if (NULL != p)          if (p != NULL)
                 *p = NULL;                  *p = NULL;
   
         quoted = ep->data[ep->cur] == '"';          quoted = ep->data[ep->cur] == '"';
   
         if (NULL == (start = eqn_nexttok(ep, &sz)))          if ((start = eqn_nexttok(ep, &sz)) == NULL)
                 return EQN_TOK_EOF;                  return EQN_TOK_EOF;
   
         if (quoted) {          if (quoted) {
                 if (p != NULL)                  if (p != NULL)
                         *p = mandoc_strndup(start, sz);                          *p = mandoc_strndup(start, sz);
                 return EQN_TOK__MAX;                  return EQN_TOK_QUOTED;
         }          }
   
         for (i = 0; i < EQN_TOK__MAX; i++) {          for (i = 0; i < EQN_TOK__MAX; i++)
                 if (NULL == eqn_toks[i])  
                         continue;  
                 if (STRNEQ(start, sz, eqn_toks[i], strlen(eqn_toks[i])))                  if (STRNEQ(start, sz, eqn_toks[i], strlen(eqn_toks[i])))
                         break;                          return i;
   
           for (i = 0; i < EQNSYM__MAX; i++) {
                   if (STRNEQ(start, sz,
                       eqnsyms[i].str, strlen(eqnsyms[i].str))) {
                           mandoc_asprintf(p, "\\[%s]", eqnsyms[i].sym);
                           return EQN_TOK_SYM;
                   }
         }          }
   
         if (i == EQN_TOK__MAX && NULL != p)          if (p != NULL)
                 *p = mandoc_strndup(start, sz);                  *p = mandoc_strndup(start, sz);
   
         return i;          for (i = 0; i < sizeof(eqn_func)/sizeof(*eqn_func); i++)
                   if (STRNEQ(start, sz, eqn_func[i], strlen(eqn_func[i])))
                           return EQN_TOK_FUNC;
   
           return EQN_TOK__MAX;
 }  }
   
 static void  static void
Line 702  static enum rofferr
Line 721  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];          char             sym[64];
         struct eqn_box  *cur;          struct eqn_box  *cur, *fontp, *nbox;
         const char      *start;          const char      *cp, *cpn, *start;
         char            *p;          char            *p;
         size_t           i, sz;          size_t           sz;
         enum eqn_tok     tok, subtok;          enum eqn_tok     tok, subtok;
         enum eqn_post    pos;          enum eqn_post    pos;
         int              size;          int              size;
Line 1067  this_tok:
Line 1086  this_tok:
                  * TODO: make sure we're not in an open subexpression.                   * TODO: make sure we're not in an open subexpression.
                  */                   */
                 return ROFF_EQN;                  return ROFF_EQN;
         default:          case EQN_TOK__MAX:
                 assert(tok == EQN_TOK__MAX);          case EQN_TOK_FUNC:
                 assert(NULL != p);          case EQN_TOK_QUOTED:
           case EQN_TOK_SYM:
                   assert(p != NULL);
                 /*                  /*
                  * If we already have something in the stack and we're                   * If we already have something in the stack and we're
                  * in an expression, then rewind til we're not any more.                   * in an expression, then rewind til we're not any more.
                  */                   */
                 while (parent->args == parent->expectargs)                  while (parent->args == parent->expectargs)
                         parent = parent->parent;                          parent = parent->parent;
                   /*
                    * Wrap well-known function names in a roman box,
                    * unless they already are in roman context.
                    */
                   for (fontp = parent; fontp != NULL; fontp = fontp->parent)
                           if (fontp->font != EQNFONT_NONE)
                                   break;
                   if (tok == EQN_TOK_FUNC &&
                       (fontp == NULL || fontp->font != EQNFONT_ROMAN)) {
                           parent = fontp = eqn_box_alloc(ep, parent);
                           parent->type = EQN_LISTONE;
                           parent->font = EQNFONT_ROMAN;
                           parent->expectargs = 1;
                   }
                 cur = eqn_box_alloc(ep, parent);                  cur = eqn_box_alloc(ep, parent);
                 cur->type = EQN_TEXT;                  cur->type = EQN_TEXT;
                 for (i = 0; i < EQNSYM__MAX; i++)                  cur->text = p;
                         if (0 == strcmp(eqnsyms[i].str, p)) {                  /*
                                 (void)snprintf(sym, sizeof(sym),                   * If not inside any explicit font context,
                                         "\\[%s]", eqnsyms[i].sym);                   * quoted strings become italic, and every letter
                                 cur->text = mandoc_strdup(sym);                   * of a bare string gets its own italic box.
                                 free(p);                   */
                   do {
                           if (fontp != NULL || *p == '\0' ||
                               tok == EQN_TOK_SYM)
                                 break;                                  break;
                           if (tok == EQN_TOK_QUOTED) {
                                   cur->font = EQNFONT_ITALIC;
                                   break;
                         }                          }
                           cp = p;
                 if (i == EQNSYM__MAX)                          for (;;) {
                         cur->text = p;                                  if (isalpha((unsigned char)*cp))
                                           cur->font = EQNFONT_ITALIC;
                                   cpn = cp + 1;
                                   if (*cp == '\\')
                                           mandoc_escape(&cpn, NULL, NULL);
                                   if (*cpn == '\0')
                                           break;
                                   if (cur->font != EQNFONT_ITALIC &&
                                       isalpha((unsigned char)*cpn) == 0) {
                                           cp = cpn;
                                           continue;
                                   }
                                   nbox = eqn_box_alloc(ep, parent);
                                   nbox->type = EQN_TEXT;
                                   nbox->text = mandoc_strdup(cpn);
                                   p = mandoc_strndup(cur->text,
                                       cpn - cur->text);
                                   free(cur->text);
                                   cur->text = p;
                                   cur = nbox;
                                   cp = nbox->text;
                           }
                   } while (0);
                 /*                  /*
                  * Post-process list status.                   * Post-process list status.
                  */                   */
Line 1096  this_tok:
Line 1159  this_tok:
                     parent->args == parent->expectargs)                      parent->args == parent->expectargs)
                         parent = parent->parent;                          parent = parent->parent;
                 break;                  break;
           default:
                   abort();
         }          }
         goto next_tok;          goto next_tok;
 }  }

Legend:
Removed from v.1.63  
changed lines
  Added in v.1.67

CVSweb