1 /* $Id: man_term.c,v 1.127 2012/01/03 15:16:24 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 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 */
38 /* FIXME: have PD set the default vspace width. */
42 #define MANT_LITERAL (1 << 0)
43 size_t lmargin[MAXMARGINS]; /* margins (incl. visible page) */
44 int lmargincur; /* index of current margin */
45 int lmarginsz; /* actual number of nested margins */
46 size_t offset; /* default offset to visible page */
49 #define DECL_ARGS struct termp *p, \
51 const struct man_node *n, \
52 const struct man_meta *m
55 int (*pre)(DECL_ARGS);
56 void (*post)(DECL_ARGS);
58 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
61 static int a2width(const struct termp *, const char *);
62 static size_t a2height(const struct termp *, const char *);
64 static void print_man_nodelist(DECL_ARGS);
65 static void print_man_node(DECL_ARGS);
66 static void print_man_head(struct termp *, const void *);
67 static void print_man_foot(struct termp *, const void *);
68 static void print_bvspace(struct termp *,
69 const struct man_node *);
71 static int pre_B(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_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_ign, NULL, 0 }, /* 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 */
135 terminal_man(void *arg, const struct man *man)
138 const struct man_node *n;
139 const struct man_meta *m;
142 p = (struct termp *)arg;
144 if (0 == p->defindent)
148 p->maxrmargin = p->defrmargin;
149 p->tabwidth = term_len(p, 5);
151 if (NULL == p->symtab)
152 p->symtab = mchars_alloc();
157 term_begin(p, print_man_head, print_man_foot, m);
158 p->flags |= TERMP_NOSPACE;
160 memset(&mt, 0, sizeof(struct mtermp));
162 mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
163 mt.offset = term_len(p, p->defindent);
166 print_man_nodelist(p, &mt, n->child, m);
173 a2height(const struct termp *p, const char *cp)
177 if ( ! a2roffsu(cp, &su, SCALE_VS))
178 SCALE_VS_INIT(&su, atoi(cp));
180 return(term_vspan(p, &su));
185 a2width(const struct termp *p, const char *cp)
189 if ( ! a2roffsu(cp, &su, SCALE_BU))
192 return((int)term_hspan(p, &su));
196 * Printing leading vertical space before a block.
197 * This is used for the paragraph macros.
198 * The rules are pretty simple, since there's very little nesting going
199 * on here. Basically, if we're the first within another block (SS/SH),
200 * then don't emit vertical space. If we are (RS), then do. If not the
204 print_bvspace(struct termp *p, const struct man_node *n)
209 if (n->body && n->body->child)
210 if (MAN_TBL == n->body->child->type)
213 if (MAN_ROOT == n->parent->type || MAN_RS != n->parent->tok)
234 term_fontrepl(p, TERMFONT_UNDER);
241 pre_literal(DECL_ARGS)
246 if (MAN_nf == n->tok)
247 mt->fl |= MANT_LITERAL;
249 mt->fl &= ~MANT_LITERAL;
252 * Unlike .IP and .TP, .HP does not have a HEAD.
253 * So in case a second call to term_flushln() is needed,
254 * indentation has to be set up explicitly.
256 if (MAN_HP == n->parent->tok && p->rmargin < p->maxrmargin) {
257 p->offset = p->rmargin;
258 p->rmargin = p->maxrmargin;
259 p->flags &= ~(TERMP_NOBREAK | TERMP_TWOSPACE);
260 p->flags |= TERMP_NOSPACE;
268 pre_alternate(DECL_ARGS)
270 enum termfont font[2];
271 const struct man_node *nn;
276 font[0] = TERMFONT_NONE;
277 font[1] = TERMFONT_BOLD;
280 font[0] = TERMFONT_NONE;
281 font[1] = TERMFONT_UNDER;
284 font[0] = TERMFONT_BOLD;
285 font[1] = TERMFONT_NONE;
288 font[0] = TERMFONT_BOLD;
289 font[1] = TERMFONT_UNDER;
292 font[0] = TERMFONT_UNDER;
293 font[1] = TERMFONT_NONE;
296 font[0] = TERMFONT_UNDER;
297 font[1] = TERMFONT_BOLD;
303 savelit = MANT_LITERAL & mt->fl;
304 mt->fl &= ~MANT_LITERAL;
306 for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
307 term_fontrepl(p, font[i]);
308 if (savelit && NULL == nn->next)
309 mt->fl |= MANT_LITERAL;
310 print_man_node(p, mt, nn, m);
312 p->flags |= TERMP_NOSPACE;
323 term_fontrepl(p, TERMFONT_BOLD);
333 p->flags |= TERMP_NOSPACE;
335 if (NULL != (n = n->child)) {
336 term_fontrepl(p, TERMFONT_BOLD);
337 term_word(p, n->string);
339 if (NULL != n && NULL != n->next) {
340 term_fontrepl(p, TERMFONT_UNDER);
341 term_word(p, n->next->string);
344 term_fontrepl(p, TERMFONT_NONE);
345 p->flags |= TERMP_NOSPACE;
356 if (NULL == n->child) {
361 cp = n->child->string;
368 term_fontrepl(p, TERMFONT_BOLD);
373 term_fontrepl(p, TERMFONT_UNDER);
383 term_fontrepl(p, TERMFONT_NONE);
401 if (NULL == n->child) {
402 p->offset = mt->offset;
406 cp = n->child->string;
416 if ((len = a2width(p, ++cp)) < 0)
422 p->offset -= p->offset > v ? v : p->offset;
428 /* Don't let this creep beyond the right margin. */
430 if (p->offset > p->rmargin)
431 p->offset = p->rmargin;
443 if ((NULL == n->prev && n->parent)) {
444 if (MAN_SS == n->parent->tok)
446 if (MAN_SH == n->parent->tok)
455 len = n->child ? a2height(p, n->child->string) : 1;
461 for (i = 0; i < len; i++)
474 const struct man_node *nn;
481 p->flags |= TERMP_NOBREAK;
482 p->flags |= TERMP_TWOSPACE;
488 len = mt->lmargin[mt->lmargincur];
491 /* Calculate offset. */
493 if (NULL != (nn = n->parent->head->child))
494 if ((ival = a2width(p, nn->string)) >= 0)
497 one = term_len(p, 1);
501 p->offset = mt->offset;
502 p->rmargin = mt->offset + len;
505 mt->lmargin[mt->lmargincur] = (size_t)ival;
522 p->flags &= ~TERMP_NOBREAK;
523 p->flags &= ~TERMP_TWOSPACE;
524 p->offset = mt->offset;
525 p->rmargin = p->maxrmargin;
540 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
544 p->offset = mt->offset;
548 return(MAN_HEAD != n->type);
556 const struct man_node *nn;
562 p->flags |= TERMP_NOSPACE;
565 p->flags |= TERMP_NOBREAK;
574 len = mt->lmargin[mt->lmargincur];
577 /* Calculate the offset from the optional second argument. */
578 if (NULL != (nn = n->parent->head->child))
579 if (NULL != (nn = nn->next))
580 if ((ival = a2width(p, nn->string)) >= 0)
585 /* Handle zero-width lengths. */
587 len = term_len(p, 1);
589 p->offset = mt->offset;
590 p->rmargin = mt->offset + len;
594 /* Set the saved left-margin. */
595 mt->lmargin[mt->lmargincur] = (size_t)ival;
597 savelit = MANT_LITERAL & mt->fl;
598 mt->fl &= ~MANT_LITERAL;
601 print_man_node(p, mt, n->child, m);
604 mt->fl |= MANT_LITERAL;
608 p->offset = mt->offset + len;
609 p->rmargin = p->maxrmargin;
627 p->flags &= ~TERMP_NOBREAK;
628 p->rmargin = p->maxrmargin;
643 const struct man_node *nn;
649 p->flags |= TERMP_NOBREAK;
652 p->flags |= TERMP_NOSPACE;
661 len = (size_t)mt->lmargin[mt->lmargincur];
664 /* Calculate offset. */
666 if (NULL != (nn = n->parent->head->child))
667 if (nn->string && nn->parent->line == nn->line)
668 if ((ival = a2width(p, nn->string)) >= 0)
673 /* Handle zero-length properly. */
675 len = term_len(p, 1);
677 p->offset = mt->offset;
678 p->rmargin = mt->offset + len;
680 savelit = MANT_LITERAL & mt->fl;
681 mt->fl &= ~MANT_LITERAL;
683 /* Don't print same-line elements. */
684 for (nn = n->child; nn; nn = nn->next)
685 if (nn->line > n->line)
686 print_man_node(p, mt, nn, m);
689 mt->fl |= MANT_LITERAL;
691 mt->lmargin[mt->lmargincur] = (size_t)ival;
695 p->offset = mt->offset + len;
696 p->rmargin = p->maxrmargin;
714 p->flags &= ~TERMP_NOBREAK;
715 p->flags &= ~TERMP_TWOSPACE;
716 p->rmargin = p->maxrmargin;
734 mt->fl &= ~MANT_LITERAL;
735 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
736 mt->offset = term_len(p, p->defindent);
737 /* If following a prior empty `SS', no vspace. */
738 if (n->prev && MAN_SS == n->prev->tok)
739 if (NULL == n->prev->body->child)
746 term_fontrepl(p, TERMFONT_BOLD);
747 p->offset = term_len(p, p->defindent/2);
750 p->offset = mt->offset;
785 mt->fl &= ~MANT_LITERAL;
786 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
787 mt->offset = term_len(p, p->defindent);
788 /* If following a prior empty `SH', no vspace. */
789 if (n->prev && MAN_SH == n->prev->tok)
790 if (NULL == n->prev->body->child)
792 /* If the first macro, no vspae. */
798 term_fontrepl(p, TERMFONT_BOLD);
802 p->offset = mt->offset;
846 sz = term_len(p, p->defindent);
848 if (NULL != (n = n->parent->head->child))
849 if ((ival = a2width(p, n->string)) >= 0)
853 p->rmargin = p->maxrmargin;
854 p->offset = mt->offset < p->rmargin ? mt->offset : p->rmargin;
856 if (++mt->lmarginsz < MAXMARGINS)
857 mt->lmargincur = mt->lmarginsz;
859 mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
880 sz = term_len(p, p->defindent);
882 if (NULL != (n = n->parent->head->child))
883 if ((ival = a2width(p, n->string)) >= 0)
886 mt->offset = mt->offset < sz ? 0 : mt->offset - sz;
887 p->offset = mt->offset;
889 if (--mt->lmarginsz < MAXMARGINS)
890 mt->lmargincur = mt->lmarginsz;
894 print_man_node(DECL_ARGS)
902 * If we have a blank line, output a vertical space.
903 * If we have a space as the first character, break
904 * before printing the line's data.
906 if ('\0' == *n->string) {
909 } else if (' ' == *n->string && MAN_LINE & n->flags)
912 term_word(p, n->string);
915 * If we're in a literal context, make sure that words
916 * togehter on the same line stay together. This is a
917 * POST-printing call, so we check the NEXT word. Since
918 * -man doesn't have nested macros, we don't need to be
919 * more specific than this.
921 if (MANT_LITERAL & mt->fl && ! (TERMP_NOBREAK & p->flags) &&
923 n->next->line > n->line)) {
925 rmax = p->maxrmargin;
926 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
927 p->flags |= TERMP_NOSPACE;
930 p->maxrmargin = rmax;
933 if (MAN_EOS & n->flags)
934 p->flags |= TERMP_SENTENCE;
941 * Tables are preceded by a newline. Then process a
942 * table line, which will cause line termination,
944 if (TBL_SPAN_FIRST & n->span->flags)
946 term_tbl(p, n->span);
952 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
953 term_fontrepl(p, TERMFONT_NONE);
956 if (termacts[n->tok].pre)
957 c = (*termacts[n->tok].pre)(p, mt, n, m);
960 print_man_nodelist(p, mt, n->child, m);
962 if (termacts[n->tok].post)
963 (*termacts[n->tok].post)(p, mt, n, m);
964 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
965 term_fontrepl(p, TERMFONT_NONE);
967 if (MAN_EOS & n->flags)
968 p->flags |= TERMP_SENTENCE;
973 print_man_nodelist(DECL_ARGS)
976 print_man_node(p, mt, n, m);
979 print_man_nodelist(p, mt, n->next, m);
984 print_man_foot(struct termp *p, const void *arg)
988 const struct man_meta *meta;
990 meta = (const struct man_meta *)arg;
995 term_fontrepl(p, TERMFONT_NONE);
1000 * Temporary, undocumented option to imitate mdoc(7) output.
1001 * In the bottom right corner, use the source instead of
1005 if ( ! p->mdocstyle) {
1008 snprintf(title, BUFSIZ, "%s(%s)", meta->title, meta->msec);
1009 } else if (meta->source) {
1010 strlcpy(title, meta->source, BUFSIZ);
1014 datelen = term_strlen(p, meta->date);
1016 /* Bottom left corner: manual source. */
1018 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1020 p->rmargin = (p->maxrmargin - datelen + term_len(p, 1)) / 2;
1023 term_word(p, meta->source);
1026 /* At the bottom in the middle: manual date. */
1028 p->flags |= TERMP_NOSPACE;
1029 p->offset = p->rmargin;
1030 p->rmargin = p->maxrmargin - term_strlen(p, title);
1031 if (p->offset + datelen >= p->rmargin)
1032 p->rmargin = p->offset + datelen;
1034 term_word(p, meta->date);
1037 /* Bottom right corner: manual title and section. */
1039 p->flags &= ~TERMP_NOBREAK;
1040 p->flags |= TERMP_NOSPACE;
1041 p->offset = p->rmargin;
1042 p->rmargin = p->maxrmargin;
1044 term_word(p, title);
1050 print_man_head(struct termp *p, const void *arg)
1052 char buf[BUFSIZ], title[BUFSIZ];
1053 size_t buflen, titlen;
1054 const struct man_meta *m;
1056 m = (const struct man_meta *)arg;
1061 strlcpy(buf, m->vol, BUFSIZ);
1064 buflen = term_strlen(p, buf);
1066 /* Top left corner: manual title and section. */
1068 snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1069 titlen = term_strlen(p, title);
1071 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1073 p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1075 term_strlen(p, buf) + term_len(p, 1)) / 2 :
1076 p->maxrmargin - buflen;
1078 term_word(p, title);
1081 /* At the top in the middle: manual volume. */
1083 p->flags |= TERMP_NOSPACE;
1084 p->offset = p->rmargin;
1085 p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
1086 p->maxrmargin - titlen : p->maxrmargin;
1091 /* Top right corner: title and section, again. */
1093 p->flags &= ~TERMP_NOBREAK;
1094 if (p->rmargin + titlen <= p->maxrmargin) {
1095 p->flags |= TERMP_NOSPACE;
1096 p->offset = p->rmargin;
1097 p->rmargin = p->maxrmargin;
1098 term_word(p, title);
1102 p->flags &= ~TERMP_NOSPACE;
1104 p->rmargin = p->maxrmargin;
1107 * Groff prints three blank lines before the content.
1108 * Do the same, except in the temporary, undocumented
1109 * mode imitating mdoc(7) output.
1113 if ( ! p->mdocstyle) {