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