]> git.cameronkatri.com Git - mandoc.git/blob - man_term.c
The "value" argument to the roff(7) .nr requests ends right before
[mandoc.git] / man_term.c
1 /* $Id: man_term.c,v 1.138 2013/11/11 00:37:55 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012, 2013 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->flags &= ~(TERMP_NOBREAK | TERMP_TWOSPACE);
269 p->flags |= TERMP_NOSPACE;
270 }
271
272 return(0);
273 }
274
275 /* ARGSUSED */
276 static int
277 pre_PD(DECL_ARGS)
278 {
279
280 n = n->child;
281 if (0 == n) {
282 mt->pardist = 1;
283 return(0);
284 }
285 assert(MAN_TEXT == n->type);
286 mt->pardist = atoi(n->string);
287 return(0);
288 }
289
290 /* ARGSUSED */
291 static int
292 pre_alternate(DECL_ARGS)
293 {
294 enum termfont font[2];
295 const struct man_node *nn;
296 int savelit, i;
297
298 switch (n->tok) {
299 case (MAN_RB):
300 font[0] = TERMFONT_NONE;
301 font[1] = TERMFONT_BOLD;
302 break;
303 case (MAN_RI):
304 font[0] = TERMFONT_NONE;
305 font[1] = TERMFONT_UNDER;
306 break;
307 case (MAN_BR):
308 font[0] = TERMFONT_BOLD;
309 font[1] = TERMFONT_NONE;
310 break;
311 case (MAN_BI):
312 font[0] = TERMFONT_BOLD;
313 font[1] = TERMFONT_UNDER;
314 break;
315 case (MAN_IR):
316 font[0] = TERMFONT_UNDER;
317 font[1] = TERMFONT_NONE;
318 break;
319 case (MAN_IB):
320 font[0] = TERMFONT_UNDER;
321 font[1] = TERMFONT_BOLD;
322 break;
323 default:
324 abort();
325 }
326
327 savelit = MANT_LITERAL & mt->fl;
328 mt->fl &= ~MANT_LITERAL;
329
330 for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
331 term_fontrepl(p, font[i]);
332 if (savelit && NULL == nn->next)
333 mt->fl |= MANT_LITERAL;
334 print_man_node(p, mt, nn, meta);
335 if (nn->next)
336 p->flags |= TERMP_NOSPACE;
337 }
338
339 return(0);
340 }
341
342 /* ARGSUSED */
343 static int
344 pre_B(DECL_ARGS)
345 {
346
347 term_fontrepl(p, TERMFONT_BOLD);
348 return(1);
349 }
350
351 /* ARGSUSED */
352 static int
353 pre_OP(DECL_ARGS)
354 {
355
356 term_word(p, "[");
357 p->flags |= TERMP_NOSPACE;
358
359 if (NULL != (n = n->child)) {
360 term_fontrepl(p, TERMFONT_BOLD);
361 term_word(p, n->string);
362 }
363 if (NULL != n && NULL != n->next) {
364 term_fontrepl(p, TERMFONT_UNDER);
365 term_word(p, n->next->string);
366 }
367
368 term_fontrepl(p, TERMFONT_NONE);
369 p->flags |= TERMP_NOSPACE;
370 term_word(p, "]");
371 return(0);
372 }
373
374 /* ARGSUSED */
375 static int
376 pre_ft(DECL_ARGS)
377 {
378 const char *cp;
379
380 if (NULL == n->child) {
381 term_fontlast(p);
382 return(0);
383 }
384
385 cp = n->child->string;
386 switch (*cp) {
387 case ('4'):
388 /* FALLTHROUGH */
389 case ('3'):
390 /* FALLTHROUGH */
391 case ('B'):
392 term_fontrepl(p, TERMFONT_BOLD);
393 break;
394 case ('2'):
395 /* FALLTHROUGH */
396 case ('I'):
397 term_fontrepl(p, TERMFONT_UNDER);
398 break;
399 case ('P'):
400 term_fontlast(p);
401 break;
402 case ('1'):
403 /* FALLTHROUGH */
404 case ('C'):
405 /* FALLTHROUGH */
406 case ('R'):
407 term_fontrepl(p, TERMFONT_NONE);
408 break;
409 default:
410 break;
411 }
412 return(0);
413 }
414
415 /* ARGSUSED */
416 static int
417 pre_in(DECL_ARGS)
418 {
419 int len, less;
420 size_t v;
421 const char *cp;
422
423 term_newln(p);
424
425 if (NULL == n->child) {
426 p->offset = mt->offset;
427 return(0);
428 }
429
430 cp = n->child->string;
431 less = 0;
432
433 if ('-' == *cp)
434 less = -1;
435 else if ('+' == *cp)
436 less = 1;
437 else
438 cp--;
439
440 if ((len = a2width(p, ++cp)) < 0)
441 return(0);
442
443 v = (size_t)len;
444
445 if (less < 0)
446 p->offset -= p->offset > v ? v : p->offset;
447 else if (less > 0)
448 p->offset += v;
449 else
450 p->offset = v;
451
452 /* Don't let this creep beyond the right margin. */
453
454 if (p->offset > p->rmargin)
455 p->offset = p->rmargin;
456
457 return(0);
458 }
459
460
461 /* ARGSUSED */
462 static int
463 pre_sp(DECL_ARGS)
464 {
465 char *s;
466 size_t i, len;
467 int neg;
468
469 if ((NULL == n->prev && n->parent)) {
470 switch (n->parent->tok) {
471 case (MAN_SH):
472 /* FALLTHROUGH */
473 case (MAN_SS):
474 /* FALLTHROUGH */
475 case (MAN_PP):
476 /* FALLTHROUGH */
477 case (MAN_LP):
478 /* FALLTHROUGH */
479 case (MAN_P):
480 /* FALLTHROUGH */
481 return(0);
482 default:
483 break;
484 }
485 }
486
487 neg = 0;
488 switch (n->tok) {
489 case (MAN_br):
490 len = 0;
491 break;
492 default:
493 if (NULL == n->child) {
494 len = 1;
495 break;
496 }
497 s = n->child->string;
498 if ('-' == *s) {
499 neg = 1;
500 s++;
501 }
502 len = a2height(p, s);
503 break;
504 }
505
506 if (0 == len)
507 term_newln(p);
508 else if (neg)
509 p->skipvsp += len;
510 else
511 for (i = 0; i < len; i++)
512 term_vspace(p);
513
514 return(0);
515 }
516
517
518 /* ARGSUSED */
519 static int
520 pre_HP(DECL_ARGS)
521 {
522 size_t len, one;
523 int ival;
524 const struct man_node *nn;
525
526 switch (n->type) {
527 case (MAN_BLOCK):
528 print_bvspace(p, n, mt->pardist);
529 return(1);
530 case (MAN_BODY):
531 break;
532 default:
533 return(0);
534 }
535
536 if ( ! (MANT_LITERAL & mt->fl)) {
537 p->flags |= TERMP_NOBREAK;
538 p->flags |= TERMP_TWOSPACE;
539 }
540
541 len = mt->lmargin[mt->lmargincur];
542 ival = -1;
543
544 /* Calculate offset. */
545
546 if (NULL != (nn = n->parent->head->child))
547 if ((ival = a2width(p, nn->string)) >= 0)
548 len = (size_t)ival;
549
550 one = term_len(p, 1);
551 if (len < one)
552 len = one;
553
554 p->offset = mt->offset;
555 p->rmargin = mt->offset + len;
556
557 if (ival >= 0)
558 mt->lmargin[mt->lmargincur] = (size_t)ival;
559
560 return(1);
561 }
562
563
564 /* ARGSUSED */
565 static void
566 post_HP(DECL_ARGS)
567 {
568
569 switch (n->type) {
570 case (MAN_BODY):
571 term_newln(p);
572 p->flags &= ~TERMP_NOBREAK;
573 p->flags &= ~TERMP_TWOSPACE;
574 p->offset = mt->offset;
575 p->rmargin = p->maxrmargin;
576 break;
577 default:
578 break;
579 }
580 }
581
582
583 /* ARGSUSED */
584 static int
585 pre_PP(DECL_ARGS)
586 {
587
588 switch (n->type) {
589 case (MAN_BLOCK):
590 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
591 print_bvspace(p, n, mt->pardist);
592 break;
593 default:
594 p->offset = mt->offset;
595 break;
596 }
597
598 return(MAN_HEAD != n->type);
599 }
600
601
602 /* ARGSUSED */
603 static int
604 pre_IP(DECL_ARGS)
605 {
606 const struct man_node *nn;
607 size_t len;
608 int savelit, ival;
609
610 switch (n->type) {
611 case (MAN_BODY):
612 p->flags |= TERMP_NOSPACE;
613 break;
614 case (MAN_HEAD):
615 p->flags |= TERMP_NOBREAK;
616 break;
617 case (MAN_BLOCK):
618 print_bvspace(p, n, mt->pardist);
619 /* FALLTHROUGH */
620 default:
621 return(1);
622 }
623
624 len = mt->lmargin[mt->lmargincur];
625 ival = -1;
626
627 /* Calculate the offset from the optional second argument. */
628 if (NULL != (nn = n->parent->head->child))
629 if (NULL != (nn = nn->next))
630 if ((ival = a2width(p, nn->string)) >= 0)
631 len = (size_t)ival;
632
633 switch (n->type) {
634 case (MAN_HEAD):
635 /* Handle zero-width lengths. */
636 if (0 == len)
637 len = term_len(p, 1);
638
639 p->offset = mt->offset;
640 p->rmargin = mt->offset + len;
641 if (ival < 0)
642 break;
643
644 /* Set the saved left-margin. */
645 mt->lmargin[mt->lmargincur] = (size_t)ival;
646
647 savelit = MANT_LITERAL & mt->fl;
648 mt->fl &= ~MANT_LITERAL;
649
650 if (n->child)
651 print_man_node(p, mt, n->child, meta);
652
653 if (savelit)
654 mt->fl |= MANT_LITERAL;
655
656 return(0);
657 case (MAN_BODY):
658 p->offset = mt->offset + len;
659 p->rmargin = p->maxrmargin;
660 break;
661 default:
662 break;
663 }
664
665 return(1);
666 }
667
668
669 /* ARGSUSED */
670 static void
671 post_IP(DECL_ARGS)
672 {
673
674 switch (n->type) {
675 case (MAN_HEAD):
676 term_flushln(p);
677 p->flags &= ~TERMP_NOBREAK;
678 p->rmargin = p->maxrmargin;
679 break;
680 case (MAN_BODY):
681 term_newln(p);
682 p->offset = mt->offset;
683 break;
684 default:
685 break;
686 }
687 }
688
689
690 /* ARGSUSED */
691 static int
692 pre_TP(DECL_ARGS)
693 {
694 const struct man_node *nn;
695 size_t len;
696 int savelit, ival;
697
698 switch (n->type) {
699 case (MAN_HEAD):
700 p->flags |= TERMP_NOBREAK;
701 break;
702 case (MAN_BODY):
703 p->flags |= TERMP_NOSPACE;
704 break;
705 case (MAN_BLOCK):
706 print_bvspace(p, n, mt->pardist);
707 /* FALLTHROUGH */
708 default:
709 return(1);
710 }
711
712 len = (size_t)mt->lmargin[mt->lmargincur];
713 ival = -1;
714
715 /* Calculate offset. */
716
717 if (NULL != (nn = n->parent->head->child))
718 if (nn->string && nn->parent->line == nn->line)
719 if ((ival = a2width(p, nn->string)) >= 0)
720 len = (size_t)ival;
721
722 switch (n->type) {
723 case (MAN_HEAD):
724 /* Handle zero-length properly. */
725 if (0 == len)
726 len = term_len(p, 1);
727
728 p->offset = mt->offset;
729 p->rmargin = mt->offset + len;
730
731 savelit = MANT_LITERAL & mt->fl;
732 mt->fl &= ~MANT_LITERAL;
733
734 /* Don't print same-line elements. */
735 for (nn = n->child; nn; nn = nn->next)
736 if (nn->line > n->line)
737 print_man_node(p, mt, nn, meta);
738
739 if (savelit)
740 mt->fl |= MANT_LITERAL;
741 if (ival >= 0)
742 mt->lmargin[mt->lmargincur] = (size_t)ival;
743
744 return(0);
745 case (MAN_BODY):
746 p->offset = mt->offset + len;
747 p->rmargin = p->maxrmargin;
748 p->flags &= ~TERMP_NOBREAK;
749 p->flags &= ~TERMP_TWOSPACE;
750 break;
751 default:
752 break;
753 }
754
755 return(1);
756 }
757
758
759 /* ARGSUSED */
760 static void
761 post_TP(DECL_ARGS)
762 {
763
764 switch (n->type) {
765 case (MAN_HEAD):
766 term_flushln(p);
767 break;
768 case (MAN_BODY):
769 term_newln(p);
770 p->offset = mt->offset;
771 break;
772 default:
773 break;
774 }
775 }
776
777
778 /* ARGSUSED */
779 static int
780 pre_SS(DECL_ARGS)
781 {
782 int i;
783
784 switch (n->type) {
785 case (MAN_BLOCK):
786 mt->fl &= ~MANT_LITERAL;
787 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
788 mt->offset = term_len(p, p->defindent);
789 /* If following a prior empty `SS', no vspace. */
790 if (n->prev && MAN_SS == n->prev->tok)
791 if (NULL == n->prev->body->child)
792 break;
793 if (NULL == n->prev)
794 break;
795 for (i = 0; i < mt->pardist; i++)
796 term_vspace(p);
797 break;
798 case (MAN_HEAD):
799 term_fontrepl(p, TERMFONT_BOLD);
800 p->offset = term_len(p, 3);
801 break;
802 case (MAN_BODY):
803 p->offset = mt->offset;
804 break;
805 default:
806 break;
807 }
808
809 return(1);
810 }
811
812
813 /* ARGSUSED */
814 static void
815 post_SS(DECL_ARGS)
816 {
817
818 switch (n->type) {
819 case (MAN_HEAD):
820 term_newln(p);
821 break;
822 case (MAN_BODY):
823 term_newln(p);
824 break;
825 default:
826 break;
827 }
828 }
829
830
831 /* ARGSUSED */
832 static int
833 pre_SH(DECL_ARGS)
834 {
835 int i;
836
837 switch (n->type) {
838 case (MAN_BLOCK):
839 mt->fl &= ~MANT_LITERAL;
840 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
841 mt->offset = term_len(p, p->defindent);
842 /* If following a prior empty `SH', no vspace. */
843 if (n->prev && MAN_SH == n->prev->tok)
844 if (NULL == n->prev->body->child)
845 break;
846 /* If the first macro, no vspae. */
847 if (NULL == n->prev)
848 break;
849 for (i = 0; i < mt->pardist; i++)
850 term_vspace(p);
851 break;
852 case (MAN_HEAD):
853 term_fontrepl(p, TERMFONT_BOLD);
854 p->offset = 0;
855 break;
856 case (MAN_BODY):
857 p->offset = mt->offset;
858 break;
859 default:
860 break;
861 }
862
863 return(1);
864 }
865
866
867 /* ARGSUSED */
868 static void
869 post_SH(DECL_ARGS)
870 {
871
872 switch (n->type) {
873 case (MAN_HEAD):
874 term_newln(p);
875 break;
876 case (MAN_BODY):
877 term_newln(p);
878 break;
879 default:
880 break;
881 }
882 }
883
884 /* ARGSUSED */
885 static int
886 pre_RS(DECL_ARGS)
887 {
888 int ival;
889 size_t sz;
890
891 switch (n->type) {
892 case (MAN_BLOCK):
893 term_newln(p);
894 return(1);
895 case (MAN_HEAD):
896 return(0);
897 default:
898 break;
899 }
900
901 sz = term_len(p, p->defindent);
902
903 if (NULL != (n = n->parent->head->child))
904 if ((ival = a2width(p, n->string)) >= 0)
905 sz = (size_t)ival;
906
907 mt->offset += sz;
908 p->rmargin = p->maxrmargin;
909 p->offset = mt->offset < p->rmargin ? mt->offset : p->rmargin;
910
911 if (++mt->lmarginsz < MAXMARGINS)
912 mt->lmargincur = mt->lmarginsz;
913
914 mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
915 return(1);
916 }
917
918 /* ARGSUSED */
919 static void
920 post_RS(DECL_ARGS)
921 {
922 int ival;
923 size_t sz;
924
925 switch (n->type) {
926 case (MAN_BLOCK):
927 return;
928 case (MAN_HEAD):
929 return;
930 default:
931 term_newln(p);
932 break;
933 }
934
935 sz = term_len(p, p->defindent);
936
937 if (NULL != (n = n->parent->head->child))
938 if ((ival = a2width(p, n->string)) >= 0)
939 sz = (size_t)ival;
940
941 mt->offset = mt->offset < sz ? 0 : mt->offset - sz;
942 p->offset = mt->offset;
943
944 if (--mt->lmarginsz < MAXMARGINS)
945 mt->lmargincur = mt->lmarginsz;
946 }
947
948 /* ARGSUSED */
949 static int
950 pre_UR(DECL_ARGS)
951 {
952
953 return (MAN_HEAD != n->type);
954 }
955
956 /* ARGSUSED */
957 static void
958 post_UR(DECL_ARGS)
959 {
960
961 if (MAN_BLOCK != n->type)
962 return;
963
964 term_word(p, "<");
965 p->flags |= TERMP_NOSPACE;
966
967 if (NULL != n->child->child)
968 print_man_node(p, mt, n->child->child, meta);
969
970 p->flags |= TERMP_NOSPACE;
971 term_word(p, ">");
972 }
973
974 static void
975 print_man_node(DECL_ARGS)
976 {
977 size_t rm, rmax;
978 int c;
979
980 switch (n->type) {
981 case(MAN_TEXT):
982 /*
983 * If we have a blank line, output a vertical space.
984 * If we have a space as the first character, break
985 * before printing the line's data.
986 */
987 if ('\0' == *n->string) {
988 term_vspace(p);
989 return;
990 } else if (' ' == *n->string && MAN_LINE & n->flags)
991 term_newln(p);
992
993 term_word(p, n->string);
994 goto out;
995
996 case (MAN_EQN):
997 term_eqn(p, n->eqn);
998 return;
999 case (MAN_TBL):
1000 /*
1001 * Tables are preceded by a newline. Then process a
1002 * table line, which will cause line termination,
1003 */
1004 if (TBL_SPAN_FIRST & n->span->flags)
1005 term_newln(p);
1006 term_tbl(p, n->span);
1007 return;
1008 default:
1009 break;
1010 }
1011
1012 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
1013 term_fontrepl(p, TERMFONT_NONE);
1014
1015 c = 1;
1016 if (termacts[n->tok].pre)
1017 c = (*termacts[n->tok].pre)(p, mt, n, meta);
1018
1019 if (c && n->child)
1020 print_man_nodelist(p, mt, n->child, meta);
1021
1022 if (termacts[n->tok].post)
1023 (*termacts[n->tok].post)(p, mt, n, meta);
1024 if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
1025 term_fontrepl(p, TERMFONT_NONE);
1026
1027 out:
1028 /*
1029 * If we're in a literal context, make sure that words
1030 * together on the same line stay together. This is a
1031 * POST-printing call, so we check the NEXT word. Since
1032 * -man doesn't have nested macros, we don't need to be
1033 * more specific than this.
1034 */
1035 if (MANT_LITERAL & mt->fl && ! (TERMP_NOBREAK & p->flags) &&
1036 (NULL == n->next || n->next->line > n->line)) {
1037 rm = p->rmargin;
1038 rmax = p->maxrmargin;
1039 p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
1040 p->flags |= TERMP_NOSPACE;
1041 if (NULL != n->string && '\0' != *n->string)
1042 term_flushln(p);
1043 else
1044 term_newln(p);
1045 if (rm < rmax && n->parent->tok == MAN_HP) {
1046 p->offset = rm;
1047 p->rmargin = rmax;
1048 } else
1049 p->rmargin = rm;
1050 p->maxrmargin = rmax;
1051 }
1052 if (MAN_EOS & n->flags)
1053 p->flags |= TERMP_SENTENCE;
1054 }
1055
1056
1057 static void
1058 print_man_nodelist(DECL_ARGS)
1059 {
1060
1061 print_man_node(p, mt, n, meta);
1062 if ( ! n->next)
1063 return;
1064 print_man_nodelist(p, mt, n->next, meta);
1065 }
1066
1067
1068 static void
1069 print_man_foot(struct termp *p, const void *arg)
1070 {
1071 char title[BUFSIZ];
1072 size_t datelen;
1073 const struct man_meta *meta;
1074
1075 meta = (const struct man_meta *)arg;
1076 assert(meta->title);
1077 assert(meta->msec);
1078 assert(meta->date);
1079
1080 term_fontrepl(p, TERMFONT_NONE);
1081
1082 term_vspace(p);
1083
1084 /*
1085 * Temporary, undocumented option to imitate mdoc(7) output.
1086 * In the bottom right corner, use the source instead of
1087 * the title.
1088 */
1089
1090 if ( ! p->mdocstyle) {
1091 term_vspace(p);
1092 term_vspace(p);
1093 snprintf(title, BUFSIZ, "%s(%s)", meta->title, meta->msec);
1094 } else if (meta->source) {
1095 strlcpy(title, meta->source, BUFSIZ);
1096 } else {
1097 title[0] = '\0';
1098 }
1099 datelen = term_strlen(p, meta->date);
1100
1101 /* Bottom left corner: manual source. */
1102
1103 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1104 p->offset = 0;
1105 p->rmargin = (p->maxrmargin - datelen + term_len(p, 1)) / 2;
1106
1107 if (meta->source)
1108 term_word(p, meta->source);
1109 term_flushln(p);
1110
1111 /* At the bottom in the middle: manual date. */
1112
1113 p->flags |= TERMP_NOSPACE;
1114 p->offset = p->rmargin;
1115 p->rmargin = p->maxrmargin - term_strlen(p, title);
1116 if (p->offset + datelen >= p->rmargin)
1117 p->rmargin = p->offset + datelen;
1118
1119 term_word(p, meta->date);
1120 term_flushln(p);
1121
1122 /* Bottom right corner: manual title and section. */
1123
1124 p->flags &= ~TERMP_NOBREAK;
1125 p->flags |= TERMP_NOSPACE;
1126 p->offset = p->rmargin;
1127 p->rmargin = p->maxrmargin;
1128
1129 term_word(p, title);
1130 term_flushln(p);
1131 }
1132
1133
1134 static void
1135 print_man_head(struct termp *p, const void *arg)
1136 {
1137 char buf[BUFSIZ], title[BUFSIZ];
1138 size_t buflen, titlen;
1139 const struct man_meta *meta;
1140
1141 meta = (const struct man_meta *)arg;
1142 assert(meta->title);
1143 assert(meta->msec);
1144
1145 if (meta->vol)
1146 strlcpy(buf, meta->vol, BUFSIZ);
1147 else
1148 buf[0] = '\0';
1149 buflen = term_strlen(p, buf);
1150
1151 /* Top left corner: manual title and section. */
1152
1153 snprintf(title, BUFSIZ, "%s(%s)", meta->title, meta->msec);
1154 titlen = term_strlen(p, title);
1155
1156 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1157 p->offset = 0;
1158 p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1159 (p->maxrmargin -
1160 term_strlen(p, buf) + term_len(p, 1)) / 2 :
1161 p->maxrmargin - buflen;
1162
1163 term_word(p, title);
1164 term_flushln(p);
1165
1166 /* At the top in the middle: manual volume. */
1167
1168 p->flags |= TERMP_NOSPACE;
1169 p->offset = p->rmargin;
1170 p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
1171 p->maxrmargin - titlen : p->maxrmargin;
1172
1173 term_word(p, buf);
1174 term_flushln(p);
1175
1176 /* Top right corner: title and section, again. */
1177
1178 p->flags &= ~TERMP_NOBREAK;
1179 if (p->rmargin + titlen <= p->maxrmargin) {
1180 p->flags |= TERMP_NOSPACE;
1181 p->offset = p->rmargin;
1182 p->rmargin = p->maxrmargin;
1183 term_word(p, title);
1184 term_flushln(p);
1185 }
1186
1187 p->flags &= ~TERMP_NOSPACE;
1188 p->offset = 0;
1189 p->rmargin = p->maxrmargin;
1190
1191 /*
1192 * Groff prints three blank lines before the content.
1193 * Do the same, except in the temporary, undocumented
1194 * mode imitating mdoc(7) output.
1195 */
1196
1197 term_vspace(p);
1198 if ( ! p->mdocstyle) {
1199 term_vspace(p);
1200 term_vspace(p);
1201 }
1202 }