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

Diff for /mandoc/roff.c between version 1.167 and 1.173

version 1.167, 2011/07/29 10:16:59 version 1.173, 2012/05/31 22:41:19
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>   * Copyright (c) 2010, 2011, 2012 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 31 
Line 31 
 /* Maximum number of nested if-else conditionals. */  /* Maximum number of nested if-else conditionals. */
 #define RSTACK_MAX      128  #define RSTACK_MAX      128
   
   /* Maximum number of string expansions per line, to break infinite loops. */
   #define EXPAND_LIMIT    1000
   
 enum    rofft {  enum    rofft {
         ROFF_ad,          ROFF_ad,
         ROFF_am,          ROFF_am,
Line 179  static const char *roff_getstrn(const struct roff *, 
Line 182  static const char *roff_getstrn(const struct roff *, 
                                 const char *, size_t);                                  const char *, size_t);
 static  enum rofferr     roff_line_ignore(ROFF_ARGS);  static  enum rofferr     roff_line_ignore(ROFF_ARGS);
 static  enum rofferr     roff_nr(ROFF_ARGS);  static  enum rofferr     roff_nr(ROFF_ARGS);
 static  void             roff_openeqn(struct roff *, const char *,  static  void             roff_openeqn(struct roff *, const char *,
                                 int, int, const char *);                                  int, int, const char *);
 static  enum rofft       roff_parse(struct roff *, const char *, int *);  static  enum rofft       roff_parse(struct roff *, const char *, int *);
 static  enum rofferr     roff_parsetext(char *);  static  enum rofferr     roff_parsetext(char *);
 static  void             roff_res(struct roff *,  static  enum rofferr     roff_res(struct roff *,
                                 char **, size_t *, int, int);                                  char **, size_t *, int, int);
 static  enum rofferr     roff_rm(ROFF_ARGS);  static  enum rofferr     roff_rm(ROFF_ARGS);
 static  void             roff_setstr(struct roff *,  static  void             roff_setstr(struct roff *,
Line 429  roff_alloc(struct mparse *parse)
Line 432  roff_alloc(struct mparse *parse)
  * is processed.   * is processed.
  * This also checks the syntax of regular escapes.   * This also checks the syntax of regular escapes.
  */   */
 static void  static enum rofferr
 roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)  roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
 {  {
         enum mandoc_esc  esc;          enum mandoc_esc  esc;
Line 437  roff_res(struct roff *r, char **bufp, size_t *szp, int
Line 440  roff_res(struct roff *r, char **bufp, size_t *szp, int
         const char      *stnam; /* start of the name, after "[(*" */          const char      *stnam; /* start of the name, after "[(*" */
         const char      *cp;    /* end of the name, e.g. before ']' */          const char      *cp;    /* end of the name, e.g. before ']' */
         const char      *res;   /* the string to be substituted */          const char      *res;   /* the string to be substituted */
         int              i, maxl;          int              i, maxl, expand_count;
         size_t           nsz;          size_t           nsz;
         char            *n;          char            *n;
   
           expand_count = 0;
   
 again:  again:
         cp = *bufp + pos;          cp = *bufp + pos;
         while (NULL != (cp = strchr(cp, '\\'))) {          while (NULL != (cp = strchr(cp, '\\'))) {
Line 453  again:
Line 458  again:
                  */                   */
   
                 if ('\0' == *cp)                  if ('\0' == *cp)
                         return;                          return(ROFF_CONT);
   
                 if ('*' != *cp) {                  if ('*' != *cp) {
                         res = cp;                          res = cp;
Line 464  again:
Line 469  again:
                         mandoc_msg                          mandoc_msg
                                 (MANDOCERR_BADESCAPE, r->parse,                                  (MANDOCERR_BADESCAPE, r->parse,
                                  ln, (int)(stesc - *bufp), NULL);                                   ln, (int)(stesc - *bufp), NULL);
                         return;                          return(ROFF_CONT);
                 }                  }
   
                 cp++;                  cp++;
Line 477  again:
Line 482  again:
   
                 switch (*cp) {                  switch (*cp) {
                 case ('\0'):                  case ('\0'):
                         return;                          return(ROFF_CONT);
                 case ('('):                  case ('('):
                         cp++;                          cp++;
                         maxl = 2;                          maxl = 2;
Line 500  again:
Line 505  again:
                                         (MANDOCERR_BADESCAPE,                                          (MANDOCERR_BADESCAPE,
                                          r->parse, ln,                                           r->parse, ln,
                                          (int)(stesc - *bufp), NULL);                                           (int)(stesc - *bufp), NULL);
                                 return;                                  return(ROFF_CONT);
                         }                          }
                         if (0 == maxl && ']' == *cp)                          if (0 == maxl && ']' == *cp)
                                 break;                                  break;
Line 535  again:
Line 540  again:
   
                 *bufp = n;                  *bufp = n;
                 *szp = nsz;                  *szp = nsz;
                 goto again;  
                   if (EXPAND_LIMIT >= ++expand_count)
                           goto again;
   
                   /* Just leave the string unexpanded. */
                   mandoc_msg(MANDOCERR_ROFFLOOP, r->parse, ln, pos, NULL);
                   return(ROFF_IGN);
         }          }
           return(ROFF_CONT);
 }  }
   
 /*  /*
Line 545  again:
Line 557  again:
 static enum rofferr  static enum rofferr
 roff_parsetext(char *p)  roff_parsetext(char *p)
 {  {
         char             l, r;  
         size_t           sz;          size_t           sz;
         const char      *start;          const char      *start;
         enum mandoc_esc  esc;          enum mandoc_esc  esc;
Line 572  roff_parsetext(char *p)
Line 583  roff_parsetext(char *p)
                         continue;                          continue;
                 }                  }
   
                 l = *(p - 1);                  if (isalpha((unsigned char)p[-1]) &&
                 r = *(p + 1);                      isalpha((unsigned char)p[1]))
                 if ('\\' != l &&  
                                 '\t' != r && '\t' != l &&  
                                 ' ' != r && ' ' != l &&  
                                 '-' != r && '-' != l &&  
                                 ! isdigit((unsigned char)l) &&  
                                 ! isdigit((unsigned char)r))  
                         *p = ASCII_HYPH;                          *p = ASCII_HYPH;
                 p++;                  p++;
         }          }
Line 600  roff_parseln(struct roff *r, int ln, char **bufp, 
Line 605  roff_parseln(struct roff *r, int ln, char **bufp, 
          * words to fill in.           * words to fill in.
          */           */
   
         roff_res(r, bufp, szp, ln, pos);          e = roff_res(r, bufp, szp, ln, pos);
           if (ROFF_IGN == e)
                   return(e);
           assert(ROFF_CONT == e);
   
         ppos = pos;          ppos = pos;
         ctl = mandoc_getcontrol(*bufp, &pos);          ctl = mandoc_getcontrol(*bufp, &pos);
Line 770  roffnode_cleanscope(struct roff *r)
Line 778  roffnode_cleanscope(struct roff *r)
 {  {
   
         while (r->last) {          while (r->last) {
                 if (--r->last->endspan < 0)                  if (--r->last->endspan != 0)
                         break;                          break;
                 roffnode_pop(r);                  roffnode_pop(r);
         }          }
Line 1090  roff_line_ignore(ROFF_ARGS)
Line 1098  roff_line_ignore(ROFF_ARGS)
 static enum rofferr  static enum rofferr
 roff_cond(ROFF_ARGS)  roff_cond(ROFF_ARGS)
 {  {
         int              sv;  
         enum roffrule    rule;  
   
           roffnode_push(r, tok, NULL, ln, ppos);
   
         /*          /*
          * An `.el' has no conditional body: it will consume the value           * An `.el' has no conditional body: it will consume the value
          * of the current rstack entry set in prior `ie' calls or           * of the current rstack entry set in prior `ie' calls or
Line 1101  roff_cond(ROFF_ARGS)
Line 1109  roff_cond(ROFF_ARGS)
          * If we're not an `el', however, then evaluate the conditional.           * If we're not an `el', however, then evaluate the conditional.
          */           */
   
         rule = ROFF_el == tok ?          r->last->rule = ROFF_el == tok ?
                 (r->rstackpos < 0 ?                  (r->rstackpos < 0 ?
                  ROFFRULE_DENY : r->rstack[r->rstackpos--]) :                   ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
                 roff_evalcond(*bufp, &pos);                  roff_evalcond(*bufp, &pos);
   
         sv = pos;  
         while (' ' == (*bufp)[pos])  
                 pos++;  
   
         /*          /*
          * Roff is weird.  If we have just white-space after the  
          * conditional, it's considered the BODY and we exit without  
          * really doing anything.  Warn about this.  It's probably  
          * wrong.  
          */  
   
         if ('\0' == (*bufp)[pos] && sv != pos) {  
                 mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);  
                 return(ROFF_IGN);  
         }  
   
         roffnode_push(r, tok, NULL, ln, ppos);  
   
         r->last->rule = rule;  
   
         /*  
          * An if-else will put the NEGATION of the current evaluated           * An if-else will put the NEGATION of the current evaluated
          * conditional into the stack of rules.           * conditional into the stack of rules.
          */           */
Line 1148  roff_cond(ROFF_ARGS)
Line 1136  roff_cond(ROFF_ARGS)
                 r->last->rule = ROFFRULE_DENY;                  r->last->rule = ROFFRULE_DENY;
   
         /*          /*
          * Determine scope.  If we're invoked with "\{" trailing the           * Determine scope.
          * conditional, then we're in a multiline scope.  Else our scope           * If there is nothing on the line after the conditional,
          * expires on the next line.           * not even whitespace, use next-line scope.
          */           */
   
         r->last->endspan = 1;          if ('\0' == (*bufp)[pos]) {
                   r->last->endspan = 2;
                   goto out;
           }
   
           while (' ' == (*bufp)[pos])
                   pos++;
   
           /* An opening brace requests multiline scope. */
   
         if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {          if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
                 r->last->endspan = -1;                  r->last->endspan = -1;
                 pos += 2;                  pos += 2;
                   goto out;
         }          }
   
         /*          /*
          * If there are no arguments on the line, the next-line scope is           * Anything else following the conditional causes
          * assumed.           * single-line scope.  Warn if the scope contains
            * nothing but trailing whitespace.
          */           */
   
         if ('\0' == (*bufp)[pos])          if ('\0' == (*bufp)[pos])
                 return(ROFF_IGN);                  mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
   
         /* Otherwise re-run the roff parser after recalculating. */          r->last->endspan = 1;
   
   out:
         *offs = pos;          *offs = pos;
         return(ROFF_RERUN);          return(ROFF_RERUN);
 }  }
Line 1665  roff_eqn(const struct roff *r)
Line 1664  roff_eqn(const struct roff *r)
 {  {
   
         return(r->last_eqn ? &r->last_eqn->eqn : NULL);          return(r->last_eqn ? &r->last_eqn->eqn : NULL);
 }  
   
 char  
 roff_eqndelim(const struct roff *r)  
 {  
   
         return('\0');  
 }  }
   
 /*  /*

Legend:
Removed from v.1.167  
changed lines
  Added in v.1.173

CVSweb