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

Diff for /mandoc/roff.c between version 1.277 and 1.294

version 1.277, 2015/10/06 18:32:20 version 1.294, 2017/04/24 23:06:18
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008-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, 2017 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 33 
Line 33 
 #include "roff_int.h"  #include "roff_int.h"
 #include "libroff.h"  #include "libroff.h"
   
 /* Maximum number of nested if-else conditionals. */  
 #define RSTACK_MAX      128  
   
 /* Maximum number of string expansions per line, to break infinite loops. */  /* Maximum number of string expansions per line, to break infinite loops. */
 #define EXPAND_LIMIT    1000  #define EXPAND_LIMIT    1000
   
 /* --- data types --------------------------------------------------------- */  /* --- data types --------------------------------------------------------- */
   
 enum    rofft {  
         ROFF_ab,  
         ROFF_ad,  
         ROFF_af,  
         ROFF_aln,  
         ROFF_als,  
         ROFF_am,  
         ROFF_am1,  
         ROFF_ami,  
         ROFF_ami1,  
         ROFF_as,  
         ROFF_as1,  
         ROFF_asciify,  
         ROFF_backtrace,  
         ROFF_bd,  
         ROFF_bleedat,  
         ROFF_blm,  
         ROFF_box,  
         ROFF_boxa,  
         ROFF_bp,  
         ROFF_BP,  
         /* MAN_br, MDOC_br */  
         ROFF_break,  
         ROFF_breakchar,  
         ROFF_brnl,  
         ROFF_brp,  
         ROFF_brpnl,  
         ROFF_c2,  
         ROFF_cc,  
         ROFF_ce,  
         ROFF_cf,  
         ROFF_cflags,  
         ROFF_ch,  
         ROFF_char,  
         ROFF_chop,  
         ROFF_class,  
         ROFF_close,  
         ROFF_CL,  
         ROFF_color,  
         ROFF_composite,  
         ROFF_continue,  
         ROFF_cp,  
         ROFF_cropat,  
         ROFF_cs,  
         ROFF_cu,  
         ROFF_da,  
         ROFF_dch,  
         ROFF_Dd,  
         ROFF_de,  
         ROFF_de1,  
         ROFF_defcolor,  
         ROFF_dei,  
         ROFF_dei1,  
         ROFF_device,  
         ROFF_devicem,  
         ROFF_di,  
         ROFF_do,  
         ROFF_ds,  
         ROFF_ds1,  
         ROFF_dwh,  
         ROFF_dt,  
         ROFF_ec,  
         ROFF_ecr,  
         ROFF_ecs,  
         ROFF_el,  
         ROFF_em,  
         ROFF_EN,  
         ROFF_eo,  
         ROFF_EP,  
         ROFF_EQ,  
         ROFF_errprint,  
         ROFF_ev,  
         ROFF_evc,  
         ROFF_ex,  
         ROFF_fallback,  
         ROFF_fam,  
         ROFF_fc,  
         ROFF_fchar,  
         ROFF_fcolor,  
         ROFF_fdeferlig,  
         ROFF_feature,  
         /* MAN_fi; ignored in mdoc(7) */  
         ROFF_fkern,  
         ROFF_fl,  
         ROFF_flig,  
         ROFF_fp,  
         ROFF_fps,  
         ROFF_fschar,  
         ROFF_fspacewidth,  
         ROFF_fspecial,  
         /* MAN_ft; ignored in mdoc(7) */  
         ROFF_ftr,  
         ROFF_fzoom,  
         ROFF_gcolor,  
         ROFF_hc,  
         ROFF_hcode,  
         ROFF_hidechar,  
         ROFF_hla,  
         ROFF_hlm,  
         ROFF_hpf,  
         ROFF_hpfa,  
         ROFF_hpfcode,  
         ROFF_hw,  
         ROFF_hy,  
         ROFF_hylang,  
         ROFF_hylen,  
         ROFF_hym,  
         ROFF_hypp,  
         ROFF_hys,  
         ROFF_ie,  
         ROFF_if,  
         ROFF_ig,  
         /* MAN_in; ignored in mdoc(7) */  
         ROFF_index,  
         ROFF_it,  
         ROFF_itc,  
         ROFF_IX,  
         ROFF_kern,  
         ROFF_kernafter,  
         ROFF_kernbefore,  
         ROFF_kernpair,  
         ROFF_lc,  
         ROFF_lc_ctype,  
         ROFF_lds,  
         ROFF_length,  
         ROFF_letadj,  
         ROFF_lf,  
         ROFF_lg,  
         ROFF_lhang,  
         ROFF_linetabs,  
         /* MAN_ll, MDOC_ll */  
         ROFF_lnr,  
         ROFF_lnrf,  
         ROFF_lpfx,  
         ROFF_ls,  
         ROFF_lsm,  
         ROFF_lt,  
         ROFF_mc,  
         ROFF_mediasize,  
         ROFF_minss,  
         ROFF_mk,  
         ROFF_mso,  
         ROFF_na,  
         ROFF_ne,  
         /* MAN_nf; ignored in mdoc(7) */  
         ROFF_nh,  
         ROFF_nhychar,  
         ROFF_nm,  
         ROFF_nn,  
         ROFF_nop,  
         ROFF_nr,  
         ROFF_nrf,  
         ROFF_nroff,  
         ROFF_ns,  
         ROFF_nx,  
         ROFF_open,  
         ROFF_opena,  
         ROFF_os,  
         ROFF_output,  
         ROFF_padj,  
         ROFF_papersize,  
         ROFF_pc,  
         ROFF_pev,  
         ROFF_pi,  
         ROFF_PI,  
         ROFF_pl,  
         ROFF_pm,  
         ROFF_pn,  
         ROFF_pnr,  
         ROFF_po,  
         ROFF_ps,  
         ROFF_psbb,  
         ROFF_pshape,  
         ROFF_pso,  
         ROFF_ptr,  
         ROFF_pvs,  
         ROFF_rchar,  
         ROFF_rd,  
         ROFF_recursionlimit,  
         ROFF_return,  
         ROFF_rfschar,  
         ROFF_rhang,  
         ROFF_rj,  
         ROFF_rm,  
         ROFF_rn,  
         ROFF_rnn,  
         ROFF_rr,  
         ROFF_rs,  
         ROFF_rt,  
         ROFF_schar,  
         ROFF_sentchar,  
         ROFF_shc,  
         ROFF_shift,  
         ROFF_sizes,  
         ROFF_so,  
         /* MAN_sp, MDOC_sp */  
         ROFF_spacewidth,  
         ROFF_special,  
         ROFF_spreadwarn,  
         ROFF_ss,  
         ROFF_sty,  
         ROFF_substring,  
         ROFF_sv,  
         ROFF_sy,  
         ROFF_T_,  
         ROFF_ta,  
         ROFF_tc,  
         ROFF_TE,  
         ROFF_TH,  
         ROFF_ti,  
         ROFF_tkf,  
         ROFF_tl,  
         ROFF_tm,  
         ROFF_tm1,  
         ROFF_tmc,  
         ROFF_tr,  
         ROFF_track,  
         ROFF_transchar,  
         ROFF_trf,  
         ROFF_trimat,  
         ROFF_trin,  
         ROFF_trnt,  
         ROFF_troff,  
         ROFF_TS,  
         ROFF_uf,  
         ROFF_ul,  
         ROFF_unformat,  
         ROFF_unwatch,  
         ROFF_unwatchn,  
         ROFF_vpt,  
         ROFF_vs,  
         ROFF_warn,  
         ROFF_warnscale,  
         ROFF_watch,  
         ROFF_watchlength,  
         ROFF_watchn,  
         ROFF_wh,  
         ROFF_while,  
         ROFF_write,  
         ROFF_writec,  
         ROFF_writem,  
         ROFF_xflag,  
         ROFF_cblock,  
         ROFF_USERDEF,  
         ROFF_MAX  
 };  
   
 /*  /*
  * An incredibly-simple string buffer.   * An incredibly-simple string buffer.
  */   */
