1 /* $Id: man_term.c,v 1.136 2013/01/05 22:19:12 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
36 #define MAXMARGINS 64 /* maximum number of indented scopes */
40 #define MANT_LITERAL (1 << 0)
41 size_t lmargin[MAXMARGINS]; /* margins (incl. visible page) */
42 int lmargincur; /* index of current margin */
43 int lmarginsz; /* actual number of nested margins */
44 size_t offset; /* default offset to visible page */
45 int pardist; /* vert. space before par., unit: [v] */
48 #define DECL_ARGS struct termp *p, \
50 const struct man_node *n, \
51 const struct man_meta *meta
54 int (*pre)(DECL_ARGS);
55 void (*post)(DECL_ARGS);
57 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
60 static int a2width(const struct termp *, const char *);
61 static size_t a2height(const struct termp *, const char *);
63 static void print_man_nodelist(DECL_ARGS);
64 static void print_man_node(DECL_ARGS);
65 static void print_man_head(struct termp *, const void *);
66 static void print_man_foot(struct termp *, const void *);
67 static void print_bvspace(struct termp *,
68 const struct man_node *, int);
70 static int pre_B(DECL_ARGS);
71 static int pre_HP(DECL_ARGS);
72 static int pre_I(DECL_ARGS);
73 static int pre_IP(DECL_ARGS);
74 static int pre_OP(DECL_ARGS);
75 static int pre_PD(DECL_ARGS);
76 static int pre_PP(DECL_ARGS);
77 static int pre_RS(DECL_ARGS);
78 static int pre_SH(DECL_ARGS);
79 static int pre_SS(DECL_ARGS);
80 static int pre_TP(DECL_ARGS);
81 static int pre_alternate(DECL_ARGS);
82 static int pre_ft(DECL_ARGS);
83 static int pre_ign(DECL_ARGS);
84 static int pre_in(DECL_ARGS);
85 static int pre_literal(DECL_ARGS);
86 static int pre_sp(DECL_ARGS);
88 static void post_IP(DECL_ARGS);
89 static void post_HP(DECL_ARGS);
90 static void post_RS(DECL_ARGS);
91 static void post_SH(DECL_ARGS);
92 static void post_SS(DECL_ARGS);
93 static void post_TP(DECL_ARGS);
95 static const struct termact termacts[MAN_MAX] = {
96 { pre_sp, NULL, MAN_NOTEXT }, /* br */
97 { NULL, NULL, 0 }, /* TH */
98 { pre_SH, post_SH, 0 }, /* SH */
99 { pre_SS, post_SS, 0 }, /* SS */
100 { pre_TP, post_TP, 0 }, /* TP */
101 { pre_PP, NULL, 0 }, /* LP */
102 { pre_PP, NULL, 0 }, /* PP */
103 { pre_PP, NULL, 0 }, /* P */
104 { pre_IP, post_IP, 0 }, /* IP */
105 { pre_HP, post_HP, 0 }, /* HP */
106 { NULL, NULL, 0 }, /* SM */
107 { pre_B, NULL, 0 }, /* SB */
108 { pre_alternate, NULL, 0 }, /* BI */
109 { pre_alternate, NULL, 0 }, /* IB */
110 { pre_alternate, NULL, 0 }, /* BR */
111 { pre_alternate, NULL, 0 }, /* RB */
112 { NULL, NULL, 0 }, /* R */
113 { pre_B, NULL, 0 }, /* B */
114 { pre_I, NULL, 0 }, /* I */
115 { pre_alternate, NULL, 0 }, /* IR */
116 { pre_alternate, NULL, 0 }, /* RI */
117 { pre_ign, NULL, MAN_NOTEXT }, /* na */
118 { pre_sp, NULL, MAN_NOTEXT }, /* sp */
119 { pre_literal, NULL, 0 }, /* nf */
120 { pre_literal, NULL, 0 }, /* fi */
121 { NULL, NULL, 0 }, /* RE */
122 { pre_RS, post_RS, 0 }, /* RS */
123 { pre_ign, NULL, 0 }, /* DT */
124 { pre_ign, NULL, 0 }, /* UC */
125 { pre_PD, NULL, MAN_NOTEXT }, /* PD */
126 { pre_ign, NULL, 0 }, /* AT */
127 { pre_in, NULL, MAN_NOTEXT }, /* in */
128 { pre_ft, NULL, MAN_NOTEXT }, /* ft */
129 { pre_OP, NULL, 0 }, /* OP */
130 { pre_literal, NULL, 0 }, /* EX */
131 { pre_literal, NULL, 0 }, /* EE */
137 terminal_man(void *arg, const struct man *man)
140 const struct man_node *n;
141 const struct man_meta *meta;
144 p = (struct termp *)arg;
146 if (0 == p->defindent)
150 p->maxrmargin = p->defrmargin;
151 p->tabwidth = term_len(p, 5);
153 if (NULL == p->symtab)
154 p->symtab = mchars_alloc();
157 meta = man_meta(man);
159 term_begin(p, print_man_head, print_man_foot, meta);
160 p->flags |= TERMP_NOSPACE;
162 memset(&mt, 0, sizeof(struct mtermp));
164 mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
165 mt.offset = term_len(p, p->defindent);
169 print_man_nodelist(p, &mt, n->child, meta);
176 a2height(const struct termp *p, const char *cp)
180 if ( ! a2roffsu(cp, &su, SCALE_VS))
181 SCALE_VS_INIT(&su, atoi(cp));
183 return(term_vspan(p, &su));
188 a2width(const struct termp *p, const char *cp)
192 if ( ! a2roffsu(cp, &su, SCALE_BU))
195 return((int)term_hspan(p, &su));
199 * Printing leading vertical space before a block.
200 * This is used for the paragraph macros.
201 * The rules are pretty simple, since there's very little nesting going
202 * on here. Basically, if we're the first within another block (SS/SH),
203 * then don't emit vertical space. If we are (RS), then do. If not the
207 print_bvspace(struct termp *p, const struct man_node *n, int pardist)
213 if (n->body && n->body->child)
214 if (MAN_TBL == n->body->child->type)
217 if (MAN_ROOT == n->parent->type || MAN_RS != n->parent->tok)
221 for (i = 0; i < pardist; i++)
239 term_fontrepl(p, TERMFONT_UNDER);
246 pre_literal(DECL_ARGS)
251 if (MAN_nf == n->tok || MAN_EX == n->tok)
252 mt->fl |= MANT_LITERAL;
254 mt->fl &= ~MANT_LITERAL;
257 * Unlike .IP and .TP, .HP does not have a HEAD.
258 * So in case a second call to term_flushln() is needed,
259 * indentation has to be set up explicitly.
261 if (MAN_HP == n->parent->tok && p->rmargin < p->maxrmargin) {
262 p->offset = p->rmargin;
263 p->rmargin = p->maxrmargin;
264 p->flags &= ~(TERMP_NOBREAK | TERMP_TWOSPACE);
265 p->flags |= TERMP_NOSPACE;
281 assert(MAN_TEXT == n->type);
282 mt->pardist = atoi(n->string);
288 pre_alternate(DECL_ARGS)
290 enum termfont font[2];
291 const struct man_node *nn;
296 font[0] = TERMFONT_NONE;
297 font[1] = TERMFONT_BOLD;
300 font[0] = TERMFONT_NONE;
301 font[1] = TERMFONT_UNDER;
304 font[0] = TERMFONT_BOLD;
305 font[1] = TERMFONT_NONE;
308 font[0] = TERMFONT_BOLD;
309 font[1] = TERMFONT_UNDER;
312 font[0] = TERMFONT_UNDER;
313 font[1] = TERMFONT_NONE;
316 font[0] = TERMFONT_UNDER;
317 font[1] = TERMFONT_BOLD;
323 savelit = MANT_LITERAL & mt->fl;
324 mt->fl &= ~MANT_LITERAL;
326 for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
327 term_fontrepl(p, font[i]);
328 if (savelit && NULL == nn->next)
329 mt->fl |= MANT_LITERAL;
330 print_man_node(p, mt, nn, meta);
332 p->flags |= TERMP_NOSPACE;
343 term_fontrepl(p, TERMFONT_BOLD);
353 p->flags |= TERMP_NOSPACE;
355 if (NULL != (n = n->child)) {
356 term_fontrepl(p, TERMFONT_BOLD);
357 term_word(p, n->string);
359 if (NULL != n && NULL != n->next) {
360 term_fontrepl(p, TERMFONT_UNDER);
361 term_word(p, n->next->string);
364 term_fontrepl(p, TERMFONT_NONE);
365 p->flags |= TERMP_NOSPACE;
376 if (NULL == n->child) {
381 cp = n->child->string;
388 term_fontrepl(p, TERMFONT_BOLD);
393 term_fontrepl(p, TERMFONT_UNDER);
403 term_fontrepl(p, TERMFONT_NONE);
421 if (NULL == n->child) {
422 p->offset = mt->offset;
426 cp = n->child->string;
436 if ((len = a2width(p, ++cp)) < 0)
442 p->offset -= p->offset > v ? v : p->offset;
448 /* Don't let this creep beyond the right margin. */
450 if (p->offset > p->rmargin)
451 p->offset = p->rmargin;
465 if ((NULL == n->prev && n->parent)) {
466 switch (n->parent->tok) {
489 if (NULL == n->child) {
493 s = n->child->string;
498 len = a2height(p, s);
507 for (i = 0; i < len; i++)
520 const struct man_node *nn;
524 print_bvspace(p, n, mt->pardist);
532 if ( ! (MANT_LITERAL & mt->fl)) {
533 p->flags |= TERMP_NOBREAK;
534 p->flags |= TERMP_TWOSPACE;
537 len = mt->lmargin[mt->lmargincur];
540 /* Calculate offset. */
542 if (NULL != (nn = n->parent->head->child))
543 if ((ival = a2width(p, nn->string)) >= 0)
546 one = term_len(p, 1);
550 p->offset = mt->offset;
551 p->rmargin = mt->offset + len;
554 mt->lmargin[mt->lmargincur] = (size_t)ival;
568 p->flags &= ~TERMP_NOBREAK;
569 p->flags &= ~TERMP_TWOSPACE;
570 p->offset = mt->offset;
571 p->rmargin = p->maxrmargin;
586 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
587 print_bvspace(p, n, mt->pardist);
590 p->offset = mt->offset;
594 return(MAN_HEAD != n->type);
602 const struct man_node *nn;
608 p->flags |= TERMP_NOSPACE;
611 p->flags |= TERMP_NOBREAK;
614 print_bvspace(p, n, mt->pardist);
620 len = mt->lmargin[mt->lmargincur];
623 /* Calculate the offset from the optional second argument. */
624 if (NULL != (nn = n->parent->head->child))
625 if (NULL != (nn = nn->next))
626 if ((ival = a2width(p, nn->string)) >= 0)
631 /* Handle zero-width lengths. */
633 len = term_len(p, 1);
635 p->offset = mt->offset;
636 p->rmargin = mt->offset + len;
640 /* Set the saved left-margin. */
641 mt->lmargin[mt->lmargincur] = (size_t)ival;
643 savelit = MANT_LITERAL & mt->fl;
644 mt->fl &= ~MANT_LITERAL;
647 print_man_node(p, mt, n->child, meta);
650 mt->fl |= MANT_LITERAL;
654 p->offset = mt->offset + len;
655 p->rmargin = p->maxrmargin;
673 p->flags &= ~TERMP_NOBREAK;
674 p->rmargin = p->maxrmargin;
689 const struct man_node *nn;
695 p->flags |= TERMP_NOBREAK;
698 p->flags |= TERMP_NOSPACE;
701 print_bvspace(p, n, mt->pardist);
707 len = (size_t)mt->lmargin[mt->lmargincur];
710 /* Calculate offset. */
712 if (NULL != (nn = n->parent->head->child))
713 if (nn->string && nn->parent->line == nn->line)
714 if ((ival = a2width(p, nn->string)) >= 0)
719 /* Handle zero-length properly. */
721 len = term_len(p, 1);
723 p->offset = mt->offset;
724 p->rmargin = mt->offset + len;
726 savelit = MANT_LITERAL & mt->fl;
727 mt->fl &= ~MANT_LITERAL;
729 /* Don't print same-line elements. */
730 for (nn = n->child; nn; nn = nn->next)
731 if (nn->line > n->line)
732 print_man_node(p, mt, nn, meta);
735 mt->fl |= MANT_LITERAL;
737 mt->lmargin[mt->lmargincur] = (size_t)ival;
741 p->offset = mt->offset + len;
742 p->rmargin = p->maxrmargin;
743 p->flags &= ~TERMP_NOBREAK;
744 p->flags &= ~TERMP_TWOSPACE;
780 mt->fl &= ~MANT_LITERAL;
781 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
782 mt->offset = term_len(p, p->defindent);
783 /* If following a prior empty `SS', no vspace. */
784 if (n->prev && MAN_SS == n->prev->tok)
785 if (NULL == n->prev->body->child)
789 for (i = 0; i < mt->pardist; i++)
793 term_fontrepl(p, TERMFONT_BOLD);
794 p->offset = term_len(p, 3);
797 p->offset = mt->offset;
833 mt->fl &= ~MANT_LITERAL;
834 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
835 mt->offset = term_len(p, p->defindent);
836 /* If following a prior empty `SH', no vspace. */
837 if (n->prev && MAN_SH == n->prev->tok)
838 if (NULL == n->prev->body->child)
840 /* If the first macro, no vspae. */
843 for (i = 0; i < mt->pardist; i++)
847 term_fontrepl(p, TERMFONT_BOLD);
851 p->offset = mt->offset;
895 sz = term_len(p, p->defindent);
897 if (NULL != (n = n->parent->head->child))
898 if ((ival = a2width(p, n->string)) >= 0)
902 p->rmargin = p->maxrmargin;
903 p->offset = mt->offset < p->rmargin ? mt->offset : p->rmargin;
905 if (++mt->lmarginsz < MAXMARGINS)
906 mt->lmargincur = mt->lmarginsz;
908 mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
929 sz = term_len(p, p->defindent);
931 if (NULL != (n = n->parent->head->child))
932 if ((ival = a2width(p, n->string)) >= 0)
935 mt->offset = mt->offset < sz ? 0 : mt->offset - sz;
936 p->offset = mt->offset;
938 if (--mt->lmarginsz < MAXMARGINS)
939 mt->lmargincur = mt->lmarginsz;
943 print_man_node(DECL_ARGS)
951 * If we have a blank line, output a vertical space.
952 * If we have a space as the first character, break
953 * before printing the line's data.
955 if ('\0' == *n->string) {
958 } else if (' ' == *n->string && MAN_LINE & n->flags)
961 term_word(p, n->string);
969 * Tables are preceded by a newline. Then process a
970 * table line, which will cause line termination,
972 if (TBL_SPAN_FIRST & n->span->flags)
974 term_tbl(p, n->span);
980 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
981 term_fontrepl(p, TERMFONT_NONE);
984 if (termacts[n->tok].pre)
985 c = (*termacts[n->tok].pre)(p, mt, n, meta);
988 print_man_nodelist(p, mt, n->child, meta);
990 if (termacts[n->tok].post)
991 (*termacts[n->tok].post)(p, mt, n, meta);
992 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
993 term_fontrepl(p, TERMFONT_NONE);
997 * If we're in a literal context, make sure that words
998 * together on the same line stay together. This is a
999 * POST-printing call, so we check the NEXT word. Since
1000 * -man doesn't have nested macros, we don't need to be
1001 * more specific than this.
1003 if (MANT_LITERAL & mt->fl && ! (TERMP_NOBREAK & p->flags) &&
1004 (NULL == n->next || n->next->line > n->line)) {
1006 rmax = p->maxrmargin;
1007 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1008 p->flags |= TERMP_NOSPACE;
1009 if (NULL != n->string && '\0' != *n->string)
1013 if (rm < rmax && n->parent->tok == MAN_HP) {
1018 p->maxrmargin = rmax;
1020 if (MAN_EOS & n->flags)
1021 p->flags |= TERMP_SENTENCE;
1026 print_man_nodelist(DECL_ARGS)
1029 print_man_node(p, mt, n, meta);
1032 print_man_nodelist(p, mt, n->next, meta);
1037 print_man_foot(struct termp *p, const void *arg)
1041 const struct man_meta *meta;
1043 meta = (const struct man_meta *)arg;
1044 assert(meta->title);
1048 term_fontrepl(p, TERMFONT_NONE);
1053 * Temporary, undocumented option to imitate mdoc(7) output.
1054 * In the bottom right corner, use the source instead of
1058 if ( ! p->mdocstyle) {
1061 snprintf(title, BUFSIZ, "%s(%s)", meta->title, meta->msec);
1062 } else if (meta->source) {
1063 strlcpy(title, meta->source, BUFSIZ);
1067 datelen = term_strlen(p, meta->date);
1069 /* Bottom left corner: manual source. */
1071 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1073 p->rmargin = (p->maxrmargin - datelen + term_len(p, 1)) / 2;
1076 term_word(p, meta->source);
1079 /* At the bottom in the middle: manual date. */
1081 p->flags |= TERMP_NOSPACE;
1082 p->offset = p->rmargin;
1083 p->rmargin = p->maxrmargin - term_strlen(p, title);
1084 if (p->offset + datelen >= p->rmargin)
1085 p->rmargin = p->offset + datelen;
1087 term_word(p, meta->date);
1090 /* Bottom right corner: manual title and section. */
1092 p->flags &= ~TERMP_NOBREAK;
1093 p->flags |= TERMP_NOSPACE;
1094 p->offset = p->rmargin;
1095 p->rmargin = p->maxrmargin;
1097 term_word(p, title);
1103 print_man_head(struct termp *p, const void *arg)
1105 char buf[BUFSIZ], title[BUFSIZ];
1106 size_t buflen, titlen;
1107 const struct man_meta *meta;
1109 meta = (const struct man_meta *)arg;
1110 assert(meta->title);
1114 strlcpy(buf, meta->vol, BUFSIZ);
1117 buflen = term_strlen(p, buf);
1119 /* Top left corner: manual title and section. */
1121 snprintf(title, BUFSIZ, "%s(%s)", meta->title, meta->msec);
1122 titlen = term_strlen(p, title);
1124 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1126 p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1128 term_strlen(p, buf) + term_len(p, 1)) / 2 :
1129 p->maxrmargin - buflen;
1131 term_word(p, title);
1134 /* At the top in the middle: manual volume. */
1136 p->flags |= TERMP_NOSPACE;
1137 p->offset = p->rmargin;
1138 p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
1139 p->maxrmargin - titlen : p->maxrmargin;
1144 /* Top right corner: title and section, again. */
1146 p->flags &= ~TERMP_NOBREAK;
1147 if (p->rmargin + titlen <= p->maxrmargin) {
1148 p->flags |= TERMP_NOSPACE;
1149 p->offset = p->rmargin;
1150 p->rmargin = p->maxrmargin;
1151 term_word(p, title);
1155 p->flags &= ~TERMP_NOSPACE;
1157 p->rmargin = p->maxrmargin;
1160 * Groff prints three blank lines before the content.
1161 * Do the same, except in the temporary, undocumented
1162 * mode imitating mdoc(7) output.
1166 if ( ! p->mdocstyle) {