1 /* $Vendor-Id: man_term.c,v 1.54 2009/11/12 08:21: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>
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_nodelist(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_sp(DECL_ARGS
);
101 static void post_IP(DECL_ARGS
);
102 static void post_HP(DECL_ARGS
);
103 static void post_RS(DECL_ARGS
);
104 static void post_SH(DECL_ARGS
);
105 static void post_SS(DECL_ARGS
);
106 static void post_TP(DECL_ARGS
);
108 static const struct termact termacts
[MAN_MAX
] = {
109 { pre_br
, NULL
}, /* br */
110 { NULL
, NULL
}, /* TH */
111 { pre_SH
, post_SH
}, /* SH */
112 { pre_SS
, post_SS
}, /* SS */
113 { pre_TP
, post_TP
}, /* TP */
114 { pre_PP
, NULL
}, /* LP */
115 { pre_PP
, NULL
}, /* PP */
116 { pre_PP
, NULL
}, /* P */
117 { pre_IP
, post_IP
}, /* IP */
118 { pre_HP
, post_HP
}, /* HP */
119 { NULL
, NULL
}, /* SM */
120 { pre_B
, NULL
}, /* SB */
121 { pre_BI
, NULL
}, /* BI */
122 { pre_BI
, NULL
}, /* IB */
123 { pre_RB
, NULL
}, /* BR */
124 { pre_RB
, NULL
}, /* RB */
125 { NULL
, NULL
}, /* R */
126 { pre_B
, NULL
}, /* B */
127 { pre_I
, NULL
}, /* I */
128 { pre_RI
, NULL
}, /* IR */
129 { pre_RI
, NULL
}, /* RI */
130 { NULL
, NULL
}, /* na */
131 { pre_I
, NULL
}, /* i */
132 { pre_sp
, NULL
}, /* sp */
133 { pre_nf
, NULL
}, /* nf */
134 { pre_fi
, NULL
}, /* fi */
135 { NULL
, NULL
}, /* r */
136 { NULL
, NULL
}, /* RE */
137 { pre_RS
, post_RS
}, /* RS */
138 { pre_ign
, NULL
}, /* DT */
139 { pre_ign
, NULL
}, /* UC */
140 { pre_ign
, NULL
}, /* PD */
146 terminal_man(void *arg
, const struct man
*man
)
149 const struct man_node
*n
;
150 const struct man_meta
*m
;
153 p
= (struct termp
*)arg
;
155 if (NULL
== p
->symtab
)
157 case (TERMENC_ASCII
):
158 p
->symtab
= chars_init(CHARS_ASCII
);
168 print_man_head(p
, m
);
169 p
->flags
|= TERMP_NOSPACE
;
176 print_man_nodelist(p
, &mt
, n
->child
, m
);
177 print_man_foot(p
, m
);
182 a2height(const struct man_node
*n
)
186 assert(MAN_TEXT
== n
->type
);
188 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
189 SCALE_VS_INIT(&su
, strlen(n
->string
));
191 return((int)term_vspan(&su
));
196 a2width(const struct man_node
*n
)
200 assert(MAN_TEXT
== n
->type
);
202 if ( ! a2roffsu(n
->string
, &su
, SCALE_BU
))
205 return((int)term_hspan(&su
));
210 print_bvspace(struct termp
*p
, const struct man_node
*n
)
217 if (MAN_SS
== n
->prev
->tok
)
219 if (MAN_SH
== n
->prev
->tok
)
240 term_fontrepl(p
, TERMFONT_UNDER
);
250 mt
->fl
&= ~MANT_LITERAL
;
261 mt
->fl
|= MANT_LITERAL
;
270 const struct man_node
*nn
;
273 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
274 if (i
% 2 && MAN_RB
== n
->tok
)
275 term_fontrepl(p
, TERMFONT_BOLD
);
276 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
277 term_fontrepl(p
, TERMFONT_BOLD
);
279 term_fontrepl(p
, TERMFONT_NONE
);
282 p
->flags
|= TERMP_NOSPACE
;
284 print_man_node(p
, mt
, nn
, m
);
294 const struct man_node
*nn
;
297 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
298 if (i
% 2 && MAN_RI
== n
->tok
)
299 term_fontrepl(p
, TERMFONT_UNDER
);
300 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
301 term_fontrepl(p
, TERMFONT_UNDER
);
303 term_fontrepl(p
, TERMFONT_NONE
);
306 p
->flags
|= TERMP_NOSPACE
;
308 print_man_node(p
, mt
, nn
, m
);
318 const struct man_node
*nn
;
321 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
322 if (i
% 2 && MAN_BI
== n
->tok
)
323 term_fontrepl(p
, TERMFONT_UNDER
);
325 term_fontrepl(p
, TERMFONT_BOLD
);
326 else if (MAN_BI
== n
->tok
)
327 term_fontrepl(p
, TERMFONT_BOLD
);
329 term_fontrepl(p
, TERMFONT_UNDER
);
332 p
->flags
|= TERMP_NOSPACE
;
334 print_man_node(p
, mt
, nn
, m
);
345 term_fontrepl(p
, TERMFONT_BOLD
);
356 len
= n
->child
? a2height(n
->child
) : 1;
360 for (i
= 0; i
< len
; i
++)
383 const struct man_node
*nn
;
390 p
->flags
|= TERMP_NOBREAK
;
391 p
->flags
|= TERMP_TWOSPACE
;
400 /* Calculate offset. */
402 if (NULL
!= (nn
= n
->parent
->head
->child
))
403 if ((ival
= a2width(nn
)) >= 0)
409 p
->offset
= mt
->offset
;
410 p
->rmargin
= mt
->offset
+ len
;
413 mt
->lmargin
= (size_t)ival
;
430 p
->flags
&= ~TERMP_NOBREAK
;
431 p
->flags
&= ~TERMP_TWOSPACE
;
432 p
->offset
= mt
->offset
;
433 p
->rmargin
= p
->maxrmargin
;
448 mt
->lmargin
= INDENT
;
452 p
->offset
= mt
->offset
;
464 const struct man_node
*nn
;
470 p
->flags
|= TERMP_NOLPAD
;
471 p
->flags
|= TERMP_NOSPACE
;
474 p
->flags
|= TERMP_NOBREAK
;
475 p
->flags
|= TERMP_TWOSPACE
;
487 /* Calculate offset. */
489 if (NULL
!= (nn
= n
->parent
->head
->child
))
490 if (NULL
!= (nn
= nn
->next
)) {
491 for ( ; nn
->next
; nn
= nn
->next
)
493 if ((ival
= a2width(nn
)) >= 0)
499 /* Handle zero-width lengths. */
503 p
->offset
= mt
->offset
;
504 p
->rmargin
= mt
->offset
+ len
;
508 /* Set the saved left-margin. */
509 mt
->lmargin
= (size_t)ival
;
511 /* Don't print the length value. */
512 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
513 print_man_node(p
, mt
, nn
, m
);
516 p
->offset
= mt
->offset
+ len
;
517 p
->rmargin
= p
->maxrmargin
;
535 p
->flags
&= ~TERMP_NOBREAK
;
536 p
->flags
&= ~TERMP_TWOSPACE
;
537 p
->rmargin
= p
->maxrmargin
;
541 p
->flags
&= ~TERMP_NOLPAD
;
553 const struct man_node
*nn
;
559 p
->flags
|= TERMP_NOBREAK
;
560 p
->flags
|= TERMP_TWOSPACE
;
563 p
->flags
|= TERMP_NOLPAD
;
564 p
->flags
|= TERMP_NOSPACE
;
573 len
= (size_t)mt
->lmargin
;
576 /* Calculate offset. */
578 if (NULL
!= (nn
= n
->parent
->head
->child
))
579 if (NULL
!= nn
->next
)
580 if ((ival
= a2width(nn
)) >= 0)
585 /* Handle zero-length properly. */
589 p
->offset
= mt
->offset
;
590 p
->rmargin
= mt
->offset
+ len
;
592 /* Don't print same-line elements. */
593 for (nn
= n
->child
; nn
; nn
= nn
->next
)
594 if (nn
->line
> n
->line
)
595 print_man_node(p
, mt
, nn
, m
);
598 mt
->lmargin
= (size_t)ival
;
602 p
->offset
= mt
->offset
+ len
;
603 p
->rmargin
= p
->maxrmargin
;
621 p
->flags
&= ~TERMP_NOBREAK
;
622 p
->flags
&= ~TERMP_TWOSPACE
;
623 p
->rmargin
= p
->maxrmargin
;
627 p
->flags
&= ~TERMP_NOLPAD
;
642 mt
->lmargin
= INDENT
;
644 /* If following a prior empty `SS', no vspace. */
645 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
646 if (NULL
== n
->prev
->body
->child
)
653 term_fontrepl(p
, TERMFONT_BOLD
);
654 p
->offset
= HALFINDENT
;
657 p
->offset
= mt
->offset
;
692 mt
->lmargin
= INDENT
;
694 /* If following a prior empty `SH', no vspace. */
695 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
696 if (NULL
== n
->prev
->body
->child
)
701 term_fontrepl(p
, TERMFONT_BOLD
);
705 p
->offset
= mt
->offset
;
737 const struct man_node
*nn
;
750 if (NULL
== (nn
= n
->parent
->head
->child
)) {
751 mt
->offset
= mt
->lmargin
+ INDENT
;
752 p
->offset
= mt
->offset
;
756 if ((ival
= a2width(nn
)) < 0)
759 mt
->offset
= INDENT
+ (size_t)ival
;
760 p
->offset
= mt
->offset
;
773 mt
->offset
= mt
->lmargin
= INDENT
;
784 print_man_node(DECL_ARGS
)
792 if (0 == *n
->string
) {
797 term_word(p
, n
->string
);
799 /* FIXME: this means that macro lines are munged! */
801 if (MANT_LITERAL
& mt
->fl
) {
802 p
->flags
|= TERMP_NOSPACE
;
807 term_fontrepl(p
, TERMFONT_NONE
);
808 if (termacts
[n
->tok
].pre
)
809 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
814 print_man_nodelist(p
, mt
, n
->child
, m
);
816 if (MAN_TEXT
!= n
->type
) {
817 if (termacts
[n
->tok
].post
)
818 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
819 term_fontrepl(p
, TERMFONT_NONE
);
825 print_man_nodelist(DECL_ARGS
)
828 print_man_node(p
, mt
, n
, m
);
831 print_man_nodelist(p
, mt
, n
->next
, m
);
836 print_man_foot(struct termp
*p
, const struct man_meta
*meta
)
840 term_fontrepl(p
, TERMFONT_NONE
);
842 time2a(meta
->date
, buf
, DATESIZ
);
846 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
847 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
851 term_word(p
, meta
->source
);
856 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
857 p
->offset
= p
->rmargin
;
858 p
->rmargin
= p
->maxrmargin
;
859 p
->flags
&= ~TERMP_NOBREAK
;
867 print_man_head(struct termp
*p
, const struct man_meta
*m
)
869 char buf
[BUFSIZ
], title
[BUFSIZ
];
871 p
->rmargin
= p
->maxrmargin
;
873 buf
[0] = title
[0] = '\0';
876 strlcpy(buf
, m
->vol
, BUFSIZ
);
878 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
881 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
882 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
887 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
888 p
->offset
= p
->rmargin
;
889 p
->rmargin
= p
->maxrmargin
- strlen(title
);
894 p
->offset
= p
->rmargin
;
895 p
->rmargin
= p
->maxrmargin
;
896 p
->flags
&= ~TERMP_NOBREAK
;
897 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
902 p
->rmargin
= p
->maxrmargin
;
904 p
->flags
&= ~TERMP_NOSPACE
;