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