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