]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.116 2011/09/18 21:08:34 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 INDENT 7 /* fixed-width char full-indent */
37 #define HALFINDENT 3 /* fixed-width char half-indent */
38 #define MAXMARGINS 64 /* maximum number of indented scopes */
40 /* FIXME: have PD set the default vspace width. */
44 #define MANT_LITERAL (1 << 0)
45 size_t lmargin
[MAXMARGINS
]; /* margins (incl. visible page) */
46 int lmargincur
; /* index of current margin */
47 int lmarginsz
; /* actual number of nested margins */
48 size_t offset
; /* default offset to visible page */
51 #define DECL_ARGS struct termp *p, \
53 const struct man_node *n, \
54 const struct man_meta *m
57 int (*pre
)(DECL_ARGS
);
58 void (*post
)(DECL_ARGS
);
60 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
63 static int a2width(const struct termp
*, const char *);
64 static size_t a2height(const struct termp
*, const char *);
66 static void print_man_nodelist(DECL_ARGS
);
67 static void print_man_node(DECL_ARGS
);
68 static void print_man_head(struct termp
*, const void *);
69 static void print_man_foot(struct termp
*, const void *);
70 static void print_bvspace(struct termp
*,
71 const struct man_node
*);
73 static int pre_alternate(DECL_ARGS
);
74 static int pre_B(DECL_ARGS
);
75 static int pre_HP(DECL_ARGS
);
76 static int pre_I(DECL_ARGS
);
77 static int pre_IP(DECL_ARGS
);
78 static int pre_PP(DECL_ARGS
);
79 static int pre_RS(DECL_ARGS
);
80 static int pre_SH(DECL_ARGS
);
81 static int pre_SS(DECL_ARGS
);
82 static int pre_TP(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
);
87 static int pre_ft(DECL_ARGS
);
89 static void post_IP(DECL_ARGS
);
90 static void post_HP(DECL_ARGS
);
91 static void post_RS(DECL_ARGS
);
92 static void post_SH(DECL_ARGS
);
93 static void post_SS(DECL_ARGS
);
94 static void post_TP(DECL_ARGS
);
96 static const struct termact termacts
[MAN_MAX
] = {
97 { pre_sp
, NULL
, MAN_NOTEXT
}, /* br */
98 { NULL
, NULL
, 0 }, /* TH */
99 { pre_SH
, post_SH
, 0 }, /* SH */
100 { pre_SS
, post_SS
, 0 }, /* SS */
101 { pre_TP
, post_TP
, 0 }, /* TP */
102 { pre_PP
, NULL
, 0 }, /* LP */
103 { pre_PP
, NULL
, 0 }, /* PP */
104 { pre_PP
, NULL
, 0 }, /* P */
105 { pre_IP
, post_IP
, 0 }, /* IP */
106 { pre_HP
, post_HP
, 0 }, /* HP */
107 { NULL
, NULL
, 0 }, /* SM */
108 { pre_B
, NULL
, 0 }, /* SB */
109 { pre_alternate
, NULL
, 0 }, /* BI */
110 { pre_alternate
, NULL
, 0 }, /* IB */
111 { pre_alternate
, NULL
, 0 }, /* BR */
112 { pre_alternate
, NULL
, 0 }, /* RB */
113 { NULL
, NULL
, 0 }, /* R */
114 { pre_B
, NULL
, 0 }, /* B */
115 { pre_I
, NULL
, 0 }, /* I */
116 { pre_alternate
, NULL
, 0 }, /* IR */
117 { pre_alternate
, NULL
, 0 }, /* RI */
118 { pre_ign
, NULL
, MAN_NOTEXT
}, /* na */
119 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
120 { pre_literal
, NULL
, 0 }, /* nf */
121 { pre_literal
, NULL
, 0 }, /* fi */
122 { NULL
, NULL
, 0 }, /* RE */
123 { pre_RS
, post_RS
, 0 }, /* RS */
124 { pre_ign
, NULL
, 0 }, /* DT */
125 { pre_ign
, NULL
, 0 }, /* UC */
126 { pre_ign
, NULL
, 0 }, /* PD */
127 { pre_ign
, NULL
, 0 }, /* AT */
128 { pre_in
, NULL
, MAN_NOTEXT
}, /* in */
129 { pre_ft
, NULL
, MAN_NOTEXT
}, /* ft */
135 terminal_man(void *arg
, const struct man
*man
)
138 const struct man_node
*n
;
139 const struct man_meta
*m
;
142 p
= (struct termp
*)arg
;
145 p
->maxrmargin
= p
->defrmargin
;
146 p
->tabwidth
= term_len(p
, 5);
148 if (NULL
== p
->symtab
)
149 p
->symtab
= mchars_alloc();
154 term_begin(p
, print_man_head
, print_man_foot
, m
);
155 p
->flags
|= TERMP_NOSPACE
;
157 memset(&mt
, 0, sizeof(struct mtermp
));
159 mt
.lmargin
[mt
.lmargincur
] = term_len(p
, INDENT
);
160 mt
.offset
= term_len(p
, INDENT
);
163 print_man_nodelist(p
, &mt
, n
->child
, m
);
170 a2height(const struct termp
*p
, const char *cp
)
174 if ( ! a2roffsu(cp
, &su
, SCALE_VS
))
175 SCALE_VS_INIT(&su
, atoi(cp
));
177 return(term_vspan(p
, &su
));
182 a2width(const struct termp
*p
, const char *cp
)
186 if ( ! a2roffsu(cp
, &su
, SCALE_BU
))
189 return((int)term_hspan(p
, &su
));
193 * Printing leading vertical space before a block.
194 * This is used for the paragraph macros.
195 * The rules are pretty simple, since there's very little nesting going
196 * on here. Basically, if we're the first within another block (SS/SH),
197 * then don't emit vertical space. If we are (RS), then do. If not the
201 print_bvspace(struct termp
*p
, const struct man_node
*n
)
206 if (n
->body
&& n
->body
->child
)
207 if (MAN_TBL
== n
->body
->child
->type
)
210 if (MAN_ROOT
== n
->parent
->type
|| MAN_RS
!= n
->parent
->tok
)
231 term_fontrepl(p
, TERMFONT_UNDER
);
238 pre_literal(DECL_ARGS
)
243 if (MAN_nf
== n
->tok
)
244 mt
->fl
|= MANT_LITERAL
;
246 mt
->fl
&= ~MANT_LITERAL
;
253 pre_alternate(DECL_ARGS
)
255 enum termfont font
[2];
256 const struct man_node
*nn
;
261 font
[0] = TERMFONT_NONE
;
262 font
[1] = TERMFONT_BOLD
;
265 font
[0] = TERMFONT_NONE
;
266 font
[1] = TERMFONT_UNDER
;
269 font
[0] = TERMFONT_BOLD
;
270 font
[1] = TERMFONT_NONE
;
273 font
[0] = TERMFONT_BOLD
;
274 font
[1] = TERMFONT_UNDER
;
277 font
[0] = TERMFONT_UNDER
;
278 font
[1] = TERMFONT_NONE
;
281 font
[0] = TERMFONT_UNDER
;
282 font
[1] = TERMFONT_BOLD
;
288 savelit
= MANT_LITERAL
& mt
->fl
;
289 mt
->fl
&= ~MANT_LITERAL
;
291 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
= 1 - i
) {
292 term_fontrepl(p
, font
[i
]);
293 if (savelit
&& NULL
== nn
->next
)
294 mt
->fl
|= MANT_LITERAL
;
295 print_man_node(p
, mt
, nn
, m
);
297 p
->flags
|= TERMP_NOSPACE
;
308 term_fontrepl(p
, TERMFONT_BOLD
);
318 if (NULL
== n
->child
) {
323 cp
= n
->child
->string
;
330 term_fontrepl(p
, TERMFONT_BOLD
);
335 term_fontrepl(p
, TERMFONT_UNDER
);
345 term_fontrepl(p
, TERMFONT_NONE
);
363 if (NULL
== n
->child
) {
364 p
->offset
= mt
->offset
;
368 cp
= n
->child
->string
;
378 if ((len
= a2width(p
, ++cp
)) < 0)
384 p
->offset
-= p
->offset
> v
? v
: p
->offset
;
390 /* Don't let this creep beyond the right margin. */
392 if (p
->offset
> p
->rmargin
)
393 p
->offset
= p
->rmargin
;
405 if ((NULL
== n
->prev
&& n
->parent
)) {
406 if (MAN_SS
== n
->parent
->tok
)
408 if (MAN_SH
== n
->parent
->tok
)
417 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
423 for (i
= 0; i
< len
; i
++)
436 const struct man_node
*nn
;
443 p
->flags
|= TERMP_NOBREAK
;
444 p
->flags
|= TERMP_TWOSPACE
;
450 len
= mt
->lmargin
[mt
->lmargincur
];
453 /* Calculate offset. */
455 if (NULL
!= (nn
= n
->parent
->head
->child
))
456 if ((ival
= a2width(p
, nn
->string
)) >= 0)
460 len
= term_len(p
, 1);
462 p
->offset
= mt
->offset
;
463 p
->rmargin
= mt
->offset
+ len
;
466 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
483 p
->flags
&= ~TERMP_NOBREAK
;
484 p
->flags
&= ~TERMP_TWOSPACE
;
485 p
->offset
= mt
->offset
;
486 p
->rmargin
= p
->maxrmargin
;
501 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, INDENT
);
505 p
->offset
= mt
->offset
;
509 return(MAN_HEAD
!= n
->type
);
517 const struct man_node
*nn
;
523 p
->flags
|= TERMP_NOLPAD
;
524 p
->flags
|= TERMP_NOSPACE
;
527 p
->flags
|= TERMP_NOBREAK
;
536 len
= mt
->lmargin
[mt
->lmargincur
];
539 /* Calculate the offset from the optional second argument. */
540 if (NULL
!= (nn
= n
->parent
->head
->child
))
541 if (NULL
!= (nn
= nn
->next
))
542 if ((ival
= a2width(p
, nn
->string
)) >= 0)
547 /* Handle zero-width lengths. */
549 len
= term_len(p
, 1);
551 p
->offset
= mt
->offset
;
552 p
->rmargin
= mt
->offset
+ len
;
556 /* Set the saved left-margin. */
557 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
559 savelit
= MANT_LITERAL
& mt
->fl
;
560 mt
->fl
&= ~MANT_LITERAL
;
563 print_man_node(p
, mt
, n
->child
, m
);
566 mt
->fl
|= MANT_LITERAL
;
570 p
->offset
= mt
->offset
+ len
;
571 p
->rmargin
= p
->maxrmargin
;
589 p
->flags
&= ~TERMP_NOBREAK
;
590 p
->rmargin
= p
->maxrmargin
;
594 p
->flags
&= ~TERMP_NOLPAD
;
606 const struct man_node
*nn
;
612 p
->flags
|= TERMP_NOBREAK
;
615 p
->flags
|= TERMP_NOLPAD
;
616 p
->flags
|= TERMP_NOSPACE
;
625 len
= (size_t)mt
->lmargin
[mt
->lmargincur
];
628 /* Calculate offset. */
630 if (NULL
!= (nn
= n
->parent
->head
->child
))
631 if (nn
->parent
->line
== nn
->line
)
632 if ((ival
= a2width(p
, nn
->string
)) >= 0)
637 /* Handle zero-length properly. */
639 len
= term_len(p
, 1);
641 p
->offset
= mt
->offset
;
642 p
->rmargin
= mt
->offset
+ len
;
644 savelit
= MANT_LITERAL
& mt
->fl
;
645 mt
->fl
&= ~MANT_LITERAL
;
647 /* Don't print same-line elements. */
648 for (nn
= n
->child
; nn
; nn
= nn
->next
)
649 if (nn
->line
> n
->line
)
650 print_man_node(p
, mt
, nn
, m
);
653 mt
->fl
|= MANT_LITERAL
;
655 mt
->lmargin
[mt
->lmargincur
] = (size_t)ival
;
659 p
->offset
= mt
->offset
+ len
;
660 p
->rmargin
= p
->maxrmargin
;
678 p
->flags
&= ~TERMP_NOBREAK
;
679 p
->flags
&= ~TERMP_TWOSPACE
;
680 p
->rmargin
= p
->maxrmargin
;
684 p
->flags
&= ~TERMP_NOLPAD
;
699 mt
->fl
&= ~MANT_LITERAL
;
700 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, INDENT
);
701 mt
->offset
= term_len(p
, INDENT
);
702 /* If following a prior empty `SS', no vspace. */
703 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
704 if (NULL
== n
->prev
->body
->child
)
711 term_fontrepl(p
, TERMFONT_BOLD
);
712 p
->offset
= term_len(p
, HALFINDENT
);
715 p
->offset
= mt
->offset
;
750 mt
->fl
&= ~MANT_LITERAL
;
751 mt
->lmargin
[mt
->lmargincur
] = term_len(p
, INDENT
);
752 mt
->offset
= term_len(p
, INDENT
);
753 /* If following a prior empty `SH', no vspace. */
754 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
755 if (NULL
== n
->prev
->body
->child
)
757 /* If the first macro, no vspae. */
763 term_fontrepl(p
, TERMFONT_BOLD
);
767 p
->offset
= mt
->offset
;
811 sz
= term_len(p
, INDENT
);
813 if (NULL
!= (n
= n
->parent
->head
->child
))
814 if ((ival
= a2width(p
, n
->string
)) >= 0)
818 p
->offset
= mt
->offset
;
820 if (++mt
->lmarginsz
< MAXMARGINS
)
821 mt
->lmargincur
= mt
->lmarginsz
;
823 mt
->lmargin
[mt
->lmargincur
] = mt
->lmargin
[mt
->lmargincur
- 1];
844 sz
= term_len(p
, INDENT
);
846 if (NULL
!= (n
= n
->parent
->head
->child
))
847 if ((ival
= a2width(p
, n
->string
)) >= 0)
850 mt
->offset
= mt
->offset
< sz
? 0 : mt
->offset
- sz
;
851 p
->offset
= mt
->offset
;
853 if (--mt
->lmarginsz
< MAXMARGINS
)
854 mt
->lmargincur
= mt
->lmarginsz
;
858 print_man_node(DECL_ARGS
)
866 * If we have a blank line, output a vertical space.
867 * If we have a space as the first character, break
868 * before printing the line's data.
870 if ('\0' == *n
->string
) {
873 } else if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
876 term_word(p
, n
->string
);
879 * If we're in a literal context, make sure that words
880 * togehter on the same line stay together. This is a
881 * POST-printing call, so we check the NEXT word. Since
882 * -man doesn't have nested macros, we don't need to be
883 * more specific than this.
885 if (MANT_LITERAL
& mt
->fl
&&
887 n
->next
->line
> n
->line
)) {
889 rmax
= p
->maxrmargin
;
890 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
891 p
->flags
|= TERMP_NOSPACE
;
893 p
->flags
&= ~TERMP_NOLPAD
;
895 p
->maxrmargin
= rmax
;
898 if (MAN_EOS
& n
->flags
)
899 p
->flags
|= TERMP_SENTENCE
;
906 * Tables are preceded by a newline. Then process a
907 * table line, which will cause line termination,
909 if (TBL_SPAN_FIRST
& n
->span
->flags
)
911 term_tbl(p
, n
->span
);
917 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
918 term_fontrepl(p
, TERMFONT_NONE
);
921 if (termacts
[n
->tok
].pre
)
922 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
925 print_man_nodelist(p
, mt
, n
->child
, m
);
927 if (termacts
[n
->tok
].post
)
928 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
929 if ( ! (MAN_NOTEXT
& termacts
[n
->tok
].flags
))
930 term_fontrepl(p
, TERMFONT_NONE
);
932 if (MAN_EOS
& n
->flags
)
933 p
->flags
|= TERMP_SENTENCE
;
938 print_man_nodelist(DECL_ARGS
)
941 print_man_node(p
, mt
, n
, m
);
944 print_man_nodelist(p
, mt
, n
->next
, m
);
949 print_man_foot(struct termp
*p
, const void *arg
)
951 const struct man_meta
*meta
;
953 meta
= (const struct man_meta
*)arg
;
955 term_fontrepl(p
, TERMFONT_NONE
);
961 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
962 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, meta
->date
);
965 /* term_strlen() can return zero. */
966 if (p
->rmargin
== p
->maxrmargin
)
970 term_word(p
, meta
->source
);
975 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
976 p
->offset
= p
->rmargin
;
977 p
->rmargin
= p
->maxrmargin
;
978 p
->flags
&= ~TERMP_NOBREAK
;
980 term_word(p
, meta
->date
);
986 print_man_head(struct termp
*p
, const void *arg
)
988 char buf
[BUFSIZ
], title
[BUFSIZ
];
989 size_t buflen
, titlen
;
990 const struct man_meta
*m
;
992 m
= (const struct man_meta
*)arg
;
995 * Note that old groff would spit out some spaces before the
996 * header. We discontinue this strange behaviour, but at one
997 * point we did so here.
1000 p
->rmargin
= p
->maxrmargin
;
1003 buf
[0] = title
[0] = '\0';
1006 strlcpy(buf
, m
->vol
, BUFSIZ
);
1007 buflen
= term_strlen(p
, buf
);
1009 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
1010 titlen
= term_strlen(p
, title
);
1013 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
1015 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
1016 p
->maxrmargin
- buflen
;
1017 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
1019 term_word(p
, title
);
1022 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1023 p
->offset
= p
->rmargin
;
1024 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
1025 p
->maxrmargin
- titlen
: p
->maxrmargin
;
1030 p
->flags
&= ~TERMP_NOBREAK
;
1031 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
1032 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1033 p
->offset
= p
->rmargin
;
1034 p
->rmargin
= p
->maxrmargin
;
1035 term_word(p
, title
);
1039 p
->rmargin
= p
->maxrmargin
;
1041 p
->flags
&= ~TERMP_NOSPACE
;
1044 * Groff likes to have some leading spaces before content. Well
1045 * that's fine by me.