]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
1 /* $Id: man_term.c,v 1.44 2009/10/24 05:45:05 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>
35 /* FIXME: have PD set the default vspace width. */
39 #define MANT_LITERAL (1 << 0)
41 * Default amount to indent the left margin after leading text
42 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
43 * indent). This needs to be saved because `HP' and so on, if
44 * not having a specified value, must default.
46 * Note that this is the indentation AFTER the left offset, so
47 * the total offset is usually offset + lmargin.
51 * The default offset, i.e., the amount between any text and the
57 #define DECL_ARGS struct termp *p, \
59 const struct man_node *n, \
60 const struct man_meta *m
63 int (*pre
)(DECL_ARGS
);
64 void (*post
)(DECL_ARGS
);
68 extern size_t strlcpy(char *, const char *, size_t);
69 extern size_t strlcat(char *, const char *, size_t);
72 static int arg2width(const struct man_node
*);
73 static int arg2height(const struct man_node
*);
75 static void print_head(struct termp
*,
76 const struct man_meta
*);
77 static void print_body(DECL_ARGS
);
78 static void print_node(DECL_ARGS
);
79 static void print_foot(struct termp
*,
80 const struct man_meta
*);
81 static void print_bvspace(struct termp
*,
82 const struct man_node
*);
84 static int pre_B(DECL_ARGS
);
85 static int pre_BI(DECL_ARGS
);
86 static int pre_HP(DECL_ARGS
);
87 static int pre_I(DECL_ARGS
);
88 static int pre_IP(DECL_ARGS
);
89 static int pre_IR(DECL_ARGS
);
90 static int pre_PP(DECL_ARGS
);
91 static int pre_RB(DECL_ARGS
);
92 static int pre_RI(DECL_ARGS
);
93 static int pre_RS(DECL_ARGS
);
94 static int pre_SH(DECL_ARGS
);
95 static int pre_SS(DECL_ARGS
);
96 static int pre_TP(DECL_ARGS
);
97 static int pre_br(DECL_ARGS
);
98 static int pre_fi(DECL_ARGS
);
99 static int pre_ign(DECL_ARGS
);
100 static int pre_nf(DECL_ARGS
);
101 static int pre_r(DECL_ARGS
);
102 static int pre_sp(DECL_ARGS
);
104 static void post_B(DECL_ARGS
);
105 static void post_I(DECL_ARGS
);
106 static void post_IP(DECL_ARGS
);
107 static void post_HP(DECL_ARGS
);
108 static void post_RS(DECL_ARGS
);
109 static void post_SH(DECL_ARGS
);
110 static void post_SS(DECL_ARGS
);
111 static void post_TP(DECL_ARGS
);
112 static void post_i(DECL_ARGS
);
114 static const struct termact termacts
[MAN_MAX
] = {
115 { pre_br
, NULL
}, /* br */
116 { NULL
, NULL
}, /* TH */
117 { pre_SH
, post_SH
}, /* SH */
118 { pre_SS
, post_SS
}, /* SS */
119 { pre_TP
, post_TP
}, /* TP */
120 { pre_PP
, NULL
}, /* LP */
121 { pre_PP
, NULL
}, /* PP */
122 { pre_PP
, NULL
}, /* P */
123 { pre_IP
, post_IP
}, /* IP */
124 { pre_HP
, post_HP
}, /* HP */
125 { NULL
, NULL
}, /* SM */
126 { pre_B
, post_B
}, /* SB */
127 { pre_BI
, NULL
}, /* BI */
128 { pre_BI
, NULL
}, /* IB */
129 { pre_RB
, NULL
}, /* BR */
130 { pre_RB
, NULL
}, /* RB */
131 { NULL
, NULL
}, /* R */
132 { pre_B
, post_B
}, /* B */
133 { pre_I
, post_I
}, /* I */
134 { pre_IR
, NULL
}, /* IR */
135 { pre_RI
, NULL
}, /* RI */
136 { NULL
, NULL
}, /* na */
137 { pre_I
, post_i
}, /* i */
138 { pre_sp
, NULL
}, /* sp */
139 { pre_nf
, NULL
}, /* nf */
140 { pre_fi
, NULL
}, /* fi */
141 { pre_r
, NULL
}, /* r */
142 { NULL
, NULL
}, /* RE */
143 { pre_RS
, post_RS
}, /* RS */
144 { pre_ign
, NULL
}, /* DT */
145 { pre_ign
, NULL
}, /* UC */
146 { pre_ign
, NULL
}, /* PD */
152 terminal_man(void *arg
, const struct man
*man
)
155 const struct man_node
*n
;
156 const struct man_meta
*m
;
159 p
= (struct termp
*)arg
;
161 if (NULL
== p
->symtab
)
163 case (TERMENC_ASCII
):
164 p
->symtab
= chars_init(CHARS_ASCII
);
175 p
->flags
|= TERMP_NOSPACE
;
182 print_body(p
, &mt
, n
->child
, m
);
188 arg2height(const struct man_node
*n
)
192 assert(MAN_TEXT
== n
->type
);
194 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
195 SCALE_VS_INIT(&su
, strlen(n
->string
));
197 return((int)term_vspan(&su
));
202 arg2width(const struct man_node
*n
)
206 assert(MAN_TEXT
== n
->type
);
208 if ( ! a2roffsu(n
->string
, &su
, SCALE_BU
))
211 return((int)term_hspan(&su
));
216 print_bvspace(struct termp
*p
, const struct man_node
*n
)
223 if (MAN_SS
== n
->prev
->tok
)
225 if (MAN_SH
== n
->prev
->tok
)
256 p
->bold
= p
->under
= 0;
285 mt
->fl
&= ~MANT_LITERAL
;
296 mt
->fl
|= MANT_LITERAL
;
305 const struct man_node
*nn
;
308 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
312 p
->flags
|= TERMP_NOSPACE
;
313 print_node(p
, mt
, nn
, m
);
325 const struct man_node
*nn
;
328 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
329 if (i
% 2 && MAN_RB
== n
->tok
)
331 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
335 p
->flags
|= TERMP_NOSPACE
;
337 print_node(p
, mt
, nn
, m
);
339 if (i
% 2 && MAN_RB
== n
->tok
)
341 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
352 const struct man_node
*nn
;
355 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
359 p
->flags
|= TERMP_NOSPACE
;
360 print_node(p
, mt
, nn
, m
);
372 const struct man_node
*nn
;
375 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
376 if (i
% 2 && MAN_BI
== n
->tok
)
380 else if (MAN_BI
== n
->tok
)
386 p
->flags
|= TERMP_NOSPACE
;
387 print_node(p
, mt
, nn
, m
);
389 if (i
% 2 && MAN_BI
== n
->tok
)
393 else if (MAN_BI
== n
->tok
)
427 len
= n
->child
? arg2height(n
->child
) : 1;
431 for (i
= 0; i
< len
; i
++)
454 const struct man_node
*nn
;
461 p
->flags
|= TERMP_NOBREAK
;
462 p
->flags
|= TERMP_TWOSPACE
;
471 /* Calculate offset. */
473 if (NULL
!= (nn
= n
->parent
->head
->child
))
474 if ((ival
= arg2width(nn
)) >= 0)
480 p
->offset
= mt
->offset
;
481 p
->rmargin
= mt
->offset
+ len
;
484 mt
->lmargin
= (size_t)ival
;
501 p
->flags
&= ~TERMP_NOBREAK
;
502 p
->flags
&= ~TERMP_TWOSPACE
;
503 p
->offset
= mt
->offset
;
504 p
->rmargin
= p
->maxrmargin
;
519 mt
->lmargin
= INDENT
;
523 p
->offset
= mt
->offset
;
535 const struct man_node
*nn
;
541 p
->flags
|= TERMP_NOLPAD
;
542 p
->flags
|= TERMP_NOSPACE
;
545 p
->flags
|= TERMP_NOBREAK
;
546 p
->flags
|= TERMP_TWOSPACE
;
558 /* Calculate offset. */
560 if (NULL
!= (nn
= n
->parent
->head
->child
))
561 if (NULL
!= (nn
= nn
->next
)) {
562 for ( ; nn
->next
; nn
= nn
->next
)
564 if ((ival
= arg2width(nn
)) >= 0)
570 /* Handle zero-width lengths. */
574 p
->offset
= mt
->offset
;
575 p
->rmargin
= mt
->offset
+ len
;
579 /* Set the saved left-margin. */
580 mt
->lmargin
= (size_t)ival
;
582 /* Don't print the length value. */
583 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
584 print_node(p
, mt
, nn
, m
);
587 p
->offset
= mt
->offset
+ len
;
588 p
->rmargin
= p
->maxrmargin
;
606 p
->flags
&= ~TERMP_NOBREAK
;
607 p
->flags
&= ~TERMP_TWOSPACE
;
608 p
->rmargin
= p
->maxrmargin
;
612 p
->flags
&= ~TERMP_NOLPAD
;
624 const struct man_node
*nn
;
630 p
->flags
|= TERMP_NOBREAK
;
631 p
->flags
|= TERMP_TWOSPACE
;
634 p
->flags
|= TERMP_NOLPAD
;
635 p
->flags
|= TERMP_NOSPACE
;
644 len
= (size_t)mt
->lmargin
;
647 /* Calculate offset. */
649 if (NULL
!= (nn
= n
->parent
->head
->child
))
650 if (NULL
!= nn
->next
)
651 if ((ival
= arg2width(nn
)) >= 0)
656 /* Handle zero-length properly. */
660 p
->offset
= mt
->offset
;
661 p
->rmargin
= mt
->offset
+ len
;
663 /* Don't print same-line elements. */
664 for (nn
= n
->child
; nn
; nn
= nn
->next
)
665 if (nn
->line
> n
->line
)
666 print_node(p
, mt
, nn
, m
);
669 mt
->lmargin
= (size_t)ival
;
673 p
->offset
= mt
->offset
+ len
;
674 p
->rmargin
= p
->maxrmargin
;
692 p
->flags
&= ~TERMP_NOBREAK
;
693 p
->flags
&= ~TERMP_TWOSPACE
;
694 p
->rmargin
= p
->maxrmargin
;
698 p
->flags
&= ~TERMP_NOLPAD
;
713 mt
->lmargin
= INDENT
;
715 /* If following a prior empty `SS', no vspace. */
716 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
717 if (NULL
== n
->prev
->body
->child
)
725 p
->offset
= HALFINDENT
;
728 p
->offset
= mt
->offset
;
764 mt
->lmargin
= INDENT
;
766 /* If following a prior empty `SH', no vspace. */
767 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
768 if (NULL
== n
->prev
->body
->child
)
777 p
->offset
= mt
->offset
;
810 const struct man_node
*nn
;
823 if (NULL
== (nn
= n
->parent
->head
->child
)) {
824 mt
->offset
= mt
->lmargin
+ INDENT
;
825 p
->offset
= mt
->offset
;
829 if ((ival
= arg2width(nn
)) < 0)
832 mt
->offset
= INDENT
+ (size_t)ival
;
833 p
->offset
= mt
->offset
;
846 mt
->offset
= mt
->lmargin
= INDENT
;
857 print_node(DECL_ARGS
)
865 if (0 == *n
->string
) {
870 * Note! This is hacky. Here, we recognise the `\c'
871 * escape embedded in so many -man pages. It's supposed
872 * to remove the subsequent space, so we mark NOSPACE if
873 * it's encountered in the string.
875 sz
= (int)strlen(n
->string
);
876 term_word(p
, n
->string
);
877 if (sz
>= 2 && n
->string
[sz
- 1] == 'c' &&
878 n
->string
[sz
- 2] == '\\')
879 p
->flags
|= TERMP_NOSPACE
;
880 /* FIXME: this means that macro lines are munged! */
881 if (MANT_LITERAL
& mt
->fl
) {
882 p
->flags
|= TERMP_NOSPACE
;
887 if (termacts
[n
->tok
].pre
)
888 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
893 print_body(p
, mt
, n
->child
, m
);
895 if (MAN_TEXT
!= n
->type
)
896 if (termacts
[n
->tok
].post
)
897 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
902 print_body(DECL_ARGS
)
905 print_node(p
, mt
, n
, m
);
908 print_body(p
, mt
, n
->next
, m
);
913 print_foot(struct termp
*p
, const struct man_meta
*meta
)
917 time2a(meta
->date
, buf
, DATESIZ
);
921 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
922 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
926 term_word(p
, meta
->source
);
931 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
932 p
->offset
= p
->rmargin
;
933 p
->rmargin
= p
->maxrmargin
;
934 p
->flags
&= ~TERMP_NOBREAK
;
942 print_head(struct termp
*p
, const struct man_meta
*meta
)
946 p
->rmargin
= p
->maxrmargin
;
949 if (NULL
== (buf
= malloc(p
->rmargin
)))
950 err(EXIT_FAILURE
, "malloc");
951 if (NULL
== (title
= malloc(p
->rmargin
)))
952 err(EXIT_FAILURE
, "malloc");
955 (void)strlcpy(buf
, meta
->vol
, p
->rmargin
);
959 (void)snprintf(title
, p
->rmargin
, "%s(%d)",
960 meta
->title
, meta
->msec
);
963 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
964 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
969 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
970 p
->offset
= p
->rmargin
;
971 p
->rmargin
= p
->maxrmargin
- strlen(title
);
976 p
->offset
= p
->rmargin
;
977 p
->rmargin
= p
->maxrmargin
;
978 p
->flags
&= ~TERMP_NOBREAK
;
979 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
984 p
->rmargin
= p
->maxrmargin
;
986 p
->flags
&= ~TERMP_NOSPACE
;