=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.1.1.1 retrieving revision 1.5 diff -u -p -r1.1.1.1 -r1.5 --- docbook2mdoc/docbook2mdoc.c 2014/03/28 02:04:47 1.1.1.1 +++ docbook2mdoc/docbook2mdoc.c 2014/03/28 10:03:36 1.5 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.1.1.1 2014/03/28 02:04:47 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.5 2014/03/28 10:03:36 kristaps Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * @@ -31,12 +31,21 @@ enum nodeid { NODE_ROOT = 0, /* Must comes first. */ /* Alpha-ordered hereafter. */ + NODE_ARG, NODE_CITEREFENTRY, + NODE_CMDSYNOPSIS, NODE_CODE, + NODE_COMMAND, + NODE_FUNCDEF, + NODE_FUNCPROTOTYPE, NODE_FUNCSYNOPSIS, NODE_FUNCSYNOPSISINFO, + NODE_FUNCTION, NODE_MANVOLNUM, + NODE_OPTION, NODE_PARA, + NODE_PARAMDEF, + NODE_PARAMETER, NODE_PROGRAMLISTING, NODE_REFCLASS, NODE_REFDESCRIPTOR, @@ -88,12 +97,21 @@ struct pnode { static const struct node nodes[NODE__MAX] = { { NULL, 0 }, + { "arg", 0 }, { "citerefentry", NODE_IGNTEXT }, + { "cmdsynopsis", NODE_IGNTEXT }, { "code", 0 }, + { "command", 0 }, + { "funcdef", 0 }, + { "funcprototype", NODE_IGNTEXT }, { "funcsynopsis", NODE_IGNTEXT }, { "funcsynopsisinfo", 0 }, + { "function", 0 }, { "manvolnum", 0 }, + { "option", 0 }, { "para", 0 }, + { "paramdef", 0 }, + { "parameter", 0 }, { "programlisting", 0 }, { "refclass", NODE_IGNTEXT }, { "refdescriptor", NODE_IGNTEXT }, @@ -121,6 +139,15 @@ isparent(enum nodeid node, enum nodeid parent) switch (node) { case (NODE_ROOT): return(0); + case (NODE_ARG): + switch (parent) { + case (NODE_ARG): + case (NODE_CMDSYNOPSIS): + return(1); + default: + break; + } + return(0); case (NODE_CITEREFENTRY): switch (parent) { case (NODE_FUNCSYNOPSISINFO): @@ -137,6 +164,16 @@ isparent(enum nodeid node, enum nodeid parent) break; } return(0); + case (NODE_CMDSYNOPSIS): + switch (parent) { + case (NODE_PARA): + case (NODE_REFSECT1): + case (NODE_REFSYNOPSISDIV): + return(1); + default: + break; + } + return(0); case (NODE_CODE): switch (parent) { case (NODE_FUNCSYNOPSISINFO): @@ -153,15 +190,27 @@ isparent(enum nodeid node, enum nodeid parent) break; } return(0); - case (NODE_MANVOLNUM): + case (NODE_COMMAND): switch (parent) { - case (NODE_CITEREFENTRY): - case (NODE_REFMETA): + case (NODE_CMDSYNOPSIS): + case (NODE_FUNCSYNOPSISINFO): + case (NODE_PARA): + case (NODE_PROGRAMLISTING): + case (NODE_REFDESCRIPTOR): + case (NODE_REFENTRYTITLE): + case (NODE_REFNAME): + case (NODE_REFPURPOSE): + case (NODE_SYNOPSIS): + case (NODE_TITLE): return(1); default: break; } return(0); + case (NODE_FUNCDEF): + return(NODE_FUNCPROTOTYPE == parent); + case (NODE_FUNCPROTOTYPE): + return(NODE_FUNCSYNOPSIS == parent); case (NODE_FUNCSYNOPSIS): switch (parent) { case (NODE_PARA): @@ -174,6 +223,50 @@ isparent(enum nodeid node, enum nodeid parent) return(0); case (NODE_FUNCSYNOPSISINFO): return(NODE_FUNCSYNOPSIS == parent); + case (NODE_FUNCTION): + switch (parent) { + case (NODE_CODE): + case (NODE_FUNCDEF): + case (NODE_FUNCSYNOPSISINFO): + case (NODE_PARA): + case (NODE_PROGRAMLISTING): + case (NODE_REFDESCRIPTOR): + case (NODE_REFENTRYTITLE): + case (NODE_REFNAME): + case (NODE_REFPURPOSE): + case (NODE_SYNOPSIS): + case (NODE_TITLE): + return(1); + default: + break; + } + return(0); + case (NODE_MANVOLNUM): + switch (parent) { + case (NODE_CITEREFENTRY): + case (NODE_REFMETA): + return(1); + default: + break; + } + return(0); + case (NODE_OPTION): + switch (parent) { + case (NODE_ARG): + case (NODE_FUNCSYNOPSISINFO): + case (NODE_PARA): + case (NODE_PROGRAMLISTING): + case (NODE_REFDESCRIPTOR): + case (NODE_REFENTRYTITLE): + case (NODE_REFNAME): + case (NODE_REFPURPOSE): + case (NODE_SYNOPSIS): + case (NODE_TITLE): + return(1); + default: + break; + } + return(0); case (NODE_PARA): switch (parent) { case (NODE_REFSECT1): @@ -183,6 +276,26 @@ isparent(enum nodeid node, enum nodeid parent) break; } return(0); + case (NODE_PARAMDEF): + return(NODE_FUNCPROTOTYPE == parent); + case (NODE_PARAMETER): + switch (parent) { + case (NODE_CODE): + case (NODE_FUNCSYNOPSISINFO): + case (NODE_PARA): + case (NODE_PARAMDEF): + case (NODE_PROGRAMLISTING): + case (NODE_REFDESCRIPTOR): + case (NODE_REFENTRYTITLE): + case (NODE_REFNAME): + case (NODE_REFPURPOSE): + case (NODE_SYNOPSIS): + case (NODE_TITLE): + return(1); + default: + break; + } + return(0); case (NODE_PROGRAMLISTING): switch (parent) { case (NODE_PARA): @@ -254,6 +367,7 @@ xml_char(void *arg, const XML_Char *p, int sz) { struct parse *ps = arg; struct pnode *dat; + int i; /* Stopped or no tree yet. */ if (ps->stop || NODE_ROOT == ps->node) @@ -268,8 +382,15 @@ xml_char(void *arg, const XML_Char *p, int sz) * Are we in the midst of processing text? * If we're not processing text right now, then create a text * node for doing so. + * However, don't do so unless we have some non-whitespace to + * process! */ if (NODE_TEXT != ps->node) { + for (i = 0; i < sz; i++) + if ( ! isspace((int)p[i])) + break; + if (i == sz) + return; dat = calloc(1, sizeof(struct pnode)); if (NULL == dat) { perror(NULL); @@ -423,6 +544,14 @@ pnode_unlink(struct pnode *pn) } static void +pnode_unlinksub(struct pnode *pn) +{ + + while ( ! TAILQ_EMPTY(&pn->childq)) + pnode_unlink(TAILQ_FIRST(&pn->childq)); +} + +static void bufclear(struct parse *p) { @@ -446,6 +575,17 @@ bufappend(struct parse *p, struct pnode *pn) p->b[p->bsz] = '\0'; } +static void +bufappend_r(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + + if (NODE_TEXT == pn->node) + bufappend(p, pn); + TAILQ_FOREACH(pp, &pn->childq, child) + bufappend_r(p, pp); +} + /* * Print text presumably on a macro line. * Ignore any child macros. @@ -454,15 +594,10 @@ bufappend(struct parse *p, struct pnode *pn) static void pnode_printmacrolinepart(struct parse *p, struct pnode *pn) { - struct pnode *pp; char *cp; bufclear(p); - while (NULL != (pp = TAILQ_FIRST(&pn->childq))) { - if (NODE_TEXT == pp->node) - bufappend(p, pp); - pnode_unlink(pp); - } + bufappend_r(p, pn); /* Convert all space to spaces. */ for (cp = p->b; '\0' != *cp; cp++) @@ -470,8 +605,7 @@ pnode_printmacrolinepart(struct parse *p, struct pnode *cp = ' '; for (cp = p->b; isspace((int)*cp); cp++) - /* Spin. */ ; - + /* Spin past whitespace (XXX: necessary?) */ ; for ( ; '\0' != *cp; cp++) { /* Escape us if we look like a macro. */ if ((cp == p->b || ' ' == *(cp - 1)) && @@ -512,12 +646,13 @@ pnode_printrefsect(struct parse *p, struct pnode *pn) if (NODE_TITLE == pp->node) break; + fputs(".Sh ", stdout); + if (NULL != pp) { - fputs(".Sh ", stdout); pnode_printmacroline(p, pp); pnode_unlink(pp); } else - puts(".Sh UNKNOWN"); + puts("UNKNOWN"); } static void @@ -533,16 +668,16 @@ pnode_printciterefentry(struct parse *p, struct pnode title = pp; fputs(".Xr ", stdout); + if (NULL != title) { pnode_printmacrolinepart(p, title); - pnode_unlink(title); + putchar(' '); } else - fputs("unknown", stdout); - putchar(' '); - if (NULL != manvol) { + fputs("unknown ", stdout); + + if (NULL != manvol) pnode_printmacroline(p, manvol); - pnode_unlink(manvol); - } else + else puts("1"); } @@ -558,24 +693,118 @@ pnode_printrefmeta(struct parse *p, struct pnode *pn) else if (NODE_REFENTRYTITLE == pp->node) title = pp; - puts(".Dd $Mdocdate: March 28 2014 $"); + puts(".Dd $Mdocdate" "$"); fputs(".Dt ", stdout); if (NULL != title) { pnode_printmacrolinepart(p, title); - pnode_unlink(title); + putchar(' '); } else - fputs("UNKNOWN", stdout); - putchar(' '); - if (NULL != manvol) { + fputs("UNKNOWN ", stdout); + + if (NULL != manvol) pnode_printmacroline(p, manvol); - pnode_unlink(manvol); - } else + else puts("1"); puts(".Os"); } +static void +pnode_printfuncdef(struct parse *p, struct pnode *pn) +{ + struct pnode *pp, *ftype, *func; + + ftype = func = NULL; + TAILQ_FOREACH(pp, &pn->childq, child) + if (NODE_TEXT == pp->node) + ftype = pp; + else if (NODE_FUNCTION == pp->node) + func = pp; + + if (NULL != ftype) { + fputs(".Ft ", stdout); + pnode_printmacroline(p, ftype); + } + + if (NULL != func) { + fputs(".Fo ", stdout); + pnode_printmacroline(p, func); + } else + puts(".Fo UNKNOWN"); +} + +static void +pnode_printparamdef(struct parse *p, struct pnode *pn) +{ + struct pnode *pp, *ptype, *param; + + ptype = param = NULL; + TAILQ_FOREACH(pp, &pn->childq, child) + if (NODE_TEXT == pp->node) + ptype = pp; + else if (NODE_PARAMETER == pp->node) + param = pp; + + fputs(".Fa \"", stdout); + if (NULL != ptype) { + pnode_printmacrolinepart(p, ptype); + putchar(' '); + } + + if (NULL != param) + pnode_printmacrolinepart(p, param); + else + fputs("UNKNOWN", stdout); + + puts("\""); +} + +static void +pnode_printfuncprototype(struct parse *p, struct pnode *pn) +{ + struct pnode *pp, *fdef; + + TAILQ_FOREACH(fdef, &pn->childq, child) + if (NODE_FUNCDEF == fdef->node) + break; + + if (NULL != fdef) + pnode_printfuncdef(p, fdef); + else + puts(".Fo UNKNOWN"); + + TAILQ_FOREACH(pp, &pn->childq, child) + if (NODE_PARAMDEF == pp->node) + pnode_printparamdef(p, pp); + + puts(".Fc"); +} + +/* TODO: handle "optional" values. */ +static void +pnode_printarg(struct parse *p, struct pnode *pn, int nested) +{ + struct pnode *pp; + int sv = nested; + + if ( ! nested) + fputs(".", stdout); + nested = 1; + TAILQ_FOREACH(pp, &pn->childq, child) + if (NODE_OPTION == pp->node) { + fputs("Fl ", stdout); + pnode_printmacrolinepart(p, pp); + } else if (NODE_TEXT == pp->node) { + fputs("Ar ", stdout); + pnode_printmacrolinepart(p, pp); + } else if (NODE_ARG == pp->node) + pnode_printarg(p, pp, nested); + + if ( ! sv) + puts(""); +} + /* * Print a parsed node (or ignore it--whatever). * This is a recursive function. @@ -595,30 +824,59 @@ pnode_print(struct parse *p, struct pnode *pn) printf(".\\\" %s\n", nodes[pn->node].name); switch (pn->node) { + case (NODE_ARG): + pnode_printarg(p, pn, 0); + pnode_unlinksub(pn); + break; case (NODE_CITEREFENTRY): pnode_printciterefentry(p, pn); + pnode_unlinksub(pn); break; case (NODE_CODE): fputs(".Li ", stdout); pnode_printmacroline(p, pn); + pnode_unlinksub(pn); break; + case (NODE_COMMAND): + fputs(".Nm ", stdout); + pnode_printmacroline(p, pn); + pnode_unlinksub(pn); + break; + case (NODE_FUNCTION): + fputs(".Fn ", stdout); + pnode_printmacroline(p, pn); + pnode_unlinksub(pn); + break; + case (NODE_FUNCPROTOTYPE): + pnode_printfuncprototype(p, pn); + pnode_unlinksub(pn); + break; case (NODE_FUNCSYNOPSISINFO): fputs(".Fd ", stdout); pnode_printmacroline(p, pn); + pnode_unlinksub(pn); break; case (NODE_PARA): /* FIXME: not always. */ puts(".Pp"); break; + case (NODE_PARAMETER): + fputs(".Fa \"", stdout); + pnode_printmacrolinepart(p, pn); + puts("\""); + pnode_unlinksub(pn); + break; case (NODE_PROGRAMLISTING): puts(".Bd -literal"); break; case (NODE_REFMETA): pnode_printrefmeta(p, pn); + pnode_unlinksub(pn); break; case (NODE_REFNAME): fputs(".Nm ", stdout); pnode_printmacroline(p, pn); + pnode_unlinksub(pn); return; case (NODE_REFNAMEDIV): puts(".Sh NAME"); @@ -626,6 +884,7 @@ pnode_print(struct parse *p, struct pnode *pn) case (NODE_REFPURPOSE): fputs(".Nd ", stdout); pnode_printmacroline(p, pn); + pnode_unlinksub(pn); return; case (NODE_REFSYNOPSISDIV): puts(".Sh SYNOPSIS");