=================================================================== RCS file: /cvs/mandoc/mdoc_term.c,v retrieving revision 1.91 retrieving revision 1.102 diff -u -p -r1.91 -r1.102 --- mandoc/mdoc_term.c 2009/10/18 13:34:17 1.91 +++ mandoc/mdoc_term.c 2009/11/12 05:50:12 1.102 @@ -1,4 +1,4 @@ -/* $Id: mdoc_term.c,v 1.91 2009/10/18 13:34:17 kristaps Exp $ */ +/* $Id: mdoc_term.c,v 1.102 2009/11/12 05:50:12 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -18,11 +18,11 @@ #include #include -#include #include #include #include +#include "out.h" #include "term.h" #include "mdoc.h" #include "chars.h" @@ -47,6 +47,28 @@ struct termact { void (*post)(DECL_ARGS); }; +static size_t a2width(const struct mdoc_argv *, int); +static size_t a2height(const struct mdoc_node *); +static size_t a2offs(const struct mdoc_argv *); + +static int arg_hasattr(int, const struct mdoc_node *); +static int arg_getattrs(const int *, int *, size_t, + const struct mdoc_node *); +static int arg_getattr(int, const struct mdoc_node *); +static int arg_listtype(const struct mdoc_node *); +static void print_bvspace(struct termp *, + const struct mdoc_node *, + const struct mdoc_node *); +static void print_mdoc_node(DECL_ARGS); +static void print_mdoc_head(DECL_ARGS); +static void print_mdoc_nodelist(DECL_ARGS); +static void print_foot(DECL_ARGS); + +#ifdef __linux__ +extern size_t strlcpy(char *, const char *, size_t); +extern size_t strlcat(char *, const char *, size_t); +#endif + static void termp____post(DECL_ARGS); static void termp_an_post(DECL_ARGS); static void termp_aq_post(DECL_ARGS); @@ -94,6 +116,7 @@ static int termp_fo_pre(DECL_ARGS); static int termp_ft_pre(DECL_ARGS); static int termp_in_pre(DECL_ARGS); static int termp_it_pre(DECL_ARGS); +static int termp_li_pre(DECL_ARGS); static int termp_lk_pre(DECL_ARGS); static int termp_nd_pre(DECL_ARGS); static int termp_nm_pre(DECL_ARGS); @@ -145,7 +168,7 @@ static const struct termact termacts[MDOC_MAX] = { { termp_ft_pre, termp_ft_post }, /* Ft */ { termp_bold_pre, NULL }, /* Ic */ { termp_in_pre, termp_in_post }, /* In */ - { NULL, NULL }, /* Li */ + { termp_li_pre, NULL }, /* Li */ { termp_nd_pre, NULL }, /* Nd */ { termp_nm_pre, NULL }, /* Nm */ { termp_op_pre, termp_op_post }, /* Op */ @@ -235,31 +258,10 @@ static const struct termact termacts[MDOC_MAX] = { { NULL, termp____post }, /* %Q */ { termp_sp_pre, NULL }, /* br */ { termp_sp_pre, NULL }, /* sp */ + { termp_under_pre, termp____post }, /* %U */ }; -#ifdef __linux__ -extern size_t strlcpy(char *, const char *, size_t); -extern size_t strlcat(char *, const char *, size_t); -#endif -static size_t arg2width(const struct mdoc_argv *, int); -static size_t arg2height(const struct mdoc_node *); -static size_t arg2offs(const struct mdoc_argv *); - -static int arg_hasattr(int, const struct mdoc_node *); -static int arg_getattrs(const int *, int *, size_t, - const struct mdoc_node *); -static int arg_getattr(int, const struct mdoc_node *); -static int arg_listtype(const struct mdoc_node *); -static void print_bvspace(struct termp *, - const struct mdoc_node *, - const struct mdoc_node *); -static void print_node(DECL_ARGS); -static void print_head(DECL_ARGS); -static void print_body(DECL_ARGS); -static void print_foot(DECL_ARGS); - - void terminal_mdoc(void *arg, const struct mdoc *mdoc) { @@ -282,38 +284,38 @@ terminal_mdoc(void *arg, const struct mdoc *mdoc) n = mdoc_node(mdoc); m = mdoc_meta(mdoc); - print_head(p, NULL, m, n); + print_mdoc_head(p, NULL, m, n); if (n->child) - print_body(p, NULL, m, n->child); + print_mdoc_nodelist(p, NULL, m, n->child); print_foot(p, NULL, m, n); } static void -print_body(DECL_ARGS) +print_mdoc_nodelist(DECL_ARGS) { - print_node(p, pair, m, n); + print_mdoc_node(p, pair, m, n); if (n->next) - print_body(p, pair, m, n->next); + print_mdoc_nodelist(p, pair, m, n->next); } /* ARGSUSED */ static void -print_node(DECL_ARGS) +print_mdoc_node(DECL_ARGS) { - int chld, bold, under; + int chld; + const void *font; struct termpair npair; size_t offset, rmargin; chld = 1; offset = p->offset; rmargin = p->rmargin; - bold = p->bold; - under = p->under; + font = term_fontq(p); - bzero(&npair, sizeof(struct termpair)); + memset(&npair, 0, sizeof(struct termpair)); npair.ppair = pair; if (MDOC_TEXT != n->type) { @@ -321,18 +323,12 @@ print_node(DECL_ARGS) chld = (*termacts[n->tok].pre)(p, &npair, m, n); } else term_word(p, n->string); + if (chld && n->child) - print_body(p, &npair, m, n->child); + print_mdoc_nodelist(p, &npair, m, n->child); - /* - * XXX - if bold/under were to span scopes, this wouldn't be - * possible, but because decoration is always in-scope, we can - * get away with this. - */ + term_fontpopq(p, font); - p->bold = bold; - p->under = under; - if (MDOC_TEXT != n->type) if (termacts[n->tok].post) (*termacts[n->tok].post)(p, &npair, m, n); @@ -346,9 +342,10 @@ print_node(DECL_ARGS) static void print_foot(DECL_ARGS) { - struct tm *tm; - char *buf, *os; + char buf[DATESIZ], os[BUFSIZ]; + term_fontrepl(p, TERMFONT_NONE); + /* * Output the footer in new-groff style, that is, three columns * with the middle being the manual date and flanking columns @@ -357,18 +354,9 @@ print_foot(DECL_ARGS) * SYSTEM DATE SYSTEM */ - if (NULL == (buf = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); - if (NULL == (os = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); + time2a(m->date, buf, DATESIZ); + strlcpy(os, m->os, BUFSIZ); - tm = localtime(&m->date); - - if (0 == strftime(buf, p->rmargin, "%B %e, %Y", tm)) - err(EXIT_FAILURE, "strftime"); - - (void)strlcpy(os, m->os, p->rmargin); - term_vspace(p); p->offset = 0; @@ -396,27 +384,18 @@ print_foot(DECL_ARGS) p->offset = 0; p->rmargin = p->maxrmargin; p->flags = 0; - - free(buf); - free(os); } -/* FIXME: put in utility library. */ /* ARGSUSED */ static void -print_head(DECL_ARGS) +print_mdoc_head(DECL_ARGS) { - char *buf, *title; + char buf[BUFSIZ], title[BUFSIZ]; p->rmargin = p->maxrmargin; p->offset = 0; - if (NULL == (buf = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); - if (NULL == (title = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); - /* * The header is strange. It has three components, which are * really two with the first duplicated. It goes like this: @@ -431,15 +410,15 @@ print_head(DECL_ARGS) */ assert(m->vol); - (void)strlcpy(buf, m->vol, p->rmargin); + strlcpy(buf, m->vol, BUFSIZ); if (m->arch) { - (void)strlcat(buf, " (", p->rmargin); - (void)strlcat(buf, m->arch, p->rmargin); - (void)strlcat(buf, ")", p->rmargin); + strlcat(buf, " (", BUFSIZ); + strlcat(buf, m->arch, BUFSIZ); + strlcat(buf, ")", BUFSIZ); } - snprintf(title, p->rmargin, "%s(%d)", m->title, m->msec); + snprintf(title, BUFSIZ, "%s(%d)", m->title, m->msec); p->offset = 0; p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2; @@ -466,44 +445,37 @@ print_head(DECL_ARGS) p->offset = 0; p->rmargin = p->maxrmargin; p->flags &= ~TERMP_NOSPACE; - - free(title); - free(buf); } static size_t -arg2height(const struct mdoc_node *n) +a2height(const struct mdoc_node *n) { - int r; + struct roffsu su; assert(MDOC_TEXT == n->type); assert(n->string); + if ( ! a2roffsu(n->string, &su, SCALE_VS)) + SCALE_VS_INIT(&su, strlen(n->string)); - if ((r = a2height(n->string)) < 0) - return(1); - - return((size_t)r); + return(term_vspan(&su)); } static size_t -arg2width(const struct mdoc_argv *arg, int pos) +a2width(const struct mdoc_argv *arg, int pos) { - int r; + struct roffsu su; assert(arg->value[pos]); - if ('\0' == arg->value[pos][0]) - return(2); + if ( ! a2roffsu(arg->value[pos], &su, SCALE_MAX)) + SCALE_HS_INIT(&su, strlen(arg->value[pos])); - if ((r = a2width(arg->value[pos])) >= 0) - return((size_t)r); - - return(strlen(arg->value[pos]) + 2); + /* XXX: pachemu? */ + return(term_hspan(&su) + 2); } -/* FIXME: put in utility file for front-ends. */ static int arg_listtype(const struct mdoc_node *n) { @@ -546,9 +518,9 @@ arg_listtype(const struct mdoc_node *n) static size_t -arg2offs(const struct mdoc_argv *arg) +a2offs(const struct mdoc_argv *arg) { - int r; + struct roffsu su; if ('\0' == arg->value[0][0]) return(0); @@ -558,10 +530,10 @@ arg2offs(const struct mdoc_argv *arg) return(INDENT + 1); else if (0 == strcmp(arg->value[0], "indent-two")) return((INDENT + 1) * 2); - else if ((r = a2width(arg->value[0])) >= 0) - return((size_t)r); + else if ( ! a2roffsu(arg->value[0], &su, SCALE_MAX)) + SCALE_HS_INIT(&su, strlen(arg->value[0])); - return(strlen(arg->value[0])); + return(term_hspan(&su)); } @@ -725,23 +697,23 @@ termp_it_pre(DECL_ARGS) for (i = 0, nn = n->prev; nn && i < (int)bl->args->argv[vals[2]].sz; nn = nn->prev, i++) - offset += arg2width + offset += a2width (&bl->args->argv[vals[2]], i); /* Whether exceeds maximum column. */ if (i < (int)bl->args->argv[vals[2]].sz) - width = arg2width(&bl->args->argv[vals[2]], i); + width = a2width(&bl->args->argv[vals[2]], i); else width = 0; if (vals[1] >= 0) - offset += arg2offs(&bl->args->argv[vals[1]]); + offset += a2offs(&bl->args->argv[vals[1]]); break; default: if (vals[0] >= 0) - width = arg2width(&bl->args->argv[vals[0]], 0); + width = a2width(&bl->args->argv[vals[0]], 0); if (vals[1] >= 0) - offset += arg2offs(&bl->args->argv[vals[1]]); + offset += a2offs(&bl->args->argv[vals[1]]); break; } @@ -803,7 +775,7 @@ termp_it_pre(DECL_ARGS) switch (type) { case (MDOC_Diag): if (MDOC_HEAD == n->type) - p->bold++; + term_fontpush(p, TERMFONT_BOLD); break; default: break; @@ -941,16 +913,16 @@ termp_it_pre(DECL_ARGS) if (MDOC_HEAD == n->type) switch (type) { case (MDOC_Bullet): - p->bold++; + term_fontpush(p, TERMFONT_BOLD); term_word(p, "\\[bu]"); - p->bold--; + term_fontpop(p); break; case (MDOC_Dash): /* FALLTHROUGH */ case (MDOC_Hyphen): - p->bold++; + term_fontpush(p, TERMFONT_BOLD); term_word(p, "\\(hy"); - p->bold--; + term_fontpop(p); break; case (MDOC_Enum): (pair->ppair->ppair->count)++; @@ -1032,7 +1004,9 @@ termp_nm_pre(DECL_ARGS) if (SEC_SYNOPSIS == n->sec) term_newln(p); - p->bold++; + + term_fontpush(p, TERMFONT_BOLD); + if (NULL == n->child) term_word(p, m->name); return(1); @@ -1044,7 +1018,7 @@ static int termp_fl_pre(DECL_ARGS) { - p->bold++; + term_fontpush(p, TERMFONT_BOLD); term_word(p, "\\-"); p->flags |= TERMP_NOSPACE; return(1); @@ -1142,9 +1116,9 @@ termp_rv_pre(DECL_ARGS) term_word(p, "The"); for (nn = n->child; nn; nn = nn->next) { - p->bold++; + term_fontpush(p, TERMFONT_BOLD); term_word(p, nn->string); - p->bold--; + term_fontpop(p); p->flags |= TERMP_NOSPACE; if (nn->next && NULL == nn->next->next) term_word(p, "(), and"); @@ -1162,9 +1136,9 @@ termp_rv_pre(DECL_ARGS) term_word(p, "the value 0 if successful; otherwise the value " "-1 is returned and the global variable"); - p->under++; + term_fontpush(p, TERMFONT_UNDER); term_word(p, "errno"); - p->under--; + term_fontpop(p); term_word(p, "is set to indicate the error."); @@ -1181,9 +1155,9 @@ termp_ex_pre(DECL_ARGS) term_word(p, "The"); for (nn = n->child; nn; nn = nn->next) { - p->bold++; + term_fontpush(p, TERMFONT_BOLD); term_word(p, nn->string); - p->bold--; + term_fontpop(p); p->flags |= TERMP_NOSPACE; if (nn->next && NULL == nn->next->next) term_word(p, ", and"); @@ -1285,7 +1259,7 @@ static int termp_bold_pre(DECL_ARGS) { - p->bold++; + term_fontpush(p, TERMFONT_BOLD); return(1); } @@ -1319,7 +1293,7 @@ termp_sh_pre(DECL_ARGS) term_vspace(p); break; case (MDOC_HEAD): - p->bold++; + term_fontpush(p, TERMFONT_BOLD); break; case (MDOC_BODY): p->offset = INDENT; @@ -1454,7 +1428,8 @@ termp_ft_pre(DECL_ARGS) if (SEC_SYNOPSIS == n->sec) if (n->prev && MDOC_Fo == n->prev->tok) term_vspace(p); - p->under++; + + term_fontpush(p, TERMFONT_UNDER); return(1); } @@ -1475,17 +1450,18 @@ termp_fn_pre(DECL_ARGS) { const struct mdoc_node *nn; - p->bold++; + term_fontpush(p, TERMFONT_BOLD); term_word(p, n->child->string); - p->bold--; + term_fontpop(p); p->flags |= TERMP_NOSPACE; term_word(p, "("); for (nn = n->child->next; nn; nn = nn->next) { - p->under++; + term_fontpush(p, TERMFONT_UNDER); term_word(p, nn->string); - p->under--; + term_fontpop(p); + if (nn->next) term_word(p, ","); } @@ -1516,14 +1492,15 @@ termp_fa_pre(DECL_ARGS) const struct mdoc_node *nn; if (n->parent->tok != MDOC_Fo) { - p->under++; + term_fontpush(p, TERMFONT_UNDER); return(1); } for (nn = n->child; nn; nn = nn->next) { - p->under++; + term_fontpush(p, TERMFONT_UNDER); term_word(p, nn->string); - p->under--; + term_fontpop(p); + if (nn->next) term_word(p, ","); } @@ -1564,7 +1541,7 @@ termp_bd_pre(DECL_ARGS) type = nn->args->argv[i].arg; break; case (MDOC_Offset): - p->offset += arg2offs(&nn->args->argv[i]); + p->offset += a2offs(&nn->args->argv[i]); break; default: break; @@ -1585,7 +1562,7 @@ termp_bd_pre(DECL_ARGS) for (nn = n->child; nn; nn = nn->next) { p->flags |= TERMP_NOSPACE; - print_node(p, pair, m, nn); + print_mdoc_node(p, pair, m, nn); if (NULL == nn->next) continue; if (nn->prev && nn->prev->line < nn->line) @@ -1658,7 +1635,7 @@ termp_xx_pre(DECL_ARGS) pp = "BSDI BSD/OS"; break; case (MDOC_Dx): - pp = "DragonFlyBSD"; + pp = "DragonFly"; break; case (MDOC_Fx): pp = "FreeBSD"; @@ -1739,7 +1716,7 @@ termp_ss_pre(DECL_ARGS) term_vspace(p); break; case (MDOC_HEAD): - p->bold++; + term_fontpush(p, TERMFONT_BOLD); p->offset = HALFINDENT; break; default: @@ -1765,7 +1742,7 @@ static int termp_cd_pre(DECL_ARGS) { - p->bold++; + term_fontpush(p, TERMFONT_BOLD); term_newln(p); return(1); } @@ -1776,7 +1753,7 @@ static int termp_in_pre(DECL_ARGS) { - p->bold++; + term_fontpush(p, TERMFONT_BOLD); if (SEC_SYNOPSIS == n->sec) term_word(p, "#include"); @@ -1791,10 +1768,10 @@ static void termp_in_post(DECL_ARGS) { - p->bold++; + term_fontpush(p, TERMFONT_BOLD); p->flags |= TERMP_NOSPACE; term_word(p, ">"); - p->bold--; + term_fontpop(p); if (SEC_SYNOPSIS != n->sec) return; @@ -1819,7 +1796,7 @@ termp_sp_pre(DECL_ARGS) switch (n->tok) { case (MDOC_sp): - len = n->child ? arg2height(n->child) : 1; + len = n->child ? a2height(n->child) : 1; break; case (MDOC_br): len = 0; @@ -1926,12 +1903,12 @@ termp_fo_pre(DECL_ARGS) } else if (MDOC_HEAD != n->type) return(1); - p->bold++; + term_fontpush(p, TERMFONT_BOLD); for (nn = n->child; nn; nn = nn->next) { assert(MDOC_TEXT == nn->type); term_word(p, nn->string); } - p->bold--; + term_fontpop(p); return(0); } @@ -1965,18 +1942,22 @@ termp_bf_pre(DECL_ARGS) if (NULL == (nn = n->head->child)) { if (arg_hasattr(MDOC_Emphasis, n)) - p->under++; + term_fontpush(p, TERMFONT_UNDER); else if (arg_hasattr(MDOC_Symbolic, n)) - p->bold++; + term_fontpush(p, TERMFONT_BOLD); + else + term_fontpush(p, TERMFONT_NONE); return(1); } assert(MDOC_TEXT == nn->type); if (0 == strcmp("Em", nn->string)) - p->under++; + term_fontpush(p, TERMFONT_UNDER); else if (0 == strcmp("Sy", nn->string)) - p->bold++; + term_fontpush(p, TERMFONT_BOLD); + else + term_fontpush(p, TERMFONT_NONE); return(1); } @@ -2015,6 +1996,8 @@ static void termp____post(DECL_ARGS) { + /* TODO: %U. */ + p->flags |= TERMP_NOSPACE; switch (n->tok) { case (MDOC__T): @@ -2030,25 +2013,36 @@ termp____post(DECL_ARGS) /* ARGSUSED */ static int +termp_li_pre(DECL_ARGS) +{ + + term_fontpush(p, TERMFONT_NONE); + return(1); +} + + +/* ARGSUSED */ +static int termp_lk_pre(DECL_ARGS) { const struct mdoc_node *nn; - if (NULL == (nn = n->child->next)) { - p->under++; + term_fontpush(p, TERMFONT_UNDER); + nn = n->child; + + if (NULL == nn->next) return(1); - } - p->under++; term_word(p, nn->string); + term_fontpop(p); + p->flags |= TERMP_NOSPACE; term_word(p, ":"); - p->under--; - p->bold++; + term_fontpush(p, TERMFONT_BOLD); for (nn = nn->next; nn; nn = nn->next) term_word(p, nn->string); - p->bold--; + term_fontpop(p); return(0); } @@ -2059,7 +2053,7 @@ static int termp_under_pre(DECL_ARGS) { - p->under++; + term_fontpush(p, TERMFONT_UNDER); return(1); }