1 /* $Id: man_term.c,v 1.204 2017/06/08 12:54:58 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015, 2017 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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.
20 #include <sys/types.h>
29 #include "mandoc_aux.h"
37 #define MAXMARGINS 64 /* maximum number of indented scopes */
41 #define MANT_LITERAL (1 << 0)
42 int lmargin[MAXMARGINS]; /* margins (incl. vis. page) */
43 int lmargincur; /* index of current margin */
44 int lmarginsz; /* actual number of nested margins */
45 size_t offset; /* default offset to visible page */
46 int pardist; /* vert. space before par., unit: [v] */
49 #define DECL_ARGS struct termp *p, \
51 struct roff_node *n, \
52 const struct roff_meta *meta
55 int (*pre)(DECL_ARGS);
56 void (*post)(DECL_ARGS);
58 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
61 static void print_man_nodelist(DECL_ARGS);
62 static void print_man_node(DECL_ARGS);
63 static void print_man_head(struct termp *,
64 const struct roff_meta *);
65 static void print_man_foot(struct termp *,
66 const struct roff_meta *);
67 static void print_bvspace(struct termp *,
68 const struct roff_node *, int);
70 static int pre_B(DECL_ARGS);
71 static int pre_DT(DECL_ARGS);
72 static int pre_HP(DECL_ARGS);
73 static int pre_I(DECL_ARGS);
74 static int pre_IP(DECL_ARGS);
75 static int pre_OP(DECL_ARGS);
76 static int pre_PD(DECL_ARGS);
77 static int pre_PP(DECL_ARGS);
78 static int pre_RS(DECL_ARGS);
79 static int pre_SH(DECL_ARGS);
80 static int pre_SS(DECL_ARGS);
81 static int pre_TP(DECL_ARGS);
82 static int pre_UR(DECL_ARGS);
83 static int pre_alternate(DECL_ARGS);
84 static int pre_ign(DECL_ARGS);
85 static int pre_in(DECL_ARGS);
86 static int pre_literal(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);
94 static void post_UR(DECL_ARGS);
96 static const struct termact __termacts[MAN_MAX - MAN_TH] = {
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_literal, NULL, 0 }, /* nf */
118 { pre_literal, NULL, 0 }, /* fi */
119 { NULL, NULL, 0 }, /* RE */
120 { pre_RS, post_RS, 0 }, /* RS */
121 { pre_DT, NULL, 0 }, /* DT */
122 { pre_ign, NULL, MAN_NOTEXT }, /* UC */
123 { pre_PD, NULL, MAN_NOTEXT }, /* PD */
124 { pre_ign, NULL, 0 }, /* AT */
125 { pre_in, NULL, MAN_NOTEXT }, /* in */
126 { pre_OP, NULL, 0 }, /* OP */
127 { pre_literal, NULL, 0 }, /* EX */
128 { pre_literal, NULL, 0 }, /* EE */
129 { pre_UR, post_UR, 0 }, /* UR */
130 { NULL, NULL, 0 }, /* UE */
132 static const struct termact *termacts = __termacts - MAN_TH;
136 terminal_man(void *arg, const struct roff_man *man)
141 size_t save_defindent;
143 p = (struct termp *)arg;
144 p->tcol->rmargin = p->maxrmargin = p->defrmargin;
145 term_tab_set(p, NULL);
146 term_tab_set(p, "T");
147 term_tab_set(p, ".5i");
149 memset(&mt, 0, sizeof(struct mtermp));
150 mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
151 mt.offset = term_len(p, p->defindent);
154 n = man->first->child;
155 if (p->synopsisonly) {
157 if (n->tok == MAN_SH &&
158 n->child->child->type == ROFFT_TEXT &&
159 !strcmp(n->child->child->string, "SYNOPSIS")) {
160 if (n->child->next->child != NULL)
161 print_man_nodelist(p, &mt,
162 n->child->next->child,
170 save_defindent = p->defindent;
171 if (p->defindent == 0)
173 term_begin(p, print_man_head, print_man_foot, &man->meta);
174 p->flags |= TERMP_NOSPACE;
176 print_man_nodelist(p, &mt, n, &man->meta);
178 p->defindent = save_defindent;
183 * Printing leading vertical space before a block.
184 * This is used for the paragraph macros.
185 * The rules are pretty simple, since there's very little nesting going
186 * on here. Basically, if we're the first within another block (SS/SH),
187 * then don't emit vertical space. If we are (RS), then do. If not the
191 print_bvspace(struct termp *p, const struct roff_node *n, int pardist)
197 if (n->body && n->body->child)
198 if (n->body->child->type == ROFFT_TBL)
201 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
205 for (i = 0; i < pardist; i++)
221 term_fontrepl(p, TERMFONT_UNDER);
226 pre_literal(DECL_ARGS)
231 if (n->tok == MAN_nf || n->tok == MAN_EX)
232 mt->fl |= MANT_LITERAL;
234 mt->fl &= ~MANT_LITERAL;
237 * Unlike .IP and .TP, .HP does not have a HEAD.
238 * So in case a second call to term_flushln() is needed,
239 * indentation has to be set up explicitly.
241 if (n->parent->tok == MAN_HP && p->tcol->rmargin < p->maxrmargin) {
242 p->tcol->offset = p->tcol->rmargin;
243 p->tcol->rmargin = p->maxrmargin;
245 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
246 p->flags |= TERMP_NOSPACE;
262 assert(n->type == ROFFT_TEXT);
263 if (a2roffsu(n->string, &su, SCALE_VS) != NULL)
264 mt->pardist = term_vspan(p, &su);
269 pre_alternate(DECL_ARGS)
271 enum termfont font[2];
272 struct roff_node *nn;
277 font[0] = TERMFONT_NONE;
278 font[1] = TERMFONT_BOLD;
281 font[0] = TERMFONT_NONE;
282 font[1] = TERMFONT_UNDER;
285 font[0] = TERMFONT_BOLD;
286 font[1] = TERMFONT_NONE;
289 font[0] = TERMFONT_BOLD;
290 font[1] = TERMFONT_UNDER;
293 font[0] = TERMFONT_UNDER;
294 font[1] = TERMFONT_NONE;
297 font[0] = TERMFONT_UNDER;
298 font[1] = TERMFONT_BOLD;
304 savelit = MANT_LITERAL & mt->fl;
305 mt->fl &= ~MANT_LITERAL;
307 for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
308 term_fontrepl(p, font[i]);
309 if (savelit && NULL == nn->next)
310 mt->fl |= MANT_LITERAL;
311 assert(nn->type == ROFFT_TEXT);
312 term_word(p, nn->string);
313 if (nn->flags & NODE_EOS)
314 p->flags |= TERMP_SENTENCE;
316 p->flags |= TERMP_NOSPACE;
326 term_fontrepl(p, TERMFONT_BOLD);
335 p->flags |= TERMP_NOSPACE;
337 if (NULL != (n = n->child)) {
338 term_fontrepl(p, TERMFONT_BOLD);
339 term_word(p, n->string);
341 if (NULL != n && NULL != n->next) {
342 term_fontrepl(p, TERMFONT_UNDER);
343 term_word(p, n->next->string);
346 term_fontrepl(p, TERMFONT_NONE);
347 p->flags |= TERMP_NOSPACE;
362 if (n->child == NULL) {
363 p->tcol->offset = mt->offset;
367 cp = n->child->string;
377 if (a2roffsu(++cp, &su, SCALE_EN) == NULL)
380 v = (term_hspan(p, &su) + 11) / 24;
383 p->tcol->offset -= p->tcol->offset > v ? v : p->tcol->offset;
385 p->tcol->offset += v;
388 if (p->tcol->offset > SHRT_MAX)
389 p->tcol->offset = term_len(p, p->defindent);
397 term_tab_set(p, NULL);
398 term_tab_set(p, "T");
399 term_tab_set(p, ".5i");
407 const struct roff_node *nn;
412 print_bvspace(p, n, mt->pardist);
420 if ( ! (MANT_LITERAL & mt->fl)) {
421 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
425 /* Calculate offset. */
427 if ((nn = n->parent->head->child) != NULL &&
428 a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
429 len = term_hspan(p, &su) / 24;
430 if (len < 0 && (size_t)(-len) > mt->offset)
432 else if (len > SHRT_MAX)
433 len = term_len(p, p->defindent);
434 mt->lmargin[mt->lmargincur] = len;
436 len = mt->lmargin[mt->lmargincur];
438 p->tcol->offset = mt->offset;
439 p->tcol->rmargin = mt->offset + len;
452 * Compatibility with a groff bug.
453 * The .HP macro uses the undocumented .tag request
454 * which causes a line break and cancels no-space
455 * mode even if there isn't any output.
458 if (n->child == NULL)
461 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
463 p->tcol->offset = mt->offset;
464 p->tcol->rmargin = p->maxrmargin;
477 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
478 print_bvspace(p, n, mt->pardist);
481 p->tcol->offset = mt->offset;
485 return n->type != ROFFT_HEAD;
492 const struct roff_node *nn;
497 p->flags |= TERMP_NOSPACE;
500 p->flags |= TERMP_NOBREAK;
504 print_bvspace(p, n, mt->pardist);
510 /* Calculate the offset from the optional second argument. */
511 if ((nn = n->parent->head->child) != NULL &&
512 (nn = nn->next) != NULL &&
513 a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
514 len = term_hspan(p, &su) / 24;
515 if (len < 0 && (size_t)(-len) > mt->offset)
517 else if (len > SHRT_MAX)
518 len = term_len(p, p->defindent);
519 mt->lmargin[mt->lmargincur] = len;
521 len = mt->lmargin[mt->lmargincur];
525 p->tcol->offset = mt->offset;
526 p->tcol->rmargin = mt->offset + len;
528 savelit = MANT_LITERAL & mt->fl;
529 mt->fl &= ~MANT_LITERAL;
532 print_man_node(p, mt, n->child, meta);
535 mt->fl |= MANT_LITERAL;
539 p->tcol->offset = mt->offset + len;
540 p->tcol->rmargin = p->maxrmargin;
556 p->flags &= ~TERMP_NOBREAK;
558 p->tcol->rmargin = p->maxrmargin;
562 p->tcol->offset = mt->offset;
573 struct roff_node *nn;
578 p->flags |= TERMP_NOBREAK | TERMP_BRTRSP;
582 p->flags |= TERMP_NOSPACE;
585 print_bvspace(p, n, mt->pardist);
591 /* Calculate offset. */
593 if ((nn = n->parent->head->child) != NULL &&
594 nn->string != NULL && ! (NODE_LINE & nn->flags) &&
595 a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
596 len = term_hspan(p, &su) / 24;
597 if (len < 0 && (size_t)(-len) > mt->offset)
599 else if (len > SHRT_MAX)
600 len = term_len(p, p->defindent);
601 mt->lmargin[mt->lmargincur] = len;
603 len = mt->lmargin[mt->lmargincur];
607 p->tcol->offset = mt->offset;
608 p->tcol->rmargin = mt->offset + len;
610 savelit = MANT_LITERAL & mt->fl;
611 mt->fl &= ~MANT_LITERAL;
613 /* Don't print same-line elements. */
615 while (NULL != nn && 0 == (NODE_LINE & nn->flags))
619 print_man_node(p, mt, nn, meta);
624 mt->fl |= MANT_LITERAL;
627 p->tcol->offset = mt->offset + len;
628 p->tcol->rmargin = p->maxrmargin;
630 p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
649 p->tcol->offset = mt->offset;
663 mt->fl &= ~MANT_LITERAL;
664 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
665 mt->offset = term_len(p, p->defindent);
668 * No vertical space before the first subsection
669 * and after an empty subsection.
674 } while (n != NULL && n->tok != TOKEN_NONE &&
675 termacts[n->tok].flags & MAN_NOTEXT);
676 if (n == NULL || (n->tok == MAN_SS && n->body->child == NULL))
679 for (i = 0; i < mt->pardist; i++)
683 term_fontrepl(p, TERMFONT_BOLD);
684 p->tcol->offset = term_len(p, 3);
685 p->tcol->rmargin = mt->offset;
686 p->trailspace = mt->offset;
687 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
690 p->tcol->offset = mt->offset;
691 p->tcol->rmargin = p->maxrmargin;
693 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
725 mt->fl &= ~MANT_LITERAL;
726 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
727 mt->offset = term_len(p, p->defindent);
730 * No vertical space before the first section
731 * and after an empty section.
736 } while (n != NULL && n->tok != TOKEN_NONE &&
737 termacts[n->tok].flags & MAN_NOTEXT);
738 if (n == NULL || (n->tok == MAN_SH && n->body->child == NULL))
741 for (i = 0; i < mt->pardist; i++)
745 term_fontrepl(p, TERMFONT_BOLD);
747 p->tcol->rmargin = mt->offset;
748 p->trailspace = mt->offset;
749 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
752 p->tcol->offset = mt->offset;
753 p->tcol->rmargin = p->maxrmargin;
755 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
796 n->aux = SHRT_MAX + 1;
797 if (n->child == NULL)
798 n->aux = mt->lmargin[mt->lmargincur];
799 else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL)
800 n->aux = term_hspan(p, &su) / 24;
801 if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
802 n->aux = -mt->offset;
803 else if (n->aux > SHRT_MAX)
804 n->aux = term_len(p, p->defindent);
806 mt->offset += n->aux;
807 p->tcol->offset = mt->offset;
808 p->tcol->rmargin = p->maxrmargin;
810 if (++mt->lmarginsz < MAXMARGINS)
811 mt->lmargincur = mt->lmarginsz;
813 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
831 mt->offset -= n->parent->head->aux;
832 p->tcol->offset = mt->offset;
834 if (--mt->lmarginsz < MAXMARGINS)
835 mt->lmargincur = mt->lmarginsz;
842 return n->type != ROFFT_HEAD;
849 if (n->type != ROFFT_BLOCK)
853 p->flags |= TERMP_NOSPACE;
855 if (NULL != n->child->child)
856 print_man_node(p, mt, n->child->child, meta);
858 p->flags |= TERMP_NOSPACE;
863 print_man_node(DECL_ARGS)
870 * If we have a blank line, output a vertical space.
871 * If we have a space as the first character, break
872 * before printing the line's data.
874 if (*n->string == '\0') {
877 } else if (*n->string == ' ' && n->flags & NODE_LINE &&
878 (p->flags & TERMP_NONEWLINE) == 0)
881 term_word(p, n->string);
885 if ( ! (n->flags & NODE_LINE))
886 p->flags |= TERMP_NOSPACE;
888 if (n->next != NULL && ! (n->next->flags & NODE_LINE))
889 p->flags |= TERMP_NOSPACE;
892 if (p->tbl.cols == NULL)
894 term_tbl(p, n->span);
900 if (n->tok < ROFF_MAX) {
905 assert(n->tok >= MAN_TH && n->tok <= MAN_MAX);
906 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
907 term_fontrepl(p, TERMFONT_NONE);
910 if (termacts[n->tok].pre)
911 c = (*termacts[n->tok].pre)(p, mt, n, meta);
914 print_man_nodelist(p, mt, n->child, meta);
916 if (termacts[n->tok].post)
917 (*termacts[n->tok].post)(p, mt, n, meta);
918 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
919 term_fontrepl(p, TERMFONT_NONE);
923 * If we're in a literal context, make sure that words
924 * together on the same line stay together. This is a
925 * POST-printing call, so we check the NEXT word. Since
926 * -man doesn't have nested macros, we don't need to be
927 * more specific than this.
929 if (mt->fl & MANT_LITERAL &&
930 ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
931 (n->next == NULL || n->next->flags & NODE_LINE)) {
932 p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
933 if (n->string != NULL && *n->string != '\0')
937 p->flags &= ~TERMP_BRNEVER;
938 if (p->tcol->rmargin < p->maxrmargin &&
939 n->parent->tok == MAN_HP) {
940 p->tcol->offset = p->tcol->rmargin;
941 p->tcol->rmargin = p->maxrmargin;
944 if (NODE_EOS & n->flags)
945 p->flags |= TERMP_SENTENCE;
950 print_man_nodelist(DECL_ARGS)
954 print_man_node(p, mt, n, meta);
960 print_man_foot(struct termp *p, const struct roff_meta *meta)
963 size_t datelen, titlen;
969 term_fontrepl(p, TERMFONT_NONE);
975 * Temporary, undocumented option to imitate mdoc(7) output.
976 * In the bottom right corner, use the operating system
977 * instead of the title.
980 if ( ! p->mdocstyle) {
985 mandoc_asprintf(&title, "%s(%s)",
986 meta->title, meta->msec);
987 } else if (meta->os) {
988 title = mandoc_strdup(meta->os);
990 title = mandoc_strdup("");
992 datelen = term_strlen(p, meta->date);
994 /* Bottom left corner: operating system. */
996 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
999 p->tcol->rmargin = p->maxrmargin > datelen ?
1000 (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1003 term_word(p, meta->os);
1006 /* At the bottom in the middle: manual date. */
1008 p->tcol->offset = p->tcol->rmargin;
1009 titlen = term_strlen(p, title);
1010 p->tcol->rmargin = p->maxrmargin > titlen ?
1011 p->maxrmargin - titlen : 0;
1012 p->flags |= TERMP_NOSPACE;
1014 term_word(p, meta->date);
1017 /* Bottom right corner: manual title and section. */
1019 p->flags &= ~TERMP_NOBREAK;
1020 p->flags |= TERMP_NOSPACE;
1022 p->tcol->offset = p->tcol->rmargin;
1023 p->tcol->rmargin = p->maxrmargin;
1025 term_word(p, title);
1031 print_man_head(struct termp *p, const struct roff_meta *meta)
1035 size_t vollen, titlen;
1037 assert(meta->title);
1040 volume = NULL == meta->vol ? "" : meta->vol;
1041 vollen = term_strlen(p, volume);
1043 /* Top left corner: manual title and section. */
1045 mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1046 titlen = term_strlen(p, title);
1048 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1050 p->tcol->offset = 0;
1051 p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1052 (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1053 vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1055 term_word(p, title);
1058 /* At the top in the middle: manual volume. */
1060 p->flags |= TERMP_NOSPACE;
1061 p->tcol->offset = p->tcol->rmargin;
1062 p->tcol->rmargin = p->tcol->offset + vollen + titlen <
1063 p->maxrmargin ? p->maxrmargin - titlen : p->maxrmargin;
1065 term_word(p, volume);
1068 /* Top right corner: title and section, again. */
1070 p->flags &= ~TERMP_NOBREAK;
1072 if (p->tcol->rmargin + titlen <= p->maxrmargin) {
1073 p->flags |= TERMP_NOSPACE;
1074 p->tcol->offset = p->tcol->rmargin;
1075 p->tcol->rmargin = p->maxrmargin;
1076 term_word(p, title);
1080 p->flags &= ~TERMP_NOSPACE;
1081 p->tcol->offset = 0;
1082 p->tcol->rmargin = p->maxrmargin;
1085 * Groff prints three blank lines before the content.
1086 * Do the same, except in the temporary, undocumented
1087 * mode imitating mdoc(7) output.
1091 if ( ! p->mdocstyle) {