]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
65a6e7b4db352618acf4675cf89cd4261425d1e0
1 /* $Id: man_term.c,v 1.78 2010/06/26 15:36:37 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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>
40 /* FIXME: have PD set the default vspace width. */
44 #define MANT_LITERAL (1 << 0)
46 * Default amount to indent the left margin after leading text
47 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
48 * indent). This needs to be saved because `HP' and so on, if
49 * not having a specified value, must default.
51 * Note that this is the indentation AFTER the left offset, so
52 * the total offset is usually offset + lmargin.
56 * The default offset, i.e., the amount between any text and the
62 #define DECL_ARGS struct termp *p, \
64 const struct man_node *n, \
65 const struct man_meta *m
68 int (*pre
)(DECL_ARGS
);
69 void (*post
)(DECL_ARGS
);
71 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
74 static int a2width(const struct termp
*, const char *);
75 static size_t a2height(const struct termp
*, const char *);
77 static void print_man_nodelist(DECL_ARGS
);
78 static void print_man_node(DECL_ARGS
);
79 static void print_man_head(struct termp
*, const void *);
80 static void print_man_foot(struct termp
*, const void *);
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
, 0 }, /* AT */
151 terminal_man(void *arg
, const struct man
*man
)
154 const struct man_node
*n
;
155 const struct man_meta
*m
;
158 p
= (struct termp
*)arg
;
161 p
->maxrmargin
= p
->defrmargin
;
162 p
->tabwidth
= term_len(p
, 5);
164 if (NULL
== p
->symtab
)
166 case (TERMENC_ASCII
):
167 p
->symtab
= chars_init(CHARS_ASCII
);
177 term_begin(p
, print_man_head
, print_man_foot
, m
);
178 p
->flags
|= TERMP_NOSPACE
;
181 mt
.lmargin
= term_len(p
, INDENT
);
182 mt
.offset
= term_len(p
, INDENT
);
185 print_man_nodelist(p
, &mt
, n
->child
, m
);
192 a2height(const struct termp
*p
, const char *cp
)
196 if ( ! a2roffsu(cp
, &su
, SCALE_VS
))
197 SCALE_VS_INIT(&su
, term_strlen(p
, cp
));
199 return(term_vspan(p
, &su
));
204 a2width(const struct termp
*p
, const char *cp
)
208 if ( ! a2roffsu(cp
, &su
, SCALE_BU
))
211 return((int)term_hspan(p
, &su
));
216 print_bvspace(struct termp
*p
, const struct man_node
*n
)
223 if (MAN_SS
== n
->prev
->tok
)
225 if (MAN_SH
== n
->prev
->tok
)
246 term_fontrepl(p
, TERMFONT_UNDER
);
256 mt
->fl
&= ~MANT_LITERAL
;
266 mt
->fl
|= MANT_LITERAL
;
267 return(MAN_Vb
!= n
->tok
);
275 const struct man_node
*nn
;
278 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
279 if (i
% 2 && MAN_RB
== n
->tok
)
280 term_fontrepl(p
, TERMFONT_BOLD
);
281 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
282 term_fontrepl(p
, TERMFONT_BOLD
);
284 term_fontrepl(p
, TERMFONT_NONE
);
287 p
->flags
|= TERMP_NOSPACE
;
289 print_man_node(p
, mt
, nn
, m
);
299 const struct man_node
*nn
;
302 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
303 if (i
% 2 && MAN_RI
== n
->tok
)
304 term_fontrepl(p
, TERMFONT_UNDER
);
305 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
306 term_fontrepl(p
, TERMFONT_UNDER
);
308 term_fontrepl(p
, TERMFONT_NONE
);
311 p
->flags
|= TERMP_NOSPACE
;
313 print_man_node(p
, mt
, nn
, m
);
323 const struct man_node
*nn
;
326 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
327 if (i
% 2 && MAN_BI
== n
->tok
)
328 term_fontrepl(p
, TERMFONT_UNDER
);
330 term_fontrepl(p
, TERMFONT_BOLD
);
331 else if (MAN_BI
== n
->tok
)
332 term_fontrepl(p
, TERMFONT_BOLD
);
334 term_fontrepl(p
, TERMFONT_UNDER
);
337 p
->flags
|= TERMP_NOSPACE
;
339 print_man_node(p
, mt
, nn
, m
);
350 term_fontrepl(p
, TERMFONT_BOLD
);
362 a2height(p
, n
->child
->string
) : term_len(p
, 1);
366 for (i
= 0; i
<= len
; i
++)
389 const struct man_node
*nn
;
396 p
->flags
|= TERMP_NOBREAK
;
397 p
->flags
|= TERMP_TWOSPACE
;
406 /* Calculate offset. */
408 if (NULL
!= (nn
= n
->parent
->head
->child
))
409 if ((ival
= a2width(p
, nn
->string
)) >= 0)
413 len
= term_len(p
, 1);
415 p
->offset
= mt
->offset
;
416 p
->rmargin
= mt
->offset
+ len
;
419 mt
->lmargin
= (size_t)ival
;
436 p
->flags
&= ~TERMP_NOBREAK
;
437 p
->flags
&= ~TERMP_TWOSPACE
;
438 p
->offset
= mt
->offset
;
439 p
->rmargin
= p
->maxrmargin
;
454 mt
->lmargin
= term_len(p
, INDENT
);
458 p
->offset
= mt
->offset
;
470 const struct man_node
*nn
;
476 p
->flags
|= TERMP_NOLPAD
;
477 p
->flags
|= TERMP_NOSPACE
;
480 p
->flags
|= TERMP_NOBREAK
;
492 /* Calculate offset. */
494 if (NULL
!= (nn
= n
->parent
->head
->child
))
495 if (NULL
!= (nn
= nn
->next
)) {
496 for ( ; nn
->next
; nn
= nn
->next
)
498 if ((ival
= a2width(p
, nn
->string
)) >= 0)
504 /* Handle zero-width lengths. */
506 len
= term_len(p
, 1);
508 p
->offset
= mt
->offset
;
509 p
->rmargin
= mt
->offset
+ len
;
513 /* Set the saved left-margin. */
514 mt
->lmargin
= (size_t)ival
;
516 /* Don't print the length value. */
517 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
518 print_man_node(p
, mt
, nn
, m
);
521 p
->offset
= mt
->offset
+ len
;
522 p
->rmargin
= p
->maxrmargin
;
540 p
->flags
&= ~TERMP_NOBREAK
;
541 p
->rmargin
= p
->maxrmargin
;
545 p
->flags
&= ~TERMP_NOLPAD
;
557 const struct man_node
*nn
;
563 p
->flags
|= TERMP_NOBREAK
;
564 p
->flags
|= TERMP_TWOSPACE
;
567 p
->flags
|= TERMP_NOLPAD
;
568 p
->flags
|= TERMP_NOSPACE
;
577 len
= (size_t)mt
->lmargin
;
580 /* Calculate offset. */
582 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
583 while (nn
&& MAN_TEXT
!= nn
->type
)
586 if ((ival
= a2width(p
, nn
->string
)) >= 0)
592 /* Handle zero-length properly. */
594 len
= term_len(p
, 1);
596 p
->offset
= mt
->offset
;
597 p
->rmargin
= mt
->offset
+ len
;
599 /* Don't print same-line elements. */
600 for (nn
= n
->child
; nn
; nn
= nn
->next
)
601 if (nn
->line
> n
->line
)
602 print_man_node(p
, mt
, nn
, m
);
605 mt
->lmargin
= (size_t)ival
;
609 p
->offset
= mt
->offset
+ len
;
610 p
->rmargin
= p
->maxrmargin
;
628 p
->flags
&= ~TERMP_NOBREAK
;
629 p
->flags
&= ~TERMP_TWOSPACE
;
630 p
->rmargin
= p
->maxrmargin
;
634 p
->flags
&= ~TERMP_NOLPAD
;
649 mt
->lmargin
= term_len(p
, INDENT
);
650 mt
->offset
= term_len(p
, INDENT
);
651 /* If following a prior empty `SS', no vspace. */
652 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
653 if (NULL
== n
->prev
->body
->child
)
660 term_fontrepl(p
, TERMFONT_BOLD
);
661 p
->offset
= term_len(p
, HALFINDENT
);
664 p
->offset
= mt
->offset
;
699 mt
->lmargin
= term_len(p
, INDENT
);
700 mt
->offset
= term_len(p
, INDENT
);
701 /* If following a prior empty `SH', no vspace. */
702 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
703 if (NULL
== n
->prev
->body
->child
)
705 /* If the first macro, no vspae. */
711 term_fontrepl(p
, TERMFONT_BOLD
);
715 p
->offset
= mt
->offset
;
747 const struct man_node
*nn
;
760 if (NULL
== (nn
= n
->parent
->head
->child
)) {
761 mt
->offset
= mt
->lmargin
+ term_len(p
, INDENT
);
762 p
->offset
= mt
->offset
;
766 if ((ival
= a2width(p
, nn
->string
)) < 0)
769 mt
->offset
= term_len(p
, INDENT
) + (size_t)ival
;
770 p
->offset
= mt
->offset
;
783 mt
->offset
= mt
->lmargin
= term_len(p
, INDENT
);
789 p
->offset
= term_len(p
, INDENT
);
796 print_man_node(DECL_ARGS
)
805 if (0 == *n
->string
) {
810 term_word(p
, n
->string
);
812 /* FIXME: this means that macro lines are munged! */
814 if (MANT_LITERAL
& mt
->fl
) {
816 rmax
= p
->maxrmargin
;
817 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
818 p
->flags
|= TERMP_NOSPACE
;
821 p
->maxrmargin
= rmax
;
825 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
826 term_fontrepl(p
, TERMFONT_NONE
);
827 if (termacts
[n
->tok
].pre
)
828 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
833 print_man_nodelist(p
, mt
, n
->child
, m
);
835 if (MAN_TEXT
!= n
->type
) {
836 if (termacts
[n
->tok
].post
)
837 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
838 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
839 term_fontrepl(p
, TERMFONT_NONE
);
842 if (MAN_EOS
& n
->flags
)
843 p
->flags
|= TERMP_SENTENCE
;
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 void *arg
)
862 const struct man_meta
*meta
;
864 meta
= (const struct man_meta
*)arg
;
866 term_fontrepl(p
, TERMFONT_NONE
);
869 strlcpy(buf
, meta
->rawdate
, DATESIZ
);
871 time2a(meta
->date
, buf
, DATESIZ
);
877 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
878 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, buf
);
882 term_word(p
, meta
->source
);
887 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
888 p
->offset
= p
->rmargin
;
889 p
->rmargin
= p
->maxrmargin
;
890 p
->flags
&= ~TERMP_NOBREAK
;
898 print_man_head(struct termp
*p
, const void *arg
)
900 char buf
[BUFSIZ
], title
[BUFSIZ
];
901 size_t buflen
, titlen
;
902 const struct man_meta
*m
;
904 m
= (const struct man_meta
*)arg
;
907 * Note that old groff would spit out some spaces before the
908 * header. We discontinue this strange behaviour, but at one
909 * point we did so here.
912 p
->rmargin
= p
->maxrmargin
;
915 buf
[0] = title
[0] = '\0';
918 strlcpy(buf
, m
->vol
, BUFSIZ
);
919 buflen
= term_strlen(p
, buf
);
921 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
922 titlen
= term_strlen(p
, title
);
925 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
927 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
928 p
->maxrmargin
- buflen
;
929 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
934 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
935 p
->offset
= p
->rmargin
;
936 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
937 p
->maxrmargin
- titlen
: p
->maxrmargin
;
942 p
->flags
&= ~TERMP_NOBREAK
;
943 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
944 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
945 p
->offset
= p
->rmargin
;
946 p
->rmargin
= p
->maxrmargin
;
951 p
->rmargin
= p
->maxrmargin
;
953 p
->flags
&= ~TERMP_NOSPACE
;
956 * Groff likes to have some leading spaces before content. Well