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

Diff for /mandoc/man.c between version 1.53 and 1.158

version 1.53, 2010/03/23 21:50:43 version 1.158, 2015/04/19 13:50:25
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
    * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.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
  * copyright notice and this permission notice appear in all copies.   * copyright notice and this permission notice appear in all copies.
  *   *
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * 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 <sys/types.h>
   
Line 27 
Line 27 
 #include <stdio.h>  #include <stdio.h>
 #include <string.h>  #include <string.h>
   
 #include "libman.h"  #include "mandoc_aux.h"
   #include "mandoc.h"
   #include "roff.h"
   #include "man.h"
 #include "libmandoc.h"  #include "libmandoc.h"
   #include "roff_int.h"
   #include "libman.h"
   
 const   char *const __man_merrnames[WERRMAX] = {  const   char *const __man_macronames[MAN_MAX] = {
         "invalid character", /* WNPRINT */  
         "invalid manual section", /* WMSEC */  
         "invalid date format", /* WDATE */  
         "scope of prior line violated", /* WLNSCOPE */  
         "over-zealous prior line scope violation", /* WLNSCOPE2 */  
         "trailing whitespace", /* WTSPACE */  
         "unterminated quoted parameter", /* WTQUOTE */  
         "document has no body", /* WNODATA */  
         "document has no title/section", /* WNOTITLE */  
         "invalid escape sequence", /* WESCAPE */  
         "invalid number format", /* WNUMFMT */  
         "expected block head arguments", /* WHEADARGS */  
         "expected block body arguments", /* WBODYARGS */  
         "expected empty block head", /* WNHEADARGS */  
         "ill-formed macro", /* WMACROFORM */  
         "scope open on exit", /* WEXITSCOPE */  
         "no scope context", /* WNOSCOPE */  
         "literal context already open", /* WOLITERAL */  
         "no literal context open" /* WNLITERAL */  
 };  
   
 const   char *const __man_macronames[MAN_MAX] = {  
         "br",           "TH",           "SH",           "SS",          "br",           "TH",           "SH",           "SS",
         "TP",           "LP",           "PP",           "P",          "TP",           "LP",           "PP",           "P",
         "IP",           "HP",           "SM",           "SB",          "IP",           "HP",           "SM",           "SB",
         "BI",           "IB",           "BR",           "RB",          "BI",           "IB",           "BR",           "RB",
         "R",            "B",            "I",            "IR",          "R",            "B",            "I",            "IR",
         "RI",           "na",           "i",            "sp",          "RI",           "sp",           "nf",
         "nf",           "fi",           "r",            "RE",          "fi",           "RE",           "RS",           "DT",
         "RS",           "DT",           "UC",           "PD",          "UC",           "PD",           "AT",           "in",
         "Sp",           "Vb",           "Ve",          "ft",           "OP",           "EX",           "EE",
           "UR",           "UE",           "ll"
         };          };
   
 const   char * const *man_macronames = __man_macronames;  const   char * const *man_macronames = __man_macronames;
   
 static  struct man_node *man_node_alloc(int, int,  static  void             man_breakscope(struct roff_man *, int);
                                 enum man_type, enum mant);  static  void             man_descope(struct roff_man *, int, int);
 static  int              man_node_append(struct man *,  static  int              man_ptext(struct roff_man *, int, char *, int);
                                 struct man_node *);  static  int              man_pmacro(struct roff_man *, int, char *, int);
 static  int              man_ptext(struct man *, int, char *);  
 static  int              man_pmacro(struct man *, int, char *);  
 static  void             man_free1(struct man *);  
 static  void             man_alloc1(struct man *);  
 static  int              pstring(struct man *, int, int,  
                                 const char *, size_t);  
 static  int              macrowarn(struct man *, int, const char *);  
   
   
 const struct man_node *  void
 man_node(const struct man *m)  man_endparse(struct roff_man *man)
 {  {
   
         return(MAN_HALT & m->flags ? NULL : m->first);          man_macroend(man);
 }  }
   
   int
 const struct man_meta *  man_parseln(struct roff_man *man, int ln, char *buf, int offs)
 man_meta(const struct man *m)  
 {  {
   
         return(MAN_HALT & m->flags ? NULL : &m->meta);          if (man->last->type != ROFFT_EQN || ln > man->last->line)
                   man->flags |= MAN_NEWLINE;
   
           return (roff_getcontrol(man->roff, buf, &offs) ?
               man_pmacro(man, ln, buf, offs) :
               man_ptext(man, ln, buf, offs));
 }  }
   
   
 void  void
 man_reset(struct man *man)  man_elem_alloc(struct roff_man *man, int line, int pos, int tok)
 {  {
           struct roff_node *p;
   
         man_free1(man);          p = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
         man_alloc1(man);          roff_node_append(man, p);
           man->next = ROFF_NEXT_CHILD;
 }  }
   
   
 void  void
 man_free(struct man *man)  man_block_alloc(struct roff_man *man, int line, int pos, int tok)
 {  {
           struct roff_node *p;
   
         man_free1(man);          p = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
         free(man);          roff_node_append(man, p);
           man->next = ROFF_NEXT_CHILD;
 }  }
   
   void
 struct man *  man_word_alloc(struct roff_man *man, int line, int pos, const char *word)
 man_alloc(void *data, int pflags, const struct man_cb *cb)  
 {  {
         struct man      *p;          struct roff_node *n;
   
         p = mandoc_calloc(1, sizeof(struct man));          n = roff_node_alloc(man, line, pos, ROFFT_TEXT, MAN_MAX);
           n->string = roff_strdup(man->roff, word);
         if (cb)          roff_node_append(man, n);
                 memcpy(&p->cb, cb, sizeof(struct man_cb));          man_valid_post(man);
           man->next = ROFF_NEXT_SIBLING;
         man_hash_init();  
         p->data = data;  
         p->pflags = pflags;  
   
         man_alloc1(p);  
         return(p);  
 }  }
   
   void
 int  man_word_append(struct roff_man *man, const char *word)
 man_endparse(struct man *m)  
 {  {
           struct roff_node *n;
           char            *addstr, *newstr;
   
         if (MAN_HALT & m->flags)          n = man->last;
                 return(0);          addstr = roff_strdup(man->roff, word);
         else if (man_macroend(m))          mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
                 return(1);          free(addstr);
         m->flags |= MAN_HALT;          free(n->string);
         return(0);          n->string = newstr;
           man->next = ROFF_NEXT_SIBLING;
 }  }
   
   void
 int  man_addeqn(struct roff_man *man, const struct eqn *ep)
 man_parseln(struct man *m, int ln, char *buf)  
 {  {
           struct roff_node *n;
   
         return('.' == *buf ?          n = roff_node_alloc(man, ep->ln, ep->pos, ROFFT_EQN, MAN_MAX);
                         man_pmacro(m, ln, buf) :          n->eqn = ep;
                         man_ptext(m, ln, buf));          if (ep->ln > man->last->line)
                   n->flags |= MAN_LINE;
           roff_node_append(man, n);
           man->next = ROFF_NEXT_SIBLING;
           man_descope(man, ep->ln, ep->pos);
 }  }
   
   void
 static void  man_addspan(struct roff_man *man, const struct tbl_span *sp)
 man_free1(struct man *man)  
 {  {
           struct roff_node *n;
   
         if (man->first)          man_breakscope(man, MAN_MAX);
                 man_node_freelist(man->first);          n = roff_node_alloc(man, sp->line, 0, ROFFT_TBL, MAN_MAX);
         if (man->meta.title)          n->span = sp;
                 free(man->meta.title);          roff_node_append(man, n);
         if (man->meta.source)          man_valid_post(man);
                 free(man->meta.source);          man->next = ROFF_NEXT_SIBLING;
         if (man->meta.vol)          man_descope(man, sp->line, 0);
                 free(man->meta.vol);  
 }  }
   
   
 static void  static void
 man_alloc1(struct man *m)  man_descope(struct roff_man *man, int line, int offs)
 {  {
           /*
            * Co-ordinate what happens with having a next-line scope open:
            * first close out the element scope (if applicable), then close
            * out the block scope (also if applicable).
            */
   
         memset(&m->meta, 0, sizeof(struct man_meta));          if (man->flags & MAN_ELINE) {
         m->flags = 0;                  man->flags &= ~MAN_ELINE;
         m->last = mandoc_calloc(1, sizeof(struct man_node));                  man_unscope(man, man->last->parent);
         m->first = m->last;          }
         m->last->type = MAN_ROOT;          if ( ! (man->flags & MAN_BLINE))
         m->next = MAN_NEXT_CHILD;                  return;
           man->flags &= ~MAN_BLINE;
           man_unscope(man, man->last->parent);
           roff_body_alloc(man, line, offs, man->last->tok);
 }  }
   
   
 static int  static int
 man_node_append(struct man *man, struct man_node *p)  man_ptext(struct roff_man *man, int line, char *buf, int offs)
 {  {
           int              i;
   
         assert(man->last);          /* Literal free-form text whitespace is preserved. */
         assert(man->first);  
         assert(MAN_ROOT != p->type);  
   
         switch (man->next) {          if (man->flags & MAN_LITERAL) {
         case (MAN_NEXT_SIBLING):                  man_word_alloc(man, line, offs, buf + offs);
                 man->last->next = p;                  man_descope(man, line, offs);
                 p->prev = man->last;                  return(1);
                 p->parent = man->last->parent;  
                 break;  
         case (MAN_NEXT_CHILD):  
                 man->last->child = p;  
                 p->parent = man->last;  
                 break;  
         default:  
                 abort();  
                 /* NOTREACHED */  
         }          }
   
         p->parent->nchild++;  
   
         if ( ! man_valid_pre(man, p))          for (i = offs; buf[i] == ' '; i++)
                 return(0);                  /* Skip leading whitespace. */ ;
   
         switch (p->type) {          /*
         case (MAN_HEAD):           * Blank lines are ignored right after headings
                 assert(MAN_BLOCK == p->parent->type);           * but add a single vertical space elsewhere.
                 p->parent->head = p;           */
                 break;  
         case (MAN_BODY):  
                 assert(MAN_BLOCK == p->parent->type);  
                 p->parent->body = p;  
                 break;  
         default:  
                 break;  
         }  
   
         man->last = p;          if (buf[i] == '\0') {
                   /* Allocate a blank entry. */
         switch (p->type) {                  if (man->last->tok != MAN_SH &&
         case (MAN_TEXT):                      man->last->tok != MAN_SS) {
                 if ( ! man_valid_post(man))                          man_elem_alloc(man, line, offs, MAN_sp);
                         return(0);                          man->next = ROFF_NEXT_SIBLING;
                 if ( ! man_action_post(man))                  }
                         return(0);                  return(1);
                 break;  
         default:  
                 break;  
         }          }
   
         return(1);          /*
 }           * Warn if the last un-escaped character is whitespace. Then
            * strip away the remaining spaces (tabs stay!).
            */
   
           i = (int)strlen(buf);
           assert(i);
   
 static struct man_node *          if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
 man_node_alloc(int line, int pos, enum man_type type, enum mant tok)                  if (i > 1 && '\\' != buf[i - 2])
 {                          mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
         struct man_node *p;                              line, i - 1, NULL);
   
         p = mandoc_calloc(1, sizeof(struct man_node));                  for (--i; i && ' ' == buf[i]; i--)
         p->line = line;                          /* Spin back to non-space. */ ;
         p->pos = pos;  
         p->type = type;  
         p->tok = tok;  
         return(p);  
 }  
   
                   /* Jump ahead of escaped whitespace. */
                   i += '\\' == buf[i] ? 2 : 1;
   
 int                  buf[i] = '\0';
 man_elem_alloc(struct man *m, int line, int pos, enum mant tok)          }
 {          man_word_alloc(man, line, offs, buf + offs);
         struct man_node *p;  
   
         p = man_node_alloc(line, pos, MAN_ELEM, tok);          /*
         if ( ! man_node_append(m, p))           * End-of-sentence check.  If the last character is an unescaped
                 return(0);           * EOS character, then flag the node as being the end of a
         m->next = MAN_NEXT_CHILD;           * sentence.  The front-end will know how to interpret this.
         return(1);           */
 }  
   
           assert(i);
           if (mandoc_eos(buf, (size_t)i))
                   man->last->flags |= MAN_EOS;
   
 int          man_descope(man, line, offs);
 man_head_alloc(struct man *m, int line, int pos, enum mant tok)  
 {  
         struct man_node *p;  
   
         p = man_node_alloc(line, pos, MAN_HEAD, tok);  
         if ( ! man_node_append(m, p))  
                 return(0);  
         m->next = MAN_NEXT_CHILD;  
         return(1);          return(1);
 }  }
   
   
 int  
 man_body_alloc(struct man *m, int line, int pos, enum mant tok)  
 {  
         struct man_node *p;  
   
         p = man_node_alloc(line, pos, MAN_BODY, tok);  
         if ( ! man_node_append(m, p))  
                 return(0);  
         m->next = MAN_NEXT_CHILD;  
         return(1);  
 }  
   
   
 int  
 man_block_alloc(struct man *m, int line, int pos, enum mant tok)  
 {  
         struct man_node *p;  
   
         p = man_node_alloc(line, pos, MAN_BLOCK, tok);  
         if ( ! man_node_append(m, p))  
                 return(0);  
         m->next = MAN_NEXT_CHILD;  
         return(1);  
 }  
   
   
 static int  static int
 pstring(struct man *m, int line, int pos,  man_pmacro(struct roff_man *man, int ln, char *buf, int offs)
                 const char *p, size_t len)  
 {  {
         struct man_node *n;          struct roff_node *n;
         size_t           sv;          const char      *cp;
           int              tok;
           int              i, ppos;
           int              bline;
           char             mac[5];
   
         n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);          ppos = offs;
         n->string = mandoc_malloc(len + 1);  
         sv = strlcpy(n->string, p, len + 1);  
   
         /* Prohibit truncation. */          /*
         assert(sv < len + 1);           * Copy the first word into a nil-terminated buffer.
            * Stop when a space, tab, escape, or eoln is encountered.
            */
   
         if ( ! man_node_append(m, n))          i = 0;
                 return(0);          while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
         m->next = MAN_NEXT_SIBLING;                  mac[i++] = buf[offs++];
         return(1);  
 }  
   
           mac[i] = '\0';
   
 int          tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
 man_word_alloc(struct man *m, int line, int pos, const char *word)  
 {  
   
         return(pstring(m, line, pos, word, strlen(word)));          if (tok == MAN_MAX) {
 }                  mandoc_msg(MANDOCERR_MACRO, man->parse,
                       ln, ppos, buf + ppos - 1);
                   return(1);
 void  
 man_node_free(struct man_node *p)  
 {  
   
         if (p->string)  
                 free(p->string);  
         if (p->parent)  
                 p->parent->nchild--;  
         free(p);  
 }  
   
   
 void  
 man_node_freelist(struct man_node *p)  
 {  
         struct man_node *n;  
   
         if (p->child)  
                 man_node_freelist(p->child);  
         assert(0 == p->nchild);  
         n = p->next;  
         man_node_free(p);  
         if (n)  
                 man_node_freelist(n);  
 }  
   
   
 static int  
 man_ptext(struct man *m, int line, char *buf)  
 {  
         int              i, j;  
         char             sv;  
   
         /* Literal free-form text whitespace is preserved. */  
   
         if (MAN_LITERAL & m->flags) {  
                 if ( ! man_word_alloc(m, line, 0, buf))  
                         return(0);  
                 goto descope;  
         }          }
   
         /* First de-chunk and allocate words. */          /* Skip a leading escape sequence or tab. */
   
         for (i = 0; ' ' == buf[i]; i++)          switch (buf[offs]) {
                 /* Skip leading whitespace. */ ;          case '\\':
                   cp = buf + offs + 1;
         if ('\0' == buf[i]) {                  mandoc_escape(&cp, NULL, NULL);
                 /* Trailing whitespace? */                  offs = cp - buf;
                 if (i && ' ' == buf[i - 1])                  break;
                         if ( ! man_pwarn(m, line, i - 1, WTSPACE))          case '\t':
                                 return(0);                  offs++;
                 if ( ! pstring(m, line, 0, &buf[i], 0))                  break;
                         return(0);          default:
                 goto descope;                  break;
         }          }
   
         for (j = i; buf[i]; i++) {          /* Jump to the next non-whitespace word. */
                 if (' ' != buf[i])  
                         continue;  
   
                 /* Escaped whitespace. */          while (buf[offs] && buf[offs] == ' ')
                 if (i && ' ' == buf[i] && '\\' == buf[i - 1])                  offs++;
                         continue;  
   
                 sv = buf[i];          /*
                 buf[i++] = '\0';           * Trailing whitespace.  Note that tabs are allowed to be passed
            * into the parser as "text", so we only warn about spaces here.
            */
   
                 if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))          if (buf[offs] == '\0' && buf[offs - 1] == ' ')
                         return(0);                  mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
                       ln, offs - 1, NULL);
   
                 /* Trailing whitespace?  Check at overwritten byte. */          /*
            * Some macros break next-line scopes; otherwise, remember
            * whether we are in next-line scope for a block head.
            */
   
                 if (' ' == sv && '\0' == buf[i])          man_breakscope(man, tok);
                         if ( ! man_pwarn(m, line, i - 1, WTSPACE))          bline = man->flags & MAN_BLINE;
                                 return(0);  
   
                 for ( ; ' ' == buf[i]; i++)          /* Call to handler... */
                         /* Skip trailing whitespace. */ ;  
   
                 j = i;          assert(man_macros[tok].fp);
           (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf);
   
                 /* Trailing whitespace? */          /* In quick mode (for mandocdb), abort after the NAME section. */
   
                 if (' ' == buf[i - 1] && '\0' == buf[i])          if (man->quick && tok == MAN_SH) {
                         if ( ! man_pwarn(m, line, i - 1, WTSPACE))                  n = man->last;
                                 return(0);                  if (n->type == ROFFT_BODY &&
                       strcmp(n->prev->child->string, "NAME"))
                 if ('\0' == buf[i])                          return(2);
                         break;  
         }          }
   
         if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))  
                 return(0);  
   
 descope:  
   
         /*          /*
          * Co-ordinate what happens with having a next-line scope open:           * If we are in a next-line scope for a block head,
          * first close out the element scope (if applicable), then close           * close it out now and switch to the body,
          * out the block scope (also if applicable).           * unless the next-line scope is allowed to continue.
          */           */
   
         if (MAN_ELINE & m->flags) {          if ( ! bline || man->flags & MAN_ELINE ||
                 m->flags &= ~MAN_ELINE;              man_macros[tok].flags & MAN_NSCOPED)
                 if ( ! man_unscope(m, m->last->parent))  
                         return(0);  
         }  
   
         if ( ! (MAN_BLINE & m->flags))  
                 return(1);                  return(1);
         m->flags &= ~MAN_BLINE;  
   
         if ( ! man_unscope(m, m->last->parent))          assert(man->flags & MAN_BLINE);
                 return(0);          man->flags &= ~MAN_BLINE;
         return(man_body_alloc(m, line, 0, m->last->tok));  
 }  
   
           man_unscope(man, man->last->parent);
 static int          roff_body_alloc(man, ln, ppos, man->last->tok);
 macrowarn(struct man *m, int ln, const char *buf)          return(1);
 {  
         if ( ! (MAN_IGN_MACRO & m->pflags))  
                 return(man_verr(m, ln, 0,  
                                 "unknown macro: %s%s",  
                                 buf, strlen(buf) > 3 ? "..." : ""));  
         return(man_vwarn(m, ln, 0, "unknown macro: %s%s",  
                                 buf, strlen(buf) > 3 ? "..." : ""));  
 }  }
   
   void
 int  man_breakscope(struct roff_man *man, int tok)
 man_pmacro(struct man *m, int ln, char *buf)  
 {  {
         int              i, j, ppos, fl;          struct roff_node *n;
         enum mant        tok;  
         char             mac[5];  
         struct man_node *n;  
   
         /* Comments and empties are quickly ignored. */          /*
            * An element next line scope is open,
         fl = m->flags;           * and the new macro is not allowed inside elements.
            * Delete the element that is being broken.
         if ('\0' == buf[1])  
                 return(1);  
   
         i = 1;  
   
         if (' ' == buf[i]) {  
                 i++;  
                 while (buf[i] && ' ' == buf[i])  
                         i++;  
                 if ('\0' == buf[i])  
                         goto out;  
         }  
   
         ppos = i;  
   
         /* Copy the first word into a nil-terminated buffer. */  
   
         for (j = 0; j < 4; j++, i++) {  
                 if ('\0' == (mac[j] = buf[i]))  
                         break;  
                 else if (' ' == buf[i])  
                         break;  
   
                 /* Check for invalid characters. */  
   
                 if (isgraph((u_char)buf[i]))  
                         continue;  
                 return(man_perr(m, ln, i, WNPRINT));  
         }  
   
         mac[j] = '\0';  
   
         if (j == 4 || j < 1) {  
                 if ( ! (MAN_IGN_MACRO & m->pflags)) {  
                         (void)man_perr(m, ln, ppos, WMACROFORM);  
                         goto err;  
                 }  
                 if ( ! man_pwarn(m, ln, ppos, WMACROFORM))  
                         goto err;  
                 return(1);  
         }  
   
         if (MAN_MAX == (tok = man_hash_find(mac))) {  
                 if ( ! macrowarn(m, ln, mac))  
                         goto err;  
                 return(1);  
         }  
   
         /* The macro is sane.  Jump to the next word. */  
   
         while (buf[i] && ' ' == buf[i])  
                 i++;  
   
         /* Trailing whitespace? */  
   
         if ('\0' == buf[i] && ' ' == buf[i - 1])  
                 if ( ! man_pwarn(m, ln, i - 1, WTSPACE))  
                         goto err;  
   
         /*  
          * Remove prior ELINE macro, as it's being clobbering by a new  
          * macro.  Note that NSCOPED macros do not close out ELINE  
          * macros---they don't print text---so we let those slip by.  
          */           */
   
         if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&          if (man->flags & MAN_ELINE && (tok == MAN_MAX ||
                         m->flags & MAN_ELINE) {              ! (man_macros[tok].flags & MAN_NSCOPED))) {
                 assert(MAN_TEXT != m->last->type);                  n = man->last;
                   assert(n->type != ROFFT_TEXT);
                   if (man_macros[n->tok].flags & MAN_NSCOPED)
                           n = n->parent;
   
                 /*                  mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
                  * This occurs in the following construction:                      n->line, n->pos, "%s breaks %s",
                  *   .B                      tok == MAN_MAX ? "TS" : man_macronames[tok],
                  *   .br                      man_macronames[n->tok]);
                  *   .B  
                  *   .br  
                  *   I hate man macros.  
                  * Flat-out disallow this madness.  
                  */  
                 if (MAN_NSCOPED & man_macros[m->last->tok].flags)  
                         return(man_perr(m, ln, ppos, WLNSCOPE));  
   
                 n = m->last;                  roff_node_delete(man, n);
                   man->flags &= ~MAN_ELINE;
                 assert(n);  
                 assert(NULL == n->child);  
                 assert(0 == n->nchild);  
   
                 if ( ! man_nwarn(m, n, WLNSCOPE))  
                         return(0);  
   
                 man_node_unlink(m, n);  
                 man_node_free(n);  
                 m->flags &= ~MAN_ELINE;  
         }          }
   
         /* Begin recursive parse sequence. */  
   
         assert(man_macros[tok].fp);  
   
         if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))  
                 goto err;  
   
 out:  
         /*  
          * We weren't in a block-line scope when entering the  
          * above-parsed macro, so return.  
          */  
   
         if ( ! (MAN_BLINE & fl)) {  
                 m->flags &= ~MAN_ILINE;  
                 return(1);  
         }  
   
         /*          /*
          * If we're in a block scope, then allow this macro to slip by           * A block header next line scope is open,
          * without closing scope around it.           * and the new macro is not allowed inside block headers.
            * Delete the block that is being broken.
          */           */
   
         if (MAN_ILINE & m->flags) {          if (man->flags & MAN_BLINE && (tok == MAN_MAX ||
                 m->flags &= ~MAN_ILINE;              man_macros[tok].flags & MAN_BSCOPE)) {
                 return(1);                  n = man->last;
         }                  if (n->type == ROFFT_TEXT)
                           n = n->parent;
                   if ( ! (man_macros[n->tok].flags & MAN_BSCOPE))
                           n = n->parent;
   
         /*                  assert(n->type == ROFFT_HEAD);
          * If we've opened a new next-line element scope, then return                  n = n->parent;
          * now, as the next line will close out the block scope.                  assert(n->type == ROFFT_BLOCK);
          */                  assert(man_macros[n->tok].flags & MAN_SCOPED);
   
         if (MAN_ELINE & m->flags)                  mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
                 return(1);                      n->line, n->pos, "%s breaks %s",
                       tok == MAN_MAX ? "TS" : man_macronames[tok],
                       man_macronames[n->tok]);
   
         /* Close out the block scope opened in the prior line.  */                  roff_node_delete(man, n);
                   man->flags &= ~MAN_BLINE;
         assert(MAN_BLINE & m->flags);          }
         m->flags &= ~MAN_BLINE;  
   
         if ( ! man_unscope(m, m->last->parent))  
                 return(0);  
         return(man_body_alloc(m, ln, 0, m->last->tok));  
   
 err:    /* Error out. */  
   
         m->flags |= MAN_HALT;  
         return(0);  
 }  }
   
   const struct mparse *
 int  man_mparse(const struct roff_man *man)
 man_verr(struct man *man, int ln, int pos, const char *fmt, ...)  
 {  {
         char             buf[256];  
         va_list          ap;  
   
         if (NULL == man->cb.man_err)          assert(man && man->parse);
                 return(0);          return(man->parse);
   
         va_start(ap, fmt);  
         (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);  
         va_end(ap);  
         return((*man->cb.man_err)(man->data, ln, pos, buf));  
 }  }
   
   void
 int  man_deroff(char **dest, const struct roff_node *n)
 man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)  
 {  {
         char             buf[256];          char    *cp;
         va_list          ap;          size_t   sz;
   
         if (NULL == man->cb.man_warn)          if (n->type != ROFFT_TEXT) {
                 return(0);                  for (n = n->child; n; n = n->next)
                           man_deroff(dest, n);
                   return;
           }
   
         va_start(ap, fmt);          /* Skip leading whitespace and escape sequences. */
         (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);  
         va_end(ap);  
         return((*man->cb.man_warn)(man->data, ln, pos, buf));  
 }  
   
           cp = n->string;
           while ('\0' != *cp) {
                   if ('\\' == *cp) {
                           cp++;
                           mandoc_escape((const char **)&cp, NULL, NULL);
                   } else if (isspace((unsigned char)*cp))
                           cp++;
                   else
                           break;
           }
   
 int          /* Skip trailing whitespace. */
 man_err(struct man *m, int line, int pos, int iserr, enum merr type)  
 {  
         const char       *p;  
   
         p = __man_merrnames[(int)type];  
         assert(p);  
   
         if (iserr)          for (sz = strlen(cp); sz; sz--)
                 return(man_verr(m, line, pos, p));                  if (0 == isspace((unsigned char)cp[sz-1]))
                           break;
   
         return(man_vwarn(m, line, pos, p));          /* Skip empty strings. */
 }  
   
           if (0 == sz)
                   return;
   
 void          if (NULL == *dest) {
 man_node_unlink(struct man *m, struct man_node *n)                  *dest = mandoc_strndup(cp, sz);
 {                  return;
   
         if (n->prev) {  
                 n->prev->next = n->next;  
                 if (m->last == n) {  
                         assert(NULL == n->next);  
                         m->last = n->prev;  
                         m->next = MAN_NEXT_SIBLING;  
                 }  
         } else {  
                 n->parent->child = n->next;  
                 if (m->last == n) {  
                         assert(NULL == n->next);  
                         m->last = n->parent;  
                         m->next = MAN_NEXT_CHILD;  
                 }  
         }          }
   
         if (n->next)          mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                 n->next->prev = n->prev;          free(*dest);
           *dest = cp;
 }  }

Legend:
Removed from v.1.53  
changed lines
  Added in v.1.158

CVSweb