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