version 1.166, 2010/07/02 10:53:28 |
version 1.186, 2010/09/25 15:51:30 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv> |
|
* Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
#include "mandoc.h" |
#include "mandoc.h" |
#include "out.h" |
#include "out.h" |
#include "term.h" |
#include "term.h" |
#include "regs.h" |
|
#include "mdoc.h" |
#include "mdoc.h" |
#include "chars.h" |
#include "chars.h" |
#include "main.h" |
#include "main.h" |
Line 57 static size_t a2width(const struct termp *, const ch |
|
Line 57 static size_t a2width(const struct termp *, const ch |
|
static size_t a2height(const struct termp *, const char *); |
static size_t a2height(const struct termp *, const char *); |
static size_t a2offs(const struct termp *, const char *); |
static size_t a2offs(const struct termp *, const char *); |
|
|
static int arg_hasattr(int, const struct mdoc_node *); |
|
static int arg_getattr(int, const struct mdoc_node *); |
|
static void print_bvspace(struct termp *, |
static void print_bvspace(struct termp *, |
const struct mdoc_node *, |
const struct mdoc_node *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
Line 94 static void termp_sh_post(DECL_ARGS); |
|
Line 92 static void termp_sh_post(DECL_ARGS); |
|
static void termp_sq_post(DECL_ARGS); |
static void termp_sq_post(DECL_ARGS); |
static void termp_ss_post(DECL_ARGS); |
static void termp_ss_post(DECL_ARGS); |
|
|
|
static int termp__a_pre(DECL_ARGS); |
static int termp_an_pre(DECL_ARGS); |
static int termp_an_pre(DECL_ARGS); |
static int termp_ap_pre(DECL_ARGS); |
static int termp_ap_pre(DECL_ARGS); |
static int termp_aq_pre(DECL_ARGS); |
static int termp_aq_pre(DECL_ARGS); |
Line 153 static const struct termact termacts[MDOC_MAX] = { |
|
Line 152 static const struct termact termacts[MDOC_MAX] = { |
|
{ termp_bl_pre, termp_bl_post }, /* Bl */ |
{ termp_bl_pre, termp_bl_post }, /* Bl */ |
{ NULL, NULL }, /* El */ |
{ NULL, NULL }, /* El */ |
{ termp_it_pre, termp_it_post }, /* It */ |
{ termp_it_pre, termp_it_post }, /* It */ |
{ NULL, NULL }, /* Ad */ |
{ termp_under_pre, NULL }, /* Ad */ |
{ termp_an_pre, termp_an_post }, /* An */ |
{ termp_an_pre, termp_an_post }, /* An */ |
{ termp_under_pre, NULL }, /* Ar */ |
{ termp_under_pre, NULL }, /* Ar */ |
{ termp_cd_pre, NULL }, /* Cd */ |
{ termp_cd_pre, NULL }, /* Cd */ |
Line 180 static const struct termact termacts[MDOC_MAX] = { |
|
Line 179 static const struct termact termacts[MDOC_MAX] = { |
|
{ termp_under_pre, NULL }, /* Va */ |
{ termp_under_pre, NULL }, /* Va */ |
{ termp_vt_pre, NULL }, /* Vt */ |
{ termp_vt_pre, NULL }, /* Vt */ |
{ termp_xr_pre, NULL }, /* Xr */ |
{ termp_xr_pre, NULL }, /* Xr */ |
{ NULL, termp____post }, /* %A */ |
{ termp__a_pre, termp____post }, /* %A */ |
{ termp_under_pre, termp____post }, /* %B */ |
{ termp_under_pre, termp____post }, /* %B */ |
{ NULL, termp____post }, /* %D */ |
{ NULL, termp____post }, /* %D */ |
{ termp_under_pre, termp____post }, /* %I */ |
{ termp_under_pre, termp____post }, /* %I */ |
Line 210 static const struct termact termacts[MDOC_MAX] = { |
|
Line 209 static const struct termact termacts[MDOC_MAX] = { |
|
{ termp_under_pre, NULL }, /* Em */ |
{ termp_under_pre, NULL }, /* Em */ |
{ NULL, NULL }, /* Eo */ |
{ NULL, NULL }, /* Eo */ |
{ termp_xx_pre, NULL }, /* Fx */ |
{ termp_xx_pre, NULL }, /* Fx */ |
{ termp_bold_pre, NULL }, /* Ms */ /* FIXME: convert to symbol? */ |
{ termp_bold_pre, NULL }, /* Ms */ |
{ NULL, NULL }, /* No */ |
{ NULL, NULL }, /* No */ |
{ termp_ns_pre, NULL }, /* Ns */ |
{ termp_ns_pre, NULL }, /* Ns */ |
{ termp_xx_pre, NULL }, /* Nx */ |
{ termp_xx_pre, NULL }, /* Nx */ |
Line 331 print_mdoc_node(DECL_ARGS) |
|
Line 330 print_mdoc_node(DECL_ARGS) |
|
else if (termacts[n->tok].pre && ENDBODY_NOT == n->end) |
else if (termacts[n->tok].pre && ENDBODY_NOT == n->end) |
chld = (*termacts[n->tok].pre)(p, &npair, m, n); |
chld = (*termacts[n->tok].pre)(p, &npair, m, n); |
|
|
|
/* |
|
* Keeps only work until the end of a line. If a keep was |
|
* invoked in a prior line, revert it to PREKEEP. |
|
*/ |
|
|
|
if (TERMP_KEEP & p->flags) { |
|
if (n->prev && n->prev->line != n->line) { |
|
p->flags &= ~TERMP_KEEP; |
|
p->flags |= TERMP_PREKEEP; |
|
} else if (NULL == n->prev) { |
|
if (n->parent && n->parent->line != n->line) { |
|
p->flags &= ~TERMP_KEEP; |
|
p->flags |= TERMP_PREKEEP; |
|
} |
|
} |
|
} |
|
|
if (chld && n->child) |
if (chld && n->child) |
print_mdoc_nodelist(p, &npair, m, n->child); |
print_mdoc_nodelist(p, &npair, m, n->child); |
|
|
Line 528 a2offs(const struct termp *p, const char *v) |
|
Line 544 a2offs(const struct termp *p, const char *v) |
|
|
|
|
|
/* |
/* |
* Return 1 if an argument has a particular argument value or 0 if it |
|
* does not. See arg_getattr(). |
|
*/ |
|
static int |
|
arg_hasattr(int arg, const struct mdoc_node *n) |
|
{ |
|
|
|
return(-1 != arg_getattr(arg, n)); |
|
} |
|
|
|
|
|
/* |
|
* Get the index of an argument in a node's argument list or -1 if it |
|
* does not exist. |
|
*/ |
|
static int |
|
arg_getattr(int v, const struct mdoc_node *n) |
|
{ |
|
int i; |
|
|
|
if (NULL == n->args) |
|
return(0); |
|
|
|
for (i = 0; i < (int)n->args->argc; i++) |
|
if (n->args->argv[i].arg == v) |
|
return(i); |
|
|
|
return(-1); |
|
} |
|
|
|
|
|
/* |
|
* Determine how much space to print out before block elements of `It' |
* Determine how much space to print out before block elements of `It' |
* (and thus `Bl') and `Bd'. And then go ahead and print that space, |
* (and thus `Bl') and `Bd'. And then go ahead and print that space, |
* too. |
* too. |
Line 644 termp_it_pre(DECL_ARGS) |
|
Line 628 termp_it_pre(DECL_ARGS) |
|
{ |
{ |
const struct mdoc_node *bl, *nn; |
const struct mdoc_node *bl, *nn; |
char buf[7]; |
char buf[7]; |
int i, col; |
int i; |
size_t width, offset, ncols, dcol; |
size_t width, offset, ncols, dcol; |
enum mdoc_list type; |
enum mdoc_list type; |
|
|
Line 673 termp_it_pre(DECL_ARGS) |
|
Line 657 termp_it_pre(DECL_ARGS) |
|
if (MDOC_HEAD == n->type) |
if (MDOC_HEAD == n->type) |
break; |
break; |
|
|
col = arg_getattr(MDOC_Column, bl); |
|
|
|
/* |
/* |
* Imitate groff's column handling: |
* Imitate groff's column handling: |
* - For each earlier column, add its width. |
* - For each earlier column, add its width. |
Line 684 termp_it_pre(DECL_ARGS) |
|
Line 666 termp_it_pre(DECL_ARGS) |
|
* column. |
* column. |
* - For more than 5 columns, add only one column. |
* - For more than 5 columns, add only one column. |
*/ |
*/ |
ncols = bl->args->argv[col].sz; |
ncols = bl->data.Bl->ncols; |
|
|
/* LINTED */ |
/* LINTED */ |
dcol = ncols < 5 ? term_len(p, 4) : |
dcol = ncols < 5 ? term_len(p, 4) : |
ncols == 5 ? term_len(p, 3) : term_len(p, 1); |
ncols == 5 ? term_len(p, 3) : term_len(p, 1); |
Line 698 termp_it_pre(DECL_ARGS) |
|
Line 681 termp_it_pre(DECL_ARGS) |
|
nn->prev && i < (int)ncols; |
nn->prev && i < (int)ncols; |
nn = nn->prev, i++) |
nn = nn->prev, i++) |
offset += dcol + a2width |
offset += dcol + a2width |
(p, bl->args->argv[col].value[i]); |
(p, bl->data.Bl->cols[i]); |
|
|
/* |
/* |
* When exceeding the declared number of columns, leave |
* When exceeding the declared number of columns, leave |
Line 713 termp_it_pre(DECL_ARGS) |
|
Line 696 termp_it_pre(DECL_ARGS) |
|
* Use the declared column widths, extended as explained |
* Use the declared column widths, extended as explained |
* in the preceding paragraph. |
* in the preceding paragraph. |
*/ |
*/ |
width = a2width(p, bl->args->argv[col].value[i]) + dcol; |
width = a2width(p, bl->data.Bl->cols[i]) + dcol; |
break; |
break; |
default: |
default: |
if (NULL == bl->data.Bl->width) |
if (NULL == bl->data.Bl->width) |
Line 1044 termp_nm_pre(DECL_ARGS) |
|
Line 1027 termp_nm_pre(DECL_ARGS) |
|
if (NULL == n->child && NULL == m->name) |
if (NULL == n->child && NULL == m->name) |
return(0); |
return(0); |
|
|
synopsis_pre(p, n); |
if (MDOC_HEAD == n->type) |
|
synopsis_pre(p, n->parent); |
|
|
if (MDOC_HEAD == n->type && n->next->child) { |
if (MDOC_HEAD == n->type && n->next->child) { |
p->flags |= TERMP_NOSPACE | TERMP_NOBREAK | TERMP_HANG; |
p->flags |= TERMP_NOSPACE | TERMP_NOBREAK; |
p->rmargin = p->offset + term_len(p, 1) + |
p->rmargin = p->offset + term_len(p, 1); |
(NULL == n->child ? term_strlen(p, m->name) : |
if (NULL == n->child) { |
MDOC_TEXT == n->child->type ? |
p->rmargin += term_strlen(p, m->name); |
term_strlen(p, n->child->string) : |
} else if (MDOC_TEXT == n->child->type) { |
term_len(p, 5)); |
p->rmargin += term_strlen(p, n->child->string); |
|
if (n->child->next) |
|
p->flags |= TERMP_HANG; |
|
} else { |
|
p->rmargin += term_len(p, 5); |
|
p->flags |= TERMP_HANG; |
|
} |
} |
} |
|
|
term_fontpush(p, TERMFONT_BOLD); |
term_fontpush(p, TERMFONT_BOLD); |
Line 1096 termp_fl_pre(DECL_ARGS) |
|
Line 1086 termp_fl_pre(DECL_ARGS) |
|
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
static int |
static int |
|
termp__a_pre(DECL_ARGS) |
|
{ |
|
|
|
if (n->prev && MDOC__A == n->prev->tok) |
|
if (NULL == n->next || MDOC__A != n->next->tok) |
|
term_word(p, "and"); |
|
|
|
return(1); |
|
} |
|
|
|
|
|
/* ARGSUSED */ |
|
static int |
termp_an_pre(DECL_ARGS) |
termp_an_pre(DECL_ARGS) |
{ |
{ |
|
|
Line 1141 termp_an_post(DECL_ARGS) |
|
Line 1144 termp_an_post(DECL_ARGS) |
|
return; |
return; |
} |
} |
|
|
if (arg_hasattr(MDOC_Split, n)) { |
if (AUTH_split == n->data.An.auth) { |
p->flags &= ~TERMP_NOSPLIT; |
p->flags &= ~TERMP_NOSPLIT; |
p->flags |= TERMP_SPLIT; |
p->flags |= TERMP_SPLIT; |
} else { |
} else if (AUTH_nosplit == n->data.An.auth) { |
p->flags &= ~TERMP_SPLIT; |
p->flags &= ~TERMP_SPLIT; |
p->flags |= TERMP_NOSPLIT; |
p->flags |= TERMP_NOSPLIT; |
} |
} |
Line 1638 termp_fa_pre(DECL_ARGS) |
|
Line 1641 termp_fa_pre(DECL_ARGS) |
|
static int |
static int |
termp_bd_pre(DECL_ARGS) |
termp_bd_pre(DECL_ARGS) |
{ |
{ |
size_t tabwidth; |
size_t tabwidth, rm, rmax; |
size_t rm, rmax; |
|
const struct mdoc_node *nn; |
const struct mdoc_node *nn; |
|
|
if (MDOC_BLOCK == n->type) { |
if (MDOC_BLOCK == n->type) { |
Line 1671 termp_bd_pre(DECL_ARGS) |
|
Line 1673 termp_bd_pre(DECL_ARGS) |
|
p->rmargin = p->maxrmargin = TERM_MAXMARGIN; |
p->rmargin = p->maxrmargin = TERM_MAXMARGIN; |
|
|
for (nn = n->child; nn; nn = nn->next) { |
for (nn = n->child; nn; nn = nn->next) { |
p->flags |= TERMP_NOSPACE; |
|
print_mdoc_node(p, pair, m, nn); |
print_mdoc_node(p, pair, m, nn); |
if (NULL == nn->prev || |
if (nn->next && nn->next->line == nn->line) |
nn->prev->line < nn->line || |
continue; |
NULL == nn->next) |
term_flushln(p); |
term_flushln(p); |
p->flags |= TERMP_NOSPACE; |
} |
} |
|
|
p->tabwidth = tabwidth; |
p->tabwidth = tabwidth; |
Line 1923 termp_sp_pre(DECL_ARGS) |
|
Line 1924 termp_sp_pre(DECL_ARGS) |
|
len = 0; |
len = 0; |
break; |
break; |
default: |
default: |
|
assert(n->parent); |
|
if ((NULL == n->next || NULL == n->prev) && |
|
(MDOC_Ss == n->parent->tok || |
|
MDOC_Sh == n->parent->tok)) |
|
return(0); |
len = 1; |
len = 1; |
break; |
break; |
} |
} |
Line 2025 termp_fo_pre(DECL_ARGS) |
|
Line 2031 termp_fo_pre(DECL_ARGS) |
|
return(1); |
return(1); |
} |
} |
|
|
|
if (NULL == n->child) |
|
return(0); |
|
|
/* XXX: we drop non-initial arguments as per groff. */ |
/* XXX: we drop non-initial arguments as per groff. */ |
|
|
assert(n->child); |
|
assert(n->child->string); |
assert(n->child->string); |
term_fontpush(p, TERMFONT_BOLD); |
term_fontpush(p, TERMFONT_BOLD); |
term_word(p, n->child->string); |
term_word(p, n->child->string); |
Line 2057 termp_fo_post(DECL_ARGS) |
|
Line 2065 termp_fo_post(DECL_ARGS) |
|
static int |
static int |
termp_bf_pre(DECL_ARGS) |
termp_bf_pre(DECL_ARGS) |
{ |
{ |
const struct mdoc_node *nn; |
|
|
|
if (MDOC_HEAD == n->type) |
if (MDOC_HEAD == n->type) |
return(0); |
return(0); |
else if (MDOC_BLOCK != n->type) |
else if (MDOC_BLOCK != n->type) |
return(1); |
return(1); |
|
|
if (NULL == (nn = n->head->child)) { |
assert(n->data.Bf); |
if (arg_hasattr(MDOC_Emphasis, n)) |
|
term_fontpush(p, TERMFONT_UNDER); |
|
else if (arg_hasattr(MDOC_Symbolic, n)) |
|
term_fontpush(p, TERMFONT_BOLD); |
|
else |
|
term_fontpush(p, TERMFONT_NONE); |
|
|
|
return(1); |
if (FONT_Em == n->data.Bf->font) |
} |
|
|
|
assert(MDOC_TEXT == nn->type); |
|
if (0 == strcmp("Em", nn->string)) |
|
term_fontpush(p, TERMFONT_UNDER); |
term_fontpush(p, TERMFONT_UNDER); |
else if (0 == strcmp("Sy", nn->string)) |
else if (FONT_Sy == n->data.Bf->font) |
term_fontpush(p, TERMFONT_BOLD); |
term_fontpush(p, TERMFONT_BOLD); |
else |
else |
term_fontpush(p, TERMFONT_NONE); |
term_fontpush(p, TERMFONT_NONE); |
|
|
return(1); |
return(1); |
Line 2093 termp_sm_pre(DECL_ARGS) |
|
Line 2090 termp_sm_pre(DECL_ARGS) |
|
{ |
{ |
|
|
assert(n->child && MDOC_TEXT == n->child->type); |
assert(n->child && MDOC_TEXT == n->child->type); |
if (0 == strcmp("on", n->child->string)) |
if (0 == strcmp("on", n->child->string)) { |
|
if (p->col) |
|
p->flags &= ~TERMP_NOSPACE; |
p->flags &= ~TERMP_NONOSPACE; |
p->flags &= ~TERMP_NONOSPACE; |
else |
} else |
p->flags |= TERMP_NONOSPACE; |
p->flags |= TERMP_NONOSPACE; |
|
|
return(0); |
return(0); |
|
|
termp____post(DECL_ARGS) |
termp____post(DECL_ARGS) |
{ |
{ |
|
|
|
/* |
|
* Handle lists of authors. In general, print each followed by |
|
* a comma. Don't print the comma if there are only two |
|
* authors. |
|
*/ |
|
if (MDOC__A == n->tok && n->next && MDOC__A == n->next->tok) |
|
if (NULL == n->next->next || MDOC__A != n->next->next->tok) |
|
if (NULL == n->prev || MDOC__A != n->prev->tok) |
|
return; |
|
|
/* TODO: %U. */ |
/* TODO: %U. */ |
|
|
p->flags |= TERMP_NOSPACE; |
p->flags |= TERMP_NOSPACE; |
term_word(p, n->next ? "," : "."); |
if (NULL == n->next) { |
|
term_word(p, "."); |
|
p->flags |= TERMP_SENTENCE; |
|
} else |
|
term_word(p, ","); |
} |
} |
|
|
|
|
Line 2140 termp_li_pre(DECL_ARGS) |
|
Line 2153 termp_li_pre(DECL_ARGS) |
|
static int |
static int |
termp_lk_pre(DECL_ARGS) |
termp_lk_pre(DECL_ARGS) |
{ |
{ |
const struct mdoc_node *nn; |
const struct mdoc_node *nn, *sv; |
|
|
term_fontpush(p, TERMFONT_UNDER); |
term_fontpush(p, TERMFONT_UNDER); |
nn = n->child; |
|
|
|
|
nn = sv = n->child; |
|
|
if (NULL == nn->next) |
if (NULL == nn->next) |
return(1); |
return(1); |
|
|
term_word(p, nn->string); |
for (nn = nn->next; nn; nn = nn->next) |
|
term_word(p, nn->string); |
|
|
term_fontpop(p); |
term_fontpop(p); |
|
|
p->flags |= TERMP_NOSPACE; |
p->flags |= TERMP_NOSPACE; |
term_word(p, ":"); |
term_word(p, ":"); |
|
|
term_fontpush(p, TERMFONT_BOLD); |
term_fontpush(p, TERMFONT_BOLD); |
for (nn = nn->next; nn; nn = nn->next) |
term_word(p, sv->string); |
term_word(p, nn->string); |
|
term_fontpop(p); |
term_fontpop(p); |
|
|
return(0); |
return(0); |