Line 316  struct roffreg {
Line 66  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 340  struct roff {
Line 89  struct roff {
 };  };
   
 struct  roffnode {  struct  roffnode {
         enum rofft       tok; /* type of node */          enum roff_tok    tok; /* type of node */
         struct roffnode *parent; /* up one in stack */          struct roffnode *parent; /* up one in stack */
         int              line; /* parse line */          int              line; /* parse line */
         int              col; /* parse col */          int              col; /* parse col */
Line 351  struct roffnode {
Line 100  struct roffnode {
 };  };
   
 #define ROFF_ARGS        struct roff *r, /* parse ctx */ \  #define ROFF_ARGS        struct roff *r, /* parse ctx */ \
                          enum rofft tok, /* tok of macro */ \                           enum roff_tok tok, /* tok of macro */ \
                          struct buf *buf, /* input buffer */ \                           struct buf *buf, /* input buffer */ \
                          int ln, /* parse line */ \                           int ln, /* parse line */ \
                          int ppos, /* original pos in buffer */ \                           int ppos, /* original pos in buffer */ \
Line 380  struct predef {
Line 129  struct predef {
   
 /* --- function prototypes ------------------------------------------------ */  /* --- function prototypes ------------------------------------------------ */
   
 static  enum rofft       roffhash_find(const char *, size_t);  static  enum roff_tok    roffhash_find(const char *, size_t);
 static  void             roffhash_init(void);  static  void             roffhash_init(void);
 static  void             roffnode_cleanscope(struct roff *);  static  void             roffnode_cleanscope(struct roff *);
 static  void             roffnode_pop(struct roff *);  static  void             roffnode_pop(struct roff *);
 static  void             roffnode_push(struct roff *, enum rofft,  static  void             roffnode_push(struct roff *, enum roff_tok,
                                 const char *, int, int);                                  const char *, int, int);
 static  enum rofferr     roff_block(ROFF_ARGS);  static  enum rofferr     roff_block(ROFF_ARGS);
 static  enum rofferr     roff_block_text(ROFF_ARGS);  static  enum rofferr     roff_block_text(ROFF_ARGS);
Line 424  static enum rofferr  roff_line_ignore(ROFF_ARGS);
Line 173  static enum rofferr  roff_line_ignore(ROFF_ARGS);
 static  void             roff_man_alloc1(struct roff_man *);  static  void             roff_man_alloc1(struct roff_man *);
 static  void             roff_man_free1(struct roff_man *);  static  void             roff_man_free1(struct roff_man *);
 static  enum rofferr     roff_nr(ROFF_ARGS);  static  enum rofferr     roff_nr(ROFF_ARGS);
 static  enum rofft       roff_parse(struct roff *, char *, int *,  static  enum roff_tok    roff_parse(struct roff *, char *, int *,
                                 int, int);                                  int, int);
 static  enum rofferr     roff_parsetext(struct buf *, int, int *);  static  enum rofferr     roff_parsetext(struct buf *, int, int *);
 static  enum rofferr     roff_res(struct roff *, struct buf *, int, int);  static  enum rofferr     roff_res(struct roff *, struct buf *, int, int);
Line 457  static enum rofferr  roff_userdef(ROFF_ARGS);
Line 206  static enum rofferr  roff_userdef(ROFF_ARGS);
 #define ROFFNUM_SCALE   (1 << 0)  /* Honour scaling in roff_getnum(). */  #define ROFFNUM_SCALE   (1 << 0)  /* Honour scaling in roff_getnum(). */
 #define ROFFNUM_WHITE   (1 << 1)  /* Skip whitespace in roff_evalnum(). */  #define ROFFNUM_WHITE   (1 << 1)  /* Skip whitespace in roff_evalnum(). */
   
   const char *__roff_name[MAN_MAX + 1] = {
           "ab",           "ad",           "af",           "aln",
           "als",          "am",           "am1",          "ami",
           "ami1",         "as",           "as1",          "asciify",
           "backtrace",    "bd",           "bleedat",      "blm",
           "box",          "boxa",         "bp",           "BP",
           "break",        "breakchar",    "brnl",         "brp",
           "brpnl",        "c2",           "cc",           "ce",
           "cf",           "cflags",       "ch",           "char",
           "chop",         "class",        "close",        "CL",
           "color",        "composite",    "continue",     "cp",
           "cropat",       "cs",           "cu",           "da",
           "dch",          "Dd",           "de",           "de1",
           "defcolor",     "dei",          "dei1",         "device",
           "devicem",      "di",           "do",           "ds",
           "ds1",          "dwh",          "dt",           "ec",
           "ecr",          "ecs",          "el",           "em",
           "EN",           "eo",           "EP",           "EQ",
           "errprint",     "ev",           "evc",          "ex",
           "fallback",     "fam",          "fc",           "fchar",
           "fcolor",       "fdeferlig",    "feature",      "fkern",
           "fl",           "flig",         "fp",           "fps",
           "fschar",       "fspacewidth",  "fspecial",     "ftr",
           "fzoom",        "gcolor",       "hc",           "hcode",
           "hidechar",     "hla",          "hlm",          "hpf",
           "hpfa",         "hpfcode",      "hw",           "hy",
           "hylang",       "hylen",        "hym",          "hypp",
           "hys",          "ie",           "if",           "ig",
           "index",        "it",           "itc",          "IX",
           "kern",         "kernafter",    "kernbefore",   "kernpair",
           "lc",           "lc_ctype",     "lds",          "length",
           "letadj",       "lf",           "lg",           "lhang",
           "linetabs",     "lnr",          "lnrf",         "lpfx",
           "ls",           "lsm",          "lt",           "mc",
           "mediasize",    "minss",        "mk",           "mso",
           "na",           "ne",           "nh",           "nhychar",
           "nm",           "nn",           "nop",          "nr",
           "nrf",          "nroff",        "ns",           "nx",
           "open",         "opena",        "os",           "output",
           "padj",         "papersize",    "pc",           "pev",
           "pi",           "PI",           "pl",           "pm",
           "pn",           "pnr",          "po",           "ps",
           "psbb",         "pshape",       "pso",          "ptr",
           "pvs",          "rchar",        "rd",           "recursionlimit",
           "return",       "rfschar",      "rhang",        "rj",
           "rm",           "rn",           "rnn",          "rr",
           "rs",           "rt",           "schar",        "sentchar",
           "shc",          "shift",        "sizes",        "so",
           "spacewidth",   "special",      "spreadwarn",   "ss",
           "sty",          "substring",    "sv",           "sy",
           "T&",           "ta",           "tc",           "TE",
           "TH",           "ti",           "tkf",          "tl",
           "tm",           "tm1",          "tmc",          "tr",
           "track",        "transchar",    "trf",          "trimat",
           "trin",         "trnt",         "troff",        "TS",
           "uf",           "ul",           "unformat",     "unwatch",
           "unwatchn",     "vpt",          "vs",           "warn",
           "warnscale",    "watch",        "watchlength",  "watchn",
           "wh",           "while",        "write",        "writec",
           "writem",       "xflag",        ".",            NULL,
           "text",
           "Dd",           "Dt",           "Os",           "Sh",
           "Ss",           "Pp",           "D1",           "Dl",
           "Bd",           "Ed",           "Bl",           "El",
           "It",           "Ad",           "An",           "Ap",
           "Ar",           "Cd",           "Cm",           "Dv",
           "Er",           "Ev",           "Ex",           "Fa",
           "Fd",           "Fl",           "Fn",           "Ft",
           "Ic",           "In",           "Li",           "Nd",
           "Nm",           "Op",           "Ot",           "Pa",
           "Rv",           "St",           "Va",           "Vt",
           "Xr",           "%A",           "%B",           "%D",
           "%I",           "%J",           "%N",           "%O",
           "%P",           "%R",           "%T",           "%V",
           "Ac",           "Ao",           "Aq",           "At",
           "Bc",           "Bf",           "Bo",           "Bq",
           "Bsx",          "Bx",           "Db",           "Dc",
           "Do",           "Dq",           "Ec",           "Ef",
           "Em",           "Eo",           "Fx",           "Ms",
           "No",           "Ns",           "Nx",           "Ox",
           "Pc",           "Pf",           "Po",           "Pq",
           "Qc",           "Ql",           "Qo",           "Qq",
           "Re",           "Rs",           "Sc",           "So",
           "Sq",           "Sm",           "Sx",           "Sy",
           "Tn",           "Ux",           "Xc",           "Xo",
           "Fo",           "Fc",           "Oo",           "Oc",
           "Bk",           "Ek",           "Bt",           "Hf",
           "Fr",           "Ud",           "Lb",           "Lp",
           "Lk",           "Mt",           "Brq",          "Bro",
           "Brc",          "%C",           "Es",           "En",
           "Dx",           "%Q",           "br",           "sp",
           "%U",           "Ta",           "ll",           NULL,
           "TH",           "SH",           "SS",           "TP",
           "LP",           "PP",           "P",            "IP",
           "HP",           "SM",           "SB",           "BI",
           "IB",           "BR",           "RB",           "R",
           "B",            "I",            "IR",           "RI",
           "br",           "sp",           "nf",           "fi",
           "RE",           "RS",           "DT",           "UC",
           "PD",           "AT",           "in",           "ft",
           "OP",           "EX",           "EE",           "UR",
           "UE",           "ll",           NULL
   };
   const   char *const *roff_name = __roff_name;
   
 static  struct roffmac  *hash[HASHWIDTH];  static  struct roffmac  *hash[HASHWIDTH];
   
 static  struct roffmac   roffs[ROFF_MAX] = {  static  struct roffmac   roffs[TOKEN_NONE] = {
         { "ab", roff_unsupp, NULL, NULL, 0, NULL },          { "ab", roff_unsupp, NULL, NULL, 0, NULL },
         { "ad", roff_line_ignore, NULL, NULL, 0, NULL },          { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
         { "af", roff_line_ignore, NULL, NULL, 0, NULL },          { "af", roff_line_ignore, NULL, NULL, 0, NULL },
Line 768  roffhash_init(void)
Line 622  roffhash_init(void)
 }  }
   
 /*  /*
  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by   * Look up a roff token by its name.  Returns TOKEN_NONE if no macro by
  * the nil-terminated string name could be found.   * the nil-terminated string name could be found.
  */   */
 static enum rofft  static enum roff_tok
 roffhash_find(const char *p, size_t s)  roffhash_find(const char *p, size_t s)
 {  {
         int              buc;          int              buc;
Line 785  roffhash_find(const char *p, size_t s)
Line 639  roffhash_find(const char *p, size_t s)
          */           */
   
         if (p[0] < ASCII_LO || p[0] > ASCII_HI)          if (p[0] < ASCII_LO || p[0] > ASCII_HI)
                 return ROFF_MAX;                  return TOKEN_NONE;
   
         buc = ROFF_HASH(p);          buc = ROFF_HASH(p);
   
         if (NULL == (n = hash[buc]))          if (NULL == (n = hash[buc]))
                 return ROFF_MAX;                  return TOKEN_NONE;
         for ( ; n; n = n->next)          for ( ; n; n = n->next)
                 if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])                  if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
                         return (enum rofft)(n - roffs);                          return (enum roff_tok)(n - roffs);
   
         return ROFF_MAX;          return TOKEN_NONE;
 }  }
   
 /* --- stack of request blocks -------------------------------------------- */  /* --- stack of request blocks -------------------------------------------- */
Line 823  roffnode_pop(struct roff *r)
Line 677  roffnode_pop(struct roff *r)
  * removed with roffnode_pop().   * removed with roffnode_pop().
  */   */
 static void  static void
 roffnode_push(struct roff *r, enum rofft tok, const char *name,  roffnode_push(struct roff *r, enum roff_tok tok, const char *name,
                 int line, int col)                  int line, int col)
 {  {
         struct roffnode *p;          struct roffnode *p;
Line 901  roff_free(struct roff *r)
Line 755  roff_free(struct roff *r)
 }  }
   
 struct roff *  struct roff *
 roff_alloc(struct mparse *parse, const struct mchars *mchars, int options)  roff_alloc(struct mparse *parse, 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->format = options & (MPARSE_MDOC | MPARSE_MAN);
         r->rstackpos = -1;          r->rstackpos = -1;
Line 996  roff_node_alloc(struct roff_man *man, int line, int po
Line 849  roff_node_alloc(struct roff_man *man, int line, int po
         n->sec = man->lastsec;          n->sec = man->lastsec;
   
         if (man->flags & MDOC_SYNOPSIS)          if (man->flags & MDOC_SYNOPSIS)
                 n->flags |= MDOC_SYNPRETTY;                  n->flags |= NODE_SYNPRETTY;
         else          else
                 n->flags &= ~MDOC_SYNPRETTY;                  n->flags &= ~NODE_SYNPRETTY;
         if (man->flags & MDOC_NEWLINE)          if (man->flags & MDOC_NEWLINE)
                 n->flags |= MDOC_LINE;                  n->flags |= NODE_LINE;
         man->flags &= ~MDOC_NEWLINE;          man->flags &= ~MDOC_NEWLINE;
   
         return n;          return n;
Line 1012  roff_node_append(struct roff_man *man, struct roff_nod
Line 865  roff_node_append(struct roff_man *man, struct roff_nod
   
         switch (man->next) {          switch (man->next) {
         case ROFF_NEXT_SIBLING:          case ROFF_NEXT_SIBLING:
                   if (man->last->next != NULL) {
                           n->next = man->last->next;
                           man->last->next->prev = n;
                   } else
                           man->last->parent->last = n;
                 man->last->next = n;                  man->last->next = n;
                 n->prev = man->last;                  n->prev = man->last;
                 n->parent = man->last->parent;                  n->parent = man->last->parent;
                 break;                  break;
         case ROFF_NEXT_CHILD:          case ROFF_NEXT_CHILD:
                   if (man->last->child != NULL) {
                           n->next = man->last->child;
                           man->last->child->prev = n;
                   } else
                           man->last->last = n;
                 man->last->child = n;                  man->last->child = n;
                 n->parent = man->last;                  n->parent = man->last;
                 break;                  break;
         default:          default:
                 abort();                  abort();
         }          }
         n->parent->nchild++;          man->last = n;
         n->parent->last = n;  
   
         /*  
          * Copy over the normalised-data pointer of our parent.  Not  
          * everybody has one, but copying a null pointer is fine.  
          */  
   
         switch (n->type) {          switch (n->type) {
         case ROFFT_BODY:  
                 if (n->end != ENDBODY_NOT)  
                         break;  
                 /* FALLTHROUGH */  
         case ROFFT_TAIL:  
                 /* FALLTHROUGH */  
         case ROFFT_HEAD:          case ROFFT_HEAD:
                 n->norm = n->parent->norm;  
                 break;  
         default:  
                 break;  
         }  
   
         if (man->macroset == MACROSET_MDOC)  
                 mdoc_valid_pre(man, n);  
   
         switch (n->type) {  
         case ROFFT_HEAD:  
                 assert(n->parent->type == ROFFT_BLOCK);  
                 n->parent->head = n;                  n->parent->head = n;
                 break;                  break;
         case ROFFT_BODY:          case ROFFT_BODY:
                 if (n->end)                  if (n->end != ENDBODY_NOT)
                         break;                          return;
                 assert(n->parent->type == ROFFT_BLOCK);  
                 n->parent->body = n;                  n->parent->body = n;
                 break;                  break;
         case ROFFT_TAIL:          case ROFFT_TAIL:
                 assert(n->parent->type == ROFFT_BLOCK);  
                 n->parent->tail = n;                  n->parent->tail = n;
                 break;                  break;
         default:          default:
                 break;                  return;
         }          }
         man->last = n;  
           /*
            * Copy over the normalised-data pointer of our parent.  Not
            * everybody has one, but copying a null pointer is fine.
            */
   
           n->norm = n->parent->norm;
           assert(n->parent->type == ROFFT_BLOCK);
 }  }
   
 void  void
Line 1077  roff_word_alloc(struct roff_man *man, int line, int po
Line 921  roff_word_alloc(struct roff_man *man, int line, int po
         n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);          n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
         n->string = roff_strdup(man->roff, word);          n->string = roff_strdup(man->roff, word);
         roff_node_append(man, n);          roff_node_append(man, n);
         if (man->macroset == MACROSET_MDOC)          n->flags |= NODE_VALID | NODE_ENDED;
                 mdoc_valid_post(man);  
         else  
                 man_valid_post(man);  
         man->next = ROFF_NEXT_SIBLING;          man->next = ROFF_NEXT_SIBLING;
 }  }
   
Line 1150  roff_addeqn(struct roff_man *man, const struct eqn *eq
Line 991  roff_addeqn(struct roff_man *man, const struct eqn *eq
         n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE);          n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE);
         n->eqn = eqn;          n->eqn = eqn;
         if (eqn->ln > man->last->line)          if (eqn->ln > man->last->line)
                 n->flags |= MDOC_LINE;                  n->flags |= NODE_LINE;
         roff_node_append(man, n);          roff_node_append(man, n);
         man->next = ROFF_NEXT_SIBLING;          man->next = ROFF_NEXT_SIBLING;
 }  }
Line 1165  roff_addtbl(struct roff_man *man, const struct tbl_spa
Line 1006  roff_addtbl(struct roff_man *man, const struct tbl_spa
         n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);          n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);
         n->span = tbl;          n->span = tbl;
         roff_node_append(man, n);          roff_node_append(man, n);
         if (man->macroset == MACROSET_MDOC)          n->flags |= NODE_VALID | NODE_ENDED;
                 mdoc_valid_post(man);  
         else  
                 man_valid_post(man);  
         man->next = ROFF_NEXT_SIBLING;          man->next = ROFF_NEXT_SIBLING;
 }  }
   
Line 1186  roff_node_unlink(struct roff_man *man, struct roff_nod
Line 1024  roff_node_unlink(struct roff_man *man, struct roff_nod
         /* Adjust parent. */          /* Adjust parent. */
   
         if (n->parent != NULL) {          if (n->parent != NULL) {
                 n->parent->nchild--;  
                 if (n->parent->child == n)                  if (n->parent->child == n)
                         n->parent->child = n->next;                          n->parent->child = n->next;
                 if (n->parent->last == n)                  if (n->parent->last == n)
Line 1228  roff_node_delete(struct roff_man *man, struct roff_nod
Line 1065  roff_node_delete(struct roff_man *man, struct roff_nod
   
         while (n->child != NULL)          while (n->child != NULL)
                 roff_node_delete(man, n->child);                  roff_node_delete(man, n->child);
         assert(n->nchild == 0);  
         roff_node_unlink(man, n);          roff_node_unlink(man, n);
         roff_node_free(n);          roff_node_free(n);
 }  }
Line 1245  deroff(char **dest, const struct roff_node *n)
Line 1081  deroff(char **dest, const struct roff_node *n)
                 return;                  return;
         }          }
   
         /* Skip leading whitespace and escape sequences. */          /* Skip leading whitespace. */
   
         cp = n->string;          for (cp = n->string; *cp != '\0'; cp++) {
         while (*cp != '\0') {                  if (cp[0] == '\\' && cp[1] != '\0' &&
                 if ('\\' == *cp) {                      strchr(" %&0^|~", cp[1]) != NULL)
                         cp++;                          cp++;
                         mandoc_escape((const char **)&cp, NULL, NULL);                  else if ( ! isspace((unsigned char)*cp))
                 } else if (isspace((unsigned char)*cp))  
                         cp++;  
                 else  
                         break;                          break;
         }          }
   
           /* Skip trailing backslash. */
   
           sz = strlen(cp);
           if (sz > 0 && cp[sz - 1] == '\\')
                   sz--;
   
         /* Skip trailing whitespace. */          /* Skip trailing whitespace. */
   
         for (sz = strlen(cp); sz; sz--)          for (; sz; sz--)
                 if ( ! isspace((unsigned char)cp[sz-1]))                  if ( ! isspace((unsigned char)cp[sz-1]))
                         break;                          break;
   
Line 1335  roff_res(struct roff *r, struct buf *buf, int ln, int 
Line 1174  roff_res(struct roff *r, struct buf *buf, int ln, int 
                         res = NULL;                          res = NULL;
                         break;                          break;
                 case 'B':                  case 'B':
                         /* FALLTHROUGH */  
                 case 'w':                  case 'w':
                         term = cp[1];                          term = cp[1];
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
Line 1346  roff_res(struct roff *r, struct buf *buf, int ln, int 
Line 1184  roff_res(struct roff *r, struct buf *buf, int ln, int 
                         esc = mandoc_escape(&cp, &stnam, &inaml);                          esc = mandoc_escape(&cp, &stnam, &inaml);
                         if (esc == ESCAPE_ERROR ||                          if (esc == ESCAPE_ERROR ||
                             (esc == ESCAPE_SPECIAL &&                              (esc == ESCAPE_SPECIAL &&
                              mchars_spec2cp(r->mchars, stnam, inaml) < 0))                               mchars_spec2cp(stnam, inaml) < 0))
                                 mandoc_vmsg(MANDOCERR_ESC_BAD,                                  mandoc_vmsg(MANDOCERR_ESC_BAD,
                                     r->parse, ln, (int)(stesc - buf->buf),                                      r->parse, ln, (int)(stesc - buf->buf),
                                     "%.*s", (int)(cp - stesc), stesc);                                      "%.*s", (int)(cp - stesc), stesc);
Line 1410  roff_res(struct roff *r, struct buf *buf, int ln, int 
Line 1248  roff_res(struct roff *r, struct buf *buf, int ln, int 
                         }                          }
                         switch (mandoc_escape(&cp, NULL, NULL)) {                          switch (mandoc_escape(&cp, NULL, NULL)) {
                         case ESCAPE_SPECIAL:                          case ESCAPE_SPECIAL:
                                 /* FALLTHROUGH */  
                         case ESCAPE_UNICODE:                          case ESCAPE_UNICODE:
                                 /* FALLTHROUGH */  
                         case ESCAPE_NUMBERED:                          case ESCAPE_NUMBERED:
                                 /* FALLTHROUGH */  
                         case ESCAPE_OVERSTRIKE:                          case ESCAPE_OVERSTRIKE:
                                 naml++;                                  naml++;
                                 break;                                  break;
Line 1544  roff_parsetext(struct buf *buf, int pos, int *offs)
Line 1379  roff_parsetext(struct buf *buf, int pos, int *offs)
 enum rofferr  enum rofferr
 roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)  roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
 {  {
         enum rofft       t;          enum roff_tok    t;
         enum rofferr     e;          enum rofferr     e;
         int              pos;   /* parse point */          int              pos;   /* parse point */
         int              spos;  /* saved parse point for messages */          int              spos;  /* saved parse point for messages */
Line 1583  roff_parseln(struct roff *r, int ln, struct buf *buf, 
Line 1418  roff_parseln(struct roff *r, int ln, struct buf *buf, 
   
         if (r->last != NULL && ! ctl) {          if (r->last != NULL && ! ctl) {
                 t = r->last->tok;                  t = r->last->tok;
                 assert(roffs[t].text);  
                 e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);                  e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
                 assert(e == ROFF_IGN || e == ROFF_CONT);                  if (e == ROFF_IGN)
                 if (e != ROFF_CONT)  
                         return e;                          return e;
                   assert(e == ROFF_CONT);
         }          }
         if (r->eqn != NULL)          if (r->eqn != NULL)
                 return eqn_read(&r->eqn, ln, buf->buf, ppos, offs);                  return eqn_read(&r->eqn, ln, buf->buf, ppos, offs);
Line 1613  roff_parseln(struct roff *r, int ln, struct buf *buf, 
Line 1447  roff_parseln(struct roff *r, int ln, struct buf *buf, 
   
         if (r->last) {          if (r->last) {
                 t = r->last->tok;                  t = r->last->tok;
                 assert(roffs[t].sub);  
                 return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);                  return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
         }          }
   
Line 1624  roff_parseln(struct roff *r, int ln, struct buf *buf, 
Line 1457  roff_parseln(struct roff *r, int ln, struct buf *buf, 
   
         /* Tables ignore most macros. */          /* Tables ignore most macros. */
   
         if (r->tbl != NULL && (t == ROFF_MAX || t == ROFF_TS)) {          if (r->tbl != NULL && (t == TOKEN_NONE || t == ROFF_TS)) {
                 mandoc_msg(MANDOCERR_TBLMACRO, r->parse,                  mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
                     ln, pos, buf->buf + spos);                      ln, pos, buf->buf + spos);
                 if (t == ROFF_TS)                  if (t == ROFF_TS)
                         return ROFF_IGN;                          return ROFF_IGN;
                 while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')                  while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
                         pos++;                          pos++;
                 while (buf->buf[pos] != '\0' && buf->buf[pos] == ' ')                  while (buf->buf[pos] == ' ')
                         pos++;                          pos++;
                 return tbl_read(r->tbl, ln, buf->buf, pos);                  return tbl_read(r->tbl, ln, buf->buf, pos);
         }          }
Line 1641  roff_parseln(struct roff *r, int ln, struct buf *buf, 
Line 1474  roff_parseln(struct roff *r, int ln, struct buf *buf, 
          * Let the standard macro set parsers handle it.           * Let the standard macro set parsers handle it.
          */           */
   
         if (t == ROFF_MAX)          if (t == TOKEN_NONE)
                 return ROFF_CONT;                  return ROFF_CONT;
   
         /* Execute a roff request or a user defined macro. */          /* Execute a roff request or a user defined macro. */
   
         assert(roffs[t].proc);  
         return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);          return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
 }  }
   
Line 1676  roff_endparse(struct roff *r)
Line 1508  roff_endparse(struct roff *r)
  * Parse a roff node's type from the input buffer.  This must be in the   * Parse a roff node's type from the input buffer.  This must be in the
  * form of ".foo xxx" in the usual way.   * form of ".foo xxx" in the usual way.
  */   */
 static enum rofft  static enum roff_tok
 roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)  roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
 {  {
         char            *cp;          char            *cp;
         const char      *mac;          const char      *mac;
         size_t           maclen;          size_t           maclen;
         enum rofft       t;          enum roff_tok    t;
   
         cp = buf + *pos;          cp = buf + *pos;
   
         if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)          if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
                 return ROFF_MAX;                  return TOKEN_NONE;
   
         mac = cp;          mac = cp;
         maclen = roff_getname(r, &cp, ln, ppos);          maclen = roff_getname(r, &cp, ln, ppos);
Line 1695  roff_parse(struct roff *r, char *buf, int *pos, int ln
Line 1527  roff_parse(struct roff *r, char *buf, int *pos, int ln
         t = (r->current_string = roff_getstrn(r, mac, maclen))          t = (r->current_string = roff_getstrn(r, mac, maclen))
             ? ROFF_USERDEF : roffhash_find(mac, maclen);              ? ROFF_USERDEF : roffhash_find(mac, maclen);
   
         if (ROFF_MAX != t)          if (t != TOKEN_NONE)
                 *pos = cp - buf;                  *pos = cp - buf;
   
         return t;          return t;
Line 1721  roff_cblock(ROFF_ARGS)
Line 1553  roff_cblock(ROFF_ARGS)
         switch (r->last->tok) {          switch (r->last->tok) {
         case ROFF_am:          case ROFF_am:
                 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */                  /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
                 /* FALLTHROUGH */  
         case ROFF_ami:          case ROFF_ami:
                 /* FALLTHROUGH */  
         case ROFF_de:          case ROFF_de:
                 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */                  /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
                 /* FALLTHROUGH */  
         case ROFF_dei:          case ROFF_dei:
                 /* FALLTHROUGH */  
         case ROFF_ig:          case ROFF_ig:
                 break;                  break;
         default:          default:
Line 1770  roff_ccond(struct roff *r, int ln, int ppos)
Line 1598  roff_ccond(struct roff *r, int ln, int ppos)
   
         switch (r->last->tok) {          switch (r->last->tok) {
         case ROFF_el:          case ROFF_el:
                 /* FALLTHROUGH */  
         case ROFF_ie:          case ROFF_ie:
                 /* FALLTHROUGH */  
         case ROFF_if:          case ROFF_if:
                 break;                  break;
         default:          default:
Line 1886  roff_block(ROFF_ARGS)
Line 1712  roff_block(ROFF_ARGS)
 static enum rofferr  static enum rofferr
 roff_block_sub(ROFF_ARGS)  roff_block_sub(ROFF_ARGS)
 {  {
         enum rofft      t;          enum roff_tok   t;
         int             i, j;          int             i, j;
   
         /*          /*
Line 1915  roff_block_sub(ROFF_ARGS)
Line 1741  roff_block_sub(ROFF_ARGS)
   
                         pos = i;                          pos = i;
                         if (roff_parse(r, buf->buf, &pos, ln, ppos) !=                          if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
                             ROFF_MAX)                              TOKEN_NONE)
                                 return ROFF_RERUN;                                  return ROFF_RERUN;
                         return ROFF_IGN;                          return ROFF_IGN;
                 }                  }
Line 1934  roff_block_sub(ROFF_ARGS)
Line 1760  roff_block_sub(ROFF_ARGS)
                 return ROFF_IGN;                  return ROFF_IGN;
         }          }
   
         assert(roffs[t].proc);  
         return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);          return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
 }  }
   
Line 1951  roff_block_text(ROFF_ARGS)
Line 1776  roff_block_text(ROFF_ARGS)
 static enum rofferr  static enum rofferr
 roff_cond_sub(ROFF_ARGS)  roff_cond_sub(ROFF_ARGS)
 {  {
         enum rofft       t;          enum roff_tok    t;
         char            *ep;          char            *ep;
         int              rr;          int              rr;
   
Line 1964  roff_cond_sub(ROFF_ARGS)
Line 1789  roff_cond_sub(ROFF_ARGS)
          * required or when the conditional evaluated to true.           * required or when the conditional evaluated to true.
          */           */
   
         if ((t != ROFF_MAX) &&          if (t != TOKEN_NONE && (rr || roffs[t].flags & ROFFMAC_STRUCT))
             (rr || roffs[t].flags & ROFFMAC_STRUCT)) {  
                 assert(roffs[t].proc);  
                 return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);                  return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
         }  
   
         /*          /*
          * If `\}' occurs on a macro line without a preceding macro,           * If `\}' occurs on a macro line without a preceding macro,
Line 2059  roff_getnum(const char *v, int *pos, int *res, int fla
Line 1881  roff_getnum(const char *v, int *pos, int *res, int fla
                 scaled = *res * 240 / 2.54;                  scaled = *res * 240 / 2.54;
                 break;                  break;
         case 'v':          case 'v':
                 /* FALLTHROUGH */  
         case 'P':          case 'P':
                 scaled = *res * 40;                  scaled = *res * 40;
                 break;                  break;
         case 'm':          case 'm':
                 /* FALLTHROUGH */  
         case 'n':          case 'n':
                 scaled = *res * 24;                  scaled = *res * 24;
                 break;                  break;
Line 2153  roff_evalcond(struct roff *r, int ln, char *v, int *po
Line 1973  roff_evalcond(struct roff *r, int ln, char *v, int *po
         case '\0':          case '\0':
                 return 0;                  return 0;
         case 'n':          case 'n':
                 /* FALLTHROUGH */  
         case 'o':          case 'o':
                 (*pos)++;                  (*pos)++;
                 return wanttrue;                  return wanttrue;
         case 'c':          case 'c':
                 /* FALLTHROUGH */  
         case 'd':          case 'd':
                 /* FALLTHROUGH */  
         case 'e':          case 'e':
                 /* FALLTHROUGH */  
         case 't':          case 't':
                 /* FALLTHROUGH */  
         case 'v':          case 'v':
                 (*pos)++;                  (*pos)++;
                 return !wanttrue;                  return !wanttrue;
Line 2340  roff_getop(const char *v, int *pos, char *res)
Line 2155  roff_getop(const char *v, int *pos, char *res)
   
         switch (*res) {          switch (*res) {
         case '+':          case '+':
                 /* FALLTHROUGH */  
         case '-':          case '-':
                 /* FALLTHROUGH */  
         case '*':          case '*':
                 /* FALLTHROUGH */  
         case '/':          case '/':
                 /* FALLTHROUGH */  
         case '%':          case '%':
                 /* FALLTHROUGH */  
         case '&':          case '&':
                 /* FALLTHROUGH */  
         case ':':          case ':':
                 break;                  break;
         case '<':          case '<':
Line 3080  roff_userdef(ROFF_ARGS)
Line 2889  roff_userdef(ROFF_ARGS)
 {  {
         const char       *arg[9], *ap;          const char       *arg[9], *ap;
         char             *cp, *n1, *n2;          char             *cp, *n1, *n2;
         int               i, ib, ie;          int               expand_count, i, ib, ie;
         size_t            asz, rsz;          size_t            asz, rsz;
   
         /*          /*
Line 3104  roff_userdef(ROFF_ARGS)
Line 2913  roff_userdef(ROFF_ARGS)
          */           */
   
         buf->sz = strlen(r->current_string) + 1;          buf->sz = strlen(r->current_string) + 1;
         n1 = cp = mandoc_malloc(buf->sz);          n1 = n2 = cp = mandoc_malloc(buf->sz);
         memcpy(n1, r->current_string, buf->sz);          memcpy(n1, r->current_string, buf->sz);
           expand_count = 0;
         while (*cp != '\0') {          while (*cp != '\0') {
   
                 /* Scan ahead for the next argument invocation. */                  /* Scan ahead for the next argument invocation. */
Line 3125  roff_userdef(ROFF_ARGS)
Line 2935  roff_userdef(ROFF_ARGS)
                 cp -= 2;                  cp -= 2;
   
                 /*                  /*
                    * Prevent infinite recursion.
                    */
   
                   if (cp >= n2)
                           expand_count = 1;
                   else if (++expand_count > EXPAND_LIMIT) {
                           mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
                               ln, (int)(cp - n1), NULL);
                           free(buf->buf);
                           buf->buf = n1;
                           return ROFF_IGN;
                   }
   
                   /*
                  * Determine the size of the expanded argument,                   * Determine the size of the expanded argument,
                  * taking escaping of quotes into account.                   * taking escaping of quotes into account.
                  */                   */
Line 3407  roff_strdup(const struct roff *r, const char *p)
Line 3231  roff_strdup(const struct roff *r, const char *p)
         ssz = 0;          ssz = 0;
   
         while ('\0' != *p) {          while ('\0' != *p) {
                 if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {                  assert((unsigned int)*p < 128);
                   if ('\\' != *p && r->xtab && r->xtab[(unsigned int)*p].p) {
                         sz = r->xtab[(int)*p].sz;                          sz = r->xtab[(int)*p].sz;
                         res = mandoc_realloc(res, ssz + sz + 1);                          res = mandoc_realloc(res, ssz + sz + 1);
                         memcpy(res + ssz, r->xtab[(int)*p].p, sz);                          memcpy(res + ssz, r->xtab[(int)*p].p, sz);

Legend:
Removed from v.1.277  
changed lines
  Added in v.1.294

CVSweb