]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.71 2010/05/17 22:11:42 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.
21 #include <sys/types.h>
39 /* FIXME: have PD set the default vspace width. */
43 #define MANT_LITERAL (1 << 0)
45 * Default amount to indent the left margin after leading text
46 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
47 * indent). This needs to be saved because `HP' and so on, if
48 * not having a specified value, must default.
50 * Note that this is the indentation AFTER the left offset, so
51 * the total offset is usually offset + lmargin.
55 * The default offset, i.e., the amount between any text and the
61 #define DECL_ARGS struct termp *p, \
63 const struct man_node *n, \
64 const struct man_meta *m
67 int (*pre
)(DECL_ARGS
);
68 void (*post
)(DECL_ARGS
);
70 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
73 static int a2width(const struct man_node
*);
74 static int a2height(const struct man_node
*);
76 static void print_man_head(struct termp
*,
77 const struct man_meta
*);
78 static void print_man_nodelist(DECL_ARGS
);
79 static void print_man_node(DECL_ARGS
);
80 static void print_man_foot(struct termp
*,
81 const struct man_meta
*);
82 static void print_bvspace(struct termp
*,
83 const struct man_node
*);
85 static int pre_B(DECL_ARGS
);
86 static int pre_BI(DECL_ARGS
);
87 static int pre_HP(DECL_ARGS
);
88 static int pre_I(DECL_ARGS
);
89 static int pre_IP(DECL_ARGS
);
90 static int pre_PP(DECL_ARGS
);
91 static int pre_RB(DECL_ARGS
);
92 static int pre_RI(DECL_ARGS
);
93 static int pre_RS(DECL_ARGS
);
94 static int pre_SH(DECL_ARGS
);
95 static int pre_SS(DECL_ARGS
);
96 static int pre_TP(DECL_ARGS
);
97 static int pre_br(DECL_ARGS
);
98 static int pre_fi(DECL_ARGS
);
99 static int pre_ign(DECL_ARGS
);
100 static int pre_nf(DECL_ARGS
);
101 static int pre_sp(DECL_ARGS
);
103 static void post_IP(DECL_ARGS
);
104 static void post_HP(DECL_ARGS
);
105 static void post_RS(DECL_ARGS
);
106 static void post_SH(DECL_ARGS
);
107 static void post_SS(DECL_ARGS
);
108 static void post_TP(DECL_ARGS
);
110 static const struct termact termacts
[MAN_MAX
] = {
111 { pre_br
, NULL
, MAN_NOTEXT
}, /* br */
112 { NULL
, NULL
, 0 }, /* TH */
113 { pre_SH
, post_SH
, 0 }, /* SH */
114 { pre_SS
, post_SS
, 0 }, /* SS */
115 { pre_TP
, post_TP
, 0 }, /* TP */
116 { pre_PP
, NULL
, 0 }, /* LP */
117 { pre_PP
, NULL
, 0 }, /* PP */
118 { pre_PP
, NULL
, 0 }, /* P */
119 { pre_IP
, post_IP
, 0 }, /* IP */
120 { pre_HP
, post_HP
, 0 }, /* HP */
121 { NULL
, NULL
, 0 }, /* SM */
122 { pre_B
, NULL
, 0 }, /* SB */
123 { pre_BI
, NULL
, 0 }, /* BI */
124 { pre_BI
, NULL
, 0 }, /* IB */
125 { pre_RB
, NULL
, 0 }, /* BR */
126 { pre_RB
, NULL
, 0 }, /* RB */
127 { NULL
, NULL
, 0 }, /* R */
128 { pre_B
, NULL
, 0 }, /* B */
129 { pre_I
, NULL
, 0 }, /* I */
130 { pre_RI
, NULL
, 0 }, /* IR */
131 { pre_RI
, NULL
, 0 }, /* RI */
132 { NULL
, NULL
, MAN_NOTEXT
}, /* na */
133 { pre_I
, NULL
, 0 }, /* i */
134 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
135 { pre_nf
, NULL
, 0 }, /* nf */
136 { pre_fi
, NULL
, 0 }, /* fi */
137 { NULL
, NULL
, 0 }, /* r */
138 { NULL
, NULL
, 0 }, /* RE */
139 { pre_RS
, post_RS
, 0 }, /* RS */
140 { pre_ign
, NULL
, 0 }, /* DT */
141 { pre_ign
, NULL
, 0 }, /* UC */
142 { pre_ign
, NULL
, 0 }, /* PD */
143 { pre_sp
, NULL
, MAN_NOTEXT
}, /* Sp */
144 { pre_nf
, NULL
, 0 }, /* Vb */
145 { pre_fi
, NULL
, 0 }, /* Ve */
146 { pre_ign
, NULL
, 0 }, /* AT */
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
;
162 p
->maxrmargin
= p
->defrmargin
;
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 mt
->fl
&= ~MANT_LITERAL
;
269 mt
->fl
|= MANT_LITERAL
;
270 return(MAN_Vb
!= n
->tok
);
278 const struct man_node
*nn
;
281 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
282 if (i
% 2 && MAN_RB
== n
->tok
)
283 term_fontrepl(p
, TERMFONT_BOLD
);
284 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
285 term_fontrepl(p
, TERMFONT_BOLD
);
287 term_fontrepl(p
, TERMFONT_NONE
);
290 p
->flags
|= TERMP_NOSPACE
;
292 print_man_node(p
, mt
, nn
, m
);
302 const struct man_node
*nn
;
305 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
306 if (i
% 2 && MAN_RI
== n
->tok
)
307 term_fontrepl(p
, TERMFONT_UNDER
);
308 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
309 term_fontrepl(p
, TERMFONT_UNDER
);
311 term_fontrepl(p
, TERMFONT_NONE
);
314 p
->flags
|= TERMP_NOSPACE
;
316 print_man_node(p
, mt
, nn
, m
);
326 const struct man_node
*nn
;
329 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
330 if (i
% 2 && MAN_BI
== n
->tok
)
331 term_fontrepl(p
, TERMFONT_UNDER
);
333 term_fontrepl(p
, TERMFONT_BOLD
);
334 else if (MAN_BI
== n
->tok
)
335 term_fontrepl(p
, TERMFONT_BOLD
);
337 term_fontrepl(p
, TERMFONT_UNDER
);
340 p
->flags
|= TERMP_NOSPACE
;
342 print_man_node(p
, mt
, nn
, m
);
353 term_fontrepl(p
, TERMFONT_BOLD
);
364 len
= n
->child
? a2height(n
->child
) : 1;
368 for (i
= 0; i
<= len
; i
++)
391 const struct man_node
*nn
;
398 p
->flags
|= TERMP_NOBREAK
;
399 p
->flags
|= TERMP_TWOSPACE
;
408 /* Calculate offset. */
410 if (NULL
!= (nn
= n
->parent
->head
->child
))
411 if ((ival
= a2width(nn
)) >= 0)
417 p
->offset
= mt
->offset
;
418 p
->rmargin
= mt
->offset
+ len
;
421 mt
->lmargin
= (size_t)ival
;
438 p
->flags
&= ~TERMP_NOBREAK
;
439 p
->flags
&= ~TERMP_TWOSPACE
;
440 p
->offset
= mt
->offset
;
441 p
->rmargin
= p
->maxrmargin
;
456 mt
->lmargin
= INDENT
;
460 p
->offset
= mt
->offset
;
472 const struct man_node
*nn
;
478 p
->flags
|= TERMP_NOLPAD
;
479 p
->flags
|= TERMP_NOSPACE
;
482 p
->flags
|= TERMP_NOBREAK
;
494 /* Calculate offset. */
496 if (NULL
!= (nn
= n
->parent
->head
->child
))
497 if (NULL
!= (nn
= nn
->next
)) {
498 for ( ; nn
->next
; nn
= nn
->next
)
500 if ((ival
= a2width(nn
)) >= 0)
506 /* Handle zero-width lengths. */
510 p
->offset
= mt
->offset
;
511 p
->rmargin
= mt
->offset
+ len
;
515 /* Set the saved left-margin. */
516 mt
->lmargin
= (size_t)ival
;
518 /* Don't print the length value. */
519 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
520 print_man_node(p
, mt
, nn
, m
);
523 p
->offset
= mt
->offset
+ len
;
524 p
->rmargin
= p
->maxrmargin
;
542 p
->flags
&= ~TERMP_NOBREAK
;
543 p
->rmargin
= p
->maxrmargin
;
547 p
->flags
&= ~TERMP_NOLPAD
;
559 const struct man_node
*nn
;
565 p
->flags
|= TERMP_NOBREAK
;
566 p
->flags
|= TERMP_TWOSPACE
;
569 p
->flags
|= TERMP_NOLPAD
;
570 p
->flags
|= TERMP_NOSPACE
;
579 len
= (size_t)mt
->lmargin
;
582 /* Calculate offset. */
584 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
585 while (nn
&& MAN_TEXT
!= nn
->type
)
588 if ((ival
= a2width(nn
)) >= 0)
594 /* Handle zero-length properly. */
598 p
->offset
= mt
->offset
;
599 p
->rmargin
= mt
->offset
+ len
;
601 /* Don't print same-line elements. */
602 for (nn
= n
->child
; nn
; nn
= nn
->next
)
603 if (nn
->line
> n
->line
)
604 print_man_node(p
, mt
, nn
, m
);
607 mt
->lmargin
= (size_t)ival
;
611 p
->offset
= mt
->offset
+ len
;
612 p
->rmargin
= p
->maxrmargin
;
630 p
->flags
&= ~TERMP_NOBREAK
;
631 p
->flags
&= ~TERMP_TWOSPACE
;
632 p
->rmargin
= p
->maxrmargin
;
636 p
->flags
&= ~TERMP_NOLPAD
;
651 mt
->lmargin
= INDENT
;
653 /* If following a prior empty `SS', no vspace. */
654 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
655 if (NULL
== n
->prev
->body
->child
)
662 term_fontrepl(p
, TERMFONT_BOLD
);
663 p
->offset
= HALFINDENT
;
666 p
->offset
= mt
->offset
;
701 mt
->lmargin
= INDENT
;
703 /* If following a prior empty `SH', no vspace. */
704 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
705 if (NULL
== n
->prev
->body
->child
)
707 /* If the first macro, no vspae. */
713 term_fontrepl(p
, TERMFONT_BOLD
);
717 p
->offset
= mt
->offset
;
749 const struct man_node
*nn
;
762 if (NULL
== (nn
= n
->parent
->head
->child
)) {
763 mt
->offset
= mt
->lmargin
+ INDENT
;
764 p
->offset
= mt
->offset
;
768 if ((ival
= a2width(nn
)) < 0)
771 mt
->offset
= INDENT
+ (size_t)ival
;
772 p
->offset
= mt
->offset
;
785 mt
->offset
= mt
->lmargin
= INDENT
;
798 print_man_node(DECL_ARGS
)
807 if (0 == *n
->string
) {
812 term_word(p
, n
->string
);
814 /* FIXME: this means that macro lines are munged! */
816 if (MANT_LITERAL
& mt
->fl
) {
818 rmax
= p
->maxrmargin
;
819 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
820 p
->flags
|= TERMP_NOSPACE
;
823 p
->maxrmargin
= rmax
;
827 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
828 term_fontrepl(p
, TERMFONT_NONE
);
829 if (termacts
[n
->tok
].pre
)
830 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
835 print_man_nodelist(p
, mt
, n
->child
, m
);
837 if (MAN_TEXT
!= n
->type
) {
838 if (termacts
[n
->tok
].post
)
839 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
840 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
841 term_fontrepl(p
, TERMFONT_NONE
);
844 if (MAN_EOS
& n
->flags
)
845 p
->flags
|= TERMP_SENTENCE
;
850 print_man_nodelist(DECL_ARGS
)
853 print_man_node(p
, mt
, n
, m
);
856 print_man_nodelist(p
, mt
, n
->next
, m
);
861 print_man_foot(struct termp
*p
, const struct man_meta
*meta
)
865 term_fontrepl(p
, TERMFONT_NONE
);
867 time2a(meta
->date
, buf
, DATESIZ
);
873 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
874 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
878 term_word(p
, meta
->source
);
883 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
884 p
->offset
= p
->rmargin
;
885 p
->rmargin
= p
->maxrmargin
;
886 p
->flags
&= ~TERMP_NOBREAK
;
894 print_man_head(struct termp
*p
, const struct man_meta
*m
)
896 char buf
[BUFSIZ
], title
[BUFSIZ
];
897 size_t buflen
, titlen
;
900 * Note that old groff would spit out some spaces before the
901 * header. We discontinue this strange behaviour, but at one
902 * point we did so here.
905 p
->rmargin
= p
->maxrmargin
;
908 buf
[0] = title
[0] = '\0';
911 strlcpy(buf
, m
->vol
, BUFSIZ
);
912 buflen
= strlen(buf
);
914 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
915 titlen
= strlen(title
);
918 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
919 (p
->maxrmargin
- strlen(buf
) + 1) / 2 :
920 p
->maxrmargin
- buflen
;
921 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
926 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
927 p
->offset
= p
->rmargin
;
928 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
929 p
->maxrmargin
- titlen
: p
->maxrmargin
;
934 p
->flags
&= ~TERMP_NOBREAK
;
935 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
936 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
937 p
->offset
= p
->rmargin
;
938 p
->rmargin
= p
->maxrmargin
;
943 p
->rmargin
= p
->maxrmargin
;
945 p
->flags
&= ~TERMP_NOSPACE
;
948 * Groff likes to have some leading spaces before content. Well