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

Diff for /docbook2mdoc/parse.c between version 1.6 and 1.14

version 1.6, 2019/03/28 15:05:40 version 1.14, 2019/04/05 14:37:36
Line 30 
Line 30 
  * The implementation of the DocBook parser.   * The implementation of the DocBook parser.
  */   */
   
   enum    pstate {
           PARSE_ELEM,
           PARSE_TAG,
           PARSE_ARG,
           PARSE_SQ,
           PARSE_DQ
   };
   
 /*  /*
  * Global parse state.   * Global parse state.
  * Keep this as simple and small as possible.   * Keep this as simple and small as possible.
Line 69  static const struct element elements[] = {
Line 77  static const struct element elements[] = {
         { "citerefentry",       NODE_CITEREFENTRY },          { "citerefentry",       NODE_CITEREFENTRY },
         { "citetitle",          NODE_CITETITLE },          { "citetitle",          NODE_CITETITLE },
         { "cmdsynopsis",        NODE_CMDSYNOPSIS },          { "cmdsynopsis",        NODE_CMDSYNOPSIS },
         { "code",               NODE_CODE },          { "code",               NODE_LITERAL },
         { "colspec",            NODE_COLSPEC },          { "colspec",            NODE_COLSPEC },
         { "command",            NODE_COMMAND },          { "command",            NODE_COMMAND },
         { "constant",           NODE_CONSTANT },          { "constant",           NODE_CONSTANT },
           { "contrib",            NODE_CONTRIB },
         { "copyright",          NODE_COPYRIGHT },          { "copyright",          NODE_COPYRIGHT },
         { "date",               NODE_DATE },          { "date",               NODE_DATE },
         { "editor",             NODE_EDITOR },          { "editor",             NODE_EDITOR },
Line 80  static const struct element elements[] = {
Line 89  static const struct element elements[] = {
         { "emphasis",           NODE_EMPHASIS },          { "emphasis",           NODE_EMPHASIS },
         { "entry",              NODE_ENTRY },          { "entry",              NODE_ENTRY },
         { "envar",              NODE_ENVAR },          { "envar",              NODE_ENVAR },
           { "errorname",          NODE_ERRORNAME },
         { "fieldsynopsis",      NODE_FIELDSYNOPSIS },          { "fieldsynopsis",      NODE_FIELDSYNOPSIS },
         { "filename",           NODE_FILENAME },          { "filename",           NODE_FILENAME },
         { "firstname",          NODE_IGNORE },          { "firstname",          NODE_PERSONNAME },
         { "firstterm",          NODE_FIRSTTERM },          { "firstterm",          NODE_FIRSTTERM },
         { "footnote",           NODE_FOOTNOTE },          { "footnote",           NODE_FOOTNOTE },
         { "funcdef",            NODE_FUNCDEF },          { "funcdef",            NODE_FUNCDEF },
Line 97  static const struct element elements[] = {
Line 107  static const struct element elements[] = {
         { "indexterm",          NODE_DELETE },          { "indexterm",          NODE_DELETE },
         { "info",               NODE_INFO },          { "info",               NODE_INFO },
         { "informalequation",   NODE_INFORMALEQUATION },          { "informalequation",   NODE_INFORMALEQUATION },
         { "informaltable",      NODE_INFORMALTABLE },          { "informaltable",      NODE_TABLE },
         { "inlineequation",     NODE_INLINEEQUATION },          { "inlineequation",     NODE_INLINEEQUATION },
         { "itemizedlist",       NODE_ITEMIZEDLIST },          { "itemizedlist",       NODE_ITEMIZEDLIST },
         { "keysym",             NODE_KEYSYM },          { "keysym",             NODE_KEYSYM },
Line 122  static const struct element elements[] = {
Line 132  static const struct element elements[] = {
         { "option",             NODE_OPTION },          { "option",             NODE_OPTION },
         { "orderedlist",        NODE_ORDEREDLIST },          { "orderedlist",        NODE_ORDEREDLIST },
         { "orgname",            NODE_ORGNAME },          { "orgname",            NODE_ORGNAME },
         { "othername",          NODE_IGNORE },          { "othername",          NODE_PERSONNAME },
         { "para",               NODE_PARA },          { "para",               NODE_PARA },
         { "paramdef",           NODE_PARAMDEF },          { "paramdef",           NODE_PARAMDEF },
         { "parameter",          NODE_PARAMETER },          { "parameter",          NODE_PARAMETER },
Line 162  static const struct element elements[] = {
Line 172  static const struct element elements[] = {
         { "sgmltag",            NODE_SGMLTAG },          { "sgmltag",            NODE_SGMLTAG },
         { "simplelist",         NODE_SIMPLELIST },          { "simplelist",         NODE_SIMPLELIST },
         { "spanspec",           NODE_SPANSPEC },          { "spanspec",           NODE_SPANSPEC },
         { "structname",         NODE_STRUCTNAME },          { "structfield",        NODE_PARAMETER },
           { "structname",         NODE_TYPE },
         { "subtitle",           NODE_SUBTITLE },          { "subtitle",           NODE_SUBTITLE },
         { "surname",            NODE_IGNORE },          { "surname",            NODE_PERSONNAME },
           { "symbol",             NODE_CONSTANT },
         { "synopsis",           NODE_SYNOPSIS },          { "synopsis",           NODE_SYNOPSIS },
         { "table",              NODE_TABLE },          { "table",              NODE_TABLE },
         { "tbody",              NODE_TBODY },          { "tbody",              NODE_TBODY },
Line 177  static const struct element elements[] = {
Line 189  static const struct element elements[] = {
         { "trademark",          NODE_IGNORE },          { "trademark",          NODE_IGNORE },
         { "type",               NODE_TYPE },          { "type",               NODE_TYPE },
         { "ulink",              NODE_ULINK },          { "ulink",              NODE_ULINK },
         { "userinput",          NODE_USERINPUT },          { "userinput",          NODE_LITERAL },
         { "variablelist",       NODE_VARIABLELIST },          { "variablelist",       NODE_VARIABLELIST },
         { "varlistentry",       NODE_VARLISTENTRY },          { "varlistentry",       NODE_VARLISTENTRY },
         { "varname",            NODE_VARNAME },          { "varname",            NODE_VARNAME },
Line 188  static const struct element elements[] = {
Line 200  static const struct element elements[] = {
         { NULL,                 NODE_IGNORE }          { NULL,                 NODE_IGNORE }
 };  };
   
   struct  entity {
           const char      *name;
           const char      *roff;
   };
   
   /*
    * XML character entity references found in the wild.
    * Those that don't have an exact mandoc_char(7) representation
    * are approximated, and the desired codepoint is given as a comment.
    * Encoding them as \\[u...] would leave -Tascii out in the cold.
    */
   static  const struct entity entities[] = {
           { "alpha",      "\\(*a" },
           { "amp",        "&" },
           { "apos",       "'" },
           { "auml",       "\\(:a" },
           { "beta",       "\\(*b" },
           { "circ",       "^" },      /* U+02C6 */
           { "copy",       "\\(co" },
           { "dagger",     "\\(dg" },
           { "Delta",      "\\(*D" },
           { "eacute",     "\\('e" },
           { "emsp",       "\\ " },    /* U+2003 */
           { "gt",         ">" },
           { "hairsp",     "\\^" },
           { "kappa",      "\\(*k" },
           { "larr",       "\\(<-" },
           { "ldquo",      "\\(lq" },
           { "le",         "\\(<=" },
           { "lowbar",     "_" },
           { "lsqb",       "[" },
           { "lt",         "<" },
           { "mdash",      "\\(em" },
           { "minus",      "\\-" },
           { "ndash",      "\\(en" },
           { "nbsp",       "\\ " },
           { "num",        "#" },
           { "oslash",     "\\(/o" },
           { "ouml",       "\\(:o" },
           { "percnt",     "%" },
           { "quot",       "\\(dq" },
           { "rarr",       "\\(->" },
           { "rArr",       "\\(rA" },
           { "rdquo",      "\\(rq" },
           { "reg",        "\\(rg" },
           { "rho",        "\\(*r" },
           { "rsqb",       "]" },
           { "sigma",      "\\(*s" },
           { "shy",        "\\&" },     /* U+00AD */
           { "tau",        "\\(*t" },
           { "tilde",      "\\[u02DC]" },
           { "times",      "\\[tmu]" },
           { "uuml",       "\\(:u" },
           { NULL,         NULL }
   };
   
 static void  static void
 error_msg(struct parse *p, const char *fmt, ...)  error_msg(struct parse *p, const char *fmt, ...)
 {  {
Line 274  pnode_trim(struct pnode *pn)
Line 342  pnode_trim(struct pnode *pn)
                         break;                          break;
 }  }
   
   static void
   xml_entity(struct parse *p, const char *name)
   {
           const struct entity     *entity;
           struct pnode            *dat;
   
           if (p->del > 0)
                   return;
   
           if (p->cur == NULL) {
                   error_msg(p, "discarding entity before document: &%s;", name);
                   return;
           }
   
           /* Close out the text node, if there is one. */
           if (p->cur->node == NODE_TEXT) {
                   pnode_trim(p->cur);
                   p->cur = p->cur->parent;
           }
   
           if (p->tree->flags & TREE_CLOSED && p->cur == p->tree->root)
                   warn_msg(p, "entity after end of document: &%s;", name);
   
           for (entity = entities; entity->name != NULL; entity++)
                   if (strcmp(name, entity->name) == 0)
                           break;
   
           if (entity->roff == NULL) {
                   error_msg(p, "unknown entity &%s;", name);
                   return;
           }
   
           /* Create, append, and close out an entity node. */
           if ((dat = calloc(1, sizeof(*dat))) == NULL ||
               (dat->b = dat->real = strdup(entity->roff)) == NULL) {
                   perror(NULL);
                   exit(1);
           }
           dat->node = NODE_ESCAPE;
           dat->bsz = strlen(dat->b);
           dat->parent = p->cur;
           TAILQ_INIT(&dat->childq);
           TAILQ_INIT(&dat->attrq);
           TAILQ_INSERT_TAIL(&p->cur->childq, dat, child);
   }
   
 /*  /*
  * Begin an element.   * Begin an element.
  */   */
