]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.98 2011/01/16 20:12:45 schwarze 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 { NULL
, 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
)
218 if (MAN_SS
== n
->prev
->tok
)
220 if (MAN_SH
== n
->prev
->tok
)
241 term_fontrepl(p
, TERMFONT_UNDER
);
248 pre_literal(DECL_ARGS
)
253 if (MAN_nf
== n
->tok
)
254 mt
->fl
|= MANT_LITERAL
;
256 mt
->fl
&= ~MANT_LITERAL
;
263 pre_alternate(DECL_ARGS
)
265 enum termfont font
[2];
266 const struct man_node
*nn
;
271 font
[0] = TERMFONT_NONE
;
272 font
[1] = TERMFONT_BOLD
;
275 font
[0] = TERMFONT_NONE
;
276 font
[1] = TERMFONT_UNDER
;
279 font
[0] = TERMFONT_BOLD
;
280 font
[1] = TERMFONT_NONE
;
283 font
[0] = TERMFONT_BOLD
;
284 font
[1] = TERMFONT_UNDER
;
287 font
[0] = TERMFONT_UNDER
;
288 font
[1] = TERMFONT_NONE
;
291 font
[0] = TERMFONT_UNDER
;
292 font
[1] = TERMFONT_BOLD
;
298 savelit
= MANT_LITERAL
& mt
->fl
;
299 mt
->fl
&= ~MANT_LITERAL
;
301 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
= 1 - i
) {
302 term_fontrepl(p
, font
[i
]);
303 if (savelit
&& NULL
== nn
->next
)
304 mt
->fl
|= MANT_LITERAL
;
305 print_man_node(p
, mt
, nn
, m
);
307 p
->flags
|= TERMP_NOSPACE
;
318 term_fontrepl(p
, TERMFONT_BOLD
);
328 if (NULL
== n
->child
) {
333 cp
= n
->child
->string
;
340 term_fontrepl(p
, TERMFONT_BOLD
);
345 term_fontrepl(p
, TERMFONT_UNDER
);
355 term_fontrepl(p
, TERMFONT_NONE
);
373 if (NULL
== n
->child
) {
374 p
->offset
= mt
->offset
;
378 cp
= n
->child
->string
;
388 if ((len
= a2width(p
, ++cp
)) < 0)
394 p
->offset
-= p
->offset
> v
? v
: p
->offset
;
400 /* Don't let this creep beyond the right margin. */
402 if (p
->offset
> p
->rmargin
)
403 p
->offset
= p
->rmargin
;
420 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
426 for (i
= 0; i
< len
; i
++)
439 const struct man_node
*nn
;
446 p
->flags
|= TERMP_NOBREAK
;
447 p
->flags
|= TERMP_TWOSPACE
;
456 /* Calculate offset. */
458 if (NULL
!= (nn
= n
->parent
->head
->child
))
459 if ((ival
= a2width(p
, nn
->string
)) >= 0)
463 len
= term_len(p
, 1);
465 p
->offset
= mt
->offset
;
466 p
->rmargin
= mt
->offset
+ len
;
469 mt
->lmargin
= (size_t)ival
;
486 p
->flags
&= ~TERMP_NOBREAK
;
487 p
->flags
&= ~TERMP_TWOSPACE
;
488 p
->offset
= mt
->offset
;
489 p
->rmargin
= p
->maxrmargin
;
504 mt
->lmargin
= term_len(p
, INDENT
);
508 p
->offset
= mt
->offset
;
512 return(MAN_HEAD
!= n
->type
);
520 const struct man_node
*nn
;
526 p
->flags
|= TERMP_NOLPAD
;
527 p
->flags
|= TERMP_NOSPACE
;
530 p
->flags
|= TERMP_NOBREAK
;
542 /* Calculate the offset from the optional second argument. */
543 if (NULL
!= (nn
= n
->parent
->head
->child
))
544 if (NULL
!= (nn
= nn
->next
))
545 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 savelit
= MANT_LITERAL
& mt
->fl
;
563 mt
->fl
&= ~MANT_LITERAL
;
566 print_man_node(p
, mt
, n
->child
, m
);
569 mt
->fl
|= MANT_LITERAL
;
573 p
->offset
= mt
->offset
+ len
;
574 p
->rmargin
= p
->maxrmargin
;
592 p
->flags
&= ~TERMP_NOBREAK
;
593 p
->rmargin
= p
->maxrmargin
;
597 p
->flags
&= ~TERMP_NOLPAD
;
609 const struct man_node
*nn
;
615 p
->flags
|= TERMP_NOBREAK
;
618 p
->flags
|= TERMP_NOLPAD
;
619 p
->flags
|= TERMP_NOSPACE
;
628 len
= (size_t)mt
->lmargin
;
631 /* Calculate offset. */
633 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
634 while (nn
&& MAN_TEXT
!= nn
->type
)
637 if ((ival
= a2width(p
, nn
->string
)) >= 0)
643 /* Handle zero-length properly. */
645 len
= term_len(p
, 1);
647 p
->offset
= mt
->offset
;
648 p
->rmargin
= mt
->offset
+ len
;
650 savelit
= MANT_LITERAL
& mt
->fl
;
651 mt
->fl
&= ~MANT_LITERAL
;
653 /* Don't print same-line elements. */
654 for (nn
= n
->child
; nn
; nn
= nn
->next
)
655 if (nn
->line
> n
->line
)
656 print_man_node(p
, mt
, nn
, m
);
659 mt
->fl
|= MANT_LITERAL
;
662 mt
->lmargin
= (size_t)ival
;
666 p
->offset
= mt
->offset
+ len
;
667 p
->rmargin
= p
->maxrmargin
;
685 p
->flags
&= ~TERMP_NOBREAK
;
686 p
->flags
&= ~TERMP_TWOSPACE
;
687 p
->rmargin
= p
->maxrmargin
;
691 p
->flags
&= ~TERMP_NOLPAD
;
706 mt
->lmargin
= term_len(p
, INDENT
);
707 mt
->offset
= term_len(p
, INDENT
);
708 /* If following a prior empty `SS', no vspace. */
709 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
710 if (NULL
== n
->prev
->body
->child
)
717 term_fontrepl(p
, TERMFONT_BOLD
);
718 p
->offset
= term_len(p
, HALFINDENT
);
721 p
->offset
= mt
->offset
;
756 mt
->lmargin
= term_len(p
, INDENT
);
757 mt
->offset
= term_len(p
, INDENT
);
758 /* If following a prior empty `SH', no vspace. */
759 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
760 if (NULL
== n
->prev
->body
->child
)
762 /* If the first macro, no vspae. */
768 term_fontrepl(p
, TERMFONT_BOLD
);
772 p
->offset
= mt
->offset
;
804 const struct man_node
*nn
;
817 if (NULL
== (nn
= n
->parent
->head
->child
)) {
818 mt
->offset
= mt
->lmargin
+ term_len(p
, INDENT
);
819 p
->offset
= mt
->offset
;
823 if ((ival
= a2width(p
, nn
->string
)) < 0)
826 mt
->offset
= term_len(p
, INDENT
) + (size_t)ival
;
827 p
->offset
= mt
->offset
;
840 mt
->offset
= mt
->lmargin
= term_len(p
, INDENT
);
846 p
->offset
= term_len(p
, INDENT
);
853 print_man_node(DECL_ARGS
)
861 * If we have a blank line, output a vertical space.
862 * If we have a space as the first character, break
863 * before printing the line's data.
865 if ('\0' == *n
->string
) {
868 } else if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
871 term_word(p
, n
->string
);
874 * If we're in a literal context, make sure that words
875 * togehter on the same line stay together. This is a
876 * POST-printing call, so we check the NEXT word. Since
877 * -man doesn't have nested macros, we don't need to be
878 * more specific than this.
880 if (MANT_LITERAL
& mt
->fl
&&
882 n
->next
->line
> n
->line
)) {
884 rmax
= p
->maxrmargin
;
885 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
886 p
->flags
|= TERMP_NOSPACE
;
888 p
->flags
&= ~TERMP_NOLPAD
;
890 p
->maxrmargin
= rmax
;
895 * Tables are preceded by a newline. Then process a
896 * table line, which will cause line termination,
898 if (TBL_SPAN_FIRST
& n
->span
->flags
)
900 term_tbl(p
, n
->span
);
906 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
907 term_fontrepl(p
, TERMFONT_NONE
);
910 if (termacts
[n
->tok
].pre
)
911 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
914 print_man_nodelist(p
, mt
, n
->child
, m
);
916 if (termacts
[n
->tok
].post
)
917 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
918 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
919 term_fontrepl(p
, TERMFONT_NONE
);
921 if (MAN_EOS
& n
->flags
)
922 p
->flags
|= TERMP_SENTENCE
;
927 print_man_nodelist(DECL_ARGS
)
930 print_man_node(p
, mt
, n
, m
);
933 print_man_nodelist(p
, mt
, n
->next
, m
);
938 print_man_foot(struct termp
*p
, const void *arg
)
941 const struct man_meta
*meta
;
943 meta
= (const struct man_meta
*)arg
;
945 term_fontrepl(p
, TERMFONT_NONE
);
948 strlcpy(buf
, meta
->rawdate
, DATESIZ
);
950 time2a(meta
->date
, buf
, DATESIZ
);
956 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
957 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, buf
);
960 /* term_strlen() can return zero. */
961 if (p
->rmargin
== p
->maxrmargin
)
965 term_word(p
, meta
->source
);
970 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
971 p
->offset
= p
->rmargin
;
972 p
->rmargin
= p
->maxrmargin
;
973 p
->flags
&= ~TERMP_NOBREAK
;
981 print_man_head(struct termp
*p
, const void *arg
)
983 char buf
[BUFSIZ
], title
[BUFSIZ
];
984 size_t buflen
, titlen
;
985 const struct man_meta
*m
;
987 m
= (const struct man_meta
*)arg
;
990 * Note that old groff would spit out some spaces before the
991 * header. We discontinue this strange behaviour, but at one
992 * point we did so here.
995 p
->rmargin
= p
->maxrmargin
;
998 buf
[0] = title
[0] = '\0';
1001 strlcpy(buf
, m
->vol
, BUFSIZ
);
1002 buflen
= term_strlen(p
, buf
);
1004 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
1005 titlen
= term_strlen(p
, title
);
1008 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
1010 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
1011 p
->maxrmargin
- buflen
;
1012 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
1014 term_word(p
, title
);
1017 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1018 p
->offset
= p
->rmargin
;
1019 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
1020 p
->maxrmargin
- titlen
: p
->maxrmargin
;
1025 p
->flags
&= ~TERMP_NOBREAK
;
1026 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
1027 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1028 p
->offset
= p
->rmargin
;
1029 p
->rmargin
= p
->maxrmargin
;
1030 term_word(p
, title
);
1034 p
->rmargin
= p
->maxrmargin
;
1036 p
->flags
&= ~TERMP_NOSPACE
;
1039 * Groff likes to have some leading spaces before content. Well
1040 * that's fine by me.