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