]> git.cameronkatri.com Git - mandoc.git/blob - man_term.c
14e13246969d168211fa5698756a3483ff199bbc
[mandoc.git] / man_term.c
1 /* $Id: man_term.c,v 1.131 2012/07/13 14:19:49 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012 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 char *s;
444 size_t i, len;
445 int neg;
446
447 if ((NULL == n->prev && n->parent)) {
448 if (MAN_SS == n->parent->tok)
449 return(0);
450 if (MAN_SH == n->parent->tok)
451 return(0);
452 }
453
454 neg = 0;
455 switch (n->tok) {
456 case (MAN_br):
457 len = 0;
458 break;
459 default:
460 if (NULL == n->child) {
461 len = 1;
462 break;
463 }
464 s = n->child->string;
465 if ('-' == *s) {
466 neg = 1;
467 s++;
468 }
469 len = a2height(p, s);
470 break;
471 }
472
473 if (0 == len)
474 term_newln(p);
475 else if (neg)
476 p->skipvsp += len;
477 else
478 for (i = 0; i < len; i++)
479 term_vspace(p);
480
481 return(0);
482 }
483
484
485 /* ARGSUSED */
486 static int
487 pre_HP(DECL_ARGS)
488 {
489 size_t len, one;
490 int ival;
491 const struct man_node *nn;
492
493 switch (n->type) {
494 case (MAN_BLOCK):
495 print_bvspace(p, n);
496 return(1);
497 case (MAN_BODY):
498 break;
499 default:
500 return(0);
501 }
502
503 if ( ! (MANT_LITERAL & mt->fl)) {
504 p->flags |= TERMP_NOBREAK;
505 p->flags |= TERMP_TWOSPACE;
506 }
507
508 len = mt->lmargin[mt->lmargincur];
509 ival = -1;
510
511 /* Calculate offset. */
512
513 if (NULL != (nn = n->parent->head->child))
514 if ((ival = a2width(p, nn->string)) >= 0)
515 len = (size_t)ival;
516
517 one = term_len(p, 1);
518 if (len < one)
519 len = one;
520
521 p->offset = mt->offset;
522 p->rmargin = mt->offset + len;
523
524 if (ival >= 0)
525 mt->lmargin[mt->lmargincur] = (size_t)ival;
526
527 return(1);
528 }
529
530
531 /* ARGSUSED */
532 static void
533 post_HP(DECL_ARGS)
534 {
535
536 switch (n->type) {
537 case (MAN_BODY):
538 term_flushln(p);
539 p->flags &= ~TERMP_NOBREAK;
540 p->flags &= ~TERMP_TWOSPACE;
541 p->offset = mt->offset;
542 p->rmargin = p->maxrmargin;
543 break;
544 default:
545 break;
546 }
547 }
548
549
550 /* ARGSUSED */
551 static int
552 pre_PP(DECL_ARGS)
553 {
554
555 switch (n->type) {
556 case (MAN_BLOCK):
557 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
558 print_bvspace(p, n);
559 break;
560 default:
561 p->offset = mt->offset;
562 break;
563 }
564
565 return(MAN_HEAD != n->type);
566 }
567
568
569 /* ARGSUSED */
570 static int
571 pre_IP(DECL_ARGS)
572 {
573 const struct man_node *nn;
574 size_t len;
575 int savelit, ival;
576
577 switch (n->type) {
578 case (MAN_BODY):
579 p->flags |= TERMP_NOSPACE;
580 break;
581 case (MAN_HEAD):
582 p->flags |= TERMP_NOBREAK;
583 break;
584 case (MAN_BLOCK):
585 print_bvspace(p, n);
586 /* FALLTHROUGH */
587 default:
588 return(1);
589 }
590
591 len = mt->lmargin[mt->lmargincur];
592 ival = -1;
593
594 /* Calculate the offset from the optional second argument. */
595 if (NULL != (nn = n->parent->head->child))
596 if (NULL != (nn = nn->next))
597 if ((ival = a2width(p, nn->string)) >= 0)
598 len = (size_t)ival;
599
600 switch (n->type) {
601 case (MAN_HEAD):
602 /* Handle zero-width lengths. */
603 if (0 == len)
604 len = term_len(p, 1);
605
606 p->offset = mt->offset;
607 p->rmargin = mt->offset + len;
608 if (ival < 0)
609 break;
610
611 /* Set the saved left-margin. */
612 mt->lmargin[mt->lmargincur] = (size_t)ival;
613
614 savelit = MANT_LITERAL & mt->fl;
615 mt->fl &= ~MANT_LITERAL;
616
617 if (n->child)
618 print_man_node(p, mt, n->child, m);
619
620 if (savelit)
621 mt->fl |= MANT_LITERAL;
622
623 return(0);
624 case (MAN_BODY):
625 p->offset = mt->offset + len;
626 p->rmargin = p->maxrmargin;
627 break;
628 default:
629 break;
630 }
631
632 return(1);
633 }
634
635
636 /* ARGSUSED */
637 static void
638 post_IP(DECL_ARGS)
639 {
640
641 switch (n->type) {
642 case (MAN_HEAD):
643 term_flushln(p);
644 p->flags &= ~TERMP_NOBREAK;
645 p->rmargin = p->maxrmargin;
646 break;
647 case (MAN_BODY):
648 term_newln(p);
649 break;
650 default:
651 break;
652 }
653 }
654
655
656 /* ARGSUSED */
657 static int
658 pre_TP(DECL_ARGS)
659 {
660 const struct man_node *nn;
661 size_t len;
662 int savelit, ival;
663
664 switch (n->type) {
665 case (MAN_HEAD):
666 p->flags |= TERMP_NOBREAK;
667 break;
668 case (MAN_BODY):
669 p->flags |= TERMP_NOSPACE;
670 break;
671 case (MAN_BLOCK):
672 print_bvspace(p, n);
673 /* FALLTHROUGH */
674 default:
675 return(1);
676 }
677
678 len = (size_t)mt->lmargin[mt->lmargincur];
679 ival = -1;
680
681 /* Calculate offset. */
682
683 if (NULL != (nn = n->parent->head->child))
684 if (nn->string && nn->parent->line == nn->line)
685 if ((ival = a2width(p, nn->string)) >= 0)
686 len = (size_t)ival;
687
688 switch (n->type) {
689 case (MAN_HEAD):
690 /* Handle zero-length properly. */
691 if (0 == len)
692 len = term_len(p, 1);
693
694 p->offset = mt->offset;
695 p->rmargin = mt->offset + len;
696
697 savelit = MANT_LITERAL & mt->fl;
698 mt->fl &= ~MANT_LITERAL;
699
700 /* Don't print same-line elements. */
701 for (nn = n->child; nn; nn = nn->next)
702 if (nn->line > n->line)
703 print_man_node(p, mt, nn, m);
704
705 if (savelit)
706 mt->fl |= MANT_LITERAL;
707 if (ival >= 0)
708 mt->lmargin[mt->lmargincur] = (size_t)ival;
709
710 return(0);
711 case (MAN_BODY):
712 p->offset = mt->offset + len;
713 p->rmargin = p->maxrmargin;
714 p->flags &= ~TERMP_NOBREAK;
715 p->flags &= ~TERMP_TWOSPACE;
716 break;
717 default:
718 break;
719 }
720
721 return(1);
722 }
723
724
725 /* ARGSUSED */
726 static void
727 post_TP(DECL_ARGS)
728 {
729
730 switch (n->type) {
731 case (MAN_HEAD):
732 term_flushln(p);
733 break;
734 case (MAN_BODY):
735 term_newln(p);
736 break;
737 default:
738 break;
739 }
740 }
741
742
743 /* ARGSUSED */
744 static int
745 pre_SS(DECL_ARGS)
746 {
747
748 switch (n->type) {
749 case (MAN_BLOCK):
750 mt->fl &= ~MANT_LITERAL;
751 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
752 mt->offset = term_len(p, p->defindent);
753 /* If following a prior empty `SS', no vspace. */
754 if (n->prev && MAN_SS == n->prev->tok)
755 if (NULL == n->prev->body->child)
756 break;
757 if (NULL == n->prev)
758 break;
759 term_vspace(p);
760 break;
761 case (MAN_HEAD):
762 term_fontrepl(p, TERMFONT_BOLD);
763 p->offset = term_len(p, p->defindent/2);
764 break;
765 case (MAN_BODY):
766 p->offset = mt->offset;
767 break;
768 default:
769 break;
770 }
771
772 return(1);
773 }
774
775
776 /* ARGSUSED */
777 static void
778 post_SS(DECL_ARGS)
779 {
780
781 switch (n->type) {
782 case (MAN_HEAD):
783 term_newln(p);
784 break;
785 case (MAN_BODY):
786 term_newln(p);
787 break;
788 default:
789 break;
790 }
791 }
792
793
794 /* ARGSUSED */
795 static int
796 pre_SH(DECL_ARGS)
797 {
798
799 switch (n->type) {
800 case (MAN_BLOCK):
801 mt->fl &= ~MANT_LITERAL;
802 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
803 mt->offset = term_len(p, p->defindent);
804 /* If following a prior empty `SH', no vspace. */
805 if (n->prev && MAN_SH == n->prev->tok)
806 if (NULL == n->prev->body->child)
807 break;
808 /* If the first macro, no vspae. */
809 if (NULL == n->prev)
810 break;
811 term_vspace(p);
812 break;
813 case (MAN_HEAD):
814 term_fontrepl(p, TERMFONT_BOLD);
815 p->offset = 0;
816 break;
817 case (MAN_BODY):
818 p->offset = mt->offset;
819 break;
820 default:
821 break;
822 }
823
824 return(1);
825 }
826
827
828 /* ARGSUSED */
829 static void
830 post_SH(DECL_ARGS)
831 {
832
833 switch (n->type) {
834 case (MAN_HEAD):
835 term_newln(p);
836 break;
837 case (MAN_BODY):
838 term_newln(p);
839 break;
840 default:
841 break;
842 }
843 }
844
845 /* ARGSUSED */
846 static int
847 pre_RS(DECL_ARGS)
848 {
849 int ival;
850 size_t sz;
851
852 switch (n->type) {
853 case (MAN_BLOCK):
854 term_newln(p);
855 return(1);
856 case (MAN_HEAD):
857 return(0);
858 default:
859 break;
860 }
861
862 sz = term_len(p, p->defindent);
863
864 if (NULL != (n = n->parent->head->child))
865 if ((ival = a2width(p, n->string)) >= 0)
866 sz = (size_t)ival;
867
868 mt->offset += sz;
869 p->rmargin = p->maxrmargin;
870 p->offset = mt->offset < p->rmargin ? mt->offset : p->rmargin;
871
872 if (++mt->lmarginsz < MAXMARGINS)
873 mt->lmargincur = mt->lmarginsz;
874
875 mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
876 return(1);
877 }
878
879 /* ARGSUSED */
880 static void
881 post_RS(DECL_ARGS)
882 {
883 int ival;
884 size_t sz;
885
886 switch (n->type) {
887 case (MAN_BLOCK):
888 return;
889 case (MAN_HEAD):
890 return;
891 default:
892 term_newln(p);
893 break;
894 }
895
896 sz = term_len(p, p->defindent);
897
898 if (NULL != (n = n->parent->head->child))
899 if ((ival = a2width(p, n->string)) >= 0)
900 sz = (size_t)ival;
901
902 mt->offset = mt->offset < sz ? 0 : mt->offset - sz;
903 p->offset = mt->offset;
904
905 if (--mt->lmarginsz < MAXMARGINS)
906 mt->lmargincur = mt->lmarginsz;
907 }
908
909 static void
910 print_man_node(DECL_ARGS)
911 {
912 size_t rm, rmax;
913 int c;
914
915 switch (n->type) {
916 case(MAN_TEXT):
917 /*
918 * If we have a blank line, output a vertical space.
919 * If we have a space as the first character, break
920 * before printing the line's data.
921 */
922 if ('\0' == *n->string) {
923 term_vspace(p);
924 return;
925 } else if (' ' == *n->string && MAN_LINE & n->flags)
926 term_newln(p);
927
928 term_word(p, n->string);
929 goto out;
930
931 case (MAN_EQN):
932 term_eqn(p, n->eqn);
933 return;
934 case (MAN_TBL):
935 /*
936 * Tables are preceded by a newline. Then process a
937 * table line, which will cause line termination,
938 */
939 if (TBL_SPAN_FIRST & n->span->flags)
940 term_newln(p);
941 term_tbl(p, n->span);
942 return;
943 default:
944 break;
945 }
946
947 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
948 term_fontrepl(p, TERMFONT_NONE);
949
950 c = 1;
951 if (termacts[n->tok].pre)
952 c = (*termacts[n->tok].pre)(p, mt, n, m);
953
954 if (c && n->child)
955 print_man_nodelist(p, mt, n->child, m);
956
957 if (termacts[n->tok].post)
958 (*termacts[n->tok].post)(p, mt, n, m);
959 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
960 term_fontrepl(p, TERMFONT_NONE);
961
962 out:
963 /*
964 * If we're in a literal context, make sure that words
965 * together on the same line stay together. This is a
966 * POST-printing call, so we check the NEXT word. Since
967 * -man doesn't have nested macros, we don't need to be
968 * more specific than this.
969 */
970 if (MANT_LITERAL & mt->fl && ! (TERMP_NOBREAK & p->flags) &&
971 NULL != n->next && n->next->line > n->line) {
972 rm = p->rmargin;
973 rmax = p->maxrmargin;
974 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
975 p->flags |= TERMP_NOSPACE;
976 if (NULL != n->string && '\0' != *n->string)
977 term_flushln(p);
978 else
979 term_newln(p);
980 if (rm < rmax && n->parent->tok == MAN_HP) {
981 p->offset = rm;
982 p->rmargin = rmax;
983 } else
984 p->rmargin = rm;
985 p->maxrmargin = rmax;
986 }
987 if (MAN_EOS & n->flags)
988 p->flags |= TERMP_SENTENCE;
989 }
990
991
992 static void
993 print_man_nodelist(DECL_ARGS)
994 {
995
996 print_man_node(p, mt, n, m);
997 if ( ! n->next)
998 return;
999 print_man_nodelist(p, mt, n->next, m);
1000 }
1001
1002
1003 static void
1004 print_man_foot(struct termp *p, const void *arg)
1005 {
1006 char title[BUFSIZ];
1007 size_t datelen;
1008 const struct man_meta *meta;
1009
1010 meta = (const struct man_meta *)arg;
1011 assert(meta->title);
1012 assert(meta->msec);
1013 assert(meta->date);
1014
1015 term_fontrepl(p, TERMFONT_NONE);
1016
1017 term_vspace(p);
1018
1019 /*
1020 * Temporary, undocumented option to imitate mdoc(7) output.
1021 * In the bottom right corner, use the source instead of
1022 * the title.
1023 */
1024
1025 if ( ! p->mdocstyle) {
1026 term_vspace(p);
1027 term_vspace(p);
1028 snprintf(title, BUFSIZ, "%s(%s)", meta->title, meta->msec);
1029 } else if (meta->source) {
1030 strlcpy(title, meta->source, BUFSIZ);
1031 } else {
1032 title[0] = '\0';
1033 }
1034 datelen = term_strlen(p, meta->date);
1035
1036 /* Bottom left corner: manual source. */
1037
1038 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1039 p->offset = 0;
1040 p->rmargin = (p->maxrmargin - datelen + term_len(p, 1)) / 2;
1041
1042 if (meta->source)
1043 term_word(p, meta->source);
1044 term_flushln(p);
1045
1046 /* At the bottom in the middle: manual date. */
1047
1048 p->flags |= TERMP_NOSPACE;
1049 p->offset = p->rmargin;
1050 p->rmargin = p->maxrmargin - term_strlen(p, title);
1051 if (p->offset + datelen >= p->rmargin)
1052 p->rmargin = p->offset + datelen;
1053
1054 term_word(p, meta->date);
1055 term_flushln(p);
1056
1057 /* Bottom right corner: manual title and section. */
1058
1059 p->flags &= ~TERMP_NOBREAK;
1060 p->flags |= TERMP_NOSPACE;
1061 p->offset = p->rmargin;
1062 p->rmargin = p->maxrmargin;
1063
1064 term_word(p, title);
1065 term_flushln(p);
1066 }
1067
1068
1069 static void
1070 print_man_head(struct termp *p, const void *arg)
1071 {
1072 char buf[BUFSIZ], title[BUFSIZ];
1073 size_t buflen, titlen;
1074 const struct man_meta *m;
1075
1076 m = (const struct man_meta *)arg;
1077 assert(m->title);
1078 assert(m->msec);
1079
1080 if (m->vol)
1081 strlcpy(buf, m->vol, BUFSIZ);
1082 else
1083 buf[0] = '\0';
1084 buflen = term_strlen(p, buf);
1085
1086 /* Top left corner: manual title and section. */
1087
1088 snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1089 titlen = term_strlen(p, title);
1090
1091 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1092 p->offset = 0;
1093 p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1094 (p->maxrmargin -
1095 term_strlen(p, buf) + term_len(p, 1)) / 2 :
1096 p->maxrmargin - buflen;
1097
1098 term_word(p, title);
1099 term_flushln(p);
1100
1101 /* At the top in the middle: manual volume. */
1102
1103 p->flags |= TERMP_NOSPACE;
1104 p->offset = p->rmargin;
1105 p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
1106 p->maxrmargin - titlen : p->maxrmargin;
1107
1108 term_word(p, buf);
1109 term_flushln(p);
1110
1111 /* Top right corner: title and section, again. */
1112
1113 p->flags &= ~TERMP_NOBREAK;
1114 if (p->rmargin + titlen <= p->maxrmargin) {
1115 p->flags |= TERMP_NOSPACE;
1116 p->offset = p->rmargin;
1117 p->rmargin = p->maxrmargin;
1118 term_word(p, title);
1119 term_flushln(p);
1120 }
1121
1122 p->flags &= ~TERMP_NOSPACE;
1123 p->offset = 0;
1124 p->rmargin = p->maxrmargin;
1125
1126 /*
1127 * Groff prints three blank lines before the content.
1128 * Do the same, except in the temporary, undocumented
1129 * mode imitating mdoc(7) output.
1130 */
1131
1132 term_vspace(p);
1133 if ( ! p->mdocstyle) {
1134 term_vspace(p);
1135 term_vspace(p);
1136 }
1137 }