Annotation of src/usr.bin/mandoc/man_term.c, Revision 1.120
1.120 ! schwarze 1: /* $OpenBSD: man_term.c,v 1.119 2015/01/24 02:41:32 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.117 schwarze 22: #include <limits.h>
1.1 kristaps 23: #include <stdio.h>
24: #include <stdlib.h>
25: #include <string.h>
26:
1.37 schwarze 27: #include "mandoc.h"
1.101 schwarze 28: #include "mandoc_aux.h"
1.18 schwarze 29: #include "out.h"
30: #include "man.h"
1.1 kristaps 31: #include "term.h"
1.18 schwarze 32: #include "main.h"
1.10 schwarze 33:
1.70 schwarze 34: #define MAXMARGINS 64 /* maximum number of indented scopes */
1.1 kristaps 35:
1.11 schwarze 36: struct mtermp {
37: int fl;
38: #define MANT_LITERAL (1 << 0)
1.116 schwarze 39: int lmargin[MAXMARGINS]; /* margins (incl. vis. page) */
1.70 schwarze 40: int lmargincur; /* index of current margin */
41: int lmarginsz; /* actual number of nested margins */
42: size_t offset; /* default offset to visible page */
1.88 schwarze 43: int pardist; /* vert. space before par., unit: [v] */
1.11 schwarze 44: };
45:
1.100 schwarze 46: #define DECL_ARGS struct termp *p, \
1.11 schwarze 47: struct mtermp *mt, \
1.118 schwarze 48: struct man_node *n, \
1.89 schwarze 49: const struct man_meta *meta
1.1 kristaps 50:
51: struct termact {
52: int (*pre)(DECL_ARGS);
53: void (*post)(DECL_ARGS);
1.26 schwarze 54: int flags;
55: #define MAN_NOTEXT (1 << 0) /* Never has text children. */
1.1 kristaps 56: };
57:
1.21 schwarze 58: static void print_man_nodelist(DECL_ARGS);
1.19 schwarze 59: static void print_man_node(DECL_ARGS);
1.41 schwarze 60: static void print_man_head(struct termp *, const void *);
61: static void print_man_foot(struct termp *, const void *);
1.100 schwarze 62: static void print_bvspace(struct termp *,
1.88 schwarze 63: const struct man_node *, int);
1.18 schwarze 64:
1.1 kristaps 65: static int pre_B(DECL_ARGS);
1.11 schwarze 66: static int pre_HP(DECL_ARGS);
1.1 kristaps 67: static int pre_I(DECL_ARGS);
68: static int pre_IP(DECL_ARGS);
1.82 schwarze 69: static int pre_OP(DECL_ARGS);
1.88 schwarze 70: static int pre_PD(DECL_ARGS);
1.1 kristaps 71: static int pre_PP(DECL_ARGS);
1.13 schwarze 72: static int pre_RS(DECL_ARGS);
1.1 kristaps 73: static int pre_SH(DECL_ARGS);
74: static int pre_SS(DECL_ARGS);
75: static int pre_TP(DECL_ARGS);
1.91 schwarze 76: static int pre_UR(DECL_ARGS);
1.82 schwarze 77: static int pre_alternate(DECL_ARGS);
78: static int pre_ft(DECL_ARGS);
1.14 schwarze 79: static int pre_ign(DECL_ARGS);
1.45 schwarze 80: static int pre_in(DECL_ARGS);
81: static int pre_literal(DECL_ARGS);
1.97 schwarze 82: static int pre_ll(DECL_ARGS);
1.11 schwarze 83: static int pre_sp(DECL_ARGS);
1.1 kristaps 84:
1.11 schwarze 85: static void post_IP(DECL_ARGS);
86: static void post_HP(DECL_ARGS);
1.13 schwarze 87: static void post_RS(DECL_ARGS);
1.1 kristaps 88: static void post_SH(DECL_ARGS);
89: static void post_SS(DECL_ARGS);
1.11 schwarze 90: static void post_TP(DECL_ARGS);
1.91 schwarze 91: static void post_UR(DECL_ARGS);
1.1 kristaps 92:
1.17 schwarze 93: static const struct termact termacts[MAN_MAX] = {
1.45 schwarze 94: { pre_sp, NULL, MAN_NOTEXT }, /* br */
1.26 schwarze 95: { NULL, NULL, 0 }, /* TH */
96: { pre_SH, post_SH, 0 }, /* SH */
97: { pre_SS, post_SS, 0 }, /* SS */
98: { pre_TP, post_TP, 0 }, /* TP */
99: { pre_PP, NULL, 0 }, /* LP */
100: { pre_PP, NULL, 0 }, /* PP */
101: { pre_PP, NULL, 0 }, /* P */
102: { pre_IP, post_IP, 0 }, /* IP */
1.100 schwarze 103: { pre_HP, post_HP, 0 }, /* HP */
1.26 schwarze 104: { NULL, NULL, 0 }, /* SM */
105: { pre_B, NULL, 0 }, /* SB */
1.51 schwarze 106: { pre_alternate, NULL, 0 }, /* BI */
107: { pre_alternate, NULL, 0 }, /* IB */
108: { pre_alternate, NULL, 0 }, /* BR */
109: { pre_alternate, NULL, 0 }, /* RB */
1.26 schwarze 110: { NULL, NULL, 0 }, /* R */
111: { pre_B, NULL, 0 }, /* B */
112: { pre_I, NULL, 0 }, /* I */
1.51 schwarze 113: { pre_alternate, NULL, 0 }, /* IR */
114: { pre_alternate, NULL, 0 }, /* RI */
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];
1.118 schwarze 280: struct man_node *nn;
1.51 schwarze 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.117 schwarze 433: if (p->offset > SHRT_MAX)
434: p->offset = term_len(p, p->defindent);
1.59 schwarze 435:
1.11 schwarze 436: return(0);
437: }
438:
439: static int
1.45 schwarze 440: pre_sp(DECL_ARGS)
1.8 schwarze 441: {
1.115 schwarze 442: struct roffsu su;
1.116 schwarze 443: int i, len;
1.45 schwarze 444:
1.69 schwarze 445: if ((NULL == n->prev && n->parent)) {
1.86 schwarze 446: switch (n->parent->tok) {
1.100 schwarze 447: case MAN_SH:
1.86 schwarze 448: /* FALLTHROUGH */
1.100 schwarze 449: case MAN_SS:
1.86 schwarze 450: /* FALLTHROUGH */
1.100 schwarze 451: case MAN_PP:
1.86 schwarze 452: /* FALLTHROUGH */
1.100 schwarze 453: case MAN_LP:
1.86 schwarze 454: /* FALLTHROUGH */
1.100 schwarze 455: case MAN_P:
1.86 schwarze 456: /* FALLTHROUGH */
1.69 schwarze 457: return(0);
1.86 schwarze 458: default:
459: break;
460: }
1.69 schwarze 461: }
462:
1.116 schwarze 463: if (n->tok == MAN_br)
1.45 schwarze 464: len = 0;
1.116 schwarze 465: else if (n->child == NULL)
466: len = 1;
467: else {
468: if ( ! a2roffsu(n->child->string, &su, SCALE_VS))
1.115 schwarze 469: su.scale = 1.0;
470: len = term_vspan(p, &su);
1.45 schwarze 471: }
472:
1.116 schwarze 473: if (len == 0)
1.45 schwarze 474: term_newln(p);
1.116 schwarze 475: else if (len < 0)
476: p->skipvsp -= len;
1.85 schwarze 477: else
478: for (i = 0; i < len; i++)
479: term_vspace(p);
1.8 schwarze 480:
481: return(0);
482: }
483:
484: static int
1.11 schwarze 485: pre_HP(DECL_ARGS)
486: {
1.116 schwarze 487: struct roffsu su;
1.11 schwarze 488: const struct man_node *nn;
1.116 schwarze 489: int len;
1.11 schwarze 490:
491: switch (n->type) {
1.100 schwarze 492: case MAN_BLOCK:
1.88 schwarze 493: print_bvspace(p, n, mt->pardist);
1.11 schwarze 494: return(1);
1.100 schwarze 495: case MAN_BODY:
1.11 schwarze 496: break;
497: default:
498: return(0);
499: }
500:
1.84 schwarze 501: if ( ! (MANT_LITERAL & mt->fl)) {
1.99 schwarze 502: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.93 schwarze 503: p->trailspace = 2;
1.84 schwarze 504: }
505:
1.11 schwarze 506: /* Calculate offset. */
507:
1.116 schwarze 508: if ((nn = n->parent->head->child) != NULL &&
509: a2roffsu(nn->string, &su, SCALE_EN)) {
510: len = term_hspan(p, &su);
1.117 schwarze 511: if (len < 0 && (size_t)(-len) > mt->offset)
512: len = -mt->offset;
513: else if (len > SHRT_MAX)
514: len = term_len(p, p->defindent);
1.116 schwarze 515: mt->lmargin[mt->lmargincur] = len;
516: } else
517: len = mt->lmargin[mt->lmargincur];
1.11 schwarze 518:
1.13 schwarze 519: p->offset = mt->offset;
1.117 schwarze 520: p->rmargin = mt->offset + len;
1.11 schwarze 521: return(1);
522: }
523:
524: static void
525: post_HP(DECL_ARGS)
526: {
527:
528: switch (n->type) {
1.100 schwarze 529: case MAN_BODY:
1.90 schwarze 530: term_newln(p);
1.99 schwarze 531: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.93 schwarze 532: p->trailspace = 0;
1.13 schwarze 533: p->offset = mt->offset;
1.11 schwarze 534: p->rmargin = p->maxrmargin;
535: break;
536: default:
537: break;
538: }
539: }
540:
541: static int
1.1 kristaps 542: pre_PP(DECL_ARGS)
543: {
544:
1.11 schwarze 545: switch (n->type) {
1.100 schwarze 546: case MAN_BLOCK:
1.77 schwarze 547: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.88 schwarze 548: print_bvspace(p, n, mt->pardist);
1.11 schwarze 549: break;
550: default:
1.13 schwarze 551: p->offset = mt->offset;
1.11 schwarze 552: break;
553: }
554:
1.54 schwarze 555: return(MAN_HEAD != n->type);
1.1 kristaps 556: }
557:
558: static int
559: pre_IP(DECL_ARGS)
560: {
1.116 schwarze 561: struct roffsu su;
1.11 schwarze 562: const struct man_node *nn;
1.116 schwarze 563: int len, savelit;
1.11 schwarze 564:
565: switch (n->type) {
1.100 schwarze 566: case MAN_BODY:
1.11 schwarze 567: p->flags |= TERMP_NOSPACE;
568: break;
1.100 schwarze 569: case MAN_HEAD:
1.11 schwarze 570: p->flags |= TERMP_NOBREAK;
1.93 schwarze 571: p->trailspace = 1;
1.11 schwarze 572: break;
1.100 schwarze 573: case MAN_BLOCK:
1.88 schwarze 574: print_bvspace(p, n, mt->pardist);
1.11 schwarze 575: /* FALLTHROUGH */
576: default:
577: return(1);
578: }
579:
1.57 schwarze 580: /* Calculate the offset from the optional second argument. */
1.116 schwarze 581: if ((nn = n->parent->head->child) != NULL &&
582: (nn = nn->next) != NULL &&
583: a2roffsu(nn->string, &su, SCALE_EN)) {
584: len = term_hspan(p, &su);
585: if (len < 0 && (size_t)(-len) > mt->offset)
586: len = -mt->offset;
1.117 schwarze 587: else if (len > SHRT_MAX)
588: len = term_len(p, p->defindent);
589: mt->lmargin[mt->lmargincur] = len;
1.116 schwarze 590: } else
591: len = mt->lmargin[mt->lmargincur];
1.11 schwarze 592:
593: switch (n->type) {
1.100 schwarze 594: case MAN_HEAD:
1.13 schwarze 595: p->offset = mt->offset;
596: p->rmargin = mt->offset + len;
1.11 schwarze 597:
1.57 schwarze 598: savelit = MANT_LITERAL & mt->fl;
599: mt->fl &= ~MANT_LITERAL;
600:
601: if (n->child)
1.89 schwarze 602: print_man_node(p, mt, n->child, meta);
1.57 schwarze 603:
604: if (savelit)
605: mt->fl |= MANT_LITERAL;
606:
1.11 schwarze 607: return(0);
1.100 schwarze 608: case MAN_BODY:
1.13 schwarze 609: p->offset = mt->offset + len;
1.109 schwarze 610: p->rmargin = p->maxrmargin;
1.11 schwarze 611: break;
612: default:
613: break;
614: }
1.1 kristaps 615:
1.11 schwarze 616: return(1);
617: }
1.1 kristaps 618:
1.11 schwarze 619: static void
620: post_IP(DECL_ARGS)
621: {
1.4 schwarze 622:
1.11 schwarze 623: switch (n->type) {
1.100 schwarze 624: case MAN_HEAD:
1.11 schwarze 625: term_flushln(p);
626: p->flags &= ~TERMP_NOBREAK;
1.93 schwarze 627: p->trailspace = 0;
1.11 schwarze 628: p->rmargin = p->maxrmargin;
629: break;
1.100 schwarze 630: case MAN_BODY:
1.57 schwarze 631: term_newln(p);
1.92 schwarze 632: p->offset = mt->offset;
1.11 schwarze 633: break;
634: default:
635: break;
636: }
1.1 kristaps 637: }
638:
639: static int
640: pre_TP(DECL_ARGS)
641: {
1.116 schwarze 642: struct roffsu su;
1.118 schwarze 643: struct man_node *nn;
1.116 schwarze 644: int len, savelit;
1.11 schwarze 645:
646: switch (n->type) {
1.100 schwarze 647: case MAN_HEAD:
1.11 schwarze 648: p->flags |= TERMP_NOBREAK;
1.93 schwarze 649: p->trailspace = 1;
1.11 schwarze 650: break;
1.100 schwarze 651: case MAN_BODY:
1.11 schwarze 652: p->flags |= TERMP_NOSPACE;
653: break;
1.100 schwarze 654: case MAN_BLOCK:
1.88 schwarze 655: print_bvspace(p, n, mt->pardist);
1.11 schwarze 656: /* FALLTHROUGH */
657: default:
658: return(1);
659: }
660:
661: /* Calculate offset. */
1.1 kristaps 662:
1.116 schwarze 663: if ((nn = n->parent->head->child) != NULL &&
664: nn->string != NULL && ! (MAN_LINE & nn->flags) &&
665: a2roffsu(nn->string, &su, SCALE_EN)) {
666: len = term_hspan(p, &su);
667: if (len < 0 && (size_t)(-len) > mt->offset)
668: len = -mt->offset;
1.117 schwarze 669: else if (len > SHRT_MAX)
670: len = term_len(p, p->defindent);
671: mt->lmargin[mt->lmargincur] = len;
1.116 schwarze 672: } else
673: len = mt->lmargin[mt->lmargincur];
1.8 schwarze 674:
1.11 schwarze 675: switch (n->type) {
1.100 schwarze 676: case MAN_HEAD:
1.13 schwarze 677: p->offset = mt->offset;
678: p->rmargin = mt->offset + len;
1.11 schwarze 679:
1.57 schwarze 680: savelit = MANT_LITERAL & mt->fl;
681: mt->fl &= ~MANT_LITERAL;
682:
1.11 schwarze 683: /* Don't print same-line elements. */
1.95 schwarze 684: nn = n->child;
685: while (NULL != nn && 0 == (MAN_LINE & nn->flags))
686: nn = nn->next;
687:
688: while (NULL != nn) {
689: print_man_node(p, mt, nn, meta);
690: nn = nn->next;
691: }
1.11 schwarze 692:
1.57 schwarze 693: if (savelit)
694: mt->fl |= MANT_LITERAL;
1.11 schwarze 695: return(0);
1.100 schwarze 696: case MAN_BODY:
1.13 schwarze 697: p->offset = mt->offset + len;
1.109 schwarze 698: p->rmargin = p->maxrmargin;
1.93 schwarze 699: p->trailspace = 0;
1.84 schwarze 700: p->flags &= ~TERMP_NOBREAK;
1.11 schwarze 701: break;
702: default:
703: break;
704: }
1.1 kristaps 705:
1.11 schwarze 706: return(1);
707: }
1.1 kristaps 708:
1.11 schwarze 709: static void
710: post_TP(DECL_ARGS)
711: {
1.1 kristaps 712:
1.11 schwarze 713: switch (n->type) {
1.100 schwarze 714: case MAN_HEAD:
1.11 schwarze 715: term_flushln(p);
716: break;
1.100 schwarze 717: case MAN_BODY:
1.57 schwarze 718: term_newln(p);
1.92 schwarze 719: p->offset = mt->offset;
1.11 schwarze 720: break;
721: default:
722: break;
723: }
1.1 kristaps 724: }
725:
726: static int
727: pre_SS(DECL_ARGS)
728: {
1.88 schwarze 729: int i;
1.1 kristaps 730:
1.11 schwarze 731: switch (n->type) {
1.100 schwarze 732: case MAN_BLOCK:
1.69 schwarze 733: mt->fl &= ~MANT_LITERAL;
1.77 schwarze 734: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
735: mt->offset = term_len(p, p->defindent);
1.111 schwarze 736:
737: /*
738: * No vertical space before the first subsection
739: * and after an empty subsection.
740: */
741:
742: do {
743: n = n->prev;
744: } while (n != NULL && termacts[n->tok].flags & MAN_NOTEXT);
745: if (n == NULL || (n->tok == MAN_SS && n->body->child == NULL))
1.11 schwarze 746: break;
1.111 schwarze 747:
1.88 schwarze 748: for (i = 0; i < mt->pardist; i++)
749: term_vspace(p);
1.11 schwarze 750: break;
1.100 schwarze 751: case MAN_HEAD:
1.21 schwarze 752: term_fontrepl(p, TERMFONT_BOLD);
1.87 schwarze 753: p->offset = term_len(p, 3);
1.11 schwarze 754: break;
1.100 schwarze 755: case MAN_BODY:
1.13 schwarze 756: p->offset = mt->offset;
1.11 schwarze 757: break;
758: default:
759: break;
760: }
761:
1.1 kristaps 762: return(1);
763: }
764:
765: static void
766: post_SS(DECL_ARGS)
767: {
1.100 schwarze 768:
1.11 schwarze 769: switch (n->type) {
1.100 schwarze 770: case MAN_HEAD:
1.11 schwarze 771: term_newln(p);
772: break;
1.100 schwarze 773: case MAN_BODY:
1.11 schwarze 774: term_newln(p);
775: break;
776: default:
777: break;
778: }
1.1 kristaps 779: }
780:
781: static int
782: pre_SH(DECL_ARGS)
783: {
1.88 schwarze 784: int i;
1.1 kristaps 785:
1.11 schwarze 786: switch (n->type) {
1.100 schwarze 787: case MAN_BLOCK:
1.69 schwarze 788: mt->fl &= ~MANT_LITERAL;
1.77 schwarze 789: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
790: mt->offset = term_len(p, p->defindent);
1.111 schwarze 791:
792: /*
793: * No vertical space before the first section
794: * and after an empty section.
795: */
796:
797: do {
798: n = n->prev;
799: } while (n != NULL && termacts[n->tok].flags & MAN_NOTEXT);
800: if (n == NULL || (n->tok == MAN_SH && n->body->child == NULL))
1.29 schwarze 801: break;
1.111 schwarze 802:
1.88 schwarze 803: for (i = 0; i < mt->pardist; i++)
804: term_vspace(p);
1.11 schwarze 805: break;
1.100 schwarze 806: case MAN_HEAD:
1.21 schwarze 807: term_fontrepl(p, TERMFONT_BOLD);
1.11 schwarze 808: p->offset = 0;
809: break;
1.100 schwarze 810: case MAN_BODY:
1.13 schwarze 811: p->offset = mt->offset;
1.11 schwarze 812: break;
813: default:
814: break;
815: }
816:
1.1 kristaps 817: return(1);
818: }
819:
820: static void
821: post_SH(DECL_ARGS)
822: {
1.100 schwarze 823:
1.11 schwarze 824: switch (n->type) {
1.100 schwarze 825: case MAN_HEAD:
1.11 schwarze 826: term_newln(p);
827: break;
1.100 schwarze 828: case MAN_BODY:
1.11 schwarze 829: term_newln(p);
830: break;
831: default:
1.13 schwarze 832: break;
833: }
834: }
835:
836: static int
837: pre_RS(DECL_ARGS)
838: {
1.116 schwarze 839: struct roffsu su;
1.13 schwarze 840:
841: switch (n->type) {
1.100 schwarze 842: case MAN_BLOCK:
1.13 schwarze 843: term_newln(p);
844: return(1);
1.100 schwarze 845: case MAN_HEAD:
1.13 schwarze 846: return(0);
847: default:
848: break;
849: }
850:
1.118 schwarze 851: n = n->parent->head;
852: n->aux = SHRT_MAX + 1;
853: if (n->child != NULL && a2roffsu(n->child->string, &su, SCALE_EN))
854: n->aux = term_hspan(p, &su);
855: if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
856: n->aux = -mt->offset;
857: else if (n->aux > SHRT_MAX)
858: n->aux = term_len(p, p->defindent);
1.13 schwarze 859:
1.118 schwarze 860: mt->offset += n->aux;
1.94 schwarze 861: p->offset = mt->offset;
1.109 schwarze 862: p->rmargin = p->maxrmargin;
1.13 schwarze 863:
1.70 schwarze 864: if (++mt->lmarginsz < MAXMARGINS)
865: mt->lmargincur = mt->lmarginsz;
866:
867: mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
1.13 schwarze 868: return(1);
869: }
870:
871: static void
872: post_RS(DECL_ARGS)
873: {
874:
875: switch (n->type) {
1.100 schwarze 876: case MAN_BLOCK:
1.69 schwarze 877: return;
1.100 schwarze 878: case MAN_HEAD:
1.69 schwarze 879: return;
1.13 schwarze 880: default:
881: term_newln(p);
1.11 schwarze 882: break;
883: }
1.69 schwarze 884:
1.118 schwarze 885: mt->offset -= n->parent->head->aux;
1.69 schwarze 886: p->offset = mt->offset;
1.70 schwarze 887:
888: if (--mt->lmarginsz < MAXMARGINS)
889: mt->lmargincur = mt->lmarginsz;
1.91 schwarze 890: }
891:
892: static int
893: pre_UR(DECL_ARGS)
894: {
895:
896: return (MAN_HEAD != n->type);
897: }
898:
899: static void
900: post_UR(DECL_ARGS)
901: {
902:
903: if (MAN_BLOCK != n->type)
904: return;
905:
906: term_word(p, "<");
907: p->flags |= TERMP_NOSPACE;
908:
909: if (NULL != n->child->child)
910: print_man_node(p, mt, n->child->child, meta);
911:
912: p->flags |= TERMP_NOSPACE;
913: term_word(p, ">");
1.47 schwarze 914: }
915:
1.1 kristaps 916: static void
1.19 schwarze 917: print_man_node(DECL_ARGS)
1.1 kristaps 918: {
1.32 schwarze 919: size_t rm, rmax;
1.21 schwarze 920: int c;
1.1 kristaps 921:
922: switch (n->type) {
1.100 schwarze 923: case MAN_TEXT:
1.61 schwarze 924: /*
925: * If we have a blank line, output a vertical space.
926: * If we have a space as the first character, break
927: * before printing the line's data.
928: */
1.60 schwarze 929: if ('\0' == *n->string) {
1.1 kristaps 930: term_vspace(p);
1.61 schwarze 931: return;
932: } else if (' ' == *n->string && MAN_LINE & n->flags)
1.60 schwarze 933: term_newln(p);
1.21 schwarze 934:
1.1 kristaps 935: term_word(p, n->string);
1.84 schwarze 936: goto out;
1.21 schwarze 937:
1.100 schwarze 938: case MAN_EQN:
1.105 schwarze 939: if ( ! (n->flags & MAN_LINE))
940: p->flags |= TERMP_NOSPACE;
1.71 schwarze 941: term_eqn(p, n->eqn);
1.107 schwarze 942: if (n->next != NULL && ! (n->next->flags & MAN_LINE))
1.106 schwarze 943: p->flags |= TERMP_NOSPACE;
1.61 schwarze 944: return;
1.100 schwarze 945: case MAN_TBL:
1.61 schwarze 946: /*
947: * Tables are preceded by a newline. Then process a
948: * table line, which will cause line termination,
949: */
1.120 ! schwarze 950: if (n->span->prev == NULL)
1.58 schwarze 951: term_newln(p);
952: term_tbl(p, n->span);
1.61 schwarze 953: return;
1.1 kristaps 954: default:
955: break;
956: }
957:
1.61 schwarze 958: if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
959: term_fontrepl(p, TERMFONT_NONE);
960:
961: c = 1;
962: if (termacts[n->tok].pre)
1.89 schwarze 963: c = (*termacts[n->tok].pre)(p, mt, n, meta);
1.61 schwarze 964:
1.1 kristaps 965: if (c && n->child)
1.89 schwarze 966: print_man_nodelist(p, mt, n->child, meta);
1.1 kristaps 967:
1.61 schwarze 968: if (termacts[n->tok].post)
1.89 schwarze 969: (*termacts[n->tok].post)(p, mt, n, meta);
1.61 schwarze 970: if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
971: term_fontrepl(p, TERMFONT_NONE);
1.30 schwarze 972:
1.84 schwarze 973: out:
974: /*
975: * If we're in a literal context, make sure that words
976: * together on the same line stay together. This is a
977: * POST-printing call, so we check the NEXT word. Since
978: * -man doesn't have nested macros, we don't need to be
979: * more specific than this.
980: */
1.110 schwarze 981: if (mt->fl & MANT_LITERAL &&
982: ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
983: (n->next == NULL || n->next->flags & MAN_LINE)) {
1.84 schwarze 984: rm = p->rmargin;
985: rmax = p->maxrmargin;
986: p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
987: p->flags |= TERMP_NOSPACE;
1.110 schwarze 988: if (n->string != NULL && *n->string != '\0')
1.84 schwarze 989: term_flushln(p);
990: else
991: term_newln(p);
992: if (rm < rmax && n->parent->tok == MAN_HP) {
993: p->offset = rm;
994: p->rmargin = rmax;
995: } else
996: p->rmargin = rm;
997: p->maxrmargin = rmax;
998: }
1.30 schwarze 999: if (MAN_EOS & n->flags)
1000: p->flags |= TERMP_SENTENCE;
1.1 kristaps 1001: }
1002:
1003:
1004: static void
1.21 schwarze 1005: print_man_nodelist(DECL_ARGS)
1.1 kristaps 1006: {
1.11 schwarze 1007:
1.89 schwarze 1008: print_man_node(p, mt, n, meta);
1.1 kristaps 1009: if ( ! n->next)
1010: return;
1.89 schwarze 1011: print_man_nodelist(p, mt, n->next, meta);
1.1 kristaps 1012: }
1013:
1014: static void
1.41 schwarze 1015: print_man_foot(struct termp *p, const void *arg)
1.1 kristaps 1016: {
1.101 schwarze 1017: const struct man_meta *meta;
1018: char *title;
1.109 schwarze 1019: size_t datelen, titlen;
1.41 schwarze 1020:
1021: meta = (const struct man_meta *)arg;
1.80 schwarze 1022: assert(meta->title);
1023: assert(meta->msec);
1024: assert(meta->date);
1.21 schwarze 1025:
1026: term_fontrepl(p, TERMFONT_NONE);
1.1 kristaps 1027:
1.103 schwarze 1028: if (meta->hasbody)
1029: term_vspace(p);
1.81 schwarze 1030:
1031: /*
1032: * Temporary, undocumented option to imitate mdoc(7) output.
1033: * In the bottom right corner, use the source instead of
1034: * the title.
1035: */
1036:
1.79 schwarze 1037: if ( ! p->mdocstyle) {
1.103 schwarze 1038: if (meta->hasbody) {
1039: term_vspace(p);
1040: term_vspace(p);
1041: }
1.101 schwarze 1042: mandoc_asprintf(&title, "%s(%s)",
1043: meta->title, meta->msec);
1.79 schwarze 1044: } else if (meta->source) {
1.101 schwarze 1045: title = mandoc_strdup(meta->source);
1.79 schwarze 1046: } else {
1.101 schwarze 1047: title = mandoc_strdup("");
1.79 schwarze 1048: }
1.78 schwarze 1049: datelen = term_strlen(p, meta->date);
1.1 kristaps 1050:
1.81 schwarze 1051: /* Bottom left corner: manual source. */
1052:
1.1 kristaps 1053: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.93 schwarze 1054: p->trailspace = 1;
1.1 kristaps 1055: p->offset = 0;
1.109 schwarze 1056: p->rmargin = p->maxrmargin > datelen ?
1057: (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1.1 kristaps 1058:
1059: if (meta->source)
1060: term_word(p, meta->source);
1061: term_flushln(p);
1062:
1.81 schwarze 1063: /* At the bottom in the middle: manual date. */
1064:
1.109 schwarze 1065: p->offset = p->rmargin;
1066: titlen = term_strlen(p, title);
1067: p->rmargin = p->maxrmargin > titlen ? p->maxrmargin - titlen : 0;
1.72 schwarze 1068: p->flags |= TERMP_NOSPACE;
1.78 schwarze 1069:
1070: term_word(p, meta->date);
1071: term_flushln(p);
1072:
1.81 schwarze 1073: /* Bottom right corner: manual title and section. */
1074:
1.78 schwarze 1075: p->flags &= ~TERMP_NOBREAK;
1076: p->flags |= TERMP_NOSPACE;
1.93 schwarze 1077: p->trailspace = 0;
1.78 schwarze 1078: p->offset = p->rmargin;
1.1 kristaps 1079: p->rmargin = p->maxrmargin;
1080:
1.78 schwarze 1081: term_word(p, title);
1.1 kristaps 1082: term_flushln(p);
1.101 schwarze 1083: free(title);
1.1 kristaps 1084: }
1085:
1086: static void
1.41 schwarze 1087: print_man_head(struct termp *p, const void *arg)
1.1 kristaps 1088: {
1.101 schwarze 1089: const struct man_meta *meta;
1.102 schwarze 1090: const char *volume;
1.101 schwarze 1091: char *title;
1.102 schwarze 1092: size_t vollen, titlen;
1.41 schwarze 1093:
1.89 schwarze 1094: meta = (const struct man_meta *)arg;
1095: assert(meta->title);
1096: assert(meta->msec);
1.1 kristaps 1097:
1.102 schwarze 1098: volume = NULL == meta->vol ? "" : meta->vol;
1099: vollen = term_strlen(p, volume);
1.1 kristaps 1100:
1.81 schwarze 1101: /* Top left corner: manual title and section. */
1102:
1.101 schwarze 1103: mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.42 schwarze 1104: titlen = term_strlen(p, title);
1.1 kristaps 1105:
1.73 schwarze 1106: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.93 schwarze 1107: p->trailspace = 1;
1.1 kristaps 1108: p->offset = 0;
1.102 schwarze 1109: p->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1110: (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.109 schwarze 1111: vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1.1 kristaps 1112:
1113: term_word(p, title);
1114: term_flushln(p);
1115:
1.81 schwarze 1116: /* At the top in the middle: manual volume. */
1117:
1.72 schwarze 1118: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1119: p->offset = p->rmargin;
1.102 schwarze 1120: p->rmargin = p->offset + vollen + titlen < p->maxrmargin ?
1.25 schwarze 1121: p->maxrmargin - titlen : p->maxrmargin;
1.1 kristaps 1122:
1.102 schwarze 1123: term_word(p, volume);
1.1 kristaps 1124: term_flushln(p);
1125:
1.81 schwarze 1126: /* Top right corner: title and section, again. */
1127:
1.1 kristaps 1128: p->flags &= ~TERMP_NOBREAK;
1.93 schwarze 1129: p->trailspace = 0;
1.25 schwarze 1130: if (p->rmargin + titlen <= p->maxrmargin) {
1.72 schwarze 1131: p->flags |= TERMP_NOSPACE;
1.25 schwarze 1132: p->offset = p->rmargin;
1133: p->rmargin = p->maxrmargin;
1134: term_word(p, title);
1135: term_flushln(p);
1136: }
1.1 kristaps 1137:
1.73 schwarze 1138: p->flags &= ~TERMP_NOSPACE;
1139: p->offset = 0;
1.1 kristaps 1140: p->rmargin = p->maxrmargin;
1.29 schwarze 1141:
1.100 schwarze 1142: /*
1.81 schwarze 1143: * Groff prints three blank lines before the content.
1144: * Do the same, except in the temporary, undocumented
1145: * mode imitating mdoc(7) output.
1.29 schwarze 1146: */
1147:
1148: term_vspace(p);
1.79 schwarze 1149: if ( ! p->mdocstyle) {
1150: term_vspace(p);
1151: term_vspace(p);
1152: }
1.101 schwarze 1153: free(title);
1.1 kristaps 1154: }