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

Diff for /docbook2mdoc/docbook2mdoc.c between version 1.11 and 1.12

version 1.11, 2014/03/29 11:13:49 version 1.12, 2014/03/29 22:44:06
Line 67  enum nodeid {
Line 67  enum nodeid {
 };  };
   
 /*  /*
    * All recognised attribute keys.
    */
   enum    attrkey {
           /* Alpha-order... */
           ATTRKEY_CHOICE = 0,
           ATTRKEY_ID,
           ATTRKEY_REP,
           ATTRKEY__MAX
   };
   
   /*
    * All [explicitly] recognised attribute values.
    * If an attribute has ATTRVAL__MAX, it could be a free-form.
    */
   enum    attrval {
           /* Alpha-order... */
           ATTRVAL_NOREPEAT,
           ATTRVAL_OPT,
           ATTRVAL_PLAIN,
           ATTRVAL_REPEAT,
           ATTRVAL_REQ,
           ATTRVAL__MAX
   };
   
   /*
  * Global parse state.   * Global parse state.
  * Keep this as simple and small as possible.   * Keep this as simple and small as possible.
  */   */
 struct  parse {  struct  parse {
           XML_Parser       xml;
         enum nodeid      node; /* current (NODE_ROOT if pre-tree) */          enum nodeid      node; /* current (NODE_ROOT if pre-tree) */
           const char      *fname; /* filename */
         int              stop; /* should we stop now? */          int              stop; /* should we stop now? */
         struct pnode    *root; /* root of parse tree */          struct pnode    *root; /* root of parse tree */
         struct pnode    *cur; /* current node in tree */          struct pnode    *cur; /* current node in tree */
Line 88  struct node {
Line 115  struct node {
 };  };
   
 TAILQ_HEAD(pnodeq, pnode);  TAILQ_HEAD(pnodeq, pnode);
   TAILQ_HEAD(pattrq, pattr);
   
   struct  pattr {
           enum attrkey     key;
           enum attrval     val;
           char            *rawval;
           TAILQ_ENTRY(pattr) child;
   };
   
 struct  pnode {  struct  pnode {
         enum nodeid      node; /* node type */          enum nodeid      node; /* node type */
         char            *b; /* binary data buffer */          char            *b; /* binary data buffer */
         size_t           bsz; /* data buffer size */          size_t           bsz; /* data buffer size */
         struct pnode    *parent; /* parent (or NULL if top) */          struct pnode    *parent; /* parent (or NULL if top) */
         struct pnodeq    childq; /* queue of children */          struct pnodeq    childq; /* queue of children */
           struct pattrq    attrq; /* attributes of node */
         TAILQ_ENTRY(pnode) child;          TAILQ_ENTRY(pnode) child;
 };  };
   
   static  const char *attrkeys[ATTRKEY__MAX] = {
           "choice",
           "id",
           "rep"
   };
   
   static  const char *attrvals[ATTRVAL__MAX] = {
           "norepeat",
           "opt",
           "plain",
           "repeat",
           "req"
   };
   
 static  const struct node nodes[NODE__MAX] = {  static  const struct node nodes[NODE__MAX] = {
         { NULL, 0 },          { NULL, 0 },
         { "arg", 0 },          { "arg", 0 },
Line 136  static const struct node nodes[NODE__MAX] = {
Line 186  static const struct node nodes[NODE__MAX] = {
 static void  static void
 pnode_print(struct parse *p, struct pnode *pn);  pnode_print(struct parse *p, struct pnode *pn);
   
   static int
   isattrkey(enum nodeid node, enum attrkey key)
   {
   
           switch (key) {
           case (ATTRKEY_CHOICE):
                   return(node == NODE_ARG);
           case (ATTRKEY_ID):
                   /* Common to all. */
                   return(1);
           case (ATTRKEY_REP):
                   return(node == NODE_ARG);
           default:
                   break;
           }
           abort();
           return(0);
   }
   
   static int
   isattrval(enum attrkey key, enum attrval val)
   {
   
           switch (val) {
           case (ATTRVAL_OPT):
           case (ATTRVAL_PLAIN):
           case (ATTRVAL_REQ):
                   return(key == ATTRKEY_CHOICE);
           case (ATTRVAL_REPEAT):
           case (ATTRVAL_NOREPEAT):
                   return(key == ATTRKEY_REP);
           default:
                   break;
           }
           abort();
           return(0);
   }
   
 /*  /*
  * Look up whether "parent" is a valid parent for "node".   * Look up whether "parent" is a valid parent for "node".
  * This is sucked directly from the DocBook specification: look at the   * This is sucked directly from the DocBook specification: look at the
Line 437  xml_char(void *arg, const XML_Char *p, int sz)
Line 525  xml_char(void *arg, const XML_Char *p, int sz)
                 dat->node = ps->node = NODE_TEXT;                  dat->node = ps->node = NODE_TEXT;
                 dat->parent = ps->cur;                  dat->parent = ps->cur;
                 TAILQ_INIT(&dat->childq);                  TAILQ_INIT(&dat->childq);
                   TAILQ_INIT(&dat->attrq);
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);                  TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);
                 ps->cur = dat;                  ps->cur = dat;
                 assert(NULL != ps->root);                  assert(NULL != ps->root);
Line 481  pnode_trim(struct pnode *pn)
Line 570  pnode_trim(struct pnode *pn)
 static void  static void
 xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts)  xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts)
 {  {
         struct parse    *ps = arg;          struct parse     *ps = arg;
         enum nodeid      node;          enum nodeid       node;
         struct pnode    *dat;          enum attrkey      key;
           enum attrval      val;
           struct pnode     *dat;
           struct pattr     *pattr;
           const XML_Char  **att;
   
         if (ps->stop)          if (ps->stop)
                 return;                  return;
Line 503  xml_elem_start(void *arg, const XML_Char *name, const 
Line 596  xml_elem_start(void *arg, const XML_Char *name, const 
                 else if (0 == strcmp(nodes[node].name, name))                  else if (0 == strcmp(nodes[node].name, name))
                         break;                          break;
   
         /* FIXME: do more with these error messages... */  
         if (NODE__MAX == node && NODE_ROOT == ps->node) {          if (NODE__MAX == node && NODE_ROOT == ps->node) {
                 fprintf(stderr, "%s: ignoring node\n", name);  
                 return;                  return;
         } else if (NODE__MAX == node) {          } else if (NODE__MAX == node) {
                 fprintf(stderr, "%s: unknown node\n", name);                  fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n",
                           ps->fname, XML_GetCurrentLineNumber(ps->xml),
                           XML_GetCurrentColumnNumber(ps->xml), name);
                 ps->stop = 1;                  ps->stop = 1;
                 return;                  return;
         } else if (NODE_ROOT == ps->node && NULL != ps->root) {          } else if (NODE_ROOT == ps->node && NULL != ps->root) {
                 fprintf(stderr, "%s: reentering?\n", name);                  fprintf(stderr, "%s:%zu:%zu: multiple refentries\n",
                           ps->fname, XML_GetCurrentLineNumber(ps->xml),
                           XML_GetCurrentColumnNumber(ps->xml));
                 ps->stop = 1;                  ps->stop = 1;
                 return;                  return;
         } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) {          } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) {
                 fprintf(stderr, "%s: known node w/o context\n", name);  
                 return;                  return;
         } else if ( ! isparent(node, ps->node)) {          } else if ( ! isparent(node, ps->node)) {
                 fprintf(stderr, "%s: bad parent\n", name);                  fprintf(stderr, "%s:%zu:%zu: bad parent \"%s\"\n",
                           ps->fname, XML_GetCurrentLineNumber(ps->xml),
                           XML_GetCurrentColumnNumber(ps->xml),
                           NULL == nodes[ps->node].name ?
                           "(none)" : nodes[ps->node].name);
                 ps->stop = 1;                  ps->stop = 1;
                 return;                  return;
         }          }
Line 532  xml_elem_start(void *arg, const XML_Char *name, const 
Line 630  xml_elem_start(void *arg, const XML_Char *name, const 
         dat->node = ps->node = node;          dat->node = ps->node = node;
         dat->parent = ps->cur;          dat->parent = ps->cur;
         TAILQ_INIT(&dat->childq);          TAILQ_INIT(&dat->childq);
           TAILQ_INIT(&dat->attrq);
   
         if (NULL != ps->cur)          if (NULL != ps->cur)
                 TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);                  TAILQ_INSERT_TAIL(&ps->cur->childq, dat, child);
Line 539  xml_elem_start(void *arg, const XML_Char *name, const 
Line 638  xml_elem_start(void *arg, const XML_Char *name, const 
         ps->cur = dat;          ps->cur = dat;
         if (NULL == ps->root)          if (NULL == ps->root)
                 ps->root = dat;                  ps->root = dat;
   
           /*
            * Process attributes.
            */
           for (att = atts; NULL != *att; att += 2) {
                   for (key = 0; key < ATTRKEY__MAX; key++)
                           if (0 == strcmp(*att, attrkeys[key]))
                                   break;
                   if (ATTRKEY__MAX == key) {
                           fprintf(stderr, "%s:%zu:%zu: unknown "
                                   "attribute \"%s\"\n", ps->fname,
                                   XML_GetCurrentLineNumber(ps->xml),
                                   XML_GetCurrentColumnNumber(ps->xml),
                                   *att);
                           continue;
                   } else if ( ! isattrkey(node, key)) {
                           fprintf(stderr, "%s:%zu:%zu: bad "
                                   "attribute \"%s\"\n", ps->fname,
                                   XML_GetCurrentLineNumber(ps->xml),
                                   XML_GetCurrentColumnNumber(ps->xml),
                                   *att);
                           continue;
                   }
                   for (val = 0; val < ATTRVAL__MAX; val++)
                           if (0 == strcmp(*(att + 1), attrvals[val]))
                                   break;
                   if (ATTRVAL__MAX != val && ! isattrval(key, val)) {
                           fprintf(stderr, "%s:%zu:%zu: bad "
                                   "value \"%s\"\n", ps->fname,
                                   XML_GetCurrentLineNumber(ps->xml),
                                   XML_GetCurrentColumnNumber(ps->xml),
                                   *(att + 1));
                           continue;
                   }
                   pattr = calloc(1, sizeof(struct pattr));
                   pattr->key = key;
                   pattr->val = val;
                   if (ATTRVAL__MAX == val)
                           pattr->rawval = strdup(*(att + 1));
                   TAILQ_INSERT_TAIL(&dat->attrq, pattr, child);
           }
   
 }  }
   
 /*  /*
Line 576  static void
Line 717  static void
 pnode_free(struct pnode *pn)  pnode_free(struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           struct pattr    *ap;
   
         if (NULL == pn)          if (NULL == pn)
                 return;                  return;
Line 585  pnode_free(struct pnode *pn)
Line 727  pnode_free(struct pnode *pn)
                 pnode_free(pp);                  pnode_free(pp);
         }          }
   
           while (NULL != (ap = TAILQ_FIRST(&pn->attrq))) {
                   TAILQ_REMOVE(&pn->attrq, ap, child);
                   free(ap->rawval);
                   free(ap);
           }
   
         free(pn->b);          free(pn->b);
         free(pn);          free(pn);
 }  }
Line 661  bufappend_r(struct parse *p, struct pnode *pn)
Line 809  bufappend_r(struct parse *p, struct pnode *pn)
                 bufappend_r(p, pp);                  bufappend_r(p, pp);
 }  }
   
   #define MACROLINE_NORM  0
   #define MACROLINE_UPPER 1
 /*  /*
  * Recursively print text presumably on a macro line.   * Recursively print text presumably on a macro line.
  * Convert all whitespace to regular spaces.   * Convert all whitespace to regular spaces.
  */   */
 static void  static void
 pnode_printmacrolinepart(struct parse *p, struct pnode *pn)  pnode_printmacrolinetext(struct parse *p, struct pnode *pn, int fl)
 {  {
         char            *cp;          char            *cp;
   
Line 692  pnode_printmacrolinepart(struct parse *p, struct pnode
Line 842  pnode_printmacrolinepart(struct parse *p, struct pnode
                           ('\0' == *(cp + 3) ||                            ('\0' == *(cp + 3) ||
                            ' ' == *(cp + 3)))))                             ' ' == *(cp + 3)))))
                         fputs("\\&", stdout);                          fputs("\\&", stdout);
                 putchar(*cp);                  if (MACROLINE_UPPER & fl)
                           putchar(toupper((int)*cp));
                   else
                           putchar((int)*cp);
                 /* If we're a character escape, escape us. */                  /* If we're a character escape, escape us. */
                 if ('\\' == *cp)                  if ('\\' == *cp)
                         putchar('e');                          putchar('e');
         }          }
 }  }
   
   static void
   pnode_printmacrolinepart(struct parse *p, struct pnode *pn)
   {
   
           pnode_printmacrolinetext(p, pn, 0);
   }
   
 /*  /*
  * Just pnode_printmacrolinepart() but with a newline.   * Just pnode_printmacrolinepart() but with a newline.
  * If no text, just the newline.   * If no text, just the newline.
Line 707  static void
Line 867  static void
 pnode_printmacroline(struct parse *p, struct pnode *pn)  pnode_printmacroline(struct parse *p, struct pnode *pn)
 {  {
   
         pnode_printmacrolinepart(p, pn);          pnode_printmacrolinetext(p, pn, 0);
         putchar('\n');          putchar('\n');
 }  }
   
Line 813  pnode_printrefmeta(struct parse *p, struct pnode *pn)
Line 973  pnode_printrefmeta(struct parse *p, struct pnode *pn)
   
         if (NULL != title) {          if (NULL != title) {
                 /* FIXME: uppercase. */                  /* FIXME: uppercase. */
                 pnode_printmacrolinepart(p, title);                  pnode_printmacrolinetext(p, title, MACROLINE_UPPER);
                 putchar(' ');                  putchar(' ');
         } else          } else
                 fputs("UNKNOWN ", stdout);                  fputs("UNKNOWN ", stdout);
