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