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