1 /* $Id: man_term.c,v 1.59 2010/03/24 20:10:53 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
34 /* FIXME: have PD set the default vspace width. */
38 #define MANT_LITERAL (1 << 0)
40 * Default amount to indent the left margin after leading text
41 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
42 * indent). This needs to be saved because `HP' and so on, if
43 * not having a specified value, must default.
45 * Note that this is the indentation AFTER the left offset, so
46 * the total offset is usually offset + lmargin.
50 * The default offset, i.e., the amount between any text and the
56 #define DECL_ARGS struct termp *p, \
58 const struct man_node *n, \
59 const struct man_meta *m
62 int (*pre
)(DECL_ARGS
);
63 void (*post
)(DECL_ARGS
);
65 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
68 static int a2width(const struct man_node
*);
69 static int a2height(const struct man_node
*);
71 static void print_man_head(struct termp
*,
72 const struct man_meta
*);
73 static void print_man_nodelist(DECL_ARGS
);
74 static void print_man_node(DECL_ARGS
);
75 static void print_man_foot(struct termp
*,
76 const struct man_meta
*);
77 static void print_bvspace(struct termp
*,
78 const struct man_node
*);
80 static int pre_B(DECL_ARGS
);
81 static int pre_BI(DECL_ARGS
);
82 static int pre_HP(DECL_ARGS
);
83 static int pre_I(DECL_ARGS
);
84 static int pre_IP(DECL_ARGS
);
85 static int pre_PP(DECL_ARGS
);
86 static int pre_RB(DECL_ARGS
);
87 static int pre_RI(DECL_ARGS
);
88 static int pre_RS(DECL_ARGS
);
89 static int pre_SH(DECL_ARGS
);
90 static int pre_SS(DECL_ARGS
);
91 static int pre_TP(DECL_ARGS
);
92 static int pre_br(DECL_ARGS
);
93 static int pre_fi(DECL_ARGS
);
94 static int pre_ign(DECL_ARGS
);
95 static int pre_nf(DECL_ARGS
);
96 static int pre_sp(DECL_ARGS
);
98 static void post_IP(DECL_ARGS
);
99 static void post_HP(DECL_ARGS
);
100 static void post_RS(DECL_ARGS
);
101 static void post_SH(DECL_ARGS
);
102 static void post_SS(DECL_ARGS
);
103 static void post_TP(DECL_ARGS
);
105 static const struct termact termacts
[MAN_MAX
] = {
106 { pre_br
, NULL
, MAN_NOTEXT
}, /* br */
107 { NULL
, NULL
, 0 }, /* TH */
108 { pre_SH
, post_SH
, 0 }, /* SH */
109 { pre_SS
, post_SS
, 0 }, /* SS */
110 { pre_TP
, post_TP
, 0 }, /* TP */
111 { pre_PP
, NULL
, 0 }, /* LP */
112 { pre_PP
, NULL
, 0 }, /* PP */
113 { pre_PP
, NULL
, 0 }, /* P */
114 { pre_IP
, post_IP
, 0 }, /* IP */
115 { pre_HP
, post_HP
, 0 }, /* HP */
116 { NULL
, NULL
, 0 }, /* SM */
117 { pre_B
, NULL
, 0 }, /* SB */
118 { pre_BI
, NULL
, 0 }, /* BI */
119 { pre_BI
, NULL
, 0 }, /* IB */
120 { pre_RB
, NULL
, 0 }, /* BR */
121 { pre_RB
, NULL
, 0 }, /* RB */
122 { NULL
, NULL
, 0 }, /* R */
123 { pre_B
, NULL
, 0 }, /* B */
124 { pre_I
, NULL
, 0 }, /* I */
125 { pre_RI
, NULL
, 0 }, /* IR */
126 { pre_RI
, NULL
, 0 }, /* RI */
127 { NULL
, NULL
, MAN_NOTEXT
}, /* na */
128 { pre_I
, NULL
, 0 }, /* i */
129 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
130 { pre_nf
, NULL
, 0 }, /* nf */
131 { pre_fi
, NULL
, 0 }, /* fi */
132 { NULL
, NULL
, 0 }, /* r */
133 { NULL
, NULL
, 0 }, /* RE */
134 { pre_RS
, post_RS
, 0 }, /* RS */
135 { pre_ign
, NULL
, 0 }, /* DT */
136 { pre_ign
, NULL
, 0 }, /* UC */
137 { pre_ign
, NULL
, 0 }, /* PD */
138 { pre_sp
, NULL
, MAN_NOTEXT
}, /* Sp */
139 { pre_nf
, NULL
, 0 }, /* Vb */
140 { pre_fi
, NULL
, 0 }, /* Ve */
141 { pre_ign
, NULL
, MAN_NOTEXT
}, /* de */
142 { pre_ign
, NULL
, MAN_NOTEXT
}, /* dei */
143 { pre_ign
, NULL
, MAN_NOTEXT
}, /* am */
144 { pre_ign
, NULL
, MAN_NOTEXT
}, /* ami */
145 { pre_ign
, NULL
, MAN_NOTEXT
}, /* ig */
146 { NULL
, NULL
, 0 }, /* . */
152 terminal_man(void *arg
, const struct man
*man
)
155 const struct man_node
*n
;
156 const struct man_meta
*m
;
159 p
= (struct termp
*)arg
;
164 if (NULL
== p
->symtab
)
166 case (TERMENC_ASCII
):
167 p
->symtab
= chars_init(CHARS_ASCII
);
177 print_man_head(p
, m
);
178 p
->flags
|= TERMP_NOSPACE
;
185 print_man_nodelist(p
, &mt
, n
->child
, m
);
186 print_man_foot(p
, m
);
191 a2height(const struct man_node
*n
)
195 assert(MAN_TEXT
== n
->type
);
197 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
198 SCALE_VS_INIT(&su
, strlen(n
->string
));
200 return((int)term_vspan(&su
));
205 a2width(const struct man_node
*n
)
209 assert(MAN_TEXT
== n
->type
);
211 if ( ! a2roffsu(n
->string
, &su
, SCALE_BU
))
214 return((int)term_hspan(&su
));
219 print_bvspace(struct termp
*p
, const struct man_node
*n
)
226 if (MAN_SS
== n
->prev
->tok
)
228 if (MAN_SH
== n
->prev
->tok
)
249 term_fontrepl(p
, TERMFONT_UNDER
);
259 p
->rmargin
= p
->maxrmargin
= 65;
260 mt
->fl
&= ~MANT_LITERAL
;
270 p
->rmargin
= p
->maxrmargin
= 78;
272 mt
->fl
|= MANT_LITERAL
;
274 return(MAN_Vb
!= n
->tok
);
282 const struct man_node
*nn
;
285 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
286 if (i
% 2 && MAN_RB
== n
->tok
)
287 term_fontrepl(p
, TERMFONT_BOLD
);
288 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
289 term_fontrepl(p
, TERMFONT_BOLD
);
291 term_fontrepl(p
, TERMFONT_NONE
);
294 p
->flags
|= TERMP_NOSPACE
;
296 print_man_node(p
, mt
, nn
, m
);
306 const struct man_node
*nn
;
309 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
310 if (i
% 2 && MAN_RI
== n
->tok
)
311 term_fontrepl(p
, TERMFONT_UNDER
);
312 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
313 term_fontrepl(p
, TERMFONT_UNDER
);
315 term_fontrepl(p
, TERMFONT_NONE
);
318 p
->flags
|= TERMP_NOSPACE
;
320 print_man_node(p
, mt
, nn
, m
);
330 const struct man_node
*nn
;
333 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
334 if (i
% 2 && MAN_BI
== n
->tok
)
335 term_fontrepl(p
, TERMFONT_UNDER
);
337 term_fontrepl(p
, TERMFONT_BOLD
);
338 else if (MAN_BI
== n
->tok
)
339 term_fontrepl(p
, TERMFONT_BOLD
);
341 term_fontrepl(p
, TERMFONT_UNDER
);
344 p
->flags
|= TERMP_NOSPACE
;
346 print_man_node(p
, mt
, nn
, m
);
357 term_fontrepl(p
, TERMFONT_BOLD
);
368 len
= n
->child
? a2height(n
->child
) : 1;
372 for (i
= 0; i
< len
; i
++)
395 const struct man_node
*nn
;
402 p
->flags
|= TERMP_NOBREAK
;
403 p
->flags
|= TERMP_TWOSPACE
;
412 /* Calculate offset. */
414 if (NULL
!= (nn
= n
->parent
->head
->child
))
415 if ((ival
= a2width(nn
)) >= 0)
421 p
->offset
= mt
->offset
;
422 p
->rmargin
= mt
->offset
+ len
;
425 mt
->lmargin
= (size_t)ival
;
442 p
->flags
&= ~TERMP_NOBREAK
;
443 p
->flags
&= ~TERMP_TWOSPACE
;
444 p
->offset
= mt
->offset
;
445 p
->rmargin
= p
->maxrmargin
;
460 mt
->lmargin
= INDENT
;
464 p
->offset
= mt
->offset
;
476 const struct man_node
*nn
;
482 p
->flags
|= TERMP_NOLPAD
;
483 p
->flags
|= TERMP_NOSPACE
;
486 p
->flags
|= TERMP_NOBREAK
;
487 p
->flags
|= TERMP_TWOSPACE
;
499 /* Calculate offset. */
501 if (NULL
!= (nn
= n
->parent
->head
->child
))
502 if (NULL
!= (nn
= nn
->next
)) {
503 for ( ; nn
->next
; nn
= nn
->next
)
505 if ((ival
= a2width(nn
)) >= 0)
511 /* Handle zero-width lengths. */
515 p
->offset
= mt
->offset
;
516 p
->rmargin
= mt
->offset
+ len
;
520 /* Set the saved left-margin. */
521 mt
->lmargin
= (size_t)ival
;
523 /* Don't print the length value. */
524 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
525 print_man_node(p
, mt
, nn
, m
);
528 p
->offset
= mt
->offset
+ len
;
529 p
->rmargin
= p
->maxrmargin
;
547 p
->flags
&= ~TERMP_NOBREAK
;
548 p
->flags
&= ~TERMP_TWOSPACE
;
549 p
->rmargin
= p
->maxrmargin
;
553 p
->flags
&= ~TERMP_NOLPAD
;
565 const struct man_node
*nn
;
571 p
->flags
|= TERMP_NOBREAK
;
572 p
->flags
|= TERMP_TWOSPACE
;
575 p
->flags
|= TERMP_NOLPAD
;
576 p
->flags
|= TERMP_NOSPACE
;
585 len
= (size_t)mt
->lmargin
;
588 /* Calculate offset. */
590 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
591 while (nn
&& MAN_TEXT
!= nn
->type
)
594 if ((ival
= a2width(nn
)) >= 0)
600 /* Handle zero-length properly. */
604 p
->offset
= mt
->offset
;
605 p
->rmargin
= mt
->offset
+ len
;
607 /* Don't print same-line elements. */
608 for (nn
= n
->child
; nn
; nn
= nn
->next
)
609 if (nn
->line
> n
->line
)
610 print_man_node(p
, mt
, nn
, m
);
613 mt
->lmargin
= (size_t)ival
;
617 p
->offset
= mt
->offset
+ len
;
618 p
->rmargin
= p
->maxrmargin
;
636 p
->flags
&= ~TERMP_NOBREAK
;
637 p
->flags
&= ~TERMP_TWOSPACE
;
638 p
->rmargin
= p
->maxrmargin
;
642 p
->flags
&= ~TERMP_NOLPAD
;
657 mt
->lmargin
= INDENT
;
659 /* If following a prior empty `SS', no vspace. */
660 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
661 if (NULL
== n
->prev
->body
->child
)
668 term_fontrepl(p
, TERMFONT_BOLD
);
669 p
->offset
= HALFINDENT
;
672 p
->offset
= mt
->offset
;
707 mt
->lmargin
= INDENT
;
709 /* If following a prior empty `SH', no vspace. */
710 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
711 if (NULL
== n
->prev
->body
->child
)
716 term_fontrepl(p
, TERMFONT_BOLD
);
720 p
->offset
= mt
->offset
;
752 const struct man_node
*nn
;
765 if (NULL
== (nn
= n
->parent
->head
->child
)) {
766 mt
->offset
= mt
->lmargin
+ INDENT
;
767 p
->offset
= mt
->offset
;
771 if ((ival
= a2width(nn
)) < 0)
774 mt
->offset
= INDENT
+ (size_t)ival
;
775 p
->offset
= mt
->offset
;
788 mt
->offset
= mt
->lmargin
= INDENT
;
801 print_man_node(DECL_ARGS
)
809 if (0 == *n
->string
) {
814 term_word(p
, n
->string
);
816 /* FIXME: this means that macro lines are munged! */
818 if (MANT_LITERAL
& mt
->fl
) {
819 p
->flags
|= TERMP_NOSPACE
;
824 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
825 term_fontrepl(p
, TERMFONT_NONE
);
826 if (termacts
[n
->tok
].pre
)
827 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
832 print_man_nodelist(p
, mt
, n
->child
, m
);
834 if (MAN_TEXT
!= n
->type
) {
835 if (termacts
[n
->tok
].post
)
836 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
837 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
838 term_fontrepl(p
, TERMFONT_NONE
);
844 print_man_nodelist(DECL_ARGS
)
847 print_man_node(p
, mt
, n
, m
);
850 print_man_nodelist(p
, mt
, n
->next
, m
);
855 print_man_foot(struct termp
*p
, const struct man_meta
*meta
)
859 term_fontrepl(p
, TERMFONT_NONE
);
861 time2a(meta
->date
, buf
, DATESIZ
);
865 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
866 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
870 term_word(p
, meta
->source
);
875 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
876 p
->offset
= p
->rmargin
;
877 p
->rmargin
= p
->maxrmargin
;
878 p
->flags
&= ~TERMP_NOBREAK
;
886 print_man_head(struct termp
*p
, const struct man_meta
*m
)
888 char buf
[BUFSIZ
], title
[BUFSIZ
];
889 size_t buflen
, titlen
;
891 p
->rmargin
= p
->maxrmargin
;
894 buf
[0] = title
[0] = '\0';
897 strlcpy(buf
, m
->vol
, BUFSIZ
);
898 buflen
= strlen(buf
);
900 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
901 titlen
= strlen(title
);
904 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
905 (p
->maxrmargin
- strlen(buf
) + 1) / 2 :
906 p
->maxrmargin
- buflen
;
907 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
912 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
913 p
->offset
= p
->rmargin
;
914 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
915 p
->maxrmargin
- titlen
: p
->maxrmargin
;
920 p
->flags
&= ~TERMP_NOBREAK
;
921 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
922 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
923 p
->offset
= p
->rmargin
;
924 p
->rmargin
= p
->maxrmargin
;
929 p
->rmargin
= p
->maxrmargin
;
931 p
->flags
&= ~TERMP_NOSPACE
;