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