Line 899  pnode_printfuncprototype(struct parse *p, struct pnode
Line 1059  pnode_printfuncprototype(struct parse *p, struct pnode
  * The <arg> element is more complicated than it should be because text   * The <arg> element is more complicated than it should be because text
  * nodes are treated like ".Ar foo", but non-text nodes need to be   * nodes are treated like ".Ar foo", but non-text nodes need to be
  * re-sent into the printer (i.e., without the preceding ".Ar").   * re-sent into the printer (i.e., without the preceding ".Ar").
  * TODO: handle "optional" attribute.   * This also handles the case of "repetition" (or in other words, the
    * ellipsis following an argument) and optionality.
  */   */
 static void  static void
 pnode_printarg(struct parse *p, struct pnode *pn)  pnode_printarg(struct parse *p, struct pnode *pn)
 {  {
         struct pnode    *pp;          struct pnode    *pp;
           struct pattr    *ap;
           int              isop, isrep;
   
           isop = 1;
           isrep = 0;
           TAILQ_FOREACH(ap, &pn->attrq, child)
                   if (ATTRKEY_CHOICE == ap->key &&
                           (ATTRVAL_PLAIN == ap->val ||
                            ATTRVAL_REQ == ap->val))
                           isop = 0;
                   else if (ATTRKEY_REP == ap->key &&
                           (ATTRVAL_REPEAT == ap->val))
                           isrep = 1;
   
           if (isop) {
                   pnode_printmopen(p);
                   fputs("Op ", stdout);
           }
   
         TAILQ_FOREACH(pp, &pn->childq, child) {          TAILQ_FOREACH(pp, &pn->childq, child) {
                 if (NODE_TEXT == pp->node) {                  if (NODE_TEXT == pp->node) {
                         pnode_printmopen(p);                          pnode_printmopen(p);
                         fputs("Ar ", stdout);                          fputs("Ar ", stdout);
                 }                  }
                 pnode_print(p, pp);                  pnode_print(p, pp);
                   if (NODE_TEXT == pp->node && isrep)
                           fputs("...", stdout);
         }          }
 }  }
   
Line 1094  pnode_print(struct parse *p, struct pnode *pn)
Line 1275  pnode_print(struct parse *p, struct pnode *pn)
         case (NODE_FUNCSYNOPSISINFO):          case (NODE_FUNCSYNOPSISINFO):
         case (NODE_OPTION):          case (NODE_OPTION):
         case (NODE_PARAMETER):          case (NODE_PARAMETER):
         case (NODE_REFNAME):  
         case (NODE_STRUCTNAME):          case (NODE_STRUCTNAME):
         case (NODE_TEXT):          case (NODE_TEXT):
                 pnode_printmclose(p, sv);                  pnode_printmclose(p, sv);
                 break;                  break;
           case (NODE_REFNAME):
                   /*
                    * If we're in the NAME macro and we have multiple
                    * <refname> macros in sequence, then print out a
                    * trailing comma before the newline.
                    */
                   if (NULL != pn->parent &&
                           NODE_REFNAMEDIV == pn->parent->node &&
                           NULL != TAILQ_NEXT(pn, child) &&
                           NODE_REFNAME == TAILQ_NEXT(pn, child)->node)
                           fputs(" ,", stdout);
                   pnode_printmclose(p, sv);
                   break;
         case (NODE_PROGRAMLISTING):          case (NODE_PROGRAMLISTING):
                 assert(p->newln);                  assert(p->newln);
                 puts(".Ed");                  puts(".Ed");
Line 1124  readfile(XML_Parser xp, int fd, 
Line 1317  readfile(XML_Parser xp, int fd, 
         memset(&p, 0, sizeof(struct parse));          memset(&p, 0, sizeof(struct parse));
   
         p.b = malloc(p.bsz = p.mbsz = 1024);          p.b = malloc(p.bsz = p.mbsz = 1024);
           p.fname = fn;
           p.xml = xp;
   
         XML_SetCharacterDataHandler(xp, xml_char);          XML_SetCharacterDataHandler(xp, xml_char);
         XML_SetElementHandler(xp, xml_elem_start, xml_elem_end);          XML_SetElementHandler(xp, xml_elem_start, xml_elem_end);

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

CVSweb