1 /* $Id: man_term.c,v 1.47 2009/10/30 18:53:08 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_IR(DECL_ARGS);
89 static int pre_PP(DECL_ARGS);
90 static int pre_RB(DECL_ARGS);
91 static int pre_RI(DECL_ARGS);
92 static int pre_RS(DECL_ARGS);
93 static int pre_SH(DECL_ARGS);
94 static int pre_SS(DECL_ARGS);
95 static int pre_TP(DECL_ARGS);
96 static int pre_br(DECL_ARGS);
97 static int pre_fi(DECL_ARGS);
98 static int pre_ign(DECL_ARGS);
99 static int pre_nf(DECL_ARGS);
100 static int pre_r(DECL_ARGS);
101 static int pre_sp(DECL_ARGS);
103 static void post_B(DECL_ARGS);
104 static void post_I(DECL_ARGS);
105 static void post_IP(DECL_ARGS);
106 static void post_HP(DECL_ARGS);
107 static void post_RS(DECL_ARGS);
108 static void post_SH(DECL_ARGS);
109 static void post_SS(DECL_ARGS);
110 static void post_TP(DECL_ARGS);
111 static void post_i(DECL_ARGS);
113 static const struct termact termacts[MAN_MAX] = {
114 { pre_br, NULL }, /* br */
115 { NULL, NULL }, /* TH */
116 { pre_SH, post_SH }, /* SH */
117 { pre_SS, post_SS }, /* SS */
118 { pre_TP, post_TP }, /* TP */
119 { pre_PP, NULL }, /* LP */
120 { pre_PP, NULL }, /* PP */
121 { pre_PP, NULL }, /* P */
122 { pre_IP, post_IP }, /* IP */
123 { pre_HP, post_HP }, /* HP */
124 { NULL, NULL }, /* SM */
125 { pre_B, post_B }, /* SB */
126 { pre_BI, NULL }, /* BI */
127 { pre_BI, NULL }, /* IB */
128 { pre_RB, NULL }, /* BR */
129 { pre_RB, NULL }, /* RB */
130 { NULL, NULL }, /* R */
131 { pre_B, post_B }, /* B */
132 { pre_I, post_I }, /* I */
133 { pre_IR, NULL }, /* IR */
134 { pre_RI, NULL }, /* RI */
135 { NULL, NULL }, /* na */
136 { pre_I, post_i }, /* i */
137 { pre_sp, NULL }, /* sp */
138 { pre_nf, NULL }, /* nf */
139 { pre_fi, NULL }, /* fi */
140 { pre_r, NULL }, /* r */
141 { NULL, NULL }, /* RE */
142 { pre_RS, post_RS }, /* RS */
143 { pre_ign, NULL }, /* DT */
144 { pre_ign, NULL }, /* UC */
145 { pre_ign, NULL }, /* PD */
151 terminal_man(void *arg, const struct man *man)
154 const struct man_node *n;
155 const struct man_meta *m;
158 p = (struct termp *)arg;
160 if (NULL == p->symtab)
162 case (TERMENC_ASCII):
163 p->symtab = chars_init(CHARS_ASCII);
173 print_man_head(p, m);
174 p->flags |= TERMP_NOSPACE;
181 print_man_body(p, &mt, n->child, m);
182 print_man_foot(p, m);
187 a2height(const struct man_node *n)
191 assert(MAN_TEXT == n->type);
193 if ( ! a2roffsu(n->string, &su, SCALE_VS))
194 SCALE_VS_INIT(&su, strlen(n->string));
196 return((int)term_vspan(&su));
201 a2width(const struct man_node *n)
205 assert(MAN_TEXT == n->type);
207 if ( ! a2roffsu(n->string, &su, SCALE_BU))
210 return((int)term_hspan(&su));
215 print_bvspace(struct termp *p, const struct man_node *n)
222 if (MAN_SS == n->prev->tok)
224 if (MAN_SH == n->prev->tok)
255 p->bold = p->under = 0;
284 mt->fl &= ~MANT_LITERAL;
295 mt->fl |= MANT_LITERAL;
304 const struct man_node *nn;
307 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
311 p->flags |= TERMP_NOSPACE;
312 print_man_node(p, mt, nn, m);
324 const struct man_node *nn;
327 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
328 if (i % 2 && MAN_RB == n->tok)
330 else if ( ! (i % 2) && MAN_RB != n->tok)
334 p->flags |= TERMP_NOSPACE;
336 print_man_node(p, mt, nn, m);
338 if (i % 2 && MAN_RB == n->tok)
340 else if ( ! (i % 2) && MAN_RB != n->tok)
351 const struct man_node *nn;
354 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
358 p->flags |= TERMP_NOSPACE;
359 print_man_node(p, mt, nn, m);
371 const struct man_node *nn;
374 for (i = 0, nn = n->child; nn; nn = nn->next, i++) {
375 if (i % 2 && MAN_BI == n->tok)
379 else if (MAN_BI == n->tok)
385 p->flags |= TERMP_NOSPACE;
386 print_man_node(p, mt, nn, m);
388 if (i % 2 && MAN_BI == n->tok)
392 else if (MAN_BI == n->tok)
426 len = n->child ? a2height(n->child) : 1;
430 for (i = 0; i < len; i++)
453 const struct man_node *nn;
460 p->flags |= TERMP_NOBREAK;
461 p->flags |= TERMP_TWOSPACE;
470 /* Calculate offset. */
472 if (NULL != (nn = n->parent->head->child))
473 if ((ival = a2width(nn)) >= 0)
479 p->offset = mt->offset;
480 p->rmargin = mt->offset + len;
483 mt->lmargin = (size_t)ival;
500 p->flags &= ~TERMP_NOBREAK;
501 p->flags &= ~TERMP_TWOSPACE;
502 p->offset = mt->offset;
503 p->rmargin = p->maxrmargin;
518 mt->lmargin = INDENT;
522 p->offset = mt->offset;
534 const struct man_node *nn;
540 p->flags |= TERMP_NOLPAD;
541 p->flags |= TERMP_NOSPACE;
544 p->flags |= TERMP_NOBREAK;
545 p->flags |= TERMP_TWOSPACE;
557 /* Calculate offset. */
559 if (NULL != (nn = n->parent->head->child))
560 if (NULL != (nn = nn->next)) {
561 for ( ; nn->next; nn = nn->next)
563 if ((ival = a2width(nn)) >= 0)
569 /* Handle zero-width lengths. */
573 p->offset = mt->offset;
574 p->rmargin = mt->offset + len;
578 /* Set the saved left-margin. */
579 mt->lmargin = (size_t)ival;
581 /* Don't print the length value. */
582 for (nn = n->child; nn->next; nn = nn->next)
583 print_man_node(p, mt, nn, m);
586 p->offset = mt->offset + len;
587 p->rmargin = p->maxrmargin;
605 p->flags &= ~TERMP_NOBREAK;
606 p->flags &= ~TERMP_TWOSPACE;
607 p->rmargin = p->maxrmargin;
611 p->flags &= ~TERMP_NOLPAD;
623 const struct man_node *nn;
629 p->flags |= TERMP_NOBREAK;
630 p->flags |= TERMP_TWOSPACE;
633 p->flags |= TERMP_NOLPAD;
634 p->flags |= TERMP_NOSPACE;
643 len = (size_t)mt->lmargin;
646 /* Calculate offset. */
648 if (NULL != (nn = n->parent->head->child))
649 if (NULL != nn->next)
650 if ((ival = a2width(nn)) >= 0)
655 /* Handle zero-length properly. */
659 p->offset = mt->offset;
660 p->rmargin = mt->offset + len;
662 /* Don't print same-line elements. */
663 for (nn = n->child; nn; nn = nn->next)
664 if (nn->line > n->line)
665 print_man_node(p, mt, nn, m);
668 mt->lmargin = (size_t)ival;
672 p->offset = mt->offset + len;
673 p->rmargin = p->maxrmargin;
691 p->flags &= ~TERMP_NOBREAK;
692 p->flags &= ~TERMP_TWOSPACE;
693 p->rmargin = p->maxrmargin;
697 p->flags &= ~TERMP_NOLPAD;
712 mt->lmargin = INDENT;
714 /* If following a prior empty `SS', no vspace. */
715 if (n->prev && MAN_SS == n->prev->tok)
716 if (NULL == n->prev->body->child)
724 p->offset = HALFINDENT;
727 p->offset = mt->offset;
763 mt->lmargin = INDENT;
765 /* If following a prior empty `SH', no vspace. */
766 if (n->prev && MAN_SH == n->prev->tok)
767 if (NULL == n->prev->body->child)
776 p->offset = mt->offset;
809 const struct man_node *nn;
822 if (NULL == (nn = n->parent->head->child)) {
823 mt->offset = mt->lmargin + INDENT;
824 p->offset = mt->offset;
828 if ((ival = a2width(nn)) < 0)
831 mt->offset = INDENT + (size_t)ival;
832 p->offset = mt->offset;
845 mt->offset = mt->lmargin = INDENT;
856 print_man_node(DECL_ARGS)
864 if (0 == *n->string) {
869 * Note! This is hacky. Here, we recognise the `\c'
870 * escape embedded in so many -man pages. It's supposed
871 * to remove the subsequent space, so we mark NOSPACE if
872 * it's encountered in the string.
874 sz = (int)strlen(n->string);
875 term_word(p, n->string);
876 if (sz >= 2 && n->string[sz - 1] == 'c' &&
877 n->string[sz - 2] == '\\')
878 p->flags |= TERMP_NOSPACE;
879 /* FIXME: this means that macro lines are munged! */
880 if (MANT_LITERAL & mt->fl) {
881 p->flags |= TERMP_NOSPACE;
886 if (termacts[n->tok].pre)
887 c = (*termacts[n->tok].pre)(p, mt, n, m);
892 print_man_body(p, mt, n->child, m);
894 if (MAN_TEXT != n->type)
895 if (termacts[n->tok].post)
896 (*termacts[n->tok].post)(p, mt, n, m);
901 print_man_body(DECL_ARGS)
904 print_man_node(p, mt, n, m);
907 print_man_body(p, mt, n->next, m);
912 print_man_foot(struct termp *p, const struct man_meta *meta)
916 time2a(meta->date, buf, DATESIZ);
920 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
921 p->rmargin = p->maxrmargin - strlen(buf);
925 term_word(p, meta->source);
930 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
931 p->offset = p->rmargin;
932 p->rmargin = p->maxrmargin;
933 p->flags &= ~TERMP_NOBREAK;
941 print_man_head(struct termp *p, const struct man_meta *m)
943 char buf[BUFSIZ], title[BUFSIZ];
945 p->rmargin = p->maxrmargin;
947 buf[0] = title[0] = '\0';
950 strlcpy(buf, m->vol, BUFSIZ);
952 snprintf(title, BUFSIZ, "%s(%d)", m->title, m->msec);
955 p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
956 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
961 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
962 p->offset = p->rmargin;
963 p->rmargin = p->maxrmargin - strlen(title);
968 p->offset = p->rmargin;
969 p->rmargin = p->maxrmargin;
970 p->flags &= ~TERMP_NOBREAK;
971 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
976 p->rmargin = p->maxrmargin;
978 p->flags &= ~TERMP_NOSPACE;