]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.63 2010/05/12 16:46:28 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>
38 /* FIXME: have PD set the default vspace width. */
42 #define MANT_LITERAL (1 << 0)
44 * Default amount to indent the left margin after leading text
45 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
46 * indent). This needs to be saved because `HP' and so on, if
47 * not having a specified value, must default.
49 * Note that this is the indentation AFTER the left offset, so
50 * the total offset is usually offset + lmargin.
54 * The default offset, i.e., the amount between any text and the
60 #define DECL_ARGS struct termp *p, \
62 const struct man_node *n, \
63 const struct man_meta *m
66 int (*pre
)(DECL_ARGS
);
67 void (*post
)(DECL_ARGS
);
69 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
72 static int a2width(const struct man_node
*);
73 static int a2height(const struct man_node
*);
75 static void print_man_head(struct termp
*,
76 const struct man_meta
*);
77 static void print_man_nodelist(DECL_ARGS
);
78 static void print_man_node(DECL_ARGS
);
79 static void print_man_foot(struct termp
*,
80 const struct man_meta
*);
81 static void print_bvspace(struct termp
*,
82 const struct man_node
*);
84 static int pre_B(DECL_ARGS
);
85 static int pre_BI(DECL_ARGS
);
86 static int pre_HP(DECL_ARGS
);
87 static int pre_I(DECL_ARGS
);
88 static int pre_IP(DECL_ARGS
);
89 static int pre_PP(DECL_ARGS
);
90 static int pre_RB(DECL_ARGS
);
91 static int pre_RI(DECL_ARGS
);
92 static int pre_RS(DECL_ARGS
);
93 static int pre_SH(DECL_ARGS
);
94 static int pre_SS(DECL_ARGS
);
95 static int pre_TP(DECL_ARGS
);
96 static int pre_br(DECL_ARGS
);
97 static int pre_fi(DECL_ARGS
);
98 static int pre_ign(DECL_ARGS
);
99 static int pre_nf(DECL_ARGS
);
100 static int pre_sp(DECL_ARGS
);
102 static void post_IP(DECL_ARGS
);
103 static void post_HP(DECL_ARGS
);
104 static void post_RS(DECL_ARGS
);
105 static void post_SH(DECL_ARGS
);
106 static void post_SS(DECL_ARGS
);
107 static void post_TP(DECL_ARGS
);
109 static const struct termact termacts
[MAN_MAX
] = {
110 { pre_br
, NULL
, MAN_NOTEXT
}, /* br */
111 { NULL
, NULL
, 0 }, /* TH */
112 { pre_SH
, post_SH
, 0 }, /* SH */
113 { pre_SS
, post_SS
, 0 }, /* SS */
114 { pre_TP
, post_TP
, 0 }, /* TP */
115 { pre_PP
, NULL
, 0 }, /* LP */
116 { pre_PP
, NULL
, 0 }, /* PP */
117 { pre_PP
, NULL
, 0 }, /* P */
118 { pre_IP
, post_IP
, 0 }, /* IP */
119 { pre_HP
, post_HP
, 0 }, /* HP */
120 { NULL
, NULL
, 0 }, /* SM */
121 { pre_B
, NULL
, 0 }, /* SB */
122 { pre_BI
, NULL
, 0 }, /* BI */
123 { pre_BI
, NULL
, 0 }, /* IB */
124 { pre_RB
, NULL
, 0 }, /* BR */
125 { pre_RB
, NULL
, 0 }, /* RB */
126 { NULL
, NULL
, 0 }, /* R */
127 { pre_B
, NULL
, 0 }, /* B */
128 { pre_I
, NULL
, 0 }, /* I */
129 { pre_RI
, NULL
, 0 }, /* IR */
130 { pre_RI
, NULL
, 0 }, /* RI */
131 { NULL
, NULL
, MAN_NOTEXT
}, /* na */
132 { pre_I
, NULL
, 0 }, /* i */
133 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
134 { pre_nf
, NULL
, 0 }, /* nf */
135 { pre_fi
, NULL
, 0 }, /* fi */
136 { NULL
, NULL
, 0 }, /* r */
137 { NULL
, NULL
, 0 }, /* RE */
138 { pre_RS
, post_RS
, 0 }, /* RS */
139 { pre_ign
, NULL
, 0 }, /* DT */
140 { pre_ign
, NULL
, 0 }, /* UC */
141 { pre_ign
, NULL
, 0 }, /* PD */
142 { pre_sp
, NULL
, MAN_NOTEXT
}, /* Sp */
143 { pre_nf
, NULL
, 0 }, /* Vb */
144 { pre_fi
, NULL
, 0 }, /* Ve */
145 { pre_ign
, NULL
, MAN_NOTEXT
}, /* de */
146 { pre_ign
, NULL
, MAN_NOTEXT
}, /* dei */
147 { pre_ign
, NULL
, MAN_NOTEXT
}, /* am */
148 { pre_ign
, NULL
, MAN_NOTEXT
}, /* ami */
149 { pre_ign
, NULL
, MAN_NOTEXT
}, /* ig */
150 { NULL
, NULL
, 0 }, /* . */
156 terminal_man(void *arg
, const struct man
*man
)
159 const struct man_node
*n
;
160 const struct man_meta
*m
;
163 p
= (struct termp
*)arg
;
168 if (NULL
== p
->symtab
)
170 case (TERMENC_ASCII
):
171 p
->symtab
= chars_init(CHARS_ASCII
);
181 print_man_head(p
, m
);
182 p
->flags
|= TERMP_NOSPACE
;
189 print_man_nodelist(p
, &mt
, n
->child
, m
);
190 print_man_foot(p
, m
);
195 a2height(const struct man_node
*n
)
199 assert(MAN_TEXT
== n
->type
);
201 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
202 SCALE_VS_INIT(&su
, strlen(n
->string
));
204 return((int)term_vspan(&su
));
209 a2width(const struct man_node
*n
)
213 assert(MAN_TEXT
== n
->type
);
215 if ( ! a2roffsu(n
->string
, &su
, SCALE_BU
))
218 return((int)term_hspan(&su
));
223 print_bvspace(struct termp
*p
, const struct man_node
*n
)
230 if (MAN_SS
== n
->prev
->tok
)
232 if (MAN_SH
== n
->prev
->tok
)
253 term_fontrepl(p
, TERMFONT_UNDER
);
263 mt
->fl
&= ~MANT_LITERAL
;
273 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
)
713 /* If the first macro, no vspae. */
719 term_fontrepl(p
, TERMFONT_BOLD
);
723 p
->offset
= mt
->offset
;
755 const struct man_node
*nn
;
768 if (NULL
== (nn
= n
->parent
->head
->child
)) {
769 mt
->offset
= mt
->lmargin
+ INDENT
;
770 p
->offset
= mt
->offset
;
774 if ((ival
= a2width(nn
)) < 0)
777 mt
->offset
= INDENT
+ (size_t)ival
;
778 p
->offset
= mt
->offset
;
791 mt
->offset
= mt
->lmargin
= INDENT
;
804 print_man_node(DECL_ARGS
)
812 if (0 == *n
->string
) {
817 term_word(p
, n
->string
);
819 /* FIXME: this means that macro lines are munged! */
821 if (MANT_LITERAL
& mt
->fl
) {
822 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
823 p
->flags
|= TERMP_NOSPACE
;
825 p
->rmargin
= p
->maxrmargin
= 65;
829 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
830 term_fontrepl(p
, TERMFONT_NONE
);
831 if (termacts
[n
->tok
].pre
)
832 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
837 print_man_nodelist(p
, mt
, n
->child
, m
);
839 if (MAN_TEXT
!= n
->type
) {
840 if (termacts
[n
->tok
].post
)
841 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
842 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
843 term_fontrepl(p
, TERMFONT_NONE
);
846 if (MAN_EOS
& n
->flags
)
847 p
->flags
|= TERMP_SENTENCE
;
852 print_man_nodelist(DECL_ARGS
)
855 print_man_node(p
, mt
, n
, m
);
858 print_man_nodelist(p
, mt
, n
->next
, m
);
863 print_man_foot(struct termp
*p
, const struct man_meta
*meta
)
867 term_fontrepl(p
, TERMFONT_NONE
);
869 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(%d)", 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