=================================================================== RCS file: /cvs/mandoc/term.c,v retrieving revision 1.20 retrieving revision 1.29 diff -u -p -r1.20 -r1.29 --- mandoc/term.c 2009/02/25 13:30:53 1.20 +++ mandoc/term.c 2009/02/28 20:13:06 1.29 @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.20 2009/02/25 13:30:53 kristaps Exp $ */ +/* $Id: term.c,v 1.29 2009/02/28 20:13:06 kristaps Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons * @@ -17,12 +17,15 @@ * PERFORMANCE OF THIS SOFTWARE. */ #include +#include +#include +#include #include #include #include "term.h" -#define INDENT 4 +#define INDENT 6 /* * Performs actions on nodes of the abstract syntax tree. Both pre- and @@ -30,7 +33,7 @@ */ /* FIXME: indent/tab. */ -/* FIXME: handle nested lists. */ +/* FIXME: macro arguments can be escaped. */ #define TTYPE_PROG 0 #define TTYPE_CMD_FLAG 1 @@ -105,6 +108,7 @@ 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); @@ -147,7 +151,9 @@ DECL_PRE(termp_nx); DECL_PRE(termp_ox); DECL_PRE(termp_pa); DECL_PRE(termp_pp); +DECL_PRE(termp_rs); DECL_PRE(termp_rv); +DECL_PRE(termp_sm); DECL_PRE(termp_st); DECL_PRE(termp_sx); DECL_PRE(termp_sy); @@ -156,6 +162,7 @@ DECL_PRE(termp_ux); DECL_PRE(termp_va); DECL_PRE(termp_xr); +DECL_POST(termp___); DECL_POST(termp_bl); const struct termact __termacts[MDOC_MAX] = { @@ -167,7 +174,7 @@ const struct termact __termacts[MDOC_MAX] = { { termp_ss_pre, termp_ss_post }, /* Ss */ { termp_pp_pre, NULL }, /* Pp */ { termp_d1_pre, termp_d1_post }, /* D1 */ - { NULL, NULL }, /* Dl */ + { termp_d1_pre, termp_d1_post }, /* Dl */ { termp_bd_pre, termp_bd_post }, /* Bd */ { NULL, NULL }, /* Ed */ { NULL, termp_bl_post }, /* Bl */ @@ -200,17 +207,17 @@ const struct termact __termacts[MDOC_MAX] = { { termp_va_pre, NULL }, /* Va */ { termp_vt_pre, termp_vt_post }, /* Vt */ { termp_xr_pre, NULL }, /* Xr */ - { NULL, NULL }, /* %A */ - { NULL, NULL }, /* %B */ - { NULL, NULL }, /* %D */ - { NULL, NULL }, /* %I */ - { NULL, NULL }, /* %J */ - { NULL, NULL }, /* %N */ - { NULL, NULL }, /* %O */ - { NULL, NULL }, /* %P */ - { NULL, NULL }, /* %R */ - { NULL, NULL }, /* %T */ - { NULL, NULL }, /* %V */ + { NULL, termp____post }, /* %A */ + { NULL, termp____post }, /* %B */ + { NULL, termp____post }, /* %D */ + { NULL, termp____post }, /* %I */ + { NULL, termp____post }, /* %J */ + { NULL, termp____post }, /* %N */ + { NULL, termp____post }, /* %O */ + { NULL, termp____post }, /* %P */ + { NULL, termp____post }, /* %R */ + { termp__t_pre, termp__t_post }, /* %T */ + { NULL, termp____post }, /* %V */ { NULL, NULL }, /* Ac */ { termp_aq_pre, termp_aq_post }, /* Ao */ { termp_aq_pre, termp_aq_post }, /* Aq */ @@ -244,11 +251,11 @@ const struct termact __termacts[MDOC_MAX] = { { termp_qq_pre, termp_qq_post }, /* Qo */ { termp_qq_pre, termp_qq_post }, /* Qq */ { NULL, NULL }, /* Re */ - { NULL, NULL }, /* Rs */ + { termp_rs_pre, NULL }, /* Rs */ { NULL, NULL }, /* Sc */ { termp_sq_pre, termp_sq_post }, /* So */ { termp_sq_pre, termp_sq_post }, /* Sq */ - { NULL, NULL }, /* Sm */ + { termp_sm_pre, NULL }, /* Sm */ { termp_sx_pre, NULL }, /* Sx */ { termp_sy_pre, NULL }, /* Sy */ { NULL, NULL }, /* Tn */ @@ -273,10 +280,30 @@ const struct termact *termacts = __termacts; static size_t arg_width(const struct mdoc_arg *arg) { + size_t v; + int i, len; - /* TODO */ assert(*arg->value); - return(strlen(*arg->value)); + if (0 == strcmp(*arg->value, "indent")) + return(INDENT); + if (0 == strcmp(*arg->value, "indent-two")) + return(INDENT * 2); + + len = (int)strlen(*arg->value); + assert(len > 0); + + for (i = 0; i < len - 1; i++) + if ( ! isdigit((int)(*arg->value)[i])) + break; + + if (i == len - 1) { + if ('n' == (*arg->value)[len - 1]) { + v = (size_t)atoi(*arg->value); + return(v); + } + + } + return(strlen(*arg->value) + 2); } @@ -290,7 +317,6 @@ arg_offset(const struct mdoc_arg *arg) return(INDENT); if (0 == strcmp(*arg->value, "indent-two")) return(INDENT * 2); - return(strlen(*arg->value)); } @@ -348,7 +374,8 @@ termp_it_pre(DECL_ARGS) { const struct mdoc_node *n, *it; const struct mdoc_block *bl; - int i; + char buf[7], *tp; + int i, type; size_t width, offset; switch (node->type) { @@ -364,56 +391,149 @@ termp_it_pre(DECL_ARGS) return(1); } - assert(MDOC_BLOCK == it->type); - assert(MDOC_It == it->tok); - - n = it->parent; - assert(MDOC_BODY == n->type); - assert(MDOC_Bl == n->tok); - n = n->parent; + n = it->parent->parent; bl = &n->data.block; - /* If `-compact', don't assert vertical space. */ - if (MDOC_BLOCK == node->type) { - if (arg_hasattr(MDOC_Compact, bl->argc, bl->argv)) - newln(p); - else - vspace(p); + newln(p); + if ( ! arg_hasattr(MDOC_Compact, bl->argc, bl->argv)) + if (node->prev || n->prev) + vspace(p); return(1); } + /* Get our list type. */ + + for (type = -1, i = 0; i < (int)bl->argc; i++) + switch (bl->argv[i].arg) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Enum): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + /* FALLTHROUGH */ + case (MDOC_Tag): + /* FALLTHROUGH */ + case (MDOC_Ohang): + type = bl->argv[i].arg; + i = (int)bl->argc; + break; + default: + errx(1, "list type not supported"); + /* NOTREACHED */ + } + + assert(-1 != type); + + /* Save our existing (inherited) margin and offset. */ + pair->offset = p->offset; pair->rmargin = p->rmargin; - /* FIXME: auto-size. */ + /* Get list width and offset. */ + i = arg_getattr(MDOC_Width, bl->argc, bl->argv); - width = i >= 0 ? arg_width(&bl->argv[i]) : 10; + width = i >= 0 ? arg_width(&bl->argv[i]) : 0; i = arg_getattr(MDOC_Offset, bl->argc, bl->argv); offset = i >= 0 ? arg_offset(&bl->argv[i]) : 0; - assert(MDOC_HEAD == node->type || - MDOC_BODY == node->type); + /* Override the width. */ - if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv)) { - p->flags |= TERMP_NOSPACE; + switch (type) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Enum): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + width = width > 6 ? width : 6; + break; + case (MDOC_Tag): + /* FIXME: auto-size. */ + if (0 == width) + errx(1, "need non-zero -width"); + break; + default: + break; + } - if (MDOC_HEAD == node->type) { + /* Word-wrap control. */ + + p->flags |= TERMP_NOSPACE; + + switch (type) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Enum): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + /* FALLTHROUGH */ + case (MDOC_Tag): + if (MDOC_HEAD == node->type) p->flags |= TERMP_NOBREAK; - p->offset += offset; - p->rmargin = p->offset + width; - } else { + else if (MDOC_BODY == node->type) p->flags |= TERMP_NOLPAD; - p->offset += width; + break; + default: + break; + } + + /* + * Get a token to use as the HEAD lead-in. If NULL, we use the + * HEAD child. + */ + + tp = NULL; + + if (MDOC_HEAD == node->type) { + if (arg_hasattr(MDOC_Bullet, bl->argc, bl->argv)) + tp = "\\[bu]"; + if (arg_hasattr(MDOC_Dash, bl->argc, bl->argv)) + tp = "\\-"; + if (arg_hasattr(MDOC_Enum, bl->argc, bl->argv)) { + (pair->ppair->ppair->count)++; + (void)snprintf(buf, sizeof(buf), "%d.", + pair->ppair->ppair->count); + tp = buf; } + if (arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) + tp = "\\-"; + } - } else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) { - p->flags |= TERMP_NOSPACE; - p->offset += offset; + /* Margin control. */ + + p->offset += offset; + + switch (type) { + case (MDOC_Bullet): + /* FALLTHROUGH */ + case (MDOC_Dash): + /* FALLTHROUGH */ + case (MDOC_Enum): + /* FALLTHROUGH */ + case (MDOC_Hyphen): + /* FALLTHROUGH */ + case (MDOC_Tag): + if (MDOC_HEAD == node->type) + p->rmargin = p->offset + width; + else if (MDOC_BODY == node->type) + p->offset += width; + break; + default: + break; } - return(1); + if (NULL == tp) + return(1); + + word(p, tp); + return(0); } @@ -421,46 +541,20 @@ termp_it_pre(DECL_ARGS) static void termp_it_post(DECL_ARGS) { - const struct mdoc_node *n, *it; - const struct mdoc_block *bl; - switch (node->type) { - case (MDOC_BODY): - /* FALLTHROUGH */ - case (MDOC_HEAD): - break; - default: + if (MDOC_BODY != node->type && MDOC_HEAD != node->type) return; - } - it = node->parent; - assert(MDOC_BLOCK == it->type); - assert(MDOC_It == it->tok); + flushln(p); - n = it->parent; - assert(MDOC_BODY == n->type); - assert(MDOC_Bl == n->tok); - n = n->parent; - bl = &n->data.block; + p->offset = pair->offset; + p->rmargin = pair->rmargin; - /* If `-tag', adjust our margins accordingly. */ - - if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv)) { - flushln(p); - - if (MDOC_HEAD == node->type) { - p->rmargin = pair->rmargin; - p->offset = pair->offset; - p->flags &= ~TERMP_NOBREAK; - } else { - p->offset = pair->offset; - p->flags &= ~TERMP_NOLPAD; - } - - } else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) { - flushln(p); - p->offset = pair->offset; - } + if (MDOC_HEAD == node->type) { + p->flags &= ~TERMP_NOBREAK; + p->flags &= ~TERMP_NORPAD; + } else if (MDOC_BODY == node->type) + p->flags &= ~TERMP_NOLPAD; } @@ -469,6 +563,9 @@ static int termp_nm_pre(DECL_ARGS) { + if (SEC_SYNOPSIS == node->sec) + newln(p); + TERMPAIR_SETFLAG(pair, ttypes[TTYPE_PROG]); if (NULL == node->child) word(p, meta->name); @@ -493,9 +590,14 @@ static int termp_ar_pre(DECL_ARGS) { - TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CMD_ARG]); - if (NULL == node->child) - word(p, "..."); + if (node->child) { + TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CMD_ARG]); + return(1); + } + p->flags |= ttypes[TTYPE_CMD_ARG]; + word(p, "file"); + word(p, "..."); + p->flags &= ~ttypes[TTYPE_CMD_ARG]; return(1); } @@ -537,6 +639,17 @@ termp_st_pre(DECL_ARGS) /* ARGSUSED */ static int +termp_rs_pre(DECL_ARGS) +{ + + if (MDOC_BLOCK == node->type) + vspace(p); + return(1); +} + + +/* ARGSUSED */ +static int termp_rv_pre(DECL_ARGS) { int i; @@ -1082,7 +1195,7 @@ termp_sq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "\'"); + word(p, "`"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1239,7 +1352,7 @@ termp_bq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "["); + word(p, "\\["); p->flags |= TERMP_NOSPACE; return(1); } @@ -1263,7 +1376,7 @@ termp_pq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "("); + word(p, "\\&("); p->flags |= TERMP_NOSPACE; return(1); } @@ -1374,3 +1487,55 @@ termp_ms_pre(DECL_ARGS) return(1); } + + +/* ARGSUSED */ +static int +termp_sm_pre(DECL_ARGS) +{ + +#if notyet + assert(node->child); + if (0 == strcmp("off", node->child->data.text.string)) { + p->flags &= ~TERMP_NONOSPACE; + p->flags &= ~TERMP_NOSPACE; + } else { + p->flags |= TERMP_NONOSPACE; + p->flags |= TERMP_NOSPACE; + } +#endif + + return(0); +} + + +/* ARGSUSED */ +static int +termp__t_pre(DECL_ARGS) +{ + + word(p, "\""); + p->flags |= TERMP_NOSPACE; + return(1); +} + + +/* ARGSUSED */ +static void +termp__t_post(DECL_ARGS) +{ + + p->flags |= TERMP_NOSPACE; + word(p, "\""); + word(p, node->next ? "," : "."); +} + + +/* ARGSUSED */ +static void +termp____post(DECL_ARGS) +{ + + p->flags |= TERMP_NOSPACE; + word(p, node->next ? "," : "."); +}