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