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

version 1.266, 2015/04/19 13:50:26 version 1.274, 2015/08/29 21:37:20
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 336  struct roff {
Line 335  struct roff {
         int              rstacksz; /* current size limit of rstack */          int              rstacksz; /* current size limit of rstack */
         int              rstackpos; /* position in rstack */          int              rstackpos; /* position in rstack */
         int              format; /* current file in mdoc or man format */          int              format; /* current file in mdoc or man format */
           int              argc; /* number of args of the last macro */
         char             control; /* control character */          char             control; /* control character */
 };  };
   
Line 398  static enum rofferr  roff_cond_text(ROFF_ARGS);
Line 398  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 413  static int   roff_getnum(const char *, int *, int *, i
Line 412  static int   roff_getnum(const char *, int *, int *, i
 static  int              roff_getop(const char *, int *, char *);  static  int              roff_getop(const char *, int *, char *);
 static  int              roff_getregn(const struct roff *,  static  int              roff_getregn(const struct roff *,
                                 const char *, size_t);                                  const char *, size_t);
 static  int              roff_getregro(const char *name);  static  int              roff_getregro(const struct roff *,
                                   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 1025  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 1070  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 1144  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 1234  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 2137  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 2163  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 2132  roff_cond(ROFF_ARGS)
Line 2267  roff_cond(ROFF_ARGS)
         if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {          if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
                 r->last->endspan = -1;                  r->last->endspan = -1;
                 pos += 2;                  pos += 2;
                   while (buf->buf[pos] == ' ')
                           pos++;
                 goto out;                  goto out;
         }          }
   
Line 2440  roff_setreg(struct roff *r, const char *name, int val,
Line 2577  roff_setreg(struct roff *r, const char *name, int val,
  * were to turn up, another special value would have to be chosen.   * were to turn up, another special value would have to be chosen.
  */   */
 static int  static int
 roff_getregro(const char *name)  roff_getregro(const struct roff *r, const char *name)
 {  {
   
         switch (*name) {          switch (*name) {
           case '$':  /* Number of arguments of the last macro evaluated. */
                   return(r->argc);
         case 'A':  /* ASCII approximation mode is always off. */          case 'A':  /* ASCII approximation mode is always off. */
                 return(0);                  return(0);
         case 'g':  /* Groff compatibility mode is always on. */          case 'g':  /* Groff compatibility mode is always on. */
Line 2468  roff_getreg(const struct roff *r, const char *name)
Line 2607  roff_getreg(const struct roff *r, const char *name)
         int              val;          int              val;
   
         if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {          if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
                 val = roff_getregro(name + 1);                  val = roff_getregro(r, name + 1);
                 if (-1 != val)                  if (-1 != val)
                         return (val);                          return (val);
         }          }
Line 2487  roff_getregn(const struct roff *r, const char *name, s
Line 2626  roff_getregn(const struct roff *r, const char *name, s
         int              val;          int              val;
   
         if ('.' == name[0] && 2 == len) {          if ('.' == name[0] && 2 == len) {
                 val = roff_getregro(name + 1);                  val = roff_getregro(r, name + 1);
                 if (-1 != val)                  if (-1 != val)
                         return (val);                          return (val);
         }          }
Line 2500  roff_getregn(const struct roff *r, const char *name, s
Line 2639  roff_getregn(const struct roff *r, const char *name, s
         return(0);          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(r, 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);
   }
   
 static void  static void
 roff_freereg(struct roffreg *reg)  roff_freereg(struct roffreg *reg)
 {  {
Line 2921  roff_userdef(ROFF_ARGS)
Line 3080  roff_userdef(ROFF_ARGS)
 {  {
         const char       *arg[9], *ap;          const char       *arg[9], *ap;
         char             *cp, *n1, *n2;          char             *cp, *n1, *n2;
         int               i;          int               i, ib, ie;
         size_t            asz, rsz;          size_t            asz, rsz;
   
         /*          /*
Line 2929  roff_userdef(ROFF_ARGS)
Line 3088  roff_userdef(ROFF_ARGS)
          * and NUL-terminate them.           * and NUL-terminate them.
          */           */
   
           r->argc = 0;
         cp = buf->buf + pos;          cp = buf->buf + pos;
         for (i = 0; i < 9; i++)          for (i = 0; i < 9; i++) {
                 arg[i] = *cp == '\0' ? "" :                  if (*cp == '\0')
                     mandoc_getarg(r->parse, &cp, ln, &pos);                          arg[i] = "";
                   else {
                           arg[i] = mandoc_getarg(r->parse, &cp, ln, &pos);
                           r->argc = i + 1;
                   }
           }
   
         /*          /*
          * Expand macro arguments.           * Expand macro arguments.
Line 2949  roff_userdef(ROFF_ARGS)
Line 3114  roff_userdef(ROFF_ARGS)
                         continue;                          continue;
                 if (*cp++ != '$')                  if (*cp++ != '$')
                         continue;                          continue;
                 i = *cp - '1';                  if (*cp == '*') {  /* \\$* inserts all arguments */
                 if (0 > i || 8 < i)                          ib = 0;
                         continue;                          ie = r->argc - 1;
                   } else {  /* \\$1 .. \\$9 insert one argument */
                           ib = ie = *cp - '1';
                           if (ib < 0 || ib > 8)
                                   continue;
                   }
                 cp -= 2;                  cp -= 2;
   
                 /*                  /*
Line 2959  roff_userdef(ROFF_ARGS)
Line 3129  roff_userdef(ROFF_ARGS)
                  * taking escaping of quotes into account.                   * taking escaping of quotes into account.
                  */                   */
   
                 asz = 0;                  asz = ie > ib ? ie - ib : 0;  /* for blanks */
                 for (ap = arg[i]; *ap != '\0'; ap++) {                  for (i = ib; i <= ie; i++) {
                         asz++;                          for (ap = arg[i]; *ap != '\0'; ap++) {
                         if (*ap == '"')                                  asz++;
                                 asz += 3;                                  if (*ap == '"')
                                           asz += 3;
                           }
                 }                  }
                 if (asz != 3) {                  if (asz != 3) {
   
Line 3004  roff_userdef(ROFF_ARGS)
Line 3176  roff_userdef(ROFF_ARGS)
                 /* Copy the expanded argument, escaping quotes. */                  /* Copy the expanded argument, escaping quotes. */
   
                 n2 = cp;                  n2 = cp;
                 for (ap = arg[i]; *ap != '\0'; ap++) {                  for (i = ib; i <= ie; i++) {
                         if (*ap == '"') {                          for (ap = arg[i]; *ap != '\0'; ap++) {
                                 memcpy(n2, "\\(dq", 4);                                  if (*ap == '"') {
                                 n2 += 4;                                          memcpy(n2, "\\(dq", 4);
                         } else                                          n2 += 4;
                                 *n2++ = *ap;                                  } else
                                           *n2++ = *ap;
                           }
                           if (i < ie)
                                   *n2++ = ' ';
                 }                  }
         }          }
   

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

CVSweb