]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.108 2011/04/30 22:14:42 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>
39 /* FIXME: have PD set the default vspace width. */
43 #define MANT_LITERAL (1 << 0)
45 * Default amount to indent the left margin after leading text
46 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
47 * indent). This needs to be saved because `HP' and so on, if
48 * not having a specified value, must default.
50 * Note that this is the indentation AFTER the left offset, so
51 * the total offset is usually offset + lmargin.
55 * The default offset, i.e., the amount between any text and the
61 #define DECL_ARGS struct termp *p, \
63 const struct man_node *n, \
64 const struct man_meta *m
67 int (*pre
)(DECL_ARGS
);
68 void (*post
)(DECL_ARGS
);
70 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
73 static int a2width(const struct termp
*, const char *);
74 static size_t a2height(const struct termp
*, const char *);
76 static void print_man_nodelist(DECL_ARGS
);
77 static void print_man_node(DECL_ARGS
);
78 static void print_man_head(struct termp
*, const void *);
79 static void print_man_foot(struct termp
*, const void *);
80 static void print_bvspace(struct termp
*,
81 const struct man_node
*);
83 static int pre_alternate(DECL_ARGS
);
84 static int pre_B(DECL_ARGS
);
85 static int pre_HP(DECL_ARGS
);
86 static int pre_I(DECL_ARGS
);
87 static int pre_IP(DECL_ARGS
);
88 static int pre_PP(DECL_ARGS
);
89 static int pre_RS(DECL_ARGS
);
90 static int pre_SH(DECL_ARGS
);
91 static int pre_SS(DECL_ARGS
);
92 static int pre_TP(DECL_ARGS
);
93 static int pre_ign(DECL_ARGS
);
94 static int pre_in(DECL_ARGS
);
95 static int pre_literal(DECL_ARGS
);
96 static int pre_sp(DECL_ARGS
);
97 static int pre_ft(DECL_ARGS
);
99 static void post_IP(DECL_ARGS
);
100 static void post_HP(DECL_ARGS
);
101 static void post_RS(DECL_ARGS
);
102 static void post_SH(DECL_ARGS
);
103 static void post_SS(DECL_ARGS
);
104 static void post_TP(DECL_ARGS
);
106 static const struct termact termacts
[MAN_MAX
] = {
107 { pre_sp
, NULL
, MAN_NOTEXT
}, /* br */
108 { NULL
, NULL
, 0 }, /* TH */
109 { pre_SH
, post_SH
, 0 }, /* SH */
110 { pre_SS
, post_SS
, 0 }, /* SS */
111 { pre_TP
, post_TP
, 0 }, /* TP */
112 { pre_PP
, NULL
, 0 }, /* LP */
113 { pre_PP
, NULL
, 0 }, /* PP */
114 { pre_PP
, NULL
, 0 }, /* P */
115 { pre_IP
, post_IP
, 0 }, /* IP */
116 { pre_HP
, post_HP
, 0 }, /* HP */
117 { NULL
, NULL
, 0 }, /* SM */
118 { pre_B
, NULL
, 0 }, /* SB */
119 { pre_alternate
, NULL
, 0 }, /* BI */
120 { pre_alternate
, NULL
, 0 }, /* IB */
121 { pre_alternate
, NULL
, 0 }, /* BR */
122 { pre_alternate
, NULL
, 0 }, /* RB */
123 { NULL
, NULL
, 0 }, /* R */
124 { pre_B
, NULL
, 0 }, /* B */
125 { pre_I
, NULL
, 0 }, /* I */
126 { pre_alternate
, NULL
, 0 }, /* IR */
127 { pre_alternate
, NULL
, 0 }, /* RI */
128 { pre_ign
, NULL
, MAN_NOTEXT
}, /* na */
129 { pre_sp
, NULL
, MAN_NOTEXT
}, /* sp */
130 { pre_literal
, NULL
, 0 }, /* nf */
131 { pre_literal
, NULL
, 0 }, /* fi */
132 { NULL
, NULL
, 0 }, /* RE */
133 { pre_RS
, post_RS
, 0 }, /* RS */
134 { pre_ign
, NULL
, 0 }, /* DT */
135 { pre_ign
, NULL
, 0 }, /* UC */
136 { pre_ign
, NULL
, 0 }, /* PD */
137 { pre_ign
, NULL
, 0 }, /* AT */
138 { pre_in
, NULL
, MAN_NOTEXT
}, /* in */
139 { pre_ft
, NULL
, MAN_NOTEXT
}, /* ft */
145 terminal_man(void *arg
, const struct man
*man
)
148 const struct man_node
*n
;
149 const struct man_meta
*m
;
152 p
= (struct termp
*)arg
;
155 p
->maxrmargin
= p
->defrmargin
;
156 p
->tabwidth
= term_len(p
, 5);
158 if (NULL
== p
->symtab
)
160 case (TERMENC_ASCII
):
161 p
->symtab
= mchars_alloc();
171 term_begin(p
, print_man_head
, print_man_foot
, m
);
172 p
->flags
|= TERMP_NOSPACE
;
175 mt
.lmargin
= term_len(p
, INDENT
);
176 mt
.offset
= term_len(p
, INDENT
);
179 print_man_nodelist(p
, &mt
, n
->child
, m
);
186 a2height(const struct termp
*p
, const char *cp
)
190 if ( ! a2roffsu(cp
, &su
, SCALE_VS
))
191 SCALE_VS_INIT(&su
, term_strlen(p
, cp
));
193 return(term_vspan(p
, &su
));
198 a2width(const struct termp
*p
, const char *cp
)
202 if ( ! a2roffsu(cp
, &su
, SCALE_BU
))
205 return((int)term_hspan(p
, &su
));
210 print_bvspace(struct termp
*p
, const struct man_node
*n
)
214 if (n
->body
&& n
->body
->child
&& MAN_TBL
== n
->body
->child
->type
)
220 if (MAN_SS
== n
->prev
->tok
)
222 if (MAN_SH
== n
->prev
->tok
)
243 term_fontrepl(p
, TERMFONT_UNDER
);
250 pre_literal(DECL_ARGS
)
255 if (MAN_nf
== n
->tok
)
256 mt
->fl
|= MANT_LITERAL
;
258 mt
->fl
&= ~MANT_LITERAL
;
265 pre_alternate(DECL_ARGS
)
267 enum termfont font
[2];
268 const struct man_node
*nn
;
273 font
[0] = TERMFONT_NONE
;
274 font
[1] = TERMFONT_BOLD
;
277 font
[0] = TERMFONT_NONE
;
278 font
[1] = TERMFONT_UNDER
;
281 font
[0] = TERMFONT_BOLD
;
282 font
[1] = TERMFONT_NONE
;
285 font
[0] = TERMFONT_BOLD
;
286 font
[1] = TERMFONT_UNDER
;
289 font
[0] = TERMFONT_UNDER
;
290 font
[1] = TERMFONT_NONE
;
293 font
[0] = TERMFONT_UNDER
;
294 font
[1] = TERMFONT_BOLD
;
300 savelit
= MANT_LITERAL
& mt
->fl
;
301 mt
->fl
&= ~MANT_LITERAL
;
303 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
= 1 - i
) {
304 term_fontrepl(p
, font
[i
]);
305 if (savelit
&& NULL
== nn
->next
)
306 mt
->fl
|= MANT_LITERAL
;
307 print_man_node(p
, mt
, nn
, m
);
309 p
->flags
|= TERMP_NOSPACE
;
320 term_fontrepl(p
, TERMFONT_BOLD
);
330 if (NULL
== n
->child
) {
335 cp
= n
->child
->string
;
342 term_fontrepl(p
, TERMFONT_BOLD
);
347 term_fontrepl(p
, TERMFONT_UNDER
);
357 term_fontrepl(p
, TERMFONT_NONE
);
375 if (NULL
== n
->child
) {
376 p
->offset
= mt
->offset
;
380 cp
= n
->child
->string
;
390 if ((len
= a2width(p
, ++cp
)) < 0)
396 p
->offset
-= p
->offset
> v
? v
: p
->offset
;
402 /* Don't let this creep beyond the right margin. */
404 if (p
->offset
> p
->rmargin
)
405 p
->offset
= p
->rmargin
;
422 len
= n
->child
? a2height(p
, n
->child
->string
) : 1;
428 for (i
= 0; i
< len
; i
++)
441 const struct man_node
*nn
;
448 p
->flags
|= TERMP_NOBREAK
;
449 p
->flags
|= TERMP_TWOSPACE
;
458 /* Calculate offset. */
460 if (NULL
!= (nn
= n
->parent
->head
->child
))
461 if ((ival
= a2width(p
, nn
->string
)) >= 0)
465 len
= term_len(p
, 1);
467 p
->offset
= mt
->offset
;
468 p
->rmargin
= mt
->offset
+ len
;
471 mt
->lmargin
= (size_t)ival
;
488 p
->flags
&= ~TERMP_NOBREAK
;
489 p
->flags
&= ~TERMP_TWOSPACE
;
490 p
->offset
= mt
->offset
;
491 p
->rmargin
= p
->maxrmargin
;
506 mt
->lmargin
= term_len(p
, INDENT
);
510 p
->offset
= mt
->offset
;
514 return(MAN_HEAD
!= n
->type
);
522 const struct man_node
*nn
;
528 p
->flags
|= TERMP_NOLPAD
;
529 p
->flags
|= TERMP_NOSPACE
;
532 p
->flags
|= TERMP_NOBREAK
;
544 /* Calculate the offset from the optional second argument. */
545 if (NULL
!= (nn
= n
->parent
->head
->child
))
546 if (NULL
!= (nn
= nn
->next
))
547 if ((ival
= a2width(p
, nn
->string
)) >= 0)
552 /* Handle zero-width lengths. */
554 len
= term_len(p
, 1);
556 p
->offset
= mt
->offset
;
557 p
->rmargin
= mt
->offset
+ len
;
561 /* Set the saved left-margin. */
562 mt
->lmargin
= (size_t)ival
;
564 savelit
= MANT_LITERAL
& mt
->fl
;
565 mt
->fl
&= ~MANT_LITERAL
;
568 print_man_node(p
, mt
, n
->child
, m
);
571 mt
->fl
|= MANT_LITERAL
;
575 p
->offset
= mt
->offset
+ len
;
576 p
->rmargin
= p
->maxrmargin
;
594 p
->flags
&= ~TERMP_NOBREAK
;
595 p
->rmargin
= p
->maxrmargin
;
599 p
->flags
&= ~TERMP_NOLPAD
;
611 const struct man_node
*nn
;
617 p
->flags
|= TERMP_NOBREAK
;
620 p
->flags
|= TERMP_NOLPAD
;
621 p
->flags
|= TERMP_NOSPACE
;
630 len
= (size_t)mt
->lmargin
;
633 /* Calculate offset. */
635 if (NULL
!= (nn
= n
->parent
->head
->child
)) {
636 while (nn
&& MAN_TEXT
!= nn
->type
)
639 if ((ival
= a2width(p
, nn
->string
)) >= 0)
645 /* Handle zero-length properly. */
647 len
= term_len(p
, 1);
649 p
->offset
= mt
->offset
;
650 p
->rmargin
= mt
->offset
+ len
;
652 savelit
= MANT_LITERAL
& mt
->fl
;
653 mt
->fl
&= ~MANT_LITERAL
;
655 /* Don't print same-line elements. */
656 for (nn
= n
->child
; nn
; nn
= nn
->next
)
657 if (nn
->line
> n
->line
)
658 print_man_node(p
, mt
, nn
, m
);
661 mt
->fl
|= MANT_LITERAL
;
664 mt
->lmargin
= (size_t)ival
;
668 p
->offset
= mt
->offset
+ len
;
669 p
->rmargin
= p
->maxrmargin
;
687 p
->flags
&= ~TERMP_NOBREAK
;
688 p
->flags
&= ~TERMP_TWOSPACE
;
689 p
->rmargin
= p
->maxrmargin
;
693 p
->flags
&= ~TERMP_NOLPAD
;
708 mt
->lmargin
= term_len(p
, INDENT
);
709 mt
->offset
= term_len(p
, INDENT
);
710 /* If following a prior empty `SS', no vspace. */
711 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
712 if (NULL
== n
->prev
->body
->child
)
719 term_fontrepl(p
, TERMFONT_BOLD
);
720 p
->offset
= term_len(p
, HALFINDENT
);
723 p
->offset
= mt
->offset
;
758 mt
->lmargin
= term_len(p
, INDENT
);
759 mt
->offset
= term_len(p
, INDENT
);
760 /* If following a prior empty `SH', no vspace. */
761 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
762 if (NULL
== n
->prev
->body
->child
)
764 /* If the first macro, no vspae. */
770 term_fontrepl(p
, TERMFONT_BOLD
);
774 p
->offset
= mt
->offset
;
806 const struct man_node
*nn
;
819 if (NULL
== (nn
= n
->parent
->head
->child
)) {
820 mt
->offset
= mt
->lmargin
+ term_len(p
, INDENT
);
821 p
->offset
= mt
->offset
;
825 if ((ival
= a2width(p
, nn
->string
)) < 0)
828 mt
->offset
= term_len(p
, INDENT
) + (size_t)ival
;
829 p
->offset
= mt
->offset
;
842 mt
->offset
= mt
->lmargin
= term_len(p
, INDENT
);
848 p
->offset
= term_len(p
, INDENT
);
855 print_man_node(DECL_ARGS
)
863 * If we have a blank line, output a vertical space.
864 * If we have a space as the first character, break
865 * before printing the line's data.
867 if ('\0' == *n
->string
) {
870 } else if (' ' == *n
->string
&& MAN_LINE
& n
->flags
)
873 term_word(p
, n
->string
);
876 * If we're in a literal context, make sure that words
877 * togehter on the same line stay together. This is a
878 * POST-printing call, so we check the NEXT word. Since
879 * -man doesn't have nested macros, we don't need to be
880 * more specific than this.
882 if (MANT_LITERAL
& mt
->fl
&&
884 n
->next
->line
> n
->line
)) {
886 rmax
= p
->maxrmargin
;
887 p
->rmargin
= p
->maxrmargin
= TERM_MAXMARGIN
;
888 p
->flags
|= TERMP_NOSPACE
;
890 p
->flags
&= ~TERMP_NOLPAD
;
892 p
->maxrmargin
= rmax
;
895 if (MAN_EOS
& n
->flags
)
896 p
->flags
|= TERMP_SENTENCE
;
899 term_word(p
, n
->eqn
->data
);
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
)
948 const struct man_meta
*meta
;
950 meta
= (const struct man_meta
*)arg
;
952 term_fontrepl(p
, TERMFONT_NONE
);
958 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
959 p
->rmargin
= p
->maxrmargin
- term_strlen(p
, meta
->date
);
962 /* term_strlen() can return zero. */
963 if (p
->rmargin
== p
->maxrmargin
)
967 term_word(p
, meta
->source
);
972 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
973 p
->offset
= p
->rmargin
;
974 p
->rmargin
= p
->maxrmargin
;
975 p
->flags
&= ~TERMP_NOBREAK
;
977 term_word(p
, meta
->date
);
983 print_man_head(struct termp
*p
, const void *arg
)
985 char buf
[BUFSIZ
], title
[BUFSIZ
];
986 size_t buflen
, titlen
;
987 const struct man_meta
*m
;
989 m
= (const struct man_meta
*)arg
;
992 * Note that old groff would spit out some spaces before the
993 * header. We discontinue this strange behaviour, but at one
994 * point we did so here.
997 p
->rmargin
= p
->maxrmargin
;
1000 buf
[0] = title
[0] = '\0';
1003 strlcpy(buf
, m
->vol
, BUFSIZ
);
1004 buflen
= term_strlen(p
, buf
);
1006 snprintf(title
, BUFSIZ
, "%s(%s)", m
->title
, m
->msec
);
1007 titlen
= term_strlen(p
, title
);
1010 p
->rmargin
= 2 * (titlen
+1) + buflen
< p
->maxrmargin
?
1012 term_strlen(p
, buf
) + term_len(p
, 1)) / 2 :
1013 p
->maxrmargin
- buflen
;
1014 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
1016 term_word(p
, title
);
1019 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1020 p
->offset
= p
->rmargin
;
1021 p
->rmargin
= p
->offset
+ buflen
+ titlen
< p
->maxrmargin
?
1022 p
->maxrmargin
- titlen
: p
->maxrmargin
;
1027 p
->flags
&= ~TERMP_NOBREAK
;
1028 if (p
->rmargin
+ titlen
<= p
->maxrmargin
) {
1029 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
1030 p
->offset
= p
->rmargin
;
1031 p
->rmargin
= p
->maxrmargin
;
1032 term_word(p
, title
);
1036 p
->rmargin
= p
->maxrmargin
;
1038 p
->flags
&= ~TERMP_NOSPACE
;
1041 * Groff likes to have some leading spaces before content. Well
1042 * that's fine by me.