Annotation of mandoc/man_term.c, Revision 1.210
1.210 ! schwarze 1: /* $Id: man_term.c,v 1.209 2017/07/31 15:19:06 schwarze Exp $ */
1.1 kristaps 2: /*
1.128 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.210 ! schwarze 4: * Copyright (c) 2010-2015, 2017, 2018 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.171 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.8 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.171 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.8 kristaps 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.171 schwarze 29: #include "mandoc_aux.h"
1.71 kristaps 30: #include "mandoc.h"
1.171 schwarze 31: #include "roff.h"
32: #include "man.h"
1.40 kristaps 33: #include "out.h"
1.1 kristaps 34: #include "term.h"
1.36 kristaps 35: #include "main.h"
1.1 kristaps 36:
1.116 schwarze 37: #define MAXMARGINS 64 /* maximum number of indented scopes */
1.18 kristaps 38:
1.24 kristaps 39: struct mtermp {
40: int fl;
1.19 kristaps 41: #define MANT_LITERAL (1 << 0)
1.163 schwarze 42: int lmargin[MAXMARGINS]; /* margins (incl. vis. page) */
1.116 schwarze 43: int lmargincur; /* index of current margin */
44: int lmarginsz; /* actual number of nested margins */
45: size_t offset; /* default offset to visible page */
1.134 schwarze 46: int pardist; /* vert. space before par., unit: [v] */
1.24 kristaps 47: };
1.19 kristaps 48:
1.146 schwarze 49: #define DECL_ARGS struct termp *p, \
1.24 kristaps 50: struct mtermp *mt, \
1.172 schwarze 51: struct roff_node *n, \
1.173 schwarze 52: const struct roff_meta *meta
1.1 kristaps 53:
54: struct termact {
55: int (*pre)(DECL_ARGS);
56: void (*post)(DECL_ARGS);
1.56 kristaps 57: int flags;
58: #define MAN_NOTEXT (1 << 0) /* Never has text children. */
1.1 kristaps 59: };
60:
1.52 kristaps 61: static void print_man_nodelist(DECL_ARGS);
1.45 kristaps 62: static void print_man_node(DECL_ARGS);
1.173 schwarze 63: static void print_man_head(struct termp *,
64: const struct roff_meta *);
65: static void print_man_foot(struct termp *,
66: const struct roff_meta *);
1.146 schwarze 67: static void print_bvspace(struct termp *,
1.172 schwarze 68: const struct roff_node *, int);
1.39 kristaps 69:
1.1 kristaps 70: static int pre_B(DECL_ARGS);
1.199 schwarze 71: static int pre_DT(DECL_ARGS);
1.19 kristaps 72: static int pre_HP(DECL_ARGS);
1.1 kristaps 73: static int pre_I(DECL_ARGS);
1.4 kristaps 74: static int pre_IP(DECL_ARGS);
1.127 kristaps 75: static int pre_OP(DECL_ARGS);
1.134 schwarze 76: static int pre_PD(DECL_ARGS);
1.1 kristaps 77: static int pre_PP(DECL_ARGS);
1.26 kristaps 78: static int pre_RS(DECL_ARGS);
1.1 kristaps 79: static int pre_SH(DECL_ARGS);
80: static int pre_SS(DECL_ARGS);
81: static int pre_TP(DECL_ARGS);
1.137 schwarze 82: static int pre_UR(DECL_ARGS);
1.127 kristaps 83: static int pre_alternate(DECL_ARGS);
1.29 kristaps 84: static int pre_ign(DECL_ARGS);
1.83 kristaps 85: static int pre_in(DECL_ARGS);
1.84 kristaps 86: static int pre_literal(DECL_ARGS);
1.1 kristaps 87:
1.22 kristaps 88: static void post_IP(DECL_ARGS);
1.20 kristaps 89: static void post_HP(DECL_ARGS);
1.26 kristaps 90: static void post_RS(DECL_ARGS);
1.1 kristaps 91: static void post_SH(DECL_ARGS);
92: static void post_SS(DECL_ARGS);
1.21 kristaps 93: static void post_TP(DECL_ARGS);
1.137 schwarze 94: static void post_UR(DECL_ARGS);
1.1 kristaps 95:
1.192 schwarze 96: static const struct termact __termacts[MAN_MAX - MAN_TH] = {
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.84 kristaps 117: { pre_literal, NULL, 0 }, /* nf */
118: { pre_literal, NULL, 0 }, /* fi */
1.56 kristaps 119: { NULL, NULL, 0 }, /* RE */
120: { pre_RS, post_RS, 0 }, /* RS */
1.199 schwarze 121: { pre_DT, NULL, 0 }, /* DT */
1.158 schwarze 122: { pre_ign, NULL, MAN_NOTEXT }, /* UC */
1.134 schwarze 123: { pre_PD, NULL, MAN_NOTEXT }, /* PD */
1.70 joerg 124: { pre_ign, NULL, 0 }, /* AT */
1.83 kristaps 125: { pre_in, NULL, MAN_NOTEXT }, /* in */
1.127 kristaps 126: { pre_OP, NULL, 0 }, /* OP */
1.129 schwarze 127: { pre_literal, NULL, 0 }, /* EX */
128: { pre_literal, NULL, 0 }, /* EE */
1.137 schwarze 129: { pre_UR, post_UR, 0 }, /* UR */
130: { NULL, NULL, 0 }, /* UE */
1.208 schwarze 131: { pre_UR, post_UR, 0 }, /* MT */
132: { NULL, NULL, 0 }, /* ME */
1.1 kristaps 133: };
1.192 schwarze 134: static const struct termact *termacts = __termacts - MAN_TH;
1.1 kristaps 135:
136:
1.31 kristaps 137: void
1.180 schwarze 138: terminal_man(void *arg, const struct roff_man *man)
1.1 kristaps 139: {
1.36 kristaps 140: struct termp *p;
1.172 schwarze 141: struct roff_node *n;
1.36 kristaps 142: struct mtermp mt;
1.189 schwarze 143: size_t save_defindent;
1.36 kristaps 144:
145: p = (struct termp *)arg;
1.206 schwarze 146: save_defindent = p->defindent;
147: if (p->synopsisonly == 0 && p->defindent == 0)
148: p->defindent = 7;
1.203 schwarze 149: p->tcol->rmargin = p->maxrmargin = p->defrmargin;
1.198 schwarze 150: term_tab_set(p, NULL);
151: term_tab_set(p, "T");
152: term_tab_set(p, ".5i");
1.36 kristaps 153:
1.116 schwarze 154: memset(&mt, 0, sizeof(struct mtermp));
1.122 schwarze 155: mt.lmargin[mt.lmargincur] = term_len(p, p->defindent);
156: mt.offset = term_len(p, p->defindent);
1.134 schwarze 157: mt.pardist = 1;
1.24 kristaps 158:
1.181 schwarze 159: n = man->first->child;
1.151 schwarze 160: if (p->synopsisonly) {
161: while (n != NULL) {
162: if (n->tok == MAN_SH &&
1.171 schwarze 163: n->child->child->type == ROFFT_TEXT &&
1.151 schwarze 164: !strcmp(n->child->child->string, "SYNOPSIS")) {
165: if (n->child->next->child != NULL)
166: print_man_nodelist(p, &mt,
1.181 schwarze 167: n->child->next->child,
168: &man->meta);
1.151 schwarze 169: term_newln(p);
170: break;
171: }
172: n = n->next;
173: }
174: } else {
1.181 schwarze 175: term_begin(p, print_man_head, print_man_foot, &man->meta);
1.151 schwarze 176: p->flags |= TERMP_NOSPACE;
177: if (n != NULL)
1.181 schwarze 178: print_man_nodelist(p, &mt, n, &man->meta);
1.151 schwarze 179: term_end(p);
180: }
1.206 schwarze 181: p->defindent = save_defindent;
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.172 schwarze 193: print_bvspace(struct termp *p, const struct roff_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)
1.171 schwarze 200: if (n->body->child->type == ROFFT_TBL)
1.111 kristaps 201: return;
202:
1.171 schwarze 203: if (n->parent->type == ROFFT_ROOT || n->parent->tok != MAN_RS)
1.111 kristaps 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.185 schwarze 216: return 0;
1.29 kristaps 217: }
218:
219: static int
1.1 kristaps 220: pre_I(DECL_ARGS)
221: {
222:
1.52 kristaps 223: term_fontrepl(p, TERMFONT_UNDER);
1.185 schwarze 224: return 1;
1.19 kristaps 225: }
226:
1.3 kristaps 227: static int
1.84 kristaps 228: pre_literal(DECL_ARGS)
1.19 kristaps 229: {
230:
1.81 kristaps 231: term_newln(p);
1.88 kristaps 232:
1.203 schwarze 233: if (n->tok == MAN_nf || n->tok == MAN_EX)
1.84 kristaps 234: mt->fl |= MANT_LITERAL;
1.88 kristaps 235: else
1.84 kristaps 236: mt->fl &= ~MANT_LITERAL;
237:
1.117 schwarze 238: /*
239: * Unlike .IP and .TP, .HP does not have a HEAD.
240: * So in case a second call to term_flushln() is needed,
241: * indentation has to be set up explicitly.
242: */
1.203 schwarze 243: if (n->parent->tok == MAN_HP && p->tcol->rmargin < p->maxrmargin) {
244: p->tcol->offset = p->tcol->rmargin;
245: p->tcol->rmargin = p->maxrmargin;
1.139 schwarze 246: p->trailspace = 0;
1.145 schwarze 247: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.117 schwarze 248: p->flags |= TERMP_NOSPACE;
249: }
250:
1.185 schwarze 251: return 0;
1.19 kristaps 252: }
253:
254: static int
1.134 schwarze 255: pre_PD(DECL_ARGS)
256: {
1.160 schwarze 257: struct roffsu su;
1.134 schwarze 258:
259: n = n->child;
1.160 schwarze 260: if (n == NULL) {
1.134 schwarze 261: mt->pardist = 1;
1.185 schwarze 262: return 0;
1.134 schwarze 263: }
1.171 schwarze 264: assert(n->type == ROFFT_TEXT);
1.204 schwarze 265: if (a2roffsu(n->string, &su, SCALE_VS) != NULL)
1.160 schwarze 266: mt->pardist = term_vspan(p, &su);
1.185 schwarze 267: return 0;
1.134 schwarze 268: }
269:
270: static int
1.88 kristaps 271: pre_alternate(DECL_ARGS)
1.3 kristaps 272: {
1.88 kristaps 273: enum termfont font[2];
1.172 schwarze 274: struct roff_node *nn;
1.88 kristaps 275: int savelit, i;
1.3 kristaps 276:
1.88 kristaps 277: switch (n->tok) {
1.146 schwarze 278: case MAN_RB:
1.88 kristaps 279: font[0] = TERMFONT_NONE;
280: font[1] = TERMFONT_BOLD;
281: break;
1.146 schwarze 282: case MAN_RI:
1.88 kristaps 283: font[0] = TERMFONT_NONE;
284: font[1] = TERMFONT_UNDER;
285: break;
1.146 schwarze 286: case MAN_BR:
1.88 kristaps 287: font[0] = TERMFONT_BOLD;
288: font[1] = TERMFONT_NONE;
289: break;
1.146 schwarze 290: case MAN_BI:
1.88 kristaps 291: font[0] = TERMFONT_BOLD;
292: font[1] = TERMFONT_UNDER;
293: break;
1.146 schwarze 294: case MAN_IR:
1.88 kristaps 295: font[0] = TERMFONT_UNDER;
296: font[1] = TERMFONT_NONE;
297: break;
1.146 schwarze 298: case MAN_IB:
1.88 kristaps 299: font[0] = TERMFONT_UNDER;
300: font[1] = TERMFONT_BOLD;
301: break;
302: default:
303: abort();
304: }
1.35 kristaps 305:
1.88 kristaps 306: savelit = MANT_LITERAL & mt->fl;
307: mt->fl &= ~MANT_LITERAL;
1.35 kristaps 308:
1.88 kristaps 309: for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
310: term_fontrepl(p, font[i]);
311: if (savelit && NULL == nn->next)
312: mt->fl |= MANT_LITERAL;
1.179 schwarze 313: assert(nn->type == ROFFT_TEXT);
314: term_word(p, nn->string);
1.188 schwarze 315: if (nn->flags & NODE_EOS)
1.179 schwarze 316: p->flags |= TERMP_SENTENCE;
1.88 kristaps 317: if (nn->next)
1.4 kristaps 318: p->flags |= TERMP_NOSPACE;
1.3 kristaps 319: }
320:
1.185 schwarze 321: return 0;
1.3 kristaps 322: }
323:
1.1 kristaps 324: static int
325: pre_B(DECL_ARGS)
326: {
327:
1.52 kristaps 328: term_fontrepl(p, TERMFONT_BOLD);
1.185 schwarze 329: return 1;
1.127 kristaps 330: }
331:
332: static int
333: pre_OP(DECL_ARGS)
334: {
335:
336: term_word(p, "[");
337: p->flags |= TERMP_NOSPACE;
338:
339: if (NULL != (n = n->child)) {
340: term_fontrepl(p, TERMFONT_BOLD);
341: term_word(p, n->string);
342: }
343: if (NULL != n && NULL != n->next) {
344: term_fontrepl(p, TERMFONT_UNDER);
345: term_word(p, n->next->string);
346: }
347:
348: term_fontrepl(p, TERMFONT_NONE);
349: p->flags |= TERMP_NOSPACE;
350: term_word(p, "]");
1.185 schwarze 351: return 0;
1.83 kristaps 352: }
353:
354: static int
355: pre_in(DECL_ARGS)
356: {
1.163 schwarze 357: struct roffsu su;
358: const char *cp;
1.83 kristaps 359: size_t v;
1.163 schwarze 360: int less;
1.83 kristaps 361:
362: term_newln(p);
363:
1.203 schwarze 364: if (n->child == NULL) {
365: p->tcol->offset = mt->offset;
1.185 schwarze 366: return 0;
1.83 kristaps 367: }
368:
369: cp = n->child->string;
370: less = 0;
371:
372: if ('-' == *cp)
373: less = -1;
374: else if ('+' == *cp)
375: less = 1;
376: else
377: cp--;
378:
1.204 schwarze 379: if (a2roffsu(++cp, &su, SCALE_EN) == NULL)
1.185 schwarze 380: return 0;
1.83 kristaps 381:
1.205 schwarze 382: v = term_hen(p, &su);
1.83 kristaps 383:
384: if (less < 0)
1.203 schwarze 385: p->tcol->offset -= p->tcol->offset > v ? v : p->tcol->offset;
1.83 kristaps 386: else if (less > 0)
1.203 schwarze 387: p->tcol->offset += v;
1.146 schwarze 388: else
1.203 schwarze 389: p->tcol->offset = v;
390: if (p->tcol->offset > SHRT_MAX)
391: p->tcol->offset = term_len(p, p->defindent);
1.19 kristaps 392:
1.199 schwarze 393: return 0;
394: }
395:
396: static int
397: pre_DT(DECL_ARGS)
398: {
399: term_tab_set(p, NULL);
400: term_tab_set(p, "T");
401: term_tab_set(p, ".5i");
1.185 schwarze 402: return 0;
1.15 kristaps 403: }
404:
405: static int
1.19 kristaps 406: pre_HP(DECL_ARGS)
407: {
1.163 schwarze 408: struct roffsu su;
1.172 schwarze 409: const struct roff_node *nn;
1.163 schwarze 410: int len;
1.19 kristaps 411:
1.20 kristaps 412: switch (n->type) {
1.171 schwarze 413: case ROFFT_BLOCK:
1.134 schwarze 414: print_bvspace(p, n, mt->pardist);
1.185 schwarze 415: return 1;
1.171 schwarze 416: case ROFFT_BODY:
1.20 kristaps 417: break;
418: default:
1.185 schwarze 419: return 0;
1.20 kristaps 420: }
421:
1.130 schwarze 422: if ( ! (MANT_LITERAL & mt->fl)) {
1.145 schwarze 423: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.139 schwarze 424: p->trailspace = 2;
1.130 schwarze 425: }
426:
1.24 kristaps 427: /* Calculate offset. */
428:
1.163 schwarze 429: if ((nn = n->parent->head->child) != NULL &&
1.204 schwarze 430: a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205 schwarze 431: len = term_hen(p, &su);
1.164 schwarze 432: if (len < 0 && (size_t)(-len) > mt->offset)
433: len = -mt->offset;
434: else if (len > SHRT_MAX)
435: len = term_len(p, p->defindent);
1.163 schwarze 436: mt->lmargin[mt->lmargincur] = len;
437: } else
438: len = mt->lmargin[mt->lmargincur];
1.24 kristaps 439:
1.203 schwarze 440: p->tcol->offset = mt->offset;
441: p->tcol->rmargin = mt->offset + len;
1.185 schwarze 442: return 1;
1.19 kristaps 443: }
444:
1.20 kristaps 445: static void
446: post_HP(DECL_ARGS)
447: {
448:
449: switch (n->type) {
1.171 schwarze 450: case ROFFT_BODY:
1.136 schwarze 451: term_newln(p);
1.174 schwarze 452:
453: /*
454: * Compatibility with a groff bug.
455: * The .HP macro uses the undocumented .tag request
456: * which causes a line break and cancels no-space
457: * mode even if there isn't any output.
458: */
459:
460: if (n->child == NULL)
461: term_vspace(p);
462:
1.145 schwarze 463: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.139 schwarze 464: p->trailspace = 0;
1.203 schwarze 465: p->tcol->offset = mt->offset;
466: p->tcol->rmargin = p->maxrmargin;
1.20 kristaps 467: break;
468: default:
469: break;
470: }
471: }
472:
1.19 kristaps 473: static int
1.1 kristaps 474: pre_PP(DECL_ARGS)
475: {
476:
1.19 kristaps 477: switch (n->type) {
1.171 schwarze 478: case ROFFT_BLOCK:
1.122 schwarze 479: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.134 schwarze 480: print_bvspace(p, n, mt->pardist);
1.19 kristaps 481: break;
482: default:
1.203 schwarze 483: p->tcol->offset = mt->offset;
1.19 kristaps 484: break;
485: }
486:
1.185 schwarze 487: return n->type != ROFFT_HEAD;
1.1 kristaps 488: }
489:
490: static int
1.4 kristaps 491: pre_IP(DECL_ARGS)
492: {
1.163 schwarze 493: struct roffsu su;
1.172 schwarze 494: const struct roff_node *nn;
1.163 schwarze 495: int len, savelit;
1.18 kristaps 496:
1.22 kristaps 497: switch (n->type) {
1.171 schwarze 498: case ROFFT_BODY:
1.22 kristaps 499: p->flags |= TERMP_NOSPACE;
500: break;
1.171 schwarze 501: case ROFFT_HEAD:
1.22 kristaps 502: p->flags |= TERMP_NOBREAK;
1.139 schwarze 503: p->trailspace = 1;
1.22 kristaps 504: break;
1.171 schwarze 505: case ROFFT_BLOCK:
1.134 schwarze 506: print_bvspace(p, n, mt->pardist);
1.23 kristaps 507: /* FALLTHROUGH */
1.22 kristaps 508: default:
1.185 schwarze 509: return 1;
1.22 kristaps 510: }
1.18 kristaps 511:
1.94 schwarze 512: /* Calculate the offset from the optional second argument. */
1.163 schwarze 513: if ((nn = n->parent->head->child) != NULL &&
514: (nn = nn->next) != NULL &&
1.204 schwarze 515: a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205 schwarze 516: len = term_hen(p, &su);
1.163 schwarze 517: if (len < 0 && (size_t)(-len) > mt->offset)
518: len = -mt->offset;
1.164 schwarze 519: else if (len > SHRT_MAX)
520: len = term_len(p, p->defindent);
521: mt->lmargin[mt->lmargincur] = len;
1.163 schwarze 522: } else
523: len = mt->lmargin[mt->lmargincur];
1.4 kristaps 524:
1.22 kristaps 525: switch (n->type) {
1.171 schwarze 526: case ROFFT_HEAD:
1.203 schwarze 527: p->tcol->offset = mt->offset;
528: p->tcol->rmargin = mt->offset + len;
1.18 kristaps 529:
1.94 schwarze 530: savelit = MANT_LITERAL & mt->fl;
531: mt->fl &= ~MANT_LITERAL;
532:
533: if (n->child)
1.135 schwarze 534: print_man_node(p, mt, n->child, meta);
1.94 schwarze 535:
536: if (savelit)
537: mt->fl |= MANT_LITERAL;
538:
1.185 schwarze 539: return 0;
1.171 schwarze 540: case ROFFT_BODY:
1.203 schwarze 541: p->tcol->offset = mt->offset + len;
542: p->tcol->rmargin = p->maxrmargin;
1.23 kristaps 543: break;
1.22 kristaps 544: default:
545: break;
1.18 kristaps 546: }
547:
1.185 schwarze 548: return 1;
1.22 kristaps 549: }
1.18 kristaps 550:
1.22 kristaps 551: static void
552: post_IP(DECL_ARGS)
553: {
1.4 kristaps 554:
1.22 kristaps 555: switch (n->type) {
1.171 schwarze 556: case ROFFT_HEAD:
1.22 kristaps 557: term_flushln(p);
558: p->flags &= ~TERMP_NOBREAK;
1.139 schwarze 559: p->trailspace = 0;
1.203 schwarze 560: p->tcol->rmargin = p->maxrmargin;
1.22 kristaps 561: break;
1.171 schwarze 562: case ROFFT_BODY:
1.94 schwarze 563: term_newln(p);
1.203 schwarze 564: p->tcol->offset = mt->offset;
1.22 kristaps 565: break;
566: default:
567: break;
568: }
1.4 kristaps 569: }
570:
571: static int
1.1 kristaps 572: pre_TP(DECL_ARGS)
573: {
1.163 schwarze 574: struct roffsu su;
1.172 schwarze 575: struct roff_node *nn;
1.163 schwarze 576: int len, savelit;
1.1 kristaps 577:
1.21 kristaps 578: switch (n->type) {
1.171 schwarze 579: case ROFFT_HEAD:
1.184 schwarze 580: p->flags |= TERMP_NOBREAK | TERMP_BRTRSP;
1.139 schwarze 581: p->trailspace = 1;
1.21 kristaps 582: break;
1.171 schwarze 583: case ROFFT_BODY:
1.21 kristaps 584: p->flags |= TERMP_NOSPACE;
1.23 kristaps 585: break;
1.171 schwarze 586: case ROFFT_BLOCK:
1.134 schwarze 587: print_bvspace(p, n, mt->pardist);
1.23 kristaps 588: /* FALLTHROUGH */
589: default:
1.185 schwarze 590: return 1;
1.23 kristaps 591: }
592:
593: /* Calculate offset. */
594:
1.163 schwarze 595: if ((nn = n->parent->head->child) != NULL &&
1.188 schwarze 596: nn->string != NULL && ! (NODE_LINE & nn->flags) &&
1.204 schwarze 597: a2roffsu(nn->string, &su, SCALE_EN) != NULL) {
1.205 schwarze 598: len = term_hen(p, &su);
1.163 schwarze 599: if (len < 0 && (size_t)(-len) > mt->offset)
600: len = -mt->offset;
1.164 schwarze 601: else if (len > SHRT_MAX)
602: len = term_len(p, p->defindent);
603: mt->lmargin[mt->lmargincur] = len;
1.163 schwarze 604: } else
605: len = mt->lmargin[mt->lmargincur];
1.23 kristaps 606:
607: switch (n->type) {
1.171 schwarze 608: case ROFFT_HEAD:
1.203 schwarze 609: p->tcol->offset = mt->offset;
610: p->tcol->rmargin = mt->offset + len;
1.23 kristaps 611:
1.94 schwarze 612: savelit = MANT_LITERAL & mt->fl;
613: mt->fl &= ~MANT_LITERAL;
614:
1.23 kristaps 615: /* Don't print same-line elements. */
1.141 schwarze 616: nn = n->child;
1.188 schwarze 617: while (NULL != nn && 0 == (NODE_LINE & nn->flags))
1.141 schwarze 618: nn = nn->next;
619:
620: while (NULL != nn) {
621: print_man_node(p, mt, nn, meta);
622: nn = nn->next;
623: }
1.24 kristaps 624:
1.94 schwarze 625: if (savelit)
626: mt->fl |= MANT_LITERAL;
1.185 schwarze 627: return 0;
1.171 schwarze 628: case ROFFT_BODY:
1.203 schwarze 629: p->tcol->offset = mt->offset + len;
630: p->tcol->rmargin = p->maxrmargin;
1.139 schwarze 631: p->trailspace = 0;
1.184 schwarze 632: p->flags &= ~(TERMP_NOBREAK | TERMP_BRTRSP);
1.21 kristaps 633: break;
634: default:
635: break;
636: }
1.16 kristaps 637:
1.185 schwarze 638: return 1;
1.21 kristaps 639: }
1.1 kristaps 640:
1.21 kristaps 641: static void
642: post_TP(DECL_ARGS)
643: {
1.1 kristaps 644:
1.21 kristaps 645: switch (n->type) {
1.171 schwarze 646: case ROFFT_HEAD:
1.21 kristaps 647: term_flushln(p);
648: break;
1.171 schwarze 649: case ROFFT_BODY:
1.94 schwarze 650: term_newln(p);
1.203 schwarze 651: p->tcol->offset = mt->offset;
1.21 kristaps 652: break;
653: default:
654: break;
655: }
1.1 kristaps 656: }
657:
658: static int
659: pre_SS(DECL_ARGS)
660: {
1.134 schwarze 661: int i;
1.1 kristaps 662:
1.19 kristaps 663: switch (n->type) {
1.171 schwarze 664: case ROFFT_BLOCK:
1.113 kristaps 665: mt->fl &= ~MANT_LITERAL;
1.122 schwarze 666: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
667: mt->offset = term_len(p, p->defindent);
1.158 schwarze 668:
669: /*
670: * No vertical space before the first subsection
671: * and after an empty subsection.
672: */
673:
674: do {
675: n = n->prev;
1.209 schwarze 676: } while (n != NULL && n->tok >= MAN_TH &&
1.170 schwarze 677: termacts[n->tok].flags & MAN_NOTEXT);
1.210 ! schwarze 678: if (n == NULL || n->type == ROFFT_COMMENT ||
! 679: (n->tok == MAN_SS && n->body->child == NULL))
1.24 kristaps 680: break;
1.158 schwarze 681:
1.134 schwarze 682: for (i = 0; i < mt->pardist; i++)
683: term_vspace(p);
1.19 kristaps 684: break;
1.171 schwarze 685: case ROFFT_HEAD:
1.52 kristaps 686: term_fontrepl(p, TERMFONT_BOLD);
1.203 schwarze 687: p->tcol->offset = term_len(p, 3);
688: p->tcol->rmargin = mt->offset;
1.176 schwarze 689: p->trailspace = mt->offset;
690: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19 kristaps 691: break;
1.171 schwarze 692: case ROFFT_BODY:
1.203 schwarze 693: p->tcol->offset = mt->offset;
694: p->tcol->rmargin = p->maxrmargin;
1.176 schwarze 695: p->trailspace = 0;
696: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.24 kristaps 697: break;
1.19 kristaps 698: default:
699: break;
700: }
701:
1.185 schwarze 702: return 1;
1.1 kristaps 703: }
704:
705: static void
706: post_SS(DECL_ARGS)
707: {
1.146 schwarze 708:
1.19 kristaps 709: switch (n->type) {
1.171 schwarze 710: case ROFFT_HEAD:
1.19 kristaps 711: term_newln(p);
712: break;
1.171 schwarze 713: case ROFFT_BODY:
1.24 kristaps 714: term_newln(p);
715: break;
1.19 kristaps 716: default:
717: break;
718: }
1.1 kristaps 719: }
720:
721: static int
722: pre_SH(DECL_ARGS)
723: {
1.134 schwarze 724: int i;
1.22 kristaps 725:
1.19 kristaps 726: switch (n->type) {
1.171 schwarze 727: case ROFFT_BLOCK:
1.113 kristaps 728: mt->fl &= ~MANT_LITERAL;
1.122 schwarze 729: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
730: mt->offset = term_len(p, p->defindent);
1.158 schwarze 731:
732: /*
733: * No vertical space before the first section
734: * and after an empty section.
735: */
736:
737: do {
738: n = n->prev;
1.209 schwarze 739: } while (n != NULL && n->tok >= MAN_TH &&
1.190 schwarze 740: termacts[n->tok].flags & MAN_NOTEXT);
1.210 ! schwarze 741: if (n == NULL || n->type == ROFFT_COMMENT ||
! 742: (n->tok == MAN_SH && n->body->child == NULL))
1.61 kristaps 743: break;
1.158 schwarze 744:
1.134 schwarze 745: for (i = 0; i < mt->pardist; i++)
746: term_vspace(p);
1.19 kristaps 747: break;
1.171 schwarze 748: case ROFFT_HEAD:
1.52 kristaps 749: term_fontrepl(p, TERMFONT_BOLD);
1.203 schwarze 750: p->tcol->offset = 0;
751: p->tcol->rmargin = mt->offset;
1.176 schwarze 752: p->trailspace = mt->offset;
753: p->flags |= TERMP_NOBREAK | TERMP_BRIND;
1.19 kristaps 754: break;
1.171 schwarze 755: case ROFFT_BODY:
1.203 schwarze 756: p->tcol->offset = mt->offset;
757: p->tcol->rmargin = p->maxrmargin;
1.176 schwarze 758: p->trailspace = 0;
759: p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
1.19 kristaps 760: break;
761: default:
762: break;
763: }
1.1 kristaps 764:
1.185 schwarze 765: return 1;
1.1 kristaps 766: }
767:
768: static void
769: post_SH(DECL_ARGS)
770: {
1.146 schwarze 771:
1.19 kristaps 772: switch (n->type) {
1.171 schwarze 773: case ROFFT_HEAD:
1.19 kristaps 774: term_newln(p);
775: break;
1.171 schwarze 776: case ROFFT_BODY:
1.19 kristaps 777: term_newln(p);
778: break;
779: default:
780: break;
781: }
1.1 kristaps 782: }
783:
1.26 kristaps 784: static int
785: pre_RS(DECL_ARGS)
786: {
1.163 schwarze 787: struct roffsu su;
1.26 kristaps 788:
789: switch (n->type) {
1.171 schwarze 790: case ROFFT_BLOCK:
1.26 kristaps 791: term_newln(p);
1.185 schwarze 792: return 1;
1.171 schwarze 793: case ROFFT_HEAD:
1.185 schwarze 794: return 0;
1.26 kristaps 795: default:
796: break;
797: }
798:
1.165 schwarze 799: n = n->parent->head;
800: n->aux = SHRT_MAX + 1;
1.177 schwarze 801: if (n->child == NULL)
802: n->aux = mt->lmargin[mt->lmargincur];
1.204 schwarze 803: else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL)
1.205 schwarze 804: n->aux = term_hen(p, &su);
1.165 schwarze 805: if (n->aux < 0 && (size_t)(-n->aux) > mt->offset)
806: n->aux = -mt->offset;
807: else if (n->aux > SHRT_MAX)
808: n->aux = term_len(p, p->defindent);
1.26 kristaps 809:
1.165 schwarze 810: mt->offset += n->aux;
1.203 schwarze 811: p->tcol->offset = mt->offset;
812: p->tcol->rmargin = p->maxrmargin;
1.26 kristaps 813:
1.116 schwarze 814: if (++mt->lmarginsz < MAXMARGINS)
815: mt->lmargincur = mt->lmarginsz;
816:
1.178 schwarze 817: mt->lmargin[mt->lmargincur] = term_len(p, p->defindent);
1.185 schwarze 818: return 1;
1.26 kristaps 819: }
820:
821: static void
822: post_RS(DECL_ARGS)
823: {
824:
825: switch (n->type) {
1.171 schwarze 826: case ROFFT_BLOCK:
1.110 kristaps 827: return;
1.171 schwarze 828: case ROFFT_HEAD:
1.110 kristaps 829: return;
1.26 kristaps 830: default:
831: term_newln(p);
832: break;
833: }
1.110 kristaps 834:
1.165 schwarze 835: mt->offset -= n->parent->head->aux;
1.203 schwarze 836: p->tcol->offset = mt->offset;
1.116 schwarze 837:
838: if (--mt->lmarginsz < MAXMARGINS)
839: mt->lmargincur = mt->lmarginsz;
1.137 schwarze 840: }
841:
842: static int
843: pre_UR(DECL_ARGS)
844: {
845:
1.185 schwarze 846: return n->type != ROFFT_HEAD;
1.137 schwarze 847: }
848:
849: static void
850: post_UR(DECL_ARGS)
851: {
852:
1.171 schwarze 853: if (n->type != ROFFT_BLOCK)
1.137 schwarze 854: return;
855:
856: term_word(p, "<");
857: p->flags |= TERMP_NOSPACE;
858:
859: if (NULL != n->child->child)
860: print_man_node(p, mt, n->child->child, meta);
861:
862: p->flags |= TERMP_NOSPACE;
863: term_word(p, ">");
1.26 kristaps 864: }
865:
1.1 kristaps 866: static void
1.45 kristaps 867: print_man_node(DECL_ARGS)
1.1 kristaps 868: {
1.54 kristaps 869: int c;
1.1 kristaps 870:
871: switch (n->type) {
1.171 schwarze 872: case ROFFT_TEXT:
1.97 kristaps 873: /*
874: * If we have a blank line, output a vertical space.
875: * If we have a space as the first character, break
876: * before printing the line's data.
877: */
1.200 schwarze 878: if (*n->string == '\0') {
1.207 schwarze 879: if (p->flags & TERMP_NONEWLINE)
880: term_newln(p);
881: else
882: term_vspace(p);
1.98 schwarze 883: return;
1.200 schwarze 884: } else if (*n->string == ' ' && n->flags & NODE_LINE &&
885: (p->flags & TERMP_NONEWLINE) == 0)
1.96 kristaps 886: term_newln(p);
1.54 kristaps 887:
1.4 kristaps 888: term_word(p, n->string);
1.130 schwarze 889: goto out;
1.210 ! schwarze 890: case ROFFT_COMMENT:
! 891: return;
1.171 schwarze 892: case ROFFT_EQN:
1.188 schwarze 893: if ( ! (n->flags & NODE_LINE))
1.152 schwarze 894: p->flags |= TERMP_NOSPACE;
1.115 kristaps 895: term_eqn(p, n->eqn);
1.188 schwarze 896: if (n->next != NULL && ! (n->next->flags & NODE_LINE))
1.153 schwarze 897: p->flags |= TERMP_NOSPACE;
1.97 kristaps 898: return;
1.171 schwarze 899: case ROFFT_TBL:
1.169 schwarze 900: if (p->tbl.cols == NULL)
901: term_vspace(p);
1.92 kristaps 902: term_tbl(p, n->span);
1.97 kristaps 903: return;
1.1 kristaps 904: default:
905: break;
906: }
907:
1.193 schwarze 908: if (n->tok < ROFF_MAX) {
1.194 schwarze 909: roff_term_pre(p, n);
1.193 schwarze 910: return;
911: }
912:
913: assert(n->tok >= MAN_TH && n->tok <= MAN_MAX);
1.97 kristaps 914: if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
915: term_fontrepl(p, TERMFONT_NONE);
916:
917: c = 1;
918: if (termacts[n->tok].pre)
1.135 schwarze 919: c = (*termacts[n->tok].pre)(p, mt, n, meta);
1.97 kristaps 920:
1.1 kristaps 921: if (c && n->child)
1.135 schwarze 922: print_man_nodelist(p, mt, n->child, meta);
1.1 kristaps 923:
1.97 kristaps 924: if (termacts[n->tok].post)
1.135 schwarze 925: (*termacts[n->tok].post)(p, mt, n, meta);
1.97 kristaps 926: if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
927: term_fontrepl(p, TERMFONT_NONE);
1.63 kristaps 928:
1.130 schwarze 929: out:
930: /*
931: * If we're in a literal context, make sure that words
932: * together on the same line stay together. This is a
933: * POST-printing call, so we check the NEXT word. Since
934: * -man doesn't have nested macros, we don't need to be
935: * more specific than this.
936: */
1.157 schwarze 937: if (mt->fl & MANT_LITERAL &&
938: ! (p->flags & (TERMP_NOBREAK | TERMP_NONEWLINE)) &&
1.188 schwarze 939: (n->next == NULL || n->next->flags & NODE_LINE)) {
1.202 schwarze 940: p->flags |= TERMP_BRNEVER | TERMP_NOSPACE;
1.157 schwarze 941: if (n->string != NULL && *n->string != '\0')
1.130 schwarze 942: term_flushln(p);
943: else
944: term_newln(p);
1.202 schwarze 945: p->flags &= ~TERMP_BRNEVER;
1.203 schwarze 946: if (p->tcol->rmargin < p->maxrmargin &&
947: n->parent->tok == MAN_HP) {
948: p->tcol->offset = p->tcol->rmargin;
949: p->tcol->rmargin = p->maxrmargin;
1.202 schwarze 950: }
1.130 schwarze 951: }
1.188 schwarze 952: if (NODE_EOS & n->flags)
1.63 kristaps 953: p->flags |= TERMP_SENTENCE;
1.1 kristaps 954: }
955:
956:
957: static void
1.52 kristaps 958: print_man_nodelist(DECL_ARGS)
1.1 kristaps 959: {
1.19 kristaps 960:
1.168 schwarze 961: while (n != NULL) {
962: print_man_node(p, mt, n, meta);
963: n = n->next;
964: }
1.1 kristaps 965: }
966:
1.31 kristaps 967: static void
1.173 schwarze 968: print_man_foot(struct termp *p, const struct roff_meta *meta)
1.1 kristaps 969: {
1.147 schwarze 970: char *title;
1.156 schwarze 971: size_t datelen, titlen;
1.73 kristaps 972:
1.125 schwarze 973: assert(meta->title);
974: assert(meta->msec);
975: assert(meta->date);
1.1 kristaps 976:
1.52 kristaps 977: term_fontrepl(p, TERMFONT_NONE);
1.50 kristaps 978:
1.149 schwarze 979: if (meta->hasbody)
980: term_vspace(p);
1.126 schwarze 981:
982: /*
983: * Temporary, undocumented option to imitate mdoc(7) output.
1.173 schwarze 984: * In the bottom right corner, use the operating system
985: * instead of the title.
1.126 schwarze 986: */
987:
988: if ( ! p->mdocstyle) {
1.149 schwarze 989: if (meta->hasbody) {
990: term_vspace(p);
991: term_vspace(p);
992: }
1.147 schwarze 993: mandoc_asprintf(&title, "%s(%s)",
994: meta->title, meta->msec);
1.173 schwarze 995: } else if (meta->os) {
996: title = mandoc_strdup(meta->os);
1.126 schwarze 997: } else {
1.147 schwarze 998: title = mandoc_strdup("");
1.126 schwarze 999: }
1.125 schwarze 1000: datelen = term_strlen(p, meta->date);
1.1 kristaps 1001:
1.173 schwarze 1002: /* Bottom left corner: operating system. */
1.126 schwarze 1003:
1.1 kristaps 1004: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
1.139 schwarze 1005: p->trailspace = 1;
1.203 schwarze 1006: p->tcol->offset = 0;
1007: p->tcol->rmargin = p->maxrmargin > datelen ?
1.156 schwarze 1008: (p->maxrmargin + term_len(p, 1) - datelen) / 2 : 0;
1.1 kristaps 1009:
1.173 schwarze 1010: if (meta->os)
1011: term_word(p, meta->os);
1.1 kristaps 1012: term_flushln(p);
1013:
1.126 schwarze 1014: /* At the bottom in the middle: manual date. */
1015:
1.203 schwarze 1016: p->tcol->offset = p->tcol->rmargin;
1.156 schwarze 1017: titlen = term_strlen(p, title);
1.203 schwarze 1018: p->tcol->rmargin = p->maxrmargin > titlen ?
1019: p->maxrmargin - titlen : 0;
1.117 schwarze 1020: p->flags |= TERMP_NOSPACE;
1.123 schwarze 1021:
1.125 schwarze 1022: term_word(p, meta->date);
1.123 schwarze 1023: term_flushln(p);
1024:
1.126 schwarze 1025: /* Bottom right corner: manual title and section. */
1026:
1.123 schwarze 1027: p->flags &= ~TERMP_NOBREAK;
1028: p->flags |= TERMP_NOSPACE;
1.139 schwarze 1029: p->trailspace = 0;
1.203 schwarze 1030: p->tcol->offset = p->tcol->rmargin;
1031: p->tcol->rmargin = p->maxrmargin;
1.1 kristaps 1032:
1.123 schwarze 1033: term_word(p, title);
1.1 kristaps 1034: term_flushln(p);
1.147 schwarze 1035: free(title);
1.1 kristaps 1036: }
1037:
1.31 kristaps 1038: static void
1.173 schwarze 1039: print_man_head(struct termp *p, const struct roff_meta *meta)
1.1 kristaps 1040: {
1.148 schwarze 1041: const char *volume;
1.147 schwarze 1042: char *title;
1.148 schwarze 1043: size_t vollen, titlen;
1.73 kristaps 1044:
1.135 schwarze 1045: assert(meta->title);
1046: assert(meta->msec);
1.1 kristaps 1047:
1.148 schwarze 1048: volume = NULL == meta->vol ? "" : meta->vol;
1049: vollen = term_strlen(p, volume);
1.1 kristaps 1050:
1.126 schwarze 1051: /* Top left corner: manual title and section. */
1052:
1.147 schwarze 1053: mandoc_asprintf(&title, "%s(%s)", meta->title, meta->msec);
1.77 kristaps 1054: titlen = term_strlen(p, title);
1.1 kristaps 1055:
1.118 schwarze 1056: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1.139 schwarze 1057: p->trailspace = 1;
1.203 schwarze 1058: p->tcol->offset = 0;
1059: p->tcol->rmargin = 2 * (titlen+1) + vollen < p->maxrmargin ?
1.148 schwarze 1060: (p->maxrmargin - vollen + term_len(p, 1)) / 2 :
1.156 schwarze 1061: vollen < p->maxrmargin ? p->maxrmargin - vollen : 0;
1.1 kristaps 1062:
1063: term_word(p, title);
1064: term_flushln(p);
1065:
1.126 schwarze 1066: /* At the top in the middle: manual volume. */
1067:
1.117 schwarze 1068: p->flags |= TERMP_NOSPACE;
1.203 schwarze 1069: p->tcol->offset = p->tcol->rmargin;
1070: p->tcol->rmargin = p->tcol->offset + vollen + titlen <
1071: p->maxrmargin ? p->maxrmargin - titlen : p->maxrmargin;
1.1 kristaps 1072:
1.148 schwarze 1073: term_word(p, volume);
1.1 kristaps 1074: term_flushln(p);
1075:
1.126 schwarze 1076: /* Top right corner: title and section, again. */
1077:
1.1 kristaps 1078: p->flags &= ~TERMP_NOBREAK;
1.139 schwarze 1079: p->trailspace = 0;
1.203 schwarze 1080: if (p->tcol->rmargin + titlen <= p->maxrmargin) {
1.117 schwarze 1081: p->flags |= TERMP_NOSPACE;
1.203 schwarze 1082: p->tcol->offset = p->tcol->rmargin;
1083: p->tcol->rmargin = p->maxrmargin;
1.57 kristaps 1084: term_word(p, title);
1085: term_flushln(p);
1086: }
1.1 kristaps 1087:
1.118 schwarze 1088: p->flags &= ~TERMP_NOSPACE;
1.203 schwarze 1089: p->tcol->offset = 0;
1090: p->tcol->rmargin = p->maxrmargin;
1.61 kristaps 1091:
1.146 schwarze 1092: /*
1.126 schwarze 1093: * Groff prints three blank lines before the content.
1094: * Do the same, except in the temporary, undocumented
1095: * mode imitating mdoc(7) output.
1.62 kristaps 1096: */
1097:
1.61 kristaps 1098: term_vspace(p);
1.126 schwarze 1099: if ( ! p->mdocstyle) {
1100: term_vspace(p);
1101: term_vspace(p);
1102: }
1.147 schwarze 1103: free(title);
1.1 kristaps 1104: }
CVSweb