=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.37 retrieving revision 1.41 diff -u -p -r1.37 -r1.41 --- docbook2mdoc/docbook2mdoc.c 2014/04/30 12:34:44 1.37 +++ docbook2mdoc/docbook2mdoc.c 2014/10/12 15:34:44 1.41 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.37 2014/04/30 12:34:44 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.41 2014/10/12 15:34:44 kristaps Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * @@ -74,7 +74,9 @@ struct pnode { static const char *attrkeys[ATTRKEY__MAX] = { "choice", + "close", "id", + "open", "rep" }; @@ -96,6 +98,7 @@ static const struct node nodes[NODE__MAX] = { { "citerefentry", NODE_IGNTEXT }, { "cmdsynopsis", NODE_IGNTEXT }, { "code", 0 }, + { "colspec", NODE_IGNTEXT }, { "command", 0 }, { "constant", 0 }, { "copyright", NODE_IGNTEXT }, @@ -103,6 +106,7 @@ static const struct node nodes[NODE__MAX] = { { "emphasis", 0 }, { "entry", 0 }, { "envar", 0 }, + { "fieldsynopsis", NODE_IGNTEXT }, { "filename", 0 }, { "funcdef", 0 }, { "funcprototype", NODE_IGNTEXT }, @@ -112,12 +116,24 @@ static const struct node nodes[NODE__MAX] = { { "group", NODE_IGNTEXT }, { "holder", NODE_IGNTEXT }, { "info", NODE_IGNTEXT }, + { "informalequation", NODE_IGNTEXT }, { "informaltable", NODE_IGNTEXT }, + { "inlineequation", NODE_IGNTEXT }, { "itemizedlist", NODE_IGNTEXT }, { "link", 0 }, { "listitem", NODE_IGNTEXT }, { "literal", 0 }, { "manvolnum", 0 }, + { "mml:math", NODE_IGNTEXT }, + { "mml:mfenced", 0 }, + { "mml:mfrac", 0 }, + { "mml:mi", 0 }, + { "mml:mn", 0 }, + { "mml:mo", 0 }, + { "mml:mrow", 0 }, + { "mml:msub", 0 }, + { "mml:msup", 0 }, + { "modifier", 0 }, { "note", NODE_IGNTEXT }, { "option", 0 }, { "orderedlist", NODE_IGNTEXT }, @@ -133,6 +149,7 @@ static const struct node nodes[NODE__MAX] = { { "refentryinfo", NODE_IGNTEXT }, { "refentrytitle", 0 }, { "refmeta", NODE_IGNTEXT }, + { "refmetainfo", NODE_IGNTEXT }, { "refmiscinfo", NODE_IGNTEXT }, { "refname", 0 }, { "refnamediv", NODE_IGNTEXT }, @@ -159,6 +176,7 @@ static const struct node nodes[NODE__MAX] = { { "tip", NODE_IGNTEXT }, { "title", 0 }, { "trademark", 0 }, + { "type", 0 }, { "ulink", 0 }, { "userinput", 0 }, { "variablelist", NODE_IGNTEXT }, @@ -169,6 +187,8 @@ static const struct node nodes[NODE__MAX] = { { "year", NODE_IGNTEXT }, }; +static int warn = 0; + static void pnode_print(struct parse *p, struct pnode *pn); @@ -346,32 +366,35 @@ xml_elem_start(void *arg, const XML_Char *name, const if (0 == strcmp(*att, attrkeys[key])) break; if (ATTRKEY__MAX == key) { - fprintf(stderr, "%s:%zu:%zu: warning: " - "unknown attribute \"%s\"\n", - ps->fname, - XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(ps->xml), - *att); + if (warn) + fprintf(stderr, "%s:%zu:%zu: warning: " + "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: warning: " - "bad attribute \"%s\"\n", - ps->fname, - XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(ps->xml), - *att); + if (warn) + fprintf(stderr, "%s:%zu:%zu: warning: " + "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: warning: " - "bad attribute value \"%s\"\n", - ps->fname, - XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(ps->xml), - *(att + 1)); + if (warn) + fprintf(stderr, "%s:%zu:%zu: warning: " + "bad attribute value \"%s\"\n", + ps->fname, + XML_GetCurrentLineNumber(ps->xml), + XML_GetCurrentColumnNumber(ps->xml), + *(att + 1)); continue; } pattr = calloc(1, sizeof(struct pattr)); @@ -841,7 +864,76 @@ pnode_printparamdef(struct parse *p, struct pnode *pn) p->newln = 1; } +/* + * The node is a little peculiar. + * First, it can have arbitrary open and closing tokens, which default + * to parentheses. + * Second, >1 arguments are separated by commas. + */ static void +pnode_printmathfenced(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + struct pattr *ap; + + TAILQ_FOREACH(ap, &pn->attrq, child) + if (ATTRKEY_OPEN == ap->key) { + printf("left %s ", ap->rawval); + break; + } + if (NULL == ap) + printf("left ( "); + + pp = TAILQ_FIRST(&pn->childq); + pnode_print(p, pp); + + while (NULL != (pp = TAILQ_NEXT(pp, child))) { + putchar(','); + pnode_print(p, pp); + } + + TAILQ_FOREACH(ap, &pn->attrq, child) + if (ATTRKEY_CLOSE == ap->key) { + printf("right %s ", ap->rawval); + break; + } + if (NULL == ap) + printf("right ) "); +} + +/* + * These math nodes require special handling because they have infix + * syntax, instead of the usual prefix or prefix. + * So we need to break up the first and second child node with a + * particular eqn(7) word. + */ +static void +pnode_printmath(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + + pp = TAILQ_FIRST(&pn->childq); + pnode_print(p, pp); + + switch (pn->node) { + case (NODE_MML_MSUP): + printf(" sup "); + break; + case (NODE_MML_MFRAC): + printf(" over "); + break; + case (NODE_MML_MSUB): + printf(" sub "); + break; + default: + break; + } + + pp = TAILQ_NEXT(pp, child); + pnode_print(p, pp); +} + +static void pnode_printfuncprototype(struct parse *p, struct pnode *pn) { struct pnode *pp, *fdef; @@ -1181,6 +1273,28 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_printmopen(p); fputs("Li", stdout); break; + case (NODE_MML_MATH): + if ( ! p->newln) + putchar('\n'); + puts(".EQ"); + p->newln = 0; + break; + case (NODE_MML_MFENCED): + pnode_printmathfenced(p, pn); + pnode_unlinksub(pn); + break; + case (NODE_MML_MROW): + case (NODE_MML_MI): + case (NODE_MML_MN): + case (NODE_MML_MO): + putchar('{'); + break; + case (NODE_MML_MFRAC): + case (NODE_MML_MSUB): + case (NODE_MML_MSUP): + pnode_printmath(p, pn); + pnode_unlinksub(pn); + break; case (NODE_OPTION): pnode_printmopen(p); fputs("Fl", stdout); @@ -1332,6 +1446,10 @@ pnode_print(struct parse *p, struct pnode *pn) } p->newln = 0; break; + case (NODE_TYPE): + pnode_printmopen(p); + fputs("Vt", stdout); + break; case (NODE_USERINPUT): pnode_printmopen(p); fputs("Li", stdout); @@ -1357,6 +1475,18 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_print(p, pp); switch (pn->node) { + case (NODE_MML_MATH): + if ( ! p->newln) + putchar('\n'); + puts(".EN"); + p->newln = 1; + break; + case (NODE_MML_MROW): + case (NODE_MML_MI): + case (NODE_MML_MN): + case (NODE_MML_MO): + putchar('}'); + break; case (NODE_APPLICATION): case (NODE_ARG): case (NODE_CITEREFENTRY): @@ -1376,6 +1506,7 @@ pnode_print(struct parse *p, struct pnode *pn) case (NODE_SGMLTAG): case (NODE_STRUCTNAME): case (NODE_TEXT): + case (NODE_TYPE): case (NODE_USERINPUT): case (NODE_VARNAME): pnode_printmclosepunct(p, pn, sv); @@ -1468,15 +1599,28 @@ main(int argc, char *argv[]) XML_Parser xp; const char *fname; char *buf; - int fd, rc; + int fd, rc, ch; + const char *progname; + progname = strrchr(argv[0], '/'); + if (progname == NULL) + progname = argv[0]; + else + ++progname; + fname = "-"; xp = NULL; buf = NULL; rc = 0; - if (-1 != getopt(argc, argv, "")) - return(EXIT_FAILURE); + while (-1 != (ch = getopt(argc, argv, "W"))) + switch (ch) { + case ('W'): + warn = 1; + break; + default: + goto usage; + } argc -= optind; argv += optind; @@ -1510,4 +1654,8 @@ main(int argc, char *argv[]) if (STDIN_FILENO != fd) close(fd); return(rc ? EXIT_SUCCESS : EXIT_FAILURE); + +usage: + fprintf(stderr, "usage: %s [-W]\n", progname); + return(EXIT_FAILURE); }