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

Diff for /mandoc/roff.c between version 1.266 and 1.271

version 1.266, 2015/04/19 13:50:26 version 1.271, 2015/05/31 23:13:22
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2009-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>   * Copyright (c) 2010-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
Line 32 
Line 32 
 #include "libmandoc.h"  #include "libmandoc.h"
 #include "roff_int.h"  #include "roff_int.h"
 #include "libroff.h"  #include "libroff.h"
 #include "libmdoc.h"  
   
 /* Maximum number of nested if-else conditionals. */  /* Maximum number of nested if-else conditionals. */
 #define RSTACK_MAX      128  #define RSTACK_MAX      128
Line 398  static enum rofferr  roff_cond_text(ROFF_ARGS);
Line 397  static enum rofferr  roff_cond_text(ROFF_ARGS);
 static  enum rofferr     roff_cond_sub(ROFF_ARGS);  static  enum rofferr     roff_cond_sub(ROFF_ARGS);
 static  enum rofferr     roff_ds(ROFF_ARGS);  static  enum rofferr     roff_ds(ROFF_ARGS);
 static  enum rofferr     roff_eqndelim(struct roff *, struct buf *, int);  static  enum rofferr     roff_eqndelim(struct roff *, struct buf *, int);
 static  int              roff_evalcond(struct roff *r, int,  static  int              roff_evalcond(struct roff *r, int, char *, int *);
                                 const char *, int *);  
 static  int              roff_evalnum(struct roff *, int,  static  int              roff_evalnum(struct roff *, int,
                                 const char *, int *, int *, int);                                  const char *, int *, int *, int);
 static  int              roff_evalpar(struct roff *, int,  static  int              roff_evalpar(struct roff *, int,
Line 416  static int   roff_getregn(const struct roff *,
Line 414  static int   roff_getregn(const struct roff *,
 static  int              roff_getregro(const char *name);  static  int              roff_getregro(const char *name);
 static  const char      *roff_getstrn(const struct roff *,  static  const char      *roff_getstrn(const struct roff *,
                                 const char *, size_t);                                  const char *, size_t);
   static  int              roff_hasregn(const struct roff *,
                                   const char *, size_t);
 static  enum rofferr     roff_insec(ROFF_ARGS);  static  enum rofferr     roff_insec(ROFF_ARGS);
 static  enum rofferr     roff_it(ROFF_ARGS);  static  enum rofferr     roff_it(ROFF_ARGS);
 static  enum rofferr     roff_line_ignore(ROFF_ARGS);  static  enum rofferr     roff_line_ignore(ROFF_ARGS);
Line 1023  roff_node_append(struct roff_man *man, struct roff_nod
Line 1023  roff_node_append(struct roff_man *man, struct roff_nod
                 /* NOTREACHED */                  /* NOTREACHED */
         }          }
         n->parent->nchild++;          n->parent->nchild++;
           n->parent->last = n;
   
         /*          /*
          * Copy over the normalised-data pointer of our parent.  Not           * Copy over the normalised-data pointer of our parent.  Not
Line 1067  roff_node_append(struct roff_man *man, struct roff_nod
Line 1068  roff_node_append(struct roff_man *man, struct roff_nod
         man->last = n;          man->last = n;
 }  }
   
   void
   roff_word_alloc(struct roff_man *man, int line, int pos, const char *word)
   {
           struct roff_node        *n;
   
           n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
           n->string = roff_strdup(man->roff, word);
           roff_node_append(man, n);
           if (man->macroset == MACROSET_MDOC)
                   mdoc_valid_post(man);
           else
                   man_valid_post(man);
           man->next = ROFF_NEXT_SIBLING;
   }
   
   void
   roff_word_append(struct roff_man *man, const char *word)
   {
           struct roff_node        *n;
           char                    *addstr, *newstr;
   
           n = man->last;
           addstr = roff_strdup(man->roff, word);
           mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
           free(addstr);
           free(n->string);
           n->string = newstr;
           man->next = ROFF_NEXT_SIBLING;
   }
   
   void
   roff_elem_alloc(struct roff_man *man, int line, int pos, int tok)
   {
           struct roff_node        *n;
   
           n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
           roff_node_append(man, n);
           man->next = ROFF_NEXT_CHILD;
   }
   
 struct roff_node *  struct roff_node *
   roff_block_alloc(struct roff_man *man, int line, int pos, int tok)
   {
           struct roff_node        *n;
   
           n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
           roff_node_append(man, n);
           man->next = ROFF_NEXT_CHILD;
           return(n);
   }
   
   struct roff_node *
 roff_head_alloc(struct roff_man *man, int line, int pos, int tok)  roff_head_alloc(struct roff_man *man, int line, int pos, int tok)
 {  {
         struct roff_node        *n;          struct roff_node        *n;
Line 1090  roff_body_alloc(struct roff_man *man, int line, int po
Line 1142  roff_body_alloc(struct roff_man *man, int line, int po
 }  }
   
 void  void
   roff_addeqn(struct roff_man *man, const struct eqn *eqn)
   {
           struct roff_node        *n;
   
           n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE);
           n->eqn = eqn;
           if (eqn->ln > man->last->line)
                   n->flags |= MDOC_LINE;
           roff_node_append(man, n);
           man->next = ROFF_NEXT_SIBLING;
   }
   
   void
   roff_addtbl(struct roff_man *man, const struct tbl_span *tbl)
   {
           struct roff_node        *n;
   
           if (man->macroset == MACROSET_MAN)
                   man_breakscope(man, TOKEN_NONE);
           n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);
           n->span = tbl;
           roff_node_append(man, n);
           if (man->macroset == MACROSET_MDOC)
                   mdoc_valid_post(man);
           else
                   man_valid_post(man);
           man->next = ROFF_NEXT_SIBLING;
   }
   
   void
 roff_node_unlink(struct roff_man *man, struct roff_node *n)  roff_node_unlink(struct roff_man *man, struct roff_node *n)
 {  {
   
Line 1150  roff_node_delete(struct roff_man *man, struct roff_nod
Line 1232  roff_node_delete(struct roff_man *man, struct roff_nod
         roff_node_free(n);          roff_node_free(n);
 }  }
   
   void
   deroff(char **dest, const struct roff_node *n)
   {
           char    *cp;
           size_t   sz;
   
           if (n->type != ROFFT_TEXT) {
                   for (n = n->child; n != NULL; n = n->next)
                           deroff(dest, n);
                   return;
           }
   
           /* Skip leading whitespace and escape sequences. */
   
           cp = n->string;
           while (*cp != '\0') {
                   if ('\\' == *cp) {
                           cp++;
                           mandoc_escape((const char **)&cp, NULL, NULL);
                   } else if (isspace((unsigned char)*cp))
                           cp++;
                   else
                           break;
           }
   
           /* Skip trailing whitespace. */
   
           for (sz = strlen(cp); sz; sz--)
                   if ( ! isspace((unsigned char)cp[sz-1]))
                           break;
   
           /* Skip empty strings. */
   
           if (sz == 0)
                   return;
   
           if (*dest == NULL) {
                   *dest = mandoc_strndup(cp, sz);
                   return;
           }
   
           mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
           free(*dest);
           *dest = cp;
   }
   
 /* --- main functions of the roff parser ---------------------------------- */  /* --- main functions of the roff parser ---------------------------------- */
   
 /*  /*
Line 2007  out:
Line 2135  out:
  * or string condition.   * or string condition.
  */   */
 static int  static int
 roff_evalcond(struct roff *r, int ln, const char *v, int *pos)  roff_evalcond(struct roff *r, int ln, char *v, int *pos)
 {  {
           char    *cp, *name;
           size_t   sz;
         int      number, savepos, wanttrue;          int      number, savepos, wanttrue;
   
         if ('!' == v[*pos]) {          if ('!' == v[*pos]) {
Line 2031  roff_evalcond(struct roff *r, int ln, const char *v, i
Line 2161  roff_evalcond(struct roff *r, int ln, const char *v, i
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case 'e':          case 'e':
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case 'r':  
                 /* FALLTHROUGH */  
         case 't':          case 't':
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case 'v':          case 'v':
                 (*pos)++;                  (*pos)++;
                 return(!wanttrue);                  return(!wanttrue);
           case 'r':
                   cp = name = v + ++*pos;
                   sz = roff_getname(r, &cp, ln, *pos);
                   *pos = cp - v;
                   return((sz && roff_hasregn(r, name, sz)) == wanttrue);
         default:          default:
                 break;                  break;
         }          }
Line 2496  roff_getregn(const struct roff *r, const char *name, s
Line 2629  roff_getregn(const struct roff *r, const char *name, s
                 if (len == reg->key.sz &&                  if (len == reg->key.sz &&
                     0 == strncmp(name, reg->key.p, len))                      0 == strncmp(name, reg->key.p, len))
                         return(reg->val);                          return(reg->val);
   
           return(0);
   }
   
   static int
   roff_hasregn(const struct roff *r, const char *name, size_t len)
   {
           struct roffreg  *reg;
           int              val;
   
           if ('.' == name[0] && 2 == len) {
                   val = roff_getregro(name + 1);
                   if (-1 != val)
                           return(1);
           }
   
           for (reg = r->regtab; reg; reg = reg->next)
                   if (len == reg->key.sz &&
                       0 == strncmp(name, reg->key.p, len))
                           return(1);
   
         return(0);          return(0);
 }  }

Legend:
Removed from v.1.266  
changed lines
  Added in v.1.271

CVSweb