]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
14e13246969d168211fa5698756a3483ff199bbc
1 /* $Id: man_term.c,v 1.131 2012/07/13 14:19:49 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012 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>
36 #define MAXMARGINS 64 /* maximum number of indented scopes */
38 /* FIXME: have PD set the default vspace width. */
42 #define MANT_LITERAL (1 << 0)
43 size_t lmargin
[MAXMARGINS
]; /* margins (incl. visible page) */
44 int lmargincur
; /* index of current margin */
45 int lmarginsz
; /* actual number of nested margins */
46 size_t offset
; /* default offset to visible page */
49 #define DECL_ARGS struct termp *p, \
51 const struct man_node *n, \
52 const struct man_meta *m
55 int (*pre
)(DECL_ARGS
);
56 void (*post
)(DECL_ARGS
);
58 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
61 static int a2width(const struct termp
*, const char *);
62 static size_t a2height(const struct termp
*, const char *);
64 static void print_man_nodelist(DECL_ARGS
);
65 static void print_man_node(DECL_ARGS
);
66 static void print_man_head(struct termp
*, const void *);
67 static void print_man_foot(struct termp
*, const void *);
68 static void print_bvspace(struct termp
*,
69 const struct man_node
*);
71 static int pre_B(DECL_ARGS
);
72 static int pre_HP(DECL_ARGS
);
73 static int pre_I(DECL_ARGS
);
74 static int pre_IP(DECL_ARGS
);
75 static int pre_OP(DECL_ARGS
);
76 static int pre_PP(DECL_ARGS
);
77 static int pre_RS(DECL_ARGS
);
78 static int pre_SH(DECL_ARGS
);
79 static int pre_SS(DECL_ARGS
);
80 static int pre_TP(DECL_ARGS
);
81 static int pre_alternate(DECL_ARGS
);
82 static int pre_ft(DECL_ARGS
);
83 static int pre_ign(DECL_ARGS
);
84 static int pre_in(DECL_ARGS
);
85 static int pre_literal(DECL_ARGS
);
86 static int pre_sp(DECL_ARGS
);
88 static void post_IP(DECL_ARGS
);
89 static void post_HP(DECL_ARGS
);
90 static void post_RS(DECL_ARGS
);
91 static void post_SH(DECL_ARGS
);
92 static void post_SS(DECL_ARGS
);
93 static void post_TP(DECL_ARGS
);
95 static const struct termact termacts
[MAN_MAX
] = {
96 { pre_sp
, NULL
, MAN_NOTEXT
}, /* br */
97 { NULL
, NULL
, 0 }, /* TH */
98 { pre_SH
, post_SH
, 0 }, /* SH */
99 { pre_SS
, post_SS
, 0 }, /* SS */
100 { pre_TP
, post_TP
, 0 }, /* TP */
101 { pre_PP
, NULL
, 0 }, /* LP */
102 { pre_PP
, NULL
, 0 }, /* PP */
103 { pre_PP
, NULL
, 0 }, /* P */
104 { pre_IP
, post_IP
, 0 }, /* IP */
105 { pre_HP
, post_HP
, 0 }, /* HP */
106 { NULL
, NULL
, 0 }, /* SM */
107 { pre_B
, NULL
, 0 }, /* SB */
108 { pre_alternate
, NULL
, 0 }, /* BI */
109 { pre_alternate
, NULL
, 0 }, /* IB */
110 { pre_alternate
, NULL
, 0 }, /* BR */
111 { pre_alternate
, NULL
, 0 }, /* RB */
112 { NULL
, NULL
, 0 }, /* R */
113 { pre_B
, NULL
, 0 }, /* B */
114 { pre_I
, NULL
, 0 }, /* I */
115 { pre_alternate
, NULL
, 0 }, /* IR */
116 { pre_alternate
, NULL
, 0 }, /* RI */
117 { pre_ign
, NULL
, MAN_NOTEXT
}, /* na */
118 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
119 { pre_literal
, NULL
, 0 }, /* nf */
120 { pre_literal
, NULL
, 0 }, /* fi */
121 { NULL
, NULL
, 0 }, /* RE */
122 { pre_RS
, post_RS
, 0 }, /* RS */
123 { pre_ign
, NULL
, 0 }, /* DT */
124 { pre_ign
, NULL
, 0 }, /* UC */
125 { pre_ign
, NULL
, 0 }, /* PD */
126 { pre_ign
, NULL
, 0 }, /* AT */
127 { pre_in
, NULL
, MAN_NOTEXT
}, /* in */
128 { pre_ft
, NULL
, MAN_NOTEXT
}, /* ft */
129 { pre_OP
, NULL
, 0 }, /* OP */
130 { pre_literal
, NULL
, 0 }, /* EX */
131 { pre_literal
, NULL
, 0 }, /* EE */
137 terminal_man(void *arg
, const struct man
*man
)
140 const struct man_node
*n
;
141 const struct man_meta
*m
;
144 p
= (struct termp
*)arg
;
146 if (0 == p
->defindent
)
150 p
->maxrmargin
= p
->defrmargin
;
151 p
->tabwidth
= term_len(p
, 5);
153 if (NULL
== p
->symtab
)
154 p
->symtab
= mchars_alloc();
159 term_begin(p
, print_man_head
, print_man_foot
, m
);
160 p
->flags
|= TERMP_NOSPACE
;
162 memset(&mt
, 0, sizeof(struct mtermp
));
164 mt
.lmargin
[mt
.lmargincur
] = term_len(p
, p
->defindent
);
165 mt
.offset
= term_len(p
, p
->defindent
);
168 print_man_nodelist(p
, &mt
, n
->child
, m
);
175 a2height(const struct termp
*p
, const char *cp
)
179 if ( ! a2roffsu(cp
, &su
, SCALE_VS
))
180 SCALE_VS_INIT(&su
, atoi(cp
));
182 return(term_vspan(p
, &su
));
187 a2width(const struct termp
*p
, const char *cp
)
191 if ( ! a2roffsu(cp
, &su
, SCALE_BU
))
194 return((int)term_hspan(p
, &su
));
198 * Printing leading vertical space before a block.
199 * This is used for the paragraph macros.
200 * The rules are pretty simple, since there's very little nesting going
201 * on here. Basically, if we're the first within another block (SS/SH),
202 * then don't emit vertical space. If we are (RS), then do. If not the
206 print_bvspace(struct termp
*p
, const struct man_node
*n
)
211 if (n
->body
&& n
->body
->child
)
212 if (MAN_TBL
== n
->body
->child
->type
)
215 if (MAN_ROOT
== n
->parent
->type
|| MAN_RS
!= n
->parent
->tok
)
236 term_fontrepl(p
, TERMFONT_UNDER
);
243 pre_literal(DECL_ARGS
)
248 if (MAN_nf
== n
->tok
|| MAN_EX
== n
->tok
)
249 mt
->fl
|= MANT_LITERAL
;
251 mt
->fl
&= ~MANT_LITERAL
;
254 * Unlike .IP and .TP, .HP does not have a HEAD.
255 * So in case a second call to term_flushln() is needed,
256 * indentation has to be set up explicitly.
258 if (MAN_HP
== n
->parent
->tok
&& p
->rmargin
< p
->maxrmargin
) {
259 p
->offset
= p
->rmargin
;
260 p
->rmargin
= p
->maxrmargin
;
261 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_TWOSPACE
);
262 p
->flags
|= TERMP_NOSPACE
;
270 pre_alternate(DECL_ARGS
)
272 enum termfont font
[2];
273 const struct man_node
*nn
;
278 font
[0] = TERMFONT_NONE
;
279 font
[1] = TERMFONT_BOLD
;
282 font
[0] = TERMFONT_NONE
;
283 font
[1] = TERMFONT_UNDER
;
286 font
[0] = TERMFONT_BOLD
;
287 font
[1] = TERMFONT_NONE
;
290 font
[0] = TERMFONT_BOLD
;
291 font
[1] = TERMFONT_UNDER
;
294 font
[0] = TERMFONT_UNDER
;
295 font
[1] = TERMFONT_NONE
;
298 font
[0] = TERMFONT_UNDER
;
299 font
[1] = TERMFONT_BOLD
;
305 savelit
= MANT_LITERAL
& mt
->fl
;
306 mt
->fl
&= ~MANT_LITERAL
;
308 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
= 1 - i
) {
309 term_fontrepl(p
, font
[i
]);
310 if (savelit
&& NULL
== nn
->next
)
311 mt
->fl
|= MANT_LITERAL
;
312 print_man_node(p
, mt
, nn
, m
);
314 p
->flags
|= TERMP_NOSPACE
;
325 term_fontrepl(p
, TERMFONT_BOLD
);
335 p
->flags
|= TERMP_NOSPACE
;
337 if (NULL
!= (n
= n
->child
)) {
338 term_fontrepl(p
, TERMFONT_BOLD
);
339 term_word(p
, n
->string
);
341 if (NULL
!= n
&& NULL
!= n
->next
) {
342 term_fontrepl(p
, TERMFONT_UNDER
);
343 term_word(p
, n
->next
->string
);
346 term_fontrepl(p
, TERMFONT_NONE
);
347 p
->flags
|= TERMP_NOSPACE
;
358 if (NULL
== n
->child
) {
363 cp
= n
->child
->string
;
370 term_fontrepl(p
, TERMFONT_BOLD
);
375 term_fontrepl(p
, TERMFONT_UNDER
);
385 term_fontrepl(p
, TERMFONT_NONE
);
403 if (NULL
== n
->child
) {
404 p
->offset
= mt
->offset
;
408 cp
= n
->child
->string
;
418 if ((len
= a2width(p
, ++cp
)) < 0)
424 p
->offset
-= p
->offset
> v
? v
: p
->offset
;
430 /* Don't let this creep beyond the right margin. */
432 if (p
->offset
> p
->rmargin
)
433 p
->offset
= p
->rmargin
;
447 if ((NULL
== n
->prev
&& n
->parent
)) {
448 if (MAN_SS
== n
->parent
->tok
)
450 if (MAN_SH
== n
->parent
->tok
)
460 if (NULL
== n
->child
) {
464 s
= n
->child
->string
;
469 len
= a2height(p
, s
);
478 for (i
= 0; i
< len
; i
++)
491 const struct man_node
*nn
;
503 if ( ! (MANT_LITERAL
& mt
->fl
)) {
504 p
->flags
|= TERMP_NOBREAK
;
505 p
->flags
|= TERMP_TWOSPACE
;
508 len
= mt
->lmargin
[mt
->lmargincur
];
511 /* Calculate offset. */
513 if (NULL
!= (nn
= n
->parent
->head
->child
))
514 if ((ival
= a2width(p
, nn
->string
)) >= 0)
517 one
= term_len(p
, 1);
521 p
->offset
= mt
->offset
;
522 p
->rmargin
= mt
->offset
+ len
;
525 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
539 p
->flags
&= ~TERMP_NOBREAK
;
540 p
->flags
&= ~TERMP_TWOSPACE
;
541 p
->offset
= mt
->offset
;
542 p
->rmargin
= p
->maxrmargin
;
557 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, p
->defindent
);
561 p
->offset
= mt
->offset
;
565 return(MAN_HEAD
!= n
->type
);
573 const struct man_node
*nn
;
579 p
->flags
|= TERMP_NOSPACE
;
582 p
->flags
|= TERMP_NOBREAK
;
591 len
= mt
->lmargin
[mt
->lmargincur
];
594 /* Calculate the offset from the optional second argument. */
595 if (NULL
!= (nn
= n
->parent
->head
->child
))
596 if (NULL
!= (nn
= nn
->next
))
597 if ((ival
= a2width(p
, nn
->string
)) >= 0)
602 /* Handle zero-width lengths. */
604 len
= term_len(p
, 1);
606 p
->offset
= mt
->offset
;
607 p
->rmargin
= mt
->offset
+ len
;
611 /* Set the saved left-margin. */
612 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
614 savelit
= MANT_LITERAL
& mt
->fl
;
615 mt
->fl
&= ~MANT_LITERAL
;
618 print_man_node(p
, mt
, n
->child
, m
);
621 mt
->fl
|= MANT_LITERAL
;
625 p
->offset
= mt
->offset
+ len
;
626 p
->rmargin
= p
->maxrmargin
;
644 p
->flags
&= ~TERMP_NOBREAK
;
645 p
->rmargin
= p
->maxrmargin
;
660 const struct man_node
*nn
;
666 p
->flags
|= TERMP_NOBREAK
;
669 p
->flags
|= TERMP_NOSPACE
;
678 len
= (size_t)mt
->lmargin
[mt
->lmargincur
];
681 /* Calculate offset. */
683 if (NULL
!= (nn
= n
->parent
->head
->child
))
684 if (nn
->string
&& nn
->parent
->line
== nn
->line
)
685 if ((ival
= a2width(p
, nn
->string
)) >= 0)
690 /* Handle zero-length properly. */
692 len
= term_len(p
, 1);
694 p
->offset
= mt
->offset
;
695 p
->rmargin
= mt
->offset
+ len
;
697 savelit
= MANT_LITERAL
& mt
->fl
;
698 mt
->fl
&= ~MANT_LITERAL
;
700 /* Don't print same-line elements. */
701 for (nn
= n
->child
; nn
; nn
= nn
->next
)
702 if (nn
->line
> n
->line
)
703 print_man_node(p
, mt
, nn
, m
);
706 mt
->fl
|= MANT_LITERAL
;
708 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
712 p
->offset
= mt
->offset
+ len
;
713 p
->rmargin
= p
->maxrmargin
;
714 p
->flags
&= ~TERMP_NOBREAK
;
715 p
->flags
&= ~TERMP_TWOSPACE
;
750 mt
->fl
&= ~MANT_LITERAL
;
751 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, p
->defindent
);
752 mt
->offset
= term_len(p
, p
->defindent
);
753 /* If following a prior empty `SS', no vspace. */
754 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
755 if (NULL
== n
->prev
->body
->child
)
762 term_fontrepl(p
, TERMFONT_BOLD
);
763 p
->offset
= term_len(p
, p
->defindent
/2);
766 p
->offset
= mt
->offset
;
801 mt
->fl
&= ~MANT_LITERAL
;
802 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, p
->defindent
);
803 mt
->offset
= term_len(p
, p
->defindent
);
804 /* If following a prior empty `SH', no vspace. */
805 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
806 if (NULL
== n
->prev
->body
->child
)
808 /* If the first macro, no vspae. */
814 term_fontrepl(p
, TERMFONT_BOLD
);
818 p
->offset
= mt
->offset
;
862 sz
= term_len(p
, p
->defindent
);
864 if (NULL
!= (n
= n
->parent
->head
->child
))
865 if ((ival
= a2width(p
, n
->string
)) >= 0)
869 p
->rmargin
= p
->maxrmargin
;
870 p
->offset
= mt
->offset
< p
->rmargin
? mt
->offset
: p
->rmargin
;
872 if (++mt
->lmarginsz
< MAXMARGINS
)
873 mt
->lmargincur
= mt
->lmarginsz
;
875 mt
->lmargin
[mt
->lmargincur
] = mt
->lmargin
[mt
->lmargincur
- 1];
896 sz
= term_len(p
, p
->defindent
);
898 if (NULL
!= (n
= n
->parent
->head
->child
))
899 if ((ival
= a2width(p
, n
->string
)) >= 0)
902 mt
->offset
= mt
->offset
< sz
? 0 : mt
->offset
- sz
;
903 p
->offset
= mt
->offset
;
905 if (--mt
->lmarginsz
< MAXMARGINS
)
906 mt
->lmargincur
= mt
->lmarginsz
;
910 print_man_node(DECL_ARGS
)
918 * If we have a blank line, output a vertical space.
919 * If we have a space as the first character, break
920 * before printing the line's data.
922 if ('\0' == *n
->string
) {
925 } else if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
928 term_word(p
, n
->string
);
936 * Tables are preceded by a newline. Then process a
937 * table line, which will cause line termination,
939 if (TBL_SPAN_FIRST
& n
->span
->flags
)
941 term_tbl(p
, n
->span
);
947 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
948 term_fontrepl(p
, TERMFONT_NONE
);
951 if (termacts
[n
->tok
].pre
)
952 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
955 print_man_nodelist(p
, mt
, n
->child
, m
);
957 if (termacts
[n
->tok
].post
)
958 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
959 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
960 term_fontrepl(p
, TERMFONT_NONE
);
964 * If we're in a literal context, make sure that words
965 * together on the same line stay together. This is a
966 * POST-printing call, so we check the NEXT word. Since
967 * -man doesn't have nested macros, we don't need to be
968 * more specific than this.
970 if (MANT_LITERAL
& mt
->fl
&& ! (TERMP_NOBREAK
& p
->flags
) &&
971 NULL
!= n
->next
&& n
->next
->line
> n
->line
) {
973 rmax
= p
->maxrmargin
;
974 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
975 p
->flags
|= TERMP_NOSPACE
;
976 if (NULL
!= n
->string
&& '\0' != *n
->string
)
980 if (rm
< rmax
&& n
->parent
->tok
== MAN_HP
) {
985 p
->maxrmargin
= rmax
;
987 if (MAN_EOS
& n
->flags
)
988 p
->flags
|= TERMP_SENTENCE
;
993 print_man_nodelist(DECL_ARGS
)
996 print_man_node(p
, mt
, n
, m
);
999 print_man_nodelist(p
, mt
, n
->next
, m
);
1004 print_man_foot(struct termp
*p
, const void *arg
)
1008 const struct man_meta
*meta
;
1010 meta
= (const struct man_meta
*)arg
;
1011 assert(meta
->title
);
1015 term_fontrepl(p
, TERMFONT_NONE
);
1020 * Temporary, undocumented option to imitate mdoc(7) output.
1021 * In the bottom right corner, use the source instead of
1025 if ( ! p
->mdocstyle
) {
1028 snprintf(title
, BUFSIZ
, "%s(%s)", meta
->title
, meta
->msec
);
1029 } else if (meta
->source
) {
1030 strlcpy(title
, meta
->source
, BUFSIZ
);
1034 datelen
= term_strlen(p
, meta
->date
);
1036 /* Bottom left corner: manual source. */
1038 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
1040 p
->rmargin
= (p
->maxrmargin
- datelen
+ term_len(p
, 1)) / 2;
1043 term_word(p
, meta
->source
);
1046 /* At the bottom in the middle: manual date. */
1048 p
->flags
|= TERMP_NOSPACE
;
1049 p
->offset
= p
->rmargin
;
1050 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, title
);
1051 if (p
->offset
+ datelen
>= p
->rmargin
)
1052 p
->rmargin
= p
->offset
+ datelen
;
1054 term_word(p
, meta
->date
);
1057 /* Bottom right corner: manual title and section. */
1059 p
->flags
&= ~TERMP_NOBREAK
;
1060 p
->flags
|= TERMP_NOSPACE
;
1061 p
->offset
= p
->rmargin
;
1062 p
->rmargin
= p
->maxrmargin
;
1064 term_word(p
, title
);
1070 print_man_head(struct termp
*p
, const void *arg
)
1072 char buf
[BUFSIZ
], title
[BUFSIZ
];
1073 size_t buflen
, titlen
;
1074 const struct man_meta
*m
;
1076 m
= (const struct man_meta
*)arg
;
1081 strlcpy(buf
, m
->vol
, BUFSIZ
);
1084 buflen
= term_strlen(p
, buf
);
1086 /* Top left corner: manual title and section. */
1088 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
1089 titlen
= term_strlen(p
, title
);
1091 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
1093 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
1095 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
1096 p
->maxrmargin
- buflen
;
1098 term_word(p
, title
);
1101 /* At the top in the middle: manual volume. */
1103 p
->flags
|= TERMP_NOSPACE
;
1104 p
->offset
= p
->rmargin
;
1105 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
1106 p
->maxrmargin
- titlen
: p
->maxrmargin
;
1111 /* Top right corner: title and section, again. */
1113 p
->flags
&= ~TERMP_NOBREAK
;
1114 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
1115 p
->flags
|= TERMP_NOSPACE
;
1116 p
->offset
= p
->rmargin
;
1117 p
->rmargin
= p
->maxrmargin
;
1118 term_word(p
, title
);
1122 p
->flags
&= ~TERMP_NOSPACE
;
1124 p
->rmargin
= p
->maxrmargin
;
1127 * Groff prints three blank lines before the content.
1128 * Do the same, except in the temporary, undocumented
1129 * mode imitating mdoc(7) output.
1133 if ( ! p
->mdocstyle
) {