=================================================================== RCS file: /cvs/mandoc/term.c,v retrieving revision 1.48 retrieving revision 1.60 diff -u -p -r1.48 -r1.60 --- mandoc/term.c 2009/03/08 20:50:12 1.48 +++ mandoc/term.c 2009/03/16 22:19:19 1.60 @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.48 2009/03/08 20:50:12 kristaps Exp $ */ +/* $Id: term.c,v 1.60 2009/03/16 22:19:19 kristaps Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons * @@ -16,6 +16,8 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ +#include + #include #include #include @@ -51,7 +53,10 @@ #define TTYPE_SYMB 16 #define TTYPE_SYMBOL 17 #define TTYPE_DIAG 18 -#define TTYPE_NMAX 19 +#define TTYPE_LINK_ANCHOR 19 +#define TTYPE_LINK_TEXT 20 +#define TTYPE_REF_TITLE 21 +#define TTYPE_NMAX 22 /* * These define "styles" for element types, like command arguments or @@ -62,31 +67,36 @@ /* TODO: abstract this into mdocterm.c. */ const int ttypes[TTYPE_NMAX] = { - TERMP_BOLD, /* TTYPE_PROG */ + TERMP_BOLD, /* TTYPE_PROG */ TERMP_BOLD, /* TTYPE_CMD_FLAG */ - TERMP_UNDERLINE, /* TTYPE_CMD_ARG */ + TERMP_UNDER, /* TTYPE_CMD_ARG */ TERMP_BOLD, /* TTYPE_SECTION */ TERMP_BOLD, /* TTYPE_FUNC_DECL */ - TERMP_UNDERLINE, /* TTYPE_VAR_DECL */ - TERMP_UNDERLINE, /* TTYPE_FUNC_TYPE */ + TERMP_UNDER, /* TTYPE_VAR_DECL */ + TERMP_UNDER, /* TTYPE_FUNC_TYPE */ TERMP_BOLD, /* TTYPE_FUNC_NAME */ - TERMP_UNDERLINE, /* TTYPE_FUNC_ARG */ - TERMP_UNDERLINE, /* TTYPE_LINK */ + TERMP_UNDER, /* TTYPE_FUNC_ARG */ + TERMP_UNDER, /* TTYPE_LINK */ TERMP_BOLD, /* TTYPE_SSECTION */ - TERMP_UNDERLINE, /* TTYPE_FILE */ - TERMP_UNDERLINE, /* TTYPE_EMPH */ + TERMP_UNDER, /* TTYPE_FILE */ + TERMP_UNDER, /* TTYPE_EMPH */ TERMP_BOLD, /* TTYPE_CONFIG */ TERMP_BOLD, /* TTYPE_CMD */ TERMP_BOLD, /* TTYPE_INCLUDE */ TERMP_BOLD, /* TTYPE_SYMB */ TERMP_BOLD, /* TTYPE_SYMBOL */ - TERMP_BOLD /* TTYPE_DIAG */ + TERMP_BOLD, /* TTYPE_DIAG */ + TERMP_UNDER, /* TTYPE_LINK_ANCHOR */ + TERMP_BOLD, /* TTYPE_LINK_TEXT */ + TERMP_UNDER /* TTYPE_REF_TITLE */ }; 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 size_t arg_offset(const struct mdoc_argv *); -static size_t arg_width(const struct mdoc_argv *); +static size_t arg_width(const struct mdoc_argv *, int); static int arg_listtype(const struct mdoc_node *); /* @@ -108,10 +118,10 @@ static void name##_post(DECL_ARGS) DECL_PRE(name); \ DECL_POST(name); -DECL_PREPOST(termp__t); DECL_PREPOST(termp_aq); DECL_PREPOST(termp_bd); DECL_PREPOST(termp_bq); +DECL_PREPOST(termp_brq); DECL_PREPOST(termp_d1); DECL_PREPOST(termp_dq); DECL_PREPOST(termp_fd); @@ -130,6 +140,7 @@ DECL_PREPOST(termp_ss); DECL_PREPOST(termp_sq); DECL_PREPOST(termp_vt); +DECL_PRE(termp__t); DECL_PRE(termp_ap); DECL_PRE(termp_ar); DECL_PRE(termp_at); @@ -144,7 +155,9 @@ DECL_PRE(termp_fa); DECL_PRE(termp_fl); DECL_PRE(termp_fx); DECL_PRE(termp_ic); +DECL_PRE(termp_lk); DECL_PRE(termp_ms); +DECL_PRE(termp_mt); DECL_PRE(termp_nd); DECL_PRE(termp_nm); DECL_PRE(termp_ns); @@ -218,7 +231,7 @@ const struct termact __termacts[MDOC_MAX] = { { NULL, termp____post }, /* %O */ { NULL, termp____post }, /* %P */ { NULL, termp____post }, /* %R */ - { termp__t_pre, termp__t_post }, /* %T */ + { termp__t_pre, termp____post }, /* %T */ { NULL, termp____post }, /* %V */ { NULL, NULL }, /* Ac */ { termp_aq_pre, termp_aq_post }, /* Ao */ @@ -276,38 +289,48 @@ const struct termact __termacts[MDOC_MAX] = { { termp_ud_pre, NULL }, /* Ud */ { termp_lb_pre, termp_lb_post }, /* Lb */ { termp_ap_pre, NULL }, /* Lb */ + { termp_pp_pre, NULL }, /* Pp */ + { termp_lk_pre, NULL }, /* Lk */ + { termp_mt_pre, NULL }, /* Mt */ + { termp_brq_pre, termp_brq_post }, /* Brq */ + { termp_brq_pre, termp_brq_post }, /* Bro */ + { NULL, NULL }, /* Brc */ + { NULL, NULL }, /* %C */ + { NULL, NULL }, /* Es */ + { NULL, NULL }, /* En */ }; const struct termact *termacts = __termacts; static size_t -arg_width(const struct mdoc_argv *arg) +arg_width(const struct mdoc_argv *arg, int pos) { size_t v; int i, len; - assert(*arg->value); - if (0 == strcmp(*arg->value, "indent")) + assert(pos < (int)arg->sz && pos >= 0); + assert(arg->value[pos]); + if (0 == strcmp(arg->value[pos], "indent")) return(INDENT); - if (0 == strcmp(*arg->value, "indent-two")) + if (0 == strcmp(arg->value[pos], "indent-two")) return(INDENT * 2); - len = (int)strlen(*arg->value); + len = (int)strlen(arg->value[pos]); assert(len > 0); for (i = 0; i < len - 1; i++) - if ( ! isdigit((u_char)(*arg->value)[i])) + if ( ! isdigit((u_char)arg->value[pos][i])) break; if (i == len - 1) { - if ('n' == (*arg->value)[len - 1]) { - v = (size_t)atoi(*arg->value); + if ('n' == arg->value[pos][len - 1]) { + v = (size_t)atoi(arg->value[pos]); return(v); } } - return(strlen(*arg->value) + 1); + return(strlen(arg->value[pos]) + 1); } @@ -338,6 +361,8 @@ arg_listtype(const struct mdoc_node *n) /* FALLTHROUGH */ case (MDOC_Item): /* FALLTHROUGH */ + case (MDOC_Column): + /* FALLTHROUGH */ case (MDOC_Ohang): return(n->args->argv[i].arg); default: @@ -372,16 +397,30 @@ arg_hasattr(int arg, const struct mdoc_node *n) static int -arg_getattr(int arg, const struct mdoc_node *n) +arg_getattr(int v, const struct mdoc_node *n) { - int i; + int val; + return(arg_getattrs(&v, &val, 1, n) ? val : -1); +} + + +static int +arg_getattrs(const int *keys, int *vals, + size_t sz, const struct mdoc_node *n) +{ + int i, j, k; + if (NULL == n->args) - return(-1); - for (i = 0; i < (int)n->args->argc; i++) - if (n->args->argv[i].arg == arg) - return(i); - return(-1); + return(0); + + for (k = i = 0; i < (int)n->args->argc; i++) + for (j = 0; j < (int)sz; j++) + if (n->args->argv[i].arg == keys[j]) { + vals[j] = i; + k++; + } + return(k); } @@ -393,7 +432,7 @@ termp_dq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "``"); + word(p, "\\(lq"); p->flags |= TERMP_NOSPACE; return(1); } @@ -408,7 +447,7 @@ termp_dq_post(DECL_ARGS) return; p->flags |= TERMP_NOSPACE; - word(p, "''"); + word(p, "\\(rq"); } @@ -419,6 +458,7 @@ termp_it_pre_block(DECL_ARGS) newln(p); if ( ! arg_hasattr(MDOC_Compact, node->parent->parent)) + /* FIXME: parent->parent->parent? */ if (node->prev || node->parent->parent->prev) vspace(p); @@ -430,18 +470,15 @@ termp_it_pre_block(DECL_ARGS) static int termp_it_pre(DECL_ARGS) { - const struct mdoc_node *bl; - char buf[7]; - int i, type; - size_t width, offset; + const struct mdoc_node *bl, *n; + char buf[7]; + int i, type, keys[3], vals[3]; + size_t width, offset; if (MDOC_BLOCK == node->type) return(termp_it_pre_block(p, pair, meta, node)); - /* Get ptr to list block, type, etc. */ - bl = node->parent->parent->parent; - type = arg_listtype(bl); /* Save parent attributes. */ @@ -451,12 +488,40 @@ termp_it_pre(DECL_ARGS) /* Get list width and offset. */ - i = arg_getattr(MDOC_Width, bl); - width = i >= 0 ? arg_width(&bl->args->argv[i]) : 0; + keys[0] = MDOC_Width; + keys[1] = MDOC_Offset; + keys[2] = MDOC_Column; - i = arg_getattr(MDOC_Offset, bl); - offset = i >= 0 ? arg_offset(&bl->args->argv[i]) : 0; + vals[0] = vals[1] = vals[2] = -1; + width = offset = 0; + + (void)arg_getattrs(keys, vals, 3, bl); + + type = arg_listtype(bl); + + /* Calculate real width and offset. */ + + switch (type) { + case (MDOC_Column): + if (MDOC_BODY == node->type) + break; + for (i = 0, n = node->prev; n; n = n->prev, i++) + offset += arg_width + (&bl->args->argv[vals[2]], i); + assert(i < (int)bl->args->argv[vals[2]].sz); + width = arg_width(&bl->args->argv[vals[2]], i); + if (vals[1] >= 0) + offset += arg_offset(&bl->args->argv[vals[1]]); + break; + default: + if (vals[0] >= 0) + width = arg_width(&bl->args->argv[vals[0]], 0); + if (vals[1] >= 0) + offset = arg_offset(&bl->args->argv[vals[1]]); + break; + } + /* * List-type can override the width in the case of fixed-head * values (bullet, dash/hyphen, enum). Tags need a non-zero @@ -471,13 +536,13 @@ termp_it_pre(DECL_ARGS) case (MDOC_Enum): /* FALLTHROUGH */ case (MDOC_Hyphen): - width = width > 4 ? width : 4; + if (width < 4) + width = 4; break; case (MDOC_Tag): - if (width) - break; - errx(1, "need non-zero %s for list type", - mdoc_argnames[MDOC_Width]); + if (0 == width) + width = 10; + break; default: break; } @@ -540,6 +605,17 @@ termp_it_pre(DECL_ARGS) NULL == node->next->child) p->flags |= TERMP_NONOBREAK; break; + case (MDOC_Column): + if (MDOC_HEAD == node->type) { + assert(node->next); + if (MDOC_BODY == node->next->type) + p->flags &= ~TERMP_NOBREAK; + else + p->flags |= TERMP_NOBREAK; + if (node->prev) + p->flags |= TERMP_NOLPAD; + } + break; case (MDOC_Diag): if (MDOC_HEAD == node->type) p->flags |= TERMP_NOBREAK; @@ -570,7 +646,10 @@ termp_it_pre(DECL_ARGS) p->rmargin = p->offset + width; else p->offset += width; - /* FALLTHROUGH */ + break; + case (MDOC_Column): + p->rmargin = p->offset + width; + break; default: break; } @@ -591,7 +670,6 @@ termp_it_pre(DECL_ARGS) word(p, "\\-"); break; case (MDOC_Enum): - /* TODO: have a wordfmt or something. */ (pair->ppair->ppair->count)++; (void)snprintf(buf, sizeof(buf), "%d.", pair->ppair->ppair->count); @@ -602,24 +680,28 @@ termp_it_pre(DECL_ARGS) } /* - * If we're not going to process our header children, indicate - * so here. + * If we're not going to process our children, indicate so here. */ - if (MDOC_HEAD == node->type) - switch (type) { - case (MDOC_Bullet): - /* FALLTHROUGH */ - case (MDOC_Item): - /* FALLTHROUGH */ - case (MDOC_Dash): - /* FALLTHROUGH */ - case (MDOC_Hyphen): - /* FALLTHROUGH */ - case (MDOC_Enum): + switch (type) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Item): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + /* FALLTHROUGH */ + case (MDOC_Enum): + if (MDOC_HEAD == node->type) return(0); - default: - break; + break; + case (MDOC_Column): + if (MDOC_BODY == node->type) + return(0); + break; + default: + break; } return(1); @@ -643,10 +725,13 @@ termp_it_post(DECL_ARGS) case (MDOC_Item): /* FALLTHROUGH */ case (MDOC_Inset): - if (MDOC_BODY != node->type) - break; - flushln(p); + if (MDOC_BODY == node->type) + flushln(p); break; + case (MDOC_Column): + if (MDOC_HEAD == node->type) + flushln(p); + break; default: flushln(p); break; @@ -722,12 +807,8 @@ termp_st_pre(DECL_ARGS) { const char *cp; - if (node->child) { - if (MDOC_TEXT != node->child->type) - errx(1, "expected text line arguments"); - if ((cp = mdoc_a2st(node->child->string))) - word(p, cp); - } + if (node->child && (cp = mdoc_a2st(node->child->string))) + word(p, cp); return(0); } @@ -760,6 +841,7 @@ termp_rv_pre(DECL_ARGS) p->flags |= ttypes[TTYPE_FUNC_NAME]; word(p, *node->args->argv[i].value); p->flags &= ~ttypes[TTYPE_FUNC_NAME]; + p->flags |= TERMP_NOSPACE; word(p, "() function returns the value 0 if successful;"); word(p, "otherwise the value -1 is returned and the"); @@ -836,23 +918,15 @@ termp_xr_pre(DECL_ARGS) if (NULL == (n = node->child)) errx(1, "expected text line argument"); - if (MDOC_TEXT != n->type) - errx(1, "expected text line argument"); - word(p, n->string); - if (NULL == (n = n->next)) return(0); - if (MDOC_TEXT != n->type) - errx(1, "expected text line argument"); - p->flags |= TERMP_NOSPACE; word(p, "("); p->flags |= TERMP_NOSPACE; word(p, n->string); p->flags |= TERMP_NOSPACE; word(p, ")"); - return(0); } @@ -979,14 +1053,10 @@ termp_lb_pre(DECL_ARGS) if (NULL == node->child) errx(1, "expected text line argument"); - if (MDOC_TEXT != node->child->type) - errx(1, "expected text line argument"); - if ((lb = mdoc_a2lib(node->child->string))) { word(p, lb); return(0); } - word(p, "library"); return(1); } @@ -1092,8 +1162,6 @@ termp_fn_pre(DECL_ARGS) if (NULL == node->child) errx(1, "expected text line arguments"); - if (MDOC_TEXT != node->child->type) - errx(1, "expected text line arguments"); /* FIXME: can be "type funcname" "type varname"... */ @@ -1105,8 +1173,6 @@ termp_fn_pre(DECL_ARGS) p->flags |= TERMP_NOSPACE; for (n = node->child->next; n; n = n->next) { - if (MDOC_TEXT != n->type) - errx(1, "expected text line arguments"); p->flags |= ttypes[TTYPE_FUNC_ARG]; word(p, n->string); p->flags &= ~ttypes[TTYPE_FUNC_ARG]; @@ -1156,13 +1222,9 @@ termp_fa_pre(DECL_ARGS) } for (n = node->child; n; n = n->next) { - if (MDOC_TEXT != n->type) - errx(1, "expected text line arguments"); - p->flags |= ttypes[TTYPE_FUNC_ARG]; word(p, n->string); p->flags &= ~ttypes[TTYPE_FUNC_ARG]; - if (n->next) word(p, ","); } @@ -1192,6 +1254,7 @@ termp_bd_pre(DECL_ARGS) int i, type; if (MDOC_BLOCK == node->type) { + /* FIXME: parent prev? */ if (node->prev) vspace(p); return(1); @@ -1364,7 +1427,7 @@ termp_sq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "`"); + word(p, "\\(oq"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1378,7 +1441,7 @@ termp_sq_post(DECL_ARGS) if (MDOC_BODY != node->type) return; p->flags |= TERMP_NOSPACE; - word(p, "\'"); + word(p, "\\(aq"); } @@ -1408,8 +1471,12 @@ termp_ss_pre(DECL_ARGS) { switch (node->type) { + case (MDOC_BLOCK): + newln(p); + if (node->prev) + vspace(p); + break; case (MDOC_HEAD): - vspace(p); TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SSECTION]); p->offset = INDENT / 2; break; @@ -1525,12 +1592,8 @@ termp_at_pre(DECL_ARGS) att = NULL; - if (node->child) { - if (MDOC_TEXT != node->child->type) - errx(1, "expected text line argument"); + if (node->child) att = mdoc_a2att(node->child->string); - } - if (NULL == att) att = "AT&T UNIX"; @@ -1541,12 +1604,37 @@ termp_at_pre(DECL_ARGS) /* ARGSUSED */ static int +termp_brq_pre(DECL_ARGS) +{ + + if (MDOC_BODY != node->type) + return(1); + word(p, "\\(lC"); + p->flags |= TERMP_NOSPACE; + return(1); +} + + +/* ARGSUSED */ +static void +termp_brq_post(DECL_ARGS) +{ + + if (MDOC_BODY != node->type) + return; + p->flags |= TERMP_NOSPACE; + word(p, "\\(rC"); +} + + +/* ARGSUSED */ +static int termp_bq_pre(DECL_ARGS) { if (MDOC_BODY != node->type) return(1); - word(p, "["); + word(p, "\\(lB"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1559,7 +1647,8 @@ termp_bq_post(DECL_ARGS) if (MDOC_BODY != node->type) return; - word(p, "]"); + p->flags |= TERMP_NOSPACE; + word(p, "\\(rB"); } @@ -1621,7 +1710,9 @@ termp_fo_post(DECL_ARGS) if (MDOC_BODY != node->type) return; + p->flags |= TERMP_NOSPACE; word(p, ")"); + p->flags |= TERMP_NOSPACE; word(p, ";"); newln(p); } @@ -1717,30 +1808,52 @@ static int termp__t_pre(DECL_ARGS) { - /* FIXME: titles are underlined. */ - word(p, "\""); - p->flags |= TERMP_NOSPACE; + TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_REF_TITLE]); return(1); } /* ARGSUSED */ static void -termp__t_post(DECL_ARGS) +termp____post(DECL_ARGS) { p->flags |= TERMP_NOSPACE; - /* FIXME: titles are underlined. */ - word(p, "\""); word(p, node->next ? "," : "."); } /* ARGSUSED */ -static void -termp____post(DECL_ARGS) +static int +termp_lk_pre(DECL_ARGS) { + const struct mdoc_node *n; + if (NULL == (n = node->child)) + errx(1, "expected line argument"); + + p->flags |= ttypes[TTYPE_LINK_ANCHOR]; + word(p, n->string); + p->flags &= ~ttypes[TTYPE_LINK_ANCHOR]; p->flags |= TERMP_NOSPACE; - word(p, node->next ? "," : "."); + word(p, ":"); + + p->flags |= ttypes[TTYPE_LINK_TEXT]; + for ( ; n; n = n->next) { + word(p, n->string); + } + p->flags &= ~ttypes[TTYPE_LINK_TEXT]; + + return(0); } + + +/* ARGSUSED */ +static int +termp_mt_pre(DECL_ARGS) +{ + + TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_LINK_ANCHOR]); + return(1); +} +