]> git.cameronkatri.com Git - mandoc.git/blob - man_term.c
Massively reduce the amount of text, cutting it down to what is needed
[mandoc.git] / man_term.c
1 /* $Id: man_term.c,v 1.215 2018/08/18 02:08:27 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015, 2017, 2018 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "mandoc_aux.h"
30 #include "mandoc.h"
31 #include "roff.h"
32 #include "man.h"
33 #include "out.h"
34 #include "term.h"
35 #include "main.h"
36
37 #define MAXMARGINS 64 /* maximum number of indented scopes */
38
39 struct mtermp {
40 int fl;
41 #define MANT_LITERAL (1 << 0)
42 int lmargin[MAXMARGINS]; /* margins (incl. vis. page) */
43 int lmargincur; /* index of current margin */
44 int lmarginsz; /* actual number of nested margins */
45 size_t offset; /* default offset to visible page */
46 int pardist; /* vert. space before par., unit: [v] */
47 };
48
49 #define DECL_ARGS struct termp *p, \
50 struct mtermp *mt, \
51 struct roff_node *n, \
52 const struct roff_meta *meta
53
54 struct man_term_act {
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 void print_man_nodelist(DECL_ARGS);
62 static void print_man_node(DECL_ARGS);
63 static void print_man_head(struct termp *,
64 const struct roff_meta *);
65 static void print_man_foot(struct termp *,
66 const struct roff_meta *);
67 static void print_bvspace(struct termp *,
68 const struct roff_node *, int);
69
70 static int pre_B(DECL_ARGS);
71 static int pre_DT(DECL_ARGS);
72 static int pre_HP(DECL_ARGS);
73 static int pre_I(DECL_ARGS);
74 static int pre_IP(DECL_ARGS);
75 static int pre_OP(DECL_ARGS);
76 static int pre_PD(DECL_ARGS);
77 static int pre_PP(DECL_ARGS);
78 static int pre_RS(DECL_ARGS);
79 static int pre_SH(DECL_ARGS);
80 static int pre_SS(DECL_ARGS);
81 static int pre_SY(DECL_ARGS);
82 static int pre_TP(DECL_ARGS);
83 static int pre_UR(DECL_ARGS);
84 static int pre_alternate(DECL_ARGS);
85 static int pre_ign(DECL_ARGS);
86 static int pre_in(DECL_ARGS);
87 static int pre_literal(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_SY(DECL_ARGS);
95 static void post_TP(DECL_ARGS);
96 static void post_UR(DECL_ARGS);
97
98 static const struct man_term_act man_term_acts[MAN_MAX - MAN_TH] = {
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_TP, post_TP, 0 }, /* TQ */
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_literal, NULL, 0 }, /* nf */
121 { pre_literal, NULL, 0 }, /* fi */
122 { NULL, NULL, 0 }, /* RE */
123 { pre_RS, post_RS, 0 }, /* RS */
124 { pre_DT, NULL, 0 }, /* DT */
125 { pre_ign, NULL, MAN_NOTEXT }, /* UC */
126 { pre_PD, NULL, MAN_NOTEXT }, /* PD */
127 { pre_ign, NULL, 0 }, /* AT */
128 { pre_in, NULL, MAN_NOTEXT }, /* in */
129 { pre_SY, post_SY, 0 }, /* SY */
130 { NULL, NULL, 0 }, /* YS */
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 { pre_UR, post_UR, 0 }, /* MT */
137 { NULL, NULL, 0 }, /* ME */
138 };
139 static const struct man_term_act *man_term_act(enum roff_tok);
140
141
142 static const struct man_term_act *
143 man_term_act(enum roff_tok tok)
144 {
145 assert(tok >= MAN_TH && tok <= MAN_MAX);
146 return man_term_acts + (tok - MAN_TH);
147 }
148
149 void
150 terminal_man(void *arg, const struct roff_man *man)
151 {
152 struct termp *p;
153 struct roff_node *n;
154 struct mtermp mt;
155 size_t save_defindent;
156
157 p = (struct termp *)arg;
158 save_defindent = p->defindent;
159 if (p->synopsisonly == 0 && p->defindent == 0)
160 p->defindent = 7;
161 p->tcol->rmargin = p->maxrmargin = p->defrmargin;
162 term_tab_set(p, NULL);
163 term_tab_set(p, "T");
164 term_tab_set(p, ".5i");
165
166 memset(&mt, 0, sizeof(struct mtermp));
167 mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
168 mt.offset = term_len(p, p->defindent);
169 mt.pardist = 1;
170
171 n = man->first->child;
172 if (p->synopsisonly) {
173 while (n != NULL) {
174 if (n->tok == MAN_SH &&
175 n->child->child->type == ROFFT_TEXT &&
176 !strcmp(n->child->child->string, "SYNOPSIS")) {
177 if (n->child->next->child != NULL)
178 print_man_nodelist(p, &mt,
179 n->child->next->child,
180 &man->meta);
181 term_newln(p);
182 break;
183 }
184 n = n->next;
185 }
186 } else {
187 term_begin(p, print_man_head, print_man_foot, &man->meta);
188 p->flags |= TERMP_NOSPACE;
189 if (n != NULL)
190 print_man_nodelist(p, &mt, n, &man->meta);
191 term_end(p);
192 }
193 p->defindent = save_defindent;
194 }
195
196 /*
197 * Printing leading vertical space before a block.
198 * This is used for the paragraph macros.
199 * The rules are pretty simple, since there's very little nesting going
200 * on here. Basically, if we're the first within another block (SS/SH),
201 * then don't emit vertical space. If we are (RS), then do. If not the
202 * first, print it.
203 */
204 static void
205 print_bvspace(struct termp *p, const struct roff_node *n, int pardist)
206 {
207 int i;
208
209 term_newln(p);
210
211 if (n->body && n->body->child)
212 if (n->body->child->type == ROFFT_TBL)
213 return;
214
215 if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
216 if (NULL == n->prev)
217 return;
218
219 for (i = 0; i < pardist; i++)
220 term_vspace(p);
221 }
222
223
224 static int
225 pre_ign(DECL_ARGS)
226 {
227
228 return 0;
229 }
230
231 static int
232 pre_I(DECL_ARGS)
233 {
234
235 term_fontrepl(p, TERMFONT_UNDER);
236 return 1;
237 }
238
239 static int
240 pre_literal(DECL_ARGS)
241 {
242
243 term_newln(p);
244
245 if (n->tok == MAN_nf || n->tok == MAN_EX)
246 mt->fl |= MANT_LITERAL;
247 else
248 mt->fl &= ~MANT_LITERAL;
249
250 /*
251 * Unlike .IP and .TP, .HP does not have a HEAD.
252 * So in case a second call to term_flushln() is needed,
253 * indentation has to be set up explicitly.
254 */
255 if (n->parent->tok == MAN_HP && p->tcol->rmargin < p->maxrmargin) {
256 p->tcol->offset = p->tcol->rmargin;
257 p->tcol->rmargin = p->maxrmargin;
258 p->trailspace = 0;
259 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
260 p->flags |= TERMP_NOSPACE;
261 }
262
263 return 0;
264 }
265
266 static int
267 pre_PD(DECL_ARGS)
268 {
269 struct roffsu su;
270
271 n = n->child;
272 if (n == NULL) {
273 mt->pardist = 1;
274 return 0;
275 }
276 assert(n->type == ROFFT_TEXT);
277 if (a2roffsu(n->string, &su, SCALE_VS) != NULL)
278 mt->pardist = term_vspan(p, &su);
279 return 0;
280 }
281
282 static int
283 pre_alternate(DECL_ARGS)
284 {
285 enum termfont font[2];
286 struct roff_node *nn;
287 int savelit, i;
288
289 switch (n->tok) {
290 case MAN_RB:
291 font[0] = TERMFONT_NONE;
292 font[1] = TERMFONT_BOLD;
293 break;
294 case MAN_RI:
295 font[0] = TERMFONT_NONE;
296 font[1] = TERMFONT_UNDER;
297 break;
298 case MAN_BR:
299 font[0] = TERMFONT_BOLD;
300 font[1] = TERMFONT_NONE;
301 break;
302 case MAN_BI:
303 font[0] = TERMFONT_BOLD;
304 font[1] = TERMFONT_UNDER;
305 break;
306 case MAN_IR:
307 font[0] = TERMFONT_UNDER;
308 font[1] = TERMFONT_NONE;
309 break;
310 case MAN_IB:
311 font[0] = TERMFONT_UNDER;
312 font[1] = TERMFONT_BOLD;
313 break;
314 default:
315 abort();
316 }
317
318 savelit = MANT_LITERAL & mt->fl;
319 mt->fl &= ~MANT_LITERAL;
320
321 for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
322 term_fontrepl(p, font[i]);
323 if (savelit && NULL == nn->next)
324 mt->fl |= MANT_LITERAL;
325 assert(nn->type == ROFFT_TEXT);
326 term_word(p, nn->string);
327 if (nn->flags & NODE_EOS)
328 p->flags |= TERMP_SENTENCE;
329 if (nn->next)
330 p->flags |= TERMP_NOSPACE;
331 }
332
333 return 0;
334 }
335
336 static int
337 pre_B(DECL_ARGS)
338 {
339
340 term_fontrepl(p, TERMFONT_BOLD);
341 return 1;
342 }
343
344 static int
345 pre_OP(DECL_ARGS)
346 {
347
348 term_word(p, "[");
349 p->flags |= TERMP_NOSPACE;
350
351 if (NULL != (n = n->child)) {
352 term_fontrepl(p, TERMFONT_BOLD);
353 term_word(p, n->string);
354 }
355 if (NULL != n && NULL != n->next) {
356 term_fontrepl(p, TERMFONT_UNDER);
357 term_word(p, n->next->string);
358 }
359
360 term_fontrepl(p, TERMFONT_NONE);
361 p->flags |= TERMP_NOSPACE;
362 term_word(p, "]");
363 return 0;
364 }
365
366 static int
367 pre_in(DECL_ARGS)
368 {
369 struct roffsu su;
370 const char *cp;
371 size_t v;
372 int less;
373
374 term_newln(p);
375
376 if (n->child == NULL) {
377 p->tcol->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 (a2roffsu(++cp, &su, SCALE_EN) == NULL)
392 return 0;
393
394 v = term_hen(p, &su);
395
396 if (less < 0)
397 p->tcol->offset -= p->tcol->offset > v ? v : p->tcol->offset;
398 else if (less > 0)
399 p->tcol->offset += v;
400 else
401 p->tcol->offset = v;
402 if (p->tcol->offset > SHRT_MAX)
403 p->tcol->offset = term_len(p, p->defindent);
404
405 return 0;
406 }
407
408 static int
409 pre_DT(DECL_ARGS)
410 {
411 term_tab_set(p, NULL);
412 term_tab_set(p, "T");
413 term_tab_set(p, ".5i");
414 return 0;
415 }
416
417 static int
418 pre_HP(DECL_ARGS)
419 {
420 struct roffsu su;
421 const struct roff_node *nn;
422 int len;
423
424 switch (n->type) {
425 case ROFFT_BLOCK:
426 print_bvspace(p, n, mt->pardist);
427 return 1;
428 case ROFFT_BODY:
429 break;
430 default:
431 return 0;
432 }
433
434 if ( ! (MANT_LITERAL & mt->fl)) {
435 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
436 p->trailspace = 2;
437 }
438
439 /* Calculate offset. */
440
441 if ((nn = n->parent->head->child) != NULL &&
442 a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
443 len = term_hen(p, &su);
444 if (len < 0 && (size_t)(-len) > mt->offset)
445 len = -mt->offset;
446 else if (len > SHRT_MAX)
447 len = term_len(p, p->defindent);
448 mt->lmargin[mt->lmargincur] = len;
449 } else
450 len = mt->lmargin[mt->lmargincur];
451
452 p->tcol->offset = mt->offset;
453 p->tcol->rmargin = mt->offset + len;
454 return 1;
455 }
456
457 static void
458 post_HP(DECL_ARGS)
459 {
460
461 switch (n->type) {
462 case ROFFT_BODY:
463 term_newln(p);
464
465 /*
466 * Compatibility with a groff bug.
467 * The .HP macro uses the undocumented .tag request
468 * which causes a line break and cancels no-space
469 * mode even if there isn't any output.
470 */
471
472 if (n->child == NULL)
473 term_vspace(p);
474
475 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
476 p->trailspace = 0;
477 p->tcol->offset = mt->offset;
478 p->tcol->rmargin = p->maxrmargin;
479 break;
480 default:
481 break;
482 }
483 }
484
485 static int
486 pre_PP(DECL_ARGS)
487 {
488
489 switch (n->type) {
490 case ROFFT_BLOCK:
491 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
492 print_bvspace(p, n, mt->pardist);
493 break;
494 default:
495 p->tcol->offset = mt->offset;
496 break;
497 }
498
499 return n->type != ROFFT_HEAD;
500 }
501
502 static int
503 pre_IP(DECL_ARGS)
504 {
505 struct roffsu su;
506 const struct roff_node *nn;
507 int len, savelit;
508
509 switch (n->type) {
510 case ROFFT_BODY:
511 p->flags |= TERMP_NOSPACE;
512 break;
513 case ROFFT_HEAD:
514 p->flags |= TERMP_NOBREAK;
515 p->trailspace = 1;
516 break;
517 case ROFFT_BLOCK:
518 print_bvspace(p, n, mt->pardist);
519 /* FALLTHROUGH */
520 default:
521 return 1;
522 }
523
524 /* Calculate the offset from the optional second argument. */
525 if ((nn = n->parent->head->child) != NULL &&
526 (nn = nn->next) != NULL &&
527 a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
528 len = term_hen(p, &su);
529 if (len < 0 && (size_t)(-len) > mt->offset)
530 len = -mt->offset;
531 else if (len > SHRT_MAX)
532 len = term_len(p, p->defindent);
533 mt->lmargin[mt->lmargincur] = len;
534 } else
535 len = mt->lmargin[mt->lmargincur];
536
537 switch (n->type) {
538 case ROFFT_HEAD:
539 p->tcol->offset = mt->offset;
540 p->tcol->rmargin = mt->offset + len;
541
542 savelit = MANT_LITERAL & mt->fl;
543 mt->fl &= ~MANT_LITERAL;
544
545 if (n->child)
546 print_man_node(p, mt, n->child, meta);
547
548 if (savelit)
549 mt->fl |= MANT_LITERAL;
550
551 return 0;
552 case ROFFT_BODY:
553 p->tcol->offset = mt->offset + len;
554 p->tcol->rmargin = p->maxrmargin;
555 break;
556 default:
557 break;
558 }
559
560 return 1;
561 }
562
563 static void
564 post_IP(DECL_ARGS)
565 {
566
567 switch (n->type) {
568 case ROFFT_HEAD:
569 term_flushln(p);
570 p->flags &= ~TERMP_NOBREAK;
571 p->trailspace = 0;
572 p->tcol->rmargin = p->maxrmargin;
573 break;
574 case ROFFT_BODY:
575 term_newln(p);
576 p->tcol->offset = mt->offset;
577 break;
578 default:
579 break;
580 }
581 }
582
583 static int
584 pre_TP(DECL_ARGS)
585 {
586 struct roffsu su;
587 struct roff_node *nn;
588 int len, savelit;
589
590 switch (n->type) {
591 case ROFFT_HEAD:
592 p->flags |= TERMP_NOBREAK | TERMP_BRTRSP;
593 p->trailspace = 1;
594 break;
595 case ROFFT_BODY:
596 p->flags |= TERMP_NOSPACE;
597 break;
598 case ROFFT_BLOCK:
599 if (n->tok == MAN_TP)
600 print_bvspace(p, n, mt->pardist);
601 /* FALLTHROUGH */
602 default:
603 return 1;
604 }
605
606 /* Calculate offset. */
607
608 if ((nn = n->parent->head->child) != NULL &&
609 nn->string != NULL && ! (NODE_LINE & nn->flags) &&
610 a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
611 len = term_hen(p, &su);
612 if (len < 0 && (size_t)(-len) > mt->offset)
613 len = -mt->offset;
614 else if (len > SHRT_MAX)
615 len = term_len(p, p->defindent);
616 mt->lmargin[mt->lmargincur] = len;
617 } else
618 len = mt->lmargin[mt->lmargincur];
619
620 switch (n->type) {
621 case ROFFT_HEAD:
622 p->tcol->offset = mt->offset;
623 p->tcol->rmargin = mt->offset + len;
624
625 savelit = MANT_LITERAL & mt->fl;
626 mt->fl &= ~MANT_LITERAL;
627
628 /* Don't print same-line elements. */
629 nn = n->child;
630 while (NULL != nn && 0 == (NODE_LINE & nn->flags))
631 nn = nn->next;
632
633 while (NULL != nn) {
634 print_man_node(p, mt, nn, meta);
635 nn = nn->next;
636 }
637
638 if (savelit)
639 mt->fl |= MANT_LITERAL;
640 return 0;
641 case ROFFT_BODY:
642 p->tcol->offset = mt->offset + len;
643 p->tcol->rmargin = p->maxrmargin;
644 p->trailspace = 0;
645 p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
646 break;
647 default:
648 break;
649 }
650
651 return 1;
652 }
653
654 static void
655 post_TP(DECL_ARGS)
656 {
657
658 switch (n->type) {
659 case ROFFT_HEAD:
660 term_flushln(p);
661 break;
662 case ROFFT_BODY:
663 term_newln(p);
664 p->tcol->offset = mt->offset;
665 break;
666 default:
667 break;
668 }
669 }
670
671 static int
672 pre_SS(DECL_ARGS)
673 {
674 int i;
675
676 switch (n->type) {
677 case ROFFT_BLOCK:
678 mt->fl &= ~MANT_LITERAL;
679 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
680 mt->offset = term_len(p, p->defindent);
681
682 /*
683 * No vertical space before the first subsection
684 * and after an empty subsection.
685 */
686
687 do {
688 n = n->prev;
689 } while (n != NULL && n->tok >= MAN_TH &&
690 man_term_act(n->tok)->flags & MAN_NOTEXT);
691 if (n == NULL || n->type == ROFFT_COMMENT ||
692 (n->tok == MAN_SS && n->body->child == NULL))
693 break;
694
695 for (i = 0; i < mt->pardist; i++)
696 term_vspace(p);
697 break;
698 case ROFFT_HEAD:
699 term_fontrepl(p, TERMFONT_BOLD);
700 p->tcol->offset = term_len(p, 3);
701 p->tcol->rmargin = mt->offset;
702 p->trailspace = mt->offset;
703 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
704 break;
705 case ROFFT_BODY:
706 p->tcol->offset = mt->offset;
707 p->tcol->rmargin = p->maxrmargin;
708 p->trailspace = 0;
709 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
710 break;
711 default:
712 break;
713 }
714
715 return 1;
716 }
717
718 static void
719 post_SS(DECL_ARGS)
720 {
721
722 switch (n->type) {
723 case ROFFT_HEAD:
724 term_newln(p);
725 break;
726 case ROFFT_BODY:
727 term_newln(p);
728 break;
729 default:
730 break;
731 }
732 }
733
734 static int
735 pre_SH(DECL_ARGS)
736 {
737 int i;
738
739 switch (n->type) {
740 case ROFFT_BLOCK:
741 mt->fl &= ~MANT_LITERAL;
742 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
743 mt->offset = term_len(p, p->defindent);
744
745 /*
746 * No vertical space before the first section
747 * and after an empty section.
748 */
749
750 do {
751 n = n->prev;
752 } while (n != NULL && n->tok >= MAN_TH &&
753 man_term_act(n->tok)->flags & MAN_NOTEXT);
754 if (n == NULL || n->type == ROFFT_COMMENT ||
755 (n->tok == MAN_SH && n->body->child == NULL))
756 break;
757
758 for (i = 0; i < mt->pardist; i++)
759 term_vspace(p);
760 break;
761 case ROFFT_HEAD:
762 term_fontrepl(p, TERMFONT_BOLD);
763 p->tcol->offset = 0;
764 p->tcol->rmargin = mt->offset;
765 p->trailspace = mt->offset;
766 p->flags |= TERMP_NOBREAK | TERMP_BRIND;
767 break;
768 case ROFFT_BODY:
769 p->tcol->offset = mt->offset;
770 p->tcol->rmargin = p->maxrmargin;
771 p->trailspace = 0;
772 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
773 break;
774 default:
775 break;
776 }
777
778 return 1;
779 }
780
781 static void
782 post_SH(DECL_ARGS)
783 {
784
785 switch (n->type) {
786 case ROFFT_HEAD:
787 term_newln(p);
788 break;
789 case ROFFT_BODY:
790 term_newln(p);
791 break;
792 default:
793 break;
794 }
795 }
796
797 static int
798 pre_RS(DECL_ARGS)
799 {
800 struct roffsu su;
801
802 switch (n->type) {
803 case ROFFT_BLOCK:
804 term_newln(p);
805 return 1;
806 case ROFFT_HEAD:
807 return 0;
808 default:
809 break;
810 }
811
812 n = n->parent->head;
813 n->aux = SHRT_MAX + 1;
814 if (n->child == NULL)
815 n->aux = mt->lmargin[mt->lmargincur];
816 else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL)
817 n->aux = term_hen(p, &su);
818 if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
819 n->aux = -mt->offset;
820 else if (n->aux > SHRT_MAX)
821 n->aux = term_len(p, p->defindent);
822
823 mt->offset += n->aux;
824 p->tcol->offset = mt->offset;
825 p->tcol->rmargin = p->maxrmargin;
826
827 if (++mt->lmarginsz < MAXMARGINS)
828 mt->lmargincur = mt->lmarginsz;
829
830 mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
831 return 1;
832 }
833
834 static void
835 post_RS(DECL_ARGS)
836 {
837
838 switch (n->type) {
839 case ROFFT_BLOCK:
840 return;
841 case ROFFT_HEAD:
842 return;
843 default:
844 term_newln(p);
845 break;
846 }
847
848 mt->offset -= n->parent->head->aux;
849 p->tcol->offset = mt->offset;
850
851 if (--mt->lmarginsz < MAXMARGINS)
852 mt->lmargincur = mt->lmarginsz;
853 }
854
855 static int
856 pre_SY(DECL_ARGS)
857 {
858 const struct roff_node *nn;
859 int len;
860
861 switch (n->type) {
862 case ROFFT_BLOCK:
863 print_bvspace(p, n, mt->pardist);
864 return 1;
865 case ROFFT_HEAD:
866 case ROFFT_BODY:
867 break;
868 default:
869 abort();
870 }
871
872 nn = n->parent->head->child;
873 len = nn == NULL ? 0 : term_strlen(p, nn->string) + 1;
874
875 switch (n->type) {
876 case ROFFT_HEAD:
877 p->tcol->offset = mt->offset;
878 p->tcol->rmargin = mt->offset + len;
879 p->flags |= TERMP_NOBREAK;
880 term_fontrepl(p, TERMFONT_BOLD);
881 break;
882 case ROFFT_BODY:
883 mt->lmargin[mt->lmargincur] = len;
884 p->tcol->offset = mt->offset + len;
885 p->tcol->rmargin = p->maxrmargin;
886 p->flags |= TERMP_NOSPACE;
887 break;
888 default:
889 abort();
890 }
891 return 1;
892 }
893
894 static void
895 post_SY(DECL_ARGS)
896 {
897 switch (n->type) {
898 case ROFFT_HEAD:
899 term_flushln(p);
900 p->flags &= ~TERMP_NOBREAK;
901 break;
902 case ROFFT_BODY:
903 term_newln(p);
904 p->tcol->offset = mt->offset;
905 break;
906 default:
907 break;
908 }
909 }
910
911 static int
912 pre_UR(DECL_ARGS)
913 {
914
915 return n->type != ROFFT_HEAD;
916 }
917
918 static void
919 post_UR(DECL_ARGS)
920 {
921
922 if (n->type != ROFFT_BLOCK)
923 return;
924
925 term_word(p, "<");
926 p->flags |= TERMP_NOSPACE;
927
928 if (NULL != n->child->child)
929 print_man_node(p, mt, n->child->child, meta);
930
931 p->flags |= TERMP_NOSPACE;
932 term_word(p, ">");
933 }
934
935 static void
936 print_man_node(DECL_ARGS)
937 {
938 const struct man_term_act *act;
939 int c;
940
941 switch (n->type) {
942 case ROFFT_TEXT:
943 /*
944 * If we have a blank line, output a vertical space.
945 * If we have a space as the first character, break
946 * before printing the line's data.
947 */
948 if (*n->string == '\0') {
949 if (p->flags & TERMP_NONEWLINE)
950 term_newln(p);
951 else
952 term_vspace(p);
953 return;
954 } else if (*n->string == ' ' && n->flags & NODE_LINE &&
955 (p->flags & TERMP_NONEWLINE) == 0)
956 term_newln(p);
957 else if (n->flags & NODE_DELIMC)
958 p->flags |= TERMP_NOSPACE;
959
960 term_word(p, n->string);
961 goto out;
962 case ROFFT_COMMENT:
963 return;
964 case ROFFT_EQN:
965 if ( ! (n->flags & NODE_LINE))
966 p->flags |= TERMP_NOSPACE;
967 term_eqn(p, n->eqn);
968 if (n->next != NULL && ! (n->next->flags & NODE_LINE))
969 p->flags |= TERMP_NOSPACE;
970 return;
971 case ROFFT_TBL:
972 if (p->tbl.cols == NULL)
973 term_vspace(p);
974 term_tbl(p, n->span);
975 return;
976 default:
977 break;
978 }
979
980 if (n->tok < ROFF_MAX) {
981 roff_term_pre(p, n);
982 return;
983 }
984
985 act = man_term_act(n->tok);
986 if ((act->flags & MAN_NOTEXT) == 0)
987 term_fontrepl(p, TERMFONT_NONE);
988
989 c = 1;
990 if (act->pre != NULL)
991 c = (*act->pre)(p, mt, n, meta);
992
993 if (c && n->child)
994 print_man_nodelist(p, mt, n->child, meta);
995
996 if (act->post != NULL)
997 (*act->post)(p, mt, n, meta);
998 if ((act->flags & MAN_NOTEXT) == 0)
999 term_fontrepl(p, TERMFONT_NONE);
1000
1001 out:
1002 /*
1003 * If we're in a literal context, make sure that words
1004 * together on the same line stay together. This is a
1005 * POST-printing call, so we check the NEXT word. Since
1006 * -man doesn't have nested macros, we don't need to be
1007 * more specific than this.
1008 */
1009 if (mt->fl & MANT_LITERAL &&
1010 ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1011 (n->next == NULL || n->next->flags & NODE_LINE)) {
1012 p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
1013 if (n->string != NULL && *n->string != '\0')
1014 term_flushln(p);
1015 else
1016 term_newln(p);
1017 p->flags &= ~TERMP_BRNEVER;
1018 if (p->tcol->rmargin < p->maxrmargin &&
1019 n->parent->tok == MAN_HP) {
1020 p->tcol->offset = p->tcol->rmargin;
1021 p->tcol->rmargin = p->maxrmargin;
1022 }
1023 }
1024 if (NODE_EOS & n->flags)
1025 p->flags |= TERMP_SENTENCE;
1026 }
1027
1028
1029 static void
1030 print_man_nodelist(DECL_ARGS)
1031 {
1032
1033 while (n != NULL) {
1034 print_man_node(p, mt, n, meta);
1035 n = n->next;
1036 }
1037 }
1038
1039 static void
1040 print_man_foot(struct termp *p, const struct roff_meta *meta)
1041 {
1042 char *title;
1043 size_t datelen, titlen;
1044
1045 assert(meta->title);
1046 assert(meta->msec);
1047 assert(meta->date);
1048
1049 term_fontrepl(p, TERMFONT_NONE);
1050
1051 if (meta->hasbody)
1052 term_vspace(p);
1053
1054 /*
1055 * Temporary, undocumented option to imitate mdoc(7) output.
1056 * In the bottom right corner, use the operating system
1057 * instead of the title.
1058 */
1059
1060 if ( ! p->mdocstyle) {
1061 if (meta->hasbody) {
1062 term_vspace(p);
1063 term_vspace(p);
1064 }
1065 mandoc_asprintf(&title, "%s(%s)",
1066 meta->title, meta->msec);
1067 } else if (meta->os) {
1068 title = mandoc_strdup(meta->os);
1069 } else {
1070 title = mandoc_strdup("");
1071 }
1072 datelen = term_strlen(p, meta->date);
1073
1074 /* Bottom left corner: operating system. */
1075
1076 p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1077 p->trailspace = 1;
1078 p->tcol->offset = 0;
1079 p->tcol->rmargin = p->maxrmargin > datelen ?
1080 (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1081
1082 if (meta->os)
1083 term_word(p, meta->os);
1084 term_flushln(p);
1085
1086 /* At the bottom in the middle: manual date. */
1087
1088 p->tcol->offset = p->tcol->rmargin;
1089 titlen = term_strlen(p, title);
1090 p->tcol->rmargin = p->maxrmargin > titlen ?
1091 p->maxrmargin - titlen : 0;
1092 p->flags |= TERMP_NOSPACE;
1093
1094 term_word(p, meta->date);
1095 term_flushln(p);
1096
1097 /* Bottom right corner: manual title and section. */
1098
1099 p->flags &= ~TERMP_NOBREAK;
1100 p->flags |= TERMP_NOSPACE;
1101 p->trailspace = 0;
1102 p->tcol->offset = p->tcol->rmargin;
1103 p->tcol->rmargin = p->maxrmargin;
1104
1105 term_word(p, title);
1106 term_flushln(p);
1107
1108 /*
1109 * Reset the terminal state for more output after the footer:
1110 * Some output modes, in particular PostScript and PDF, print
1111 * the header and the footer into a buffer such that it can be
1112 * reused for multiple output pages, then go on to format the
1113 * main text.
1114 */
1115
1116 p->tcol->offset = 0;
1117 p->flags = 0;
1118
1119 free(title);
1120 }
1121
1122 static void
1123 print_man_head(struct termp *p, const struct roff_meta *meta)
1124 {
1125 const char *volume;
1126 char *title;
1127 size_t vollen, titlen;
1128
1129 assert(meta->title);
1130 assert(meta->msec);
1131
1132 volume = NULL == meta->vol ? "" : meta->vol;
1133 vollen = term_strlen(p, volume);
1134
1135 /* Top left corner: manual title and section. */
1136
1137 mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1138 titlen = term_strlen(p, title);
1139
1140 p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1141 p->trailspace = 1;
1142 p->tcol->offset = 0;
1143 p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1144 (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1145 vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1146
1147 term_word(p, title);
1148 term_flushln(p);
1149
1150 /* At the top in the middle: manual volume. */
1151
1152 p->flags |= TERMP_NOSPACE;
1153 p->tcol->offset = p->tcol->rmargin;
1154 p->tcol->rmargin = p->tcol->offset + vollen + titlen <
1155 p->maxrmargin ? p->maxrmargin - titlen : p->maxrmargin;
1156
1157 term_word(p, volume);
1158 term_flushln(p);
1159
1160 /* Top right corner: title and section, again. */
1161
1162 p->flags &= ~TERMP_NOBREAK;
1163 p->trailspace = 0;
1164 if (p->tcol->rmargin + titlen <= p->maxrmargin) {
1165 p->flags |= TERMP_NOSPACE;
1166 p->tcol->offset = p->tcol->rmargin;
1167 p->tcol->rmargin = p->maxrmargin;
1168 term_word(p, title);
1169 term_flushln(p);
1170 }
1171
1172 p->flags &= ~TERMP_NOSPACE;
1173 p->tcol->offset = 0;
1174 p->tcol->rmargin = p->maxrmargin;
1175
1176 /*
1177 * Groff prints three blank lines before the content.
1178 * Do the same, except in the temporary, undocumented
1179 * mode imitating mdoc(7) output.
1180 */
1181
1182 term_vspace(p);
1183 if ( ! p->mdocstyle) {
1184 term_vspace(p);
1185 term_vspace(p);
1186 }
1187 free(title);
1188 }