=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.43 retrieving revision 1.62 diff -u -p -r1.43 -r1.62 --- docbook2mdoc/docbook2mdoc.c 2014/10/19 19:11:29 1.43 +++ docbook2mdoc/docbook2mdoc.c 2019/03/22 17:42:53 1.62 @@ -1,6 +1,7 @@ -/* $Id: docbook2mdoc.c,v 1.43 2014/10/19 19:11:29 kristaps Exp $ */ +/* $Id: docbook2mdoc.c,v 1.62 2019/03/22 17:42:53 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons + * Copyright (c) 2019 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -41,16 +42,16 @@ struct parse { unsigned int flags; /* document-wide flags */ struct pnode *root; /* root of parse tree */ struct pnode *cur; /* current node in tree */ - char *b; /* nil-terminated buffer for pre-print */ + char *b; /* NUL-terminated buffer for pre-print */ size_t bsz; /* current length of b */ size_t mbsz; /* max bsz allocation */ + int level; /* header level, starting at 1 */ int newln; /* output: are we on a fresh line */ }; struct node { const char *name; /* docbook element name */ - unsigned int flags; -#define NODE_IGNTEXT 1 /* ignore all contained text */ + enum nodeid node; /* docbook element to generate */ }; TAILQ_HEAD(pnodeq, pnode); @@ -76,13 +77,16 @@ struct pnode { static const char *attrkeys[ATTRKEY__MAX] = { "choice", + "class", "close", "id", + "linkend", "open", "rep" }; static const char *attrvals[ATTRVAL__MAX] = { + "monospaced", "norepeat", "opt", "plain", @@ -90,103 +94,136 @@ static const char *attrvals[ATTRVAL__MAX] = { "req" }; -static const struct node nodes[NODE__MAX] = { - { NULL, 0 }, - { "acronym", 0 }, - { "anchor", NODE_IGNTEXT }, - { "application", 0 }, - { "arg", 0 }, - { "caution", NODE_IGNTEXT }, - { "citerefentry", NODE_IGNTEXT }, - { "cmdsynopsis", NODE_IGNTEXT }, - { "code", 0 }, - { "colspec", NODE_IGNTEXT }, - { "command", 0 }, - { "constant", 0 }, - { "copyright", NODE_IGNTEXT }, - { "date", 0 }, - { "emphasis", 0 }, - { "entry", 0 }, - { "envar", 0 }, - { "fieldsynopsis", NODE_IGNTEXT }, - { "filename", 0 }, - { "funcdef", 0 }, - { "funcprototype", NODE_IGNTEXT }, - { "funcsynopsis", NODE_IGNTEXT }, - { "funcsynopsisinfo", 0 }, - { "function", 0 }, - { "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 }, - { "para", 0 }, - { "paramdef", 0 }, - { "parameter", 0 }, - { "programlisting", 0 }, - { "prompt", 0 }, - { "quote", 0 }, - { "refclass", NODE_IGNTEXT }, - { "refdescriptor", NODE_IGNTEXT }, - { "refentry", NODE_IGNTEXT }, - { "refentryinfo", NODE_IGNTEXT }, - { "refentrytitle", 0 }, - { "refmeta", NODE_IGNTEXT }, - { "refmetainfo", NODE_IGNTEXT }, - { "refmiscinfo", NODE_IGNTEXT }, - { "refname", 0 }, - { "refnamediv", NODE_IGNTEXT }, - { "refpurpose", 0 }, - { "refsect1", NODE_IGNTEXT }, - { "refsect2", NODE_IGNTEXT }, - { "refsect3", NODE_IGNTEXT }, - { "refsection", NODE_IGNTEXT }, - { "refsynopsisdiv", NODE_IGNTEXT }, - { "replaceable", 0 }, - { "row", NODE_IGNTEXT }, - { "sbr", NODE_IGNTEXT }, - { "screen", NODE_IGNTEXT }, - { "sgmltag", 0 }, - { "structname", 0 }, - { "synopsis", 0 }, - { "table", NODE_IGNTEXT }, - { "tbody", NODE_IGNTEXT }, - { "term", 0 }, - { NULL, 0 }, - { "tfoot", NODE_IGNTEXT }, - { "tgroup", NODE_IGNTEXT }, - { "thead", NODE_IGNTEXT }, - { "tip", NODE_IGNTEXT }, - { "title", 0 }, - { "trademark", 0 }, - { "type", 0 }, - { "ulink", 0 }, - { "userinput", 0 }, - { "variablelist", NODE_IGNTEXT }, - { "varlistentry", NODE_IGNTEXT }, - { "varname", 0 }, - { "warning", NODE_IGNTEXT }, - { "wordasword", 0 }, - { "year", NODE_IGNTEXT }, +static const struct node nodes[] = { + { "acronym", NODE_ACRONYM }, + { "affiliation", NODE_AFFILIATION }, + { "anchor", NODE_ANCHOR }, + { "application", NODE_APPLICATION }, + { "arg", NODE_ARG }, + { "author", NODE_AUTHOR }, + { "authorgroup", NODE_AUTHORGROUP }, + { "blockquote", NODE_BLOCKQUOTE }, + { "book", NODE_BOOK }, + { "bookinfo", NODE_BOOKINFO }, + { "caution", NODE_CAUTION }, + { "chapter", NODE_SECTION }, + { "citerefentry", NODE_CITEREFENTRY }, + { "citetitle", NODE_CITETITLE }, + { "cmdsynopsis", NODE_CMDSYNOPSIS }, + { "code", NODE_CODE }, + { "colspec", NODE_COLSPEC }, + { "command", NODE_COMMAND }, + { "constant", NODE_CONSTANT }, + { "copyright", NODE_COPYRIGHT }, + { "date", NODE_DATE }, + { "editor", NODE_EDITOR }, + { "emphasis", NODE_EMPHASIS }, + { "entry", NODE_ENTRY }, + { "envar", NODE_ENVAR }, + { "fieldsynopsis", NODE_FIELDSYNOPSIS }, + { "filename", NODE_FILENAME }, + { "firstname", NODE_FIRSTNAME }, + { "firstterm", NODE_FIRSTTERM }, + { "footnote", NODE_FOOTNOTE }, + { "funcdef", NODE_FUNCDEF }, + { "funcprototype", NODE_FUNCPROTOTYPE }, + { "funcsynopsis", NODE_FUNCSYNOPSIS }, + { "funcsynopsisinfo", NODE_FUNCSYNOPSISINFO }, + { "function", NODE_FUNCTION }, + { "glossterm", NODE_GLOSSTERM }, + { "group", NODE_GROUP }, + { "holder", NODE_HOLDER }, + { "index", NODE_INDEX }, + { "indexterm", NODE_INDEXTERM }, + { "info", NODE_INFO }, + { "informalequation", NODE_INFORMALEQUATION }, + { "informaltable", NODE_INFORMALTABLE }, + { "inlineequation", NODE_INLINEEQUATION }, + { "itemizedlist", NODE_ITEMIZEDLIST }, + { "keysym", NODE_KEYSYM }, + { "legalnotice", NODE_LEGALNOTICE }, + { "link", NODE_LINK }, + { "listitem", NODE_LISTITEM }, + { "literal", NODE_LITERAL }, + { "literallayout", NODE_LITERALLAYOUT }, + { "manvolnum", NODE_MANVOLNUM }, + { "member", NODE_MEMBER }, + { "mml:math", NODE_MML_MATH }, + { "mml:mfenced", NODE_MML_MFENCED }, + { "mml:mfrac", NODE_MML_MFRAC }, + { "mml:mi", NODE_MML_MI }, + { "mml:mn", NODE_MML_MN }, + { "mml:mo", NODE_MML_MO }, + { "mml:mrow", NODE_MML_MROW }, + { "mml:msub", NODE_MML_MSUB }, + { "mml:msup", NODE_MML_MSUP }, + { "modifier", NODE_MODIFIER }, + { "note", NODE_NOTE }, + { "option", NODE_OPTION }, + { "orderedlist", NODE_ORDEREDLIST }, + { "orgname", NODE_ORGNAME }, + { "othername", NODE_OTHERNAME }, + { "para", NODE_PARA }, + { "paramdef", NODE_PARAMDEF }, + { "parameter", NODE_PARAMETER }, + { "part", NODE_SECTION }, + { "phrase", NODE_PHRASE }, + { "preface", NODE_PREFACE }, + { "primary", NODE_PRIMARY }, + { "programlisting", NODE_PROGRAMLISTING }, + { "prompt", NODE_PROMPT }, + { "quote", NODE_QUOTE }, + { "refclass", NODE_REFCLASS }, + { "refdescriptor", NODE_REFDESCRIPTOR }, + { "refentry", NODE_REFENTRY }, + { "refentryinfo", NODE_REFENTRYINFO }, + { "refentrytitle", NODE_REFENTRYTITLE }, + { "refmeta", NODE_REFMETA }, + { "refmetainfo", NODE_REFMETAINFO }, + { "refmiscinfo", NODE_REFMISCINFO }, + { "refname", NODE_REFNAME }, + { "refnamediv", NODE_REFNAMEDIV }, + { "refpurpose", NODE_REFPURPOSE }, + { "refsect1", NODE_SECTION }, + { "refsect2", NODE_SECTION }, + { "refsect3", NODE_SECTION }, + { "refsection", NODE_SECTION }, + { "refsynopsisdiv", NODE_REFSYNOPSISDIV }, + { "releaseinfo", NODE_RELEASEINFO }, + { "replaceable", NODE_REPLACEABLE }, + { "row", NODE_ROW }, + { "sbr", NODE_SBR }, + { "screen", NODE_SCREEN }, + { "secondary", NODE_SECONDARY }, + { "sect1", NODE_SECTION }, + { "sect2", NODE_SECTION }, + { "section", NODE_SECTION }, + { "sgmltag", NODE_SGMLTAG }, + { "simplelist", NODE_SIMPLELIST }, + { "spanspec", NODE_SPANSPEC }, + { "structname", NODE_STRUCTNAME }, + { "subtitle", NODE_SUBTITLE }, + { "surname", NODE_SURNAME }, + { "synopsis", NODE_SYNOPSIS }, + { "table", NODE_TABLE }, + { "tbody", NODE_TBODY }, + { "term", NODE_TERM }, + { "tfoot", NODE_TFOOT }, + { "tgroup", NODE_TGROUP }, + { "thead", NODE_THEAD }, + { "tip", NODE_TIP }, + { "title", NODE_TITLE }, + { "trademark", NODE_TRADEMARK }, + { "type", NODE_TYPE }, + { "ulink", NODE_ULINK }, + { "userinput", NODE_USERINPUT }, + { "variablelist", NODE_VARIABLELIST }, + { "varlistentry", NODE_VARLISTENTRY }, + { "varname", NODE_VARNAME }, + { "warning", NODE_WARNING }, + { "wordasword", NODE_WORDASWORD }, + { "year", NODE_YEAR }, + { NULL, NODE__MAX } }; static int warn = 0; @@ -211,10 +248,7 @@ xml_char(void *arg, const XML_Char *p, int sz) if (ps->stop || NODE_ROOT == ps->node) return; - /* Not supposed to be collecting text. */ assert(NULL != ps->cur); - if (NODE_IGNTEXT & nodes[ps->node].flags) - return; /* * Are we in the midst of processing text? @@ -225,7 +259,7 @@ xml_char(void *arg, const XML_Char *p, int sz) */ if (NODE_TEXT != ps->node) { for (i = 0; i < sz; i++) - if ( ! isspace((int)p[i])) + if ( ! isspace((unsigned char)p[i])) break; if (i == sz) return; @@ -248,7 +282,7 @@ xml_char(void *arg, const XML_Char *p, int sz) /* Append to current buffer. */ assert(sz >= 0); - ps->cur->b = realloc(ps->cur->b, + ps->cur->b = realloc(ps->cur->b, ps->cur->bsz + (size_t)sz); if (NULL == ps->cur->b) { perror(NULL); @@ -265,7 +299,7 @@ pnode_trim(struct pnode *pn) assert(NODE_TEXT == pn->node); for ( ; pn->bsz > 0; pn->bsz--) - if ( ! isspace((int)pn->b[pn->bsz - 1])) + if ( ! isspace((unsigned char)pn->b[pn->bsz - 1])) break; } @@ -287,7 +321,7 @@ static void xml_elem_start(void *arg, const XML_Char *name, const XML_Char **atts) { struct parse *ps = arg; - enum nodeid node; + const struct node *node; enum attrkey key; enum attrval val; struct pnode *dat; @@ -307,16 +341,14 @@ xml_elem_start(void *arg, const XML_Char *name, const ps->node = ps->cur->node; } - for (node = 0; node < NODE__MAX; node++) - if (NULL == nodes[node].name) - continue; - else if (0 == strcmp(nodes[node].name, name)) + for (node = nodes; NULL != node->name; node++) + if (0 == strcmp(node->name, name)) break; - if (NODE__MAX == node && NODE_ROOT == ps->node) { - return; - } else if (NODE__MAX == node) { - fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n", + if (NULL == node->name) { + if (NODE_ROOT == ps->node) + return; + fprintf(stderr, "%s:%zu:%zu: unknown node \"%s\"\n", ps->fname, XML_GetCurrentLineNumber(ps->xml), XML_GetCurrentColumnNumber(ps->xml), name); ps->stop = 1; @@ -327,22 +359,9 @@ xml_elem_start(void *arg, const XML_Char *name, const XML_GetCurrentColumnNumber(ps->xml)); ps->stop = 1; return; - } else if (NODE_ROOT == ps->node && NODE_REFENTRY != node) { - return; - } else if ( ! isparent(node, ps->node)) { - fprintf(stderr, "%s:%zu:%zu: bad parent \"%s\" " - "of node \"%s\"\n", - ps->fname, XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(ps->xml), - NULL == nodes[ps->node].name ? - "(none)" : nodes[ps->node].name, - NULL == nodes[node].name ? - "(none)" : nodes[node].name); - ps->stop = 1; - return; } - if (NODE_INLINEEQUATION == node) + if (NODE_INLINEEQUATION == node->node) ps->flags |= PARSE_EQN; if (NULL == (dat = calloc(1, sizeof(struct pnode)))) { @@ -350,7 +369,7 @@ xml_elem_start(void *arg, const XML_Char *name, const exit(EXIT_FAILURE); } - dat->node = ps->node = node; + dat->node = ps->node = node->node; dat->parent = ps->cur; TAILQ_INIT(&dat->childq); TAILQ_INIT(&dat->attrq); @@ -370,37 +389,18 @@ xml_elem_start(void *arg, const XML_Char *name, const if (0 == strcmp(*att, attrkeys[key])) break; if (ATTRKEY__MAX == key) { - 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)) { if (warn) fprintf(stderr, "%s:%zu:%zu: warning: " - "bad attribute \"%s\"\n", - ps->fname, + "unknown attribute \"%s\"\n", + ps->fname, XML_GetCurrentLineNumber(ps->xml), - XML_GetCurrentColumnNumber(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)) { - 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)); pattr->key = key; pattr->val = val; @@ -493,6 +493,37 @@ pnode_unlinksub(struct pnode *pn) } /* + * Retrieve an enumeration attribute from a node. + * Return ATTRVAL__MAX if the node has no such attribute. + */ +enum attrval +pnode_getattr(struct pnode *pn, enum attrkey key) +{ + struct pattr *ap; + + TAILQ_FOREACH(ap, &pn->attrq, child) + if (ap->key == key) + return ap->val; + return(ATTRVAL__MAX); +} + +/* + * Retrieve an attribute string from a node. + * Return defval if the node has no such attribute. + */ +const char * +pnode_getattr_raw(struct pnode *pn, enum attrkey key, const char *defval) +{ + struct pattr *ap; + + TAILQ_FOREACH(ap, &pn->attrq, child) + if (ap->key == key) + return ATTRVAL__MAX == ap->val ? ap->rawval : + attrvals[ap->val]; + return(defval); +} + +/* * Reset the lookaside buffer. */ static void @@ -505,7 +536,7 @@ bufclear(struct parse *p) /* * Append NODE_TEXT contents to the current buffer, reallocating its * size if necessary. - * The buffer is ALWAYS nil-terminated. + * The buffer is ALWAYS NUL-terminated. */ static void bufappend(struct parse *p, struct pnode *pn) @@ -541,7 +572,7 @@ bufappend_r(struct parse *p, struct pnode *pn) bufappend_r(p, pp); } -/* +/* * Recursively search and return the first instance of "node". */ static struct pnode * @@ -580,27 +611,27 @@ pnode_printmacrolinetext(struct parse *p, struct pnode /* Convert all space to spaces. */ for (cp = p->b; '\0' != *cp; cp++) - if (isspace((int)*cp)) + if (isspace((unsigned char)*cp)) *cp = ' '; - for (cp = p->b; isspace((int)*cp); cp++) + for (cp = p->b; isspace((unsigned char)*cp); cp++) /* Spin past whitespace (XXX: necessary?) */ ; for ( ; '\0' != *cp; cp++) { /* Escape us if we look like a macro. */ if ((cp == p->b || ' ' == *(cp - 1)) && - isupper((int)*cp) && - '\0' != *(cp + 1) && - islower((int)*(cp + 1)) && - ('\0' == *(cp + 2) || + isupper((unsigned char)*cp) && + '\0' != *(cp + 1) && + islower((unsigned char)*(cp + 1)) && + ('\0' == *(cp + 2) || ' ' == *(cp + 2) || - (islower((int)*(cp + 2)) && - ('\0' == *(cp + 3) || + (islower((unsigned char)*(cp + 2)) && + ('\0' == *(cp + 3) || ' ' == *(cp + 3))))) fputs("\\&", stdout); if (MACROLINE_UPPER & fl) - putchar(toupper((int)*cp)); + putchar(toupper((unsigned char)*cp)); else - putchar((int)*cp); + putchar(*cp); /* If we're a character escape, escape us. */ if ('\\' == *cp) putchar('e'); @@ -657,7 +688,7 @@ static void pnode_printmclosepunct(struct parse *p, struct pnode *pn, int sv) { /* We wouldn't have done anything anyway. */ - if ( ! (sv && ! p->newln)) + if ( ! (sv && ! p->newln)) return; /* No next node or it's not text. */ @@ -667,22 +698,47 @@ pnode_printmclosepunct(struct parse *p, struct pnode * } else if (NODE_TEXT != pn->node) { pnode_printmclose(p, sv); return; - } + } /* Only do this for the comma/period. */ if (pn->bsz > 0 && (',' == pn->b[0] || '.' == pn->b[0]) && - (1 == pn->bsz || isspace((int)pn->b[1]))) { + (1 == pn->bsz || isspace((unsigned char)pn->b[1]))) { putchar(' '); putchar(pn->b[0]); pn->b++; pn->bsz--; - } + } putchar('\n'); p->newln = 1; } +static void +pnode_printpara(struct parse *p, struct pnode *pn) +{ + struct pnode *pp; + + assert(p->newln); + if ((pp = TAILQ_PREV(pn, pnodeq, child)) == NULL && + (pp = pn->parent) == NULL) + return; + + switch (pp->node) { + case NODE_ENTRY: + case NODE_LISTITEM: + return; + case NODE_PREFACE: + case NODE_SECTION: + if (p->level < 3) + return; + break; + default: + break; + } + puts(".Pp"); +} + /* * If the SYNOPSIS macro has a superfluous title, kill it. */ @@ -691,7 +747,7 @@ pnode_printrefsynopsisdiv(struct parse *p, struct pnod { struct pnode *pp; - TAILQ_FOREACH(pp, &pn->childq, child) + TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TITLE == pp->node) { pnode_unlink(pp); return; @@ -705,53 +761,74 @@ static void pnode_printrefsect(struct parse *p, struct pnode *pn) { struct pnode *pp; + const char *title; + int flags, level; + if (NULL == pn->parent) + return; + + level = ++p->level; + flags = 1 == level ? MACROLINE_UPPER : 0; + if (3 > level) { + switch (pn->node) { + case NODE_CAUTION: + case NODE_NOTE: + case NODE_TIP: + case NODE_WARNING: + level = 3; + break; + default: + break; + } + } + TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TITLE == pp->node) break; - switch (pn->node) { - case (NODE_REFSECT1): + if (NULL == pp) { + switch (pn->node) { + case NODE_PREFACE: + title = "Preface"; + break; + case NODE_CAUTION: + title = "Caution"; + break; + case NODE_NOTE: + title = "Note"; + break; + case NODE_TIP: + title = "Tip"; + break; + case NODE_WARNING: + title = "Warning"; + break; + default: + title = "Unknown"; + break; + } + } + + switch (level) { + case 1: fputs(".Sh", stdout); break; - case (NODE_REFSECT2): + case 2: fputs(".Ss", stdout); break; - case (NODE_REFSECT3): - puts(".Pp"); + default: + pnode_printpara(p, pn); fputs(".Sy", stdout); break; - case (NODE_NOTE): - /* FALLTHROUGH */ - case (NODE_REFSECTION): - /* FALLTHROUGH */ - case (NODE_TIP): - /* FALLTHROUGH */ - case (NODE_CAUTION): - /* FALLTHROUGH */ - case (NODE_WARNING): - puts(".Pp"); - if (NULL == pp) - return; - fputs(".Em", stdout); - break; - default: - break; } - p->newln = 0; - if (NULL != pp) { - pnode_printmacrolinetext(p, pp, - NODE_REFSECT1 == pn->node ? - MACROLINE_UPPER : 0); + p->newln = 0; + pnode_printmacrolinetext(p, pp, flags); pnode_printmclose(p, 1); pnode_unlink(pp); - } else { - puts(NODE_REFSECT1 == pn->node ? - "UNKNOWN" : "unknown"); - p->newln = 1; - } + } else + printf(" %s\n", title); } /* @@ -794,7 +871,6 @@ pnode_printrefmeta(struct parse *p, struct pnode *pn) else if (NODE_REFENTRYTITLE == pp->node) title = pp; - puts(".Dd $Mdocdate" "$"); fputs(".Dt", stdout); p->newln = 0; @@ -808,8 +884,6 @@ pnode_printrefmeta(struct parse *p, struct pnode *pn) p->newln = 1; } else pnode_printmacroline(p, manvol); - - puts(".Os"); } static void @@ -878,15 +952,8 @@ 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 ( "); + printf("left %s ", pnode_getattr_raw(pn, ATTRKEY_OPEN, "(")); pp = TAILQ_FIRST(&pn->childq); pnode_print(p, pp); @@ -896,13 +963,7 @@ pnode_printmathfenced(struct parse *p, struct pnode *p 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 ) "); + printf("right %s ", pnode_getattr_raw(pn, ATTRKEY_CLOSE, ")")); } /* @@ -920,13 +981,13 @@ pnode_printmath(struct parse *p, struct pnode *pn) pnode_print(p, pp); switch (pn->node) { - case (NODE_MML_MSUP): + case NODE_MML_MSUP: fputs(" sup ", stdout); break; - case (NODE_MML_MFRAC): + case NODE_MML_MFRAC: fputs(" over ", stdout); break; - case (NODE_MML_MSUB): + case NODE_MML_MSUB: fputs(" sub ", stdout); break; default: @@ -944,7 +1005,7 @@ pnode_printfuncprototype(struct parse *p, struct pnode assert(p->newln); TAILQ_FOREACH(fdef, &pn->childq, child) - if (NODE_FUNCDEF == fdef->node) + if (NODE_FUNCDEF == fdef->node) break; if (NULL != fdef) @@ -952,7 +1013,7 @@ pnode_printfuncprototype(struct parse *p, struct pnode else puts(".Fo UNKNOWN"); - TAILQ_FOREACH(pp, &pn->childq, child) + TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_PARAMDEF == pp->node) pnode_printparamdef(p, pp); @@ -960,7 +1021,7 @@ pnode_printfuncprototype(struct parse *p, struct pnode p->newln = 1; } -/* +/* * The element is more complicated than it should be because text * nodes are treated like ".Ar foo", but non-text nodes need to be * re-sent into the printer (i.e., without the preceding ".Ar"). @@ -976,10 +1037,10 @@ pnode_printarg(struct parse *p, struct pnode *pn) isop = 1; isrep = 0; - TAILQ_FOREACH(ap, &pn->attrq, child) + TAILQ_FOREACH(ap, &pn->attrq, child) if (ATTRKEY_CHOICE == ap->key && (ATTRVAL_PLAIN == ap->val || - ATTRVAL_REQ == ap->val)) + ATTRVAL_REQ == ap->val)) isop = 0; else if (ATTRKEY_REP == ap->key && (ATTRVAL_REPEAT == ap->val)) @@ -994,9 +1055,9 @@ pnode_printarg(struct parse *p, struct pnode *pn) if (NODE_TEXT == pp->node) { pnode_printmopen(p); fputs("Ar", stdout); - } + } pnode_print(p, pp); - if (NODE_TEXT == pp->node && isrep) + if (NODE_TEXT == pp->node && isrep) fputs("...", stdout); } } @@ -1009,7 +1070,7 @@ pnode_printgroup(struct parse *p, struct pnode *pn) int isop, sv; isop = 1; - TAILQ_FOREACH(ap, &pn->attrq, child) + TAILQ_FOREACH(ap, &pn->attrq, child) if (ATTRKEY_CHOICE == ap->key && (ATTRVAL_PLAIN == ap->val || ATTRVAL_REQ == ap->val)) { @@ -1017,14 +1078,14 @@ pnode_printgroup(struct parse *p, struct pnode *pn) break; } - /* + /* * Make sure we're on a macro line. * This will prevent pnode_print() for putting us on a * subsequent line. */ sv = p->newln; pnode_printmopen(p); - if (isop) + if (isop) fputs("Op", stdout); else if (sv) fputs("No", stdout); @@ -1061,15 +1122,14 @@ pnode_printprologue(struct parse *p, struct pnode *pn) pp = NULL == p->root ? NULL : pnode_findfirst(p->root, NODE_REFMETA); + puts(".Dd $Mdocdate" "$"); if (NULL != pp) { pnode_printrefmeta(p, pp); pnode_unlink(pp); - } else { - puts(".\\\" Supplying bogus prologue..."); - puts(".Dd $Mdocdate" "$"); - puts(".Dt UNKNOWN 1"); - puts(".Os"); - } + } else + printf(".Dt %s 1\n", + pnode_getattr_raw(p->root, ATTRKEY_ID, "UNKNOWN")); + puts(".Os"); if (PARSE_EQN & p->flags) { puts(".EQ"); @@ -1131,7 +1191,7 @@ pnode_printtable(struct parse *p, struct pnode *pn) assert(p->newln); TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TITLE == pp->node) { - puts(".Pp"); + pnode_printpara(p, pp); pnode_print(p, pp); pnode_unlink(pp); } @@ -1155,7 +1215,7 @@ pnode_printlist(struct parse *p, struct pnode *pn) assert(p->newln); TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TITLE == pp->node) { - puts(".Pp"); + pnode_printpara(p, pp); pnode_print(p, pp); pnode_unlink(pp); } @@ -1164,7 +1224,7 @@ pnode_printlist(struct parse *p, struct pnode *pn) if (NODE_ORDEREDLIST == pn->node) puts(".Bl -enum"); else - puts(".Bl -item"); + puts(".Bl -bullet"); TAILQ_FOREACH(pp, &pn->childq, child) { assert(p->newln); @@ -1184,7 +1244,7 @@ pnode_printvariablelist(struct parse *p, struct pnode assert(p->newln); TAILQ_FOREACH(pp, &pn->childq, child) if (NODE_TITLE == pp->node) { - puts(".Pp"); + pnode_printpara(p, pp); pnode_print(p, pp); pnode_unlink(pp); } @@ -1214,6 +1274,7 @@ static void pnode_print(struct parse *p, struct pnode *pn) { struct pnode *pp; + const char *ccp; char *cp; int last, sv; @@ -1223,118 +1284,150 @@ pnode_print(struct parse *p, struct pnode *pn) sv = p->newln; switch (pn->node) { - case (NODE_APPLICATION): + case NODE_APPLICATION: pnode_printmopen(p); fputs("Nm", stdout); break; - case (NODE_ANCHOR): + case NODE_ANCHOR: /* Don't print anything! */ return; - case (NODE_ARG): + case NODE_ARG: pnode_printarg(p, pn); pnode_unlinksub(pn); break; - case (NODE_CITEREFENTRY): + case NODE_AUTHOR: pnode_printmopen(p); + fputs("An", stdout); + break; + case NODE_AUTHORGROUP: + assert(p->newln); + puts(".An -split"); + break; + case NODE_BOOKINFO: + assert(p->newln); + puts(".Sh NAME"); + break; + case NODE_CITEREFENTRY: + pnode_printmopen(p); fputs("Xr", stdout); pnode_printciterefentry(p, pn); pnode_unlinksub(pn); break; - case (NODE_CODE): + case NODE_CODE: pnode_printmopen(p); fputs("Li", stdout); break; - case (NODE_COMMAND): + case NODE_COMMAND: pnode_printmopen(p); fputs("Nm", stdout); break; - case (NODE_CONSTANT): + case NODE_CONSTANT: pnode_printmopen(p); fputs("Dv", stdout); break; - case (NODE_EMPHASIS): + case NODE_EDITOR: + puts("editor: "); pnode_printmopen(p); + fputs("An", stdout); + break; + case NODE_EMPHASIS: + case NODE_FIRSTTERM: + pnode_printmopen(p); fputs("Em", stdout); break; - case (NODE_ENVAR): + case NODE_ENVAR: pnode_printmopen(p); fputs("Ev", stdout); break; - case (NODE_FILENAME): + case NODE_FILENAME: pnode_printmopen(p); fputs("Pa", stdout); break; - case (NODE_FUNCTION): + case NODE_FUNCTION: pnode_printmopen(p); fputs("Fn", stdout); break; - case (NODE_FUNCPROTOTYPE): + case NODE_FUNCPROTOTYPE: assert(p->newln); pnode_printfuncprototype(p, pn); pnode_unlinksub(pn); break; - case (NODE_FUNCSYNOPSISINFO): + case NODE_FUNCSYNOPSISINFO: pnode_printmopen(p); fputs("Fd", stdout); break; - case (NODE_INFORMALEQUATION): + case NODE_INDEXTERM: + return; + case NODE_INFORMALEQUATION: if ( ! p->newln) putchar('\n'); puts(".EQ"); p->newln = 0; break; - case (NODE_INLINEEQUATION): + case NODE_INLINEEQUATION: fputc('$', stdout); p->newln = 0; break; - case (NODE_ITEMIZEDLIST): + case NODE_ITEMIZEDLIST: assert(p->newln); pnode_printlist(p, pn); pnode_unlinksub(pn); break; - case (NODE_GROUP): + case NODE_GROUP: pnode_printgroup(p, pn); pnode_unlinksub(pn); break; - case (NODE_LITERAL): + case NODE_LEGALNOTICE: + assert(p->newln); + puts(".Sh LEGAL NOTICE"); + break; + case NODE_LINK: + ccp = pnode_getattr_raw(pn, ATTRKEY_LINKEND, NULL); + if (NULL == ccp) + break; pnode_printmopen(p); + printf("Sx %s\n", ccp); + p->newln = 1; + return; + case NODE_LITERAL: + pnode_printmopen(p); fputs("Li", stdout); break; - case (NODE_MML_MFENCED): + case NODE_LITERALLAYOUT: + assert(p->newln); + puts(".Bd -literal"); + 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): + case NODE_MML_MROW: + case NODE_MML_MI: + case NODE_MML_MN: + case NODE_MML_MO: if (TAILQ_EMPTY(&pn->childq)) break; fputs(" { ", stdout); break; - case (NODE_MML_MFRAC): - case (NODE_MML_MSUB): - case (NODE_MML_MSUP): + case NODE_MML_MFRAC: + case NODE_MML_MSUB: + case NODE_MML_MSUP: pnode_printmath(p, pn); pnode_unlinksub(pn); break; - case (NODE_OPTION): + case NODE_OPTION: pnode_printmopen(p); fputs("Fl", stdout); break; - case (NODE_ORDEREDLIST): + case NODE_ORDEREDLIST: assert(p->newln); pnode_printlist(p, pn); pnode_unlinksub(pn); break; - case (NODE_PARA): - assert(p->newln); - if (NULL != pn->parent && - NODE_LISTITEM == pn->parent->node) - break; - puts(".Pp"); + case NODE_PARA: + pnode_printpara(p, pn); break; - case (NODE_PARAMETER): + case NODE_PARAMETER: /* Suppress non-text children... */ pnode_printmopen(p); fputs("Fa \"", stdout); @@ -1342,24 +1435,25 @@ pnode_print(struct parse *p, struct pnode *pn) fputs("\"", stdout); pnode_unlinksub(pn); break; - case (NODE_QUOTE): + case NODE_QUOTE: pnode_printmopen(p); fputs("Qo", stdout); break; - case (NODE_PROGRAMLISTING): - /* FALLTHROUGH */ - case (NODE_SCREEN): + case NODE_PROGRAMLISTING: + case NODE_SCREEN: assert(p->newln); - puts(".Bd -literal"); + printf(".Bd %s\n", ATTRVAL_MONOSPACED == + pnode_getattr(pn, ATTRKEY_CLASS) ? + "-literal" : "-unfilled"); break; - case (NODE_REFENTRYINFO): + case NODE_REFENTRYINFO: /* Suppress. */ pnode_unlinksub(pn); break; - case (NODE_REFMETA): + case NODE_REFMETA: abort(); break; - case (NODE_REFNAME): + case NODE_REFNAME: /* Suppress non-text children... */ pnode_printmopen(p); fputs("Nm", stdout); @@ -1367,62 +1461,52 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_printmacrolinepart(p, pn); pnode_unlinksub(pn); break; - case (NODE_REFNAMEDIV): + case NODE_REFNAMEDIV: assert(p->newln); puts(".Sh NAME"); break; - case (NODE_REFPURPOSE): + case NODE_REFPURPOSE: assert(p->newln); pnode_printmopen(p); fputs("Nd", stdout); break; - case (NODE_REFSYNOPSISDIV): + case NODE_REFSYNOPSISDIV: assert(p->newln); pnode_printrefsynopsisdiv(p, pn); puts(".Sh SYNOPSIS"); break; - case (NODE_REFSECT1): - /* FALLTHROUGH */ - case (NODE_REFSECT2): - /* FALLTHROUGH */ - case (NODE_REFSECT3): - /* FALLTHROUGH */ - case (NODE_REFSECTION): - /* FALLTHROUGH */ - case (NODE_NOTE): - /* FALLTHROUGH */ - case (NODE_TIP): - /* FALLTHROUGH */ - case (NODE_CAUTION): - /* FALLTHROUGH */ - case (NODE_WARNING): + case NODE_PREFACE: + case NODE_SECTION: + case NODE_NOTE: + case NODE_TIP: + case NODE_CAUTION: + case NODE_WARNING: assert(p->newln); pnode_printrefsect(p, pn); break; - case (NODE_REPLACEABLE): + case NODE_REPLACEABLE: pnode_printmopen(p); fputs("Ar", stdout); break; - case (NODE_SBR): + case NODE_SBR: assert(p->newln); puts(".br"); break; - case (NODE_SGMLTAG): + case NODE_SGMLTAG: pnode_printmopen(p); fputs("Li", stdout); break; - case (NODE_STRUCTNAME): + case NODE_STRUCTNAME: pnode_printmopen(p); fputs("Vt", stdout); break; - case (NODE_TABLE): - /* FALLTHROUGH */ - case (NODE_INFORMALTABLE): + case NODE_TABLE: + case NODE_INFORMALTABLE: assert(p->newln); pnode_printtable(p, pn); pnode_unlinksub(pn); break; - case (NODE_TEXT): + case NODE_TEXT: if (0 == p->newln) putchar(' '); @@ -1436,7 +1520,7 @@ pnode_print(struct parse *p, struct pnode *pn) /* * Output all characters, squeezing out whitespace - * between newlines. + * between newlines. * XXX: all whitespace, including tabs (?). * Remember to escape control characters and escapes. */ @@ -1455,8 +1539,8 @@ pnode_print(struct parse *p, struct pnode *pn) for (last = '\n'; '\0' != *cp; ) { if ('\n' == last) { /* Consume all whitespace. */ - if (isspace((int)*cp)) { - while (isspace((int)*cp)) + if (isspace((unsigned char)*cp)) { + while (isspace((unsigned char)*cp)) cp++; continue; } else if ('\'' == *cp || '.' == *cp) @@ -1469,24 +1553,30 @@ pnode_print(struct parse *p, struct pnode *pn) } p->newln = 0; break; - case (NODE_TYPE): + case NODE_TITLE: + if (pn->parent->node == NODE_BOOKINFO) { + pnode_printmopen(p); + fputs("Nd", stdout); + } + break; + case NODE_TYPE: pnode_printmopen(p); fputs("Vt", stdout); break; - case (NODE_USERINPUT): + case NODE_USERINPUT: pnode_printmopen(p); fputs("Li", stdout); break; - case (NODE_VARIABLELIST): + case NODE_VARIABLELIST: assert(p->newln); pnode_printvariablelist(p, pn); pnode_unlinksub(pn); break; - case (NODE_VARLISTENTRY): + case NODE_VARLISTENTRY: assert(p->newln); pnode_printvarlistentry(p, pn); break; - case (NODE_VARNAME): + case NODE_VARNAME: pnode_printmopen(p); fputs("Va", stdout); break; @@ -1498,75 +1588,92 @@ pnode_print(struct parse *p, struct pnode *pn) pnode_print(p, pp); switch (pn->node) { - case (NODE_INFORMALEQUATION): + case NODE_INFORMALEQUATION: if ( ! p->newln) putchar('\n'); puts(".EN"); p->newln = 1; break; - case (NODE_INLINEEQUATION): + case NODE_INLINEEQUATION: fputs("$ ", stdout); p->newln = sv; break; - case (NODE_MML_MROW): - case (NODE_MML_MI): - case (NODE_MML_MN): - case (NODE_MML_MO): + case NODE_MML_MROW: + case NODE_MML_MI: + case NODE_MML_MN: + case NODE_MML_MO: if (TAILQ_EMPTY(&pn->childq)) break; fputs(" } ", stdout); break; - case (NODE_APPLICATION): - case (NODE_ARG): - case (NODE_CITEREFENTRY): - case (NODE_CODE): - case (NODE_COMMAND): - case (NODE_CONSTANT): - case (NODE_EMPHASIS): - case (NODE_ENVAR): - case (NODE_FILENAME): - case (NODE_FUNCTION): - case (NODE_FUNCSYNOPSISINFO): - case (NODE_LITERAL): - case (NODE_OPTION): - case (NODE_PARAMETER): - case (NODE_REPLACEABLE): - case (NODE_REFPURPOSE): - case (NODE_SGMLTAG): - case (NODE_STRUCTNAME): - case (NODE_TEXT): - case (NODE_TYPE): - case (NODE_USERINPUT): - case (NODE_VARNAME): + case NODE_APPLICATION: + case NODE_ARG: + case NODE_AUTHOR: + case NODE_CITEREFENTRY: + case NODE_CODE: + case NODE_COMMAND: + case NODE_CONSTANT: + case NODE_EDITOR: + case NODE_EMPHASIS: + case NODE_ENVAR: + case NODE_FILENAME: + case NODE_FIRSTTERM: + case NODE_FUNCTION: + case NODE_FUNCSYNOPSISINFO: + case NODE_LITERAL: + case NODE_OPTION: + case NODE_PARAMETER: + case NODE_REPLACEABLE: + case NODE_REFPURPOSE: + case NODE_SGMLTAG: + case NODE_STRUCTNAME: + case NODE_TEXT: + case NODE_TYPE: + case NODE_USERINPUT: + case NODE_VARNAME: pnode_printmclosepunct(p, pn, sv); break; - case (NODE_QUOTE): + case NODE_QUOTE: pnode_printmclose(p, sv); sv = p->newln; pnode_printmopen(p); fputs("Qc", stdout); pnode_printmclose(p, sv); break; - case (NODE_REFNAME): + case NODE_REFNAME: /* * If we're in the NAME macro and we have multiple * macros in sequence, then print out a * trailing comma before the newline. */ - if (NULL != pn->parent && + if (NULL != pn->parent && NODE_REFNAMEDIV == pn->parent->node && NULL != TAILQ_NEXT(pn, child) && - NODE_REFNAME == TAILQ_NEXT(pn, child)->node) + NODE_REFNAME == TAILQ_NEXT(pn, child)->node) fputs(" ,", stdout); pnode_printmclose(p, sv); break; - case (NODE_PROGRAMLISTING): - /* FALLTHROUGH */ - case (NODE_SCREEN): + case NODE_PREFACE: + case NODE_SECTION: + case NODE_NOTE: + case NODE_TIP: + case NODE_CAUTION: + case NODE_WARNING: + p->level--; + break; + case NODE_LITERALLAYOUT: + case NODE_PROGRAMLISTING: + case NODE_SCREEN: assert(p->newln); puts(".Ed"); p->newln = 1; break; + case NODE_TITLE: + if (pn->parent->node == NODE_BOOKINFO) { + pnode_printmclose(p, 1); + puts(".Sh AUTHORS"); + } + break; default: break; } @@ -1577,7 +1684,7 @@ pnode_print(struct parse *p, struct pnode *pn) * Invoke the parser context with each buffer fill. */ static int -readfile(XML_Parser xp, int fd, +readfile(XML_Parser xp, int fd, char *b, size_t bsz, const char *fn) { struct parse p; @@ -1598,12 +1705,12 @@ readfile(XML_Parser xp, int fd, if (0 == (rc = XML_Parse(xp, b, ssz, 0 == ssz))) fprintf(stderr, "%s:%zu:%zu: %s\n", fn, XML_GetCurrentLineNumber(xp), - XML_GetCurrentColumnNumber(xp), + XML_GetCurrentColumnNumber(xp), XML_ErrorString (XML_GetErrorCode(xp))); else if ( ! p.stop && ssz > 0) continue; - /* + /* * Exit when we've read all or errors have occured * during the parse sequence. */ @@ -1644,7 +1751,7 @@ main(int argc, char *argv[]) while (-1 != (ch = getopt(argc, argv, "W"))) switch (ch) { - case ('W'): + case 'W': warn = 1; break; default: @@ -1654,13 +1761,14 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - if (argc > 1) - return(EXIT_FAILURE); - else if (argc > 0) + if (argc > 1) { + fprintf(stderr, "%s: Too many arguments\n", argv[1]); + goto usage; + } else if (argc > 0) fname = argv[0]; /* Read from stdin or a file. */ - fd = 0 == strcmp(fname, "-") ? + fd = 0 == strcmp(fname, "-") ? STDIN_FILENO : open(fname, O_RDONLY, 0); /* @@ -1685,6 +1793,6 @@ main(int argc, char *argv[]) return(rc ? EXIT_SUCCESS : EXIT_FAILURE); usage: - fprintf(stderr, "usage: %s [-W]\n", progname); + fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname); return(EXIT_FAILURE); }