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