=================================================================== RCS file: /cvs/docbook2mdoc/docbook2mdoc.c,v retrieving revision 1.134 retrieving revision 1.143 diff -u -p -r1.134 -r1.143 --- docbook2mdoc/docbook2mdoc.c 2019/04/23 22:25:28 1.134 +++ docbook2mdoc/docbook2mdoc.c 2019/04/28 19:59:01 1.143 @@ -1,4 +1,4 @@ -/* $Id: docbook2mdoc.c,v 1.134 2019/04/23 22:25:28 schwarze Exp $ */ +/* $Id: docbook2mdoc.c,v 1.143 2019/04/28 19:59:01 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -21,6 +21,7 @@ #include #include +#include "xmalloc.h" #include "node.h" #include "macro.h" #include "format.h" @@ -30,7 +31,6 @@ */ static void pnode_print(struct format *, struct pnode *); -static void pnode_printrefentry(struct format *, struct pnode *); static void @@ -42,7 +42,8 @@ pnode_printtext(struct format *f, struct pnode *n) cp = n->b; accept_arg = f->flags & FMT_ARG; - if (f->linestate == LINE_MACRO && !n->spc && !accept_arg) { + if (f->linestate == LINE_MACRO && !accept_arg && + (n->flags & NFLAG_SPC) == 0) { for (;;) { if (*cp == '\0') return; @@ -71,14 +72,15 @@ pnode_printtext(struct format *f, struct pnode *n) */ if (f->linestate != LINE_MACRO && - (nn = TAILQ_NEXT(n, child)) != NULL && nn->spc == 0) { + (nn = TAILQ_NEXT(n, child)) != NULL && + (nn->flags & NFLAG_SPC) == 0) { switch (pnode_class(nn->node)) { case CLASS_LINE: case CLASS_ENCL: macro_open(f, "Pf"); accept_arg = 1; f->flags |= FMT_CHILD; - nn->spc = 1; + nn->flags |= NFLAG_SPC; break; default: break; @@ -89,18 +91,22 @@ pnode_printtext(struct format *f, struct pnode *n) case LINE_NEW: break; case LINE_TEXT: - if (n->spc) { - if (n->node == NODE_TEXT) + if (n->flags & NFLAG_SPC) { + if (n->flags & NFLAG_LINE && + pnode_class(n->node) == CLASS_TEXT) macro_close(f); else putchar(' '); } break; case LINE_MACRO: - if (accept_arg) - putchar(' '); - else + if (accept_arg == 0) macro_close(f); + else if (n->flags & NFLAG_SPC || + (f->flags & FMT_ARG) == 0 || + (nn = TAILQ_PREV(n, pnodeq, child)) == NULL || + pnode_class(nn->node) != CLASS_TEXT) + putchar(' '); break; } @@ -187,67 +193,35 @@ static void pnode_printsection(struct format *f, struct pnode *n) { struct pnode *nc, *ncc; - const char *title; int flags, level; - if (n->parent == NULL) { - pnode_printrefentry(f, n); + if (n->parent == NULL) return; - } level = ++f->level; flags = ARG_SPACE; switch (n->node) { - case NODE_PREFACE: case NODE_SECTION: case NODE_APPENDIX: if (level == 1) flags |= ARG_UPPER; break; case NODE_SIMPLESECT: - case NODE_LEGALNOTICE: if (level < 2) level = 2; break; - default: + case NODE_NOTE: if (level < 3) level = 3; break; + default: + abort(); } TAILQ_FOREACH(nc, &n->childq, child) if (nc->node == NODE_TITLE) break; - if (nc == NULL) { - switch (n->node) { - case NODE_PREFACE: - title = "Preface"; - break; - case NODE_APPENDIX: - title = "Appendix"; - break; - case NODE_LEGALNOTICE: - title = "Legal Notice"; - 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: macro_close(f); @@ -265,11 +239,7 @@ pnode_printsection(struct format *f, struct pnode *n) macro_open(f, "Sy"); break; } - - if (nc != NULL) - macro_addnode(f, nc, flags); - else - macro_addarg(f, title, flags | ARG_QUOTED); + macro_addnode(f, nc, flags); macro_close(f); /* @@ -722,208 +692,37 @@ pnode_printolink(struct format *f, struct pnode *n) static void pnode_printprologue(struct format *f, struct pnode *root) { - struct pnode *date, *refmeta, *name, *vol, *descr, *nc, *nn; - const char *sname; + struct pnode *name, *nc; - /* Collect information. */ + nc = TAILQ_FIRST(&root->childq); + assert(nc->node == NODE_DATE); + macro_nodeline(f, "Dd", nc, 0); + pnode_unlink(nc); - if ((date = pnode_takefirst(root, NODE_PUBDATE)) == NULL) - date = pnode_takefirst(root, NODE_DATE); - - name = vol = NULL; - if ((refmeta = pnode_findfirst(root, NODE_REFMETA)) != NULL) { - TAILQ_FOREACH_SAFE(nc, &refmeta->childq, child, nn) { - switch (nc->node) { - case NODE_REFENTRYTITLE: - name = nc; - break; - case NODE_MANVOLNUM: - vol = nc; - break; - default: - continue; - } - TAILQ_REMOVE(&refmeta->childq, nc, child); - } - } - - if (pnode_findfirst(root, NODE_REFNAMEDIV) == NULL && - ((nc = pnode_findfirst(root, NODE_BOOKINFO)) != NULL || - (nc = pnode_findfirst(root, NODE_REFENTRYINFO)) != NULL)) - descr = pnode_takefirst(nc, NODE_TITLE); - else - descr = NULL; - - /* Print prologue. */ - - if (date == NULL) - macro_line(f, "Dd $Mdocdate" "$"); - else - macro_nodeline(f, "Dd", date, 0); - macro_open(f, "Dt"); - if (name == NULL) { - sname = pnode_getattr_raw(root, ATTRKEY_ID, "UNKNOWN"); - macro_addarg(f, sname, ARG_SPACE | ARG_SINGLE | ARG_UPPER); - } else - macro_addnode(f, name, ARG_SPACE | ARG_SINGLE | ARG_UPPER); - if (vol == NULL) - macro_addarg(f, "1", ARG_SPACE); - else - macro_addnode(f, vol, ARG_SPACE | ARG_SINGLE); + name = TAILQ_FIRST(&root->childq); + assert(name->node == NODE_REFENTRYTITLE); + macro_addnode(f, name, ARG_SPACE | ARG_SINGLE | ARG_UPPER); + TAILQ_REMOVE(&root->childq, name, child); + name->parent = NULL; + nc = TAILQ_FIRST(&root->childq); + assert (nc->node == NODE_MANVOLNUM); + macro_addnode(f, nc, ARG_SPACE | ARG_SINGLE); + pnode_unlink(nc); macro_line(f, "Os"); - if (descr != NULL) { + nc = TAILQ_FIRST(&root->childq); + if (nc != NULL && nc->node == NODE_TITLE) { macro_line(f, "Sh NAME"); - if (name == NULL) - macro_argline(f, "Nm", sname); - else - macro_nodeline(f, "Nm", name, ARG_SINGLE); - macro_nodeline(f, "Nd", descr, 0); + macro_nodeline(f, "Nm", name, ARG_SINGLE); + macro_nodeline(f, "Nd", nc, 0); + pnode_unlink(nc); } - - /* Clean up. */ - - pnode_unlink(date); pnode_unlink(name); - pnode_unlink(vol); - pnode_unlink(descr); f->parastate = PARA_HAVE; } -static void -pnode_printrefentry(struct format *f, struct pnode *n) -{ - struct pnode *info, *meta, *nc, *title; - struct pnode *match, *later; - - /* Collect nodes that remained behind when writing the prologue. */ - - meta = NULL; - info = pnode_takefirst(n, NODE_BOOKINFO); - if (info != NULL && TAILQ_FIRST(&info->childq) == NULL) { - pnode_unlink(info); - info = NULL; - } - if (info == NULL) { - info = pnode_takefirst(n, NODE_REFENTRYINFO); - if (info != NULL && TAILQ_FIRST(&info->childq) == NULL) { - pnode_unlink(info); - info = NULL; - } - if (info == NULL) - info = pnode_takefirst(n, NODE_INFO); - meta = pnode_takefirst(n, NODE_REFMETA); - if (meta != NULL && TAILQ_FIRST(&meta->childq) == NULL) { - pnode_unlink(meta); - meta = NULL; - } - } - if (info == NULL && meta == NULL) - return; - - /* - * Find the best place to put this information. - * Use the last existing AUTHORS node, if any. - * Otherwise, put it behind all standard sections that - * conventionally precede AUTHORS, and also behind any - * non-standard sections that follow the last of these, - * but before the next standard section. - */ - - match = later = NULL; - TAILQ_FOREACH(nc, &n->childq, child) { - switch (nc->node) { - case NODE_REFENTRY: - case NODE_REFNAMEDIV: - case NODE_REFSYNOPSISDIV: - case NODE_PREFACE: - later = NULL; - continue; - case NODE_APPENDIX: - case NODE_INDEX: - if (later == NULL) - later = nc; - continue; - default: - break; - } - if ((title = pnode_findfirst(nc, NODE_TITLE)) == NULL || - (title = TAILQ_FIRST(&title->childq)) == NULL || - title->node != NODE_TEXT) - continue; - if (strcasecmp(title->b, "AUTHORS") == 0 || - strcasecmp(title->b, "AUTHOR") == 0) - match = nc; - else if (strcasecmp(title->b, "NAME") == 0 || - strcasecmp(title->b, "SYNOPSIS") == 0 || - strcasecmp(title->b, "DESCRIPTION") == 0 || - strcasecmp(title->b, "RETURN VALUES") == 0 || - strcasecmp(title->b, "ENVIRONMENT") == 0 || - strcasecmp(title->b, "FILES") == 0 || - strcasecmp(title->b, "EXIT STATUS") == 0 || - strcasecmp(title->b, "EXAMPLES") == 0 || - strcasecmp(title->b, "DIAGNOSTICS") == 0 || - strcasecmp(title->b, "ERRORS") == 0 || - strcasecmp(title->b, "SEE ALSO") == 0 || - strcasecmp(title->b, "STANDARDS") == 0 || - strcasecmp(title->b, "HISTORY") == 0) - later = NULL; - else if ((strcasecmp(title->b, "CAVEATS") == 0 || - strcasecmp(title->b, "BUGS") == 0) && - later == NULL) - later = nc; - } - - /* - * If no AUTHORS section was found, create one from scratch, - * and insert that at the place selected earlier. - */ - - if (match == NULL) { - if ((match = calloc(1, sizeof(*match))) == NULL) { - perror(NULL); - exit(1); - } - match->node = NODE_SECTION; - match->spc = 1; - match->parent = n; - TAILQ_INIT(&match->childq); - TAILQ_INIT(&match->attrq); - if ((nc = pnode_alloc(match)) == NULL) { - perror(NULL); - exit(1); - } - nc->node = NODE_TITLE; - nc->spc = 1; - if ((nc = pnode_alloc(nc)) == NULL) { - perror(NULL); - exit(1); - } - nc->node = NODE_TEXT; - if ((nc->b = strdup("AUTHORS")) == NULL) { - perror(NULL); - exit(1); - } - nc->spc = 1; - if (later == NULL) - TAILQ_INSERT_TAIL(&n->childq, match, child); - else - TAILQ_INSERT_BEFORE(later, match, child); - } - - /* - * Dump the stuff excised at the beginning - * into this AUTHORS section. - */ - - if (info != NULL) - TAILQ_INSERT_TAIL(&match->childq, info, child); - if (meta != NULL) - TAILQ_INSERT_TAIL(&match->childq, meta, child); -} - /* * We can have multiple elements within a , which * we should comma-separate as list headers. @@ -1037,6 +836,7 @@ pnode_printtgroup2(struct format *f, struct pnode *n) pnode_unlink(nr); } macro_line(f, "El"); + f->parastate = PARA_WANT; pnode_unlinksub(n); } @@ -1063,6 +863,7 @@ pnode_printtgroup(struct format *f, struct pnode *n) pnode_printrow(f, nc); } macro_line(f, "El"); + f->parastate = PARA_WANT; pnode_unlinksub(n); } @@ -1082,6 +883,7 @@ pnode_printlist(struct format *f, struct pnode *n) f->parastate = PARA_HAVE; } macro_line(f, "El"); + f->parastate = PARA_WANT; pnode_unlinksub(n); } @@ -1100,6 +902,7 @@ pnode_printvariablelist(struct format *f, struct pnode macro_nodeline(f, "It", nc, 0); } macro_line(f, "El"); + f->parastate = PARA_WANT; pnode_unlinksub(n); } @@ -1119,7 +922,7 @@ pnode_print(struct format *f, struct pnode *n) return; was_impl = f->flags & FMT_IMPL; - if (n->spc) + if (n->flags & NFLAG_SPC) f->flags &= ~FMT_NOSPC; else f->flags |= FMT_NOSPC; @@ -1284,8 +1087,8 @@ pnode_print(struct format *f, struct pnode *n) if ((nc = TAILQ_FIRST(&n->childq)) != NULL && nc->node == NODE_FILENAME && TAILQ_NEXT(nc, child) == NULL) { - if (n->spc) - nc->spc = 1; + if (n->flags & NFLAG_SPC) + nc->flags |= NFLAG_SPC; } else if (was_impl) macro_open(f, "Do"); else { @@ -1303,9 +1106,6 @@ pnode_print(struct format *f, struct pnode *n) case NODE_SYSTEMITEM: pnode_printsystemitem(f, n); break; - case NODE_REFENTRY: - pnode_printrefentry(f, n); - break; case NODE_REFNAME: /* More often, these appear inside NODE_REFNAMEDIV. */ macro_open(f, "Nm"); @@ -1319,15 +1119,10 @@ pnode_print(struct format *f, struct pnode *n) case NODE_REFSYNOPSISDIV: pnode_printrefsynopsisdiv(f, n); break; - case NODE_PREFACE: case NODE_SECTION: case NODE_SIMPLESECT: case NODE_APPENDIX: - case NODE_LEGALNOTICE: case NODE_NOTE: - case NODE_TIP: - case NODE_CAUTION: - case NODE_WARNING: pnode_printsection(f, n); break; case NODE_REPLACEABLE: @@ -1343,12 +1138,12 @@ pnode_print(struct format *f, struct pnode *n) else print_text(f, "_", 0); if ((nc = TAILQ_FIRST(&n->childq)) != NULL) - nc->spc = 0; + nc->flags &= ~(NFLAG_LINE | NFLAG_SPC); break; case NODE_SUPERSCRIPT: fputs("\\(ha", stdout); if ((nc = TAILQ_FIRST(&n->childq)) != NULL) - nc->spc = 0; + nc->flags &= ~(NFLAG_LINE | NFLAG_SPC); break; case NODE_TEXT: case NODE_ESCAPE: @@ -1379,6 +1174,12 @@ pnode_print(struct format *f, struct pnode *n) case NODE_XREF: pnode_printxref(f, n); break; + case NODE_CAUTION: + case NODE_LEGALNOTICE: + case NODE_PREFACE: + case NODE_TIP: + case NODE_WARNING: + abort(); default: break; } @@ -1463,15 +1264,12 @@ pnode_print(struct format *f, struct pnode *n) } else f->flags &= ~FMT_IMPL; break; - case NODE_PREFACE: case NODE_SECTION: + case NODE_SIMPLESECT: case NODE_APPENDIX: - case NODE_LEGALNOTICE: case NODE_NOTE: - case NODE_TIP: - case NODE_CAUTION: - case NODE_WARNING: - f->level--; + if (n->parent != NULL) + f->level--; break; case NODE_BLOCKQUOTE: case NODE_LITERALLAYOUT: @@ -1480,6 +1278,7 @@ pnode_print(struct format *f, struct pnode *n) case NODE_SYNOPSIS: f->parastate = PARA_HAVE; macro_line(f, "Ed"); + f->parastate = PARA_WANT; break; case NODE_TITLE: case NODE_SUBTITLE: