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