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