]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
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.
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 p
->rmargin
= p
->maxrmargin
= 65;
264 mt
->fl
&= ~MANT_LITERAL
;
274 p
->rmargin
= p
->maxrmargin
= 78;
276 mt
->fl
|= MANT_LITERAL
;
278 return(MAN_Vb
!= n
->tok
);
286 const struct man_node
*nn
;
289 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
290 if (i
% 2 && MAN_RB
== n
->tok
)
291 term_fontrepl(p
, TERMFONT_BOLD
);
292 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
293 term_fontrepl(p
, TERMFONT_BOLD
);
295 term_fontrepl(p
, TERMFONT_NONE
);
298 p
->flags
|= TERMP_NOSPACE
;
300 print_man_node(p
, mt
, nn
, m
);
310 const struct man_node
*nn
;
313 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
314 if (i
% 2 && MAN_RI
== n
->tok
)
315 term_fontrepl(p
, TERMFONT_UNDER
);
316 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
317 term_fontrepl(p
, TERMFONT_UNDER
);
319 term_fontrepl(p
, TERMFONT_NONE
);
322 p
->flags
|= TERMP_NOSPACE
;
324 print_man_node(p
, mt
, nn
, m
);
334 const struct man_node
*nn
;
337 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
338 if (i
% 2 && MAN_BI
== n
->tok
)
339 term_fontrepl(p
, TERMFONT_UNDER
);
341 term_fontrepl(p
, TERMFONT_BOLD
);
342 else if (MAN_BI
== n
->tok
)
343 term_fontrepl(p
, TERMFONT_BOLD
);
345 term_fontrepl(p
, TERMFONT_UNDER
);
348 p
->flags
|= TERMP_NOSPACE
;
350 print_man_node(p
, mt
, nn
, m
);
361 term_fontrepl(p
, TERMFONT_BOLD
);
372 len
= n
->child
? a2height(n
->child
) : 1;
376 for (i
= 0; i
< len
; i
++)
399 const struct man_node
*nn
;
406 p
->flags
|= TERMP_NOBREAK
;
407 p
->flags
|= TERMP_TWOSPACE
;
416 /* Calculate offset. */
418 if (NULL
!= (nn
= n
->parent
->head
->child
))
419 if ((ival
= a2width(nn
)) >= 0)
425 p
->offset
= mt
->offset
;
426 p
->rmargin
= mt
->offset
+ len
;
429 mt
->lmargin
= (size_t)ival
;
446 p
->flags
&= ~TERMP_NOBREAK
;
447 p
->flags
&= ~TERMP_TWOSPACE
;
448 p
->offset
= mt
->offset
;
449 p
->rmargin
= p
->maxrmargin
;
464 mt
->lmargin
= INDENT
;
468 p
->offset
= mt
->offset
;
480 const struct man_node
*nn
;
486 p
->flags
|= TERMP_NOLPAD
;
487 p
->flags
|= TERMP_NOSPACE
;
490 p
->flags
|= TERMP_NOBREAK
;
491 p
->flags
|= TERMP_TWOSPACE
;
503 /* Calculate offset. */
505 if (NULL
!= (nn
= n
->parent
->head
->child
))
506 if (NULL
!= (nn
= nn
->next
)) {
507 for ( ; nn
->next
; nn
= nn
->next
)
509 if ((ival
= a2width(nn
)) >= 0)
515 /* Handle zero-width lengths. */
519 p
->offset
= mt
->offset
;
520 p
->rmargin
= mt
->offset
+ len
;
524 /* Set the saved left-margin. */
525 mt
->lmargin
= (size_t)ival
;
527 /* Don't print the length value. */
528 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
529 print_man_node(p
, mt
, nn
, m
);
532 p
->offset
= mt
->offset
+ len
;
533 p
->rmargin
= p
->maxrmargin
;
551 p
->flags
&= ~TERMP_NOBREAK
;
552 p
->flags
&= ~TERMP_TWOSPACE
;
553 p
->rmargin
= p
->maxrmargin
;
557 p
->flags
&= ~TERMP_NOLPAD
;
569 const struct man_node
*nn
;
575 p
->flags
|= TERMP_NOBREAK
;
576 p
->flags
|= TERMP_TWOSPACE
;
579 p
->flags
|= TERMP_NOLPAD
;
580 p
->flags
|= TERMP_NOSPACE
;
589 len
= (size_t)mt
->lmargin
;
592 /* Calculate offset. */
594 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
595 while (nn
&& MAN_TEXT
!= nn
->type
)
598 if ((ival
= a2width(nn
)) >= 0)
604 /* Handle zero-length properly. */
608 p
->offset
= mt
->offset
;
609 p
->rmargin
= mt
->offset
+ len
;
611 /* Don't print same-line elements. */
612 for (nn
= n
->child
; nn
; nn
= nn
->next
)
613 if (nn
->line
> n
->line
)
614 print_man_node(p
, mt
, nn
, m
);
617 mt
->lmargin
= (size_t)ival
;
621 p
->offset
= mt
->offset
+ len
;
622 p
->rmargin
= p
->maxrmargin
;
640 p
->flags
&= ~TERMP_NOBREAK
;
641 p
->flags
&= ~TERMP_TWOSPACE
;
642 p
->rmargin
= p
->maxrmargin
;
646 p
->flags
&= ~TERMP_NOLPAD
;
661 mt
->lmargin
= INDENT
;
663 /* If following a prior empty `SS', no vspace. */
664 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
665 if (NULL
== n
->prev
->body
->child
)
672 term_fontrepl(p
, TERMFONT_BOLD
);
673 p
->offset
= HALFINDENT
;
676 p
->offset
= mt
->offset
;
711 mt
->lmargin
= INDENT
;
713 /* If following a prior empty `SH', no vspace. */
714 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
715 if (NULL
== n
->prev
->body
->child
)
720 term_fontrepl(p
, TERMFONT_BOLD
);
724 p
->offset
= mt
->offset
;
756 const struct man_node
*nn
;
769 if (NULL
== (nn
= n
->parent
->head
->child
)) {
770 mt
->offset
= mt
->lmargin
+ INDENT
;
771 p
->offset
= mt
->offset
;
775 if ((ival
= a2width(nn
)) < 0)
778 mt
->offset
= INDENT
+ (size_t)ival
;
779 p
->offset
= mt
->offset
;
792 mt
->offset
= mt
->lmargin
= INDENT
;
805 print_man_node(DECL_ARGS
)
813 if (0 == *n
->string
) {
818 term_word(p
, n
->string
);
820 /* FIXME: this means that macro lines are munged! */
822 if (MANT_LITERAL
& mt
->fl
) {
823 p
->flags
|= TERMP_NOSPACE
;
828 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
829 term_fontrepl(p
, TERMFONT_NONE
);
830 if (termacts
[n
->tok
].pre
)
831 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
836 print_man_nodelist(p
, mt
, n
->child
, m
);
838 if (MAN_TEXT
!= n
->type
) {
839 if (termacts
[n
->tok
].post
)
840 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
841 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
842 term_fontrepl(p
, TERMFONT_NONE
);
848 print_man_nodelist(DECL_ARGS
)
851 print_man_node(p
, mt
, n
, m
);
854 print_man_nodelist(p
, mt
, n
->next
, m
);
859 print_man_foot(struct termp
*p
, const struct man_meta
*meta
)
863 term_fontrepl(p
, TERMFONT_NONE
);
865 time2a(meta
->date
, buf
, DATESIZ
);
869 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
870 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
874 term_word(p
, meta
->source
);
879 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
880 p
->offset
= p
->rmargin
;
881 p
->rmargin
= p
->maxrmargin
;
882 p
->flags
&= ~TERMP_NOBREAK
;
890 print_man_head(struct termp
*p
, const struct man_meta
*m
)
892 char buf
[BUFSIZ
], title
[BUFSIZ
];
893 size_t buflen
, titlen
;
895 p
->rmargin
= p
->maxrmargin
;
898 buf
[0] = title
[0] = '\0';
901 strlcpy(buf
, m
->vol
, BUFSIZ
);
902 buflen
= strlen(buf
);
904 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
905 titlen
= strlen(title
);
908 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
909 (p
->maxrmargin
- strlen(buf
) + 1) / 2 :
910 p
->maxrmargin
- buflen
;
911 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
916 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
917 p
->offset
= p
->rmargin
;
918 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
919 p
->maxrmargin
- titlen
: p
->maxrmargin
;
924 p
->flags
&= ~TERMP_NOBREAK
;
925 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
926 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
927 p
->offset
= p
->rmargin
;
928 p
->rmargin
= p
->maxrmargin
;
933 p
->rmargin
= p
->maxrmargin
;
935 p
->flags
&= ~TERMP_NOSPACE
;