-/* $Id: mdoc_term.c,v 1.86 2009/10/03 19:02:45 kristaps Exp $ */
+/* $Id: mdoc_term.c,v 1.95 2009/10/24 05:52:14 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
#include <stdlib.h>
#include <string.h>
+#include "out.h"
#include "term.h"
#include "mdoc.h"
-
-/* FIXME: check HANG lists: they seem to be broken... :
- * .Bl -hang -width Ds
- * .It a
- * b
- * .It Fl f Ns Ar option...
- * Override default compiler behaviour. See
- * .Sx Compiler Options
- * for details.
- * Override default compiler behaviour. See
- * .Sx Compiler Options
- * for details.
- * Override default compiler behaviour. See
- * .Sx Compiler Options
- * for details.
- * Override default compiler behaviour. See
- * .Sx Compiler Options
- * for details.
- * .
- * .It a sasd fasd as afsd sfad sfds sadfs sd sfd ssfad asfd
- * Override default compiler behaviour. See
- * .Sx Compiler Options
- * for details.
- * Override default compiler behaviour. See
- * .Sx Compiler Options
- * for details.
- * Override default compiler behaviour. See
- * .Sx Compiler Options
- * for details.
- * Override default compiler behaviour. See
- * .Sx Compiler Options
- * for details.
- * .El
- *
- */
+#include "chars.h"
+#include "main.h"
#define INDENT 5
#define HALFINDENT 3
{ NULL, termp____post }, /* %Q */
{ termp_sp_pre, NULL }, /* br */
{ termp_sp_pre, NULL }, /* sp */
+ { NULL, termp____post }, /* %U */
};
#ifdef __linux__
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 size_t arg_offset(const struct mdoc_argv *);
-static size_t arg_width(const struct mdoc_argv *, int);
static int arg_listtype(const struct mdoc_node *);
-static void fmt_block_vspace(struct termp *,
+static void print_bvspace(struct termp *,
const struct mdoc_node *,
const struct mdoc_node *);
static void print_node(DECL_ARGS);
void
-mdoc_run(struct termp *p, const struct mdoc *mdoc)
+terminal_mdoc(void *arg, const struct mdoc *mdoc)
{
const struct mdoc_node *n;
const struct mdoc_meta *m;
+ struct termp *p;
+
+ p = (struct termp *)arg;
+
+ if (NULL == p->symtab)
+ switch (p->enc) {
+ case (TERMENC_ASCII):
+ p->symtab = chars_init(CHARS_ASCII);
+ break;
+ default:
+ abort();
+ /* NOTREACHED */
+ }
n = mdoc_node(mdoc);
m = mdoc_meta(mdoc);
static void
print_foot(DECL_ARGS)
{
- struct tm *tm;
- char *buf, *os;
+ char buf[DATESIZ];
+ char *os;
/*
* Output the footer in new-groff style, that is, three columns
* SYSTEM DATE SYSTEM
*/
- if (NULL == (buf = malloc(p->rmargin)))
- err(EXIT_FAILURE, "malloc");
if (NULL == (os = malloc(p->rmargin)))
err(EXIT_FAILURE, "malloc");
- tm = localtime(&m->date);
-
- if (0 == strftime(buf, p->rmargin, "%B %e, %Y", tm))
- err(EXIT_FAILURE, "strftime");
+ time2a(m->date, buf, DATESIZ);
(void)strlcpy(os, m->os, p->rmargin);
p->rmargin = p->maxrmargin;
p->flags = 0;
- free(buf);
free(os);
}
}
-/* FIXME: put in utility file for front-ends. */
static size_t
-arg_width(const struct mdoc_argv *arg, int pos)
+arg2height(const struct mdoc_node *n)
{
- int i, len;
- const char *p;
+ struct roffsu su;
- assert(pos < (int)arg->sz && pos >= 0);
- assert(arg->value[pos]);
+ assert(MDOC_TEXT == n->type);
+ assert(n->string);
+ if ( ! a2roffsu(n->string, &su, SCALE_VS))
+ SCALE_VS_INIT(&su, strlen(n->string));
- p = arg->value[pos];
+ return(term_vspan(&su));
+}
- if (0 == (len = (int)strlen(p)))
- return(0);
- for (i = 0; i < len - 1; i++)
- if ( ! isdigit((u_char)p[i]))
- break;
+static size_t
+arg2width(const struct mdoc_argv *arg, int pos)
+{
+ struct roffsu su;
- if (i == len - 1)
- if ('n' == p[len - 1] || 'm' == p[len - 1])
- return((size_t)atoi(p) + 2);
+ assert(arg->value[pos]);
+ if ( ! a2roffsu(arg->value[pos], &su, SCALE_MAX))
+ SCALE_HS_INIT(&su, strlen(arg->value[pos]));
- return((size_t)len + 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)
{
}
-/* FIXME: put in utility file for front-ends. */
static size_t
-arg_offset(const struct mdoc_argv *arg)
+arg2offs(const struct mdoc_argv *arg)
{
- int len, i;
- const char *p;
-
- assert(*arg->value);
- p = *arg->value;
+ struct roffsu su;
- if (0 == strcmp(p, "left"))
+ if ('\0' == arg->value[0][0])
return(0);
- if (0 == strcmp(p, "indent"))
+ else if (0 == strcmp(arg->value[0], "left"))
+ return(0);
+ else if (0 == strcmp(arg->value[0], "indent"))
return(INDENT + 1);
- if (0 == strcmp(p, "indent-two"))
+ else if (0 == strcmp(arg->value[0], "indent-two"))
return((INDENT + 1) * 2);
+ else if ( ! a2roffsu(arg->value[0], &su, SCALE_MAX))
+ SCALE_HS_INIT(&su, strlen(arg->value[0]));
- if (0 == (len = (int)strlen(p)))
- return(0);
-
- for (i = 0; i < len - 1; i++)
- if ( ! isdigit((u_char)p[i]))
- break;
-
- if (i == len - 1)
- if ('n' == p[len - 1] || 'm' == p[len - 1])
- return((size_t)atoi(p));
-
- return((size_t)len);
+ return(term_hspan(&su));
}
}
-/* ARGSUSED */
static void
-fmt_block_vspace(struct termp *p,
+print_bvspace(struct termp *p,
const struct mdoc_node *bl,
const struct mdoc_node *n)
{
size_t width, offset;
if (MDOC_BLOCK == n->type) {
- fmt_block_vspace(p, n->parent->parent, n);
+ print_bvspace(p, n->parent->parent, n);
return(1);
}
for (i = 0, nn = n->prev; nn &&
i < (int)bl->args->argv[vals[2]].sz;
nn = nn->prev, i++)
- offset += arg_width
+ offset += arg2width
(&bl->args->argv[vals[2]], i);
/* Whether exceeds maximum column. */
if (i < (int)bl->args->argv[vals[2]].sz)
- width = arg_width(&bl->args->argv[vals[2]], i);
+ width = arg2width(&bl->args->argv[vals[2]], i);
else
width = 0;
if (vals[1] >= 0)
- offset += arg_offset(&bl->args->argv[vals[1]]);
+ offset += arg2offs(&bl->args->argv[vals[1]]);
break;
default:
if (vals[0] >= 0)
- width = arg_width(&bl->args->argv[vals[0]], 0);
+ width = arg2width(&bl->args->argv[vals[0]], 0);
if (vals[1] >= 0)
- offset += arg_offset(&bl->args->argv[vals[1]]);
+ offset += arg2offs(&bl->args->argv[vals[1]]);
break;
}
const struct mdoc_node *nn;
if (MDOC_BLOCK == n->type) {
- fmt_block_vspace(p, n, n);
+ print_bvspace(p, n, n);
return(1);
} else if (MDOC_BODY != n->type)
return(1);
for (type = -1, i = 0; i < (int)nn->args->argc; i++) {
switch (nn->args->argv[i].arg) {
+ case (MDOC_Centred):
+ /* FALLTHROUGH */
case (MDOC_Ragged):
/* FALLTHROUGH */
case (MDOC_Filled):
type = nn->args->argv[i].arg;
break;
case (MDOC_Offset):
- p->offset += arg_offset(&nn->args->argv[i]);
+ p->offset += arg2offs(&nn->args->argv[i]);
break;
default:
break;
return(1);
for (nn = n->child; nn; nn = nn->next) {
+ p->flags |= TERMP_NOSPACE;
print_node(p, pair, m, nn);
if (NULL == nn->next)
continue;
if (nn->prev && nn->prev->line < nn->line)
term_flushln(p);
+ else if (NULL == nn->prev)
+ term_flushln(p);
}
return(0);
static int
termp_sp_pre(DECL_ARGS)
{
- int i, len;
+ size_t i, len;
switch (n->tok) {
case (MDOC_sp):
- len = n->child ? atoi(n->child->string) : 1;
+ len = n->child ? arg2height(n->child) : 1;
break;
case (MDOC_br):
len = 0;
termp____post(DECL_ARGS)
{
+ /* TODO: %U. */
+
p->flags |= TERMP_NOSPACE;
switch (n->tok) {
case (MDOC__T):