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

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

version 1.173, 2012/05/31 22:41:19 version 1.187, 2013/12/15 21:23:52
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>   * Copyright (c) 2010, 2011, 2012, 2013 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 21 
Line 21 
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
   #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
Line 39  enum rofft {
Line 40  enum rofft {
         ROFF_am,          ROFF_am,
         ROFF_ami,          ROFF_ami,
         ROFF_am1,          ROFF_am1,
           ROFF_cc,
         ROFF_de,          ROFF_de,
         ROFF_dei,          ROFF_dei,
         ROFF_de1,          ROFF_de1,
         ROFF_ds,          ROFF_ds,
         ROFF_el,          ROFF_el,
           ROFF_fam,
           ROFF_hw,
         ROFF_hy,          ROFF_hy,
         ROFF_ie,          ROFF_ie,
         ROFF_if,          ROFF_if,
Line 58  enum rofft {
Line 62  enum rofft {
         ROFF_so,          ROFF_so,
         ROFF_ta,          ROFF_ta,
         ROFF_tr,          ROFF_tr,
           ROFF_Dd,
           ROFF_TH,
         ROFF_TS,          ROFF_TS,
         ROFF_TE,          ROFF_TE,
         ROFF_T_,          ROFF_T_,
Line 70  enum rofft {
Line 76  enum rofft {
 };  };
   
 enum    roffrule {  enum    roffrule {
         ROFFRULE_ALLOW,          ROFFRULE_DENY,
         ROFFRULE_DENY          ROFFRULE_ALLOW
 };  };
   
 /*  /*
  * A single register entity.  If "set" is zero, the value of the  
  * register should be the default one, which is per-register.  
  * Registers are assumed to be unsigned ints for now.  
  */  
 struct  reg {  
         int              set; /* whether set or not */  
         unsigned int     u; /* unsigned integer */  
 };  
   
 /*  
  * An incredibly-simple string buffer.   * An incredibly-simple string buffer.
  */   */
 struct  roffstr {  struct  roffstr {
Line 101  struct roffkv {
Line 97  struct roffkv {
         struct roffkv   *next; /* next in list */          struct roffkv   *next; /* next in list */
 };  };
   
   /*
    * A single number register as part of a singly-linked list.
    */
   struct  roffreg {
           struct roffstr   key;
           int              val;
           struct roffreg  *next;
   };
   
 struct  roff {  struct  roff {
           enum mparset     parsetype; /* requested parse type */
         struct mparse   *parse; /* parse point */          struct mparse   *parse; /* parse point */
         struct roffnode *last; /* leaf of stack */          struct roffnode *last; /* leaf of stack */
         enum roffrule    rstack[RSTACK_MAX]; /* stack of !`ie' rules */          enum roffrule    rstack[RSTACK_MAX]; /* stack of !`ie' rules */
           char             control; /* control character */
         int              rstackpos; /* position in rstack */          int              rstackpos; /* position in rstack */
         struct reg       regs[REG__MAX];          struct roffreg  *regtab; /* number registers */
         struct roffkv   *strtab; /* user-defined strings & macros */          struct roffkv   *strtab; /* user-defined strings & macros */
         struct roffkv   *xmbtab; /* multi-byte trans table (`tr') */          struct roffkv   *xmbtab; /* multi-byte trans table (`tr') */
         struct roffstr  *xtab; /* single-byte trans table (`tr') */          struct roffstr  *xtab; /* single-byte trans table (`tr') */
Line 169  static enum rofferr  roff_block(ROFF_ARGS);
Line 176  static enum rofferr  roff_block(ROFF_ARGS);
 static  enum rofferr     roff_block_text(ROFF_ARGS);  static  enum rofferr     roff_block_text(ROFF_ARGS);
 static  enum rofferr     roff_block_sub(ROFF_ARGS);  static  enum rofferr     roff_block_sub(ROFF_ARGS);
 static  enum rofferr     roff_cblock(ROFF_ARGS);  static  enum rofferr     roff_cblock(ROFF_ARGS);
   static  enum rofferr     roff_cc(ROFF_ARGS);
 static  enum rofferr     roff_ccond(ROFF_ARGS);  static  enum rofferr     roff_ccond(ROFF_ARGS);
 static  enum rofferr     roff_cond(ROFF_ARGS);  static  enum rofferr     roff_cond(ROFF_ARGS);
 static  enum rofferr     roff_cond_text(ROFF_ARGS);  static  enum rofferr     roff_cond_text(ROFF_ARGS);
Line 176  static enum rofferr  roff_cond_sub(ROFF_ARGS);
Line 184  static enum rofferr  roff_cond_sub(ROFF_ARGS);
 static  enum rofferr     roff_ds(ROFF_ARGS);  static  enum rofferr     roff_ds(ROFF_ARGS);
 static  enum roffrule    roff_evalcond(const char *, int *);  static  enum roffrule    roff_evalcond(const char *, int *);
 static  void             roff_free1(struct roff *);  static  void             roff_free1(struct roff *);
   static  void             roff_freereg(struct roffreg *);
 static  void             roff_freestr(struct roffkv *);  static  void             roff_freestr(struct roffkv *);
 static  char            *roff_getname(struct roff *, char **, int, int);  static  char            *roff_getname(struct roff *, char **, int, int);
   static  int              roff_getnum(const char *, int *, int *);
   static  int              roff_getop(const char *, int *, char *);
   static  int              roff_getregn(const struct roff *,
                                   const char *, size_t);
 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  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 *,  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 **, size_t *, int, int *);
 static  enum rofferr     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);
Line 195  static void   roff_setstrn(struct roffkv **, const cha
Line 209  static void   roff_setstrn(struct roffkv **, const cha
                                 size_t, const char *, size_t, int);                                  size_t, const char *, size_t, int);
 static  enum rofferr     roff_so(ROFF_ARGS);  static  enum rofferr     roff_so(ROFF_ARGS);
 static  enum rofferr     roff_tr(ROFF_ARGS);  static  enum rofferr     roff_tr(ROFF_ARGS);
   static  enum rofferr     roff_Dd(ROFF_ARGS);
   static  enum rofferr     roff_TH(ROFF_ARGS);
 static  enum rofferr     roff_TE(ROFF_ARGS);  static  enum rofferr     roff_TE(ROFF_ARGS);
 static  enum rofferr     roff_TS(ROFF_ARGS);  static  enum rofferr     roff_TS(ROFF_ARGS);
 static  enum rofferr     roff_EQ(ROFF_ARGS);  static  enum rofferr     roff_EQ(ROFF_ARGS);
Line 215  static struct roffmac  roffs[ROFF_MAX] = {
Line 231  static struct roffmac  roffs[ROFF_MAX] = {
         { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },          { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
         { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },          { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
         { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },          { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
           { "cc", roff_cc, NULL, NULL, 0, NULL },
         { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },          { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
         { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },          { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
         { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },          { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
         { "ds", roff_ds, NULL, NULL, 0, NULL },          { "ds", roff_ds, NULL, NULL, 0, NULL },
         { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },          { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
           { "fam", roff_line_ignore, NULL, NULL, 0, NULL },
           { "hw", roff_line_ignore, NULL, NULL, 0, NULL },
         { "hy", roff_line_ignore, NULL, NULL, 0, NULL },          { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
         { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },          { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
         { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },          { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
         { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },          { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
         { "it", roff_line_ignore, NULL, NULL, 0, NULL },          { "it", roff_it, NULL, NULL, 0, NULL },
         { "ne", roff_line_ignore, NULL, NULL, 0, NULL },          { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
         { "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 },
Line 234  static struct roffmac  roffs[ROFF_MAX] = {
Line 253  static struct roffmac  roffs[ROFF_MAX] = {
         { "so", roff_so, NULL, NULL, 0, NULL },          { "so", roff_so, NULL, NULL, 0, NULL },
         { "ta", roff_line_ignore, NULL, NULL, 0, NULL },          { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
         { "tr", roff_tr, NULL, NULL, 0, NULL },          { "tr", roff_tr, NULL, NULL, 0, NULL },
           { "Dd", roff_Dd, NULL, NULL, 0, NULL },
           { "TH", roff_TH, NULL, NULL, 0, NULL },
         { "TS", roff_TS, NULL, NULL, 0, NULL },          { "TS", roff_TS, NULL, NULL, 0, NULL },
         { "TE", roff_TE, NULL, NULL, 0, NULL },          { "TE", roff_TE, NULL, NULL, 0, NULL },
         { "T&", roff_T_, NULL, NULL, 0, NULL },          { "T&", roff_T_, NULL, NULL, 0, NULL },
Line 244  static struct roffmac  roffs[ROFF_MAX] = {
Line 265  static struct roffmac  roffs[ROFF_MAX] = {
         { NULL, roff_userdef, NULL, NULL, 0, NULL },          { NULL, roff_userdef, NULL, NULL, 0, NULL },
 };  };
   
   const   char *const __mdoc_reserved[] = {
           "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
           "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
           "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
           "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
           "Ds", "Dt", "Dv", "Dx", "D1",
           "Ec", "Ed", "Ef", "Ek", "El", "Em", "em",
           "En", "Eo", "Eq", "Er", "Es", "Ev", "Ex",
           "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
           "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp", "LP",
           "Me", "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
           "Oc", "Oo", "Op", "Os", "Ot", "Ox",
           "Pa", "Pc", "Pf", "Po", "Pp", "PP", "pp", "Pq",
           "Qc", "Ql", "Qo", "Qq", "Or", "Rd", "Re", "Rs", "Rv",
           "Sc", "Sf", "Sh", "SH", "Sm", "So", "Sq",
           "Ss", "St", "Sx", "Sy",
           "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
           "%A", "%B", "%D", "%I", "%J", "%N", "%O",
           "%P", "%Q", "%R", "%T", "%U", "%V",
           NULL
   };
   
   const   char *const __man_reserved[] = {
           "AT", "B", "BI", "BR", "BT", "DE", "DS", "DT",
           "EE", "EN", "EQ", "EX", "HF", "HP", "I", "IB", "IP", "IR",
           "LP", "ME", "MT", "OP", "P", "PD", "PP", "PT",
           "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS", "SY",
           "TE", "TH", "TP", "TQ", "TS", "T&", "UC", "UE", "UR", "YS",
           NULL
   };
   
 /* Array of injected predefined strings. */  /* Array of injected predefined strings. */
 #define PREDEFS_MAX      38  #define PREDEFS_MAX      38
 static  const struct predef predefs[PREDEFS_MAX] = {  static  const struct predef predefs[PREDEFS_MAX] = {
Line 253  static const struct predef predefs[PREDEFS_MAX] = {
Line 305  static const struct predef predefs[PREDEFS_MAX] = {
 /* See roffhash_find() */  /* See roffhash_find() */
 #define ROFF_HASH(p)    (p[0] - ASCII_LO)  #define ROFF_HASH(p)    (p[0] - ASCII_LO)
   
   static  int      roffit_lines;  /* number of lines to delay */
   static  char    *roffit_macro;  /* nil-terminated macro line */
   
 static void  static void
 roffhash_init(void)  roffhash_init(void)
 {  {
Line 351  roffnode_push(struct roff *r, enum rofft tok, const ch
Line 406  roffnode_push(struct roff *r, enum rofft tok, const ch
 static void  static void
 roff_free1(struct roff *r)  roff_free1(struct roff *r)
 {  {
         struct tbl_node *t;          struct tbl_node *tbl;
         struct eqn_node *e;          struct eqn_node *e;
         int              i;          int              i;
   
         while (NULL != (t = r->first_tbl)) {          while (NULL != (tbl = r->first_tbl)) {
                 r->first_tbl = t->next;                  r->first_tbl = tbl->next;
                 tbl_free(t);                  tbl_free(tbl);
         }          }
   
         r->first_tbl = r->last_tbl = r->tbl = NULL;          r->first_tbl = r->last_tbl = r->tbl = NULL;
Line 377  roff_free1(struct roff *r)
Line 432  roff_free1(struct roff *r)
   
         r->strtab = r->xmbtab = NULL;          r->strtab = r->xmbtab = NULL;
   
           roff_freereg(r->regtab);
   
           r->regtab = NULL;
   
         if (r->xtab)          if (r->xtab)
                 for (i = 0; i < 128; i++)                  for (i = 0; i < 128; i++)
                         free(r->xtab[i].p);                          free(r->xtab[i].p);
Line 392  roff_reset(struct roff *r)
Line 451  roff_reset(struct roff *r)
   
         roff_free1(r);          roff_free1(r);
   
         memset(&r->regs, 0, sizeof(struct reg) * REG__MAX);          r->control = 0;
   
         for (i = 0; i < PREDEFS_MAX; i++)          for (i = 0; i < PREDEFS_MAX; i++)
                 roff_setstr(r, predefs[i].name, predefs[i].str, 0);                  roff_setstr(r, predefs[i].name, predefs[i].str, 0);
Line 409  roff_free(struct roff *r)
Line 468  roff_free(struct roff *r)
   
   
 struct roff *  struct roff *
 roff_alloc(struct mparse *parse)  roff_alloc(enum mparset type, struct mparse *parse)
 {  {
         struct roff     *r;          struct roff     *r;
         int              i;          int              i;
   
         r = mandoc_calloc(1, sizeof(struct roff));          r = mandoc_calloc(1, sizeof(struct roff));
           r->parsetype = type;
         r->parse = parse;          r->parse = parse;
         r->rstackpos = -1;          r->rstackpos = -1;
   
Line 427  roff_alloc(struct mparse *parse)
Line 487  roff_alloc(struct mparse *parse)
 }  }
   
 /*  /*
  * Pre-filter each and every line for reserved words (one beginning with   * In the current line, expand user-defined strings ("\*")
  * `\*', e.g., `\*(ab').  These must be handled before the actual line   * and references to number registers ("\n").
  * is processed.   * Also check the syntax of other escape sequences.
  * This also checks the syntax of regular escapes.  
  */   */
 static enum rofferr  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;          char             ubuf[12]; /* buffer to print the number */
         const char      *stesc; /* start of an escape sequence ('\\') */          const char      *stesc; /* start of an escape sequence ('\\') */
         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, expand_count;          char            *nbuf;  /* new buffer to copy bufp to */
         size_t           nsz;          size_t           nsz;   /* size of the new buffer */
         char            *n;          size_t           maxl;  /* expected length of the escape name */
           size_t           naml;  /* actual length of the escape name */
           int              expand_count;  /* to avoid infinite loops */
   
         expand_count = 0;          expand_count = 0;
   
Line 452  again:
Line 513  again:
                 stesc = cp++;                  stesc = cp++;
   
                 /*                  /*
                  * The second character must be an asterisk.                   * The second character must be an asterisk or an n.
                  * If it isn't, skip it anyway:  It is escaped,                   * If it isn't, skip it anyway:  It is escaped,
                  * so it can't start another escape sequence.                   * so it can't start another escape sequence.
                  */                   */
Line 460  again:
Line 521  again:
                 if ('\0' == *cp)                  if ('\0' == *cp)
                         return(ROFF_CONT);                          return(ROFF_CONT);
   
                 if ('*' != *cp) {                  switch (*cp) {
                         res = cp;                  case ('*'):
                         esc = mandoc_escape(&cp, NULL, NULL);                          res = NULL;
                         if (ESCAPE_ERROR != esc)                          break;
                   case ('n'):
                           res = ubuf;
                           break;
                   default:
                           if (ESCAPE_ERROR != mandoc_escape(&cp, NULL, NULL))
                                 continue;                                  continue;
                         cp = res;  
                         mandoc_msg                          mandoc_msg
                                 (MANDOCERR_BADESCAPE, r->parse,                                  (MANDOCERR_BADESCAPE, r->parse,
                                  ln, (int)(stesc - *bufp), NULL);                                   ln, (int)(stesc - *bufp), NULL);
Line 476  again:
Line 541  again:
   
                 /*                  /*
                  * The third character decides the length                   * The third character decides the length
                  * of the name of the string.                   * of the name of the string or register.
                  * Save a pointer to the name.                   * Save a pointer to the name.
                  */                   */
   
Line 499  again:
Line 564  again:
   
                 /* Advance to the end of the name. */                  /* Advance to the end of the name. */
   
                 for (i = 0; 0 == maxl || i < maxl; i++, cp++) {                  for (naml = 0; 0 == maxl || naml < maxl; naml++, cp++) {
                         if ('\0' == *cp) {                          if ('\0' == *cp) {
                                 mandoc_msg                                  mandoc_msg
                                         (MANDOCERR_BADESCAPE,                                          (MANDOCERR_BADESCAPE,
Line 516  again:
Line 581  again:
                  * undefined, resume searching for escapes.                   * undefined, resume searching for escapes.
                  */                   */
   
                 res = roff_getstrn(r, stnam, (size_t)i);                  if (NULL == res)
                           res = roff_getstrn(r, stnam, naml);
                   else
                           snprintf(ubuf, sizeof(ubuf), "%d",
                               roff_getregn(r, stnam, naml));
   
                 if (NULL == res) {                  if (NULL == res) {
                         mandoc_msg                          mandoc_msg
Line 530  again:
Line 599  again:
                 pos = stesc - *bufp;                  pos = stesc - *bufp;
   
                 nsz = *szp + strlen(res) + 1;                  nsz = *szp + strlen(res) + 1;
                 n = mandoc_malloc(nsz);                  nbuf = mandoc_malloc(nsz);
   
                 strlcpy(n, *bufp, (size_t)(stesc - *bufp + 1));                  strlcpy(nbuf, *bufp, (size_t)(stesc - *bufp + 1));
                 strlcat(n, res, nsz);                  strlcat(nbuf, res, nsz);
                 strlcat(n, cp + (maxl ? 0 : 1), nsz);                  strlcat(nbuf, cp + (maxl ? 0 : 1), nsz);
   
                 free(*bufp);                  free(*bufp);
   
                 *bufp = n;                  *bufp = nbuf;
                 *szp = nsz;                  *szp = nsz;
   
                 if (EXPAND_LIMIT >= ++expand_count)                  if (EXPAND_LIMIT >= ++expand_count)
Line 552  again:
Line 621  again:
 }  }
   
 /*  /*
  * Process text streams: convert all breakable hyphens into ASCII_HYPH.   * Process text streams:
    * Convert all breakable hyphens into ASCII_HYPH.
    * Decrement and spring input line trap.
  */   */
 static enum rofferr  static enum rofferr
 roff_parsetext(char *p)  roff_parsetext(char **bufp, size_t *szp, int pos, int *offs)
 {  {
         size_t           sz;          size_t           sz;
         const char      *start;          const char      *start;
           char            *p;
           int              isz;
         enum mandoc_esc  esc;          enum mandoc_esc  esc;
   
         start = p;          start = p = *bufp + pos;
   
         while ('\0' != *p) {          while ('\0' != *p) {
                 sz = strcspn(p, "-\\");                  sz = strcspn(p, "-\\");
Line 574  roff_parsetext(char *p)
Line 647  roff_parsetext(char *p)
                         /* Skip over escapes. */                          /* Skip over escapes. */
                         p++;                          p++;
                         esc = mandoc_escape                          esc = mandoc_escape
                                 ((const char **)&p, NULL, NULL);                                  ((const char const **)&p, NULL, NULL);
                         if (ESCAPE_ERROR == esc)                          if (ESCAPE_ERROR == esc)
                                 break;                                  break;
                         continue;                          continue;
Line 589  roff_parsetext(char *p)
Line 662  roff_parsetext(char *p)
                 p++;                  p++;
         }          }
   
           /* Spring the input line trap. */
           if (1 == roffit_lines) {
                   isz = asprintf(&p, "%s\n.%s", *bufp, roffit_macro);
                   if (-1 == isz) {
                           perror(NULL);
                           exit((int)MANDOCLEVEL_SYSERR);
                   }
                   free(*bufp);
                   *bufp = p;
                   *szp = isz + 1;
                   *offs = 0;
                   free(roffit_macro);
                   roffit_lines = 0;
                   return(ROFF_REPARSE);
           } else if (1 < roffit_lines)
                   --roffit_lines;
         return(ROFF_CONT);          return(ROFF_CONT);
 }  }
   
Line 611  roff_parseln(struct roff *r, int ln, char **bufp, 
Line 700  roff_parseln(struct roff *r, int ln, char **bufp, 
         assert(ROFF_CONT == e);          assert(ROFF_CONT == e);
   
         ppos = pos;          ppos = pos;
         ctl = mandoc_getcontrol(*bufp, &pos);          ctl = roff_getcontrol(r, *bufp, &pos);
   
         /*          /*
          * First, if a scope is open and we're not a macro, pass the           * First, if a scope is open and we're not a macro, pass the
Line 629  roff_parseln(struct roff *r, int ln, char **bufp, 
Line 718  roff_parseln(struct roff *r, int ln, char **bufp, 
                 assert(ROFF_IGN == e || ROFF_CONT == e);                  assert(ROFF_IGN == e || ROFF_CONT == e);
                 if (ROFF_CONT != e)                  if (ROFF_CONT != e)
                         return(e);                          return(e);
                 if (r->eqn)          }
                         return(eqn_read(&r->eqn, ln, *bufp, pos, offs));          if (r->eqn)
                   return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
           if ( ! ctl) {
                 if (r->tbl)                  if (r->tbl)
                         return(tbl_read(r->tbl, ln, *bufp, pos));                          return(tbl_read(r->tbl, ln, *bufp, pos));
                 return(roff_parsetext(*bufp + pos));                  return(roff_parsetext(bufp, szp, pos, offs));
         } else if ( ! ctl) {          }
                 if (r->eqn)  
                         return(eqn_read(&r->eqn, ln, *bufp, pos, offs));  
                 if (r->tbl)  
                         return(tbl_read(r->tbl, ln, *bufp, pos));  
                 return(roff_parsetext(*bufp + pos));  
         } else if (r->eqn)  
                 return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));  
   
         /*          /*
          * 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,
Line 984  roff_cond_sub(ROFF_ARGS)
Line 1068  roff_cond_sub(ROFF_ARGS)
   
         rr = r->last->rule;          rr = r->last->rule;
         roffnode_cleanscope(r);          roffnode_cleanscope(r);
           t = roff_parse(r, *bufp, &pos);
   
         /*          /*
          * If the macro is unknown, first check if it contains a closing           * Fully handle known macros when they are structurally
          * delimiter `\}'.  If it does, close out our scope and return           * required or when the conditional evaluated to true.
          * the currently-scoped rule (ignore or continue).  Else, drop  
          * into the currently-scoped rule.  
          */           */
   
         if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) {          if ((ROFF_MAX != t) &&
                 ep = &(*bufp)[pos];              (ROFF_ccond == t || ROFFRULE_ALLOW == rr ||
                 for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {               ROFFMAC_STRUCT & roffs[t].flags)) {
                         ep++;                  assert(roffs[t].proc);
                         if ('}' != *ep)                  return((*roffs[t].proc)(r, t, bufp, szp,
                                 continue;                                          ln, ppos, pos, offs));
           }
   
                         /*          /* Always check for the closing delimiter `\}'. */
                          * Make the \} go away.  
                          * This is a little haphazard, as it's not quite  
                          * clear how nroff does this.  
                          * If we're at the end of line, then just chop  
                          * off the \} and resize the buffer.  
                          * If we aren't, then conver it to spaces.  
                          */  
   
                         if ('\0' == *(ep + 1)) {          ep = &(*bufp)[pos];
                                 *--ep = '\0';          while (NULL != (ep = strchr(ep, '\\'))) {
                                 *szp -= 2;                  if ('}' != *(++ep))
                         } else                          continue;
                                 *(ep - 1) = *ep = ' ';  
   
                         roff_ccond(r, ROFF_ccond, bufp, szp,                  /*
                                         ln, pos, pos + 2, offs);                   * If we're at the end of line, then just chop
                         break;                   * off the \} and resize the buffer.
                 }                   * If we aren't, then convert it to spaces.
                 return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);                   */
         }  
   
         /*                  if ('\0' == *(ep + 1)) {
          * A denied conditional must evaluate its children if and only                          *--ep = '\0';
          * if they're either structurally required (such as loops and                          *szp -= 2;
          * conditionals) or a closing macro.                  } else
          */                          *(ep - 1) = *ep = ' ';
   
         if (ROFFRULE_DENY == rr)                  roff_ccond(r, ROFF_ccond, bufp, szp,
                 if ( ! (ROFFMAC_STRUCT & roffs[t].flags))                                  ln, pos, pos + 2, offs);
                         if (ROFF_ccond != t)                  break;
                                 return(ROFF_IGN);          }
           return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
         assert(roffs[t].proc);  
         return((*roffs[t].proc)(r, t, bufp, szp,  
                                 ln, ppos, pos, offs));  
 }  }
   
 /* ARGSUSED */  /* ARGSUSED */
Line 1059  roff_cond_text(ROFF_ARGS)
Line 1131  roff_cond_text(ROFF_ARGS)
         return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);          return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
 }  }
   
   static int
   roff_getnum(const char *v, int *pos, int *res)
   {
           int p, n;
   
           p = *pos;
           n = v[p] == '-';
           if (n)
                   p++;
   
           for (*res = 0; isdigit((unsigned char)v[p]); p++)
                   *res += 10 * *res + v[p] - '0';
           if (p == *pos + n)
                   return 0;
   
           if (n)
                   *res = -*res;
   
           *pos = p;
           return 1;
   }
   
   static int
   roff_getop(const char *v, int *pos, char *res)
   {
           int e;
   
           *res = v[*pos];
           e = v[*pos + 1] == '=';
   
           switch (*res) {
           case '=':
                   break;
           case '>':
                   if (e)
                           *res = 'g';
                   break;
           case '<':
                   if (e)
                           *res = 'l';
                   break;
           default:
                   return(0);
           }
   
           *pos += 1 + e;
   
           return(*res);
   }
   
 static enum roffrule  static enum roffrule
 roff_evalcond(const char *v, int *pos)  roff_evalcond(const char *v, int *pos)
 {  {
           int      not, lh, rh;
           char     op;
   
         switch (v[*pos]) {          switch (v[*pos]) {
         case ('n'):          case ('n'):
Line 1074  roff_evalcond(const char *v, int *pos)
Line 1198  roff_evalcond(const char *v, int *pos)
         case ('t'):          case ('t'):
                 (*pos)++;                  (*pos)++;
                 return(ROFFRULE_DENY);                  return(ROFFRULE_DENY);
           case ('!'):
                   (*pos)++;
                   not = 1;
                   break;
         default:          default:
                   not = 0;
                 break;                  break;
         }          }
   
         while (v[*pos] && ' ' != v[*pos])          if (!roff_getnum(v, pos, &lh))
                 (*pos)++;                  return ROFFRULE_DENY;
         return(ROFFRULE_DENY);          if (!roff_getop(v, pos, &op)) {
                   if (lh < 0)
                           lh = 0;
                   goto out;
           }
           if (!roff_getnum(v, pos, &rh))
                   return ROFFRULE_DENY;
           switch (op) {
           case 'g':
                   lh = lh >= rh;
                   break;
           case 'l':
                   lh = lh <= rh;
                   break;
           case '=':
                   lh = lh == rh;
                   break;
           case '>':
                   lh = lh > rh;
                   break;
           case '<':
                   lh = lh < rh;
                   break;
           default:
                   return ROFFRULE_DENY;
           }
   out:
           if (not)
                   lh = !lh;
           return lh ? ROFFRULE_ALLOW : ROFFRULE_DENY;
 }  }
   
 /* ARGSUSED */  /* ARGSUSED */
Line 1088  static enum rofferr
Line 1246  static enum rofferr
 roff_line_ignore(ROFF_ARGS)  roff_line_ignore(ROFF_ARGS)
 {  {
   
         if (ROFF_it == tok)  
                 mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos, "it");  
   
         return(ROFF_IGN);          return(ROFF_IGN);
 }  }
   
Line 1204  roff_ds(ROFF_ARGS)
Line 1359  roff_ds(ROFF_ARGS)
         return(ROFF_IGN);          return(ROFF_IGN);
 }  }
   
   void
   roff_setreg(struct roff *r, const char *name, int val, char sign)
   {
           struct roffreg  *reg;
   
           /* Search for an existing register with the same name. */
           reg = r->regtab;
   
           while (reg && strcmp(name, reg->key.p))
                   reg = reg->next;
   
           if (NULL == reg) {
                   /* Create a new register. */
                   reg = mandoc_malloc(sizeof(struct roffreg));
                   reg->key.p = mandoc_strdup(name);
                   reg->key.sz = strlen(name);
                   reg->val = 0;
                   reg->next = r->regtab;
                   r->regtab = reg;
           }
   
           if ('+' == sign)
                   reg->val += val;
           else if ('-' == sign)
                   reg->val -= val;
           else
                   reg->val = val;
   }
   
 int  int
 roff_regisset(const struct roff *r, enum regs reg)  roff_getreg(const struct roff *r, const char *name)
 {  {
           struct roffreg  *reg;
   
         return(r->regs[(int)reg].set);          for (reg = r->regtab; reg; reg = reg->next)
                   if (0 == strcmp(name, reg->key.p))
                           return(reg->val);
   
           return(0);
 }  }
   
 unsigned int  static int
 roff_regget(const struct roff *r, enum regs reg)  roff_getregn(const struct roff *r, const char *name, size_t len)
 {  {
           struct roffreg  *reg;
   
         return(r->regs[(int)reg].u);          for (reg = r->regtab; reg; reg = reg->next)
                   if (len == reg->key.sz &&
                       0 == strncmp(name, reg->key.p, len))
                           return(reg->val);
   
           return(0);
 }  }
   
 void  static void
 roff_regunset(struct roff *r, enum regs reg)  roff_freereg(struct roffreg *reg)
 {  {
           struct roffreg  *old_reg;
   
         r->regs[(int)reg].set = 0;          while (NULL != reg) {
                   free(reg->key.p);
                   old_reg = reg;
                   reg = reg->next;
                   free(old_reg);
           }
 }  }
   
 /* ARGSUSED */  /* ARGSUSED */
Line 1231  roff_nr(ROFF_ARGS)
Line 1432  roff_nr(ROFF_ARGS)
 {  {
         const char      *key;          const char      *key;
         char            *val;          char            *val;
           size_t           sz;
         int              iv;          int              iv;
           char             sign;
   
         val = *bufp + pos;          val = *bufp + pos;
         key = roff_getname(r, &val, ln, pos);          key = roff_getname(r, &val, ln, pos);
   
         if (0 == strcmp(key, "nS")) {          sign = *val;
                 r->regs[(int)REG_nS].set = 1;          if ('+' == sign || '-' == sign)
                 if ((iv = mandoc_strntoi(val, strlen(val), 10)) >= 0)                  val++;
                         r->regs[(int)REG_nS].u = (unsigned)iv;  
                 else  
                         r->regs[(int)REG_nS].u = 0u;  
         }  
   
           sz = strspn(val, "0123456789");
           iv = sz ? mandoc_strntoi(val, sz, 10) : 0;
   
           roff_setreg(r, key, iv, sign);
   
         return(ROFF_IGN);          return(ROFF_IGN);
 }  }
   
Line 1265  roff_rm(ROFF_ARGS)
Line 1469  roff_rm(ROFF_ARGS)
   
 /* ARGSUSED */  /* ARGSUSED */
 static enum rofferr  static enum rofferr
   roff_it(ROFF_ARGS)
   {
           char            *cp;
           size_t           len;
           int              iv;
   
           /* Parse the number of lines. */
           cp = *bufp + pos;
           len = strcspn(cp, " \t");
           cp[len] = '\0';
           if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
                   mandoc_msg(MANDOCERR_NUMERIC, r->parse,
                                   ln, ppos, *bufp + 1);
                   return(ROFF_IGN);
           }
           cp += len + 1;
   
           /* Arm the input line trap. */
           roffit_lines = iv;
           roffit_macro = mandoc_strdup(cp);
           return(ROFF_IGN);
   }
   
   /* ARGSUSED */
   static enum rofferr
   roff_Dd(ROFF_ARGS)
   {
           const char *const       *cp;
   
           if (MPARSE_MDOC != r->parsetype)
                   for (cp = __mdoc_reserved; *cp; cp++)
                           roff_setstr(r, *cp, NULL, 0);
   
           return(ROFF_CONT);
   }
   
   /* ARGSUSED */
   static enum rofferr
   roff_TH(ROFF_ARGS)
   {
           const char *const       *cp;
   
           if (MPARSE_MDOC != r->parsetype)
                   for (cp = __man_reserved; *cp; cp++)
                           roff_setstr(r, *cp, NULL, 0);
   
           return(ROFF_CONT);
   }
   
   /* ARGSUSED */
   static enum rofferr
 roff_TE(ROFF_ARGS)  roff_TE(ROFF_ARGS)
 {  {
   
Line 1343  roff_EN(ROFF_ARGS)
Line 1598  roff_EN(ROFF_ARGS)
 static enum rofferr  static enum rofferr
 roff_TS(ROFF_ARGS)  roff_TS(ROFF_ARGS)
 {  {
         struct tbl_node *t;          struct tbl_node *tbl;
   
         if (r->tbl) {          if (r->tbl) {
                 mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);                  mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
                 tbl_end(&r->tbl);                  tbl_end(&r->tbl);
         }          }
   
         t = tbl_alloc(ppos, ln, r->parse);          tbl = tbl_alloc(ppos, ln, r->parse);
   
         if (r->last_tbl)          if (r->last_tbl)
                 r->last_tbl->next = t;                  r->last_tbl->next = tbl;
         else          else
                 r->first_tbl = r->last_tbl = t;                  r->first_tbl = r->last_tbl = tbl;
   
         r->tbl = r->last_tbl = t;          r->tbl = r->last_tbl = tbl;
         return(ROFF_IGN);          return(ROFF_IGN);
 }  }
   
 /* ARGSUSED */  /* ARGSUSED */
 static enum rofferr  static enum rofferr
   roff_cc(ROFF_ARGS)
   {
           const char      *p;
   
           p = *bufp + pos;
   
           if ('\0' == *p || '.' == (r->control = *p++))
                   r->control = 0;
   
           if ('\0' != *p)
                   mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
   
           return(ROFF_IGN);
   }
   
   /* ARGSUSED */
   static enum rofferr
 roff_tr(ROFF_ARGS)  roff_tr(ROFF_ARGS)
 {  {
         const char      *p, *first, *second;          const char      *p, *first, *second;
Line 1756  roff_strdup(const struct roff *r, const char *p)
Line 2028  roff_strdup(const struct roff *r, const char *p)
   
         res[(int)ssz] = '\0';          res[(int)ssz] = '\0';
         return(res);          return(res);
   }
   
   /*
    * Find out whether a line is a macro line or not.
    * If it is, adjust the current position and return one; if it isn't,
    * return zero and don't change the current position.
    * If the control character has been set with `.cc', then let that grain
    * precedence.
    * This is slighly contrary to groff, where using the non-breaking
    * control character when `cc' has been invoked will cause the
    * non-breaking macro contents to be printed verbatim.
    */
   int
   roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
   {
           int             pos;
   
           pos = *ppos;
   
           if (0 != r->control && cp[pos] == r->control)
                   pos++;
           else if (0 != r->control)
                   return(0);
           else if ('\\' == cp[pos] && '.' == cp[pos + 1])
                   pos += 2;
           else if ('.' == cp[pos] || '\'' == cp[pos])
                   pos++;
           else
                   return(0);
   
           while (' ' == cp[pos] || '\t' == cp[pos])
                   pos++;
   
           *ppos = pos;
           return(1);
 }  }

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

CVSweb