]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.41 2009/10/18 19:17:14 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
37 #define MANT_LITERAL (1 << 0)
39 * Default amount to indent the left margin after leading text
40 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
41 * indent). This needs to be saved because `HP' and so on, if
42 * not having a specified value, must default.
44 * Note that this is the indentation AFTER the left offset, so
45 * the total offset is usually offset + lmargin.
49 * The default offset, i.e., the amount between any text and the
55 #define DECL_ARGS struct termp *p, \
57 const struct man_node *n, \
58 const struct man_meta *m
61 int (*pre
)(DECL_ARGS
);
62 void (*post
)(DECL_ARGS
);
66 extern size_t strlcpy(char *, const char *, size_t);
67 extern size_t strlcat(char *, const char *, size_t);
70 static int arg2width(const struct man_node
*);
71 static int arg2height(const struct man_node
*);
73 static void print_head(struct termp
*,
74 const struct man_meta
*);
75 static void print_body(DECL_ARGS
);
76 static void print_node(DECL_ARGS
);
77 static void print_foot(struct termp
*,
78 const struct man_meta
*);
79 static void print_bvspace(struct termp
*,
80 const struct man_node
*);
82 static int pre_B(DECL_ARGS
);
83 static int pre_BI(DECL_ARGS
);
84 static int pre_HP(DECL_ARGS
);
85 static int pre_I(DECL_ARGS
);
86 static int pre_IP(DECL_ARGS
);
87 static int pre_IR(DECL_ARGS
);
88 static int pre_PP(DECL_ARGS
);
89 static int pre_RB(DECL_ARGS
);
90 static int pre_RI(DECL_ARGS
);
91 static int pre_RS(DECL_ARGS
);
92 static int pre_SH(DECL_ARGS
);
93 static int pre_SS(DECL_ARGS
);
94 static int pre_TP(DECL_ARGS
);
95 static int pre_br(DECL_ARGS
);
96 static int pre_fi(DECL_ARGS
);
97 static int pre_ign(DECL_ARGS
);
98 static int pre_nf(DECL_ARGS
);
99 static int pre_r(DECL_ARGS
);
100 static int pre_sp(DECL_ARGS
);
102 static void post_B(DECL_ARGS
);
103 static void post_I(DECL_ARGS
);
104 static void post_IP(DECL_ARGS
);
105 static void post_HP(DECL_ARGS
);
106 static void post_RS(DECL_ARGS
);
107 static void post_SH(DECL_ARGS
);
108 static void post_SS(DECL_ARGS
);
109 static void post_TP(DECL_ARGS
);
110 static void post_i(DECL_ARGS
);
112 static const struct termact termacts
[MAN_MAX
] = {
113 { pre_br
, NULL
}, /* br */
114 { NULL
, NULL
}, /* TH */
115 { pre_SH
, post_SH
}, /* SH */
116 { pre_SS
, post_SS
}, /* SS */
117 { pre_TP
, post_TP
}, /* TP */
118 { pre_PP
, NULL
}, /* LP */
119 { pre_PP
, NULL
}, /* PP */
120 { pre_PP
, NULL
}, /* P */
121 { pre_IP
, post_IP
}, /* IP */
122 { pre_HP
, post_HP
}, /* HP */
123 { NULL
, NULL
}, /* SM */
124 { pre_B
, post_B
}, /* SB */
125 { pre_BI
, NULL
}, /* BI */
126 { pre_BI
, NULL
}, /* IB */
127 { pre_RB
, NULL
}, /* BR */
128 { pre_RB
, NULL
}, /* RB */
129 { NULL
, NULL
}, /* R */
130 { pre_B
, post_B
}, /* B */
131 { pre_I
, post_I
}, /* I */
132 { pre_IR
, NULL
}, /* IR */
133 { pre_RI
, NULL
}, /* RI */
134 { NULL
, NULL
}, /* na */
135 { pre_I
, post_i
}, /* i */
136 { pre_sp
, NULL
}, /* sp */
137 { pre_nf
, NULL
}, /* nf */
138 { pre_fi
, NULL
}, /* fi */
139 { pre_r
, NULL
}, /* r */
140 { NULL
, NULL
}, /* RE */
141 { pre_RS
, post_RS
}, /* RS */
142 { pre_ign
, NULL
}, /* DT */
143 { pre_ign
, NULL
}, /* UC */
149 terminal_man(void *arg
, const struct man
*man
)
152 const struct man_node
*n
;
153 const struct man_meta
*m
;
156 p
= (struct termp
*)arg
;
158 if (NULL
== p
->symtab
)
160 case (TERMENC_ASCII
):
161 p
->symtab
= chars_init(CHARS_ASCII
);
172 p
->flags
|= TERMP_NOSPACE
;
179 print_body(p
, &mt
, n
->child
, m
);
185 arg2height(const struct man_node
*n
)
189 assert(MAN_TEXT
== n
->type
);
191 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
192 SCALE_VS_INIT(&su
, strlen(n
->string
));
194 return((int)term_vspan(&su
));
199 arg2width(const struct man_node
*n
)
203 assert(MAN_TEXT
== n
->type
);
205 if ( ! a2roffsu(n
->string
, &su
, SCALE_BU
))
208 return((int)term_hspan(&su
));
213 print_bvspace(struct termp
*p
, const struct man_node
*n
)
220 if (MAN_SS
== n
->prev
->tok
)
222 if (MAN_SH
== n
->prev
->tok
)
253 p
->bold
= p
->under
= 0;
282 mt
->fl
&= ~MANT_LITERAL
;
293 mt
->fl
|= MANT_LITERAL
;
302 const struct man_node
*nn
;
305 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
309 p
->flags
|= TERMP_NOSPACE
;
310 print_node(p
, mt
, nn
, m
);
322 const struct man_node
*nn
;
325 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
326 if (i
% 2 && MAN_RB
== n
->tok
)
328 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
332 p
->flags
|= TERMP_NOSPACE
;
334 print_node(p
, mt
, nn
, m
);
336 if (i
% 2 && MAN_RB
== n
->tok
)
338 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
349 const struct man_node
*nn
;
352 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
356 p
->flags
|= TERMP_NOSPACE
;
357 print_node(p
, mt
, nn
, m
);
369 const struct man_node
*nn
;
372 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
373 if (i
% 2 && MAN_BI
== n
->tok
)
377 else if (MAN_BI
== n
->tok
)
383 p
->flags
|= TERMP_NOSPACE
;
384 print_node(p
, mt
, nn
, m
);
386 if (i
% 2 && MAN_BI
== n
->tok
)
390 else if (MAN_BI
== n
->tok
)
424 len
= n
->child
? arg2height(n
->child
) : 1;
428 for (i
= 0; i
< len
; i
++)
451 const struct man_node
*nn
;
458 p
->flags
|= TERMP_NOBREAK
;
459 p
->flags
|= TERMP_TWOSPACE
;
468 /* Calculate offset. */
470 if (NULL
!= (nn
= n
->parent
->head
->child
))
471 if ((ival
= arg2width(nn
)) >= 0)
477 p
->offset
= mt
->offset
;
478 p
->rmargin
= mt
->offset
+ len
;
481 mt
->lmargin
= (size_t)ival
;
498 p
->flags
&= ~TERMP_NOBREAK
;
499 p
->flags
&= ~TERMP_TWOSPACE
;
500 p
->offset
= mt
->offset
;
501 p
->rmargin
= p
->maxrmargin
;
516 mt
->lmargin
= INDENT
;
520 p
->offset
= mt
->offset
;
532 const struct man_node
*nn
;
538 p
->flags
|= TERMP_NOLPAD
;
539 p
->flags
|= TERMP_NOSPACE
;
542 p
->flags
|= TERMP_NOBREAK
;
543 p
->flags
|= TERMP_TWOSPACE
;
555 /* Calculate offset. */
557 if (NULL
!= (nn
= n
->parent
->head
->child
))
558 if (NULL
!= (nn
= nn
->next
)) {
559 for ( ; nn
->next
; nn
= nn
->next
)
561 if ((ival
= arg2width(nn
)) >= 0)
567 /* Handle zero-width lengths. */
571 p
->offset
= mt
->offset
;
572 p
->rmargin
= mt
->offset
+ len
;
576 /* Set the saved left-margin. */
577 mt
->lmargin
= (size_t)ival
;
579 /* Don't print the length value. */
580 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
581 print_node(p
, mt
, nn
, m
);
584 p
->offset
= mt
->offset
+ len
;
585 p
->rmargin
= p
->maxrmargin
;
603 p
->flags
&= ~TERMP_NOBREAK
;
604 p
->flags
&= ~TERMP_TWOSPACE
;
605 p
->rmargin
= p
->maxrmargin
;
609 p
->flags
&= ~TERMP_NOLPAD
;
621 const struct man_node
*nn
;
627 p
->flags
|= TERMP_NOBREAK
;
628 p
->flags
|= TERMP_TWOSPACE
;
631 p
->flags
|= TERMP_NOLPAD
;
632 p
->flags
|= TERMP_NOSPACE
;
641 len
= (size_t)mt
->lmargin
;
644 /* Calculate offset. */
646 if (NULL
!= (nn
= n
->parent
->head
->child
))
647 if (NULL
!= nn
->next
)
648 if ((ival
= arg2width(nn
)) >= 0)
653 /* Handle zero-length properly. */
657 p
->offset
= mt
->offset
;
658 p
->rmargin
= mt
->offset
+ len
;
660 /* Don't print same-line elements. */
661 for (nn
= n
->child
; nn
; nn
= nn
->next
)
662 if (nn
->line
> n
->line
)
663 print_node(p
, mt
, nn
, m
);
666 mt
->lmargin
= (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
;
695 p
->flags
&= ~TERMP_NOLPAD
;
710 mt
->lmargin
= INDENT
;
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
)
722 p
->offset
= HALFINDENT
;
725 p
->offset
= mt
->offset
;
761 mt
->lmargin
= INDENT
;
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
)
774 p
->offset
= mt
->offset
;
807 const struct man_node
*nn
;
820 if (NULL
== (nn
= n
->parent
->head
->child
)) {
821 mt
->offset
= mt
->lmargin
+ INDENT
;
822 p
->offset
= mt
->offset
;
826 if ((ival
= arg2width(nn
)) < 0)
829 mt
->offset
= INDENT
+ (size_t)ival
;
830 p
->offset
= mt
->offset
;
843 mt
->offset
= mt
->lmargin
= INDENT
;
854 print_node(DECL_ARGS
)
862 if (0 == *n
->string
) {
867 * Note! This is hacky. Here, we recognise the `\c'
868 * escape embedded in so many -man pages. It's supposed
869 * to remove the subsequent space, so we mark NOSPACE if
870 * it's encountered in the string.
872 sz
= (int)strlen(n
->string
);
873 term_word(p
, n
->string
);
874 if (sz
>= 2 && n
->string
[sz
- 1] == 'c' &&
875 n
->string
[sz
- 2] == '\\')
876 p
->flags
|= TERMP_NOSPACE
;
877 /* FIXME: this means that macro lines are munged! */
878 if (MANT_LITERAL
& mt
->fl
) {
879 p
->flags
|= TERMP_NOSPACE
;
884 if (termacts
[n
->tok
].pre
)
885 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
890 print_body(p
, mt
, n
->child
, m
);
892 if (MAN_TEXT
!= n
->type
)
893 if (termacts
[n
->tok
].post
)
894 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
899 print_body(DECL_ARGS
)
902 print_node(p
, mt
, n
, m
);
905 print_body(p
, mt
, n
->next
, m
);
910 print_foot(struct termp
*p
, const struct man_meta
*meta
)
915 tm
= localtime(&meta
->date
);
917 if (0 == strftime(buf
, p
->rmargin
, "%B %d, %Y", tm
))
918 (void)strlcpy(buf
, "(invalid date)", BUFSIZ
);
922 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
923 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
927 term_word(p
, meta
->source
);
932 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
933 p
->offset
= p
->rmargin
;
934 p
->rmargin
= p
->maxrmargin
;
935 p
->flags
&= ~TERMP_NOBREAK
;
943 print_head(struct termp
*p
, const struct man_meta
*meta
)
947 p
->rmargin
= p
->maxrmargin
;
950 if (NULL
== (buf
= malloc(p
->rmargin
)))
951 err(EXIT_FAILURE
, "malloc");
952 if (NULL
== (title
= malloc(p
->rmargin
)))
953 err(EXIT_FAILURE
, "malloc");
956 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
960 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
961 meta
->title
, meta
->msec
);
964 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
965 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
970 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
971 p
->offset
= p
->rmargin
;
972 p
->rmargin
= p
->maxrmargin
- strlen(title
);
977 p
->offset
= p
->rmargin
;
978 p
->rmargin
= p
->maxrmargin
;
979 p
->flags
&= ~TERMP_NOBREAK
;
980 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
985 p
->rmargin
= p
->maxrmargin
;
987 p
->flags
&= ~TERMP_NOSPACE
;