]>
git.cameronkatri.com Git - mandoc.git/blob - man_term.c
c400d91c3a9eeedb189cb5f3027420149c0f595c
1 /* $Id: man_term.c,v 1.49 2009/11/05 08:39:36 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>
34 /* FIXME: have PD set the default vspace width. */
38 #define MANT_LITERAL (1 << 0)
40 * Default amount to indent the left margin after leading text
41 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
42 * indent). This needs to be saved because `HP' and so on, if
43 * not having a specified value, must default.
45 * Note that this is the indentation AFTER the left offset, so
46 * the total offset is usually offset + lmargin.
50 * The default offset, i.e., the amount between any text and the
56 #define DECL_ARGS struct termp *p, \
58 const struct man_node *n, \
59 const struct man_meta *m
62 int (*pre
)(DECL_ARGS
);
63 void (*post
)(DECL_ARGS
);
67 extern size_t strlcpy(char *, const char *, size_t);
68 extern size_t strlcat(char *, const char *, size_t);
71 static int a2width(const struct man_node
*);
72 static int a2height(const struct man_node
*);
74 static void print_man_head(struct termp
*,
75 const struct man_meta
*);
76 static void print_man_body(DECL_ARGS
);
77 static void print_man_node(DECL_ARGS
);
78 static void print_man_foot(struct termp
*,
79 const struct man_meta
*);
80 static void print_bvspace(struct termp
*,
81 const struct man_node
*);
83 static int pre_B(DECL_ARGS
);
84 static int pre_BI(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_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_RI
, 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 */
144 { pre_ign
, NULL
}, /* PD */
150 terminal_man(void *arg
, const struct man
*man
)
153 const struct man_node
*n
;
154 const struct man_meta
*m
;
157 p
= (struct termp
*)arg
;
159 if (NULL
== p
->symtab
)
161 case (TERMENC_ASCII
):
162 p
->symtab
= chars_init(CHARS_ASCII
);
172 print_man_head(p
, m
);
173 p
->flags
|= TERMP_NOSPACE
;
180 print_man_body(p
, &mt
, n
->child
, m
);
181 print_man_foot(p
, m
);
186 a2height(const struct man_node
*n
)
190 assert(MAN_TEXT
== n
->type
);
192 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
193 SCALE_VS_INIT(&su
, strlen(n
->string
));
195 return((int)term_vspan(&su
));
200 a2width(const struct man_node
*n
)
204 assert(MAN_TEXT
== n
->type
);
206 if ( ! a2roffsu(n
->string
, &su
, SCALE_BU
))
209 return((int)term_hspan(&su
));
214 print_bvspace(struct termp
*p
, const struct man_node
*n
)
221 if (MAN_SS
== n
->prev
->tok
)
223 if (MAN_SH
== n
->prev
->tok
)
254 p
->bold
= p
->under
= 0;
283 mt
->fl
&= ~MANT_LITERAL
;
294 mt
->fl
|= MANT_LITERAL
;
303 const struct man_node
*nn
;
306 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
307 if (i
% 2 && MAN_RB
== n
->tok
)
309 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
313 p
->flags
|= TERMP_NOSPACE
;
315 print_man_node(p
, mt
, nn
, m
);
317 if (i
% 2 && MAN_RB
== n
->tok
)
319 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
330 const struct man_node
*nn
;
333 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
334 if (i
% 2 && MAN_RI
== n
->tok
)
336 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
340 p
->flags
|= TERMP_NOSPACE
;
341 print_man_node(p
, mt
, nn
, m
);
343 if (i
% 2 && MAN_RI
== n
->tok
)
345 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
356 const struct man_node
*nn
;
359 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
360 if (i
% 2 && MAN_BI
== n
->tok
)
364 else if (MAN_BI
== n
->tok
)
370 p
->flags
|= TERMP_NOSPACE
;
371 print_man_node(p
, mt
, nn
, m
);
373 if (i
% 2 && MAN_BI
== n
->tok
)
377 else if (MAN_BI
== n
->tok
)
411 len
= n
->child
? a2height(n
->child
) : 1;
415 for (i
= 0; i
< len
; i
++)
438 const struct man_node
*nn
;
445 p
->flags
|= TERMP_NOBREAK
;
446 p
->flags
|= TERMP_TWOSPACE
;
455 /* Calculate offset. */
457 if (NULL
!= (nn
= n
->parent
->head
->child
))
458 if ((ival
= a2width(nn
)) >= 0)
464 p
->offset
= mt
->offset
;
465 p
->rmargin
= mt
->offset
+ len
;
468 mt
->lmargin
= (size_t)ival
;
485 p
->flags
&= ~TERMP_NOBREAK
;
486 p
->flags
&= ~TERMP_TWOSPACE
;
487 p
->offset
= mt
->offset
;
488 p
->rmargin
= p
->maxrmargin
;
503 mt
->lmargin
= INDENT
;
507 p
->offset
= mt
->offset
;
519 const struct man_node
*nn
;
525 p
->flags
|= TERMP_NOLPAD
;
526 p
->flags
|= TERMP_NOSPACE
;
529 p
->flags
|= TERMP_NOBREAK
;
530 p
->flags
|= TERMP_TWOSPACE
;
542 /* Calculate offset. */
544 if (NULL
!= (nn
= n
->parent
->head
->child
))
545 if (NULL
!= (nn
= nn
->next
)) {
546 for ( ; nn
->next
; nn
= nn
->next
)
548 if ((ival
= a2width(nn
)) >= 0)
554 /* Handle zero-width lengths. */
558 p
->offset
= mt
->offset
;
559 p
->rmargin
= mt
->offset
+ len
;
563 /* Set the saved left-margin. */
564 mt
->lmargin
= (size_t)ival
;
566 /* Don't print the length value. */
567 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
568 print_man_node(p
, mt
, nn
, m
);
571 p
->offset
= mt
->offset
+ len
;
572 p
->rmargin
= p
->maxrmargin
;
590 p
->flags
&= ~TERMP_NOBREAK
;
591 p
->flags
&= ~TERMP_TWOSPACE
;
592 p
->rmargin
= p
->maxrmargin
;
596 p
->flags
&= ~TERMP_NOLPAD
;
608 const struct man_node
*nn
;
614 p
->flags
|= TERMP_NOBREAK
;
615 p
->flags
|= TERMP_TWOSPACE
;
618 p
->flags
|= TERMP_NOLPAD
;
619 p
->flags
|= TERMP_NOSPACE
;
628 len
= (size_t)mt
->lmargin
;
631 /* Calculate offset. */
633 if (NULL
!= (nn
= n
->parent
->head
->child
))
634 if (NULL
!= nn
->next
)
635 if ((ival
= a2width(nn
)) >= 0)
640 /* Handle zero-length properly. */
644 p
->offset
= mt
->offset
;
645 p
->rmargin
= mt
->offset
+ len
;
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
->lmargin
= (size_t)ival
;
657 p
->offset
= mt
->offset
+ len
;
658 p
->rmargin
= p
->maxrmargin
;
676 p
->flags
&= ~TERMP_NOBREAK
;
677 p
->flags
&= ~TERMP_TWOSPACE
;
678 p
->rmargin
= p
->maxrmargin
;
682 p
->flags
&= ~TERMP_NOLPAD
;
697 mt
->lmargin
= INDENT
;
699 /* If following a prior empty `SS', no vspace. */
700 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
701 if (NULL
== n
->prev
->body
->child
)
709 p
->offset
= HALFINDENT
;
712 p
->offset
= mt
->offset
;
748 mt
->lmargin
= INDENT
;
750 /* If following a prior empty `SH', no vspace. */
751 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
752 if (NULL
== n
->prev
->body
->child
)
761 p
->offset
= mt
->offset
;
794 const struct man_node
*nn
;
807 if (NULL
== (nn
= n
->parent
->head
->child
)) {
808 mt
->offset
= mt
->lmargin
+ INDENT
;
809 p
->offset
= mt
->offset
;
813 if ((ival
= a2width(nn
)) < 0)
816 mt
->offset
= INDENT
+ (size_t)ival
;
817 p
->offset
= mt
->offset
;
830 mt
->offset
= mt
->lmargin
= INDENT
;
841 print_man_node(DECL_ARGS
)
849 if (0 == *n
->string
) {
854 * Note! This is hacky. Here, we recognise the `\c'
855 * escape embedded in so many -man pages. It's supposed
856 * to remove the subsequent space, so we mark NOSPACE if
857 * it's encountered in the string.
859 sz
= (int)strlen(n
->string
);
860 term_word(p
, n
->string
);
861 if (sz
>= 2 && n
->string
[sz
- 1] == 'c' &&
862 n
->string
[sz
- 2] == '\\')
863 p
->flags
|= TERMP_NOSPACE
;
864 /* FIXME: this means that macro lines are munged! */
865 if (MANT_LITERAL
& mt
->fl
) {
866 p
->flags
|= TERMP_NOSPACE
;
871 if (termacts
[n
->tok
].pre
)
872 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
877 print_man_body(p
, mt
, n
->child
, m
);
879 if (MAN_TEXT
!= n
->type
)
880 if (termacts
[n
->tok
].post
)
881 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
886 print_man_body(DECL_ARGS
)
889 print_man_node(p
, mt
, n
, m
);
892 print_man_body(p
, mt
, n
->next
, m
);
897 print_man_foot(struct termp
*p
, const struct man_meta
*meta
)
901 time2a(meta
->date
, buf
, DATESIZ
);
905 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
906 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
910 term_word(p
, meta
->source
);
915 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
916 p
->offset
= p
->rmargin
;
917 p
->rmargin
= p
->maxrmargin
;
918 p
->flags
&= ~TERMP_NOBREAK
;
926 print_man_head(struct termp
*p
, const struct man_meta
*m
)
928 char buf
[BUFSIZ
], title
[BUFSIZ
];
930 p
->rmargin
= p
->maxrmargin
;
932 buf
[0] = title
[0] = '\0';
935 strlcpy(buf
, m
->vol
, BUFSIZ
);
937 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
940 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
941 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
946 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
947 p
->offset
= p
->rmargin
;
948 p
->rmargin
= p
->maxrmargin
- strlen(title
);
953 p
->offset
= p
->rmargin
;
954 p
->rmargin
= p
->maxrmargin
;
955 p
->flags
&= ~TERMP_NOBREAK
;
956 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
961 p
->rmargin
= p
->maxrmargin
;
963 p
->flags
&= ~TERMP_NOSPACE
;