]> git.cameronkatri.com Git - mandoc.git/blob - man_term.c
Fix two TODOs with one check-in. Both of these relate to vertical space
[mandoc.git] / man_term.c
1 /* $Id: man_term.c,v 1.111 2011/06/18 17:58:48 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 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 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.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "mandoc.h"
31 #include "out.h"
32 #include "man.h"
33 #include "term.h"
34 #include "main.h"
35
36 #define INDENT 7
37 #define HALFINDENT 3
38
39 /* FIXME: have PD set the default vspace width. */
40
41 struct mtermp {
42 int fl;
43 #define MANT_LITERAL (1 << 0)
44 /*
45 * Default amount to indent the left margin after leading text
46 * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
47 * indent). This needs to be saved because `HP' and so on, if
48 * not having a specified value, must default.
49 *
50 * Note that this is the indentation AFTER the left offset, so
51 * the total offset is usually offset + lmargin.
52 */
53 size_t lmargin;
54 /*
55 * The default offset, i.e., the amount between any text and the
56 * page boundary.
57 */
58 size_t offset;
59 };
60
61 #define DECL_ARGS struct termp *p, \
62 struct mtermp *mt, \
63 const struct man_node *n, \
64 const struct man_meta *m
65
66 struct termact {
67 int (*pre)(DECL_ARGS);
68 void (*post)(DECL_ARGS);
69 int flags;
70 #define MAN_NOTEXT (1 << 0) /* Never has text children. */
71 };
72
73 static int a2width(const struct termp *, const char *);
74 static size_t a2height(const struct termp *, const char *);
75
76 static void print_man_nodelist(DECL_ARGS);
77 static void print_man_node(DECL_ARGS);
78 static void print_man_head(struct termp *, const void *);
79 static void print_man_foot(struct termp *, const void *);
80 static void print_bvspace(struct termp *,
81 const struct man_node *);
82
83 static int pre_alternate(DECL_ARGS);
84 static int pre_B(DECL_ARGS);
85 static int pre_HP(DECL_ARGS);
86 static int pre_I(DECL_ARGS);
87 static int pre_IP(DECL_ARGS);
88 static int pre_PP(DECL_ARGS);
89 static int pre_RS(DECL_ARGS);
90 static int pre_SH(DECL_ARGS);
91 static int pre_SS(DECL_ARGS);
92 static int pre_TP(DECL_ARGS);
93 static int pre_ign(DECL_ARGS);
94 static int pre_in(DECL_ARGS);
95 static int pre_literal(DECL_ARGS);
96 static int pre_sp(DECL_ARGS);
97 static int pre_ft(DECL_ARGS);
98
99 static void post_IP(DECL_ARGS);
100 static void post_HP(DECL_ARGS);
101 static void post_RS(DECL_ARGS);
102 static void post_SH(DECL_ARGS);
103 static void post_SS(DECL_ARGS);
104 static void post_TP(DECL_ARGS);
105
106 static const struct termact termacts[MAN_MAX] = {
107 { pre_sp, NULL, MAN_NOTEXT }, /* br */
108 { NULL, NULL, 0 }, /* TH */
109 { pre_SH, post_SH, 0 }, /* SH */
110 { pre_SS, post_SS, 0 }, /* SS */
111 { pre_TP, post_TP, 0 }, /* TP */
112 { pre_PP, NULL, 0 }, /* LP */
113 { pre_PP, NULL, 0 }, /* PP */
114 { pre_PP, NULL, 0 }, /* P */
115 { pre_IP, post_IP, 0 }, /* IP */
116 { pre_HP, post_HP, 0 }, /* HP */
117 { NULL, NULL, 0 }, /* SM */
118 { pre_B, NULL, 0 }, /* SB */
119 { pre_alternate, NULL, 0 }, /* BI */
120 { pre_alternate, NULL, 0 }, /* IB */
121 { pre_alternate, NULL, 0 }, /* BR */
122 { pre_alternate, NULL, 0 }, /* RB */
123 { NULL, NULL, 0 }, /* R */
124 { pre_B, NULL, 0 }, /* B */
125 { pre_I, NULL, 0 }, /* I */
126 { pre_alternate, NULL, 0 }, /* IR */
127 { pre_alternate, NULL, 0 }, /* RI */
128 { pre_ign, NULL, MAN_NOTEXT }, /* na */
129 { pre_sp, NULL, MAN_NOTEXT }, /* sp */
130 { pre_literal, NULL, 0 }, /* nf */
131 { pre_literal, NULL, 0 }, /* fi */
132 { NULL, NULL, 0 }, /* RE */
133 { pre_RS, post_RS, 0 }, /* RS */
134 { pre_ign, NULL, 0 }, /* DT */
135 { pre_ign, NULL, 0 }, /* UC */
136 { pre_ign, NULL, 0 }, /* PD */
137 { pre_ign, NULL, 0 }, /* AT */
138 { pre_in, NULL, MAN_NOTEXT }, /* in */
139 { pre_ft, NULL, MAN_NOTEXT }, /* ft */
140 };
141
142
143
144 void
145 terminal_man(void *arg, const struct man *man)
146 {
147 struct termp *p;
148 const struct man_node *n;
149 const struct man_meta *m;
150 struct mtermp mt;
151
152 p = (struct termp *)arg;
153
154 p->overstep = 0;
155 p->maxrmargin = p->defrmargin;
156 p->tabwidth = term_len(p, 5);
157
158 if (NULL == p->symtab)
159 p->symtab = mchars_alloc();
160
161 n = man_node(man);
162 m = man_meta(man);
163
164 term_begin(p, print_man_head, print_man_foot, m);
165 p->flags |= TERMP_NOSPACE;
166
167 mt.fl = 0;
168 mt.lmargin = term_len(p, INDENT);
169 mt.offset = term_len(p, INDENT);
170
171 if (n->child)
172 print_man_nodelist(p, &mt, n->child, m);
173
174 term_end(p);
175 }
176
177
178 static size_t
179 a2height(const struct termp *p, const char *cp)
180 {
181 struct roffsu su;
182
183 if ( ! a2roffsu(cp, &su, SCALE_VS))
184 SCALE_VS_INIT(&su, term_strlen(p, cp));
185
186 return(term_vspan(p, &su));
187 }
188
189
190 static int
191 a2width(const struct termp *p, const char *cp)
192 {
193 struct roffsu su;
194
195 if ( ! a2roffsu(cp, &su, SCALE_BU))
196 return(-1);
197
198 return((int)term_hspan(p, &su));
199 }
200
201 /*
202 * Printing leading vertical space before a block.
203 * This is used for the paragraph macros.
204 * The rules are pretty simple, since there's very little nesting going
205 * on here. Basically, if we're the first within another block (SS/SH),
206 * then don't emit vertical space. If we are (RS), then do. If not the
207 * first, print it.
208 */
209 static void
210 print_bvspace(struct termp *p, const struct man_node *n)
211 {
212
213 term_newln(p);
214
215 if (n->body && n->body->child)
216 if (MAN_TBL == n->body->child->type)
217 return;
218
219 if (MAN_ROOT == n->parent->type || MAN_RS != n->parent->tok)
220 if (NULL == n->prev)
221 return;
222
223 term_vspace(p);
224 }
225
226 /* ARGSUSED */
227 static int
228 pre_ign(DECL_ARGS)
229 {
230
231 return(0);
232 }
233
234
235 /* ARGSUSED */
236 static int
237 pre_I(DECL_ARGS)
238 {
239
240 term_fontrepl(p, TERMFONT_UNDER);
241 return(1);
242 }
243
244
245 /* ARGSUSED */
246 static int
247 pre_literal(DECL_ARGS)
248 {
249
250 term_newln(p);
251
252 if (MAN_nf == n->tok)
253 mt->fl |= MANT_LITERAL;
254 else
255 mt->fl &= ~MANT_LITERAL;
256
257 return(0);
258 }
259
260 /* ARGSUSED */
261 static int
262 pre_alternate(DECL_ARGS)
263 {
264 enum termfont font[2];
265 const struct man_node *nn;
266 int savelit, i;
267
268 switch (n->tok) {
269 case (MAN_RB):
270 font[0] = TERMFONT_NONE;
271 font[1] = TERMFONT_BOLD;
272 break;
273 case (MAN_RI):
274 font[0] = TERMFONT_NONE;
275 font[1] = TERMFONT_UNDER;
276 break;
277 case (MAN_BR):
278 font[0] = TERMFONT_BOLD;
279 font[1] = TERMFONT_NONE;
280 break;
281 case (MAN_BI):
282 font[0] = TERMFONT_BOLD;
283 font[1] = TERMFONT_UNDER;
284 break;
285 case (MAN_IR):
286 font[0] = TERMFONT_UNDER;
287 font[1] = TERMFONT_NONE;
288 break;
289 case (MAN_IB):
290 font[0] = TERMFONT_UNDER;
291 font[1] = TERMFONT_BOLD;
292 break;
293 default:
294 abort();
295 }
296
297 savelit = MANT_LITERAL & mt->fl;
298 mt->fl &= ~MANT_LITERAL;
299
300 for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
301 term_fontrepl(p, font[i]);
302 if (savelit && NULL == nn->next)
303 mt->fl |= MANT_LITERAL;
304 print_man_node(p, mt, nn, m);
305 if (nn->next)
306 p->flags |= TERMP_NOSPACE;
307 }
308
309 return(0);
310 }
311
312 /* ARGSUSED */
313 static int
314 pre_B(DECL_ARGS)
315 {
316
317 term_fontrepl(p, TERMFONT_BOLD);
318 return(1);
319 }
320
321 /* ARGSUSED */
322 static int
323 pre_ft(DECL_ARGS)
324 {
325 const char *cp;
326
327 if (NULL == n->child) {
328 term_fontlast(p);
329 return(0);
330 }
331
332 cp = n->child->string;
333 switch (*cp) {
334 case ('4'):
335 /* FALLTHROUGH */
336 case ('3'):
337 /* FALLTHROUGH */
338 case ('B'):
339 term_fontrepl(p, TERMFONT_BOLD);
340 break;
341 case ('2'):
342 /* FALLTHROUGH */
343 case ('I'):
344 term_fontrepl(p, TERMFONT_UNDER);
345 break;
346 case ('P'):
347 term_fontlast(p);
348 break;
349 case ('1'):
350 /* FALLTHROUGH */
351 case ('C'):
352 /* FALLTHROUGH */
353 case ('R'):
354 term_fontrepl(p, TERMFONT_NONE);
355 break;
356 default:
357 break;
358 }
359 return(0);
360 }
361
362 /* ARGSUSED */
363 static int
364 pre_in(DECL_ARGS)
365 {
366 int len, less;
367 size_t v;
368 const char *cp;
369
370 term_newln(p);
371
372 if (NULL == n->child) {
373 p->offset = mt->offset;
374 return(0);
375 }
376
377 cp = n->child->string;
378 less = 0;
379
380 if ('-' == *cp)
381 less = -1;
382 else if ('+' == *cp)
383 less = 1;
384 else
385 cp--;
386
387 if ((len = a2width(p, ++cp)) < 0)
388 return(0);
389
390 v = (size_t)len;
391
392 if (less < 0)
393 p->offset -= p->offset > v ? v : p->offset;
394 else if (less > 0)
395 p->offset += v;
396 else
397 p->offset = v;
398
399 /* Don't let this creep beyond the right margin. */
400
401 if (p->offset > p->rmargin)
402 p->offset = p->rmargin;
403
404 return(0);
405 }
406
407
408 /* ARGSUSED */
409 static int
410 pre_sp(DECL_ARGS)
411 {
412 size_t i, len;
413
414 switch (n->tok) {
415 case (MAN_br):
416 len = 0;
417 break;
418 default:
419 len = n->child ? a2height(p, n->child->string) : 1;
420 break;
421 }
422
423 if (0 == len)
424 term_newln(p);
425 for (i = 0; i < len; i++)
426 term_vspace(p);
427
428 return(0);
429 }
430
431
432 /* ARGSUSED */
433 static int
434 pre_HP(DECL_ARGS)
435 {
436 size_t len;
437 int ival;
438 const struct man_node *nn;
439
440 switch (n->type) {
441 case (MAN_BLOCK):
442 print_bvspace(p, n);
443 return(1);
444 case (MAN_BODY):
445 p->flags |= TERMP_NOBREAK;
446 p->flags |= TERMP_TWOSPACE;
447 break;
448 default:
449 return(0);
450 }
451
452 len = mt->lmargin;
453 ival = -1;
454
455 /* Calculate offset. */
456
457 if (NULL != (nn = n->parent->head->child))
458 if ((ival = a2width(p, nn->string)) >= 0)
459 len = (size_t)ival;
460
461 if (0 == len)
462 len = term_len(p, 1);
463
464 p->offset = mt->offset;
465 p->rmargin = mt->offset + len;
466
467 if (ival >= 0)
468 mt->lmargin = (size_t)ival;
469
470 return(1);
471 }
472
473
474 /* ARGSUSED */
475 static void
476 post_HP(DECL_ARGS)
477 {
478
479 switch (n->type) {
480 case (MAN_BLOCK):
481 term_flushln(p);
482 break;
483 case (MAN_BODY):
484 term_flushln(p);
485 p->flags &= ~TERMP_NOBREAK;
486 p->flags &= ~TERMP_TWOSPACE;
487 p->offset = mt->offset;
488 p->rmargin = p->maxrmargin;
489 break;
490 default:
491 break;
492 }
493 }
494
495
496 /* ARGSUSED */
497 static int
498 pre_PP(DECL_ARGS)
499 {
500
501 switch (n->type) {
502 case (MAN_BLOCK):
503 mt->lmargin = term_len(p, INDENT);
504 print_bvspace(p, n);
505 break;
506 default:
507 p->offset = mt->offset;
508 break;
509 }
510
511 return(MAN_HEAD != n->type);
512 }
513
514
515 /* ARGSUSED */
516 static int
517 pre_IP(DECL_ARGS)
518 {
519 const struct man_node *nn;
520 size_t len;
521 int savelit, ival;
522
523 switch (n->type) {
524 case (MAN_BODY):
525 p->flags |= TERMP_NOLPAD;
526 p->flags |= TERMP_NOSPACE;
527 break;
528 case (MAN_HEAD):
529 p->flags |= TERMP_NOBREAK;
530 break;
531 case (MAN_BLOCK):
532 print_bvspace(p, n);
533 /* FALLTHROUGH */
534 default:
535 return(1);
536 }
537
538 len = mt->lmargin;
539 ival = -1;
540
541 /* Calculate the offset from the optional second argument. */
542 if (NULL != (nn = n->parent->head->child))
543 if (NULL != (nn = nn->next))
544 if ((ival = a2width(p, nn->string)) >= 0)
545 len = (size_t)ival;
546
547 switch (n->type) {
548 case (MAN_HEAD):
549 /* Handle zero-width lengths. */
550 if (0 == len)
551 len = term_len(p, 1);
552
553 p->offset = mt->offset;
554 p->rmargin = mt->offset + len;
555 if (ival < 0)
556 break;
557
558 /* Set the saved left-margin. */
559 mt->lmargin = (size_t)ival;
560
561 savelit = MANT_LITERAL & mt->fl;
562 mt->fl &= ~MANT_LITERAL;
563
564 if (n->child)
565 print_man_node(p, mt, n->child, m);
566
567 if (savelit)
568 mt->fl |= MANT_LITERAL;
569
570 return(0);
571 case (MAN_BODY):
572 p->offset = mt->offset + len;
573 p->rmargin = p->maxrmargin;
574 break;
575 default:
576 break;
577 }
578
579 return(1);
580 }
581
582
583 /* ARGSUSED */
584 static void
585 post_IP(DECL_ARGS)
586 {
587
588 switch (n->type) {
589 case (MAN_HEAD):
590 term_flushln(p);
591 p->flags &= ~TERMP_NOBREAK;
592 p->rmargin = p->maxrmargin;
593 break;
594 case (MAN_BODY):
595 term_newln(p);
596 p->flags &= ~TERMP_NOLPAD;
597 break;
598 default:
599 break;
600 }
601 }
602
603
604 /* ARGSUSED */
605 static int
606 pre_TP(DECL_ARGS)
607 {
608 const struct man_node *nn;
609 size_t len;
610 int savelit, ival;
611
612 switch (n->type) {
613 case (MAN_HEAD):
614 p->flags |= TERMP_NOBREAK;
615 break;
616 case (MAN_BODY):
617 p->flags |= TERMP_NOLPAD;
618 p->flags |= TERMP_NOSPACE;
619 break;
620 case (MAN_BLOCK):
621 print_bvspace(p, n);
622 /* FALLTHROUGH */
623 default:
624 return(1);
625 }
626
627 len = (size_t)mt->lmargin;
628 ival = -1;
629
630 /* Calculate offset. */
631
632 if (NULL != (nn = n->parent->head->child)) {
633 while (nn && MAN_TEXT != nn->type)
634 nn = nn->next;
635 if (nn && nn->next)
636 if ((ival = a2width(p, nn->string)) >= 0)
637 len = (size_t)ival;
638 }
639
640 switch (n->type) {
641 case (MAN_HEAD):
642 /* Handle zero-length properly. */
643 if (0 == len)
644 len = term_len(p, 1);
645
646 p->offset = mt->offset;
647 p->rmargin = mt->offset + len;
648
649 savelit = MANT_LITERAL & mt->fl;
650 mt->fl &= ~MANT_LITERAL;
651
652 /* Don't print same-line elements. */
653 for (nn = n->child; nn; nn = nn->next)
654 if (nn->line > n->line)
655 print_man_node(p, mt, nn, m);
656
657 if (savelit)
658 mt->fl |= MANT_LITERAL;
659
660 if (ival >= 0)
661 mt->lmargin = (size_t)ival;
662
663 return(0);
664 case (MAN_BODY):
665 p->offset = mt->offset + len;
666 p->rmargin = p->maxrmargin;
667 break;
668 default:
669 break;
670 }
671
672 return(1);
673 }
674
675
676 /* ARGSUSED */
677 static void
678 post_TP(DECL_ARGS)
679 {
680
681 switch (n->type) {
682 case (MAN_HEAD):
683 term_flushln(p);
684 p->flags &= ~TERMP_NOBREAK;
685 p->flags &= ~TERMP_TWOSPACE;
686 p->rmargin = p->maxrmargin;
687 break;
688 case (MAN_BODY):
689 term_newln(p);
690 p->flags &= ~TERMP_NOLPAD;
691 break;
692 default:
693 break;
694 }
695 }
696
697
698 /* ARGSUSED */
699 static int
700 pre_SS(DECL_ARGS)
701 {
702
703 switch (n->type) {
704 case (MAN_BLOCK):
705 mt->lmargin = term_len(p, INDENT);
706 mt->offset = term_len(p, INDENT);
707 /* If following a prior empty `SS', no vspace. */
708 if (n->prev && MAN_SS == n->prev->tok)
709 if (NULL == n->prev->body->child)
710 break;
711 if (NULL == n->prev)
712 break;
713 term_vspace(p);
714 break;
715 case (MAN_HEAD):
716 term_fontrepl(p, TERMFONT_BOLD);
717 p->offset = term_len(p, HALFINDENT);
718 break;
719 case (MAN_BODY):
720 p->offset = mt->offset;
721 break;
722 default:
723 break;
724 }
725
726 return(1);
727 }
728
729
730 /* ARGSUSED */
731 static void
732 post_SS(DECL_ARGS)
733 {
734
735 switch (n->type) {
736 case (MAN_HEAD):
737 term_newln(p);
738 break;
739 case (MAN_BODY):
740 term_newln(p);
741 break;
742 default:
743 break;
744 }
745 }
746
747
748 /* ARGSUSED */
749 static int
750 pre_SH(DECL_ARGS)
751 {
752
753 switch (n->type) {
754 case (MAN_BLOCK):
755 mt->lmargin = term_len(p, INDENT);
756 mt->offset = term_len(p, INDENT);
757 /* If following a prior empty `SH', no vspace. */
758 if (n->prev && MAN_SH == n->prev->tok)
759 if (NULL == n->prev->body->child)
760 break;
761 /* If the first macro, no vspae. */
762 if (NULL == n->prev)
763 break;
764 term_vspace(p);
765 break;
766 case (MAN_HEAD):
767 term_fontrepl(p, TERMFONT_BOLD);
768 p->offset = 0;
769 break;
770 case (MAN_BODY):
771 p->offset = mt->offset;
772 break;
773 default:
774 break;
775 }
776
777 return(1);
778 }
779
780
781 /* ARGSUSED */
782 static void
783 post_SH(DECL_ARGS)
784 {
785
786 switch (n->type) {
787 case (MAN_HEAD):
788 term_newln(p);
789 break;
790 case (MAN_BODY):
791 term_newln(p);
792 break;
793 default:
794 break;
795 }
796 }
797
798 /* ARGSUSED */
799 static int
800 pre_RS(DECL_ARGS)
801 {
802 int ival;
803 size_t sz;
804
805 switch (n->type) {
806 case (MAN_BLOCK):
807 term_newln(p);
808 return(1);
809 case (MAN_HEAD):
810 return(0);
811 default:
812 break;
813 }
814
815 sz = term_len(p, INDENT);
816
817 if (NULL != (n = n->parent->head->child))
818 if ((ival = a2width(p, n->string)) >= 0)
819 sz = (size_t)ival;
820
821 mt->offset += sz;
822 p->offset = mt->offset;
823
824 return(1);
825 }
826
827 /* ARGSUSED */
828 static void
829 post_RS(DECL_ARGS)
830 {
831 int ival;
832 size_t sz;
833
834 switch (n->type) {
835 case (MAN_BLOCK):
836 return;
837 case (MAN_HEAD):
838 return;
839 default:
840 term_newln(p);
841 break;
842 }
843
844 sz = term_len(p, INDENT);
845
846 if (NULL != (n = n->parent->head->child))
847 if ((ival = a2width(p, n->string)) >= 0)
848 sz = (size_t)ival;
849
850 mt->offset = mt->offset < sz ? 0 : mt->offset - sz;
851 p->offset = mt->offset;
852 }
853
854 static void
855 print_man_node(DECL_ARGS)
856 {
857 size_t rm, rmax;
858 int c;
859
860 switch (n->type) {
861 case(MAN_TEXT):
862 /*
863 * If we have a blank line, output a vertical space.
864 * If we have a space as the first character, break
865 * before printing the line's data.
866 */
867 if ('\0' == *n->string) {
868 term_vspace(p);
869 return;
870 } else if (' ' == *n->string && MAN_LINE & n->flags)
871 term_newln(p);
872
873 term_word(p, n->string);
874
875 /*
876 * If we're in a literal context, make sure that words
877 * togehter on the same line stay together. This is a
878 * POST-printing call, so we check the NEXT word. Since
879 * -man doesn't have nested macros, we don't need to be
880 * more specific than this.
881 */
882 if (MANT_LITERAL & mt->fl &&
883 (NULL == n->next ||
884 n->next->line > n->line)) {
885 rm = p->rmargin;
886 rmax = p->maxrmargin;
887 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
888 p->flags |= TERMP_NOSPACE;
889 term_flushln(p);
890 p->flags &= ~TERMP_NOLPAD;
891 p->rmargin = rm;
892 p->maxrmargin = rmax;
893 }
894
895 if (MAN_EOS & n->flags)
896 p->flags |= TERMP_SENTENCE;
897 return;
898 case (MAN_EQN):
899 term_word(p, n->eqn->data);
900 return;
901 case (MAN_TBL):
902 /*
903 * Tables are preceded by a newline. Then process a
904 * table line, which will cause line termination,
905 */
906 if (TBL_SPAN_FIRST & n->span->flags)
907 term_newln(p);
908 term_tbl(p, n->span);
909 return;
910 default:
911 break;
912 }
913
914 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
915 term_fontrepl(p, TERMFONT_NONE);
916
917 c = 1;
918 if (termacts[n->tok].pre)
919 c = (*termacts[n->tok].pre)(p, mt, n, m);
920
921 if (c && n->child)
922 print_man_nodelist(p, mt, n->child, m);
923
924 if (termacts[n->tok].post)
925 (*termacts[n->tok].post)(p, mt, n, m);
926 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
927 term_fontrepl(p, TERMFONT_NONE);
928
929 if (MAN_EOS & n->flags)
930 p->flags |= TERMP_SENTENCE;
931 }
932
933
934 static void
935 print_man_nodelist(DECL_ARGS)
936 {
937
938 print_man_node(p, mt, n, m);
939 if ( ! n->next)
940 return;
941 print_man_nodelist(p, mt, n->next, m);
942 }
943
944
945 static void
946 print_man_foot(struct termp *p, const void *arg)
947 {
948 const struct man_meta *meta;
949
950 meta = (const struct man_meta *)arg;
951
952 term_fontrepl(p, TERMFONT_NONE);
953
954 term_vspace(p);
955 term_vspace(p);
956 term_vspace(p);
957
958 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
959 p->rmargin = p->maxrmargin - term_strlen(p, meta->date);
960 p->offset = 0;
961
962 /* term_strlen() can return zero. */
963 if (p->rmargin == p->maxrmargin)
964 p->rmargin--;
965
966 if (meta->source)
967 term_word(p, meta->source);
968 if (meta->source)
969 term_word(p, "");
970 term_flushln(p);
971
972 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
973 p->offset = p->rmargin;
974 p->rmargin = p->maxrmargin;
975 p->flags &= ~TERMP_NOBREAK;
976
977 term_word(p, meta->date);
978 term_flushln(p);
979 }
980
981
982 static void
983 print_man_head(struct termp *p, const void *arg)
984 {
985 char buf[BUFSIZ], title[BUFSIZ];
986 size_t buflen, titlen;
987 const struct man_meta *m;
988
989 m = (const struct man_meta *)arg;
990
991 /*
992 * Note that old groff would spit out some spaces before the
993 * header. We discontinue this strange behaviour, but at one
994 * point we did so here.
995 */
996
997 p->rmargin = p->maxrmargin;
998
999 p->offset = 0;
1000 buf[0] = title[0] = '\0';
1001
1002 if (m->vol)
1003 strlcpy(buf, m->vol, BUFSIZ);
1004 buflen = term_strlen(p, buf);
1005
1006 snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1007 titlen = term_strlen(p, title);
1008
1009 p->offset = 0;
1010 p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1011 (p->maxrmargin -
1012 term_strlen(p, buf) + term_len(p, 1)) / 2 :
1013 p->maxrmargin - buflen;
1014 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1015
1016 term_word(p, title);
1017 term_flushln(p);
1018
1019 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1020 p->offset = p->rmargin;
1021 p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
1022 p->maxrmargin - titlen : p->maxrmargin;
1023
1024 term_word(p, buf);
1025 term_flushln(p);
1026
1027 p->flags &= ~TERMP_NOBREAK;
1028 if (p->rmargin + titlen <= p->maxrmargin) {
1029 p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1030 p->offset = p->rmargin;
1031 p->rmargin = p->maxrmargin;
1032 term_word(p, title);
1033 term_flushln(p);
1034 }
1035
1036 p->rmargin = p->maxrmargin;
1037 p->offset = 0;
1038 p->flags &= ~TERMP_NOSPACE;
1039
1040 /*
1041 * Groff likes to have some leading spaces before content. Well
1042 * that's fine by me.
1043 */
1044
1045 term_vspace(p);
1046 term_vspace(p);
1047 term_vspace(p);
1048 }