]> git.cameronkatri.com Git - mandoc.git/blob - man_term.c
dc424128ba179e2898c95515f57438bd8f6e0c84
[mandoc.git] / man_term.c
1 /* $Id: man_term.c,v 1.180 2015/04/18 16:06:40 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
5 *
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.
9 *
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.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "mandoc_aux.h"
30 #include "mandoc.h"
31 #include "roff.h"
32 #include "man.h"
33 #include "out.h"
34 #include "term.h"
35 #include "main.h"
36
37 #define MAXMARGINS 64 /* maximum number of indented scopes */
38
39 struct mtermp {
40 int fl;
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] */
47 };
48
49 #define DECL_ARGS struct termp *p, \
50 struct mtermp *mt, \
51 struct roff_node *n, \
52 const struct roff_meta *meta
53
54 struct termact {
55 int (*pre)(DECL_ARGS);
56 void (*post)(DECL_ARGS);
57 int flags;
58 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
59 };
60
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);
69
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_UR(DECL_ARGS);
82 static int pre_alternate(DECL_ARGS);
83 static int pre_ft(DECL_ARGS);
84 static int pre_ign(DECL_ARGS);
85 static int pre_in(DECL_ARGS);
86 static int pre_literal(DECL_ARGS);
87 static int pre_ll(DECL_ARGS);
88 static int pre_sp(DECL_ARGS);
89
90 static void post_IP(DECL_ARGS);
91 static void post_HP(DECL_ARGS);
92 static void post_RS(DECL_ARGS);
93 static void post_SH(DECL_ARGS);
94 static void post_SS(DECL_ARGS);
95 static void post_TP(DECL_ARGS);
96 static void post_UR(DECL_ARGS);
97
98 static const struct termact termacts[MAN_MAX] = {
99 { pre_sp, NULL, MAN_NOTEXT }, /* br */
100 { NULL, NULL, 0 }, /* TH */
101 { pre_SH, post_SH, 0 }, /* SH */
102 { pre_SS, post_SS, 0 }, /* SS */
103 { pre_TP, post_TP, 0 }, /* TP */
104 { pre_PP, NULL, 0 }, /* LP */
105 { pre_PP, NULL, 0 }, /* PP */
106 { pre_PP, NULL, 0 }, /* P */
107 { pre_IP, post_IP, 0 }, /* IP */
108 { pre_HP, post_HP, 0 }, /* HP */
109 { NULL, NULL, 0 }, /* SM */
110 { pre_B, NULL, 0 }, /* SB */
111 { pre_alternate, NULL, 0 }, /* BI */
112 { pre_alternate, NULL, 0 }, /* IB */
113 { pre_alternate, NULL, 0 }, /* BR */
114 { pre_alternate, NULL, 0 }, /* RB */
115 { NULL, NULL, 0 }, /* R */
116 { pre_B, NULL, 0 }, /* B */
117 { pre_I, NULL, 0 }, /* I */
118 { pre_alternate, NULL, 0 }, /* IR */
119 { pre_alternate, NULL, 0 }, /* RI */
120 { pre_sp, NULL, MAN_NOTEXT }, /* sp */
121 { pre_literal, NULL, 0 }, /* nf */
122 { pre_literal, NULL, 0 }, /* fi */
123 { NULL, NULL, 0 }, /* RE */
124 { pre_RS, post_RS, 0 }, /* RS */
125 { pre_ign, NULL, 0 }, /* DT */
126 { pre_ign, NULL, MAN_NOTEXT }, /* UC */
127 { pre_PD, NULL, MAN_NOTEXT }, /* PD */
128 { pre_ign, NULL, 0 }, /* AT */
129 { pre_in, NULL, MAN_NOTEXT }, /* in */
130 { pre_ft, NULL, MAN_NOTEXT }, /* ft */
131 { pre_OP, NULL, 0 }, /* OP */
132 { pre_literal, NULL, 0 }, /* EX */
133 { pre_literal, NULL, 0 }, /* EE */
134 { pre_UR, post_UR, 0 }, /* UR */
135 { NULL, NULL, 0 }, /* UE */
136 { pre_ll, NULL, MAN_NOTEXT }, /* ll */
137 };
138
139
140 void
141 terminal_man(void *arg, const struct roff_man *man)
142 {
143 struct termp *p;
144 const struct roff_meta *meta;
145 struct roff_node *n;
146 struct mtermp mt;
147
148 p = (struct termp *)arg;
149
150 p->overstep = 0;
151 p->rmargin = p->maxrmargin = p->defrmargin;
152 p->tabwidth = term_len(p, 5);
153
154 n = man_node(man)->child;
155 meta = man_meta(man);
156
157 memset(&mt, 0, sizeof(struct mtermp));
158
159 mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
160 mt.offset = term_len(p, p->defindent);
161 mt.pardist = 1;
162
163 if (p->synopsisonly) {
164 while (n != NULL) {
165 if (n->tok == MAN_SH &&
166 n->child->child->type == ROFFT_TEXT &&
167 !strcmp(n->child->child->string, "SYNOPSIS")) {
168 if (n->child->next->child != NULL)
169 print_man_nodelist(p, &mt,
170 n->child->next->child, meta);
171 term_newln(p);
172 break;
173 }
174 n = n->next;
175 }
176 } else {
177 if (p->defindent == 0)
178 p->defindent = 7;
179 term_begin(p, print_man_head, print_man_foot, meta);
180 p->flags |= TERMP_NOSPACE;
181 if (n != NULL)
182 print_man_nodelist(p, &mt, n, meta);
183 term_end(p);
184 }
185 }
186
187 /*
188 * Printing leading vertical space before a block.
189 * This is used for the paragraph macros.
190 * The rules are pretty simple, since there's very little nesting going
191 * on here. Basically, if we're the first within another block (SS/SH),
192 * then don't emit vertical space. If we are (RS), then do. If not the
193 * first, print it.
194 */
195 static void
196 print_bvspace(struct termp *p, const struct roff_node *n, int pardist)
197 {
198 int i;
199
200 term_newln(p);
201
202 if (n->body && n->body->child)
203 if (n->body->child->type == ROFFT_TBL)
204 return;
205
206 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
207 if (NULL == n->prev)
208 return;
209
210 for (i = 0; i < pardist; i++)
211 term_vspace(p);
212 }
213
214
215 static int
216 pre_ign(DECL_ARGS)
217 {
218
219 return(0);
220 }
221
222 static int
223 pre_ll(DECL_ARGS)
224 {
225
226 term_setwidth(p, n->nchild ? n->child->string : NULL);
227 return(0);
228 }
229
230 static int
231 pre_I(DECL_ARGS)
232 {
233
234 term_fontrepl(p, TERMFONT_UNDER);
235 return(1);
236 }
237
238 static int
239 pre_literal(DECL_ARGS)
240 {
241
242 term_newln(p);
243
244 if (MAN_nf == n->tok || MAN_EX == n->tok)
245 mt->fl |= MANT_LITERAL;
246 else
247 mt->fl &= ~MANT_LITERAL;
248
249 /*
250 * Unlike .IP and .TP, .HP does not have a HEAD.
251 * So in case a second call to term_flushln() is needed,
252 * indentation has to be set up explicitly.
253 */
254 if (MAN_HP == n->parent->tok && p->rmargin < p->maxrmargin) {
255 p->offset = p->rmargin;
256 p->rmargin = p->maxrmargin;
257 p->trailspace = 0;
258 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
259 p->flags |= TERMP_NOSPACE;
260 }
261
262 return(0);
263 }
264
265 static int
266 pre_PD(DECL_ARGS)
267 {
268 struct roffsu su;
269
270 n = n->child;
271 if (n == NULL) {
272 mt->pardist = 1;
273 return(0);
274 }
275 assert(n->type == ROFFT_TEXT);
276 if (a2roffsu(n->string, &su, SCALE_VS))
277 mt->pardist = term_vspan(p, &su);
278 return(0);
279 }
280
281 static int
282 pre_alternate(DECL_ARGS)
283 {
284 enum termfont font[2];
285 struct roff_node *nn;
286 int savelit, i;
287
288 switch (n->tok) {
289 case MAN_RB:
290 font[0] = TERMFONT_NONE;
291 font[1] = TERMFONT_BOLD;
292 break;
293 case MAN_RI:
294 font[0] = TERMFONT_NONE;
295 font[1] = TERMFONT_UNDER;
296 break;
297 case MAN_BR:
298 font[0] = TERMFONT_BOLD;
299 font[1] = TERMFONT_NONE;
300 break;
301 case MAN_BI:
302 font[0] = TERMFONT_BOLD;
303 font[1] = TERMFONT_UNDER;
304 break;
305 case MAN_IR:
306 font[0] = TERMFONT_UNDER;
307 font[1] = TERMFONT_NONE;
308 break;
309 case MAN_IB:
310 font[0] = TERMFONT_UNDER;
311 font[1] = TERMFONT_BOLD;
312 break;
313 default:
314 abort();
315 }
316
317 savelit = MANT_LITERAL & mt->fl;
318 mt->fl &= ~MANT_LITERAL;
319
320 for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
321 term_fontrepl(p, font[i]);
322 if (savelit && NULL == nn->next)
323 mt->fl |= MANT_LITERAL;
324 assert(nn->type == ROFFT_TEXT);
325 term_word(p, nn->string);
326 if (nn->flags & MAN_EOS)
327 p->flags |= TERMP_SENTENCE;
328 if (nn->next)
329 p->flags |= TERMP_NOSPACE;
330 }
331
332 return(0);
333 }
334
335 static int
336 pre_B(DECL_ARGS)
337 {
338
339 term_fontrepl(p, TERMFONT_BOLD);
340 return(1);
341 }
342
343 static int
344 pre_OP(DECL_ARGS)
345 {
346
347 term_word(p, "[");
348 p->flags |= TERMP_NOSPACE;
349
350 if (NULL != (n = n->child)) {
351 term_fontrepl(p, TERMFONT_BOLD);
352 term_word(p, n->string);
353 }
354 if (NULL != n && NULL != n->next) {
355 term_fontrepl(p, TERMFONT_UNDER);
356 term_word(p, n->next->string);
357 }
358
359 term_fontrepl(p, TERMFONT_NONE);
360 p->flags |= TERMP_NOSPACE;
361 term_word(p, "]");
362 return(0);
363 }
364
365 static int
366 pre_ft(DECL_ARGS)
367 {
368 const char *cp;
369
370 if (NULL == n->child) {
371 term_fontlast(p);
372 return(0);
373 }
374
375 cp = n->child->string;
376 switch (*cp) {
377 case '4':
378 /* FALLTHROUGH */
379 case '3':
380 /* FALLTHROUGH */
381 case 'B':
382 term_fontrepl(p, TERMFONT_BOLD);
383 break;
384 case '2':
385 /* FALLTHROUGH */
386 case 'I':
387 term_fontrepl(p, TERMFONT_UNDER);
388 break;
389 case 'P':
390 term_fontlast(p);
391 break;
392 case '1':
393 /* FALLTHROUGH */
394 case 'C':
395 /* FALLTHROUGH */
396 case 'R':
397 term_fontrepl(p, TERMFONT_NONE);
398 break;
399 default:
400 break;
401 }
402 return(0);
403 }
404
405 static int
406 pre_in(DECL_ARGS)
407 {
408 struct roffsu su;
409 const char *cp;
410 size_t v;
411 int less;
412
413 term_newln(p);
414
415 if (NULL == n->child) {
416 p->offset = mt->offset;
417 return(0);
418 }
419
420 cp = n->child->string;
421 less = 0;
422
423 if ('-' == *cp)
424 less = -1;
425 else if ('+' == *cp)
426 less = 1;
427 else
428 cp--;
429
430 if ( ! a2roffsu(++cp, &su, SCALE_EN))
431 return(0);
432
433 v = (term_hspan(p, &su) + 11) / 24;
434
435 if (less < 0)
436 p->offset -= p->offset > v ? v : p->offset;
437 else if (less > 0)
438 p->offset += v;
439 else
440 p->offset = v;
441 if (p->offset > SHRT_MAX)
442 p->offset = term_len(p, p->defindent);
443
444 return(0);
445 }
446
447 static int
448 pre_sp(DECL_ARGS)
449 {
450 struct roffsu su;
451 int i, len;
452
453 if ((NULL == n->prev && n->parent)) {
454 switch (n->parent->tok) {
455 case MAN_SH:
456 /* FALLTHROUGH */
457 case MAN_SS:
458 /* FALLTHROUGH */
459 case MAN_PP:
460 /* FALLTHROUGH */
461 case MAN_LP:
462 /* FALLTHROUGH */
463 case MAN_P:
464 /* FALLTHROUGH */
465 return(0);
466 default:
467 break;
468 }
469 }
470
471 if (n->tok == MAN_br)
472 len = 0;
473 else if (n->child == NULL)
474 len = 1;
475 else {
476 if ( ! a2roffsu(n->child->string, &su, SCALE_VS))
477 su.scale = 1.0;
478 len = term_vspan(p, &su);
479 }
480
481 if (len == 0)
482 term_newln(p);
483 else if (len < 0)
484 p->skipvsp -= len;
485 else
486 for (i = 0; i < len; i++)
487 term_vspace(p);
488
489 return(0);
490 }
491
492 static int
493 pre_HP(DECL_ARGS)
494 {
495 struct roffsu su;
496 const struct roff_node *nn;
497 int len;
498
499 switch (n->type) {
500 case ROFFT_BLOCK:
501 print_bvspace(p, n, mt->pardist);
502 return(1);
503 case ROFFT_BODY:
504 break;
505 default:
506 return(0);
507 }
508
509 if ( ! (MANT_LITERAL & mt->fl)) {
510 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
511 p->trailspace = 2;
512 }
513
514 /* Calculate offset. */
515
516 if ((nn = n->parent->head->child) != NULL &&
517 a2roffsu(nn->string, &su, SCALE_EN)) {
518 len = term_hspan(p, &su) / 24;
519 if (len < 0 && (size_t)(-len) > mt->offset)
520 len = -mt->offset;
521 else if (len > SHRT_MAX)
522 len = term_len(p, p->defindent);
523 mt->lmargin[mt->lmargincur] = len;
524 } else
525 len = mt->lmargin[mt->lmargincur];
526
527 p->offset = mt->offset;
528 p->rmargin = mt->offset + len;
529 return(1);
530 }
531
532 static void
533 post_HP(DECL_ARGS)
534 {
535
536 switch (n->type) {
537 case ROFFT_BODY:
538 term_newln(p);
539
540 /*
541 * Compatibility with a groff bug.
542 * The .HP macro uses the undocumented .tag request
543 * which causes a line break and cancels no-space
544 * mode even if there isn't any output.
545 */
546
547 if (n->child == NULL)
548 term_vspace(p);
549
550 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
551 p->trailspace = 0;
552 p->offset = mt->offset;
553 p->rmargin = p->maxrmargin;
554 break;
555 default:
556 break;
557 }
558 }
559
560 static int
561 pre_PP(DECL_ARGS)
562 {
563
564 switch (n->type) {
565 case ROFFT_BLOCK:
566 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
567 print_bvspace(p, n, mt->pardist);
568 break;
569 default:
570 p->offset = mt->offset;
571 break;
572 }
573
574 return(n->type != ROFFT_HEAD);
575 }
576
577 static int
578 pre_IP(DECL_ARGS)
579 {
580 struct roffsu su;
581 const struct roff_node *nn;
582 int len, savelit;
583
584 switch (n->type) {
585 case ROFFT_BODY:
586 p->flags |= TERMP_NOSPACE;
587 break;
588 case ROFFT_HEAD:
589 p->flags |= TERMP_NOBREAK;
590 p->trailspace = 1;
591 break;
592 case ROFFT_BLOCK:
593 print_bvspace(p, n, mt->pardist);
594 /* FALLTHROUGH */
595 default:
596 return(1);
597 }
598
599 /* Calculate the offset from the optional second argument. */
600 if ((nn = n->parent->head->child) != NULL &&
601 (nn = nn->next) != NULL &&
602 a2roffsu(nn->string, &su, SCALE_EN)) {
603 len = term_hspan(p, &su) / 24;
604 if (len < 0 && (size_t)(-len) > mt->offset)
605 len = -mt->offset;
606 else if (len > SHRT_MAX)
607 len = term_len(p, p->defindent);
608 mt->lmargin[mt->lmargincur] = len;
609 } else
610 len = mt->lmargin[mt->lmargincur];
611
612 switch (n->type) {
613 case ROFFT_HEAD:
614 p->offset = mt->offset;
615 p->rmargin = mt->offset + len;
616
617 savelit = MANT_LITERAL & mt->fl;
618 mt->fl &= ~MANT_LITERAL;
619
620 if (n->child)
621 print_man_node(p, mt, n->child, meta);
622
623 if (savelit)
624 mt->fl |= MANT_LITERAL;
625
626 return(0);
627 case ROFFT_BODY:
628 p->offset = mt->offset + len;
629 p->rmargin = p->maxrmargin;
630 break;
631 default:
632 break;
633 }
634
635 return(1);
636 }
637
638 static void
639 post_IP(DECL_ARGS)
640 {
641
642 switch (n->type) {
643 case ROFFT_HEAD:
644 term_flushln(p);
645 p->flags &= ~TERMP_NOBREAK;
646 p->trailspace = 0;
647 p->rmargin = p->maxrmargin;
648 break;
649 case ROFFT_BODY:
650 term_newln(p);
651 p->offset = mt->offset;
652 break;
653 default:
654 break;
655 }
656 }
657
658 static int
659 pre_TP(DECL_ARGS)
660 {
661 struct roffsu su;
662 struct roff_node *nn;
663 int len, savelit;
664
665 switch (n->type) {
666 case ROFFT_HEAD:
667 p->flags |= TERMP_NOBREAK;
668 p->trailspace = 1;
669 break;
670 case ROFFT_BODY:
671 p->flags |= TERMP_NOSPACE;
672 break;
673 case ROFFT_BLOCK:
674 print_bvspace(p, n, mt->pardist);
675 /* FALLTHROUGH */
676 default:
677 return(1);
678 }
679
680 /* Calculate offset. */
681
682 if ((nn = n->parent->head->child) != NULL &&
683 nn->string != NULL && ! (MAN_LINE & nn->flags) &&
684 a2roffsu(nn->string, &su, SCALE_EN)) {
685 len = term_hspan(p, &su) / 24;
686 if (len < 0 && (size_t)(-len) > mt->offset)
687 len = -mt->offset;
688 else if (len > SHRT_MAX)
689 len = term_len(p, p->defindent);
690 mt->lmargin[mt->lmargincur] = len;
691 } else
692 len = mt->lmargin[mt->lmargincur];
693
694 switch (n->type) {
695 case ROFFT_HEAD:
696 p->offset = mt->offset;
697 p->rmargin = mt->offset + len;
698
699 savelit = MANT_LITERAL & mt->fl;
700 mt->fl &= ~MANT_LITERAL;
701
702 /* Don't print same-line elements. */
703 nn = n->child;
704 while (NULL != nn && 0 == (MAN_LINE & nn->flags))
705 nn = nn->next;
706
707 while (NULL != nn) {
708 print_man_node(p, mt, nn, meta);
709 nn = nn->next;
710 }
711
712 if (savelit)
713 mt->fl |= MANT_LITERAL;
714 return(0);
715 case ROFFT_BODY:
716 p->offset = mt->offset + len;
717 p->rmargin = p->maxrmargin;
718 p->trailspace = 0;
719 p->flags &= ~TERMP_NOBREAK;
720 break;
721 default:
722 break;
723 }
724
725 return(1);
726 }
727
728 static void
729 post_TP(DECL_ARGS)
730 {
731
732 switch (n->type) {
733 case ROFFT_HEAD:
734 term_flushln(p);
735 break;
736 case ROFFT_BODY:
737 term_newln(p);
738 p->offset = mt->offset;
739 break;
740 default:
741 break;
742 }
743 }
744
745 static int
746 pre_SS(DECL_ARGS)
747 {
748 int i;
749
750 switch (n->type) {
751 case ROFFT_BLOCK:
752 mt->fl &= ~MANT_LITERAL;
753 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
754 mt->offset = term_len(p, p->defindent);
755
756 /*
757 * No vertical space before the first subsection
758 * and after an empty subsection.
759 */
760
761 do {
762 n = n->prev;
763 } while (n != NULL && n->tok != MAN_MAX &&
764 termacts[n->tok].flags & MAN_NOTEXT);
765 if (n == NULL || (n->tok == MAN_SS && n->body->child == NULL))
766 break;
767
768 for (i = 0; i < mt->pardist; i++)
769 term_vspace(p);
770 break;
771 case ROFFT_HEAD:
772 term_fontrepl(p, TERMFONT_BOLD);
773 p->offset = term_len(p, 3);
774 p->rmargin = mt->offset;
775 p->trailspace = mt->offset;
776 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
777 break;
778 case ROFFT_BODY:
779 p->offset = mt->offset;
780 p->rmargin = p->maxrmargin;
781 p->trailspace = 0;
782 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
783 break;
784 default:
785 break;
786 }
787
788 return(1);
789 }
790
791 static void
792 post_SS(DECL_ARGS)
793 {
794
795 switch (n->type) {
796 case ROFFT_HEAD:
797 term_newln(p);
798 break;
799 case ROFFT_BODY:
800 term_newln(p);
801 break;
802 default:
803 break;
804 }
805 }
806
807 static int
808 pre_SH(DECL_ARGS)
809 {
810 int i;
811
812 switch (n->type) {
813 case ROFFT_BLOCK:
814 mt->fl &= ~MANT_LITERAL;
815 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
816 mt->offset = term_len(p, p->defindent);
817
818 /*
819 * No vertical space before the first section
820 * and after an empty section.
821 */
822
823 do {
824 n = n->prev;
825 } while (n != NULL && termacts[n->tok].flags & MAN_NOTEXT);
826 if (n == NULL || (n->tok == MAN_SH && n->body->child == NULL))
827 break;
828
829 for (i = 0; i < mt->pardist; i++)
830 term_vspace(p);
831 break;
832 case ROFFT_HEAD:
833 term_fontrepl(p, TERMFONT_BOLD);
834 p->offset = 0;
835 p->rmargin = mt->offset;
836 p->trailspace = mt->offset;
837 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
838 break;
839 case ROFFT_BODY:
840 p->offset = mt->offset;
841 p->rmargin = p->maxrmargin;
842 p->trailspace = 0;
843 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
844 break;
845 default:
846 break;
847 }
848
849 return(1);
850 }
851
852 static void
853 post_SH(DECL_ARGS)
854 {
855
856 switch (n->type) {
857 case ROFFT_HEAD:
858 term_newln(p);
859 break;
860 case ROFFT_BODY:
861 term_newln(p);
862 break;
863 default:
864 break;
865 }
866 }
867
868 static int
869 pre_RS(DECL_ARGS)
870 {
871 struct roffsu su;
872
873 switch (n->type) {
874 case ROFFT_BLOCK:
875 term_newln(p);
876 return(1);
877 case ROFFT_HEAD:
878 return(0);
879 default:
880 break;
881 }
882
883 n = n->parent->head;
884 n->aux = SHRT_MAX + 1;
885 if (n->child == NULL)
886 n->aux = mt->lmargin[mt->lmargincur];
887 else if (a2roffsu(n->child->string, &su, SCALE_EN))
888 n->aux = term_hspan(p, &su) / 24;
889 if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
890 n->aux = -mt->offset;
891 else if (n->aux > SHRT_MAX)
892 n->aux = term_len(p, p->defindent);
893
894 mt->offset += n->aux;
895 p->offset = mt->offset;
896 p->rmargin = p->maxrmargin;
897
898 if (++mt->lmarginsz < MAXMARGINS)
899 mt->lmargincur = mt->lmarginsz;
900
901 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
902 return(1);
903 }
904
905 static void
906 post_RS(DECL_ARGS)
907 {
908
909 switch (n->type) {
910 case ROFFT_BLOCK:
911 return;
912 case ROFFT_HEAD:
913 return;
914 default:
915 term_newln(p);
916 break;
917 }
918
919 mt->offset -= n->parent->head->aux;
920 p->offset = mt->offset;
921
922 if (--mt->lmarginsz < MAXMARGINS)
923 mt->lmargincur = mt->lmarginsz;
924 }
925
926 static int
927 pre_UR(DECL_ARGS)
928 {
929
930 return (n->type != ROFFT_HEAD);
931 }
932
933 static void
934 post_UR(DECL_ARGS)
935 {
936
937 if (n->type != ROFFT_BLOCK)
938 return;
939
940 term_word(p, "<");
941 p->flags |= TERMP_NOSPACE;
942
943 if (NULL != n->child->child)
944 print_man_node(p, mt, n->child->child, meta);
945
946 p->flags |= TERMP_NOSPACE;
947 term_word(p, ">");
948 }
949
950 static void
951 print_man_node(DECL_ARGS)
952 {
953 size_t rm, rmax;
954 int c;
955
956 switch (n->type) {
957 case ROFFT_TEXT:
958 /*
959 * If we have a blank line, output a vertical space.
960 * If we have a space as the first character, break
961 * before printing the line's data.
962 */
963 if ('\0' == *n->string) {
964 term_vspace(p);
965 return;
966 } else if (' ' == *n->string && MAN_LINE & n->flags)
967 term_newln(p);
968
969 term_word(p, n->string);
970 goto out;
971
972 case ROFFT_EQN:
973 if ( ! (n->flags & MAN_LINE))
974 p->flags |= TERMP_NOSPACE;
975 term_eqn(p, n->eqn);
976 if (n->next != NULL && ! (n->next->flags & MAN_LINE))
977 p->flags |= TERMP_NOSPACE;
978 return;
979 case ROFFT_TBL:
980 if (p->tbl.cols == NULL)
981 term_vspace(p);
982 term_tbl(p, n->span);
983 return;
984 default:
985 break;
986 }
987
988 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
989 term_fontrepl(p, TERMFONT_NONE);
990
991 c = 1;
992 if (termacts[n->tok].pre)
993 c = (*termacts[n->tok].pre)(p, mt, n, meta);
994
995 if (c && n->child)
996 print_man_nodelist(p, mt, n->child, meta);
997
998 if (termacts[n->tok].post)
999 (*termacts[n->tok].post)(p, mt, n, meta);
1000 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
1001 term_fontrepl(p, TERMFONT_NONE);
1002
1003 out:
1004 /*
1005 * If we're in a literal context, make sure that words
1006 * together on the same line stay together. This is a
1007 * POST-printing call, so we check the NEXT word. Since
1008 * -man doesn't have nested macros, we don't need to be
1009 * more specific than this.
1010 */
1011 if (mt->fl & MANT_LITERAL &&
1012 ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1013 (n->next == NULL || n->next->flags & MAN_LINE)) {
1014 rm = p->rmargin;
1015 rmax = p->maxrmargin;
1016 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1017 p->flags |= TERMP_NOSPACE;
1018 if (n->string != NULL && *n->string != '\0')
1019 term_flushln(p);
1020 else
1021 term_newln(p);
1022 if (rm < rmax && n->parent->tok == MAN_HP) {
1023 p->offset = rm;
1024 p->rmargin = rmax;
1025 } else
1026 p->rmargin = rm;
1027 p->maxrmargin = rmax;
1028 }
1029 if (MAN_EOS & n->flags)
1030 p->flags |= TERMP_SENTENCE;
1031 }
1032
1033
1034 static void
1035 print_man_nodelist(DECL_ARGS)
1036 {
1037
1038 while (n != NULL) {
1039 print_man_node(p, mt, n, meta);
1040 n = n->next;
1041 }
1042 }
1043
1044 static void
1045 print_man_foot(struct termp *p, const struct roff_meta *meta)
1046 {
1047 char *title;
1048 size_t datelen, titlen;
1049
1050 assert(meta->title);
1051 assert(meta->msec);
1052 assert(meta->date);
1053
1054 term_fontrepl(p, TERMFONT_NONE);
1055
1056 if (meta->hasbody)
1057 term_vspace(p);
1058
1059 /*
1060 * Temporary, undocumented option to imitate mdoc(7) output.
1061 * In the bottom right corner, use the operating system
1062 * instead of the title.
1063 */
1064
1065 if ( ! p->mdocstyle) {
1066 if (meta->hasbody) {
1067 term_vspace(p);
1068 term_vspace(p);
1069 }
1070 mandoc_asprintf(&title, "%s(%s)",
1071 meta->title, meta->msec);
1072 } else if (meta->os) {
1073 title = mandoc_strdup(meta->os);
1074 } else {
1075 title = mandoc_strdup("");
1076 }
1077 datelen = term_strlen(p, meta->date);
1078
1079 /* Bottom left corner: operating system. */
1080
1081 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1082 p->trailspace = 1;
1083 p->offset = 0;
1084 p->rmargin = p->maxrmargin > datelen ?
1085 (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1086
1087 if (meta->os)
1088 term_word(p, meta->os);
1089 term_flushln(p);
1090
1091 /* At the bottom in the middle: manual date. */
1092
1093 p->offset = p->rmargin;
1094 titlen = term_strlen(p, title);
1095 p->rmargin = p->maxrmargin > titlen ? p->maxrmargin - titlen : 0;
1096 p->flags |= TERMP_NOSPACE;
1097
1098 term_word(p, meta->date);
1099 term_flushln(p);
1100
1101 /* Bottom right corner: manual title and section. */
1102
1103 p->flags &= ~TERMP_NOBREAK;
1104 p->flags |= TERMP_NOSPACE;
1105 p->trailspace = 0;
1106 p->offset = p->rmargin;
1107 p->rmargin = p->maxrmargin;
1108
1109 term_word(p, title);
1110 term_flushln(p);
1111 free(title);
1112 }
1113
1114 static void
1115 print_man_head(struct termp *p, const struct roff_meta *meta)
1116 {
1117 const char *volume;
1118 char *title;
1119 size_t vollen, titlen;
1120
1121 assert(meta->title);
1122 assert(meta->msec);
1123
1124 volume = NULL == meta->vol ? "" : meta->vol;
1125 vollen = term_strlen(p, volume);
1126
1127 /* Top left corner: manual title and section. */
1128
1129 mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1130 titlen = term_strlen(p, title);
1131
1132 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1133 p->trailspace = 1;
1134 p->offset = 0;
1135 p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1136 (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1137 vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1138
1139 term_word(p, title);
1140 term_flushln(p);
1141
1142 /* At the top in the middle: manual volume. */
1143
1144 p->flags |= TERMP_NOSPACE;
1145 p->offset = p->rmargin;
1146 p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
1147 p->maxrmargin - titlen : p->maxrmargin;
1148
1149 term_word(p, volume);
1150 term_flushln(p);
1151
1152 /* Top right corner: title and section, again. */
1153
1154 p->flags &= ~TERMP_NOBREAK;
1155 p->trailspace = 0;
1156 if (p->rmargin + titlen <= p->maxrmargin) {
1157 p->flags |= TERMP_NOSPACE;
1158 p->offset = p->rmargin;
1159 p->rmargin = p->maxrmargin;
1160 term_word(p, title);
1161 term_flushln(p);
1162 }
1163
1164 p->flags &= ~TERMP_NOSPACE;
1165 p->offset = 0;
1166 p->rmargin = p->maxrmargin;
1167
1168 /*
1169 * Groff prints three blank lines before the content.
1170 * Do the same, except in the temporary, undocumented
1171 * mode imitating mdoc(7) output.
1172 */
1173
1174 term_vspace(p);
1175 if ( ! p->mdocstyle) {
1176 term_vspace(p);
1177 term_vspace(p);
1178 }
1179 free(title);
1180 }