]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.84 2010/07/23 13:22:35 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_sp
, NULL
, MAN_NOTEXT
}, /* Sp */
141 { pre_literal
, NULL
, 0 }, /* Vb */
142 { pre_literal
, NULL
, 0 }, /* Ve */
143 { pre_ign
, NULL
, 0 }, /* AT */
144 { pre_in
, NULL
, MAN_NOTEXT
}, /* in */
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
;
161 p
->tabwidth
= term_len(p
, 5);
163 if (NULL
== p
->symtab
)
165 case (TERMENC_ASCII
):
166 p
->symtab
= chars_init(CHARS_ASCII
);
176 term_begin(p
, print_man_head
, print_man_foot
, m
);
177 p
->flags
|= TERMP_NOSPACE
;
180 mt
.lmargin
= term_len(p
, INDENT
);
181 mt
.offset
= term_len(p
, INDENT
);
184 print_man_nodelist(p
, &mt
, n
->child
, m
);
191 a2height(const struct termp
*p
, const char *cp
)
195 if ( ! a2roffsu(cp
, &su
, SCALE_VS
))
196 SCALE_VS_INIT(&su
, term_strlen(p
, cp
));
198 return(term_vspan(p
, &su
));
203 a2width(const struct termp
*p
, const char *cp
)
207 if ( ! a2roffsu(cp
, &su
, SCALE_BU
))
210 return((int)term_hspan(p
, &su
));
215 print_bvspace(struct termp
*p
, const struct man_node
*n
)
222 if (MAN_SS
== n
->prev
->tok
)
224 if (MAN_SH
== n
->prev
->tok
)
245 term_fontrepl(p
, TERMFONT_UNDER
);
252 pre_literal(DECL_ARGS
)
260 mt
->fl
|= MANT_LITERAL
;
261 return(MAN_Vb
!= n
->tok
);
263 mt
->fl
&= ~MANT_LITERAL
;
276 const struct man_node
*nn
;
279 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
280 if (i
% 2 && MAN_RB
== n
->tok
)
281 term_fontrepl(p
, TERMFONT_BOLD
);
282 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
283 term_fontrepl(p
, TERMFONT_BOLD
);
285 term_fontrepl(p
, TERMFONT_NONE
);
288 p
->flags
|= TERMP_NOSPACE
;
290 print_man_node(p
, mt
, nn
, m
);
300 const struct man_node
*nn
;
303 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
304 if (i
% 2 && MAN_RI
== n
->tok
)
305 term_fontrepl(p
, TERMFONT_UNDER
);
306 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
307 term_fontrepl(p
, TERMFONT_UNDER
);
309 term_fontrepl(p
, TERMFONT_NONE
);
312 p
->flags
|= TERMP_NOSPACE
;
314 print_man_node(p
, mt
, nn
, m
);
324 const struct man_node
*nn
;
327 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
328 if (i
% 2 && MAN_BI
== n
->tok
)
329 term_fontrepl(p
, TERMFONT_UNDER
);
331 term_fontrepl(p
, TERMFONT_BOLD
);
332 else if (MAN_BI
== n
->tok
)
333 term_fontrepl(p
, TERMFONT_BOLD
);
335 term_fontrepl(p
, TERMFONT_UNDER
);
338 p
->flags
|= TERMP_NOSPACE
;
340 print_man_node(p
, mt
, nn
, m
);
351 term_fontrepl(p
, TERMFONT_BOLD
);
366 if (NULL
== n
->child
) {
367 p
->offset
= mt
->offset
;
371 cp
= n
->child
->string
;
381 if ((len
= a2width(p
, ++cp
)) < 0)
387 p
->offset
-= p
->offset
> v
? v
: p
->offset
;
408 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
414 for (i
= 0; i
< len
; i
++)
427 const struct man_node
*nn
;
434 p
->flags
|= TERMP_NOBREAK
;
435 p
->flags
|= TERMP_TWOSPACE
;
444 /* Calculate offset. */
446 if (NULL
!= (nn
= n
->parent
->head
->child
))
447 if ((ival
= a2width(p
, nn
->string
)) >= 0)
451 len
= term_len(p
, 1);
453 p
->offset
= mt
->offset
;
454 p
->rmargin
= mt
->offset
+ len
;
457 mt
->lmargin
= (size_t)ival
;
474 p
->flags
&= ~TERMP_NOBREAK
;
475 p
->flags
&= ~TERMP_TWOSPACE
;
476 p
->offset
= mt
->offset
;
477 p
->rmargin
= p
->maxrmargin
;
492 mt
->lmargin
= term_len(p
, INDENT
);
496 p
->offset
= mt
->offset
;
508 const struct man_node
*nn
;
514 p
->flags
|= TERMP_NOLPAD
;
515 p
->flags
|= TERMP_NOSPACE
;
518 p
->flags
|= TERMP_NOBREAK
;
530 /* Calculate offset. */
532 if (NULL
!= (nn
= n
->parent
->head
->child
))
533 if (NULL
!= (nn
= nn
->next
)) {
534 for ( ; nn
->next
; nn
= nn
->next
)
536 if ((ival
= a2width(p
, nn
->string
)) >= 0)
542 /* Handle zero-width lengths. */
544 len
= term_len(p
, 1);
546 p
->offset
= mt
->offset
;
547 p
->rmargin
= mt
->offset
+ len
;
551 /* Set the saved left-margin. */
552 mt
->lmargin
= (size_t)ival
;
554 /* Don't print the length value. */
555 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
556 print_man_node(p
, mt
, nn
, m
);
559 p
->offset
= mt
->offset
+ len
;
560 p
->rmargin
= p
->maxrmargin
;
578 p
->flags
&= ~TERMP_NOBREAK
;
579 p
->rmargin
= p
->maxrmargin
;
583 p
->flags
&= ~TERMP_NOLPAD
;
595 const struct man_node
*nn
;
601 p
->flags
|= TERMP_NOBREAK
;
602 p
->flags
|= TERMP_TWOSPACE
;
605 p
->flags
|= TERMP_NOLPAD
;
606 p
->flags
|= TERMP_NOSPACE
;
615 len
= (size_t)mt
->lmargin
;
618 /* Calculate offset. */
620 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
621 while (nn
&& MAN_TEXT
!= nn
->type
)
624 if ((ival
= a2width(p
, nn
->string
)) >= 0)
630 /* Handle zero-length properly. */
632 len
= term_len(p
, 1);
634 p
->offset
= mt
->offset
;
635 p
->rmargin
= mt
->offset
+ len
;
637 /* Don't print same-line elements. */
638 for (nn
= n
->child
; nn
; nn
= nn
->next
)
639 if (nn
->line
> n
->line
)
640 print_man_node(p
, mt
, nn
, m
);
643 mt
->lmargin
= (size_t)ival
;
647 p
->offset
= mt
->offset
+ len
;
648 p
->rmargin
= p
->maxrmargin
;
666 p
->flags
&= ~TERMP_NOBREAK
;
667 p
->flags
&= ~TERMP_TWOSPACE
;
668 p
->rmargin
= p
->maxrmargin
;
672 p
->flags
&= ~TERMP_NOLPAD
;
687 mt
->lmargin
= term_len(p
, INDENT
);
688 mt
->offset
= term_len(p
, INDENT
);
689 /* If following a prior empty `SS', no vspace. */
690 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
691 if (NULL
== n
->prev
->body
->child
)
698 term_fontrepl(p
, TERMFONT_BOLD
);
699 p
->offset
= term_len(p
, HALFINDENT
);
702 p
->offset
= mt
->offset
;
737 mt
->lmargin
= term_len(p
, INDENT
);
738 mt
->offset
= term_len(p
, INDENT
);
739 /* If following a prior empty `SH', no vspace. */
740 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
741 if (NULL
== n
->prev
->body
->child
)
743 /* If the first macro, no vspae. */
749 term_fontrepl(p
, TERMFONT_BOLD
);
753 p
->offset
= mt
->offset
;
785 const struct man_node
*nn
;
798 if (NULL
== (nn
= n
->parent
->head
->child
)) {
799 mt
->offset
= mt
->lmargin
+ term_len(p
, INDENT
);
800 p
->offset
= mt
->offset
;
804 if ((ival
= a2width(p
, nn
->string
)) < 0)
807 mt
->offset
= term_len(p
, INDENT
) + (size_t)ival
;
808 p
->offset
= mt
->offset
;
821 mt
->offset
= mt
->lmargin
= term_len(p
, INDENT
);
827 p
->offset
= term_len(p
, INDENT
);
834 print_man_node(DECL_ARGS
)
843 if (0 == *n
->string
) {
848 term_word(p
, n
->string
);
850 /* FIXME: this means that macro lines are munged! */
852 if (MANT_LITERAL
& mt
->fl
) {
854 rmax
= p
->maxrmargin
;
855 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
856 p
->flags
|= TERMP_NOSPACE
;
859 p
->maxrmargin
= rmax
;
863 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
864 term_fontrepl(p
, TERMFONT_NONE
);
865 if (termacts
[n
->tok
].pre
)
866 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
871 print_man_nodelist(p
, mt
, n
->child
, m
);
873 if (MAN_TEXT
!= n
->type
) {
874 if (termacts
[n
->tok
].post
)
875 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
876 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
877 term_fontrepl(p
, TERMFONT_NONE
);
880 if (MAN_EOS
& n
->flags
)
881 p
->flags
|= TERMP_SENTENCE
;
886 print_man_nodelist(DECL_ARGS
)
889 print_man_node(p
, mt
, n
, m
);
892 print_man_nodelist(p
, mt
, n
->next
, m
);
897 print_man_foot(struct termp
*p
, const void *arg
)
900 const struct man_meta
*meta
;
902 meta
= (const struct man_meta
*)arg
;
904 term_fontrepl(p
, TERMFONT_NONE
);
907 strlcpy(buf
, meta
->rawdate
, DATESIZ
);
909 time2a(meta
->date
, buf
, DATESIZ
);
915 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
916 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, buf
);
920 term_word(p
, meta
->source
);
925 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
926 p
->offset
= p
->rmargin
;
927 p
->rmargin
= p
->maxrmargin
;
928 p
->flags
&= ~TERMP_NOBREAK
;
936 print_man_head(struct termp
*p
, const void *arg
)
938 char buf
[BUFSIZ
], title
[BUFSIZ
];
939 size_t buflen
, titlen
;
940 const struct man_meta
*m
;
942 m
= (const struct man_meta
*)arg
;
945 * Note that old groff would spit out some spaces before the
946 * header. We discontinue this strange behaviour, but at one
947 * point we did so here.
950 p
->rmargin
= p
->maxrmargin
;
953 buf
[0] = title
[0] = '\0';
956 strlcpy(buf
, m
->vol
, BUFSIZ
);
957 buflen
= term_strlen(p
, buf
);
959 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
960 titlen
= term_strlen(p
, title
);
963 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
965 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
966 p
->maxrmargin
- buflen
;
967 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
972 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
973 p
->offset
= p
->rmargin
;
974 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
975 p
->maxrmargin
- titlen
: p
->maxrmargin
;
980 p
->flags
&= ~TERMP_NOBREAK
;
981 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
982 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
983 p
->offset
= p
->rmargin
;
984 p
->rmargin
= p
->maxrmargin
;
989 p
->rmargin
= p
->maxrmargin
;
991 p
->flags
&= ~TERMP_NOSPACE
;
994 * Groff likes to have some leading spaces before content. Well