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