]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.102 2011/02/06 21:44:36 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #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_alternate(DECL_ARGS
);
85 static int pre_B(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_RS(DECL_ARGS
);
91 static int pre_SH(DECL_ARGS
);
92 static int pre_SS(DECL_ARGS
);
93 static int pre_TP(DECL_ARGS
);
94 static int pre_ign(DECL_ARGS
);
95 static int pre_in(DECL_ARGS
);
96 static int pre_literal(DECL_ARGS
);
97 static int pre_sp(DECL_ARGS
);
98 static int pre_ft(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_alternate
, NULL
, 0 }, /* BI */
121 { pre_alternate
, NULL
, 0 }, /* IB */
122 { pre_alternate
, NULL
, 0 }, /* BR */
123 { pre_alternate
, NULL
, 0 }, /* RB */
124 { NULL
, NULL
, 0 }, /* R */
125 { pre_B
, NULL
, 0 }, /* B */
126 { pre_I
, NULL
, 0 }, /* I */
127 { pre_alternate
, NULL
, 0 }, /* IR */
128 { pre_alternate
, NULL
, 0 }, /* RI */
129 { pre_ign
, NULL
, MAN_NOTEXT
}, /* na */
130 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
131 { pre_literal
, NULL
, 0 }, /* nf */
132 { pre_literal
, NULL
, 0 }, /* fi */
133 { NULL
, NULL
, 0 }, /* RE */
134 { pre_RS
, post_RS
, 0 }, /* RS */
135 { pre_ign
, NULL
, 0 }, /* DT */
136 { pre_ign
, NULL
, 0 }, /* UC */
137 { pre_ign
, NULL
, 0 }, /* PD */
138 { pre_ign
, NULL
, 0 }, /* AT */
139 { pre_in
, NULL
, MAN_NOTEXT
}, /* in */
140 { pre_ft
, NULL
, MAN_NOTEXT
}, /* ft */
146 terminal_man(void *arg
, const struct man
*man
)
149 const struct man_node
*n
;
150 const struct man_meta
*m
;
153 p
= (struct termp
*)arg
;
156 p
->maxrmargin
= p
->defrmargin
;
157 p
->tabwidth
= term_len(p
, 5);
159 if (NULL
== p
->symtab
)
161 case (TERMENC_ASCII
):
162 p
->symtab
= chars_init(CHARS_ASCII
);
172 term_begin(p
, print_man_head
, print_man_foot
, m
);
173 p
->flags
|= TERMP_NOSPACE
;
176 mt
.lmargin
= term_len(p
, INDENT
);
177 mt
.offset
= term_len(p
, INDENT
);
180 print_man_nodelist(p
, &mt
, n
->child
, m
);
187 a2height(const struct termp
*p
, const char *cp
)
191 if ( ! a2roffsu(cp
, &su
, SCALE_VS
))
192 SCALE_VS_INIT(&su
, term_strlen(p
, cp
));
194 return(term_vspan(p
, &su
));
199 a2width(const struct termp
*p
, const char *cp
)
203 if ( ! a2roffsu(cp
, &su
, SCALE_BU
))
206 return((int)term_hspan(p
, &su
));
211 print_bvspace(struct termp
*p
, const struct man_node
*n
)
215 if (n
->body
&& n
->body
->child
&& MAN_TBL
== n
->body
->child
->type
)
221 if (MAN_SS
== n
->prev
->tok
)
223 if (MAN_SH
== n
->prev
->tok
)
244 term_fontrepl(p
, TERMFONT_UNDER
);
251 pre_literal(DECL_ARGS
)
256 if (MAN_nf
== n
->tok
)
257 mt
->fl
|= MANT_LITERAL
;
259 mt
->fl
&= ~MANT_LITERAL
;
266 pre_alternate(DECL_ARGS
)
268 enum termfont font
[2];
269 const struct man_node
*nn
;
274 font
[0] = TERMFONT_NONE
;
275 font
[1] = TERMFONT_BOLD
;
278 font
[0] = TERMFONT_NONE
;
279 font
[1] = TERMFONT_UNDER
;
282 font
[0] = TERMFONT_BOLD
;
283 font
[1] = TERMFONT_NONE
;
286 font
[0] = TERMFONT_BOLD
;
287 font
[1] = TERMFONT_UNDER
;
290 font
[0] = TERMFONT_UNDER
;
291 font
[1] = TERMFONT_NONE
;
294 font
[0] = TERMFONT_UNDER
;
295 font
[1] = TERMFONT_BOLD
;
301 savelit
= MANT_LITERAL
& mt
->fl
;
302 mt
->fl
&= ~MANT_LITERAL
;
304 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
= 1 - i
) {
305 term_fontrepl(p
, font
[i
]);
306 if (savelit
&& NULL
== nn
->next
)
307 mt
->fl
|= MANT_LITERAL
;
308 print_man_node(p
, mt
, nn
, m
);
310 p
->flags
|= TERMP_NOSPACE
;
321 term_fontrepl(p
, TERMFONT_BOLD
);
331 if (NULL
== n
->child
) {
336 cp
= n
->child
->string
;
343 term_fontrepl(p
, TERMFONT_BOLD
);
348 term_fontrepl(p
, TERMFONT_UNDER
);
358 term_fontrepl(p
, TERMFONT_NONE
);
376 if (NULL
== n
->child
) {
377 p
->offset
= mt
->offset
;
381 cp
= n
->child
->string
;
391 if ((len
= a2width(p
, ++cp
)) < 0)
397 p
->offset
-= p
->offset
> v
? v
: p
->offset
;
403 /* Don't let this creep beyond the right margin. */
405 if (p
->offset
> p
->rmargin
)
406 p
->offset
= p
->rmargin
;
423 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
429 for (i
= 0; i
< len
; i
++)
442 const struct man_node
*nn
;
449 p
->flags
|= TERMP_NOBREAK
;
450 p
->flags
|= TERMP_TWOSPACE
;
459 /* Calculate offset. */
461 if (NULL
!= (nn
= n
->parent
->head
->child
))
462 if ((ival
= a2width(p
, nn
->string
)) >= 0)
466 len
= term_len(p
, 1);
468 p
->offset
= mt
->offset
;
469 p
->rmargin
= mt
->offset
+ len
;
472 mt
->lmargin
= (size_t)ival
;
489 p
->flags
&= ~TERMP_NOBREAK
;
490 p
->flags
&= ~TERMP_TWOSPACE
;
491 p
->offset
= mt
->offset
;
492 p
->rmargin
= p
->maxrmargin
;
507 mt
->lmargin
= term_len(p
, INDENT
);
511 p
->offset
= mt
->offset
;
515 return(MAN_HEAD
!= n
->type
);
523 const struct man_node
*nn
;
529 p
->flags
|= TERMP_NOLPAD
;
530 p
->flags
|= TERMP_NOSPACE
;
533 p
->flags
|= TERMP_NOBREAK
;
545 /* Calculate the offset from the optional second argument. */
546 if (NULL
!= (nn
= n
->parent
->head
->child
))
547 if (NULL
!= (nn
= nn
->next
))
548 if ((ival
= a2width(p
, nn
->string
)) >= 0)
553 /* Handle zero-width lengths. */
555 len
= term_len(p
, 1);
557 p
->offset
= mt
->offset
;
558 p
->rmargin
= mt
->offset
+ len
;
562 /* Set the saved left-margin. */
563 mt
->lmargin
= (size_t)ival
;
565 savelit
= MANT_LITERAL
& mt
->fl
;
566 mt
->fl
&= ~MANT_LITERAL
;
569 print_man_node(p
, mt
, n
->child
, m
);
572 mt
->fl
|= MANT_LITERAL
;
576 p
->offset
= mt
->offset
+ len
;
577 p
->rmargin
= p
->maxrmargin
;
595 p
->flags
&= ~TERMP_NOBREAK
;
596 p
->rmargin
= p
->maxrmargin
;
600 p
->flags
&= ~TERMP_NOLPAD
;
612 const struct man_node
*nn
;
618 p
->flags
|= TERMP_NOBREAK
;
621 p
->flags
|= TERMP_NOLPAD
;
622 p
->flags
|= TERMP_NOSPACE
;
631 len
= (size_t)mt
->lmargin
;
634 /* Calculate offset. */
636 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
637 while (nn
&& MAN_TEXT
!= nn
->type
)
640 if ((ival
= a2width(p
, nn
->string
)) >= 0)
646 /* Handle zero-length properly. */
648 len
= term_len(p
, 1);
650 p
->offset
= mt
->offset
;
651 p
->rmargin
= mt
->offset
+ len
;
653 savelit
= MANT_LITERAL
& mt
->fl
;
654 mt
->fl
&= ~MANT_LITERAL
;
656 /* Don't print same-line elements. */
657 for (nn
= n
->child
; nn
; nn
= nn
->next
)
658 if (nn
->line
> n
->line
)
659 print_man_node(p
, mt
, nn
, m
);
662 mt
->fl
|= MANT_LITERAL
;
665 mt
->lmargin
= (size_t)ival
;
669 p
->offset
= mt
->offset
+ len
;
670 p
->rmargin
= p
->maxrmargin
;
688 p
->flags
&= ~TERMP_NOBREAK
;
689 p
->flags
&= ~TERMP_TWOSPACE
;
690 p
->rmargin
= p
->maxrmargin
;
694 p
->flags
&= ~TERMP_NOLPAD
;
709 mt
->lmargin
= term_len(p
, INDENT
);
710 mt
->offset
= term_len(p
, INDENT
);
711 /* If following a prior empty `SS', no vspace. */
712 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
713 if (NULL
== n
->prev
->body
->child
)
720 term_fontrepl(p
, TERMFONT_BOLD
);
721 p
->offset
= term_len(p
, HALFINDENT
);
724 p
->offset
= mt
->offset
;
759 mt
->lmargin
= term_len(p
, INDENT
);
760 mt
->offset
= term_len(p
, INDENT
);
761 /* If following a prior empty `SH', no vspace. */
762 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
763 if (NULL
== n
->prev
->body
->child
)
765 /* If the first macro, no vspae. */
771 term_fontrepl(p
, TERMFONT_BOLD
);
775 p
->offset
= mt
->offset
;
807 const struct man_node
*nn
;
820 if (NULL
== (nn
= n
->parent
->head
->child
)) {
821 mt
->offset
= mt
->lmargin
+ term_len(p
, INDENT
);
822 p
->offset
= mt
->offset
;
826 if ((ival
= a2width(p
, nn
->string
)) < 0)
829 mt
->offset
= term_len(p
, INDENT
) + (size_t)ival
;
830 p
->offset
= mt
->offset
;
843 mt
->offset
= mt
->lmargin
= term_len(p
, INDENT
);
849 p
->offset
= term_len(p
, INDENT
);
856 print_man_node(DECL_ARGS
)
864 * If we have a blank line, output a vertical space.
865 * If we have a space as the first character, break
866 * before printing the line's data.
868 if ('\0' == *n
->string
) {
871 } else if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
874 term_word(p
, n
->string
);
877 * If we're in a literal context, make sure that words
878 * togehter on the same line stay together. This is a
879 * POST-printing call, so we check the NEXT word. Since
880 * -man doesn't have nested macros, we don't need to be
881 * more specific than this.
883 if (MANT_LITERAL
& mt
->fl
&&
885 n
->next
->line
> n
->line
)) {
887 rmax
= p
->maxrmargin
;
888 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
889 p
->flags
|= TERMP_NOSPACE
;
891 p
->flags
&= ~TERMP_NOLPAD
;
893 p
->maxrmargin
= rmax
;
896 if (MAN_EOS
& n
->flags
)
897 p
->flags
|= TERMP_SENTENCE
;
903 * Tables are preceded by a newline. Then process a
904 * table line, which will cause line termination,
906 if (TBL_SPAN_FIRST
& n
->span
->flags
)
908 term_tbl(p
, n
->span
);
914 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
915 term_fontrepl(p
, TERMFONT_NONE
);
918 if (termacts
[n
->tok
].pre
)
919 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
922 print_man_nodelist(p
, mt
, n
->child
, m
);
924 if (termacts
[n
->tok
].post
)
925 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
926 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
927 term_fontrepl(p
, TERMFONT_NONE
);
929 if (MAN_EOS
& n
->flags
)
930 p
->flags
|= TERMP_SENTENCE
;
935 print_man_nodelist(DECL_ARGS
)
938 print_man_node(p
, mt
, n
, m
);
941 print_man_nodelist(p
, mt
, n
->next
, m
);
946 print_man_foot(struct termp
*p
, const void *arg
)
949 const struct man_meta
*meta
;
951 meta
= (const struct man_meta
*)arg
;
953 term_fontrepl(p
, TERMFONT_NONE
);
956 strlcpy(buf
, meta
->rawdate
, DATESIZ
);
958 time2a(meta
->date
, buf
, DATESIZ
);
964 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
965 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, buf
);
968 /* term_strlen() can return zero. */
969 if (p
->rmargin
== p
->maxrmargin
)
973 term_word(p
, meta
->source
);
978 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
979 p
->offset
= p
->rmargin
;
980 p
->rmargin
= p
->maxrmargin
;
981 p
->flags
&= ~TERMP_NOBREAK
;
989 print_man_head(struct termp
*p
, const void *arg
)
991 char buf
[BUFSIZ
], title
[BUFSIZ
];
992 size_t buflen
, titlen
;
993 const struct man_meta
*m
;
995 m
= (const struct man_meta
*)arg
;
998 * Note that old groff would spit out some spaces before the
999 * header. We discontinue this strange behaviour, but at one
1000 * point we did so here.
1003 p
->rmargin
= p
->maxrmargin
;
1006 buf
[0] = title
[0] = '\0';
1009 strlcpy(buf
, m
->vol
, BUFSIZ
);
1010 buflen
= term_strlen(p
, buf
);
1012 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
1013 titlen
= term_strlen(p
, title
);
1016 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
1018 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
1019 p
->maxrmargin
- buflen
;
1020 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
1022 term_word(p
, title
);
1025 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1026 p
->offset
= p
->rmargin
;
1027 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
1028 p
->maxrmargin
- titlen
: p
->maxrmargin
;
1033 p
->flags
&= ~TERMP_NOBREAK
;
1034 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
1035 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1036 p
->offset
= p
->rmargin
;
1037 p
->rmargin
= p
->maxrmargin
;
1038 term_word(p
, title
);
1042 p
->rmargin
= p
->maxrmargin
;
1044 p
->flags
&= ~TERMP_NOSPACE
;
1047 * Groff likes to have some leading spaces before content. Well
1048 * that's fine by me.