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