1 /* $Id: man_term.c,v 1.55 2010/01/01 17:14:28 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
);
66 static int a2width(const struct man_node
*);
67 static int a2height(const struct man_node
*);
69 static void print_man_head(struct termp
*,
70 const struct man_meta
*);
71 static void print_man_nodelist(DECL_ARGS
);
72 static void print_man_node(DECL_ARGS
);
73 static void print_man_foot(struct termp
*,
74 const struct man_meta
*);
75 static void print_bvspace(struct termp
*,
76 const struct man_node
*);
78 static int pre_B(DECL_ARGS
);
79 static int pre_BI(DECL_ARGS
);
80 static int pre_HP(DECL_ARGS
);
81 static int pre_I(DECL_ARGS
);
82 static int pre_IP(DECL_ARGS
);
83 static int pre_PP(DECL_ARGS
);
84 static int pre_RB(DECL_ARGS
);
85 static int pre_RI(DECL_ARGS
);
86 static int pre_RS(DECL_ARGS
);
87 static int pre_SH(DECL_ARGS
);
88 static int pre_SS(DECL_ARGS
);
89 static int pre_TP(DECL_ARGS
);
90 static int pre_br(DECL_ARGS
);
91 static int pre_fi(DECL_ARGS
);
92 static int pre_ign(DECL_ARGS
);
93 static int pre_nf(DECL_ARGS
);
94 static int pre_sp(DECL_ARGS
);
96 static void post_IP(DECL_ARGS
);
97 static void post_HP(DECL_ARGS
);
98 static void post_RS(DECL_ARGS
);
99 static void post_SH(DECL_ARGS
);
100 static void post_SS(DECL_ARGS
);
101 static void post_TP(DECL_ARGS
);
103 static const struct termact termacts
[MAN_MAX
] = {
104 { pre_br
, NULL
}, /* br */
105 { NULL
, NULL
}, /* TH */
106 { pre_SH
, post_SH
}, /* SH */
107 { pre_SS
, post_SS
}, /* SS */
108 { pre_TP
, post_TP
}, /* TP */
109 { pre_PP
, NULL
}, /* LP */
110 { pre_PP
, NULL
}, /* PP */
111 { pre_PP
, NULL
}, /* P */
112 { pre_IP
, post_IP
}, /* IP */
113 { pre_HP
, post_HP
}, /* HP */
114 { NULL
, NULL
}, /* SM */
115 { pre_B
, NULL
}, /* SB */
116 { pre_BI
, NULL
}, /* BI */
117 { pre_BI
, NULL
}, /* IB */
118 { pre_RB
, NULL
}, /* BR */
119 { pre_RB
, NULL
}, /* RB */
120 { NULL
, NULL
}, /* R */
121 { pre_B
, NULL
}, /* B */
122 { pre_I
, NULL
}, /* I */
123 { pre_RI
, NULL
}, /* IR */
124 { pre_RI
, NULL
}, /* RI */
125 { NULL
, NULL
}, /* na */
126 { pre_I
, NULL
}, /* i */
127 { pre_sp
, NULL
}, /* sp */
128 { pre_nf
, NULL
}, /* nf */
129 { pre_fi
, NULL
}, /* fi */
130 { NULL
, NULL
}, /* r */
131 { NULL
, NULL
}, /* RE */
132 { pre_RS
, post_RS
}, /* RS */
133 { pre_ign
, NULL
}, /* DT */
134 { pre_ign
, NULL
}, /* UC */
135 { pre_ign
, NULL
}, /* PD */
141 terminal_man(void *arg
, const struct man
*man
)
144 const struct man_node
*n
;
145 const struct man_meta
*m
;
148 p
= (struct termp
*)arg
;
150 if (NULL
== p
->symtab
)
152 case (TERMENC_ASCII
):
153 p
->symtab
= chars_init(CHARS_ASCII
);
163 print_man_head(p
, m
);
164 p
->flags
|= TERMP_NOSPACE
;
171 print_man_nodelist(p
, &mt
, n
->child
, m
);
172 print_man_foot(p
, m
);
177 a2height(const struct man_node
*n
)
181 assert(MAN_TEXT
== n
->type
);
183 if ( ! a2roffsu(n
->string
, &su
, SCALE_VS
))
184 SCALE_VS_INIT(&su
, strlen(n
->string
));
186 return((int)term_vspan(&su
));
191 a2width(const struct man_node
*n
)
195 assert(MAN_TEXT
== n
->type
);
197 if ( ! a2roffsu(n
->string
, &su
, SCALE_BU
))
200 return((int)term_hspan(&su
));
205 print_bvspace(struct termp
*p
, const struct man_node
*n
)
212 if (MAN_SS
== n
->prev
->tok
)
214 if (MAN_SH
== n
->prev
->tok
)
235 term_fontrepl(p
, TERMFONT_UNDER
);
245 mt
->fl
&= ~MANT_LITERAL
;
256 mt
->fl
|= MANT_LITERAL
;
265 const struct man_node
*nn
;
268 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
269 if (i
% 2 && MAN_RB
== n
->tok
)
270 term_fontrepl(p
, TERMFONT_BOLD
);
271 else if ( ! (i
% 2) && MAN_RB
!= n
->tok
)
272 term_fontrepl(p
, TERMFONT_BOLD
);
274 term_fontrepl(p
, TERMFONT_NONE
);
277 p
->flags
|= TERMP_NOSPACE
;
279 print_man_node(p
, mt
, nn
, m
);
289 const struct man_node
*nn
;
292 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
293 if (i
% 2 && MAN_RI
== n
->tok
)
294 term_fontrepl(p
, TERMFONT_UNDER
);
295 else if ( ! (i
% 2) && MAN_RI
!= n
->tok
)
296 term_fontrepl(p
, TERMFONT_UNDER
);
298 term_fontrepl(p
, TERMFONT_NONE
);
301 p
->flags
|= TERMP_NOSPACE
;
303 print_man_node(p
, mt
, nn
, m
);
313 const struct man_node
*nn
;
316 for (i
= 0, nn
= n
->child
; nn
; nn
= nn
->next
, i
++) {
317 if (i
% 2 && MAN_BI
== n
->tok
)
318 term_fontrepl(p
, TERMFONT_UNDER
);
320 term_fontrepl(p
, TERMFONT_BOLD
);
321 else if (MAN_BI
== n
->tok
)
322 term_fontrepl(p
, TERMFONT_BOLD
);
324 term_fontrepl(p
, TERMFONT_UNDER
);
327 p
->flags
|= TERMP_NOSPACE
;
329 print_man_node(p
, mt
, nn
, m
);
340 term_fontrepl(p
, TERMFONT_BOLD
);
351 len
= n
->child
? a2height(n
->child
) : 1;
355 for (i
= 0; i
< len
; i
++)
378 const struct man_node
*nn
;
385 p
->flags
|= TERMP_NOBREAK
;
386 p
->flags
|= TERMP_TWOSPACE
;
395 /* Calculate offset. */
397 if (NULL
!= (nn
= n
->parent
->head
->child
))
398 if ((ival
= a2width(nn
)) >= 0)
404 p
->offset
= mt
->offset
;
405 p
->rmargin
= mt
->offset
+ len
;
408 mt
->lmargin
= (size_t)ival
;
425 p
->flags
&= ~TERMP_NOBREAK
;
426 p
->flags
&= ~TERMP_TWOSPACE
;
427 p
->offset
= mt
->offset
;
428 p
->rmargin
= p
->maxrmargin
;
443 mt
->lmargin
= INDENT
;
447 p
->offset
= mt
->offset
;
459 const struct man_node
*nn
;
465 p
->flags
|= TERMP_NOLPAD
;
466 p
->flags
|= TERMP_NOSPACE
;
469 p
->flags
|= TERMP_NOBREAK
;
470 p
->flags
|= TERMP_TWOSPACE
;
482 /* Calculate offset. */
484 if (NULL
!= (nn
= n
->parent
->head
->child
))
485 if (NULL
!= (nn
= nn
->next
)) {
486 for ( ; nn
->next
; nn
= nn
->next
)
488 if ((ival
= a2width(nn
)) >= 0)
494 /* Handle zero-width lengths. */
498 p
->offset
= mt
->offset
;
499 p
->rmargin
= mt
->offset
+ len
;
503 /* Set the saved left-margin. */
504 mt
->lmargin
= (size_t)ival
;
506 /* Don't print the length value. */
507 for (nn
= n
->child
; nn
->next
; nn
= nn
->next
)
508 print_man_node(p
, mt
, nn
, m
);
511 p
->offset
= mt
->offset
+ len
;
512 p
->rmargin
= p
->maxrmargin
;
530 p
->flags
&= ~TERMP_NOBREAK
;
531 p
->flags
&= ~TERMP_TWOSPACE
;
532 p
->rmargin
= p
->maxrmargin
;
536 p
->flags
&= ~TERMP_NOLPAD
;
548 const struct man_node
*nn
;
554 p
->flags
|= TERMP_NOBREAK
;
555 p
->flags
|= TERMP_TWOSPACE
;
558 p
->flags
|= TERMP_NOLPAD
;
559 p
->flags
|= TERMP_NOSPACE
;
568 len
= (size_t)mt
->lmargin
;
571 /* Calculate offset. */
573 if (NULL
!= (nn
= n
->parent
->head
->child
))
574 if (NULL
!= nn
->next
)
575 if ((ival
= a2width(nn
)) >= 0)
580 /* Handle zero-length properly. */
584 p
->offset
= mt
->offset
;
585 p
->rmargin
= mt
->offset
+ len
;
587 /* Don't print same-line elements. */
588 for (nn
= n
->child
; nn
; nn
= nn
->next
)
589 if (nn
->line
> n
->line
)
590 print_man_node(p
, mt
, nn
, m
);
593 mt
->lmargin
= (size_t)ival
;
597 p
->offset
= mt
->offset
+ len
;
598 p
->rmargin
= p
->maxrmargin
;
616 p
->flags
&= ~TERMP_NOBREAK
;
617 p
->flags
&= ~TERMP_TWOSPACE
;
618 p
->rmargin
= p
->maxrmargin
;
622 p
->flags
&= ~TERMP_NOLPAD
;
637 mt
->lmargin
= INDENT
;
639 /* If following a prior empty `SS', no vspace. */
640 if (n
->prev
&& MAN_SS
== n
->prev
->tok
)
641 if (NULL
== n
->prev
->body
->child
)
648 term_fontrepl(p
, TERMFONT_BOLD
);
649 p
->offset
= HALFINDENT
;
652 p
->offset
= mt
->offset
;
687 mt
->lmargin
= INDENT
;
689 /* If following a prior empty `SH', no vspace. */
690 if (n
->prev
&& MAN_SH
== n
->prev
->tok
)
691 if (NULL
== n
->prev
->body
->child
)
696 term_fontrepl(p
, TERMFONT_BOLD
);
700 p
->offset
= mt
->offset
;
732 const struct man_node
*nn
;
745 if (NULL
== (nn
= n
->parent
->head
->child
)) {
746 mt
->offset
= mt
->lmargin
+ INDENT
;
747 p
->offset
= mt
->offset
;
751 if ((ival
= a2width(nn
)) < 0)
754 mt
->offset
= INDENT
+ (size_t)ival
;
755 p
->offset
= mt
->offset
;
768 mt
->offset
= mt
->lmargin
= INDENT
;
779 print_man_node(DECL_ARGS
)
787 if (0 == *n
->string
) {
792 term_word(p
, n
->string
);
794 /* FIXME: this means that macro lines are munged! */
796 if (MANT_LITERAL
& mt
->fl
) {
797 p
->flags
|= TERMP_NOSPACE
;
802 term_fontrepl(p
, TERMFONT_NONE
);
803 if (termacts
[n
->tok
].pre
)
804 c
= (*termacts
[n
->tok
].pre
)(p
, mt
, n
, m
);
809 print_man_nodelist(p
, mt
, n
->child
, m
);
811 if (MAN_TEXT
!= n
->type
) {
812 if (termacts
[n
->tok
].post
)
813 (*termacts
[n
->tok
].post
)(p
, mt
, n
, m
);
814 term_fontrepl(p
, TERMFONT_NONE
);
820 print_man_nodelist(DECL_ARGS
)
823 print_man_node(p
, mt
, n
, m
);
826 print_man_nodelist(p
, mt
, n
->next
, m
);
831 print_man_foot(struct termp
*p
, const struct man_meta
*meta
)
835 term_fontrepl(p
, TERMFONT_NONE
);
837 time2a(meta
->date
, buf
, DATESIZ
);
841 p
->flags
|= TERMP_NOSPACE
| TERMP_NOBREAK
;
842 p
->rmargin
= p
->maxrmargin
- strlen(buf
);
846 term_word(p
, meta
->source
);
851 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
852 p
->offset
= p
->rmargin
;
853 p
->rmargin
= p
->maxrmargin
;
854 p
->flags
&= ~TERMP_NOBREAK
;
862 print_man_head(struct termp
*p
, const struct man_meta
*m
)
864 char buf
[BUFSIZ
], title
[BUFSIZ
];
866 p
->rmargin
= p
->maxrmargin
;
868 buf
[0] = title
[0] = '\0';
871 strlcpy(buf
, m
->vol
, BUFSIZ
);
873 snprintf(title
, BUFSIZ
, "%s(%d)", m
->title
, m
->msec
);
876 p
->rmargin
= (p
->maxrmargin
- strlen(buf
) + 1) / 2;
877 p
->flags
|= TERMP_NOBREAK
| TERMP_NOSPACE
;
882 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
883 p
->offset
= p
->rmargin
;
884 p
->rmargin
= p
->maxrmargin
- strlen(title
);
889 p
->offset
= p
->rmargin
;
890 p
->rmargin
= p
->maxrmargin
;
891 p
->flags
&= ~TERMP_NOBREAK
;
892 p
->flags
|= TERMP_NOLPAD
| TERMP_NOSPACE
;
897 p
->rmargin
= p
->maxrmargin
;
899 p
->flags
&= ~TERMP_NOSPACE
;