]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.73 2010/06/07 20:57:09 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
*, const void *);
77 static void print_man_nodelist(DECL_ARGS
);
78 static void print_man_node(DECL_ARGS
);
79 static void print_man_foot(struct termp
*, const void *);
80 static void print_bvspace(struct termp
*,
81 const struct man_node
*);
83 static int pre_B(DECL_ARGS
);
84 static int pre_BI(DECL_ARGS
);
85 static int pre_HP(DECL_ARGS
);
86 static int pre_I(DECL_ARGS
);
87 static int pre_IP(DECL_ARGS
);
88 static int pre_PP(DECL_ARGS
);
89 static int pre_RB(DECL_ARGS
);
90 static int pre_RI(DECL_ARGS
);
91 static int pre_RS(DECL_ARGS
);
92 static int pre_SH(DECL_ARGS
);
93 static int pre_SS(DECL_ARGS
);
94 static int pre_TP(DECL_ARGS
);
95 static int pre_br(DECL_ARGS
);
96 static int pre_fi(DECL_ARGS
);
97 static int pre_ign(DECL_ARGS
);
98 static int pre_nf(DECL_ARGS
);
99 static int pre_sp(DECL_ARGS
);
101 static void post_IP(DECL_ARGS
);
102 static void post_HP(DECL_ARGS
);
103 static void post_RS(DECL_ARGS
);
104 static void post_SH(DECL_ARGS
);
105 static void post_SS(DECL_ARGS
);
106 static void post_TP(DECL_ARGS
);
108 static const struct termact termacts
[MAN_MAX
] = {
109 { pre_br
, NULL
, MAN_NOTEXT
}, /* br */
110 { NULL
, NULL
, 0 }, /* TH */
111 { pre_SH
, post_SH
, 0 }, /* SH */
112 { pre_SS
, post_SS
, 0 }, /* SS */
113 { pre_TP
, post_TP
, 0 }, /* TP */
114 { pre_PP
, NULL
, 0 }, /* LP */
115 { pre_PP
, NULL
, 0 }, /* PP */
116 { pre_PP
, NULL
, 0 }, /* P */
117 { pre_IP
, post_IP
, 0 }, /* IP */
118 { pre_HP
, post_HP
, 0 }, /* HP */
119 { NULL
, NULL
, 0 }, /* SM */
120 { pre_B
, NULL
, 0 }, /* SB */
121 { pre_BI
, NULL
, 0 }, /* BI */
122 { pre_BI
, NULL
, 0 }, /* IB */
123 { pre_RB
, NULL
, 0 }, /* BR */
124 { pre_RB
, NULL
, 0 }, /* RB */
125 { NULL
, NULL
, 0 }, /* R */
126 { pre_B
, NULL
, 0 }, /* B */
127 { pre_I
, NULL
, 0 }, /* I */
128 { pre_RI
, NULL
, 0 }, /* IR */
129 { pre_RI
, NULL
, 0 }, /* RI */
130 { NULL
, NULL
, MAN_NOTEXT
}, /* na */
131 { pre_I
, NULL
, 0 }, /* i */
132 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
133 { pre_nf
, NULL
, 0 }, /* nf */
134 { pre_fi
, NULL
, 0 }, /* fi */
135 { NULL
, NULL
, 0 }, /* r */
136 { NULL
, NULL
, 0 }, /* RE */
137 { pre_RS
, post_RS
, 0 }, /* RS */
138 { pre_ign
, NULL
, 0 }, /* DT */
139 { pre_ign
, NULL
, 0 }, /* UC */
140 { pre_ign
, NULL
, 0 }, /* PD */
141 { pre_sp
, NULL
, MAN_NOTEXT
}, /* Sp */
142 { pre_nf
, NULL
, 0 }, /* Vb */
143 { pre_fi
, NULL
, 0 }, /* Ve */
144 { pre_ign
, NULL
, 0 }, /* AT */
150 terminal_man(void *arg
, const struct man
*man
)
153 const struct man_node
*n
;
154 const struct man_meta
*m
;
157 p
= (struct termp
*)arg
;
160 p
->maxrmargin
= p
->defrmargin
;
162 term_begin(p
, print_man_head
, print_man_foot
, man_meta(man
));
164 if (NULL
== p
->symtab
)
166 case (TERMENC_ASCII
):
167 p
->symtab
= chars_init(CHARS_ASCII
);
177 p
->flags
|= TERMP_NOSPACE
;
184 print_man_nodelist(p
, &mt
, n
->child
, 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 void *arg
)
864 const struct man_meta
*meta
;
866 meta
= (const struct man_meta
*)arg
;
868 term_fontrepl(p
, TERMFONT_NONE
);
871 strlcpy(buf
, meta
->rawdate
, DATESIZ
);
873 time2a(meta
->date
, buf
, DATESIZ
);
879 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
880 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
884 term_word(p
, meta
->source
);
889 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
890 p
->offset
= p
->rmargin
;
891 p
->rmargin
= p
->maxrmargin
;
892 p
->flags
&= ~TERMP_NOBREAK
;
900 print_man_head(struct termp
*p
, const void *arg
)
902 char buf
[BUFSIZ
], title
[BUFSIZ
];
903 size_t buflen
, titlen
;
904 const struct man_meta
*m
;
906 m
= (const struct man_meta
*)arg
;
909 * Note that old groff would spit out some spaces before the
910 * header. We discontinue this strange behaviour, but at one
911 * point we did so here.
914 p
->rmargin
= p
->maxrmargin
;
917 buf
[0] = title
[0] = '\0';
920 strlcpy(buf
, m
->vol
, BUFSIZ
);
921 buflen
= strlen(buf
);
923 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
924 titlen
= strlen(title
);
927 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
928 (p
->maxrmargin
- strlen(buf
) + 1) / 2 :
929 p
->maxrmargin
- buflen
;
930 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
935 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
936 p
->offset
= p
->rmargin
;
937 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
938 p
->maxrmargin
- titlen
: p
->maxrmargin
;
943 p
->flags
&= ~TERMP_NOBREAK
;
944 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
945 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
946 p
->offset
= p
->rmargin
;
947 p
->rmargin
= p
->maxrmargin
;
952 p
->rmargin
= p
->maxrmargin
;
954 p
->flags
&= ~TERMP_NOSPACE
;
957 * Groff likes to have some leading spaces before content. Well