-/* $Id: term.c,v 1.30 2009/02/28 21:31:13 kristaps Exp $ */
+/* $Id: term.c,v 1.35 2009/03/02 17:14:46 kristaps Exp $ */
/*
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
*
#include "term.h"
-#define INDENT 6
-
/*
* Performs actions on nodes of the abstract syntax tree. Both pre- and
* post-fix operations are defined here.
DECL_PRE(termp_bf);
DECL_PRE(termp_bsx);
DECL_PRE(termp_bt);
-DECL_PRE(termp_bx);
DECL_PRE(termp_cd);
DECL_PRE(termp_cm);
DECL_PRE(termp_em);
DECL_POST(termp___);
DECL_POST(termp_bl);
+DECL_POST(termp_bx);
const struct termact __termacts[MDOC_MAX] = {
{ NULL, NULL }, /* \" */
{ termp_bq_pre, termp_bq_post }, /* Bo */
{ termp_bq_pre, termp_bq_post }, /* Bq */
{ termp_bsx_pre, NULL }, /* Bsx */
- { termp_bx_pre, NULL }, /* Bx */
+ { NULL, termp_bx_post }, /* Bx */
{ NULL, NULL }, /* Db */
{ NULL, NULL }, /* Dc */
{ termp_dq_pre, termp_dq_post }, /* Do */
}
}
- return(strlen(*arg->value) + 2);
+ return(strlen(*arg->value) + 1);
}
p->offset = pair->offset;
p->rmargin = pair->rmargin;
- if (MDOC_HEAD == node->type) {
+ if (MDOC_HEAD == node->type)
p->flags &= ~TERMP_NOBREAK;
- p->flags &= ~TERMP_NORPAD;
- } else if (MDOC_BODY == node->type)
+ else if (MDOC_BODY == node->type)
p->flags &= ~TERMP_NOLPAD;
}
if (SEC_SYNOPSIS == node->sec)
newln(p);
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_PROG]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_PROG]);
if (NULL == node->child)
word(p, meta->name);
+
return(1);
}
termp_fl_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CMD_FLAG]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_FLAG]);
word(p, "\\-");
p->flags |= TERMP_NOSPACE;
return(1);
termp_ar_pre(DECL_ARGS)
{
- if (node->child) {
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CMD_ARG]);
- return(1);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_ARG]);
+ if (NULL == node->child) {
+ word(p, "file");
+ word(p, "...");
}
- p->flags |= ttypes[TTYPE_CMD_ARG];
- word(p, "file");
- word(p, "...");
- p->flags &= ~ttypes[TTYPE_CMD_ARG];
return(1);
}
{
/* FIXME: this can be "type name". */
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_VAR_DECL]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_VAR_DECL]);
return(1);
}
* FIXME: this naming is bad. This value is used, in general,
* for the #include header or other preprocessor statement.
*/
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_FUNC_DECL]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_DECL]);
return(1);
}
switch (node->type) {
case (MDOC_HEAD):
vspace(p);
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_SECTION]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SECTION]);
break;
case (MDOC_BODY):
p->offset = INDENT;
termp_ft_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_FUNC_TYPE]);
+ if (SEC_SYNOPSIS == node->sec)
+ if (node->prev && MDOC_Fo == node->prev->tok)
+ vspace(p);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_TYPE]);
return(1);
}
termp_ft_post(DECL_ARGS)
{
- if (node->sec == SEC_SYNOPSIS)
+ if (SEC_SYNOPSIS == node->sec)
newln(p);
}
termp_sx_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_LINK]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_LINK]);
return(1);
}
struct mdoc_node *n;
if (node->parent->tok != MDOC_Fo) {
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_FUNC_ARG]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_ARG]);
return(1);
}
termp_va_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_VAR_DECL]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_VAR_DECL]);
return(1);
}
termp_bd_pre(DECL_ARGS)
{
const struct mdoc_block *bl;
- const struct mdoc_node *n;
- int i;
+ const struct mdoc_node *n;
+ int i, type;
if (MDOC_BLOCK == node->type) {
if (node->prev)
} else if (MDOC_BODY != node->type)
return(1);
- assert(MDOC_BLOCK == node->parent->type);
pair->offset = p->offset;
-
bl = &node->parent->data.block;
+ for (type = -1, i = 0; i < (int)bl->argc; i++) {
+ switch (bl->argv[i].arg) {
+ case (MDOC_Ragged):
+ /* FALLTHROUGH */
+ case (MDOC_Filled):
+ /* FALLTHROUGH */
+ case (MDOC_Unfilled):
+ /* FALLTHROUGH */
+ case (MDOC_Literal):
+ type = bl->argv[i].arg;
+ i = (int)bl->argc;
+ break;
+ default:
+ errx(1, "display type not supported");
+ }
+ }
+
+ assert(-1 != type);
i = arg_getattr(MDOC_Offset, bl->argc, bl->argv);
if (-1 != i) {
p->offset += arg_offset(&bl->argv[i]);
}
- if ( ! arg_hasattr(MDOC_Literal, bl->argc, bl->argv))
+
+ switch (type) {
+ case (MDOC_Literal):
+ /* FALLTHROUGH */
+ case (MDOC_Unfilled):
+ break;
+ default:
return(1);
+ }
p->flags |= TERMP_LITERAL;
for (n = node->child; n; n = n->next) {
- assert(MDOC_TEXT == n->type); /* FIXME */
- if ((*n->data.text.string)) {
- word(p, n->data.text.string);
- flushln(p);
- } else
- vspace(p);
-
+ if (MDOC_TEXT != n->type) {
+ warnx("non-text children not yet allowed");
+ continue;
+ }
+ word(p, n->data.text.string);
+ flushln(p);
}
- p->flags &= ~TERMP_LITERAL;
return(0);
}
if (MDOC_BODY != node->type)
return;
- newln(p);
+
+ if ( ! (p->flags & TERMP_LITERAL))
+ flushln(p);
+
+ p->flags &= ~TERMP_LITERAL;
p->offset = pair->offset;
}
/* ARGSUSED */
-static int
-termp_bx_pre(DECL_ARGS)
+static void
+termp_bx_post(DECL_ARGS)
{
+ if (node->child)
+ p->flags |= TERMP_NOSPACE;
word(p, "BSD");
- return(1);
}
switch (node->type) {
case (MDOC_HEAD):
vspace(p);
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_SSECTION]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SSECTION]);
p->offset = INDENT / 2;
break;
default:
termp_pa_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_FILE]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FILE]);
return(1);
}
termp_em_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_EMPH]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
return(1);
}
termp_cd_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CONFIG]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CONFIG]);
+ newln(p);
return(1);
}
termp_cm_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CMD_FLAG]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_FLAG]);
return(1);
}
termp_ic_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CMD]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD]);
return(1);
}
termp_in_pre(DECL_ARGS)
{
- p->flags |= ttypes[TTYPE_INCLUDE];
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_INCLUDE]);
word(p, "#include");
word(p, "<");
- p->flags &= ~ttypes[TTYPE_INCLUDE];
p->flags |= TERMP_NOSPACE;
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_INCLUDE]);
return(1);
}
if (MDOC_BODY != node->type)
return(1);
- word(p, "(");
+ word(p, "\\&(");
p->flags |= TERMP_NOSPACE;
return(1);
}
if (NULL == (n = b->head->child)) {
if (arg_hasattr(MDOC_Emphasis, b->argc, b->argv))
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_EMPH]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
else if (arg_hasattr(MDOC_Symbolic, b->argc, b->argv))
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_SYMB]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMB]);
return(1);
}
assert(MDOC_TEXT == n->type);
if (0 == strcmp("Em", n->data.text.string))
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_EMPH]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
else if (0 == strcmp("Sy", n->data.text.string))
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_EMPH]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
return(1);
}
termp_sy_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_SYMB]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMB]);
return(1);
}
termp_ms_pre(DECL_ARGS)
{
- TERMPAIR_SETFLAG(pair, ttypes[TTYPE_SYMBOL]);
+ TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMBOL]);
return(1);
}