Line 476  parse_free(struct parse *p)
Line 590  parse_free(struct parse *p)
         free(p);          free(p);
 }  }
   
   static void
   increment(struct parse *p, char *b, size_t *pend, int refill)
   {
           if (refill) {
                   if (b[*pend] == '\n') {
                           p->nline++;
                           p->ncol = 1;
                   } else
                           p->ncol++;
           }
           ++*pend;
   }
   
 /*  /*
  * Advance the pend pointer to the next character in the charset.   * Advance the pend pointer to the next character in the charset.
  * If the charset starts with a space, it stands for any whitespace.   * If the charset starts with a space, it stands for any whitespace.
Line 486  parse_free(struct parse *p)
Line 613  parse_free(struct parse *p)
  */   */
 static int  static int
 advance(struct parse *p, char *b, size_t rlen, size_t *pend,  advance(struct parse *p, char *b, size_t rlen, size_t *pend,
     const char *charset)      const char *charset, int refill)
 {  {
         int              space;          int              space;
   
Line 496  advance(struct parse *p, char *b, size_t rlen, size_t 
Line 623  advance(struct parse *p, char *b, size_t rlen, size_t 
         } else          } else
                 space = 0;                  space = 0;
   
         p->nline = p->line;          if (refill) {
         p->ncol = p->col;                  p->nline = p->line;
                   p->ncol = p->col;
           }
         while (*pend < rlen) {          while (*pend < rlen) {
                 if (b[*pend] == '\n') {  
                         p->nline++;  
                         p->ncol = 1;  
                 } else  
                         p->ncol++;  
                 if (space && isspace((unsigned char)b[*pend]))                  if (space && isspace((unsigned char)b[*pend]))
                         break;                          break;
                 if (strchr(charset, b[*pend]) != NULL)                  if (strchr(charset, b[*pend]) != NULL)
                         break;                          break;
                 ++*pend;                  increment(p, b, pend, refill);
         }          }
         if (*pend == rlen) {          if (*pend == rlen) {
                 b[rlen] = '\0';                  b[rlen] = '\0';
                 return 1;                  return refill;
         } else          } else
                 return 0;                  return 0;
 }  }
   
 struct ptree *  size_t
 parse_file(struct parse *p, int fd, const char *fname)  parse_string(struct parse *p, char *b, size_t rlen,
       enum pstate *pstate, int refill)
 {  {
         char             b[4096];          char            *cp;
         ssize_t          rsz;   /* Return value from read(2). */  
         size_t           rlen;  /* Number of bytes in b[]. */  
         size_t           poff;  /* Parse offset in b[]. */          size_t           poff;  /* Parse offset in b[]. */
         size_t           pend;  /* Offset of the end of the current word. */          size_t           pend;  /* Offset of the end of the current word. */
         int              in_tag, in_arg, in_quotes, elem_end;          int              elem_end;
   
         p->fname = fname;          pend = 0;
         p->nline = 1;          for (;;) {
         p->ncol = 1;  
         rlen = 0;  
         in_tag = in_arg = in_quotes = 0;  
   
         /*                  /* Proceed to the next token, skipping whitespace. */
          * Read loop.  
          *  
          * We have to enter the read loop once more even on EOF  
          * because the previous token may have been incomplete,  
          * such that it asked for more input.  
          * Once rsz is 0, incomplete tokens will no longer ask  
          * for more input but instead use whatever there is,  
          * and then exit the read loop.  
          * The minus one on the size limit for read(2) is needed  
          * such that advance() can set b[rlen] to NUL when needed.  
          */  
   
         while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0) {                  if (refill) {
                 if ((rlen += rsz) == 0)                          p->line = p->nline;
                           p->col = p->ncol;
                   }
                   if ((poff = pend) == rlen)
                         break;                          break;
                   if (isspace((unsigned char)b[pend])) {
                           increment(p, b, &pend, refill);
                           continue;
                   }
   
                 /* Token loop. */                  /*
                    * The following four cases (ARG, TAG, and starting an
                    * entity or a tag) all parse a word or quoted string.
                    * If that extends beyond the read buffer and the last
                    * read(2) still got data, they all break out of the
                    * token loop to request more data from the read loop.
                    *
                    * Also, three of them detect self-closing tags, those
                    * ending with "/>", setting the flag elem_end and
                    * calling xml_elem_end() at the very end, after
                    * handling the attribute value, attribute name, or
                    * tag name, respectively.
                    */
   
                 pend = 0;                  /* Parse an attribute value. */
                 for (;;) {  
   
                         /* Proceed to the next token, skipping whitespace. */                  if (*pstate >= PARSE_ARG) {
                           if (*pstate == PARSE_ARG &&
                         p->line = p->nline;                              (b[pend] == '\'' || b[pend] == '"')) {
                         p->col = p->ncol;                                  *pstate = b[pend] == '"' ?
                         if ((poff = pend) == rlen)                                      PARSE_DQ : PARSE_SQ;
                                 break;                                  increment(p, b, &pend, refill);
                         if (isspace((unsigned char)b[pend])) {  
                                 if (b[pend++] == '\n') {  
                                         p->nline++;  
                                         p->ncol = 1;  
                                 } else  
                                         p->ncol++;  
                                 continue;                                  continue;
                         }                          }
                           if (advance(p, b, rlen, &pend,
                               *pstate == PARSE_DQ ? "\"" :
                               *pstate == PARSE_SQ ? "'" : " >", refill))
                                   break;
                           *pstate = PARSE_TAG;
                           elem_end = 0;
                           if (b[pend] == '>') {
                                   *pstate = PARSE_ELEM;
                                   if (pend > 0 && b[pend - 1] == '/') {
                                           b[pend - 1] = '\0';
                                           elem_end = 1;
                                   }
                           }
                           b[pend] = '\0';
                           if (pend < rlen)
                                   increment(p, b, &pend, refill);
                           xml_attrval(p, b + poff);
                           if (elem_end)
                                   xml_elem_end(p, NULL);
   
                         /*                  /* Look for an attribute name. */
                          * The following three cases (in_arg, in_tag,  
                          * and starting a tag) all parse a word or  
                          * quoted string.  If that extends beyond the  
                          * read buffer and the last read(2) still got  
                          * data, they all break out of the token loop  
                          * to request more data from the read loop.  
                          *  
                          * Also, they all detect self-closing tags,  
                          * those ending with "/>", setting the flag  
                          * elem_end and calling xml_elem_end() at the  
                          * very end, after handling the attribute value,  
                          * attribute name, or tag name, respectively.  
                          */  
   
                         /* Parse an attribute value. */                  } else if (*pstate == PARSE_TAG) {
                           if (advance(p, b, rlen, &pend, " =>", refill))
                         if (in_arg) {                                  break;
                                 if (in_quotes == 0 && b[pend] == '"') {                          elem_end = 0;
                                         in_quotes = 1;                          switch (b[pend]) {
                                         p->ncol++;                          case '>':
                                         pend++;                                  *pstate = PARSE_ELEM;
                                         continue;                                  if (pend > 0 && b[pend - 1] == '/') {
                                           b[pend - 1] = '\0';
                                           elem_end = 1;
                                 }                                  }
                                 if (advance(p, b, rlen, &pend,                                  break;
                                     in_quotes ? "\"" : " >") && rsz > 0)                          case '=':
                                         break;                                  *pstate = PARSE_ARG;
                                 in_arg = in_quotes = elem_end = 0;                                  break;
                                 if (b[pend] == '>') {                          default:
                                         in_tag = 0;                                  break;
                                         if (pend > 0 && b[pend - 1] == '/') {                          }
                                                 b[pend - 1] = '\0';                          b[pend] = '\0';
                                                 elem_end = 1;                          if (pend < rlen)
                                         }                                  increment(p, b, &pend, refill);
                                 }                          xml_attrkey(p, b + poff);
                                 b[pend] = '\0';                          if (elem_end)
                                 if (pend < rlen)                                  xml_elem_end(p, NULL);
                                         pend++;  
                                 xml_attrval(p, b + poff);  
                                 if (elem_end)  
                                         xml_elem_end(p, NULL);  
   
                         /* Look for an attribute name. */                  /* Begin an opening or closing tag. */
   
                         } else if (in_tag) {                  } else if (b[poff] == '<') {
                                 if (advance(p, b, rlen, &pend, " =>") &&                          if (advance(p, b, rlen, &pend, " >", refill))
                                     rsz > 0)                                  break;
                                         break;                          if (pend > poff + 3 &&
                                 elem_end = 0;                              strncmp(b + poff, "<!--", 4) == 0) {
                                 switch (b[pend]) {  
                                 case '>':  
                                         in_tag = 0;  
                                         if (pend > 0 && b[pend - 1] == '/') {  
                                                 b[pend - 1] = '\0';  
                                                 elem_end = 1;  
                                         }  
                                         break;  
                                 case '=':  
                                         in_arg = 1;  
                                         break;  
                                 default:  
                                         break;  
                                 }  
                                 b[pend] = '\0';  
                                 if (pend < rlen)  
                                         pend++;  
                                 xml_attrkey(p, b + poff);  
                                 if (elem_end)  
                                         xml_elem_end(p, NULL);  
   
                         /* Begin an opening or closing tag. */                                  /* Skip a comment. */
   
                         } else if (b[poff] == '<') {                                  cp = strstr(b + pend - 2, "-->");
                                 if (advance(p, b, rlen, &pend, " >") &&                                  if (cp == NULL) {
                                     rsz > 0)                                          if (refill)
                                         break;                                                  break;
                                 elem_end = 0;                                          cp = b + rlen;
                                 if (b[pend] != '>')  
                                         in_tag = 1;  
                                 else if (pend > 0 && b[pend - 1] == '/') {  
                                         b[pend - 1] = '\0';  
                                         elem_end = 1;  
                                 }  
                                 b[pend] = '\0';  
                                 if (pend < rlen)  
                                         pend++;  
                                 if (b[++poff] == '/') {  
                                         elem_end = 1;  
                                         poff++;  
                                 } else                                  } else
                                         xml_elem_start(p, b + poff);                                          cp += 3;
                                 if (elem_end)                                  while (b + pend < cp)
                                         xml_elem_end(p, b + poff);                                          increment(p, b, &pend, refill);
                                   continue;
                           }
                           elem_end = 0;
                           if (b[pend] != '>')
                                   *pstate = PARSE_TAG;
                           else if (pend > 0 && b[pend - 1] == '/') {
                                   b[pend - 1] = '\0';
                                   elem_end = 1;
                           }
                           b[pend] = '\0';
                           if (pend < rlen)
                                   increment(p, b, &pend, refill);
                           if (b[++poff] == '/') {
                                   elem_end = 1;
                                   poff++;
                           } else
                                   xml_elem_start(p, b + poff);
                           if (elem_end)
                                   xml_elem_end(p, b + poff);
   
                         /* Process text up to the next tag. */                  /* Process an entity. */
   
                         } else {                  } else if (b[poff] == '&') {
                                 if (advance(p, b, rlen, &pend, "<") == 0)                          if (advance(p, b, rlen, &pend, ";", refill))
                                         p->ncol--;                                  break;
                                 xml_char(p, b + poff, pend - poff);                          b[pend] = '\0';
                         }                          if (pend < rlen)
                                   increment(p, b, &pend, refill);
                           xml_entity(p, b + poff + 1);
   
                   /* Process text up to the next tag, entity, or EOL. */
   
                   } else {
                           advance(p, b, rlen, &pend, "<&", refill);
                           xml_char(p, b + poff, pend - poff);
                 }                  }
           }
           return poff;
   }
   
                 /* Buffer exhausted; shift left and re-fill. */  struct ptree *
   parse_file(struct parse *p, int fd, const char *fname)
   {
           char             b[4096];
           ssize_t          rsz;   /* Return value from read(2). */
           size_t           rlen;  /* Number of bytes in b[]. */
           size_t           poff;  /* Parse offset in b[]. */
           enum pstate      pstate;
   
           p->fname = fname;
           p->nline = 1;
           p->ncol = 1;
           pstate = PARSE_ELEM;
           rlen = 0;
   
           /*
            * Read loop.
            *
            * If the previous token was incomplete and asked for more
            * input, we have to enter the read loop once more even on EOF.
            * Once rsz is 0, incomplete tokens will no longer ask
            * for more input but instead use whatever there is,
            * and then exit the read loop.
            * The minus one on the size limit for read(2) is needed
            * such that advance() can set b[rlen] to NUL when needed.
            */
   
           while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 &&
               (rlen += rsz) > 0) {
                   poff = parse_string(p, b, rlen, &pstate, rsz > 0);
                   /* Buffer exhausted; shift left and re-fill. */
                 assert(poff > 0);                  assert(poff > 0);
                 memmove(b, b + poff, rlen - poff);  
                 rlen -= poff;                  rlen -= poff;
                   memmove(b, b + poff, rlen);
         }          }
         if (rsz < 0) {          if (rsz < 0) {
                 perror(fname);                  perror(fname);

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.14

CVSweb