-/* $Id: term.c,v 1.23 2009/02/25 23:18:50 kristaps Exp $ */
+/* $Id: term.c,v 1.30 2009/02/28 21:31:13 kristaps Exp $ */
/*
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
*
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
+#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include "term.h"
-#define INDENT 4
+#define INDENT 6
/*
* Performs actions on nodes of the abstract syntax tree. Both pre- and
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_fn);
DECL_PREPOST(termp_fo);
DECL_PREPOST(termp_ft);
+DECL_PREPOST(termp_in);
DECL_PREPOST(termp_it);
DECL_PREPOST(termp_op);
DECL_PREPOST(termp_pf);
DECL_PRE(termp_fl);
DECL_PRE(termp_fx);
DECL_PRE(termp_ic);
-DECL_PRE(termp_in);
DECL_PRE(termp_ms);
DECL_PRE(termp_nd);
DECL_PRE(termp_nm);
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_va);
DECL_PRE(termp_xr);
+DECL_POST(termp___);
DECL_POST(termp_bl);
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 */
{ termp_fn_pre, termp_fn_post }, /* Fn */
{ termp_ft_pre, termp_ft_post }, /* Ft */
{ termp_ic_pre, NULL }, /* Ic */
- { termp_in_pre, NULL }, /* In */
+ { termp_in_pre, termp_in_post }, /* In */
{ NULL, NULL }, /* Li */
{ termp_nd_pre, NULL }, /* Nd */
{ termp_nm_pre, NULL }, /* Nm */
{ 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 */
{ 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 */
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);
}
return(INDENT);
if (0 == strcmp(*arg->value, "indent-two"))
return(INDENT * 2);
-
return(strlen(*arg->value));
}
bl = &n->data.block;
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 (i = 0; i < (int)bl->argc; i++)
+ for (type = -1, i = 0; i < (int)bl->argc; i++)
switch (bl->argv[i].arg) {
case (MDOC_Bullet):
/* FALLTHROUGH */
/* FALLTHROUGH */
case (MDOC_Hyphen):
/* FALLTHROUGH */
- case (MDOC_Item):
- /* FALLTHROUGH */
case (MDOC_Tag):
/* FALLTHROUGH */
case (MDOC_Ohang):
type = bl->argv[i].arg;
i = (int)bl->argc;
break;
- case (MDOC_Inset):
- /* FALLTHROUGH */
- case (MDOC_Hang):
- /* FALLTHROUGH */
- case (MDOC_Diag):
- /* FALLTHROUGH */
- case (MDOC_Column):
+ default:
errx(1, "list type not supported");
/* NOTREACHED */
- default:
- break;
}
+ assert(-1 != type);
+
/* Save our existing (inherited) margin and offset. */
pair->offset = p->offset;
/* Get list width and offset. */
- /* FIXME: auto-size. */
i = arg_getattr(MDOC_Width, bl->argc, bl->argv);
width = i >= 0 ? arg_width(&bl->argv[i]) : 0;
width = width > 6 ? width : 6;
break;
case (MDOC_Tag):
+ /* FIXME: auto-size. */
if (0 == width)
errx(1, "need non-zero -width");
break;
else if (MDOC_BODY == node->type)
p->flags |= TERMP_NOLPAD;
break;
- case (MDOC_Ohang):
+ default:
break;
}
*/
tp = NULL;
+
if (MDOC_HEAD == node->type) {
if (arg_hasattr(MDOC_Bullet, bl->argc, bl->argv))
tp = "\\[bu]";
}
if (arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv))
tp = "\\-";
- if (arg_hasattr(MDOC_Item, bl->argc, bl->argv))
- tp = "";
}
/* Margin control. */
/* FALLTHROUGH */
case (MDOC_Hyphen):
/* FALLTHROUGH */
- case (MDOC_Item):
- /* FALLTHROUGH */
case (MDOC_Tag):
if (MDOC_HEAD == node->type)
p->rmargin = p->offset + width;
else if (MDOC_BODY == node->type)
p->offset += width;
break;
- case (MDOC_Ohang):
+ default:
break;
}
if (NULL == tp)
return(1);
- /* XXX - ignoring children. */
-
word(p, tp);
return(0);
}
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);
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);
}
}
+/* ARGSUSED */
+static int
+termp_rs_pre(DECL_ARGS)
+{
+
+ if (MDOC_BLOCK == node->type && node->prev)
+ vspace(p);
+ return(1);
+}
+
+
/* ARGSUSED */
static int
termp_rv_pre(DECL_ARGS)
termp_fd_post(DECL_ARGS)
{
- if (node->sec == SEC_SYNOPSIS)
+ if (node->sec != SEC_SYNOPSIS)
+ return;
+ newln(p);
+ if (node->next && MDOC_Fd != node->next->tok)
vspace(p);
}
termp_fn_post(DECL_ARGS)
{
- if (node->sec == SEC_SYNOPSIS)
+ if (node->sec == SEC_SYNOPSIS && node->next)
vspace(p);
}
int i;
if (MDOC_BLOCK == node->type) {
- vspace(p);
+ if (node->prev)
+ vspace(p);
return(1);
} else if (MDOC_BODY != node->type)
return(1);
if (MDOC_BODY != node->type)
return(1);
- word(p, "\'");
+ word(p, "`");
p->flags |= TERMP_NOSPACE;
return(1);
}
termp_in_pre(DECL_ARGS)
{
+ p->flags |= 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);
}
+/* ARGSUSED */
+static void
+termp_in_post(DECL_ARGS)
+{
+
+ p->flags |= TERMP_NOSPACE;
+ word(p, ">");
+
+ newln(p);
+ if (SEC_SYNOPSIS != node->sec)
+ return;
+ if (node->next && MDOC_In != node->next->tok)
+ vspace(p);
+}
+
+
/* ARGSUSED */
static int
termp_at_pre(DECL_ARGS)
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 ? "," : ".");
+}