1 /* $Id: man_term.c,v 1.19 2009/10/27 21:40:07 schwarze 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);
67 static int a2width(const struct man_node *);
68 static int a2height(const struct man_node *);
70 static void print_man_head(struct termp *,
71 const struct man_meta *);
72 static void print_man_body(DECL_ARGS);
73 static void print_man_node(DECL_ARGS);
74 static void print_man_foot(struct termp *,
75 const struct man_meta *);
76 static void print_bvspace(struct termp *,
77 const struct man_node *);
79 static int pre_B(DECL_ARGS);
80 static int pre_BI(DECL_ARGS);
81 static int pre_HP(DECL_ARGS);
82 static int pre_I(DECL_ARGS);
83 static int pre_IP(DECL_ARGS);
84 static int pre_IR(DECL_ARGS);
85 static int pre_PP(DECL_ARGS);
86 static int pre_RB(DECL_ARGS);
87 static int pre_RI(DECL_ARGS);
88 static int pre_RS(DECL_ARGS);
89 static int pre_SH(DECL_ARGS);
90 static int pre_SS(DECL_ARGS);
91 static int pre_TP(DECL_ARGS);
92 static int pre_br(DECL_ARGS);
93 static int pre_fi(DECL_ARGS);
94 static int pre_ign(DECL_ARGS);
95 static int pre_nf(DECL_ARGS);
96 static int pre_r(DECL_ARGS);
97 static int pre_sp(DECL_ARGS);
99 static void post_B(DECL_ARGS);
100 static void post_I(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);
107 static void post_i(DECL_ARGS);
109 static const struct termact termacts[MAN_MAX] = {
110 { pre_br, NULL }, /* br */
111 { NULL, NULL }, /* TH */
112 { pre_SH, post_SH }, /* SH */
113 { pre_SS, post_SS }, /* SS */
114 { pre_TP, post_TP }, /* TP */
115 { pre_PP, NULL }, /* LP */
116 { pre_PP, NULL }, /* PP */
117 { pre_PP, NULL }, /* P */
118 { pre_IP, post_IP }, /* IP */
119 { pre_HP, post_HP }, /* HP */
120 { NULL, NULL }, /* SM */
121 { pre_B, post_B }, /* SB */
122 { pre_BI, NULL }, /* BI */
123 { pre_BI, NULL }, /* IB */
124 { pre_RB, NULL }, /* BR */
125 { pre_RB, NULL }, /* RB */
126 { NULL, NULL }, /* R */
127 { pre_B, post_B }, /* B */
128 { pre_I, post_I }, /* I */
129 { pre_IR, NULL }, /* IR */
130 { pre_RI, NULL }, /* RI */
131 { NULL, NULL }, /* na */
132 { pre_I, post_i }, /* i */
133 { pre_sp, NULL }, /* sp */
134 { pre_nf, NULL }, /* nf */
135 { pre_fi, NULL }, /* fi */
136 { pre_r, NULL }, /* r */
137 { NULL, NULL }, /* RE */
138 { pre_RS, post_RS }, /* RS */
139 { pre_ign, NULL }, /* DT */
140 { pre_ign, NULL }, /* UC */
141 { pre_ign, NULL }, /* PD */
147 terminal_man(void *arg, const struct man *man)
150 const struct man_node *n;
151 const struct man_meta *m;
154 p = (struct termp *)arg;
156 if (NULL == p->symtab)
158 case (TERMENC_ASCII):
159 p->symtab = chars_init(CHARS_ASCII);
169 print_man_head(p, m);
170 p->flags |= TERMP_NOSPACE;
177 print_man_body(p, &mt, n->child, m);
178 print_man_foot(p, m);
183 a2height(const struct man_node *n)
187 assert(MAN_TEXT == n->type);
189 if ( ! a2roffsu(n->string, &su, SCALE_VS))
190 SCALE_VS_INIT(&su, strlen(n->string));
192 return((int)term_vspan(&su));
197 a2width(const struct man_node *n)
201 assert(MAN_TEXT == n->type);
203 if ( ! a2roffsu(n->string, &su, SCALE_BU))
206 return((int)term_hspan(&su));
211 print_bvspace(struct termp *p, const struct man_node *n)
218 if (MAN_SS == n->prev->tok)
220 if (MAN_SH == n->prev->tok)
251 p->bold = p->under = 0;
280 mt->fl &= ~MANT_LITERAL;
291 mt->fl |= MANT_LITERAL;
300 const struct man_node *nn;
303 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
307 p->flags |= TERMP_NOSPACE;
308 print_man_node(p, mt, nn, m);
320 const struct man_node *nn;
323 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
324 if (i % 2 && MAN_RB == n->tok)
326 else if ( ! (i % 2) && MAN_RB != n->tok)
330 p->flags |= TERMP_NOSPACE;
332 print_man_node(p, mt, nn, m);
334 if (i % 2 && MAN_RB == n->tok)
336 else if ( ! (i % 2) && MAN_RB != n->tok)
347 const struct man_node *nn;
350 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
354 p->flags |= TERMP_NOSPACE;
355 print_man_node(p, mt, nn, m);
367 const struct man_node *nn;
370 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
371 if (i % 2 && MAN_BI == n->tok)
375 else if (MAN_BI == n->tok)
381 p->flags |= TERMP_NOSPACE;
382 print_man_node(p, mt, nn, m);
384 if (i % 2 && MAN_BI == n->tok)
388 else if (MAN_BI == n->tok)
422 len = n->child ? a2height(n->child) : 1;
426 for (i = 0; i < len; i++)
449 const struct man_node *nn;
456 p->flags |= TERMP_NOBREAK;
457 p->flags |= TERMP_TWOSPACE;
466 /* Calculate offset. */
468 if (NULL != (nn = n->parent->head->child))
469 if ((ival = a2width(nn)) >= 0)
475 p->offset = mt->offset;
476 p->rmargin = mt->offset + len;
479 mt->lmargin = (size_t)ival;
496 p->flags &= ~TERMP_NOBREAK;
497 p->flags &= ~TERMP_TWOSPACE;
498 p->offset = mt->offset;
499 p->rmargin = p->maxrmargin;
514 mt->lmargin = INDENT;
518 p->offset = mt->offset;
530 const struct man_node *nn;
536 p->flags |= TERMP_NOLPAD;
537 p->flags |= TERMP_NOSPACE;
540 p->flags |= TERMP_NOBREAK;
541 p->flags |= TERMP_TWOSPACE;
553 /* Calculate offset. */
555 if (NULL != (nn = n->parent->head->child))
556 if (NULL != (nn = nn->next)) {
557 for ( ; nn->next; nn = nn->next)
559 if ((ival = a2width(nn)) >= 0)
565 /* Handle zero-width lengths. */
569 p->offset = mt->offset;
570 p->rmargin = mt->offset + len;
574 /* Set the saved left-margin. */
575 mt->lmargin = (size_t)ival;
577 /* Don't print the length value. */
578 for (nn = n->child; nn->next; nn = nn->next)
579 print_man_node(p, mt, nn, m);
582 p->offset = mt->offset + len;
583 p->rmargin = p->maxrmargin;
601 p->flags &= ~TERMP_NOBREAK;
602 p->flags &= ~TERMP_TWOSPACE;
603 p->rmargin = p->maxrmargin;
607 p->flags &= ~TERMP_NOLPAD;
619 const struct man_node *nn;
625 p->flags |= TERMP_NOBREAK;
626 p->flags |= TERMP_TWOSPACE;
629 p->flags |= TERMP_NOLPAD;
630 p->flags |= TERMP_NOSPACE;
639 len = (size_t)mt->lmargin;
642 /* Calculate offset. */
644 if (NULL != (nn = n->parent->head->child))
645 if (NULL != nn->next)
646 if ((ival = a2width(nn)) >= 0)
651 /* Handle zero-length properly. */
655 p->offset = mt->offset;
656 p->rmargin = mt->offset + len;
658 /* Don't print same-line elements. */
659 for (nn = n->child; nn; nn = nn->next)
660 if (nn->line > n->line)
661 print_man_node(p, mt, nn, m);
664 mt->lmargin = (size_t)ival;
668 p->offset = mt->offset + len;
669 p->rmargin = p->maxrmargin;
687 p->flags &= ~TERMP_NOBREAK;
688 p->flags &= ~TERMP_TWOSPACE;
689 p->rmargin = p->maxrmargin;
693 p->flags &= ~TERMP_NOLPAD;
708 mt->lmargin = INDENT;
710 /* If following a prior empty `SS', no vspace. */
711 if (n->prev && MAN_SS == n->prev->tok)
712 if (NULL == n->prev->body->child)
720 p->offset = HALFINDENT;
723 p->offset = mt->offset;
759 mt->lmargin = INDENT;
761 /* If following a prior empty `SH', no vspace. */
762 if (n->prev && MAN_SH == n->prev->tok)
763 if (NULL == n->prev->body->child)
772 p->offset = mt->offset;
805 const struct man_node *nn;
818 if (NULL == (nn = n->parent->head->child)) {
819 mt->offset = mt->lmargin + INDENT;
820 p->offset = mt->offset;
824 if ((ival = a2width(nn)) < 0)
827 mt->offset = INDENT + (size_t)ival;
828 p->offset = mt->offset;
841 mt->offset = mt->lmargin = INDENT;
852 print_man_node(DECL_ARGS)
860 if (0 == *n->string) {
865 * Note! This is hacky. Here, we recognise the `\c'
866 * escape embedded in so many -man pages. It's supposed
867 * to remove the subsequent space, so we mark NOSPACE if
868 * it's encountered in the string.
870 sz = (int)strlen(n->string);
871 term_word(p, n->string);
872 if (sz >= 2 && n->string[sz - 1] == 'c' &&
873 n->string[sz - 2] == '\\')
874 p->flags |= TERMP_NOSPACE;
875 /* FIXME: this means that macro lines are munged! */
876 if (MANT_LITERAL & mt->fl) {
877 p->flags |= TERMP_NOSPACE;
882 if (termacts[n->tok].pre)
883 c = (*termacts[n->tok].pre)(p, mt, n, m);
888 print_man_body(p, mt, n->child, m);
890 if (MAN_TEXT != n->type)
891 if (termacts[n->tok].post)
892 (*termacts[n->tok].post)(p, mt, n, m);
897 print_man_body(DECL_ARGS)
900 print_man_node(p, mt, n, m);
903 print_man_body(p, mt, n->next, m);
908 print_man_foot(struct termp *p, const struct man_meta *meta)
912 time2a(meta->date, buf, DATESIZ);
916 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
917 p->rmargin = p->maxrmargin - strlen(buf);
921 term_word(p, meta->source);
926 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
927 p->offset = p->rmargin;
928 p->rmargin = p->maxrmargin;
929 p->flags &= ~TERMP_NOBREAK;
937 print_man_head(struct termp *p, const struct man_meta *meta)
941 p->rmargin = p->maxrmargin;
944 if (NULL == (buf = malloc(p->rmargin)))
945 err(EXIT_FAILURE, "malloc");
946 if (NULL == (title = malloc(p->rmargin)))
947 err(EXIT_FAILURE, "malloc");
950 (void)strlcpy(buf, meta->vol, p->rmargin);
954 (void)snprintf(title, p->rmargin, "%s(%d)",
955 meta->title, meta->msec);
958 p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
959 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
964 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
965 p->offset = p->rmargin;
966 p->rmargin = p->maxrmargin - strlen(title);
971 p->offset = p->rmargin;
972 p->rmargin = p->maxrmargin;
973 p->flags &= ~TERMP_NOBREAK;
974 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
979 p->rmargin = p->maxrmargin;
981 p->flags &= ~TERMP_NOSPACE;