]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.86 2010/12/05 16:14:16 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 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>
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 termp
*, const char *);
74 static size_t a2height(const struct termp
*, const char *);
76 static void print_man_nodelist(DECL_ARGS
);
77 static void print_man_node(DECL_ARGS
);
78 static void print_man_head(struct termp
*, const void *);
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_ign(DECL_ARGS
);
96 static int pre_in(DECL_ARGS
);
97 static int pre_literal(DECL_ARGS
);
98 static int pre_sp(DECL_ARGS
);
100 static void post_IP(DECL_ARGS
);
101 static void post_HP(DECL_ARGS
);
102 static void post_RS(DECL_ARGS
);
103 static void post_SH(DECL_ARGS
);
104 static void post_SS(DECL_ARGS
);
105 static void post_TP(DECL_ARGS
);
107 static const struct termact termacts
[MAN_MAX
] = {
108 { pre_sp
, NULL
, MAN_NOTEXT
}, /* br */
109 { NULL
, NULL
, 0 }, /* TH */
110 { pre_SH
, post_SH
, 0 }, /* SH */
111 { pre_SS
, post_SS
, 0 }, /* SS */
112 { pre_TP
, post_TP
, 0 }, /* TP */
113 { pre_PP
, NULL
, 0 }, /* LP */
114 { pre_PP
, NULL
, 0 }, /* PP */
115 { pre_PP
, NULL
, 0 }, /* P */
116 { pre_IP
, post_IP
, 0 }, /* IP */
117 { pre_HP
, post_HP
, 0 }, /* HP */
118 { NULL
, NULL
, 0 }, /* SM */
119 { pre_B
, NULL
, 0 }, /* SB */
120 { pre_BI
, NULL
, 0 }, /* BI */
121 { pre_BI
, NULL
, 0 }, /* IB */
122 { pre_RB
, NULL
, 0 }, /* BR */
123 { pre_RB
, NULL
, 0 }, /* RB */
124 { NULL
, NULL
, 0 }, /* R */
125 { pre_B
, NULL
, 0 }, /* B */
126 { pre_I
, NULL
, 0 }, /* I */
127 { pre_RI
, NULL
, 0 }, /* IR */
128 { pre_RI
, NULL
, 0 }, /* RI */
129 { NULL
, NULL
, MAN_NOTEXT
}, /* na */
130 { pre_I
, NULL
, 0 }, /* i */
131 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
132 { pre_literal
, NULL
, 0 }, /* nf */
133 { pre_literal
, NULL
, 0 }, /* fi */
134 { NULL
, NULL
, 0 }, /* r */
135 { NULL
, NULL
, 0 }, /* RE */
136 { pre_RS
, post_RS
, 0 }, /* RS */
137 { pre_ign
, NULL
, 0 }, /* DT */
138 { pre_ign
, NULL
, 0 }, /* UC */
139 { pre_ign
, NULL
, 0 }, /* PD */
140 { pre_ign
, NULL
, 0 }, /* AT */
141 { pre_in
, NULL
, MAN_NOTEXT
}, /* in */
147 terminal_man(void *arg
, const struct man
*man
)
150 const struct man_node
*n
;
151 const struct man_meta
*m
;
154 p
= (struct termp
*)arg
;
157 p
->maxrmargin
= p
->defrmargin
;
158 p
->tabwidth
= term_len(p
, 5);
160 if (NULL
== p
->symtab
)
162 case (TERMENC_ASCII
):
163 p
->symtab
= chars_init(CHARS_ASCII
);
173 term_begin(p
, print_man_head
, print_man_foot
, m
);
174 p
->flags
|= TERMP_NOSPACE
;
177 mt
.lmargin
= term_len(p
, INDENT
);
178 mt
.offset
= term_len(p
, INDENT
);
181 print_man_nodelist(p
, &mt
, n
->child
, m
);
188 a2height(const struct termp
*p
, const char *cp
)
192 if ( ! a2roffsu(cp
, &su
, SCALE_VS
))
193 SCALE_VS_INIT(&su
, term_strlen(p
, cp
));
195 return(term_vspan(p
, &su
));
200 a2width(const struct termp
*p
, const char *cp
)
204 if ( ! a2roffsu(cp
, &su
, SCALE_BU
))
207 return((int)term_hspan(p
, &su
));
212 print_bvspace(struct termp
*p
, const struct man_node
*n
)
219 if (MAN_SS
== n
->prev
->tok
)
221 if (MAN_SH
== n
->prev
->tok
)
242 term_fontrepl(p
, TERMFONT_UNDER
);
249 pre_literal(DECL_ARGS
)
255 mt
->fl
|= MANT_LITERAL
;
258 mt
->fl
&= ~MANT_LITERAL
;
271 const struct man_node
*nn
;
274 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
275 if (i
% 2 && MAN_RB
== n
->tok
)
276 term_fontrepl(p
, TERMFONT_BOLD
);
277 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
278 term_fontrepl(p
, TERMFONT_BOLD
);
280 term_fontrepl(p
, TERMFONT_NONE
);
283 p
->flags
|= TERMP_NOSPACE
;
285 print_man_node(p
, mt
, nn
, m
);
295 const struct man_node
*nn
;
298 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
299 if (i
% 2 && MAN_RI
== n
->tok
)
300 term_fontrepl(p
, TERMFONT_UNDER
);
301 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
302 term_fontrepl(p
, TERMFONT_UNDER
);
304 term_fontrepl(p
, TERMFONT_NONE
);
307 p
->flags
|= TERMP_NOSPACE
;
309 print_man_node(p
, mt
, nn
, m
);
319 const struct man_node
*nn
;
322 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
323 if (i
% 2 && MAN_BI
== n
->tok
)
324 term_fontrepl(p
, TERMFONT_UNDER
);
326 term_fontrepl(p
, TERMFONT_BOLD
);
327 else if (MAN_BI
== n
->tok
)
328 term_fontrepl(p
, TERMFONT_BOLD
);
330 term_fontrepl(p
, TERMFONT_UNDER
);
333 p
->flags
|= TERMP_NOSPACE
;
335 print_man_node(p
, mt
, nn
, m
);
346 term_fontrepl(p
, TERMFONT_BOLD
);
361 if (NULL
== n
->child
) {
362 p
->offset
= mt
->offset
;
366 cp
= n
->child
->string
;
376 if ((len
= a2width(p
, ++cp
)) < 0)
382 p
->offset
-= p
->offset
> v
? v
: p
->offset
;
403 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
409 for (i
= 0; i
< len
; i
++)
422 const struct man_node
*nn
;
429 p
->flags
|= TERMP_NOBREAK
;
430 p
->flags
|= TERMP_TWOSPACE
;
439 /* Calculate offset. */
441 if (NULL
!= (nn
= n
->parent
->head
->child
))
442 if ((ival
= a2width(p
, nn
->string
)) >= 0)
446 len
= term_len(p
, 1);
448 p
->offset
= mt
->offset
;
449 p
->rmargin
= mt
->offset
+ len
;
452 mt
->lmargin
= (size_t)ival
;
469 p
->flags
&= ~TERMP_NOBREAK
;
470 p
->flags
&= ~TERMP_TWOSPACE
;
471 p
->offset
= mt
->offset
;
472 p
->rmargin
= p
->maxrmargin
;
487 mt
->lmargin
= term_len(p
, INDENT
);
491 p
->offset
= mt
->offset
;
503 const struct man_node
*nn
;
509 p
->flags
|= TERMP_NOLPAD
;
510 p
->flags
|= TERMP_NOSPACE
;
513 p
->flags
|= TERMP_NOBREAK
;
525 /* Calculate offset. */
527 if (NULL
!= (nn
= n
->parent
->head
->child
))
528 if (NULL
!= (nn
= nn
->next
)) {
529 for ( ; nn
->next
; nn
= nn
->next
)
531 if ((ival
= a2width(p
, nn
->string
)) >= 0)
537 /* Handle zero-width lengths. */
539 len
= term_len(p
, 1);
541 p
->offset
= mt
->offset
;
542 p
->rmargin
= mt
->offset
+ len
;
546 /* Set the saved left-margin. */
547 mt
->lmargin
= (size_t)ival
;
549 /* Don't print the length value. */
550 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
551 print_man_node(p
, mt
, nn
, m
);
554 p
->offset
= mt
->offset
+ len
;
555 p
->rmargin
= p
->maxrmargin
;
573 p
->flags
&= ~TERMP_NOBREAK
;
574 p
->rmargin
= p
->maxrmargin
;
578 p
->flags
&= ~TERMP_NOLPAD
;
590 const struct man_node
*nn
;
596 p
->flags
|= TERMP_NOBREAK
;
597 p
->flags
|= TERMP_TWOSPACE
;
600 p
->flags
|= TERMP_NOLPAD
;
601 p
->flags
|= TERMP_NOSPACE
;
610 len
= (size_t)mt
->lmargin
;
613 /* Calculate offset. */
615 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
616 while (nn
&& MAN_TEXT
!= nn
->type
)
619 if ((ival
= a2width(p
, nn
->string
)) >= 0)
625 /* Handle zero-length properly. */
627 len
= term_len(p
, 1);
629 p
->offset
= mt
->offset
;
630 p
->rmargin
= mt
->offset
+ len
;
632 /* Don't print same-line elements. */
633 for (nn
= n
->child
; nn
; nn
= nn
->next
)
634 if (nn
->line
> n
->line
)
635 print_man_node(p
, mt
, nn
, m
);
638 mt
->lmargin
= (size_t)ival
;
642 p
->offset
= mt
->offset
+ len
;
643 p
->rmargin
= p
->maxrmargin
;
661 p
->flags
&= ~TERMP_NOBREAK
;
662 p
->flags
&= ~TERMP_TWOSPACE
;
663 p
->rmargin
= p
->maxrmargin
;
667 p
->flags
&= ~TERMP_NOLPAD
;
682 mt
->lmargin
= term_len(p
, INDENT
);
683 mt
->offset
= term_len(p
, INDENT
);
684 /* If following a prior empty `SS', no vspace. */
685 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
686 if (NULL
== n
->prev
->body
->child
)
693 term_fontrepl(p
, TERMFONT_BOLD
);
694 p
->offset
= term_len(p
, HALFINDENT
);
697 p
->offset
= mt
->offset
;
732 mt
->lmargin
= term_len(p
, INDENT
);
733 mt
->offset
= term_len(p
, INDENT
);
734 /* If following a prior empty `SH', no vspace. */
735 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
736 if (NULL
== n
->prev
->body
->child
)
738 /* If the first macro, no vspae. */
744 term_fontrepl(p
, TERMFONT_BOLD
);
748 p
->offset
= mt
->offset
;
780 const struct man_node
*nn
;
793 if (NULL
== (nn
= n
->parent
->head
->child
)) {
794 mt
->offset
= mt
->lmargin
+ term_len(p
, INDENT
);
795 p
->offset
= mt
->offset
;
799 if ((ival
= a2width(p
, nn
->string
)) < 0)
802 mt
->offset
= term_len(p
, INDENT
) + (size_t)ival
;
803 p
->offset
= mt
->offset
;
816 mt
->offset
= mt
->lmargin
= term_len(p
, INDENT
);
822 p
->offset
= term_len(p
, INDENT
);
829 print_man_node(DECL_ARGS
)
838 if (0 == *n
->string
) {
843 term_word(p
, n
->string
);
845 /* FIXME: this means that macro lines are munged! */
847 if (MANT_LITERAL
& mt
->fl
) {
849 rmax
= p
->maxrmargin
;
850 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
851 p
->flags
|= TERMP_NOSPACE
;
854 p
->maxrmargin
= rmax
;
858 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
859 term_fontrepl(p
, TERMFONT_NONE
);
860 if (termacts
[n
->tok
].pre
)
861 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
866 print_man_nodelist(p
, mt
, n
->child
, m
);
868 if (MAN_TEXT
!= n
->type
) {
869 if (termacts
[n
->tok
].post
)
870 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
871 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
872 term_fontrepl(p
, TERMFONT_NONE
);
875 if (MAN_EOS
& n
->flags
)
876 p
->flags
|= TERMP_SENTENCE
;
881 print_man_nodelist(DECL_ARGS
)
884 print_man_node(p
, mt
, n
, m
);
887 print_man_nodelist(p
, mt
, n
->next
, m
);
892 print_man_foot(struct termp
*p
, const void *arg
)
895 const struct man_meta
*meta
;
897 meta
= (const struct man_meta
*)arg
;
899 term_fontrepl(p
, TERMFONT_NONE
);
902 strlcpy(buf
, meta
->rawdate
, DATESIZ
);
904 time2a(meta
->date
, buf
, DATESIZ
);
910 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
911 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, buf
);
914 /* term_strlen() can return zero. */
915 if (p
->rmargin
== p
->maxrmargin
)
919 term_word(p
, meta
->source
);
924 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
925 p
->offset
= p
->rmargin
;
926 p
->rmargin
= p
->maxrmargin
;
927 p
->flags
&= ~TERMP_NOBREAK
;
935 print_man_head(struct termp
*p
, const void *arg
)
937 char buf
[BUFSIZ
], title
[BUFSIZ
];
938 size_t buflen
, titlen
;
939 const struct man_meta
*m
;
941 m
= (const struct man_meta
*)arg
;
944 * Note that old groff would spit out some spaces before the
945 * header. We discontinue this strange behaviour, but at one
946 * point we did so here.
949 p
->rmargin
= p
->maxrmargin
;
952 buf
[0] = title
[0] = '\0';
955 strlcpy(buf
, m
->vol
, BUFSIZ
);
956 buflen
= term_strlen(p
, buf
);
958 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
959 titlen
= term_strlen(p
, title
);
962 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
964 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
965 p
->maxrmargin
- buflen
;
966 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
971 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
972 p
->offset
= p
->rmargin
;
973 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
974 p
->maxrmargin
- titlen
: p
->maxrmargin
;
979 p
->flags
&= ~TERMP_NOBREAK
;
980 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
981 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
982 p
->offset
= p
->rmargin
;
983 p
->rmargin
= p
->maxrmargin
;
988 p
->rmargin
= p
->maxrmargin
;
990 p
->flags
&= ~TERMP_NOSPACE
;
993 * Groff likes to have some leading spaces before content. Well