version 1.132, 2019/04/21 14:48:11 |
version 1.142, 2019/04/28 19:05:11 |
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
|
#include "xmalloc.h" |
#include "node.h" |
#include "node.h" |
#include "macro.h" |
#include "macro.h" |
#include "format.h" |
#include "format.h" |
Line 42 pnode_printtext(struct format *f, struct pnode *n) |
|
Line 43 pnode_printtext(struct format *f, struct pnode *n) |
|
|
|
cp = n->b; |
cp = n->b; |
accept_arg = f->flags & FMT_ARG; |
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 (;;) { |
for (;;) { |
if (*cp == '\0') |
if (*cp == '\0') |
return; |
return; |
Line 71 pnode_printtext(struct format *f, struct pnode *n) |
|
Line 73 pnode_printtext(struct format *f, struct pnode *n) |
|
*/ |
*/ |
|
|
if (f->linestate != LINE_MACRO && |
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)) { |
switch (pnode_class(nn->node)) { |
case CLASS_LINE: |
case CLASS_LINE: |
case CLASS_ENCL: |
case CLASS_ENCL: |
macro_open(f, "Pf"); |
macro_open(f, "Pf"); |
accept_arg = 1; |
accept_arg = 1; |
f->flags |= FMT_CHILD; |
f->flags |= FMT_CHILD; |
nn->spc = 1; |
nn->flags |= NFLAG_SPC; |
break; |
break; |
default: |
default: |
break; |
break; |
Line 89 pnode_printtext(struct format *f, struct pnode *n) |
|
Line 92 pnode_printtext(struct format *f, struct pnode *n) |
|
case LINE_NEW: |
case LINE_NEW: |
break; |
break; |
case LINE_TEXT: |
case LINE_TEXT: |
if (n->spc) { |
if (n->flags & NFLAG_SPC) { |
if (n->node == NODE_TEXT) |
if (n->flags & NFLAG_LINE && |
|
pnode_class(n->node) == CLASS_TEXT) |
macro_close(f); |
macro_close(f); |
else |
else |
putchar(' '); |
putchar(' '); |
} |
} |
break; |
break; |
case LINE_MACRO: |
case LINE_MACRO: |
if (accept_arg) |
if (accept_arg == 0) |
putchar(' '); |
|
else |
|
macro_close(f); |
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; |
break; |
} |
} |
|
|
|
|
pnode_printsection(struct format *f, struct pnode *n) |
pnode_printsection(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *nc, *ncc; |
struct pnode *nc, *ncc; |
const char *title; |
|
int flags, level; |
int flags, level; |
|
|
if (n->parent == NULL) { |
if (n->parent == NULL) { |
Line 198 pnode_printsection(struct format *f, struct pnode *n) |
|
Line 204 pnode_printsection(struct format *f, struct pnode *n) |
|
level = ++f->level; |
level = ++f->level; |
flags = ARG_SPACE; |
flags = ARG_SPACE; |
switch (n->node) { |
switch (n->node) { |
case NODE_PREFACE: |
|
case NODE_SECTION: |
case NODE_SECTION: |
case NODE_APPENDIX: |
case NODE_APPENDIX: |
if (level == 1) |
if (level == 1) |
flags |= ARG_UPPER; |
flags |= ARG_UPPER; |
break; |
break; |
case NODE_SIMPLESECT: |
case NODE_SIMPLESECT: |
case NODE_LEGALNOTICE: |
|
if (level < 2) |
if (level < 2) |
level = 2; |
level = 2; |
break; |
break; |
default: |
case NODE_NOTE: |
if (level < 3) |
if (level < 3) |
level = 3; |
level = 3; |
break; |
break; |
|
default: |
|
abort(); |
} |
} |
|
|
TAILQ_FOREACH(nc, &n->childq, child) |
TAILQ_FOREACH(nc, &n->childq, child) |
if (nc->node == NODE_TITLE) |
if (nc->node == NODE_TITLE) |
break; |
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) { |
switch (level) { |
case 1: |
case 1: |
macro_close(f); |
macro_close(f); |
Line 265 pnode_printsection(struct format *f, struct pnode *n) |
|
Line 242 pnode_printsection(struct format *f, struct pnode *n) |
|
macro_open(f, "Sy"); |
macro_open(f, "Sy"); |
break; |
break; |
} |
} |
|
macro_addnode(f, nc, flags); |
if (nc != NULL) |
|
macro_addnode(f, nc, flags); |
|
else |
|
macro_addarg(f, title, flags | ARG_QUOTED); |
|
macro_close(f); |
macro_close(f); |
|
|
/* |
/* |
Line 722 pnode_printolink(struct format *f, struct pnode *n) |
|
Line 695 pnode_printolink(struct format *f, struct pnode *n) |
|
static void |
static void |
pnode_printprologue(struct format *f, struct pnode *root) |
pnode_printprologue(struct format *f, struct pnode *root) |
{ |
{ |
struct pnode *date, *refmeta, *name, *vol, *descr, *nc, *nn; |
struct pnode *name, *nc; |
const char *sname; |
|
|
|
/* 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"); |
macro_open(f, "Dt"); |
if (name == NULL) { |
name = TAILQ_FIRST(&root->childq); |
sname = pnode_getattr_raw(root, ATTRKEY_ID, "UNKNOWN"); |
assert(name->node == NODE_REFENTRYTITLE); |
macro_addarg(f, sname, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
macro_addnode(f, name, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
} else |
TAILQ_REMOVE(&root->childq, name, child); |
macro_addnode(f, name, ARG_SPACE | ARG_SINGLE | ARG_UPPER); |
name->parent = NULL; |
if (vol == NULL) |
nc = TAILQ_FIRST(&root->childq); |
macro_addarg(f, "1", ARG_SPACE); |
assert (nc->node == NODE_MANVOLNUM); |
else |
macro_addnode(f, nc, ARG_SPACE | ARG_SINGLE); |
macro_addnode(f, vol, ARG_SPACE | ARG_SINGLE); |
pnode_unlink(nc); |
|
|
macro_line(f, "Os"); |
macro_line(f, "Os"); |
|
|
if (descr != NULL) { |
nc = TAILQ_FIRST(&root->childq); |
|
if (nc != NULL && nc->node == NODE_TITLE) { |
macro_line(f, "Sh NAME"); |
macro_line(f, "Sh NAME"); |
if (name == NULL) |
macro_nodeline(f, "Nm", name, ARG_SINGLE); |
macro_argline(f, "Nm", sname); |
macro_nodeline(f, "Nd", nc, 0); |
else |
pnode_unlink(nc); |
macro_nodeline(f, "Nm", name, ARG_SINGLE); |
|
macro_nodeline(f, "Nd", descr, 0); |
|
} |
} |
|
|
/* Clean up. */ |
|
|
|
pnode_unlink(date); |
|
pnode_unlink(name); |
pnode_unlink(name); |
pnode_unlink(vol); |
|
pnode_unlink(descr); |
|
f->parastate = PARA_HAVE; |
f->parastate = PARA_HAVE; |
} |
} |
|
|
Line 812 pnode_printrefentry(struct format *f, struct pnode *n) |
|
Line 746 pnode_printrefentry(struct format *f, struct pnode *n) |
|
pnode_unlink(info); |
pnode_unlink(info); |
info = NULL; |
info = NULL; |
} |
} |
|
if (info == NULL) |
|
info = pnode_takefirst(n, NODE_INFO); |
meta = pnode_takefirst(n, NODE_REFMETA); |
meta = pnode_takefirst(n, NODE_REFMETA); |
if (meta != NULL && TAILQ_FIRST(&meta->childq) == NULL) { |
if (meta != NULL && TAILQ_FIRST(&meta->childq) == NULL) { |
pnode_unlink(meta); |
pnode_unlink(meta); |
Line 836 pnode_printrefentry(struct format *f, struct pnode *n) |
|
Line 772 pnode_printrefentry(struct format *f, struct pnode *n) |
|
case NODE_REFENTRY: |
case NODE_REFENTRY: |
case NODE_REFNAMEDIV: |
case NODE_REFNAMEDIV: |
case NODE_REFSYNOPSISDIV: |
case NODE_REFSYNOPSISDIV: |
case NODE_PREFACE: |
|
later = NULL; |
later = NULL; |
continue; |
continue; |
case NODE_APPENDIX: |
case NODE_APPENDIX: |
Line 880 pnode_printrefentry(struct format *f, struct pnode *n) |
|
Line 815 pnode_printrefentry(struct format *f, struct pnode *n) |
|
*/ |
*/ |
|
|
if (match == NULL) { |
if (match == NULL) { |
if ((match = calloc(1, sizeof(*match))) == NULL) { |
match = xcalloc(1, sizeof(*match)); |
perror(NULL); |
|
exit(1); |
|
} |
|
match->node = NODE_SECTION; |
match->node = NODE_SECTION; |
match->spc = 1; |
match->flags |= NFLAG_SPC; |
match->parent = n; |
match->parent = n; |
TAILQ_INIT(&match->childq); |
TAILQ_INIT(&match->childq); |
TAILQ_INIT(&match->attrq); |
TAILQ_INIT(&match->attrq); |
if ((nc = pnode_alloc(match)) == NULL) { |
nc = pnode_alloc(match); |
perror(NULL); |
|
exit(1); |
|
} |
|
nc->node = NODE_TITLE; |
nc->node = NODE_TITLE; |
nc->spc = 1; |
nc->flags |= NFLAG_SPC; |
if ((nc = pnode_alloc(nc)) == NULL) { |
nc = pnode_alloc_text(nc, "AUTHORS"); |
perror(NULL); |
nc->flags |= NFLAG_SPC; |
exit(1); |
|
} |
|
nc->node = NODE_TEXT; |
|
if ((nc->b = strdup("AUTHORS")) == NULL) { |
|
perror(NULL); |
|
exit(1); |
|
} |
|
nc->spc = 1; |
|
if (later == NULL) |
if (later == NULL) |
TAILQ_INSERT_TAIL(&n->childq, match, child); |
TAILQ_INSERT_TAIL(&n->childq, match, child); |
else |
else |
Line 929 pnode_printrefentry(struct format *f, struct pnode *n) |
|
Line 850 pnode_printrefentry(struct format *f, struct pnode *n) |
|
static void |
static void |
pnode_printvarlistentry(struct format *f, struct pnode *n) |
pnode_printvarlistentry(struct format *f, struct pnode *n) |
{ |
{ |
struct pnode *nc, *nn; |
struct pnode *nc, *nn, *ncc; |
int first = 1; |
int comma; |
|
|
macro_open(f, "It"); |
macro_open(f, "It"); |
f->parastate = PARA_HAVE; |
f->parastate = PARA_HAVE; |
f->flags |= FMT_IMPL; |
f->flags |= FMT_IMPL; |
|
comma = -1; |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
TAILQ_FOREACH_SAFE(nc, &n->childq, child, nn) { |
if (nc->node != NODE_TERM && nc->node != NODE_GLOSSTERM) |
if (nc->node != NODE_TERM && nc->node != NODE_GLOSSTERM) |
continue; |
continue; |
if (first == 0) { |
if (comma != -1) { |
switch (f->linestate) { |
switch (f->linestate) { |
case LINE_NEW: |
case LINE_NEW: |
break; |
break; |
Line 946 pnode_printvarlistentry(struct format *f, struct pnode |
|
Line 868 pnode_printvarlistentry(struct format *f, struct pnode |
|
print_text(f, ",", 0); |
print_text(f, ",", 0); |
break; |
break; |
case LINE_MACRO: |
case LINE_MACRO: |
macro_addarg(f, ",", 0); |
macro_addarg(f, ",", comma); |
break; |
break; |
} |
} |
} |
} |
f->parastate = PARA_HAVE; |
f->parastate = PARA_HAVE; |
|
comma = (ncc = TAILQ_FIRST(&nc->childq)) == NULL || |
|
pnode_class(ncc->node) == CLASS_TEXT ? 0 : ARG_SPACE; |
pnode_print(f, nc); |
pnode_print(f, nc); |
pnode_unlink(nc); |
pnode_unlink(nc); |
first = 0; |
|
} |
} |
macro_close(f); |
macro_close(f); |
f->parastate = PARA_HAVE; |
f->parastate = PARA_HAVE; |
Line 1033 pnode_printtgroup2(struct format *f, struct pnode *n) |
|
Line 956 pnode_printtgroup2(struct format *f, struct pnode *n) |
|
pnode_unlink(nr); |
pnode_unlink(nr); |
} |
} |
macro_line(f, "El"); |
macro_line(f, "El"); |
|
f->parastate = PARA_WANT; |
pnode_unlinksub(n); |
pnode_unlinksub(n); |
} |
} |
|
|
Line 1059 pnode_printtgroup(struct format *f, struct pnode *n) |
|
Line 983 pnode_printtgroup(struct format *f, struct pnode *n) |
|
pnode_printrow(f, nc); |
pnode_printrow(f, nc); |
} |
} |
macro_line(f, "El"); |
macro_line(f, "El"); |
|
f->parastate = PARA_WANT; |
pnode_unlinksub(n); |
pnode_unlinksub(n); |
} |
} |
|
|
Line 1078 pnode_printlist(struct format *f, struct pnode *n) |
|
Line 1003 pnode_printlist(struct format *f, struct pnode *n) |
|
f->parastate = PARA_HAVE; |
f->parastate = PARA_HAVE; |
} |
} |
macro_line(f, "El"); |
macro_line(f, "El"); |
|
f->parastate = PARA_WANT; |
pnode_unlinksub(n); |
pnode_unlinksub(n); |
} |
} |
|
|
Line 1096 pnode_printvariablelist(struct format *f, struct pnode |
|
Line 1022 pnode_printvariablelist(struct format *f, struct pnode |
|
macro_nodeline(f, "It", nc, 0); |
macro_nodeline(f, "It", nc, 0); |
} |
} |
macro_line(f, "El"); |
macro_line(f, "El"); |
|
f->parastate = PARA_WANT; |
pnode_unlinksub(n); |
pnode_unlinksub(n); |
} |
} |
|
|
Line 1115 pnode_print(struct format *f, struct pnode *n) |
|
Line 1042 pnode_print(struct format *f, struct pnode *n) |
|
return; |
return; |
|
|
was_impl = f->flags & FMT_IMPL; |
was_impl = f->flags & FMT_IMPL; |
if (n->spc) |
if (n->flags & NFLAG_SPC) |
f->flags &= ~FMT_NOSPC; |
f->flags &= ~FMT_NOSPC; |
else |
else |
f->flags |= FMT_NOSPC; |
f->flags |= FMT_NOSPC; |
Line 1280 pnode_print(struct format *f, struct pnode *n) |
|
Line 1207 pnode_print(struct format *f, struct pnode *n) |
|
if ((nc = TAILQ_FIRST(&n->childq)) != NULL && |
if ((nc = TAILQ_FIRST(&n->childq)) != NULL && |
nc->node == NODE_FILENAME && |
nc->node == NODE_FILENAME && |
TAILQ_NEXT(nc, child) == NULL) { |
TAILQ_NEXT(nc, child) == NULL) { |
if (n->spc) |
if (n->flags & NFLAG_SPC) |
nc->spc = 1; |
nc->flags |= NFLAG_SPC; |
} else if (was_impl) |
} else if (was_impl) |
macro_open(f, "Do"); |
macro_open(f, "Do"); |
else { |
else { |
Line 1315 pnode_print(struct format *f, struct pnode *n) |
|
Line 1242 pnode_print(struct format *f, struct pnode *n) |
|
case NODE_REFSYNOPSISDIV: |
case NODE_REFSYNOPSISDIV: |
pnode_printrefsynopsisdiv(f, n); |
pnode_printrefsynopsisdiv(f, n); |
break; |
break; |
case NODE_PREFACE: |
|
case NODE_SECTION: |
case NODE_SECTION: |
case NODE_SIMPLESECT: |
case NODE_SIMPLESECT: |
case NODE_APPENDIX: |
case NODE_APPENDIX: |
case NODE_LEGALNOTICE: |
|
case NODE_NOTE: |
case NODE_NOTE: |
case NODE_TIP: |
|
case NODE_CAUTION: |
|
case NODE_WARNING: |
|
pnode_printsection(f, n); |
pnode_printsection(f, n); |
break; |
break; |
case NODE_REPLACEABLE: |
case NODE_REPLACEABLE: |
Line 1339 pnode_print(struct format *f, struct pnode *n) |
|
Line 1261 pnode_print(struct format *f, struct pnode *n) |
|
else |
else |
print_text(f, "_", 0); |
print_text(f, "_", 0); |
if ((nc = TAILQ_FIRST(&n->childq)) != NULL) |
if ((nc = TAILQ_FIRST(&n->childq)) != NULL) |
nc->spc = 0; |
nc->flags &= ~(NFLAG_LINE | NFLAG_SPC); |
break; |
break; |
case NODE_SUPERSCRIPT: |
case NODE_SUPERSCRIPT: |
fputs("\\(ha", stdout); |
fputs("\\(ha", stdout); |
if ((nc = TAILQ_FIRST(&n->childq)) != NULL) |
if ((nc = TAILQ_FIRST(&n->childq)) != NULL) |
nc->spc = 0; |
nc->flags &= ~(NFLAG_LINE | NFLAG_SPC); |
break; |
break; |
case NODE_TEXT: |
case NODE_TEXT: |
case NODE_ESCAPE: |
case NODE_ESCAPE: |
Line 1375 pnode_print(struct format *f, struct pnode *n) |
|
Line 1297 pnode_print(struct format *f, struct pnode *n) |
|
case NODE_XREF: |
case NODE_XREF: |
pnode_printxref(f, n); |
pnode_printxref(f, n); |
break; |
break; |
|
case NODE_CAUTION: |
|
case NODE_LEGALNOTICE: |
|
case NODE_PREFACE: |
|
case NODE_TIP: |
|
case NODE_WARNING: |
|
abort(); |
default: |
default: |
break; |
break; |
} |
} |
Line 1459 pnode_print(struct format *f, struct pnode *n) |
|
Line 1387 pnode_print(struct format *f, struct pnode *n) |
|
} else |
} else |
f->flags &= ~FMT_IMPL; |
f->flags &= ~FMT_IMPL; |
break; |
break; |
case NODE_PREFACE: |
|
case NODE_SECTION: |
case NODE_SECTION: |
|
case NODE_SIMPLESECT: |
case NODE_APPENDIX: |
case NODE_APPENDIX: |
case NODE_LEGALNOTICE: |
|
case NODE_NOTE: |
case NODE_NOTE: |
case NODE_TIP: |
|
case NODE_CAUTION: |
|
case NODE_WARNING: |
|
f->level--; |
f->level--; |
break; |
break; |
case NODE_BLOCKQUOTE: |
case NODE_BLOCKQUOTE: |
Line 1476 pnode_print(struct format *f, struct pnode *n) |
|
Line 1400 pnode_print(struct format *f, struct pnode *n) |
|
case NODE_SYNOPSIS: |
case NODE_SYNOPSIS: |
f->parastate = PARA_HAVE; |
f->parastate = PARA_HAVE; |
macro_line(f, "Ed"); |
macro_line(f, "Ed"); |
|
f->parastate = PARA_WANT; |
break; |
break; |
case NODE_TITLE: |
case NODE_TITLE: |
case NODE_SUBTITLE: |
case NODE_SUBTITLE: |