]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.89 2010/12/06 15:31:19 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_alternate(DECL_ARGS
);
84 static int pre_B(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_RS(DECL_ARGS
);
90 static int pre_SH(DECL_ARGS
);
91 static int pre_SS(DECL_ARGS
);
92 static int pre_TP(DECL_ARGS
);
93 static int pre_ign(DECL_ARGS
);
94 static int pre_in(DECL_ARGS
);
95 static int pre_literal(DECL_ARGS
);
96 static int pre_sp(DECL_ARGS
);
97 static int pre_ft(DECL_ARGS
);
99 static void post_IP(DECL_ARGS
);
100 static void post_HP(DECL_ARGS
);
101 static void post_RS(DECL_ARGS
);
102 static void post_SH(DECL_ARGS
);
103 static void post_SS(DECL_ARGS
);
104 static void post_TP(DECL_ARGS
);
106 static const struct termact termacts
[MAN_MAX
] = {
107 { pre_sp
, NULL
, MAN_NOTEXT
}, /* br */
108 { NULL
, NULL
, 0 }, /* TH */
109 { pre_SH
, post_SH
, 0 }, /* SH */
110 { pre_SS
, post_SS
, 0 }, /* SS */
111 { pre_TP
, post_TP
, 0 }, /* TP */
112 { pre_PP
, NULL
, 0 }, /* LP */
113 { pre_PP
, NULL
, 0 }, /* PP */
114 { pre_PP
, NULL
, 0 }, /* P */
115 { pre_IP
, post_IP
, 0 }, /* IP */
116 { pre_HP
, post_HP
, 0 }, /* HP */
117 { NULL
, NULL
, 0 }, /* SM */
118 { pre_B
, NULL
, 0 }, /* SB */
119 { pre_alternate
, NULL
, 0 }, /* BI */
120 { pre_alternate
, NULL
, 0 }, /* IB */
121 { pre_alternate
, NULL
, 0 }, /* BR */
122 { pre_alternate
, NULL
, 0 }, /* RB */
123 { NULL
, NULL
, 0 }, /* R */
124 { pre_B
, NULL
, 0 }, /* B */
125 { pre_I
, NULL
, 0 }, /* I */
126 { pre_alternate
, NULL
, 0 }, /* IR */
127 { pre_alternate
, NULL
, 0 }, /* RI */
128 { NULL
, NULL
, MAN_NOTEXT
}, /* na */
129 { pre_I
, NULL
, 0 }, /* i */
130 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
131 { pre_literal
, NULL
, 0 }, /* nf */
132 { pre_literal
, NULL
, 0 }, /* fi */
133 { NULL
, NULL
, 0 }, /* r */
134 { NULL
, NULL
, 0 }, /* RE */
135 { pre_RS
, post_RS
, 0 }, /* RS */
136 { pre_ign
, NULL
, 0 }, /* DT */
137 { pre_ign
, NULL
, 0 }, /* UC */
138 { pre_ign
, NULL
, 0 }, /* PD */
139 { pre_ign
, NULL
, 0 }, /* AT */
140 { pre_in
, NULL
, MAN_NOTEXT
}, /* in */
141 { pre_ft
, NULL
, MAN_NOTEXT
}, /* ft */
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
)
254 if (MAN_nf
== n
->tok
)
255 mt
->fl
|= MANT_LITERAL
;
257 mt
->fl
&= ~MANT_LITERAL
;
264 pre_alternate(DECL_ARGS
)
266 enum termfont font
[2];
267 const struct man_node
*nn
;
272 font
[0] = TERMFONT_NONE
;
273 font
[1] = TERMFONT_BOLD
;
276 font
[0] = TERMFONT_NONE
;
277 font
[1] = TERMFONT_UNDER
;
280 font
[0] = TERMFONT_BOLD
;
281 font
[1] = TERMFONT_NONE
;
284 font
[0] = TERMFONT_BOLD
;
285 font
[1] = TERMFONT_UNDER
;
288 font
[0] = TERMFONT_UNDER
;
289 font
[1] = TERMFONT_NONE
;
292 font
[0] = TERMFONT_UNDER
;
293 font
[1] = TERMFONT_BOLD
;
299 savelit
= MANT_LITERAL
& mt
->fl
;
300 mt
->fl
&= ~MANT_LITERAL
;
302 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
= 1 - i
) {
303 term_fontrepl(p
, font
[i
]);
304 if (savelit
&& NULL
== nn
->next
)
305 mt
->fl
|= MANT_LITERAL
;
306 print_man_node(p
, mt
, nn
, m
);
308 p
->flags
|= TERMP_NOSPACE
;
319 term_fontrepl(p
, TERMFONT_BOLD
);
329 if (NULL
== n
->child
) {
334 cp
= n
->child
->string
;
341 term_fontrepl(p
, TERMFONT_BOLD
);
346 term_fontrepl(p
, TERMFONT_UNDER
);
356 term_fontrepl(p
, TERMFONT_NONE
);
374 if (NULL
== n
->child
) {
375 p
->offset
= mt
->offset
;
379 cp
= n
->child
->string
;
389 if ((len
= a2width(p
, ++cp
)) < 0)
395 p
->offset
-= p
->offset
> v
? v
: p
->offset
;
416 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
422 for (i
= 0; i
< len
; i
++)
435 const struct man_node
*nn
;
442 p
->flags
|= TERMP_NOBREAK
;
443 p
->flags
|= TERMP_TWOSPACE
;
452 /* Calculate offset. */
454 if (NULL
!= (nn
= n
->parent
->head
->child
))
455 if ((ival
= a2width(p
, nn
->string
)) >= 0)
459 len
= term_len(p
, 1);
461 p
->offset
= mt
->offset
;
462 p
->rmargin
= mt
->offset
+ len
;
465 mt
->lmargin
= (size_t)ival
;
482 p
->flags
&= ~TERMP_NOBREAK
;
483 p
->flags
&= ~TERMP_TWOSPACE
;
484 p
->offset
= mt
->offset
;
485 p
->rmargin
= p
->maxrmargin
;
500 mt
->lmargin
= term_len(p
, INDENT
);
504 p
->offset
= mt
->offset
;
508 return(MAN_HEAD
!= n
->type
);
516 const struct man_node
*nn
;
522 p
->flags
|= TERMP_NOLPAD
;
523 p
->flags
|= TERMP_NOSPACE
;
526 p
->flags
|= TERMP_NOBREAK
;
538 /* Calculate offset. */
540 if (NULL
!= (nn
= n
->parent
->head
->child
))
541 if (NULL
!= (nn
= nn
->next
)) {
542 for ( ; nn
->next
; nn
= nn
->next
)
544 if ((ival
= a2width(p
, nn
->string
)) >= 0)
550 /* Handle zero-width lengths. */
552 len
= term_len(p
, 1);
554 p
->offset
= mt
->offset
;
555 p
->rmargin
= mt
->offset
+ len
;
559 /* Set the saved left-margin. */
560 mt
->lmargin
= (size_t)ival
;
562 /* Don't print the length value. */
563 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
564 print_man_node(p
, mt
, nn
, m
);
567 p
->offset
= mt
->offset
+ len
;
568 p
->rmargin
= p
->maxrmargin
;
586 p
->flags
&= ~TERMP_NOBREAK
;
587 p
->rmargin
= p
->maxrmargin
;
591 p
->flags
&= ~TERMP_NOLPAD
;
603 const struct man_node
*nn
;
609 p
->flags
|= TERMP_NOBREAK
;
610 p
->flags
|= TERMP_TWOSPACE
;
613 p
->flags
|= TERMP_NOLPAD
;
614 p
->flags
|= TERMP_NOSPACE
;
623 len
= (size_t)mt
->lmargin
;
626 /* Calculate offset. */
628 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
629 while (nn
&& MAN_TEXT
!= nn
->type
)
632 if ((ival
= a2width(p
, nn
->string
)) >= 0)
638 /* Handle zero-length properly. */
640 len
= term_len(p
, 1);
642 p
->offset
= mt
->offset
;
643 p
->rmargin
= mt
->offset
+ len
;
645 /* Don't print same-line elements. */
646 for (nn
= n
->child
; nn
; nn
= nn
->next
)
647 if (nn
->line
> n
->line
)
648 print_man_node(p
, mt
, nn
, m
);
651 mt
->lmargin
= (size_t)ival
;
655 p
->offset
= mt
->offset
+ len
;
656 p
->rmargin
= p
->maxrmargin
;
674 p
->flags
&= ~TERMP_NOBREAK
;
675 p
->flags
&= ~TERMP_TWOSPACE
;
676 p
->rmargin
= p
->maxrmargin
;
680 p
->flags
&= ~TERMP_NOLPAD
;
695 mt
->lmargin
= term_len(p
, INDENT
);
696 mt
->offset
= term_len(p
, INDENT
);
697 /* If following a prior empty `SS', no vspace. */
698 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
699 if (NULL
== n
->prev
->body
->child
)
706 term_fontrepl(p
, TERMFONT_BOLD
);
707 p
->offset
= term_len(p
, HALFINDENT
);
710 p
->offset
= mt
->offset
;
745 mt
->lmargin
= term_len(p
, INDENT
);
746 mt
->offset
= term_len(p
, INDENT
);
747 /* If following a prior empty `SH', no vspace. */
748 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
749 if (NULL
== n
->prev
->body
->child
)
751 /* If the first macro, no vspae. */
757 term_fontrepl(p
, TERMFONT_BOLD
);
761 p
->offset
= mt
->offset
;
793 const struct man_node
*nn
;
806 if (NULL
== (nn
= n
->parent
->head
->child
)) {
807 mt
->offset
= mt
->lmargin
+ term_len(p
, INDENT
);
808 p
->offset
= mt
->offset
;
812 if ((ival
= a2width(p
, nn
->string
)) < 0)
815 mt
->offset
= term_len(p
, INDENT
) + (size_t)ival
;
816 p
->offset
= mt
->offset
;
829 mt
->offset
= mt
->lmargin
= term_len(p
, INDENT
);
835 p
->offset
= term_len(p
, INDENT
);
842 print_man_node(DECL_ARGS
)
851 if (0 == *n
->string
) {
856 term_word(p
, n
->string
);
858 /* FIXME: this means that macro lines are munged! */
860 if (MANT_LITERAL
& mt
->fl
) {
862 rmax
= p
->maxrmargin
;
863 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
864 p
->flags
|= TERMP_NOSPACE
;
867 p
->maxrmargin
= rmax
;
871 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
872 term_fontrepl(p
, TERMFONT_NONE
);
873 if (termacts
[n
->tok
].pre
)
874 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
879 print_man_nodelist(p
, mt
, n
->child
, m
);
881 if (MAN_TEXT
!= n
->type
) {
882 if (termacts
[n
->tok
].post
)
883 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
884 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
885 term_fontrepl(p
, TERMFONT_NONE
);
888 if (MAN_EOS
& n
->flags
)
889 p
->flags
|= TERMP_SENTENCE
;
894 print_man_nodelist(DECL_ARGS
)
897 print_man_node(p
, mt
, n
, m
);
900 print_man_nodelist(p
, mt
, n
->next
, m
);
905 print_man_foot(struct termp
*p
, const void *arg
)
908 const struct man_meta
*meta
;
910 meta
= (const struct man_meta
*)arg
;
912 term_fontrepl(p
, TERMFONT_NONE
);
915 strlcpy(buf
, meta
->rawdate
, DATESIZ
);
917 time2a(meta
->date
, buf
, DATESIZ
);
923 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
924 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, buf
);
927 /* term_strlen() can return zero. */
928 if (p
->rmargin
== p
->maxrmargin
)
932 term_word(p
, meta
->source
);
937 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
938 p
->offset
= p
->rmargin
;
939 p
->rmargin
= p
->maxrmargin
;
940 p
->flags
&= ~TERMP_NOBREAK
;
948 print_man_head(struct termp
*p
, const void *arg
)
950 char buf
[BUFSIZ
], title
[BUFSIZ
];
951 size_t buflen
, titlen
;
952 const struct man_meta
*m
;
954 m
= (const struct man_meta
*)arg
;
957 * Note that old groff would spit out some spaces before the
958 * header. We discontinue this strange behaviour, but at one
959 * point we did so here.
962 p
->rmargin
= p
->maxrmargin
;
965 buf
[0] = title
[0] = '\0';
968 strlcpy(buf
, m
->vol
, BUFSIZ
);
969 buflen
= term_strlen(p
, buf
);
971 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
972 titlen
= term_strlen(p
, title
);
975 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
977 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
978 p
->maxrmargin
- buflen
;
979 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
984 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
985 p
->offset
= p
->rmargin
;
986 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
987 p
->maxrmargin
- titlen
: p
->maxrmargin
;
992 p
->flags
&= ~TERMP_NOBREAK
;
993 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
994 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
995 p
->offset
= p
->rmargin
;
996 p
->rmargin
= p
->maxrmargin
;
1001 p
->rmargin
= p
->maxrmargin
;
1003 p
->flags
&= ~TERMP_NOSPACE
;
1006 * Groff likes to have some leading spaces before content. Well
1007 * that's fine by me.