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