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

Diff for /mandoc/roff.c between version 1.224 and 1.237

version 1.224, 2014/08/01 17:27:44 version 1.237, 2014/10/28 17:36:19
Line 15 
Line 15 
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */   */
 #ifdef HAVE_CONFIG_H  
 #include "config.h"  #include "config.h"
 #endif  
   
   #include <sys/types.h>
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
 #include <stdio.h>  #include <stdio.h>
Line 27 
Line 27 
   
 #include "mandoc.h"  #include "mandoc.h"
 #include "mandoc_aux.h"  #include "mandoc_aux.h"
 #include "libroff.h"  
 #include "libmandoc.h"  #include "libmandoc.h"
   #include "libroff.h"
   
 /* Maximum number of nested if-else conditionals. */  /* Maximum number of nested if-else conditionals. */
 #define RSTACK_MAX      128  #define RSTACK_MAX      128
Line 60  enum rofft {
Line 60  enum rofft {
         ROFF_nh,          ROFF_nh,
         ROFF_nr,          ROFF_nr,
         ROFF_ns,          ROFF_ns,
           ROFF_pl,
         ROFF_ps,          ROFF_ps,
         ROFF_rm,          ROFF_rm,
         ROFF_rr,          ROFF_rr,
Line 106  struct roffreg {
Line 107  struct roffreg {
   
 struct  roff {  struct  roff {
         struct mparse   *parse; /* parse point */          struct mparse   *parse; /* parse point */
           const struct mchars *mchars; /* character table */
         struct roffnode *last; /* leaf of stack */          struct roffnode *last; /* leaf of stack */
         int             *rstack; /* stack of inverted `ie' values */          int             *rstack; /* stack of inverted `ie' values */
         struct roffreg  *regtab; /* number registers */          struct roffreg  *regtab; /* number registers */
Line 119  struct roff {
Line 121  struct roff {
         struct eqn_node *last_eqn; /* last equation parsed */          struct eqn_node *last_eqn; /* last equation parsed */
         struct eqn_node *first_eqn; /* first equation parsed */          struct eqn_node *first_eqn; /* first equation parsed */
         struct eqn_node *eqn; /* current equation being parsed */          struct eqn_node *eqn; /* current equation being parsed */
           int              eqn_inline; /* current equation is inline */
         int              options; /* parse options */          int              options; /* parse options */
         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 */
         char             control; /* control character */          char             control; /* control character */
 };  };
   
Line 181  static enum rofferr  roff_cond(ROFF_ARGS);
Line 185  static enum rofferr  roff_cond(ROFF_ARGS);
 static  enum rofferr     roff_cond_text(ROFF_ARGS);  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  int              roff_evalcond(const char *, int *);  static  enum rofferr     roff_eqndelim(struct roff *,
 static  int              roff_evalnum(const char *, int *, int *, int);                                  char **, size_t *, int);
 static  int              roff_evalpar(const char *, int *, int *);  static  int              roff_evalcond(struct roff *r, int,
                                   const char *, int *);
   static  int              roff_evalnum(struct roff *, int,
                                   const char *, int *, int *, int);
   static  int              roff_evalpar(struct roff *, int,
                                   const char *, int *, int *);
 static  int              roff_evalstrcond(const char *, int *);  static  int              roff_evalstrcond(const char *, int *);
 static  void             roff_free1(struct roff *);  static  void             roff_free1(struct roff *);
 static  void             roff_freereg(struct roffreg *);  static  void             roff_freereg(struct roffreg *);
Line 199  static const char *roff_getstrn(const struct roff *,
Line 208  static const char *roff_getstrn(const struct roff *,
 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);
 static  enum rofferr     roff_nr(ROFF_ARGS);  static  enum rofferr     roff_nr(ROFF_ARGS);
 static  void             roff_openeqn(struct roff *, const char *,  
                                 int, int, const char *);  
 static  enum rofft       roff_parse(struct roff *, char *, int *,  static  enum rofft       roff_parse(struct roff *, char *, int *,
                                 int, int);                                  int, int);
 static  enum rofferr     roff_parsetext(char **, size_t *, int, int *);  static  enum rofferr     roff_parsetext(char **, size_t *, int, int *);
Line 255  static struct roffmac  roffs[ROFF_MAX] = {
Line 262  static struct roffmac  roffs[ROFF_MAX] = {
         { "nh", roff_line_ignore, NULL, NULL, 0, NULL },          { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
         { "nr", roff_nr, NULL, NULL, 0, NULL },          { "nr", roff_nr, NULL, NULL, 0, NULL },
         { "ns", roff_line_ignore, NULL, NULL, 0, NULL },          { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
           { "pl", roff_line_ignore, NULL, NULL, 0, NULL },
         { "ps", roff_line_ignore, NULL, NULL, 0, NULL },          { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
         { "rm", roff_rm, NULL, NULL, 0, NULL },          { "rm", roff_rm, NULL, NULL, 0, NULL },
         { "rr", roff_rr, NULL, NULL, 0, NULL },          { "rr", roff_rr, NULL, NULL, 0, NULL },
Line 456  roff_reset(struct roff *r)
Line 464  roff_reset(struct roff *r)
 {  {
   
         roff_free1(r);          roff_free1(r);
           r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
         r->control = 0;          r->control = 0;
 }  }
   
Line 468  roff_free(struct roff *r)
Line 477  roff_free(struct roff *r)
 }  }
   
 struct roff *  struct roff *
 roff_alloc(struct mparse *parse, int options)  roff_alloc(struct mparse *parse, const struct mchars *mchars, int options)
 {  {
         struct roff     *r;          struct roff     *r;
   
         r = mandoc_calloc(1, sizeof(struct roff));          r = mandoc_calloc(1, sizeof(struct roff));
         r->parse = parse;          r->parse = parse;
           r->mchars = mchars;
         r->options = options;          r->options = options;
           r->format = options & (MPARSE_MDOC | MPARSE_MAN);
         r->rstackpos = -1;          r->rstackpos = -1;
   
         roffhash_init();          roffhash_init();
Line 499  roff_res(struct roff *r, char **bufp, size_t *szp, int
Line 510  roff_res(struct roff *r, char **bufp, size_t *szp, int
         char            *nbuf;  /* new buffer to copy bufp to */          char            *nbuf;  /* new buffer to copy bufp to */
         size_t           maxl;  /* expected length of the escape name */          size_t           maxl;  /* expected length of the escape name */
         size_t           naml;  /* actual length of the escape name */          size_t           naml;  /* actual length of the escape name */
           enum mandoc_esc  esc;   /* type of the escape sequence */
           int              inaml; /* length returned from mandoc_escape() */
         int              expand_count;  /* to avoid infinite loops */          int              expand_count;  /* to avoid infinite loops */
         int              npos;  /* position in numeric expression */          int              npos;  /* position in numeric expression */
         int              arg_complete; /* argument not interrupted by eol */          int              arg_complete; /* argument not interrupted by eol */
Line 542  roff_res(struct roff *r, char **bufp, size_t *szp, int
Line 555  roff_res(struct roff *r, char **bufp, size_t *szp, int
                         res = ubuf;                          res = ubuf;
                         break;                          break;
                 default:                  default:
                         if (ESCAPE_ERROR == mandoc_escape(&cp, NULL, NULL))                          esc = mandoc_escape(&cp, &stnam, &inaml);
                           if (esc == ESCAPE_ERROR ||
                               (esc == ESCAPE_SPECIAL &&
                                mchars_spec2cp(r->mchars, stnam, inaml) < 0))
                                 mandoc_vmsg(MANDOCERR_ESC_BAD,                                  mandoc_vmsg(MANDOCERR_ESC_BAD,
                                     r->parse, ln, (int)(stesc - *bufp),                                      r->parse, ln, (int)(stesc - *bufp),
                                     "%.*s", (int)(cp - stesc), stesc);                                      "%.*s", (int)(cp - stesc), stesc);
Line 614  roff_res(struct roff *r, char **bufp, size_t *szp, int
Line 630  roff_res(struct roff *r, char **bufp, size_t *szp, int
                 case 'B':                  case 'B':
                         npos = 0;                          npos = 0;
                         ubuf[0] = arg_complete &&                          ubuf[0] = arg_complete &&
                             roff_evalnum(stnam, &npos, NULL, 0) &&                              roff_evalnum(r, ln, stnam, &npos, NULL, 0) &&
                             stnam + npos + 1 == cp ? '1' : '0';                              stnam + npos + 1 == cp ? '1' : '0';
                         ubuf[1] = '\0';                          ubuf[1] = '\0';
                         break;                          break;
Line 719  roff_parseln(struct roff *r, int ln, char **bufp,
Line 735  roff_parseln(struct roff *r, int ln, char **bufp,
         enum rofferr     e;          enum rofferr     e;
         int              ppos, ctl;          int              ppos, ctl;
   
         /*          /* Handle in-line equation delimiters. */
          * Run the reserved-word filter only if we have some reserved  
          * words to fill in.  
          */  
   
           if (r->tbl == NULL &&
               r->last_eqn != NULL && r->last_eqn->delim &&
               (r->eqn == NULL || r->eqn_inline)) {
                   e = roff_eqndelim(r, bufp, szp, pos);
                   if (e == ROFF_REPARSE)
                           return(e);
                   assert(e == ROFF_CONT);
           }
   
           /* Expand some escape sequences. */
   
         e = roff_res(r, bufp, szp, ln, pos);          e = roff_res(r, bufp, szp, ln, pos);
         if (ROFF_IGN == e)          if (ROFF_IGN == e)
                 return(e);                  return(e);
Line 756  roff_parseln(struct roff *r, int ln, char **bufp,
Line 780  roff_parseln(struct roff *r, int ln, char **bufp,
                 return(roff_parsetext(bufp, szp, pos, offs));                  return(roff_parsetext(bufp, szp, pos, offs));
         }          }
   
           /* Skip empty request lines. */
   
           if ((*bufp)[pos] == '"') {
                   mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
                       ln, pos, NULL);
                   return(ROFF_IGN);
           } else if ((*bufp)[pos] == '\0')
                   return(ROFF_IGN);
   
         /*          /*
          * If a scope is open, go to the child handler for that macro,           * If a scope is open, go to the child handler for that macro,
          * as it may want to preprocess before doing anything with it.           * as it may want to preprocess before doing anything with it.
Line 1216  out:
Line 1249  out:
  * or string condition.   * or string condition.
  */   */
 static int  static int
 roff_evalcond(const char *v, int *pos)  roff_evalcond(struct roff *r, int ln, const char *v, int *pos)
 {  {
         int      wanttrue, number;          int      wanttrue, number;
   
Line 1247  roff_evalcond(const char *v, int *pos)
Line 1280  roff_evalcond(const char *v, int *pos)
                 break;                  break;
         }          }
   
         if (roff_evalnum(v, pos, &number, 0))          if (roff_evalnum(r, ln, v, pos, &number, 0))
                 return((number > 0) == wanttrue);                  return((number > 0) == wanttrue);
         else          else
                 return(roff_evalstrcond(v, pos) == wanttrue);                  return(roff_evalstrcond(v, pos) == wanttrue);
Line 1276  roff_cond(ROFF_ARGS)
Line 1309  roff_cond(ROFF_ARGS)
   
         r->last->rule = ROFF_el == tok ?          r->last->rule = ROFF_el == tok ?
             (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :              (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
             roff_evalcond(*bufp, &pos);              roff_evalcond(r, ln, *bufp, &pos);
   
         /*          /*
          * An if-else will put the NEGATION of the current evaluated           * An if-else will put the NEGATION of the current evaluated
Line 1442  roff_getop(const char *v, int *pos, char *res)
Line 1475  roff_getop(const char *v, int *pos, char *res)
  * or a single signed integer number.   * or a single signed integer number.
  */   */
 static int  static int
 roff_evalpar(const char *v, int *pos, int *res)  roff_evalpar(struct roff *r, int ln,
           const char *v, int *pos, int *res)
 {  {
   
         if ('(' != v[*pos])          if ('(' != v[*pos])
                 return(roff_getnum(v, pos, res));                  return(roff_getnum(v, pos, res));
   
         (*pos)++;          (*pos)++;
         if ( ! roff_evalnum(v, pos, res, 1))          if ( ! roff_evalnum(r, ln, v, pos, res, 1))
                 return(0);                  return(0);
   
         /*          /*
Line 1471  roff_evalpar(const char *v, int *pos, int *res)
Line 1505  roff_evalpar(const char *v, int *pos, int *res)
  * Proceed left to right, there is no concept of precedence.   * Proceed left to right, there is no concept of precedence.
  */   */
 static int  static int
 roff_evalnum(const char *v, int *pos, int *res, int skipwhite)  roff_evalnum(struct roff *r, int ln, const char *v,
           int *pos, int *res, int skipwhite)
 {  {
         int              mypos, operand2;          int              mypos, operand2;
         char             operator;          char             operator;
Line 1485  roff_evalnum(const char *v, int *pos, int *res, int sk
Line 1520  roff_evalnum(const char *v, int *pos, int *res, int sk
                 while (isspace((unsigned char)v[*pos]))                  while (isspace((unsigned char)v[*pos]))
                         (*pos)++;                          (*pos)++;
   
         if ( ! roff_evalpar(v, pos, res))          if ( ! roff_evalpar(r, ln, v, pos, res))
                 return(0);                  return(0);
   
         while (1) {          while (1) {
Line 1500  roff_evalnum(const char *v, int *pos, int *res, int sk
Line 1535  roff_evalnum(const char *v, int *pos, int *res, int sk
                         while (isspace((unsigned char)v[*pos]))                          while (isspace((unsigned char)v[*pos]))
                                 (*pos)++;                                  (*pos)++;
   
                 if ( ! roff_evalpar(v, pos, &operand2))                  if ( ! roff_evalpar(r, ln, v, pos, &operand2))
                         return(0);                          return(0);
   
                 if (skipwhite)                  if (skipwhite)
Line 1521  roff_evalnum(const char *v, int *pos, int *res, int sk
Line 1556  roff_evalnum(const char *v, int *pos, int *res, int sk
                         *res *= operand2;                          *res *= operand2;
                         break;                          break;
                 case '/':                  case '/':
                           if (0 == operand2) {
                                   mandoc_msg(MANDOCERR_DIVZERO,
                                           r->parse, ln, *pos, v);
                                   *res = 0;
                                   break;
                           }
                         *res /= operand2;                          *res /= operand2;
                         break;                          break;
                 case '%':                  case '%':
Line 1695  roff_nr(ROFF_ARGS)
Line 1736  roff_nr(ROFF_ARGS)
         if ('+' == sign || '-' == sign)          if ('+' == sign || '-' == sign)
                 val++;                  val++;
   
         if (roff_evalnum(val, NULL, &iv, 0))          if (roff_evalnum(r, ln, val, NULL, &iv, 0))
                 roff_setreg(r, key, iv, sign);                  roff_setreg(r, key, iv, sign);
   
         return(ROFF_IGN);          return(ROFF_IGN);
Line 1776  roff_Dd(ROFF_ARGS)
Line 1817  roff_Dd(ROFF_ARGS)
 {  {
         const char *const       *cp;          const char *const       *cp;
   
         if (0 == ((MPARSE_MDOC | MPARSE_QUICK) & r->options))          if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
                 for (cp = __mdoc_reserved; *cp; cp++)                  for (cp = __mdoc_reserved; *cp; cp++)
                         roff_setstr(r, *cp, NULL, 0);                          roff_setstr(r, *cp, NULL, 0);
   
           if (r->format == 0)
                   r->format = MPARSE_MDOC;
   
         return(ROFF_CONT);          return(ROFF_CONT);
 }  }
   
Line 1788  roff_TH(ROFF_ARGS)
Line 1832  roff_TH(ROFF_ARGS)
 {  {
         const char *const       *cp;          const char *const       *cp;
   
         if (0 == (MPARSE_QUICK & r->options))          if ((r->options & MPARSE_QUICK) == 0)
                 for (cp = __man_reserved; *cp; cp++)                  for (cp = __man_reserved; *cp; cp++)
                         roff_setstr(r, *cp, NULL, 0);                          roff_setstr(r, *cp, NULL, 0);
   
           if (r->format == 0)
                   r->format = MPARSE_MAN;
   
         return(ROFF_CONT);          return(ROFF_CONT);
 }  }
   
Line 1821  roff_T_(ROFF_ARGS)
Line 1868  roff_T_(ROFF_ARGS)
         return(ROFF_IGN);          return(ROFF_IGN);
 }  }
   
 #if 0  /*
 static int   * Handle in-line equation delimiters.
 roff_closeeqn(struct roff *r)   */
   static enum rofferr
   roff_eqndelim(struct roff *r, char **bufp, size_t *szp, int pos)
 {  {
           char            *cp1, *cp2;
           const char      *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
   
         return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);          /*
            * Outside equations, look for an opening delimiter.
            * If we are inside an equation, we already know it is
            * in-line, or this function wouldn't have been called;
            * so look for a closing delimiter.
            */
   
           cp1 = *bufp + pos;
           cp2 = strchr(cp1, r->eqn == NULL ?
               r->last_eqn->odelim : r->last_eqn->cdelim);
           if (cp2 == NULL)
                   return(ROFF_CONT);
   
           *cp2++ = '\0';
           bef_pr = bef_nl = aft_nl = aft_pr = "";
   
           /* Handle preceding text, protecting whitespace. */
   
           if (**bufp != '\0') {
                   if (r->eqn == NULL)
                           bef_pr = "\\&";
                   bef_nl = "\n";
           }
   
           /*
            * Prepare replacing the delimiter with an equation macro
            * and drop leading white space from the equation.
            */
   
           if (r->eqn == NULL) {
                   while (*cp2 == ' ')
                           cp2++;
                   mac = ".EQ";
           } else
                   mac = ".EN";
   
           /* Handle following text, protecting whitespace. */
   
           if (*cp2 != '\0') {
                   aft_nl = "\n";
                   if (r->eqn != NULL)
                           aft_pr = "\\&";
           }
   
           /* Do the actual replacement. */
   
           *szp = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", *bufp,
               bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
           free(*bufp);
           *bufp = cp1;
   
           /* Toggle the in-line state of the eqn subsystem. */
   
           r->eqn_inline = r->eqn == NULL;
           return(ROFF_REPARSE);
 }  }
 #endif  
   
 static void  static enum rofferr
 roff_openeqn(struct roff *r, const char *name, int line,  roff_EQ(ROFF_ARGS)
                 int offs, const char *buf)  
 {  {
         struct eqn_node *e;          struct eqn_node *e;
         int              poff;  
   
         assert(NULL == r->eqn);          assert(NULL == r->eqn);
         e = eqn_alloc(name, offs, line, r->parse);          e = eqn_alloc(ppos, ln, r->parse);
   
         if (r->last_eqn)          if (r->last_eqn) {
                 r->last_eqn->next = e;                  r->last_eqn->next = e;
         else                  e->delim = r->last_eqn->delim;
                   e->odelim = r->last_eqn->odelim;
                   e->cdelim = r->last_eqn->cdelim;
           } else
                 r->first_eqn = r->last_eqn = e;                  r->first_eqn = r->last_eqn = e;
   
         r->eqn = r->last_eqn = e;          r->eqn = r->last_eqn = e;
   
         if (buf) {          if ((*bufp)[pos])
                 poff = 0;                  mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
                 eqn_read(&r->eqn, line, buf, offs, &poff);                      ".EQ %s", *bufp + pos);
         }  
 }  
   
 static enum rofferr  
 roff_EQ(ROFF_ARGS)  
 {  
   
         roff_openeqn(r, *bufp + pos, ln, ppos, NULL);  
         return(ROFF_IGN);          return(ROFF_IGN);
 }  }
   
Line 2305  roff_strdup(const struct roff *r, const char *p)
Line 2403  roff_strdup(const struct roff *r, const char *p)
   
         res[(int)ssz] = '\0';          res[(int)ssz] = '\0';
         return(res);          return(res);
   }
   
   int
   roff_getformat(const struct roff *r)
   {
   
           return(r->format);
 }  }
   
 /*  /*

Legend:
Removed from v.1.224  
changed lines
  Added in v.1.237

CVSweb