=================================================================== RCS file: /cvs/docbook2mdoc/macro.c,v retrieving revision 1.7 retrieving revision 1.11 diff -u -p -r1.7 -r1.11 --- docbook2mdoc/macro.c 2019/04/07 15:06:56 1.7 +++ docbook2mdoc/macro.c 2019/04/12 09:01:48 1.11 @@ -1,4 +1,4 @@ -/* $Id: macro.c,v 1.7 2019/04/07 15:06:56 schwarze Exp $ */ +/* $Id: macro.c,v 1.11 2019/04/12 09:01:48 schwarze Exp $ */ /* * Copyright (c) 2019 Ingo Schwarze * @@ -31,29 +31,37 @@ void macro_open(struct format *f, const char *name) { switch (f->linestate) { + case LINE_MACRO: + if (f->flags & FMT_NOSPC) { + fputs(" Ns ", stdout); + break; + } + if (f->flags & (FMT_CHILD | FMT_IMPL)) { + putchar(' '); + break; + } + /* FALLTHROUGH */ case LINE_TEXT: putchar('\n'); /* FALLTHROUGH */ case LINE_NEW: putchar('.'); f->linestate = LINE_MACRO; + f->flags = 0; break; - case LINE_MACRO: - putchar(' '); - if (f->spc == 0) - fputs("Ns ", stdout); - break; } fputs(name, stdout); + f->flags &= FMT_IMPL; + f->flags |= FMT_ARG; } void macro_close(struct format *f) { - if (f->linestate == LINE_NEW) - return; - putchar('\n'); + if (f->linestate != LINE_NEW) + putchar('\n'); f->linestate = LINE_NEW; + f->flags = 0; } void @@ -65,73 +73,6 @@ macro_line(struct format *f, const char *name) } /* - * At the end of a macro, decide whether the line needs to remain open - * because the next node follows without intervening whitespace; - * otherwise, close the line. - */ -void -macro_closepunct(struct format *f, struct pnode *pn) -{ - char *cp; - - if ((pn = TAILQ_NEXT(pn, child)) != NULL && pn->spc == 0) { - - /* - * If a non-text node follows without intervening - * whitespace, the handler of that node will decide - * whether and how to suppress whitespace. To allow - * that, the macro line needs to remain open. - */ - - if (pn->node != NODE_TEXT && pn->node != NODE_ESCAPE) - return; - - /* - * Give closing punctuation - * in the form of trailing macro arguments. - */ - - while (*pn->b != '\0' && - strchr("!),.:;?]", *pn->b) != NULL) { - putchar(' '); - putchar(*pn->b); - pn->b++; - pn->bsz--; - } - - /* - * Text follows without intervening whitespace. - * Append the first word with .Ns. - */ - - if (*pn->b != '\0' && isspace((unsigned char)*pn->b) == 0) { - fputs(" Ns", stdout); - for (cp = pn->b; *cp != '\0'; cp++) - if (isspace((unsigned char)*cp)) - break; - *cp = '\0'; - macro_addarg(f, pn->b, ARG_SPACE); - pn->bsz -= cp - pn->b; - pn->b = cp; - if (pn->bsz > 0) { - pn->b++; - pn->bsz--; - pn->spc = 1; - } - } - - /* Skip whitespace after the first word. */ - - while (isspace((unsigned char)*pn->b)) { - pn->b++; - pn->bsz--; - pn->spc = 1; - } - } - macro_close(f); -} - -/* * Print an argument string on a macro line, collapsing whitespace. */ void @@ -203,7 +144,7 @@ macro_argline(struct format *f, const char *name, cons * Recursively append text from the children of a node to a macro line. */ void -macro_addnode(struct format *f, struct pnode *pn, int flags) +macro_addnode(struct format *f, struct pnode *n, int flags) { struct pnode *nc; int quote_now; @@ -215,12 +156,12 @@ macro_addnode(struct format *f, struct pnode *pn, int * that text, letting macro_addarg() decide about quoting. */ - while ((nc = TAILQ_FIRST(&pn->childq)) != NULL && + while ((nc = TAILQ_FIRST(&n->childq)) != NULL && TAILQ_NEXT(nc, child) == NULL) - pn = nc; + n = nc; - if (pn->node == NODE_TEXT || pn->node == NODE_ESCAPE) { - macro_addarg(f, pn->b, flags); + if (n->node == NODE_TEXT || n->node == NODE_ESCAPE) { + macro_addarg(f, n->b, flags); return; } @@ -258,10 +199,10 @@ macro_addnode(struct format *f, struct pnode *pn, int } void -macro_nodeline(struct format *f, const char *name, struct pnode *pn, int flags) +macro_nodeline(struct format *f, const char *name, struct pnode *n, int flags) { macro_open(f, name); - macro_addnode(f, pn, ARG_SPACE | flags); + macro_addnode(f, n, ARG_SPACE | flags); macro_close(f); } @@ -271,7 +212,8 @@ macro_nodeline(struct format *f, const char *name, str * line otherwise. The flag ARG_SPACE inserts spaces between words. */ void -print_text(struct format *f, const char *word, int flags) { +print_text(struct format *f, const char *word, int flags) +{ switch (f->linestate) { case LINE_NEW: break; @@ -283,8 +225,15 @@ print_text(struct format *f, const char *word, int fla macro_close(f); break; } - fputs(word, stdout); + if (f->linestate == LINE_NEW && (*word == '.' || *word == '\'')) + fputs("\\&", stdout); + while (*word != '\0') { + putchar(*word); + if (*word++ == '\\') + putchar('e'); + } f->linestate = LINE_TEXT; + f->flags = 0; } /*