]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.126 2011/12/04 23:10:52 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>
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_alternate(DECL_ARGS
);
72 static int pre_B(DECL_ARGS
);
73 static int pre_HP(DECL_ARGS
);
74 static int pre_I(DECL_ARGS
);
75 static int pre_IP(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_ign(DECL_ARGS
);
82 static int pre_in(DECL_ARGS
);
83 static int pre_literal(DECL_ARGS
);
84 static int pre_sp(DECL_ARGS
);
85 static int pre_ft(DECL_ARGS
);
87 static void post_IP(DECL_ARGS
);
88 static void post_HP(DECL_ARGS
);
89 static void post_RS(DECL_ARGS
);
90 static void post_SH(DECL_ARGS
);
91 static void post_SS(DECL_ARGS
);
92 static void post_TP(DECL_ARGS
);
94 static const struct termact termacts
[MAN_MAX
] = {
95 { pre_sp
, NULL
, MAN_NOTEXT
}, /* br */
96 { NULL
, NULL
, 0 }, /* TH */
97 { pre_SH
, post_SH
, 0 }, /* SH */
98 { pre_SS
, post_SS
, 0 }, /* SS */
99 { pre_TP
, post_TP
, 0 }, /* TP */
100 { pre_PP
, NULL
, 0 }, /* LP */
101 { pre_PP
, NULL
, 0 }, /* PP */
102 { pre_PP
, NULL
, 0 }, /* P */
103 { pre_IP
, post_IP
, 0 }, /* IP */
104 { pre_HP
, post_HP
, 0 }, /* HP */
105 { NULL
, NULL
, 0 }, /* SM */
106 { pre_B
, NULL
, 0 }, /* SB */
107 { pre_alternate
, NULL
, 0 }, /* BI */
108 { pre_alternate
, NULL
, 0 }, /* IB */
109 { pre_alternate
, NULL
, 0 }, /* BR */
110 { pre_alternate
, NULL
, 0 }, /* RB */
111 { NULL
, NULL
, 0 }, /* R */
112 { pre_B
, NULL
, 0 }, /* B */
113 { pre_I
, NULL
, 0 }, /* I */
114 { pre_alternate
, NULL
, 0 }, /* IR */
115 { pre_alternate
, NULL
, 0 }, /* RI */
116 { pre_ign
, NULL
, MAN_NOTEXT
}, /* na */
117 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
118 { pre_literal
, NULL
, 0 }, /* nf */
119 { pre_literal
, NULL
, 0 }, /* fi */
120 { NULL
, NULL
, 0 }, /* RE */
121 { pre_RS
, post_RS
, 0 }, /* RS */
122 { pre_ign
, NULL
, 0 }, /* DT */
123 { pre_ign
, NULL
, 0 }, /* UC */
124 { pre_ign
, NULL
, 0 }, /* PD */
125 { pre_ign
, NULL
, 0 }, /* AT */
126 { pre_in
, NULL
, MAN_NOTEXT
}, /* in */
127 { pre_ft
, NULL
, MAN_NOTEXT
}, /* ft */
133 terminal_man(void *arg
, const struct man
*man
)
136 const struct man_node
*n
;
137 const struct man_meta
*m
;
140 p
= (struct termp
*)arg
;
142 if (0 == p
->defindent
)
146 p
->maxrmargin
= p
->defrmargin
;
147 p
->tabwidth
= term_len(p
, 5);
149 if (NULL
== p
->symtab
)
150 p
->symtab
= mchars_alloc();
155 term_begin(p
, print_man_head
, print_man_foot
, m
);
156 p
->flags
|= TERMP_NOSPACE
;
158 memset(&mt
, 0, sizeof(struct mtermp
));
160 mt
.lmargin
[mt
.lmargincur
] = term_len(p
, p
->defindent
);
161 mt
.offset
= term_len(p
, p
->defindent
);
164 print_man_nodelist(p
, &mt
, n
->child
, m
);
171 a2height(const struct termp
*p
, const char *cp
)
175 if ( ! a2roffsu(cp
, &su
, SCALE_VS
))
176 SCALE_VS_INIT(&su
, atoi(cp
));
178 return(term_vspan(p
, &su
));
183 a2width(const struct termp
*p
, const char *cp
)
187 if ( ! a2roffsu(cp
, &su
, SCALE_BU
))
190 return((int)term_hspan(p
, &su
));
194 * Printing leading vertical space before a block.
195 * This is used for the paragraph macros.
196 * The rules are pretty simple, since there's very little nesting going
197 * on here. Basically, if we're the first within another block (SS/SH),
198 * then don't emit vertical space. If we are (RS), then do. If not the
202 print_bvspace(struct termp
*p
, const struct man_node
*n
)
207 if (n
->body
&& n
->body
->child
)
208 if (MAN_TBL
== n
->body
->child
->type
)
211 if (MAN_ROOT
== n
->parent
->type
|| MAN_RS
!= n
->parent
->tok
)
232 term_fontrepl(p
, TERMFONT_UNDER
);
239 pre_literal(DECL_ARGS
)
244 if (MAN_nf
== n
->tok
)
245 mt
->fl
|= MANT_LITERAL
;
247 mt
->fl
&= ~MANT_LITERAL
;
250 * Unlike .IP and .TP, .HP does not have a HEAD.
251 * So in case a second call to term_flushln() is needed,
252 * indentation has to be set up explicitly.
254 if (MAN_HP
== n
->parent
->tok
&& p
->rmargin
< p
->maxrmargin
) {
255 p
->offset
= p
->rmargin
;
256 p
->rmargin
= p
->maxrmargin
;
257 p
->flags
&= ~(TERMP_NOBREAK
| TERMP_TWOSPACE
);
258 p
->flags
|= TERMP_NOSPACE
;
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
;
418 if ((NULL
== n
->prev
&& n
->parent
)) {
419 if (MAN_SS
== n
->parent
->tok
)
421 if (MAN_SH
== n
->parent
->tok
)
430 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
436 for (i
= 0; i
< len
; i
++)
449 const struct man_node
*nn
;
456 p
->flags
|= TERMP_NOBREAK
;
457 p
->flags
|= TERMP_TWOSPACE
;
463 len
= mt
->lmargin
[mt
->lmargincur
];
466 /* Calculate offset. */
468 if (NULL
!= (nn
= n
->parent
->head
->child
))
469 if ((ival
= a2width(p
, nn
->string
)) >= 0)
472 one
= term_len(p
, 1);
476 p
->offset
= mt
->offset
;
477 p
->rmargin
= mt
->offset
+ len
;
480 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
497 p
->flags
&= ~TERMP_NOBREAK
;
498 p
->flags
&= ~TERMP_TWOSPACE
;
499 p
->offset
= mt
->offset
;
500 p
->rmargin
= p
->maxrmargin
;
515 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, p
->defindent
);
519 p
->offset
= mt
->offset
;
523 return(MAN_HEAD
!= n
->type
);
531 const struct man_node
*nn
;
537 p
->flags
|= TERMP_NOSPACE
;
540 p
->flags
|= TERMP_NOBREAK
;
549 len
= mt
->lmargin
[mt
->lmargincur
];
552 /* Calculate the offset from the optional second argument. */
553 if (NULL
!= (nn
= n
->parent
->head
->child
))
554 if (NULL
!= (nn
= nn
->next
))
555 if ((ival
= a2width(p
, nn
->string
)) >= 0)
560 /* Handle zero-width lengths. */
562 len
= term_len(p
, 1);
564 p
->offset
= mt
->offset
;
565 p
->rmargin
= mt
->offset
+ len
;
569 /* Set the saved left-margin. */
570 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
572 savelit
= MANT_LITERAL
& mt
->fl
;
573 mt
->fl
&= ~MANT_LITERAL
;
576 print_man_node(p
, mt
, n
->child
, m
);
579 mt
->fl
|= MANT_LITERAL
;
583 p
->offset
= mt
->offset
+ len
;
584 p
->rmargin
= p
->maxrmargin
;
602 p
->flags
&= ~TERMP_NOBREAK
;
603 p
->rmargin
= p
->maxrmargin
;
618 const struct man_node
*nn
;
624 p
->flags
|= TERMP_NOBREAK
;
627 p
->flags
|= TERMP_NOSPACE
;
636 len
= (size_t)mt
->lmargin
[mt
->lmargincur
];
639 /* Calculate offset. */
641 if (NULL
!= (nn
= n
->parent
->head
->child
))
642 if (nn
->string
&& nn
->parent
->line
== nn
->line
)
643 if ((ival
= a2width(p
, nn
->string
)) >= 0)
648 /* Handle zero-length properly. */
650 len
= term_len(p
, 1);
652 p
->offset
= mt
->offset
;
653 p
->rmargin
= mt
->offset
+ len
;
655 savelit
= MANT_LITERAL
& mt
->fl
;
656 mt
->fl
&= ~MANT_LITERAL
;
658 /* Don't print same-line elements. */
659 for (nn
= n
->child
; nn
; nn
= nn
->next
)
660 if (nn
->line
> n
->line
)
661 print_man_node(p
, mt
, nn
, m
);
664 mt
->fl
|= MANT_LITERAL
;
666 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
670 p
->offset
= mt
->offset
+ len
;
671 p
->rmargin
= p
->maxrmargin
;
689 p
->flags
&= ~TERMP_NOBREAK
;
690 p
->flags
&= ~TERMP_TWOSPACE
;
691 p
->rmargin
= p
->maxrmargin
;
709 mt
->fl
&= ~MANT_LITERAL
;
710 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, p
->defindent
);
711 mt
->offset
= term_len(p
, p
->defindent
);
712 /* If following a prior empty `SS', no vspace. */
713 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
714 if (NULL
== n
->prev
->body
->child
)
721 term_fontrepl(p
, TERMFONT_BOLD
);
722 p
->offset
= term_len(p
, p
->defindent
/2);
725 p
->offset
= mt
->offset
;
760 mt
->fl
&= ~MANT_LITERAL
;
761 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, p
->defindent
);
762 mt
->offset
= term_len(p
, p
->defindent
);
763 /* If following a prior empty `SH', no vspace. */
764 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
765 if (NULL
== n
->prev
->body
->child
)
767 /* If the first macro, no vspae. */
773 term_fontrepl(p
, TERMFONT_BOLD
);
777 p
->offset
= mt
->offset
;
821 sz
= term_len(p
, p
->defindent
);
823 if (NULL
!= (n
= n
->parent
->head
->child
))
824 if ((ival
= a2width(p
, n
->string
)) >= 0)
828 p
->rmargin
= p
->maxrmargin
;
829 p
->offset
= mt
->offset
< p
->rmargin
? mt
->offset
: p
->rmargin
;
831 if (++mt
->lmarginsz
< MAXMARGINS
)
832 mt
->lmargincur
= mt
->lmarginsz
;
834 mt
->lmargin
[mt
->lmargincur
] = mt
->lmargin
[mt
->lmargincur
- 1];
855 sz
= term_len(p
, p
->defindent
);
857 if (NULL
!= (n
= n
->parent
->head
->child
))
858 if ((ival
= a2width(p
, n
->string
)) >= 0)
861 mt
->offset
= mt
->offset
< sz
? 0 : mt
->offset
- sz
;
862 p
->offset
= mt
->offset
;
864 if (--mt
->lmarginsz
< MAXMARGINS
)
865 mt
->lmargincur
= mt
->lmarginsz
;
869 print_man_node(DECL_ARGS
)
877 * If we have a blank line, output a vertical space.
878 * If we have a space as the first character, break
879 * before printing the line's data.
881 if ('\0' == *n
->string
) {
884 } else if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
887 term_word(p
, n
->string
);
890 * If we're in a literal context, make sure that words
891 * togehter on the same line stay together. This is a
892 * POST-printing call, so we check the NEXT word. Since
893 * -man doesn't have nested macros, we don't need to be
894 * more specific than this.
896 if (MANT_LITERAL
& mt
->fl
&& ! (TERMP_NOBREAK
& p
->flags
) &&
898 n
->next
->line
> n
->line
)) {
900 rmax
= p
->maxrmargin
;
901 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
902 p
->flags
|= TERMP_NOSPACE
;
905 p
->maxrmargin
= rmax
;
908 if (MAN_EOS
& n
->flags
)
909 p
->flags
|= TERMP_SENTENCE
;
916 * Tables are preceded by a newline. Then process a
917 * table line, which will cause line termination,
919 if (TBL_SPAN_FIRST
& n
->span
->flags
)
921 term_tbl(p
, n
->span
);
927 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
928 term_fontrepl(p
, TERMFONT_NONE
);
931 if (termacts
[n
->tok
].pre
)
932 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
935 print_man_nodelist(p
, mt
, n
->child
, m
);
937 if (termacts
[n
->tok
].post
)
938 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
939 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
940 term_fontrepl(p
, TERMFONT_NONE
);
942 if (MAN_EOS
& n
->flags
)
943 p
->flags
|= TERMP_SENTENCE
;
948 print_man_nodelist(DECL_ARGS
)
951 print_man_node(p
, mt
, n
, m
);
954 print_man_nodelist(p
, mt
, n
->next
, m
);
959 print_man_foot(struct termp
*p
, const void *arg
)
963 const struct man_meta
*meta
;
965 meta
= (const struct man_meta
*)arg
;
970 term_fontrepl(p
, TERMFONT_NONE
);
975 * Temporary, undocumented option to imitate mdoc(7) output.
976 * In the bottom right corner, use the source instead of
980 if ( ! p
->mdocstyle
) {
983 snprintf(title
, BUFSIZ
, "%s(%s)", meta
->title
, meta
->msec
);
984 } else if (meta
->source
) {
985 strlcpy(title
, meta
->source
, BUFSIZ
);
989 datelen
= term_strlen(p
, meta
->date
);
991 /* Bottom left corner: manual source. */
993 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
995 p
->rmargin
= (p
->maxrmargin
- datelen
+ term_len(p
, 1)) / 2;
998 term_word(p
, meta
->source
);
1001 /* At the bottom in the middle: manual date. */
1003 p
->flags
|= TERMP_NOSPACE
;
1004 p
->offset
= p
->rmargin
;
1005 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, title
);
1006 if (p
->offset
+ datelen
>= p
->rmargin
)
1007 p
->rmargin
= p
->offset
+ datelen
;
1009 term_word(p
, meta
->date
);
1012 /* Bottom right corner: manual title and section. */
1014 p
->flags
&= ~TERMP_NOBREAK
;
1015 p
->flags
|= TERMP_NOSPACE
;
1016 p
->offset
= p
->rmargin
;
1017 p
->rmargin
= p
->maxrmargin
;
1019 term_word(p
, title
);
1025 print_man_head(struct termp
*p
, const void *arg
)
1027 char buf
[BUFSIZ
], title
[BUFSIZ
];
1028 size_t buflen
, titlen
;
1029 const struct man_meta
*m
;
1031 m
= (const struct man_meta
*)arg
;
1036 strlcpy(buf
, m
->vol
, BUFSIZ
);
1039 buflen
= term_strlen(p
, buf
);
1041 /* Top left corner: manual title and section. */
1043 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
1044 titlen
= term_strlen(p
, title
);
1046 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
1048 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
1050 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
1051 p
->maxrmargin
- buflen
;
1053 term_word(p
, title
);
1056 /* At the top in the middle: manual volume. */
1058 p
->flags
|= TERMP_NOSPACE
;
1059 p
->offset
= p
->rmargin
;
1060 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
1061 p
->maxrmargin
- titlen
: p
->maxrmargin
;
1066 /* Top right corner: title and section, again. */
1068 p
->flags
&= ~TERMP_NOBREAK
;
1069 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
1070 p
->flags
|= TERMP_NOSPACE
;
1071 p
->offset
= p
->rmargin
;
1072 p
->rmargin
= p
->maxrmargin
;
1073 term_word(p
, title
);
1077 p
->flags
&= ~TERMP_NOSPACE
;
1079 p
->rmargin
= p
->maxrmargin
;
1082 * Groff prints three blank lines before the content.
1083 * Do the same, except in the temporary, undocumented
1084 * mode imitating mdoc(7) output.
1088 if ( ! p
->mdocstyle
) {